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 (cmethod->klass == mono_defaults.array_class) {
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, ialign;
3309 int num_calls = 0, inline_costs = 0;
3310 int breakpoint_id = 0;
3312 guint real_offset, num_args;
3313 MonoBoolean security, pinvoke;
3314 MonoSecurityManager* secman = NULL;
3315 MonoDeclSecurityActions actions;
3316 GSList *class_inits = NULL;
3318 image = method->klass->image;
3319 header = mono_method_get_header (method);
3320 generic_container = method->generic_container;
3321 sig = mono_method_signature (method);
3322 num_args = sig->hasthis + sig->param_count;
3323 ip = (unsigned char*)header->code;
3324 end = ip + header->code_size;
3325 mono_jit_stats.cil_code_size += header->code_size;
3327 if (sig->is_inflated)
3328 generic_context = ((MonoMethodInflated *) method)->context;
3329 else if (generic_container)
3330 generic_context = &generic_container->context;
3332 g_assert (!sig->has_type_parameters);
3334 if (cfg->method == method) {
3336 bbhash = cfg->bb_hash;
3338 real_offset = inline_offset;
3339 bbhash = g_hash_table_new (g_direct_hash, NULL);
3342 if (cfg->verbose_level > 2)
3343 g_print ("method to IR %s\n", mono_method_full_name (method, TRUE));
3345 dont_inline = g_list_prepend (dont_inline, method);
3346 if (cfg->method == method) {
3348 if (cfg->method->save_lmf)
3349 /* Needed by the prolog code */
3350 mono_get_got_var (cfg);
3352 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
3353 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
3356 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
3357 start_bblock->cil_code = NULL;
3358 start_bblock->cil_length = 0;
3359 start_bblock->block_num = cfg->num_bblocks++;
3362 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
3363 end_bblock->cil_code = NULL;
3364 end_bblock->cil_length = 0;
3365 end_bblock->block_num = cfg->num_bblocks++;
3366 g_assert (cfg->num_bblocks == 2);
3368 arg_array = alloca (sizeof (MonoInst *) * num_args);
3369 for (i = num_args - 1; i >= 0; i--)
3370 arg_array [i] = cfg->varinfo [i];
3372 if (header->num_clauses) {
3373 cfg->spvars = g_hash_table_new (NULL, NULL);
3374 cfg->exvars = g_hash_table_new (NULL, NULL);
3376 /* handle exception clauses */
3377 for (i = 0; i < header->num_clauses; ++i) {
3378 MonoBasicBlock *try_bb;
3379 MonoExceptionClause *clause = &header->clauses [i];
3380 GET_BBLOCK (cfg, bbhash, try_bb, ip + clause->try_offset);
3381 try_bb->real_offset = clause->try_offset;
3382 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
3383 tblock->real_offset = clause->handler_offset;
3384 tblock->flags |= BB_EXCEPTION_HANDLER;
3386 link_bblock (cfg, try_bb, tblock);
3388 if (*(ip + clause->handler_offset) == CEE_POP)
3389 tblock->flags |= BB_EXCEPTION_DEAD_OBJ;
3391 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
3392 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
3393 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
3394 MONO_ADD_INS (tblock, ins);
3396 /* todo: is a fault block unsafe to optimize? */
3397 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
3398 tblock->flags |= BB_EXCEPTION_UNSAFE;
3402 /*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);
3404 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
3406 /* catch and filter blocks get the exception object on the stack */
3407 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
3408 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
3409 MonoInst *load, *dummy_use;
3411 /* mostly like handle_stack_args (), but just sets the input args */
3412 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
3413 tblock->in_scount = 1;
3414 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
3415 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
3418 * Add a dummy use for the exvar so its liveness info will be
3421 NEW_TEMPLOAD (cfg, load, tblock->in_stack [0]->inst_c0);
3422 NEW_DUMMY_USE (cfg, dummy_use, load);
3423 MONO_ADD_INS (tblock, dummy_use);
3425 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
3426 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->data.filter_offset);
3427 tblock->real_offset = clause->data.filter_offset;
3428 tblock->in_scount = 1;
3429 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
3430 /* The filter block shares the exvar with the handler block */
3431 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
3432 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
3433 MONO_ADD_INS (tblock, ins);
3438 arg_array = alloca (sizeof (MonoInst *) * num_args);
3439 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
3442 /* FIRST CODE BLOCK */
3443 bblock = NEW_BBLOCK (cfg);
3444 bblock->cil_code = ip;
3446 ADD_BBLOCK (cfg, bbhash, bblock);
3448 if (cfg->method == method) {
3449 breakpoint_id = mono_debugger_method_has_breakpoint (method);
3450 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
3451 MONO_INST_NEW (cfg, ins, CEE_BREAK);
3452 MONO_ADD_INS (bblock, ins);
3456 if (mono_use_security_manager)
3457 secman = mono_security_manager_get_methods ();
3459 security = (secman && mono_method_has_declsec (method));
3460 /* at this point having security doesn't mean we have any code to generate */
3461 if (security && (cfg->method == method)) {
3462 /* Only Demand, NonCasDemand and DemandChoice requires code generation.
3463 * And we do not want to enter the next section (with allocation) if we
3464 * have nothing to generate */
3465 security = mono_declsec_get_demands (method, &actions);
3468 /* we must Demand SecurityPermission.Unmanaged before P/Invoking */
3469 pinvoke = (secman && (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE));
3471 MonoMethod *wrapped = mono_marshal_method_from_wrapper (method);
3472 if (wrapped && (wrapped->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
3473 MonoCustomAttrInfo* custom = mono_custom_attrs_from_method (wrapped);
3475 /* unless the method or it's class has the [SuppressUnmanagedCodeSecurity] attribute */
3476 if (custom && mono_custom_attrs_has_attr (custom, secman->suppressunmanagedcodesecurity)) {
3481 custom = mono_custom_attrs_from_class (wrapped->klass);
3482 if (custom && mono_custom_attrs_has_attr (custom, secman->suppressunmanagedcodesecurity)) {
3487 /* not a P/Invoke after all */
3492 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED))) || mono_compile_aot || security || pinvoke) {
3493 /* we use a separate basic block for the initialization code */
3494 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
3495 init_localsbb->real_offset = real_offset;
3496 start_bblock->next_bb = init_localsbb;
3497 init_localsbb->next_bb = bblock;
3498 link_bblock (cfg, start_bblock, init_localsbb);
3499 link_bblock (cfg, init_localsbb, bblock);
3500 init_localsbb->block_num = cfg->num_bblocks++;
3502 start_bblock->next_bb = bblock;
3503 link_bblock (cfg, start_bblock, bblock);
3506 /* at this point we know, if security is TRUE, that some code needs to be generated */
3507 if (security && (cfg->method == method)) {
3510 mono_jit_stats.cas_demand_generation++;
3512 if (actions.demand.blob) {
3513 /* Add code for SecurityAction.Demand */
3514 NEW_DECLSECCONST (cfg, args[0], image, actions.demand);
3515 NEW_ICONST (cfg, args [1], actions.demand.size);
3516 /* Calls static void SecurityManager.InternalDemand (byte* permissions, int size); */
3517 mono_emit_method_call_spilled (cfg, init_localsbb, secman->demand, mono_method_signature (secman->demand), args, ip, NULL);
3519 if (actions.noncasdemand.blob) {
3520 /* CLR 1.x uses a .noncasdemand (but 2.x doesn't) */
3521 /* For Mono we re-route non-CAS Demand to Demand (as the managed code must deal with it anyway) */
3522 NEW_DECLSECCONST (cfg, args[0], image, actions.noncasdemand);
3523 NEW_ICONST (cfg, args [1], actions.noncasdemand.size);
3524 /* Calls static void SecurityManager.InternalDemand (byte* permissions, int size); */
3525 mono_emit_method_call_spilled (cfg, init_localsbb, secman->demand, mono_method_signature (secman->demand), args, ip, NULL);
3527 if (actions.demandchoice.blob) {
3528 /* New in 2.0, Demand must succeed for one of the permissions (i.e. not all) */
3529 NEW_DECLSECCONST (cfg, args[0], image, actions.demandchoice);
3530 NEW_ICONST (cfg, args [1], actions.demandchoice.size);
3531 /* Calls static void SecurityManager.InternalDemandChoice (byte* permissions, int size); */
3532 mono_emit_method_call_spilled (cfg, init_localsbb, secman->demandchoice, mono_method_signature (secman->demandchoice), args, ip, NULL);
3536 /* we must Demand SecurityPermission.Unmanaged before p/invoking */
3538 mono_emit_method_call_spilled (cfg, init_localsbb, secman->demandunmanaged, mono_method_signature (secman->demandunmanaged), NULL, ip, NULL);
3541 if (get_basic_blocks (cfg, bbhash, header, real_offset, ip, end, &err_pos)) {
3546 if (cfg->method == method)
3547 mono_debug_init_method (cfg, bblock, breakpoint_id);
3549 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
3551 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
3552 for (n = 0; n < sig->param_count; ++n)
3553 param_types [n + sig->hasthis] = sig->params [n];
3556 /* do this somewhere outside - not here */
3557 NEW_ICONST (cfg, zero_int32, 0);
3558 NEW_ICONST (cfg, zero_int64, 0);
3559 zero_int64->type = STACK_I8;
3560 NEW_PCONST (cfg, zero_ptr, 0);
3561 NEW_PCONST (cfg, zero_obj, 0);
3562 zero_obj->type = STACK_OBJ;
3564 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
3565 zero_r8->type = STACK_R8;
3566 zero_r8->inst_p0 = &r8_0;
3568 /* add a check for this != NULL to inlined methods */
3569 if (is_virtual_call) {
3570 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
3571 NEW_ARGLOAD (cfg, ins->inst_left, 0);
3573 MONO_ADD_INS (bblock, ins);
3576 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
3577 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
3580 start_new_bblock = 0;
3583 if (cfg->method == method)
3584 real_offset = ip - header->code;
3586 real_offset = inline_offset;
3588 if (start_new_bblock) {
3589 bblock->cil_length = ip - bblock->cil_code;
3590 if (start_new_bblock == 2) {
3591 g_assert (ip == tblock->cil_code);
3593 GET_BBLOCK (cfg, bbhash, tblock, ip);
3595 bblock->next_bb = tblock;
3597 start_new_bblock = 0;
3598 for (i = 0; i < bblock->in_scount; ++i) {
3599 if (cfg->verbose_level > 3)
3600 g_print ("loading %d from temp %d\n", i, (int)bblock->in_stack [i]->inst_c0);
3601 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
3604 g_slist_free (class_inits);
3607 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
3608 link_bblock (cfg, bblock, tblock);
3609 if (sp != stack_start) {
3610 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3613 bblock->next_bb = tblock;
3615 for (i = 0; i < bblock->in_scount; ++i) {
3616 if (cfg->verbose_level > 3)
3617 g_print ("loading %d from temp %d\n", i, (int)bblock->in_stack [i]->inst_c0);
3618 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
3621 g_slist_free (class_inits);
3626 bblock->real_offset = real_offset;
3628 if ((cfg->method == method) && cfg->coverage_info) {
3629 MonoInst *store, *one;
3630 guint32 cil_offset = ip - header->code;
3631 cfg->coverage_info->data [cil_offset].cil_code = ip;
3633 /* TODO: Use an increment here */
3634 NEW_ICONST (cfg, one, 1);
3637 NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
3640 MONO_INST_NEW (cfg, store, CEE_STIND_I);
3641 store->cil_code = ip;
3642 store->inst_left = ins;
3643 store->inst_right = one;
3645 MONO_ADD_INS (bblock, store);
3648 if (cfg->verbose_level > 3)
3649 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
3654 MONO_INST_NEW (cfg, ins, *ip);
3655 ins->cil_code = ip++;
3656 MONO_ADD_INS (bblock, ins);
3662 CHECK_STACK_OVF (1);
3663 n = (*ip)-CEE_LDARG_0;
3665 NEW_ARGLOAD (cfg, ins, n);
3666 ins->cil_code = ip++;
3673 CHECK_STACK_OVF (1);
3674 n = (*ip)-CEE_LDLOC_0;
3676 NEW_LOCLOAD (cfg, ins, n);
3677 ins->cil_code = ip++;
3685 n = (*ip)-CEE_STLOC_0;
3688 handle_loaded_temps (cfg, bblock, stack_start, sp);
3689 NEW_LOCSTORE (cfg, ins, n, *sp);
3691 if (ins->opcode == CEE_STOBJ) {
3692 NEW_LOCLOADA (cfg, ins, n);
3693 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3695 MONO_ADD_INS (bblock, ins);
3701 CHECK_STACK_OVF (1);
3703 NEW_ARGLOAD (cfg, ins, ip [1]);
3710 CHECK_STACK_OVF (1);
3712 NEW_ARGLOADA (cfg, ins, ip [1]);
3722 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
3723 handle_loaded_temps (cfg, bblock, stack_start, sp);
3725 if (ins->opcode == CEE_STOBJ) {
3726 NEW_ARGLOADA (cfg, ins, ip [1]);
3727 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3729 MONO_ADD_INS (bblock, ins);
3734 CHECK_STACK_OVF (1);
3735 CHECK_LOCAL (ip [1]);
3736 NEW_LOCLOAD (cfg, ins, ip [1]);
3743 CHECK_STACK_OVF (1);
3744 CHECK_LOCAL (ip [1]);
3745 NEW_LOCLOADA (cfg, ins, ip [1]);
3754 handle_loaded_temps (cfg, bblock, stack_start, sp);
3755 CHECK_LOCAL (ip [1]);
3756 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
3758 if (ins->opcode == CEE_STOBJ) {
3759 NEW_LOCLOADA (cfg, ins, ip [1]);
3760 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3762 MONO_ADD_INS (bblock, ins);
3767 CHECK_STACK_OVF (1);
3768 NEW_PCONST (cfg, ins, NULL);
3770 ins->type = STACK_OBJ;
3775 CHECK_STACK_OVF (1);
3776 NEW_ICONST (cfg, ins, -1);
3790 CHECK_STACK_OVF (1);
3791 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
3798 CHECK_STACK_OVF (1);
3800 NEW_ICONST (cfg, ins, *((signed char*)ip));
3807 CHECK_STACK_OVF (1);
3808 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
3815 CHECK_STACK_OVF (1);
3816 MONO_INST_NEW (cfg, ins, OP_I8CONST);
3818 ins->type = STACK_I8;
3820 ins->inst_l = (gint64)read64 (ip);
3826 /* we should really allocate this only late in the compilation process */
3827 mono_domain_lock (cfg->domain);
3828 f = mono_mempool_alloc (cfg->domain->mp, sizeof (float));
3829 mono_domain_unlock (cfg->domain);
3831 CHECK_STACK_OVF (1);
3832 MONO_INST_NEW (cfg, ins, OP_R4CONST);
3833 ins->type = STACK_R8;
3844 mono_domain_lock (cfg->domain);
3845 d = mono_mempool_alloc (cfg->domain->mp, sizeof (double));
3846 mono_domain_unlock (cfg->domain);
3848 CHECK_STACK_OVF (1);
3849 MONO_INST_NEW (cfg, ins, OP_R8CONST);
3850 ins->type = STACK_R8;
3860 MonoInst *temp, *store;
3862 CHECK_STACK_OVF (1);
3867 * small optimization: if the loaded value was from a local already,
3868 * just load it twice.
3870 if (ins->ssa_op == MONO_SSA_LOAD &&
3871 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
3873 MONO_INST_NEW (cfg, temp, 0);
3875 temp->cil_code = ip;
3878 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3879 temp->flags |= MONO_INST_IS_TEMP;
3880 temp->cil_code = ip;
3881 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3882 store->cil_code = ip;
3883 if (store->opcode == CEE_STOBJ) {
3884 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
3885 handle_stobj (cfg, bblock, store, sp [0], sp [0]->cil_code, store->klass, TRUE, FALSE);
3887 MONO_ADD_INS (bblock, store);
3889 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3892 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3902 MONO_INST_NEW (cfg, ins, CEE_POP);
3903 MONO_ADD_INS (bblock, ins);
3904 ins->cil_code = ip++;
3910 if (stack_start != sp)
3912 MONO_INST_NEW (cfg, ins, CEE_JMP);
3913 token = read32 (ip + 1);
3914 /* FIXME: check the signature matches */
3915 cmethod = mini_get_method (method, token, NULL, generic_context);
3920 if (mono_use_security_manager) {
3921 check_linkdemand (cfg, method, cmethod, bblock, ip);
3924 ins->inst_p0 = cmethod;
3925 MONO_ADD_INS (bblock, ins);
3927 start_new_bblock = 1;
3931 case CEE_CALLVIRT: {
3932 MonoInst *addr = NULL;
3933 MonoMethodSignature *fsig = NULL;
3934 int temp, array_rank = 0;
3935 int virtual = *ip == CEE_CALLVIRT;
3938 token = read32 (ip + 1);
3940 if (*ip == CEE_CALLI) {
3945 if (method->wrapper_type != MONO_WRAPPER_NONE)
3946 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
3948 fsig = mono_metadata_parse_signature (image, token);
3950 n = fsig->param_count + fsig->hasthis;
3952 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3953 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
3954 } else if (constrained_call) {
3955 cmethod = mono_get_method_constrained (image, token, constrained_call, generic_context);
3956 cmethod = mono_get_inflated_method (cmethod);
3958 cmethod = mini_get_method (method, token, NULL, generic_context);
3964 if (!virtual && (cmethod->flags & METHOD_ATTRIBUTE_ABSTRACT))
3965 /* MS.NET seems to silently convert this to a callvirt */
3968 if (!cmethod->klass->inited)
3969 mono_class_init (cmethod->klass);
3971 if (mono_method_signature (cmethod)->pinvoke) {
3972 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
3973 fsig = mono_method_signature (wrapper);
3974 } else if (constrained_call) {
3975 fsig = mono_method_signature (cmethod);
3977 fsig = mono_method_get_signature_full (cmethod, image, token, generic_context);
3980 n = fsig->param_count + fsig->hasthis;
3982 if (mono_use_security_manager) {
3983 check_linkdemand (cfg, method, cmethod, bblock, ip);
3986 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
3987 mini_class_is_system_array (cmethod->klass)) {
3988 array_rank = cmethod->klass->rank;
3991 if (cmethod->string_ctor)
3992 g_assert_not_reached ();
3997 mono_get_got_var (cfg);
3999 /* code in inssel.brg might transform a virtual call to a normal call */
4000 if (!(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
4001 ((cmethod->flags & METHOD_ATTRIBUTE_FINAL) &&
4002 cmethod->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK))
4003 mono_get_got_var (cfg);
4006 if (cmethod && cmethod->klass->generic_container) {
4013 //g_assert (!virtual || fsig->hasthis);
4017 if (constrained_call) {
4019 * We have the `constrained.' prefix opcode.
4021 if (constrained_call->valuetype && !cmethod->klass->valuetype) {
4024 * The type parameter is instantiated as a valuetype,
4025 * but that type doesn't override the method we're
4026 * calling, so we need to box `this'.
4027 * sp [0] is a pointer to the data: we need the value
4028 * in handle_box (), so load it here.
4030 MONO_INST_NEW (cfg, load, mono_type_to_ldind (&constrained_call->byval_arg));
4031 type_to_eval_stack_type (&constrained_call->byval_arg, load);
4032 load->cil_code = ip;
4033 load->inst_left = sp [0];
4034 sp [0] = handle_box (cfg, bblock, load, ip, constrained_call);
4035 } else if (!constrained_call->valuetype) {
4039 * The type parameter is instantiated as a reference
4040 * type. We have a managed pointer on the stack, so
4041 * we need to dereference it here.
4044 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
4046 ins->inst_i0 = sp [0];
4047 ins->type = STACK_OBJ;
4049 } else if (cmethod->klass->valuetype)
4051 constrained_call = NULL;
4054 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp)) {
4059 if (cmethod && virtual &&
4060 (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) &&
4061 !((cmethod->flags & METHOD_ATTRIBUTE_FINAL) &&
4062 cmethod->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK) &&
4063 mono_method_signature (cmethod)->generic_param_count) {
4064 MonoInst *this_temp, *store;
4065 MonoInst *iargs [3];
4067 g_assert (mono_method_signature (cmethod)->is_inflated);
4069 this_temp = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
4070 this_temp->cil_code = ip;
4071 NEW_TEMPSTORE (cfg, store, this_temp->inst_c0, sp [0]);
4073 store->cil_code = ip;
4074 MONO_ADD_INS (bblock, store);
4076 NEW_TEMPLOAD (cfg, iargs [0], this_temp->inst_c0);
4077 NEW_PCONST (cfg, iargs [1], cmethod);
4078 NEW_PCONST (cfg, iargs [2], ((MonoMethodInflated *) cmethod)->context);
4079 temp = mono_emit_jit_icall (cfg, bblock, helper_compile_generic_method, iargs, ip);
4081 NEW_TEMPLOAD (cfg, addr, temp);
4082 NEW_TEMPLOAD (cfg, sp [0], this_temp->inst_c0);
4084 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
4085 NEW_TEMPLOAD (cfg, *sp, temp);
4093 if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL) &&
4094 (mono_metadata_signature_equal (mono_method_signature (method), mono_method_signature (cmethod)))) {
4096 /* FIXME: This assumes the two methods has the same number and type of arguments */
4097 for (i = 0; i < n; ++i) {
4098 /* Check if argument is the same */
4099 NEW_ARGLOAD (cfg, ins, i);
4100 if ((ins->opcode == sp [i]->opcode) && (ins->inst_i0 == sp [i]->inst_i0))
4103 /* Prevent argument from being register allocated */
4104 arg_array [i]->flags |= MONO_INST_VOLATILE;
4105 NEW_ARGSTORE (cfg, ins, i, sp [i]);
4107 if (ins->opcode == CEE_STOBJ) {
4108 NEW_ARGLOADA (cfg, ins, i);
4109 handle_stobj (cfg, bblock, ins, sp [i], sp [i]->cil_code, ins->klass, FALSE, FALSE);
4112 MONO_ADD_INS (bblock, ins);
4114 MONO_INST_NEW (cfg, ins, CEE_JMP);
4116 ins->inst_p0 = cmethod;
4117 ins->inst_p1 = arg_array [0];
4118 MONO_ADD_INS (bblock, ins);
4119 link_bblock (cfg, bblock, end_bblock);
4120 start_new_bblock = 1;
4121 /* skip CEE_RET as well */
4126 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_inst_for_method (cfg, cmethod, fsig, sp))) {
4129 if (MONO_TYPE_IS_VOID (fsig->ret)) {
4130 MONO_ADD_INS (bblock, ins);
4132 type_to_eval_stack_type (fsig->ret, ins);
4141 handle_loaded_temps (cfg, bblock, stack_start, sp);
4143 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
4144 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
4145 mono_method_check_inlining (cfg, cmethod) &&
4146 !g_list_find (dont_inline, cmethod)) {
4148 MonoBasicBlock *ebblock;
4149 gboolean allways = FALSE;
4151 if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
4152 (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
4153 cmethod = mono_marshal_get_native_wrapper (cmethod);
4157 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, allways))) {
4161 GET_BBLOCK (cfg, bbhash, bblock, ip);
4162 ebblock->next_bb = bblock;
4163 link_bblock (cfg, ebblock, bblock);
4165 if (!MONO_TYPE_IS_VOID (fsig->ret))
4168 /* indicates start of a new block, and triggers a load of all
4169 stack arguments at bb boundarie */
4172 inline_costs += costs;
4177 inline_costs += 10 * num_calls++;
4179 /* tail recursion elimination */
4180 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == method && ip [5] == CEE_RET) {
4181 gboolean has_vtargs = FALSE;
4184 /* keep it simple */
4185 for (i = fsig->param_count - 1; i >= 0; i--) {
4186 if (MONO_TYPE_ISSTRUCT (mono_method_signature (cmethod)->params [i]))
4191 for (i = 0; i < n; ++i) {
4192 NEW_ARGSTORE (cfg, ins, i, sp [i]);
4194 MONO_ADD_INS (bblock, ins);
4196 MONO_INST_NEW (cfg, ins, CEE_BR);
4198 MONO_ADD_INS (bblock, ins);
4199 tblock = start_bblock->out_bb [0];
4200 link_bblock (cfg, bblock, tblock);
4201 ins->inst_target_bb = tblock;
4202 start_new_bblock = 1;
4204 /* skip the CEE_RET, too */
4205 if (ip_in_bb (cfg, bblock, ip + 5))
4214 if (*ip == CEE_CALLI) {
4216 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
4217 NEW_TEMPLOAD (cfg, *sp, temp);
4221 } else if (array_rank) {
4224 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
4225 if (sp [fsig->param_count]->type == STACK_OBJ) {
4226 MonoInst *iargs [2];
4227 MonoInst *array, *to_store, *store;
4229 handle_loaded_temps (cfg, bblock, stack_start, sp);
4231 array = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
4232 NEW_TEMPSTORE (cfg, store, array->inst_c0, sp [0]);
4233 store->cil_code = ip;
4234 MONO_ADD_INS (bblock, store);
4235 NEW_TEMPLOAD (cfg, iargs [0], array->inst_c0);
4237 to_store = mono_compile_create_var (cfg, type_from_stack_type (sp [fsig->param_count]), OP_LOCAL);
4238 NEW_TEMPSTORE (cfg, store, to_store->inst_c0, sp [fsig->param_count]);
4239 store->cil_code = ip;
4240 MONO_ADD_INS (bblock, store);
4241 NEW_TEMPLOAD (cfg, iargs [1], to_store->inst_c0);
4244 * We first save the args for the call so that the args are copied to the stack
4245 * and a new instruction tree for them is created. If we don't do this,
4246 * the same MonoInst is added to two different trees and this is not
4249 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref_check, iargs, ip);
4251 NEW_TEMPLOAD (cfg, sp [0], array->inst_c0);
4252 NEW_TEMPLOAD (cfg, sp [fsig->param_count], to_store->inst_c0);
4255 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
4256 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
4258 if (ins->opcode == CEE_STOBJ) {
4259 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
4261 MONO_ADD_INS (bblock, ins);
4264 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
4265 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
4266 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
4270 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
4271 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
4274 g_assert_not_reached ();
4278 if (ip_in_bb (cfg, bblock, ip + 5)
4279 && (!MONO_TYPE_ISSTRUCT (fsig->ret))
4280 && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)
4281 && (CODE_IS_STLOC (ip + 5) || ip [5] == CEE_POP || ip [5] == CEE_RET)) {
4282 /* no need to spill */
4283 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
4286 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) {
4287 NEW_TEMPLOAD (cfg, *sp, temp);
4297 if (cfg->method != method) {
4298 /* return from inlined methode */
4303 //g_assert (returnvar != -1);
4304 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
4305 store->cil_code = sp [0]->cil_code;
4306 if (store->opcode == CEE_STOBJ) {
4307 g_assert_not_reached ();
4308 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
4309 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
4311 MONO_ADD_INS (bblock, store);
4315 g_assert (!return_var);
4318 MONO_INST_NEW (cfg, ins, CEE_NOP);
4319 ins->opcode = mono_type_to_stind (mono_method_signature (method)->ret);
4320 if (ins->opcode == CEE_STOBJ) {
4321 NEW_RETLOADA (cfg, ins);
4322 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4324 ins->opcode = OP_SETRET;
4326 ins->inst_i0 = *sp;;
4327 ins->inst_i1 = NULL;
4328 MONO_ADD_INS (bblock, ins);
4332 if (sp != stack_start)
4334 MONO_INST_NEW (cfg, ins, CEE_BR);
4335 ins->cil_code = ip++;
4336 ins->inst_target_bb = end_bblock;
4337 MONO_ADD_INS (bblock, ins);
4338 link_bblock (cfg, bblock, end_bblock);
4339 start_new_bblock = 1;
4343 MONO_INST_NEW (cfg, ins, CEE_BR);
4344 ins->cil_code = ip++;
4345 MONO_ADD_INS (bblock, ins);
4346 target = ip + 1 + (signed char)(*ip);
4348 GET_BBLOCK (cfg, bbhash, tblock, target);
4349 link_bblock (cfg, bblock, tblock);
4350 CHECK_BBLOCK (target, ip, tblock);
4351 ins->inst_target_bb = tblock;
4352 if (sp != stack_start) {
4353 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4356 start_new_bblock = 1;
4363 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
4364 ins->cil_code = ip++;
4365 target = ip + 1 + *(signed char*)ip;
4367 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
4368 if (sp != stack_start) {
4369 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4386 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
4387 ins->cil_code = ip++;
4388 target = ip + 1 + *(signed char*)ip;
4391 if (sp != stack_start) {
4392 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4399 MONO_INST_NEW (cfg, ins, CEE_BR);
4400 ins->cil_code = ip++;
4401 MONO_ADD_INS (bblock, ins);
4402 target = ip + 4 + (gint32)read32(ip);
4404 GET_BBLOCK (cfg, bbhash, tblock, target);
4405 link_bblock (cfg, bblock, tblock);
4406 CHECK_BBLOCK (target, ip, tblock);
4407 ins->inst_target_bb = tblock;
4408 if (sp != stack_start) {
4409 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4412 start_new_bblock = 1;
4419 MONO_INST_NEW (cfg, ins, *ip);
4420 ins->cil_code = ip++;
4421 target = ip + 4 + (gint32)read32(ip);
4423 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
4424 if (sp != stack_start) {
4425 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4442 MONO_INST_NEW (cfg, ins, *ip);
4443 ins->cil_code = ip++;
4444 target = ip + 4 + (gint32)read32(ip);
4447 if (sp != stack_start) {
4448 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4456 n = read32 (ip + 1);
4457 MONO_INST_NEW (cfg, ins, *ip);
4459 ins->inst_left = *sp;
4460 if ((ins->inst_left->type != STACK_I4) && (ins->inst_left->type != STACK_PTR))
4464 CHECK_OPSIZE (n * sizeof (guint32));
4465 target = ip + n * sizeof (guint32);
4466 MONO_ADD_INS (bblock, ins);
4467 GET_BBLOCK (cfg, bbhash, tblock, target);
4468 link_bblock (cfg, bblock, tblock);
4469 ins->klass = GUINT_TO_POINTER (n);
4470 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
4471 ins->inst_many_bb [n] = tblock;
4473 for (i = 0; i < n; ++i) {
4474 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
4475 link_bblock (cfg, bblock, tblock);
4476 ins->inst_many_bb [i] = tblock;
4479 if (sp != stack_start) {
4480 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4483 /* Needed by the code generated in inssel.brg */
4484 mono_get_got_var (cfg);
4499 MONO_INST_NEW (cfg, ins, *ip);
4504 ins->type = ldind_type [*ip - CEE_LDIND_I1];
4505 ins->flags |= ins_flag;
4517 MONO_INST_NEW (cfg, ins, *ip);
4518 ins->cil_code = ip++;
4520 handle_loaded_temps (cfg, bblock, stack_start, sp);
4521 MONO_ADD_INS (bblock, ins);
4522 ins->inst_i0 = sp [0];
4523 ins->inst_i1 = sp [1];
4524 ins->flags |= ins_flag;
4532 #ifdef MONO_ARCH_NO_EMULATE_MUL_IMM
4533 /* FIXME: This breaks with ssapre (mono -O=ssapre loader.exe) */
4534 if ((ins->inst_right->opcode == OP_ICONST) && !(cfg->opt & MONO_OPT_SSAPRE)) {
4535 switch (ins->opcode) {
4537 ins->opcode = OP_IMUL_IMM;
4538 ins->inst_imm = ins->inst_right->inst_c0;
4541 ins->opcode = OP_LMUL_IMM;
4542 ins->inst_imm = ins->inst_right->inst_c0;
4545 g_assert_not_reached ();
4550 if (mono_find_jit_opcode_emulation (ins->opcode)) {
4552 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
4553 mono_get_got_var (cfg);
4571 /* special case that gives a nice speedup and happens to workaorund a ppc jit but (for the release)
4572 * later apply the speedup to the left shift as well
4575 if ((ins->opcode == OP_LSHR_UN) && (ins->type == STACK_I8)
4576 && (ins->inst_right->opcode == OP_ICONST) && (ins->inst_right->inst_c0 == 32)) {
4577 ins->opcode = OP_LONG_SHRUN_32;
4578 /*g_print ("applied long shr speedup to %s\n", cfg->method->name);*/
4582 if (mono_find_jit_opcode_emulation (ins->opcode)) {
4584 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
4585 mono_get_got_var (cfg);
4599 case CEE_CONV_OVF_I8:
4600 case CEE_CONV_OVF_U8:
4604 if (mono_find_jit_opcode_emulation (ins->opcode)) {
4606 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
4607 mono_get_got_var (cfg);
4611 case CEE_CONV_OVF_I4:
4612 case CEE_CONV_OVF_I1:
4613 case CEE_CONV_OVF_I2:
4614 case CEE_CONV_OVF_I:
4615 case CEE_CONV_OVF_U:
4618 if (sp [-1]->type == STACK_R8) {
4619 ADD_UNOP (CEE_CONV_OVF_I8);
4627 case CEE_CONV_OVF_U1:
4628 case CEE_CONV_OVF_U2:
4629 case CEE_CONV_OVF_U4:
4632 if (sp [-1]->type == STACK_R8) {
4633 ADD_UNOP (CEE_CONV_OVF_U8);
4641 case CEE_CONV_OVF_I1_UN:
4642 case CEE_CONV_OVF_I2_UN:
4643 case CEE_CONV_OVF_I4_UN:
4644 case CEE_CONV_OVF_I8_UN:
4645 case CEE_CONV_OVF_U1_UN:
4646 case CEE_CONV_OVF_U2_UN:
4647 case CEE_CONV_OVF_U4_UN:
4648 case CEE_CONV_OVF_U8_UN:
4649 case CEE_CONV_OVF_I_UN:
4650 case CEE_CONV_OVF_U_UN:
4658 token = read32 (ip + 1);
4659 klass = mini_get_class (method, token, generic_context);
4663 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4664 MonoInst *store, *load;
4665 MONO_INST_NEW (cfg, load, CEE_LDIND_REF);
4666 load->cil_code = ip;
4667 load->inst_i0 = sp [1];
4668 load->type = STACK_OBJ;
4669 load->flags |= ins_flag;
4670 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
4671 store->cil_code = ip;
4672 handle_loaded_temps (cfg, bblock, stack_start, sp);
4673 MONO_ADD_INS (bblock, store);
4674 store->inst_i0 = sp [0];
4675 store->inst_i1 = load;
4676 store->flags |= ins_flag;
4678 n = mono_class_value_size (klass, NULL);
4679 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
4681 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
4682 copy->inst_left = sp [0];
4683 copy->inst_right = sp [1];
4684 copy->cil_code = ip;
4686 MONO_ADD_INS (bblock, copy);
4688 MonoMethod *memcpy_method = get_memcpy_method ();
4689 MonoInst *iargs [3];
4692 NEW_ICONST (cfg, iargs [2], n);
4693 iargs [2]->cil_code = ip;
4695 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
4702 MonoInst *iargs [3];
4708 token = read32 (ip + 1);
4709 klass = mini_get_class (method, token, generic_context);
4712 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4713 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
4715 ins->inst_i0 = sp [0];
4716 ins->type = STACK_OBJ;
4717 ins->flags |= ins_flag;
4724 /* Optimize the common ldobj+stloc combination */
4734 loc_index = ip [5] - CEE_STLOC_0;
4741 if ((loc_index != -1) && ip_in_bb (cfg, bblock, ip + 5)) {
4742 CHECK_LOCAL (loc_index);
4743 NEW_LOCSTORE (cfg, ins, loc_index, *sp);
4745 if (ins->opcode == CEE_STOBJ) {
4746 handle_loaded_temps (cfg, bblock, stack_start, sp);
4748 g_assert (ins->opcode == CEE_STOBJ);
4749 NEW_LOCLOADA (cfg, ins, loc_index);
4750 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4757 n = mono_class_value_size (klass, NULL);
4758 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
4759 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
4760 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
4762 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
4763 copy->inst_left = iargs [0];
4764 copy->inst_right = *sp;
4765 copy->cil_code = ip;
4767 MONO_ADD_INS (bblock, copy);
4769 MonoMethod *memcpy_method = get_memcpy_method ();
4771 NEW_ICONST (cfg, iargs [2], n);
4772 iargs [2]->cil_code = ip;
4774 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
4776 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
4784 CHECK_STACK_OVF (1);
4786 n = read32 (ip + 1);
4788 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
4789 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, n));
4791 ins->type = STACK_OBJ;
4794 else if (method->wrapper_type != MONO_WRAPPER_NONE) {
4796 MonoInst *iargs [1];
4798 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
4799 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
4800 NEW_TEMPLOAD (cfg, *sp, temp);
4804 if (cfg->opt & MONO_OPT_SHARED) {
4806 MonoInst *iargs [3];
4807 MonoInst* domain_var;
4809 if (cfg->compile_aot) {
4810 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, GINT_TO_POINTER (n));
4812 /* avoid depending on undefined C behavior in sequence points */
4813 domain_var = mono_get_domainvar (cfg);
4814 NEW_TEMPLOAD (cfg, iargs [0], domain_var->inst_c0);
4815 NEW_IMAGECONST (cfg, iargs [1], image);
4816 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
4817 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
4818 NEW_TEMPLOAD (cfg, *sp, temp);
4819 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
4821 if (bblock->out_of_line) {
4822 MonoInst *iargs [2];
4825 /* Avoid creating the string object */
4826 NEW_IMAGECONST (cfg, iargs [0], image);
4827 NEW_ICONST (cfg, iargs [1], mono_metadata_token_index (n));
4828 temp = mono_emit_jit_icall (cfg, bblock, helper_ldstr, iargs, ip);
4829 NEW_TEMPLOAD (cfg, *sp, temp);
4832 if (cfg->compile_aot) {
4833 NEW_LDSTRCONST (cfg, ins, image, n);
4837 NEW_PCONST (cfg, ins, NULL);
4839 ins->type = STACK_OBJ;
4840 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
4850 MonoInst *iargs [2];
4851 MonoMethodSignature *fsig;
4855 token = read32 (ip + 1);
4856 cmethod = mini_get_method (method, token, NULL, generic_context);
4859 fsig = mono_method_get_signature (cmethod, image, token);
4861 mono_class_init (cmethod->klass);
4863 if (mono_use_security_manager) {
4864 check_linkdemand (cfg, method, cmethod, bblock, ip);
4867 n = fsig->param_count;
4870 /* move the args to allow room for 'this' in the first position */
4876 handle_loaded_temps (cfg, bblock, stack_start, sp);
4878 if (mini_class_is_system_array (cmethod->klass)) {
4879 NEW_METHODCONST (cfg, *sp, cmethod);
4880 temp = handle_array_new (cfg, bblock, fsig->param_count, sp, ip);
4881 } else if (cmethod->string_ctor) {
4882 /* we simply pass a null pointer */
4883 NEW_PCONST (cfg, *sp, NULL);
4884 /* now call the string ctor */
4885 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
4887 MonoInst* callvirt_this_arg = NULL;
4889 if (cmethod->klass->valuetype) {
4890 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
4891 temp = iargs [0]->inst_c0;
4893 NEW_TEMPLOADA (cfg, *sp, temp);
4895 handle_initobj (cfg, bblock, *sp, NULL, cmethod->klass, stack_start, sp);
4897 NEW_TEMPLOADA (cfg, *sp, temp);
4900 * The code generated by mini_emit_virtual_call () expects
4901 * iargs [0] to be a boxed instance, but luckily the vcall
4902 * will be transformed into a normal call there. The AOT
4903 * case needs an already allocate got_var.
4905 mono_get_got_var (cfg);
4907 temp = handle_alloc (cfg, bblock, cmethod->klass, FALSE, ip);
4908 NEW_TEMPLOAD (cfg, *sp, temp);
4911 /* Avoid virtual calls to ctors if possible */
4912 if (cmethod->klass->marshalbyref)
4913 callvirt_this_arg = sp [0];
4915 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
4916 mono_method_check_inlining (cfg, cmethod) &&
4917 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
4918 !g_list_find (dont_inline, cmethod)) {
4920 MonoBasicBlock *ebblock;
4921 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, FALSE))) {
4926 GET_BBLOCK (cfg, bbhash, bblock, ip);
4927 ebblock->next_bb = bblock;
4928 link_bblock (cfg, ebblock, bblock);
4930 NEW_TEMPLOAD (cfg, *sp, temp);
4933 /* indicates start of a new block, and triggers a load
4934 of all stack arguments at bb boundarie */
4937 inline_costs += costs;
4941 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, callvirt_this_arg);
4944 /* now call the actual ctor */
4945 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, callvirt_this_arg);
4949 NEW_TEMPLOAD (cfg, *sp, temp);
4960 token = read32 (ip + 1);
4961 klass = mini_get_class (method, token, generic_context);
4965 /* Needed by the code generated in inssel.brg */
4966 mono_get_got_var (cfg);
4968 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4970 MonoMethod *mono_isinst;
4971 MonoInst *iargs [1];
4972 MonoBasicBlock *ebblock;
4976 mono_isinst = mono_marshal_get_isinst (klass);
4979 costs = inline_method (cfg, mono_isinst, mono_method_signature (mono_isinst), bblock,
4980 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4982 g_assert (costs > 0);
4987 GET_BBLOCK (cfg, bbhash, bblock, ip);
4988 ebblock->next_bb = bblock;
4989 link_bblock (cfg, ebblock, bblock);
4991 temp = iargs [0]->inst_i0->inst_c0;
4992 NEW_TEMPLOAD (cfg, *sp, temp);
4996 inline_costs += costs;
5000 MONO_INST_NEW (cfg, ins, *ip);
5001 ins->type = STACK_OBJ;
5002 ins->inst_left = *sp;
5003 ins->inst_newa_class = klass;
5005 *sp++ = emit_tree (cfg, bblock, ins, ip + 5);
5009 case CEE_UNBOX_ANY: {
5010 MonoInst *add, *vtoffset;
5011 MonoInst *iargs [3];
5016 token = read32 (ip + 1);
5017 klass = mini_get_class (method, token, generic_context);
5021 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5023 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
5024 MonoMethod *mono_castclass;
5025 MonoInst *iargs [1];
5026 MonoBasicBlock *ebblock;
5030 mono_castclass = mono_marshal_get_castclass (klass);
5033 costs = inline_method (cfg, mono_castclass, mono_method_signature (mono_castclass), bblock,
5034 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
5036 g_assert (costs > 0);
5041 GET_BBLOCK (cfg, bbhash, bblock, ip);
5042 ebblock->next_bb = bblock;
5043 link_bblock (cfg, ebblock, bblock);
5045 temp = iargs [0]->inst_i0->inst_c0;
5046 NEW_TEMPLOAD (cfg, *sp, temp);
5050 inline_costs += costs;
5053 MONO_INST_NEW (cfg, ins, CEE_CASTCLASS);
5054 ins->type = STACK_OBJ;
5055 ins->inst_left = *sp;
5057 ins->inst_newa_class = klass;
5065 if (mono_class_is_nullable (klass)) {
5066 int v = handle_unbox_nullable (cfg, bblock, *sp, ip, klass);
5067 NEW_TEMPLOAD (cfg, *sp, v);
5073 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
5074 ins->type = STACK_OBJ;
5075 ins->inst_left = *sp;
5077 ins->inst_newa_class = klass;
5080 MONO_INST_NEW (cfg, add, OP_PADD);
5081 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
5082 add->inst_left = ins;
5083 add->inst_right = vtoffset;
5084 add->type = STACK_MP;
5088 n = mono_class_value_size (klass, NULL);
5089 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
5090 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
5091 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
5093 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
5094 copy->inst_left = iargs [0];
5095 copy->inst_right = *sp;
5096 copy->cil_code = ip;
5098 MONO_ADD_INS (bblock, copy);
5100 MonoMethod *memcpy_method = get_memcpy_method ();
5102 NEW_ICONST (cfg, iargs [2], n);
5103 iargs [2]->cil_code = ip;
5105 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
5107 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
5113 MonoInst *add, *vtoffset;
5118 token = read32 (ip + 1);
5119 klass = mini_get_class (method, token, generic_context);
5123 if (mono_class_is_nullable (klass)) {
5124 int v = handle_unbox_nullable (cfg, bblock, *sp, ip, klass);
5125 NEW_TEMPLOAD (cfg, *sp, v);
5131 /* Needed by the code generated in inssel.brg */
5132 mono_get_got_var (cfg);
5134 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
5135 ins->type = STACK_OBJ;
5136 ins->inst_left = *sp;
5138 ins->inst_newa_class = klass;
5141 MONO_INST_NEW (cfg, add, OP_PADD);
5142 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
5143 add->inst_left = ins;
5144 add->inst_right = vtoffset;
5145 add->type = STACK_MP;
5155 token = read32 (ip + 1);
5156 klass = mini_get_class (method, token, generic_context);
5160 /* Needed by the code generated in inssel.brg */
5161 mono_get_got_var (cfg);
5163 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
5165 MonoMethod *mono_castclass;
5166 MonoInst *iargs [1];
5167 MonoBasicBlock *ebblock;
5171 mono_castclass = mono_marshal_get_castclass (klass);
5174 costs = inline_method (cfg, mono_castclass, mono_method_signature (mono_castclass), bblock,
5175 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
5177 g_assert (costs > 0);
5182 GET_BBLOCK (cfg, bbhash, bblock, ip);
5183 ebblock->next_bb = bblock;
5184 link_bblock (cfg, ebblock, bblock);
5186 temp = iargs [0]->inst_i0->inst_c0;
5187 NEW_TEMPLOAD (cfg, *sp, temp);
5191 inline_costs += costs;
5194 MONO_INST_NEW (cfg, ins, *ip);
5195 ins->type = STACK_OBJ;
5196 ins->inst_left = *sp;
5198 ins->inst_newa_class = klass;
5200 *sp++ = emit_tree (cfg, bblock, ins, ip + 5);
5206 MONO_INST_NEW (cfg, ins, *ip);
5208 ins->inst_left = *sp;
5209 ins->cil_code = ip++;
5210 bblock->out_of_line = TRUE;
5211 MONO_ADD_INS (bblock, ins);
5212 MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
5213 ins->cil_code = ip - 1;
5214 MONO_ADD_INS (bblock, ins);
5217 link_bblock (cfg, bblock, end_bblock);
5218 start_new_bblock = 1;
5219 mono_get_got_var (cfg);
5224 MonoInst *offset_ins;
5225 MonoClassField *field;
5226 MonoBasicBlock *ebblock;
5230 if (*ip == CEE_STFLD) {
5237 // FIXME: enable this test later.
5238 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
5241 token = read32 (ip + 1);
5242 field = mono_field_from_token (image, token, &klass, generic_context);
5245 mono_class_init (klass);
5247 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
5248 /* FIXME: mark instructions for use in SSA */
5249 if (*ip == CEE_STFLD) {
5250 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound || klass == mono_defaults.marshalbyrefobject_class) {
5251 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
5252 MonoInst *iargs [5];
5255 NEW_CLASSCONST (cfg, iargs [1], klass);
5256 NEW_FIELDCONST (cfg, iargs [2], field);
5257 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
5261 if (cfg->opt & MONO_OPT_INLINE) {
5262 costs = inline_method (cfg, stfld_wrapper, mono_method_signature (stfld_wrapper), bblock,
5263 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
5264 g_assert (costs > 0);
5269 GET_BBLOCK (cfg, bbhash, bblock, ip);
5270 ebblock->next_bb = bblock;
5271 link_bblock (cfg, ebblock, bblock);
5273 /* indicates start of a new block, and triggers a load
5274 of all stack arguments at bb boundarie */
5277 inline_costs += costs;
5280 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, mono_method_signature (stfld_wrapper), iargs, ip, NULL);
5284 NEW_ICONST (cfg, offset_ins, foffset);
5285 MONO_INST_NEW (cfg, ins, OP_PADD);
5287 ins->inst_left = *sp;
5288 ins->inst_right = offset_ins;
5289 ins->type = STACK_MP;
5291 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
5292 store->cil_code = ip;
5293 store->inst_left = ins;
5294 store->inst_right = sp [1];
5295 handle_loaded_temps (cfg, bblock, stack_start, sp);
5296 store->flags |= ins_flag;
5298 if (store->opcode == CEE_STOBJ) {
5299 handle_stobj (cfg, bblock, ins, sp [1], ip,
5300 mono_class_from_mono_type (field->type), FALSE, FALSE);
5302 MONO_ADD_INS (bblock, store);
5305 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound || klass == mono_defaults.marshalbyrefobject_class) {
5306 MonoMethod *wrapper = (*ip == CEE_LDFLDA) ? mono_marshal_get_ldflda_wrapper (field->type) : mono_marshal_get_ldfld_wrapper (field->type);
5307 MonoInst *iargs [4];
5311 NEW_CLASSCONST (cfg, iargs [1], klass);
5312 NEW_FIELDCONST (cfg, iargs [2], field);
5313 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
5314 if ((cfg->opt & MONO_OPT_INLINE) && !MONO_TYPE_ISSTRUCT (mono_method_signature (wrapper)->ret)) {
5315 costs = inline_method (cfg, wrapper, mono_method_signature (wrapper), bblock,
5316 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
5317 g_assert (costs > 0);
5322 GET_BBLOCK (cfg, bbhash, bblock, ip);
5323 ebblock->next_bb = bblock;
5324 link_bblock (cfg, ebblock, bblock);
5326 temp = iargs [0]->inst_i0->inst_c0;
5328 NEW_TEMPLOAD (cfg, *sp, temp);
5331 /* indicates start of a new block, and triggers a load of
5332 all stack arguments at bb boundarie */
5335 inline_costs += costs;
5338 temp = mono_emit_method_call_spilled (cfg, bblock, wrapper, mono_method_signature (wrapper), iargs, ip, NULL);
5339 NEW_TEMPLOAD (cfg, *sp, temp);
5343 NEW_ICONST (cfg, offset_ins, foffset);
5344 MONO_INST_NEW (cfg, ins, OP_PADD);
5346 ins->inst_left = *sp;
5347 ins->inst_right = offset_ins;
5348 ins->type = STACK_MP;
5350 if (*ip == CEE_LDFLDA) {
5354 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
5355 type_to_eval_stack_type (field->type, load);
5356 load->cil_code = ip;
5357 load->inst_left = ins;
5358 load->flags |= ins_flag;
5370 MonoClassField *field;
5371 gpointer addr = NULL;
5374 token = read32 (ip + 1);
5376 field = mono_field_from_token (image, token, &klass, generic_context);
5379 mono_class_init (klass);
5381 g_assert (!(field->type->attrs & FIELD_ATTRIBUTE_LITERAL));
5383 if ((*ip) == CEE_STSFLD)
5384 handle_loaded_temps (cfg, bblock, stack_start, sp);
5386 /* The special_static_fields field is init'd in mono_class_vtable, so it needs
5387 * to be called here.
5389 if (!(cfg->opt & MONO_OPT_SHARED))
5390 mono_class_vtable (cfg->domain, klass);
5391 mono_domain_lock (cfg->domain);
5392 if (cfg->domain->special_static_fields)
5393 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
5394 mono_domain_unlock (cfg->domain);
5396 if ((cfg->opt & MONO_OPT_SHARED) || (cfg->compile_aot && addr)) {
5398 MonoInst *iargs [2];
5399 MonoInst *domain_var;
5401 g_assert (field->parent);
5402 /* avoid depending on undefined C behavior in sequence points */
5403 domain_var = mono_get_domainvar (cfg);
5404 NEW_TEMPLOAD (cfg, iargs [0], domain_var->inst_c0);
5405 NEW_FIELDCONST (cfg, iargs [1], field);
5406 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
5407 NEW_TEMPLOAD (cfg, ins, temp);
5410 vtable = mono_class_vtable (cfg->domain, klass);
5412 if (mini_field_access_needs_cctor_run (cfg, method, vtable) && !(g_slist_find (class_inits, vtable))) {
5413 guint8 *tramp = mono_create_class_init_trampoline (vtable);
5414 mono_emit_native_call (cfg, bblock, tramp,
5415 helper_sig_class_init_trampoline,
5416 NULL, ip, FALSE, FALSE);
5417 if (cfg->verbose_level > 2)
5418 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
5419 class_inits = g_slist_prepend (class_inits, vtable);
5421 if (cfg->run_cctors)
5422 mono_runtime_class_init (vtable);
5424 addr = (char*)vtable->data + field->offset;
5426 if (cfg->compile_aot)
5427 NEW_SFLDACONST (cfg, ins, field);
5429 NEW_PCONST (cfg, ins, addr);
5433 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
5434 * This could be later optimized to do just a couple of
5435 * memory dereferences with constant offsets.
5438 MonoInst *iargs [1];
5439 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
5440 temp = mono_emit_jit_icall (cfg, bblock, mono_get_special_static_data, iargs, ip);
5441 NEW_TEMPLOAD (cfg, ins, temp);
5445 /* FIXME: mark instructions for use in SSA */
5446 if (*ip == CEE_LDSFLDA) {
5448 } else if (*ip == CEE_STSFLD) {
5452 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
5453 store->cil_code = ip;
5454 store->inst_left = ins;
5455 store->inst_right = sp [0];
5456 store->flags |= ins_flag;
5459 if (store->opcode == CEE_STOBJ) {
5460 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
5462 MONO_ADD_INS (bblock, store);
5464 gboolean is_const = FALSE;
5465 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
5466 if (!((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) &&
5467 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
5468 gpointer addr = (char*)vtable->data + field->offset;
5469 int ro_type = field->type->type;
5470 if (ro_type == MONO_TYPE_VALUETYPE && field->type->data.klass->enumtype) {
5471 ro_type = field->type->data.klass->enum_basetype->type;
5473 /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
5476 case MONO_TYPE_BOOLEAN:
5478 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
5482 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
5485 case MONO_TYPE_CHAR:
5487 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
5491 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
5496 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
5500 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
5505 case MONO_TYPE_STRING:
5506 case MONO_TYPE_OBJECT:
5507 case MONO_TYPE_CLASS:
5508 case MONO_TYPE_SZARRAY:
5510 case MONO_TYPE_FNPTR:
5511 case MONO_TYPE_ARRAY:
5512 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
5513 type_to_eval_stack_type (field->type, *sp);
5518 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
5519 sp [0]->type = STACK_I8;
5520 sp [0]->inst_l = *((gint64 *)addr);
5525 case MONO_TYPE_VALUETYPE:
5534 CHECK_STACK_OVF (1);
5535 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
5536 type_to_eval_stack_type (field->type, load);
5537 load->cil_code = ip;
5538 load->inst_left = ins;
5540 load->flags |= ins_flag;
5542 /* fixme: dont see the problem why this does not work */
5543 //cfg->disable_aot = TRUE;
5553 token = read32 (ip + 1);
5554 klass = mini_get_class (method, token, generic_context);
5557 n = mono_type_to_stind (&klass->byval_arg);
5558 if (n == CEE_STOBJ) {
5559 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
5561 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
5563 MONO_INST_NEW (cfg, store, n);
5564 store->cil_code = ip;
5565 store->inst_left = sp [0];
5566 store->inst_right = sp [1];
5567 store->flags |= ins_flag;
5568 MONO_ADD_INS (bblock, store);
5580 token = read32 (ip + 1);
5581 klass = mini_get_class (method, token, generic_context);
5585 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5590 *sp++ = handle_box (cfg, bblock, val, ip, klass);
5597 MONO_INST_NEW (cfg, ins, *ip);
5602 token = read32 (ip + 1);
5604 /* allocate the domainvar - becaus this is used in decompose_foreach */
5605 if (cfg->opt & MONO_OPT_SHARED) {
5606 mono_get_domainvar (cfg);
5607 /* LAME-IR: Mark it as used since otherwise it will be optimized away */
5608 cfg->domainvar->flags |= MONO_INST_VOLATILE;
5612 mono_get_got_var (cfg);
5614 klass = mini_get_class (method, token, generic_context);
5617 ins->inst_newa_class = klass;
5618 ins->inst_newa_len = *sp;
5619 ins->type = STACK_OBJ;
5623 * we store the object so calls to create the array are not interleaved
5624 * with the arguments of other calls.
5627 MonoInst *store, *temp, *load;
5629 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
5630 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
5631 store->cil_code = ins->cil_code;
5632 MONO_ADD_INS (bblock, store);
5633 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
5634 load->cil_code = ins->cil_code;
5641 MONO_INST_NEW (cfg, ins, *ip);
5642 ins->cil_code = ip++;
5644 ins->inst_left = *sp;
5645 ins->type = STACK_PTR;
5653 klass = mini_get_class (method, read32 (ip + 1), generic_context);
5656 /* we need to make sure that this array is exactly the type it needs
5657 * to be for correctness. the wrappers are lax with their usage
5658 * so we need to ignore them here
5660 if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE) {
5662 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
5663 check->cil_code = ip;
5664 check->klass = klass;
5665 check->inst_left = sp [0];
5666 check->type = STACK_OBJ;
5670 mono_class_init (klass);
5671 NEW_LDELEMA (cfg, ins, sp, klass);
5676 case CEE_LDELEM_ANY: {
5681 token = read32 (ip + 1);
5682 klass = mono_class_get_full (image, token, generic_context);
5685 mono_class_init (klass);
5686 NEW_LDELEMA (cfg, load, sp, klass);
5687 load->cil_code = ip;
5688 MONO_INST_NEW (cfg, ins, mono_type_to_ldind (&klass->byval_arg));
5690 ins->inst_left = load;
5692 type_to_eval_stack_type (&klass->byval_arg, ins);
5706 case CEE_LDELEM_REF: {
5710 * ldind.x (ldelema (array, index))
5711 * ldelema does the bounds check
5715 klass = array_access_to_klass (*ip);
5716 NEW_LDELEMA (cfg, load, sp, klass);
5717 load->cil_code = ip;
5718 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
5720 ins->inst_left = load;
5722 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
5732 case CEE_STELEM_R8: {
5736 * stind.x (ldelema (array, index), val)
5737 * ldelema does the bounds check
5741 klass = array_access_to_klass (*ip);
5742 NEW_LDELEMA (cfg, load, sp, klass);
5743 load->cil_code = ip;
5744 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
5746 ins->inst_left = load;
5747 ins->inst_right = sp [2];
5749 handle_loaded_temps (cfg, bblock, stack_start, sp);
5750 MONO_ADD_INS (bblock, ins);
5754 case CEE_STELEM_ANY: {
5758 * stind.x (ldelema (array, index), val)
5759 * ldelema does the bounds check
5764 token = read32 (ip + 1);
5765 klass = mono_class_get_full (image, token, generic_context);
5768 mono_class_init (klass);
5769 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5770 MonoMethod* helper = mono_marshal_get_stelemref ();
5771 MonoInst *iargs [3];
5772 handle_loaded_temps (cfg, bblock, stack_start, sp);
5778 mono_emit_method_call_spilled (cfg, bblock, helper, mono_method_signature (helper), iargs, ip, NULL);
5780 NEW_LDELEMA (cfg, load, sp, klass);
5781 load->cil_code = ip;
5783 n = mono_type_to_stind (&klass->byval_arg);
5785 handle_stobj (cfg, bblock, load, sp [2], ip, klass, FALSE, FALSE);
5787 MONO_INST_NEW (cfg, ins, n);
5789 ins->inst_left = load;
5790 ins->inst_right = sp [2];
5791 handle_loaded_temps (cfg, bblock, stack_start, sp);
5792 MONO_ADD_INS (bblock, ins);
5799 case CEE_STELEM_REF: {
5800 MonoInst *iargs [3];
5801 MonoMethod* helper = mono_marshal_get_stelemref ();
5806 handle_loaded_temps (cfg, bblock, stack_start, sp);
5812 mono_emit_method_call_spilled (cfg, bblock, helper, mono_method_signature (helper), iargs, ip, NULL);
5816 NEW_GROUP (cfg, group, sp [0], sp [1]);
5817 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
5819 ins->inst_left = group;
5820 ins->inst_right = sp [2];
5821 MONO_ADD_INS (bblock, ins);
5828 case CEE_CKFINITE: {
5829 MonoInst *store, *temp;
5832 /* this instr. can throw exceptions as side effect,
5833 * so we cant eliminate dead code which contains CKFINITE opdodes.
5834 * Spilling to memory makes sure that we always perform
5838 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
5840 ins->inst_left = sp [-1];
5841 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
5843 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
5844 store->cil_code = ip;
5845 MONO_ADD_INS (bblock, store);
5847 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
5854 MONO_INST_NEW (cfg, ins, *ip);
5857 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
5860 mono_class_init (klass);
5861 ins->type = STACK_MP;
5862 ins->inst_left = *sp;
5864 ins->inst_newa_class = klass;
5869 case CEE_MKREFANY: {
5870 MonoInst *loc, *klassconst;
5873 MONO_INST_NEW (cfg, ins, *ip);
5876 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
5879 mono_class_init (klass);
5882 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
5883 NEW_TEMPLOADA (cfg, ins->inst_right, loc->inst_c0);
5885 NEW_PCONST (cfg, klassconst, klass);
5886 NEW_GROUP (cfg, ins->inst_left, *sp, klassconst);
5888 MONO_ADD_INS (bblock, ins);
5890 NEW_TEMPLOAD (cfg, *sp, loc->inst_c0);
5897 MonoClass *handle_class;
5899 CHECK_STACK_OVF (1);
5902 n = read32 (ip + 1);
5904 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
5905 handle = mono_method_get_wrapper_data (method, n);
5906 handle_class = mono_method_get_wrapper_data (method, n + 1);
5909 handle = mono_ldtoken (image, n, &handle_class, generic_context);
5913 mono_class_init (handle_class);
5915 if (cfg->opt & MONO_OPT_SHARED) {
5917 MonoInst *res, *store, *addr, *vtvar, *iargs [3];
5919 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
5921 NEW_IMAGECONST (cfg, iargs [0], image);
5922 NEW_ICONST (cfg, iargs [1], n);
5923 NEW_PCONST (cfg, iargs [2], generic_context);
5924 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
5925 NEW_TEMPLOAD (cfg, res, temp);
5926 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5927 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
5928 MONO_ADD_INS (bblock, store);
5929 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5931 if ((ip [5] == CEE_CALL) && (cmethod = mini_get_method (method, read32 (ip + 6), NULL, generic_context)) &&
5932 (cmethod->klass == mono_defaults.monotype_class->parent) &&
5933 (strcmp (cmethod->name, "GetTypeFromHandle") == 0) && ip_in_bb (cfg, bblock, ip + 5)) {
5934 MonoClass *tclass = mono_class_from_mono_type (handle);
5935 mono_class_init (tclass);
5936 if (cfg->compile_aot)
5937 NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n);
5939 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
5940 ins->type = STACK_OBJ;
5941 ins->klass = cmethod->klass;
5944 MonoInst *store, *addr, *vtvar;
5946 if (cfg->compile_aot)
5947 NEW_LDTOKENCONST (cfg, ins, image, n);
5949 NEW_PCONST (cfg, ins, handle);
5950 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
5951 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5952 NEW_INDSTORE (cfg, store, addr, ins, &mono_defaults.int_class->byval_arg);
5953 MONO_ADD_INS (bblock, store);
5954 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5970 case CEE_ADD_OVF_UN:
5972 case CEE_MUL_OVF_UN:
5974 case CEE_SUB_OVF_UN:
5977 if (mono_find_jit_opcode_emulation (ins->opcode)) {
5979 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
5980 mono_get_got_var (cfg);
5984 case CEE_ENDFINALLY:
5985 MONO_INST_NEW (cfg, ins, *ip);
5986 MONO_ADD_INS (bblock, ins);
5987 ins->cil_code = ip++;
5988 start_new_bblock = 1;
5991 * Control will leave the method so empty the stack, otherwise
5992 * the next basic block will start with a nonempty stack.
5994 while (sp != stack_start) {
5995 MONO_INST_NEW (cfg, ins, CEE_POP);
5999 MONO_ADD_INS (bblock, ins);
6006 if (*ip == CEE_LEAVE) {
6008 target = ip + 5 + (gint32)read32(ip + 1);
6011 target = ip + 2 + (signed char)(ip [1]);
6014 /* empty the stack */
6015 while (sp != stack_start) {
6016 MONO_INST_NEW (cfg, ins, CEE_POP);
6020 MONO_ADD_INS (bblock, ins);
6024 * If this leave statement is in a catch block, check for a
6025 * pending exception, and rethrow it if necessary.
6027 for (i = 0; i < header->num_clauses; ++i) {
6028 MonoExceptionClause *clause = &header->clauses [i];
6030 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)) {
6034 NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, clause->handler_offset)->inst_c0);
6035 load->cil_code = ip;
6037 temp = mono_emit_jit_icall (cfg, bblock, mono_thread_get_pending_exception, NULL, ip);
6038 NEW_TEMPLOAD (cfg, *sp, temp);
6040 MONO_INST_NEW (cfg, ins, OP_THROW_OR_NULL);
6041 ins->inst_left = *sp;
6042 ins->inst_right = load;
6044 MONO_ADD_INS (bblock, ins);
6048 /* fixme: call fault handler ? */
6050 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
6052 for (tmp = handlers; tmp; tmp = tmp->next) {
6054 link_bblock (cfg, bblock, tblock);
6055 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
6057 ins->inst_target_bb = tblock;
6058 MONO_ADD_INS (bblock, ins);
6060 g_list_free (handlers);
6063 MONO_INST_NEW (cfg, ins, CEE_BR);
6065 MONO_ADD_INS (bblock, ins);
6066 GET_BBLOCK (cfg, bbhash, tblock, target);
6067 link_bblock (cfg, bblock, tblock);
6068 CHECK_BBLOCK (target, ip, tblock);
6069 ins->inst_target_bb = tblock;
6070 start_new_bblock = 1;
6072 if (*ip == CEE_LEAVE)
6081 MONO_INST_NEW (cfg, ins, *ip);
6083 handle_loaded_temps (cfg, bblock, stack_start, sp);
6084 MONO_ADD_INS (bblock, ins);
6085 ins->cil_code = ip++;
6086 ins->inst_i0 = sp [0];
6087 ins->inst_i1 = sp [1];
6095 /* trampoline mono specific opcodes */
6096 case MONO_CUSTOM_PREFIX: {
6098 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
6103 case CEE_MONO_ICALL: {
6106 MonoJitICallInfo *info;
6108 token = read32 (ip + 2);
6109 func = mono_method_get_wrapper_data (method, token);
6110 info = mono_find_jit_icall_by_addr (func);
6113 CHECK_STACK (info->sig->param_count);
6114 sp -= info->sig->param_count;
6116 temp = mono_emit_jit_icall (cfg, bblock, info->func, sp, ip);
6117 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
6118 NEW_TEMPLOAD (cfg, *sp, temp);
6123 inline_costs += 10 * num_calls++;
6127 case CEE_MONO_LDPTR:
6128 CHECK_STACK_OVF (1);
6130 token = read32 (ip + 2);
6131 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
6135 inline_costs += 10 * num_calls++;
6136 /* Can't embed random pointers into AOT code */
6137 cfg->disable_aot = 1;
6139 case CEE_MONO_VTADDR:
6142 MONO_INST_NEW (cfg, ins, OP_VTADDR);
6144 ins->type = STACK_MP;
6145 ins->inst_left = *sp;
6149 case CEE_MONO_NEWOBJ: {
6150 MonoInst *iargs [2];
6152 CHECK_STACK_OVF (1);
6154 token = read32 (ip + 2);
6155 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
6156 mono_class_init (klass);
6157 NEW_DOMAINCONST (cfg, iargs [0]);
6158 NEW_CLASSCONST (cfg, iargs [1], klass);
6159 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
6160 NEW_TEMPLOAD (cfg, *sp, temp);
6163 inline_costs += 10 * num_calls++;
6166 case CEE_MONO_OBJADDR:
6169 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
6171 ins->type = STACK_MP;
6172 ins->inst_left = *sp;
6176 case CEE_MONO_LDNATIVEOBJ:
6179 token = read32 (ip + 2);
6180 klass = mono_method_get_wrapper_data (method, token);
6181 g_assert (klass->valuetype);
6182 mono_class_init (klass);
6183 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
6187 case CEE_MONO_RETOBJ:
6188 g_assert (cfg->ret);
6189 g_assert (mono_method_signature (method)->pinvoke);
6194 token = read32 (ip + 2);
6195 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
6197 NEW_RETLOADA (cfg, ins);
6198 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
6200 if (sp != stack_start)
6203 MONO_INST_NEW (cfg, ins, CEE_BR);
6205 ins->inst_target_bb = end_bblock;
6206 MONO_ADD_INS (bblock, ins);
6207 link_bblock (cfg, bblock, end_bblock);
6208 start_new_bblock = 1;
6211 case CEE_MONO_CISINST:
6212 case CEE_MONO_CCASTCLASS: {
6217 token = read32 (ip + 2);
6218 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
6219 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_CISINST) ? OP_CISINST : OP_CCASTCLASS);
6220 ins->type = STACK_I4;
6221 ins->inst_left = *sp;
6222 ins->inst_newa_class = klass;
6224 *sp++ = emit_tree (cfg, bblock, ins, ip + 6);
6228 case CEE_MONO_SAVE_LMF:
6229 case CEE_MONO_RESTORE_LMF:
6230 #ifdef MONO_ARCH_HAVE_LMF_OPS
6231 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_SAVE_LMF) ? OP_SAVE_LMF : OP_RESTORE_LMF);
6232 MONO_ADD_INS (bblock, ins);
6233 cfg->need_lmf_area = TRUE;
6237 case CEE_MONO_CLASSCONST:
6238 CHECK_STACK_OVF (1);
6240 token = read32 (ip + 2);
6241 NEW_CLASSCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
6245 inline_costs += 10 * num_calls++;
6247 case CEE_MONO_NOT_TAKEN:
6248 bblock->out_of_line = TRUE;
6252 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
6261 /* somewhat similar to LDTOKEN */
6262 MonoInst *addr, *vtvar;
6263 CHECK_STACK_OVF (1);
6264 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
6266 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
6267 addr->cil_code = ip;
6268 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
6270 ins->inst_left = addr;
6271 MONO_ADD_INS (bblock, ins);
6272 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
6286 * The following transforms:
6287 * CEE_CEQ into OP_CEQ
6288 * CEE_CGT into OP_CGT
6289 * CEE_CGT_UN into OP_CGT_UN
6290 * CEE_CLT into OP_CLT
6291 * CEE_CLT_UN into OP_CLT_UN
6293 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
6295 MONO_INST_NEW (cfg, ins, cmp->opcode);
6297 cmp->inst_i0 = sp [0];
6298 cmp->inst_i1 = sp [1];
6302 if ((sp [0]->type == STACK_I8) || ((sizeof (gpointer) == 8) && ((sp [0]->type == STACK_PTR) || (sp [0]->type == STACK_OBJ) || (sp [0]->type == STACK_MP))))
6303 cmp->opcode = OP_LCOMPARE;
6305 cmp->opcode = OP_COMPARE;
6307 ins->type = STACK_I4;
6310 /* spill it to reduce the expression complexity
6311 * and workaround bug 54209
6313 if (cmp->inst_left->type == STACK_I8) {
6315 *sp++ = emit_tree (cfg, bblock, ins, ip + 2);
6324 CHECK_STACK_OVF (1);
6326 n = read32 (ip + 2);
6327 cmethod = mini_get_method (method, n, NULL, generic_context);
6330 mono_class_init (cmethod->klass);
6332 if (mono_use_security_manager) {
6333 check_linkdemand (cfg, method, cmethod, bblock, ip);
6336 handle_loaded_temps (cfg, bblock, stack_start, sp);
6338 NEW_METHODCONST (cfg, argconst, cmethod);
6339 if (method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED)
6340 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
6342 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn_nosync, &argconst, ip);
6343 NEW_TEMPLOAD (cfg, *sp, temp);
6347 inline_costs += 10 * num_calls++;
6350 case CEE_LDVIRTFTN: {
6356 n = read32 (ip + 2);
6357 cmethod = mini_get_method (method, n, NULL, generic_context);
6360 mono_class_init (cmethod->klass);
6362 if (mono_use_security_manager) {
6363 check_linkdemand (cfg, method, cmethod, bblock, ip);
6366 handle_loaded_temps (cfg, bblock, stack_start, sp);
6370 NEW_METHODCONST (cfg, args [1], cmethod);
6371 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
6372 NEW_TEMPLOAD (cfg, *sp, temp);
6376 inline_costs += 10 * num_calls++;
6380 CHECK_STACK_OVF (1);
6382 n = read16 (ip + 2);
6384 NEW_ARGLOAD (cfg, ins, n);
6390 CHECK_STACK_OVF (1);
6392 n = read16 (ip + 2);
6394 NEW_ARGLOADA (cfg, ins, n);
6402 handle_loaded_temps (cfg, bblock, stack_start, sp);
6404 n = read16 (ip + 2);
6406 NEW_ARGSTORE (cfg, ins, n, *sp);
6408 if (ins->opcode == CEE_STOBJ) {
6409 NEW_ARGLOADA (cfg, ins, n);
6410 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
6412 MONO_ADD_INS (bblock, ins);
6416 CHECK_STACK_OVF (1);
6418 n = read16 (ip + 2);
6420 NEW_LOCLOAD (cfg, ins, n);
6426 CHECK_STACK_OVF (1);
6428 n = read16 (ip + 2);
6430 NEW_LOCLOADA (cfg, ins, n);
6439 n = read16 (ip + 2);
6441 handle_loaded_temps (cfg, bblock, stack_start, sp);
6442 NEW_LOCSTORE (cfg, ins, n, *sp);
6444 if (ins->opcode == CEE_STOBJ) {
6445 NEW_LOCLOADA (cfg, ins, n);
6446 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
6448 MONO_ADD_INS (bblock, ins);
6455 if (sp != stack_start)
6457 if (cfg->method != method)
6459 * Inlining this into a loop in a parent could lead to
6460 * stack overflows which is different behavior than the
6461 * non-inlined case, thus disable inlining in this case.
6463 goto inline_failure;
6464 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
6465 ins->inst_left = *sp;
6467 ins->type = STACK_MP;
6469 cfg->flags |= MONO_CFG_HAS_ALLOCA;
6470 if (header->init_locals)
6471 ins->flags |= MONO_INST_INIT;
6475 /* FIXME: set init flag if locals init is set in this method */
6477 case CEE_ENDFILTER: {
6478 MonoExceptionClause *clause, *nearest;
6479 int cc, nearest_num;
6483 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
6485 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
6486 ins->inst_left = *sp;
6488 MONO_ADD_INS (bblock, ins);
6489 start_new_bblock = 1;
6494 for (cc = 0; cc < header->num_clauses; ++cc) {
6495 clause = &header->clauses [cc];
6496 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
6497 ((ip - header->code) > clause->data.filter_offset && (ip - header->code) <= clause->handler_offset) &&
6498 (!nearest || (clause->data.filter_offset < nearest->data.filter_offset))) {
6504 if ((ip - header->code) != nearest->handler_offset)
6509 case CEE_UNALIGNED_:
6510 ins_flag |= MONO_INST_UNALIGNED;
6511 /* FIXME: record alignment? we can assume 1 for now */
6516 ins_flag |= MONO_INST_VOLATILE;
6520 ins_flag |= MONO_INST_TAILCALL;
6521 cfg->flags |= MONO_CFG_HAS_TAIL;
6522 /* Can't inline tail calls at this time */
6523 inline_costs += 100000;
6530 token = read32 (ip + 2);
6531 klass = mini_get_class (method, token, generic_context);
6534 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
6535 MonoInst *store, *load;
6536 NEW_PCONST (cfg, load, NULL);
6537 load->cil_code = ip;
6538 load->type = STACK_OBJ;
6539 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
6540 store->cil_code = ip;
6541 handle_loaded_temps (cfg, bblock, stack_start, sp);
6542 MONO_ADD_INS (bblock, store);
6543 store->inst_i0 = sp [0];
6544 store->inst_i1 = load;
6546 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
6551 case CEE_CONSTRAINED_:
6552 /* FIXME: implement */
6554 token = read32 (ip + 2);
6555 constrained_call = mono_class_get_full (image, token, generic_context);
6556 if (!constrained_call)
6562 MonoInst *iargs [3];
6565 if ((cfg->opt & MONO_OPT_INTRINS) && (ip [1] == CEE_CPBLK) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5)) {
6567 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
6568 copy->inst_left = sp [0];
6569 copy->inst_right = sp [1];
6570 copy->cil_code = ip;
6572 MONO_ADD_INS (bblock, copy);
6579 handle_loaded_temps (cfg, bblock, stack_start, sp);
6580 if (ip [1] == CEE_CPBLK) {
6581 MonoMethod *memcpy_method = get_memcpy_method ();
6582 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
6584 MonoMethod *memset_method = get_memset_method ();
6585 mono_emit_method_call_spilled (cfg, bblock, memset_method, memset_method->signature, iargs, ip, NULL);
6594 ins_flag |= MONO_INST_NOTYPECHECK;
6596 ins_flag |= MONO_INST_NORANGECHECK;
6597 /* we ignore the no-nullcheck for now since we
6598 * really do it explicitly only when doing callvirt->call
6604 int handler_offset = -1;
6606 for (i = 0; i < header->num_clauses; ++i) {
6607 MonoExceptionClause *clause = &header->clauses [i];
6608 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code) && !(clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY))
6609 handler_offset = clause->handler_offset;
6612 bblock->flags |= BB_EXCEPTION_UNSAFE;
6614 g_assert (handler_offset != -1);
6616 NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, handler_offset)->inst_c0);
6617 load->cil_code = ip;
6618 MONO_INST_NEW (cfg, ins, OP_RETHROW);
6619 ins->inst_left = load;
6621 MONO_ADD_INS (bblock, ins);
6623 link_bblock (cfg, bblock, end_bblock);
6624 start_new_bblock = 1;
6626 mono_get_got_var (cfg);
6630 CHECK_STACK_OVF (1);
6632 token = read32 (ip + 2);
6633 /* FIXXME: handle generics. */
6634 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
6635 MonoType *type = mono_type_create_from_typespec (image, token);
6636 token = mono_type_size (type, &ialign);
6638 MonoClass *klass = mono_class_get_full (image, token, generic_context);
6641 mono_class_init (klass);
6642 token = mono_class_value_size (klass, &align);
6644 NEW_ICONST (cfg, ins, token);
6649 case CEE_REFANYTYPE:
6651 MONO_INST_NEW (cfg, ins, OP_REFANYTYPE);
6653 ins->type = STACK_MP;
6654 ins->inst_left = *sp;
6655 ins->type = STACK_VTYPE;
6656 ins->klass = mono_defaults.typehandle_class;
6665 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
6670 g_error ("opcode 0x%02x not handled", *ip);
6673 if (start_new_bblock != 1)
6676 bblock->cil_length = ip - bblock->cil_code;
6677 bblock->next_bb = end_bblock;
6679 if (cfg->method == method && cfg->domainvar) {
6681 MonoInst *get_domain;
6683 if (! (get_domain = mono_arch_get_domain_intrinsic (cfg))) {
6686 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
6687 call->signature = helper_sig_domain_get;
6688 call->inst.type = STACK_PTR;
6689 call->fptr = mono_domain_get;
6690 get_domain = (MonoInst*)call;
6693 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, get_domain);
6694 MONO_ADD_INS (init_localsbb, store);
6697 if (cfg->method == method && cfg->got_var)
6698 mono_emit_load_got_addr (cfg);
6700 if (header->init_locals) {
6702 for (i = 0; i < header->num_locals; ++i) {
6703 MonoType *ptype = header->locals [i];
6704 int t = ptype->type;
6705 if (t == MONO_TYPE_VALUETYPE && ptype->data.klass->enumtype)
6706 t = ptype->data.klass->enum_basetype->type;
6708 NEW_PCONST (cfg, ins, NULL);
6709 NEW_LOCSTORE (cfg, store, i, ins);
6710 MONO_ADD_INS (init_localsbb, store);
6711 } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
6712 NEW_ICONST (cfg, ins, 0);
6713 NEW_LOCSTORE (cfg, store, i, ins);
6714 MONO_ADD_INS (init_localsbb, store);
6715 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
6716 MONO_INST_NEW (cfg, ins, OP_I8CONST);
6717 ins->type = STACK_I8;
6719 NEW_LOCSTORE (cfg, store, i, ins);
6720 MONO_ADD_INS (init_localsbb, store);
6721 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
6722 MONO_INST_NEW (cfg, ins, OP_R8CONST);
6723 ins->type = STACK_R8;
6724 ins->inst_p0 = (void*)&r8_0;
6725 NEW_LOCSTORE (cfg, store, i, ins);
6726 MONO_ADD_INS (init_localsbb, store);
6727 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
6728 ((t == MONO_TYPE_GENERICINST) && mono_metadata_generic_class_is_valuetype (ptype->data.generic_class))) {
6729 NEW_LOCLOADA (cfg, ins, i);
6730 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (ptype), NULL, NULL);
6732 NEW_PCONST (cfg, ins, NULL);
6733 NEW_LOCSTORE (cfg, store, i, ins);
6734 MONO_ADD_INS (init_localsbb, store);
6739 /* resolve backward branches in the middle of an existing basic block */
6740 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
6742 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
6743 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
6744 if (tblock != start_bblock) {
6746 split_bblock (cfg, tblock, bblock);
6747 l = bblock->cil_code - header->code;
6748 bblock->cil_length = tblock->cil_length - l;
6749 tblock->cil_length = l;
6751 g_print ("recheck failed.\n");
6755 if (cfg->method == method) {
6757 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6758 bb->region = mono_find_block_region (cfg, bb->real_offset);
6760 mono_create_spvar_for_region (cfg, bb->region);
6761 if (cfg->verbose_level > 2)
6762 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
6765 g_hash_table_destroy (bbhash);
6768 g_slist_free (class_inits);
6769 dont_inline = g_list_remove (dont_inline, method);
6770 return inline_costs;
6773 if (cfg->method != method)
6774 g_hash_table_destroy (bbhash);
6775 g_slist_free (class_inits);
6776 dont_inline = g_list_remove (dont_inline, method);
6780 if (cfg->method != method)
6781 g_hash_table_destroy (bbhash);
6782 g_slist_free (class_inits);
6783 dont_inline = g_list_remove (dont_inline, method);
6787 if (cfg->method != method)
6788 g_hash_table_destroy (bbhash);
6789 g_slist_free (class_inits);
6790 g_error ("Invalid IL code at IL%04x in %s: %s\n", (int)(ip - header->code),
6791 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
6792 dont_inline = g_list_remove (dont_inline, method);
6797 mono_print_tree (MonoInst *tree) {
6803 arity = mono_burg_arity [tree->opcode];
6805 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
6807 switch (tree->opcode) {
6809 printf ("[%d]", (int)tree->inst_c0);
6812 printf ("[%lld]", (long long)tree->inst_l);
6815 printf ("[%f]", *(double*)tree->inst_p0);
6818 printf ("[%f]", *(float*)tree->inst_p0);
6822 printf ("[%d]", (int)tree->inst_c0);
6825 if (tree->inst_offset < 0)
6826 printf ("[-0x%x(%s)]", (int)(-tree->inst_offset), mono_arch_regname (tree->inst_basereg));
6828 printf ("[0x%x(%s)]", (int)(tree->inst_offset), mono_arch_regname (tree->inst_basereg));
6831 printf ("[%s]", mono_arch_regname (tree->dreg));
6834 printf ("[%s]", tree->inst_newa_class->name);
6835 mono_print_tree (tree->inst_newa_len);
6846 case OP_VOIDCALLVIRT: {
6847 MonoCallInst *call = (MonoCallInst*)tree;
6849 printf ("[%s]", call->method->name);
6850 else if (call->fptr) {
6851 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (call->fptr);
6853 printf ("[%s]", info->name);
6859 printf ("[%d (", (int)tree->inst_c0);
6860 for (i = 0; i < tree->inst_phi_args [0]; i++) {
6863 printf ("%d", tree->inst_phi_args [i + 1]);
6874 case OP_LOAD_MEMBASE:
6875 case OP_LOADI4_MEMBASE:
6876 case OP_LOADU4_MEMBASE:
6877 case OP_LOADU1_MEMBASE:
6878 case OP_LOADI1_MEMBASE:
6879 case OP_LOADU2_MEMBASE:
6880 case OP_LOADI2_MEMBASE:
6881 printf ("[%s] <- [%s + 0x%x]", mono_arch_regname (tree->dreg), mono_arch_regname (tree->inst_basereg), (int)tree->inst_offset);
6884 case OP_CALL_HANDLER:
6885 printf ("[B%d]", tree->inst_target_bb->block_num);
6895 case OP_VOIDCALL_REG:
6896 mono_print_tree (tree->inst_left);
6908 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
6909 mono_print_tree (tree->inst_left);
6912 if (!mono_arch_print_tree(tree, arity)) {
6914 mono_print_tree (tree->inst_left);
6916 mono_print_tree (tree->inst_right);
6927 mono_print_tree_nl (MonoInst *tree)
6929 mono_print_tree (tree);
6934 create_helper_signature (void)
6936 helper_sig_domain_get = mono_create_icall_signature ("ptr");
6937 helper_sig_class_init_trampoline = mono_create_icall_signature ("void");
6941 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
6944 MonoMethod *wrapper;
6947 if (callinfo->wrapper)
6948 return callinfo->wrapper;
6950 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
6951 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
6952 /* Must be domain neutral since there is only one copy */
6953 code = mono_jit_compile_method_with_opt (wrapper, default_opt | MONO_OPT_SHARED);
6955 if (!callinfo->wrapper) {
6956 callinfo->wrapper = code;
6957 mono_register_jit_icall_wrapper (callinfo, code);
6958 mono_debug_add_icall_wrapper (wrapper, callinfo);
6962 return callinfo->wrapper;
6966 mono_init_trampolines (void)
6968 mono_trampoline_code [MONO_TRAMPOLINE_GENERIC] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_GENERIC);
6969 mono_trampoline_code [MONO_TRAMPOLINE_JUMP] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_JUMP);
6970 mono_trampoline_code [MONO_TRAMPOLINE_CLASS_INIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_CLASS_INIT);
6971 #ifdef MONO_ARCH_HAVE_PIC_AOT
6972 mono_trampoline_code [MONO_TRAMPOLINE_AOT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_AOT);
6974 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
6975 mono_trampoline_code [MONO_TRAMPOLINE_DELEGATE] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_DELEGATE);
6980 mono_init_exceptions (void)
6982 #ifndef CUSTOM_EXCEPTION_HANDLING
6983 mono_arch_get_restore_context ();
6984 mono_arch_get_call_filter ();
6985 mono_arch_get_throw_exception ();
6986 mono_arch_get_rethrow_exception ();
6991 mono_get_trampoline_code (MonoTrampolineType tramp_type)
6993 return mono_trampoline_code [tramp_type];
6997 mono_create_class_init_trampoline (MonoVTable *vtable)
7001 /* previously created trampoline code */
7002 mono_domain_lock (vtable->domain);
7004 g_hash_table_lookup (vtable->domain->class_init_trampoline_hash,
7006 mono_domain_unlock (vtable->domain);
7010 #ifdef MONO_ARCH_HAVE_CREATE_SPECIFIC_TRAMPOLINE
7011 code = mono_arch_create_specific_trampoline (vtable, MONO_TRAMPOLINE_CLASS_INIT, vtable->domain, NULL);
7013 code = mono_arch_create_class_init_trampoline (vtable);
7016 ptr = mono_create_ftnptr (vtable->domain, code);
7018 /* store trampoline address */
7019 mono_domain_lock (vtable->domain);
7020 g_hash_table_insert (vtable->domain->class_init_trampoline_hash,
7022 mono_domain_unlock (vtable->domain);
7025 if (!class_init_hash_addr)
7026 class_init_hash_addr = g_hash_table_new (NULL, NULL);
7027 g_hash_table_insert (class_init_hash_addr, ptr, vtable);
7034 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method,
7035 gboolean add_sync_wrapper)
7039 #ifdef MONO_ARCH_HAVE_CREATE_SPECIFIC_TRAMPOLINE
7043 if (add_sync_wrapper && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
7044 return mono_create_jump_trampoline (domain, mono_marshal_get_synchronized_wrapper (method), FALSE);
7046 code = mono_jit_find_compiled_method (domain, method);
7050 mono_domain_lock (domain);
7051 code = g_hash_table_lookup (domain->jump_trampoline_hash, method);
7052 mono_domain_unlock (domain);
7056 #ifdef MONO_ARCH_HAVE_CREATE_SPECIFIC_TRAMPOLINE
7057 code = mono_arch_create_specific_trampoline (method, MONO_TRAMPOLINE_JUMP, mono_domain_get (), &code_size);
7059 ji = g_new0 (MonoJitInfo, 1);
7060 ji->code_start = code;
7061 ji->code_size = code_size;
7062 ji->method = method;
7064 ji = mono_arch_create_jump_trampoline (method);
7068 * mono_delegate_ctor needs to find the method metadata from the
7069 * trampoline address, so we save it here.
7072 mono_jit_info_table_add (domain, ji);
7074 mono_domain_lock (domain);
7075 g_hash_table_insert (domain->jump_trampoline_hash, method, ji->code_start);
7076 mono_domain_unlock (domain);
7078 return ji->code_start;
7082 mono_create_jit_trampoline (MonoMethod *method)
7084 MonoDomain *domain = mono_domain_get ();
7087 mono_domain_lock (domain);
7088 tramp = g_hash_table_lookup (domain->jit_trampoline_hash, method);
7089 mono_domain_unlock (domain);
7093 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
7094 return mono_create_jit_trampoline (mono_marshal_get_synchronized_wrapper (method));
7096 #ifdef MONO_ARCH_HAVE_CREATE_SPECIFIC_TRAMPOLINE
7097 tramp = mono_arch_create_specific_trampoline (method, MONO_TRAMPOLINE_GENERIC, mono_domain_get (), NULL);
7099 tramp = mono_arch_create_jit_trampoline (method);
7102 mono_domain_lock (domain);
7103 g_hash_table_insert (domain->jit_trampoline_hash, method, tramp);
7104 mono_domain_unlock (domain);
7106 mono_jit_stats.method_trampolines++;
7111 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
7113 mono_create_jit_trampoline_from_token (MonoImage *image, guint32 token)
7117 MonoDomain *domain = mono_domain_get ();
7118 guint8 *buf, *start;
7120 mono_domain_lock (domain);
7121 buf = start = mono_code_manager_reserve (domain->code_mp, 2 * sizeof (gpointer));
7122 mono_domain_unlock (domain);
7124 *(gpointer*)(gpointer)buf = image;
7125 buf += sizeof (gpointer);
7126 *(guint32*)(gpointer)buf = token;
7128 tramp = mono_arch_create_specific_trampoline (start, MONO_TRAMPOLINE_AOT, domain, NULL);
7130 mono_jit_stats.method_trampolines++;
7137 mono_create_delegate_trampoline (MonoMethod *method, gpointer addr)
7139 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
7142 MonoDomain *domain = mono_domain_get ();
7145 code = mono_jit_find_compiled_method (domain, method);
7150 * FIXME: We should return a function descriptor here but it is not stored
7151 * anywhere so it would be leaked.
7155 mono_domain_lock (domain);
7156 ptr = g_hash_table_lookup (domain->delegate_trampoline_hash, method);
7157 mono_domain_unlock (domain);
7161 code = mono_arch_create_specific_trampoline (method, MONO_TRAMPOLINE_DELEGATE, domain, &code_size);
7163 ptr = mono_create_ftnptr (domain, code);
7165 /* store trampoline address */
7166 mono_domain_lock (domain);
7167 g_hash_table_insert (domain->delegate_trampoline_hash,
7169 mono_domain_unlock (domain);
7178 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
7183 if (class_init_hash_addr)
7184 res = g_hash_table_lookup (class_init_hash_addr, addr);
7192 mono_dynamic_code_hash_insert (MonoDomain *domain, MonoMethod *method, MonoJitDynamicMethodInfo *ji)
7194 if (!domain->dynamic_code_hash)
7195 domain->dynamic_code_hash = g_hash_table_new (NULL, NULL);
7196 g_hash_table_insert (domain->dynamic_code_hash, method, ji);
7199 static MonoJitDynamicMethodInfo*
7200 mono_dynamic_code_hash_lookup (MonoDomain *domain, MonoMethod *method)
7202 MonoJitDynamicMethodInfo *res;
7204 if (domain->dynamic_code_hash)
7205 res = g_hash_table_lookup (domain->dynamic_code_hash, method);
7218 * mono_allocate_stack_slots_full:
7220 * Allocate stack slots for all non register allocated variables using a
7221 * linear scan algorithm.
7222 * Returns: an array of stack offsets which the caller should free.
7223 * STACK_SIZE is set to the amount of stack space needed.
7224 * STACK_ALIGN is set to the alignment needed by the locals area.
7227 mono_allocate_stack_slots_full (MonoCompile *m, gboolean backward, guint32 *stack_size, guint32 *stack_align)
7229 int i, slot, offset, size;
7234 GList *vars = NULL, *l;
7235 StackSlotInfo *scalar_stack_slots, *vtype_stack_slots, *slot_info;
7239 scalar_stack_slots = g_new0 (StackSlotInfo, MONO_TYPE_PINNED);
7240 vtype_stack_slots = g_new0 (StackSlotInfo, 256);
7243 offsets = g_new (gint32, m->num_varinfo);
7244 for (i = 0; i < m->num_varinfo; ++i)
7247 for (i = m->locals_start; i < m->num_varinfo; i++) {
7248 inst = m->varinfo [i];
7249 vmv = MONO_VARINFO (m, i);
7251 if ((inst->flags & MONO_INST_IS_DEAD) || inst->opcode == OP_REGVAR || inst->opcode == OP_REGOFFSET)
7254 vars = g_list_prepend (vars, vmv);
7257 vars = mono_varlist_sort (m, vars, 0);
7260 for (l = vars; l; l = l->next) {
7262 inst = m->varinfo [vmv->idx];
7264 /* inst->unused indicates native sized value types, this is used by the
7265 * pinvoke wrappers when they call functions returning structures */
7266 if (inst->unused && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
7267 size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
7271 size = mono_type_size (inst->inst_vtype, &ialign);
7275 t = mono_type_get_underlying_type (inst->inst_vtype);
7277 case MONO_TYPE_VALUETYPE:
7278 for (i = 0; i < nvtypes; ++i)
7279 if (t->data.klass == vtype_stack_slots [i].vtype)
7282 slot_info = &vtype_stack_slots [i];
7284 g_assert (nvtypes < 256);
7285 vtype_stack_slots [nvtypes].vtype = t->data.klass;
7286 slot_info = &vtype_stack_slots [nvtypes];
7291 slot_info = &scalar_stack_slots [t->type];
7295 if (m->comp_done & MONO_COMP_LIVENESS) {
7296 //printf ("START %2d %08x %08x\n", vmv->idx, vmv->range.first_use.abs_pos, vmv->range.last_use.abs_pos);
7298 /* expire old intervals in active */
7299 while (slot_info->active) {
7300 MonoMethodVar *amv = (MonoMethodVar *)slot_info->active->data;
7302 if (amv->range.last_use.abs_pos > vmv->range.first_use.abs_pos)
7305 //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);
7307 slot_info->active = g_list_delete_link (slot_info->active, slot_info->active);
7308 slot_info->slots = g_list_prepend (slot_info->slots, GINT_TO_POINTER (offsets [amv->idx]));
7312 * This also handles the case when the variable is used in an
7313 * exception region, as liveness info is not computed there.
7316 * FIXME: All valuetypes are marked as INDIRECT because of LDADDR
7319 if (! (inst->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
7320 if (slot_info->slots) {
7321 slot = GPOINTER_TO_INT (slot_info->slots->data);
7323 slot_info->slots = g_list_delete_link (slot_info->slots, slot_info->slots);
7326 slot_info->active = mono_varlist_insert_sorted (m, slot_info->active, vmv, TRUE);
7331 static int count = 0;
7335 if (count == atoi (getenv ("COUNT")))
7336 printf ("LAST: %s\n", mono_method_full_name (m->method, TRUE));
7337 if (count > atoi (getenv ("COUNT")))
7340 mono_print_tree_nl (inst);
7344 if (slot == 0xffffff) {
7346 * Allways allocate valuetypes to sizeof (gpointer) to allow more
7347 * efficient copying (and to work around the fact that OP_MEMCPY
7348 * and OP_MEMSET ignores alignment).
7350 if (t->type == MONO_TYPE_VALUETYPE)
7351 align = sizeof (gpointer);
7355 offset += align - 1;
7356 offset &= ~(align - 1);
7360 offset += align - 1;
7361 offset &= ~(align - 1);
7366 if (*stack_align == 0)
7367 *stack_align = align;
7370 offsets [vmv->idx] = slot;
7373 for (i = 0; i < MONO_TYPE_PINNED; ++i) {
7374 g_list_free (scalar_stack_slots [i].active);
7375 g_list_free (scalar_stack_slots [i].slots);
7377 for (i = 0; i < nvtypes; ++i) {
7378 g_list_free (vtype_stack_slots [i].active);
7379 g_list_free (vtype_stack_slots [i].slots);
7381 g_free (scalar_stack_slots);
7382 g_free (vtype_stack_slots);
7384 *stack_size = offset;
7389 mono_allocate_stack_slots (MonoCompile *m, guint32 *stack_size, guint32 *stack_align)
7391 return mono_allocate_stack_slots_full (m, TRUE, stack_size, stack_align);
7395 mono_register_opcode_emulation (int opcode, const char *name, const char *sigstr, gpointer func, gboolean no_throw)
7397 MonoJitICallInfo *info;
7398 MonoMethodSignature *sig = mono_create_icall_signature (sigstr);
7400 if (!emul_opcode_map)
7401 emul_opcode_map = g_new0 (MonoJitICallInfo*, OP_LAST + 1);
7403 g_assert (!sig->hasthis);
7404 g_assert (sig->param_count < 3);
7406 info = mono_register_jit_icall (func, name, sig, no_throw);
7408 emul_opcode_map [opcode] = info;
7412 register_icall (gpointer func, const char *name, const char *sigstr, gboolean save)
7414 MonoMethodSignature *sig;
7417 sig = mono_create_icall_signature (sigstr);
7421 mono_register_jit_icall (func, name, sig, save);
7425 decompose_foreach (MonoInst *tree, gpointer data)
7427 static MonoJitICallInfo *newarr_info = NULL;
7428 static MonoJitICallInfo *newarr_specific_info = NULL;
7429 MonoJitICallInfo *info;
7432 switch (tree->opcode) {
7434 MonoCompile *cfg = data;
7435 MonoInst *iargs [3];
7438 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
7439 g_assert (newarr_info);
7440 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
7441 g_assert (newarr_specific_info);
7444 if (cfg->opt & MONO_OPT_SHARED) {
7445 NEW_DOMAINCONST (cfg, iargs [0]);
7446 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
7447 iargs [2] = tree->inst_newa_len;
7452 MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
7454 NEW_VTABLECONST (cfg, iargs [0], vtable);
7455 iargs [1] = tree->inst_newa_len;
7457 info = newarr_specific_info;
7460 mono_emulate_opcode (cfg, tree, iargs, info);
7462 /* Need to decompose arguments after the the opcode is decomposed */
7463 for (i = 0; i < info->sig->param_count; ++i)
7464 dec_foreach (iargs [i], cfg);
7474 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
7476 switch (mono_burg_arity [tree->opcode]) {
7479 mono_inst_foreach (tree->inst_left, func, data);
7482 mono_inst_foreach (tree->inst_left, func, data);
7483 mono_inst_foreach (tree->inst_right, func, data);
7486 g_assert_not_reached ();
7493 mono_print_bb_code (MonoBasicBlock *bb) {
7495 MonoInst *c = bb->code;
7497 mono_print_tree (c);
7505 print_dfn (MonoCompile *cfg) {
7510 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
7512 for (i = 0; i < cfg->num_bblocks; ++i) {
7513 bb = cfg->bblocks [i];
7514 /*if (bb->cil_code) {
7515 char* code1, *code2;
7516 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
7517 if (bb->last_ins->cil_code)
7518 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
7520 code2 = g_strdup ("");
7522 code1 [strlen (code1) - 1] = 0;
7523 code = g_strdup_printf ("%s -> %s", code1, code2);
7527 code = g_strdup ("\n");
7528 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
7530 MonoInst *c = bb->code;
7532 mono_print_tree (c);
7540 g_print ("\tprev:");
7541 for (j = 0; j < bb->in_count; ++j) {
7542 g_print (" BB%d", bb->in_bb [j]->block_num);
7544 g_print ("\t\tsucc:");
7545 for (j = 0; j < bb->out_count; ++j) {
7546 g_print (" BB%d", bb->out_bb [j]->block_num);
7548 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
7551 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
7554 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
7556 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
7564 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
7568 g_assert (bb->code);
7569 bb->last_ins->next = inst;
7570 bb->last_ins = inst;
7572 bb->last_ins = bb->code = inst;
7577 mono_destroy_compile (MonoCompile *cfg)
7579 //mono_mempool_stats (cfg->mempool);
7580 g_hash_table_destroy (cfg->bb_hash);
7581 mono_free_loop_info (cfg);
7583 mono_regstate_free (cfg->rs);
7585 g_hash_table_destroy (cfg->spvars);
7587 g_hash_table_destroy (cfg->exvars);
7588 mono_mempool_destroy (cfg->mempool);
7589 g_list_free (cfg->ldstr_list);
7591 g_free (cfg->varinfo);
7596 #ifdef HAVE_KW_THREAD
7597 static __thread gpointer mono_lmf_addr MONO_TLS_FAST;
7601 mono_get_jit_tls_key (void)
7603 return mono_jit_tls_id;
7607 mono_get_lmf_tls_offset (void)
7610 MONO_THREAD_VAR_OFFSET(mono_lmf_addr,offset);
7615 mono_get_lmf_addr (void)
7617 #ifdef HAVE_KW_THREAD
7618 return mono_lmf_addr;
7620 MonoJitTlsData *jit_tls;
7622 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
7623 return &jit_tls->lmf;
7625 g_assert_not_reached ();
7630 /* Called by native->managed wrappers */
7632 mono_jit_thread_attach (MonoDomain *domain)
7634 #ifdef HAVE_KW_THREAD
7635 if (!mono_lmf_addr) {
7636 mono_thread_attach (domain);
7639 if (!TlsGetValue (mono_jit_tls_id))
7640 mono_thread_attach (domain);
7645 * mono_thread_abort:
7646 * @obj: exception object
7648 * abort the thread, print exception information and stack trace
7651 mono_thread_abort (MonoObject *obj)
7653 /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
7655 /* handle_remove should be eventually called for this thread, too
7658 mono_thread_exit ();
7662 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
7664 MonoJitTlsData *jit_tls;
7667 jit_tls = TlsGetValue (mono_jit_tls_id);
7671 jit_tls = g_new0 (MonoJitTlsData, 1);
7673 TlsSetValue (mono_jit_tls_id, jit_tls);
7675 jit_tls->abort_func = abort_func;
7676 jit_tls->end_of_stack = stack_start;
7678 lmf = g_new0 (MonoLMF, 1);
7681 jit_tls->lmf = jit_tls->first_lmf = lmf;
7683 #ifdef HAVE_KW_THREAD
7684 mono_lmf_addr = &jit_tls->lmf;
7687 mono_arch_setup_jit_tls_data (jit_tls);
7689 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
7690 mono_setup_altstack (jit_tls);
7697 mono_thread_start_cb (gsize tid, gpointer stack_start, gpointer func)
7700 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
7701 thread = mono_thread_current ();
7703 thread->jit_data = jit_tls;
7706 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
7709 mono_thread_abort_dummy (MonoObject *obj)
7711 if (mono_thread_attach_aborted_cb)
7712 mono_thread_attach_aborted_cb (obj);
7714 mono_thread_abort (obj);
7718 mono_thread_attach_cb (gsize tid, gpointer stack_start)
7721 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
7722 thread = mono_thread_current ();
7724 thread->jit_data = jit_tls;
7725 if (mono_profiler_get_events () & MONO_PROFILE_STATISTICAL)
7726 setup_stat_profiler ();
7730 mini_thread_cleanup (MonoThread *thread)
7732 MonoJitTlsData *jit_tls = thread->jit_data;
7735 mono_arch_free_jit_tls_data (jit_tls);
7737 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
7738 mono_free_altstack (jit_tls);
7740 g_free (jit_tls->first_lmf);
7742 thread->jit_data = NULL;
7747 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
7749 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
7753 ji->data.target = target;
7754 ji->next = cfg->patch_info;
7756 cfg->patch_info = ji;
7760 mono_remove_patch_info (MonoCompile *cfg, int ip)
7762 MonoJumpInfo **ji = &cfg->patch_info;
7765 if ((*ji)->ip.i == ip)
7768 ji = &((*ji)->next);
7773 mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors)
7775 unsigned char *ip = patch_info->ip.i + code;
7776 gconstpointer target = NULL;
7778 switch (patch_info->type) {
7779 case MONO_PATCH_INFO_BB:
7780 target = patch_info->data.bb->native_offset + code;
7782 case MONO_PATCH_INFO_ABS:
7783 target = patch_info->data.target;
7785 case MONO_PATCH_INFO_LABEL:
7786 target = patch_info->data.inst->inst_c0 + code;
7788 case MONO_PATCH_INFO_IP:
7791 case MONO_PATCH_INFO_METHOD_REL:
7792 target = code + patch_info->data.offset;
7794 case MONO_PATCH_INFO_INTERNAL_METHOD: {
7795 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
7797 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
7798 g_assert_not_reached ();
7800 target = mono_icall_get_wrapper (mi);
7803 case MONO_PATCH_INFO_METHOD_JUMP: {
7806 /* get the trampoline to the method from the domain */
7807 target = mono_create_jump_trampoline (domain, patch_info->data.method, TRUE);
7808 if (!domain->jump_target_hash)
7809 domain->jump_target_hash = g_hash_table_new (NULL, NULL);
7810 list = g_hash_table_lookup (domain->jump_target_hash, patch_info->data.method);
7811 list = g_slist_prepend (list, ip);
7812 g_hash_table_insert (domain->jump_target_hash, patch_info->data.method, list);
7815 case MONO_PATCH_INFO_METHOD:
7816 if (patch_info->data.method == method) {
7819 /* get the trampoline to the method from the domain */
7820 target = mono_create_jit_trampoline (patch_info->data.method);
7822 case MONO_PATCH_INFO_SWITCH: {
7823 gpointer *jump_table;
7826 if (method && method->dynamic) {
7827 jump_table = mono_code_manager_reserve (mono_dynamic_code_hash_lookup (domain, method)->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
7829 mono_domain_lock (domain);
7830 jump_table = mono_code_manager_reserve (domain->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
7831 mono_domain_unlock (domain);
7834 for (i = 0; i < patch_info->data.table->table_size; i++) {
7835 jump_table [i] = code + GPOINTER_TO_INT (patch_info->data.table->table [i]);
7837 target = jump_table;
7840 case MONO_PATCH_INFO_METHODCONST:
7841 case MONO_PATCH_INFO_CLASS:
7842 case MONO_PATCH_INFO_IMAGE:
7843 case MONO_PATCH_INFO_FIELD:
7844 target = patch_info->data.target;
7846 case MONO_PATCH_INFO_IID:
7847 mono_class_init (patch_info->data.klass);
7848 target = GINT_TO_POINTER ((int)patch_info->data.klass->interface_id);
7850 case MONO_PATCH_INFO_VTABLE:
7851 target = mono_class_vtable (domain, patch_info->data.klass);
7853 case MONO_PATCH_INFO_CLASS_INIT:
7854 target = mono_create_class_init_trampoline (mono_class_vtable (domain, patch_info->data.klass));
7856 case MONO_PATCH_INFO_SFLDA: {
7857 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
7858 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && (method && mono_class_needs_cctor_run (vtable->klass, method)))
7859 /* Done by the generated code */
7863 mono_runtime_class_init (vtable);
7865 target = (char*)vtable->data + patch_info->data.field->offset;
7868 case MONO_PATCH_INFO_R4:
7869 case MONO_PATCH_INFO_R8:
7870 target = patch_info->data.target;
7872 case MONO_PATCH_INFO_EXC_NAME:
7873 target = patch_info->data.name;
7875 case MONO_PATCH_INFO_LDSTR:
7877 mono_ldstr (domain, patch_info->data.token->image,
7878 mono_metadata_token_index (patch_info->data.token->token));
7880 case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
7882 MonoClass *handle_class;
7884 handle = mono_ldtoken (patch_info->data.token->image,
7885 patch_info->data.token->token, &handle_class, NULL);
7886 mono_class_init (handle_class);
7887 mono_class_init (mono_class_from_mono_type (handle));
7890 mono_type_get_object (domain, handle);
7893 case MONO_PATCH_INFO_LDTOKEN: {
7895 MonoClass *handle_class;
7897 handle = mono_ldtoken (patch_info->data.token->image,
7898 patch_info->data.token->token, &handle_class, NULL);
7899 mono_class_init (handle_class);
7904 case MONO_PATCH_INFO_DECLSEC:
7905 target = (mono_metadata_blob_heap (patch_info->data.token->image, patch_info->data.token->token) + 2);
7907 case MONO_PATCH_INFO_BB_OVF:
7908 case MONO_PATCH_INFO_EXC_OVF:
7909 case MONO_PATCH_INFO_GOT_OFFSET:
7910 case MONO_PATCH_INFO_NONE:
7913 g_assert_not_reached ();
7916 return (gpointer)target;
7920 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
7921 MonoJitICallInfo *info;
7923 decompose_foreach (tree, cfg);
7925 switch (mono_burg_arity [tree->opcode]) {
7928 dec_foreach (tree->inst_left, cfg);
7930 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
7931 MonoInst *iargs [2];
7933 iargs [0] = tree->inst_left;
7935 mono_emulate_opcode (cfg, tree, iargs, info);
7941 #ifdef MONO_ARCH_BIGMUL_INTRINS
7942 if (tree->opcode == OP_LMUL
7943 && (cfg->opt & MONO_OPT_INTRINS)
7944 && (tree->inst_left->opcode == CEE_CONV_I8
7945 || tree->inst_left->opcode == CEE_CONV_U8)
7946 && tree->inst_left->inst_left->type == STACK_I4
7947 && (tree->inst_right->opcode == CEE_CONV_I8
7948 || tree->inst_right->opcode == CEE_CONV_U8)
7949 && tree->inst_right->inst_left->type == STACK_I4
7950 && tree->inst_left->opcode == tree->inst_right->opcode) {
7951 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
7952 tree->inst_left = tree->inst_left->inst_left;
7953 tree->inst_right = tree->inst_right->inst_left;
7954 dec_foreach (tree, cfg);
7957 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
7958 MonoInst *iargs [2];
7960 iargs [0] = tree->inst_i0;
7961 iargs [1] = tree->inst_i1;
7963 mono_emulate_opcode (cfg, tree, iargs, info);
7965 dec_foreach (iargs [0], cfg);
7966 dec_foreach (iargs [1], cfg);
7969 dec_foreach (tree->inst_left, cfg);
7970 dec_foreach (tree->inst_right, cfg);
7974 g_assert_not_reached ();
7979 decompose_pass (MonoCompile *cfg) {
7982 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7985 cfg->prev_ins = NULL;
7986 for (tree = cfg->cbb->code; tree; tree = tree->next) {
7987 dec_foreach (tree, cfg);
7988 cfg->prev_ins = tree;
7994 nullify_basic_block (MonoBasicBlock *bb)
8001 bb->code = bb->last_ins = NULL;
8002 bb->cil_code = NULL;
8006 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
8010 for (i = 0; i < bb->out_count; i++) {
8011 MonoBasicBlock *ob = bb->out_bb [i];
8014 if (bb->out_count > 1) {
8015 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
8019 bb->out_bb [i] = repl;
8026 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
8030 for (i = 0; i < bb->in_count; i++) {
8031 MonoBasicBlock *ib = bb->in_bb [i];
8034 if (bb->in_count > 1) {
8035 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
8039 bb->in_bb [i] = repl;
8046 replace_or_add_in_block (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
8048 gboolean found = FALSE;
8051 for (i = 0; i < bb->in_count; i++) {
8052 MonoBasicBlock *ib = bb->in_bb [i];
8055 if (bb->in_count > 1) {
8056 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
8060 bb->in_bb [i] = repl;
8067 MonoBasicBlock **new_in_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (bb->in_count + 1));
8068 for (i = 0; i < bb->in_count; i++) {
8069 new_in_bb [i] = bb->in_bb [i];
8071 new_in_bb [i] = repl;
8073 bb->in_bb = new_in_bb;
8079 replace_out_block_in_code (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl) {
8082 for (inst = bb->code; inst != NULL; inst = inst->next) {
8083 if (inst->opcode == OP_CALL_HANDLER) {
8084 if (inst->inst_target_bb == orig) {
8085 inst->inst_target_bb = repl;
8089 if (bb->last_ins != NULL) {
8090 switch (bb->last_ins->opcode) {
8092 if (bb->last_ins->inst_target_bb == orig) {
8093 bb->last_ins->inst_target_bb = repl;
8098 int n = GPOINTER_TO_INT (bb->last_ins->klass);
8099 for (i = 0; i < n; i++ ) {
8100 if (bb->last_ins->inst_many_bb [i] == orig) {
8101 bb->last_ins->inst_many_bb [i] = repl;
8116 if (bb->last_ins->inst_true_bb == orig) {
8117 bb->last_ins->inst_true_bb = repl;
8119 if (bb->last_ins->inst_false_bb == orig) {
8120 bb->last_ins->inst_false_bb = repl;
8130 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
8134 for (i = 0; i < bb->out_count; i++) {
8135 MonoBasicBlock *ob = bb->out_bb [i];
8136 for (j = 0; j < ob->in_count; j++) {
8137 if (ob->in_bb [j] == orig) {
8138 ob->in_bb [j] = repl;
8146 * Check if a bb is useless (is just made of NOPs and ends with an
8147 * unconditional branch, or nothing).
8148 * If it is so, unlink it from the CFG and nullify it, and return TRUE.
8149 * Otherwise, return FALSE;
8152 remove_block_if_useless (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *previous_bb) {
8153 MonoBasicBlock *target_bb = NULL;
8156 /* Do not touch handlers */
8157 if (bb->region != -1) return FALSE;
8159 for (inst = bb->code; inst != NULL; inst = inst->next) {
8160 switch (inst->opcode) {
8164 target_bb = inst->inst_target_bb;
8171 if (target_bb == NULL) {
8172 if ((bb->out_count == 1) && (bb->out_bb [0] == bb->next_bb)) {
8173 target_bb = bb->next_bb;
8175 /* Do not touch empty BBs that do not "fall through" to their next BB (like the exit BB) */
8180 /* Do not touch BBs following a switch (they are the "default" branch) */
8181 if ((previous_bb->last_ins != NULL) && (previous_bb->last_ins->opcode == CEE_SWITCH)) {
8185 /* Do not touch BBs following the entry BB and jumping to something that is not */
8186 /* thiry "next" bb (the entry BB cannot contain the branch) */
8187 if ((previous_bb == cfg->bb_entry) && (bb->next_bb != target_bb)) {
8191 /* Check that there is a target BB, and that bb is not an empty loop (Bug 75061) */
8192 if ((target_bb != NULL) && (target_bb != bb)) {
8195 if (cfg->verbose_level > 1) {
8196 printf ("remove_block_if_useless %s, removed BB%d\n", mono_method_full_name (cfg->method, TRUE), bb->block_num);
8199 for (i = 0; i < bb->in_count; i++) {
8200 MonoBasicBlock *in_bb = bb->in_bb [i];
8201 replace_out_block (in_bb, bb, target_bb);
8202 replace_out_block_in_code (in_bb, bb, target_bb);
8203 if (bb->in_count == 1) {
8204 replace_in_block (target_bb, bb, in_bb);
8206 replace_or_add_in_block (cfg, target_bb, bb, in_bb);
8210 if ((previous_bb != cfg->bb_entry) &&
8211 (previous_bb->region == bb->region) &&
8212 ((previous_bb->last_ins == NULL) ||
8213 ((previous_bb->last_ins->opcode != CEE_BR) &&
8214 (! (MONO_IS_COND_BRANCH_OP (previous_bb->last_ins))) &&
8215 (previous_bb->last_ins->opcode != CEE_SWITCH)))) {
8216 for (i = 0; i < previous_bb->out_count; i++) {
8217 if (previous_bb->out_bb [i] == target_bb) {
8219 MONO_INST_NEW (cfg, jump, CEE_BR);
8220 MONO_ADD_INS (previous_bb, jump);
8221 jump->cil_code = previous_bb->cil_code;
8222 jump->inst_target_bb = target_bb;
8228 previous_bb->next_bb = bb->next_bb;
8229 nullify_basic_block (bb);
8238 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
8240 bb->out_count = bbn->out_count;
8241 bb->out_bb = bbn->out_bb;
8243 replace_basic_block (bb, bbn, bb);
8247 bb->last_ins->next = bbn->code;
8248 bb->last_ins = bbn->last_ins;
8251 bb->code = bbn->code;
8252 bb->last_ins = bbn->last_ins;
8254 bb->next_bb = bbn->next_bb;
8255 nullify_basic_block (bbn);
8259 move_basic_block_to_end (MonoCompile *cfg, MonoBasicBlock *bb)
8261 MonoBasicBlock *bbn, *next;
8265 /* Find the previous */
8266 for (bbn = cfg->bb_entry; bbn->next_bb && bbn->next_bb != bb; bbn = bbn->next_bb)
8269 bbn->next_bb = bb->next_bb;
8273 for (bbn = cfg->bb_entry; bbn->next_bb; bbn = bbn->next_bb)
8279 if (next && (!bb->last_ins || (bb->last_ins->opcode != OP_NOT_REACHED))) {
8282 MONO_INST_NEW (cfg, ins, CEE_BR);
8283 MONO_ADD_INS (bb, ins);
8284 link_bblock (cfg, bb, next);
8285 ins->inst_target_bb = next;
8290 * Optimizes the branches on the Control Flow Graph
8294 optimize_branches (MonoCompile *cfg)
8296 int i, changed = FALSE;
8297 MonoBasicBlock *bb, *bbn;
8298 guint32 niterations;
8301 * Some crazy loops could cause the code below to go into an infinite
8302 * loop, see bug #53003 for an example. To prevent this, we put an upper
8303 * bound on the number of iterations.
8305 if (cfg->num_bblocks > 1000)
8306 niterations = cfg->num_bblocks * 2;
8310 MonoBasicBlock *previous_bb;
8314 /* we skip the entry block (exit is handled specially instead ) */
8315 for (previous_bb = cfg->bb_entry, bb = cfg->bb_entry->next_bb; bb; previous_bb = bb, bb = bb->next_bb) {
8317 /* dont touch code inside exception clauses */
8318 if (bb->region != -1)
8321 if (remove_block_if_useless (cfg, bb, previous_bb)) {
8326 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
8327 if (cfg->verbose_level > 2)
8328 g_print ("nullify block triggered %d\n", bbn->block_num);
8330 bb->next_bb = bbn->next_bb;
8332 for (i = 0; i < bbn->out_count; i++)
8333 replace_in_block (bbn->out_bb [i], bbn, NULL);
8335 nullify_basic_block (bbn);
8339 if (bb->out_count == 1) {
8340 bbn = bb->out_bb [0];
8342 /* conditional branches where true and false targets are the same can be also replaced with CEE_BR */
8343 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins)) {
8345 MONO_INST_NEW (cfg, pop, CEE_POP);
8346 pop->inst_left = bb->last_ins->inst_left->inst_left;
8347 mono_add_ins_to_end (bb, pop);
8348 MONO_INST_NEW (cfg, pop, CEE_POP);
8349 pop->inst_left = bb->last_ins->inst_left->inst_right;
8350 mono_add_ins_to_end (bb, pop);
8351 bb->last_ins->opcode = CEE_BR;
8352 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
8354 if (cfg->verbose_level > 2)
8355 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
8358 if (bb->region == bbn->region && bb->next_bb == bbn) {
8359 /* the block are in sequence anyway ... */
8361 /* branches to the following block can be removed */
8362 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
8363 bb->last_ins->opcode = CEE_NOP;
8365 if (cfg->verbose_level > 2)
8366 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
8369 if (bbn->in_count == 1) {
8371 if (bbn != cfg->bb_exit) {
8372 if (cfg->verbose_level > 2)
8373 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
8374 merge_basic_blocks (bb, bbn);
8378 //mono_print_bb_code (bb);
8383 } while (changed && (niterations > 0));
8390 /* we skip the entry block (exit is handled specially instead ) */
8391 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
8393 /* dont touch code inside exception clauses */
8394 if (bb->region != -1)
8397 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
8398 if (cfg->verbose_level > 2) {
8399 g_print ("nullify block triggered %d\n", bbn->block_num);
8401 bb->next_bb = bbn->next_bb;
8403 for (i = 0; i < bbn->out_count; i++)
8404 replace_in_block (bbn->out_bb [i], bbn, NULL);
8406 nullify_basic_block (bbn);
8412 if (bb->out_count == 1) {
8413 bbn = bb->out_bb [0];
8415 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
8416 bbn = bb->last_ins->inst_target_bb;
8417 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
8418 bbn->code->inst_target_bb->region == bb->region) {
8420 if (cfg->verbose_level > 2)
8421 g_print ("in %s branch to branch triggered %d -> %d -> %d\n", cfg->method->name,
8422 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num);
8424 replace_in_block (bbn, bb, NULL);
8425 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
8426 link_bblock (cfg, bb, bbn->code->inst_target_bb);
8427 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
8432 } else if (bb->out_count == 2) {
8433 if (bb->last_ins && MONO_IS_COND_BRANCH_NOFP (bb->last_ins)) {
8434 int branch_result = mono_eval_cond_branch (bb->last_ins);
8435 MonoBasicBlock *taken_branch_target = NULL, *untaken_branch_target = NULL;
8436 if (branch_result == BRANCH_TAKEN) {
8437 taken_branch_target = bb->last_ins->inst_true_bb;
8438 untaken_branch_target = bb->last_ins->inst_false_bb;
8439 } else if (branch_result == BRANCH_NOT_TAKEN) {
8440 taken_branch_target = bb->last_ins->inst_false_bb;
8441 untaken_branch_target = bb->last_ins->inst_true_bb;
8443 if (taken_branch_target) {
8444 /* if mono_eval_cond_branch () is ever taken to handle
8445 * non-constant values to compare, issue a pop here.
8447 bb->last_ins->opcode = CEE_BR;
8448 bb->last_ins->inst_target_bb = taken_branch_target;
8449 replace_out_block (bb, untaken_branch_target, NULL);
8450 replace_in_block (untaken_branch_target, bb, NULL);
8454 bbn = bb->last_ins->inst_true_bb;
8455 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
8456 bbn->code->inst_target_bb->region == bb->region) {
8457 if (cfg->verbose_level > 2)
8458 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
8459 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
8462 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
8464 replace_in_block (bbn, bb, NULL);
8466 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
8467 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
8469 link_bblock (cfg, bb, bbn->code->inst_target_bb);
8475 bbn = bb->last_ins->inst_false_bb;
8476 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
8477 bbn->code->inst_target_bb->region == bb->region) {
8478 if (cfg->verbose_level > 2)
8479 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
8480 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
8483 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
8485 replace_in_block (bbn, bb, NULL);
8487 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
8488 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
8490 link_bblock (cfg, bb, bbn->code->inst_target_bb);
8497 if (bb->last_ins && MONO_IS_COND_BRANCH_NOFP (bb->last_ins)) {
8498 if (bb->last_ins->inst_false_bb->out_of_line && (bb->region == bb->last_ins->inst_false_bb->region)) {
8499 /* Reverse the branch */
8500 bb->last_ins->opcode = reverse_branch_op (bb->last_ins->opcode);
8501 bbn = bb->last_ins->inst_false_bb;
8502 bb->last_ins->inst_false_bb = bb->last_ins->inst_true_bb;
8503 bb->last_ins->inst_true_bb = bbn;
8505 move_basic_block_to_end (cfg, bb->last_ins->inst_true_bb);
8506 if (cfg->verbose_level > 2)
8507 g_print ("cbranch to throw block triggered %d.\n",
8513 } while (changed && (niterations > 0));
8518 mono_compile_create_vars (MonoCompile *cfg)
8520 MonoMethodSignature *sig;
8521 MonoMethodHeader *header;
8524 header = mono_method_get_header (cfg->method);
8526 sig = mono_method_signature (cfg->method);
8528 if (!MONO_TYPE_IS_VOID (sig->ret)) {
8529 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
8530 cfg->ret->opcode = OP_RETARG;
8531 cfg->ret->inst_vtype = sig->ret;
8532 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
8534 if (cfg->verbose_level > 2)
8535 g_print ("creating vars\n");
8538 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
8540 for (i = 0; i < sig->param_count; ++i) {
8541 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
8542 if (sig->params [i]->byref) {
8543 cfg->disable_ssa = TRUE;
8547 cfg->locals_start = cfg->num_varinfo;
8549 if (cfg->verbose_level > 2)
8550 g_print ("creating locals\n");
8551 for (i = 0; i < header->num_locals; ++i)
8552 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
8553 if (cfg->verbose_level > 2)
8554 g_print ("locals done\n");
8556 #ifdef MONO_ARCH_HAVE_CREATE_VARS
8557 mono_arch_create_vars (cfg);
8562 mono_print_code (MonoCompile *cfg)
8566 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
8567 MonoInst *tree = bb->code;
8572 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
8574 for (; tree; tree = tree->next) {
8575 mono_print_tree (tree);
8580 bb->last_ins->next = NULL;
8584 extern const char * const mono_burg_rule_string [];
8587 emit_state (MonoCompile *cfg, MBState *state, int goal)
8590 int ern = mono_burg_rule (state, goal);
8591 const guint16 *nts = mono_burg_nts [ern];
8594 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
8598 // state->reg1 = state->reg2; /* chain rule */
8600 #ifdef MONO_ARCH_ENABLE_EMIT_STATE_OPT
8603 state->reg1 = mono_regstate_next_int (cfg->rs);
8604 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
8607 state->reg1 = mono_regstate_next_int (cfg->rs);
8608 state->reg2 = mono_regstate_next_int (cfg->rs);
8611 state->reg1 = mono_regstate_next_float (cfg->rs);
8614 #ifdef MONO_ARCH_ENABLE_EMIT_STATE_OPT
8616 * Enabling this might cause bugs to surface in the local register
8617 * allocators on some architectures like x86.
8619 if ((state->tree->ssa_op == MONO_SSA_STORE) && (state->left->tree->opcode == OP_REGVAR)) {
8620 /* Do not optimize away reg-reg moves */
8621 if (! ((state->right->tree->ssa_op == MONO_SSA_LOAD) && (state->right->left->tree->opcode == OP_REGVAR))) {
8622 state->right->reg1 = state->left->tree->dreg;
8631 mono_burg_kids (state, ern, kids);
8633 emit_state (cfg, kids [0], nts [0]);
8635 emit_state (cfg, kids [1], nts [1]);
8637 g_assert (!nts [3]);
8638 emit_state (cfg, kids [2], nts [2]);
8643 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
8644 if ((emit = mono_burg_func [ern]))
8645 emit (state, state->tree, cfg);
8648 #define DEBUG_SELECTION
8651 mini_select_instructions (MonoCompile *cfg)
8655 cfg->state_pool = mono_mempool_new ();
8656 cfg->rs = mono_regstate_new ();
8658 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
8659 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins) &&
8660 bb->next_bb != bb->last_ins->inst_false_bb) {
8662 /* we are careful when inverting, since bugs like #59580
8663 * could show up when dealing with NaNs.
8665 if (MONO_IS_COND_BRANCH_NOFP(bb->last_ins) && bb->next_bb == bb->last_ins->inst_true_bb) {
8666 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
8667 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
8668 bb->last_ins->inst_false_bb = tmp;
8670 bb->last_ins->opcode = reverse_branch_op (bb->last_ins->opcode);
8672 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
8673 inst->opcode = CEE_BR;
8674 inst->inst_target_bb = bb->last_ins->inst_false_bb;
8675 mono_bblock_add_inst (bb, inst);
8680 #ifdef DEBUG_SELECTION
8681 if (cfg->verbose_level >= 4) {
8682 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
8683 MonoInst *tree = bb->code;
8684 g_print ("DUMP BLOCK %d:\n", bb->block_num);
8687 for (; tree; tree = tree->next) {
8688 mono_print_tree (tree);
8695 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
8696 MonoInst *tree = bb->code, *next;
8702 bb->last_ins = NULL;
8705 mono_regstate_reset (cfg->rs);
8707 #ifdef DEBUG_SELECTION
8708 if (cfg->verbose_level >= 3)
8709 g_print ("LABEL BLOCK %d:\n", bb->block_num);
8711 for (; tree; tree = next) {
8713 #ifdef DEBUG_SELECTION
8714 if (cfg->verbose_level >= 3) {
8715 mono_print_tree (tree);
8720 if (!(mbstate = mono_burg_label (tree, cfg))) {
8721 g_warning ("unable to label tree %p", tree);
8722 mono_print_tree (tree);
8724 g_assert_not_reached ();
8726 emit_state (cfg, mbstate, MB_NTERM_stmt);
8728 bb->max_ireg = cfg->rs->next_vireg;
8729 bb->max_freg = cfg->rs->next_vfreg;
8732 bb->last_ins->next = NULL;
8734 mono_mempool_empty (cfg->state_pool);
8736 mono_mempool_destroy (cfg->state_pool);
8740 mono_codegen (MonoCompile *cfg)
8742 MonoJumpInfo *patch_info;
8744 int i, max_epilog_size;
8747 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
8748 cfg->spill_count = 0;
8749 /* we reuse dfn here */
8750 /* bb->dfn = bb_count++; */
8751 mono_arch_local_regalloc (cfg, bb);
8754 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
8755 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
8757 code = mono_arch_emit_prolog (cfg);
8759 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
8760 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
8762 cfg->code_len = code - cfg->native_code;
8763 cfg->prolog_end = cfg->code_len;
8765 mono_debug_open_method (cfg);
8767 /* emit code all basic blocks */
8768 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
8769 bb->native_offset = cfg->code_len;
8770 mono_arch_output_basic_block (cfg, bb);
8772 if (bb == cfg->bb_exit) {
8773 cfg->epilog_begin = cfg->code_len;
8775 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE) {
8776 code = cfg->native_code + cfg->code_len;
8777 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
8778 cfg->code_len = code - cfg->native_code;
8781 mono_arch_emit_epilog (cfg);
8785 mono_arch_emit_exceptions (cfg);
8787 max_epilog_size = 0;
8789 code = cfg->native_code + cfg->code_len;
8791 /* we always allocate code in cfg->domain->code_mp to increase locality */
8792 cfg->code_size = cfg->code_len + max_epilog_size;
8793 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
8795 if (cfg->method->dynamic) {
8796 /* Allocate the code into a separate memory pool so it can be freed */
8797 cfg->dynamic_info = g_new0 (MonoJitDynamicMethodInfo, 1);
8798 cfg->dynamic_info->code_mp = mono_code_manager_new_dynamic ();
8799 mono_domain_lock (cfg->domain);
8800 mono_dynamic_code_hash_insert (cfg->domain, cfg->method, cfg->dynamic_info);
8801 mono_domain_unlock (cfg->domain);
8803 code = mono_code_manager_reserve (cfg->dynamic_info->code_mp, cfg->code_size);
8805 mono_domain_lock (cfg->domain);
8806 code = mono_code_manager_reserve (cfg->domain->code_mp, cfg->code_size);
8807 mono_domain_unlock (cfg->domain);
8810 memcpy (code, cfg->native_code, cfg->code_len);
8811 g_free (cfg->native_code);
8812 cfg->native_code = code;
8813 code = cfg->native_code + cfg->code_len;
8815 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
8816 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
8817 switch (patch_info->type) {
8818 case MONO_PATCH_INFO_ABS: {
8819 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
8821 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
8822 if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) &&
8823 strstr (cfg->method->name, info->name))
8825 * This is an icall wrapper, and this is a call to the
8830 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
8831 patch_info->data.name = info->name;
8835 MonoVTable *vtable = mono_find_class_init_trampoline_by_addr (patch_info->data.target);
8837 patch_info->type = MONO_PATCH_INFO_CLASS_INIT;
8838 patch_info->data.klass = vtable->klass;
8843 case MONO_PATCH_INFO_SWITCH: {
8845 if (cfg->method->dynamic) {
8846 table = mono_code_manager_reserve (cfg->dynamic_info->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
8848 mono_domain_lock (cfg->domain);
8849 table = mono_code_manager_reserve (cfg->domain->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
8850 mono_domain_unlock (cfg->domain);
8853 if (!cfg->compile_aot)
8854 /* In the aot case, the patch already points to the correct location */
8855 patch_info->ip.i = patch_info->ip.label->inst_c0;
8856 for (i = 0; i < patch_info->data.table->table_size; i++) {
8857 table [i] = GINT_TO_POINTER (patch_info->data.table->table [i]->native_offset);
8859 patch_info->data.table->table = (MonoBasicBlock**)table;
8868 if (cfg->verbose_level > 0) {
8869 char* nm = mono_method_full_name (cfg->method, TRUE);
8870 g_print ("Method %s emitted at %p to %p [%s]\n",
8872 cfg->native_code, cfg->native_code + cfg->code_len, cfg->domain->friendly_name);
8876 #ifdef MONO_ARCH_HAVE_SAVE_UNWIND_INFO
8877 mono_arch_save_unwind_info (cfg);
8880 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info, cfg->run_cctors);
8882 if (cfg->method->dynamic) {
8883 mono_code_manager_commit (cfg->dynamic_info->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
8885 mono_domain_lock (cfg->domain);
8886 mono_code_manager_commit (cfg->domain->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
8887 mono_domain_unlock (cfg->domain);
8890 mono_arch_flush_icache (cfg->native_code, cfg->code_len);
8892 mono_debug_close_method (cfg);
8896 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
8901 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
8902 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
8904 if (cp->opcode == OP_ICONST) {
8905 if (cfg->opt & MONO_OPT_CONSPROP) {
8906 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
8910 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
8911 if (cfg->opt & MONO_OPT_COPYPROP) {
8912 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
8918 arity = mono_burg_arity [tree->opcode];
8921 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
8922 if (cfg->opt & MONO_OPT_CFOLD)
8923 mono_constant_fold_inst (tree, NULL);
8924 /* The opcode may have changed */
8925 if (mono_burg_arity [tree->opcode] > 1) {
8926 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
8927 if (cfg->opt & MONO_OPT_CFOLD)
8928 mono_constant_fold_inst (tree, NULL);
8930 mono_constant_fold_inst (tree, NULL);
8936 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
8940 switch (tree->opcode) {
8950 if ((tree->ssa_op == MONO_SSA_NOP) || (tree->ssa_op & MONO_SSA_ADDRESS_TAKEN)) {
8951 memset (acp, 0, sizeof (MonoInst *) * acp_size);
8968 case OP_VOIDCALL_REG:
8969 case OP_VOIDCALLVIRT:
8971 MonoCallInst *call = (MonoCallInst *)tree;
8972 MonoMethodSignature *sig = call->signature;
8973 int i, byref = FALSE;
8975 for (i = 0; i < sig->param_count; i++) {
8976 if (sig->params [i]->byref) {
8983 memset (acp, 0, sizeof (MonoInst *) * acp_size);
8991 arity = mono_burg_arity [tree->opcode];
8997 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
9000 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
9001 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
9004 g_assert_not_reached ();
9009 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
9011 MonoInst *tree = bb->code;
9017 for (; tree; tree = tree->next) {
9019 mono_cprop_copy_values (cfg, tree, acp);
9021 mono_cprop_invalidate_values (tree, acp, acp_size);
9023 if (tree->ssa_op == MONO_SSA_STORE &&
9024 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
9025 MonoInst *i1 = tree->inst_i1;
9027 acp [tree->inst_i0->inst_c0] = NULL;
9029 for (i = 0; i < acp_size; i++) {
9030 if (acp [i] && acp [i]->opcode != OP_ICONST &&
9031 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
9036 if (i1->opcode == OP_ICONST) {
9037 acp [tree->inst_i0->inst_c0] = i1;
9038 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
9040 if (i1->ssa_op == MONO_SSA_LOAD &&
9041 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
9042 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
9043 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
9044 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
9049 if (tree->opcode == CEE_BEQ) {
9050 g_assert (tree->inst_i0->opcode == OP_COMPARE);
9051 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
9052 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
9054 tree->opcode = CEE_BR;
9055 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
9056 tree->inst_target_bb = tree->inst_true_bb;
9058 tree->inst_target_bb = tree->inst_false_bb;
9067 mono_local_cprop (MonoCompile *cfg)
9072 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
9074 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
9075 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
9076 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
9081 remove_critical_edges (MonoCompile *cfg) {
9083 MonoBasicBlock *previous_bb;
9085 if (cfg->verbose_level > 3) {
9086 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
9088 printf ("remove_critical_edges %s, BEFORE BB%d (in:", mono_method_full_name (cfg->method, TRUE), bb->block_num);
9089 for (i = 0; i < bb->in_count; i++) {
9090 printf (" %d", bb->in_bb [i]->block_num);
9093 for (i = 0; i < bb->out_count; i++) {
9094 printf (" %d", bb->out_bb [i]->block_num);
9097 if (bb->last_ins != NULL) {
9099 mono_print_tree (bb->last_ins);
9105 for (previous_bb = cfg->bb_entry, bb = previous_bb->next_bb; bb != NULL; previous_bb = previous_bb->next_bb, bb = bb->next_bb) {
9106 if (bb->in_count > 1) {
9108 for (in_bb_index = 0; in_bb_index < bb->in_count; in_bb_index++) {
9109 MonoBasicBlock *in_bb = bb->in_bb [in_bb_index];
9110 if (in_bb->out_count > 1) {
9111 MonoBasicBlock *new_bb = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock));
9112 new_bb->block_num = cfg->num_bblocks++;
9113 // new_bb->real_offset = bb->real_offset;
9114 new_bb->region = bb->region;
9116 /* Do not alter the CFG while altering the BB list */
9117 if (previous_bb->region == bb->region) {
9118 if (previous_bb != cfg->bb_entry) {
9119 /* If previous_bb "followed through" to bb, */
9120 /* keep it linked with a CEE_BR */
9121 if ((previous_bb->last_ins == NULL) ||
9122 ((previous_bb->last_ins->opcode != CEE_BR) &&
9123 (! (MONO_IS_COND_BRANCH_OP (previous_bb->last_ins))) &&
9124 (previous_bb->last_ins->opcode != CEE_SWITCH))) {
9126 /* Make sure previous_bb really falls through bb */
9127 for (i = 0; i < previous_bb->out_count; i++) {
9128 if (previous_bb->out_bb [i] == bb) {
9130 MONO_INST_NEW (cfg, jump, CEE_BR);
9131 MONO_ADD_INS (previous_bb, jump);
9132 jump->cil_code = previous_bb->cil_code;
9133 jump->inst_target_bb = bb;
9139 /* We cannot add any inst to the entry BB, so we must */
9140 /* put a new BB in the middle to hold the CEE_BR */
9142 MonoBasicBlock *new_bb_after_entry = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock));
9143 new_bb_after_entry->block_num = cfg->num_bblocks++;
9144 // new_bb_after_entry->real_offset = bb->real_offset;
9145 new_bb_after_entry->region = bb->region;
9147 MONO_INST_NEW (cfg, jump, CEE_BR);
9148 MONO_ADD_INS (new_bb_after_entry, jump);
9149 jump->cil_code = bb->cil_code;
9150 jump->inst_target_bb = bb;
9152 previous_bb->next_bb = new_bb_after_entry;
9153 previous_bb = new_bb_after_entry;
9155 if (cfg->verbose_level > 2) {
9156 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);
9161 /* Insert new_bb in the BB list */
9162 previous_bb->next_bb = new_bb;
9163 new_bb->next_bb = bb;
9164 previous_bb = new_bb;
9166 /* Setup in_bb and out_bb */
9167 new_bb->in_bb = mono_mempool_alloc ((cfg)->mempool, sizeof (MonoBasicBlock*));
9168 new_bb->in_bb [0] = in_bb;
9169 new_bb->in_count = 1;
9170 new_bb->out_bb = mono_mempool_alloc ((cfg)->mempool, sizeof (MonoBasicBlock*));
9171 new_bb->out_bb [0] = bb;
9172 new_bb->out_count = 1;
9174 /* Relink in_bb and bb to (from) new_bb */
9175 replace_out_block (in_bb, bb, new_bb);
9176 replace_out_block_in_code (in_bb, bb, new_bb);
9177 replace_in_block (bb, in_bb, new_bb);
9179 if (cfg->verbose_level > 2) {
9180 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);
9187 if (cfg->verbose_level > 3) {
9188 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
9190 printf ("remove_critical_edges %s, AFTER BB%d (in:", mono_method_full_name (cfg->method, TRUE), bb->block_num);
9191 for (i = 0; i < bb->in_count; i++) {
9192 printf (" %d", bb->in_bb [i]->block_num);
9195 for (i = 0; i < bb->out_count; i++) {
9196 printf (" %d", bb->out_bb [i]->block_num);
9199 if (bb->last_ins != NULL) {
9201 mono_print_tree (bb->last_ins);
9209 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, gboolean compile_aot, int parts)
9211 MonoMethodHeader *header = mono_method_get_header (method);
9215 int dfn = 0, i, code_size_ratio;
9216 gboolean deadce_has_run = FALSE;
9221 ip = (guint8 *)header->code;
9223 mono_jit_stats.methods_compiled++;
9224 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
9225 mono_profiler_method_jit (method);
9227 cfg = g_new0 (MonoCompile, 1);
9228 cfg->method = method;
9229 cfg->mempool = mono_mempool_new ();
9231 cfg->prof_options = mono_profiler_get_events ();
9232 cfg->run_cctors = run_cctors;
9233 cfg->bb_hash = g_hash_table_new (NULL, NULL);
9234 cfg->domain = domain;
9235 cfg->verbose_level = mini_verbose;
9236 cfg->compile_aot = compile_aot;
9237 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
9238 mono_method_get_header (method)->max_stack);
9239 cfg->aliasing_info = NULL;
9241 if (cfg->verbose_level > 2)
9242 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
9245 * create MonoInst* which represents arguments and local variables
9247 mono_compile_create_vars (cfg);
9249 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
9250 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
9251 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
9252 mono_destroy_compile (cfg);
9256 mono_jit_stats.basic_blocks += cfg->num_bblocks;
9257 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
9259 if ((cfg->num_varinfo > 2000) && !mono_compile_aot) {
9261 * we disable some optimizations if there are too many variables
9262 * because JIT time may become too expensive. The actual number needs
9263 * to be tweaked and eventually the non-linear algorithms should be fixed.
9265 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
9266 cfg->disable_ssa = TRUE;
9269 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
9271 if (cfg->opt & MONO_OPT_BRANCH)
9272 optimize_branches (cfg);
9274 if (cfg->opt & MONO_OPT_SSAPRE) {
9275 remove_critical_edges (cfg);
9278 /* Depth-first ordering on basic blocks */
9279 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
9281 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
9282 if (cfg->num_bblocks != dfn + 1) {
9285 cfg->num_bblocks = dfn + 1;
9287 if (!header->clauses) {
9288 /* remove unreachable code, because the code in them may be
9289 * inconsistent (access to dead variables for example) */
9290 for (bb = cfg->bb_entry; bb;) {
9291 MonoBasicBlock *bbn = bb->next_bb;
9293 if (bbn && bbn->region == -1 && !bbn->dfn) {
9294 if (cfg->verbose_level > 1)
9295 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
9296 bb->next_bb = bbn->next_bb;
9297 nullify_basic_block (bbn);
9305 if (cfg->opt & MONO_OPT_LOOP) {
9306 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
9307 mono_compute_natural_loops (cfg);
9310 /* after method_to_ir */
9314 //#define DEBUGSSA "logic_run"
9315 #define DEBUGSSA_CLASS "Tests"
9318 if (!header->num_clauses && !cfg->disable_ssa) {
9319 mono_local_cprop (cfg);
9320 mono_ssa_compute (cfg);
9324 /* fixme: add all optimizations which requires SSA */
9325 if (cfg->opt & (MONO_OPT_SSA | MONO_OPT_ABCREM | MONO_OPT_SSAPRE)) {
9326 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
9327 mono_local_cprop (cfg);
9328 mono_ssa_compute (cfg);
9330 if (cfg->verbose_level >= 2) {
9337 /* after SSA translation */
9341 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
9342 if (cfg->comp_done & MONO_COMP_SSA) {
9343 mono_ssa_cprop (cfg);
9345 mono_local_cprop (cfg);
9349 if (cfg->comp_done & MONO_COMP_SSA) {
9350 //mono_ssa_deadce (cfg);
9352 //mono_ssa_strength_reduction (cfg);
9354 if (cfg->opt & MONO_OPT_SSAPRE) {
9355 mono_perform_ssapre (cfg);
9356 //mono_local_cprop (cfg);
9359 if (cfg->opt & MONO_OPT_DEADCE) {
9360 mono_ssa_deadce (cfg);
9361 deadce_has_run = TRUE;
9364 if ((cfg->flags & MONO_CFG_HAS_LDELEMA) && (cfg->opt & MONO_OPT_ABCREM))
9365 mono_perform_abc_removal (cfg);
9367 mono_ssa_remove (cfg);
9369 if (cfg->opt & MONO_OPT_BRANCH)
9370 optimize_branches (cfg);
9373 /* after SSA removal */
9377 decompose_pass (cfg);
9382 g_assert (cfg->got_var_allocated);
9385 * Allways allocate the GOT var to a register, because keeping it
9386 * in memory will increase the number of live temporaries in some
9387 * code created by inssel.brg, leading to the well known spills+
9388 * branches problem. Testcase: mcs crash in
9389 * System.MonoCustomAttrs:GetCustomAttributes.
9391 regs = mono_arch_get_global_int_regs (cfg);
9393 cfg->got_var->opcode = OP_REGVAR;
9394 cfg->got_var->dreg = GPOINTER_TO_INT (regs->data);
9395 cfg->used_int_regs |= 1LL << cfg->got_var->dreg;
9400 if (cfg->opt & MONO_OPT_LINEARS) {
9403 /* For now, compute aliasing info only if needed for deadce... */
9404 if ((cfg->opt & MONO_OPT_DEADCE) && (! deadce_has_run) && (header->num_clauses == 0)) {
9405 cfg->aliasing_info = mono_build_aliasing_information (cfg);
9408 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
9409 cfg->comp_done &= ~MONO_COMP_LIVENESS;
9410 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
9411 mono_analyze_liveness (cfg);
9413 if (cfg->aliasing_info != NULL) {
9414 mono_aliasing_deadce (cfg->aliasing_info);
9415 deadce_has_run = TRUE;
9418 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
9419 regs = mono_arch_get_global_int_regs (cfg);
9421 regs = g_list_delete_link (regs, regs);
9422 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
9425 if (cfg->aliasing_info != NULL) {
9426 mono_destroy_aliasing_information (cfg->aliasing_info);
9427 cfg->aliasing_info = NULL;
9431 //mono_print_code (cfg);
9435 /* variables are allocated after decompose, since decompose could create temps */
9436 mono_arch_allocate_vars (cfg);
9438 if (cfg->opt & MONO_OPT_CFOLD)
9439 mono_constant_fold (cfg);
9441 mini_select_instructions (cfg);
9444 if (cfg->verbose_level >= 2) {
9445 char *id = mono_method_full_name (cfg->method, FALSE);
9446 mono_disassemble_code (cfg, cfg->native_code, cfg->code_len, id + 3);
9450 if (cfg->method->dynamic) {
9451 jinfo = g_malloc0 (sizeof (MonoJitInfo) + (header->num_clauses * sizeof (MonoJitExceptionInfo)));
9453 /* we access cfg->domain->mp */
9454 mono_domain_lock (cfg->domain);
9455 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo) + (header->num_clauses * sizeof (MonoJitExceptionInfo)));
9456 mono_domain_unlock (cfg->domain);
9459 jinfo->method = method;
9460 jinfo->code_start = cfg->native_code;
9461 jinfo->code_size = cfg->code_len;
9462 jinfo->used_regs = cfg->used_int_regs;
9463 jinfo->domain_neutral = (cfg->opt & MONO_OPT_SHARED) != 0;
9464 jinfo->cas_inited = FALSE; /* initialization delayed at the first stalk walk using this method */
9466 if (header->num_clauses) {
9469 jinfo->num_clauses = header->num_clauses;
9471 for (i = 0; i < header->num_clauses; i++) {
9472 MonoExceptionClause *ec = &header->clauses [i];
9473 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
9474 MonoBasicBlock *tblock;
9477 ei->flags = ec->flags;
9479 exvar = mono_find_exvar_for_offset (cfg, ec->handler_offset);
9480 ei->exvar_offset = exvar ? exvar->inst_offset : 0;
9482 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
9483 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->data.filter_offset);
9485 ei->data.filter = cfg->native_code + tblock->native_offset;
9487 ei->data.catch_class = ec->data.catch_class;
9490 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
9492 ei->try_start = cfg->native_code + tblock->native_offset;
9493 g_assert (tblock->native_offset);
9494 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
9496 ei->try_end = cfg->native_code + tblock->native_offset;
9497 g_assert (tblock->native_offset);
9498 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
9500 ei->handler_start = cfg->native_code + tblock->native_offset;
9504 cfg->jit_info = jinfo;
9505 #if defined(__arm__)
9506 mono_arch_fixup_jinfo (cfg);
9509 mono_domain_lock (cfg->domain);
9510 mono_jit_info_table_add (cfg->domain, jinfo);
9512 if (cfg->method->dynamic)
9513 mono_dynamic_code_hash_lookup (cfg->domain, cfg->method)->ji = jinfo;
9514 mono_domain_unlock (cfg->domain);
9516 /* collect statistics */
9517 mono_jit_stats.allocated_code_size += cfg->code_len;
9518 code_size_ratio = cfg->code_len;
9519 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
9520 mono_jit_stats.biggest_method_size = code_size_ratio;
9521 mono_jit_stats.biggest_method = method;
9523 code_size_ratio = (code_size_ratio * 100) / mono_method_get_header (method)->code_size;
9524 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
9525 mono_jit_stats.max_code_size_ratio = code_size_ratio;
9526 mono_jit_stats.max_ratio_method = method;
9528 mono_jit_stats.native_code_size += cfg->code_len;
9530 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
9531 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
9533 /* this can only be set if the security manager is active */
9534 if (cfg->exception_type == MONO_EXCEPTION_SECURITY_LINKDEMAND) {
9535 MonoAssembly *assembly = mono_image_get_assembly (method->klass->image);
9536 MonoReflectionAssembly *refass = (MonoReflectionAssembly*) mono_assembly_get_object (domain, assembly);
9537 MonoReflectionMethod *refmet = mono_method_get_object (domain, method, NULL);
9538 MonoSecurityManager* secman = mono_security_manager_get_methods ();
9539 MonoObject *exc = NULL;
9542 args [0] = &cfg->exception_data;
9545 mono_runtime_invoke (secman->linkdemandsecurityexception, NULL, args, &exc);
9547 mono_destroy_compile (cfg);
9550 mono_raise_exception ((MonoException*)exc);
9557 mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain)
9560 GHashTable *jit_code_hash;
9561 gpointer code = NULL;
9567 jit_code_hash = target_domain->jit_code_hash;
9569 method = mono_get_inflated_method (method);
9571 #ifdef MONO_USE_AOT_COMPILER
9572 if (!mono_compile_aot && (opt & MONO_OPT_AOT) && !(mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)) {
9574 MonoDomain *domain = mono_domain_get ();
9576 mono_domain_lock (domain);
9578 mono_class_init (method->klass);
9579 if ((info = mono_aot_get_method (domain, method))) {
9580 g_hash_table_insert (domain->jit_code_hash, method, info);
9581 mono_domain_unlock (domain);
9582 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
9583 return info->code_start;
9586 mono_domain_unlock (domain);
9590 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
9591 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
9593 MonoMethodPInvoke* piinfo = (MonoMethodPInvoke *) method;
9595 if (method->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE)
9596 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);
9598 if (!piinfo->addr) {
9599 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
9600 piinfo->addr = mono_lookup_internal_call (method);
9602 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
9603 mono_lookup_pinvoke_call (method, NULL, NULL);
9605 nm = mono_marshal_get_native_wrapper (method);
9606 return mono_get_addr_from_ftnptr (mono_compile_method (nm));
9608 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
9609 //mono_debug_add_wrapper (method, nm);
9610 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
9611 const char *name = method->name;
9614 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
9615 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
9616 MonoJitICallInfo *mi = mono_find_jit_icall_by_name ("mono_delegate_ctor");
9618 return mono_get_addr_from_ftnptr ((gpointer)mono_icall_get_wrapper (mi));
9619 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
9620 nm = mono_marshal_get_delegate_invoke (method);
9621 return mono_get_addr_from_ftnptr (mono_compile_method (nm));
9622 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
9623 nm = mono_marshal_get_delegate_begin_invoke (method);
9624 return mono_get_addr_from_ftnptr (mono_compile_method (nm));
9625 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
9626 nm = mono_marshal_get_delegate_end_invoke (method);
9627 return mono_get_addr_from_ftnptr (mono_compile_method (nm));
9633 cfg = mini_method_compile (method, opt, target_domain, TRUE, FALSE, 0);
9636 /* Throw a type load exception if needed */
9637 MonoLoaderError *error = mono_loader_get_last_error ();
9640 MonoException *ex = mini_loader_error_to_exception (error);
9641 mono_loader_clear_error ();
9642 mono_raise_exception (ex);
9645 g_assert_not_reached ();
9648 mono_domain_lock (target_domain);
9650 /* Check if some other thread already did the job. In this case, we can
9651 discard the code this thread generated. */
9653 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
9654 /* We can't use a domain specific method in another domain */
9655 if ((target_domain == mono_domain_get ()) || info->domain_neutral) {
9656 code = info->code_start;
9657 // printf("Discarding code for method %s\n", method->name);
9662 g_hash_table_insert (jit_code_hash, method, cfg->jit_info);
9663 code = cfg->native_code;
9666 mono_destroy_compile (cfg);
9668 if (target_domain->jump_target_hash) {
9669 MonoJumpInfo patch_info;
9671 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
9673 patch_info.next = NULL;
9674 patch_info.ip.i = 0;
9675 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
9676 patch_info.data.method = method;
9677 g_hash_table_remove (target_domain->jump_target_hash, method);
9679 for (tmp = list; tmp; tmp = tmp->next)
9680 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info, TRUE);
9681 g_slist_free (list);
9684 mono_domain_unlock (target_domain);
9686 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
9691 mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt)
9693 /* FIXME: later copy the code from mono */
9694 MonoDomain *target_domain, *domain = mono_domain_get ();
9698 if (opt & MONO_OPT_SHARED)
9699 target_domain = mono_get_root_domain ();
9701 target_domain = domain;
9703 mono_domain_lock (target_domain);
9705 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
9706 /* We can't use a domain specific method in another domain */
9707 if (! ((domain != target_domain) && !info->domain_neutral)) {
9708 mono_domain_unlock (target_domain);
9709 mono_jit_stats.methods_lookups++;
9710 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
9711 return mono_create_ftnptr (target_domain, info->code_start);
9715 mono_domain_unlock (target_domain);
9716 p = mono_jit_compile_method_inner (method, target_domain);
9717 return mono_create_ftnptr (target_domain, p);
9721 mono_jit_compile_method (MonoMethod *method)
9723 return mono_jit_compile_method_with_opt (method, default_opt);
9727 invalidated_delegate_trampoline (char *desc)
9729 g_error ("Unmanaged code called delegate of type %s which was already garbage collected.\n"
9730 "See http://www.go-mono.com/delegate.html for an explanation and ways to fix this.",
9735 * mono_jit_free_method:
9737 * Free all memory allocated by the JIT for METHOD.
9740 mono_jit_free_method (MonoDomain *domain, MonoMethod *method)
9742 MonoJitDynamicMethodInfo *ji;
9743 gboolean destroy = TRUE;
9745 g_assert (method->dynamic);
9747 mono_domain_lock (domain);
9748 ji = mono_dynamic_code_hash_lookup (domain, method);
9749 mono_domain_unlock (domain);
9753 mono_domain_lock (domain);
9754 g_hash_table_remove (domain->dynamic_code_hash, method);
9755 g_hash_table_remove (domain->jit_code_hash, method);
9756 mono_domain_unlock (domain);
9758 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
9759 if (debug_options.keep_delegates && method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
9761 * Instead of freeing the code, change it to call an error routine
9762 * so people can fix their code.
9764 char *type = mono_type_full_name (&method->klass->byval_arg);
9765 char *type_and_method = g_strdup_printf ("%s.%s", type, method->name);
9768 mono_arch_invalidate_method (ji->ji, invalidated_delegate_trampoline, type_and_method);
9774 * This needs to be done before freeing code_mp, since the code address is the
9775 * key in the table, so if we the code_mp first, another thread can grab the
9776 * same code address and replace our entry in the table.
9778 mono_jit_info_table_remove (domain, ji->ji);
9781 mono_code_manager_destroy (ji->code_mp);
9787 mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method)
9789 MonoDomain *target_domain;
9792 if (default_opt & MONO_OPT_SHARED)
9793 target_domain = mono_get_root_domain ();
9795 target_domain = domain;
9797 mono_domain_lock (target_domain);
9799 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
9800 /* We can't use a domain specific method in another domain */
9801 if (! ((domain != target_domain) && !info->domain_neutral)) {
9802 mono_domain_unlock (target_domain);
9803 mono_jit_stats.methods_lookups++;
9804 return info->code_start;
9808 mono_domain_unlock (target_domain);
9814 * mono_jit_runtime_invoke:
9815 * @method: the method to invoke
9816 * @obj: this pointer
9817 * @params: array of parameter values.
9818 * @exc: used to catch exceptions objects
9821 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
9824 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc, void* compiled_method);
9825 void* compiled_method;
9827 if (obj == NULL && !(method->flags & METHOD_ATTRIBUTE_STATIC) && !method->string_ctor && (method->wrapper_type == 0)) {
9828 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
9832 method = mono_get_inflated_method (method);
9833 invoke = mono_marshal_get_runtime_invoke (method);
9834 runtime_invoke = mono_jit_compile_method (invoke);
9836 /* We need this here becuase mono_marshal_get_runtime_invoke can be place
9837 * the helper method in System.Object and not the target class
9839 mono_runtime_class_init (mono_class_vtable (mono_domain_get (), method->klass));
9841 compiled_method = mono_jit_compile_method (method);
9842 return runtime_invoke (obj, params, exc, compiled_method);
9845 #ifdef PLATFORM_WIN32
9846 #define GET_CONTEXT \
9847 struct sigcontext *ctx = (struct sigcontext*)_dummy;
9850 #define GET_CONTEXT \
9851 void *ctx = context;
9852 #elif defined(sun) // Solaris x86
9853 #define GET_CONTEXT \
9854 ucontext_t *uctx = context; \
9855 struct sigcontext *ctx = (struct sigcontext *)&(uctx->uc_mcontext);
9856 #elif defined (MONO_ARCH_USE_SIGACTION)
9857 #define GET_CONTEXT \
9858 void *ctx = context;
9860 #define GET_CONTEXT \
9861 void **_p = (void **)&_dummy; \
9862 struct sigcontext *ctx = (struct sigcontext *)++_p;
9866 #ifdef MONO_ARCH_USE_SIGACTION
9867 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, siginfo_t *info, void *context)
9869 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy)
9873 SIG_HANDLER_SIGNATURE (sigfpe_signal_handler)
9875 MonoException *exc = NULL;
9876 #ifndef MONO_ARCH_USE_SIGACTION
9881 #if defined(MONO_ARCH_HAVE_IS_INT_OVERFLOW)
9882 if (mono_arch_is_int_overflow (ctx, info))
9883 exc = mono_get_exception_arithmetic ();
9885 exc = mono_get_exception_divide_by_zero ();
9887 exc = mono_get_exception_divide_by_zero ();
9890 mono_arch_handle_exception (ctx, exc, FALSE);
9894 SIG_HANDLER_SIGNATURE (sigill_signal_handler)
9899 exc = mono_get_exception_execution_engine ("SIGILL");
9901 mono_arch_handle_exception (ctx, exc, FALSE);
9904 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
9906 #ifndef MONO_ARCH_USE_SIGACTION
9907 #error "Can't use sigaltstack without sigaction"
9913 SIG_HANDLER_SIGNATURE (sigsegv_signal_handler)
9915 MonoException *exc = NULL;
9918 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
9919 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
9923 #ifdef MONO_ARCH_USE_SIGACTION
9924 if (debug_options.collect_pagefault_stats) {
9925 if (mono_raw_buffer_is_pagefault (info->si_addr)) {
9926 mono_raw_buffer_handle_pagefault (info->si_addr);
9929 if (mono_aot_is_pagefault (info->si_addr)) {
9930 mono_aot_handle_pagefault (info->si_addr);
9936 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
9937 /* Can't allocate memory using Boehm GC on altstack */
9938 if (jit_tls->stack_size &&
9939 ((guint8*)info->si_addr >= (guint8*)jit_tls->end_of_stack - jit_tls->stack_size) &&
9940 ((guint8*)info->si_addr < (guint8*)jit_tls->end_of_stack))
9941 exc = mono_domain_get ()->stack_overflow_ex;
9943 exc = mono_domain_get ()->null_reference_ex;
9946 ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx));
9948 mono_handle_native_sigsegv (ctx);
9951 mono_arch_handle_exception (ctx, exc, FALSE);
9955 SIG_HANDLER_SIGNATURE (sigusr1_signal_handler)
9957 gboolean running_managed;
9965 * This is an async signal, so the code below must not call anything which
9966 * is not async safe. That includes the pthread locking functions. If we
9967 * know that we interrupted managed code, then locking is safe.
9969 ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx));
9970 running_managed = ji != NULL;
9972 exc = mono_thread_request_interruption (running_managed);
9975 mono_arch_handle_exception (ctx, exc, FALSE);
9979 SIG_HANDLER_SIGNATURE (sigprof_signal_handler)
9983 mono_profiler_stat_hit (mono_arch_ip_from_context (ctx), ctx);
9987 SIG_HANDLER_SIGNATURE (sigquit_signal_handler)
9992 exc = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
9994 mono_arch_handle_exception (ctx, exc, FALSE);
9998 SIG_HANDLER_SIGNATURE (sigint_signal_handler)
10000 MonoException *exc;
10003 exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
10005 mono_arch_handle_exception (ctx, exc, FALSE);
10009 SIG_HANDLER_SIGNATURE (sigusr2_signal_handler)
10011 gboolean enabled = mono_trace_is_enabled ();
10013 mono_trace_enable (!enabled);
10016 #ifndef PLATFORM_WIN32
10018 add_signal_handler (int signo, gpointer handler)
10020 struct sigaction sa;
10022 #ifdef MONO_ARCH_USE_SIGACTION
10023 sa.sa_sigaction = handler;
10024 sigemptyset (&sa.sa_mask);
10025 sa.sa_flags = SA_SIGINFO;
10027 sa.sa_handler = handler;
10028 sigemptyset (&sa.sa_mask);
10031 g_assert (sigaction (signo, &sa, NULL) != -1);
10036 mono_runtime_install_handlers (void)
10038 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
10039 struct sigaction sa;
10042 #ifdef PLATFORM_WIN32
10044 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
10045 win32_seh_set_handler(SIGILL, sigill_signal_handler);
10046 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
10047 if (debug_options.handle_sigint)
10048 win32_seh_set_handler(SIGINT, sigint_signal_handler);
10050 #else /* !PLATFORM_WIN32 */
10052 /* libpthreads has its own implementation of sigaction(),
10053 * but it seems to work well with our current exception
10054 * handlers. If not we must call syscall directly instead
10057 if (debug_options.handle_sigint)
10058 add_signal_handler (SIGINT, sigint_signal_handler);
10060 add_signal_handler (SIGFPE, sigfpe_signal_handler);
10061 add_signal_handler (SIGQUIT, sigquit_signal_handler);
10062 add_signal_handler (SIGILL, sigill_signal_handler);
10063 add_signal_handler (SIGBUS, sigsegv_signal_handler);
10064 if (mono_jit_trace_calls != NULL)
10065 add_signal_handler (SIGUSR2, sigusr2_signal_handler);
10067 add_signal_handler (mono_thread_get_abort_signal (), sigusr1_signal_handler);
10068 signal (SIGPIPE, SIG_IGN);
10070 /* catch SIGSEGV */
10071 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
10072 sa.sa_sigaction = sigsegv_signal_handler;
10073 sigemptyset (&sa.sa_mask);
10074 sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
10075 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
10077 add_signal_handler (SIGSEGV, sigsegv_signal_handler);
10080 #endif /* PLATFORM_WIN32 */
10084 #ifdef HAVE_LINUX_RTC_H
10085 #include <linux/rtc.h>
10086 #include <sys/ioctl.h>
10088 static int rtc_fd = -1;
10091 enable_rtc_timer (gboolean enable)
10094 flags = fcntl (rtc_fd, F_GETFL);
10096 perror ("getflags");
10103 if (fcntl (rtc_fd, F_SETFL, flags) == -1) {
10104 perror ("setflags");
10112 setup_stat_profiler (void)
10115 struct itimerval itval;
10116 static int inited = 0;
10117 #ifdef HAVE_LINUX_RTC_H
10118 const char *rtc_freq;
10119 if (!inited && (rtc_freq = g_getenv ("MONO_RTC"))) {
10123 freq = atoi (rtc_freq);
10126 rtc_fd = open ("/dev/rtc", O_RDONLY);
10127 if (rtc_fd == -1) {
10128 perror ("open /dev/rtc");
10131 add_signal_handler (SIGPROF, sigprof_signal_handler);
10132 if (ioctl (rtc_fd, RTC_IRQP_SET, freq) == -1) {
10133 perror ("set rtc freq");
10136 if (ioctl (rtc_fd, RTC_PIE_ON, 0) == -1) {
10137 perror ("start rtc");
10140 if (fcntl (rtc_fd, F_SETSIG, SIGPROF) == -1) {
10144 if (fcntl (rtc_fd, F_SETOWN, getpid ()) == -1) {
10148 enable_rtc_timer (TRUE);
10155 itval.it_interval.tv_usec = 999;
10156 itval.it_interval.tv_sec = 0;
10157 itval.it_value = itval.it_interval;
10158 setitimer (ITIMER_PROF, &itval, NULL);
10162 add_signal_handler (SIGPROF, sigprof_signal_handler);
10166 /* mono_jit_create_remoting_trampoline:
10167 * @method: pointer to the method info
10169 * Creates a trampoline which calls the remoting functions. This
10170 * is used in the vtable of transparent proxies.
10172 * Returns: a pointer to the newly created code
10175 mono_jit_create_remoting_trampoline (MonoMethod *method, MonoRemotingTarget target)
10178 guint8 *addr = NULL;
10180 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
10181 (mono_method_signature (method)->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
10182 nm = mono_marshal_get_remoting_invoke_for_target (method, target);
10183 addr = mono_compile_method (nm);
10185 addr = mono_compile_method (method);
10187 return mono_get_addr_from_ftnptr (addr);
10191 mini_parse_debug_options (void)
10193 char *options = getenv ("MONO_DEBUG");
10194 gchar **args, **ptr;
10199 args = g_strsplit (options, ",", -1);
10201 for (ptr = args; ptr && *ptr; ptr++) {
10202 const char *arg = *ptr;
10204 if (!strcmp (arg, "handle-sigint"))
10205 debug_options.handle_sigint = TRUE;
10206 else if (!strcmp (arg, "keep-delegates"))
10207 debug_options.keep_delegates = TRUE;
10208 else if (!strcmp (arg, "collect-pagefault-stats"))
10209 debug_options.collect_pagefault_stats = TRUE;
10211 fprintf (stderr, "Invalid option for the MONO_DEBUG env variable: %s\n", arg);
10212 fprintf (stderr, "Available options: 'handle-sigint', 'keep-delegates', 'collect-pagefault-stats'\n");
10219 mini_init (const char *filename)
10221 MonoDomain *domain;
10223 /* Happens when using the embedding interface */
10224 if (default_opt == 0)
10225 default_opt = mono_parse_default_optimizations (NULL);
10227 InitializeCriticalSection (&jit_mutex);
10229 global_codeman = mono_code_manager_new ();
10230 jit_icall_name_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
10232 mono_arch_cpu_init ();
10234 mono_init_trampolines ();
10236 mono_init_exceptions ();
10238 if (!g_thread_supported ())
10239 g_thread_init (NULL);
10241 if (getenv ("MONO_DEBUG") != NULL)
10242 mini_parse_debug_options ();
10244 if (mono_running_on_valgrind ()) {
10245 gsize stack_bottom = (gsize)&domain;
10246 stack_bottom += 4095;
10247 stack_bottom &= ~4095;
10248 GC_stackbottom = (char*)stack_bottom;
10250 MONO_GC_PRE_INIT ();
10252 mono_jit_tls_id = TlsAlloc ();
10253 setup_jit_tls_data ((gpointer)-1, mono_thread_abort);
10257 if (default_opt & MONO_OPT_AOT)
10260 mono_runtime_install_handlers ();
10261 mono_threads_install_cleanup (mini_thread_cleanup);
10263 #define JIT_TRAMPOLINES_WORK
10264 #ifdef JIT_TRAMPOLINES_WORK
10265 mono_install_compile_method (mono_jit_compile_method);
10266 mono_install_free_method (mono_jit_free_method);
10267 mono_install_trampoline (mono_create_jit_trampoline);
10268 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
10269 mono_install_delegate_trampoline (mono_create_delegate_trampoline);
10271 #define JIT_INVOKE_WORKS
10272 #ifdef JIT_INVOKE_WORKS
10273 mono_install_runtime_invoke (mono_jit_runtime_invoke);
10274 mono_install_handler (mono_arch_get_throw_exception ());
10276 mono_install_stack_walk (mono_jit_walk_stack);
10277 mono_install_init_vtable (mono_aot_init_vtable);
10278 mono_install_get_cached_class_info (mono_aot_get_cached_class_info);
10279 mono_install_jit_info_find_in_aot (mono_aot_find_jit_info);
10281 if (debug_options.collect_pagefault_stats) {
10282 mono_raw_buffer_set_make_unreadable (TRUE);
10283 mono_aot_set_make_unreadable (TRUE);
10286 domain = mono_init_from_assembly (filename, filename);
10287 mono_icall_init ();
10289 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
10290 ves_icall_get_frame_info);
10291 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
10292 ves_icall_get_trace);
10293 mono_add_internal_call ("System.Exception::get_trace",
10294 ves_icall_System_Exception_get_trace);
10295 mono_add_internal_call ("System.Security.SecurityFrame::_GetSecurityFrame",
10296 ves_icall_System_Security_SecurityFrame_GetSecurityFrame);
10297 mono_add_internal_call ("System.Security.SecurityFrame::_GetSecurityStack",
10298 ves_icall_System_Security_SecurityFrame_GetSecurityStack);
10299 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
10300 mono_runtime_install_handlers);
10303 create_helper_signature ();
10305 #define JIT_CALLS_WORK
10306 #ifdef JIT_CALLS_WORK
10307 /* Needs to be called here since register_jit_icall depends on it */
10308 mono_marshal_init ();
10310 mono_arch_register_lowlevel_calls ();
10311 register_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
10312 register_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
10313 register_icall (mono_trace_enter_method, "mono_trace_enter_method", NULL, TRUE);
10314 register_icall (mono_trace_leave_method, "mono_trace_leave_method", NULL, TRUE);
10315 register_icall (mono_get_lmf_addr, "mono_get_lmf_addr", "ptr", TRUE);
10316 register_icall (mono_jit_thread_attach, "mono_jit_thread_attach", "void", TRUE);
10317 register_icall (mono_domain_get, "mono_domain_get", "ptr", TRUE);
10319 register_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", "void object", TRUE);
10320 register_icall (mono_arch_get_rethrow_exception (), "mono_arch_rethrow_exception", "void object", TRUE);
10321 register_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name", "void ptr", TRUE);
10322 #if MONO_ARCH_HAVE_THROW_CORLIB_EXCEPTION
10323 register_icall (mono_arch_get_throw_corlib_exception (), "mono_arch_throw_corlib_exception",
10326 register_icall (mono_thread_get_pending_exception, "mono_thread_get_pending_exception", "object", FALSE);
10327 register_icall (mono_thread_interruption_checkpoint, "mono_thread_interruption_checkpoint", "void", FALSE);
10328 register_icall (mono_thread_force_interruption_checkpoint, "mono_thread_force_interruption_checkpoint", "void", FALSE);
10329 register_icall (mono_load_remote_field_new, "mono_load_remote_field_new", "object object ptr ptr", FALSE);
10330 register_icall (mono_store_remote_field_new, "mono_store_remote_field_new", "void object ptr ptr object", FALSE);
10333 * NOTE, NOTE, NOTE, NOTE:
10334 * when adding emulation for some opcodes, remember to also add a dummy
10335 * rule to the burg files, because we need the arity information to be correct.
10337 #ifndef MONO_ARCH_NO_EMULATE_LONG_MUL_OPTS
10338 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", "long long long", mono_llmult, TRUE);
10339 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", "long long long", mono_lldiv, FALSE);
10340 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", "long long long", mono_lldiv_un, FALSE);
10341 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", "long long long", mono_llrem, FALSE);
10342 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", "long long long", mono_llrem_un, FALSE);
10343 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", "long long long", mono_llmult_ovf_un, FALSE);
10344 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", "long long long", mono_llmult_ovf, FALSE);
10347 #ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
10348 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", "long long int32", mono_lshl, TRUE);
10349 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", "long long int32", mono_lshr, TRUE);
10350 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", "long long int32", mono_lshr_un, TRUE);
10353 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
10354 mono_register_opcode_emulation (CEE_DIV, "__emul_idiv", "int32 int32 int32", mono_idiv, TRUE);
10355 mono_register_opcode_emulation (CEE_DIV_UN, "__emul_idiv_un", "int32 int32 int32", mono_idiv_un, TRUE);
10356 mono_register_opcode_emulation (CEE_REM, "__emul_irem", "int32 int32 int32", mono_irem, TRUE);
10357 mono_register_opcode_emulation (CEE_REM_UN, "__emul_irem_un", "int32 int32 int32", mono_irem_un, TRUE);
10360 #ifdef MONO_ARCH_EMULATE_MUL_DIV
10361 mono_register_opcode_emulation (CEE_MUL_OVF, "__emul_imul_ovf", "int32 int32 int32", mono_imul_ovf, TRUE);
10362 mono_register_opcode_emulation (CEE_MUL_OVF_UN, "__emul_imul_ovf_un", "int32 int32 int32", mono_imul_ovf_un, TRUE);
10363 mono_register_opcode_emulation (CEE_MUL, "__emul_imul", "int32 int32 int32", mono_imul, TRUE);
10364 mono_register_opcode_emulation (OP_FDIV, "__emul_fdiv", "double double double", mono_fdiv, TRUE);
10367 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", "ulong double", mono_fconv_u8, FALSE);
10368 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", "uint32 double", mono_fconv_u4, FALSE);
10369 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", "long double", mono_fconv_ovf_i8, FALSE);
10370 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", "ulong double", mono_fconv_ovf_u8, FALSE);
10372 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
10373 mono_register_opcode_emulation (OP_FCONV_TO_I8, "__emul_fconv_to_i8", "long double", mono_fconv_i8, FALSE);
10375 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
10376 mono_register_opcode_emulation (CEE_CONV_R_UN, "__emul_conv_r_un", "double int32", mono_conv_to_r8_un, FALSE);
10378 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
10379 mono_register_opcode_emulation (OP_LCONV_TO_R8, "__emul_lconv_to_r8", "double long", mono_lconv_to_r8, FALSE);
10381 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
10382 mono_register_opcode_emulation (OP_LCONV_TO_R4, "__emul_lconv_to_r4", "float long", mono_lconv_to_r4, FALSE);
10384 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
10385 mono_register_opcode_emulation (OP_LCONV_TO_R_UN, "__emul_lconv_to_r8_un", "double long", mono_lconv_to_r8_un, FALSE);
10387 #ifdef MONO_ARCH_EMULATE_FREM
10388 mono_register_opcode_emulation (OP_FREM, "__emul_frem", "double double double", fmod, FALSE);
10391 #if SIZEOF_VOID_P == 4
10392 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", "uint32 double", mono_fconv_u4, TRUE);
10395 /* other jit icalls */
10396 register_icall (mono_delegate_ctor, "mono_delegate_ctor", "void object object ptr", FALSE);
10397 register_icall (mono_class_static_field_address , "mono_class_static_field_address",
10398 "ptr ptr ptr", FALSE);
10399 register_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", "ptr ptr ptr ptr", FALSE);
10400 register_icall (mono_get_special_static_data, "mono_get_special_static_data", "ptr int", FALSE);
10401 register_icall (mono_ldstr, "mono_ldstr", "object ptr ptr int32", FALSE);
10402 register_icall (helper_stelem_ref, "helper_stelem_ref", "void ptr int32 object", FALSE);
10403 register_icall (helper_stelem_ref_check, "helper_stelem_ref_check", "void object object", FALSE);
10404 register_icall (mono_object_new, "mono_object_new", "object ptr ptr", FALSE);
10405 register_icall (mono_object_new_specific, "mono_object_new_specific", "object ptr", FALSE);
10406 register_icall (mono_array_new, "mono_array_new", "object ptr ptr int32", FALSE);
10407 register_icall (mono_array_new_specific, "mono_array_new_specific", "object ptr int32", FALSE);
10408 register_icall (mono_runtime_class_init, "mono_runtime_class_init", "void ptr", FALSE);
10409 register_icall (mono_ldftn, "mono_ldftn", "ptr ptr", FALSE);
10410 register_icall (mono_ldftn_nosync, "mono_ldftn_nosync", "ptr ptr", FALSE);
10411 register_icall (mono_ldvirtfn, "mono_ldvirtfn", "ptr object ptr", FALSE);
10412 register_icall (helper_compile_generic_method, "compile_generic_method", "ptr object ptr ptr", FALSE);
10413 register_icall (helper_ldstr, "helper_ldstr", "object ptr int", FALSE);
10416 #define JIT_RUNTIME_WORKS
10417 #ifdef JIT_RUNTIME_WORKS
10418 mono_install_runtime_cleanup ((MonoDomainFunc)mini_cleanup);
10419 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
10422 mono_thread_attach (domain);
10426 MonoJitStats mono_jit_stats = {0};
10429 print_jit_stats (void)
10431 if (mono_jit_stats.enabled) {
10432 g_print ("Mono Jit statistics\n");
10433 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
10434 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
10435 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
10436 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
10437 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
10438 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
10439 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
10440 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
10441 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
10442 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
10443 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
10444 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
10445 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
10446 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
10447 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
10448 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
10449 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
10450 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
10452 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
10453 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
10454 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
10455 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
10456 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
10458 g_print ("\nGeneric instances: %ld\n", mono_stats.generic_instance_count);
10459 g_print ("Initialized classes: %ld\n", mono_stats.generic_class_count);
10460 g_print ("Inflated methods: %ld / %ld\n", mono_stats.inflated_method_count_2,
10461 mono_stats.inflated_method_count);
10462 g_print ("Inflated types: %ld\n", mono_stats.inflated_type_count);
10463 g_print ("Generics metadata size: %ld\n", mono_stats.generics_metadata_size);
10465 if (mono_use_security_manager) {
10466 g_print ("\nDecl security check : %ld\n", mono_jit_stats.cas_declsec_check);
10467 g_print ("LinkDemand (user) : %ld\n", mono_jit_stats.cas_linkdemand);
10468 g_print ("LinkDemand (icall) : %ld\n", mono_jit_stats.cas_linkdemand_icall);
10469 g_print ("LinkDemand (pinvoke) : %ld\n", mono_jit_stats.cas_linkdemand_pinvoke);
10470 g_print ("LinkDemand (aptc) : %ld\n", mono_jit_stats.cas_linkdemand_aptc);
10471 g_print ("Demand (code gen) : %ld\n", mono_jit_stats.cas_demand_generation);
10473 if (debug_options.collect_pagefault_stats) {
10474 g_print ("Metadata pagefaults : %d\n", mono_raw_buffer_get_n_pagefaults ());
10475 g_print ("AOT pagefaults : %d\n", mono_aot_get_n_pagefaults ());
10481 mini_cleanup (MonoDomain *domain)
10483 #ifdef HAVE_LINUX_RTC_H
10485 enable_rtc_timer (FALSE);
10489 * mono_runtime_cleanup() and mono_domain_finalize () need to
10490 * be called early since they need the execution engine still
10491 * fully working (mono_domain_finalize may invoke managed finalizers
10492 * and mono_runtime_cleanup will wait for other threads to finish).
10494 mono_domain_finalize (domain, 2000);
10496 mono_runtime_cleanup (domain);
10498 mono_profiler_shutdown ();
10500 mono_debug_cleanup ();
10502 mono_icall_cleanup ();
10504 #ifdef PLATFORM_WIN32
10505 win32_seh_cleanup();
10508 mono_domain_free (domain, TRUE);
10510 mono_code_manager_destroy (global_codeman);
10511 g_hash_table_destroy (jit_icall_name_hash);
10512 if (class_init_hash_addr)
10513 g_hash_table_destroy (class_init_hash_addr);
10515 print_jit_stats ();
10519 mono_set_defaults (int verbose_level, guint32 opts)
10521 mini_verbose = verbose_level;
10522 default_opt = opts;
10526 mono_precompile_assembly (MonoAssembly *ass, void *user_data)
10528 GHashTable *assemblies = (GHashTable*)user_data;
10529 MonoImage *image = mono_assembly_get_image (ass);
10530 MonoMethod *method, *invoke;
10533 if (g_hash_table_lookup (assemblies, ass))
10536 g_hash_table_insert (assemblies, ass, ass);
10538 if (mini_verbose > 0)
10539 printf ("PRECOMPILE: %s.\n", mono_image_get_filename (image));
10541 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
10542 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
10543 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
10547 if (mini_verbose > 1) {
10548 char * desc = mono_method_full_name (method, TRUE);
10549 g_print ("Compiling %d %s\n", count, desc);
10552 mono_compile_method (method);
10553 if (strcmp (method->name, "Finalize") == 0) {
10554 invoke = mono_marshal_get_runtime_invoke (method);
10555 mono_compile_method (invoke);
10557 if (method->klass->marshalbyref && mono_method_signature (method)->hasthis) {
10558 invoke = mono_marshal_get_remoting_invoke_with_check (method);
10559 mono_compile_method (invoke);
10563 /* Load and precompile referenced assemblies as well */
10564 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_ASSEMBLYREF); ++i) {
10565 mono_assembly_load_reference (image, i);
10566 if (image->references [i])
10567 mono_precompile_assembly (image->references [i], assemblies);
10571 void mono_precompile_assemblies ()
10573 GHashTable *assemblies = g_hash_table_new (NULL, NULL);
10575 mono_assembly_foreach ((GFunc)mono_precompile_assembly, assemblies);
10577 g_hash_table_destroy (assemblies);