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 sun // Solaris x86
18 #include <sys/types.h>
19 #include <sys/ucontext.h>
22 #ifdef HAVE_VALGRIND_MEMCHECK_H
23 #include <valgrind/memcheck.h>
26 #include <mono/metadata/assembly.h>
27 #include <mono/metadata/loader.h>
28 #include <mono/metadata/cil-coff.h>
29 #include <mono/metadata/tabledefs.h>
30 #include <mono/metadata/class.h>
31 #include <mono/metadata/object.h>
32 #include <mono/metadata/exception.h>
33 #include <mono/metadata/opcodes.h>
34 #include <mono/metadata/mono-endian.h>
35 #include <mono/metadata/tokentype.h>
36 #include <mono/metadata/tabledefs.h>
37 #include <mono/metadata/threads.h>
38 #include <mono/metadata/marshal.h>
39 #include <mono/metadata/socket-io.h>
40 #include <mono/metadata/appdomain.h>
41 #include <mono/metadata/debug-helpers.h>
42 #include <mono/io-layer/io-layer.h>
43 #include "mono/metadata/profiler.h"
44 #include <mono/metadata/profiler-private.h>
45 #include <mono/metadata/mono-config.h>
46 #include <mono/metadata/environment.h>
47 #include <mono/metadata/mono-debug.h>
48 #include <mono/metadata/mono-debug-debugger.h>
49 #include <mono/metadata/monitor.h>
50 #include <mono/metadata/security-manager.h>
51 #include <mono/metadata/threads-types.h>
52 #include <mono/metadata/rawbuffer.h>
53 #include <mono/utils/mono-math.h>
54 #include <mono/utils/mono-compiler.h>
55 #include <mono/os/gc_wrapper.h>
63 #include "jit-icalls.c"
68 * this is used to determine when some branch optimizations are possible: we exclude FP compares
69 * because they have weird semantics with NaNs.
71 #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))
72 #define MONO_IS_COND_BRANCH_NOFP(ins) (MONO_IS_COND_BRANCH_OP(ins) && (ins)->inst_left->inst_left->type != STACK_R8)
74 #define MONO_CHECK_THIS(ins) (mono_method_signature (cfg->method)->hasthis && (ins)->ssa_op == MONO_SSA_LOAD && (ins)->inst_left->inst_c0 == 0)
76 static void setup_stat_profiler (void);
77 gboolean mono_arch_print_tree(MonoInst *tree, int arity);
78 static gpointer mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt);
79 static gpointer mono_jit_compile_method (MonoMethod *method);
80 static gpointer mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method);
82 static void handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src,
83 const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native);
85 static void dec_foreach (MonoInst *tree, MonoCompile *cfg);
87 static int mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
88 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
89 guint inline_offset, gboolean is_virtual_call);
91 extern guint8 mono_burg_arity [];
92 /* helper methods signature */
93 static MonoMethodSignature *helper_sig_class_init_trampoline = NULL;
94 static MonoMethodSignature *helper_sig_domain_get = NULL;
96 static guint32 default_opt = 0;
98 guint32 mono_jit_tls_id = -1;
99 MonoTraceSpec *mono_jit_trace_calls = NULL;
100 gboolean mono_break_on_exc = FALSE;
102 gboolean mono_compile_aot = FALSE;
104 gboolean mono_use_security_manager = FALSE;
106 static int mini_verbose = 0;
108 #define mono_jit_lock() EnterCriticalSection (&jit_mutex)
109 #define mono_jit_unlock() LeaveCriticalSection (&jit_mutex)
110 static CRITICAL_SECTION jit_mutex;
112 static GHashTable *class_init_hash_addr = NULL;
114 static MonoCodeManager *global_codeman = NULL;
116 static GHashTable *jit_icall_name_hash = NULL;
118 static MonoDebugOptions debug_options;
121 * Address of the trampoline code. This is used by the debugger to check
122 * whether a method is a trampoline.
124 guint8* mono_trampoline_code [MONO_TRAMPOLINE_NUM];
127 mono_running_on_valgrind (void)
129 #ifdef HAVE_VALGRIND_MEMCHECK_H
130 if (RUNNING_ON_VALGRIND)
140 * mono_create_ftnptr:
142 * Given a function address, create a function descriptor for it.
143 * This is only needed on IA64.
146 mono_create_ftnptr (MonoDomain *domain, gpointer addr)
151 mono_domain_lock (domain);
152 desc = mono_code_manager_reserve (domain->code_mp, 2 * sizeof (gpointer));
153 mono_domain_unlock (domain);
165 G_GNUC_UNUSED static char*
166 get_method_from_ip (void *ip)
172 MonoDomain *domain = mono_domain_get ();
174 ji = mono_jit_info_table_find (domain, ip);
178 method = mono_method_full_name (ji->method, TRUE);
179 source = mono_debug_source_location_from_address (ji->method, (guint32)((guint8*)ip - (guint8*)ji->code_start), NULL, domain);
181 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);
192 return get_method_from_ip (ip);
196 G_GNUC_UNUSED static void
197 print_method_from_ip (void *ip)
202 MonoDomain *domain = mono_domain_get ();
204 ji = mono_jit_info_table_find (domain, ip);
206 g_print ("No method at %p\n", ip);
209 method = mono_method_full_name (ji->method, TRUE);
210 source = mono_debug_source_location_from_address (ji->method, (guint32)((guint8*)ip - (guint8*)ji->code_start), NULL, domain);
212 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);
215 g_print ("%s\n", source);
222 mono_print_method_from_ip (void *ip)
224 print_method_from_ip (ip);
228 * mono_method_same_domain:
230 * Determine whenever two compiled methods are in the same domain, thus
231 * the address of the callee can be embedded in the caller.
233 gboolean mono_method_same_domain (MonoJitInfo *caller, MonoJitInfo *callee)
235 if (!caller || !callee)
239 * If the call was made from domain-neutral to domain-specific
240 * code, we can't patch the call site.
242 if (caller->domain_neutral && !callee->domain_neutral)
245 if ((caller->method->klass == mono_defaults.appdomain_class) &&
246 (strstr (caller->method->name, "InvokeInDomain"))) {
247 /* The InvokeInDomain methods change the current appdomain */
255 * mono_global_codeman_reserve:
257 * Allocate code memory from the global code manager.
259 void *mono_global_codeman_reserve (int size)
263 if (!global_codeman) {
264 /* This can happen during startup */
265 global_codeman = mono_code_manager_new ();
266 return mono_code_manager_reserve (global_codeman, size);
270 ptr = mono_code_manager_reserve (global_codeman, size);
277 mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token)
279 MonoJumpInfoToken *res = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoToken));
286 #define MONO_INIT_VARINFO(vi,id) do { \
287 (vi)->range.first_use.pos.bid = 0xffff; \
293 * Basic blocks have two numeric identifiers:
294 * dfn: Depth First Number
295 * block_num: unique ID assigned at bblock creation
297 #define NEW_BBLOCK(cfg) (mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)))
298 #define ADD_BBLOCK(cfg,bbhash,b) do { \
299 g_hash_table_insert (bbhash, (b)->cil_code, (b)); \
300 (b)->block_num = cfg->num_bblocks++; \
301 (b)->real_offset = real_offset; \
304 #define GET_BBLOCK(cfg,bbhash,tblock,ip) do { \
305 (tblock) = g_hash_table_lookup (bbhash, (ip)); \
307 if ((ip) >= end || (ip) < header->code) goto unverified; \
308 (tblock) = NEW_BBLOCK (cfg); \
309 (tblock)->cil_code = (ip); \
310 ADD_BBLOCK (cfg, (bbhash), (tblock)); \
314 #define CHECK_BBLOCK(target,ip,tblock) do { \
315 if ((target) < (ip) && !(tblock)->code) { \
316 bb_recheck = g_list_prepend (bb_recheck, (tblock)); \
317 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)); \
321 #define NEW_ICONST(cfg,dest,val) do { \
322 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
323 (dest)->opcode = OP_ICONST; \
324 (dest)->inst_c0 = (val); \
325 (dest)->type = STACK_I4; \
328 #define NEW_PCONST(cfg,dest,val) do { \
329 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
330 (dest)->opcode = OP_PCONST; \
331 (dest)->inst_p0 = (val); \
332 (dest)->type = STACK_PTR; \
336 #ifdef MONO_ARCH_NEED_GOT_VAR
338 #define NEW_PATCH_INFO(cfg,dest,el1,el2) do { \
339 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
340 (dest)->opcode = OP_PATCH_INFO; \
341 (dest)->inst_left = (gpointer)(el1); \
342 (dest)->inst_right = (gpointer)(el2); \
345 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do { \
346 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
347 (dest)->opcode = cfg->compile_aot ? OP_GOT_ENTRY : OP_PCONST; \
348 if (cfg->compile_aot) { \
349 MonoInst *group, *got_var, *got_loc; \
350 got_loc = mono_get_got_var (cfg); \
351 NEW_TEMPLOAD ((cfg), got_var, got_loc->inst_c0); \
352 NEW_PATCH_INFO ((cfg), group, cons, patch_type); \
353 (dest)->inst_p0 = got_var; \
354 (dest)->inst_p1 = group; \
356 (dest)->inst_p0 = (cons); \
357 (dest)->inst_i1 = (gpointer)(patch_type); \
359 (dest)->type = STACK_PTR; \
362 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,stack_type) do { \
363 MonoInst *group, *got_var, *got_loc; \
364 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
365 (dest)->opcode = OP_GOT_ENTRY; \
366 got_loc = mono_get_got_var (cfg); \
367 NEW_TEMPLOAD ((cfg), got_var, got_loc->inst_c0); \
368 NEW_PATCH_INFO ((cfg), group, NULL, patch_type); \
369 group->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
370 (dest)->inst_p0 = got_var; \
371 (dest)->inst_p1 = group; \
372 (dest)->type = (stack_type); \
377 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do { \
378 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
379 (dest)->opcode = cfg->compile_aot ? OP_AOTCONST : OP_PCONST; \
380 (dest)->inst_p0 = (cons); \
381 (dest)->inst_i1 = (gpointer)(patch_type); \
382 (dest)->type = STACK_PTR; \
385 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,stack_type) do { \
386 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
387 (dest)->opcode = OP_AOTCONST; \
388 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
389 (dest)->inst_p1 = (gpointer)(patch_type); \
390 (dest)->type = (stack_type); \
395 #define NEW_CLASSCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_CLASS, (val))
397 #define NEW_IMAGECONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_IMAGE, (val))
399 #define NEW_FIELDCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_FIELD, (val))
401 #define NEW_METHODCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHODCONST, (val))
403 #define NEW_VTABLECONST(cfg,dest,vtable) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_VTABLE, cfg->compile_aot ? (gpointer)((vtable)->klass) : (vtable))
405 #define NEW_SFLDACONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_SFLDA, (val))
407 #define NEW_LDSTRCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDSTR, (image), (token), STACK_OBJ)
409 #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)
411 #define NEW_LDTOKENCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDTOKEN, (image), (token), STACK_PTR)
413 #define NEW_DECLSECCONST(cfg,dest,image,entry) do { \
414 if (cfg->compile_aot) { \
415 NEW_AOTCONST_TOKEN (cfg, dest, MONO_PATCH_INFO_DECLSEC, image, (entry).index, STACK_OBJ); \
417 NEW_PCONST (cfg, args [0], (entry).blob); \
421 #define NEW_DOMAINCONST(cfg,dest) do { \
422 if (cfg->opt & MONO_OPT_SHARED) { \
423 /* avoid depending on undefined C behavior in sequence points */ \
424 MonoInst* __domain_var = mono_get_domainvar (cfg); \
425 NEW_TEMPLOAD (cfg, dest, __domain_var->inst_c0); \
427 NEW_PCONST (cfg, dest, (cfg)->domain); \
431 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
433 #define NEW_ARGLOAD(cfg,dest,num) do { \
434 if (arg_array [(num)]->opcode == OP_ICONST) (dest) = arg_array [(num)]; else { \
435 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
436 (dest)->ssa_op = MONO_SSA_LOAD; \
437 (dest)->inst_i0 = arg_array [(num)]; \
438 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
439 type_to_eval_stack_type (param_types [(num)], (dest)); \
440 (dest)->klass = (dest)->inst_i0->klass; \
443 #define NEW_LOCLOAD(cfg,dest,num) do { \
444 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
445 (dest)->ssa_op = MONO_SSA_LOAD; \
446 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
447 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
448 type_to_eval_stack_type (header->locals [(num)], (dest)); \
449 (dest)->klass = (dest)->inst_i0->klass; \
452 #define NEW_LOCLOADA(cfg,dest,num) do { \
453 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
454 (dest)->ssa_op = MONO_SSA_ADDRESS_TAKEN; \
455 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
456 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
457 (dest)->opcode = OP_LDADDR; \
458 (dest)->type = STACK_MP; \
459 (dest)->klass = (dest)->inst_i0->klass; \
460 if (!MONO_TYPE_ISSTRUCT (header->locals [(num)])) \
461 (cfg)->disable_ssa = TRUE; \
464 #define NEW_RETLOADA(cfg,dest) do { \
465 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
466 (dest)->ssa_op = MONO_SSA_ADDRESS_TAKEN; \
467 (dest)->inst_i0 = (cfg)->ret; \
468 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
469 (dest)->opcode = cfg->ret_var_is_local ? OP_LDADDR : CEE_LDIND_I; \
470 (dest)->type = STACK_MP; \
471 (dest)->klass = (dest)->inst_i0->klass; \
472 (cfg)->disable_ssa = TRUE; \
475 #define NEW_ARGLOADA(cfg,dest,num) do { \
476 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
477 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
478 (dest)->ssa_op = MONO_SSA_ADDRESS_TAKEN; \
479 (dest)->inst_i0 = arg_array [(num)]; \
480 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
481 (dest)->opcode = OP_LDADDR; \
482 (dest)->type = STACK_MP; \
483 (dest)->klass = (dest)->inst_i0->klass; \
484 (cfg)->disable_ssa = TRUE; \
487 #define NEW_TEMPLOAD(cfg,dest,num) do { \
488 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
489 (dest)->ssa_op = MONO_SSA_LOAD; \
490 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
491 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
492 type_to_eval_stack_type ((dest)->inst_i0->inst_vtype, (dest)); \
493 (dest)->klass = (dest)->inst_i0->klass; \
496 #define NEW_TEMPLOADA(cfg,dest,num) do { \
497 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
498 (dest)->ssa_op = MONO_SSA_ADDRESS_TAKEN; \
499 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
500 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
501 (dest)->opcode = OP_LDADDR; \
502 (dest)->type = STACK_MP; \
503 (dest)->klass = (dest)->inst_i0->klass; \
504 if (!MONO_TYPE_ISSTRUCT (cfg->varinfo [(num)]->inst_vtype)) \
505 (cfg)->disable_ssa = TRUE; \
509 #define NEW_INDLOAD(cfg,dest,addr,vtype) do { \
510 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
511 (dest)->inst_left = addr; \
512 (dest)->opcode = mono_type_to_ldind (vtype); \
513 type_to_eval_stack_type (vtype, (dest)); \
514 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
517 #define NEW_INDSTORE(cfg,dest,addr,value,vtype) do { \
518 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
519 (dest)->inst_i0 = addr; \
520 (dest)->opcode = mono_type_to_stind (vtype); \
521 (dest)->inst_i1 = (value); \
522 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
525 #define NEW_TEMPSTORE(cfg,dest,num,inst) do { \
526 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
527 (dest)->ssa_op = MONO_SSA_STORE; \
528 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
529 (dest)->opcode = mono_type_to_stind ((dest)->inst_i0->inst_vtype); \
530 (dest)->inst_i1 = (inst); \
531 (dest)->klass = (dest)->inst_i0->klass; \
534 #define NEW_LOCSTORE(cfg,dest,num,inst) do { \
535 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
536 (dest)->opcode = mono_type_to_stind (header->locals [(num)]); \
537 (dest)->ssa_op = MONO_SSA_STORE; \
538 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
539 (dest)->inst_i1 = (inst); \
540 (dest)->klass = (dest)->inst_i0->klass; \
543 #define NEW_ARGSTORE(cfg,dest,num,inst) do { \
544 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
545 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
546 (dest)->opcode = mono_type_to_stind (param_types [(num)]); \
547 (dest)->ssa_op = MONO_SSA_STORE; \
548 (dest)->inst_i0 = arg_array [(num)]; \
549 (dest)->inst_i1 = (inst); \
550 (dest)->klass = (dest)->inst_i0->klass; \
553 #define NEW_DUMMY_USE(cfg,dest,load) do { \
554 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
555 (dest)->opcode = OP_DUMMY_USE; \
556 (dest)->inst_left = (load); \
559 #define NEW_DUMMY_STORE(cfg,dest,num) do { \
560 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
561 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
562 (dest)->opcode = OP_DUMMY_STORE; \
563 (dest)->klass = (dest)->inst_i0->klass; \
566 #define ADD_BINOP(op) do { \
567 MONO_INST_NEW (cfg, ins, (op)); \
568 ins->cil_code = ip; \
570 ins->inst_i0 = sp [0]; \
571 ins->inst_i1 = sp [1]; \
573 type_from_op (ins); \
577 #define ADD_UNOP(op) do { \
578 MONO_INST_NEW (cfg, ins, (op)); \
579 ins->cil_code = ip; \
581 ins->inst_i0 = sp [0]; \
583 type_from_op (ins); \
587 #define ADD_BINCOND(next_block) do { \
590 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
591 cmp->inst_i0 = sp [0]; \
592 cmp->inst_i1 = sp [1]; \
593 cmp->cil_code = ins->cil_code; \
594 type_from_op (cmp); \
596 ins->inst_i0 = cmp; \
597 MONO_ADD_INS (bblock, ins); \
598 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
599 GET_BBLOCK (cfg, bbhash, tblock, target); \
600 link_bblock (cfg, bblock, tblock); \
601 ins->inst_true_bb = tblock; \
602 CHECK_BBLOCK (target, ip, tblock); \
603 if ((next_block)) { \
604 link_bblock (cfg, bblock, (next_block)); \
605 ins->inst_false_bb = (next_block); \
606 start_new_bblock = 1; \
608 GET_BBLOCK (cfg, bbhash, tblock, ip); \
609 link_bblock (cfg, bblock, tblock); \
610 ins->inst_false_bb = tblock; \
611 start_new_bblock = 2; \
615 /* FIXME: handle float, long ... */
616 #define ADD_UNCOND(istrue) do { \
619 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
620 cmp->inst_i0 = sp [0]; \
621 switch (cmp->inst_i0->type) { \
623 cmp->inst_i1 = zero_int64; break; \
625 cmp->inst_i1 = zero_r8; break; \
628 cmp->inst_i1 = zero_ptr; break; \
630 cmp->inst_i1 = zero_obj; break; \
632 cmp->inst_i1 = zero_int32; \
634 cmp->cil_code = ins->cil_code; \
635 type_from_op (cmp); \
637 ins->inst_i0 = cmp; \
638 ins->opcode = (istrue)? CEE_BNE_UN: CEE_BEQ; \
639 MONO_ADD_INS (bblock, ins); \
640 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
641 GET_BBLOCK (cfg, bbhash, tblock, target); \
642 link_bblock (cfg, bblock, tblock); \
643 ins->inst_true_bb = tblock; \
644 CHECK_BBLOCK (target, ip, tblock); \
645 GET_BBLOCK (cfg, bbhash, tblock, ip); \
646 link_bblock (cfg, bblock, tblock); \
647 ins->inst_false_bb = tblock; \
648 start_new_bblock = 2; \
651 #define NEW_LDELEMA(cfg,dest,sp,k) do { \
652 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
653 (dest)->opcode = CEE_LDELEMA; \
654 (dest)->inst_left = (sp) [0]; \
655 (dest)->inst_right = (sp) [1]; \
656 (dest)->type = STACK_MP; \
657 (dest)->klass = (k); \
658 (cfg)->flags |= MONO_CFG_HAS_LDELEMA; \
661 #define NEW_GROUP(cfg,dest,el1,el2) do { \
662 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
663 (dest)->opcode = OP_GROUP; \
664 (dest)->inst_left = (el1); \
665 (dest)->inst_right = (el2); \
670 compare_bblock (gconstpointer a, gconstpointer b)
672 const MonoBasicBlock *b1 = a;
673 const MonoBasicBlock *b2 = b;
675 return b2->cil_code - b1->cil_code;
680 * link_bblock: Links two basic blocks
682 * links two basic blocks in the control flow graph, the 'from'
683 * argument is the starting block and the 'to' argument is the block
684 * the control flow ends to after 'from'.
687 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
689 MonoBasicBlock **newa;
693 if (from->cil_code) {
695 g_print ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
697 g_print ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
700 g_print ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
702 g_print ("edge from entry to exit\n");
706 for (i = 0; i < from->out_count; ++i) {
707 if (to == from->out_bb [i]) {
713 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
714 for (i = 0; i < from->out_count; ++i) {
715 newa [i] = from->out_bb [i];
723 for (i = 0; i < to->in_count; ++i) {
724 if (from == to->in_bb [i]) {
730 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
731 for (i = 0; i < to->in_count; ++i) {
732 newa [i] = to->in_bb [i];
741 * mono_find_block_region:
743 * We mark each basic block with a region ID. We use that to avoid BB
744 * optimizations when blocks are in different regions.
747 * A region token that encodes where this region is, and information
748 * about the clause owner for this block.
750 * The region encodes the try/catch/filter clause that owns this block
751 * as well as the type. -1 is a special value that represents a block
752 * that is in none of try/catch/filter.
755 mono_find_block_region (MonoCompile *cfg, int offset)
757 MonoMethod *method = cfg->method;
758 MonoMethodHeader *header = mono_method_get_header (method);
759 MonoExceptionClause *clause;
762 /* first search for handlers and filters */
763 for (i = 0; i < header->num_clauses; ++i) {
764 clause = &header->clauses [i];
765 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->data.filter_offset) &&
766 (offset < (clause->handler_offset)))
767 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
769 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
770 if (clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)
771 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
773 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
777 /* search the try blocks */
778 for (i = 0; i < header->num_clauses; ++i) {
779 clause = &header->clauses [i];
780 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
781 return ((i + 1) << 8) | clause->flags;
788 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
790 MonoMethod *method = cfg->method;
791 MonoMethodHeader *header = mono_method_get_header (method);
792 MonoExceptionClause *clause;
793 MonoBasicBlock *handler;
797 for (i = 0; i < header->num_clauses; ++i) {
798 clause = &header->clauses [i];
799 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
800 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
801 if (clause->flags == type) {
802 handler = g_hash_table_lookup (cfg->bb_hash, header->code + clause->handler_offset);
804 res = g_list_append (res, handler);
812 mono_find_spvar_for_region (MonoCompile *cfg, int region)
814 return g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
818 mono_create_spvar_for_region (MonoCompile *cfg, int region)
822 var = g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
826 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
827 /* prevent it from being register allocated */
828 var->flags |= MONO_INST_INDIRECT;
830 g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
834 mono_find_exvar_for_offset (MonoCompile *cfg, int offset)
836 return g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
840 mono_create_exvar_for_offset (MonoCompile *cfg, int offset)
844 var = g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
848 var = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
849 /* prevent it from being register allocated */
850 var->flags |= MONO_INST_INDIRECT;
852 g_hash_table_insert (cfg->exvars, GINT_TO_POINTER (offset), var);
858 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
862 array [*dfn] = start;
863 /*g_print ("visit %d at %p (BB%ld)\n", *dfn, start->cil_code, start->block_num);*/
864 for (i = 0; i < start->out_count; ++i) {
865 if (start->out_bb [i]->dfn)
868 start->out_bb [i]->dfn = *dfn;
869 start->out_bb [i]->df_parent = start;
870 array [*dfn] = start->out_bb [i];
871 df_visit (start->out_bb [i], dfn, array);
877 MonoBasicBlock *best;
881 previous_foreach (gconstpointer key, gpointer val, gpointer data)
883 PrevStruct *p = data;
884 MonoBasicBlock *bb = val;
885 //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,
886 //bb->method == p->best->method, bb->cil_code < p->code, bb->cil_code > p->best->cil_code);
888 if (bb->cil_code && bb->cil_code < p->code && bb->cil_code > p->best->cil_code)
892 static MonoBasicBlock*
893 find_previous (GHashTable *bb_hash, MonoBasicBlock *start, const guchar *code) {
899 g_hash_table_foreach (bb_hash, (GHFunc)previous_foreach, &p);
904 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
913 * FIXME: take into account all the details:
914 * second may have been the target of more than one bblock
916 second->out_count = first->out_count;
917 second->out_bb = first->out_bb;
919 for (i = 0; i < first->out_count; ++i) {
920 bb = first->out_bb [i];
921 for (j = 0; j < bb->in_count; ++j) {
922 if (bb->in_bb [j] == first)
923 bb->in_bb [j] = second;
927 first->out_count = 0;
928 first->out_bb = NULL;
929 link_bblock (cfg, first, second);
931 second->last_ins = first->last_ins;
933 /*g_print ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
934 for (inst = first->code; inst && inst->next; inst = inst->next) {
935 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
936 g_print ("found %p: %s", inst->next->cil_code, code);
938 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
939 second->code = inst->next;
941 first->last_ins = inst;
942 second->next_bb = first->next_bb;
943 first->next_bb = second;
948 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
954 reverse_branch_op (guint32 opcode)
956 static const int reverse_map [] = {
957 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
958 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
960 static const int reverse_fmap [] = {
961 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
962 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
964 static const int reverse_lmap [] = {
965 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
966 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
968 static const int reverse_imap [] = {
969 OP_IBNE_UN, OP_IBLT, OP_IBLE, OP_IBGT, OP_IBGE,
970 OP_IBEQ, OP_IBLT_UN, OP_IBLE_UN, OP_IBGT_UN, OP_IBGE_UN
973 if (opcode >= CEE_BEQ && opcode <= CEE_BLT_UN) {
974 opcode = reverse_map [opcode - CEE_BEQ];
975 } else if (opcode >= OP_FBEQ && opcode <= OP_FBLT_UN) {
976 opcode = reverse_fmap [opcode - OP_FBEQ];
977 } else if (opcode >= OP_LBEQ && opcode <= OP_LBLT_UN) {
978 opcode = reverse_lmap [opcode - OP_LBEQ];
979 } else if (opcode >= OP_IBEQ && opcode <= OP_IBLT_UN) {
980 opcode = reverse_imap [opcode - OP_IBEQ];
982 g_assert_not_reached ();
988 mono_type_to_ldind (MonoType *type)
994 switch (type->type) {
998 case MONO_TYPE_BOOLEAN:
1001 return CEE_LDIND_I2;
1003 case MONO_TYPE_CHAR:
1004 return CEE_LDIND_U2;
1006 return CEE_LDIND_I4;
1008 return CEE_LDIND_U4;
1012 case MONO_TYPE_FNPTR:
1014 case MONO_TYPE_CLASS:
1015 case MONO_TYPE_STRING:
1016 case MONO_TYPE_OBJECT:
1017 case MONO_TYPE_SZARRAY:
1018 case MONO_TYPE_ARRAY:
1019 return CEE_LDIND_REF;
1022 return CEE_LDIND_I8;
1024 return CEE_LDIND_R4;
1026 return CEE_LDIND_R8;
1027 case MONO_TYPE_VALUETYPE:
1028 if (type->data.klass->enumtype) {
1029 type = type->data.klass->enum_basetype;
1033 case MONO_TYPE_TYPEDBYREF:
1035 case MONO_TYPE_GENERICINST:
1036 type = &type->data.generic_class->container_class->byval_arg;
1039 g_error ("unknown type 0x%02x in type_to_ldind", type->type);
1045 mono_type_to_stind (MonoType *type)
1051 switch (type->type) {
1054 case MONO_TYPE_BOOLEAN:
1055 return CEE_STIND_I1;
1058 case MONO_TYPE_CHAR:
1059 return CEE_STIND_I2;
1062 return CEE_STIND_I4;
1066 case MONO_TYPE_FNPTR:
1068 case MONO_TYPE_CLASS:
1069 case MONO_TYPE_STRING:
1070 case MONO_TYPE_OBJECT:
1071 case MONO_TYPE_SZARRAY:
1072 case MONO_TYPE_ARRAY:
1073 return CEE_STIND_REF;
1076 return CEE_STIND_I8;
1078 return CEE_STIND_R4;
1080 return CEE_STIND_R8;
1081 case MONO_TYPE_VALUETYPE:
1082 if (type->data.klass->enumtype) {
1083 type = type->data.klass->enum_basetype;
1087 case MONO_TYPE_TYPEDBYREF:
1089 case MONO_TYPE_GENERICINST:
1090 type = &type->data.generic_class->container_class->byval_arg;
1093 g_error ("unknown type 0x%02x in type_to_stind", type->type);
1099 * Returns the type used in the eval stack when @type is loaded.
1100 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
1103 type_to_eval_stack_type (MonoType *type, MonoInst *inst)
1108 inst->type = STACK_MP;
1112 klass = mono_class_from_mono_type (type);
1115 switch (type->type) {
1116 case MONO_TYPE_VOID:
1117 inst->type = STACK_INV;
1121 case MONO_TYPE_BOOLEAN:
1124 case MONO_TYPE_CHAR:
1127 inst->type = STACK_I4;
1132 case MONO_TYPE_FNPTR:
1133 inst->type = STACK_PTR;
1135 case MONO_TYPE_CLASS:
1136 case MONO_TYPE_STRING:
1137 case MONO_TYPE_OBJECT:
1138 case MONO_TYPE_SZARRAY:
1139 case MONO_TYPE_ARRAY:
1140 inst->type = STACK_OBJ;
1144 inst->type = STACK_I8;
1148 inst->type = STACK_R8;
1150 case MONO_TYPE_VALUETYPE:
1151 if (type->data.klass->enumtype) {
1152 type = type->data.klass->enum_basetype;
1155 inst->klass = klass;
1156 inst->type = STACK_VTYPE;
1159 case MONO_TYPE_TYPEDBYREF:
1160 inst->klass = mono_defaults.typed_reference_class;
1161 inst->type = STACK_VTYPE;
1163 case MONO_TYPE_GENERICINST:
1164 type = &type->data.generic_class->container_class->byval_arg;
1167 g_error ("unknown type 0x%02x in eval stack type", type->type);
1172 * The following tables are used to quickly validate the IL code in type_from_op ().
1175 bin_num_table [STACK_MAX] [STACK_MAX] = {
1176 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1177 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1178 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1179 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1180 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
1181 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
1182 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1183 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1188 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
1191 /* reduce the size of this table */
1193 bin_int_table [STACK_MAX] [STACK_MAX] = {
1194 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1195 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1196 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1197 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1198 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1199 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1200 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1201 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1205 bin_comp_table [STACK_MAX] [STACK_MAX] = {
1207 {0, 1, 0, 1, 0, 0, 4, 0},
1208 {0, 0, 1, 0, 0, 0, 0, 0},
1209 {0, 1, 0, 1, 0, 2, 4, 0},
1210 {0, 0, 0, 0, 1, 0, 0, 0},
1211 {0, 0, 0, 2, 0, 1, 0, 0},
1212 {0, 4, 0, 4, 0, 0, 3, 0},
1213 {0, 0, 0, 0, 0, 0, 0, 0},
1216 /* reduce the size of this table */
1218 shift_table [STACK_MAX] [STACK_MAX] = {
1219 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1220 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1221 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1222 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1223 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1224 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1225 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1226 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1230 * Tables to map from the non-specific opcode to the matching
1231 * type-specific opcode.
1233 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
1234 static const guint16
1235 binops_op_map [STACK_MAX] = {
1236 0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, OP_PADD-CEE_ADD
1239 /* handles from CEE_NEG to CEE_CONV_U8 */
1240 static const guint16
1241 unops_op_map [STACK_MAX] = {
1242 0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, OP_PNEG-CEE_NEG
1245 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
1246 static const guint16
1247 ovfops_op_map [STACK_MAX] = {
1248 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
1251 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
1252 static const guint16
1253 ovf2ops_op_map [STACK_MAX] = {
1254 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
1257 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
1258 static const guint16
1259 ovf3ops_op_map [STACK_MAX] = {
1260 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
1263 /* handles from CEE_CEQ to CEE_CLT_UN */
1264 static const guint16
1265 ceqops_op_map [STACK_MAX] = {
1266 0, 0, OP_LCEQ-CEE_CEQ, OP_PCEQ-CEE_CEQ, OP_FCEQ-CEE_CEQ, OP_LCEQ-CEE_CEQ
1270 * Sets ins->type (the type on the eval stack) according to the
1271 * type of the opcode and the arguments to it.
1272 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1274 * FIXME: this function sets ins->type unconditionally in some cases, but
1275 * it should set it to invalid for some types (a conv.x on an object)
1278 type_from_op (MonoInst *ins) {
1279 switch (ins->opcode) {
1286 /* FIXME: check unverifiable args for STACK_MP */
1287 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1288 ins->opcode += binops_op_map [ins->type];
1295 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1296 ins->opcode += binops_op_map [ins->type];
1301 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1302 ins->opcode += binops_op_map [ins->type];
1306 /* FIXME: handle some specifics with ins->next->type */
1307 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1308 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))))
1309 ins->opcode = OP_LCOMPARE;
1316 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1317 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1321 ins->type = neg_table [ins->inst_i0->type];
1322 ins->opcode += unops_op_map [ins->type];
1325 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1326 ins->type = ins->inst_i0->type;
1328 ins->type = STACK_INV;
1329 ins->opcode += unops_op_map [ins->type];
1335 ins->type = STACK_I4;
1336 ins->opcode += unops_op_map [ins->inst_i0->type];
1339 ins->type = STACK_R8;
1340 switch (ins->inst_i0->type) {
1345 ins->opcode = OP_LCONV_TO_R_UN;
1349 case CEE_CONV_OVF_I1:
1350 case CEE_CONV_OVF_U1:
1351 case CEE_CONV_OVF_I2:
1352 case CEE_CONV_OVF_U2:
1353 case CEE_CONV_OVF_I4:
1354 case CEE_CONV_OVF_U4:
1355 ins->type = STACK_I4;
1356 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1358 case CEE_CONV_OVF_I_UN:
1359 case CEE_CONV_OVF_U_UN:
1360 ins->type = STACK_PTR;
1361 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1363 case CEE_CONV_OVF_I1_UN:
1364 case CEE_CONV_OVF_I2_UN:
1365 case CEE_CONV_OVF_I4_UN:
1366 case CEE_CONV_OVF_U1_UN:
1367 case CEE_CONV_OVF_U2_UN:
1368 case CEE_CONV_OVF_U4_UN:
1369 ins->type = STACK_I4;
1370 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1373 ins->type = STACK_PTR;
1374 switch (ins->inst_i0->type) {
1380 ins->opcode = OP_LCONV_TO_U;
1383 ins->opcode = OP_FCONV_TO_U;
1389 ins->type = STACK_I8;
1390 ins->opcode += unops_op_map [ins->inst_i0->type];
1392 case CEE_CONV_OVF_I8:
1393 case CEE_CONV_OVF_U8:
1394 ins->type = STACK_I8;
1395 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1397 case CEE_CONV_OVF_U8_UN:
1398 case CEE_CONV_OVF_I8_UN:
1399 ins->type = STACK_I8;
1400 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1404 ins->type = STACK_R8;
1405 ins->opcode += unops_op_map [ins->inst_i0->type];
1408 ins->type = STACK_R8;
1412 ins->type = STACK_I4;
1413 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1416 case CEE_CONV_OVF_I:
1417 case CEE_CONV_OVF_U:
1418 ins->type = STACK_PTR;
1419 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1422 case CEE_ADD_OVF_UN:
1424 case CEE_MUL_OVF_UN:
1426 case CEE_SUB_OVF_UN:
1427 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1428 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1431 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1438 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_MP, STACK_R8, STACK_R8, STACK_OBJ
1441 /* map ldelem.x to the matching ldind.x opcode */
1443 ldelem_to_ldind [] = {
1457 /* map stelem.x to the matching stind.x opcode */
1459 stelem_to_stind [] = {
1473 param_table [STACK_MAX] [STACK_MAX] = {
1478 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1482 switch (args->type) {
1492 for (i = 0; i < sig->param_count; ++i) {
1493 switch (args [i].type) {
1497 if (!sig->params [i]->byref)
1501 if (sig->params [i]->byref)
1503 switch (sig->params [i]->type) {
1504 case MONO_TYPE_CLASS:
1505 case MONO_TYPE_STRING:
1506 case MONO_TYPE_OBJECT:
1507 case MONO_TYPE_SZARRAY:
1508 case MONO_TYPE_ARRAY:
1515 if (sig->params [i]->byref)
1517 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1526 /*if (!param_table [args [i].type] [sig->params [i]->type])
1534 * When we need a pointer to the current domain many times in a method, we
1535 * call mono_domain_get() once and we store the result in a local variable.
1536 * This function returns the variable that represents the MonoDomain*.
1538 inline static MonoInst *
1539 mono_get_domainvar (MonoCompile *cfg)
1541 if (!cfg->domainvar)
1542 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1543 return cfg->domainvar;
1547 * The got_var contains the address of the Global Offset Table when AOT
1550 inline static MonoInst *
1551 mono_get_got_var (MonoCompile *cfg)
1553 #ifdef MONO_ARCH_NEED_GOT_VAR
1554 if (!cfg->compile_aot)
1556 if (!cfg->got_var) {
1557 cfg->got_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1559 return cfg->got_var;
1566 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
1569 int num = cfg->num_varinfo;
1571 if ((num + 1) >= cfg->varinfo_count) {
1572 cfg->varinfo_count = (cfg->varinfo_count + 2) * 2;
1573 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
1574 cfg->vars = (MonoMethodVar **)g_realloc (cfg->vars, sizeof (MonoMethodVar*) * cfg->varinfo_count);
1577 /*g_print ("created temp %d of type 0x%x\n", num, type->type);*/
1578 mono_jit_stats.allocate_var++;
1580 MONO_INST_NEW (cfg, inst, opcode);
1581 inst->inst_c0 = num;
1582 inst->inst_vtype = type;
1583 inst->klass = mono_class_from_mono_type (type);
1584 /* if set to 1 the variable is native */
1587 cfg->varinfo [num] = inst;
1589 cfg->vars [num] = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMethodVar));
1590 MONO_INIT_VARINFO (cfg->vars [num], num);
1593 //g_print ("created temp %d of type %s\n", num, mono_type_get_name (type));
1598 * Transform a MonoInst into a load from the variable of index var_index.
1601 mono_compile_make_var_load (MonoCompile *cfg, MonoInst *dest, gssize var_index) {
1602 memset (dest, 0, sizeof (MonoInst));
1603 dest->ssa_op = MONO_SSA_LOAD;
1604 dest->inst_i0 = cfg->varinfo [var_index];
1605 dest->opcode = mono_type_to_ldind (dest->inst_i0->inst_vtype);
1606 type_to_eval_stack_type (dest->inst_i0->inst_vtype, dest);
1607 dest->klass = dest->inst_i0->klass;
1611 * Create a MonoInst that is a load from the variable of index var_index.
1614 mono_compile_create_var_load (MonoCompile *cfg, gssize var_index) {
1616 NEW_TEMPLOAD (cfg,dest,var_index);
1621 * Create a MonoInst that is a store of the given value into the variable of index var_index.
1624 mono_compile_create_var_store (MonoCompile *cfg, gssize var_index, MonoInst *value) {
1626 NEW_TEMPSTORE (cfg, dest, var_index, value);
1631 type_from_stack_type (MonoInst *ins) {
1632 switch (ins->type) {
1633 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1634 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1635 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1636 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1637 case STACK_MP: return &mono_defaults.int_class->byval_arg;
1638 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1639 case STACK_VTYPE: return &ins->klass->byval_arg;
1641 g_error ("stack type %d to montype not handled\n", ins->type);
1647 mono_type_from_stack_type (MonoInst *ins) {
1648 return type_from_stack_type (ins);
1652 array_access_to_klass (int opcode)
1656 return mono_defaults.byte_class;
1658 return mono_defaults.uint16_class;
1661 return mono_defaults.int_class;
1664 return mono_defaults.sbyte_class;
1667 return mono_defaults.int16_class;
1670 return mono_defaults.int32_class;
1672 return mono_defaults.uint32_class;
1675 return mono_defaults.int64_class;
1678 return mono_defaults.single_class;
1681 return mono_defaults.double_class;
1682 case CEE_LDELEM_REF:
1683 case CEE_STELEM_REF:
1684 return mono_defaults.object_class;
1686 g_assert_not_reached ();
1692 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
1696 MONO_ADD_INS (bb, inst);
1699 switch (bb->last_ins->opcode) {
1713 while (prev->next && prev->next != bb->last_ins)
1715 if (prev == bb->code) {
1716 if (bb->last_ins == bb->code) {
1717 inst->next = bb->code;
1720 inst->next = prev->next;
1724 inst->next = bb->last_ins;
1728 // g_warning ("handle conditional jump in add_ins_to_end ()\n");
1730 MONO_ADD_INS (bb, inst);
1736 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
1738 MonoInst *inst, *load;
1740 NEW_TEMPLOAD (cfg, load, src);
1742 NEW_TEMPSTORE (cfg, inst, dest, load);
1743 if (inst->opcode == CEE_STOBJ) {
1744 NEW_TEMPLOADA (cfg, inst, dest);
1745 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE);
1747 inst->cil_code = NULL;
1748 mono_add_ins_to_end (bb, inst);
1753 * We try to share variables when possible
1756 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1761 /* inlining can result in deeper stacks */
1762 if (slot >= mono_method_get_header (cfg->method)->max_stack)
1763 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1765 pos = ins->type - 1 + slot * STACK_MAX;
1767 switch (ins->type) {
1774 if ((vnum = cfg->intvars [pos]))
1775 return cfg->varinfo [vnum];
1776 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1777 cfg->intvars [pos] = res->inst_c0;
1780 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1786 * This function is called to handle items that are left on the evaluation stack
1787 * at basic block boundaries. What happens is that we save the values to local variables
1788 * and we reload them later when first entering the target basic block (with the
1789 * handle_loaded_temps () function).
1790 * It is also used to handle items on the stack in store opcodes, since it is
1791 * possible that the variable to be stored into is already on the stack, in
1792 * which case its old value should be used.
1793 * A single joint point will use the same variables (stored in the array bb->out_stack or
1794 * bb->in_stack, if the basic block is before or after the joint point).
1797 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count) {
1799 MonoBasicBlock *outb;
1800 MonoInst *inst, **locals;
1805 if (cfg->verbose_level > 3)
1806 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
1807 if (!bb->out_scount) {
1808 bb->out_scount = count;
1809 //g_print ("bblock %d has out:", bb->block_num);
1811 for (i = 0; i < bb->out_count; ++i) {
1812 outb = bb->out_bb [i];
1813 /* exception handlers are linked, but they should not be considered for stack args */
1814 if (outb->flags & BB_EXCEPTION_HANDLER)
1816 //g_print (" %d", outb->block_num);
1817 if (outb->in_stack) {
1819 bb->out_stack = outb->in_stack;
1825 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1826 for (i = 0; i < count; ++i) {
1828 * try to reuse temps already allocated for this purpouse, if they occupy the same
1829 * stack slot and if they are of the same type.
1830 * This won't cause conflicts since if 'local' is used to
1831 * store one of the values in the in_stack of a bblock, then
1832 * the same variable will be used for the same outgoing stack
1834 * This doesn't work when inlining methods, since the bblocks
1835 * in the inlined methods do not inherit their in_stack from
1836 * the bblock they are inlined to. See bug #58863 for an
1839 if (cfg->inlined_method)
1840 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1842 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1847 for (i = 0; i < bb->out_count; ++i) {
1848 outb = bb->out_bb [i];
1849 /* exception handlers are linked, but they should not be considered for stack args */
1850 if (outb->flags & BB_EXCEPTION_HANDLER)
1852 if (outb->in_scount) {
1853 if (outb->in_scount != bb->out_scount)
1855 continue; /* check they are the same locals */
1857 outb->in_scount = count;
1858 outb->in_stack = bb->out_stack;
1861 locals = bb->out_stack;
1862 for (i = 0; i < count; ++i) {
1863 /* add store ops at the end of the bb, before the branch */
1864 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1865 if (inst->opcode == CEE_STOBJ) {
1866 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
1867 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE);
1869 inst->cil_code = sp [i]->cil_code;
1870 mono_add_ins_to_end (bb, inst);
1872 if (cfg->verbose_level > 3)
1873 g_print ("storing %d to temp %d\n", i, (int)locals [i]->inst_c0);
1877 * It is possible that the out bblocks already have in_stack assigned, and
1878 * the in_stacks differ. In this case, we will store to all the different
1885 /* Find a bblock which has a different in_stack */
1887 while (bindex < bb->out_count) {
1888 outb = bb->out_bb [bindex];
1889 /* exception handlers are linked, but they should not be considered for stack args */
1890 if (outb->flags & BB_EXCEPTION_HANDLER) {
1894 if (outb->in_stack != locals) {
1896 * Instead of storing sp [i] to locals [i], we need to store
1897 * locals [i] to <new locals>[i], since the sp [i] tree can't
1898 * be shared between trees.
1900 for (i = 0; i < count; ++i)
1901 mono_add_varcopy_to_end (cfg, bb, locals [i]->inst_c0, outb->in_stack [i]->inst_c0);
1902 locals = outb->in_stack;
1914 ret_type_to_call_opcode (MonoType *type, int calli, int virt)
1917 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1920 switch (type->type) {
1921 case MONO_TYPE_VOID:
1922 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
1925 case MONO_TYPE_BOOLEAN:
1928 case MONO_TYPE_CHAR:
1931 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1935 case MONO_TYPE_FNPTR:
1936 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1937 case MONO_TYPE_CLASS:
1938 case MONO_TYPE_STRING:
1939 case MONO_TYPE_OBJECT:
1940 case MONO_TYPE_SZARRAY:
1941 case MONO_TYPE_ARRAY:
1942 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1945 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
1948 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
1949 case MONO_TYPE_VALUETYPE:
1950 if (type->data.klass->enumtype) {
1951 type = type->data.klass->enum_basetype;
1954 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1955 case MONO_TYPE_TYPEDBYREF:
1956 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1957 case MONO_TYPE_GENERICINST:
1958 type = &type->data.generic_class->container_class->byval_arg;
1961 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
1967 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
1969 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
1970 MonoJumpInfoBBTable *table;
1972 table = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfoBBTable));
1974 table->table_size = num_blocks;
1976 ji->ip.label = label;
1977 ji->type = MONO_PATCH_INFO_SWITCH;
1978 ji->data.table = table;
1979 ji->next = cfg->patch_info;
1980 cfg->patch_info = ji;
1984 * When we add a tree of instructions, we need to ensure the instructions currently
1985 * on the stack are executed before (like, if we load a value from a local).
1986 * We ensure this by saving the currently loaded values to temps and rewriting the
1987 * instructions to load the values.
1988 * This is not done for opcodes that terminate a basic block (because it's handled already
1989 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
1992 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
1994 MonoInst *load, *store, *temp, *ins;
1996 while (stack < sp) {
1998 /* handle also other constants */
1999 if ((ins->opcode != OP_ICONST) &&
2000 /* temps never get written to again, so we can safely avoid duplicating them */
2001 !(ins->ssa_op == MONO_SSA_LOAD && ins->inst_i0->opcode == OP_LOCAL && ins->inst_i0->flags & MONO_INST_IS_TEMP)) {
2002 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
2003 temp->flags |= MONO_INST_IS_TEMP;
2004 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2005 store->cil_code = ins->cil_code;
2006 if (store->opcode == CEE_STOBJ) {
2007 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2008 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE);
2010 MONO_ADD_INS (bblock, store);
2011 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2012 load->cil_code = ins->cil_code;
2020 * Prepare arguments for passing to a function call.
2021 * Return a non-zero value if the arguments can't be passed to the given
2023 * The type checks are not yet complete and some conversions may need
2024 * casts on 32 or 64 bit architectures.
2027 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
2029 MonoType *simple_type;
2033 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
2037 for (i = 0; i < sig->param_count; ++i) {
2038 if (sig->params [i]->byref) {
2039 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
2043 simple_type = sig->params [i];
2045 switch (simple_type->type) {
2046 case MONO_TYPE_VOID:
2051 case MONO_TYPE_BOOLEAN:
2054 case MONO_TYPE_CHAR:
2057 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
2063 case MONO_TYPE_FNPTR:
2064 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
2067 case MONO_TYPE_CLASS:
2068 case MONO_TYPE_STRING:
2069 case MONO_TYPE_OBJECT:
2070 case MONO_TYPE_SZARRAY:
2071 case MONO_TYPE_ARRAY:
2072 if (args [i]->type != STACK_OBJ)
2077 if (args [i]->type != STACK_I8)
2082 if (args [i]->type != STACK_R8)
2085 case MONO_TYPE_VALUETYPE:
2086 if (simple_type->data.klass->enumtype) {
2087 simple_type = simple_type->data.klass->enum_basetype;
2090 if (args [i]->type != STACK_VTYPE)
2093 case MONO_TYPE_TYPEDBYREF:
2094 if (args [i]->type != STACK_VTYPE)
2097 case MONO_TYPE_GENERICINST:
2098 simple_type = &simple_type->data.generic_class->container_class->byval_arg;
2102 g_error ("unknown type 0x%02x in check_call_signature",
2110 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
2111 const guint8 *ip, gboolean to_end)
2113 MonoInst *temp, *store, *ins = (MonoInst*)call;
2114 MonoType *ret = sig->ret;
2116 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
2118 call->inst.type = STACK_OBJ;
2119 call->inst.opcode = CEE_CALL;
2120 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
2122 type_to_eval_stack_type (ret, ins);
2123 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
2126 temp->flags |= MONO_INST_IS_TEMP;
2128 if (MONO_TYPE_ISSTRUCT (ret)) {
2129 MonoInst *loada, *dummy_store;
2132 * Emit a dummy store to the local holding the result so the
2133 * liveness info remains correct.
2135 NEW_DUMMY_STORE (cfg, dummy_store, temp->inst_c0);
2137 mono_add_ins_to_end (bblock, dummy_store);
2139 MONO_ADD_INS (bblock, dummy_store);
2141 /* we use this to allocate native sized structs */
2142 temp->unused = sig->pinvoke;
2144 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
2145 if (call->inst.opcode == OP_VCALL)
2146 ins->inst_left = loada;
2148 ins->inst_right = loada; /* a virtual or indirect call */
2151 mono_add_ins_to_end (bblock, ins);
2153 MONO_ADD_INS (bblock, ins);
2155 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2156 store->cil_code = ip;
2158 mono_add_ins_to_end (bblock, store);
2160 MONO_ADD_INS (bblock, store);
2162 return temp->inst_c0;
2165 mono_add_ins_to_end (bblock, ins);
2167 MONO_ADD_INS (bblock, ins);
2172 inline static MonoCallInst *
2173 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
2174 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
2179 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual));
2181 call->inst.cil_code = ip;
2183 call->signature = sig;
2184 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
2185 type_to_eval_stack_type (sig->ret, &call->inst);
2187 for (arg = call->out_args; arg;) {
2188 MonoInst *narg = arg->next;
2193 mono_add_ins_to_end (bblock, arg);
2195 MONO_ADD_INS (bblock, arg);
2202 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
2203 MonoInst **args, MonoInst *addr, const guint8 *ip)
2205 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
2207 call->inst.inst_i0 = addr;
2209 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
2212 static MonoCallInst*
2213 mono_emit_method_call_full (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
2214 MonoInst **args, const guint8 *ip, MonoInst *this, gboolean to_end)
2216 gboolean virtual = this != NULL;
2219 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, to_end);
2221 if (this && sig->hasthis &&
2222 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
2223 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
2224 call->method = mono_marshal_get_remoting_invoke_with_check (method);
2226 call->method = method;
2228 call->inst.flags |= MONO_INST_HAS_METHOD;
2229 call->inst.inst_left = this;
2232 mono_get_got_var (cfg);
2233 else if (call->method->klass->flags & TYPE_ATTRIBUTE_INTERFACE)
2234 /* Needed by the code generated in inssel.brg */
2235 mono_get_got_var (cfg);
2240 static MonoCallInst*
2241 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
2242 MonoInst **args, const guint8 *ip, MonoInst *this)
2244 return mono_emit_method_call_full (cfg, bblock, method, sig, args, ip, this, FALSE);
2248 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
2249 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
2251 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, signature, args, ip, this);
2253 return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
2257 mono_emit_method_call_spilled_full (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
2258 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this,
2259 gboolean ret_object, gboolean to_end)
2261 MonoCallInst *call = mono_emit_method_call_full (cfg, bblock, method, signature, args, ip, this, to_end);
2263 return mono_spill_call (cfg, bblock, call, signature, ret_object, ip, to_end);
2267 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
2268 MonoInst **args, const guint8 *ip, gboolean ret_object, gboolean to_end)
2274 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
2277 mono_get_got_var (cfg);
2279 return mono_spill_call (cfg, bblock, call, sig, ret_object, ip, to_end);
2283 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
2285 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
2288 g_warning ("unregistered JIT ICall");
2289 g_assert_not_reached ();
2292 mono_get_got_var (cfg);
2293 return mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, args, ip, FALSE, FALSE);
2297 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
2299 MonoInst *ins, *temp = NULL, *store, *load, *begin;
2300 MonoInst *last_arg = NULL;
2304 //g_print ("emulating: ");
2305 //mono_print_tree_nl (tree);
2306 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE));
2307 ins = (MonoInst*)call;
2309 call->inst.cil_code = tree->cil_code;
2311 call->signature = info->sig;
2313 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
2315 mono_get_got_var (cfg);
2317 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2318 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
2319 temp->flags |= MONO_INST_IS_TEMP;
2320 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2321 store->cil_code = tree->cil_code;
2326 nargs = info->sig->param_count + info->sig->hasthis;
2328 for (last_arg = call->out_args; last_arg && last_arg->next; last_arg = last_arg->next) ;
2331 last_arg->next = store;
2334 begin = call->out_args;
2338 if (cfg->prev_ins) {
2340 * This assumes that that in a tree, emulate_opcode is called for a
2341 * node before it is called for its children. dec_foreach needs to
2342 * take this into account.
2344 store->next = cfg->prev_ins->next;
2345 cfg->prev_ins->next = begin;
2347 store->next = cfg->cbb->code;
2348 cfg->cbb->code = begin;
2351 call->fptr = mono_icall_get_wrapper (info);
2353 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2354 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2359 static MonoMethodSignature *
2360 mono_get_element_address_signature (int arity)
2362 static GHashTable *sighash = NULL;
2363 MonoMethodSignature *res;
2368 sighash = g_hash_table_new (NULL, NULL);
2370 else if ((res = g_hash_table_lookup (sighash, GINT_TO_POINTER (arity)))) {
2371 LeaveCriticalSection (&jit_mutex);
2375 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
2378 #ifdef MONO_ARCH_VARARG_ICALLS
2379 /* Only set this only some archs since not all backends can handle varargs+pinvoke */
2380 res->call_convention = MONO_CALL_VARARG;
2382 res->params [0] = &mono_defaults.array_class->byval_arg;
2384 for (i = 1; i <= arity; i++)
2385 res->params [i] = &mono_defaults.int_class->byval_arg;
2387 res->ret = &mono_defaults.int_class->byval_arg;
2389 g_hash_table_insert (sighash, GINT_TO_POINTER (arity), res);
2395 static MonoMethodSignature *
2396 mono_get_array_new_va_signature (int arity)
2398 static GHashTable *sighash = NULL;
2399 MonoMethodSignature *res;
2404 sighash = g_hash_table_new (NULL, NULL);
2406 else if ((res = g_hash_table_lookup (sighash, GINT_TO_POINTER (arity)))) {
2411 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
2414 #ifdef MONO_ARCH_VARARG_ICALLS
2415 /* Only set this only some archs since not all backends can handle varargs+pinvoke */
2416 res->call_convention = MONO_CALL_VARARG;
2419 res->params [0] = &mono_defaults.int_class->byval_arg;
2420 for (i = 0; i < arity; i++)
2421 res->params [i + 1] = &mono_defaults.int_class->byval_arg;
2423 res->ret = &mono_defaults.int_class->byval_arg;
2425 g_hash_table_insert (sighash, GINT_TO_POINTER (arity), res);
2432 get_memcpy_method (void)
2434 static MonoMethod *memcpy_method = NULL;
2435 if (!memcpy_method) {
2436 memcpy_method = mono_class_get_method_from_name (mono_defaults.string_class, "memcpy", 3);
2438 g_error ("Old corlib found. Install a new one");
2440 return memcpy_method;
2444 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native) {
2445 MonoInst *iargs [3];
2448 MonoMethod *memcpy_method;
2452 * This check breaks with spilled vars... need to handle it during verification anyway.
2453 * g_assert (klass && klass == src->klass && klass == dest->klass);
2457 n = mono_class_native_size (klass, &align);
2459 n = mono_class_value_size (klass, &align);
2461 if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
2463 if (dest->opcode == OP_LDADDR) {
2464 /* Keep liveness info correct */
2465 NEW_DUMMY_STORE (cfg, inst, dest->inst_i0->inst_c0);
2466 MONO_ADD_INS (bblock, inst);
2468 MONO_INST_NEW (cfg, inst, OP_MEMCPY);
2469 inst->inst_left = dest;
2470 inst->inst_right = src;
2471 inst->cil_code = ip;
2473 MONO_ADD_INS (bblock, inst);
2478 NEW_ICONST (cfg, iargs [2], n);
2480 memcpy_method = get_memcpy_method ();
2481 mono_emit_method_call_spilled_full (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL, FALSE, to_end);
2485 get_memset_method (void)
2487 static MonoMethod *memset_method = NULL;
2488 if (!memset_method) {
2489 memset_method = mono_class_get_method_from_name (mono_defaults.string_class, "memset", 3);
2491 g_error ("Old corlib found. Install a new one");
2493 return memset_method;
2497 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
2499 MonoInst *iargs [3];
2500 MonoInst *ins, *zero_int32;
2502 MonoMethod *memset_method;
2504 NEW_ICONST (cfg, zero_int32, 0);
2506 mono_class_init (klass);
2507 n = mono_class_value_size (klass, NULL);
2508 MONO_INST_NEW (cfg, ins, 0);
2510 ins->inst_left = dest;
2511 ins->inst_right = zero_int32;
2514 ins->opcode = CEE_STIND_I1;
2515 MONO_ADD_INS (bblock, ins);
2518 ins->opcode = CEE_STIND_I2;
2519 MONO_ADD_INS (bblock, ins);
2522 ins->opcode = CEE_STIND_I4;
2523 MONO_ADD_INS (bblock, ins);
2526 if (n <= sizeof (gpointer) * 5) {
2527 ins->opcode = OP_MEMSET;
2530 MONO_ADD_INS (bblock, ins);
2533 memset_method = get_memset_method ();
2534 handle_loaded_temps (cfg, bblock, stack_start, sp);
2536 NEW_ICONST (cfg, iargs [1], 0);
2537 NEW_ICONST (cfg, iargs [2], n);
2538 mono_emit_method_call_spilled (cfg, bblock, memset_method, memset_method->signature, iargs, ip, NULL);
2544 handle_alloc (MonoCompile *cfg, MonoBasicBlock *bblock, MonoClass *klass, gboolean for_box, const guchar *ip)
2546 MonoInst *iargs [2];
2549 if (cfg->opt & MONO_OPT_SHARED) {
2550 NEW_DOMAINCONST (cfg, iargs [0]);
2551 NEW_CLASSCONST (cfg, iargs [1], klass);
2553 alloc_ftn = mono_object_new;
2555 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
2558 alloc_ftn = mono_class_get_allocation_ftn (vtable, for_box, &pass_lw);
2560 guint32 lw = vtable->klass->instance_size;
2561 lw = ((lw + (sizeof (gpointer) - 1)) & ~(sizeof (gpointer) - 1)) / sizeof (gpointer);
2562 NEW_ICONST (cfg, iargs [0], lw);
2563 NEW_VTABLECONST (cfg, iargs [1], vtable);
2566 NEW_VTABLECONST (cfg, iargs [0], vtable);
2569 return mono_emit_jit_icall (cfg, bblock, alloc_ftn, iargs, ip);
2573 * Handles unbox of a Nullable<T>, returning a temp variable
2574 * where the result is stored
2577 handle_unbox_nullable (MonoCompile* cfg, MonoBasicBlock* bblock, MonoInst* val, const guchar *ip, MonoClass* klass)
2579 MonoMethod* method = mono_class_get_method_from_name (klass, "Unbox", 1);
2580 return mono_emit_method_call_spilled (cfg, bblock, method, mono_method_signature (method), &val, ip, NULL);
2587 handle_box (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *val, const guchar *ip, MonoClass *klass)
2589 MonoInst *dest, *vtoffset, *add, *vstore;
2592 if (mono_class_is_nullable (klass)) {
2593 MonoMethod* method = mono_class_get_method_from_name (klass, "Box", 1);
2594 temp = mono_emit_method_call_spilled (cfg, bblock, method, mono_method_signature (method), &val, ip, NULL);
2595 NEW_TEMPLOAD (cfg, dest, temp);
2600 temp = handle_alloc (cfg, bblock, klass, TRUE, ip);
2601 NEW_TEMPLOAD (cfg, dest, temp);
2602 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
2603 MONO_INST_NEW (cfg, add, OP_PADD);
2604 add->inst_left = dest;
2605 add->inst_right = vtoffset;
2608 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
2609 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
2610 vstore->cil_code = ip;
2611 vstore->inst_left = add;
2612 vstore->inst_right = val;
2614 if (vstore->opcode == CEE_STOBJ) {
2615 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
2617 MONO_ADD_INS (bblock, vstore);
2619 NEW_TEMPLOAD (cfg, dest, temp);
2624 handle_array_new (MonoCompile *cfg, MonoBasicBlock *bblock, int rank, MonoInst **sp, unsigned char *ip)
2626 MonoMethodSignature *esig;
2627 char icall_name [256];
2629 MonoJitICallInfo *info;
2631 /* Need to register the icall so it gets an icall wrapper */
2632 sprintf (icall_name, "ves_array_new_va_%d", rank);
2634 info = mono_find_jit_icall_by_name (icall_name);
2636 esig = mono_get_array_new_va_signature (rank);
2637 name = g_strdup (icall_name);
2638 info = mono_register_jit_icall (mono_array_new_va, name, esig, FALSE);
2641 g_hash_table_insert (jit_icall_name_hash, name, name);
2645 cfg->flags |= MONO_CFG_HAS_VARARGS;
2647 return mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, sp, ip, TRUE, FALSE);
2651 mono_emit_load_got_addr (MonoCompile *cfg)
2653 MonoInst *load, *store, *dummy_use;
2656 if (!cfg->got_var || cfg->got_var_allocated)
2659 MONO_INST_NEW (cfg, get_got, OP_LOAD_GOTADDR);
2660 NEW_TEMPSTORE (cfg, store, cfg->got_var->inst_c0, get_got);
2662 /* Add it to the start of the first bblock */
2663 if (cfg->bb_entry->code) {
2664 store->next = cfg->bb_entry->code;
2665 cfg->bb_entry->code = store;
2668 MONO_ADD_INS (cfg->bb_entry, store);
2670 cfg->got_var_allocated = TRUE;
2673 * Add a dummy use to keep the got_var alive, since real uses might
2674 * only be generated in the decompose or instruction selection phases.
2675 * Add it to end_bblock, so the variable's lifetime covers the whole
2678 NEW_TEMPLOAD (cfg, load, cfg->got_var->inst_c0);
2679 NEW_DUMMY_USE (cfg, dummy_use, load);
2680 MONO_ADD_INS (cfg->bb_exit, dummy_use);
2683 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
2686 mini_class_is_system_array (MonoClass *klass)
2688 if (klass->parent == mono_defaults.array_class)
2690 else if (mono_defaults.generic_array_class && klass->parent && klass->parent->generic_class)
2691 return klass->parent->generic_class->container_class == mono_defaults.generic_array_class;
2697 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
2699 MonoMethodHeader *header = mono_method_get_header (method);
2700 MonoMethodSignature *signature = mono_method_signature (method);
2704 #ifdef MONO_ARCH_HAVE_LMF_OPS
2705 if (((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2706 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) &&
2707 !MONO_TYPE_ISSTRUCT (signature->ret) && !mini_class_is_system_array (method->klass))
2711 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2712 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2713 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
2714 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
2715 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2716 (method->klass->marshalbyref) ||
2717 !header || header->num_clauses ||
2718 /* fixme: why cant we inline valuetype returns? */
2719 MONO_TYPE_ISSTRUCT (signature->ret))
2722 /* its not worth to inline methods with valuetype arguments?? */
2723 for (i = 0; i < signature->param_count; i++) {
2724 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
2730 * if we can initialize the class of the method right away, we do,
2731 * otherwise we don't allow inlining if the class needs initialization,
2732 * since it would mean inserting a call to mono_runtime_class_init()
2733 * inside the inlined code
2735 if (!(cfg->opt & MONO_OPT_SHARED)) {
2736 vtable = mono_class_vtable (cfg->domain, method->klass);
2737 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
2738 if (cfg->run_cctors)
2739 mono_runtime_class_init (vtable);
2741 else if (!vtable->initialized && mono_class_needs_cctor_run (method->klass, NULL))
2745 * If we're compiling for shared code
2746 * the cctor will need to be run at aot method load time, for example,
2747 * or at the end of the compilation of the inlining method.
2749 if (mono_class_needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
2752 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
2755 * CAS - do not inline methods with declarative security
2756 * Note: this has to be before any possible return TRUE;
2758 if (mono_method_has_declsec (method))
2761 /* also consider num_locals? */
2762 if (getenv ("MONO_INLINELIMIT")) {
2763 if (header->code_size < atoi (getenv ("MONO_INLINELIMIT"))) {
2766 } else if (header->code_size < 20)
2773 mini_field_access_needs_cctor_run (MonoCompile *cfg, MonoMethod *method, MonoVTable *vtable)
2775 if (vtable->initialized && !cfg->compile_aot)
2778 if (vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)
2781 if (!mono_class_needs_cctor_run (vtable->klass, method))
2784 if (! (method->flags & METHOD_ATTRIBUTE_STATIC) && (vtable->klass == method->klass))
2785 /* The initialization is already done before the method is called */
2792 mini_get_ldelema_ins (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
2796 MonoMethodSignature *esig;
2797 char icall_name [256];
2799 MonoJitICallInfo *info;
2801 rank = mono_method_signature (cmethod)->param_count - (is_set? 1: 0);
2803 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
2804 #ifdef MONO_ARCH_EMULATE_MUL_DIV
2805 /* OP_LDELEMA2D depends on OP_LMUL */
2808 NEW_GROUP (cfg, indexes, sp [1], sp [2]);
2809 MONO_INST_NEW (cfg, addr, OP_LDELEMA2D);
2810 addr->inst_left = sp [0];
2811 addr->inst_right = indexes;
2812 addr->cil_code = ip;
2813 addr->type = STACK_MP;
2814 addr->klass = cmethod->klass;
2819 /* Need to register the icall so it gets an icall wrapper */
2820 sprintf (icall_name, "ves_array_element_address_%d", rank);
2822 info = mono_find_jit_icall_by_name (icall_name);
2824 esig = mono_get_element_address_signature (rank);
2825 name = g_strdup (icall_name);
2826 info = mono_register_jit_icall (ves_array_element_address, name, esig, FALSE);
2829 g_hash_table_insert (jit_icall_name_hash, name, name);
2833 temp = mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, sp, ip, FALSE, FALSE);
2834 cfg->flags |= MONO_CFG_HAS_VARARGS;
2836 NEW_TEMPLOAD (cfg, addr, temp);
2840 static MonoJitICallInfo **emul_opcode_map = NULL;
2842 static inline MonoJitICallInfo *
2843 mono_find_jit_opcode_emulation (int opcode)
2845 if (emul_opcode_map)
2846 return emul_opcode_map [opcode];
2851 static MonoException*
2852 mini_loader_error_to_exception (MonoLoaderError *error)
2854 MonoException *ex = NULL;
2856 switch (error->kind) {
2857 case MONO_LOADER_ERROR_TYPE: {
2858 MonoString *class_name = mono_string_new (mono_domain_get (), error->class_name);
2860 ex = mono_get_exception_type_load (class_name, error->assembly_name);
2863 case MONO_LOADER_ERROR_METHOD:
2864 case MONO_LOADER_ERROR_FIELD: {
2867 class_name = g_strdup_printf ("%s%s%s", error->klass->name_space, *error->klass->name_space ? "." : "", error->klass->name);
2869 if (error->kind == MONO_LOADER_ERROR_METHOD)
2870 ex = mono_get_exception_missing_method (class_name, error->member_name);
2872 ex = mono_get_exception_missing_field (class_name, error->member_name);
2873 g_free (class_name);
2877 g_assert_not_reached ();
2884 mini_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
2886 MonoInst *ins = NULL;
2888 static MonoClass *runtime_helpers_class = NULL;
2889 if (! runtime_helpers_class)
2890 runtime_helpers_class = mono_class_from_name (mono_defaults.corlib,
2891 "System.Runtime.CompilerServices", "RuntimeHelpers");
2893 if (cmethod->klass == mono_defaults.string_class) {
2894 if (cmethod->name [0] != 'g')
2897 if (strcmp (cmethod->name, "get_Chars") == 0) {
2898 MONO_INST_NEW (cfg, ins, OP_GETCHR);
2899 ins->inst_i0 = args [0];
2900 ins->inst_i1 = args [1];
2902 } else if (strcmp (cmethod->name, "get_Length") == 0) {
2903 MONO_INST_NEW (cfg, ins, OP_STRLEN);
2904 ins->inst_i0 = args [0];
2908 } else if (cmethod->klass == mono_defaults.object_class) {
2909 if (strcmp (cmethod->name, "GetType") == 0) {
2910 MONO_INST_NEW (cfg, ins, OP_GETTYPE);
2911 ins->inst_i0 = args [0];
2913 } else if (strcmp (cmethod->name, "InternalGetHashCode") == 0) {
2914 #ifdef MONO_ARCH_EMULATE_MUL_DIV
2915 /* The OP_GETHASHCODE rule depends on OP_MUL */
2917 MONO_INST_NEW (cfg, ins, OP_GETHASHCODE);
2918 ins->inst_i0 = args [0];
2921 } else if (strcmp (cmethod->name, ".ctor") == 0) {
2922 MONO_INST_NEW (cfg, ins, CEE_NOP);
2926 } else if (mini_class_is_system_array (cmethod->klass)) {
2927 if (cmethod->name [0] != 'g')
2930 if (strcmp (cmethod->name, "get_Rank") == 0) {
2931 MONO_INST_NEW (cfg, ins, OP_ARRAY_RANK);
2932 ins->inst_i0 = args [0];
2934 } else if (strcmp (cmethod->name, "get_Length") == 0) {
2935 MONO_INST_NEW (cfg, ins, CEE_LDLEN);
2936 ins->inst_i0 = args [0];
2940 } else if (cmethod->klass == runtime_helpers_class) {
2941 if (strcmp (cmethod->name, "get_OffsetToStringData") == 0) {
2942 NEW_ICONST (cfg, ins, G_STRUCT_OFFSET (MonoString, chars));
2946 } else if (cmethod->klass == mono_defaults.thread_class) {
2947 if (strcmp (cmethod->name, "get_CurrentThread") == 0 && (ins = mono_arch_get_thread_intrinsic (cfg)))
2951 return mono_arch_get_inst_for_method (cfg, cmethod, fsig, args);
2955 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
2957 MonoInst *store, *temp;
2960 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
2962 if (!sig->hasthis && sig->param_count == 0)
2966 if (sp [0]->opcode == OP_ICONST) {
2969 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
2971 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2972 store->cil_code = sp [0]->cil_code;
2973 MONO_ADD_INS (bblock, store);
2978 for (i = 0; i < sig->param_count; ++i) {
2979 if (sp [0]->opcode == OP_ICONST) {
2982 temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
2984 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2985 store->cil_code = sp [0]->cil_code;
2986 if (store->opcode == CEE_STOBJ) {
2987 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2988 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE);
2990 MONO_ADD_INS (bblock, store);
2998 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
2999 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b, gboolean inline_allways)
3001 MonoInst *ins, *rvar = NULL;
3002 MonoMethodHeader *cheader;
3003 MonoBasicBlock *ebblock, *sbblock;
3004 int i, costs, new_locals_offset;
3005 MonoMethod *prev_inlined_method;
3007 if (cfg->verbose_level > 2)
3008 g_print ("INLINE START %p %s -> %s\n", cmethod, mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
3010 if (!cmethod->inline_info) {
3011 mono_jit_stats.inlineable_methods++;
3012 cmethod->inline_info = 1;
3014 /* allocate space to store the return value */
3015 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3016 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
3019 /* allocate local variables */
3020 cheader = mono_method_get_header (cmethod);
3021 new_locals_offset = cfg->num_varinfo;
3022 for (i = 0; i < cheader->num_locals; ++i)
3023 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
3025 /* allocate starte and end blocks */
3026 sbblock = NEW_BBLOCK (cfg);
3027 sbblock->block_num = cfg->num_bblocks++;
3028 sbblock->real_offset = real_offset;
3030 ebblock = NEW_BBLOCK (cfg);
3031 ebblock->block_num = cfg->num_bblocks++;
3032 ebblock->real_offset = real_offset;
3034 prev_inlined_method = cfg->inlined_method;
3035 cfg->inlined_method = cmethod;
3037 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
3039 cfg->inlined_method = prev_inlined_method;
3041 if ((costs >= 0 && costs < 60) || inline_allways) {
3042 if (cfg->verbose_level > 2)
3043 g_print ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
3045 mono_jit_stats.inlined_methods++;
3047 /* always add some code to avoid block split failures */
3048 MONO_INST_NEW (cfg, ins, CEE_NOP);
3049 MONO_ADD_INS (bblock, ins);
3052 bblock->next_bb = sbblock;
3053 link_bblock (cfg, bblock, sbblock);
3056 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
3062 if (cfg->verbose_level > 2)
3063 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
3069 * Some of these comments may well be out-of-date.
3070 * Design decisions: we do a single pass over the IL code (and we do bblock
3071 * splitting/merging in the few cases when it's required: a back jump to an IL
3072 * address that was not already seen as bblock starting point).
3073 * Code is validated as we go (full verification is still better left to metadata/verify.c).
3074 * Complex operations are decomposed in simpler ones right away. We need to let the
3075 * arch-specific code peek and poke inside this process somehow (except when the
3076 * optimizations can take advantage of the full semantic info of coarse opcodes).
3077 * All the opcodes of the form opcode.s are 'normalized' to opcode.
3078 * MonoInst->opcode initially is the IL opcode or some simplification of that
3079 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
3080 * opcode with value bigger than OP_LAST.
3081 * At this point the IR can be handed over to an interpreter, a dumb code generator
3082 * or to the optimizing code generator that will translate it to SSA form.
3084 * Profiling directed optimizations.
3085 * We may compile by default with few or no optimizations and instrument the code
3086 * or the user may indicate what methods to optimize the most either in a config file
3087 * or through repeated runs where the compiler applies offline the optimizations to
3088 * each method and then decides if it was worth it.
3091 * * consider using an array instead of an hash table (bb_hash)
3094 #define CHECK_TYPE(ins) if (!(ins)->type) goto unverified
3095 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) goto unverified
3096 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) goto unverified
3097 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) goto unverified
3098 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) goto unverified
3099 #define CHECK_OPSIZE(size) if (ip + size > end) goto unverified
3102 /* offset from br.s -> br like opcodes */
3103 #define BIG_BRANCH_OFFSET 13
3106 ip_in_bb (MonoCompile *cfg, MonoBasicBlock *bb, const guint8* ip)
3108 MonoBasicBlock *b = g_hash_table_lookup (cfg->bb_hash, ip);
3110 return b == NULL || b == bb;
3114 get_basic_blocks (MonoCompile *cfg, GHashTable *bbhash, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
3116 unsigned char *ip = start;
3117 unsigned char *target;
3120 MonoBasicBlock *bblock;
3121 const MonoOpcode *opcode;
3124 cli_addr = ip - start;
3125 i = mono_opcode_value ((const guint8 **)&ip, end);
3128 opcode = &mono_opcodes [i];
3129 switch (opcode->argument) {
3130 case MonoInlineNone:
3133 case MonoInlineString:
3134 case MonoInlineType:
3135 case MonoInlineField:
3136 case MonoInlineMethod:
3139 case MonoShortInlineR:
3146 case MonoShortInlineVar:
3147 case MonoShortInlineI:
3150 case MonoShortInlineBrTarget:
3151 target = start + cli_addr + 2 + (signed char)ip [1];
3152 GET_BBLOCK (cfg, bbhash, bblock, target);
3155 GET_BBLOCK (cfg, bbhash, bblock, ip);
3157 case MonoInlineBrTarget:
3158 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
3159 GET_BBLOCK (cfg, bbhash, bblock, target);
3162 GET_BBLOCK (cfg, bbhash, bblock, ip);
3164 case MonoInlineSwitch: {
3165 guint32 n = read32 (ip + 1);
3168 cli_addr += 5 + 4 * n;
3169 target = start + cli_addr;
3170 GET_BBLOCK (cfg, bbhash, bblock, target);
3172 for (j = 0; j < n; ++j) {
3173 target = start + cli_addr + (gint32)read32 (ip);
3174 GET_BBLOCK (cfg, bbhash, bblock, target);
3184 g_assert_not_reached ();
3187 if (i == CEE_THROW) {
3188 unsigned char *bb_start = ip - 1;
3190 /* Find the start of the bblock containing the throw */
3192 while ((bb_start > start) && !bblock) {
3193 bblock = g_hash_table_lookup (bbhash, (bb_start));
3197 bblock->out_of_line = 1;
3207 emit_tree (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *ins, const guint8* ip_next)
3209 MonoInst *store, *temp, *load;
3211 if (ip_in_bb (cfg, bblock, ip_next) &&
3212 (CODE_IS_STLOC (ip_next) || *ip_next == CEE_RET))
3215 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3216 temp->flags |= MONO_INST_IS_TEMP;
3217 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3218 store->cil_code = ins->cil_code;
3219 MONO_ADD_INS (bblock, store);
3220 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
3221 load->cil_code = ins->cil_code;
3225 static inline MonoMethod *
3226 mini_get_method (MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context)
3230 if (m->wrapper_type != MONO_WRAPPER_NONE)
3231 return mono_method_get_wrapper_data (m, token);
3233 method = mono_get_method_full (m->klass->image, token, klass, context);
3235 if (method && method->is_inflated)
3236 method = mono_get_inflated_method (method);
3241 static inline MonoClass*
3242 mini_get_class (MonoMethod *method, guint32 token, MonoGenericContext *context)
3246 if (method->wrapper_type != MONO_WRAPPER_NONE)
3247 klass = mono_method_get_wrapper_data (method, token);
3249 klass = mono_class_get_full (method->klass->image, token, context);
3251 mono_class_init (klass);
3256 void check_linkdemand (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee, MonoBasicBlock *bblock, unsigned char *ip)
3258 guint32 result = mono_declsec_linkdemand (cfg->domain, caller, callee);
3259 if (result == MONO_JIT_SECURITY_OK)
3262 if (result == MONO_JIT_LINKDEMAND_ECMA) {
3263 /* Generate code to throw a SecurityException before the actual call/link */
3264 MonoAssembly *assembly = mono_image_get_assembly (caller->klass->image);
3265 MonoReflectionAssembly *refass = (MonoReflectionAssembly*) mono_assembly_get_object (cfg->domain, assembly);
3266 MonoReflectionMethod *refmet = mono_method_get_object (cfg->domain, caller, NULL);
3267 MonoSecurityManager *secman = mono_security_manager_get_methods ();
3270 NEW_ICONST (cfg, args [0], 4);
3271 NEW_PCONST (cfg, args [1], refass);
3272 NEW_PCONST (cfg, args [2], refmet);
3273 mono_emit_method_call_spilled (cfg, bblock, secman->linkdemandsecurityexception, mono_method_signature (secman->linkdemandsecurityexception), args, ip, NULL);
3274 } else if (cfg->exception_type == MONO_EXCEPTION_NONE) {
3275 /* don't hide previous results */
3276 cfg->exception_type = MONO_EXCEPTION_SECURITY_LINKDEMAND;
3277 cfg->exception_data = result;
3283 * mono_method_to_ir: translates IL into basic blocks containing trees
3286 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
3287 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
3288 guint inline_offset, gboolean is_virtual_call)
3290 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
3291 MonoInst *ins, **sp, **stack_start;
3292 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
3294 MonoMethod *cmethod;
3295 MonoInst **arg_array;
3296 MonoMethodHeader *header;
3298 guint32 token, ins_flag;
3300 MonoClass *constrained_call = NULL;
3301 unsigned char *ip, *end, *target, *err_pos;
3302 static double r8_0 = 0.0;
3303 MonoMethodSignature *sig;
3304 MonoGenericContext *generic_context = NULL;
3305 MonoGenericContainer *generic_container = NULL;
3306 MonoType **param_types;
3307 GList *bb_recheck = NULL, *tmp;
3308 int i, n, start_new_bblock, align;
3309 int num_calls = 0, inline_costs = 0;
3310 int breakpoint_id = 0;
3311 guint real_offset, num_args;
3312 MonoBoolean security, pinvoke;
3313 MonoSecurityManager* secman = NULL;
3314 MonoDeclSecurityActions actions;
3315 GSList *class_inits = NULL;
3317 image = method->klass->image;
3318 header = mono_method_get_header (method);
3319 generic_container = method->generic_container;
3320 sig = mono_method_signature (method);
3321 num_args = sig->hasthis + sig->param_count;
3322 ip = (unsigned char*)header->code;
3323 end = ip + header->code_size;
3324 mono_jit_stats.cil_code_size += header->code_size;
3326 if (sig->is_inflated)
3327 generic_context = ((MonoMethodInflated *) method)->context;
3328 else if (generic_container)
3329 generic_context = &generic_container->context;
3331 g_assert (!sig->has_type_parameters);
3333 if (cfg->method == method) {
3335 bbhash = cfg->bb_hash;
3337 real_offset = inline_offset;
3338 bbhash = g_hash_table_new (g_direct_hash, NULL);
3341 if (cfg->verbose_level > 2)
3342 g_print ("method to IR %s\n", mono_method_full_name (method, TRUE));
3344 dont_inline = g_list_prepend (dont_inline, method);
3345 if (cfg->method == method) {
3347 if (cfg->method->save_lmf)
3348 /* Needed by the prolog code */
3349 mono_get_got_var (cfg);
3351 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
3352 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
3355 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
3356 start_bblock->cil_code = NULL;
3357 start_bblock->cil_length = 0;
3358 start_bblock->block_num = cfg->num_bblocks++;
3361 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
3362 end_bblock->cil_code = NULL;
3363 end_bblock->cil_length = 0;
3364 end_bblock->block_num = cfg->num_bblocks++;
3365 g_assert (cfg->num_bblocks == 2);
3367 arg_array = alloca (sizeof (MonoInst *) * num_args);
3368 for (i = num_args - 1; i >= 0; i--)
3369 arg_array [i] = cfg->varinfo [i];
3371 if (header->num_clauses) {
3372 cfg->spvars = g_hash_table_new (NULL, NULL);
3373 cfg->exvars = g_hash_table_new (NULL, NULL);
3375 /* handle exception clauses */
3376 for (i = 0; i < header->num_clauses; ++i) {
3377 MonoBasicBlock *try_bb;
3378 MonoExceptionClause *clause = &header->clauses [i];
3379 GET_BBLOCK (cfg, bbhash, try_bb, ip + clause->try_offset);
3380 try_bb->real_offset = clause->try_offset;
3381 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
3382 tblock->real_offset = clause->handler_offset;
3383 tblock->flags |= BB_EXCEPTION_HANDLER;
3385 link_bblock (cfg, try_bb, tblock);
3387 if (*(ip + clause->handler_offset) == CEE_POP)
3388 tblock->flags |= BB_EXCEPTION_DEAD_OBJ;
3390 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
3391 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
3392 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
3393 MONO_ADD_INS (tblock, ins);
3395 /* todo: is a fault block unsafe to optimize? */
3396 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
3397 tblock->flags |= BB_EXCEPTION_UNSAFE;
3401 /*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);
3403 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
3405 /* catch and filter blocks get the exception object on the stack */
3406 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
3407 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
3408 MonoInst *load, *dummy_use;
3410 /* mostly like handle_stack_args (), but just sets the input args */
3411 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
3412 tblock->in_scount = 1;
3413 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
3414 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
3417 * Add a dummy use for the exvar so its liveness info will be
3420 NEW_TEMPLOAD (cfg, load, tblock->in_stack [0]->inst_c0);
3421 NEW_DUMMY_USE (cfg, dummy_use, load);
3422 MONO_ADD_INS (tblock, dummy_use);
3424 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
3425 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->data.filter_offset);
3426 tblock->real_offset = clause->data.filter_offset;
3427 tblock->in_scount = 1;
3428 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
3429 /* The filter block shares the exvar with the handler block */
3430 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
3431 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
3432 MONO_ADD_INS (tblock, ins);
3437 arg_array = alloca (sizeof (MonoInst *) * num_args);
3438 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
3441 /* FIRST CODE BLOCK */
3442 bblock = NEW_BBLOCK (cfg);
3443 bblock->cil_code = ip;
3445 ADD_BBLOCK (cfg, bbhash, bblock);
3447 if (cfg->method == method) {
3448 breakpoint_id = mono_debugger_method_has_breakpoint (method);
3449 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
3450 MONO_INST_NEW (cfg, ins, CEE_BREAK);
3451 MONO_ADD_INS (bblock, ins);
3455 if (mono_use_security_manager)
3456 secman = mono_security_manager_get_methods ();
3458 security = (secman && mono_method_has_declsec (method));
3459 /* at this point having security doesn't mean we have any code to generate */
3460 if (security && (cfg->method == method)) {
3461 /* Only Demand, NonCasDemand and DemandChoice requires code generation.
3462 * And we do not want to enter the next section (with allocation) if we
3463 * have nothing to generate */
3464 security = mono_declsec_get_demands (method, &actions);
3467 /* we must Demand SecurityPermission.Unmanaged before P/Invoking */
3468 pinvoke = (secman && (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE));
3470 MonoMethod *wrapped = mono_marshal_method_from_wrapper (method);
3471 if (wrapped && (wrapped->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
3472 MonoCustomAttrInfo* custom = mono_custom_attrs_from_method (wrapped);
3474 /* unless the method or it's class has the [SuppressUnmanagedCodeSecurity] attribute */
3475 if (custom && mono_custom_attrs_has_attr (custom, secman->suppressunmanagedcodesecurity)) {
3480 custom = mono_custom_attrs_from_class (wrapped->klass);
3481 if (custom && mono_custom_attrs_has_attr (custom, secman->suppressunmanagedcodesecurity)) {
3486 /* not a P/Invoke after all */
3491 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED))) || mono_compile_aot || security || pinvoke) {
3492 /* we use a separate basic block for the initialization code */
3493 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
3494 init_localsbb->real_offset = real_offset;
3495 start_bblock->next_bb = init_localsbb;
3496 init_localsbb->next_bb = bblock;
3497 link_bblock (cfg, start_bblock, init_localsbb);
3498 link_bblock (cfg, init_localsbb, bblock);
3499 init_localsbb->block_num = cfg->num_bblocks++;
3501 start_bblock->next_bb = bblock;
3502 link_bblock (cfg, start_bblock, bblock);
3505 /* at this point we know, if security is TRUE, that some code needs to be generated */
3506 if (security && (cfg->method == method)) {
3509 mono_jit_stats.cas_demand_generation++;
3511 if (actions.demand.blob) {
3512 /* Add code for SecurityAction.Demand */
3513 NEW_DECLSECCONST (cfg, args[0], image, actions.demand);
3514 NEW_ICONST (cfg, args [1], actions.demand.size);
3515 /* Calls static void SecurityManager.InternalDemand (byte* permissions, int size); */
3516 mono_emit_method_call_spilled (cfg, init_localsbb, secman->demand, mono_method_signature (secman->demand), args, ip, NULL);
3518 if (actions.noncasdemand.blob) {
3519 /* CLR 1.x uses a .noncasdemand (but 2.x doesn't) */
3520 /* For Mono we re-route non-CAS Demand to Demand (as the managed code must deal with it anyway) */
3521 NEW_DECLSECCONST (cfg, args[0], image, actions.noncasdemand);
3522 NEW_ICONST (cfg, args [1], actions.noncasdemand.size);
3523 /* Calls static void SecurityManager.InternalDemand (byte* permissions, int size); */
3524 mono_emit_method_call_spilled (cfg, init_localsbb, secman->demand, mono_method_signature (secman->demand), args, ip, NULL);
3526 if (actions.demandchoice.blob) {
3527 /* New in 2.0, Demand must succeed for one of the permissions (i.e. not all) */
3528 NEW_DECLSECCONST (cfg, args[0], image, actions.demandchoice);
3529 NEW_ICONST (cfg, args [1], actions.demandchoice.size);
3530 /* Calls static void SecurityManager.InternalDemandChoice (byte* permissions, int size); */
3531 mono_emit_method_call_spilled (cfg, init_localsbb, secman->demandchoice, mono_method_signature (secman->demandchoice), args, ip, NULL);
3535 /* we must Demand SecurityPermission.Unmanaged before p/invoking */
3537 mono_emit_method_call_spilled (cfg, init_localsbb, secman->demandunmanaged, mono_method_signature (secman->demandunmanaged), NULL, ip, NULL);
3540 if (get_basic_blocks (cfg, bbhash, header, real_offset, ip, end, &err_pos)) {
3545 if (cfg->method == method)
3546 mono_debug_init_method (cfg, bblock, breakpoint_id);
3548 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
3550 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
3551 for (n = 0; n < sig->param_count; ++n)
3552 param_types [n + sig->hasthis] = sig->params [n];
3555 /* do this somewhere outside - not here */
3556 NEW_ICONST (cfg, zero_int32, 0);
3557 NEW_ICONST (cfg, zero_int64, 0);
3558 zero_int64->type = STACK_I8;
3559 NEW_PCONST (cfg, zero_ptr, 0);
3560 NEW_PCONST (cfg, zero_obj, 0);
3561 zero_obj->type = STACK_OBJ;
3563 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
3564 zero_r8->type = STACK_R8;
3565 zero_r8->inst_p0 = &r8_0;
3567 /* add a check for this != NULL to inlined methods */
3568 if (is_virtual_call) {
3569 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
3570 NEW_ARGLOAD (cfg, ins->inst_left, 0);
3572 MONO_ADD_INS (bblock, ins);
3575 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
3576 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
3579 start_new_bblock = 0;
3582 if (cfg->method == method)
3583 real_offset = ip - header->code;
3585 real_offset = inline_offset;
3587 if (start_new_bblock) {
3588 bblock->cil_length = ip - bblock->cil_code;
3589 if (start_new_bblock == 2) {
3590 g_assert (ip == tblock->cil_code);
3592 GET_BBLOCK (cfg, bbhash, tblock, ip);
3594 bblock->next_bb = tblock;
3596 start_new_bblock = 0;
3597 for (i = 0; i < bblock->in_scount; ++i) {
3598 if (cfg->verbose_level > 3)
3599 g_print ("loading %d from temp %d\n", i, (int)bblock->in_stack [i]->inst_c0);
3600 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
3603 g_slist_free (class_inits);
3606 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
3607 link_bblock (cfg, bblock, tblock);
3608 if (sp != stack_start) {
3609 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3612 bblock->next_bb = tblock;
3614 for (i = 0; i < bblock->in_scount; ++i) {
3615 if (cfg->verbose_level > 3)
3616 g_print ("loading %d from temp %d\n", i, (int)bblock->in_stack [i]->inst_c0);
3617 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
3620 g_slist_free (class_inits);
3625 bblock->real_offset = real_offset;
3627 if ((cfg->method == method) && cfg->coverage_info) {
3628 MonoInst *store, *one;
3629 guint32 cil_offset = ip - header->code;
3630 cfg->coverage_info->data [cil_offset].cil_code = ip;
3632 /* TODO: Use an increment here */
3633 NEW_ICONST (cfg, one, 1);
3636 NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
3639 MONO_INST_NEW (cfg, store, CEE_STIND_I);
3640 store->cil_code = ip;
3641 store->inst_left = ins;
3642 store->inst_right = one;
3644 MONO_ADD_INS (bblock, store);
3647 if (cfg->verbose_level > 3)
3648 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
3653 MONO_INST_NEW (cfg, ins, *ip);
3654 ins->cil_code = ip++;
3655 MONO_ADD_INS (bblock, ins);
3661 CHECK_STACK_OVF (1);
3662 n = (*ip)-CEE_LDARG_0;
3664 NEW_ARGLOAD (cfg, ins, n);
3665 ins->cil_code = ip++;
3672 CHECK_STACK_OVF (1);
3673 n = (*ip)-CEE_LDLOC_0;
3675 NEW_LOCLOAD (cfg, ins, n);
3676 ins->cil_code = ip++;
3684 n = (*ip)-CEE_STLOC_0;
3687 handle_loaded_temps (cfg, bblock, stack_start, sp);
3688 NEW_LOCSTORE (cfg, ins, n, *sp);
3690 if (ins->opcode == CEE_STOBJ) {
3691 NEW_LOCLOADA (cfg, ins, n);
3692 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3694 MONO_ADD_INS (bblock, ins);
3700 CHECK_STACK_OVF (1);
3702 NEW_ARGLOAD (cfg, ins, ip [1]);
3709 CHECK_STACK_OVF (1);
3711 NEW_ARGLOADA (cfg, ins, ip [1]);
3721 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
3722 handle_loaded_temps (cfg, bblock, stack_start, sp);
3724 if (ins->opcode == CEE_STOBJ) {
3725 NEW_ARGLOADA (cfg, ins, ip [1]);
3726 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3728 MONO_ADD_INS (bblock, ins);
3733 CHECK_STACK_OVF (1);
3734 CHECK_LOCAL (ip [1]);
3735 NEW_LOCLOAD (cfg, ins, ip [1]);
3742 CHECK_STACK_OVF (1);
3743 CHECK_LOCAL (ip [1]);
3744 NEW_LOCLOADA (cfg, ins, ip [1]);
3753 handle_loaded_temps (cfg, bblock, stack_start, sp);
3754 CHECK_LOCAL (ip [1]);
3755 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
3757 if (ins->opcode == CEE_STOBJ) {
3758 NEW_LOCLOADA (cfg, ins, ip [1]);
3759 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3761 MONO_ADD_INS (bblock, ins);
3766 CHECK_STACK_OVF (1);
3767 NEW_PCONST (cfg, ins, NULL);
3769 ins->type = STACK_OBJ;
3774 CHECK_STACK_OVF (1);
3775 NEW_ICONST (cfg, ins, -1);
3789 CHECK_STACK_OVF (1);
3790 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
3797 CHECK_STACK_OVF (1);
3799 NEW_ICONST (cfg, ins, *((signed char*)ip));
3806 CHECK_STACK_OVF (1);
3807 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
3814 CHECK_STACK_OVF (1);
3815 MONO_INST_NEW (cfg, ins, OP_I8CONST);
3817 ins->type = STACK_I8;
3819 ins->inst_l = (gint64)read64 (ip);
3825 /* we should really allocate this only late in the compilation process */
3826 mono_domain_lock (cfg->domain);
3827 f = mono_mempool_alloc (cfg->domain->mp, sizeof (float));
3828 mono_domain_unlock (cfg->domain);
3830 CHECK_STACK_OVF (1);
3831 MONO_INST_NEW (cfg, ins, OP_R4CONST);
3832 ins->type = STACK_R8;
3843 mono_domain_lock (cfg->domain);
3844 d = mono_mempool_alloc (cfg->domain->mp, sizeof (double));
3845 mono_domain_unlock (cfg->domain);
3847 CHECK_STACK_OVF (1);
3848 MONO_INST_NEW (cfg, ins, OP_R8CONST);
3849 ins->type = STACK_R8;
3859 MonoInst *temp, *store;
3861 CHECK_STACK_OVF (1);
3866 * small optimization: if the loaded value was from a local already,
3867 * just load it twice.
3869 if (ins->ssa_op == MONO_SSA_LOAD &&
3870 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
3872 MONO_INST_NEW (cfg, temp, 0);
3874 temp->cil_code = ip;
3877 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3878 temp->flags |= MONO_INST_IS_TEMP;
3879 temp->cil_code = ip;
3880 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3881 store->cil_code = ip;
3882 if (store->opcode == CEE_STOBJ) {
3883 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
3884 handle_stobj (cfg, bblock, store, sp [0], sp [0]->cil_code, store->klass, TRUE, FALSE);
3886 MONO_ADD_INS (bblock, store);
3888 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3891 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3901 MONO_INST_NEW (cfg, ins, CEE_POP);
3902 MONO_ADD_INS (bblock, ins);
3903 ins->cil_code = ip++;
3909 if (stack_start != sp)
3911 MONO_INST_NEW (cfg, ins, CEE_JMP);
3912 token = read32 (ip + 1);
3913 /* FIXME: check the signature matches */
3914 cmethod = mini_get_method (method, token, NULL, generic_context);
3919 if (mono_use_security_manager) {
3920 check_linkdemand (cfg, method, cmethod, bblock, ip);
3923 ins->inst_p0 = cmethod;
3924 MONO_ADD_INS (bblock, ins);
3926 start_new_bblock = 1;
3930 case CEE_CALLVIRT: {
3931 MonoInst *addr = NULL;
3932 MonoMethodSignature *fsig = NULL;
3933 int temp, array_rank = 0;
3934 int virtual = *ip == CEE_CALLVIRT;
3937 token = read32 (ip + 1);
3939 if (*ip == CEE_CALLI) {
3944 if (method->wrapper_type != MONO_WRAPPER_NONE)
3945 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
3947 fsig = mono_metadata_parse_signature (image, token);
3949 n = fsig->param_count + fsig->hasthis;
3951 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3952 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
3953 } else if (constrained_call) {
3954 cmethod = mono_get_method_constrained (image, token, constrained_call, generic_context);
3955 cmethod = mono_get_inflated_method (cmethod);
3957 cmethod = mini_get_method (method, token, NULL, generic_context);
3963 if (!virtual && (cmethod->flags & METHOD_ATTRIBUTE_ABSTRACT))
3964 /* MS.NET seems to silently convert this to a callvirt */
3967 if (!cmethod->klass->inited)
3968 mono_class_init (cmethod->klass);
3970 if (mono_method_signature (cmethod)->pinvoke) {
3971 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
3972 fsig = mono_method_signature (wrapper);
3973 } else if (constrained_call) {
3974 fsig = mono_method_signature (cmethod);
3976 fsig = mono_method_get_signature_full (cmethod, image, token, generic_context);
3979 n = fsig->param_count + fsig->hasthis;
3981 if (mono_use_security_manager) {
3982 check_linkdemand (cfg, method, cmethod, bblock, ip);
3985 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
3986 mini_class_is_system_array (cmethod->klass)) {
3987 array_rank = cmethod->klass->rank;
3990 if (cmethod->string_ctor)
3991 g_assert_not_reached ();
3996 mono_get_got_var (cfg);
3998 /* code in inssel.brg might transform a virtual call to a normal call */
3999 if (!(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
4000 ((cmethod->flags & METHOD_ATTRIBUTE_FINAL) &&
4001 cmethod->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK))
4002 mono_get_got_var (cfg);
4005 if (cmethod && cmethod->klass->generic_container) {
4012 //g_assert (!virtual || fsig->hasthis);
4016 if (constrained_call) {
4018 * We have the `constrained.' prefix opcode.
4020 if (constrained_call->valuetype && !cmethod->klass->valuetype) {
4023 * The type parameter is instantiated as a valuetype,
4024 * but that type doesn't override the method we're
4025 * calling, so we need to box `this'.
4026 * sp [0] is a pointer to the data: we need the value
4027 * in handle_box (), so load it here.
4029 MONO_INST_NEW (cfg, load, mono_type_to_ldind (&constrained_call->byval_arg));
4030 type_to_eval_stack_type (&constrained_call->byval_arg, load);
4031 load->cil_code = ip;
4032 load->inst_left = sp [0];
4033 sp [0] = handle_box (cfg, bblock, load, ip, constrained_call);
4034 } else if (!constrained_call->valuetype) {
4038 * The type parameter is instantiated as a reference
4039 * type. We have a managed pointer on the stack, so
4040 * we need to dereference it here.
4043 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
4045 ins->inst_i0 = sp [0];
4046 ins->type = STACK_OBJ;
4048 } else if (cmethod->klass->valuetype)
4050 constrained_call = NULL;
4053 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp)) {
4058 if (cmethod && virtual &&
4059 (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) &&
4060 !((cmethod->flags & METHOD_ATTRIBUTE_FINAL) &&
4061 cmethod->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK) &&
4062 mono_method_signature (cmethod)->generic_param_count) {
4063 MonoInst *this_temp, *store;
4064 MonoInst *iargs [3];
4066 g_assert (mono_method_signature (cmethod)->is_inflated);
4068 this_temp = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
4069 this_temp->cil_code = ip;
4070 NEW_TEMPSTORE (cfg, store, this_temp->inst_c0, sp [0]);
4072 store->cil_code = ip;
4073 MONO_ADD_INS (bblock, store);
4075 NEW_TEMPLOAD (cfg, iargs [0], this_temp->inst_c0);
4076 NEW_PCONST (cfg, iargs [1], cmethod);
4077 NEW_PCONST (cfg, iargs [2], ((MonoMethodInflated *) cmethod)->context);
4078 temp = mono_emit_jit_icall (cfg, bblock, helper_compile_generic_method, iargs, ip);
4080 NEW_TEMPLOAD (cfg, addr, temp);
4081 NEW_TEMPLOAD (cfg, sp [0], this_temp->inst_c0);
4083 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
4084 NEW_TEMPLOAD (cfg, *sp, temp);
4092 if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL) &&
4093 (mono_metadata_signature_equal (mono_method_signature (method), mono_method_signature (cmethod)))) {
4095 /* FIXME: This assumes the two methods has the same number and type of arguments */
4096 for (i = 0; i < n; ++i) {
4097 /* Check if argument is the same */
4098 NEW_ARGLOAD (cfg, ins, i);
4099 if ((ins->opcode == sp [i]->opcode) && (ins->inst_i0 == sp [i]->inst_i0))
4102 /* Prevent argument from being register allocated */
4103 arg_array [i]->flags |= MONO_INST_VOLATILE;
4104 NEW_ARGSTORE (cfg, ins, i, sp [i]);
4106 if (ins->opcode == CEE_STOBJ) {
4107 NEW_ARGLOADA (cfg, ins, i);
4108 handle_stobj (cfg, bblock, ins, sp [i], sp [i]->cil_code, ins->klass, FALSE, FALSE);
4111 MONO_ADD_INS (bblock, ins);
4113 MONO_INST_NEW (cfg, ins, CEE_JMP);
4115 ins->inst_p0 = cmethod;
4116 ins->inst_p1 = arg_array [0];
4117 MONO_ADD_INS (bblock, ins);
4118 link_bblock (cfg, bblock, end_bblock);
4119 start_new_bblock = 1;
4120 /* skip CEE_RET as well */
4125 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_inst_for_method (cfg, cmethod, fsig, sp))) {
4128 if (MONO_TYPE_IS_VOID (fsig->ret)) {
4129 MONO_ADD_INS (bblock, ins);
4131 type_to_eval_stack_type (fsig->ret, ins);
4140 handle_loaded_temps (cfg, bblock, stack_start, sp);
4142 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
4143 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
4144 mono_method_check_inlining (cfg, cmethod) &&
4145 !g_list_find (dont_inline, cmethod)) {
4147 MonoBasicBlock *ebblock;
4148 gboolean allways = FALSE;
4150 if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
4151 (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
4152 cmethod = mono_marshal_get_native_wrapper (cmethod);
4156 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, allways))) {
4160 GET_BBLOCK (cfg, bbhash, bblock, ip);
4161 ebblock->next_bb = bblock;
4162 link_bblock (cfg, ebblock, bblock);
4164 if (!MONO_TYPE_IS_VOID (fsig->ret))
4167 /* indicates start of a new block, and triggers a load of all
4168 stack arguments at bb boundarie */
4171 inline_costs += costs;
4176 inline_costs += 10 * num_calls++;
4178 /* tail recursion elimination */
4179 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == method && ip [5] == CEE_RET) {
4180 gboolean has_vtargs = FALSE;
4183 /* keep it simple */
4184 for (i = fsig->param_count - 1; i >= 0; i--) {
4185 if (MONO_TYPE_ISSTRUCT (mono_method_signature (cmethod)->params [i]))
4190 for (i = 0; i < n; ++i) {
4191 NEW_ARGSTORE (cfg, ins, i, sp [i]);
4193 MONO_ADD_INS (bblock, ins);
4195 MONO_INST_NEW (cfg, ins, CEE_BR);
4197 MONO_ADD_INS (bblock, ins);
4198 tblock = start_bblock->out_bb [0];
4199 link_bblock (cfg, bblock, tblock);
4200 ins->inst_target_bb = tblock;
4201 start_new_bblock = 1;
4203 /* skip the CEE_RET, too */
4204 if (ip_in_bb (cfg, bblock, ip + 5))
4213 if (*ip == CEE_CALLI) {
4215 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
4216 NEW_TEMPLOAD (cfg, *sp, temp);
4220 } else if (array_rank) {
4223 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
4224 if (sp [fsig->param_count]->type == STACK_OBJ) {
4225 MonoInst *iargs [2];
4226 MonoInst *array, *to_store, *store;
4228 handle_loaded_temps (cfg, bblock, stack_start, sp);
4230 array = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
4231 NEW_TEMPSTORE (cfg, store, array->inst_c0, sp [0]);
4232 store->cil_code = ip;
4233 MONO_ADD_INS (bblock, store);
4234 NEW_TEMPLOAD (cfg, iargs [0], array->inst_c0);
4236 to_store = mono_compile_create_var (cfg, type_from_stack_type (sp [fsig->param_count]), OP_LOCAL);
4237 NEW_TEMPSTORE (cfg, store, to_store->inst_c0, sp [fsig->param_count]);
4238 store->cil_code = ip;
4239 MONO_ADD_INS (bblock, store);
4240 NEW_TEMPLOAD (cfg, iargs [1], to_store->inst_c0);
4243 * We first save the args for the call so that the args are copied to the stack
4244 * and a new instruction tree for them is created. If we don't do this,
4245 * the same MonoInst is added to two different trees and this is not
4248 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref_check, iargs, ip);
4250 NEW_TEMPLOAD (cfg, sp [0], array->inst_c0);
4251 NEW_TEMPLOAD (cfg, sp [fsig->param_count], to_store->inst_c0);
4254 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
4255 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
4257 if (ins->opcode == CEE_STOBJ) {
4258 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
4260 MONO_ADD_INS (bblock, ins);
4263 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
4264 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
4265 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
4269 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
4270 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
4273 g_assert_not_reached ();
4277 if (ip_in_bb (cfg, bblock, ip + 5)
4278 && (!MONO_TYPE_ISSTRUCT (fsig->ret))
4279 && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)
4280 && (CODE_IS_STLOC (ip + 5) || ip [5] == CEE_POP || ip [5] == CEE_RET)) {
4281 /* no need to spill */
4282 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
4285 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) {
4286 NEW_TEMPLOAD (cfg, *sp, temp);
4296 if (cfg->method != method) {
4297 /* return from inlined methode */
4302 //g_assert (returnvar != -1);
4303 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
4304 store->cil_code = sp [0]->cil_code;
4305 if (store->opcode == CEE_STOBJ) {
4306 g_assert_not_reached ();
4307 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
4308 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
4310 MONO_ADD_INS (bblock, store);
4314 g_assert (!return_var);
4317 MONO_INST_NEW (cfg, ins, CEE_NOP);
4318 ins->opcode = mono_type_to_stind (mono_method_signature (method)->ret);
4319 if (ins->opcode == CEE_STOBJ) {
4320 NEW_RETLOADA (cfg, ins);
4321 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4323 ins->opcode = OP_SETRET;
4325 ins->inst_i0 = *sp;;
4326 ins->inst_i1 = NULL;
4327 MONO_ADD_INS (bblock, ins);
4331 if (sp != stack_start)
4333 MONO_INST_NEW (cfg, ins, CEE_BR);
4334 ins->cil_code = ip++;
4335 ins->inst_target_bb = end_bblock;
4336 MONO_ADD_INS (bblock, ins);
4337 link_bblock (cfg, bblock, end_bblock);
4338 start_new_bblock = 1;
4342 MONO_INST_NEW (cfg, ins, CEE_BR);
4343 ins->cil_code = ip++;
4344 MONO_ADD_INS (bblock, ins);
4345 target = ip + 1 + (signed char)(*ip);
4347 GET_BBLOCK (cfg, bbhash, tblock, target);
4348 link_bblock (cfg, bblock, tblock);
4349 CHECK_BBLOCK (target, ip, tblock);
4350 ins->inst_target_bb = tblock;
4351 if (sp != stack_start) {
4352 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4355 start_new_bblock = 1;
4362 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
4363 ins->cil_code = ip++;
4364 target = ip + 1 + *(signed char*)ip;
4366 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
4367 if (sp != stack_start) {
4368 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4385 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
4386 ins->cil_code = ip++;
4387 target = ip + 1 + *(signed char*)ip;
4390 if (sp != stack_start) {
4391 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4398 MONO_INST_NEW (cfg, ins, CEE_BR);
4399 ins->cil_code = ip++;
4400 MONO_ADD_INS (bblock, ins);
4401 target = ip + 4 + (gint32)read32(ip);
4403 GET_BBLOCK (cfg, bbhash, tblock, target);
4404 link_bblock (cfg, bblock, tblock);
4405 CHECK_BBLOCK (target, ip, tblock);
4406 ins->inst_target_bb = tblock;
4407 if (sp != stack_start) {
4408 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4411 start_new_bblock = 1;
4418 MONO_INST_NEW (cfg, ins, *ip);
4419 ins->cil_code = ip++;
4420 target = ip + 4 + (gint32)read32(ip);
4422 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
4423 if (sp != stack_start) {
4424 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4441 MONO_INST_NEW (cfg, ins, *ip);
4442 ins->cil_code = ip++;
4443 target = ip + 4 + (gint32)read32(ip);
4446 if (sp != stack_start) {
4447 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4455 n = read32 (ip + 1);
4456 MONO_INST_NEW (cfg, ins, *ip);
4458 ins->inst_left = *sp;
4459 if ((ins->inst_left->type != STACK_I4) && (ins->inst_left->type != STACK_PTR))
4463 CHECK_OPSIZE (n * sizeof (guint32));
4464 target = ip + n * sizeof (guint32);
4465 MONO_ADD_INS (bblock, ins);
4466 GET_BBLOCK (cfg, bbhash, tblock, target);
4467 link_bblock (cfg, bblock, tblock);
4468 ins->klass = GUINT_TO_POINTER (n);
4469 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
4470 ins->inst_many_bb [n] = tblock;
4472 for (i = 0; i < n; ++i) {
4473 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
4474 link_bblock (cfg, bblock, tblock);
4475 ins->inst_many_bb [i] = tblock;
4478 if (sp != stack_start) {
4479 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4482 /* Needed by the code generated in inssel.brg */
4483 mono_get_got_var (cfg);
4498 MONO_INST_NEW (cfg, ins, *ip);
4503 ins->type = ldind_type [*ip - CEE_LDIND_I1];
4504 ins->flags |= ins_flag;
4516 MONO_INST_NEW (cfg, ins, *ip);
4517 ins->cil_code = ip++;
4519 handle_loaded_temps (cfg, bblock, stack_start, sp);
4520 MONO_ADD_INS (bblock, ins);
4521 ins->inst_i0 = sp [0];
4522 ins->inst_i1 = sp [1];
4523 ins->flags |= ins_flag;
4531 #ifdef MONO_ARCH_NO_EMULATE_MUL_IMM
4532 /* FIXME: This breaks with ssapre (mono -O=ssapre loader.exe) */
4533 if ((ins->inst_right->opcode == OP_ICONST) && !(cfg->opt & MONO_OPT_SSAPRE)) {
4534 switch (ins->opcode) {
4536 ins->opcode = OP_IMUL_IMM;
4537 ins->inst_imm = ins->inst_right->inst_c0;
4540 ins->opcode = OP_LMUL_IMM;
4541 ins->inst_imm = ins->inst_right->inst_c0;
4544 g_assert_not_reached ();
4549 if (mono_find_jit_opcode_emulation (ins->opcode)) {
4551 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
4552 mono_get_got_var (cfg);
4570 /* special case that gives a nice speedup and happens to workaorund a ppc jit but (for the release)
4571 * later apply the speedup to the left shift as well
4574 if ((ins->opcode == OP_LSHR_UN) && (ins->type == STACK_I8)
4575 && (ins->inst_right->opcode == OP_ICONST) && (ins->inst_right->inst_c0 == 32)) {
4576 ins->opcode = OP_LONG_SHRUN_32;
4577 /*g_print ("applied long shr speedup to %s\n", cfg->method->name);*/
4581 if (mono_find_jit_opcode_emulation (ins->opcode)) {
4583 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
4584 mono_get_got_var (cfg);
4598 case CEE_CONV_OVF_I8:
4599 case CEE_CONV_OVF_U8:
4603 if (mono_find_jit_opcode_emulation (ins->opcode)) {
4605 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
4606 mono_get_got_var (cfg);
4610 case CEE_CONV_OVF_I4:
4611 case CEE_CONV_OVF_I1:
4612 case CEE_CONV_OVF_I2:
4613 case CEE_CONV_OVF_I:
4614 case CEE_CONV_OVF_U:
4617 if (sp [-1]->type == STACK_R8) {
4618 ADD_UNOP (CEE_CONV_OVF_I8);
4626 case CEE_CONV_OVF_U1:
4627 case CEE_CONV_OVF_U2:
4628 case CEE_CONV_OVF_U4:
4631 if (sp [-1]->type == STACK_R8) {
4632 ADD_UNOP (CEE_CONV_OVF_U8);
4640 case CEE_CONV_OVF_I1_UN:
4641 case CEE_CONV_OVF_I2_UN:
4642 case CEE_CONV_OVF_I4_UN:
4643 case CEE_CONV_OVF_I8_UN:
4644 case CEE_CONV_OVF_U1_UN:
4645 case CEE_CONV_OVF_U2_UN:
4646 case CEE_CONV_OVF_U4_UN:
4647 case CEE_CONV_OVF_U8_UN:
4648 case CEE_CONV_OVF_I_UN:
4649 case CEE_CONV_OVF_U_UN:
4657 token = read32 (ip + 1);
4658 klass = mini_get_class (method, token, generic_context);
4662 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4663 MonoInst *store, *load;
4664 MONO_INST_NEW (cfg, load, CEE_LDIND_REF);
4665 load->cil_code = ip;
4666 load->inst_i0 = sp [1];
4667 load->type = STACK_OBJ;
4668 load->flags |= ins_flag;
4669 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
4670 store->cil_code = ip;
4671 handle_loaded_temps (cfg, bblock, stack_start, sp);
4672 MONO_ADD_INS (bblock, store);
4673 store->inst_i0 = sp [0];
4674 store->inst_i1 = load;
4675 store->flags |= ins_flag;
4677 n = mono_class_value_size (klass, NULL);
4678 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
4680 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
4681 copy->inst_left = sp [0];
4682 copy->inst_right = sp [1];
4683 copy->cil_code = ip;
4685 MONO_ADD_INS (bblock, copy);
4687 MonoMethod *memcpy_method = get_memcpy_method ();
4688 MonoInst *iargs [3];
4691 NEW_ICONST (cfg, iargs [2], n);
4692 iargs [2]->cil_code = ip;
4694 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
4701 MonoInst *iargs [3];
4707 token = read32 (ip + 1);
4708 klass = mini_get_class (method, token, generic_context);
4711 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4712 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
4714 ins->inst_i0 = sp [0];
4715 ins->type = STACK_OBJ;
4716 ins->flags |= ins_flag;
4723 /* Optimize the common ldobj+stloc combination */
4733 loc_index = ip [5] - CEE_STLOC_0;
4740 if ((loc_index != -1) && ip_in_bb (cfg, bblock, ip + 5)) {
4741 CHECK_LOCAL (loc_index);
4742 NEW_LOCSTORE (cfg, ins, loc_index, *sp);
4744 if (ins->opcode == CEE_STOBJ) {
4745 handle_loaded_temps (cfg, bblock, stack_start, sp);
4747 g_assert (ins->opcode == CEE_STOBJ);
4748 NEW_LOCLOADA (cfg, ins, loc_index);
4749 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4756 n = mono_class_value_size (klass, NULL);
4757 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
4758 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
4759 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
4761 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
4762 copy->inst_left = iargs [0];
4763 copy->inst_right = *sp;
4764 copy->cil_code = ip;
4766 MONO_ADD_INS (bblock, copy);
4768 MonoMethod *memcpy_method = get_memcpy_method ();
4770 NEW_ICONST (cfg, iargs [2], n);
4771 iargs [2]->cil_code = ip;
4773 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
4775 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
4783 CHECK_STACK_OVF (1);
4785 n = read32 (ip + 1);
4787 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
4788 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, n));
4790 ins->type = STACK_OBJ;
4793 else if (method->wrapper_type != MONO_WRAPPER_NONE) {
4795 MonoInst *iargs [1];
4797 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
4798 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
4799 NEW_TEMPLOAD (cfg, *sp, temp);
4803 if (cfg->opt & MONO_OPT_SHARED) {
4805 MonoInst *iargs [3];
4806 MonoInst* domain_var;
4808 if (cfg->compile_aot) {
4809 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, GINT_TO_POINTER (n));
4811 /* avoid depending on undefined C behavior in sequence points */
4812 domain_var = mono_get_domainvar (cfg);
4813 NEW_TEMPLOAD (cfg, iargs [0], domain_var->inst_c0);
4814 NEW_IMAGECONST (cfg, iargs [1], image);
4815 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
4816 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
4817 NEW_TEMPLOAD (cfg, *sp, temp);
4818 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
4820 if (bblock->out_of_line) {
4821 MonoInst *iargs [2];
4824 /* Avoid creating the string object */
4825 NEW_IMAGECONST (cfg, iargs [0], image);
4826 NEW_ICONST (cfg, iargs [1], mono_metadata_token_index (n));
4827 temp = mono_emit_jit_icall (cfg, bblock, helper_ldstr, iargs, ip);
4828 NEW_TEMPLOAD (cfg, *sp, temp);
4831 if (cfg->compile_aot) {
4832 NEW_LDSTRCONST (cfg, ins, image, n);
4836 NEW_PCONST (cfg, ins, NULL);
4838 ins->type = STACK_OBJ;
4839 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
4849 MonoInst *iargs [2];
4850 MonoMethodSignature *fsig;
4854 token = read32 (ip + 1);
4855 cmethod = mini_get_method (method, token, NULL, generic_context);
4858 fsig = mono_method_get_signature (cmethod, image, token);
4860 mono_class_init (cmethod->klass);
4862 if (mono_use_security_manager) {
4863 check_linkdemand (cfg, method, cmethod, bblock, ip);
4866 n = fsig->param_count;
4869 /* move the args to allow room for 'this' in the first position */
4875 handle_loaded_temps (cfg, bblock, stack_start, sp);
4877 if (mini_class_is_system_array (cmethod->klass)) {
4878 NEW_METHODCONST (cfg, *sp, cmethod);
4879 temp = handle_array_new (cfg, bblock, fsig->param_count, sp, ip);
4880 } else if (cmethod->string_ctor) {
4881 /* we simply pass a null pointer */
4882 NEW_PCONST (cfg, *sp, NULL);
4883 /* now call the string ctor */
4884 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
4886 MonoInst* callvirt_this_arg = NULL;
4888 if (cmethod->klass->valuetype) {
4889 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
4890 temp = iargs [0]->inst_c0;
4892 NEW_TEMPLOADA (cfg, *sp, temp);
4894 handle_initobj (cfg, bblock, *sp, NULL, cmethod->klass, stack_start, sp);
4896 NEW_TEMPLOADA (cfg, *sp, temp);
4899 * The code generated by mini_emit_virtual_call () expects
4900 * iargs [0] to be a boxed instance, but luckily the vcall
4901 * will be transformed into a normal call there. The AOT
4902 * case needs an already allocate got_var.
4904 mono_get_got_var (cfg);
4906 temp = handle_alloc (cfg, bblock, cmethod->klass, FALSE, ip);
4907 NEW_TEMPLOAD (cfg, *sp, temp);
4910 /* Avoid virtual calls to ctors if possible */
4911 if (cmethod->klass->marshalbyref)
4912 callvirt_this_arg = sp [0];
4914 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
4915 mono_method_check_inlining (cfg, cmethod) &&
4916 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
4917 !g_list_find (dont_inline, cmethod)) {
4919 MonoBasicBlock *ebblock;
4920 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, FALSE))) {
4925 GET_BBLOCK (cfg, bbhash, bblock, ip);
4926 ebblock->next_bb = bblock;
4927 link_bblock (cfg, ebblock, bblock);
4929 NEW_TEMPLOAD (cfg, *sp, temp);
4932 /* indicates start of a new block, and triggers a load
4933 of all stack arguments at bb boundarie */
4936 inline_costs += costs;
4940 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, callvirt_this_arg);
4943 /* now call the actual ctor */
4944 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, callvirt_this_arg);
4948 NEW_TEMPLOAD (cfg, *sp, temp);
4959 token = read32 (ip + 1);
4960 klass = mini_get_class (method, token, generic_context);
4964 /* Needed by the code generated in inssel.brg */
4965 mono_get_got_var (cfg);
4967 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4969 MonoMethod *mono_isinst;
4970 MonoInst *iargs [1];
4971 MonoBasicBlock *ebblock;
4975 mono_isinst = mono_marshal_get_isinst (klass);
4978 costs = inline_method (cfg, mono_isinst, mono_method_signature (mono_isinst), bblock,
4979 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4981 g_assert (costs > 0);
4986 GET_BBLOCK (cfg, bbhash, bblock, ip);
4987 ebblock->next_bb = bblock;
4988 link_bblock (cfg, ebblock, bblock);
4990 temp = iargs [0]->inst_i0->inst_c0;
4991 NEW_TEMPLOAD (cfg, *sp, temp);
4995 inline_costs += costs;
4999 MONO_INST_NEW (cfg, ins, *ip);
5000 ins->type = STACK_OBJ;
5001 ins->inst_left = *sp;
5002 ins->inst_newa_class = klass;
5004 *sp++ = emit_tree (cfg, bblock, ins, ip + 5);
5008 case CEE_UNBOX_ANY: {
5009 MonoInst *add, *vtoffset;
5010 MonoInst *iargs [3];
5015 token = read32 (ip + 1);
5016 klass = mini_get_class (method, token, generic_context);
5020 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5022 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
5023 MonoMethod *mono_castclass;
5024 MonoInst *iargs [1];
5025 MonoBasicBlock *ebblock;
5029 mono_castclass = mono_marshal_get_castclass (klass);
5032 costs = inline_method (cfg, mono_castclass, mono_method_signature (mono_castclass), bblock,
5033 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
5035 g_assert (costs > 0);
5040 GET_BBLOCK (cfg, bbhash, bblock, ip);
5041 ebblock->next_bb = bblock;
5042 link_bblock (cfg, ebblock, bblock);
5044 temp = iargs [0]->inst_i0->inst_c0;
5045 NEW_TEMPLOAD (cfg, *sp, temp);
5049 inline_costs += costs;
5052 MONO_INST_NEW (cfg, ins, CEE_CASTCLASS);
5053 ins->type = STACK_OBJ;
5054 ins->inst_left = *sp;
5056 ins->inst_newa_class = klass;
5064 if (mono_class_is_nullable (klass)) {
5065 int v = handle_unbox_nullable (cfg, bblock, *sp, ip, klass);
5066 NEW_TEMPLOAD (cfg, *sp, v);
5072 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
5073 ins->type = STACK_OBJ;
5074 ins->inst_left = *sp;
5076 ins->inst_newa_class = klass;
5079 MONO_INST_NEW (cfg, add, OP_PADD);
5080 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
5081 add->inst_left = ins;
5082 add->inst_right = vtoffset;
5083 add->type = STACK_MP;
5087 n = mono_class_value_size (klass, NULL);
5088 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
5089 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
5090 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
5092 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
5093 copy->inst_left = iargs [0];
5094 copy->inst_right = *sp;
5095 copy->cil_code = ip;
5097 MONO_ADD_INS (bblock, copy);
5099 MonoMethod *memcpy_method = get_memcpy_method ();
5101 NEW_ICONST (cfg, iargs [2], n);
5102 iargs [2]->cil_code = ip;
5104 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
5106 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
5112 MonoInst *add, *vtoffset;
5117 token = read32 (ip + 1);
5118 klass = mini_get_class (method, token, generic_context);
5122 if (mono_class_is_nullable (klass)) {
5123 int v = handle_unbox_nullable (cfg, bblock, *sp, ip, klass);
5124 NEW_TEMPLOAD (cfg, *sp, v);
5130 /* Needed by the code generated in inssel.brg */
5131 mono_get_got_var (cfg);
5133 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
5134 ins->type = STACK_OBJ;
5135 ins->inst_left = *sp;
5137 ins->inst_newa_class = klass;
5140 MONO_INST_NEW (cfg, add, OP_PADD);
5141 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
5142 add->inst_left = ins;
5143 add->inst_right = vtoffset;
5144 add->type = STACK_MP;
5154 token = read32 (ip + 1);
5155 klass = mini_get_class (method, token, generic_context);
5159 /* Needed by the code generated in inssel.brg */
5160 mono_get_got_var (cfg);
5162 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
5164 MonoMethod *mono_castclass;
5165 MonoInst *iargs [1];
5166 MonoBasicBlock *ebblock;
5170 mono_castclass = mono_marshal_get_castclass (klass);
5173 costs = inline_method (cfg, mono_castclass, mono_method_signature (mono_castclass), bblock,
5174 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
5176 g_assert (costs > 0);
5181 GET_BBLOCK (cfg, bbhash, bblock, ip);
5182 ebblock->next_bb = bblock;
5183 link_bblock (cfg, ebblock, bblock);
5185 temp = iargs [0]->inst_i0->inst_c0;
5186 NEW_TEMPLOAD (cfg, *sp, temp);
5190 inline_costs += costs;
5193 MONO_INST_NEW (cfg, ins, *ip);
5194 ins->type = STACK_OBJ;
5195 ins->inst_left = *sp;
5197 ins->inst_newa_class = klass;
5199 *sp++ = emit_tree (cfg, bblock, ins, ip + 5);
5205 MONO_INST_NEW (cfg, ins, *ip);
5207 ins->inst_left = *sp;
5208 ins->cil_code = ip++;
5209 bblock->out_of_line = TRUE;
5210 MONO_ADD_INS (bblock, ins);
5211 MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
5212 ins->cil_code = ip - 1;
5213 MONO_ADD_INS (bblock, ins);
5216 link_bblock (cfg, bblock, end_bblock);
5217 start_new_bblock = 1;
5218 mono_get_got_var (cfg);
5223 MonoInst *offset_ins;
5224 MonoClassField *field;
5225 MonoBasicBlock *ebblock;
5229 if (*ip == CEE_STFLD) {
5236 // FIXME: enable this test later.
5237 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
5240 token = read32 (ip + 1);
5241 field = mono_field_from_token (image, token, &klass, generic_context);
5244 mono_class_init (klass);
5246 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
5247 /* FIXME: mark instructions for use in SSA */
5248 if (*ip == CEE_STFLD) {
5249 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound || klass == mono_defaults.marshalbyrefobject_class) {
5250 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
5251 MonoInst *iargs [5];
5254 NEW_CLASSCONST (cfg, iargs [1], klass);
5255 NEW_FIELDCONST (cfg, iargs [2], field);
5256 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
5260 if (cfg->opt & MONO_OPT_INLINE) {
5261 costs = inline_method (cfg, stfld_wrapper, mono_method_signature (stfld_wrapper), bblock,
5262 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
5263 g_assert (costs > 0);
5268 GET_BBLOCK (cfg, bbhash, bblock, ip);
5269 ebblock->next_bb = bblock;
5270 link_bblock (cfg, ebblock, bblock);
5272 /* indicates start of a new block, and triggers a load
5273 of all stack arguments at bb boundarie */
5276 inline_costs += costs;
5279 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, mono_method_signature (stfld_wrapper), iargs, ip, NULL);
5283 NEW_ICONST (cfg, offset_ins, foffset);
5284 MONO_INST_NEW (cfg, ins, OP_PADD);
5286 ins->inst_left = *sp;
5287 ins->inst_right = offset_ins;
5288 ins->type = STACK_MP;
5290 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
5291 store->cil_code = ip;
5292 store->inst_left = ins;
5293 store->inst_right = sp [1];
5294 handle_loaded_temps (cfg, bblock, stack_start, sp);
5295 store->flags |= ins_flag;
5297 if (store->opcode == CEE_STOBJ) {
5298 handle_stobj (cfg, bblock, ins, sp [1], ip,
5299 mono_class_from_mono_type (field->type), FALSE, FALSE);
5301 MONO_ADD_INS (bblock, store);
5304 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound || klass == mono_defaults.marshalbyrefobject_class) {
5305 MonoMethod *wrapper = (*ip == CEE_LDFLDA) ? mono_marshal_get_ldflda_wrapper (field->type) : mono_marshal_get_ldfld_wrapper (field->type);
5306 MonoInst *iargs [4];
5310 NEW_CLASSCONST (cfg, iargs [1], klass);
5311 NEW_FIELDCONST (cfg, iargs [2], field);
5312 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
5313 if ((cfg->opt & MONO_OPT_INLINE) && !MONO_TYPE_ISSTRUCT (mono_method_signature (wrapper)->ret)) {
5314 costs = inline_method (cfg, wrapper, mono_method_signature (wrapper), bblock,
5315 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
5316 g_assert (costs > 0);
5321 GET_BBLOCK (cfg, bbhash, bblock, ip);
5322 ebblock->next_bb = bblock;
5323 link_bblock (cfg, ebblock, bblock);
5325 temp = iargs [0]->inst_i0->inst_c0;
5327 NEW_TEMPLOAD (cfg, *sp, temp);
5330 /* indicates start of a new block, and triggers a load of
5331 all stack arguments at bb boundarie */
5334 inline_costs += costs;
5337 temp = mono_emit_method_call_spilled (cfg, bblock, wrapper, mono_method_signature (wrapper), iargs, ip, NULL);
5338 NEW_TEMPLOAD (cfg, *sp, temp);
5342 NEW_ICONST (cfg, offset_ins, foffset);
5343 MONO_INST_NEW (cfg, ins, OP_PADD);
5345 ins->inst_left = *sp;
5346 ins->inst_right = offset_ins;
5347 ins->type = STACK_MP;
5349 if (*ip == CEE_LDFLDA) {
5353 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
5354 type_to_eval_stack_type (field->type, load);
5355 load->cil_code = ip;
5356 load->inst_left = ins;
5357 load->flags |= ins_flag;
5369 MonoClassField *field;
5370 gpointer addr = NULL;
5373 token = read32 (ip + 1);
5375 field = mono_field_from_token (image, token, &klass, generic_context);
5378 mono_class_init (klass);
5380 g_assert (!(field->type->attrs & FIELD_ATTRIBUTE_LITERAL));
5382 if ((*ip) == CEE_STSFLD)
5383 handle_loaded_temps (cfg, bblock, stack_start, sp);
5385 /* The special_static_fields field is init'd in mono_class_vtable, so it needs
5386 * to be called here.
5388 if (!(cfg->opt & MONO_OPT_SHARED))
5389 mono_class_vtable (cfg->domain, klass);
5390 mono_domain_lock (cfg->domain);
5391 if (cfg->domain->special_static_fields)
5392 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
5393 mono_domain_unlock (cfg->domain);
5395 if ((cfg->opt & MONO_OPT_SHARED) || (cfg->compile_aot && addr)) {
5397 MonoInst *iargs [2];
5398 MonoInst *domain_var;
5400 g_assert (field->parent);
5401 /* avoid depending on undefined C behavior in sequence points */
5402 domain_var = mono_get_domainvar (cfg);
5403 NEW_TEMPLOAD (cfg, iargs [0], domain_var->inst_c0);
5404 NEW_FIELDCONST (cfg, iargs [1], field);
5405 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
5406 NEW_TEMPLOAD (cfg, ins, temp);
5409 vtable = mono_class_vtable (cfg->domain, klass);
5411 if (mini_field_access_needs_cctor_run (cfg, method, vtable) && !(g_slist_find (class_inits, vtable))) {
5412 guint8 *tramp = mono_create_class_init_trampoline (vtable);
5413 mono_emit_native_call (cfg, bblock, tramp,
5414 helper_sig_class_init_trampoline,
5415 NULL, ip, FALSE, FALSE);
5416 if (cfg->verbose_level > 2)
5417 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
5418 class_inits = g_slist_prepend (class_inits, vtable);
5420 if (cfg->run_cctors)
5421 mono_runtime_class_init (vtable);
5423 addr = (char*)vtable->data + field->offset;
5425 if (cfg->compile_aot)
5426 NEW_SFLDACONST (cfg, ins, field);
5428 NEW_PCONST (cfg, ins, addr);
5432 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
5433 * This could be later optimized to do just a couple of
5434 * memory dereferences with constant offsets.
5437 MonoInst *iargs [1];
5438 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
5439 temp = mono_emit_jit_icall (cfg, bblock, mono_get_special_static_data, iargs, ip);
5440 NEW_TEMPLOAD (cfg, ins, temp);
5444 /* FIXME: mark instructions for use in SSA */
5445 if (*ip == CEE_LDSFLDA) {
5447 } else if (*ip == CEE_STSFLD) {
5451 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
5452 store->cil_code = ip;
5453 store->inst_left = ins;
5454 store->inst_right = sp [0];
5455 store->flags |= ins_flag;
5458 if (store->opcode == CEE_STOBJ) {
5459 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
5461 MONO_ADD_INS (bblock, store);
5463 gboolean is_const = FALSE;
5464 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
5465 if (!((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) &&
5466 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
5467 gpointer addr = (char*)vtable->data + field->offset;
5468 int ro_type = field->type->type;
5469 if (ro_type == MONO_TYPE_VALUETYPE && field->type->data.klass->enumtype) {
5470 ro_type = field->type->data.klass->enum_basetype->type;
5472 /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
5475 case MONO_TYPE_BOOLEAN:
5477 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
5481 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
5484 case MONO_TYPE_CHAR:
5486 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
5490 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
5495 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
5499 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
5504 case MONO_TYPE_STRING:
5505 case MONO_TYPE_OBJECT:
5506 case MONO_TYPE_CLASS:
5507 case MONO_TYPE_SZARRAY:
5509 case MONO_TYPE_FNPTR:
5510 case MONO_TYPE_ARRAY:
5511 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
5512 type_to_eval_stack_type (field->type, *sp);
5517 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
5518 sp [0]->type = STACK_I8;
5519 sp [0]->inst_l = *((gint64 *)addr);
5524 case MONO_TYPE_VALUETYPE:
5533 CHECK_STACK_OVF (1);
5534 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
5535 type_to_eval_stack_type (field->type, load);
5536 load->cil_code = ip;
5537 load->inst_left = ins;
5539 load->flags |= ins_flag;
5541 /* fixme: dont see the problem why this does not work */
5542 //cfg->disable_aot = TRUE;
5552 token = read32 (ip + 1);
5553 klass = mini_get_class (method, token, generic_context);
5556 n = mono_type_to_stind (&klass->byval_arg);
5557 if (n == CEE_STOBJ) {
5558 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
5560 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
5562 MONO_INST_NEW (cfg, store, n);
5563 store->cil_code = ip;
5564 store->inst_left = sp [0];
5565 store->inst_right = sp [1];
5566 store->flags |= ins_flag;
5567 MONO_ADD_INS (bblock, store);
5579 token = read32 (ip + 1);
5580 klass = mini_get_class (method, token, generic_context);
5584 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5589 *sp++ = handle_box (cfg, bblock, val, ip, klass);
5596 MONO_INST_NEW (cfg, ins, *ip);
5601 token = read32 (ip + 1);
5603 /* allocate the domainvar - becaus this is used in decompose_foreach */
5604 if (cfg->opt & MONO_OPT_SHARED) {
5605 mono_get_domainvar (cfg);
5606 /* LAME-IR: Mark it as used since otherwise it will be optimized away */
5607 cfg->domainvar->flags |= MONO_INST_VOLATILE;
5611 mono_get_got_var (cfg);
5613 klass = mini_get_class (method, token, generic_context);
5616 ins->inst_newa_class = klass;
5617 ins->inst_newa_len = *sp;
5618 ins->type = STACK_OBJ;
5622 * we store the object so calls to create the array are not interleaved
5623 * with the arguments of other calls.
5626 MonoInst *store, *temp, *load;
5628 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
5629 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
5630 store->cil_code = ins->cil_code;
5631 MONO_ADD_INS (bblock, store);
5632 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
5633 load->cil_code = ins->cil_code;
5640 MONO_INST_NEW (cfg, ins, *ip);
5641 ins->cil_code = ip++;
5643 ins->inst_left = *sp;
5644 ins->type = STACK_PTR;
5652 klass = mini_get_class (method, read32 (ip + 1), generic_context);
5655 /* we need to make sure that this array is exactly the type it needs
5656 * to be for correctness. the wrappers are lax with their usage
5657 * so we need to ignore them here
5659 if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE) {
5661 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
5662 check->cil_code = ip;
5663 check->klass = klass;
5664 check->inst_left = sp [0];
5665 check->type = STACK_OBJ;
5669 mono_class_init (klass);
5670 NEW_LDELEMA (cfg, ins, sp, klass);
5675 case CEE_LDELEM_ANY: {
5680 token = read32 (ip + 1);
5681 klass = mono_class_get_full (image, token, generic_context);
5684 mono_class_init (klass);
5685 NEW_LDELEMA (cfg, load, sp, klass);
5686 load->cil_code = ip;
5687 MONO_INST_NEW (cfg, ins, mono_type_to_ldind (&klass->byval_arg));
5689 ins->inst_left = load;
5691 type_to_eval_stack_type (&klass->byval_arg, ins);
5705 case CEE_LDELEM_REF: {
5709 * ldind.x (ldelema (array, index))
5710 * ldelema does the bounds check
5714 klass = array_access_to_klass (*ip);
5715 NEW_LDELEMA (cfg, load, sp, klass);
5716 load->cil_code = ip;
5717 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
5719 ins->inst_left = load;
5721 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
5731 case CEE_STELEM_R8: {
5735 * stind.x (ldelema (array, index), val)
5736 * ldelema does the bounds check
5740 klass = array_access_to_klass (*ip);
5741 NEW_LDELEMA (cfg, load, sp, klass);
5742 load->cil_code = ip;
5743 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
5745 ins->inst_left = load;
5746 ins->inst_right = sp [2];
5748 handle_loaded_temps (cfg, bblock, stack_start, sp);
5749 MONO_ADD_INS (bblock, ins);
5753 case CEE_STELEM_ANY: {
5757 * stind.x (ldelema (array, index), val)
5758 * ldelema does the bounds check
5763 token = read32 (ip + 1);
5764 klass = mono_class_get_full (image, token, generic_context);
5767 mono_class_init (klass);
5768 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5769 MonoMethod* helper = mono_marshal_get_stelemref ();
5770 MonoInst *iargs [3];
5771 handle_loaded_temps (cfg, bblock, stack_start, sp);
5777 mono_emit_method_call_spilled (cfg, bblock, helper, mono_method_signature (helper), iargs, ip, NULL);
5779 NEW_LDELEMA (cfg, load, sp, klass);
5780 load->cil_code = ip;
5782 n = mono_type_to_stind (&klass->byval_arg);
5784 handle_stobj (cfg, bblock, load, sp [2], ip, klass, FALSE, FALSE);
5786 MONO_INST_NEW (cfg, ins, n);
5788 ins->inst_left = load;
5789 ins->inst_right = sp [2];
5790 handle_loaded_temps (cfg, bblock, stack_start, sp);
5791 MONO_ADD_INS (bblock, ins);
5798 case CEE_STELEM_REF: {
5799 MonoInst *iargs [3];
5800 MonoMethod* helper = mono_marshal_get_stelemref ();
5805 handle_loaded_temps (cfg, bblock, stack_start, sp);
5811 mono_emit_method_call_spilled (cfg, bblock, helper, mono_method_signature (helper), iargs, ip, NULL);
5815 NEW_GROUP (cfg, group, sp [0], sp [1]);
5816 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
5818 ins->inst_left = group;
5819 ins->inst_right = sp [2];
5820 MONO_ADD_INS (bblock, ins);
5827 case CEE_CKFINITE: {
5828 MonoInst *store, *temp;
5831 /* this instr. can throw exceptions as side effect,
5832 * so we cant eliminate dead code which contains CKFINITE opdodes.
5833 * Spilling to memory makes sure that we always perform
5837 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
5839 ins->inst_left = sp [-1];
5840 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
5842 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
5843 store->cil_code = ip;
5844 MONO_ADD_INS (bblock, store);
5846 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
5853 MONO_INST_NEW (cfg, ins, *ip);
5856 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
5859 mono_class_init (klass);
5860 ins->type = STACK_MP;
5861 ins->inst_left = *sp;
5863 ins->inst_newa_class = klass;
5868 case CEE_MKREFANY: {
5869 MonoInst *loc, *klassconst;
5872 MONO_INST_NEW (cfg, ins, *ip);
5875 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
5878 mono_class_init (klass);
5881 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
5882 NEW_TEMPLOADA (cfg, ins->inst_right, loc->inst_c0);
5884 NEW_PCONST (cfg, klassconst, klass);
5885 NEW_GROUP (cfg, ins->inst_left, *sp, klassconst);
5887 MONO_ADD_INS (bblock, ins);
5889 NEW_TEMPLOAD (cfg, *sp, loc->inst_c0);
5896 MonoClass *handle_class;
5898 CHECK_STACK_OVF (1);
5901 n = read32 (ip + 1);
5903 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
5904 handle = mono_method_get_wrapper_data (method, n);
5905 handle_class = mono_method_get_wrapper_data (method, n + 1);
5908 handle = mono_ldtoken (image, n, &handle_class, generic_context);
5912 mono_class_init (handle_class);
5914 if (cfg->opt & MONO_OPT_SHARED) {
5916 MonoInst *res, *store, *addr, *vtvar, *iargs [3];
5918 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
5920 NEW_IMAGECONST (cfg, iargs [0], image);
5921 NEW_ICONST (cfg, iargs [1], n);
5922 NEW_PCONST (cfg, iargs [2], generic_context);
5923 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
5924 NEW_TEMPLOAD (cfg, res, temp);
5925 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5926 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
5927 MONO_ADD_INS (bblock, store);
5928 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5930 if ((ip [5] == CEE_CALL) && (cmethod = mini_get_method (method, read32 (ip + 6), NULL, generic_context)) &&
5931 (cmethod->klass == mono_defaults.monotype_class->parent) &&
5932 (strcmp (cmethod->name, "GetTypeFromHandle") == 0) && ip_in_bb (cfg, bblock, ip + 5)) {
5933 MonoClass *tclass = mono_class_from_mono_type (handle);
5934 mono_class_init (tclass);
5935 if (cfg->compile_aot)
5936 NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n);
5938 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
5939 ins->type = STACK_OBJ;
5940 ins->klass = cmethod->klass;
5943 MonoInst *store, *addr, *vtvar;
5945 if (cfg->compile_aot)
5946 NEW_LDTOKENCONST (cfg, ins, image, n);
5948 NEW_PCONST (cfg, ins, handle);
5949 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
5950 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5951 NEW_INDSTORE (cfg, store, addr, ins, &mono_defaults.int_class->byval_arg);
5952 MONO_ADD_INS (bblock, store);
5953 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5969 case CEE_ADD_OVF_UN:
5971 case CEE_MUL_OVF_UN:
5973 case CEE_SUB_OVF_UN:
5976 if (mono_find_jit_opcode_emulation (ins->opcode)) {
5978 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
5979 mono_get_got_var (cfg);
5983 case CEE_ENDFINALLY:
5984 MONO_INST_NEW (cfg, ins, *ip);
5985 MONO_ADD_INS (bblock, ins);
5986 ins->cil_code = ip++;
5987 start_new_bblock = 1;
5990 * Control will leave the method so empty the stack, otherwise
5991 * the next basic block will start with a nonempty stack.
5993 while (sp != stack_start) {
5994 MONO_INST_NEW (cfg, ins, CEE_POP);
5998 MONO_ADD_INS (bblock, ins);
6005 if (*ip == CEE_LEAVE) {
6007 target = ip + 5 + (gint32)read32(ip + 1);
6010 target = ip + 2 + (signed char)(ip [1]);
6013 /* empty the stack */
6014 while (sp != stack_start) {
6015 MONO_INST_NEW (cfg, ins, CEE_POP);
6019 MONO_ADD_INS (bblock, ins);
6023 * If this leave statement is in a catch block, check for a
6024 * pending exception, and rethrow it if necessary.
6026 for (i = 0; i < header->num_clauses; ++i) {
6027 MonoExceptionClause *clause = &header->clauses [i];
6029 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)) {
6033 NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, clause->handler_offset)->inst_c0);
6034 load->cil_code = ip;
6036 temp = mono_emit_jit_icall (cfg, bblock, mono_thread_get_pending_exception, NULL, ip);
6037 NEW_TEMPLOAD (cfg, *sp, temp);
6039 MONO_INST_NEW (cfg, ins, OP_THROW_OR_NULL);
6040 ins->inst_left = *sp;
6041 ins->inst_right = load;
6043 MONO_ADD_INS (bblock, ins);
6047 /* fixme: call fault handler ? */
6049 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
6051 for (tmp = handlers; tmp; tmp = tmp->next) {
6053 link_bblock (cfg, bblock, tblock);
6054 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
6056 ins->inst_target_bb = tblock;
6057 MONO_ADD_INS (bblock, ins);
6059 g_list_free (handlers);
6062 MONO_INST_NEW (cfg, ins, CEE_BR);
6064 MONO_ADD_INS (bblock, ins);
6065 GET_BBLOCK (cfg, bbhash, tblock, target);
6066 link_bblock (cfg, bblock, tblock);
6067 CHECK_BBLOCK (target, ip, tblock);
6068 ins->inst_target_bb = tblock;
6069 start_new_bblock = 1;
6071 if (*ip == CEE_LEAVE)
6080 MONO_INST_NEW (cfg, ins, *ip);
6082 handle_loaded_temps (cfg, bblock, stack_start, sp);
6083 MONO_ADD_INS (bblock, ins);
6084 ins->cil_code = ip++;
6085 ins->inst_i0 = sp [0];
6086 ins->inst_i1 = sp [1];
6094 /* trampoline mono specific opcodes */
6095 case MONO_CUSTOM_PREFIX: {
6097 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
6102 case CEE_MONO_ICALL: {
6105 MonoJitICallInfo *info;
6107 token = read32 (ip + 2);
6108 func = mono_method_get_wrapper_data (method, token);
6109 info = mono_find_jit_icall_by_addr (func);
6112 CHECK_STACK (info->sig->param_count);
6113 sp -= info->sig->param_count;
6115 temp = mono_emit_jit_icall (cfg, bblock, info->func, sp, ip);
6116 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
6117 NEW_TEMPLOAD (cfg, *sp, temp);
6122 inline_costs += 10 * num_calls++;
6126 case CEE_MONO_LDPTR:
6127 CHECK_STACK_OVF (1);
6129 token = read32 (ip + 2);
6130 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
6134 inline_costs += 10 * num_calls++;
6135 /* Can't embed random pointers into AOT code */
6136 cfg->disable_aot = 1;
6138 case CEE_MONO_VTADDR:
6141 MONO_INST_NEW (cfg, ins, OP_VTADDR);
6143 ins->type = STACK_MP;
6144 ins->inst_left = *sp;
6148 case CEE_MONO_NEWOBJ: {
6149 MonoInst *iargs [2];
6151 CHECK_STACK_OVF (1);
6153 token = read32 (ip + 2);
6154 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
6155 mono_class_init (klass);
6156 NEW_DOMAINCONST (cfg, iargs [0]);
6157 NEW_CLASSCONST (cfg, iargs [1], klass);
6158 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
6159 NEW_TEMPLOAD (cfg, *sp, temp);
6162 inline_costs += 10 * num_calls++;
6165 case CEE_MONO_OBJADDR:
6168 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
6170 ins->type = STACK_MP;
6171 ins->inst_left = *sp;
6175 case CEE_MONO_LDNATIVEOBJ:
6178 token = read32 (ip + 2);
6179 klass = mono_method_get_wrapper_data (method, token);
6180 g_assert (klass->valuetype);
6181 mono_class_init (klass);
6182 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
6186 case CEE_MONO_RETOBJ:
6187 g_assert (cfg->ret);
6188 g_assert (mono_method_signature (method)->pinvoke);
6193 token = read32 (ip + 2);
6194 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
6196 NEW_RETLOADA (cfg, ins);
6197 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
6199 if (sp != stack_start)
6202 MONO_INST_NEW (cfg, ins, CEE_BR);
6204 ins->inst_target_bb = end_bblock;
6205 MONO_ADD_INS (bblock, ins);
6206 link_bblock (cfg, bblock, end_bblock);
6207 start_new_bblock = 1;
6210 case CEE_MONO_CISINST:
6211 case CEE_MONO_CCASTCLASS: {
6216 token = read32 (ip + 2);
6217 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
6218 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_CISINST) ? OP_CISINST : OP_CCASTCLASS);
6219 ins->type = STACK_I4;
6220 ins->inst_left = *sp;
6221 ins->inst_newa_class = klass;
6223 *sp++ = emit_tree (cfg, bblock, ins, ip + 6);
6227 case CEE_MONO_SAVE_LMF:
6228 case CEE_MONO_RESTORE_LMF:
6229 #ifdef MONO_ARCH_HAVE_LMF_OPS
6230 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_SAVE_LMF) ? OP_SAVE_LMF : OP_RESTORE_LMF);
6231 MONO_ADD_INS (bblock, ins);
6232 cfg->need_lmf_area = TRUE;
6236 case CEE_MONO_CLASSCONST:
6237 CHECK_STACK_OVF (1);
6239 token = read32 (ip + 2);
6240 NEW_CLASSCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
6244 inline_costs += 10 * num_calls++;
6246 case CEE_MONO_NOT_TAKEN:
6247 bblock->out_of_line = TRUE;
6251 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
6260 /* somewhat similar to LDTOKEN */
6261 MonoInst *addr, *vtvar;
6262 CHECK_STACK_OVF (1);
6263 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
6265 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
6266 addr->cil_code = ip;
6267 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
6269 ins->inst_left = addr;
6270 MONO_ADD_INS (bblock, ins);
6271 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
6285 * The following transforms:
6286 * CEE_CEQ into OP_CEQ
6287 * CEE_CGT into OP_CGT
6288 * CEE_CGT_UN into OP_CGT_UN
6289 * CEE_CLT into OP_CLT
6290 * CEE_CLT_UN into OP_CLT_UN
6292 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
6294 MONO_INST_NEW (cfg, ins, cmp->opcode);
6296 cmp->inst_i0 = sp [0];
6297 cmp->inst_i1 = sp [1];
6301 if ((sp [0]->type == STACK_I8) || ((sizeof (gpointer) == 8) && ((sp [0]->type == STACK_PTR) || (sp [0]->type == STACK_OBJ) || (sp [0]->type == STACK_MP))))
6302 cmp->opcode = OP_LCOMPARE;
6304 cmp->opcode = OP_COMPARE;
6306 ins->type = STACK_I4;
6309 /* spill it to reduce the expression complexity
6310 * and workaround bug 54209
6312 if (cmp->inst_left->type == STACK_I8) {
6314 *sp++ = emit_tree (cfg, bblock, ins, ip + 2);
6323 CHECK_STACK_OVF (1);
6325 n = read32 (ip + 2);
6326 cmethod = mini_get_method (method, n, NULL, generic_context);
6329 mono_class_init (cmethod->klass);
6331 if (mono_use_security_manager) {
6332 check_linkdemand (cfg, method, cmethod, bblock, ip);
6335 handle_loaded_temps (cfg, bblock, stack_start, sp);
6337 NEW_METHODCONST (cfg, argconst, cmethod);
6338 if (method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED)
6339 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
6341 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn_nosync, &argconst, ip);
6342 NEW_TEMPLOAD (cfg, *sp, temp);
6346 inline_costs += 10 * num_calls++;
6349 case CEE_LDVIRTFTN: {
6355 n = read32 (ip + 2);
6356 cmethod = mini_get_method (method, n, NULL, generic_context);
6359 mono_class_init (cmethod->klass);
6361 if (mono_use_security_manager) {
6362 check_linkdemand (cfg, method, cmethod, bblock, ip);
6365 handle_loaded_temps (cfg, bblock, stack_start, sp);
6369 NEW_METHODCONST (cfg, args [1], cmethod);
6370 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
6371 NEW_TEMPLOAD (cfg, *sp, temp);
6375 inline_costs += 10 * num_calls++;
6379 CHECK_STACK_OVF (1);
6381 n = read16 (ip + 2);
6383 NEW_ARGLOAD (cfg, ins, n);
6389 CHECK_STACK_OVF (1);
6391 n = read16 (ip + 2);
6393 NEW_ARGLOADA (cfg, ins, n);
6401 handle_loaded_temps (cfg, bblock, stack_start, sp);
6403 n = read16 (ip + 2);
6405 NEW_ARGSTORE (cfg, ins, n, *sp);
6407 if (ins->opcode == CEE_STOBJ) {
6408 NEW_ARGLOADA (cfg, ins, n);
6409 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
6411 MONO_ADD_INS (bblock, ins);
6415 CHECK_STACK_OVF (1);
6417 n = read16 (ip + 2);
6419 NEW_LOCLOAD (cfg, ins, n);
6425 CHECK_STACK_OVF (1);
6427 n = read16 (ip + 2);
6429 NEW_LOCLOADA (cfg, ins, n);
6438 n = read16 (ip + 2);
6440 handle_loaded_temps (cfg, bblock, stack_start, sp);
6441 NEW_LOCSTORE (cfg, ins, n, *sp);
6443 if (ins->opcode == CEE_STOBJ) {
6444 NEW_LOCLOADA (cfg, ins, n);
6445 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
6447 MONO_ADD_INS (bblock, ins);
6454 if (sp != stack_start)
6456 if (cfg->method != method)
6458 * Inlining this into a loop in a parent could lead to
6459 * stack overflows which is different behavior than the
6460 * non-inlined case, thus disable inlining in this case.
6462 goto inline_failure;
6463 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
6464 ins->inst_left = *sp;
6466 ins->type = STACK_MP;
6468 cfg->flags |= MONO_CFG_HAS_ALLOCA;
6469 if (header->init_locals)
6470 ins->flags |= MONO_INST_INIT;
6474 /* FIXME: set init flag if locals init is set in this method */
6476 case CEE_ENDFILTER: {
6477 MonoExceptionClause *clause, *nearest;
6478 int cc, nearest_num;
6482 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
6484 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
6485 ins->inst_left = *sp;
6487 MONO_ADD_INS (bblock, ins);
6488 start_new_bblock = 1;
6493 for (cc = 0; cc < header->num_clauses; ++cc) {
6494 clause = &header->clauses [cc];
6495 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
6496 ((ip - header->code) > clause->data.filter_offset && (ip - header->code) <= clause->handler_offset) &&
6497 (!nearest || (clause->data.filter_offset < nearest->data.filter_offset))) {
6503 if ((ip - header->code) != nearest->handler_offset)
6508 case CEE_UNALIGNED_:
6509 ins_flag |= MONO_INST_UNALIGNED;
6510 /* FIXME: record alignment? we can assume 1 for now */
6515 ins_flag |= MONO_INST_VOLATILE;
6519 ins_flag |= MONO_INST_TAILCALL;
6520 cfg->flags |= MONO_CFG_HAS_TAIL;
6521 /* Can't inline tail calls at this time */
6522 inline_costs += 100000;
6529 token = read32 (ip + 2);
6530 klass = mini_get_class (method, token, generic_context);
6533 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
6534 MonoInst *store, *load;
6535 NEW_PCONST (cfg, load, NULL);
6536 load->cil_code = ip;
6537 load->type = STACK_OBJ;
6538 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
6539 store->cil_code = ip;
6540 handle_loaded_temps (cfg, bblock, stack_start, sp);
6541 MONO_ADD_INS (bblock, store);
6542 store->inst_i0 = sp [0];
6543 store->inst_i1 = load;
6545 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
6550 case CEE_CONSTRAINED_:
6551 /* FIXME: implement */
6553 token = read32 (ip + 2);
6554 constrained_call = mono_class_get_full (image, token, generic_context);
6555 if (!constrained_call)
6561 MonoInst *iargs [3];
6564 if ((cfg->opt & MONO_OPT_INTRINS) && (ip [1] == CEE_CPBLK) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5)) {
6566 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
6567 copy->inst_left = sp [0];
6568 copy->inst_right = sp [1];
6569 copy->cil_code = ip;
6571 MONO_ADD_INS (bblock, copy);
6578 handle_loaded_temps (cfg, bblock, stack_start, sp);
6579 if (ip [1] == CEE_CPBLK) {
6580 MonoMethod *memcpy_method = get_memcpy_method ();
6581 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
6583 MonoMethod *memset_method = get_memset_method ();
6584 mono_emit_method_call_spilled (cfg, bblock, memset_method, memset_method->signature, iargs, ip, NULL);
6593 ins_flag |= MONO_INST_NOTYPECHECK;
6595 ins_flag |= MONO_INST_NORANGECHECK;
6596 /* we ignore the no-nullcheck for now since we
6597 * really do it explicitly only when doing callvirt->call
6603 int handler_offset = -1;
6605 for (i = 0; i < header->num_clauses; ++i) {
6606 MonoExceptionClause *clause = &header->clauses [i];
6607 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code) && !(clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY))
6608 handler_offset = clause->handler_offset;
6611 bblock->flags |= BB_EXCEPTION_UNSAFE;
6613 g_assert (handler_offset != -1);
6615 NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, handler_offset)->inst_c0);
6616 load->cil_code = ip;
6617 MONO_INST_NEW (cfg, ins, OP_RETHROW);
6618 ins->inst_left = load;
6620 MONO_ADD_INS (bblock, ins);
6622 link_bblock (cfg, bblock, end_bblock);
6623 start_new_bblock = 1;
6625 mono_get_got_var (cfg);
6629 CHECK_STACK_OVF (1);
6631 token = read32 (ip + 2);
6632 /* FIXXME: handle generics. */
6633 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
6634 MonoType *type = mono_type_create_from_typespec (image, token);
6635 token = mono_type_size (type, &align);
6637 MonoClass *klass = mono_class_get_full (image, token, generic_context);
6640 mono_class_init (klass);
6641 token = mono_class_value_size (klass, &align);
6643 NEW_ICONST (cfg, ins, token);
6648 case CEE_REFANYTYPE:
6650 MONO_INST_NEW (cfg, ins, OP_REFANYTYPE);
6652 ins->type = STACK_MP;
6653 ins->inst_left = *sp;
6654 ins->type = STACK_VTYPE;
6655 ins->klass = mono_defaults.typehandle_class;
6664 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
6669 g_error ("opcode 0x%02x not handled", *ip);
6672 if (start_new_bblock != 1)
6675 bblock->cil_length = ip - bblock->cil_code;
6676 bblock->next_bb = end_bblock;
6678 if (cfg->method == method && cfg->domainvar) {
6680 MonoInst *get_domain;
6682 if (! (get_domain = mono_arch_get_domain_intrinsic (cfg))) {
6685 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
6686 call->signature = helper_sig_domain_get;
6687 call->inst.type = STACK_PTR;
6688 call->fptr = mono_domain_get;
6689 get_domain = (MonoInst*)call;
6692 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, get_domain);
6693 MONO_ADD_INS (init_localsbb, store);
6696 if (cfg->method == method && cfg->got_var)
6697 mono_emit_load_got_addr (cfg);
6699 if (header->init_locals) {
6701 for (i = 0; i < header->num_locals; ++i) {
6702 MonoType *ptype = header->locals [i];
6703 int t = ptype->type;
6704 if (t == MONO_TYPE_VALUETYPE && ptype->data.klass->enumtype)
6705 t = ptype->data.klass->enum_basetype->type;
6707 NEW_PCONST (cfg, ins, NULL);
6708 NEW_LOCSTORE (cfg, store, i, ins);
6709 MONO_ADD_INS (init_localsbb, store);
6710 } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
6711 NEW_ICONST (cfg, ins, 0);
6712 NEW_LOCSTORE (cfg, store, i, ins);
6713 MONO_ADD_INS (init_localsbb, store);
6714 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
6715 MONO_INST_NEW (cfg, ins, OP_I8CONST);
6716 ins->type = STACK_I8;
6718 NEW_LOCSTORE (cfg, store, i, ins);
6719 MONO_ADD_INS (init_localsbb, store);
6720 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
6721 MONO_INST_NEW (cfg, ins, OP_R8CONST);
6722 ins->type = STACK_R8;
6723 ins->inst_p0 = (void*)&r8_0;
6724 NEW_LOCSTORE (cfg, store, i, ins);
6725 MONO_ADD_INS (init_localsbb, store);
6726 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
6727 ((t == MONO_TYPE_GENERICINST) && mono_metadata_generic_class_is_valuetype (ptype->data.generic_class))) {
6728 NEW_LOCLOADA (cfg, ins, i);
6729 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (ptype), NULL, NULL);
6731 NEW_PCONST (cfg, ins, NULL);
6732 NEW_LOCSTORE (cfg, store, i, ins);
6733 MONO_ADD_INS (init_localsbb, store);
6738 /* resolve backward branches in the middle of an existing basic block */
6739 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
6741 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
6742 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
6743 if (tblock != start_bblock) {
6745 split_bblock (cfg, tblock, bblock);
6746 l = bblock->cil_code - header->code;
6747 bblock->cil_length = tblock->cil_length - l;
6748 tblock->cil_length = l;
6750 g_print ("recheck failed.\n");
6754 if (cfg->method == method) {
6756 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6757 bb->region = mono_find_block_region (cfg, bb->real_offset);
6759 mono_create_spvar_for_region (cfg, bb->region);
6760 if (cfg->verbose_level > 2)
6761 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
6764 g_hash_table_destroy (bbhash);
6767 g_slist_free (class_inits);
6768 dont_inline = g_list_remove (dont_inline, method);
6769 return inline_costs;
6772 if (cfg->method != method)
6773 g_hash_table_destroy (bbhash);
6774 g_slist_free (class_inits);
6775 dont_inline = g_list_remove (dont_inline, method);
6779 if (cfg->method != method)
6780 g_hash_table_destroy (bbhash);
6781 g_slist_free (class_inits);
6782 dont_inline = g_list_remove (dont_inline, method);
6786 if (cfg->method != method)
6787 g_hash_table_destroy (bbhash);
6788 g_slist_free (class_inits);
6789 g_error ("Invalid IL code at IL%04x in %s: %s\n", (int)(ip - header->code),
6790 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
6791 dont_inline = g_list_remove (dont_inline, method);
6796 mono_print_tree (MonoInst *tree) {
6802 arity = mono_burg_arity [tree->opcode];
6804 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
6806 switch (tree->opcode) {
6808 printf ("[%d]", (int)tree->inst_c0);
6811 printf ("[%lld]", (long long)tree->inst_l);
6814 printf ("[%f]", *(double*)tree->inst_p0);
6817 printf ("[%f]", *(float*)tree->inst_p0);
6821 printf ("[%d]", (int)tree->inst_c0);
6824 if (tree->inst_offset < 0)
6825 printf ("[-0x%x(%s)]", (int)(-tree->inst_offset), mono_arch_regname (tree->inst_basereg));
6827 printf ("[0x%x(%s)]", (int)(tree->inst_offset), mono_arch_regname (tree->inst_basereg));
6830 printf ("[%s]", mono_arch_regname (tree->dreg));
6833 printf ("[%s]", tree->inst_newa_class->name);
6834 mono_print_tree (tree->inst_newa_len);
6845 case OP_VOIDCALLVIRT: {
6846 MonoCallInst *call = (MonoCallInst*)tree;
6848 printf ("[%s]", call->method->name);
6849 else if (call->fptr) {
6850 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (call->fptr);
6852 printf ("[%s]", info->name);
6858 printf ("[%d (", (int)tree->inst_c0);
6859 for (i = 0; i < tree->inst_phi_args [0]; i++) {
6862 printf ("%d", tree->inst_phi_args [i + 1]);
6873 case OP_LOAD_MEMBASE:
6874 case OP_LOADI4_MEMBASE:
6875 case OP_LOADU4_MEMBASE:
6876 case OP_LOADU1_MEMBASE:
6877 case OP_LOADI1_MEMBASE:
6878 case OP_LOADU2_MEMBASE:
6879 case OP_LOADI2_MEMBASE:
6880 printf ("[%s] <- [%s + 0x%x]", mono_arch_regname (tree->dreg), mono_arch_regname (tree->inst_basereg), (int)tree->inst_offset);
6883 case OP_CALL_HANDLER:
6884 printf ("[B%d]", tree->inst_target_bb->block_num);
6894 case OP_VOIDCALL_REG:
6895 mono_print_tree (tree->inst_left);
6907 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
6908 mono_print_tree (tree->inst_left);
6911 if (!mono_arch_print_tree(tree, arity)) {
6913 mono_print_tree (tree->inst_left);
6915 mono_print_tree (tree->inst_right);
6926 mono_print_tree_nl (MonoInst *tree)
6928 mono_print_tree (tree);
6933 create_helper_signature (void)
6935 helper_sig_domain_get = mono_create_icall_signature ("ptr");
6936 helper_sig_class_init_trampoline = mono_create_icall_signature ("void");
6940 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
6943 MonoMethod *wrapper;
6946 if (callinfo->wrapper)
6947 return callinfo->wrapper;
6949 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
6950 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
6951 /* Must be domain neutral since there is only one copy */
6952 code = mono_jit_compile_method_with_opt (wrapper, default_opt | MONO_OPT_SHARED);
6954 if (!callinfo->wrapper) {
6955 callinfo->wrapper = code;
6956 mono_register_jit_icall_wrapper (callinfo, code);
6957 mono_debug_add_icall_wrapper (wrapper, callinfo);
6961 return callinfo->wrapper;
6965 mono_init_trampolines (void)
6967 mono_trampoline_code [MONO_TRAMPOLINE_GENERIC] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_GENERIC);
6968 mono_trampoline_code [MONO_TRAMPOLINE_JUMP] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_JUMP);
6969 mono_trampoline_code [MONO_TRAMPOLINE_CLASS_INIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_CLASS_INIT);
6970 #ifdef MONO_ARCH_HAVE_PIC_AOT
6971 mono_trampoline_code [MONO_TRAMPOLINE_AOT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_AOT);
6973 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
6974 mono_trampoline_code [MONO_TRAMPOLINE_DELEGATE] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_DELEGATE);
6979 mono_init_exceptions (void)
6981 #ifndef CUSTOM_EXCEPTION_HANDLING
6982 mono_arch_get_restore_context ();
6983 mono_arch_get_call_filter ();
6984 mono_arch_get_throw_exception ();
6985 mono_arch_get_rethrow_exception ();
6990 mono_get_trampoline_code (MonoTrampolineType tramp_type)
6992 return mono_trampoline_code [tramp_type];
6996 mono_create_class_init_trampoline (MonoVTable *vtable)
7000 /* previously created trampoline code */
7001 mono_domain_lock (vtable->domain);
7003 g_hash_table_lookup (vtable->domain->class_init_trampoline_hash,
7005 mono_domain_unlock (vtable->domain);
7009 #ifdef MONO_ARCH_HAVE_CREATE_SPECIFIC_TRAMPOLINE
7010 code = mono_arch_create_specific_trampoline (vtable, MONO_TRAMPOLINE_CLASS_INIT, vtable->domain, NULL);
7012 code = mono_arch_create_class_init_trampoline (vtable);
7015 ptr = mono_create_ftnptr (vtable->domain, code);
7017 /* store trampoline address */
7018 mono_domain_lock (vtable->domain);
7019 g_hash_table_insert (vtable->domain->class_init_trampoline_hash,
7021 mono_domain_unlock (vtable->domain);
7024 if (!class_init_hash_addr)
7025 class_init_hash_addr = g_hash_table_new (NULL, NULL);
7026 g_hash_table_insert (class_init_hash_addr, ptr, vtable);
7033 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method,
7034 gboolean add_sync_wrapper)
7038 #ifdef MONO_ARCH_HAVE_CREATE_SPECIFIC_TRAMPOLINE
7042 if (add_sync_wrapper && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
7043 return mono_create_jump_trampoline (domain, mono_marshal_get_synchronized_wrapper (method), FALSE);
7045 code = mono_jit_find_compiled_method (domain, method);
7049 mono_domain_lock (domain);
7050 code = g_hash_table_lookup (domain->jump_trampoline_hash, method);
7051 mono_domain_unlock (domain);
7055 #ifdef MONO_ARCH_HAVE_CREATE_SPECIFIC_TRAMPOLINE
7056 code = mono_arch_create_specific_trampoline (method, MONO_TRAMPOLINE_JUMP, mono_domain_get (), &code_size);
7058 ji = g_new0 (MonoJitInfo, 1);
7059 ji->code_start = code;
7060 ji->code_size = code_size;
7061 ji->method = method;
7063 ji = mono_arch_create_jump_trampoline (method);
7067 * mono_delegate_ctor needs to find the method metadata from the
7068 * trampoline address, so we save it here.
7071 mono_jit_info_table_add (domain, ji);
7073 mono_domain_lock (domain);
7074 g_hash_table_insert (domain->jump_trampoline_hash, method, ji->code_start);
7075 mono_domain_unlock (domain);
7077 return ji->code_start;
7081 mono_create_jit_trampoline (MonoMethod *method)
7083 MonoDomain *domain = mono_domain_get ();
7086 mono_domain_lock (domain);
7087 tramp = g_hash_table_lookup (domain->jit_trampoline_hash, method);
7088 mono_domain_unlock (domain);
7092 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
7093 return mono_create_jit_trampoline (mono_marshal_get_synchronized_wrapper (method));
7095 #ifdef MONO_ARCH_HAVE_CREATE_SPECIFIC_TRAMPOLINE
7096 tramp = mono_arch_create_specific_trampoline (method, MONO_TRAMPOLINE_GENERIC, mono_domain_get (), NULL);
7098 tramp = mono_arch_create_jit_trampoline (method);
7101 mono_domain_lock (domain);
7102 g_hash_table_insert (domain->jit_trampoline_hash, method, tramp);
7103 mono_domain_unlock (domain);
7105 mono_jit_stats.method_trampolines++;
7110 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
7112 mono_create_jit_trampoline_from_token (MonoImage *image, guint32 token)
7116 MonoDomain *domain = mono_domain_get ();
7117 guint8 *buf, *start;
7119 mono_domain_lock (domain);
7120 buf = start = mono_code_manager_reserve (domain->code_mp, 2 * sizeof (gpointer));
7121 mono_domain_unlock (domain);
7123 *(gpointer*)(gpointer)buf = image;
7124 buf += sizeof (gpointer);
7125 *(guint32*)(gpointer)buf = token;
7127 tramp = mono_arch_create_specific_trampoline (start, MONO_TRAMPOLINE_AOT, domain, NULL);
7129 mono_jit_stats.method_trampolines++;
7136 mono_create_delegate_trampoline (MonoMethod *method, gpointer addr)
7138 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
7141 MonoDomain *domain = mono_domain_get ();
7144 code = mono_jit_find_compiled_method (domain, method);
7149 * FIXME: We should return a function descriptor here but it is not stored
7150 * anywhere so it would be leaked.
7154 mono_domain_lock (domain);
7155 ptr = g_hash_table_lookup (domain->delegate_trampoline_hash, method);
7156 mono_domain_unlock (domain);
7160 code = mono_arch_create_specific_trampoline (method, MONO_TRAMPOLINE_DELEGATE, domain, &code_size);
7162 ptr = mono_create_ftnptr (domain, code);
7164 /* store trampoline address */
7165 mono_domain_lock (domain);
7166 g_hash_table_insert (domain->delegate_trampoline_hash,
7168 mono_domain_unlock (domain);
7177 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
7182 if (class_init_hash_addr)
7183 res = g_hash_table_lookup (class_init_hash_addr, addr);
7191 mono_dynamic_code_hash_insert (MonoDomain *domain, MonoMethod *method, MonoJitDynamicMethodInfo *ji)
7193 if (!domain->dynamic_code_hash)
7194 domain->dynamic_code_hash = g_hash_table_new (NULL, NULL);
7195 g_hash_table_insert (domain->dynamic_code_hash, method, ji);
7198 static MonoJitDynamicMethodInfo*
7199 mono_dynamic_code_hash_lookup (MonoDomain *domain, MonoMethod *method)
7201 MonoJitDynamicMethodInfo *res;
7203 if (domain->dynamic_code_hash)
7204 res = g_hash_table_lookup (domain->dynamic_code_hash, method);
7217 * mono_allocate_stack_slots_full:
7219 * Allocate stack slots for all non register allocated variables using a
7220 * linear scan algorithm.
7221 * Returns: an array of stack offsets which the caller should free.
7222 * STACK_SIZE is set to the amount of stack space needed.
7223 * STACK_ALIGN is set to the alignment needed by the locals area.
7226 mono_allocate_stack_slots_full (MonoCompile *m, gboolean backward, guint32 *stack_size, guint32 *stack_align)
7228 int i, slot, offset, size, align;
7232 GList *vars = NULL, *l;
7233 StackSlotInfo *scalar_stack_slots, *vtype_stack_slots, *slot_info;
7237 scalar_stack_slots = g_new0 (StackSlotInfo, MONO_TYPE_PINNED);
7238 vtype_stack_slots = g_new0 (StackSlotInfo, 256);
7241 offsets = g_new (gint32, m->num_varinfo);
7242 for (i = 0; i < m->num_varinfo; ++i)
7245 for (i = m->locals_start; i < m->num_varinfo; i++) {
7246 inst = m->varinfo [i];
7247 vmv = MONO_VARINFO (m, i);
7249 if ((inst->flags & MONO_INST_IS_DEAD) || inst->opcode == OP_REGVAR || inst->opcode == OP_REGOFFSET)
7252 vars = g_list_prepend (vars, vmv);
7255 vars = mono_varlist_sort (m, vars, 0);
7258 for (l = vars; l; l = l->next) {
7260 inst = m->varinfo [vmv->idx];
7262 /* inst->unused indicates native sized value types, this is used by the
7263 * pinvoke wrappers when they call functions returning structures */
7264 if (inst->unused && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
7265 size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
7267 size = mono_type_size (inst->inst_vtype, &align);
7269 t = mono_type_get_underlying_type (inst->inst_vtype);
7271 case MONO_TYPE_VALUETYPE:
7272 for (i = 0; i < nvtypes; ++i)
7273 if (t->data.klass == vtype_stack_slots [i].vtype)
7276 slot_info = &vtype_stack_slots [i];
7278 g_assert (nvtypes < 256);
7279 vtype_stack_slots [nvtypes].vtype = t->data.klass;
7280 slot_info = &vtype_stack_slots [nvtypes];
7285 slot_info = &scalar_stack_slots [t->type];
7289 if (m->comp_done & MONO_COMP_LIVENESS) {
7290 //printf ("START %2d %08x %08x\n", vmv->idx, vmv->range.first_use.abs_pos, vmv->range.last_use.abs_pos);
7292 /* expire old intervals in active */
7293 while (slot_info->active) {
7294 MonoMethodVar *amv = (MonoMethodVar *)slot_info->active->data;
7296 if (amv->range.last_use.abs_pos > vmv->range.first_use.abs_pos)
7299 //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);
7301 slot_info->active = g_list_delete_link (slot_info->active, slot_info->active);
7302 slot_info->slots = g_list_prepend (slot_info->slots, GINT_TO_POINTER (offsets [amv->idx]));
7306 * This also handles the case when the variable is used in an
7307 * exception region, as liveness info is not computed there.
7310 * FIXME: All valuetypes are marked as INDIRECT because of LDADDR
7313 if (! (inst->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
7314 if (slot_info->slots) {
7315 slot = GPOINTER_TO_INT (slot_info->slots->data);
7317 slot_info->slots = g_list_delete_link (slot_info->slots, slot_info->slots);
7320 slot_info->active = mono_varlist_insert_sorted (m, slot_info->active, vmv, TRUE);
7325 static int count = 0;
7329 if (count == atoi (getenv ("COUNT")))
7330 printf ("LAST: %s\n", mono_method_full_name (m->method, TRUE));
7331 if (count > atoi (getenv ("COUNT")))
7334 mono_print_tree_nl (inst);
7338 if (slot == 0xffffff) {
7340 * Allways allocate valuetypes to sizeof (gpointer) to allow more
7341 * efficient copying (and to work around the fact that OP_MEMCPY
7342 * and OP_MEMSET ignores alignment).
7344 if (t->type == MONO_TYPE_VALUETYPE)
7345 align = sizeof (gpointer);
7349 offset += align - 1;
7350 offset &= ~(align - 1);
7354 offset += align - 1;
7355 offset &= ~(align - 1);
7360 if (*stack_align == 0)
7361 *stack_align = align;
7364 offsets [vmv->idx] = slot;
7367 for (i = 0; i < MONO_TYPE_PINNED; ++i) {
7368 g_list_free (scalar_stack_slots [i].active);
7369 g_list_free (scalar_stack_slots [i].slots);
7371 for (i = 0; i < nvtypes; ++i) {
7372 g_list_free (vtype_stack_slots [i].active);
7373 g_list_free (vtype_stack_slots [i].slots);
7375 g_free (scalar_stack_slots);
7376 g_free (vtype_stack_slots);
7378 *stack_size = offset;
7383 mono_allocate_stack_slots (MonoCompile *m, guint32 *stack_size, guint32 *stack_align)
7385 return mono_allocate_stack_slots_full (m, TRUE, stack_size, stack_align);
7389 mono_register_opcode_emulation (int opcode, const char *name, const char *sigstr, gpointer func, gboolean no_throw)
7391 MonoJitICallInfo *info;
7392 MonoMethodSignature *sig = mono_create_icall_signature (sigstr);
7394 if (!emul_opcode_map)
7395 emul_opcode_map = g_new0 (MonoJitICallInfo*, OP_LAST + 1);
7397 g_assert (!sig->hasthis);
7398 g_assert (sig->param_count < 3);
7400 info = mono_register_jit_icall (func, name, sig, no_throw);
7402 emul_opcode_map [opcode] = info;
7406 register_icall (gpointer func, const char *name, const char *sigstr, gboolean save)
7408 MonoMethodSignature *sig;
7411 sig = mono_create_icall_signature (sigstr);
7415 mono_register_jit_icall (func, name, sig, save);
7419 decompose_foreach (MonoInst *tree, gpointer data)
7421 static MonoJitICallInfo *newarr_info = NULL;
7422 static MonoJitICallInfo *newarr_specific_info = NULL;
7423 MonoJitICallInfo *info;
7426 switch (tree->opcode) {
7428 MonoCompile *cfg = data;
7429 MonoInst *iargs [3];
7432 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
7433 g_assert (newarr_info);
7434 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
7435 g_assert (newarr_specific_info);
7438 if (cfg->opt & MONO_OPT_SHARED) {
7439 NEW_DOMAINCONST (cfg, iargs [0]);
7440 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
7441 iargs [2] = tree->inst_newa_len;
7446 MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
7448 NEW_VTABLECONST (cfg, iargs [0], vtable);
7449 iargs [1] = tree->inst_newa_len;
7451 info = newarr_specific_info;
7454 mono_emulate_opcode (cfg, tree, iargs, info);
7456 /* Need to decompose arguments after the the opcode is decomposed */
7457 for (i = 0; i < info->sig->param_count; ++i)
7458 dec_foreach (iargs [i], cfg);
7468 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
7470 switch (mono_burg_arity [tree->opcode]) {
7473 mono_inst_foreach (tree->inst_left, func, data);
7476 mono_inst_foreach (tree->inst_left, func, data);
7477 mono_inst_foreach (tree->inst_right, func, data);
7480 g_assert_not_reached ();
7487 mono_print_bb_code (MonoBasicBlock *bb) {
7489 MonoInst *c = bb->code;
7491 mono_print_tree (c);
7499 print_dfn (MonoCompile *cfg) {
7504 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
7506 for (i = 0; i < cfg->num_bblocks; ++i) {
7507 bb = cfg->bblocks [i];
7508 /*if (bb->cil_code) {
7509 char* code1, *code2;
7510 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
7511 if (bb->last_ins->cil_code)
7512 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
7514 code2 = g_strdup ("");
7516 code1 [strlen (code1) - 1] = 0;
7517 code = g_strdup_printf ("%s -> %s", code1, code2);
7521 code = g_strdup ("\n");
7522 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
7524 MonoInst *c = bb->code;
7526 mono_print_tree (c);
7534 g_print ("\tprev:");
7535 for (j = 0; j < bb->in_count; ++j) {
7536 g_print (" BB%d", bb->in_bb [j]->block_num);
7538 g_print ("\t\tsucc:");
7539 for (j = 0; j < bb->out_count; ++j) {
7540 g_print (" BB%d", bb->out_bb [j]->block_num);
7542 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
7545 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
7548 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
7550 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
7558 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
7562 g_assert (bb->code);
7563 bb->last_ins->next = inst;
7564 bb->last_ins = inst;
7566 bb->last_ins = bb->code = inst;
7571 mono_destroy_compile (MonoCompile *cfg)
7573 //mono_mempool_stats (cfg->mempool);
7574 g_hash_table_destroy (cfg->bb_hash);
7575 mono_free_loop_info (cfg);
7577 mono_regstate_free (cfg->rs);
7579 g_hash_table_destroy (cfg->spvars);
7581 g_hash_table_destroy (cfg->exvars);
7582 mono_mempool_destroy (cfg->mempool);
7583 g_list_free (cfg->ldstr_list);
7585 g_free (cfg->varinfo);
7590 #ifdef HAVE_KW_THREAD
7591 static __thread gpointer mono_lmf_addr MONO_TLS_FAST;
7595 mono_get_jit_tls_key (void)
7597 return mono_jit_tls_id;
7601 mono_get_lmf_tls_offset (void)
7604 MONO_THREAD_VAR_OFFSET(mono_lmf_addr,offset);
7609 mono_get_lmf_addr (void)
7611 #ifdef HAVE_KW_THREAD
7612 return mono_lmf_addr;
7614 MonoJitTlsData *jit_tls;
7616 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
7617 return &jit_tls->lmf;
7619 g_assert_not_reached ();
7624 /* Called by native->managed wrappers */
7626 mono_jit_thread_attach (MonoDomain *domain)
7628 #ifdef HAVE_KW_THREAD
7629 if (!mono_lmf_addr) {
7630 mono_thread_attach (domain);
7633 if (!TlsGetValue (mono_jit_tls_id))
7634 mono_thread_attach (domain);
7639 * mono_thread_abort:
7640 * @obj: exception object
7642 * abort the thread, print exception information and stack trace
7645 mono_thread_abort (MonoObject *obj)
7647 /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
7649 /* handle_remove should be eventually called for this thread, too
7652 mono_thread_exit ();
7656 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
7658 MonoJitTlsData *jit_tls;
7661 jit_tls = TlsGetValue (mono_jit_tls_id);
7665 jit_tls = g_new0 (MonoJitTlsData, 1);
7667 TlsSetValue (mono_jit_tls_id, jit_tls);
7669 jit_tls->abort_func = abort_func;
7670 jit_tls->end_of_stack = stack_start;
7672 lmf = g_new0 (MonoLMF, 1);
7675 jit_tls->lmf = jit_tls->first_lmf = lmf;
7677 #ifdef HAVE_KW_THREAD
7678 mono_lmf_addr = &jit_tls->lmf;
7681 mono_arch_setup_jit_tls_data (jit_tls);
7683 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
7684 mono_setup_altstack (jit_tls);
7691 mono_thread_start_cb (gsize tid, gpointer stack_start, gpointer func)
7694 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
7695 thread = mono_thread_current ();
7697 thread->jit_data = jit_tls;
7700 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
7703 mono_thread_abort_dummy (MonoObject *obj)
7705 if (mono_thread_attach_aborted_cb)
7706 mono_thread_attach_aborted_cb (obj);
7708 mono_thread_abort (obj);
7712 mono_thread_attach_cb (gsize tid, gpointer stack_start)
7715 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
7716 thread = mono_thread_current ();
7718 thread->jit_data = jit_tls;
7719 if (mono_profiler_get_events () & MONO_PROFILE_STATISTICAL)
7720 setup_stat_profiler ();
7724 mini_thread_cleanup (MonoThread *thread)
7726 MonoJitTlsData *jit_tls = thread->jit_data;
7729 mono_arch_free_jit_tls_data (jit_tls);
7731 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
7732 mono_free_altstack (jit_tls);
7734 g_free (jit_tls->first_lmf);
7736 thread->jit_data = NULL;
7741 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
7743 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
7747 ji->data.target = target;
7748 ji->next = cfg->patch_info;
7750 cfg->patch_info = ji;
7754 mono_remove_patch_info (MonoCompile *cfg, int ip)
7756 MonoJumpInfo **ji = &cfg->patch_info;
7759 if ((*ji)->ip.i == ip)
7762 ji = &((*ji)->next);
7767 mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors)
7769 unsigned char *ip = patch_info->ip.i + code;
7770 gconstpointer target = NULL;
7772 switch (patch_info->type) {
7773 case MONO_PATCH_INFO_BB:
7774 target = patch_info->data.bb->native_offset + code;
7776 case MONO_PATCH_INFO_ABS:
7777 target = patch_info->data.target;
7779 case MONO_PATCH_INFO_LABEL:
7780 target = patch_info->data.inst->inst_c0 + code;
7782 case MONO_PATCH_INFO_IP:
7785 case MONO_PATCH_INFO_METHOD_REL:
7786 target = code + patch_info->data.offset;
7788 case MONO_PATCH_INFO_INTERNAL_METHOD: {
7789 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
7791 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
7792 g_assert_not_reached ();
7794 target = mono_icall_get_wrapper (mi);
7797 case MONO_PATCH_INFO_METHOD_JUMP: {
7800 /* get the trampoline to the method from the domain */
7801 target = mono_create_jump_trampoline (domain, patch_info->data.method, TRUE);
7802 if (!domain->jump_target_hash)
7803 domain->jump_target_hash = g_hash_table_new (NULL, NULL);
7804 list = g_hash_table_lookup (domain->jump_target_hash, patch_info->data.method);
7805 list = g_slist_prepend (list, ip);
7806 g_hash_table_insert (domain->jump_target_hash, patch_info->data.method, list);
7809 case MONO_PATCH_INFO_METHOD:
7810 if (patch_info->data.method == method) {
7813 /* get the trampoline to the method from the domain */
7814 target = mono_create_jit_trampoline (patch_info->data.method);
7816 case MONO_PATCH_INFO_SWITCH: {
7817 gpointer *jump_table;
7820 if (method && method->dynamic) {
7821 jump_table = mono_code_manager_reserve (mono_dynamic_code_hash_lookup (domain, method)->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
7823 mono_domain_lock (domain);
7824 jump_table = mono_code_manager_reserve (domain->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
7825 mono_domain_unlock (domain);
7828 for (i = 0; i < patch_info->data.table->table_size; i++) {
7829 jump_table [i] = code + GPOINTER_TO_INT (patch_info->data.table->table [i]);
7831 target = jump_table;
7834 case MONO_PATCH_INFO_METHODCONST:
7835 case MONO_PATCH_INFO_CLASS:
7836 case MONO_PATCH_INFO_IMAGE:
7837 case MONO_PATCH_INFO_FIELD:
7838 target = patch_info->data.target;
7840 case MONO_PATCH_INFO_IID:
7841 mono_class_init (patch_info->data.klass);
7842 target = GINT_TO_POINTER ((int)patch_info->data.klass->interface_id);
7844 case MONO_PATCH_INFO_VTABLE:
7845 target = mono_class_vtable (domain, patch_info->data.klass);
7847 case MONO_PATCH_INFO_CLASS_INIT:
7848 target = mono_create_class_init_trampoline (mono_class_vtable (domain, patch_info->data.klass));
7850 case MONO_PATCH_INFO_SFLDA: {
7851 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
7852 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && (method && mono_class_needs_cctor_run (vtable->klass, method)))
7853 /* Done by the generated code */
7857 mono_runtime_class_init (vtable);
7859 target = (char*)vtable->data + patch_info->data.field->offset;
7862 case MONO_PATCH_INFO_R4:
7863 case MONO_PATCH_INFO_R8:
7864 target = patch_info->data.target;
7866 case MONO_PATCH_INFO_EXC_NAME:
7867 target = patch_info->data.name;
7869 case MONO_PATCH_INFO_LDSTR:
7871 mono_ldstr (domain, patch_info->data.token->image,
7872 mono_metadata_token_index (patch_info->data.token->token));
7874 case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
7876 MonoClass *handle_class;
7878 handle = mono_ldtoken (patch_info->data.token->image,
7879 patch_info->data.token->token, &handle_class, NULL);
7880 mono_class_init (handle_class);
7881 mono_class_init (mono_class_from_mono_type (handle));
7884 mono_type_get_object (domain, handle);
7887 case MONO_PATCH_INFO_LDTOKEN: {
7889 MonoClass *handle_class;
7891 handle = mono_ldtoken (patch_info->data.token->image,
7892 patch_info->data.token->token, &handle_class, NULL);
7893 mono_class_init (handle_class);
7898 case MONO_PATCH_INFO_DECLSEC:
7899 target = (mono_metadata_blob_heap (patch_info->data.token->image, patch_info->data.token->token) + 2);
7901 case MONO_PATCH_INFO_BB_OVF:
7902 case MONO_PATCH_INFO_EXC_OVF:
7903 case MONO_PATCH_INFO_GOT_OFFSET:
7904 case MONO_PATCH_INFO_NONE:
7907 g_assert_not_reached ();
7910 return (gpointer)target;
7914 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
7915 MonoJitICallInfo *info;
7917 decompose_foreach (tree, cfg);
7919 switch (mono_burg_arity [tree->opcode]) {
7922 dec_foreach (tree->inst_left, cfg);
7924 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
7925 MonoInst *iargs [2];
7927 iargs [0] = tree->inst_left;
7929 mono_emulate_opcode (cfg, tree, iargs, info);
7935 #ifdef MONO_ARCH_BIGMUL_INTRINS
7936 if (tree->opcode == OP_LMUL
7937 && (cfg->opt & MONO_OPT_INTRINS)
7938 && (tree->inst_left->opcode == CEE_CONV_I8
7939 || tree->inst_left->opcode == CEE_CONV_U8)
7940 && tree->inst_left->inst_left->type == STACK_I4
7941 && (tree->inst_right->opcode == CEE_CONV_I8
7942 || tree->inst_right->opcode == CEE_CONV_U8)
7943 && tree->inst_right->inst_left->type == STACK_I4
7944 && tree->inst_left->opcode == tree->inst_right->opcode) {
7945 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
7946 tree->inst_left = tree->inst_left->inst_left;
7947 tree->inst_right = tree->inst_right->inst_left;
7948 dec_foreach (tree, cfg);
7951 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
7952 MonoInst *iargs [2];
7954 iargs [0] = tree->inst_i0;
7955 iargs [1] = tree->inst_i1;
7957 mono_emulate_opcode (cfg, tree, iargs, info);
7959 dec_foreach (iargs [0], cfg);
7960 dec_foreach (iargs [1], cfg);
7963 dec_foreach (tree->inst_left, cfg);
7964 dec_foreach (tree->inst_right, cfg);
7968 g_assert_not_reached ();
7973 decompose_pass (MonoCompile *cfg) {
7976 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7979 cfg->prev_ins = NULL;
7980 for (tree = cfg->cbb->code; tree; tree = tree->next) {
7981 dec_foreach (tree, cfg);
7982 cfg->prev_ins = tree;
7988 nullify_basic_block (MonoBasicBlock *bb)
7995 bb->code = bb->last_ins = NULL;
7996 bb->cil_code = NULL;
8000 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
8004 for (i = 0; i < bb->out_count; i++) {
8005 MonoBasicBlock *ob = bb->out_bb [i];
8008 if (bb->out_count > 1) {
8009 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
8013 bb->out_bb [i] = repl;
8020 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
8024 for (i = 0; i < bb->in_count; i++) {
8025 MonoBasicBlock *ib = bb->in_bb [i];
8028 if (bb->in_count > 1) {
8029 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
8033 bb->in_bb [i] = repl;
8040 replace_or_add_in_block (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
8042 gboolean found = FALSE;
8045 for (i = 0; i < bb->in_count; i++) {
8046 MonoBasicBlock *ib = bb->in_bb [i];
8049 if (bb->in_count > 1) {
8050 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
8054 bb->in_bb [i] = repl;
8061 MonoBasicBlock **new_in_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (bb->in_count + 1));
8062 for (i = 0; i < bb->in_count; i++) {
8063 new_in_bb [i] = bb->in_bb [i];
8065 new_in_bb [i] = repl;
8067 bb->in_bb = new_in_bb;
8073 replace_out_block_in_code (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl) {
8076 for (inst = bb->code; inst != NULL; inst = inst->next) {
8077 if (inst->opcode == OP_CALL_HANDLER) {
8078 if (inst->inst_target_bb == orig) {
8079 inst->inst_target_bb = repl;
8083 if (bb->last_ins != NULL) {
8084 switch (bb->last_ins->opcode) {
8086 if (bb->last_ins->inst_target_bb == orig) {
8087 bb->last_ins->inst_target_bb = repl;
8092 int n = GPOINTER_TO_INT (bb->last_ins->klass);
8093 for (i = 0; i < n; i++ ) {
8094 if (bb->last_ins->inst_many_bb [i] == orig) {
8095 bb->last_ins->inst_many_bb [i] = repl;
8110 if (bb->last_ins->inst_true_bb == orig) {
8111 bb->last_ins->inst_true_bb = repl;
8113 if (bb->last_ins->inst_false_bb == orig) {
8114 bb->last_ins->inst_false_bb = repl;
8124 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
8128 for (i = 0; i < bb->out_count; i++) {
8129 MonoBasicBlock *ob = bb->out_bb [i];
8130 for (j = 0; j < ob->in_count; j++) {
8131 if (ob->in_bb [j] == orig) {
8132 ob->in_bb [j] = repl;
8140 * Check if a bb is useless (is just made of NOPs and ends with an
8141 * unconditional branch, or nothing).
8142 * If it is so, unlink it from the CFG and nullify it, and return TRUE.
8143 * Otherwise, return FALSE;
8146 remove_block_if_useless (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *previous_bb) {
8147 MonoBasicBlock *target_bb = NULL;
8150 /* Do not touch handlers */
8151 if (bb->region != -1) return FALSE;
8153 for (inst = bb->code; inst != NULL; inst = inst->next) {
8154 switch (inst->opcode) {
8158 target_bb = inst->inst_target_bb;
8165 if (target_bb == NULL) {
8166 if ((bb->out_count == 1) && (bb->out_bb [0] == bb->next_bb)) {
8167 target_bb = bb->next_bb;
8169 /* Do not touch empty BBs that do not "fall through" to their next BB (like the exit BB) */
8174 /* Do not touch BBs following a switch (they are the "default" branch) */
8175 if ((previous_bb->last_ins != NULL) && (previous_bb->last_ins->opcode == CEE_SWITCH)) {
8179 /* Do not touch BBs following the entry BB and jumping to something that is not */
8180 /* thiry "next" bb (the entry BB cannot contain the branch) */
8181 if ((previous_bb == cfg->bb_entry) && (bb->next_bb != target_bb)) {
8185 /* Check that there is a target BB, and that bb is not an empty loop (Bug 75061) */
8186 if ((target_bb != NULL) && (target_bb != bb)) {
8189 if (cfg->verbose_level > 1) {
8190 printf ("remove_block_if_useless %s, removed BB%d\n", mono_method_full_name (cfg->method, TRUE), bb->block_num);
8193 for (i = 0; i < bb->in_count; i++) {
8194 MonoBasicBlock *in_bb = bb->in_bb [i];
8195 replace_out_block (in_bb, bb, target_bb);
8196 replace_out_block_in_code (in_bb, bb, target_bb);
8197 if (bb->in_count == 1) {
8198 replace_in_block (target_bb, bb, in_bb);
8200 replace_or_add_in_block (cfg, target_bb, bb, in_bb);
8204 if ((previous_bb != cfg->bb_entry) &&
8205 (previous_bb->region == bb->region) &&
8206 ((previous_bb->last_ins == NULL) ||
8207 ((previous_bb->last_ins->opcode != CEE_BR) &&
8208 (! (MONO_IS_COND_BRANCH_OP (previous_bb->last_ins))) &&
8209 (previous_bb->last_ins->opcode != CEE_SWITCH)))) {
8210 for (i = 0; i < previous_bb->out_count; i++) {
8211 if (previous_bb->out_bb [i] == target_bb) {
8213 MONO_INST_NEW (cfg, jump, CEE_BR);
8214 MONO_ADD_INS (previous_bb, jump);
8215 jump->cil_code = previous_bb->cil_code;
8216 jump->inst_target_bb = target_bb;
8222 previous_bb->next_bb = bb->next_bb;
8223 nullify_basic_block (bb);
8232 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
8234 bb->out_count = bbn->out_count;
8235 bb->out_bb = bbn->out_bb;
8237 replace_basic_block (bb, bbn, bb);
8241 bb->last_ins->next = bbn->code;
8242 bb->last_ins = bbn->last_ins;
8245 bb->code = bbn->code;
8246 bb->last_ins = bbn->last_ins;
8248 bb->next_bb = bbn->next_bb;
8249 nullify_basic_block (bbn);
8253 move_basic_block_to_end (MonoCompile *cfg, MonoBasicBlock *bb)
8255 MonoBasicBlock *bbn, *next;
8259 /* Find the previous */
8260 for (bbn = cfg->bb_entry; bbn->next_bb && bbn->next_bb != bb; bbn = bbn->next_bb)
8263 bbn->next_bb = bb->next_bb;
8267 for (bbn = cfg->bb_entry; bbn->next_bb; bbn = bbn->next_bb)
8273 if (next && (!bb->last_ins || (bb->last_ins->opcode != OP_NOT_REACHED))) {
8276 MONO_INST_NEW (cfg, ins, CEE_BR);
8277 MONO_ADD_INS (bb, ins);
8278 link_bblock (cfg, bb, next);
8279 ins->inst_target_bb = next;
8284 * Optimizes the branches on the Control Flow Graph
8288 optimize_branches (MonoCompile *cfg)
8290 int i, changed = FALSE;
8291 MonoBasicBlock *bb, *bbn;
8292 guint32 niterations;
8295 * Some crazy loops could cause the code below to go into an infinite
8296 * loop, see bug #53003 for an example. To prevent this, we put an upper
8297 * bound on the number of iterations.
8299 if (cfg->num_bblocks > 1000)
8300 niterations = cfg->num_bblocks * 2;
8304 MonoBasicBlock *previous_bb;
8308 /* we skip the entry block (exit is handled specially instead ) */
8309 for (previous_bb = cfg->bb_entry, bb = cfg->bb_entry->next_bb; bb; previous_bb = bb, bb = bb->next_bb) {
8311 /* dont touch code inside exception clauses */
8312 if (bb->region != -1)
8315 if (remove_block_if_useless (cfg, bb, previous_bb)) {
8320 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
8321 if (cfg->verbose_level > 2)
8322 g_print ("nullify block triggered %d\n", bbn->block_num);
8324 bb->next_bb = bbn->next_bb;
8326 for (i = 0; i < bbn->out_count; i++)
8327 replace_in_block (bbn->out_bb [i], bbn, NULL);
8329 nullify_basic_block (bbn);
8333 if (bb->out_count == 1) {
8334 bbn = bb->out_bb [0];
8336 /* conditional branches where true and false targets are the same can be also replaced with CEE_BR */
8337 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins)) {
8339 MONO_INST_NEW (cfg, pop, CEE_POP);
8340 pop->inst_left = bb->last_ins->inst_left->inst_left;
8341 mono_add_ins_to_end (bb, pop);
8342 MONO_INST_NEW (cfg, pop, CEE_POP);
8343 pop->inst_left = bb->last_ins->inst_left->inst_right;
8344 mono_add_ins_to_end (bb, pop);
8345 bb->last_ins->opcode = CEE_BR;
8346 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
8348 if (cfg->verbose_level > 2)
8349 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
8352 if (bb->region == bbn->region && bb->next_bb == bbn) {
8353 /* the block are in sequence anyway ... */
8355 /* branches to the following block can be removed */
8356 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
8357 bb->last_ins->opcode = CEE_NOP;
8359 if (cfg->verbose_level > 2)
8360 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
8363 if (bbn->in_count == 1) {
8365 if (bbn != cfg->bb_exit) {
8366 if (cfg->verbose_level > 2)
8367 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
8368 merge_basic_blocks (bb, bbn);
8372 //mono_print_bb_code (bb);
8377 } while (changed && (niterations > 0));
8384 /* we skip the entry block (exit is handled specially instead ) */
8385 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
8387 /* dont touch code inside exception clauses */
8388 if (bb->region != -1)
8391 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
8392 if (cfg->verbose_level > 2) {
8393 g_print ("nullify block triggered %d\n", bbn->block_num);
8395 bb->next_bb = bbn->next_bb;
8397 for (i = 0; i < bbn->out_count; i++)
8398 replace_in_block (bbn->out_bb [i], bbn, NULL);
8400 nullify_basic_block (bbn);
8406 if (bb->out_count == 1) {
8407 bbn = bb->out_bb [0];
8409 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
8410 bbn = bb->last_ins->inst_target_bb;
8411 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
8412 bbn->code->inst_target_bb->region == bb->region) {
8414 if (cfg->verbose_level > 2)
8415 g_print ("in %s branch to branch triggered %d -> %d -> %d\n", cfg->method->name,
8416 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num);
8418 replace_in_block (bbn, bb, NULL);
8419 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
8420 link_bblock (cfg, bb, bbn->code->inst_target_bb);
8421 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
8426 } else if (bb->out_count == 2) {
8427 if (bb->last_ins && MONO_IS_COND_BRANCH_NOFP (bb->last_ins)) {
8428 int branch_result = mono_eval_cond_branch (bb->last_ins);
8429 MonoBasicBlock *taken_branch_target = NULL, *untaken_branch_target = NULL;
8430 if (branch_result == BRANCH_TAKEN) {
8431 taken_branch_target = bb->last_ins->inst_true_bb;
8432 untaken_branch_target = bb->last_ins->inst_false_bb;
8433 } else if (branch_result == BRANCH_NOT_TAKEN) {
8434 taken_branch_target = bb->last_ins->inst_false_bb;
8435 untaken_branch_target = bb->last_ins->inst_true_bb;
8437 if (taken_branch_target) {
8438 /* if mono_eval_cond_branch () is ever taken to handle
8439 * non-constant values to compare, issue a pop here.
8441 bb->last_ins->opcode = CEE_BR;
8442 bb->last_ins->inst_target_bb = taken_branch_target;
8443 replace_out_block (bb, untaken_branch_target, NULL);
8444 replace_in_block (untaken_branch_target, bb, NULL);
8448 bbn = bb->last_ins->inst_true_bb;
8449 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
8450 bbn->code->inst_target_bb->region == bb->region) {
8451 if (cfg->verbose_level > 2)
8452 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
8453 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
8456 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
8458 replace_in_block (bbn, bb, NULL);
8460 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
8461 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
8463 link_bblock (cfg, bb, bbn->code->inst_target_bb);
8469 bbn = bb->last_ins->inst_false_bb;
8470 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
8471 bbn->code->inst_target_bb->region == bb->region) {
8472 if (cfg->verbose_level > 2)
8473 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
8474 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
8477 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
8479 replace_in_block (bbn, bb, NULL);
8481 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
8482 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
8484 link_bblock (cfg, bb, bbn->code->inst_target_bb);
8491 if (bb->last_ins && MONO_IS_COND_BRANCH_NOFP (bb->last_ins)) {
8492 if (bb->last_ins->inst_false_bb->out_of_line && (bb->region == bb->last_ins->inst_false_bb->region)) {
8493 /* Reverse the branch */
8494 bb->last_ins->opcode = reverse_branch_op (bb->last_ins->opcode);
8495 bbn = bb->last_ins->inst_false_bb;
8496 bb->last_ins->inst_false_bb = bb->last_ins->inst_true_bb;
8497 bb->last_ins->inst_true_bb = bbn;
8499 move_basic_block_to_end (cfg, bb->last_ins->inst_true_bb);
8500 if (cfg->verbose_level > 2)
8501 g_print ("cbranch to throw block triggered %d.\n",
8507 } while (changed && (niterations > 0));
8512 mono_compile_create_vars (MonoCompile *cfg)
8514 MonoMethodSignature *sig;
8515 MonoMethodHeader *header;
8518 header = mono_method_get_header (cfg->method);
8520 sig = mono_method_signature (cfg->method);
8522 if (!MONO_TYPE_IS_VOID (sig->ret)) {
8523 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
8524 cfg->ret->opcode = OP_RETARG;
8525 cfg->ret->inst_vtype = sig->ret;
8526 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
8528 if (cfg->verbose_level > 2)
8529 g_print ("creating vars\n");
8532 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
8534 for (i = 0; i < sig->param_count; ++i) {
8535 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
8536 if (sig->params [i]->byref) {
8537 cfg->disable_ssa = TRUE;
8541 cfg->locals_start = cfg->num_varinfo;
8543 if (cfg->verbose_level > 2)
8544 g_print ("creating locals\n");
8545 for (i = 0; i < header->num_locals; ++i)
8546 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
8547 if (cfg->verbose_level > 2)
8548 g_print ("locals done\n");
8550 #ifdef MONO_ARCH_HAVE_CREATE_VARS
8551 mono_arch_create_vars (cfg);
8556 mono_print_code (MonoCompile *cfg)
8560 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
8561 MonoInst *tree = bb->code;
8566 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
8568 for (; tree; tree = tree->next) {
8569 mono_print_tree (tree);
8574 bb->last_ins->next = NULL;
8578 extern const char * const mono_burg_rule_string [];
8581 emit_state (MonoCompile *cfg, MBState *state, int goal)
8584 int ern = mono_burg_rule (state, goal);
8585 const guint16 *nts = mono_burg_nts [ern];
8588 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
8592 // state->reg1 = state->reg2; /* chain rule */
8594 #ifdef MONO_ARCH_ENABLE_EMIT_STATE_OPT
8597 state->reg1 = mono_regstate_next_int (cfg->rs);
8598 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
8601 state->reg1 = mono_regstate_next_int (cfg->rs);
8602 state->reg2 = mono_regstate_next_int (cfg->rs);
8605 state->reg1 = mono_regstate_next_float (cfg->rs);
8608 #ifdef MONO_ARCH_ENABLE_EMIT_STATE_OPT
8610 * Enabling this might cause bugs to surface in the local register
8611 * allocators on some architectures like x86.
8613 if ((state->tree->ssa_op == MONO_SSA_STORE) && (state->left->tree->opcode == OP_REGVAR)) {
8614 /* Do not optimize away reg-reg moves */
8615 if (! ((state->right->tree->ssa_op == MONO_SSA_LOAD) && (state->right->left->tree->opcode == OP_REGVAR))) {
8616 state->right->reg1 = state->left->tree->dreg;
8625 mono_burg_kids (state, ern, kids);
8627 emit_state (cfg, kids [0], nts [0]);
8629 emit_state (cfg, kids [1], nts [1]);
8631 g_assert (!nts [3]);
8632 emit_state (cfg, kids [2], nts [2]);
8637 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
8638 if ((emit = mono_burg_func [ern]))
8639 emit (state, state->tree, cfg);
8642 #define DEBUG_SELECTION
8645 mini_select_instructions (MonoCompile *cfg)
8649 cfg->state_pool = mono_mempool_new ();
8650 cfg->rs = mono_regstate_new ();
8652 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
8653 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins) &&
8654 bb->next_bb != bb->last_ins->inst_false_bb) {
8656 /* we are careful when inverting, since bugs like #59580
8657 * could show up when dealing with NaNs.
8659 if (MONO_IS_COND_BRANCH_NOFP(bb->last_ins) && bb->next_bb == bb->last_ins->inst_true_bb) {
8660 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
8661 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
8662 bb->last_ins->inst_false_bb = tmp;
8664 bb->last_ins->opcode = reverse_branch_op (bb->last_ins->opcode);
8666 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
8667 inst->opcode = CEE_BR;
8668 inst->inst_target_bb = bb->last_ins->inst_false_bb;
8669 mono_bblock_add_inst (bb, inst);
8674 #ifdef DEBUG_SELECTION
8675 if (cfg->verbose_level >= 4) {
8676 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
8677 MonoInst *tree = bb->code;
8678 g_print ("DUMP BLOCK %d:\n", bb->block_num);
8681 for (; tree; tree = tree->next) {
8682 mono_print_tree (tree);
8689 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
8690 MonoInst *tree = bb->code, *next;
8696 bb->last_ins = NULL;
8699 mono_regstate_reset (cfg->rs);
8701 #ifdef DEBUG_SELECTION
8702 if (cfg->verbose_level >= 3)
8703 g_print ("LABEL BLOCK %d:\n", bb->block_num);
8705 for (; tree; tree = next) {
8707 #ifdef DEBUG_SELECTION
8708 if (cfg->verbose_level >= 3) {
8709 mono_print_tree (tree);
8714 if (!(mbstate = mono_burg_label (tree, cfg))) {
8715 g_warning ("unable to label tree %p", tree);
8716 mono_print_tree (tree);
8718 g_assert_not_reached ();
8720 emit_state (cfg, mbstate, MB_NTERM_stmt);
8722 bb->max_ireg = cfg->rs->next_vireg;
8723 bb->max_freg = cfg->rs->next_vfreg;
8726 bb->last_ins->next = NULL;
8728 mono_mempool_empty (cfg->state_pool);
8730 mono_mempool_destroy (cfg->state_pool);
8734 mono_codegen (MonoCompile *cfg)
8736 MonoJumpInfo *patch_info;
8738 int i, max_epilog_size;
8741 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
8742 cfg->spill_count = 0;
8743 /* we reuse dfn here */
8744 /* bb->dfn = bb_count++; */
8745 mono_arch_local_regalloc (cfg, bb);
8748 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
8749 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
8751 code = mono_arch_emit_prolog (cfg);
8753 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
8754 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
8756 cfg->code_len = code - cfg->native_code;
8757 cfg->prolog_end = cfg->code_len;
8759 mono_debug_open_method (cfg);
8761 /* emit code all basic blocks */
8762 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
8763 bb->native_offset = cfg->code_len;
8764 mono_arch_output_basic_block (cfg, bb);
8766 if (bb == cfg->bb_exit) {
8767 cfg->epilog_begin = cfg->code_len;
8769 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE) {
8770 code = cfg->native_code + cfg->code_len;
8771 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
8772 cfg->code_len = code - cfg->native_code;
8775 mono_arch_emit_epilog (cfg);
8779 mono_arch_emit_exceptions (cfg);
8781 max_epilog_size = 0;
8783 code = cfg->native_code + cfg->code_len;
8785 /* we always allocate code in cfg->domain->code_mp to increase locality */
8786 cfg->code_size = cfg->code_len + max_epilog_size;
8787 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
8789 if (cfg->method->dynamic) {
8790 /* Allocate the code into a separate memory pool so it can be freed */
8791 cfg->dynamic_info = g_new0 (MonoJitDynamicMethodInfo, 1);
8792 cfg->dynamic_info->code_mp = mono_code_manager_new_dynamic ();
8793 mono_domain_lock (cfg->domain);
8794 mono_dynamic_code_hash_insert (cfg->domain, cfg->method, cfg->dynamic_info);
8795 mono_domain_unlock (cfg->domain);
8797 code = mono_code_manager_reserve (cfg->dynamic_info->code_mp, cfg->code_size);
8799 mono_domain_lock (cfg->domain);
8800 code = mono_code_manager_reserve (cfg->domain->code_mp, cfg->code_size);
8801 mono_domain_unlock (cfg->domain);
8804 memcpy (code, cfg->native_code, cfg->code_len);
8805 g_free (cfg->native_code);
8806 cfg->native_code = code;
8807 code = cfg->native_code + cfg->code_len;
8809 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
8810 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
8811 switch (patch_info->type) {
8812 case MONO_PATCH_INFO_ABS: {
8813 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
8815 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
8816 if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) &&
8817 strstr (cfg->method->name, info->name))
8819 * This is an icall wrapper, and this is a call to the
8824 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
8825 patch_info->data.name = info->name;
8829 MonoVTable *vtable = mono_find_class_init_trampoline_by_addr (patch_info->data.target);
8831 patch_info->type = MONO_PATCH_INFO_CLASS_INIT;
8832 patch_info->data.klass = vtable->klass;
8837 case MONO_PATCH_INFO_SWITCH: {
8839 if (cfg->method->dynamic) {
8840 table = mono_code_manager_reserve (cfg->dynamic_info->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
8842 mono_domain_lock (cfg->domain);
8843 table = mono_code_manager_reserve (cfg->domain->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
8844 mono_domain_unlock (cfg->domain);
8847 if (!cfg->compile_aot)
8848 /* In the aot case, the patch already points to the correct location */
8849 patch_info->ip.i = patch_info->ip.label->inst_c0;
8850 for (i = 0; i < patch_info->data.table->table_size; i++) {
8851 table [i] = GINT_TO_POINTER (patch_info->data.table->table [i]->native_offset);
8853 patch_info->data.table->table = (MonoBasicBlock**)table;
8862 if (cfg->verbose_level > 0) {
8863 char* nm = mono_method_full_name (cfg->method, TRUE);
8864 g_print ("Method %s emitted at %p to %p [%s]\n",
8866 cfg->native_code, cfg->native_code + cfg->code_len, cfg->domain->friendly_name);
8870 #ifdef MONO_ARCH_HAVE_SAVE_UNWIND_INFO
8871 mono_arch_save_unwind_info (cfg);
8874 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info, cfg->run_cctors);
8876 if (cfg->method->dynamic) {
8877 mono_code_manager_commit (cfg->dynamic_info->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
8879 mono_domain_lock (cfg->domain);
8880 mono_code_manager_commit (cfg->domain->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
8881 mono_domain_unlock (cfg->domain);
8884 mono_arch_flush_icache (cfg->native_code, cfg->code_len);
8886 mono_debug_close_method (cfg);
8890 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
8895 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
8896 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
8898 if (cp->opcode == OP_ICONST) {
8899 if (cfg->opt & MONO_OPT_CONSPROP) {
8900 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
8904 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
8905 if (cfg->opt & MONO_OPT_COPYPROP) {
8906 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
8912 arity = mono_burg_arity [tree->opcode];
8915 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
8916 if (cfg->opt & MONO_OPT_CFOLD)
8917 mono_constant_fold_inst (tree, NULL);
8918 /* The opcode may have changed */
8919 if (mono_burg_arity [tree->opcode] > 1) {
8920 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
8921 if (cfg->opt & MONO_OPT_CFOLD)
8922 mono_constant_fold_inst (tree, NULL);
8924 mono_constant_fold_inst (tree, NULL);
8930 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
8934 switch (tree->opcode) {
8944 if ((tree->ssa_op == MONO_SSA_NOP) || (tree->ssa_op & MONO_SSA_ADDRESS_TAKEN)) {
8945 memset (acp, 0, sizeof (MonoInst *) * acp_size);
8962 case OP_VOIDCALL_REG:
8963 case OP_VOIDCALLVIRT:
8965 MonoCallInst *call = (MonoCallInst *)tree;
8966 MonoMethodSignature *sig = call->signature;
8967 int i, byref = FALSE;
8969 for (i = 0; i < sig->param_count; i++) {
8970 if (sig->params [i]->byref) {
8977 memset (acp, 0, sizeof (MonoInst *) * acp_size);
8985 arity = mono_burg_arity [tree->opcode];
8991 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
8994 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
8995 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
8998 g_assert_not_reached ();
9003 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
9005 MonoInst *tree = bb->code;
9011 for (; tree; tree = tree->next) {
9013 mono_cprop_copy_values (cfg, tree, acp);
9015 mono_cprop_invalidate_values (tree, acp, acp_size);
9017 if (tree->ssa_op == MONO_SSA_STORE &&
9018 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
9019 MonoInst *i1 = tree->inst_i1;
9021 acp [tree->inst_i0->inst_c0] = NULL;
9023 for (i = 0; i < acp_size; i++) {
9024 if (acp [i] && acp [i]->opcode != OP_ICONST &&
9025 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
9030 if (i1->opcode == OP_ICONST) {
9031 acp [tree->inst_i0->inst_c0] = i1;
9032 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
9034 if (i1->ssa_op == MONO_SSA_LOAD &&
9035 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
9036 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
9037 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
9038 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
9043 if (tree->opcode == CEE_BEQ) {
9044 g_assert (tree->inst_i0->opcode == OP_COMPARE);
9045 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
9046 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
9048 tree->opcode = CEE_BR;
9049 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
9050 tree->inst_target_bb = tree->inst_true_bb;
9052 tree->inst_target_bb = tree->inst_false_bb;
9061 mono_local_cprop (MonoCompile *cfg)
9066 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
9068 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
9069 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
9070 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
9075 remove_critical_edges (MonoCompile *cfg) {
9077 MonoBasicBlock *previous_bb;
9079 if (cfg->verbose_level > 3) {
9080 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
9082 printf ("remove_critical_edges %s, BEFORE BB%d (in:", mono_method_full_name (cfg->method, TRUE), bb->block_num);
9083 for (i = 0; i < bb->in_count; i++) {
9084 printf (" %d", bb->in_bb [i]->block_num);
9087 for (i = 0; i < bb->out_count; i++) {
9088 printf (" %d", bb->out_bb [i]->block_num);
9091 if (bb->last_ins != NULL) {
9093 mono_print_tree (bb->last_ins);
9099 for (previous_bb = cfg->bb_entry, bb = previous_bb->next_bb; bb != NULL; previous_bb = previous_bb->next_bb, bb = bb->next_bb) {
9100 if (bb->in_count > 1) {
9102 for (in_bb_index = 0; in_bb_index < bb->in_count; in_bb_index++) {
9103 MonoBasicBlock *in_bb = bb->in_bb [in_bb_index];
9104 if (in_bb->out_count > 1) {
9105 MonoBasicBlock *new_bb = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock));
9106 new_bb->block_num = cfg->num_bblocks++;
9107 // new_bb->real_offset = bb->real_offset;
9108 new_bb->region = bb->region;
9110 /* Do not alter the CFG while altering the BB list */
9111 if (previous_bb->region == bb->region) {
9112 if (previous_bb != cfg->bb_entry) {
9113 /* If previous_bb "followed through" to bb, */
9114 /* keep it linked with a CEE_BR */
9115 if ((previous_bb->last_ins == NULL) ||
9116 ((previous_bb->last_ins->opcode != CEE_BR) &&
9117 (! (MONO_IS_COND_BRANCH_OP (previous_bb->last_ins))) &&
9118 (previous_bb->last_ins->opcode != CEE_SWITCH))) {
9120 /* Make sure previous_bb really falls through bb */
9121 for (i = 0; i < previous_bb->out_count; i++) {
9122 if (previous_bb->out_bb [i] == bb) {
9124 MONO_INST_NEW (cfg, jump, CEE_BR);
9125 MONO_ADD_INS (previous_bb, jump);
9126 jump->cil_code = previous_bb->cil_code;
9127 jump->inst_target_bb = bb;
9133 /* We cannot add any inst to the entry BB, so we must */
9134 /* put a new BB in the middle to hold the CEE_BR */
9136 MonoBasicBlock *new_bb_after_entry = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock));
9137 new_bb_after_entry->block_num = cfg->num_bblocks++;
9138 // new_bb_after_entry->real_offset = bb->real_offset;
9139 new_bb_after_entry->region = bb->region;
9141 MONO_INST_NEW (cfg, jump, CEE_BR);
9142 MONO_ADD_INS (new_bb_after_entry, jump);
9143 jump->cil_code = bb->cil_code;
9144 jump->inst_target_bb = bb;
9146 previous_bb->next_bb = new_bb_after_entry;
9147 previous_bb = new_bb_after_entry;
9149 if (cfg->verbose_level > 2) {
9150 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);
9155 /* Insert new_bb in the BB list */
9156 previous_bb->next_bb = new_bb;
9157 new_bb->next_bb = bb;
9158 previous_bb = new_bb;
9160 /* Setup in_bb and out_bb */
9161 new_bb->in_bb = mono_mempool_alloc ((cfg)->mempool, sizeof (MonoBasicBlock*));
9162 new_bb->in_bb [0] = in_bb;
9163 new_bb->in_count = 1;
9164 new_bb->out_bb = mono_mempool_alloc ((cfg)->mempool, sizeof (MonoBasicBlock*));
9165 new_bb->out_bb [0] = bb;
9166 new_bb->out_count = 1;
9168 /* Relink in_bb and bb to (from) new_bb */
9169 replace_out_block (in_bb, bb, new_bb);
9170 replace_out_block_in_code (in_bb, bb, new_bb);
9171 replace_in_block (bb, in_bb, new_bb);
9173 if (cfg->verbose_level > 2) {
9174 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);
9181 if (cfg->verbose_level > 3) {
9182 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
9184 printf ("remove_critical_edges %s, AFTER BB%d (in:", mono_method_full_name (cfg->method, TRUE), bb->block_num);
9185 for (i = 0; i < bb->in_count; i++) {
9186 printf (" %d", bb->in_bb [i]->block_num);
9189 for (i = 0; i < bb->out_count; i++) {
9190 printf (" %d", bb->out_bb [i]->block_num);
9193 if (bb->last_ins != NULL) {
9195 mono_print_tree (bb->last_ins);
9203 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, gboolean compile_aot, int parts)
9205 MonoMethodHeader *header = mono_method_get_header (method);
9209 int dfn = 0, i, code_size_ratio;
9210 gboolean deadce_has_run = FALSE;
9215 ip = (guint8 *)header->code;
9217 mono_jit_stats.methods_compiled++;
9218 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
9219 mono_profiler_method_jit (method);
9221 cfg = g_new0 (MonoCompile, 1);
9222 cfg->method = method;
9223 cfg->mempool = mono_mempool_new ();
9225 cfg->prof_options = mono_profiler_get_events ();
9226 cfg->run_cctors = run_cctors;
9227 cfg->bb_hash = g_hash_table_new (NULL, NULL);
9228 cfg->domain = domain;
9229 cfg->verbose_level = mini_verbose;
9230 cfg->compile_aot = compile_aot;
9231 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
9232 mono_method_get_header (method)->max_stack);
9233 cfg->aliasing_info = NULL;
9235 if (cfg->verbose_level > 2)
9236 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
9239 * create MonoInst* which represents arguments and local variables
9241 mono_compile_create_vars (cfg);
9243 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
9244 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
9245 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
9246 mono_destroy_compile (cfg);
9250 mono_jit_stats.basic_blocks += cfg->num_bblocks;
9251 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
9253 if ((cfg->num_varinfo > 2000) && !mono_compile_aot) {
9255 * we disable some optimizations if there are too many variables
9256 * because JIT time may become too expensive. The actual number needs
9257 * to be tweaked and eventually the non-linear algorithms should be fixed.
9259 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
9260 cfg->disable_ssa = TRUE;
9263 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
9265 if (cfg->opt & MONO_OPT_BRANCH)
9266 optimize_branches (cfg);
9268 if (cfg->opt & MONO_OPT_SSAPRE) {
9269 remove_critical_edges (cfg);
9272 /* Depth-first ordering on basic blocks */
9273 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
9275 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
9276 if (cfg->num_bblocks != dfn + 1) {
9279 cfg->num_bblocks = dfn + 1;
9281 if (!header->clauses) {
9282 /* remove unreachable code, because the code in them may be
9283 * inconsistent (access to dead variables for example) */
9284 for (bb = cfg->bb_entry; bb;) {
9285 MonoBasicBlock *bbn = bb->next_bb;
9287 if (bbn && bbn->region == -1 && !bbn->dfn) {
9288 if (cfg->verbose_level > 1)
9289 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
9290 bb->next_bb = bbn->next_bb;
9291 nullify_basic_block (bbn);
9299 if (cfg->opt & MONO_OPT_LOOP) {
9300 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
9301 mono_compute_natural_loops (cfg);
9304 /* after method_to_ir */
9308 //#define DEBUGSSA "logic_run"
9309 #define DEBUGSSA_CLASS "Tests"
9312 if (!header->num_clauses && !cfg->disable_ssa) {
9313 mono_local_cprop (cfg);
9314 mono_ssa_compute (cfg);
9318 /* fixme: add all optimizations which requires SSA */
9319 if (cfg->opt & (MONO_OPT_SSA | MONO_OPT_ABCREM | MONO_OPT_SSAPRE)) {
9320 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
9321 mono_local_cprop (cfg);
9322 mono_ssa_compute (cfg);
9324 if (cfg->verbose_level >= 2) {
9331 /* after SSA translation */
9335 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
9336 if (cfg->comp_done & MONO_COMP_SSA) {
9337 mono_ssa_cprop (cfg);
9339 mono_local_cprop (cfg);
9343 if (cfg->comp_done & MONO_COMP_SSA) {
9344 //mono_ssa_deadce (cfg);
9346 //mono_ssa_strength_reduction (cfg);
9348 if (cfg->opt & MONO_OPT_SSAPRE) {
9349 mono_perform_ssapre (cfg);
9350 //mono_local_cprop (cfg);
9353 if (cfg->opt & MONO_OPT_DEADCE) {
9354 mono_ssa_deadce (cfg);
9355 deadce_has_run = TRUE;
9358 if ((cfg->flags & MONO_CFG_HAS_LDELEMA) && (cfg->opt & MONO_OPT_ABCREM))
9359 mono_perform_abc_removal (cfg);
9361 mono_ssa_remove (cfg);
9363 if (cfg->opt & MONO_OPT_BRANCH)
9364 optimize_branches (cfg);
9367 /* after SSA removal */
9371 decompose_pass (cfg);
9376 g_assert (cfg->got_var_allocated);
9379 * Allways allocate the GOT var to a register, because keeping it
9380 * in memory will increase the number of live temporaries in some
9381 * code created by inssel.brg, leading to the well known spills+
9382 * branches problem. Testcase: mcs crash in
9383 * System.MonoCustomAttrs:GetCustomAttributes.
9385 regs = mono_arch_get_global_int_regs (cfg);
9387 cfg->got_var->opcode = OP_REGVAR;
9388 cfg->got_var->dreg = GPOINTER_TO_INT (regs->data);
9389 cfg->used_int_regs |= 1LL << cfg->got_var->dreg;
9394 if (cfg->opt & MONO_OPT_LINEARS) {
9397 /* For now, compute aliasing info only if needed for deadce... */
9398 if ((cfg->opt & MONO_OPT_DEADCE) && (! deadce_has_run) && (header->num_clauses == 0)) {
9399 cfg->aliasing_info = mono_build_aliasing_information (cfg);
9402 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
9403 cfg->comp_done &= ~MONO_COMP_LIVENESS;
9404 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
9405 mono_analyze_liveness (cfg);
9407 if (cfg->aliasing_info != NULL) {
9408 mono_aliasing_deadce (cfg->aliasing_info);
9409 deadce_has_run = TRUE;
9412 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
9413 regs = mono_arch_get_global_int_regs (cfg);
9415 regs = g_list_delete_link (regs, regs);
9416 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
9419 if (cfg->aliasing_info != NULL) {
9420 mono_destroy_aliasing_information (cfg->aliasing_info);
9421 cfg->aliasing_info = NULL;
9425 //mono_print_code (cfg);
9429 /* variables are allocated after decompose, since decompose could create temps */
9430 mono_arch_allocate_vars (cfg);
9432 if (cfg->opt & MONO_OPT_CFOLD)
9433 mono_constant_fold (cfg);
9435 mini_select_instructions (cfg);
9438 if (cfg->verbose_level >= 2) {
9439 char *id = mono_method_full_name (cfg->method, FALSE);
9440 mono_disassemble_code (cfg, cfg->native_code, cfg->code_len, id + 3);
9444 if (cfg->method->dynamic) {
9445 jinfo = g_malloc0 (sizeof (MonoJitInfo) + (header->num_clauses * sizeof (MonoJitExceptionInfo)));
9447 /* we access cfg->domain->mp */
9448 mono_domain_lock (cfg->domain);
9449 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo) + (header->num_clauses * sizeof (MonoJitExceptionInfo)));
9450 mono_domain_unlock (cfg->domain);
9453 jinfo->method = method;
9454 jinfo->code_start = cfg->native_code;
9455 jinfo->code_size = cfg->code_len;
9456 jinfo->used_regs = cfg->used_int_regs;
9457 jinfo->domain_neutral = (cfg->opt & MONO_OPT_SHARED) != 0;
9458 jinfo->cas_inited = FALSE; /* initialization delayed at the first stalk walk using this method */
9460 if (header->num_clauses) {
9463 jinfo->num_clauses = header->num_clauses;
9465 for (i = 0; i < header->num_clauses; i++) {
9466 MonoExceptionClause *ec = &header->clauses [i];
9467 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
9468 MonoBasicBlock *tblock;
9471 ei->flags = ec->flags;
9473 exvar = mono_find_exvar_for_offset (cfg, ec->handler_offset);
9474 ei->exvar_offset = exvar ? exvar->inst_offset : 0;
9476 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
9477 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->data.filter_offset);
9479 ei->data.filter = cfg->native_code + tblock->native_offset;
9481 ei->data.catch_class = ec->data.catch_class;
9484 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
9486 ei->try_start = cfg->native_code + tblock->native_offset;
9487 g_assert (tblock->native_offset);
9488 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
9490 ei->try_end = cfg->native_code + tblock->native_offset;
9491 g_assert (tblock->native_offset);
9492 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
9494 ei->handler_start = cfg->native_code + tblock->native_offset;
9498 cfg->jit_info = jinfo;
9499 #if defined(__arm__)
9500 mono_arch_fixup_jinfo (cfg);
9503 mono_domain_lock (cfg->domain);
9504 mono_jit_info_table_add (cfg->domain, jinfo);
9506 if (cfg->method->dynamic)
9507 mono_dynamic_code_hash_lookup (cfg->domain, cfg->method)->ji = jinfo;
9508 mono_domain_unlock (cfg->domain);
9510 /* collect statistics */
9511 mono_jit_stats.allocated_code_size += cfg->code_len;
9512 code_size_ratio = cfg->code_len;
9513 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
9514 mono_jit_stats.biggest_method_size = code_size_ratio;
9515 mono_jit_stats.biggest_method = method;
9517 code_size_ratio = (code_size_ratio * 100) / mono_method_get_header (method)->code_size;
9518 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
9519 mono_jit_stats.max_code_size_ratio = code_size_ratio;
9520 mono_jit_stats.max_ratio_method = method;
9522 mono_jit_stats.native_code_size += cfg->code_len;
9524 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
9525 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
9527 /* this can only be set if the security manager is active */
9528 if (cfg->exception_type == MONO_EXCEPTION_SECURITY_LINKDEMAND) {
9529 MonoAssembly *assembly = mono_image_get_assembly (method->klass->image);
9530 MonoReflectionAssembly *refass = (MonoReflectionAssembly*) mono_assembly_get_object (domain, assembly);
9531 MonoReflectionMethod *refmet = mono_method_get_object (domain, method, NULL);
9532 MonoSecurityManager* secman = mono_security_manager_get_methods ();
9533 MonoObject *exc = NULL;
9536 args [0] = &cfg->exception_data;
9539 mono_runtime_invoke (secman->linkdemandsecurityexception, NULL, args, &exc);
9541 mono_destroy_compile (cfg);
9544 mono_raise_exception ((MonoException*)exc);
9551 mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain)
9554 GHashTable *jit_code_hash;
9555 gpointer code = NULL;
9561 jit_code_hash = target_domain->jit_code_hash;
9563 method = mono_get_inflated_method (method);
9565 #ifdef MONO_USE_AOT_COMPILER
9566 if (!mono_compile_aot && (opt & MONO_OPT_AOT) && !(mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)) {
9568 MonoDomain *domain = mono_domain_get ();
9570 mono_domain_lock (domain);
9572 mono_class_init (method->klass);
9573 if ((info = mono_aot_get_method (domain, method))) {
9574 g_hash_table_insert (domain->jit_code_hash, method, info);
9575 mono_domain_unlock (domain);
9576 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
9577 return info->code_start;
9580 mono_domain_unlock (domain);
9584 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
9585 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
9587 MonoMethodPInvoke* piinfo = (MonoMethodPInvoke *) method;
9589 if (method->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE)
9590 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);
9592 if (!piinfo->addr) {
9593 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
9594 piinfo->addr = mono_lookup_internal_call (method);
9596 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
9597 mono_lookup_pinvoke_call (method, NULL, NULL);
9599 nm = mono_marshal_get_native_wrapper (method);
9600 return mono_get_addr_from_ftnptr (mono_compile_method (nm));
9602 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
9603 //mono_debug_add_wrapper (method, nm);
9604 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
9605 const char *name = method->name;
9608 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
9609 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
9610 MonoJitICallInfo *mi = mono_find_jit_icall_by_name ("mono_delegate_ctor");
9612 return mono_get_addr_from_ftnptr ((gpointer)mono_icall_get_wrapper (mi));
9613 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
9614 nm = mono_marshal_get_delegate_invoke (method);
9615 return mono_get_addr_from_ftnptr (mono_compile_method (nm));
9616 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
9617 nm = mono_marshal_get_delegate_begin_invoke (method);
9618 return mono_get_addr_from_ftnptr (mono_compile_method (nm));
9619 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
9620 nm = mono_marshal_get_delegate_end_invoke (method);
9621 return mono_get_addr_from_ftnptr (mono_compile_method (nm));
9627 cfg = mini_method_compile (method, opt, target_domain, TRUE, FALSE, 0);
9630 /* Throw a type load exception if needed */
9631 MonoLoaderError *error = mono_loader_get_last_error ();
9634 MonoException *ex = mini_loader_error_to_exception (error);
9635 mono_loader_clear_error ();
9636 mono_raise_exception (ex);
9639 g_assert_not_reached ();
9642 mono_domain_lock (target_domain);
9644 /* Check if some other thread already did the job. In this case, we can
9645 discard the code this thread generated. */
9647 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
9648 /* We can't use a domain specific method in another domain */
9649 if ((target_domain == mono_domain_get ()) || info->domain_neutral) {
9650 code = info->code_start;
9651 // printf("Discarding code for method %s\n", method->name);
9656 g_hash_table_insert (jit_code_hash, method, cfg->jit_info);
9657 code = cfg->native_code;
9660 mono_destroy_compile (cfg);
9662 if (target_domain->jump_target_hash) {
9663 MonoJumpInfo patch_info;
9665 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
9667 patch_info.next = NULL;
9668 patch_info.ip.i = 0;
9669 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
9670 patch_info.data.method = method;
9671 g_hash_table_remove (target_domain->jump_target_hash, method);
9673 for (tmp = list; tmp; tmp = tmp->next)
9674 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info, TRUE);
9675 g_slist_free (list);
9678 mono_domain_unlock (target_domain);
9680 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
9685 mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt)
9687 /* FIXME: later copy the code from mono */
9688 MonoDomain *target_domain, *domain = mono_domain_get ();
9692 if (opt & MONO_OPT_SHARED)
9693 target_domain = mono_get_root_domain ();
9695 target_domain = domain;
9697 mono_domain_lock (target_domain);
9699 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
9700 /* We can't use a domain specific method in another domain */
9701 if (! ((domain != target_domain) && !info->domain_neutral)) {
9702 mono_domain_unlock (target_domain);
9703 mono_jit_stats.methods_lookups++;
9704 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
9705 return mono_create_ftnptr (target_domain, info->code_start);
9709 mono_domain_unlock (target_domain);
9710 p = mono_jit_compile_method_inner (method, target_domain);
9711 return mono_create_ftnptr (target_domain, p);
9715 mono_jit_compile_method (MonoMethod *method)
9717 return mono_jit_compile_method_with_opt (method, default_opt);
9721 invalidated_delegate_trampoline (char *desc)
9723 g_error ("Unmanaged code called delegate of type %s which was already garbage collected.\n"
9724 "See http://www.go-mono.com/delegate.html for an explanation and ways to fix this.",
9729 * mono_jit_free_method:
9731 * Free all memory allocated by the JIT for METHOD.
9734 mono_jit_free_method (MonoDomain *domain, MonoMethod *method)
9736 MonoJitDynamicMethodInfo *ji;
9737 gboolean destroy = TRUE;
9739 g_assert (method->dynamic);
9741 mono_domain_lock (domain);
9742 ji = mono_dynamic_code_hash_lookup (domain, method);
9743 mono_domain_unlock (domain);
9747 mono_domain_lock (domain);
9748 g_hash_table_remove (domain->dynamic_code_hash, method);
9749 g_hash_table_remove (domain->jit_code_hash, method);
9750 mono_domain_unlock (domain);
9752 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
9753 if (debug_options.keep_delegates && method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
9755 * Instead of freeing the code, change it to call an error routine
9756 * so people can fix their code.
9758 char *type = mono_type_full_name (&method->klass->byval_arg);
9759 char *type_and_method = g_strdup_printf ("%s.%s", type, method->name);
9762 mono_arch_invalidate_method (ji->ji, invalidated_delegate_trampoline, type_and_method);
9768 * This needs to be done before freeing code_mp, since the code address is the
9769 * key in the table, so if we the code_mp first, another thread can grab the
9770 * same code address and replace our entry in the table.
9772 mono_jit_info_table_remove (domain, ji->ji);
9775 mono_code_manager_destroy (ji->code_mp);
9781 mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method)
9783 MonoDomain *target_domain;
9786 if (default_opt & MONO_OPT_SHARED)
9787 target_domain = mono_get_root_domain ();
9789 target_domain = domain;
9791 mono_domain_lock (target_domain);
9793 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
9794 /* We can't use a domain specific method in another domain */
9795 if (! ((domain != target_domain) && !info->domain_neutral)) {
9796 mono_domain_unlock (target_domain);
9797 mono_jit_stats.methods_lookups++;
9798 return info->code_start;
9802 mono_domain_unlock (target_domain);
9808 * mono_jit_runtime_invoke:
9809 * @method: the method to invoke
9810 * @obj: this pointer
9811 * @params: array of parameter values.
9812 * @exc: used to catch exceptions objects
9815 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
9818 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc, void* compiled_method);
9819 void* compiled_method;
9821 if (obj == NULL && !(method->flags & METHOD_ATTRIBUTE_STATIC) && !method->string_ctor && (method->wrapper_type == 0)) {
9822 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
9826 method = mono_get_inflated_method (method);
9827 invoke = mono_marshal_get_runtime_invoke (method);
9828 runtime_invoke = mono_jit_compile_method (invoke);
9830 /* We need this here becuase mono_marshal_get_runtime_invoke can be place
9831 * the helper method in System.Object and not the target class
9833 mono_runtime_class_init (mono_class_vtable (mono_domain_get (), method->klass));
9835 compiled_method = mono_jit_compile_method (method);
9836 return runtime_invoke (obj, params, exc, compiled_method);
9839 #ifdef PLATFORM_WIN32
9840 #define GET_CONTEXT \
9841 struct sigcontext *ctx = (struct sigcontext*)_dummy;
9844 #define GET_CONTEXT \
9845 void *ctx = context;
9846 #elif defined(sun) // Solaris x86
9847 #define GET_CONTEXT \
9848 ucontext_t *uctx = context; \
9849 struct sigcontext *ctx = (struct sigcontext *)&(uctx->uc_mcontext);
9850 #elif defined (MONO_ARCH_USE_SIGACTION)
9851 #define GET_CONTEXT \
9852 void *ctx = context;
9854 #define GET_CONTEXT \
9855 void **_p = (void **)&_dummy; \
9856 struct sigcontext *ctx = (struct sigcontext *)++_p;
9860 #ifdef MONO_ARCH_USE_SIGACTION
9861 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, siginfo_t *info, void *context)
9863 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy)
9867 SIG_HANDLER_SIGNATURE (sigfpe_signal_handler)
9869 MonoException *exc = NULL;
9870 #ifndef MONO_ARCH_USE_SIGACTION
9875 #if defined(MONO_ARCH_HAVE_IS_INT_OVERFLOW)
9876 if (mono_arch_is_int_overflow (ctx, info))
9877 exc = mono_get_exception_arithmetic ();
9879 exc = mono_get_exception_divide_by_zero ();
9881 exc = mono_get_exception_divide_by_zero ();
9884 mono_arch_handle_exception (ctx, exc, FALSE);
9888 SIG_HANDLER_SIGNATURE (sigill_signal_handler)
9893 exc = mono_get_exception_execution_engine ("SIGILL");
9895 mono_arch_handle_exception (ctx, exc, FALSE);
9898 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
9900 #ifndef MONO_ARCH_USE_SIGACTION
9901 #error "Can't use sigaltstack without sigaction"
9907 SIG_HANDLER_SIGNATURE (sigsegv_signal_handler)
9909 MonoException *exc = NULL;
9912 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
9913 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
9917 #ifdef MONO_ARCH_USE_SIGACTION
9918 if (debug_options.collect_pagefault_stats) {
9919 if (mono_raw_buffer_is_pagefault (info->si_addr)) {
9920 mono_raw_buffer_handle_pagefault (info->si_addr);
9923 if (mono_aot_is_pagefault (info->si_addr)) {
9924 mono_aot_handle_pagefault (info->si_addr);
9930 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
9931 /* Can't allocate memory using Boehm GC on altstack */
9932 if (jit_tls->stack_size &&
9933 ((guint8*)info->si_addr >= (guint8*)jit_tls->end_of_stack - jit_tls->stack_size) &&
9934 ((guint8*)info->si_addr < (guint8*)jit_tls->end_of_stack))
9935 exc = mono_domain_get ()->stack_overflow_ex;
9937 exc = mono_domain_get ()->null_reference_ex;
9940 ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx));
9942 mono_handle_native_sigsegv (ctx);
9945 mono_arch_handle_exception (ctx, exc, FALSE);
9949 SIG_HANDLER_SIGNATURE (sigusr1_signal_handler)
9951 gboolean running_managed;
9959 * This is an async signal, so the code below must not call anything which
9960 * is not async safe. That includes the pthread locking functions. If we
9961 * know that we interrupted managed code, then locking is safe.
9963 ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx));
9964 running_managed = ji != NULL;
9966 exc = mono_thread_request_interruption (running_managed);
9969 mono_arch_handle_exception (ctx, exc, FALSE);
9973 SIG_HANDLER_SIGNATURE (sigprof_signal_handler)
9977 mono_profiler_stat_hit (mono_arch_ip_from_context (ctx), ctx);
9981 SIG_HANDLER_SIGNATURE (sigquit_signal_handler)
9986 exc = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
9988 mono_arch_handle_exception (ctx, exc, FALSE);
9992 SIG_HANDLER_SIGNATURE (sigint_signal_handler)
9997 exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
9999 mono_arch_handle_exception (ctx, exc, FALSE);
10003 SIG_HANDLER_SIGNATURE (sigusr2_signal_handler)
10005 gboolean enabled = mono_trace_is_enabled ();
10007 mono_trace_enable (!enabled);
10010 #ifndef PLATFORM_WIN32
10012 add_signal_handler (int signo, gpointer handler)
10014 struct sigaction sa;
10016 #ifdef MONO_ARCH_USE_SIGACTION
10017 sa.sa_sigaction = handler;
10018 sigemptyset (&sa.sa_mask);
10019 sa.sa_flags = SA_SIGINFO;
10021 sa.sa_handler = handler;
10022 sigemptyset (&sa.sa_mask);
10025 g_assert (sigaction (signo, &sa, NULL) != -1);
10030 mono_runtime_install_handlers (void)
10032 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
10033 struct sigaction sa;
10036 #ifdef PLATFORM_WIN32
10038 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
10039 win32_seh_set_handler(SIGILL, sigill_signal_handler);
10040 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
10041 if (debug_options.handle_sigint)
10042 win32_seh_set_handler(SIGINT, sigint_signal_handler);
10044 #else /* !PLATFORM_WIN32 */
10046 /* libpthreads has its own implementation of sigaction(),
10047 * but it seems to work well with our current exception
10048 * handlers. If not we must call syscall directly instead
10051 if (debug_options.handle_sigint)
10052 add_signal_handler (SIGINT, sigint_signal_handler);
10054 add_signal_handler (SIGFPE, sigfpe_signal_handler);
10055 add_signal_handler (SIGQUIT, sigquit_signal_handler);
10056 add_signal_handler (SIGILL, sigill_signal_handler);
10057 add_signal_handler (SIGBUS, sigsegv_signal_handler);
10058 if (mono_jit_trace_calls != NULL)
10059 add_signal_handler (SIGUSR2, sigusr2_signal_handler);
10061 add_signal_handler (mono_thread_get_abort_signal (), sigusr1_signal_handler);
10062 signal (SIGPIPE, SIG_IGN);
10064 /* catch SIGSEGV */
10065 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
10066 sa.sa_sigaction = sigsegv_signal_handler;
10067 sigemptyset (&sa.sa_mask);
10068 sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
10069 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
10071 add_signal_handler (SIGSEGV, sigsegv_signal_handler);
10074 #endif /* PLATFORM_WIN32 */
10078 #ifdef HAVE_LINUX_RTC_H
10079 #include <linux/rtc.h>
10080 #include <sys/ioctl.h>
10082 static int rtc_fd = -1;
10085 enable_rtc_timer (gboolean enable)
10088 flags = fcntl (rtc_fd, F_GETFL);
10090 perror ("getflags");
10097 if (fcntl (rtc_fd, F_SETFL, flags) == -1) {
10098 perror ("setflags");
10106 setup_stat_profiler (void)
10109 struct itimerval itval;
10110 static int inited = 0;
10111 #ifdef HAVE_LINUX_RTC_H
10112 const char *rtc_freq;
10113 if (!inited && (rtc_freq = g_getenv ("MONO_RTC"))) {
10117 freq = atoi (rtc_freq);
10120 rtc_fd = open ("/dev/rtc", O_RDONLY);
10121 if (rtc_fd == -1) {
10122 perror ("open /dev/rtc");
10125 add_signal_handler (SIGPROF, sigprof_signal_handler);
10126 if (ioctl (rtc_fd, RTC_IRQP_SET, freq) == -1) {
10127 perror ("set rtc freq");
10130 if (ioctl (rtc_fd, RTC_PIE_ON, 0) == -1) {
10131 perror ("start rtc");
10134 if (fcntl (rtc_fd, F_SETSIG, SIGPROF) == -1) {
10138 if (fcntl (rtc_fd, F_SETOWN, getpid ()) == -1) {
10142 enable_rtc_timer (TRUE);
10149 itval.it_interval.tv_usec = 999;
10150 itval.it_interval.tv_sec = 0;
10151 itval.it_value = itval.it_interval;
10152 setitimer (ITIMER_PROF, &itval, NULL);
10156 add_signal_handler (SIGPROF, sigprof_signal_handler);
10160 /* mono_jit_create_remoting_trampoline:
10161 * @method: pointer to the method info
10163 * Creates a trampoline which calls the remoting functions. This
10164 * is used in the vtable of transparent proxies.
10166 * Returns: a pointer to the newly created code
10169 mono_jit_create_remoting_trampoline (MonoMethod *method, MonoRemotingTarget target)
10172 guint8 *addr = NULL;
10174 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
10175 (mono_method_signature (method)->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
10176 nm = mono_marshal_get_remoting_invoke_for_target (method, target);
10177 addr = mono_compile_method (nm);
10179 addr = mono_compile_method (method);
10181 return mono_get_addr_from_ftnptr (addr);
10185 mini_parse_debug_options (void)
10187 char *options = getenv ("MONO_DEBUG");
10188 gchar **args, **ptr;
10193 args = g_strsplit (options, ",", -1);
10195 for (ptr = args; ptr && *ptr; ptr++) {
10196 const char *arg = *ptr;
10198 if (!strcmp (arg, "handle-sigint"))
10199 debug_options.handle_sigint = TRUE;
10200 else if (!strcmp (arg, "keep-delegates"))
10201 debug_options.keep_delegates = TRUE;
10202 else if (!strcmp (arg, "collect-pagefault-stats"))
10203 debug_options.collect_pagefault_stats = TRUE;
10205 fprintf (stderr, "Invalid option for the MONO_DEBUG env variable: %s\n", arg);
10206 fprintf (stderr, "Available options: 'handle-sigint', 'keep-delegates', 'collect-pagefault-stats'\n");
10213 mini_init (const char *filename)
10215 MonoDomain *domain;
10217 /* Happens when using the embedding interface */
10218 if (default_opt == 0)
10219 default_opt = mono_parse_default_optimizations (NULL);
10221 InitializeCriticalSection (&jit_mutex);
10223 global_codeman = mono_code_manager_new ();
10224 jit_icall_name_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
10226 mono_arch_cpu_init ();
10228 mono_init_trampolines ();
10230 mono_init_exceptions ();
10232 if (!g_thread_supported ())
10233 g_thread_init (NULL);
10235 if (getenv ("MONO_DEBUG") != NULL)
10236 mini_parse_debug_options ();
10238 if (mono_running_on_valgrind ()) {
10239 gsize stack_bottom = (gsize)&domain;
10240 stack_bottom += 4095;
10241 stack_bottom &= ~4095;
10242 GC_stackbottom = (char*)stack_bottom;
10244 MONO_GC_PRE_INIT ();
10246 mono_jit_tls_id = TlsAlloc ();
10247 setup_jit_tls_data ((gpointer)-1, mono_thread_abort);
10251 if (default_opt & MONO_OPT_AOT)
10254 mono_runtime_install_handlers ();
10255 mono_threads_install_cleanup (mini_thread_cleanup);
10257 #define JIT_TRAMPOLINES_WORK
10258 #ifdef JIT_TRAMPOLINES_WORK
10259 mono_install_compile_method (mono_jit_compile_method);
10260 mono_install_free_method (mono_jit_free_method);
10261 mono_install_trampoline (mono_create_jit_trampoline);
10262 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
10263 mono_install_delegate_trampoline (mono_create_delegate_trampoline);
10265 #define JIT_INVOKE_WORKS
10266 #ifdef JIT_INVOKE_WORKS
10267 mono_install_runtime_invoke (mono_jit_runtime_invoke);
10268 mono_install_handler (mono_arch_get_throw_exception ());
10270 mono_install_stack_walk (mono_jit_walk_stack);
10271 mono_install_init_vtable (mono_aot_init_vtable);
10272 mono_install_get_cached_class_info (mono_aot_get_cached_class_info);
10273 mono_install_jit_info_find_in_aot (mono_aot_find_jit_info);
10275 if (debug_options.collect_pagefault_stats) {
10276 mono_raw_buffer_set_make_unreadable (TRUE);
10277 mono_aot_set_make_unreadable (TRUE);
10280 domain = mono_init_from_assembly (filename, filename);
10281 mono_icall_init ();
10283 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
10284 ves_icall_get_frame_info);
10285 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
10286 ves_icall_get_trace);
10287 mono_add_internal_call ("System.Exception::get_trace",
10288 ves_icall_System_Exception_get_trace);
10289 mono_add_internal_call ("System.Security.SecurityFrame::_GetSecurityFrame",
10290 ves_icall_System_Security_SecurityFrame_GetSecurityFrame);
10291 mono_add_internal_call ("System.Security.SecurityFrame::_GetSecurityStack",
10292 ves_icall_System_Security_SecurityFrame_GetSecurityStack);
10293 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
10294 mono_runtime_install_handlers);
10297 create_helper_signature ();
10299 #define JIT_CALLS_WORK
10300 #ifdef JIT_CALLS_WORK
10301 /* Needs to be called here since register_jit_icall depends on it */
10302 mono_marshal_init ();
10304 mono_arch_register_lowlevel_calls ();
10305 register_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
10306 register_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
10307 register_icall (mono_trace_enter_method, "mono_trace_enter_method", NULL, TRUE);
10308 register_icall (mono_trace_leave_method, "mono_trace_leave_method", NULL, TRUE);
10309 register_icall (mono_get_lmf_addr, "mono_get_lmf_addr", "ptr", TRUE);
10310 register_icall (mono_jit_thread_attach, "mono_jit_thread_attach", "void", TRUE);
10311 register_icall (mono_domain_get, "mono_domain_get", "ptr", TRUE);
10313 register_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", "void object", TRUE);
10314 register_icall (mono_arch_get_rethrow_exception (), "mono_arch_rethrow_exception", "void object", TRUE);
10315 register_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name", "void ptr", TRUE);
10316 #if MONO_ARCH_HAVE_THROW_CORLIB_EXCEPTION
10317 register_icall (mono_arch_get_throw_corlib_exception (), "mono_arch_throw_corlib_exception",
10320 register_icall (mono_thread_get_pending_exception, "mono_thread_get_pending_exception", "object", FALSE);
10321 register_icall (mono_thread_interruption_checkpoint, "mono_thread_interruption_checkpoint", "void", FALSE);
10322 register_icall (mono_thread_force_interruption_checkpoint, "mono_thread_force_interruption_checkpoint", "void", FALSE);
10323 register_icall (mono_load_remote_field_new, "mono_load_remote_field_new", "object object ptr ptr", FALSE);
10324 register_icall (mono_store_remote_field_new, "mono_store_remote_field_new", "void object ptr ptr object", FALSE);
10327 * NOTE, NOTE, NOTE, NOTE:
10328 * when adding emulation for some opcodes, remember to also add a dummy
10329 * rule to the burg files, because we need the arity information to be correct.
10331 #ifndef MONO_ARCH_NO_EMULATE_LONG_MUL_OPTS
10332 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", "long long long", mono_llmult, TRUE);
10333 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", "long long long", mono_lldiv, FALSE);
10334 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", "long long long", mono_lldiv_un, FALSE);
10335 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", "long long long", mono_llrem, FALSE);
10336 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", "long long long", mono_llrem_un, FALSE);
10337 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", "long long long", mono_llmult_ovf_un, FALSE);
10338 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", "long long long", mono_llmult_ovf, FALSE);
10341 #ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
10342 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", "long long int32", mono_lshl, TRUE);
10343 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", "long long int32", mono_lshr, TRUE);
10344 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", "long long int32", mono_lshr_un, TRUE);
10347 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
10348 mono_register_opcode_emulation (CEE_DIV, "__emul_idiv", "int32 int32 int32", mono_idiv, TRUE);
10349 mono_register_opcode_emulation (CEE_DIV_UN, "__emul_idiv_un", "int32 int32 int32", mono_idiv_un, TRUE);
10350 mono_register_opcode_emulation (CEE_REM, "__emul_irem", "int32 int32 int32", mono_irem, TRUE);
10351 mono_register_opcode_emulation (CEE_REM_UN, "__emul_irem_un", "int32 int32 int32", mono_irem_un, TRUE);
10354 #ifdef MONO_ARCH_EMULATE_MUL_DIV
10355 mono_register_opcode_emulation (CEE_MUL_OVF, "__emul_imul_ovf", "int32 int32 int32", mono_imul_ovf, TRUE);
10356 mono_register_opcode_emulation (CEE_MUL_OVF_UN, "__emul_imul_ovf_un", "int32 int32 int32", mono_imul_ovf_un, TRUE);
10357 mono_register_opcode_emulation (CEE_MUL, "__emul_imul", "int32 int32 int32", mono_imul, TRUE);
10358 mono_register_opcode_emulation (OP_FDIV, "__emul_fdiv", "double double double", mono_fdiv, TRUE);
10361 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", "ulong double", mono_fconv_u8, FALSE);
10362 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", "uint32 double", mono_fconv_u4, FALSE);
10363 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", "long double", mono_fconv_ovf_i8, FALSE);
10364 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", "ulong double", mono_fconv_ovf_u8, FALSE);
10366 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
10367 mono_register_opcode_emulation (OP_FCONV_TO_I8, "__emul_fconv_to_i8", "long double", mono_fconv_i8, FALSE);
10369 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
10370 mono_register_opcode_emulation (CEE_CONV_R_UN, "__emul_conv_r_un", "double int32", mono_conv_to_r8_un, FALSE);
10372 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
10373 mono_register_opcode_emulation (OP_LCONV_TO_R8, "__emul_lconv_to_r8", "double long", mono_lconv_to_r8, FALSE);
10375 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
10376 mono_register_opcode_emulation (OP_LCONV_TO_R4, "__emul_lconv_to_r4", "float long", mono_lconv_to_r4, FALSE);
10378 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
10379 mono_register_opcode_emulation (OP_LCONV_TO_R_UN, "__emul_lconv_to_r8_un", "double long", mono_lconv_to_r8_un, FALSE);
10381 #ifdef MONO_ARCH_EMULATE_FREM
10382 mono_register_opcode_emulation (OP_FREM, "__emul_frem", "double double double", fmod, FALSE);
10385 #if SIZEOF_VOID_P == 4
10386 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", "uint32 double", mono_fconv_u4, TRUE);
10389 /* other jit icalls */
10390 register_icall (mono_delegate_ctor, "mono_delegate_ctor", "void object object ptr", FALSE);
10391 register_icall (mono_class_static_field_address , "mono_class_static_field_address",
10392 "ptr ptr ptr", FALSE);
10393 register_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", "ptr ptr ptr ptr", FALSE);
10394 register_icall (mono_get_special_static_data, "mono_get_special_static_data", "ptr int", FALSE);
10395 register_icall (mono_ldstr, "mono_ldstr", "object ptr ptr int32", FALSE);
10396 register_icall (helper_stelem_ref, "helper_stelem_ref", "void ptr int32 object", FALSE);
10397 register_icall (helper_stelem_ref_check, "helper_stelem_ref_check", "void object object", FALSE);
10398 register_icall (mono_object_new, "mono_object_new", "object ptr ptr", FALSE);
10399 register_icall (mono_object_new_specific, "mono_object_new_specific", "object ptr", FALSE);
10400 register_icall (mono_array_new, "mono_array_new", "object ptr ptr int32", FALSE);
10401 register_icall (mono_array_new_specific, "mono_array_new_specific", "object ptr int32", FALSE);
10402 register_icall (mono_runtime_class_init, "mono_runtime_class_init", "void ptr", FALSE);
10403 register_icall (mono_ldftn, "mono_ldftn", "ptr ptr", FALSE);
10404 register_icall (mono_ldftn_nosync, "mono_ldftn_nosync", "ptr ptr", FALSE);
10405 register_icall (mono_ldvirtfn, "mono_ldvirtfn", "ptr object ptr", FALSE);
10406 register_icall (helper_compile_generic_method, "compile_generic_method", "ptr object ptr ptr", FALSE);
10407 register_icall (helper_ldstr, "helper_ldstr", "object ptr int", FALSE);
10410 #define JIT_RUNTIME_WORKS
10411 #ifdef JIT_RUNTIME_WORKS
10412 mono_install_runtime_cleanup ((MonoDomainFunc)mini_cleanup);
10413 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
10416 mono_thread_attach (domain);
10420 MonoJitStats mono_jit_stats = {0};
10423 print_jit_stats (void)
10425 if (mono_jit_stats.enabled) {
10426 g_print ("Mono Jit statistics\n");
10427 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
10428 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
10429 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
10430 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
10431 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
10432 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
10433 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
10434 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
10435 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
10436 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
10437 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
10438 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
10439 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
10440 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
10441 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
10442 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
10443 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
10444 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
10446 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
10447 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
10448 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
10449 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
10450 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
10452 g_print ("\nGeneric instances: %ld\n", mono_stats.generic_instance_count);
10453 g_print ("Initialized classes: %ld\n", mono_stats.generic_class_count);
10454 g_print ("Inflated methods: %ld / %ld\n", mono_stats.inflated_method_count_2,
10455 mono_stats.inflated_method_count);
10456 g_print ("Inflated types: %ld\n", mono_stats.inflated_type_count);
10457 g_print ("Generics metadata size: %ld\n", mono_stats.generics_metadata_size);
10459 if (mono_use_security_manager) {
10460 g_print ("\nDecl security check : %ld\n", mono_jit_stats.cas_declsec_check);
10461 g_print ("LinkDemand (user) : %ld\n", mono_jit_stats.cas_linkdemand);
10462 g_print ("LinkDemand (icall) : %ld\n", mono_jit_stats.cas_linkdemand_icall);
10463 g_print ("LinkDemand (pinvoke) : %ld\n", mono_jit_stats.cas_linkdemand_pinvoke);
10464 g_print ("LinkDemand (aptc) : %ld\n", mono_jit_stats.cas_linkdemand_aptc);
10465 g_print ("Demand (code gen) : %ld\n", mono_jit_stats.cas_demand_generation);
10467 if (debug_options.collect_pagefault_stats) {
10468 g_print ("Metadata pagefaults : %d\n", mono_raw_buffer_get_n_pagefaults ());
10469 g_print ("AOT pagefaults : %d\n", mono_aot_get_n_pagefaults ());
10475 mini_cleanup (MonoDomain *domain)
10477 #ifdef HAVE_LINUX_RTC_H
10479 enable_rtc_timer (FALSE);
10483 * mono_runtime_cleanup() and mono_domain_finalize () need to
10484 * be called early since they need the execution engine still
10485 * fully working (mono_domain_finalize may invoke managed finalizers
10486 * and mono_runtime_cleanup will wait for other threads to finish).
10488 mono_domain_finalize (domain, 2000);
10490 mono_runtime_cleanup (domain);
10492 mono_profiler_shutdown ();
10494 mono_debug_cleanup ();
10496 mono_icall_cleanup ();
10498 #ifdef PLATFORM_WIN32
10499 win32_seh_cleanup();
10502 mono_domain_free (domain, TRUE);
10504 mono_code_manager_destroy (global_codeman);
10505 g_hash_table_destroy (jit_icall_name_hash);
10506 if (class_init_hash_addr)
10507 g_hash_table_destroy (class_init_hash_addr);
10509 print_jit_stats ();
10513 mono_set_defaults (int verbose_level, guint32 opts)
10515 mini_verbose = verbose_level;
10516 default_opt = opts;
10520 mono_precompile_assembly (MonoAssembly *ass, void *user_data)
10522 GHashTable *assemblies = (GHashTable*)user_data;
10523 MonoImage *image = mono_assembly_get_image (ass);
10524 MonoMethod *method, *invoke;
10527 if (g_hash_table_lookup (assemblies, ass))
10530 g_hash_table_insert (assemblies, ass, ass);
10532 if (mini_verbose > 0)
10533 printf ("PRECOMPILE: %s.\n", mono_image_get_filename (image));
10535 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
10536 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
10537 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
10541 if (mini_verbose > 1) {
10542 char * desc = mono_method_full_name (method, TRUE);
10543 g_print ("Compiling %d %s\n", count, desc);
10546 mono_compile_method (method);
10547 if (strcmp (method->name, "Finalize") == 0) {
10548 invoke = mono_marshal_get_runtime_invoke (method);
10549 mono_compile_method (invoke);
10551 if (method->klass->marshalbyref && mono_method_signature (method)->hasthis) {
10552 invoke = mono_marshal_get_remoting_invoke_with_check (method);
10553 mono_compile_method (invoke);
10557 /* Load and precompile referenced assemblies as well */
10558 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_ASSEMBLYREF); ++i) {
10559 mono_assembly_load_reference (image, i);
10560 if (image->references [i])
10561 mono_precompile_assembly (image->references [i], assemblies);
10565 void mono_precompile_assemblies ()
10567 GHashTable *assemblies = g_hash_table_new (NULL, NULL);
10569 mono_assembly_foreach ((GFunc)mono_precompile_assembly, assemblies);
10571 g_hash_table_destroy (assemblies);