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_VALGRIND_MEMCHECK_H
18 #include <valgrind/memcheck.h>
21 #include <mono/metadata/assembly.h>
22 #include <mono/metadata/loader.h>
23 #include <mono/metadata/cil-coff.h>
24 #include <mono/metadata/tabledefs.h>
25 #include <mono/metadata/class.h>
26 #include <mono/metadata/object.h>
27 #include <mono/metadata/exception.h>
28 #include <mono/metadata/opcodes.h>
29 #include <mono/metadata/mono-endian.h>
30 #include <mono/metadata/tokentype.h>
31 #include <mono/metadata/tabledefs.h>
32 #include <mono/metadata/threads.h>
33 #include <mono/metadata/marshal.h>
34 #include <mono/metadata/socket-io.h>
35 #include <mono/metadata/appdomain.h>
36 #include <mono/metadata/debug-helpers.h>
37 #include <mono/io-layer/io-layer.h>
38 #include "mono/metadata/profiler.h"
39 #include <mono/metadata/profiler-private.h>
40 #include <mono/metadata/mono-config.h>
41 #include <mono/metadata/environment.h>
42 #include <mono/metadata/mono-debug.h>
43 #include <mono/metadata/monitor.h>
44 #include <mono/metadata/security-manager.h>
45 #include <mono/metadata/threads-types.h>
46 #include <mono/metadata/rawbuffer.h>
47 #include <mono/utils/mono-math.h>
48 #include <mono/utils/mono-compiler.h>
49 #include <mono/utils/mono-counters.h>
50 #include <mono/utils/mono-logger.h>
51 #include <mono/os/gc_wrapper.h>
59 #include "jit-icalls.h"
63 #define BRANCH_COST 100
64 #define INLINE_LENGTH_LIMIT 20
65 #define INLINE_FAILURE do {\
66 if ((cfg->method != method) && (method->wrapper_type == MONO_WRAPPER_NONE))\
71 * this is used to determine when some branch optimizations are possible: we exclude FP compares
72 * because they have weird semantics with NaNs.
74 #define MONO_IS_COND_BRANCH_OP(ins) (((ins)->opcode >= CEE_BEQ && (ins)->opcode <= CEE_BLT_UN) || ((ins)->opcode >= OP_LBEQ && (ins)->opcode <= OP_LBLT_UN) || ((ins)->opcode >= OP_FBEQ && (ins)->opcode <= OP_FBLT_UN) || ((ins)->opcode >= OP_IBEQ && (ins)->opcode <= OP_IBLT_UN))
75 #define MONO_IS_COND_BRANCH_NOFP(ins) (MONO_IS_COND_BRANCH_OP(ins) && (ins)->inst_left->inst_left->type != STACK_R8)
77 #define MONO_IS_BRANCH_OP(ins) (MONO_IS_COND_BRANCH_OP(ins) || ((ins)->opcode == CEE_BR) || ((ins)->opcode == OP_BR_REG) || ((ins)->opcode == CEE_SWITCH))
79 #define MONO_CHECK_THIS(ins) (mono_method_signature (cfg->method)->hasthis && (ins)->ssa_op == MONO_SSA_LOAD && (ins)->inst_left->inst_c0 == 0)
81 static void setup_stat_profiler (void);
82 gboolean mono_arch_print_tree(MonoInst *tree, int arity);
83 static gpointer mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt);
84 static gpointer mono_jit_compile_method (MonoMethod *method);
85 static gpointer mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method);
86 static gpointer mono_create_jit_trampoline_in_domain (MonoDomain *domain, MonoMethod *method);
88 static void handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src,
89 const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native, gboolean write_barrier);
91 static void dec_foreach (MonoInst *tree, MonoCompile *cfg);
93 static int mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
94 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
95 guint inline_offset, gboolean is_virtual_call);
97 /* helper methods signature */
98 static MonoMethodSignature *helper_sig_class_init_trampoline = NULL;
99 static MonoMethodSignature *helper_sig_domain_get = NULL;
101 static guint32 default_opt = 0;
102 static gboolean default_opt_set = FALSE;
104 guint32 mono_jit_tls_id = -1;
105 MonoTraceSpec *mono_jit_trace_calls = NULL;
106 gboolean mono_break_on_exc = FALSE;
108 gboolean mono_compile_aot = FALSE;
110 gboolean mono_use_security_manager = FALSE;
112 static int mini_verbose = 0;
114 #define mono_jit_lock() EnterCriticalSection (&jit_mutex)
115 #define mono_jit_unlock() LeaveCriticalSection (&jit_mutex)
116 static CRITICAL_SECTION jit_mutex;
118 static GHashTable *class_init_hash_addr = NULL;
120 static MonoCodeManager *global_codeman = NULL;
122 static GHashTable *jit_icall_name_hash = NULL;
124 static MonoDebugOptions debug_options;
127 * Address of the trampoline code. This is used by the debugger to check
128 * whether a method is a trampoline.
130 guint8* mono_trampoline_code [MONO_TRAMPOLINE_NUM];
133 mono_running_on_valgrind (void)
135 #ifdef HAVE_VALGRIND_MEMCHECK_H
136 if (RUNNING_ON_VALGRIND)
146 * mono_create_ftnptr:
148 * Given a function address, create a function descriptor for it.
149 * This is only needed on IA64.
152 mono_create_ftnptr (MonoDomain *domain, gpointer addr)
157 mono_domain_lock (domain);
158 desc = mono_code_manager_reserve (domain->code_mp, 2 * sizeof (gpointer));
159 mono_domain_unlock (domain);
176 find_tramp (gpointer key, gpointer value, gpointer user_data)
178 FindTrampUserData *ud = (FindTrampUserData*)user_data;
181 ud->method = (MonoMethod*)key;
185 G_GNUC_UNUSED static char*
186 get_method_from_ip (void *ip)
191 MonoDomain *domain = mono_domain_get ();
192 MonoDebugSourceLocation *location;
193 FindTrampUserData user_data;
195 ji = mono_jit_info_table_find (domain, ip);
198 user_data.method = NULL;
199 mono_domain_lock (domain);
200 g_hash_table_foreach (domain->jit_trampoline_hash, find_tramp, &user_data);
201 mono_domain_unlock (domain);
202 if (user_data.method) {
203 char *mname = mono_method_full_name (user_data.method, TRUE);
204 res = g_strdup_printf ("<%p - JIT trampoline for %s>", ip, mname);
211 method = mono_method_full_name (ji->method, TRUE);
212 /* FIXME: unused ? */
213 location = mono_debug_lookup_source_location (ji->method, (guint32)((guint8*)ip - (guint8*)ji->code_start), domain);
215 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);
217 mono_debug_free_source_location (location);
226 return get_method_from_ip (ip);
231 mono_print_method_from_ip (void *ip)
235 MonoDebugSourceLocation *source;
236 MonoDomain *domain = mono_domain_get ();
237 FindTrampUserData user_data;
239 ji = mono_jit_info_table_find (domain, ip);
242 user_data.method = NULL;
243 mono_domain_lock (domain);
244 g_hash_table_foreach (domain->jit_trampoline_hash, find_tramp, &user_data);
245 mono_domain_unlock (domain);
246 if (user_data.method) {
247 char *mname = mono_method_full_name (user_data.method, TRUE);
248 printf ("IP %p is a JIT trampoline for %s\n", ip, mname);
252 g_print ("No method at %p\n", ip);
255 method = mono_method_full_name (ji->method, TRUE);
256 source = mono_debug_lookup_source_location (ji->method, (guint32)((guint8*)ip - (guint8*)ji->code_start), domain);
258 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);
261 g_print ("%s:%d\n", source->source_file, source->row);
263 mono_debug_free_source_location (source);
268 * mono_method_same_domain:
270 * Determine whenever two compiled methods are in the same domain, thus
271 * the address of the callee can be embedded in the caller.
273 gboolean mono_method_same_domain (MonoJitInfo *caller, MonoJitInfo *callee)
275 if (!caller || !callee)
279 * If the call was made from domain-neutral to domain-specific
280 * code, we can't patch the call site.
282 if (caller->domain_neutral && !callee->domain_neutral)
285 if ((caller->method->klass == mono_defaults.appdomain_class) &&
286 (strstr (caller->method->name, "InvokeInDomain"))) {
287 /* The InvokeInDomain methods change the current appdomain */
295 * mono_global_codeman_reserve:
297 * Allocate code memory from the global code manager.
299 void *mono_global_codeman_reserve (int size)
303 if (!global_codeman) {
304 /* This can happen during startup */
305 global_codeman = mono_code_manager_new ();
306 return mono_code_manager_reserve (global_codeman, size);
310 ptr = mono_code_manager_reserve (global_codeman, size);
317 mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token)
319 MonoJumpInfoToken *res = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoToken));
326 #define MONO_INIT_VARINFO(vi,id) do { \
327 (vi)->range.first_use.pos.bid = 0xffff; \
332 //#define UNVERIFIED do { G_BREAKPOINT (); goto unverified; } while (0)
333 #define UNVERIFIED do { goto unverified; } while (0)
336 * Basic blocks have two numeric identifiers:
337 * dfn: Depth First Number
338 * block_num: unique ID assigned at bblock creation
340 #define NEW_BBLOCK(cfg) (mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)))
341 #define ADD_BBLOCK(cfg,bbhash,b) do { \
342 g_hash_table_insert (bbhash, (b)->cil_code, (b)); \
343 (b)->block_num = cfg->num_bblocks++; \
344 (b)->real_offset = real_offset; \
347 #define GET_BBLOCK(cfg,bbhash,tblock,ip) do { \
348 (tblock) = g_hash_table_lookup (bbhash, (ip)); \
350 if ((ip) >= end || (ip) < header->code) UNVERIFIED; \
351 (tblock) = NEW_BBLOCK (cfg); \
352 (tblock)->cil_code = (ip); \
353 ADD_BBLOCK (cfg, (bbhash), (tblock)); \
357 #define CHECK_BBLOCK(target,ip,tblock) do { \
358 if ((target) < (ip) && !(tblock)->code) { \
359 bb_recheck = g_list_prepend (bb_recheck, (tblock)); \
360 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)); \
364 #define NEW_ICONST(cfg,dest,val) do { \
365 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
366 (dest)->opcode = OP_ICONST; \
367 (dest)->inst_c0 = (val); \
368 (dest)->type = STACK_I4; \
371 #define NEW_PCONST(cfg,dest,val) do { \
372 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
373 (dest)->opcode = OP_PCONST; \
374 (dest)->inst_p0 = (val); \
375 (dest)->type = STACK_PTR; \
379 #ifdef MONO_ARCH_NEED_GOT_VAR
381 #define NEW_PATCH_INFO(cfg,dest,el1,el2) do { \
382 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
383 (dest)->opcode = OP_PATCH_INFO; \
384 (dest)->inst_left = (gpointer)(el1); \
385 (dest)->inst_right = (gpointer)(el2); \
388 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do { \
389 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
390 (dest)->opcode = cfg->compile_aot ? OP_GOT_ENTRY : OP_PCONST; \
391 if (cfg->compile_aot) { \
392 MonoInst *group, *got_var, *got_loc; \
393 got_loc = mono_get_got_var (cfg); \
394 NEW_TEMPLOAD ((cfg), got_var, got_loc->inst_c0); \
395 NEW_PATCH_INFO ((cfg), group, cons, patch_type); \
396 (dest)->inst_p0 = got_var; \
397 (dest)->inst_p1 = group; \
399 (dest)->inst_p0 = (cons); \
400 (dest)->inst_i1 = (gpointer)(patch_type); \
402 (dest)->type = STACK_PTR; \
405 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,stack_type,stack_class) do { \
406 MonoInst *group, *got_var, *got_loc; \
407 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
408 (dest)->opcode = OP_GOT_ENTRY; \
409 got_loc = mono_get_got_var (cfg); \
410 NEW_TEMPLOAD ((cfg), got_var, got_loc->inst_c0); \
411 NEW_PATCH_INFO ((cfg), group, NULL, patch_type); \
412 group->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
413 (dest)->inst_p0 = got_var; \
414 (dest)->inst_p1 = group; \
415 (dest)->type = (stack_type); \
416 (dest)->klass = (stack_class); \
421 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do { \
422 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
423 (dest)->opcode = cfg->compile_aot ? OP_AOTCONST : OP_PCONST; \
424 (dest)->inst_p0 = (cons); \
425 (dest)->inst_i1 = (gpointer)(patch_type); \
426 (dest)->type = STACK_PTR; \
429 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,stack_type,stack_class) do { \
430 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
431 (dest)->opcode = OP_AOTCONST; \
432 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
433 (dest)->inst_p1 = (gpointer)(patch_type); \
434 (dest)->type = (stack_type); \
435 (dest)->klass = (stack_class); \
440 #define NEW_CLASSCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_CLASS, (val))
442 #define NEW_IMAGECONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_IMAGE, (val))
444 #define NEW_FIELDCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_FIELD, (val))
446 #define NEW_METHODCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHODCONST, (val))
448 #define NEW_VTABLECONST(cfg,dest,vtable) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_VTABLE, cfg->compile_aot ? (gpointer)((vtable)->klass) : (vtable))
450 #define NEW_SFLDACONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_SFLDA, (val))
452 #define NEW_LDSTRCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDSTR, (image), (token), STACK_OBJ, mono_defaults.string_class)
454 #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)
456 #define NEW_LDTOKENCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDTOKEN, (image), (token), STACK_PTR, NULL)
458 #define NEW_DECLSECCONST(cfg,dest,image,entry) do { \
459 if (cfg->compile_aot) { \
460 NEW_AOTCONST_TOKEN (cfg, dest, MONO_PATCH_INFO_DECLSEC, image, (entry).index, STACK_OBJ, NULL); \
462 NEW_PCONST (cfg, args [0], (entry).blob); \
466 #define NEW_DOMAINCONST(cfg,dest) do { \
467 if (cfg->opt & MONO_OPT_SHARED) { \
468 /* avoid depending on undefined C behavior in sequence points */ \
469 MonoInst* __domain_var = mono_get_domainvar (cfg); \
470 NEW_TEMPLOAD (cfg, dest, __domain_var->inst_c0); \
472 NEW_PCONST (cfg, dest, (cfg)->domain); \
476 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
478 #define NEW_ARGLOAD(cfg,dest,num) do { \
479 if (arg_array [(num)]->opcode == OP_ICONST) (dest) = arg_array [(num)]; else { \
480 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
481 (dest)->ssa_op = MONO_SSA_LOAD; \
482 (dest)->inst_i0 = arg_array [(num)]; \
483 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
484 type_to_eval_stack_type (param_types [(num)], (dest)); \
485 (dest)->klass = (dest)->inst_i0->klass; \
488 #define NEW_LOCLOAD(cfg,dest,num) do { \
489 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
490 (dest)->ssa_op = MONO_SSA_LOAD; \
491 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
492 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
493 type_to_eval_stack_type (header->locals [(num)], (dest)); \
494 (dest)->klass = (dest)->inst_i0->klass; \
497 #define NEW_LOCLOADA(cfg,dest,num) do { \
498 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
499 (dest)->ssa_op = MONO_SSA_ADDRESS_TAKEN; \
500 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
501 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
502 (dest)->opcode = OP_LDADDR; \
503 (dest)->type = STACK_MP; \
504 (dest)->klass = (dest)->inst_i0->klass; \
505 if (!MONO_TYPE_ISSTRUCT (header->locals [(num)])) \
506 (cfg)->disable_ssa = TRUE; \
509 #define NEW_RETLOADA(cfg,dest) do { \
510 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
511 (dest)->ssa_op = MONO_SSA_ADDRESS_TAKEN; \
512 (dest)->inst_i0 = (cfg)->ret; \
513 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
514 (dest)->opcode = cfg->ret_var_is_local ? OP_LDADDR : CEE_LDIND_I; \
515 (dest)->type = STACK_MP; \
516 (dest)->klass = (dest)->inst_i0->klass; \
517 (cfg)->disable_ssa = TRUE; \
520 #define NEW_ARGLOADA(cfg,dest,num) do { \
521 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
522 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
523 (dest)->ssa_op = MONO_SSA_ADDRESS_TAKEN; \
524 (dest)->inst_i0 = arg_array [(num)]; \
525 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
526 (dest)->opcode = OP_LDADDR; \
527 (dest)->type = STACK_MP; \
528 (dest)->klass = (dest)->inst_i0->klass; \
529 (cfg)->disable_ssa = TRUE; \
532 #define NEW_TEMPLOAD(cfg,dest,num) do { \
533 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
534 (dest)->ssa_op = MONO_SSA_LOAD; \
535 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
536 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
537 type_to_eval_stack_type ((dest)->inst_i0->inst_vtype, (dest)); \
538 (dest)->klass = (dest)->inst_i0->klass; \
541 #define NEW_TEMPLOADA(cfg,dest,num) do { \
542 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
543 (dest)->ssa_op = MONO_SSA_ADDRESS_TAKEN; \
544 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
545 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
546 (dest)->opcode = OP_LDADDR; \
547 (dest)->type = STACK_MP; \
548 (dest)->klass = (dest)->inst_i0->klass; \
549 if (!MONO_TYPE_ISSTRUCT (cfg->varinfo [(num)]->inst_vtype)) \
550 (cfg)->disable_ssa = TRUE; \
554 #define NEW_INDLOAD(cfg,dest,addr,vtype) do { \
555 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
556 (dest)->inst_left = addr; \
557 (dest)->opcode = mono_type_to_ldind (vtype); \
558 type_to_eval_stack_type (vtype, (dest)); \
559 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
562 #define NEW_INDSTORE(cfg,dest,addr,value,vtype) do { \
563 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
564 (dest)->inst_i0 = addr; \
565 (dest)->opcode = mono_type_to_stind (vtype); \
566 (dest)->inst_i1 = (value); \
567 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
570 #define NEW_TEMPSTORE(cfg,dest,num,inst) do { \
571 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
572 (dest)->ssa_op = MONO_SSA_STORE; \
573 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
574 (dest)->opcode = mono_type_to_stind ((dest)->inst_i0->inst_vtype); \
575 (dest)->inst_i1 = (inst); \
576 (dest)->klass = (dest)->inst_i0->klass; \
579 #define NEW_LOCSTORE(cfg,dest,num,inst) do { \
580 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
581 (dest)->opcode = mono_type_to_stind (header->locals [(num)]); \
582 (dest)->ssa_op = MONO_SSA_STORE; \
583 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
584 (dest)->inst_i1 = (inst); \
585 (dest)->klass = (dest)->inst_i0->klass; \
588 #define NEW_ARGSTORE(cfg,dest,num,inst) do { \
589 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
590 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
591 (dest)->opcode = mono_type_to_stind (param_types [(num)]); \
592 (dest)->ssa_op = MONO_SSA_STORE; \
593 (dest)->inst_i0 = arg_array [(num)]; \
594 (dest)->inst_i1 = (inst); \
595 (dest)->klass = (dest)->inst_i0->klass; \
598 #define NEW_DUMMY_USE(cfg,dest,load) do { \
599 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
600 (dest)->opcode = OP_DUMMY_USE; \
601 (dest)->inst_left = (load); \
604 #define NEW_DUMMY_STORE(cfg,dest,num) do { \
605 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
606 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
607 (dest)->opcode = OP_DUMMY_STORE; \
608 (dest)->klass = (dest)->inst_i0->klass; \
611 #define ADD_BINOP(op) do { \
612 MONO_INST_NEW (cfg, ins, (op)); \
613 ins->cil_code = ip; \
615 ins->inst_i0 = sp [0]; \
616 ins->inst_i1 = sp [1]; \
618 type_from_op (ins); \
622 #define ADD_UNOP(op) do { \
623 MONO_INST_NEW (cfg, ins, (op)); \
624 ins->cil_code = ip; \
626 ins->inst_i0 = sp [0]; \
628 type_from_op (ins); \
632 #define ADD_BINCOND(next_block) do { \
635 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
636 cmp->inst_i0 = sp [0]; \
637 cmp->inst_i1 = sp [1]; \
638 cmp->cil_code = ins->cil_code; \
639 type_from_op (cmp); \
641 ins->inst_i0 = cmp; \
642 MONO_ADD_INS (bblock, ins); \
643 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
644 GET_BBLOCK (cfg, bbhash, tblock, target); \
645 link_bblock (cfg, bblock, tblock); \
646 ins->inst_true_bb = tblock; \
647 CHECK_BBLOCK (target, ip, tblock); \
648 if ((next_block)) { \
649 link_bblock (cfg, bblock, (next_block)); \
650 ins->inst_false_bb = (next_block); \
651 start_new_bblock = 1; \
653 GET_BBLOCK (cfg, bbhash, tblock, ip); \
654 link_bblock (cfg, bblock, tblock); \
655 ins->inst_false_bb = tblock; \
656 start_new_bblock = 2; \
660 /* FIXME: handle float, long ... */
661 #define ADD_UNCOND(istrue) do { \
664 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
665 cmp->inst_i0 = sp [0]; \
666 switch (cmp->inst_i0->type) { \
668 cmp->inst_i1 = zero_int64; break; \
670 cmp->inst_i1 = zero_r8; break; \
673 cmp->inst_i1 = zero_ptr; break; \
675 cmp->inst_i1 = zero_obj; break; \
677 cmp->inst_i1 = zero_int32; \
679 cmp->cil_code = ins->cil_code; \
680 type_from_op (cmp); \
682 ins->inst_i0 = cmp; \
683 ins->opcode = (istrue)? CEE_BNE_UN: CEE_BEQ; \
684 MONO_ADD_INS (bblock, ins); \
685 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
686 GET_BBLOCK (cfg, bbhash, tblock, target); \
687 link_bblock (cfg, bblock, tblock); \
688 ins->inst_true_bb = tblock; \
689 CHECK_BBLOCK (target, ip, tblock); \
690 GET_BBLOCK (cfg, bbhash, tblock, ip); \
691 link_bblock (cfg, bblock, tblock); \
692 ins->inst_false_bb = tblock; \
693 start_new_bblock = 2; \
696 #define NEW_LDELEMA(cfg,dest,sp,k) do { \
697 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
698 (dest)->opcode = CEE_LDELEMA; \
699 (dest)->inst_left = (sp) [0]; \
700 (dest)->inst_right = (sp) [1]; \
701 (dest)->type = STACK_MP; \
702 (dest)->klass = (k); \
703 (cfg)->flags |= MONO_CFG_HAS_LDELEMA; \
706 #define NEW_GROUP(cfg,dest,el1,el2) do { \
707 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
708 (dest)->opcode = OP_GROUP; \
709 (dest)->inst_left = (el1); \
710 (dest)->inst_right = (el2); \
715 compare_bblock (gconstpointer a, gconstpointer b)
717 const MonoBasicBlock *b1 = a;
718 const MonoBasicBlock *b2 = b;
720 return b2->cil_code - b1->cil_code;
725 * link_bblock: Links two basic blocks
727 * links two basic blocks in the control flow graph, the 'from'
728 * argument is the starting block and the 'to' argument is the block
729 * the control flow ends to after 'from'.
732 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
734 MonoBasicBlock **newa;
738 if (from->cil_code) {
740 g_print ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
742 g_print ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
745 g_print ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
747 g_print ("edge from entry to exit\n");
751 for (i = 0; i < from->out_count; ++i) {
752 if (to == from->out_bb [i]) {
758 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
759 for (i = 0; i < from->out_count; ++i) {
760 newa [i] = from->out_bb [i];
768 for (i = 0; i < to->in_count; ++i) {
769 if (from == to->in_bb [i]) {
775 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
776 for (i = 0; i < to->in_count; ++i) {
777 newa [i] = to->in_bb [i];
786 * mono_unlink_bblock:
788 * Unlink two basic blocks.
791 mono_unlink_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
797 for (i = 0; i < from->out_count; ++i) {
798 if (to == from->out_bb [i]) {
805 for (i = 0; i < from->out_count; ++i) {
806 if (from->out_bb [i] != to)
807 from->out_bb [pos ++] = from->out_bb [i];
809 g_assert (pos == from->out_count - 1);
814 for (i = 0; i < to->in_count; ++i) {
815 if (from == to->in_bb [i]) {
822 for (i = 0; i < to->in_count; ++i) {
823 if (to->in_bb [i] != from)
824 to->in_bb [pos ++] = to->in_bb [i];
826 g_assert (pos == to->in_count - 1);
832 * mono_find_block_region:
834 * We mark each basic block with a region ID. We use that to avoid BB
835 * optimizations when blocks are in different regions.
838 * A region token that encodes where this region is, and information
839 * about the clause owner for this block.
841 * The region encodes the try/catch/filter clause that owns this block
842 * as well as the type. -1 is a special value that represents a block
843 * that is in none of try/catch/filter.
846 mono_find_block_region (MonoCompile *cfg, int offset)
848 MonoMethod *method = cfg->method;
849 MonoMethodHeader *header = mono_method_get_header (method);
850 MonoExceptionClause *clause;
853 /* first search for handlers and filters */
854 for (i = 0; i < header->num_clauses; ++i) {
855 clause = &header->clauses [i];
856 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->data.filter_offset) &&
857 (offset < (clause->handler_offset)))
858 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
860 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
861 if (clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)
862 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
864 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
868 /* search the try blocks */
869 for (i = 0; i < header->num_clauses; ++i) {
870 clause = &header->clauses [i];
871 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
872 return ((i + 1) << 8) | clause->flags;
879 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
881 MonoMethod *method = cfg->method;
882 MonoMethodHeader *header = mono_method_get_header (method);
883 MonoExceptionClause *clause;
884 MonoBasicBlock *handler;
888 for (i = 0; i < header->num_clauses; ++i) {
889 clause = &header->clauses [i];
890 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
891 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
892 if (clause->flags == type) {
893 handler = g_hash_table_lookup (cfg->bb_hash, header->code + clause->handler_offset);
895 res = g_list_append (res, handler);
903 mono_find_spvar_for_region (MonoCompile *cfg, int region)
905 return g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
909 mono_create_spvar_for_region (MonoCompile *cfg, int region)
913 var = g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
917 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
918 /* prevent it from being register allocated */
919 var->flags |= MONO_INST_INDIRECT;
921 g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
925 mono_find_exvar_for_offset (MonoCompile *cfg, int offset)
927 return g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
931 mono_create_exvar_for_offset (MonoCompile *cfg, int offset)
935 var = g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
939 var = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
940 /* prevent it from being register allocated */
941 var->flags |= MONO_INST_INDIRECT;
943 g_hash_table_insert (cfg->exvars, GINT_TO_POINTER (offset), var);
949 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
953 array [*dfn] = start;
954 /*g_print ("visit %d at %p (BB%ld)\n", *dfn, start->cil_code, start->block_num);*/
955 for (i = 0; i < start->out_count; ++i) {
956 if (start->out_bb [i]->dfn)
959 start->out_bb [i]->dfn = *dfn;
960 start->out_bb [i]->df_parent = start;
961 array [*dfn] = start->out_bb [i];
962 df_visit (start->out_bb [i], dfn, array);
968 MonoBasicBlock *best;
972 previous_foreach (gconstpointer key, gpointer val, gpointer data)
974 PrevStruct *p = data;
975 MonoBasicBlock *bb = val;
976 //printf ("FIDPREV %d %p %p %p %p %p %d %d %d\n", bb->block_num, p->code, bb, p->best, bb->cil_code, p->best->cil_code,
977 //bb->method == p->best->method, bb->cil_code < p->code, bb->cil_code > p->best->cil_code);
979 if (bb->cil_code && bb->cil_code < p->code && bb->cil_code > p->best->cil_code)
983 static MonoBasicBlock*
984 find_previous (GHashTable *bb_hash, MonoBasicBlock *start, const guchar *code) {
990 g_hash_table_foreach (bb_hash, (GHFunc)previous_foreach, &p);
995 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
1004 * FIXME: take into account all the details:
1005 * second may have been the target of more than one bblock
1007 second->out_count = first->out_count;
1008 second->out_bb = first->out_bb;
1010 for (i = 0; i < first->out_count; ++i) {
1011 bb = first->out_bb [i];
1012 for (j = 0; j < bb->in_count; ++j) {
1013 if (bb->in_bb [j] == first)
1014 bb->in_bb [j] = second;
1018 first->out_count = 0;
1019 first->out_bb = NULL;
1020 link_bblock (cfg, first, second);
1022 second->last_ins = first->last_ins;
1024 /*g_print ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
1025 for (inst = first->code; inst && inst->next; inst = inst->next) {
1026 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
1027 g_print ("found %p: %s", inst->next->cil_code, code);
1029 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
1030 second->code = inst->next;
1032 first->last_ins = inst;
1033 second->next_bb = first->next_bb;
1034 first->next_bb = second;
1038 if (!second->code) {
1039 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
1045 reverse_branch_op (guint32 opcode)
1047 static const int reverse_map [] = {
1048 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
1049 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
1051 static const int reverse_fmap [] = {
1052 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
1053 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
1055 static const int reverse_lmap [] = {
1056 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
1057 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
1059 static const int reverse_imap [] = {
1060 OP_IBNE_UN, OP_IBLT, OP_IBLE, OP_IBGT, OP_IBGE,
1061 OP_IBEQ, OP_IBLT_UN, OP_IBLE_UN, OP_IBGT_UN, OP_IBGE_UN
1064 if (opcode >= CEE_BEQ && opcode <= CEE_BLT_UN) {
1065 opcode = reverse_map [opcode - CEE_BEQ];
1066 } else if (opcode >= OP_FBEQ && opcode <= OP_FBLT_UN) {
1067 opcode = reverse_fmap [opcode - OP_FBEQ];
1068 } else if (opcode >= OP_LBEQ && opcode <= OP_LBLT_UN) {
1069 opcode = reverse_lmap [opcode - OP_LBEQ];
1070 } else if (opcode >= OP_IBEQ && opcode <= OP_IBLT_UN) {
1071 opcode = reverse_imap [opcode - OP_IBEQ];
1073 g_assert_not_reached ();
1079 * Returns the type used in the eval stack when @type is loaded.
1080 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
1083 type_to_eval_stack_type (MonoType *type, MonoInst *inst)
1087 inst->klass = klass = mono_class_from_mono_type (type);
1089 inst->type = STACK_MP;
1094 switch (type->type) {
1095 case MONO_TYPE_VOID:
1096 inst->type = STACK_INV;
1100 case MONO_TYPE_BOOLEAN:
1103 case MONO_TYPE_CHAR:
1106 inst->type = STACK_I4;
1111 case MONO_TYPE_FNPTR:
1112 inst->type = STACK_PTR;
1114 case MONO_TYPE_CLASS:
1115 case MONO_TYPE_STRING:
1116 case MONO_TYPE_OBJECT:
1117 case MONO_TYPE_SZARRAY:
1118 case MONO_TYPE_ARRAY:
1119 inst->type = STACK_OBJ;
1123 inst->type = STACK_I8;
1127 inst->type = STACK_R8;
1129 case MONO_TYPE_VALUETYPE:
1130 if (type->data.klass->enumtype) {
1131 type = type->data.klass->enum_basetype;
1134 inst->klass = klass;
1135 inst->type = STACK_VTYPE;
1138 case MONO_TYPE_TYPEDBYREF:
1139 inst->klass = mono_defaults.typed_reference_class;
1140 inst->type = STACK_VTYPE;
1142 case MONO_TYPE_GENERICINST:
1143 type = &type->data.generic_class->container_class->byval_arg;
1146 g_error ("unknown type 0x%02x in eval stack type", type->type);
1151 * The following tables are used to quickly validate the IL code in type_from_op ().
1154 bin_num_table [STACK_MAX] [STACK_MAX] = {
1155 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1156 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1157 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1158 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1159 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
1160 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
1161 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1162 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1167 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
1170 /* reduce the size of this table */
1172 bin_int_table [STACK_MAX] [STACK_MAX] = {
1173 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1174 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1175 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1176 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1177 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1178 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1179 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1180 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1184 bin_comp_table [STACK_MAX] [STACK_MAX] = {
1185 /* Inv i L p F & O vt */
1187 {0, 1, 0, 1, 0, 0, 0, 0}, /* i, int32 */
1188 {0, 0, 1, 0, 0, 0, 0, 0}, /* L, int64 */
1189 {0, 1, 0, 1, 0, 2, 4, 0}, /* p, ptr */
1190 {0, 0, 0, 0, 1, 0, 0, 0}, /* F, R8 */
1191 {0, 0, 0, 2, 0, 1, 0, 0}, /* &, managed pointer */
1192 {0, 0, 0, 4, 0, 0, 3, 0}, /* O, reference */
1193 {0, 0, 0, 0, 0, 0, 0, 0}, /* vt value type */
1196 /* reduce the size of this table */
1198 shift_table [STACK_MAX] [STACK_MAX] = {
1199 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1200 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1201 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1202 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1203 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1204 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1205 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1206 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1210 * Tables to map from the non-specific opcode to the matching
1211 * type-specific opcode.
1213 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
1214 static const guint16
1215 binops_op_map [STACK_MAX] = {
1216 0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, OP_PADD-CEE_ADD
1219 /* handles from CEE_NEG to CEE_CONV_U8 */
1220 static const guint16
1221 unops_op_map [STACK_MAX] = {
1222 0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, OP_PNEG-CEE_NEG
1225 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
1226 static const guint16
1227 ovfops_op_map [STACK_MAX] = {
1228 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
1231 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
1232 static const guint16
1233 ovf2ops_op_map [STACK_MAX] = {
1234 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
1237 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
1238 static const guint16
1239 ovf3ops_op_map [STACK_MAX] = {
1240 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
1243 /* handles from CEE_CEQ to CEE_CLT_UN */
1244 static const guint16
1245 ceqops_op_map [STACK_MAX] = {
1246 0, 0, OP_LCEQ-CEE_CEQ, OP_PCEQ-CEE_CEQ, OP_FCEQ-CEE_CEQ, OP_LCEQ-CEE_CEQ
1250 * Sets ins->type (the type on the eval stack) according to the
1251 * type of the opcode and the arguments to it.
1252 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1254 * FIXME: this function sets ins->type unconditionally in some cases, but
1255 * it should set it to invalid for some types (a conv.x on an object)
1258 type_from_op (MonoInst *ins) {
1259 switch (ins->opcode) {
1266 /* FIXME: check unverifiable args for STACK_MP */
1267 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1268 ins->opcode += binops_op_map [ins->type];
1275 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1276 ins->opcode += binops_op_map [ins->type];
1281 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1282 ins->opcode += binops_op_map [ins->type];
1286 /* FIXME: handle some specifics with ins->next->type */
1287 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1288 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))))
1289 ins->opcode = OP_LCOMPARE;
1296 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1297 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1301 ins->type = neg_table [ins->inst_i0->type];
1302 ins->opcode += unops_op_map [ins->type];
1305 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1306 ins->type = ins->inst_i0->type;
1308 ins->type = STACK_INV;
1309 ins->opcode += unops_op_map [ins->type];
1315 ins->type = STACK_I4;
1316 ins->opcode += unops_op_map [ins->inst_i0->type];
1319 ins->type = STACK_R8;
1320 switch (ins->inst_i0->type) {
1325 ins->opcode = OP_LCONV_TO_R_UN;
1329 case CEE_CONV_OVF_I1:
1330 case CEE_CONV_OVF_U1:
1331 case CEE_CONV_OVF_I2:
1332 case CEE_CONV_OVF_U2:
1333 case CEE_CONV_OVF_I4:
1334 case CEE_CONV_OVF_U4:
1335 ins->type = STACK_I4;
1336 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1338 case CEE_CONV_OVF_I_UN:
1339 case CEE_CONV_OVF_U_UN:
1340 ins->type = STACK_PTR;
1341 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1343 case CEE_CONV_OVF_I1_UN:
1344 case CEE_CONV_OVF_I2_UN:
1345 case CEE_CONV_OVF_I4_UN:
1346 case CEE_CONV_OVF_U1_UN:
1347 case CEE_CONV_OVF_U2_UN:
1348 case CEE_CONV_OVF_U4_UN:
1349 ins->type = STACK_I4;
1350 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1353 ins->type = STACK_PTR;
1354 switch (ins->inst_i0->type) {
1360 ins->opcode = OP_LCONV_TO_U;
1363 ins->opcode = OP_FCONV_TO_U;
1369 ins->type = STACK_I8;
1370 ins->opcode += unops_op_map [ins->inst_i0->type];
1372 case CEE_CONV_OVF_I8:
1373 case CEE_CONV_OVF_U8:
1374 ins->type = STACK_I8;
1375 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1377 case CEE_CONV_OVF_U8_UN:
1378 case CEE_CONV_OVF_I8_UN:
1379 ins->type = STACK_I8;
1380 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1384 ins->type = STACK_R8;
1385 ins->opcode += unops_op_map [ins->inst_i0->type];
1388 ins->type = STACK_R8;
1392 ins->type = STACK_I4;
1393 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1396 case CEE_CONV_OVF_I:
1397 case CEE_CONV_OVF_U:
1398 ins->type = STACK_PTR;
1399 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1402 case CEE_ADD_OVF_UN:
1404 case CEE_MUL_OVF_UN:
1406 case CEE_SUB_OVF_UN:
1407 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1408 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1411 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1418 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_R8, STACK_OBJ
1421 /* map ldelem.x to the matching ldind.x opcode */
1423 ldelem_to_ldind [] = {
1437 /* map stelem.x to the matching stind.x opcode */
1439 stelem_to_stind [] = {
1453 param_table [STACK_MAX] [STACK_MAX] = {
1458 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1462 switch (args->type) {
1472 for (i = 0; i < sig->param_count; ++i) {
1473 switch (args [i].type) {
1477 if (!sig->params [i]->byref)
1481 if (sig->params [i]->byref)
1483 switch (sig->params [i]->type) {
1484 case MONO_TYPE_CLASS:
1485 case MONO_TYPE_STRING:
1486 case MONO_TYPE_OBJECT:
1487 case MONO_TYPE_SZARRAY:
1488 case MONO_TYPE_ARRAY:
1495 if (sig->params [i]->byref)
1497 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1506 /*if (!param_table [args [i].type] [sig->params [i]->type])
1514 * When we need a pointer to the current domain many times in a method, we
1515 * call mono_domain_get() once and we store the result in a local variable.
1516 * This function returns the variable that represents the MonoDomain*.
1518 inline static MonoInst *
1519 mono_get_domainvar (MonoCompile *cfg)
1521 if (!cfg->domainvar)
1522 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1523 return cfg->domainvar;
1527 * The got_var contains the address of the Global Offset Table when AOT
1530 inline static MonoInst *
1531 mono_get_got_var (MonoCompile *cfg)
1533 #ifdef MONO_ARCH_NEED_GOT_VAR
1534 if (!cfg->compile_aot)
1536 if (!cfg->got_var) {
1537 cfg->got_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1539 return cfg->got_var;
1546 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
1549 int num = cfg->num_varinfo;
1551 if ((num + 1) >= cfg->varinfo_count) {
1552 cfg->varinfo_count = (cfg->varinfo_count + 2) * 2;
1553 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
1554 cfg->vars = (MonoMethodVar **)g_realloc (cfg->vars, sizeof (MonoMethodVar*) * cfg->varinfo_count);
1557 /*g_print ("created temp %d of type 0x%x\n", num, type->type);*/
1558 mono_jit_stats.allocate_var++;
1560 MONO_INST_NEW (cfg, inst, opcode);
1561 inst->inst_c0 = num;
1562 inst->inst_vtype = type;
1563 inst->klass = mono_class_from_mono_type (type);
1564 /* if set to 1 the variable is native */
1567 cfg->varinfo [num] = inst;
1569 cfg->vars [num] = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMethodVar));
1570 MONO_INIT_VARINFO (cfg->vars [num], num);
1573 //g_print ("created temp %d of type %s\n", num, mono_type_get_name (type));
1578 * Transform a MonoInst into a load from the variable of index var_index.
1581 mono_compile_make_var_load (MonoCompile *cfg, MonoInst *dest, gssize var_index) {
1582 memset (dest, 0, sizeof (MonoInst));
1583 dest->ssa_op = MONO_SSA_LOAD;
1584 dest->inst_i0 = cfg->varinfo [var_index];
1585 dest->opcode = mono_type_to_ldind (dest->inst_i0->inst_vtype);
1586 type_to_eval_stack_type (dest->inst_i0->inst_vtype, dest);
1587 dest->klass = dest->inst_i0->klass;
1591 * Create a MonoInst that is a load from the variable of index var_index.
1594 mono_compile_create_var_load (MonoCompile *cfg, gssize var_index) {
1596 NEW_TEMPLOAD (cfg,dest,var_index);
1601 * Create a MonoInst that is a store of the given value into the variable of index var_index.
1604 mono_compile_create_var_store (MonoCompile *cfg, gssize var_index, MonoInst *value) {
1606 NEW_TEMPSTORE (cfg, dest, var_index, value);
1611 type_from_stack_type (MonoInst *ins) {
1612 switch (ins->type) {
1613 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1614 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1615 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1616 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1619 * FIXME: This doesn't work because mono_class_from_mono_type ()
1620 * returns the original klass for a byref type, not a 'byref' class,
1621 * causing the JIT to create variables with the wrong type, for
1626 return &ins->klass->this_arg;
1629 return &mono_defaults.object_class->this_arg;
1630 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1631 case STACK_VTYPE: return &ins->klass->byval_arg;
1633 g_error ("stack type %d to montype not handled\n", ins->type);
1639 mono_type_from_stack_type (MonoInst *ins) {
1640 return type_from_stack_type (ins);
1644 array_access_to_klass (int opcode)
1648 return mono_defaults.byte_class;
1650 return mono_defaults.uint16_class;
1653 return mono_defaults.int_class;
1656 return mono_defaults.sbyte_class;
1659 return mono_defaults.int16_class;
1662 return mono_defaults.int32_class;
1664 return mono_defaults.uint32_class;
1667 return mono_defaults.int64_class;
1670 return mono_defaults.single_class;
1673 return mono_defaults.double_class;
1674 case CEE_LDELEM_REF:
1675 case CEE_STELEM_REF:
1676 return mono_defaults.object_class;
1678 g_assert_not_reached ();
1684 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
1688 MONO_ADD_INS (bb, inst);
1691 switch (bb->last_ins->opcode) {
1705 while (prev->next && prev->next != bb->last_ins)
1707 if (prev == bb->code) {
1708 if (bb->last_ins == bb->code) {
1709 inst->next = bb->code;
1712 inst->next = prev->next;
1716 inst->next = bb->last_ins;
1720 // g_warning ("handle conditional jump in add_ins_to_end ()\n");
1722 MONO_ADD_INS (bb, inst);
1728 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
1730 MonoInst *inst, *load;
1732 NEW_TEMPLOAD (cfg, load, src);
1734 NEW_TEMPSTORE (cfg, inst, dest, load);
1735 if (inst->opcode == CEE_STOBJ) {
1736 NEW_TEMPLOADA (cfg, inst, dest);
1737 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE, FALSE);
1739 inst->cil_code = NULL;
1740 mono_add_ins_to_end (bb, inst);
1745 * We try to share variables when possible
1748 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1753 /* inlining can result in deeper stacks */
1754 if (slot >= mono_method_get_header (cfg->method)->max_stack)
1755 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1757 pos = ins->type - 1 + slot * STACK_MAX;
1759 switch (ins->type) {
1766 if ((vnum = cfg->intvars [pos]))
1767 return cfg->varinfo [vnum];
1768 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1769 cfg->intvars [pos] = res->inst_c0;
1772 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1780 * Merge stack state between two basic blocks according to Ecma 335, Partition III,
1781 * section 1.8.1.1. Store the resulting stack state into stack_2.
1782 * Returns: TRUE, if verification succeeds, FALSE otherwise.
1783 * FIXME: We should store the stack state in a dedicated structure instead of in
1787 merge_stacks (MonoCompile *cfg, MonoStackSlot *state_1, MonoStackSlot *state_2, guint32 size)
1791 if (cfg->dont_verify_stack_merge)
1794 /* FIXME: Implement all checks from the spec */
1796 for (i = 0; i < size; ++i) {
1797 MonoStackSlot *slot1 = &state_1 [i];
1798 MonoStackSlot *slot2 = &state_2 [i];
1800 if (slot1->type != slot2->type)
1803 switch (slot1->type) {
1805 /* FIXME: Perform merge ? */
1806 /* klass == NULL means a native int */
1807 if (slot1->klass && slot2->klass) {
1808 if (slot1->klass != slot2->klass)
1813 /* FIXME: Change this to an assert and fix the JIT to allways fill this */
1814 if (slot1->klass && slot2->klass) {
1815 if (slot1->klass != slot2->klass)
1820 MonoClass *klass1 = slot1->klass;
1821 MonoClass *klass2 = slot2->klass;
1824 /* slot1 is ldnull */
1825 } else if (!klass2) {
1826 /* slot2 is ldnull */
1827 slot2->klass = slot1->klass;
1838 * This function is called to handle items that are left on the evaluation stack
1839 * at basic block boundaries. What happens is that we save the values to local variables
1840 * and we reload them later when first entering the target basic block (with the
1841 * handle_loaded_temps () function).
1842 * It is also used to handle items on the stack in store opcodes, since it is
1843 * possible that the variable to be stored into is already on the stack, in
1844 * which case its old value should be used.
1845 * A single joint point will use the same variables (stored in the array bb->out_stack or
1846 * bb->in_stack, if the basic block is before or after the joint point).
1847 * If the stack merge fails at a join point, cfg->unverifiable is set.
1850 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count) {
1852 MonoBasicBlock *outb;
1853 MonoInst *inst, **locals;
1854 MonoStackSlot *stack_state;
1859 if (cfg->verbose_level > 3)
1860 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
1862 stack_state = mono_mempool_alloc (cfg->mempool, sizeof (MonoStackSlot) * count);
1863 for (i = 0; i < count; ++i) {
1864 stack_state [i].type = sp [i]->type;
1865 stack_state [i].klass = sp [i]->klass;
1867 /* Check that instructions other than ldnull have ins->klass set */
1868 if (!cfg->dont_verify_stack_merge && (sp [i]->type == STACK_OBJ) && !((sp [i]->opcode == OP_PCONST) && sp [i]->inst_c0 == 0))
1869 g_assert (sp [i]->klass);
1872 /* Perform verification and stack state merge */
1873 for (i = 0; i < bb->out_count; ++i) {
1874 outb = bb->out_bb [i];
1876 /* exception handlers are linked, but they should not be considered for stack args */
1877 if (outb->flags & BB_EXCEPTION_HANDLER)
1879 if (outb->stack_state) {
1882 if (count != outb->in_scount) {
1883 cfg->unverifiable = TRUE;
1886 verified = merge_stacks (cfg, stack_state, outb->stack_state, count);
1888 cfg->unverifiable = TRUE;
1892 if (cfg->verbose_level > 3) {
1895 for (j = 0; j < count; ++j)
1896 printf ("\tStack state of BB%d, slot %d=%d\n", outb->block_num, j, outb->stack_state [j].type);
1899 /* Make a copy of the stack state */
1900 outb->stack_state = mono_mempool_alloc (cfg->mempool, sizeof (MonoStackSlot) * count);
1901 memcpy (outb->stack_state, stack_state, sizeof (MonoStackSlot) * count);
1905 if (!bb->out_scount) {
1906 bb->out_scount = count;
1907 //g_print ("bblock %d has out:", bb->block_num);
1909 for (i = 0; i < bb->out_count; ++i) {
1910 outb = bb->out_bb [i];
1911 /* exception handlers are linked, but they should not be considered for stack args */
1912 if (outb->flags & BB_EXCEPTION_HANDLER)
1914 //g_print (" %d", outb->block_num);
1915 if (outb->in_stack) {
1917 bb->out_stack = outb->in_stack;
1923 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1924 for (i = 0; i < count; ++i) {
1926 * try to reuse temps already allocated for this purpouse, if they occupy the same
1927 * stack slot and if they are of the same type.
1928 * This won't cause conflicts since if 'local' is used to
1929 * store one of the values in the in_stack of a bblock, then
1930 * the same variable will be used for the same outgoing stack
1932 * This doesn't work when inlining methods, since the bblocks
1933 * in the inlined methods do not inherit their in_stack from
1934 * the bblock they are inlined to. See bug #58863 for an
1937 if (cfg->inlined_method)
1938 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1940 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1945 for (i = 0; i < bb->out_count; ++i) {
1946 outb = bb->out_bb [i];
1947 /* exception handlers are linked, but they should not be considered for stack args */
1948 if (outb->flags & BB_EXCEPTION_HANDLER)
1950 if (outb->in_scount) {
1951 if (outb->in_scount != bb->out_scount)
1953 continue; /* check they are the same locals */
1955 outb->in_scount = count;
1956 outb->in_stack = bb->out_stack;
1959 locals = bb->out_stack;
1960 for (i = 0; i < count; ++i) {
1961 /* add store ops at the end of the bb, before the branch */
1962 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1963 if (inst->opcode == CEE_STOBJ) {
1964 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
1965 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE, FALSE);
1967 inst->cil_code = sp [i]->cil_code;
1968 mono_add_ins_to_end (bb, inst);
1970 if (cfg->verbose_level > 3)
1971 g_print ("storing %d to temp %d\n", i, (int)locals [i]->inst_c0);
1975 * It is possible that the out bblocks already have in_stack assigned, and
1976 * the in_stacks differ. In this case, we will store to all the different
1983 /* Find a bblock which has a different in_stack */
1985 while (bindex < bb->out_count) {
1986 outb = bb->out_bb [bindex];
1987 /* exception handlers are linked, but they should not be considered for stack args */
1988 if (outb->flags & BB_EXCEPTION_HANDLER) {
1992 if (outb->in_stack != locals) {
1994 * Instead of storing sp [i] to locals [i], we need to store
1995 * locals [i] to <new locals>[i], since the sp [i] tree can't
1996 * be shared between trees.
1998 for (i = 0; i < count; ++i)
1999 mono_add_varcopy_to_end (cfg, bb, locals [i]->inst_c0, outb->in_stack [i]->inst_c0);
2000 locals = outb->in_stack;
2012 ret_type_to_call_opcode (MonoType *type, int calli, int virt)
2015 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
2018 switch (type->type) {
2019 case MONO_TYPE_VOID:
2020 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
2023 case MONO_TYPE_BOOLEAN:
2026 case MONO_TYPE_CHAR:
2029 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
2033 case MONO_TYPE_FNPTR:
2034 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
2035 case MONO_TYPE_CLASS:
2036 case MONO_TYPE_STRING:
2037 case MONO_TYPE_OBJECT:
2038 case MONO_TYPE_SZARRAY:
2039 case MONO_TYPE_ARRAY:
2040 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
2043 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
2046 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
2047 case MONO_TYPE_VALUETYPE:
2048 if (type->data.klass->enumtype) {
2049 type = type->data.klass->enum_basetype;
2052 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
2053 case MONO_TYPE_TYPEDBYREF:
2054 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
2055 case MONO_TYPE_GENERICINST:
2056 type = &type->data.generic_class->container_class->byval_arg;
2059 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
2065 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
2067 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
2068 MonoJumpInfoBBTable *table;
2070 table = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfoBBTable));
2072 table->table_size = num_blocks;
2074 ji->ip.label = label;
2075 ji->type = MONO_PATCH_INFO_SWITCH;
2076 ji->data.table = table;
2077 ji->next = cfg->patch_info;
2078 cfg->patch_info = ji;
2082 * When we add a tree of instructions, we need to ensure the instructions currently
2083 * on the stack are executed before (like, if we load a value from a local).
2084 * We ensure this by saving the currently loaded values to temps and rewriting the
2085 * instructions to load the values.
2086 * This is not done for opcodes that terminate a basic block (because it's handled already
2087 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
2090 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
2092 MonoInst *load, *store, *temp, *ins;
2094 while (stack < sp) {
2096 /* handle also other constants */
2097 if ((ins->opcode != OP_ICONST) &&
2098 /* temps never get written to again, so we can safely avoid duplicating them */
2099 !(ins->ssa_op == MONO_SSA_LOAD && ins->inst_i0->opcode == OP_LOCAL && ins->inst_i0->flags & MONO_INST_IS_TEMP)) {
2100 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
2101 temp->flags |= MONO_INST_IS_TEMP;
2102 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2103 store->cil_code = ins->cil_code;
2104 if (store->opcode == CEE_STOBJ) {
2105 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2106 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE, FALSE);
2108 MONO_ADD_INS (bblock, store);
2109 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2110 load->cil_code = ins->cil_code;
2118 * target_type_is_incompatible:
2119 * @cfg: MonoCompile context
2121 * Check that the item @arg on the evaluation stack can be stored
2122 * in the target type (can be a local, or field, etc).
2123 * The cfg arg can be used to check if we need verification or just
2126 * Returns: non-0 value if arg can't be stored on a target.
2129 target_type_is_incompatible (MonoCompile *cfg, MonoType *target, MonoInst *arg)
2131 MonoType *simple_type;
2134 if (target->byref) {
2135 /* FIXME: check that the pointed to types match */
2136 if (arg->type == STACK_MP)
2137 return arg->klass != mono_class_from_mono_type (target);
2138 if (arg->type == STACK_PTR)
2142 simple_type = mono_type_get_underlying_type (target);
2143 switch (simple_type->type) {
2144 case MONO_TYPE_VOID:
2148 case MONO_TYPE_BOOLEAN:
2151 case MONO_TYPE_CHAR:
2154 if (arg->type != STACK_I4 && arg->type != STACK_PTR)
2158 /* STACK_MP is needed when setting pinned locals */
2159 if (arg->type != STACK_I4 && arg->type != STACK_PTR && arg->type != STACK_MP)
2164 case MONO_TYPE_FNPTR:
2165 if (arg->type != STACK_I4 && arg->type != STACK_PTR)
2168 case MONO_TYPE_CLASS:
2169 case MONO_TYPE_STRING:
2170 case MONO_TYPE_OBJECT:
2171 case MONO_TYPE_SZARRAY:
2172 case MONO_TYPE_ARRAY:
2173 if (arg->type != STACK_OBJ)
2175 /* FIXME: check type compatibility */
2179 if (arg->type != STACK_I8)
2184 if (arg->type != STACK_R8)
2187 case MONO_TYPE_VALUETYPE:
2188 if (arg->type != STACK_VTYPE)
2190 klass = mono_class_from_mono_type (simple_type);
2191 if (klass != arg->klass)
2194 case MONO_TYPE_TYPEDBYREF:
2195 if (arg->type != STACK_VTYPE)
2197 klass = mono_class_from_mono_type (simple_type);
2198 if (klass != arg->klass)
2201 case MONO_TYPE_GENERICINST:
2202 if (mono_type_generic_inst_is_valuetype (simple_type)) {
2203 if (arg->type != STACK_VTYPE)
2205 klass = mono_class_from_mono_type (simple_type);
2206 if (klass != arg->klass)
2210 if (arg->type != STACK_OBJ)
2212 /* FIXME: check type compatibility */
2216 g_error ("unknown type 0x%02x in target_type_is_incompatible", simple_type->type);
2222 * Prepare arguments for passing to a function call.
2223 * Return a non-zero value if the arguments can't be passed to the given
2225 * The type checks are not yet complete and some conversions may need
2226 * casts on 32 or 64 bit architectures.
2228 * FIXME: implement this using target_type_is_incompatible ()
2231 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
2233 MonoType *simple_type;
2237 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
2241 for (i = 0; i < sig->param_count; ++i) {
2242 if (sig->params [i]->byref) {
2243 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
2247 simple_type = sig->params [i];
2249 switch (simple_type->type) {
2250 case MONO_TYPE_VOID:
2255 case MONO_TYPE_BOOLEAN:
2258 case MONO_TYPE_CHAR:
2261 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
2267 case MONO_TYPE_FNPTR:
2268 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
2271 case MONO_TYPE_CLASS:
2272 case MONO_TYPE_STRING:
2273 case MONO_TYPE_OBJECT:
2274 case MONO_TYPE_SZARRAY:
2275 case MONO_TYPE_ARRAY:
2276 if (args [i]->type != STACK_OBJ)
2281 if (args [i]->type != STACK_I8)
2286 if (args [i]->type != STACK_R8)
2289 case MONO_TYPE_VALUETYPE:
2290 if (simple_type->data.klass->enumtype) {
2291 simple_type = simple_type->data.klass->enum_basetype;
2294 if (args [i]->type != STACK_VTYPE)
2297 case MONO_TYPE_TYPEDBYREF:
2298 if (args [i]->type != STACK_VTYPE)
2301 case MONO_TYPE_GENERICINST:
2302 simple_type = &simple_type->data.generic_class->container_class->byval_arg;
2306 g_error ("unknown type 0x%02x in check_call_signature",
2314 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
2315 const guint8 *ip, gboolean to_end)
2317 MonoInst *temp, *store, *ins = (MonoInst*)call;
2318 MonoType *ret = sig->ret;
2320 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
2322 call->inst.type = STACK_OBJ;
2323 call->inst.opcode = CEE_CALL;
2324 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
2326 type_to_eval_stack_type (ret, ins);
2327 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
2330 temp->flags |= MONO_INST_IS_TEMP;
2332 if (MONO_TYPE_ISSTRUCT (ret)) {
2333 MonoInst *loada, *dummy_store;
2336 * Emit a dummy store to the local holding the result so the
2337 * liveness info remains correct.
2339 NEW_DUMMY_STORE (cfg, dummy_store, temp->inst_c0);
2341 mono_add_ins_to_end (bblock, dummy_store);
2343 MONO_ADD_INS (bblock, dummy_store);
2345 /* we use this to allocate native sized structs */
2346 temp->unused = sig->pinvoke;
2348 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
2349 if (call->inst.opcode == OP_VCALL)
2350 ins->inst_left = loada;
2352 ins->inst_right = loada; /* a virtual or indirect call */
2355 mono_add_ins_to_end (bblock, ins);
2357 MONO_ADD_INS (bblock, ins);
2359 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2360 store->cil_code = ip;
2362 mono_add_ins_to_end (bblock, store);
2364 MONO_ADD_INS (bblock, store);
2366 return temp->inst_c0;
2369 mono_add_ins_to_end (bblock, ins);
2371 MONO_ADD_INS (bblock, ins);
2376 inline static MonoCallInst *
2377 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
2378 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
2383 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual));
2385 call->inst.cil_code = ip;
2387 call->signature = sig;
2388 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
2389 type_to_eval_stack_type (sig->ret, &call->inst);
2391 for (arg = call->out_args; arg;) {
2392 MonoInst *narg = arg->next;
2397 mono_add_ins_to_end (bblock, arg);
2399 MONO_ADD_INS (bblock, arg);
2406 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
2407 MonoInst **args, MonoInst *addr, const guint8 *ip)
2409 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
2411 call->inst.inst_i0 = addr;
2413 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
2416 static MonoCallInst*
2417 mono_emit_method_call_full (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
2418 MonoInst **args, const guint8 *ip, MonoInst *this, gboolean to_end)
2420 gboolean virtual = this != NULL;
2423 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, to_end);
2425 if (this && sig->hasthis &&
2426 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
2427 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
2428 call->method = mono_marshal_get_remoting_invoke_with_check (method);
2430 call->method = method;
2432 call->inst.flags |= MONO_INST_HAS_METHOD;
2433 call->inst.inst_left = this;
2435 if (call->method->klass->flags & TYPE_ATTRIBUTE_INTERFACE)
2436 /* Needed by the code generated in inssel.brg */
2437 mono_get_got_var (cfg);
2442 static MonoCallInst*
2443 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
2444 MonoInst **args, const guint8 *ip, MonoInst *this)
2446 return mono_emit_method_call_full (cfg, bblock, method, sig, args, ip, this, FALSE);
2450 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
2451 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
2453 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, signature, args, ip, this);
2455 return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
2459 mono_emit_method_call_spilled_full (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
2460 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this,
2461 gboolean ret_object, gboolean to_end)
2463 MonoCallInst *call = mono_emit_method_call_full (cfg, bblock, method, signature, args, ip, this, to_end);
2465 return mono_spill_call (cfg, bblock, call, signature, ret_object, ip, to_end);
2469 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
2470 MonoInst **args, const guint8 *ip, gboolean ret_object, gboolean to_end)
2476 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
2479 return mono_spill_call (cfg, bblock, call, sig, ret_object, ip, to_end);
2483 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
2485 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
2488 g_warning ("unregistered JIT ICall");
2489 g_assert_not_reached ();
2492 return mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, args, ip, FALSE, FALSE);
2496 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
2498 MonoInst *ins, *temp = NULL, *store, *load, *begin;
2499 MonoInst *last_arg = NULL;
2503 //g_print ("emulating: ");
2504 //mono_print_tree_nl (tree);
2505 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE));
2506 ins = (MonoInst*)call;
2508 call->inst.cil_code = tree->cil_code;
2510 call->signature = info->sig;
2512 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
2514 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2515 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
2516 temp->flags |= MONO_INST_IS_TEMP;
2517 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2518 store->cil_code = tree->cil_code;
2523 nargs = info->sig->param_count + info->sig->hasthis;
2525 for (last_arg = call->out_args; last_arg && last_arg->next; last_arg = last_arg->next) ;
2528 last_arg->next = store;
2531 begin = call->out_args;
2535 if (cfg->prev_ins) {
2537 * This assumes that that in a tree, emulate_opcode is called for a
2538 * node before it is called for its children. dec_foreach needs to
2539 * take this into account.
2541 store->next = cfg->prev_ins->next;
2542 cfg->prev_ins->next = begin;
2544 store->next = cfg->cbb->code;
2545 cfg->cbb->code = begin;
2548 call->fptr = mono_icall_get_wrapper (info);
2550 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2551 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2556 static MonoMethodSignature *
2557 mono_get_element_address_signature (int arity)
2559 static GHashTable *sighash = NULL;
2560 MonoMethodSignature *res;
2565 sighash = g_hash_table_new (NULL, NULL);
2567 else if ((res = g_hash_table_lookup (sighash, GINT_TO_POINTER (arity)))) {
2568 LeaveCriticalSection (&jit_mutex);
2572 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
2575 #ifdef MONO_ARCH_VARARG_ICALLS
2576 /* Only set this only some archs since not all backends can handle varargs+pinvoke */
2577 res->call_convention = MONO_CALL_VARARG;
2579 res->params [0] = &mono_defaults.array_class->byval_arg;
2581 for (i = 1; i <= arity; i++)
2582 res->params [i] = &mono_defaults.int_class->byval_arg;
2584 res->ret = &mono_defaults.int_class->byval_arg;
2586 g_hash_table_insert (sighash, GINT_TO_POINTER (arity), res);
2592 static MonoMethodSignature *
2593 mono_get_array_new_va_signature (int arity)
2595 static GHashTable *sighash = NULL;
2596 MonoMethodSignature *res;
2601 sighash = g_hash_table_new (NULL, NULL);
2603 else if ((res = g_hash_table_lookup (sighash, GINT_TO_POINTER (arity)))) {
2608 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
2611 #ifdef MONO_ARCH_VARARG_ICALLS
2612 /* Only set this only some archs since not all backends can handle varargs+pinvoke */
2613 res->call_convention = MONO_CALL_VARARG;
2616 res->params [0] = &mono_defaults.int_class->byval_arg;
2617 for (i = 0; i < arity; i++)
2618 res->params [i + 1] = &mono_defaults.int_class->byval_arg;
2620 res->ret = &mono_defaults.int_class->byval_arg;
2622 g_hash_table_insert (sighash, GINT_TO_POINTER (arity), res);
2629 get_memcpy_method (void)
2631 static MonoMethod *memcpy_method = NULL;
2632 if (!memcpy_method) {
2633 memcpy_method = mono_class_get_method_from_name (mono_defaults.string_class, "memcpy", 3);
2635 g_error ("Old corlib found. Install a new one");
2637 return memcpy_method;
2641 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native, gboolean write_barrier) {
2642 MonoInst *iargs [3];
2645 MonoMethod *memcpy_method;
2649 * This check breaks with spilled vars... need to handle it during verification anyway.
2650 * g_assert (klass && klass == src->klass && klass == dest->klass);
2654 n = mono_class_native_size (klass, &align);
2656 n = mono_class_value_size (klass, &align);
2658 #if HAVE_WRITE_BARRIERS
2659 /* if native is true there should be no references in the struct */
2660 if (write_barrier && klass->has_references && !native) {
2663 NEW_PCONST (cfg, iargs [2], klass);
2665 mono_emit_jit_icall (cfg, bblock, mono_value_copy, iargs, ip);
2670 /* FIXME: add write barrier handling */
2671 if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
2673 if (dest->opcode == OP_LDADDR) {
2674 /* Keep liveness info correct */
2675 NEW_DUMMY_STORE (cfg, inst, dest->inst_i0->inst_c0);
2676 MONO_ADD_INS (bblock, inst);
2678 MONO_INST_NEW (cfg, inst, OP_MEMCPY);
2679 inst->inst_left = dest;
2680 inst->inst_right = src;
2681 inst->cil_code = ip;
2683 MONO_ADD_INS (bblock, inst);
2688 NEW_ICONST (cfg, iargs [2], n);
2690 memcpy_method = get_memcpy_method ();
2691 mono_emit_method_call_spilled_full (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL, FALSE, to_end);
2695 get_memset_method (void)
2697 static MonoMethod *memset_method = NULL;
2698 if (!memset_method) {
2699 memset_method = mono_class_get_method_from_name (mono_defaults.string_class, "memset", 3);
2701 g_error ("Old corlib found. Install a new one");
2703 return memset_method;
2707 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
2709 MonoInst *iargs [3];
2710 MonoInst *ins, *zero_int32;
2712 MonoMethod *memset_method;
2714 NEW_ICONST (cfg, zero_int32, 0);
2716 mono_class_init (klass);
2717 n = mono_class_value_size (klass, NULL);
2718 MONO_INST_NEW (cfg, ins, 0);
2720 ins->inst_left = dest;
2721 ins->inst_right = zero_int32;
2724 ins->opcode = CEE_STIND_I1;
2725 MONO_ADD_INS (bblock, ins);
2728 ins->opcode = CEE_STIND_I2;
2729 MONO_ADD_INS (bblock, ins);
2732 ins->opcode = CEE_STIND_I4;
2733 MONO_ADD_INS (bblock, ins);
2736 if (n <= sizeof (gpointer) * 5) {
2737 ins->opcode = OP_MEMSET;
2740 MONO_ADD_INS (bblock, ins);
2743 memset_method = get_memset_method ();
2744 handle_loaded_temps (cfg, bblock, stack_start, sp);
2746 NEW_ICONST (cfg, iargs [1], 0);
2747 NEW_ICONST (cfg, iargs [2], n);
2748 mono_emit_method_call_spilled (cfg, bblock, memset_method, memset_method->signature, iargs, ip, NULL);
2754 handle_alloc (MonoCompile *cfg, MonoBasicBlock *bblock, MonoClass *klass, gboolean for_box, const guchar *ip)
2756 MonoInst *iargs [2];
2759 if (cfg->opt & MONO_OPT_SHARED) {
2760 NEW_DOMAINCONST (cfg, iargs [0]);
2761 NEW_CLASSCONST (cfg, iargs [1], klass);
2763 alloc_ftn = mono_object_new;
2764 } else if (cfg->compile_aot && bblock->out_of_line && klass->type_token && klass->image == mono_defaults.corlib) {
2765 /* This happens often in argument checking code, eg. throw new FooException... */
2766 /* Avoid relocations by calling a helper function specialized to mscorlib */
2767 NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (klass->type_token));
2768 return mono_emit_jit_icall (cfg, bblock, mono_helper_newobj_mscorlib, iargs, ip);
2770 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
2773 alloc_ftn = mono_class_get_allocation_ftn (vtable, for_box, &pass_lw);
2775 guint32 lw = vtable->klass->instance_size;
2776 lw = ((lw + (sizeof (gpointer) - 1)) & ~(sizeof (gpointer) - 1)) / sizeof (gpointer);
2777 NEW_ICONST (cfg, iargs [0], lw);
2778 NEW_VTABLECONST (cfg, iargs [1], vtable);
2781 NEW_VTABLECONST (cfg, iargs [0], vtable);
2784 return mono_emit_jit_icall (cfg, bblock, alloc_ftn, iargs, ip);
2788 * Handles unbox of a Nullable<T>, returning a temp variable
2789 * where the result is stored
2792 handle_unbox_nullable (MonoCompile* cfg, MonoBasicBlock* bblock, MonoInst* val, const guchar *ip, MonoClass* klass)
2794 MonoMethod* method = mono_class_get_method_from_name (klass, "Unbox", 1);
2795 return mono_emit_method_call_spilled (cfg, bblock, method, mono_method_signature (method), &val, ip, NULL);
2802 handle_box (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *val, const guchar *ip, MonoClass *klass)
2804 MonoInst *dest, *vtoffset, *add, *vstore;
2807 if (mono_class_is_nullable (klass)) {
2808 MonoMethod* method = mono_class_get_method_from_name (klass, "Box", 1);
2809 temp = mono_emit_method_call_spilled (cfg, bblock, method, mono_method_signature (method), &val, ip, NULL);
2810 NEW_TEMPLOAD (cfg, dest, temp);
2815 temp = handle_alloc (cfg, bblock, klass, TRUE, ip);
2816 NEW_TEMPLOAD (cfg, dest, temp);
2817 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
2818 MONO_INST_NEW (cfg, add, OP_PADD);
2819 add->inst_left = dest;
2820 add->inst_right = vtoffset;
2823 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
2824 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
2825 vstore->cil_code = ip;
2826 vstore->inst_left = add;
2827 vstore->inst_right = val;
2829 if (vstore->opcode == CEE_STOBJ) {
2830 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE, TRUE);
2832 MONO_ADD_INS (bblock, vstore);
2834 NEW_TEMPLOAD (cfg, dest, temp);
2839 handle_array_new (MonoCompile *cfg, MonoBasicBlock *bblock, int rank, MonoInst **sp, unsigned char *ip)
2841 MonoMethodSignature *esig;
2842 char icall_name [256];
2844 MonoJitICallInfo *info;
2846 /* Need to register the icall so it gets an icall wrapper */
2847 sprintf (icall_name, "ves_array_new_va_%d", rank);
2849 info = mono_find_jit_icall_by_name (icall_name);
2851 esig = mono_get_array_new_va_signature (rank);
2852 name = g_strdup (icall_name);
2853 info = mono_register_jit_icall (mono_array_new_va, name, esig, FALSE);
2856 g_hash_table_insert (jit_icall_name_hash, name, name);
2860 cfg->flags |= MONO_CFG_HAS_VARARGS;
2862 return mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, sp, ip, TRUE, FALSE);
2866 mono_emit_load_got_addr (MonoCompile *cfg)
2868 MonoInst *load, *store, *dummy_use;
2871 if (!cfg->got_var || cfg->got_var_allocated)
2874 MONO_INST_NEW (cfg, get_got, OP_LOAD_GOTADDR);
2875 NEW_TEMPSTORE (cfg, store, cfg->got_var->inst_c0, get_got);
2877 /* Add it to the start of the first bblock */
2878 if (cfg->bb_entry->code) {
2879 store->next = cfg->bb_entry->code;
2880 cfg->bb_entry->code = store;
2883 MONO_ADD_INS (cfg->bb_entry, store);
2885 cfg->got_var_allocated = TRUE;
2888 * Add a dummy use to keep the got_var alive, since real uses might
2889 * only be generated in the decompose or instruction selection phases.
2890 * Add it to end_bblock, so the variable's lifetime covers the whole
2893 NEW_TEMPLOAD (cfg, load, cfg->got_var->inst_c0);
2894 NEW_DUMMY_USE (cfg, dummy_use, load);
2895 MONO_ADD_INS (cfg->bb_exit, dummy_use);
2898 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
2901 mini_class_is_system_array (MonoClass *klass)
2903 if (klass->parent == mono_defaults.array_class)
2905 else if (mono_defaults.generic_array_class && klass->parent && klass->parent->generic_class)
2906 return klass->parent->generic_class->container_class == mono_defaults.generic_array_class;
2912 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
2914 MonoMethodHeader *header = mono_method_get_header (method);
2915 MonoMethodSignature *signature = mono_method_signature (method);
2919 #ifdef MONO_ARCH_HAVE_LMF_OPS
2920 if (((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2921 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) &&
2922 !MONO_TYPE_ISSTRUCT (signature->ret) && !mini_class_is_system_array (method->klass))
2926 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2927 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2928 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
2929 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
2930 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2931 (method->klass->marshalbyref) ||
2932 !header || header->num_clauses ||
2933 /* fixme: why cant we inline valuetype returns? */
2934 MONO_TYPE_ISSTRUCT (signature->ret))
2937 /* its not worth to inline methods with valuetype arguments?? */
2938 for (i = 0; i < signature->param_count; i++) {
2939 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
2945 * if we can initialize the class of the method right away, we do,
2946 * otherwise we don't allow inlining if the class needs initialization,
2947 * since it would mean inserting a call to mono_runtime_class_init()
2948 * inside the inlined code
2950 if (!(cfg->opt & MONO_OPT_SHARED)) {
2951 vtable = mono_class_vtable (cfg->domain, method->klass);
2952 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
2953 if (cfg->run_cctors) {
2954 /* This makes so that inline cannot trigger */
2955 /* .cctors: too many apps depend on them */
2956 /* running with a specific order... */
2957 if (! vtable->initialized)
2959 mono_runtime_class_init (vtable);
2962 else if (!vtable->initialized && mono_class_needs_cctor_run (method->klass, NULL))
2966 * If we're compiling for shared code
2967 * the cctor will need to be run at aot method load time, for example,
2968 * or at the end of the compilation of the inlining method.
2970 if (mono_class_needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
2973 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
2976 * CAS - do not inline methods with declarative security
2977 * Note: this has to be before any possible return TRUE;
2979 if (mono_method_has_declsec (method))
2982 /* also consider num_locals? */
2983 if (getenv ("MONO_INLINELIMIT")) {
2984 if (header->code_size < atoi (getenv ("MONO_INLINELIMIT"))) {
2987 } else if (header->code_size < INLINE_LENGTH_LIMIT)
2994 mini_field_access_needs_cctor_run (MonoCompile *cfg, MonoMethod *method, MonoVTable *vtable)
2996 if (vtable->initialized && !cfg->compile_aot)
2999 if (vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)
3002 if (!mono_class_needs_cctor_run (vtable->klass, method))
3005 if (! (method->flags & METHOD_ATTRIBUTE_STATIC) && (vtable->klass == method->klass))
3006 /* The initialization is already done before the method is called */
3013 mini_get_ldelema_ins (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
3017 MonoMethodSignature *esig;
3018 char icall_name [256];
3020 MonoJitICallInfo *info;
3022 rank = mono_method_signature (cmethod)->param_count - (is_set? 1: 0);
3025 MONO_INST_NEW (cfg, addr, CEE_LDELEMA);
3026 addr->inst_left = sp [0];
3027 addr->inst_right = sp [1];
3028 addr->cil_code = ip;
3029 addr->type = STACK_MP;
3030 addr->klass = cmethod->klass->element_class;
3034 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
3035 #ifdef MONO_ARCH_EMULATE_MUL_DIV
3036 /* OP_LDELEMA2D depends on OP_LMUL */
3039 NEW_GROUP (cfg, indexes, sp [1], sp [2]);
3040 MONO_INST_NEW (cfg, addr, OP_LDELEMA2D);
3041 addr->inst_left = sp [0];
3042 addr->inst_right = indexes;
3043 addr->cil_code = ip;
3044 addr->type = STACK_MP;
3045 addr->klass = cmethod->klass;
3050 /* Need to register the icall so it gets an icall wrapper */
3051 sprintf (icall_name, "ves_array_element_address_%d", rank);
3053 info = mono_find_jit_icall_by_name (icall_name);
3055 esig = mono_get_element_address_signature (rank);
3056 name = g_strdup (icall_name);
3057 info = mono_register_jit_icall (ves_array_element_address, name, esig, FALSE);
3060 g_hash_table_insert (jit_icall_name_hash, name, name);
3064 temp = mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, sp, ip, FALSE, FALSE);
3065 cfg->flags |= MONO_CFG_HAS_VARARGS;
3067 NEW_TEMPLOAD (cfg, addr, temp);
3071 MonoJitICallInfo **emul_opcode_map = NULL;
3074 mono_find_jit_opcode_emulation (int opcode)
3076 if (emul_opcode_map)
3077 return emul_opcode_map [opcode];
3083 mini_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
3085 MonoInst *ins = NULL;
3087 static MonoClass *runtime_helpers_class = NULL;
3088 if (! runtime_helpers_class)
3089 runtime_helpers_class = mono_class_from_name (mono_defaults.corlib,
3090 "System.Runtime.CompilerServices", "RuntimeHelpers");
3092 if (cmethod->klass == mono_defaults.string_class) {
3093 if (strcmp (cmethod->name, "get_Chars") == 0) {
3094 MONO_INST_NEW (cfg, ins, OP_GETCHR);
3095 ins->inst_i0 = args [0];
3096 ins->inst_i1 = args [1];
3098 } else if (strcmp (cmethod->name, "get_Length") == 0) {
3099 MONO_INST_NEW (cfg, ins, OP_STRLEN);
3100 ins->inst_i0 = args [0];
3102 } else if (strcmp (cmethod->name, "InternalSetChar") == 0) {
3104 MONO_INST_NEW (cfg, get_addr, OP_STR_CHAR_ADDR);
3105 get_addr->inst_i0 = args [0];
3106 get_addr->inst_i1 = args [1];
3107 MONO_INST_NEW (cfg, ins, CEE_STIND_I2);
3108 ins->inst_i0 = get_addr;
3109 ins->inst_i1 = args [2];
3113 } else if (cmethod->klass == mono_defaults.object_class) {
3114 if (strcmp (cmethod->name, "GetType") == 0) {
3115 MONO_INST_NEW (cfg, ins, OP_GETTYPE);
3116 ins->inst_i0 = args [0];
3118 /* The OP_GETHASHCODE rule depends on OP_MUL */
3119 #if !defined(MONO_ARCH_EMULATE_MUL_DIV) && !defined(HAVE_MOVING_COLLECTOR)
3120 } else if (strcmp (cmethod->name, "InternalGetHashCode") == 0) {
3121 MONO_INST_NEW (cfg, ins, OP_GETHASHCODE);
3122 ins->inst_i0 = args [0];
3125 } else if (strcmp (cmethod->name, ".ctor") == 0) {
3126 MONO_INST_NEW (cfg, ins, CEE_NOP);
3130 } else if (cmethod->klass == mono_defaults.array_class) {
3131 if (cmethod->name [0] != 'g')
3134 if (strcmp (cmethod->name, "get_Rank") == 0) {
3135 MONO_INST_NEW (cfg, ins, OP_ARRAY_RANK);
3136 ins->inst_i0 = args [0];
3138 } else if (strcmp (cmethod->name, "get_Length") == 0) {
3139 MONO_INST_NEW (cfg, ins, CEE_LDLEN);
3140 ins->inst_i0 = args [0];
3144 } else if (cmethod->klass == runtime_helpers_class) {
3145 if (strcmp (cmethod->name, "get_OffsetToStringData") == 0) {
3146 NEW_ICONST (cfg, ins, G_STRUCT_OFFSET (MonoString, chars));
3150 } else if (cmethod->klass == mono_defaults.thread_class) {
3151 if (strcmp (cmethod->name, "get_CurrentThread") == 0 && (ins = mono_arch_get_thread_intrinsic (cfg)))
3153 } else if (mini_class_is_system_array (cmethod->klass) &&
3154 strcmp (cmethod->name, "GetGenericValueImpl") == 0) {
3156 MonoInst *ldelem, *store, *load;
3157 MonoClass *eklass = mono_class_from_mono_type (fsig->params [1]);
3159 n = mono_type_to_stind (&eklass->byval_arg);
3164 NEW_LDELEMA (cfg, ldelem, sp, eklass);
3165 ldelem->flags |= MONO_INST_NORANGECHECK;
3166 MONO_INST_NEW (cfg, store, n);
3167 n = mono_type_to_ldind (&eklass->byval_arg);
3168 MONO_INST_NEW (cfg, load, mono_type_to_ldind (&eklass->byval_arg));
3169 type_to_eval_stack_type (&eklass->byval_arg, load);
3170 load->inst_left = ldelem;
3171 store->inst_left = args [2];
3172 store->inst_right = load;
3176 return mono_arch_get_inst_for_method (cfg, cmethod, fsig, args);
3180 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
3182 MonoInst *store, *temp;
3185 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
3187 if (!sig->hasthis && sig->param_count == 0)
3191 if (sp [0]->opcode == OP_ICONST) {
3194 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
3196 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
3197 store->cil_code = sp [0]->cil_code;
3198 MONO_ADD_INS (bblock, store);
3203 for (i = 0; i < sig->param_count; ++i) {
3204 if (sp [0]->opcode == OP_ICONST) {
3207 temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
3209 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
3210 store->cil_code = sp [0]->cil_code;
3211 if (store->opcode == CEE_STOBJ) {
3212 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
3213 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE, FALSE);
3215 MONO_ADD_INS (bblock, store);
3221 #define MONO_INLINE_CALLED_LIMITED_METHODS 0
3222 #define MONO_INLINE_CALLER_LIMITED_METHODS 0
3224 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
3226 mono_inline_called_method_name_limit = NULL;
3227 static gboolean check_inline_called_method_name_limit (MonoMethod *called_method) {
3228 char *called_method_name = mono_method_full_name (called_method, TRUE);
3231 if (mono_inline_called_method_name_limit == NULL) {
3232 char *limit_string = getenv ("MONO_INLINE_CALLED_METHOD_NAME_LIMIT");
3233 if (limit_string != NULL) {
3234 mono_inline_called_method_name_limit = limit_string;
3236 mono_inline_called_method_name_limit = (char *) "";
3240 strncmp_result = strncmp (called_method_name, mono_inline_called_method_name_limit, strlen (mono_inline_called_method_name_limit));
3241 g_free (called_method_name);
3243 //return (strncmp_result <= 0);
3244 return (strncmp_result == 0);
3248 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
3250 mono_inline_caller_method_name_limit = NULL;
3251 static gboolean check_inline_caller_method_name_limit (MonoMethod *caller_method) {
3252 char *caller_method_name = mono_method_full_name (caller_method, TRUE);
3255 if (mono_inline_caller_method_name_limit == NULL) {
3256 char *limit_string = getenv ("MONO_INLINE_CALLER_METHOD_NAME_LIMIT");
3257 if (limit_string != NULL) {
3258 mono_inline_caller_method_name_limit = limit_string;
3260 mono_inline_caller_method_name_limit = (char *) "";
3264 strncmp_result = strncmp (caller_method_name, mono_inline_caller_method_name_limit, strlen (mono_inline_caller_method_name_limit));
3265 g_free (caller_method_name);
3267 //return (strncmp_result <= 0);
3268 return (strncmp_result == 0);
3273 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
3274 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b, gboolean inline_allways)
3276 MonoInst *ins, *rvar = NULL;
3277 MonoMethodHeader *cheader;
3278 MonoBasicBlock *ebblock, *sbblock;
3279 int i, costs, new_locals_offset;
3280 MonoMethod *prev_inlined_method;
3281 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
3282 if ((! inline_allways) && ! check_inline_called_method_name_limit (cmethod))
3285 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
3286 if ((! inline_allways) && ! check_inline_caller_method_name_limit (cfg->method))
3290 if (cfg->verbose_level > 2)
3291 g_print ("INLINE START %p %s -> %s\n", cmethod, mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
3293 if (!cmethod->inline_info) {
3294 mono_jit_stats.inlineable_methods++;
3295 cmethod->inline_info = 1;
3297 /* allocate space to store the return value */
3298 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3299 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
3302 /* allocate local variables */
3303 cheader = mono_method_get_header (cmethod);
3304 new_locals_offset = cfg->num_varinfo;
3305 for (i = 0; i < cheader->num_locals; ++i)
3306 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
3308 /* allocate starte and end blocks */
3309 sbblock = NEW_BBLOCK (cfg);
3310 sbblock->block_num = cfg->num_bblocks++;
3311 sbblock->real_offset = real_offset;
3313 ebblock = NEW_BBLOCK (cfg);
3314 ebblock->block_num = cfg->num_bblocks++;
3315 ebblock->real_offset = real_offset;
3317 prev_inlined_method = cfg->inlined_method;
3318 cfg->inlined_method = cmethod;
3320 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
3322 cfg->inlined_method = prev_inlined_method;
3324 if ((costs >= 0 && costs < 60) || inline_allways) {
3325 if (cfg->verbose_level > 2)
3326 g_print ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
3328 mono_jit_stats.inlined_methods++;
3330 /* always add some code to avoid block split failures */
3331 MONO_INST_NEW (cfg, ins, CEE_NOP);
3332 MONO_ADD_INS (bblock, ins);
3335 bblock->next_bb = sbblock;
3336 link_bblock (cfg, bblock, sbblock);
3339 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
3345 if (cfg->verbose_level > 2)
3346 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
3352 * Some of these comments may well be out-of-date.
3353 * Design decisions: we do a single pass over the IL code (and we do bblock
3354 * splitting/merging in the few cases when it's required: a back jump to an IL
3355 * address that was not already seen as bblock starting point).
3356 * Code is validated as we go (full verification is still better left to metadata/verify.c).
3357 * Complex operations are decomposed in simpler ones right away. We need to let the
3358 * arch-specific code peek and poke inside this process somehow (except when the
3359 * optimizations can take advantage of the full semantic info of coarse opcodes).
3360 * All the opcodes of the form opcode.s are 'normalized' to opcode.
3361 * MonoInst->opcode initially is the IL opcode or some simplification of that
3362 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
3363 * opcode with value bigger than OP_LAST.
3364 * At this point the IR can be handed over to an interpreter, a dumb code generator
3365 * or to the optimizing code generator that will translate it to SSA form.
3367 * Profiling directed optimizations.
3368 * We may compile by default with few or no optimizations and instrument the code
3369 * or the user may indicate what methods to optimize the most either in a config file
3370 * or through repeated runs where the compiler applies offline the optimizations to
3371 * each method and then decides if it was worth it.
3374 * * consider using an array instead of an hash table (bb_hash)
3377 #define CHECK_TYPE(ins) if (!(ins)->type) UNVERIFIED
3378 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) UNVERIFIED
3379 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) UNVERIFIED
3380 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) UNVERIFIED
3381 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) UNVERIFIED
3382 #define CHECK_OPSIZE(size) if (ip + size > end) UNVERIFIED
3383 #define CHECK_UNVERIFIABLE(cfg) if (cfg->unverifiable) UNVERIFIED
3385 /* offset from br.s -> br like opcodes */
3386 #define BIG_BRANCH_OFFSET 13
3389 ip_in_bb (MonoCompile *cfg, MonoBasicBlock *bb, const guint8* ip)
3391 MonoBasicBlock *b = g_hash_table_lookup (cfg->bb_hash, ip);
3393 return b == NULL || b == bb;
3397 get_basic_blocks (MonoCompile *cfg, GHashTable *bbhash, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
3399 unsigned char *ip = start;
3400 unsigned char *target;
3403 MonoBasicBlock *bblock;
3404 const MonoOpcode *opcode;
3407 cli_addr = ip - start;
3408 i = mono_opcode_value ((const guint8 **)&ip, end);
3411 opcode = &mono_opcodes [i];
3412 switch (opcode->argument) {
3413 case MonoInlineNone:
3416 case MonoInlineString:
3417 case MonoInlineType:
3418 case MonoInlineField:
3419 case MonoInlineMethod:
3422 case MonoShortInlineR:
3429 case MonoShortInlineVar:
3430 case MonoShortInlineI:
3433 case MonoShortInlineBrTarget:
3434 target = start + cli_addr + 2 + (signed char)ip [1];
3435 GET_BBLOCK (cfg, bbhash, bblock, target);
3438 GET_BBLOCK (cfg, bbhash, bblock, ip);
3440 case MonoInlineBrTarget:
3441 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
3442 GET_BBLOCK (cfg, bbhash, bblock, target);
3445 GET_BBLOCK (cfg, bbhash, bblock, ip);
3447 case MonoInlineSwitch: {
3448 guint32 n = read32 (ip + 1);
3451 cli_addr += 5 + 4 * n;
3452 target = start + cli_addr;
3453 GET_BBLOCK (cfg, bbhash, bblock, target);
3455 for (j = 0; j < n; ++j) {
3456 target = start + cli_addr + (gint32)read32 (ip);
3457 GET_BBLOCK (cfg, bbhash, bblock, target);
3467 g_assert_not_reached ();
3470 if (i == CEE_THROW) {
3471 unsigned char *bb_start = ip - 1;
3473 /* Find the start of the bblock containing the throw */
3475 while ((bb_start >= start) && !bblock) {
3476 bblock = g_hash_table_lookup (bbhash, (bb_start));
3480 bblock->out_of_line = 1;
3490 emit_tree (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *ins, const guint8* ip_next)
3492 MonoInst *store, *temp, *load;
3494 if (ip_in_bb (cfg, bblock, ip_next) &&
3495 (CODE_IS_STLOC (ip_next) || *ip_next == CEE_RET))
3498 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3499 temp->flags |= MONO_INST_IS_TEMP;
3500 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3501 store->cil_code = ins->cil_code;
3502 MONO_ADD_INS (bblock, store);
3503 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
3504 load->cil_code = ins->cil_code;
3508 static inline MonoMethod *
3509 mini_get_method (MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context)
3513 if (m->wrapper_type != MONO_WRAPPER_NONE)
3514 return mono_method_get_wrapper_data (m, token);
3516 method = mono_get_method_full (m->klass->image, token, klass, context);
3518 if (method && method->is_inflated)
3519 method = mono_get_inflated_method (method);
3524 static inline MonoClass*
3525 mini_get_class (MonoMethod *method, guint32 token, MonoGenericContext *context)
3529 if (method->wrapper_type != MONO_WRAPPER_NONE)
3530 klass = mono_method_get_wrapper_data (method, token);
3532 klass = mono_class_get_full (method->klass->image, token, context);
3534 mono_class_init (klass);
3539 * Returns TRUE if the JIT should abort inlining because "callee"
3540 * is influenced by security attributes.
3543 gboolean check_linkdemand (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee, MonoBasicBlock *bblock, unsigned char *ip)
3547 if ((cfg->method != caller) && mono_method_has_declsec (callee)) {
3551 result = mono_declsec_linkdemand (cfg->domain, caller, callee);
3552 if (result == MONO_JIT_SECURITY_OK)
3555 if (result == MONO_JIT_LINKDEMAND_ECMA) {
3556 /* Generate code to throw a SecurityException before the actual call/link */
3557 MonoAssembly *assembly = mono_image_get_assembly (caller->klass->image);
3558 MonoReflectionAssembly *refass = (MonoReflectionAssembly*) mono_assembly_get_object (cfg->domain, assembly);
3559 MonoReflectionMethod *refmet = mono_method_get_object (cfg->domain, caller, NULL);
3560 MonoSecurityManager *secman = mono_security_manager_get_methods ();
3563 NEW_ICONST (cfg, args [0], 4);
3564 NEW_PCONST (cfg, args [1], refass);
3565 NEW_PCONST (cfg, args [2], refmet);
3566 mono_emit_method_call_spilled (cfg, bblock, secman->linkdemandsecurityexception, mono_method_signature (secman->linkdemandsecurityexception), args, ip, NULL);
3567 } else if (cfg->exception_type == MONO_EXCEPTION_NONE) {
3568 /* don't hide previous results */
3569 cfg->exception_type = MONO_EXCEPTION_SECURITY_LINKDEMAND;
3570 cfg->exception_data = result;
3577 can_access_internals (MonoAssembly *accessing, MonoAssembly* accessed)
3580 if (accessing == accessed)
3582 if (!accessed || !accessing)
3584 for (tmp = accessed->friend_assembly_names; tmp; tmp = tmp->next) {
3585 MonoAssemblyName *friend = tmp->data;
3586 /* Be conservative with checks */
3589 if (strcmp (accessing->aname.name, friend->name))
3591 if (friend->public_key_token [0]) {
3592 if (!accessing->aname.public_key_token [0])
3594 if (strcmp ((char*)friend->public_key_token, (char*)accessing->aname.public_key_token))
3602 /* FIXME: check visibility of type, too */
3604 can_access_member (MonoClass *access_klass, MonoClass *member_klass, int access_level)
3606 /* Partition I 8.5.3.2 */
3607 /* the access level values are the same for fields and methods */
3608 switch (access_level) {
3609 case FIELD_ATTRIBUTE_COMPILER_CONTROLLED:
3610 /* same compilation unit */
3611 return access_klass->image == member_klass->image;
3612 case FIELD_ATTRIBUTE_PRIVATE:
3613 return access_klass == member_klass;
3614 case FIELD_ATTRIBUTE_FAM_AND_ASSEM:
3615 if (mono_class_has_parent (access_klass, member_klass) &&
3616 can_access_internals (access_klass->image->assembly, member_klass->image->assembly))
3619 case FIELD_ATTRIBUTE_ASSEMBLY:
3620 return can_access_internals (access_klass->image->assembly, member_klass->image->assembly);
3621 case FIELD_ATTRIBUTE_FAMILY:
3622 if (mono_class_has_parent (access_klass, member_klass))
3625 case FIELD_ATTRIBUTE_FAM_OR_ASSEM:
3626 if (mono_class_has_parent (access_klass, member_klass))
3628 return can_access_internals (access_klass->image->assembly, member_klass->image->assembly);
3629 case FIELD_ATTRIBUTE_PUBLIC:
3636 can_access_field (MonoMethod *method, MonoClassField *field)
3638 /* FIXME: check all overlapping fields */
3639 int can = can_access_member (method->klass, field->parent, field->type->attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK);
3641 MonoClass *nested = method->klass->nested_in;
3643 can = can_access_member (nested, field->parent, field->type->attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK);
3646 nested = nested->nested_in;
3653 can_access_method (MonoMethod *method, MonoMethod *called)
3655 int can = can_access_member (method->klass, called->klass, called->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK);
3657 MonoClass *nested = method->klass->nested_in;
3659 can = can_access_member (nested, called->klass, called->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK);
3662 nested = nested->nested_in;
3667 * with generics calls to explicit interface implementations can be expressed
3668 * directly: the method is private, but we must allow it. This may be opening
3669 * a hole or the generics code should handle this differently.
3670 * Maybe just ensure the interface type is public.
3672 if ((called->flags & METHOD_ATTRIBUTE_VIRTUAL) && (called->flags & METHOD_ATTRIBUTE_FINAL))
3678 * mono_method_to_ir: translates IL into basic blocks containing trees
3681 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
3682 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
3683 guint inline_offset, gboolean is_virtual_call)
3685 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
3686 MonoInst *ins, **sp, **stack_start;
3687 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
3689 MonoMethod *cmethod;
3690 MonoInst **arg_array;
3691 MonoMethodHeader *header;
3693 guint32 token, ins_flag;
3695 MonoClass *constrained_call = NULL;
3696 unsigned char *ip, *end, *target, *err_pos;
3697 static double r8_0 = 0.0;
3698 MonoMethodSignature *sig;
3699 MonoGenericContext *generic_context = NULL;
3700 MonoGenericContainer *generic_container = NULL;
3701 MonoType **param_types;
3702 GList *bb_recheck = NULL, *tmp;
3703 int i, n, start_new_bblock, ialign;
3704 int num_calls = 0, inline_costs = 0;
3705 int breakpoint_id = 0;
3707 guint real_offset, num_args;
3708 MonoBoolean security, pinvoke;
3709 MonoSecurityManager* secman = NULL;
3710 MonoDeclSecurityActions actions;
3711 GSList *class_inits = NULL;
3712 gboolean dont_verify, dont_verify_stloc;
3714 /* serialization and xdomain stuff may need access to private fields and methods */
3715 dont_verify = method->klass->image->assembly->corlib_internal? TRUE: FALSE;
3716 dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE;
3717 dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH;
3718 dont_verify |= method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE; /* bug #77896 */
3719 dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP;
3721 /* still some type unsafety issues in marshal wrappers... (unknown is PtrToStructure) */
3722 dont_verify_stloc = method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE;
3723 dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_UNKNOWN;
3724 dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED;
3726 /* Not turned on yet */
3727 cfg->dont_verify_stack_merge = TRUE;
3729 image = method->klass->image;
3730 header = mono_method_get_header (method);
3731 generic_container = method->generic_container;
3732 sig = mono_method_signature (method);
3733 num_args = sig->hasthis + sig->param_count;
3734 ip = (unsigned char*)header->code;
3735 end = ip + header->code_size;
3736 mono_jit_stats.cil_code_size += header->code_size;
3738 if (sig->is_inflated)
3739 generic_context = ((MonoMethodInflated *) method)->context;
3740 else if (generic_container)
3741 generic_context = &generic_container->context;
3743 g_assert (!sig->has_type_parameters);
3745 if (cfg->method == method) {
3747 bbhash = cfg->bb_hash;
3749 real_offset = inline_offset;
3750 bbhash = g_hash_table_new (g_direct_hash, NULL);
3753 if (cfg->verbose_level > 2)
3754 g_print ("method to IR %s\n", mono_method_full_name (method, TRUE));
3756 dont_inline = g_list_prepend (dont_inline, method);
3757 if (cfg->method == method) {
3759 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
3760 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
3763 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
3764 start_bblock->cil_code = NULL;
3765 start_bblock->cil_length = 0;
3766 start_bblock->block_num = cfg->num_bblocks++;
3769 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
3770 end_bblock->cil_code = NULL;
3771 end_bblock->cil_length = 0;
3772 end_bblock->block_num = cfg->num_bblocks++;
3773 g_assert (cfg->num_bblocks == 2);
3775 arg_array = alloca (sizeof (MonoInst *) * num_args);
3776 for (i = num_args - 1; i >= 0; i--)
3777 arg_array [i] = cfg->varinfo [i];
3779 if (header->num_clauses) {
3780 cfg->spvars = g_hash_table_new (NULL, NULL);
3781 cfg->exvars = g_hash_table_new (NULL, NULL);
3783 /* handle exception clauses */
3784 for (i = 0; i < header->num_clauses; ++i) {
3785 MonoBasicBlock *try_bb;
3786 MonoExceptionClause *clause = &header->clauses [i];
3787 GET_BBLOCK (cfg, bbhash, try_bb, ip + clause->try_offset);
3788 try_bb->real_offset = clause->try_offset;
3789 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
3790 tblock->real_offset = clause->handler_offset;
3791 tblock->flags |= BB_EXCEPTION_HANDLER;
3793 link_bblock (cfg, try_bb, tblock);
3795 if (*(ip + clause->handler_offset) == CEE_POP)
3796 tblock->flags |= BB_EXCEPTION_DEAD_OBJ;
3798 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
3799 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
3800 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
3801 MONO_ADD_INS (tblock, ins);
3803 /* todo: is a fault block unsafe to optimize? */
3804 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
3805 tblock->flags |= BB_EXCEPTION_UNSAFE;
3809 /*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);
3811 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
3813 /* catch and filter blocks get the exception object on the stack */
3814 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
3815 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
3816 MonoInst *load, *dummy_use;
3818 /* mostly like handle_stack_args (), but just sets the input args */
3819 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
3820 tblock->in_scount = 1;
3821 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
3822 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
3823 tblock->stack_state = mono_mempool_alloc (cfg->mempool, sizeof (MonoStackSlot));
3824 tblock->stack_state [0].type = STACK_OBJ;
3826 tblock->stack_state [0].klass = mono_defaults.object_class;
3829 * Add a dummy use for the exvar so its liveness info will be
3832 NEW_TEMPLOAD (cfg, load, tblock->in_stack [0]->inst_c0);
3833 NEW_DUMMY_USE (cfg, dummy_use, load);
3834 MONO_ADD_INS (tblock, dummy_use);
3836 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
3837 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->data.filter_offset);
3838 tblock->real_offset = clause->data.filter_offset;
3839 tblock->in_scount = 1;
3840 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
3841 tblock->stack_state = mono_mempool_alloc (cfg->mempool, sizeof (MonoStackSlot));
3842 tblock->stack_state [0].type = STACK_OBJ;
3844 tblock->stack_state [0].klass = mono_defaults.object_class;
3846 /* The filter block shares the exvar with the handler block */
3847 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
3848 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
3849 MONO_ADD_INS (tblock, ins);
3854 arg_array = alloca (sizeof (MonoInst *) * num_args);
3855 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
3858 /* FIRST CODE BLOCK */
3859 bblock = NEW_BBLOCK (cfg);
3860 bblock->cil_code = ip;
3862 ADD_BBLOCK (cfg, bbhash, bblock);
3864 if (cfg->method == method) {
3865 breakpoint_id = mono_debugger_method_has_breakpoint (method);
3866 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
3867 MONO_INST_NEW (cfg, ins, CEE_BREAK);
3868 MONO_ADD_INS (bblock, ins);
3872 if (mono_use_security_manager)
3873 secman = mono_security_manager_get_methods ();
3875 security = (secman && mono_method_has_declsec (method));
3876 /* at this point having security doesn't mean we have any code to generate */
3877 if (security && (cfg->method == method)) {
3878 /* Only Demand, NonCasDemand and DemandChoice requires code generation.
3879 * And we do not want to enter the next section (with allocation) if we
3880 * have nothing to generate */
3881 security = mono_declsec_get_demands (method, &actions);
3884 /* we must Demand SecurityPermission.Unmanaged before P/Invoking */
3885 pinvoke = (secman && (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE));
3887 MonoMethod *wrapped = mono_marshal_method_from_wrapper (method);
3888 if (wrapped && (wrapped->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
3889 MonoCustomAttrInfo* custom = mono_custom_attrs_from_method (wrapped);
3891 /* unless the method or it's class has the [SuppressUnmanagedCodeSecurity] attribute */
3892 if (custom && mono_custom_attrs_has_attr (custom, secman->suppressunmanagedcodesecurity)) {
3897 custom = mono_custom_attrs_from_class (wrapped->klass);
3898 if (custom && mono_custom_attrs_has_attr (custom, secman->suppressunmanagedcodesecurity)) {
3903 /* not a P/Invoke after all */
3908 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED))) || mono_compile_aot || security || pinvoke) {
3909 /* we use a separate basic block for the initialization code */
3910 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
3911 init_localsbb->real_offset = real_offset;
3912 start_bblock->next_bb = init_localsbb;
3913 init_localsbb->next_bb = bblock;
3914 link_bblock (cfg, start_bblock, init_localsbb);
3915 link_bblock (cfg, init_localsbb, bblock);
3916 init_localsbb->block_num = cfg->num_bblocks++;
3918 start_bblock->next_bb = bblock;
3919 link_bblock (cfg, start_bblock, bblock);
3922 /* at this point we know, if security is TRUE, that some code needs to be generated */
3923 if (security && (cfg->method == method)) {
3926 mono_jit_stats.cas_demand_generation++;
3928 if (actions.demand.blob) {
3929 /* Add code for SecurityAction.Demand */
3930 NEW_DECLSECCONST (cfg, args[0], image, actions.demand);
3931 NEW_ICONST (cfg, args [1], actions.demand.size);
3932 /* Calls static void SecurityManager.InternalDemand (byte* permissions, int size); */
3933 mono_emit_method_call_spilled (cfg, init_localsbb, secman->demand, mono_method_signature (secman->demand), args, ip, NULL);
3935 if (actions.noncasdemand.blob) {
3936 /* CLR 1.x uses a .noncasdemand (but 2.x doesn't) */
3937 /* For Mono we re-route non-CAS Demand to Demand (as the managed code must deal with it anyway) */
3938 NEW_DECLSECCONST (cfg, args[0], image, actions.noncasdemand);
3939 NEW_ICONST (cfg, args [1], actions.noncasdemand.size);
3940 /* Calls static void SecurityManager.InternalDemand (byte* permissions, int size); */
3941 mono_emit_method_call_spilled (cfg, init_localsbb, secman->demand, mono_method_signature (secman->demand), args, ip, NULL);
3943 if (actions.demandchoice.blob) {
3944 /* New in 2.0, Demand must succeed for one of the permissions (i.e. not all) */
3945 NEW_DECLSECCONST (cfg, args[0], image, actions.demandchoice);
3946 NEW_ICONST (cfg, args [1], actions.demandchoice.size);
3947 /* Calls static void SecurityManager.InternalDemandChoice (byte* permissions, int size); */
3948 mono_emit_method_call_spilled (cfg, init_localsbb, secman->demandchoice, mono_method_signature (secman->demandchoice), args, ip, NULL);
3952 /* we must Demand SecurityPermission.Unmanaged before p/invoking */
3954 mono_emit_method_call_spilled (cfg, init_localsbb, secman->demandunmanaged, mono_method_signature (secman->demandunmanaged), NULL, ip, NULL);
3957 if (get_basic_blocks (cfg, bbhash, header, real_offset, ip, end, &err_pos)) {
3962 if (cfg->method == method)
3963 mono_debug_init_method (cfg, bblock, breakpoint_id);
3965 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
3967 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
3968 for (n = 0; n < sig->param_count; ++n)
3969 param_types [n + sig->hasthis] = sig->params [n];
3970 for (n = 0; n < header->num_locals; ++n) {
3971 if (header->locals [n]->type == MONO_TYPE_VOID && !header->locals [n]->byref)
3976 /* do this somewhere outside - not here */
3977 NEW_ICONST (cfg, zero_int32, 0);
3978 NEW_ICONST (cfg, zero_int64, 0);
3979 zero_int64->type = STACK_I8;
3980 NEW_PCONST (cfg, zero_ptr, 0);
3981 NEW_PCONST (cfg, zero_obj, 0);
3982 zero_obj->type = STACK_OBJ;
3984 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
3985 zero_r8->type = STACK_R8;
3986 zero_r8->inst_p0 = &r8_0;
3988 /* add a check for this != NULL to inlined methods */
3989 if (is_virtual_call) {
3990 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
3991 NEW_ARGLOAD (cfg, ins->inst_left, 0);
3993 MONO_ADD_INS (bblock, ins);
3996 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
3997 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
4000 start_new_bblock = 0;
4003 if (cfg->method == method)
4004 real_offset = ip - header->code;
4006 real_offset = inline_offset;
4008 if (start_new_bblock) {
4009 bblock->cil_length = ip - bblock->cil_code;
4010 if (start_new_bblock == 2) {
4011 g_assert (ip == tblock->cil_code);
4013 GET_BBLOCK (cfg, bbhash, tblock, ip);
4015 bblock->next_bb = tblock;
4017 start_new_bblock = 0;
4018 for (i = 0; i < bblock->in_scount; ++i) {
4019 if (cfg->verbose_level > 3)
4020 g_print ("loading %d from temp %d\n", i, (int)bblock->in_stack [i]->inst_c0);
4021 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
4024 g_slist_free (class_inits);
4027 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
4028 link_bblock (cfg, bblock, tblock);
4029 if (sp != stack_start) {
4030 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4032 CHECK_UNVERIFIABLE (cfg);
4034 bblock->next_bb = tblock;
4036 for (i = 0; i < bblock->in_scount; ++i) {
4037 if (cfg->verbose_level > 3)
4038 g_print ("loading %d from temp %d\n", i, (int)bblock->in_stack [i]->inst_c0);
4039 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
4042 g_slist_free (class_inits);
4047 bblock->real_offset = real_offset;
4049 if ((cfg->method == method) && cfg->coverage_info) {
4050 MonoInst *store, *one;
4051 guint32 cil_offset = ip - header->code;
4052 cfg->coverage_info->data [cil_offset].cil_code = ip;
4054 /* TODO: Use an increment here */
4055 NEW_ICONST (cfg, one, 1);
4058 NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
4061 MONO_INST_NEW (cfg, store, CEE_STIND_I);
4062 store->cil_code = ip;
4063 store->inst_left = ins;
4064 store->inst_right = one;
4066 MONO_ADD_INS (bblock, store);
4069 if (cfg->verbose_level > 3)
4070 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
4075 MONO_INST_NEW (cfg, ins, *ip);
4076 ins->cil_code = ip++;
4077 MONO_ADD_INS (bblock, ins);
4083 CHECK_STACK_OVF (1);
4084 n = (*ip)-CEE_LDARG_0;
4086 NEW_ARGLOAD (cfg, ins, n);
4087 ins->cil_code = ip++;
4094 CHECK_STACK_OVF (1);
4095 n = (*ip)-CEE_LDLOC_0;
4097 NEW_LOCLOAD (cfg, ins, n);
4098 ins->cil_code = ip++;
4106 n = (*ip)-CEE_STLOC_0;
4109 handle_loaded_temps (cfg, bblock, stack_start, sp);
4110 NEW_LOCSTORE (cfg, ins, n, *sp);
4112 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
4114 if (ins->opcode == CEE_STOBJ) {
4115 NEW_LOCLOADA (cfg, ins, n);
4116 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
4118 MONO_ADD_INS (bblock, ins);
4124 CHECK_STACK_OVF (1);
4126 NEW_ARGLOAD (cfg, ins, ip [1]);
4133 CHECK_STACK_OVF (1);
4135 NEW_ARGLOADA (cfg, ins, ip [1]);
4145 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
4146 handle_loaded_temps (cfg, bblock, stack_start, sp);
4148 if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [ip [1]], *sp))
4150 if (ins->opcode == CEE_STOBJ) {
4151 NEW_ARGLOADA (cfg, ins, ip [1]);
4152 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
4154 MONO_ADD_INS (bblock, ins);
4159 CHECK_STACK_OVF (1);
4160 CHECK_LOCAL (ip [1]);
4161 NEW_LOCLOAD (cfg, ins, ip [1]);
4168 CHECK_STACK_OVF (1);
4169 CHECK_LOCAL (ip [1]);
4170 NEW_LOCLOADA (cfg, ins, ip [1]);
4179 handle_loaded_temps (cfg, bblock, stack_start, sp);
4180 CHECK_LOCAL (ip [1]);
4181 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
4183 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [ip [1]], *sp))
4185 if (ins->opcode == CEE_STOBJ) {
4186 NEW_LOCLOADA (cfg, ins, ip [1]);
4187 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
4189 MONO_ADD_INS (bblock, ins);
4194 CHECK_STACK_OVF (1);
4195 NEW_PCONST (cfg, ins, NULL);
4197 ins->type = STACK_OBJ;
4202 CHECK_STACK_OVF (1);
4203 NEW_ICONST (cfg, ins, -1);
4217 CHECK_STACK_OVF (1);
4218 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
4225 CHECK_STACK_OVF (1);
4227 NEW_ICONST (cfg, ins, *((signed char*)ip));
4234 CHECK_STACK_OVF (1);
4235 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
4242 CHECK_STACK_OVF (1);
4243 MONO_INST_NEW (cfg, ins, OP_I8CONST);
4245 ins->type = STACK_I8;
4247 ins->inst_l = (gint64)read64 (ip);
4253 /* we should really allocate this only late in the compilation process */
4254 mono_domain_lock (cfg->domain);
4255 f = mono_mempool_alloc (cfg->domain->mp, sizeof (float));
4256 mono_domain_unlock (cfg->domain);
4258 CHECK_STACK_OVF (1);
4259 MONO_INST_NEW (cfg, ins, OP_R4CONST);
4260 ins->type = STACK_R8;
4271 mono_domain_lock (cfg->domain);
4272 d = mono_mempool_alloc (cfg->domain->mp, sizeof (double));
4273 mono_domain_unlock (cfg->domain);
4275 CHECK_STACK_OVF (1);
4276 MONO_INST_NEW (cfg, ins, OP_R8CONST);
4277 ins->type = STACK_R8;
4287 MonoInst *temp, *store;
4289 CHECK_STACK_OVF (1);
4294 * small optimization: if the loaded value was from a local already,
4295 * just load it twice.
4297 if (ins->ssa_op == MONO_SSA_LOAD &&
4298 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
4300 MONO_INST_NEW (cfg, temp, 0);
4302 temp->cil_code = ip;
4305 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
4306 temp->flags |= MONO_INST_IS_TEMP;
4307 temp->cil_code = ip;
4308 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4309 store->cil_code = ip;
4310 if (store->opcode == CEE_STOBJ) {
4311 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
4312 handle_stobj (cfg, bblock, store, sp [0], sp [0]->cil_code, store->klass, TRUE, FALSE, FALSE);
4314 MONO_ADD_INS (bblock, store);
4316 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
4319 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
4329 MONO_INST_NEW (cfg, ins, CEE_POP);
4330 MONO_ADD_INS (bblock, ins);
4331 ins->cil_code = ip++;
4337 if (stack_start != sp)
4339 MONO_INST_NEW (cfg, ins, CEE_JMP);
4340 token = read32 (ip + 1);
4341 /* FIXME: check the signature matches */
4342 cmethod = mini_get_method (method, token, NULL, generic_context);
4347 if (mono_use_security_manager) {
4348 if (check_linkdemand (cfg, method, cmethod, bblock, ip))
4352 ins->inst_p0 = cmethod;
4353 MONO_ADD_INS (bblock, ins);
4355 start_new_bblock = 1;
4359 case CEE_CALLVIRT: {
4360 MonoInst *addr = NULL;
4361 MonoMethodSignature *fsig = NULL;
4362 int temp, array_rank = 0;
4363 int virtual = *ip == CEE_CALLVIRT;
4366 token = read32 (ip + 1);
4368 if (*ip == CEE_CALLI) {
4373 if (method->wrapper_type != MONO_WRAPPER_NONE)
4374 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
4376 fsig = mono_metadata_parse_signature (image, token);
4378 n = fsig->param_count + fsig->hasthis;
4380 if (method->wrapper_type != MONO_WRAPPER_NONE) {
4381 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
4382 } else if (constrained_call) {
4383 cmethod = mono_get_method_constrained (image, token, constrained_call, generic_context);
4384 cmethod = mono_get_inflated_method (cmethod);
4386 cmethod = mini_get_method (method, token, NULL, generic_context);
4391 if (!dont_verify && !can_access_method (method, cmethod))
4394 if (!virtual && (cmethod->flags & METHOD_ATTRIBUTE_ABSTRACT))
4395 /* MS.NET seems to silently convert this to a callvirt */
4398 if (!cmethod->klass->inited){
4399 if (!mono_class_init (cmethod->klass))
4403 if (mono_method_signature (cmethod)->pinvoke) {
4404 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
4405 fsig = mono_method_signature (wrapper);
4406 } else if (constrained_call) {
4407 fsig = mono_method_signature (cmethod);
4409 fsig = mono_method_get_signature_full (cmethod, image, token, generic_context);
4412 n = fsig->param_count + fsig->hasthis;
4414 if (mono_use_security_manager) {
4415 if (check_linkdemand (cfg, method, cmethod, bblock, ip))
4419 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
4420 mini_class_is_system_array (cmethod->klass)) {
4421 array_rank = cmethod->klass->rank;
4424 if (cmethod->string_ctor)
4425 g_assert_not_reached ();
4429 if (cmethod && cmethod->klass->generic_container)
4434 //g_assert (!virtual || fsig->hasthis);
4438 if (constrained_call) {
4440 * We have the `constrained.' prefix opcode.
4442 if (constrained_call->valuetype && !cmethod->klass->valuetype) {
4445 * The type parameter is instantiated as a valuetype,
4446 * but that type doesn't override the method we're
4447 * calling, so we need to box `this'.
4448 * sp [0] is a pointer to the data: we need the value
4449 * in handle_box (), so load it here.
4451 MONO_INST_NEW (cfg, load, mono_type_to_ldind (&constrained_call->byval_arg));
4452 type_to_eval_stack_type (&constrained_call->byval_arg, load);
4453 load->cil_code = ip;
4454 load->inst_left = sp [0];
4455 sp [0] = handle_box (cfg, bblock, load, ip, constrained_call);
4456 } else if (!constrained_call->valuetype) {
4460 * The type parameter is instantiated as a reference
4461 * type. We have a managed pointer on the stack, so
4462 * we need to dereference it here.
4465 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
4467 ins->inst_i0 = sp [0];
4468 ins->type = STACK_OBJ;
4469 ins->klass = mono_class_from_mono_type (&constrained_call->byval_arg);
4471 } else if (cmethod->klass->valuetype)
4473 constrained_call = NULL;
4476 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
4479 if (cmethod && virtual &&
4480 (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) &&
4481 !((cmethod->flags & METHOD_ATTRIBUTE_FINAL) &&
4482 cmethod->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK) &&
4483 mono_method_signature (cmethod)->generic_param_count) {
4484 MonoInst *this_temp, *store;
4485 MonoInst *iargs [3];
4487 g_assert (mono_method_signature (cmethod)->is_inflated);
4488 /* Prevent inlining of methods that contain indirect calls */
4491 this_temp = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
4492 this_temp->cil_code = ip;
4493 NEW_TEMPSTORE (cfg, store, this_temp->inst_c0, sp [0]);
4495 store->cil_code = ip;
4496 MONO_ADD_INS (bblock, store);
4498 NEW_TEMPLOAD (cfg, iargs [0], this_temp->inst_c0);
4499 NEW_PCONST (cfg, iargs [1], cmethod);
4500 NEW_PCONST (cfg, iargs [2], ((MonoMethodInflated *) cmethod)->context);
4501 temp = mono_emit_jit_icall (cfg, bblock, mono_helper_compile_generic_method, iargs, ip);
4503 NEW_TEMPLOAD (cfg, addr, temp);
4504 NEW_TEMPLOAD (cfg, sp [0], this_temp->inst_c0);
4506 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
4507 NEW_TEMPLOAD (cfg, *sp, temp);
4516 if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL) &&
4517 (mono_metadata_signature_equal (mono_method_signature (method), mono_method_signature (cmethod)))) {
4520 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
4522 /* FIXME: This assumes the two methods has the same number and type of arguments */
4523 for (i = 0; i < n; ++i) {
4524 /* Check if argument is the same */
4525 NEW_ARGLOAD (cfg, ins, i);
4526 if ((ins->opcode == sp [i]->opcode) && (ins->inst_i0 == sp [i]->inst_i0))
4529 /* Prevent argument from being register allocated */
4530 arg_array [i]->flags |= MONO_INST_VOLATILE;
4531 NEW_ARGSTORE (cfg, ins, i, sp [i]);
4533 if (ins->opcode == CEE_STOBJ) {
4534 NEW_ARGLOADA (cfg, ins, i);
4535 handle_stobj (cfg, bblock, ins, sp [i], sp [i]->cil_code, ins->klass, FALSE, FALSE, FALSE);
4538 MONO_ADD_INS (bblock, ins);
4540 MONO_INST_NEW (cfg, ins, CEE_JMP);
4542 ins->inst_p0 = cmethod;
4543 ins->inst_p1 = arg_array [0];
4544 MONO_ADD_INS (bblock, ins);
4545 link_bblock (cfg, bblock, end_bblock);
4546 start_new_bblock = 1;
4547 /* skip CEE_RET as well */
4552 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_inst_for_method (cfg, cmethod, fsig, sp))) {
4555 if (MONO_TYPE_IS_VOID (fsig->ret)) {
4556 MONO_ADD_INS (bblock, ins);
4558 type_to_eval_stack_type (fsig->ret, ins);
4568 handle_loaded_temps (cfg, bblock, stack_start, sp);
4570 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
4571 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
4572 mono_method_check_inlining (cfg, cmethod) &&
4573 !g_list_find (dont_inline, cmethod)) {
4575 MonoBasicBlock *ebblock;
4576 gboolean allways = FALSE;
4578 if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
4579 (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
4580 /* Prevent inlining of methods that call wrappers */
4582 cmethod = mono_marshal_get_native_wrapper (cmethod);
4586 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, allways))) {
4590 GET_BBLOCK (cfg, bbhash, bblock, ip);
4591 ebblock->next_bb = bblock;
4592 link_bblock (cfg, ebblock, bblock);
4594 if (!MONO_TYPE_IS_VOID (fsig->ret))
4597 /* indicates start of a new block, and triggers a load of all
4598 stack arguments at bb boundarie */
4601 inline_costs += costs;
4607 inline_costs += 10 * num_calls++;
4609 /* tail recursion elimination */
4610 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == method && ip [5] == CEE_RET) {
4611 gboolean has_vtargs = FALSE;
4614 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
4616 /* keep it simple */
4617 for (i = fsig->param_count - 1; i >= 0; i--) {
4618 if (MONO_TYPE_ISSTRUCT (mono_method_signature (cmethod)->params [i]))
4623 for (i = 0; i < n; ++i) {
4624 NEW_ARGSTORE (cfg, ins, i, sp [i]);
4626 MONO_ADD_INS (bblock, ins);
4628 MONO_INST_NEW (cfg, ins, CEE_BR);
4630 MONO_ADD_INS (bblock, ins);
4631 tblock = start_bblock->out_bb [0];
4632 link_bblock (cfg, bblock, tblock);
4633 ins->inst_target_bb = tblock;
4634 start_new_bblock = 1;
4636 /* skip the CEE_RET, too */
4637 if (ip_in_bb (cfg, bblock, ip + 5))
4646 if (*ip == CEE_CALLI) {
4647 /* Prevent inlining of methods with indirect calls */
4649 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
4650 NEW_TEMPLOAD (cfg, *sp, temp);
4653 } else if (array_rank) {
4656 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
4657 if (sp [fsig->param_count]->type == STACK_OBJ) {
4658 MonoInst *iargs [2];
4659 MonoInst *array, *to_store, *store;
4661 handle_loaded_temps (cfg, bblock, stack_start, sp);
4663 array = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
4664 NEW_TEMPSTORE (cfg, store, array->inst_c0, sp [0]);
4665 store->cil_code = ip;
4666 MONO_ADD_INS (bblock, store);
4667 NEW_TEMPLOAD (cfg, iargs [0], array->inst_c0);
4669 to_store = mono_compile_create_var (cfg, type_from_stack_type (sp [fsig->param_count]), OP_LOCAL);
4670 NEW_TEMPSTORE (cfg, store, to_store->inst_c0, sp [fsig->param_count]);
4671 store->cil_code = ip;
4672 MONO_ADD_INS (bblock, store);
4673 NEW_TEMPLOAD (cfg, iargs [1], to_store->inst_c0);
4676 * We first save the args for the call so that the args are copied to the stack
4677 * and a new instruction tree for them is created. If we don't do this,
4678 * the same MonoInst is added to two different trees and this is not
4681 mono_emit_jit_icall (cfg, bblock, mono_helper_stelem_ref_check, iargs, ip);
4683 NEW_TEMPLOAD (cfg, sp [0], array->inst_c0);
4684 NEW_TEMPLOAD (cfg, sp [fsig->param_count], to_store->inst_c0);
4687 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
4688 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
4690 if (ins->opcode == CEE_STOBJ) {
4691 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE, TRUE);
4693 MONO_ADD_INS (bblock, ins);
4696 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
4697 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
4698 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
4702 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
4703 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
4706 g_assert_not_reached ();
4710 /* Prevent inlining of methods which call other methods */
4712 if (ip_in_bb (cfg, bblock, ip + 5)
4713 && (!MONO_TYPE_ISSTRUCT (fsig->ret))
4714 && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)
4715 && (CODE_IS_STLOC (ip + 5) || ip [5] == CEE_POP || ip [5] == CEE_RET)) {
4716 /* no need to spill */
4717 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
4720 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) {
4721 NEW_TEMPLOAD (cfg, *sp, temp);
4732 if (cfg->method != method) {
4733 /* return from inlined methode */
4738 //g_assert (returnvar != -1);
4739 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
4740 store->cil_code = sp [0]->cil_code;
4741 if (store->opcode == CEE_STOBJ) {
4742 g_assert_not_reached ();
4743 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
4744 /* FIXME: it is possible some optimization will pass the a heap pointer for the struct address, so we'll need the write barrier */
4745 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE, FALSE);
4747 MONO_ADD_INS (bblock, store);
4751 g_assert (!return_var);
4754 MONO_INST_NEW (cfg, ins, CEE_NOP);
4755 ins->opcode = mono_type_to_stind (mono_method_signature (method)->ret);
4756 if (ins->opcode == CEE_STOBJ) {
4757 NEW_RETLOADA (cfg, ins);
4758 /* FIXME: it is possible some optimization will pass the a heap pointer for the struct address, so we'll need the write barrier */
4759 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
4761 ins->opcode = OP_SETRET;
4763 ins->inst_i0 = *sp;;
4764 ins->inst_i1 = NULL;
4765 MONO_ADD_INS (bblock, ins);
4769 if (sp != stack_start)
4771 MONO_INST_NEW (cfg, ins, CEE_BR);
4772 ins->cil_code = ip++;
4773 ins->inst_target_bb = end_bblock;
4774 MONO_ADD_INS (bblock, ins);
4775 link_bblock (cfg, bblock, end_bblock);
4776 start_new_bblock = 1;
4780 MONO_INST_NEW (cfg, ins, CEE_BR);
4781 ins->cil_code = ip++;
4782 MONO_ADD_INS (bblock, ins);
4783 target = ip + 1 + (signed char)(*ip);
4785 GET_BBLOCK (cfg, bbhash, tblock, target);
4786 link_bblock (cfg, bblock, tblock);
4787 CHECK_BBLOCK (target, ip, tblock);
4788 ins->inst_target_bb = tblock;
4789 if (sp != stack_start) {
4790 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4792 CHECK_UNVERIFIABLE (cfg);
4794 start_new_bblock = 1;
4795 inline_costs += BRANCH_COST;
4801 if (sp [-1]->type == STACK_VTYPE || sp [-1]->type == STACK_R8)
4803 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
4804 ins->cil_code = ip++;
4805 target = ip + 1 + *(signed char*)ip;
4807 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
4808 if (sp != stack_start) {
4809 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4811 CHECK_UNVERIFIABLE (cfg);
4813 inline_costs += BRANCH_COST;
4827 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
4828 ins->cil_code = ip++;
4829 target = ip + 1 + *(signed char*)ip;
4832 if (sp != stack_start) {
4833 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4835 CHECK_UNVERIFIABLE (cfg);
4837 inline_costs += BRANCH_COST;
4841 MONO_INST_NEW (cfg, ins, CEE_BR);
4842 ins->cil_code = ip++;
4843 MONO_ADD_INS (bblock, ins);
4844 target = ip + 4 + (gint32)read32(ip);
4846 GET_BBLOCK (cfg, bbhash, tblock, target);
4847 link_bblock (cfg, bblock, tblock);
4848 CHECK_BBLOCK (target, ip, tblock);
4849 ins->inst_target_bb = tblock;
4850 if (sp != stack_start) {
4851 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4853 CHECK_UNVERIFIABLE (cfg);
4855 start_new_bblock = 1;
4856 inline_costs += BRANCH_COST;
4862 if (sp [-1]->type == STACK_VTYPE || sp [-1]->type == STACK_R8)
4864 MONO_INST_NEW (cfg, ins, *ip);
4865 ins->cil_code = ip++;
4866 target = ip + 4 + (gint32)read32(ip);
4868 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
4869 if (sp != stack_start) {
4870 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4872 CHECK_UNVERIFIABLE (cfg);
4874 inline_costs += BRANCH_COST;
4888 MONO_INST_NEW (cfg, ins, *ip);
4889 ins->cil_code = ip++;
4890 target = ip + 4 + (gint32)read32(ip);
4893 if (sp != stack_start) {
4894 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4896 CHECK_UNVERIFIABLE (cfg);
4898 inline_costs += BRANCH_COST;
4903 n = read32 (ip + 1);
4904 MONO_INST_NEW (cfg, ins, *ip);
4906 ins->inst_left = *sp;
4907 if ((ins->inst_left->type != STACK_I4) && (ins->inst_left->type != STACK_PTR))
4911 CHECK_OPSIZE (n * sizeof (guint32));
4912 target = ip + n * sizeof (guint32);
4913 MONO_ADD_INS (bblock, ins);
4914 GET_BBLOCK (cfg, bbhash, tblock, target);
4915 link_bblock (cfg, bblock, tblock);
4916 ins->klass = GUINT_TO_POINTER (n);
4917 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
4918 ins->inst_many_bb [n] = tblock;
4920 for (i = 0; i < n; ++i) {
4921 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
4922 link_bblock (cfg, bblock, tblock);
4923 ins->inst_many_bb [i] = tblock;
4926 if (sp != stack_start) {
4927 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4929 CHECK_UNVERIFIABLE (cfg);
4931 /* Needed by the code generated in inssel.brg */
4932 mono_get_got_var (cfg);
4933 inline_costs += (BRANCH_COST * 2);
4947 MONO_INST_NEW (cfg, ins, *ip);
4952 ins->type = ldind_type [*ip - CEE_LDIND_I1];
4953 ins->flags |= ins_flag;
4955 if (ins->type == STACK_OBJ)
4956 ins->klass = mono_defaults.object_class;
4967 #if HAVE_WRITE_BARRIERS
4968 if (*ip == CEE_STIND_REF && method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER) {
4969 /* insert call to write barrier */
4970 MonoMethod *write_barrier = mono_marshal_get_write_barrier ();
4972 mono_emit_method_call_spilled (cfg, bblock, write_barrier, mono_method_signature (write_barrier), sp, ip, NULL);
4977 MONO_INST_NEW (cfg, ins, *ip);
4978 ins->cil_code = ip++;
4980 handle_loaded_temps (cfg, bblock, stack_start, sp);
4981 MONO_ADD_INS (bblock, ins);
4982 ins->inst_i0 = sp [0];
4983 ins->inst_i1 = sp [1];
4984 ins->flags |= ins_flag;
4992 #ifdef MONO_ARCH_NO_EMULATE_MUL_IMM
4993 /* FIXME: This breaks with ssapre (mono -O=ssapre loader.exe) */
4994 if ((ins->inst_right->opcode == OP_ICONST) && !(cfg->opt & MONO_OPT_SSAPRE)) {
4995 switch (ins->opcode) {
4997 ins->opcode = OP_IMUL_IMM;
4998 ins->inst_imm = ins->inst_right->inst_c0;
5001 ins->opcode = OP_LMUL_IMM;
5002 ins->inst_imm = ins->inst_right->inst_c0;
5005 g_assert_not_reached ();
5010 if (mono_find_jit_opcode_emulation (ins->opcode)) {
5012 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
5030 /* special case that gives a nice speedup and happens to workaorund a ppc jit but (for the release)
5031 * later apply the speedup to the left shift as well
5034 if ((ins->opcode == OP_LSHR_UN) && (ins->type == STACK_I8)
5035 && (ins->inst_right->opcode == OP_ICONST) && (ins->inst_right->inst_c0 == 32)) {
5036 ins->opcode = OP_LONG_SHRUN_32;
5037 /*g_print ("applied long shr speedup to %s\n", cfg->method->name);*/
5041 if (mono_find_jit_opcode_emulation (ins->opcode)) {
5043 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
5057 case CEE_CONV_OVF_I8:
5058 case CEE_CONV_OVF_U8:
5062 if (mono_find_jit_opcode_emulation (ins->opcode)) {
5064 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
5068 case CEE_CONV_OVF_I4:
5069 case CEE_CONV_OVF_I1:
5070 case CEE_CONV_OVF_I2:
5071 case CEE_CONV_OVF_I:
5072 case CEE_CONV_OVF_U:
5075 if (sp [-1]->type == STACK_R8) {
5076 ADD_UNOP (CEE_CONV_OVF_I8);
5084 case CEE_CONV_OVF_U1:
5085 case CEE_CONV_OVF_U2:
5086 case CEE_CONV_OVF_U4:
5089 if (sp [-1]->type == STACK_R8) {
5090 ADD_UNOP (CEE_CONV_OVF_U8);
5098 case CEE_CONV_OVF_I1_UN:
5099 case CEE_CONV_OVF_I2_UN:
5100 case CEE_CONV_OVF_I4_UN:
5101 case CEE_CONV_OVF_I8_UN:
5102 case CEE_CONV_OVF_U1_UN:
5103 case CEE_CONV_OVF_U2_UN:
5104 case CEE_CONV_OVF_U4_UN:
5105 case CEE_CONV_OVF_U8_UN:
5106 case CEE_CONV_OVF_I_UN:
5107 case CEE_CONV_OVF_U_UN:
5115 token = read32 (ip + 1);
5116 klass = mini_get_class (method, token, generic_context);
5120 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5121 MonoInst *store, *load;
5122 MONO_INST_NEW (cfg, load, CEE_LDIND_REF);
5123 load->cil_code = ip;
5124 load->inst_i0 = sp [1];
5125 load->type = STACK_OBJ;
5126 load->klass = klass;
5127 load->flags |= ins_flag;
5128 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
5129 store->cil_code = ip;
5130 handle_loaded_temps (cfg, bblock, stack_start, sp);
5131 MONO_ADD_INS (bblock, store);
5132 store->inst_i0 = sp [0];
5133 store->inst_i1 = load;
5134 store->flags |= ins_flag;
5136 n = mono_class_value_size (klass, NULL);
5137 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
5139 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
5140 copy->inst_left = sp [0];
5141 copy->inst_right = sp [1];
5142 copy->cil_code = ip;
5144 MONO_ADD_INS (bblock, copy);
5146 MonoMethod *memcpy_method = get_memcpy_method ();
5147 MonoInst *iargs [3];
5150 NEW_ICONST (cfg, iargs [2], n);
5151 iargs [2]->cil_code = ip;
5153 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
5160 MonoInst *iargs [3];
5166 token = read32 (ip + 1);
5167 klass = mini_get_class (method, token, generic_context);
5170 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5171 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
5173 ins->inst_i0 = sp [0];
5174 ins->type = STACK_OBJ;
5176 ins->flags |= ins_flag;
5183 /* Optimize the common ldobj+stloc combination */
5193 loc_index = ip [5] - CEE_STLOC_0;
5200 if ((loc_index != -1) && ip_in_bb (cfg, bblock, ip + 5)) {
5201 CHECK_LOCAL (loc_index);
5202 NEW_LOCSTORE (cfg, ins, loc_index, *sp);
5204 if (ins->opcode == CEE_STOBJ) {
5205 handle_loaded_temps (cfg, bblock, stack_start, sp);
5207 g_assert (ins->opcode == CEE_STOBJ);
5208 NEW_LOCLOADA (cfg, ins, loc_index);
5209 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
5216 n = mono_class_value_size (klass, NULL);
5217 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
5218 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
5219 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
5221 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
5222 copy->inst_left = iargs [0];
5223 copy->inst_right = *sp;
5224 copy->cil_code = ip;
5226 MONO_ADD_INS (bblock, copy);
5228 MonoMethod *memcpy_method = get_memcpy_method ();
5230 NEW_ICONST (cfg, iargs [2], n);
5231 iargs [2]->cil_code = ip;
5233 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
5235 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
5243 CHECK_STACK_OVF (1);
5245 n = read32 (ip + 1);
5247 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
5248 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, n));
5250 ins->type = STACK_OBJ;
5251 ins->klass = mono_defaults.string_class;
5254 else if (method->wrapper_type != MONO_WRAPPER_NONE) {
5256 MonoInst *iargs [1];
5258 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
5259 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
5260 NEW_TEMPLOAD (cfg, *sp, temp);
5264 if (cfg->opt & MONO_OPT_SHARED) {
5266 MonoInst *iargs [3];
5267 MonoInst* domain_var;
5269 if (cfg->compile_aot) {
5270 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, GINT_TO_POINTER (n));
5272 /* avoid depending on undefined C behavior in sequence points */
5273 domain_var = mono_get_domainvar (cfg);
5274 NEW_TEMPLOAD (cfg, iargs [0], domain_var->inst_c0);
5275 NEW_IMAGECONST (cfg, iargs [1], image);
5276 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
5277 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
5278 NEW_TEMPLOAD (cfg, *sp, temp);
5279 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
5281 if (bblock->out_of_line) {
5282 MonoInst *iargs [2];
5285 if (cfg->compile_aot && cfg->method->klass->image == mono_defaults.corlib) {
5287 * Avoid relocations by using a version of helper_ldstr
5288 * specialized to mscorlib.
5290 NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (n));
5291 temp = mono_emit_jit_icall (cfg, bblock, mono_helper_ldstr_mscorlib, iargs, ip);
5293 /* Avoid creating the string object */
5294 NEW_IMAGECONST (cfg, iargs [0], image);
5295 NEW_ICONST (cfg, iargs [1], mono_metadata_token_index (n));
5296 temp = mono_emit_jit_icall (cfg, bblock, mono_helper_ldstr, iargs, ip);
5298 NEW_TEMPLOAD (cfg, *sp, temp);
5301 if (cfg->compile_aot) {
5302 NEW_LDSTRCONST (cfg, ins, image, n);
5306 NEW_PCONST (cfg, ins, NULL);
5308 ins->type = STACK_OBJ;
5309 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
5310 ins->klass = mono_defaults.string_class;
5320 MonoInst *iargs [2];
5321 MonoMethodSignature *fsig;
5325 token = read32 (ip + 1);
5326 cmethod = mini_get_method (method, token, NULL, generic_context);
5329 fsig = mono_method_get_signature (cmethod, image, token);
5331 if (!mono_class_init (cmethod->klass))
5334 if (mono_use_security_manager) {
5335 if (check_linkdemand (cfg, method, cmethod, bblock, ip))
5339 n = fsig->param_count;
5342 /* move the args to allow room for 'this' in the first position */
5348 handle_loaded_temps (cfg, bblock, stack_start, sp);
5350 if (mini_class_is_system_array (cmethod->klass)) {
5351 NEW_METHODCONST (cfg, *sp, cmethod);
5352 temp = handle_array_new (cfg, bblock, fsig->param_count, sp, ip);
5353 } else if (cmethod->string_ctor) {
5354 /* we simply pass a null pointer */
5355 NEW_PCONST (cfg, *sp, NULL);
5356 /* now call the string ctor */
5357 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
5359 MonoInst* callvirt_this_arg = NULL;
5361 if (cmethod->klass->valuetype) {
5362 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
5363 temp = iargs [0]->inst_c0;
5365 NEW_TEMPLOADA (cfg, *sp, temp);
5367 handle_initobj (cfg, bblock, *sp, NULL, cmethod->klass, stack_start, sp);
5369 NEW_TEMPLOADA (cfg, *sp, temp);
5372 * The code generated by mini_emit_virtual_call () expects
5373 * iargs [0] to be a boxed instance, but luckily the vcall
5374 * will be transformed into a normal call there.
5377 temp = handle_alloc (cfg, bblock, cmethod->klass, FALSE, ip);
5378 NEW_TEMPLOAD (cfg, *sp, temp);
5381 /* Avoid virtual calls to ctors if possible */
5382 if (cmethod->klass->marshalbyref)
5383 callvirt_this_arg = sp [0];
5385 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
5386 mono_method_check_inlining (cfg, cmethod) &&
5387 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
5388 !g_list_find (dont_inline, cmethod)) {
5390 MonoBasicBlock *ebblock;
5391 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, FALSE))) {
5396 GET_BBLOCK (cfg, bbhash, bblock, ip);
5397 ebblock->next_bb = bblock;
5398 link_bblock (cfg, ebblock, bblock);
5400 NEW_TEMPLOAD (cfg, *sp, temp);
5403 /* indicates start of a new block, and triggers a load
5404 of all stack arguments at bb boundarie */
5407 inline_costs += costs;
5411 /* Prevent inlining of methods which call other methods */
5413 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, callvirt_this_arg);
5416 /* Prevent inlining of methods which call other methods */
5418 /* now call the actual ctor */
5419 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, callvirt_this_arg);
5423 NEW_TEMPLOAD (cfg, *sp, temp);
5434 token = read32 (ip + 1);
5435 klass = mini_get_class (method, token, generic_context);
5438 if (sp [0]->type != STACK_OBJ)
5441 /* Needed by the code generated in inssel.brg */
5442 mono_get_got_var (cfg);
5444 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
5446 MonoMethod *mono_isinst;
5447 MonoInst *iargs [1];
5448 MonoBasicBlock *ebblock;
5452 mono_isinst = mono_marshal_get_isinst (klass);
5455 costs = inline_method (cfg, mono_isinst, mono_method_signature (mono_isinst), bblock,
5456 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
5458 g_assert (costs > 0);
5463 GET_BBLOCK (cfg, bbhash, bblock, ip);
5464 ebblock->next_bb = bblock;
5465 link_bblock (cfg, ebblock, bblock);
5467 temp = iargs [0]->inst_i0->inst_c0;
5468 NEW_TEMPLOAD (cfg, *sp, temp);
5472 inline_costs += costs;
5474 MONO_INST_NEW (cfg, ins, *ip);
5475 ins->type = STACK_OBJ;
5476 ins->inst_left = *sp;
5477 ins->inst_newa_class = klass;
5480 *sp++ = emit_tree (cfg, bblock, ins, ip + 5);
5484 case CEE_UNBOX_ANY: {
5485 MonoInst *add, *vtoffset;
5486 MonoInst *iargs [3];
5491 token = read32 (ip + 1);
5492 klass = mini_get_class (method, token, generic_context);
5496 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5498 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
5499 MonoMethod *mono_castclass;
5500 MonoInst *iargs [1];
5501 MonoBasicBlock *ebblock;
5505 mono_castclass = mono_marshal_get_castclass (klass);
5508 costs = inline_method (cfg, mono_castclass, mono_method_signature (mono_castclass), bblock,
5509 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
5511 g_assert (costs > 0);
5516 GET_BBLOCK (cfg, bbhash, bblock, ip);
5517 ebblock->next_bb = bblock;
5518 link_bblock (cfg, ebblock, bblock);
5520 temp = iargs [0]->inst_i0->inst_c0;
5521 NEW_TEMPLOAD (cfg, *sp, temp);
5525 inline_costs += costs;
5527 MONO_INST_NEW (cfg, ins, CEE_CASTCLASS);
5528 ins->type = STACK_OBJ;
5529 ins->inst_left = *sp;
5531 ins->inst_newa_class = klass;
5539 if (mono_class_is_nullable (klass)) {
5540 int v = handle_unbox_nullable (cfg, bblock, *sp, ip, klass);
5541 NEW_TEMPLOAD (cfg, *sp, v);
5547 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
5548 ins->type = STACK_OBJ;
5549 ins->inst_left = *sp;
5551 ins->inst_newa_class = klass;
5554 MONO_INST_NEW (cfg, add, OP_PADD);
5555 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
5556 add->inst_left = ins;
5557 add->inst_right = vtoffset;
5558 add->type = STACK_MP;
5559 add->klass = mono_defaults.object_class;
5563 n = mono_class_value_size (klass, NULL);
5564 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
5565 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
5566 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
5568 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
5569 copy->inst_left = iargs [0];
5570 copy->inst_right = *sp;
5571 copy->cil_code = ip;
5573 MONO_ADD_INS (bblock, copy);
5575 MonoMethod *memcpy_method = get_memcpy_method ();
5577 NEW_ICONST (cfg, iargs [2], n);
5578 iargs [2]->cil_code = ip;
5580 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
5582 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
5588 MonoInst *add, *vtoffset;
5593 token = read32 (ip + 1);
5594 klass = mini_get_class (method, token, generic_context);
5598 if (mono_class_is_nullable (klass)) {
5599 int v = handle_unbox_nullable (cfg, bblock, *sp, ip, klass);
5600 NEW_TEMPLOAD (cfg, *sp, v);
5606 /* Needed by the code generated in inssel.brg */
5607 mono_get_got_var (cfg);
5609 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
5610 ins->type = STACK_OBJ;
5611 ins->inst_left = *sp;
5613 ins->inst_newa_class = klass;
5616 MONO_INST_NEW (cfg, add, OP_PADD);
5617 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
5618 add->inst_left = ins;
5619 add->inst_right = vtoffset;
5620 add->type = STACK_MP;
5631 token = read32 (ip + 1);
5632 klass = mini_get_class (method, token, generic_context);
5635 if (sp [0]->type != STACK_OBJ)
5638 /* Needed by the code generated in inssel.brg */
5639 mono_get_got_var (cfg);
5641 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
5643 MonoMethod *mono_castclass;
5644 MonoInst *iargs [1];
5645 MonoBasicBlock *ebblock;
5649 mono_castclass = mono_marshal_get_castclass (klass);
5652 costs = inline_method (cfg, mono_castclass, mono_method_signature (mono_castclass), bblock,
5653 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
5655 g_assert (costs > 0);
5660 GET_BBLOCK (cfg, bbhash, bblock, ip);
5661 ebblock->next_bb = bblock;
5662 link_bblock (cfg, ebblock, bblock);
5664 temp = iargs [0]->inst_i0->inst_c0;
5665 NEW_TEMPLOAD (cfg, *sp, temp);
5669 inline_costs += costs;
5671 MONO_INST_NEW (cfg, ins, *ip);
5672 ins->type = STACK_OBJ;
5673 ins->inst_left = *sp;
5675 ins->inst_newa_class = klass;
5677 *sp++ = emit_tree (cfg, bblock, ins, ip + 5);
5683 MONO_INST_NEW (cfg, ins, *ip);
5685 ins->inst_left = *sp;
5686 ins->cil_code = ip++;
5687 bblock->out_of_line = TRUE;
5688 MONO_ADD_INS (bblock, ins);
5689 MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
5690 ins->cil_code = ip - 1;
5691 MONO_ADD_INS (bblock, ins);
5694 link_bblock (cfg, bblock, end_bblock);
5695 start_new_bblock = 1;
5700 MonoInst *offset_ins;
5701 MonoClassField *field;
5702 MonoBasicBlock *ebblock;
5706 if (*ip == CEE_STFLD) {
5713 if (sp [0]->type == STACK_I4 || sp [0]->type == STACK_I8 || sp [0]->type == STACK_R8)
5715 if (*ip != CEE_LDFLD && sp [0]->type == STACK_VTYPE)
5718 token = read32 (ip + 1);
5719 if (method->wrapper_type != MONO_WRAPPER_NONE) {
5720 field = mono_method_get_wrapper_data (method, token);
5721 klass = field->parent;
5723 field = mono_field_from_token (image, token, &klass, generic_context);
5727 mono_class_init (klass);
5728 if (!dont_verify && !can_access_field (method, field))
5731 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
5732 /* FIXME: mark instructions for use in SSA */
5733 if (*ip == CEE_STFLD) {
5734 if (target_type_is_incompatible (cfg, field->type, sp [1]))
5736 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound || klass == mono_defaults.marshalbyrefobject_class) {
5737 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
5738 MonoInst *iargs [5];
5741 NEW_CLASSCONST (cfg, iargs [1], klass);
5742 NEW_FIELDCONST (cfg, iargs [2], field);
5743 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
5747 if (cfg->opt & MONO_OPT_INLINE) {
5748 costs = inline_method (cfg, stfld_wrapper, mono_method_signature (stfld_wrapper), bblock,
5749 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
5750 g_assert (costs > 0);
5755 GET_BBLOCK (cfg, bbhash, bblock, ip);
5756 ebblock->next_bb = bblock;
5757 link_bblock (cfg, ebblock, bblock);
5759 /* indicates start of a new block, and triggers a load
5760 of all stack arguments at bb boundarie */
5763 inline_costs += costs;
5766 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, mono_method_signature (stfld_wrapper), iargs, ip, NULL);
5768 #if HAVE_WRITE_BARRIERS
5769 } else if (mono_type_to_stind (field->type) == CEE_STIND_REF) {
5770 /* insert call to write barrier */
5771 MonoMethod *write_barrier = mono_marshal_get_write_barrier ();
5772 MonoInst *iargs [2];
5773 NEW_ICONST (cfg, offset_ins, foffset);
5774 MONO_INST_NEW (cfg, ins, OP_PADD);
5776 ins->inst_left = *sp;
5777 ins->inst_right = offset_ins;
5778 ins->type = STACK_MP;
5779 ins->klass = mono_defaults.object_class;
5782 mono_emit_method_call_spilled (cfg, bblock, write_barrier, mono_method_signature (write_barrier), iargs, ip, NULL);
5786 NEW_ICONST (cfg, offset_ins, foffset);
5787 MONO_INST_NEW (cfg, ins, OP_PADD);
5789 ins->inst_left = *sp;
5790 ins->inst_right = offset_ins;
5791 ins->type = STACK_MP;
5793 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
5794 store->cil_code = ip;
5795 store->inst_left = ins;
5796 store->inst_right = sp [1];
5797 handle_loaded_temps (cfg, bblock, stack_start, sp);
5798 store->flags |= ins_flag;
5800 if (store->opcode == CEE_STOBJ) {
5801 handle_stobj (cfg, bblock, ins, sp [1], ip,
5802 mono_class_from_mono_type (field->type), FALSE, FALSE, TRUE);
5804 MONO_ADD_INS (bblock, store);
5807 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound || klass == mono_defaults.marshalbyrefobject_class) {
5808 MonoMethod *wrapper = (*ip == CEE_LDFLDA) ? mono_marshal_get_ldflda_wrapper (field->type) : mono_marshal_get_ldfld_wrapper (field->type);
5809 MonoInst *iargs [4];
5813 NEW_CLASSCONST (cfg, iargs [1], klass);
5814 NEW_FIELDCONST (cfg, iargs [2], field);
5815 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
5816 if ((cfg->opt & MONO_OPT_INLINE) && !MONO_TYPE_ISSTRUCT (mono_method_signature (wrapper)->ret)) {
5817 costs = inline_method (cfg, wrapper, mono_method_signature (wrapper), bblock,
5818 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
5819 g_assert (costs > 0);
5824 GET_BBLOCK (cfg, bbhash, bblock, ip);
5825 ebblock->next_bb = bblock;
5826 link_bblock (cfg, ebblock, bblock);
5828 temp = iargs [0]->inst_i0->inst_c0;
5830 NEW_TEMPLOAD (cfg, *sp, temp);
5833 /* indicates start of a new block, and triggers a load of
5834 all stack arguments at bb boundarie */
5837 inline_costs += costs;
5840 temp = mono_emit_method_call_spilled (cfg, bblock, wrapper, mono_method_signature (wrapper), iargs, ip, NULL);
5841 NEW_TEMPLOAD (cfg, *sp, temp);
5845 NEW_ICONST (cfg, offset_ins, foffset);
5846 MONO_INST_NEW (cfg, ins, OP_PADD);
5848 ins->inst_left = *sp;
5849 ins->inst_right = offset_ins;
5850 ins->type = STACK_MP;
5852 if (*ip == CEE_LDFLDA) {
5853 ins->klass = mono_class_from_mono_type (field->type);
5857 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
5858 type_to_eval_stack_type (field->type, load);
5859 load->cil_code = ip;
5860 load->inst_left = ins;
5861 load->flags |= ins_flag;
5873 MonoClassField *field;
5874 gpointer addr = NULL;
5877 token = read32 (ip + 1);
5878 if (method->wrapper_type != MONO_WRAPPER_NONE) {
5879 field = mono_method_get_wrapper_data (method, token);
5880 klass = field->parent;
5883 field = mono_field_from_token (image, token, &klass, generic_context);
5886 mono_class_init (klass);
5888 g_assert (!(field->type->attrs & FIELD_ATTRIBUTE_LITERAL));
5890 if ((*ip) == CEE_STSFLD)
5891 handle_loaded_temps (cfg, bblock, stack_start, sp);
5893 /* The special_static_fields field is init'd in mono_class_vtable, so it needs
5894 * to be called here.
5896 if (!(cfg->opt & MONO_OPT_SHARED))
5897 mono_class_vtable (cfg->domain, klass);
5898 mono_domain_lock (cfg->domain);
5899 if (cfg->domain->special_static_fields)
5900 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
5901 mono_domain_unlock (cfg->domain);
5903 if ((cfg->opt & MONO_OPT_SHARED) || (cfg->compile_aot && addr)) {
5905 MonoInst *iargs [2];
5906 MonoInst *domain_var;
5908 g_assert (field->parent);
5909 /* avoid depending on undefined C behavior in sequence points */
5910 domain_var = mono_get_domainvar (cfg);
5911 NEW_TEMPLOAD (cfg, iargs [0], domain_var->inst_c0);
5912 NEW_FIELDCONST (cfg, iargs [1], field);
5913 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
5914 NEW_TEMPLOAD (cfg, ins, temp);
5917 vtable = mono_class_vtable (cfg->domain, klass);
5919 if (mini_field_access_needs_cctor_run (cfg, method, vtable) && !(g_slist_find (class_inits, vtable))) {
5920 guint8 *tramp = mono_create_class_init_trampoline (vtable);
5921 mono_emit_native_call (cfg, bblock, tramp,
5922 helper_sig_class_init_trampoline,
5923 NULL, ip, FALSE, FALSE);
5924 if (cfg->verbose_level > 2)
5925 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
5926 class_inits = g_slist_prepend (class_inits, vtable);
5928 if (cfg->run_cctors) {
5929 /* This makes so that inline cannot trigger */
5930 /* .cctors: too many apps depend on them */
5931 /* running with a specific order... */
5932 if (! vtable->initialized)
5934 mono_runtime_class_init (vtable);
5937 addr = (char*)vtable->data + field->offset;
5939 if (cfg->compile_aot)
5940 NEW_SFLDACONST (cfg, ins, field);
5942 NEW_PCONST (cfg, ins, addr);
5946 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
5947 * This could be later optimized to do just a couple of
5948 * memory dereferences with constant offsets.
5951 MonoInst *iargs [1];
5952 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
5953 temp = mono_emit_jit_icall (cfg, bblock, mono_get_special_static_data, iargs, ip);
5954 NEW_TEMPLOAD (cfg, ins, temp);
5958 /* FIXME: mark instructions for use in SSA */
5959 if (*ip == CEE_LDSFLDA) {
5960 ins->klass = mono_class_from_mono_type (field->type);
5962 } else if (*ip == CEE_STSFLD) {
5966 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
5967 store->cil_code = ip;
5968 store->inst_left = ins;
5969 store->inst_right = sp [0];
5970 store->flags |= ins_flag;
5973 if (store->opcode == CEE_STOBJ) {
5974 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE, FALSE);
5976 MONO_ADD_INS (bblock, store);
5978 gboolean is_const = FALSE;
5979 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
5980 if (!((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) &&
5981 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
5982 gpointer addr = (char*)vtable->data + field->offset;
5983 int ro_type = field->type->type;
5984 if (ro_type == MONO_TYPE_VALUETYPE && field->type->data.klass->enumtype) {
5985 ro_type = field->type->data.klass->enum_basetype->type;
5987 /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
5990 case MONO_TYPE_BOOLEAN:
5992 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
5996 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
5999 case MONO_TYPE_CHAR:
6001 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
6005 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
6010 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
6014 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
6017 #ifndef HAVE_MOVING_COLLECTOR
6020 case MONO_TYPE_STRING:
6021 case MONO_TYPE_OBJECT:
6022 case MONO_TYPE_CLASS:
6023 case MONO_TYPE_SZARRAY:
6025 case MONO_TYPE_FNPTR:
6026 case MONO_TYPE_ARRAY:
6027 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
6028 type_to_eval_stack_type (field->type, *sp);
6034 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
6035 sp [0]->type = STACK_I8;
6036 sp [0]->inst_l = *((gint64 *)addr);
6041 case MONO_TYPE_VALUETYPE:
6050 CHECK_STACK_OVF (1);
6051 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
6052 type_to_eval_stack_type (field->type, load);
6053 load->cil_code = ip;
6054 load->inst_left = ins;
6056 load->flags |= ins_flag;
6058 /* fixme: dont see the problem why this does not work */
6059 //cfg->disable_aot = TRUE;
6069 token = read32 (ip + 1);
6070 klass = mini_get_class (method, token, generic_context);
6073 n = mono_type_to_stind (&klass->byval_arg);
6074 if (n == CEE_STOBJ) {
6075 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE, TRUE);
6077 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
6079 MONO_INST_NEW (cfg, store, n);
6080 store->cil_code = ip;
6081 store->inst_left = sp [0];
6082 store->inst_right = sp [1];
6083 store->flags |= ins_flag;
6084 MONO_ADD_INS (bblock, store);
6096 token = read32 (ip + 1);
6097 klass = mini_get_class (method, token, generic_context);
6101 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
6106 if (klass == mono_defaults.void_class)
6108 if (target_type_is_incompatible (cfg, &klass->byval_arg, *sp))
6110 /* frequent check in generic code: box (struct), brtrue */
6111 if (!mono_class_is_nullable (klass) &&
6112 ip + 5 < end && ip_in_bb (cfg, bblock, ip + 5) && (ip [5] == CEE_BRTRUE || ip [5] == CEE_BRTRUE_S)) {
6113 /*g_print ("box-brtrue opt at 0x%04x in %s\n", real_offset, method->name);*/
6114 MONO_INST_NEW (cfg, ins, CEE_POP);
6115 MONO_ADD_INS (bblock, ins);
6119 MONO_INST_NEW (cfg, ins, CEE_BR);
6121 MONO_ADD_INS (bblock, ins);
6122 if (*ip == CEE_BRTRUE_S) {
6125 target = ip + 1 + (signed char)(*ip);
6130 target = ip + 4 + (gint)(read32 (ip));
6133 GET_BBLOCK (cfg, bbhash, tblock, target);
6134 link_bblock (cfg, bblock, tblock);
6135 CHECK_BBLOCK (target, ip, tblock);
6136 ins->inst_target_bb = tblock;
6137 if (sp != stack_start) {
6138 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
6140 CHECK_UNVERIFIABLE (cfg);
6142 start_new_bblock = 1;
6145 *sp++ = handle_box (cfg, bblock, val, ip, klass);
6152 MONO_INST_NEW (cfg, ins, *ip);
6157 token = read32 (ip + 1);
6159 /* allocate the domainvar - becaus this is used in decompose_foreach */
6160 if (cfg->opt & MONO_OPT_SHARED) {
6161 mono_get_domainvar (cfg);
6162 /* LAME-IR: Mark it as used since otherwise it will be optimized away */
6163 cfg->domainvar->flags |= MONO_INST_VOLATILE;
6167 mono_get_got_var (cfg);
6169 klass = mini_get_class (method, token, generic_context);
6172 ins->inst_newa_class = klass;
6173 ins->inst_newa_len = *sp;
6174 ins->type = STACK_OBJ;
6179 * we store the object so calls to create the array are not interleaved
6180 * with the arguments of other calls.
6183 MonoInst *store, *temp, *load;
6185 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
6186 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
6187 store->cil_code = ins->cil_code;
6188 MONO_ADD_INS (bblock, store);
6189 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
6190 load->cil_code = ins->cil_code;
6198 if (sp [0]->type != STACK_OBJ)
6200 MONO_INST_NEW (cfg, ins, *ip);
6201 ins->cil_code = ip++;
6202 ins->inst_left = *sp;
6203 ins->type = STACK_PTR;
6210 if (sp [0]->type != STACK_OBJ)
6213 klass = mini_get_class (method, read32 (ip + 1), generic_context);
6216 /* we need to make sure that this array is exactly the type it needs
6217 * to be for correctness. the wrappers are lax with their usage
6218 * so we need to ignore them here
6220 if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE) {
6222 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
6223 check->cil_code = ip;
6224 check->klass = klass;
6225 check->inst_left = sp [0];
6226 check->type = STACK_OBJ;
6227 check->klass = klass;
6231 mono_class_init (klass);
6232 NEW_LDELEMA (cfg, ins, sp, klass);
6237 case CEE_LDELEM_ANY: {
6241 if (sp [0]->type != STACK_OBJ)
6244 token = read32 (ip + 1);
6245 klass = mono_class_get_full (image, token, generic_context);
6248 mono_class_init (klass);
6249 NEW_LDELEMA (cfg, load, sp, klass);
6250 load->cil_code = ip;
6251 MONO_INST_NEW (cfg, ins, mono_type_to_ldind (&klass->byval_arg));
6253 ins->inst_left = load;
6255 type_to_eval_stack_type (&klass->byval_arg, ins);
6269 case CEE_LDELEM_REF: {
6273 * ldind.x (ldelema (array, index))
6274 * ldelema does the bounds check
6278 if (sp [0]->type != STACK_OBJ)
6280 klass = array_access_to_klass (*ip);
6281 NEW_LDELEMA (cfg, load, sp, klass);
6282 load->cil_code = ip;
6283 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
6285 ins->inst_left = load;
6287 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
6298 case CEE_STELEM_R8: {
6302 * stind.x (ldelema (array, index), val)
6303 * ldelema does the bounds check
6307 if (sp [0]->type != STACK_OBJ)
6309 klass = array_access_to_klass (*ip);
6310 NEW_LDELEMA (cfg, load, sp, klass);
6311 load->cil_code = ip;
6312 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
6314 ins->inst_left = load;
6315 ins->inst_right = sp [2];
6317 handle_loaded_temps (cfg, bblock, stack_start, sp);
6318 MONO_ADD_INS (bblock, ins);
6322 case CEE_STELEM_ANY: {
6326 * stind.x (ldelema (array, index), val)
6327 * ldelema does the bounds check
6331 if (sp [0]->type != STACK_OBJ)
6334 token = read32 (ip + 1);
6335 klass = mono_class_get_full (image, token, generic_context);
6338 mono_class_init (klass);
6339 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
6340 MonoMethod* helper = mono_marshal_get_stelemref ();
6341 MonoInst *iargs [3];
6342 handle_loaded_temps (cfg, bblock, stack_start, sp);
6348 mono_emit_method_call_spilled (cfg, bblock, helper, mono_method_signature (helper), iargs, ip, NULL);
6350 NEW_LDELEMA (cfg, load, sp, klass);
6351 load->cil_code = ip;
6353 n = mono_type_to_stind (&klass->byval_arg);
6355 handle_stobj (cfg, bblock, load, sp [2], ip, klass, FALSE, FALSE, TRUE);
6357 MONO_INST_NEW (cfg, ins, n);
6359 ins->inst_left = load;
6360 ins->inst_right = sp [2];
6361 handle_loaded_temps (cfg, bblock, stack_start, sp);
6362 MONO_ADD_INS (bblock, ins);
6369 case CEE_STELEM_REF: {
6370 MonoInst *iargs [3];
6371 MonoMethod* helper = mono_marshal_get_stelemref ();
6375 if (sp [0]->type != STACK_OBJ)
6377 if (sp [2]->type != STACK_OBJ)
6380 handle_loaded_temps (cfg, bblock, stack_start, sp);
6386 mono_emit_method_call_spilled (cfg, bblock, helper, mono_method_signature (helper), iargs, ip, NULL);
6390 NEW_GROUP (cfg, group, sp [0], sp [1]);
6391 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
6393 ins->inst_left = group;
6394 ins->inst_right = sp [2];
6395 MONO_ADD_INS (bblock, ins);
6402 case CEE_CKFINITE: {
6403 MonoInst *store, *temp;
6406 /* this instr. can throw exceptions as side effect,
6407 * so we cant eliminate dead code which contains CKFINITE opdodes.
6408 * Spilling to memory makes sure that we always perform
6412 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
6414 ins->inst_left = sp [-1];
6415 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
6417 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
6418 store->cil_code = ip;
6419 MONO_ADD_INS (bblock, store);
6421 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
6428 MONO_INST_NEW (cfg, ins, *ip);
6431 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
6434 mono_class_init (klass);
6435 ins->type = STACK_MP;
6436 ins->inst_left = *sp;
6438 ins->inst_newa_class = klass;
6443 case CEE_MKREFANY: {
6444 MonoInst *loc, *klassconst;
6447 MONO_INST_NEW (cfg, ins, *ip);
6450 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
6453 mono_class_init (klass);
6456 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
6457 NEW_TEMPLOADA (cfg, ins->inst_right, loc->inst_c0);
6459 NEW_PCONST (cfg, klassconst, klass);
6460 NEW_GROUP (cfg, ins->inst_left, *sp, klassconst);
6462 MONO_ADD_INS (bblock, ins);
6464 NEW_TEMPLOAD (cfg, *sp, loc->inst_c0);
6471 MonoClass *handle_class;
6473 CHECK_STACK_OVF (1);
6476 n = read32 (ip + 1);
6478 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
6479 handle = mono_method_get_wrapper_data (method, n);
6480 handle_class = mono_method_get_wrapper_data (method, n + 1);
6481 if (handle_class == mono_defaults.typehandle_class)
6482 handle = &((MonoClass*)handle)->byval_arg;
6485 handle = mono_ldtoken (image, n, &handle_class, generic_context);
6489 mono_class_init (handle_class);
6491 if (cfg->opt & MONO_OPT_SHARED) {
6493 MonoInst *res, *store, *addr, *vtvar, *iargs [3];
6495 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
6497 NEW_IMAGECONST (cfg, iargs [0], image);
6498 NEW_ICONST (cfg, iargs [1], n);
6499 NEW_PCONST (cfg, iargs [2], generic_context);
6500 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
6501 NEW_TEMPLOAD (cfg, res, temp);
6502 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
6503 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
6504 MONO_ADD_INS (bblock, store);
6505 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
6507 if ((ip [5] == CEE_CALL) && (cmethod = mini_get_method (method, read32 (ip + 6), NULL, generic_context)) &&
6508 (cmethod->klass == mono_defaults.monotype_class->parent) &&
6509 (strcmp (cmethod->name, "GetTypeFromHandle") == 0) && ip_in_bb (cfg, bblock, ip + 5)) {
6510 MonoClass *tclass = mono_class_from_mono_type (handle);
6511 mono_class_init (tclass);
6512 if (cfg->compile_aot)
6513 NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n);
6515 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
6516 ins->type = STACK_OBJ;
6517 ins->klass = cmethod->klass;
6520 MonoInst *store, *addr, *vtvar;
6522 if (cfg->compile_aot)
6523 NEW_LDTOKENCONST (cfg, ins, image, n);
6525 NEW_PCONST (cfg, ins, handle);
6526 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
6527 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
6528 NEW_INDSTORE (cfg, store, addr, ins, &mono_defaults.int_class->byval_arg);
6529 MONO_ADD_INS (bblock, store);
6530 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
6546 case CEE_ADD_OVF_UN:
6548 case CEE_MUL_OVF_UN:
6550 case CEE_SUB_OVF_UN:
6553 if (mono_find_jit_opcode_emulation (ins->opcode)) {
6555 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
6559 case CEE_ENDFINALLY:
6560 MONO_INST_NEW (cfg, ins, *ip);
6561 MONO_ADD_INS (bblock, ins);
6562 ins->cil_code = ip++;
6563 start_new_bblock = 1;
6566 * Control will leave the method so empty the stack, otherwise
6567 * the next basic block will start with a nonempty stack.
6569 while (sp != stack_start) {
6570 MONO_INST_NEW (cfg, ins, CEE_POP);
6574 MONO_ADD_INS (bblock, ins);
6581 if (*ip == CEE_LEAVE) {
6583 target = ip + 5 + (gint32)read32(ip + 1);
6586 target = ip + 2 + (signed char)(ip [1]);
6589 /* empty the stack */
6590 while (sp != stack_start) {
6591 MONO_INST_NEW (cfg, ins, CEE_POP);
6595 MONO_ADD_INS (bblock, ins);
6599 * If this leave statement is in a catch block, check for a
6600 * pending exception, and rethrow it if necessary.
6602 for (i = 0; i < header->num_clauses; ++i) {
6603 MonoExceptionClause *clause = &header->clauses [i];
6606 * Use <= in the final comparison to handle clauses with multiple
6607 * leave statements, like in bug #78024.
6608 * The ordering of the exception clauses guarantees that we find the
6611 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)) {
6615 NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, clause->handler_offset)->inst_c0);
6616 load->cil_code = ip;
6618 temp = mono_emit_jit_icall (cfg, bblock, mono_thread_get_pending_exception, NULL, ip);
6619 NEW_TEMPLOAD (cfg, *sp, temp);
6621 MONO_INST_NEW (cfg, ins, OP_THROW_OR_NULL);
6622 ins->inst_left = *sp;
6623 ins->inst_right = load;
6625 MONO_ADD_INS (bblock, ins);
6629 /* fixme: call fault handler ? */
6631 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
6633 for (tmp = handlers; tmp; tmp = tmp->next) {
6635 link_bblock (cfg, bblock, tblock);
6636 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
6638 ins->inst_target_bb = tblock;
6639 MONO_ADD_INS (bblock, ins);
6641 g_list_free (handlers);
6644 MONO_INST_NEW (cfg, ins, CEE_BR);
6646 MONO_ADD_INS (bblock, ins);
6647 GET_BBLOCK (cfg, bbhash, tblock, target);
6648 link_bblock (cfg, bblock, tblock);
6649 CHECK_BBLOCK (target, ip, tblock);
6650 ins->inst_target_bb = tblock;
6651 start_new_bblock = 1;
6653 if (*ip == CEE_LEAVE)
6662 MONO_INST_NEW (cfg, ins, *ip);
6664 handle_loaded_temps (cfg, bblock, stack_start, sp);
6665 MONO_ADD_INS (bblock, ins);
6666 ins->cil_code = ip++;
6667 ins->inst_i0 = sp [0];
6668 ins->inst_i1 = sp [1];
6676 /* trampoline mono specific opcodes */
6677 case MONO_CUSTOM_PREFIX: {
6679 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
6684 case CEE_MONO_ICALL: {
6687 MonoJitICallInfo *info;
6689 token = read32 (ip + 2);
6690 func = mono_method_get_wrapper_data (method, token);
6691 info = mono_find_jit_icall_by_addr (func);
6694 CHECK_STACK (info->sig->param_count);
6695 sp -= info->sig->param_count;
6697 temp = mono_emit_jit_icall (cfg, bblock, info->func, sp, ip);
6698 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
6699 NEW_TEMPLOAD (cfg, *sp, temp);
6704 inline_costs += 10 * num_calls++;
6708 case CEE_MONO_LDPTR:
6709 CHECK_STACK_OVF (1);
6711 token = read32 (ip + 2);
6712 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
6716 inline_costs += 10 * num_calls++;
6717 /* Can't embed random pointers into AOT code */
6718 cfg->disable_aot = 1;
6720 case CEE_MONO_VTADDR:
6723 MONO_INST_NEW (cfg, ins, OP_VTADDR);
6725 ins->type = STACK_MP;
6726 ins->inst_left = *sp;
6730 case CEE_MONO_NEWOBJ: {
6731 MonoInst *iargs [2];
6733 CHECK_STACK_OVF (1);
6735 token = read32 (ip + 2);
6736 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
6737 mono_class_init (klass);
6738 NEW_DOMAINCONST (cfg, iargs [0]);
6739 NEW_CLASSCONST (cfg, iargs [1], klass);
6740 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
6741 NEW_TEMPLOAD (cfg, *sp, temp);
6744 inline_costs += 10 * num_calls++;
6747 case CEE_MONO_OBJADDR:
6750 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
6752 ins->type = STACK_MP;
6753 ins->inst_left = *sp;
6757 case CEE_MONO_LDNATIVEOBJ:
6760 token = read32 (ip + 2);
6761 klass = mono_method_get_wrapper_data (method, token);
6762 g_assert (klass->valuetype);
6763 mono_class_init (klass);
6764 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
6768 case CEE_MONO_RETOBJ:
6769 g_assert (cfg->ret);
6770 g_assert (mono_method_signature (method)->pinvoke);
6775 token = read32 (ip + 2);
6776 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
6778 NEW_RETLOADA (cfg, ins);
6779 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE, FALSE);
6781 if (sp != stack_start)
6784 MONO_INST_NEW (cfg, ins, CEE_BR);
6786 ins->inst_target_bb = end_bblock;
6787 MONO_ADD_INS (bblock, ins);
6788 link_bblock (cfg, bblock, end_bblock);
6789 start_new_bblock = 1;
6792 case CEE_MONO_CISINST:
6793 case CEE_MONO_CCASTCLASS: {
6798 token = read32 (ip + 2);
6799 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
6800 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_CISINST) ? OP_CISINST : OP_CCASTCLASS);
6801 ins->type = STACK_I4;
6802 ins->inst_left = *sp;
6803 ins->inst_newa_class = klass;
6805 *sp++ = emit_tree (cfg, bblock, ins, ip + 6);
6809 case CEE_MONO_SAVE_LMF:
6810 case CEE_MONO_RESTORE_LMF:
6811 #ifdef MONO_ARCH_HAVE_LMF_OPS
6812 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_SAVE_LMF) ? OP_SAVE_LMF : OP_RESTORE_LMF);
6813 MONO_ADD_INS (bblock, ins);
6814 cfg->need_lmf_area = TRUE;
6818 case CEE_MONO_CLASSCONST:
6819 CHECK_STACK_OVF (1);
6821 token = read32 (ip + 2);
6822 NEW_CLASSCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
6826 inline_costs += 10 * num_calls++;
6828 case CEE_MONO_NOT_TAKEN:
6829 bblock->out_of_line = TRUE;
6833 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
6842 /* somewhat similar to LDTOKEN */
6843 MonoInst *addr, *vtvar;
6844 CHECK_STACK_OVF (1);
6845 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
6847 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
6848 addr->cil_code = ip;
6849 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
6851 ins->inst_left = addr;
6852 MONO_ADD_INS (bblock, ins);
6853 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
6867 * The following transforms:
6868 * CEE_CEQ into OP_CEQ
6869 * CEE_CGT into OP_CGT
6870 * CEE_CGT_UN into OP_CGT_UN
6871 * CEE_CLT into OP_CLT
6872 * CEE_CLT_UN into OP_CLT_UN
6874 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
6876 MONO_INST_NEW (cfg, ins, cmp->opcode);
6878 cmp->inst_i0 = sp [0];
6879 cmp->inst_i1 = sp [1];
6883 if ((sp [0]->type == STACK_I8) || ((sizeof (gpointer) == 8) && ((sp [0]->type == STACK_PTR) || (sp [0]->type == STACK_OBJ) || (sp [0]->type == STACK_MP))))
6884 cmp->opcode = OP_LCOMPARE;
6886 cmp->opcode = OP_COMPARE;
6888 ins->type = STACK_I4;
6891 /* spill it to reduce the expression complexity
6892 * and workaround bug 54209
6894 if (cmp->inst_left->type == STACK_I8) {
6896 *sp++ = emit_tree (cfg, bblock, ins, ip + 2);
6905 CHECK_STACK_OVF (1);
6907 n = read32 (ip + 2);
6908 cmethod = mini_get_method (method, n, NULL, generic_context);
6911 mono_class_init (cmethod->klass);
6913 if (mono_use_security_manager) {
6914 if (check_linkdemand (cfg, method, cmethod, bblock, ip))
6918 handle_loaded_temps (cfg, bblock, stack_start, sp);
6920 NEW_METHODCONST (cfg, argconst, cmethod);
6921 if (method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED)
6922 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
6924 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn_nosync, &argconst, ip);
6925 NEW_TEMPLOAD (cfg, *sp, temp);
6929 inline_costs += 10 * num_calls++;
6932 case CEE_LDVIRTFTN: {
6938 n = read32 (ip + 2);
6939 cmethod = mini_get_method (method, n, NULL, generic_context);
6942 mono_class_init (cmethod->klass);
6944 if (mono_use_security_manager) {
6945 if (check_linkdemand (cfg, method, cmethod, bblock, ip))
6949 handle_loaded_temps (cfg, bblock, stack_start, sp);
6953 NEW_METHODCONST (cfg, args [1], cmethod);
6954 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
6955 NEW_TEMPLOAD (cfg, *sp, temp);
6959 inline_costs += 10 * num_calls++;
6963 CHECK_STACK_OVF (1);
6965 n = read16 (ip + 2);
6967 NEW_ARGLOAD (cfg, ins, n);
6973 CHECK_STACK_OVF (1);
6975 n = read16 (ip + 2);
6977 NEW_ARGLOADA (cfg, ins, n);
6985 handle_loaded_temps (cfg, bblock, stack_start, sp);
6987 n = read16 (ip + 2);
6989 NEW_ARGSTORE (cfg, ins, n, *sp);
6991 if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [n], *sp))
6993 if (ins->opcode == CEE_STOBJ) {
6994 NEW_ARGLOADA (cfg, ins, n);
6995 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
6997 MONO_ADD_INS (bblock, ins);
7001 CHECK_STACK_OVF (1);
7003 n = read16 (ip + 2);
7005 NEW_LOCLOAD (cfg, ins, n);
7011 CHECK_STACK_OVF (1);
7013 n = read16 (ip + 2);
7015 NEW_LOCLOADA (cfg, ins, n);
7024 n = read16 (ip + 2);
7026 handle_loaded_temps (cfg, bblock, stack_start, sp);
7027 NEW_LOCSTORE (cfg, ins, n, *sp);
7028 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
7031 if (ins->opcode == CEE_STOBJ) {
7032 NEW_LOCLOADA (cfg, ins, n);
7033 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
7035 MONO_ADD_INS (bblock, ins);
7042 if (sp != stack_start)
7044 if (cfg->method != method)
7046 * Inlining this into a loop in a parent could lead to
7047 * stack overflows which is different behavior than the
7048 * non-inlined case, thus disable inlining in this case.
7050 goto inline_failure;
7051 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
7052 ins->inst_left = *sp;
7054 ins->type = STACK_PTR;
7056 cfg->flags |= MONO_CFG_HAS_ALLOCA;
7057 if (header->init_locals)
7058 ins->flags |= MONO_INST_INIT;
7062 /* FIXME: set init flag if locals init is set in this method */
7064 case CEE_ENDFILTER: {
7065 MonoExceptionClause *clause, *nearest;
7066 int cc, nearest_num;
7070 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
7072 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
7073 ins->inst_left = *sp;
7075 MONO_ADD_INS (bblock, ins);
7076 start_new_bblock = 1;
7081 for (cc = 0; cc < header->num_clauses; ++cc) {
7082 clause = &header->clauses [cc];
7083 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
7084 ((ip - header->code) > clause->data.filter_offset && (ip - header->code) <= clause->handler_offset) &&
7085 (!nearest || (clause->data.filter_offset < nearest->data.filter_offset))) {
7091 if ((ip - header->code) != nearest->handler_offset)
7096 case CEE_UNALIGNED_:
7097 ins_flag |= MONO_INST_UNALIGNED;
7098 /* FIXME: record alignment? we can assume 1 for now */
7103 ins_flag |= MONO_INST_VOLATILE;
7107 ins_flag |= MONO_INST_TAILCALL;
7108 cfg->flags |= MONO_CFG_HAS_TAIL;
7109 /* Can't inline tail calls at this time */
7110 inline_costs += 100000;
7117 token = read32 (ip + 2);
7118 klass = mini_get_class (method, token, generic_context);
7121 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
7122 MonoInst *store, *load;
7123 NEW_PCONST (cfg, load, NULL);
7124 load->cil_code = ip;
7125 load->type = STACK_OBJ;
7126 load->klass = klass;
7127 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
7128 store->cil_code = ip;
7129 handle_loaded_temps (cfg, bblock, stack_start, sp);
7130 MONO_ADD_INS (bblock, store);
7131 store->inst_i0 = sp [0];
7132 store->inst_i1 = load;
7134 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
7139 case CEE_CONSTRAINED_:
7140 /* FIXME: implement */
7142 token = read32 (ip + 2);
7143 constrained_call = mono_class_get_full (image, token, generic_context);
7144 if (!constrained_call)
7150 MonoInst *iargs [3];
7153 if ((cfg->opt & MONO_OPT_INTRINS) && (ip [1] == CEE_CPBLK) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5)) {
7155 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
7156 copy->inst_left = sp [0];
7157 copy->inst_right = sp [1];
7158 copy->cil_code = ip;
7160 MONO_ADD_INS (bblock, copy);
7167 handle_loaded_temps (cfg, bblock, stack_start, sp);
7168 if (ip [1] == CEE_CPBLK) {
7169 MonoMethod *memcpy_method = get_memcpy_method ();
7170 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
7172 MonoMethod *memset_method = get_memset_method ();
7173 mono_emit_method_call_spilled (cfg, bblock, memset_method, memset_method->signature, iargs, ip, NULL);
7182 ins_flag |= MONO_INST_NOTYPECHECK;
7184 ins_flag |= MONO_INST_NORANGECHECK;
7185 /* we ignore the no-nullcheck for now since we
7186 * really do it explicitly only when doing callvirt->call
7192 int handler_offset = -1;
7194 for (i = 0; i < header->num_clauses; ++i) {
7195 MonoExceptionClause *clause = &header->clauses [i];
7196 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code) && !(clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY))
7197 handler_offset = clause->handler_offset;
7200 bblock->flags |= BB_EXCEPTION_UNSAFE;
7202 g_assert (handler_offset != -1);
7204 NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, handler_offset)->inst_c0);
7205 load->cil_code = ip;
7206 MONO_INST_NEW (cfg, ins, OP_RETHROW);
7207 ins->inst_left = load;
7209 MONO_ADD_INS (bblock, ins);
7211 link_bblock (cfg, bblock, end_bblock);
7212 start_new_bblock = 1;
7217 CHECK_STACK_OVF (1);
7219 token = read32 (ip + 2);
7220 /* FIXXME: handle generics. */
7221 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
7222 MonoType *type = mono_type_create_from_typespec (image, token);
7223 token = mono_type_size (type, &ialign);
7225 MonoClass *klass = mono_class_get_full (image, token, generic_context);
7228 mono_class_init (klass);
7229 token = mono_class_value_size (klass, &align);
7231 NEW_ICONST (cfg, ins, token);
7236 case CEE_REFANYTYPE:
7238 MONO_INST_NEW (cfg, ins, OP_REFANYTYPE);
7240 ins->type = STACK_MP;
7241 ins->inst_left = *sp;
7242 ins->type = STACK_VTYPE;
7243 ins->klass = mono_defaults.typehandle_class;
7252 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
7257 g_error ("opcode 0x%02x not handled", *ip);
7260 if (start_new_bblock != 1)
7263 bblock->cil_length = ip - bblock->cil_code;
7264 bblock->next_bb = end_bblock;
7266 if (cfg->method == method && cfg->domainvar) {
7268 MonoInst *get_domain;
7270 if (! (get_domain = mono_arch_get_domain_intrinsic (cfg))) {
7273 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
7274 call->signature = helper_sig_domain_get;
7275 call->inst.type = STACK_PTR;
7276 call->fptr = mono_domain_get;
7277 get_domain = (MonoInst*)call;
7280 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, get_domain);
7281 MONO_ADD_INS (init_localsbb, store);
7284 if (cfg->method == method && cfg->got_var)
7285 mono_emit_load_got_addr (cfg);
7287 if (header->init_locals) {
7289 for (i = 0; i < header->num_locals; ++i) {
7290 MonoType *ptype = header->locals [i];
7291 int t = ptype->type;
7292 if (t == MONO_TYPE_VALUETYPE && ptype->data.klass->enumtype)
7293 t = ptype->data.klass->enum_basetype->type;
7295 NEW_PCONST (cfg, ins, NULL);
7296 NEW_LOCSTORE (cfg, store, i, ins);
7297 MONO_ADD_INS (init_localsbb, store);
7298 } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
7299 NEW_ICONST (cfg, ins, 0);
7300 NEW_LOCSTORE (cfg, store, i, ins);
7301 MONO_ADD_INS (init_localsbb, store);
7302 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
7303 MONO_INST_NEW (cfg, ins, OP_I8CONST);
7304 ins->type = STACK_I8;
7306 NEW_LOCSTORE (cfg, store, i, ins);
7307 MONO_ADD_INS (init_localsbb, store);
7308 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
7309 MONO_INST_NEW (cfg, ins, OP_R8CONST);
7310 ins->type = STACK_R8;
7311 ins->inst_p0 = (void*)&r8_0;
7312 NEW_LOCSTORE (cfg, store, i, ins);
7313 MONO_ADD_INS (init_localsbb, store);
7314 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
7315 ((t == MONO_TYPE_GENERICINST) && mono_type_generic_inst_is_valuetype (ptype))) {
7316 NEW_LOCLOADA (cfg, ins, i);
7317 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (ptype), NULL, NULL);
7319 NEW_PCONST (cfg, ins, NULL);
7320 NEW_LOCSTORE (cfg, store, i, ins);
7321 MONO_ADD_INS (init_localsbb, store);
7326 /* resolve backward branches in the middle of an existing basic block */
7327 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
7329 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
7330 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
7331 if (tblock != start_bblock) {
7333 split_bblock (cfg, tblock, bblock);
7334 l = bblock->cil_code - header->code;
7335 bblock->cil_length = tblock->cil_length - l;
7336 tblock->cil_length = l;
7338 g_print ("recheck failed.\n");
7342 if (cfg->method == method) {
7344 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7345 bb->region = mono_find_block_region (cfg, bb->real_offset);
7347 mono_create_spvar_for_region (cfg, bb->region);
7348 if (cfg->verbose_level > 2)
7349 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
7352 g_hash_table_destroy (bbhash);
7355 g_slist_free (class_inits);
7356 dont_inline = g_list_remove (dont_inline, method);
7358 if (inline_costs < 0) {
7361 /* Method is too large */
7362 mname = mono_method_full_name (method, TRUE);
7363 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
7364 cfg->exception_message = g_strdup_printf ("Method %s is too complex.", mname);
7369 return inline_costs;
7372 if (cfg->method != method)
7373 g_hash_table_destroy (bbhash);
7374 g_slist_free (class_inits);
7375 dont_inline = g_list_remove (dont_inline, method);
7379 if (cfg->method != method)
7380 g_hash_table_destroy (bbhash);
7381 g_slist_free (class_inits);
7382 dont_inline = g_list_remove (dont_inline, method);
7383 cfg->exception_type = MONO_EXCEPTION_TYPE_LOAD;
7387 if (cfg->method != method)
7388 g_hash_table_destroy (bbhash);
7389 g_slist_free (class_inits);
7390 dont_inline = g_list_remove (dont_inline, method);
7391 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
7392 cfg->exception_message = g_strdup_printf ("Invalid IL code in %s: %s\n",
7393 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
7398 mono_print_tree (MonoInst *tree) {
7404 arity = mono_burg_arity [tree->opcode];
7406 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
7408 switch (tree->opcode) {
7410 printf ("[%d]", (int)tree->inst_c0);
7413 printf ("[%lld]", (long long)tree->inst_l);
7416 printf ("[%f]", *(double*)tree->inst_p0);
7419 printf ("[%f]", *(float*)tree->inst_p0);
7423 printf ("[%d]", (int)tree->inst_c0);
7426 if (tree->inst_offset < 0)
7427 printf ("[-0x%x(%s)]", (int)(-tree->inst_offset), mono_arch_regname (tree->inst_basereg));
7429 printf ("[0x%x(%s)]", (int)(tree->inst_offset), mono_arch_regname (tree->inst_basereg));
7432 printf ("[%s]", mono_arch_regname (tree->dreg));
7435 printf ("[%s]", tree->inst_newa_class->name);
7436 mono_print_tree (tree->inst_newa_len);
7447 case OP_VOIDCALLVIRT: {
7448 MonoCallInst *call = (MonoCallInst*)tree;
7450 printf ("[%s]", call->method->name);
7451 else if (call->fptr) {
7452 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (call->fptr);
7454 printf ("[%s]", info->name);
7460 printf ("[%d (", (int)tree->inst_c0);
7461 for (i = 0; i < tree->inst_phi_args [0]; i++) {
7464 printf ("%d", tree->inst_phi_args [i + 1]);
7475 case OP_LOAD_MEMBASE:
7476 case OP_LOADI4_MEMBASE:
7477 case OP_LOADU4_MEMBASE:
7478 case OP_LOADU1_MEMBASE:
7479 case OP_LOADI1_MEMBASE:
7480 case OP_LOADU2_MEMBASE:
7481 case OP_LOADI2_MEMBASE:
7482 printf ("[%s] <- [%s + 0x%x]", mono_arch_regname (tree->dreg), mono_arch_regname (tree->inst_basereg), (int)tree->inst_offset);
7485 case OP_CALL_HANDLER:
7486 printf ("[B%d]", tree->inst_target_bb->block_num);
7496 case OP_VOIDCALL_REG:
7497 mono_print_tree (tree->inst_left);
7509 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
7510 mono_print_tree (tree->inst_left);
7513 if (!mono_arch_print_tree(tree, arity)) {
7515 mono_print_tree (tree->inst_left);
7517 mono_print_tree (tree->inst_right);
7528 mono_print_tree_nl (MonoInst *tree)
7530 mono_print_tree (tree);
7535 create_helper_signature (void)
7537 helper_sig_domain_get = mono_create_icall_signature ("ptr");
7538 helper_sig_class_init_trampoline = mono_create_icall_signature ("void");
7542 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
7545 MonoMethod *wrapper;
7547 if (callinfo->wrapper) {
7548 return callinfo->wrapper;
7551 if (callinfo->trampoline)
7552 return callinfo->trampoline;
7554 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
7555 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
7558 callinfo->trampoline = mono_create_ftnptr (mono_get_root_domain (), mono_create_jit_trampoline_in_domain (mono_get_root_domain (), wrapper));
7559 mono_register_jit_icall_wrapper (callinfo, callinfo->trampoline);
7561 return callinfo->trampoline;
7565 mono_init_trampolines (void)
7567 mono_trampoline_code [MONO_TRAMPOLINE_GENERIC] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_GENERIC);
7568 mono_trampoline_code [MONO_TRAMPOLINE_JUMP] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_JUMP);
7569 mono_trampoline_code [MONO_TRAMPOLINE_CLASS_INIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_CLASS_INIT);
7570 #ifdef MONO_ARCH_HAVE_PIC_AOT
7571 mono_trampoline_code [MONO_TRAMPOLINE_AOT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_AOT);
7572 mono_trampoline_code [MONO_TRAMPOLINE_AOT_PLT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_AOT_PLT);
7574 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
7575 mono_trampoline_code [MONO_TRAMPOLINE_DELEGATE] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_DELEGATE);
7580 mono_init_exceptions (void)
7582 #ifndef CUSTOM_EXCEPTION_HANDLING
7583 mono_arch_get_restore_context ();
7584 mono_arch_get_call_filter ();
7585 mono_arch_get_throw_exception ();
7586 mono_arch_get_rethrow_exception ();
7591 mono_get_trampoline_code (MonoTrampolineType tramp_type)
7593 return mono_trampoline_code [tramp_type];
7597 mono_create_class_init_trampoline (MonoVTable *vtable)
7601 /* previously created trampoline code */
7602 mono_domain_lock (vtable->domain);
7604 g_hash_table_lookup (vtable->domain->class_init_trampoline_hash,
7606 mono_domain_unlock (vtable->domain);
7610 #ifdef MONO_ARCH_HAVE_CREATE_SPECIFIC_TRAMPOLINE
7611 code = mono_arch_create_specific_trampoline (vtable, MONO_TRAMPOLINE_CLASS_INIT, vtable->domain, NULL);
7613 code = mono_arch_create_class_init_trampoline (vtable);
7616 ptr = mono_create_ftnptr (vtable->domain, code);
7618 /* store trampoline address */
7619 mono_domain_lock (vtable->domain);
7620 g_hash_table_insert (vtable->domain->class_init_trampoline_hash,
7622 mono_domain_unlock (vtable->domain);
7625 if (!class_init_hash_addr)
7626 class_init_hash_addr = g_hash_table_new (NULL, NULL);
7627 g_hash_table_insert (class_init_hash_addr, ptr, vtable);
7634 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method,
7635 gboolean add_sync_wrapper)
7639 #ifdef MONO_ARCH_HAVE_CREATE_SPECIFIC_TRAMPOLINE
7643 if (add_sync_wrapper && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
7644 return mono_create_jump_trampoline (domain, mono_marshal_get_synchronized_wrapper (method), FALSE);
7646 code = mono_jit_find_compiled_method (domain, method);
7650 mono_domain_lock (domain);
7651 code = g_hash_table_lookup (domain->jump_trampoline_hash, method);
7652 mono_domain_unlock (domain);
7656 #ifdef MONO_ARCH_HAVE_CREATE_SPECIFIC_TRAMPOLINE
7657 code = mono_arch_create_specific_trampoline (method, MONO_TRAMPOLINE_JUMP, mono_domain_get (), &code_size);
7659 mono_domain_lock (domain);
7660 ji = mono_mempool_alloc0 (domain->mp, sizeof (MonoJitInfo));
7661 mono_domain_unlock (domain);
7662 ji->code_start = code;
7663 ji->code_size = code_size;
7664 ji->method = method;
7666 ji = mono_arch_create_jump_trampoline (method);
7670 * mono_delegate_ctor needs to find the method metadata from the
7671 * trampoline address, so we save it here.
7674 mono_jit_info_table_add (domain, ji);
7676 mono_domain_lock (domain);
7677 g_hash_table_insert (domain->jump_trampoline_hash, method, ji->code_start);
7678 mono_domain_unlock (domain);
7680 return ji->code_start;
7684 mono_create_jit_trampoline_in_domain (MonoDomain *domain, MonoMethod *method)
7688 mono_domain_lock (domain);
7689 tramp = g_hash_table_lookup (domain->jit_trampoline_hash, method);
7690 mono_domain_unlock (domain);
7694 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
7695 return mono_create_jit_trampoline (mono_marshal_get_synchronized_wrapper (method));
7697 #ifdef MONO_ARCH_HAVE_CREATE_SPECIFIC_TRAMPOLINE
7698 tramp = mono_arch_create_specific_trampoline (method, MONO_TRAMPOLINE_GENERIC, domain, NULL);
7700 tramp = mono_arch_create_jit_trampoline (method);
7703 mono_domain_lock (domain);
7704 g_hash_table_insert (domain->jit_trampoline_hash, method, tramp);
7705 mono_domain_unlock (domain);
7707 mono_jit_stats.method_trampolines++;
7713 mono_create_jit_trampoline (MonoMethod *method)
7715 return mono_create_jit_trampoline_in_domain (mono_domain_get (), method);
7718 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
7720 mono_create_jit_trampoline_from_token (MonoImage *image, guint32 token)
7724 MonoDomain *domain = mono_domain_get ();
7725 guint8 *buf, *start;
7727 mono_domain_lock (domain);
7728 buf = start = mono_code_manager_reserve (domain->code_mp, 2 * sizeof (gpointer));
7729 mono_domain_unlock (domain);
7731 *(gpointer*)(gpointer)buf = image;
7732 buf += sizeof (gpointer);
7733 *(guint32*)(gpointer)buf = token;
7735 tramp = mono_arch_create_specific_trampoline (start, MONO_TRAMPOLINE_AOT, domain, NULL);
7737 mono_jit_stats.method_trampolines++;
7744 mono_create_delegate_trampoline (MonoMethod *method, gpointer addr)
7746 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
7749 MonoDomain *domain = mono_domain_get ();
7752 code = mono_jit_find_compiled_method (domain, method);
7757 * FIXME: We should return a function descriptor here but it is not stored
7758 * anywhere so it would be leaked.
7762 mono_domain_lock (domain);
7763 ptr = g_hash_table_lookup (domain->delegate_trampoline_hash, method);
7764 mono_domain_unlock (domain);
7768 code = mono_arch_create_specific_trampoline (method, MONO_TRAMPOLINE_DELEGATE, domain, &code_size);
7770 ptr = mono_create_ftnptr (domain, code);
7772 /* store trampoline address */
7773 mono_domain_lock (domain);
7774 g_hash_table_insert (domain->delegate_trampoline_hash,
7776 mono_domain_unlock (domain);
7785 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
7790 if (class_init_hash_addr)
7791 res = g_hash_table_lookup (class_init_hash_addr, addr);
7799 mono_dynamic_code_hash_insert (MonoDomain *domain, MonoMethod *method, MonoJitDynamicMethodInfo *ji)
7801 if (!domain->dynamic_code_hash)
7802 domain->dynamic_code_hash = g_hash_table_new (NULL, NULL);
7803 g_hash_table_insert (domain->dynamic_code_hash, method, ji);
7806 static MonoJitDynamicMethodInfo*
7807 mono_dynamic_code_hash_lookup (MonoDomain *domain, MonoMethod *method)
7809 MonoJitDynamicMethodInfo *res;
7811 if (domain->dynamic_code_hash)
7812 res = g_hash_table_lookup (domain->dynamic_code_hash, method);
7825 * mono_allocate_stack_slots_full:
7827 * Allocate stack slots for all non register allocated variables using a
7828 * linear scan algorithm.
7829 * Returns: an array of stack offsets which the caller should free.
7830 * STACK_SIZE is set to the amount of stack space needed.
7831 * STACK_ALIGN is set to the alignment needed by the locals area.
7834 mono_allocate_stack_slots_full (MonoCompile *m, gboolean backward, guint32 *stack_size, guint32 *stack_align)
7836 int i, slot, offset, size;
7841 GList *vars = NULL, *l;
7842 StackSlotInfo *scalar_stack_slots, *vtype_stack_slots, *slot_info;
7846 scalar_stack_slots = g_new0 (StackSlotInfo, MONO_TYPE_PINNED);
7847 vtype_stack_slots = g_new0 (StackSlotInfo, 256);
7850 offsets = g_new (gint32, m->num_varinfo);
7851 for (i = 0; i < m->num_varinfo; ++i)
7854 for (i = m->locals_start; i < m->num_varinfo; i++) {
7855 inst = m->varinfo [i];
7856 vmv = MONO_VARINFO (m, i);
7858 if ((inst->flags & MONO_INST_IS_DEAD) || inst->opcode == OP_REGVAR || inst->opcode == OP_REGOFFSET)
7861 vars = g_list_prepend (vars, vmv);
7864 vars = mono_varlist_sort (m, vars, 0);
7867 for (l = vars; l; l = l->next) {
7869 inst = m->varinfo [vmv->idx];
7871 /* inst->unused indicates native sized value types, this is used by the
7872 * pinvoke wrappers when they call functions returning structures */
7873 if (inst->unused && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
7874 size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
7878 size = mono_type_size (inst->inst_vtype, &ialign);
7882 t = mono_type_get_underlying_type (inst->inst_vtype);
7884 case MONO_TYPE_GENERICINST:
7885 if (!mono_type_generic_inst_is_valuetype (t)) {
7886 slot_info = &scalar_stack_slots [t->type];
7890 case MONO_TYPE_VALUETYPE:
7891 for (i = 0; i < nvtypes; ++i)
7892 if (t->data.klass == vtype_stack_slots [i].vtype)
7895 slot_info = &vtype_stack_slots [i];
7897 g_assert (nvtypes < 256);
7898 vtype_stack_slots [nvtypes].vtype = t->data.klass;
7899 slot_info = &vtype_stack_slots [nvtypes];
7904 slot_info = &scalar_stack_slots [t->type];
7908 if (m->comp_done & MONO_COMP_LIVENESS) {
7909 //printf ("START %2d %08x %08x\n", vmv->idx, vmv->range.first_use.abs_pos, vmv->range.last_use.abs_pos);
7911 /* expire old intervals in active */
7912 while (slot_info->active) {
7913 MonoMethodVar *amv = (MonoMethodVar *)slot_info->active->data;
7915 if (amv->range.last_use.abs_pos > vmv->range.first_use.abs_pos)
7918 //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);
7920 slot_info->active = g_list_delete_link (slot_info->active, slot_info->active);
7921 slot_info->slots = g_list_prepend (slot_info->slots, GINT_TO_POINTER (offsets [amv->idx]));
7925 * This also handles the case when the variable is used in an
7926 * exception region, as liveness info is not computed there.
7929 * FIXME: All valuetypes are marked as INDIRECT because of LDADDR
7932 if (! (inst->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
7933 if (slot_info->slots) {
7934 slot = GPOINTER_TO_INT (slot_info->slots->data);
7936 slot_info->slots = g_list_delete_link (slot_info->slots, slot_info->slots);
7939 slot_info->active = mono_varlist_insert_sorted (m, slot_info->active, vmv, TRUE);
7944 static int count = 0;
7948 if (count == atoi (getenv ("COUNT")))
7949 printf ("LAST: %s\n", mono_method_full_name (m->method, TRUE));
7950 if (count > atoi (getenv ("COUNT")))
7953 mono_print_tree_nl (inst);
7957 if (slot == 0xffffff) {
7959 * Allways allocate valuetypes to sizeof (gpointer) to allow more
7960 * efficient copying (and to work around the fact that OP_MEMCPY
7961 * and OP_MEMSET ignores alignment).
7963 if (MONO_TYPE_ISSTRUCT (t))
7964 align = sizeof (gpointer);
7968 offset += align - 1;
7969 offset &= ~(align - 1);
7973 offset += align - 1;
7974 offset &= ~(align - 1);
7979 if (*stack_align == 0)
7980 *stack_align = align;
7983 offsets [vmv->idx] = slot;
7986 for (i = 0; i < MONO_TYPE_PINNED; ++i) {
7987 g_list_free (scalar_stack_slots [i].active);
7988 g_list_free (scalar_stack_slots [i].slots);
7990 for (i = 0; i < nvtypes; ++i) {
7991 g_list_free (vtype_stack_slots [i].active);
7992 g_list_free (vtype_stack_slots [i].slots);
7994 g_free (scalar_stack_slots);
7995 g_free (vtype_stack_slots);
7997 *stack_size = offset;
8002 mono_allocate_stack_slots (MonoCompile *m, guint32 *stack_size, guint32 *stack_align)
8004 return mono_allocate_stack_slots_full (m, TRUE, stack_size, stack_align);
8008 mono_register_opcode_emulation (int opcode, const char *name, const char *sigstr, gpointer func, gboolean no_throw)
8010 MonoJitICallInfo *info;
8011 MonoMethodSignature *sig = mono_create_icall_signature (sigstr);
8013 if (!emul_opcode_map)
8014 emul_opcode_map = g_new0 (MonoJitICallInfo*, OP_LAST + 1);
8016 g_assert (!sig->hasthis);
8017 g_assert (sig->param_count < 3);
8019 info = mono_register_jit_icall (func, name, sig, no_throw);
8021 emul_opcode_map [opcode] = info;
8025 register_icall (gpointer func, const char *name, const char *sigstr, gboolean save)
8027 MonoMethodSignature *sig;
8030 sig = mono_create_icall_signature (sigstr);
8034 mono_register_jit_icall (func, name, sig, save);
8038 decompose_foreach (MonoInst *tree, gpointer data)
8040 static MonoJitICallInfo *newarr_info = NULL;
8041 static MonoJitICallInfo *newarr_specific_info = NULL;
8042 MonoJitICallInfo *info;
8045 switch (tree->opcode) {
8047 MonoCompile *cfg = data;
8048 MonoInst *iargs [3];
8051 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
8052 g_assert (newarr_info);
8053 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
8054 g_assert (newarr_specific_info);
8057 if (cfg->opt & MONO_OPT_SHARED) {
8058 NEW_DOMAINCONST (cfg, iargs [0]);
8059 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
8060 iargs [2] = tree->inst_newa_len;
8065 MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
8067 NEW_VTABLECONST (cfg, iargs [0], vtable);
8068 iargs [1] = tree->inst_newa_len;
8070 info = newarr_specific_info;
8073 mono_emulate_opcode (cfg, tree, iargs, info);
8075 /* Need to decompose arguments after the the opcode is decomposed */
8076 for (i = 0; i < info->sig->param_count; ++i)
8077 dec_foreach (iargs [i], cfg);
8087 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
8089 switch (mono_burg_arity [tree->opcode]) {
8092 mono_inst_foreach (tree->inst_left, func, data);
8095 mono_inst_foreach (tree->inst_left, func, data);
8096 mono_inst_foreach (tree->inst_right, func, data);
8099 g_assert_not_reached ();
8106 mono_print_bb_code (MonoBasicBlock *bb) {
8108 MonoInst *c = bb->code;
8110 mono_print_tree (c);
8118 print_dfn (MonoCompile *cfg) {
8123 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
8125 for (i = 0; i < cfg->num_bblocks; ++i) {
8126 bb = cfg->bblocks [i];
8127 /*if (bb->cil_code) {
8128 char* code1, *code2;
8129 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
8130 if (bb->last_ins->cil_code)
8131 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
8133 code2 = g_strdup ("");
8135 code1 [strlen (code1) - 1] = 0;
8136 code = g_strdup_printf ("%s -> %s", code1, code2);
8140 code = g_strdup ("\n");
8141 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
8143 MonoInst *c = bb->code;
8145 mono_print_tree (c);
8153 g_print ("\tprev:");
8154 for (j = 0; j < bb->in_count; ++j) {
8155 g_print (" BB%d", bb->in_bb [j]->block_num);
8157 g_print ("\t\tsucc:");
8158 for (j = 0; j < bb->out_count; ++j) {
8159 g_print (" BB%d", bb->out_bb [j]->block_num);
8161 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
8164 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
8167 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
8169 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
8177 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
8181 g_assert (bb->code);
8182 bb->last_ins->next = inst;
8183 bb->last_ins = inst;
8185 bb->last_ins = bb->code = inst;
8190 mono_destroy_compile (MonoCompile *cfg)
8192 //mono_mempool_stats (cfg->mempool);
8193 g_hash_table_destroy (cfg->bb_hash);
8194 mono_free_loop_info (cfg);
8196 mono_regstate_free (cfg->rs);
8198 g_hash_table_destroy (cfg->spvars);
8200 g_hash_table_destroy (cfg->exvars);
8201 mono_mempool_destroy (cfg->mempool);
8202 g_list_free (cfg->ldstr_list);
8204 g_free (cfg->varinfo);
8206 g_free (cfg->exception_message);
8210 #ifdef HAVE_KW_THREAD
8211 static __thread gpointer mono_lmf_addr MONO_TLS_FAST;
8215 mono_get_jit_tls_key (void)
8217 return mono_jit_tls_id;
8221 mono_get_lmf_tls_offset (void)
8224 MONO_THREAD_VAR_OFFSET(mono_lmf_addr,offset);
8229 mono_get_lmf_addr (void)
8231 #ifdef HAVE_KW_THREAD
8232 return mono_lmf_addr;
8234 MonoJitTlsData *jit_tls;
8236 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
8237 return &jit_tls->lmf;
8239 g_assert_not_reached ();
8244 /* Called by native->managed wrappers */
8246 mono_jit_thread_attach (MonoDomain *domain)
8248 #ifdef HAVE_KW_THREAD
8249 if (!mono_lmf_addr) {
8250 mono_thread_attach (domain);
8253 if (!TlsGetValue (mono_jit_tls_id))
8254 mono_thread_attach (domain);
8259 * mono_thread_abort:
8260 * @obj: exception object
8262 * abort the thread, print exception information and stack trace
8265 mono_thread_abort (MonoObject *obj)
8267 /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
8269 /* handle_remove should be eventually called for this thread, too
8272 mono_thread_exit ();
8276 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
8278 MonoJitTlsData *jit_tls;
8281 jit_tls = TlsGetValue (mono_jit_tls_id);
8285 jit_tls = g_new0 (MonoJitTlsData, 1);
8287 TlsSetValue (mono_jit_tls_id, jit_tls);
8289 jit_tls->abort_func = abort_func;
8290 jit_tls->end_of_stack = stack_start;
8292 lmf = g_new0 (MonoLMF, 1);
8295 jit_tls->lmf = jit_tls->first_lmf = lmf;
8297 #ifdef HAVE_KW_THREAD
8298 mono_lmf_addr = &jit_tls->lmf;
8301 mono_arch_setup_jit_tls_data (jit_tls);
8303 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8304 mono_setup_altstack (jit_tls);
8311 mono_thread_start_cb (gsize tid, gpointer stack_start, gpointer func)
8314 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
8315 thread = mono_thread_current ();
8317 thread->jit_data = jit_tls;
8320 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
8323 mono_thread_abort_dummy (MonoObject *obj)
8325 if (mono_thread_attach_aborted_cb)
8326 mono_thread_attach_aborted_cb (obj);
8328 mono_thread_abort (obj);
8332 mono_thread_attach_cb (gsize tid, gpointer stack_start)
8335 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
8336 thread = mono_thread_current ();
8338 thread->jit_data = jit_tls;
8339 if (mono_profiler_get_events () & MONO_PROFILE_STATISTICAL)
8340 setup_stat_profiler ();
8344 mini_thread_cleanup (MonoThread *thread)
8346 MonoJitTlsData *jit_tls = thread->jit_data;
8349 mono_arch_free_jit_tls_data (jit_tls);
8351 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8352 mono_free_altstack (jit_tls);
8354 g_free (jit_tls->first_lmf);
8356 thread->jit_data = NULL;
8357 TlsSetValue (mono_jit_tls_id, NULL);
8362 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
8364 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
8368 ji->data.target = target;
8369 ji->next = cfg->patch_info;
8371 cfg->patch_info = ji;
8375 mono_remove_patch_info (MonoCompile *cfg, int ip)
8377 MonoJumpInfo **ji = &cfg->patch_info;
8380 if ((*ji)->ip.i == ip)
8383 ji = &((*ji)->next);
8388 * mono_patch_info_dup_mp:
8390 * Make a copy of PATCH_INFO, allocating memory from the mempool MP.
8393 mono_patch_info_dup_mp (MonoMemPool *mp, MonoJumpInfo *patch_info)
8395 MonoJumpInfo *res = mono_mempool_alloc (mp, sizeof (MonoJumpInfo));
8396 memcpy (res, patch_info, sizeof (MonoJumpInfo));
8398 switch (patch_info->type) {
8399 case MONO_PATCH_INFO_LDSTR:
8400 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
8401 case MONO_PATCH_INFO_LDTOKEN:
8402 case MONO_PATCH_INFO_DECLSEC:
8403 res->data.token = mono_mempool_alloc (mp, sizeof (MonoJumpInfoToken));
8404 memcpy (res->data.token, patch_info->data.token, sizeof (MonoJumpInfoToken));
8406 case MONO_PATCH_INFO_SWITCH:
8407 res->data.table = mono_mempool_alloc (mp, sizeof (MonoJumpInfoBBTable));
8408 memcpy (res->data.table, patch_info->data.table, sizeof (MonoJumpInfoBBTable));
8418 mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors)
8420 unsigned char *ip = patch_info->ip.i + code;
8421 gconstpointer target = NULL;
8423 switch (patch_info->type) {
8424 case MONO_PATCH_INFO_BB:
8425 target = patch_info->data.bb->native_offset + code;
8427 case MONO_PATCH_INFO_ABS:
8428 target = patch_info->data.target;
8430 case MONO_PATCH_INFO_LABEL:
8431 target = patch_info->data.inst->inst_c0 + code;
8433 case MONO_PATCH_INFO_IP:
8436 case MONO_PATCH_INFO_METHOD_REL:
8437 target = code + patch_info->data.offset;
8439 case MONO_PATCH_INFO_INTERNAL_METHOD: {
8440 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
8442 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
8443 g_assert_not_reached ();
8445 target = mono_icall_get_wrapper (mi);
8448 case MONO_PATCH_INFO_METHOD_JUMP: {
8451 /* get the trampoline to the method from the domain */
8452 target = mono_create_jump_trampoline (domain, patch_info->data.method, TRUE);
8453 if (!domain->jump_target_hash)
8454 domain->jump_target_hash = g_hash_table_new (NULL, NULL);
8455 list = g_hash_table_lookup (domain->jump_target_hash, patch_info->data.method);
8456 list = g_slist_prepend (list, ip);
8457 g_hash_table_insert (domain->jump_target_hash, patch_info->data.method, list);
8460 case MONO_PATCH_INFO_METHOD:
8461 if (patch_info->data.method == method) {
8464 /* get the trampoline to the method from the domain */
8465 target = mono_create_jit_trampoline (patch_info->data.method);
8467 case MONO_PATCH_INFO_SWITCH: {
8468 gpointer *jump_table;
8471 if (method && method->dynamic) {
8472 jump_table = mono_code_manager_reserve (mono_dynamic_code_hash_lookup (domain, method)->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
8474 mono_domain_lock (domain);
8475 jump_table = mono_code_manager_reserve (domain->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
8476 mono_domain_unlock (domain);
8479 for (i = 0; i < patch_info->data.table->table_size; i++) {
8480 jump_table [i] = code + GPOINTER_TO_INT (patch_info->data.table->table [i]);
8482 target = jump_table;
8485 case MONO_PATCH_INFO_METHODCONST:
8486 case MONO_PATCH_INFO_CLASS:
8487 case MONO_PATCH_INFO_IMAGE:
8488 case MONO_PATCH_INFO_FIELD:
8489 target = patch_info->data.target;
8491 case MONO_PATCH_INFO_IID:
8492 mono_class_init (patch_info->data.klass);
8493 target = GINT_TO_POINTER ((int)patch_info->data.klass->interface_id);
8495 case MONO_PATCH_INFO_ADJUSTED_IID:
8496 mono_class_init (patch_info->data.klass);
8497 target = GINT_TO_POINTER ((int)(-((patch_info->data.klass->interface_id + 1) * SIZEOF_VOID_P)));
8499 case MONO_PATCH_INFO_VTABLE:
8500 target = mono_class_vtable (domain, patch_info->data.klass);
8502 case MONO_PATCH_INFO_CLASS_INIT:
8503 target = mono_create_class_init_trampoline (mono_class_vtable (domain, patch_info->data.klass));
8505 case MONO_PATCH_INFO_SFLDA: {
8506 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
8507 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && (method && mono_class_needs_cctor_run (vtable->klass, method)))
8508 /* Done by the generated code */
8512 mono_runtime_class_init (vtable);
8514 target = (char*)vtable->data + patch_info->data.field->offset;
8517 case MONO_PATCH_INFO_R4:
8518 case MONO_PATCH_INFO_R8:
8519 target = patch_info->data.target;
8521 case MONO_PATCH_INFO_EXC_NAME:
8522 target = patch_info->data.name;
8524 case MONO_PATCH_INFO_LDSTR:
8526 mono_ldstr (domain, patch_info->data.token->image,
8527 mono_metadata_token_index (patch_info->data.token->token));
8529 case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
8531 MonoClass *handle_class;
8533 handle = mono_ldtoken (patch_info->data.token->image,
8534 patch_info->data.token->token, &handle_class, NULL);
8535 mono_class_init (handle_class);
8536 mono_class_init (mono_class_from_mono_type (handle));
8539 mono_type_get_object (domain, handle);
8542 case MONO_PATCH_INFO_LDTOKEN: {
8544 MonoClass *handle_class;
8546 handle = mono_ldtoken (patch_info->data.token->image,
8547 patch_info->data.token->token, &handle_class, NULL);
8548 mono_class_init (handle_class);
8553 case MONO_PATCH_INFO_DECLSEC:
8554 target = (mono_metadata_blob_heap (patch_info->data.token->image, patch_info->data.token->token) + 2);
8556 case MONO_PATCH_INFO_BB_OVF:
8557 case MONO_PATCH_INFO_EXC_OVF:
8558 case MONO_PATCH_INFO_GOT_OFFSET:
8559 case MONO_PATCH_INFO_NONE:
8562 g_assert_not_reached ();
8565 return (gpointer)target;
8569 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
8570 MonoJitICallInfo *info;
8572 decompose_foreach (tree, cfg);
8574 switch (mono_burg_arity [tree->opcode]) {
8577 dec_foreach (tree->inst_left, cfg);
8579 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
8580 MonoInst *iargs [2];
8582 iargs [0] = tree->inst_left;
8584 mono_emulate_opcode (cfg, tree, iargs, info);
8590 #ifdef MONO_ARCH_BIGMUL_INTRINS
8591 if (tree->opcode == OP_LMUL
8592 && (cfg->opt & MONO_OPT_INTRINS)
8593 && (tree->inst_left->opcode == CEE_CONV_I8
8594 || tree->inst_left->opcode == CEE_CONV_U8)
8595 && tree->inst_left->inst_left->type == STACK_I4
8596 && (tree->inst_right->opcode == CEE_CONV_I8
8597 || tree->inst_right->opcode == CEE_CONV_U8)
8598 && tree->inst_right->inst_left->type == STACK_I4
8599 && tree->inst_left->opcode == tree->inst_right->opcode) {
8600 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
8601 tree->inst_left = tree->inst_left->inst_left;
8602 tree->inst_right = tree->inst_right->inst_left;
8603 dec_foreach (tree, cfg);
8606 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
8607 MonoInst *iargs [2];
8609 iargs [0] = tree->inst_i0;
8610 iargs [1] = tree->inst_i1;
8612 mono_emulate_opcode (cfg, tree, iargs, info);
8614 dec_foreach (iargs [0], cfg);
8615 dec_foreach (iargs [1], cfg);
8618 dec_foreach (tree->inst_left, cfg);
8619 dec_foreach (tree->inst_right, cfg);
8623 g_assert_not_reached ();
8628 decompose_pass (MonoCompile *cfg) {
8631 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
8634 cfg->prev_ins = NULL;
8635 for (tree = cfg->cbb->code; tree; tree = tree->next) {
8636 dec_foreach (tree, cfg);
8637 cfg->prev_ins = tree;
8643 nullify_basic_block (MonoBasicBlock *bb)
8650 bb->code = bb->last_ins = NULL;
8651 bb->cil_code = NULL;
8655 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
8659 for (i = 0; i < bb->out_count; i++) {
8660 MonoBasicBlock *ob = bb->out_bb [i];
8663 if (bb->out_count > 1) {
8664 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
8668 bb->out_bb [i] = repl;
8675 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
8679 for (i = 0; i < bb->in_count; i++) {
8680 MonoBasicBlock *ib = bb->in_bb [i];
8683 if (bb->in_count > 1) {
8684 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
8688 bb->in_bb [i] = repl;
8695 replace_out_block_in_code (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl) {
8698 for (inst = bb->code; inst != NULL; inst = inst->next) {
8699 if (inst->opcode == OP_CALL_HANDLER) {
8700 if (inst->inst_target_bb == orig) {
8701 inst->inst_target_bb = repl;
8705 if (bb->last_ins != NULL) {
8706 switch (bb->last_ins->opcode) {
8708 if (bb->last_ins->inst_target_bb == orig) {
8709 bb->last_ins->inst_target_bb = repl;
8714 int n = GPOINTER_TO_INT (bb->last_ins->klass);
8715 for (i = 0; i < n; i++ ) {
8716 if (bb->last_ins->inst_many_bb [i] == orig) {
8717 bb->last_ins->inst_many_bb [i] = repl;
8732 if (bb->last_ins->inst_true_bb == orig) {
8733 bb->last_ins->inst_true_bb = repl;
8735 if (bb->last_ins->inst_false_bb == orig) {
8736 bb->last_ins->inst_false_bb = repl;
8746 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
8750 for (i = 0; i < bb->out_count; i++) {
8751 MonoBasicBlock *ob = bb->out_bb [i];
8752 for (j = 0; j < ob->in_count; j++) {
8753 if (ob->in_bb [j] == orig) {
8754 ob->in_bb [j] = repl;
8762 * Check if a bb is useless (is just made of NOPs and ends with an
8763 * unconditional branch, or nothing).
8764 * If it is so, unlink it from the CFG and nullify it, and return TRUE.
8765 * Otherwise, return FALSE;
8768 remove_block_if_useless (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *previous_bb) {
8769 MonoBasicBlock *target_bb = NULL;
8772 /* Do not touch handlers */
8773 if (bb->region != -1) {
8774 bb->not_useless = TRUE;
8778 for (inst = bb->code; inst != NULL; inst = inst->next) {
8779 switch (inst->opcode) {
8783 target_bb = inst->inst_target_bb;
8786 bb->not_useless = TRUE;
8791 if (target_bb == NULL) {
8792 if ((bb->out_count == 1) && (bb->out_bb [0] == bb->next_bb)) {
8793 target_bb = bb->next_bb;
8795 /* Do not touch empty BBs that do not "fall through" to their next BB (like the exit BB) */
8800 /* Do not touch BBs following a switch (they are the "default" branch) */
8801 if ((previous_bb->last_ins != NULL) && (previous_bb->last_ins->opcode == CEE_SWITCH)) {
8805 /* Do not touch BBs following the entry BB and jumping to something that is not */
8806 /* thiry "next" bb (the entry BB cannot contain the branch) */
8807 if ((previous_bb == cfg->bb_entry) && (bb->next_bb != target_bb)) {
8812 * Do not touch BBs following a try block as the code in
8813 * mini_method_compile needs them to compute the length of the try block.
8815 if (MONO_BBLOCK_IS_IN_REGION (previous_bb, MONO_REGION_TRY))
8818 /* Check that there is a target BB, and that bb is not an empty loop (Bug 75061) */
8819 if ((target_bb != NULL) && (target_bb != bb)) {
8822 if (cfg->verbose_level > 1) {
8823 printf ("remove_block_if_useless %s, removed BB%d\n", mono_method_full_name (cfg->method, TRUE), bb->block_num);
8826 /* unlink_bblock () modifies the bb->in_bb array so can't use a for loop here */
8827 while (bb->in_count) {
8828 MonoBasicBlock *in_bb = bb->in_bb [0];
8829 mono_unlink_bblock (cfg, in_bb, bb);
8830 link_bblock (cfg, in_bb, target_bb);
8831 replace_out_block_in_code (in_bb, bb, target_bb);
8834 mono_unlink_bblock (cfg, bb, target_bb);
8836 if ((previous_bb != cfg->bb_entry) &&
8837 (previous_bb->region == bb->region) &&
8838 ((previous_bb->last_ins == NULL) ||
8839 ((previous_bb->last_ins->opcode != CEE_BR) &&
8840 (! (MONO_IS_COND_BRANCH_OP (previous_bb->last_ins))) &&
8841 (previous_bb->last_ins->opcode != CEE_SWITCH)))) {
8842 for (i = 0; i < previous_bb->out_count; i++) {
8843 if (previous_bb->out_bb [i] == target_bb) {
8845 MONO_INST_NEW (cfg, jump, CEE_BR);
8846 MONO_ADD_INS (previous_bb, jump);
8847 jump->cil_code = previous_bb->cil_code;
8848 jump->inst_target_bb = target_bb;
8854 previous_bb->next_bb = bb->next_bb;
8855 nullify_basic_block (bb);
8864 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
8866 bb->out_count = bbn->out_count;
8867 bb->out_bb = bbn->out_bb;
8869 replace_basic_block (bb, bbn, bb);
8871 /* Nullify branch at the end of bb */
8872 if (bb->last_ins && MONO_IS_BRANCH_OP (bb->last_ins)) {
8873 bb->last_ins->opcode = CEE_NOP;
8878 bb->last_ins->next = bbn->code;
8879 bb->last_ins = bbn->last_ins;
8882 bb->code = bbn->code;
8883 bb->last_ins = bbn->last_ins;
8885 bb->next_bb = bbn->next_bb;
8886 nullify_basic_block (bbn);
8890 move_basic_block_to_end (MonoCompile *cfg, MonoBasicBlock *bb)
8892 MonoBasicBlock *bbn, *next;
8896 /* Find the previous */
8897 for (bbn = cfg->bb_entry; bbn->next_bb && bbn->next_bb != bb; bbn = bbn->next_bb)
8900 bbn->next_bb = bb->next_bb;
8904 for (bbn = cfg->bb_entry; bbn->next_bb; bbn = bbn->next_bb)
8910 if (next && (!bb->last_ins || (bb->last_ins->opcode != OP_NOT_REACHED))) {
8913 MONO_INST_NEW (cfg, ins, CEE_BR);
8914 MONO_ADD_INS (bb, ins);
8915 link_bblock (cfg, bb, next);
8916 ins->inst_target_bb = next;
8920 /* checks that a and b represent the same instructions, conservatively,
8921 * it can return FALSE also for two trees that are equal.
8922 * FIXME: also make sure there are no side effects.
8925 same_trees (MonoInst *a, MonoInst *b)
8928 if (a->opcode != b->opcode)
8930 arity = mono_burg_arity [a->opcode];
8932 if (a->ssa_op == b->ssa_op && a->ssa_op == MONO_SSA_LOAD && a->inst_i0 == b->inst_i0)
8934 return same_trees (a->inst_left, b->inst_left);
8935 } else if (arity == 2) {
8936 return same_trees (a->inst_left, b->inst_left) && same_trees (a->inst_right, b->inst_right);
8937 } else if (arity == 0) {
8938 switch (a->opcode) {
8940 return a->inst_c0 == b->inst_c0;
8949 get_unsigned_condbranch (int opcode)
8952 case CEE_BLE: return CEE_BLE_UN;
8953 case CEE_BLT: return CEE_BLT_UN;
8954 case CEE_BGE: return CEE_BGE_UN;
8955 case CEE_BGT: return CEE_BGT_UN;
8957 g_assert_not_reached ();
8962 tree_is_unsigned (MonoInst* ins) {
8963 switch (ins->opcode) {
8965 return (int)ins->inst_c0 >= 0;
8966 /* array lengths are positive as are string sizes */
8973 case CEE_CONV_OVF_U1:
8974 case CEE_CONV_OVF_U2:
8975 case CEE_CONV_OVF_U4:
8986 /* check if an unsigned compare can be used instead of two signed compares
8987 * for (val < 0 || val > limit) conditionals.
8988 * Returns TRUE if the optimization has been applied.
8989 * Note that this can't be applied if the second arg is not positive...
8992 try_unsigned_compare (MonoCompile *cfg, MonoBasicBlock *bb)
8994 MonoBasicBlock *truet, *falset;
8995 MonoInst *cmp_inst = bb->last_ins->inst_left;
8997 if (!cmp_inst->inst_right->inst_c0 == 0)
8999 truet = bb->last_ins->inst_true_bb;
9000 falset = bb->last_ins->inst_false_bb;
9001 if (falset->in_count != 1)
9003 condb = falset->last_ins;
9004 /* target bb must have one instruction */
9005 if (!condb || (condb != falset->code))
9007 if ((((condb->opcode == CEE_BLE || condb->opcode == CEE_BLT) && (condb->inst_false_bb == truet))
9008 || ((condb->opcode == CEE_BGE || condb->opcode == CEE_BGT) && (condb->inst_true_bb == truet)))
9009 && same_trees (cmp_inst->inst_left, condb->inst_left->inst_left)) {
9010 if (!tree_is_unsigned (condb->inst_left->inst_right))
9012 condb->opcode = get_unsigned_condbranch (condb->opcode);
9013 /* change the original condbranch to just point to the new unsigned check */
9014 bb->last_ins->opcode = CEE_BR;
9015 bb->last_ins->inst_target_bb = falset;
9016 replace_out_block (bb, truet, NULL);
9017 replace_in_block (truet, bb, NULL);
9024 * Optimizes the branches on the Control Flow Graph
9028 optimize_branches (MonoCompile *cfg)
9030 int i, changed = FALSE;
9031 MonoBasicBlock *bb, *bbn;
9032 guint32 niterations;
9035 * Some crazy loops could cause the code below to go into an infinite
9036 * loop, see bug #53003 for an example. To prevent this, we put an upper
9037 * bound on the number of iterations.
9039 if (cfg->num_bblocks > 1000)
9040 niterations = cfg->num_bblocks * 2;
9045 MonoBasicBlock *previous_bb;
9049 /* we skip the entry block (exit is handled specially instead ) */
9050 for (previous_bb = cfg->bb_entry, bb = cfg->bb_entry->next_bb; bb; previous_bb = bb, bb = bb->next_bb) {
9052 /* dont touch code inside exception clauses */
9053 if (bb->region != -1)
9056 if (!bb->not_useless && remove_block_if_useless (cfg, bb, previous_bb)) {
9061 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
9062 if (cfg->verbose_level > 2)
9063 g_print ("nullify block triggered %d\n", bbn->block_num);
9065 bb->next_bb = bbn->next_bb;
9067 for (i = 0; i < bbn->out_count; i++)
9068 replace_in_block (bbn->out_bb [i], bbn, NULL);
9070 nullify_basic_block (bbn);
9074 if (bb->out_count == 1) {
9075 bbn = bb->out_bb [0];
9077 /* conditional branches where true and false targets are the same can be also replaced with CEE_BR */
9078 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins)) {
9080 MONO_INST_NEW (cfg, pop, CEE_POP);
9081 pop->inst_left = bb->last_ins->inst_left->inst_left;
9082 mono_add_ins_to_end (bb, pop);
9083 MONO_INST_NEW (cfg, pop, CEE_POP);
9084 pop->inst_left = bb->last_ins->inst_left->inst_right;
9085 mono_add_ins_to_end (bb, pop);
9086 bb->last_ins->opcode = CEE_BR;
9087 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
9089 if (cfg->verbose_level > 2)
9090 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
9093 if (bb->region == bbn->region && bb->next_bb == bbn) {
9094 /* the block are in sequence anyway ... */
9096 /* branches to the following block can be removed */
9097 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
9098 bb->last_ins->opcode = CEE_NOP;
9100 if (cfg->verbose_level > 2)
9101 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
9104 if (bbn->in_count == 1) {
9106 if (bbn != cfg->bb_exit) {
9107 if (cfg->verbose_level > 2)
9108 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
9109 merge_basic_blocks (bb, bbn);
9114 //mono_print_bb_code (bb);
9118 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
9119 if (cfg->verbose_level > 2) {
9120 g_print ("nullify block triggered %d\n", bbn->block_num);
9122 bb->next_bb = bbn->next_bb;
9124 for (i = 0; i < bbn->out_count; i++)
9125 replace_in_block (bbn->out_bb [i], bbn, NULL);
9127 nullify_basic_block (bbn);
9132 if (bb->out_count == 1) {
9133 bbn = bb->out_bb [0];
9135 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
9136 bbn = bb->last_ins->inst_target_bb;
9137 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
9138 bbn->code->inst_target_bb->region == bb->region) {
9140 if (cfg->verbose_level > 2)
9141 g_print ("in %s branch to branch triggered %d -> %d -> %d\n", cfg->method->name,
9142 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num);
9144 replace_in_block (bbn, bb, NULL);
9145 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
9146 link_bblock (cfg, bb, bbn->code->inst_target_bb);
9147 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
9152 } else if (bb->out_count == 2) {
9153 if (bb->last_ins && MONO_IS_COND_BRANCH_NOFP (bb->last_ins)) {
9154 int branch_result = mono_eval_cond_branch (bb->last_ins);
9155 MonoBasicBlock *taken_branch_target = NULL, *untaken_branch_target = NULL;
9156 if (branch_result == BRANCH_TAKEN) {
9157 taken_branch_target = bb->last_ins->inst_true_bb;
9158 untaken_branch_target = bb->last_ins->inst_false_bb;
9159 } else if (branch_result == BRANCH_NOT_TAKEN) {
9160 taken_branch_target = bb->last_ins->inst_false_bb;
9161 untaken_branch_target = bb->last_ins->inst_true_bb;
9163 if (taken_branch_target) {
9164 /* if mono_eval_cond_branch () is ever taken to handle
9165 * non-constant values to compare, issue a pop here.
9167 bb->last_ins->opcode = CEE_BR;
9168 bb->last_ins->inst_target_bb = taken_branch_target;
9169 mono_unlink_bblock (cfg, bb, untaken_branch_target);
9173 bbn = bb->last_ins->inst_true_bb;
9174 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
9175 bbn->code->inst_target_bb->region == bb->region) {
9176 if (cfg->verbose_level > 2)
9177 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
9178 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
9182 * Unlink, then relink bblocks to avoid various
9183 * tricky situations when the two targets of the branch
9184 * are equal, or will become equal after the change.
9186 mono_unlink_bblock (cfg, bb, bb->last_ins->inst_true_bb);
9187 mono_unlink_bblock (cfg, bb, bb->last_ins->inst_false_bb);
9189 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
9191 link_bblock (cfg, bb, bb->last_ins->inst_true_bb);
9192 link_bblock (cfg, bb, bb->last_ins->inst_false_bb);
9198 bbn = bb->last_ins->inst_false_bb;
9199 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
9200 bbn->code->inst_target_bb->region == bb->region) {
9201 if (cfg->verbose_level > 2)
9202 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
9203 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
9206 mono_unlink_bblock (cfg, bb, bb->last_ins->inst_true_bb);
9207 mono_unlink_bblock (cfg, bb, bb->last_ins->inst_false_bb);
9209 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
9211 link_bblock (cfg, bb, bb->last_ins->inst_true_bb);
9212 link_bblock (cfg, bb, bb->last_ins->inst_false_bb);
9219 /* detect and optimize to unsigned compares checks like: if (v < 0 || v > limit */
9220 if (bb->last_ins && bb->last_ins->opcode == CEE_BLT && bb->last_ins->inst_left->inst_right->opcode == OP_ICONST) {
9221 if (try_unsigned_compare (cfg, bb)) {
9222 /*g_print ("applied in bb %d (->%d) %s\n", bb->block_num, bb->last_ins->inst_target_bb->block_num, mono_method_full_name (cfg->method, TRUE));*/
9228 if (bb->last_ins && MONO_IS_COND_BRANCH_NOFP (bb->last_ins)) {
9229 if (bb->last_ins->inst_false_bb->out_of_line && (bb->region == bb->last_ins->inst_false_bb->region)) {
9230 /* Reverse the branch */
9231 bb->last_ins->opcode = reverse_branch_op (bb->last_ins->opcode);
9232 bbn = bb->last_ins->inst_false_bb;
9233 bb->last_ins->inst_false_bb = bb->last_ins->inst_true_bb;
9234 bb->last_ins->inst_true_bb = bbn;
9236 move_basic_block_to_end (cfg, bb->last_ins->inst_true_bb);
9237 if (cfg->verbose_level > 2)
9238 g_print ("cbranch to throw block triggered %d.\n",
9244 } while (changed && (niterations > 0));
9249 mono_compile_create_vars (MonoCompile *cfg)
9251 MonoMethodSignature *sig;
9252 MonoMethodHeader *header;
9255 header = mono_method_get_header (cfg->method);
9257 sig = mono_method_signature (cfg->method);
9259 if (!MONO_TYPE_IS_VOID (sig->ret)) {
9260 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
9261 cfg->ret->opcode = OP_RETARG;
9262 cfg->ret->inst_vtype = sig->ret;
9263 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
9265 if (cfg->verbose_level > 2)
9266 g_print ("creating vars\n");
9269 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
9271 for (i = 0; i < sig->param_count; ++i) {
9272 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
9273 if (sig->params [i]->byref) {
9274 cfg->disable_ssa = TRUE;
9278 cfg->locals_start = cfg->num_varinfo;
9280 if (cfg->verbose_level > 2)
9281 g_print ("creating locals\n");
9282 for (i = 0; i < header->num_locals; ++i)
9283 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
9284 if (cfg->verbose_level > 2)
9285 g_print ("locals done\n");
9287 #ifdef MONO_ARCH_HAVE_CREATE_VARS
9288 mono_arch_create_vars (cfg);
9293 mono_print_code (MonoCompile *cfg)
9297 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
9298 MonoInst *tree = bb->code;
9303 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
9305 for (; tree; tree = tree->next) {
9306 mono_print_tree (tree);
9311 bb->last_ins->next = NULL;
9315 extern const char * const mono_burg_rule_string [];
9318 emit_state (MonoCompile *cfg, MBState *state, int goal)
9321 int ern = mono_burg_rule (state, goal);
9322 const guint16 *nts = mono_burg_nts_data + mono_burg_nts [ern];
9325 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
9329 // state->reg1 = state->reg2; /* chain rule */
9331 #ifdef MONO_ARCH_ENABLE_EMIT_STATE_OPT
9334 state->reg1 = mono_regstate_next_int (cfg->rs);
9335 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
9338 state->reg1 = mono_regstate_next_int (cfg->rs);
9339 state->reg2 = mono_regstate_next_int (cfg->rs);
9342 state->reg1 = mono_regstate_next_float (cfg->rs);
9345 #ifdef MONO_ARCH_ENABLE_EMIT_STATE_OPT
9347 * Enabling this might cause bugs to surface in the local register
9348 * allocators on some architectures like x86.
9350 if ((state->tree->ssa_op == MONO_SSA_STORE) && (state->left->tree->opcode == OP_REGVAR)) {
9351 /* Do not optimize away reg-reg moves */
9352 if (! ((state->right->tree->ssa_op == MONO_SSA_LOAD) && (state->right->left->tree->opcode == OP_REGVAR))) {
9353 state->right->reg1 = state->left->tree->dreg;
9362 mono_burg_kids (state, ern, kids);
9364 emit_state (cfg, kids [0], nts [0]);
9366 emit_state (cfg, kids [1], nts [1]);
9368 g_assert (!nts [3]);
9369 emit_state (cfg, kids [2], nts [2]);
9374 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
9375 if ((emit = mono_burg_func [ern]))
9376 emit (state, state->tree, cfg);
9379 #define DEBUG_SELECTION
9382 mini_select_instructions (MonoCompile *cfg)
9386 cfg->state_pool = mono_mempool_new ();
9387 cfg->rs = mono_regstate_new ();
9389 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
9390 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins) &&
9391 bb->next_bb != bb->last_ins->inst_false_bb) {
9393 /* we are careful when inverting, since bugs like #59580
9394 * could show up when dealing with NaNs.
9396 if (MONO_IS_COND_BRANCH_NOFP(bb->last_ins) && bb->next_bb == bb->last_ins->inst_true_bb) {
9397 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
9398 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
9399 bb->last_ins->inst_false_bb = tmp;
9401 bb->last_ins->opcode = reverse_branch_op (bb->last_ins->opcode);
9403 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
9404 inst->opcode = CEE_BR;
9405 inst->inst_target_bb = bb->last_ins->inst_false_bb;
9406 mono_bblock_add_inst (bb, inst);
9411 #ifdef DEBUG_SELECTION
9412 if (cfg->verbose_level >= 4) {
9413 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
9414 MonoInst *tree = bb->code;
9415 g_print ("DUMP BLOCK %d:\n", bb->block_num);
9418 for (; tree; tree = tree->next) {
9419 mono_print_tree (tree);
9426 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
9427 MonoInst *tree = bb->code, *next;
9433 bb->last_ins = NULL;
9436 mono_regstate_reset (cfg->rs);
9438 #ifdef DEBUG_SELECTION
9439 if (cfg->verbose_level >= 3)
9440 g_print ("LABEL BLOCK %d:\n", bb->block_num);
9442 for (; tree; tree = next) {
9444 #ifdef DEBUG_SELECTION
9445 if (cfg->verbose_level >= 3) {
9446 mono_print_tree (tree);
9451 if (!(mbstate = mono_burg_label (tree, cfg))) {
9452 g_warning ("unable to label tree %p", tree);
9453 mono_print_tree (tree);
9455 g_assert_not_reached ();
9457 emit_state (cfg, mbstate, MB_NTERM_stmt);
9459 bb->max_ireg = cfg->rs->next_vireg;
9460 bb->max_freg = cfg->rs->next_vfreg;
9463 bb->last_ins->next = NULL;
9465 mono_mempool_empty (cfg->state_pool);
9467 mono_mempool_destroy (cfg->state_pool);
9471 mono_codegen (MonoCompile *cfg)
9473 MonoJumpInfo *patch_info;
9475 int i, max_epilog_size;
9478 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
9479 cfg->spill_count = 0;
9480 /* we reuse dfn here */
9481 /* bb->dfn = bb_count++; */
9482 mono_arch_local_regalloc (cfg, bb);
9485 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
9486 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
9488 code = mono_arch_emit_prolog (cfg);
9490 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
9491 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
9493 cfg->code_len = code - cfg->native_code;
9494 cfg->prolog_end = cfg->code_len;
9496 mono_debug_open_method (cfg);
9498 /* emit code all basic blocks */
9499 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
9500 bb->native_offset = cfg->code_len;
9501 mono_arch_output_basic_block (cfg, bb);
9503 if (bb == cfg->bb_exit) {
9504 cfg->epilog_begin = cfg->code_len;
9506 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE) {
9507 code = cfg->native_code + cfg->code_len;
9508 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
9509 cfg->code_len = code - cfg->native_code;
9512 mono_arch_emit_epilog (cfg);
9516 mono_arch_emit_exceptions (cfg);
9518 max_epilog_size = 0;
9520 code = cfg->native_code + cfg->code_len;
9522 /* we always allocate code in cfg->domain->code_mp to increase locality */
9523 cfg->code_size = cfg->code_len + max_epilog_size;
9524 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
9526 if (cfg->method->dynamic) {
9527 /* Allocate the code into a separate memory pool so it can be freed */
9528 cfg->dynamic_info = g_new0 (MonoJitDynamicMethodInfo, 1);
9529 cfg->dynamic_info->code_mp = mono_code_manager_new_dynamic ();
9530 mono_domain_lock (cfg->domain);
9531 mono_dynamic_code_hash_insert (cfg->domain, cfg->method, cfg->dynamic_info);
9532 mono_domain_unlock (cfg->domain);
9534 code = mono_code_manager_reserve (cfg->dynamic_info->code_mp, cfg->code_size);
9536 mono_domain_lock (cfg->domain);
9537 code = mono_code_manager_reserve (cfg->domain->code_mp, cfg->code_size);
9538 mono_domain_unlock (cfg->domain);
9541 memcpy (code, cfg->native_code, cfg->code_len);
9542 g_free (cfg->native_code);
9543 cfg->native_code = code;
9544 code = cfg->native_code + cfg->code_len;
9546 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
9547 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
9548 switch (patch_info->type) {
9549 case MONO_PATCH_INFO_ABS: {
9550 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
9552 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
9553 if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) &&
9554 strstr (cfg->method->name, info->name))
9556 * This is an icall wrapper, and this is a call to the
9561 /* for these array methods we currently register the same function pointer
9562 * since it's a vararg function. But this means that mono_find_jit_icall_by_addr ()
9563 * will return the incorrect one depending on the order they are registered.
9564 * See tests/test-arr.cs
9566 if (strstr (info->name, "ves_array_new_va_") == NULL && strstr (info->name, "ves_array_element_address_") == NULL) {
9567 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
9568 patch_info->data.name = info->name;
9573 MonoVTable *vtable = mono_find_class_init_trampoline_by_addr (patch_info->data.target);
9575 patch_info->type = MONO_PATCH_INFO_CLASS_INIT;
9576 patch_info->data.klass = vtable->klass;
9581 case MONO_PATCH_INFO_SWITCH: {
9583 if (cfg->method->dynamic) {
9584 table = mono_code_manager_reserve (cfg->dynamic_info->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
9586 mono_domain_lock (cfg->domain);
9587 table = mono_code_manager_reserve (cfg->domain->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
9588 mono_domain_unlock (cfg->domain);
9591 if (!cfg->compile_aot)
9592 /* In the aot case, the patch already points to the correct location */
9593 patch_info->ip.i = patch_info->ip.label->inst_c0;
9594 for (i = 0; i < patch_info->data.table->table_size; i++) {
9595 table [i] = GINT_TO_POINTER (patch_info->data.table->table [i]->native_offset);
9597 patch_info->data.table->table = (MonoBasicBlock**)table;
9606 if (cfg->verbose_level > 0) {
9607 char* nm = mono_method_full_name (cfg->method, TRUE);
9608 g_print ("Method %s emitted at %p to %p (code length %d) [%s]\n",
9610 cfg->native_code, cfg->native_code + cfg->code_len, cfg->code_len, cfg->domain->friendly_name);
9614 #ifdef MONO_ARCH_HAVE_SAVE_UNWIND_INFO
9615 mono_arch_save_unwind_info (cfg);
9618 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info, cfg->run_cctors);
9620 if (cfg->method->dynamic) {
9621 mono_code_manager_commit (cfg->dynamic_info->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
9623 mono_domain_lock (cfg->domain);
9624 mono_code_manager_commit (cfg->domain->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
9625 mono_domain_unlock (cfg->domain);
9628 mono_arch_flush_icache (cfg->native_code, cfg->code_len);
9630 mono_debug_close_method (cfg);
9636 remove_critical_edges (MonoCompile *cfg) {
9638 MonoBasicBlock *previous_bb;
9640 if (cfg->verbose_level > 3) {
9641 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
9643 printf ("remove_critical_edges %s, BEFORE BB%d (in:", mono_method_full_name (cfg->method, TRUE), bb->block_num);
9644 for (i = 0; i < bb->in_count; i++) {
9645 printf (" %d", bb->in_bb [i]->block_num);
9648 for (i = 0; i < bb->out_count; i++) {
9649 printf (" %d", bb->out_bb [i]->block_num);
9652 if (bb->last_ins != NULL) {
9654 mono_print_tree (bb->last_ins);
9660 for (previous_bb = cfg->bb_entry, bb = previous_bb->next_bb; bb != NULL; previous_bb = previous_bb->next_bb, bb = bb->next_bb) {
9661 if (bb->in_count > 1) {
9663 for (in_bb_index = 0; in_bb_index < bb->in_count; in_bb_index++) {
9664 MonoBasicBlock *in_bb = bb->in_bb [in_bb_index];
9665 if (in_bb->out_count > 1) {
9666 MonoBasicBlock *new_bb = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock));
9667 new_bb->block_num = cfg->num_bblocks++;
9668 // new_bb->real_offset = bb->real_offset;
9669 new_bb->region = bb->region;
9671 /* Do not alter the CFG while altering the BB list */
9672 if (previous_bb->region == bb->region) {
9673 if (previous_bb != cfg->bb_entry) {
9674 /* If previous_bb "followed through" to bb, */
9675 /* keep it linked with a CEE_BR */
9676 if ((previous_bb->last_ins == NULL) ||
9677 ((previous_bb->last_ins->opcode != CEE_BR) &&
9678 (! (MONO_IS_COND_BRANCH_OP (previous_bb->last_ins))) &&
9679 (previous_bb->last_ins->opcode != CEE_SWITCH))) {
9681 /* Make sure previous_bb really falls through bb */
9682 for (i = 0; i < previous_bb->out_count; i++) {
9683 if (previous_bb->out_bb [i] == bb) {
9685 MONO_INST_NEW (cfg, jump, CEE_BR);
9686 MONO_ADD_INS (previous_bb, jump);
9687 jump->cil_code = previous_bb->cil_code;
9688 jump->inst_target_bb = bb;
9694 /* We cannot add any inst to the entry BB, so we must */
9695 /* put a new BB in the middle to hold the CEE_BR */
9697 MonoBasicBlock *new_bb_after_entry = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock));
9698 new_bb_after_entry->block_num = cfg->num_bblocks++;
9699 // new_bb_after_entry->real_offset = bb->real_offset;
9700 new_bb_after_entry->region = bb->region;
9702 MONO_INST_NEW (cfg, jump, CEE_BR);
9703 MONO_ADD_INS (new_bb_after_entry, jump);
9704 jump->cil_code = bb->cil_code;
9705 jump->inst_target_bb = bb;
9707 previous_bb->next_bb = new_bb_after_entry;
9708 previous_bb = new_bb_after_entry;
9710 if (cfg->verbose_level > 2) {
9711 printf ("remove_critical_edges %s, added helper BB%d jumping to BB%d\n", mono_method_full_name (cfg->method, TRUE), new_bb_after_entry->block_num, bb->block_num);
9716 /* Insert new_bb in the BB list */
9717 previous_bb->next_bb = new_bb;
9718 new_bb->next_bb = bb;
9719 previous_bb = new_bb;
9721 /* Setup in_bb and out_bb */
9722 new_bb->in_bb = mono_mempool_alloc ((cfg)->mempool, sizeof (MonoBasicBlock*));
9723 new_bb->in_bb [0] = in_bb;
9724 new_bb->in_count = 1;
9725 new_bb->out_bb = mono_mempool_alloc ((cfg)->mempool, sizeof (MonoBasicBlock*));
9726 new_bb->out_bb [0] = bb;
9727 new_bb->out_count = 1;
9729 /* Relink in_bb and bb to (from) new_bb */
9730 replace_out_block (in_bb, bb, new_bb);
9731 replace_out_block_in_code (in_bb, bb, new_bb);
9732 replace_in_block (bb, in_bb, new_bb);
9734 if (cfg->verbose_level > 2) {
9735 printf ("remove_critical_edges %s, removed critical edge from BB%d to BB%d (added BB%d)\n", mono_method_full_name (cfg->method, TRUE), in_bb->block_num, bb->block_num, new_bb->block_num);
9742 if (cfg->verbose_level > 3) {
9743 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
9745 printf ("remove_critical_edges %s, AFTER BB%d (in:", mono_method_full_name (cfg->method, TRUE), bb->block_num);
9746 for (i = 0; i < bb->in_count; i++) {
9747 printf (" %d", bb->in_bb [i]->block_num);
9750 for (i = 0; i < bb->out_count; i++) {
9751 printf (" %d", bb->out_bb [i]->block_num);
9754 if (bb->last_ins != NULL) {
9756 mono_print_tree (bb->last_ins);
9764 * mini_method_compile:
9765 * @method: the method to compile
9766 * @opts: the optimization flags to use
9767 * @domain: the domain where the method will be compiled in
9768 * @run_cctors: whether we should run type ctors if possible
9769 * @compile_aot: whether this is an AOT compilation
9770 * @parts: debug flag
9772 * Returns: a MonoCompile* pointer. Caller must check the exception_type
9773 * field in the returned struct to see if compilation succeded.
9776 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, gboolean compile_aot, int parts)
9778 MonoMethodHeader *header = mono_method_get_header (method);
9782 int dfn = 0, i, code_size_ratio;
9783 gboolean deadce_has_run = FALSE;
9785 mono_jit_stats.methods_compiled++;
9786 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
9787 mono_profiler_method_jit (method);
9789 cfg = g_new0 (MonoCompile, 1);
9790 cfg->method = method;
9791 cfg->mempool = mono_mempool_new ();
9793 cfg->prof_options = mono_profiler_get_events ();
9794 cfg->run_cctors = run_cctors;
9795 cfg->bb_hash = g_hash_table_new (NULL, NULL);
9796 cfg->domain = domain;
9797 cfg->verbose_level = mini_verbose;
9798 cfg->compile_aot = compile_aot;
9800 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
9801 cfg->exception_message = g_strdup_printf ("Missing or incorrect header for method %s", cfg->method->name);
9802 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
9803 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_FAILED);
9807 ip = (guint8 *)header->code;
9809 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX * header->max_stack);
9810 cfg->aliasing_info = NULL;
9812 if (cfg->verbose_level > 2)
9813 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
9816 * create MonoInst* which represents arguments and local variables
9818 mono_compile_create_vars (cfg);
9820 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
9821 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
9822 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_FAILED);
9823 /* cfg contains the details of the failure, so let the caller cleanup */
9827 mono_jit_stats.basic_blocks += cfg->num_bblocks;
9828 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
9830 if ((cfg->num_varinfo > 2000) && !mono_compile_aot) {
9832 * we disable some optimizations if there are too many variables
9833 * because JIT time may become too expensive. The actual number needs
9834 * to be tweaked and eventually the non-linear algorithms should be fixed.
9836 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
9837 cfg->disable_ssa = TRUE;
9840 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
9842 if (cfg->opt & MONO_OPT_BRANCH)
9843 optimize_branches (cfg);
9845 if (cfg->opt & MONO_OPT_SSAPRE) {
9846 remove_critical_edges (cfg);
9849 /* Depth-first ordering on basic blocks */
9850 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
9852 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
9853 if (cfg->num_bblocks != dfn + 1) {
9856 cfg->num_bblocks = dfn + 1;
9858 if (!header->clauses) {
9859 /* remove unreachable code, because the code in them may be
9860 * inconsistent (access to dead variables for example) */
9861 for (bb = cfg->bb_entry; bb;) {
9862 MonoBasicBlock *bbn = bb->next_bb;
9864 if (bbn && bbn->region == -1 && !bbn->dfn) {
9865 if (cfg->verbose_level > 1)
9866 g_print ("found unreachable code in BB%d\n", bbn->block_num);
9867 bb->next_bb = bbn->next_bb;
9868 nullify_basic_block (bbn);
9876 if (cfg->opt & MONO_OPT_LOOP) {
9877 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
9878 mono_compute_natural_loops (cfg);
9881 /* after method_to_ir */
9885 //#define DEBUGSSA "logic_run"
9886 #define DEBUGSSA_CLASS "Tests"
9889 if (!header->num_clauses && !cfg->disable_ssa) {
9890 mono_local_cprop (cfg);
9891 mono_ssa_compute (cfg);
9895 /* fixme: add all optimizations which requires SSA */
9896 if (cfg->opt & (MONO_OPT_SSA | MONO_OPT_ABCREM | MONO_OPT_SSAPRE)) {
9897 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
9898 mono_local_cprop (cfg);
9899 mono_ssa_compute (cfg);
9901 if (cfg->verbose_level >= 2) {
9908 /* after SSA translation */
9912 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
9913 if (cfg->comp_done & MONO_COMP_SSA) {
9914 mono_ssa_cprop (cfg);
9916 mono_local_cprop (cfg);
9920 if (cfg->comp_done & MONO_COMP_SSA) {
9921 //mono_ssa_deadce (cfg);
9923 //mono_ssa_strength_reduction (cfg);
9925 if (cfg->opt & MONO_OPT_SSAPRE) {
9926 mono_perform_ssapre (cfg);
9927 //mono_local_cprop (cfg);
9930 if (cfg->opt & MONO_OPT_DEADCE) {
9931 mono_ssa_deadce (cfg);
9932 deadce_has_run = TRUE;
9935 if ((cfg->flags & MONO_CFG_HAS_LDELEMA) && (cfg->opt & MONO_OPT_ABCREM))
9936 mono_perform_abc_removal (cfg);
9938 mono_ssa_remove (cfg);
9940 if (cfg->opt & MONO_OPT_BRANCH)
9941 optimize_branches (cfg);
9944 /* after SSA removal */
9948 if (cfg->verbose_level > 4) {
9949 printf ("BEFORE DECOMPSE START\n");
9950 mono_print_code (cfg);
9951 printf ("BEFORE DECOMPSE END\n");
9954 decompose_pass (cfg);
9959 g_assert (cfg->got_var_allocated);
9962 * Allways allocate the GOT var to a register, because keeping it
9963 * in memory will increase the number of live temporaries in some
9964 * code created by inssel.brg, leading to the well known spills+
9965 * branches problem. Testcase: mcs crash in
9966 * System.MonoCustomAttrs:GetCustomAttributes.
9968 regs = mono_arch_get_global_int_regs (cfg);
9970 cfg->got_var->opcode = OP_REGVAR;
9971 cfg->got_var->dreg = GPOINTER_TO_INT (regs->data);
9972 cfg->used_int_regs |= 1LL << cfg->got_var->dreg;
9977 if (cfg->opt & MONO_OPT_LINEARS) {
9980 /* For now, compute aliasing info only if needed for deadce... */
9981 if ((cfg->opt & MONO_OPT_DEADCE) && (! deadce_has_run) && (header->num_clauses == 0)) {
9982 cfg->aliasing_info = mono_build_aliasing_information (cfg);
9985 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
9986 cfg->comp_done &= ~MONO_COMP_LIVENESS;
9987 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
9988 mono_analyze_liveness (cfg);
9990 if (cfg->aliasing_info != NULL) {
9991 mono_aliasing_deadce (cfg->aliasing_info);
9992 deadce_has_run = TRUE;
9995 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
9996 regs = mono_arch_get_global_int_regs (cfg);
9998 regs = g_list_delete_link (regs, regs);
9999 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
10002 if (cfg->aliasing_info != NULL) {
10003 mono_destroy_aliasing_information (cfg->aliasing_info);
10004 cfg->aliasing_info = NULL;
10008 //mono_print_code (cfg);
10012 /* variables are allocated after decompose, since decompose could create temps */
10013 mono_arch_allocate_vars (cfg);
10015 if (cfg->opt & MONO_OPT_CFOLD)
10016 mono_constant_fold (cfg);
10018 mini_select_instructions (cfg);
10020 mono_codegen (cfg);
10021 if (cfg->verbose_level >= 2) {
10022 char *id = mono_method_full_name (cfg->method, FALSE);
10023 mono_disassemble_code (cfg, cfg->native_code, cfg->code_len, id + 3);
10027 if (cfg->method->dynamic) {
10028 jinfo = g_malloc0 (sizeof (MonoJitInfo) + (header->num_clauses * sizeof (MonoJitExceptionInfo)));
10030 /* we access cfg->domain->mp */
10031 mono_domain_lock (cfg->domain);
10032 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo) + (header->num_clauses * sizeof (MonoJitExceptionInfo)));
10033 mono_domain_unlock (cfg->domain);
10036 jinfo->method = method;
10037 jinfo->code_start = cfg->native_code;
10038 jinfo->code_size = cfg->code_len;
10039 jinfo->used_regs = cfg->used_int_regs;
10040 jinfo->domain_neutral = (cfg->opt & MONO_OPT_SHARED) != 0;
10041 jinfo->cas_inited = FALSE; /* initialization delayed at the first stalk walk using this method */
10043 if (header->num_clauses) {
10046 jinfo->num_clauses = header->num_clauses;
10048 for (i = 0; i < header->num_clauses; i++) {
10049 MonoExceptionClause *ec = &header->clauses [i];
10050 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
10051 MonoBasicBlock *tblock;
10054 ei->flags = ec->flags;
10056 exvar = mono_find_exvar_for_offset (cfg, ec->handler_offset);
10057 ei->exvar_offset = exvar ? exvar->inst_offset : 0;
10059 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
10060 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->data.filter_offset);
10062 ei->data.filter = cfg->native_code + tblock->native_offset;
10064 ei->data.catch_class = ec->data.catch_class;
10067 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
10069 ei->try_start = cfg->native_code + tblock->native_offset;
10070 g_assert (tblock->native_offset);
10071 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
10073 ei->try_end = cfg->native_code + tblock->native_offset;
10074 g_assert (tblock->native_offset);
10075 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
10077 ei->handler_start = cfg->native_code + tblock->native_offset;
10081 cfg->jit_info = jinfo;
10082 #if defined(__arm__)
10083 mono_arch_fixup_jinfo (cfg);
10086 mono_domain_lock (cfg->domain);
10087 mono_jit_info_table_add (cfg->domain, jinfo);
10089 if (cfg->method->dynamic)
10090 mono_dynamic_code_hash_lookup (cfg->domain, cfg->method)->ji = jinfo;
10091 mono_domain_unlock (cfg->domain);
10093 /* collect statistics */
10094 mono_jit_stats.allocated_code_size += cfg->code_len;
10095 code_size_ratio = cfg->code_len;
10096 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
10097 mono_jit_stats.biggest_method_size = code_size_ratio;
10098 mono_jit_stats.biggest_method = method;
10100 code_size_ratio = (code_size_ratio * 100) / mono_method_get_header (method)->code_size;
10101 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
10102 mono_jit_stats.max_code_size_ratio = code_size_ratio;
10103 mono_jit_stats.max_ratio_method = method;
10105 mono_jit_stats.native_code_size += cfg->code_len;
10107 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
10108 mono_profiler_method_end_jit (method, jinfo, MONO_PROFILE_OK);
10114 mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain, int opt)
10117 GHashTable *jit_code_hash;
10118 gpointer code = NULL;
10121 jit_code_hash = target_domain->jit_code_hash;
10123 method = mono_get_inflated_method (method);
10125 #ifdef MONO_USE_AOT_COMPILER
10126 if (!mono_compile_aot && (opt & MONO_OPT_AOT) && !(mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)) {
10128 MonoDomain *domain = mono_domain_get ();
10130 mono_domain_lock (domain);
10132 mono_class_init (method->klass);
10133 if ((info = mono_aot_get_method (domain, method))) {
10134 g_hash_table_insert (domain->jit_code_hash, method, info);
10135 mono_domain_unlock (domain);
10136 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
10137 return info->code_start;
10140 mono_domain_unlock (domain);
10144 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
10145 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
10147 MonoMethodPInvoke* piinfo = (MonoMethodPInvoke *) method;
10149 if (method->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE && !MONO_CLASS_IS_IMPORT(method->klass))
10150 g_error ("Method '%s' in assembly '%s' contains native code and mono can't run it. The assembly was probably created by Managed C++.\n", mono_method_full_name (method, TRUE), method->klass->image->name);
10152 if (!piinfo->addr) {
10153 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
10154 piinfo->addr = mono_lookup_internal_call (method);
10156 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
10157 mono_lookup_pinvoke_call (method, NULL, NULL);
10159 nm = mono_marshal_get_native_wrapper (method);
10160 return mono_get_addr_from_ftnptr (mono_compile_method (nm));
10162 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
10163 //mono_debug_add_wrapper (method, nm);
10164 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
10165 const char *name = method->name;
10168 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
10169 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
10170 MonoJitICallInfo *mi = mono_find_jit_icall_by_name ("mono_delegate_ctor");
10172 return mono_get_addr_from_ftnptr ((gpointer)mono_icall_get_wrapper (mi));
10173 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
10174 nm = mono_marshal_get_delegate_invoke (method);
10175 return mono_get_addr_from_ftnptr (mono_compile_method (nm));
10176 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
10177 nm = mono_marshal_get_delegate_begin_invoke (method);
10178 return mono_get_addr_from_ftnptr (mono_compile_method (nm));
10179 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
10180 nm = mono_marshal_get_delegate_end_invoke (method);
10181 return mono_get_addr_from_ftnptr (mono_compile_method (nm));
10187 cfg = mini_method_compile (method, opt, target_domain, TRUE, FALSE, 0);
10189 switch (cfg->exception_type) {
10190 case MONO_EXCEPTION_NONE: break;
10191 case MONO_EXCEPTION_TYPE_LOAD:
10192 case MONO_EXCEPTION_MISSING_FIELD:
10193 case MONO_EXCEPTION_MISSING_METHOD: {
10194 /* Throw a type load exception if needed */
10195 MonoLoaderError *error = mono_loader_get_last_error ();
10197 mono_destroy_compile (cfg);
10199 MonoException *ex = mono_loader_error_prepare_exception (error);
10200 mono_raise_exception (ex);
10202 g_assert_not_reached ();
10205 case MONO_EXCEPTION_INVALID_PROGRAM: {
10206 MonoException *ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "InvalidProgramException", cfg->exception_message);
10207 mono_destroy_compile (cfg);
10208 mono_raise_exception (ex);
10211 case MONO_EXCEPTION_UNVERIFIABLE_IL: {
10212 MonoException *ex = mono_exception_from_name_msg (mono_defaults.corlib, "System.Security", "VerificationException", cfg->exception_message);
10213 mono_destroy_compile (cfg);
10214 mono_raise_exception (ex);
10217 /* this can only be set if the security manager is active */
10218 case MONO_EXCEPTION_SECURITY_LINKDEMAND: {
10219 MonoAssembly *assembly = mono_image_get_assembly (method->klass->image);
10220 MonoReflectionAssembly *refass = (MonoReflectionAssembly*) mono_assembly_get_object (target_domain, assembly);
10221 MonoReflectionMethod *refmet = mono_method_get_object (target_domain, method, NULL);
10222 MonoSecurityManager* secman = mono_security_manager_get_methods ();
10223 MonoObject *exc = NULL;
10226 args [0] = &cfg->exception_data;
10229 mono_runtime_invoke (secman->linkdemandsecurityexception, NULL, args, &exc);
10231 mono_destroy_compile (cfg);
10234 mono_raise_exception ((MonoException*)exc);
10237 g_assert_not_reached ();
10240 mono_domain_lock (target_domain);
10242 /* Check if some other thread already did the job. In this case, we can
10243 discard the code this thread generated. */
10245 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
10246 /* We can't use a domain specific method in another domain */
10247 if ((target_domain == mono_domain_get ()) || info->domain_neutral) {
10248 code = info->code_start;
10249 // printf("Discarding code for method %s\n", method->name);
10253 if (code == NULL) {
10254 g_hash_table_insert (jit_code_hash, method, cfg->jit_info);
10255 code = cfg->native_code;
10258 mono_destroy_compile (cfg);
10260 if (target_domain->jump_target_hash) {
10261 MonoJumpInfo patch_info;
10262 GSList *list, *tmp;
10263 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
10265 patch_info.next = NULL;
10266 patch_info.ip.i = 0;
10267 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
10268 patch_info.data.method = method;
10269 g_hash_table_remove (target_domain->jump_target_hash, method);
10271 for (tmp = list; tmp; tmp = tmp->next)
10272 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info, TRUE);
10273 g_slist_free (list);
10276 mono_domain_unlock (target_domain);
10278 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
10283 mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt)
10285 MonoDomain *target_domain, *domain = mono_domain_get ();
10288 MonoJitICallInfo *callinfo = NULL;
10291 * ICALL wrappers are handled specially, since there is only one copy of them
10292 * shared by all appdomains.
10294 if ((method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (strstr (method->name, "__icall_wrapper_") == method->name)) {
10295 const char *icall_name;
10297 icall_name = method->name + strlen ("__icall_wrapper_");
10298 g_assert (icall_name);
10299 callinfo = mono_find_jit_icall_by_name (icall_name);
10300 g_assert (callinfo);
10302 /* Must be domain neutral since there is only one copy */
10303 opt |= MONO_OPT_SHARED;
10306 if (opt & MONO_OPT_SHARED)
10307 target_domain = mono_get_root_domain ();
10309 target_domain = domain;
10311 mono_domain_lock (target_domain);
10313 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
10314 /* We can't use a domain specific method in another domain */
10315 if (! ((domain != target_domain) && !info->domain_neutral)) {
10316 mono_domain_unlock (target_domain);
10317 mono_jit_stats.methods_lookups++;
10318 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
10319 return mono_create_ftnptr (target_domain, info->code_start);
10323 mono_domain_unlock (target_domain);
10324 p = mono_create_ftnptr (target_domain, mono_jit_compile_method_inner (method, target_domain, opt));
10328 if (!callinfo->wrapper) {
10329 callinfo->wrapper = p;
10330 mono_register_jit_icall_wrapper (callinfo, p);
10331 mono_debug_add_icall_wrapper (method, callinfo);
10333 mono_jit_unlock ();
10340 mono_jit_compile_method (MonoMethod *method)
10342 return mono_jit_compile_method_with_opt (method, default_opt);
10346 invalidated_delegate_trampoline (char *desc)
10348 g_error ("Unmanaged code called delegate of type %s which was already garbage collected.\n"
10349 "See http://www.go-mono.com/delegate.html for an explanation and ways to fix this.",
10354 * mono_jit_free_method:
10356 * Free all memory allocated by the JIT for METHOD.
10359 mono_jit_free_method (MonoDomain *domain, MonoMethod *method)
10361 MonoJitDynamicMethodInfo *ji;
10362 gboolean destroy = TRUE;
10364 g_assert (method->dynamic);
10366 mono_domain_lock (domain);
10367 ji = mono_dynamic_code_hash_lookup (domain, method);
10368 mono_domain_unlock (domain);
10372 mono_domain_lock (domain);
10373 g_hash_table_remove (domain->dynamic_code_hash, method);
10374 g_hash_table_remove (domain->jit_code_hash, method);
10375 g_hash_table_remove (domain->jump_trampoline_hash, method);
10376 mono_domain_unlock (domain);
10378 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
10379 if (debug_options.keep_delegates && method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
10381 * Instead of freeing the code, change it to call an error routine
10382 * so people can fix their code.
10384 char *type = mono_type_full_name (&method->klass->byval_arg);
10385 char *type_and_method = g_strdup_printf ("%s.%s", type, method->name);
10388 mono_arch_invalidate_method (ji->ji, invalidated_delegate_trampoline, type_and_method);
10394 * This needs to be done before freeing code_mp, since the code address is the
10395 * key in the table, so if we the code_mp first, another thread can grab the
10396 * same code address and replace our entry in the table.
10398 mono_jit_info_table_remove (domain, ji->ji);
10401 mono_code_manager_destroy (ji->code_mp);
10407 mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method)
10409 MonoDomain *target_domain;
10412 if (default_opt & MONO_OPT_SHARED)
10413 target_domain = mono_get_root_domain ();
10415 target_domain = domain;
10417 mono_domain_lock (target_domain);
10419 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
10420 /* We can't use a domain specific method in another domain */
10421 if (! ((domain != target_domain) && !info->domain_neutral)) {
10422 mono_domain_unlock (target_domain);
10423 mono_jit_stats.methods_lookups++;
10424 return info->code_start;
10428 mono_domain_unlock (target_domain);
10434 * mono_jit_runtime_invoke:
10435 * @method: the method to invoke
10436 * @obj: this pointer
10437 * @params: array of parameter values.
10438 * @exc: used to catch exceptions objects
10441 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
10443 MonoMethod *invoke;
10444 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc, void* compiled_method);
10445 void* compiled_method;
10447 if (obj == NULL && !(method->flags & METHOD_ATTRIBUTE_STATIC) && !method->string_ctor && (method->wrapper_type == 0)) {
10448 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
10452 method = mono_get_inflated_method (method);
10453 invoke = mono_marshal_get_runtime_invoke (method);
10454 runtime_invoke = mono_jit_compile_method (invoke);
10456 /* We need this here becuase mono_marshal_get_runtime_invoke can be place
10457 * the helper method in System.Object and not the target class
10459 mono_runtime_class_init (mono_class_vtable (mono_domain_get (), method->klass));
10461 compiled_method = mono_jit_compile_method (method);
10462 return runtime_invoke (obj, params, exc, compiled_method);
10465 #ifdef PLATFORM_WIN32
10466 #define GET_CONTEXT \
10467 struct sigcontext *ctx = (struct sigcontext*)_dummy;
10470 #define GET_CONTEXT \
10471 void *ctx = context;
10472 #elif defined (MONO_ARCH_USE_SIGACTION)
10473 #define GET_CONTEXT \
10474 void *ctx = context;
10476 #define GET_CONTEXT \
10477 void **_p = (void **)&_dummy; \
10478 struct sigcontext *ctx = (struct sigcontext *)++_p;
10482 #ifdef MONO_ARCH_USE_SIGACTION
10483 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, siginfo_t *info, void *context)
10485 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy)
10489 SIG_HANDLER_SIGNATURE (sigfpe_signal_handler)
10491 MonoException *exc = NULL;
10492 #ifndef MONO_ARCH_USE_SIGACTION
10497 #if defined(MONO_ARCH_HAVE_IS_INT_OVERFLOW)
10498 if (mono_arch_is_int_overflow (ctx, info))
10499 exc = mono_get_exception_arithmetic ();
10501 exc = mono_get_exception_divide_by_zero ();
10503 exc = mono_get_exception_divide_by_zero ();
10506 mono_arch_handle_exception (ctx, exc, FALSE);
10510 SIG_HANDLER_SIGNATURE (sigill_signal_handler)
10512 MonoException *exc;
10515 exc = mono_get_exception_execution_engine ("SIGILL");
10517 mono_arch_handle_exception (ctx, exc, FALSE);
10520 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
10522 #ifndef MONO_ARCH_USE_SIGACTION
10523 #error "Can't use sigaltstack without sigaction"
10529 SIG_HANDLER_SIGNATURE (sigsegv_signal_handler)
10531 MonoException *exc = NULL;
10534 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
10535 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
10539 #ifdef MONO_ARCH_USE_SIGACTION
10540 if (debug_options.collect_pagefault_stats) {
10541 if (mono_raw_buffer_is_pagefault (info->si_addr)) {
10542 mono_raw_buffer_handle_pagefault (info->si_addr);
10545 if (mono_aot_is_pagefault (info->si_addr)) {
10546 mono_aot_handle_pagefault (info->si_addr);
10552 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
10553 /* Can't allocate memory using Boehm GC on altstack */
10554 if (jit_tls->stack_size &&
10555 ((guint8*)info->si_addr >= (guint8*)jit_tls->end_of_stack - jit_tls->stack_size) &&
10556 ((guint8*)info->si_addr < (guint8*)jit_tls->end_of_stack))
10557 exc = mono_domain_get ()->stack_overflow_ex;
10559 exc = mono_domain_get ()->null_reference_ex;
10562 ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx));
10564 mono_handle_native_sigsegv (SIGSEGV, ctx);
10567 mono_arch_handle_exception (ctx, exc, FALSE);
10571 SIG_HANDLER_SIGNATURE (sigabrt_signal_handler)
10576 ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx));
10578 mono_handle_native_sigsegv (SIGABRT, ctx);
10583 SIG_HANDLER_SIGNATURE (sigusr1_signal_handler)
10585 gboolean running_managed;
10586 MonoException *exc;
10587 MonoThread *thread = mono_thread_current ();
10592 if (thread->thread_dump_requested) {
10593 thread->thread_dump_requested = FALSE;
10595 mono_print_thread_dump (ctx);
10600 * This is an async signal, so the code below must not call anything which
10601 * is not async safe. That includes the pthread locking functions. If we
10602 * know that we interrupted managed code, then locking is safe.
10604 ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx));
10605 running_managed = ji != NULL;
10607 exc = mono_thread_request_interruption (running_managed);
10610 mono_arch_handle_exception (ctx, exc, FALSE);
10614 SIG_HANDLER_SIGNATURE (sigprof_signal_handler)
10618 mono_profiler_stat_hit (mono_arch_ip_from_context (ctx), ctx);
10622 SIG_HANDLER_SIGNATURE (sigquit_signal_handler)
10626 printf ("Full thread dump:\n");
10628 mono_threads_request_thread_dump ();
10631 * print_thread_dump () skips the current thread, since sending a signal
10632 * to it would invoke the signal handler below the sigquit signal handler,
10633 * and signal handlers don't create an lmf, so the stack walk could not
10636 mono_print_thread_dump (ctx);
10640 SIG_HANDLER_SIGNATURE (sigint_signal_handler)
10642 MonoException *exc;
10645 exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
10647 mono_arch_handle_exception (ctx, exc, FALSE);
10651 SIG_HANDLER_SIGNATURE (sigusr2_signal_handler)
10653 gboolean enabled = mono_trace_is_enabled ();
10655 mono_trace_enable (!enabled);
10658 #ifndef PLATFORM_WIN32
10660 add_signal_handler (int signo, gpointer handler)
10662 struct sigaction sa;
10664 #ifdef MONO_ARCH_USE_SIGACTION
10665 sa.sa_sigaction = handler;
10666 sigemptyset (&sa.sa_mask);
10667 sa.sa_flags = SA_SIGINFO;
10669 sa.sa_handler = handler;
10670 sigemptyset (&sa.sa_mask);
10673 g_assert (sigaction (signo, &sa, NULL) != -1);
10678 mono_runtime_install_handlers (void)
10680 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
10681 struct sigaction sa;
10684 #ifdef PLATFORM_WIN32
10686 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
10687 win32_seh_set_handler(SIGILL, sigill_signal_handler);
10688 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
10689 if (debug_options.handle_sigint)
10690 win32_seh_set_handler(SIGINT, sigint_signal_handler);
10692 #else /* !PLATFORM_WIN32 */
10694 /* libpthreads has its own implementation of sigaction(),
10695 * but it seems to work well with our current exception
10696 * handlers. If not we must call syscall directly instead
10699 if (debug_options.handle_sigint)
10700 add_signal_handler (SIGINT, sigint_signal_handler);
10702 add_signal_handler (SIGFPE, sigfpe_signal_handler);
10703 add_signal_handler (SIGQUIT, sigquit_signal_handler);
10704 add_signal_handler (SIGILL, sigill_signal_handler);
10705 add_signal_handler (SIGBUS, sigsegv_signal_handler);
10706 if (mono_jit_trace_calls != NULL)
10707 add_signal_handler (SIGUSR2, sigusr2_signal_handler);
10709 add_signal_handler (mono_thread_get_abort_signal (), sigusr1_signal_handler);
10710 signal (SIGPIPE, SIG_IGN);
10712 add_signal_handler (SIGABRT, sigabrt_signal_handler);
10714 /* catch SIGSEGV */
10715 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
10716 sa.sa_sigaction = sigsegv_signal_handler;
10717 sigemptyset (&sa.sa_mask);
10718 sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
10719 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
10721 add_signal_handler (SIGSEGV, sigsegv_signal_handler);
10723 #endif /* PLATFORM_WIN32 */
10727 #ifdef HAVE_LINUX_RTC_H
10728 #include <linux/rtc.h>
10729 #include <sys/ioctl.h>
10731 static int rtc_fd = -1;
10734 enable_rtc_timer (gboolean enable)
10737 flags = fcntl (rtc_fd, F_GETFL);
10739 perror ("getflags");
10746 if (fcntl (rtc_fd, F_SETFL, flags) == -1) {
10747 perror ("setflags");
10755 setup_stat_profiler (void)
10758 struct itimerval itval;
10759 static int inited = 0;
10760 #ifdef HAVE_LINUX_RTC_H
10761 const char *rtc_freq;
10762 if (!inited && (rtc_freq = g_getenv ("MONO_RTC"))) {
10766 freq = atoi (rtc_freq);
10769 rtc_fd = open ("/dev/rtc", O_RDONLY);
10770 if (rtc_fd == -1) {
10771 perror ("open /dev/rtc");
10774 add_signal_handler (SIGPROF, sigprof_signal_handler);
10775 if (ioctl (rtc_fd, RTC_IRQP_SET, freq) == -1) {
10776 perror ("set rtc freq");
10779 if (ioctl (rtc_fd, RTC_PIE_ON, 0) == -1) {
10780 perror ("start rtc");
10783 if (fcntl (rtc_fd, F_SETSIG, SIGPROF) == -1) {
10787 if (fcntl (rtc_fd, F_SETOWN, getpid ()) == -1) {
10791 enable_rtc_timer (TRUE);
10798 itval.it_interval.tv_usec = 999;
10799 itval.it_interval.tv_sec = 0;
10800 itval.it_value = itval.it_interval;
10801 setitimer (ITIMER_PROF, &itval, NULL);
10805 add_signal_handler (SIGPROF, sigprof_signal_handler);
10809 /* mono_jit_create_remoting_trampoline:
10810 * @method: pointer to the method info
10812 * Creates a trampoline which calls the remoting functions. This
10813 * is used in the vtable of transparent proxies.
10815 * Returns: a pointer to the newly created code
10818 mono_jit_create_remoting_trampoline (MonoMethod *method, MonoRemotingTarget target)
10821 guint8 *addr = NULL;
10823 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
10824 (mono_method_signature (method)->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
10825 nm = mono_marshal_get_remoting_invoke_for_target (method, target);
10826 addr = mono_compile_method (nm);
10828 addr = mono_compile_method (method);
10830 return mono_get_addr_from_ftnptr (addr);
10834 mini_parse_debug_options (void)
10836 char *options = getenv ("MONO_DEBUG");
10837 gchar **args, **ptr;
10842 args = g_strsplit (options, ",", -1);
10844 for (ptr = args; ptr && *ptr; ptr++) {
10845 const char *arg = *ptr;
10847 if (!strcmp (arg, "handle-sigint"))
10848 debug_options.handle_sigint = TRUE;
10849 else if (!strcmp (arg, "keep-delegates"))
10850 debug_options.keep_delegates = TRUE;
10851 else if (!strcmp (arg, "collect-pagefault-stats"))
10852 debug_options.collect_pagefault_stats = TRUE;
10854 fprintf (stderr, "Invalid option for the MONO_DEBUG env variable: %s\n", arg);
10855 fprintf (stderr, "Available options: 'handle-sigint', 'keep-delegates', 'collect-pagefault-stats'\n");
10862 mini_init (const char *filename)
10864 MonoDomain *domain;
10866 /* Happens when using the embedding interface */
10867 if (!default_opt_set)
10868 default_opt = mono_parse_default_optimizations (NULL);
10870 InitializeCriticalSection (&jit_mutex);
10872 global_codeman = mono_code_manager_new ();
10873 jit_icall_name_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
10875 mono_arch_cpu_init ();
10877 mono_init_trampolines ();
10879 mono_init_exceptions ();
10881 if (!g_thread_supported ())
10882 g_thread_init (NULL);
10884 if (getenv ("MONO_DEBUG") != NULL)
10885 mini_parse_debug_options ();
10888 * Handle the case when we are called from a thread different from the main thread,
10890 * FIXME: Move this to libgc where it belongs.
10892 * we used to do this only when running on valgrind,
10893 * but it happens also in other setups.
10895 #if defined(HAVE_BOEHM_GC)
10896 #if defined(HAVE_PTHREAD_GETATTR_NP) && defined(HAVE_PTHREAD_ATTR_GETSTACK)
10900 pthread_attr_t attr;
10901 pthread_getattr_np (pthread_self (), &attr);
10902 pthread_attr_getstack (&attr, &sstart, &size);
10903 pthread_attr_destroy (&attr);
10904 /*g_print ("stackbottom pth is: %p\n", (char*)sstart + size);*/
10907 * The calculation above doesn't seem to work on ia64, also we need to set
10908 * GC_register_stackbottom as well, but don't know how.
10911 GC_stackbottom = (char*)sstart + size;
10914 #elif defined(HAVE_PTHREAD_GET_STACKSIZE_NP) && defined(HAVE_PTHREAD_GET_STACKADDR_NP)
10915 GC_stackbottom = (char*)pthread_get_stackaddr_np (pthread_self ());
10918 gsize stack_bottom = (gsize)&domain;
10919 stack_bottom += 4095;
10920 stack_bottom &= ~4095;
10921 /*g_print ("stackbottom is: %p\n", (char*)stack_bottom);*/
10922 GC_stackbottom = (char*)stack_bottom;
10926 MONO_GC_PRE_INIT ();
10928 mono_jit_tls_id = TlsAlloc ();
10929 setup_jit_tls_data ((gpointer)-1, mono_thread_abort);
10933 if (default_opt & MONO_OPT_AOT)
10936 mono_runtime_install_handlers ();
10937 mono_threads_install_cleanup (mini_thread_cleanup);
10939 #define JIT_TRAMPOLINES_WORK
10940 #ifdef JIT_TRAMPOLINES_WORK
10941 mono_install_compile_method (mono_jit_compile_method);
10942 mono_install_free_method (mono_jit_free_method);
10943 mono_install_trampoline (mono_create_jit_trampoline);
10944 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
10945 mono_install_delegate_trampoline (mono_create_delegate_trampoline);
10947 #define JIT_INVOKE_WORKS
10948 #ifdef JIT_INVOKE_WORKS
10949 mono_install_runtime_invoke (mono_jit_runtime_invoke);
10950 mono_install_handler (mono_arch_get_throw_exception ());
10952 mono_install_stack_walk (mono_jit_walk_stack);
10953 mono_install_init_vtable (mono_aot_init_vtable);
10954 mono_install_get_cached_class_info (mono_aot_get_cached_class_info);
10955 mono_install_jit_info_find_in_aot (mono_aot_find_jit_info);
10957 if (debug_options.collect_pagefault_stats) {
10958 mono_raw_buffer_set_make_unreadable (TRUE);
10959 mono_aot_set_make_unreadable (TRUE);
10962 domain = mono_init_from_assembly (filename, filename);
10963 mono_icall_init ();
10965 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
10966 ves_icall_get_frame_info);
10967 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
10968 ves_icall_get_trace);
10969 mono_add_internal_call ("System.Exception::get_trace",
10970 ves_icall_System_Exception_get_trace);
10971 mono_add_internal_call ("System.Security.SecurityFrame::_GetSecurityFrame",
10972 ves_icall_System_Security_SecurityFrame_GetSecurityFrame);
10973 mono_add_internal_call ("System.Security.SecurityFrame::_GetSecurityStack",
10974 ves_icall_System_Security_SecurityFrame_GetSecurityStack);
10975 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
10976 mono_runtime_install_handlers);
10979 create_helper_signature ();
10981 #define JIT_CALLS_WORK
10982 #ifdef JIT_CALLS_WORK
10983 /* Needs to be called here since register_jit_icall depends on it */
10984 mono_marshal_init ();
10986 mono_arch_register_lowlevel_calls ();
10987 register_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
10988 register_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
10989 register_icall (mono_trace_enter_method, "mono_trace_enter_method", NULL, TRUE);
10990 register_icall (mono_trace_leave_method, "mono_trace_leave_method", NULL, TRUE);
10991 register_icall (mono_get_lmf_addr, "mono_get_lmf_addr", "ptr", TRUE);
10992 register_icall (mono_jit_thread_attach, "mono_jit_thread_attach", "void", TRUE);
10993 register_icall (mono_domain_get, "mono_domain_get", "ptr", TRUE);
10995 register_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", "void object", TRUE);
10996 register_icall (mono_arch_get_rethrow_exception (), "mono_arch_rethrow_exception", "void object", TRUE);
10997 register_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name", "void ptr", TRUE);
10998 #if MONO_ARCH_HAVE_THROW_CORLIB_EXCEPTION
10999 register_icall (mono_arch_get_throw_corlib_exception (), "mono_arch_throw_corlib_exception",
11002 register_icall (mono_thread_get_pending_exception, "mono_thread_get_pending_exception", "object", FALSE);
11003 register_icall (mono_thread_interruption_checkpoint, "mono_thread_interruption_checkpoint", "void", FALSE);
11004 register_icall (mono_thread_force_interruption_checkpoint, "mono_thread_force_interruption_checkpoint", "void", FALSE);
11005 register_icall (mono_load_remote_field_new, "mono_load_remote_field_new", "object object ptr ptr", FALSE);
11006 register_icall (mono_store_remote_field_new, "mono_store_remote_field_new", "void object ptr ptr object", FALSE);
11009 * NOTE, NOTE, NOTE, NOTE:
11010 * when adding emulation for some opcodes, remember to also add a dummy
11011 * rule to the burg files, because we need the arity information to be correct.
11013 #ifndef MONO_ARCH_NO_EMULATE_LONG_MUL_OPTS
11014 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", "long long long", mono_llmult, TRUE);
11015 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", "long long long", mono_lldiv, FALSE);
11016 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", "long long long", mono_lldiv_un, FALSE);
11017 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", "long long long", mono_llrem, FALSE);
11018 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", "long long long", mono_llrem_un, FALSE);
11019 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", "long long long", mono_llmult_ovf_un, FALSE);
11020 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", "long long long", mono_llmult_ovf, FALSE);
11023 #ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
11024 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", "long long int32", mono_lshl, TRUE);
11025 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", "long long int32", mono_lshr, TRUE);
11026 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", "long long int32", mono_lshr_un, TRUE);
11029 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
11030 mono_register_opcode_emulation (CEE_DIV, "__emul_idiv", "int32 int32 int32", mono_idiv, TRUE);
11031 mono_register_opcode_emulation (CEE_DIV_UN, "__emul_idiv_un", "int32 int32 int32", mono_idiv_un, TRUE);
11032 mono_register_opcode_emulation (CEE_REM, "__emul_irem", "int32 int32 int32", mono_irem, TRUE);
11033 mono_register_opcode_emulation (CEE_REM_UN, "__emul_irem_un", "int32 int32 int32", mono_irem_un, TRUE);
11036 #ifdef MONO_ARCH_EMULATE_MUL_DIV
11037 mono_register_opcode_emulation (CEE_MUL_OVF, "__emul_imul_ovf", "int32 int32 int32", mono_imul_ovf, TRUE);
11038 mono_register_opcode_emulation (CEE_MUL_OVF_UN, "__emul_imul_ovf_un", "int32 int32 int32", mono_imul_ovf_un, TRUE);
11039 mono_register_opcode_emulation (CEE_MUL, "__emul_imul", "int32 int32 int32", mono_imul, TRUE);
11040 mono_register_opcode_emulation (OP_FDIV, "__emul_fdiv", "double double double", mono_fdiv, TRUE);
11043 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", "ulong double", mono_fconv_u8, FALSE);
11044 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", "uint32 double", mono_fconv_u4, FALSE);
11045 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", "long double", mono_fconv_ovf_i8, FALSE);
11046 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", "ulong double", mono_fconv_ovf_u8, FALSE);
11048 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
11049 mono_register_opcode_emulation (OP_FCONV_TO_I8, "__emul_fconv_to_i8", "long double", mono_fconv_i8, FALSE);
11051 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
11052 mono_register_opcode_emulation (CEE_CONV_R_UN, "__emul_conv_r_un", "double int32", mono_conv_to_r8_un, FALSE);
11054 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
11055 mono_register_opcode_emulation (OP_LCONV_TO_R8, "__emul_lconv_to_r8", "double long", mono_lconv_to_r8, FALSE);
11057 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
11058 mono_register_opcode_emulation (OP_LCONV_TO_R4, "__emul_lconv_to_r4", "float long", mono_lconv_to_r4, FALSE);
11060 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
11061 mono_register_opcode_emulation (OP_LCONV_TO_R_UN, "__emul_lconv_to_r8_un", "double long", mono_lconv_to_r8_un, FALSE);
11063 #ifdef MONO_ARCH_EMULATE_FREM
11064 mono_register_opcode_emulation (OP_FREM, "__emul_frem", "double double double", fmod, FALSE);
11067 #if SIZEOF_VOID_P == 4
11068 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", "uint32 double", mono_fconv_u4, TRUE);
11071 /* other jit icalls */
11072 register_icall (mono_delegate_ctor, "mono_delegate_ctor", "void object object ptr", FALSE);
11073 register_icall (mono_class_static_field_address , "mono_class_static_field_address",
11074 "ptr ptr ptr", FALSE);
11075 register_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", "ptr ptr ptr ptr", FALSE);
11076 register_icall (mono_get_special_static_data, "mono_get_special_static_data", "ptr int", FALSE);
11077 register_icall (mono_ldstr, "mono_ldstr", "object ptr ptr int32", FALSE);
11078 register_icall (mono_helper_stelem_ref, "helper_stelem_ref", "void ptr int32 object", FALSE);
11079 register_icall (mono_helper_stelem_ref_check, "helper_stelem_ref_check", "void object object", FALSE);
11080 register_icall (mono_object_new, "mono_object_new", "object ptr ptr", FALSE);
11081 register_icall (mono_object_new_specific, "mono_object_new_specific", "object ptr", FALSE);
11082 register_icall (mono_array_new, "mono_array_new", "object ptr ptr int32", FALSE);
11083 register_icall (mono_array_new_specific, "mono_array_new_specific", "object ptr int32", FALSE);
11084 register_icall (mono_runtime_class_init, "mono_runtime_class_init", "void ptr", FALSE);
11085 register_icall (mono_ldftn, "mono_ldftn", "ptr ptr", FALSE);
11086 register_icall (mono_ldftn_nosync, "mono_ldftn_nosync", "ptr ptr", FALSE);
11087 register_icall (mono_ldvirtfn, "mono_ldvirtfn", "ptr object ptr", FALSE);
11088 register_icall (mono_helper_compile_generic_method, "compile_generic_method", "ptr object ptr ptr", FALSE);
11089 register_icall (mono_helper_ldstr, "helper_ldstr", "object ptr int", FALSE);
11090 register_icall (mono_helper_ldstr_mscorlib, "helper_ldstr_mscorlib", "object int", FALSE);
11091 register_icall (mono_helper_newobj_mscorlib, "helper_newobj_mscorlib", "object int", FALSE);
11092 register_icall (mono_value_copy, "mono_value_copy", "void ptr ptr ptr", FALSE);
11095 #define JIT_RUNTIME_WORKS
11096 #ifdef JIT_RUNTIME_WORKS
11097 mono_install_runtime_cleanup ((MonoDomainFunc)mini_cleanup);
11098 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
11101 mono_thread_attach (domain);
11105 MonoJitStats mono_jit_stats = {0};
11108 print_jit_stats (void)
11110 if (mono_jit_stats.enabled) {
11111 g_print ("Mono Jit statistics\n");
11112 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
11113 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
11114 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
11115 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
11116 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
11117 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
11118 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
11119 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
11120 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
11121 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
11122 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
11123 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
11124 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
11125 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
11126 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
11127 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
11128 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
11129 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
11131 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
11132 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
11133 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
11134 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
11135 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
11137 g_print ("\nGeneric instances: %ld\n", mono_stats.generic_instance_count);
11138 g_print ("Initialized classes: %ld\n", mono_stats.generic_class_count);
11139 g_print ("Inflated methods: %ld / %ld\n", mono_stats.inflated_method_count_2,
11140 mono_stats.inflated_method_count);
11141 g_print ("Inflated types: %ld\n", mono_stats.inflated_type_count);
11142 g_print ("Generics metadata size: %ld\n", mono_stats.generics_metadata_size);
11144 if (mono_use_security_manager) {
11145 g_print ("\nDecl security check : %ld\n", mono_jit_stats.cas_declsec_check);
11146 g_print ("LinkDemand (user) : %ld\n", mono_jit_stats.cas_linkdemand);
11147 g_print ("LinkDemand (icall) : %ld\n", mono_jit_stats.cas_linkdemand_icall);
11148 g_print ("LinkDemand (pinvoke) : %ld\n", mono_jit_stats.cas_linkdemand_pinvoke);
11149 g_print ("LinkDemand (aptc) : %ld\n", mono_jit_stats.cas_linkdemand_aptc);
11150 g_print ("Demand (code gen) : %ld\n", mono_jit_stats.cas_demand_generation);
11152 if (debug_options.collect_pagefault_stats) {
11153 g_print ("Metadata pagefaults : %d\n", mono_raw_buffer_get_n_pagefaults ());
11154 g_print ("AOT pagefaults : %d\n", mono_aot_get_n_pagefaults ());
11160 mini_cleanup (MonoDomain *domain)
11162 #ifdef HAVE_LINUX_RTC_H
11164 enable_rtc_timer (FALSE);
11168 * mono_runtime_cleanup() and mono_domain_finalize () need to
11169 * be called early since they need the execution engine still
11170 * fully working (mono_domain_finalize may invoke managed finalizers
11171 * and mono_runtime_cleanup will wait for other threads to finish).
11173 mono_domain_finalize (domain, 2000);
11175 /* This accesses metadata so needs to be called before runtime shutdown */
11176 print_jit_stats ();
11178 mono_runtime_cleanup (domain);
11180 mono_profiler_shutdown ();
11182 mono_debug_cleanup ();
11184 mono_icall_cleanup ();
11186 #ifdef PLATFORM_WIN32
11187 win32_seh_cleanup();
11190 mono_domain_free (domain, TRUE);
11192 mono_code_manager_destroy (global_codeman);
11193 g_hash_table_destroy (jit_icall_name_hash);
11194 if (class_init_hash_addr)
11195 g_hash_table_destroy (class_init_hash_addr);
11196 g_free (emul_opcode_map);
11200 mono_trace_cleanup ();
11202 mono_counters_dump (-1, stdout);
11204 TlsFree(mono_jit_tls_id);
11206 DeleteCriticalSection (&jit_mutex);
11208 DeleteCriticalSection (&mono_delegate_section);
11212 mono_set_defaults (int verbose_level, guint32 opts)
11214 mini_verbose = verbose_level;
11215 default_opt = opts;
11216 default_opt_set = TRUE;
11220 mono_precompile_assembly (MonoAssembly *ass, void *user_data)
11222 GHashTable *assemblies = (GHashTable*)user_data;
11223 MonoImage *image = mono_assembly_get_image (ass);
11224 MonoMethod *method, *invoke;
11227 if (g_hash_table_lookup (assemblies, ass))
11230 g_hash_table_insert (assemblies, ass, ass);
11232 if (mini_verbose > 0)
11233 printf ("PRECOMPILE: %s.\n", mono_image_get_filename (image));
11235 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
11236 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
11237 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
11241 if (mini_verbose > 1) {
11242 char * desc = mono_method_full_name (method, TRUE);
11243 g_print ("Compiling %d %s\n", count, desc);
11246 mono_compile_method (method);
11247 if (strcmp (method->name, "Finalize") == 0) {
11248 invoke = mono_marshal_get_runtime_invoke (method);
11249 mono_compile_method (invoke);
11251 if (method->klass->marshalbyref && mono_method_signature (method)->hasthis) {
11252 invoke = mono_marshal_get_remoting_invoke_with_check (method);
11253 mono_compile_method (invoke);
11257 /* Load and precompile referenced assemblies as well */
11258 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_ASSEMBLYREF); ++i) {
11259 mono_assembly_load_reference (image, i);
11260 if (image->references [i])
11261 mono_precompile_assembly (image->references [i], assemblies);
11265 void mono_precompile_assemblies ()
11267 GHashTable *assemblies = g_hash_table_new (NULL, NULL);
11269 mono_assembly_foreach ((GFunc)mono_precompile_assembly, assemblies);
11271 g_hash_table_destroy (assemblies);