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/utils/mono-math.h>
52 #include <mono/utils/mono-compiler.h>
53 #include <mono/os/gc_wrapper.h>
61 #include "jit-icalls.c"
64 * this is used to determine when some branch optimizations are possible: we exclude FP compares
65 * because they have weird semantics with NaNs.
67 #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))
68 #define MONO_IS_COND_BRANCH_NOFP(ins) (MONO_IS_COND_BRANCH_OP(ins) && (ins)->inst_left->inst_left->type != STACK_R8)
70 #define MONO_CHECK_THIS(ins) (mono_method_signature (cfg->method)->hasthis && (ins)->ssa_op == MONO_SSA_LOAD && (ins)->inst_left->inst_c0 == 0)
72 static void setup_stat_profiler (void);
73 gboolean mono_arch_print_tree(MonoInst *tree, int arity);
74 static gpointer mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt);
75 static gpointer mono_jit_compile_method (MonoMethod *method);
76 static gpointer mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method);
78 static void handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src,
79 const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native);
81 static void dec_foreach (MonoInst *tree, MonoCompile *cfg);
83 static int mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
84 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
85 guint inline_offset, gboolean is_virtual_call);
87 extern guint8 mono_burg_arity [];
88 /* helper methods signature */
89 static MonoMethodSignature *helper_sig_int_int_int = NULL;
90 static MonoMethodSignature *helper_sig_long_long_long = NULL;
91 static MonoMethodSignature *helper_sig_long_long_int = NULL;
92 static MonoMethodSignature *helper_sig_newarr = NULL;
93 static MonoMethodSignature *helper_sig_newarr_specific = NULL;
94 static MonoMethodSignature *helper_sig_ldstr = NULL;
95 static MonoMethodSignature *helper_sig_domain_get = NULL;
96 static MonoMethodSignature *helper_sig_object_new = NULL;
97 static MonoMethodSignature *helper_sig_object_new_specific = NULL;
98 static MonoMethodSignature *helper_sig_compile = NULL;
99 static MonoMethodSignature *helper_sig_compile_virt = NULL;
100 static MonoMethodSignature *helper_sig_obj_ptr = NULL;
101 static MonoMethodSignature *helper_sig_obj_ptr_ptr = NULL;
102 static MonoMethodSignature *helper_sig_obj_obj_ptr_ptr = NULL;
103 static MonoMethodSignature *helper_sig_obj_obj_obj_ptr = NULL;
104 static MonoMethodSignature *helper_sig_void_obj_obj_ptr = NULL;
105 static MonoMethodSignature *helper_sig_obj_void = NULL;
106 static MonoMethodSignature *helper_sig_ptr_void = NULL;
107 static MonoMethodSignature *helper_sig_void_void = NULL;
108 static MonoMethodSignature *helper_sig_void_ptr = NULL;
109 static MonoMethodSignature *helper_sig_void_obj = NULL;
110 static MonoMethodSignature *helper_sig_void_obj_ptr_int = NULL;
111 static MonoMethodSignature *helper_sig_void_obj_ptr_ptr_obj = NULL;
112 static MonoMethodSignature *helper_sig_void_ptr_ptr = NULL;
113 static MonoMethodSignature *helper_sig_void_ptr_ptr_ptr = NULL;
114 static MonoMethodSignature *helper_sig_ptr_ptr_ptr = NULL;
115 static MonoMethodSignature *helper_sig_ptr_ptr_ptr_ptr = NULL;
116 static MonoMethodSignature *helper_sig_ptr_obj = NULL;
117 static MonoMethodSignature *helper_sig_ptr_obj_int = NULL;
118 static MonoMethodSignature *helper_sig_ptr_int = NULL;
119 static MonoMethodSignature *helper_sig_ulong_double = NULL;
120 static MonoMethodSignature *helper_sig_long_double = NULL;
121 static MonoMethodSignature *helper_sig_double_long = NULL;
122 static MonoMethodSignature *helper_sig_double_int = NULL;
123 static MonoMethodSignature *helper_sig_float_long = NULL;
124 static MonoMethodSignature *helper_sig_double_double_double = NULL;
125 static MonoMethodSignature *helper_sig_uint_double = NULL;
126 static MonoMethodSignature *helper_sig_int_double = NULL;
127 static MonoMethodSignature *helper_sig_stelem_ref = NULL;
128 static MonoMethodSignature *helper_sig_stelem_ref_check = NULL;
129 static MonoMethodSignature *helper_sig_class_init_trampoline = NULL;
130 static MonoMethodSignature *helper_sig_compile_generic_method = NULL;
132 static guint32 default_opt = 0;
134 guint32 mono_jit_tls_id = -1;
135 MonoTraceSpec *mono_jit_trace_calls = NULL;
136 gboolean mono_break_on_exc = FALSE;
138 gboolean mono_compile_aot = FALSE;
140 gboolean mono_use_security_manager = FALSE;
142 static int mini_verbose = 0;
144 static CRITICAL_SECTION jit_mutex;
146 static GHashTable *class_init_hash_addr = NULL;
148 static MonoCodeManager *global_codeman = NULL;
150 static GHashTable *jit_icall_name_hash = NULL;
152 static MonoDebugOptions debug_options;
155 * Address of the trampoline code. This is used by the debugger to check
156 * whether a method is a trampoline.
158 guint8 *mono_generic_trampoline_code = NULL;
160 static guint8* trampoline_code [MONO_TRAMPOLINE_NUM];
163 mono_running_on_valgrind (void)
165 #ifdef HAVE_VALGRIND_MEMCHECK_H
166 if (RUNNING_ON_VALGRIND)
176 G_GNUC_UNUSED static char*
177 get_method_from_ip (void *ip)
183 MonoDomain *domain = mono_domain_get ();
185 ji = mono_jit_info_table_find (domain, ip);
189 method = mono_method_full_name (ji->method, TRUE);
190 source = mono_debug_source_location_from_address (ji->method, (guint32)((guint8*)ip - (guint8*)ji->code_start), NULL, domain);
192 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);
201 G_GNUC_UNUSED static void
202 print_method_from_ip (void *ip)
207 MonoDomain *domain = mono_domain_get ();
209 ji = mono_jit_info_table_find (domain, ip);
211 g_print ("No method at %p\n", ip);
214 method = mono_method_full_name (ji->method, TRUE);
215 source = mono_debug_source_location_from_address (ji->method, (guint32)((guint8*)ip - (guint8*)ji->code_start), NULL, domain);
217 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);
220 g_print ("%s\n", source);
227 mono_print_method_from_ip (void *ip)
229 print_method_from_ip (ip);
233 * mono_method_same_domain:
235 * Determine whenever two compiled methods are in the same domain, thus
236 * the address of the callee can be embedded in the caller.
238 gboolean mono_method_same_domain (MonoJitInfo *caller, MonoJitInfo *callee)
240 if (!caller || !callee)
244 * If the call was made from domain-neutral to domain-specific
245 * code, we can't patch the call site.
247 if (caller->domain_neutral && !callee->domain_neutral)
250 if ((caller->method->klass == mono_defaults.appdomain_class) &&
251 (strstr (caller->method->name, "InvokeInDomain"))) {
252 /* The InvokeInDomain methods change the current appdomain */
260 * mono_global_codeman_reserve:
262 * Allocate code memory from the global code manager.
264 void *mono_global_codeman_reserve (int size)
268 if (!global_codeman) {
269 /* This can happen during startup */
270 global_codeman = mono_code_manager_new ();
271 return mono_code_manager_reserve (global_codeman, size);
274 EnterCriticalSection (&jit_mutex);
275 ptr = mono_code_manager_reserve (global_codeman, size);
276 LeaveCriticalSection (&jit_mutex);
282 mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token)
284 MonoJumpInfoToken *res = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoToken));
291 #define MONO_INIT_VARINFO(vi,id) do { \
292 (vi)->range.first_use.pos.bid = 0xffff; \
298 * Basic blocks have two numeric identifiers:
299 * dfn: Depth First Number
300 * block_num: unique ID assigned at bblock creation
302 #define NEW_BBLOCK(cfg) (mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)))
303 #define ADD_BBLOCK(cfg,bbhash,b) do { \
304 g_hash_table_insert (bbhash, (b)->cil_code, (b)); \
305 (b)->block_num = cfg->num_bblocks++; \
306 (b)->real_offset = real_offset; \
309 #define GET_BBLOCK(cfg,bbhash,tblock,ip) do { \
310 (tblock) = g_hash_table_lookup (bbhash, (ip)); \
312 if ((ip) >= end || (ip) < header->code) goto unverified; \
313 (tblock) = NEW_BBLOCK (cfg); \
314 (tblock)->cil_code = (ip); \
315 ADD_BBLOCK (cfg, (bbhash), (tblock)); \
319 #define CHECK_BBLOCK(target,ip,tblock) do { \
320 if ((target) < (ip) && !(tblock)->code) { \
321 bb_recheck = g_list_prepend (bb_recheck, (tblock)); \
322 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)); \
326 #define NEW_ICONST(cfg,dest,val) do { \
327 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
328 (dest)->opcode = OP_ICONST; \
329 (dest)->inst_c0 = (val); \
330 (dest)->type = STACK_I4; \
333 #define NEW_PCONST(cfg,dest,val) do { \
334 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
335 (dest)->opcode = OP_PCONST; \
336 (dest)->inst_p0 = (val); \
337 (dest)->type = STACK_PTR; \
341 #ifdef MONO_ARCH_NEED_GOT_VAR
343 #define NEW_PATCH_INFO(cfg,dest,el1,el2) do { \
344 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
345 (dest)->opcode = OP_PATCH_INFO; \
346 (dest)->inst_left = (gpointer)(el1); \
347 (dest)->inst_right = (gpointer)(el2); \
350 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do { \
351 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
352 (dest)->opcode = cfg->compile_aot ? OP_GOT_ENTRY : OP_PCONST; \
353 if (cfg->compile_aot) { \
354 MonoInst *group, *got_var, *got_loc; \
355 got_loc = mono_get_got_var (cfg); \
356 NEW_TEMPLOAD ((cfg), got_var, got_loc->inst_c0); \
357 NEW_PATCH_INFO ((cfg), group, cons, patch_type); \
358 (dest)->inst_p0 = got_var; \
359 (dest)->inst_p1 = group; \
361 (dest)->inst_p0 = (cons); \
362 (dest)->inst_i1 = (gpointer)(patch_type); \
364 (dest)->type = STACK_PTR; \
367 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,stack_type) do { \
368 MonoInst *group, *got_var, *got_loc; \
369 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
370 (dest)->opcode = OP_GOT_ENTRY; \
371 got_loc = mono_get_got_var (cfg); \
372 NEW_TEMPLOAD ((cfg), got_var, got_loc->inst_c0); \
373 NEW_PATCH_INFO ((cfg), group, NULL, patch_type); \
374 group->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
375 (dest)->inst_p0 = got_var; \
376 (dest)->inst_p1 = group; \
377 (dest)->type = (stack_type); \
382 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do { \
383 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
384 (dest)->opcode = cfg->compile_aot ? OP_AOTCONST : OP_PCONST; \
385 (dest)->inst_p0 = (cons); \
386 (dest)->inst_i1 = (gpointer)(patch_type); \
387 (dest)->type = STACK_PTR; \
390 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,stack_type) do { \
391 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
392 (dest)->opcode = OP_AOTCONST; \
393 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
394 (dest)->inst_p1 = (gpointer)(patch_type); \
395 (dest)->type = (stack_type); \
400 #define NEW_CLASSCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_CLASS, (val))
402 #define NEW_IMAGECONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_IMAGE, (val))
404 #define NEW_FIELDCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_FIELD, (val))
406 #define NEW_METHODCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHODCONST, (val))
408 #define NEW_VTABLECONST(cfg,dest,vtable) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_VTABLE, cfg->compile_aot ? (gpointer)((vtable)->klass) : (vtable))
410 #define NEW_SFLDACONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_SFLDA, (val))
412 #define NEW_LDSTRCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDSTR, (image), (token), STACK_OBJ)
414 #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)
416 #define NEW_LDTOKENCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDTOKEN, (image), (token), STACK_PTR)
418 #define NEW_DECLSECCONST(cfg,dest,image,entry) do { \
419 if (cfg->compile_aot) { \
420 NEW_AOTCONST_TOKEN (cfg, dest, MONO_PATCH_INFO_DECLSEC, image, (entry).index, STACK_OBJ); \
422 NEW_PCONST (cfg, args [0], (entry).blob); \
426 #define NEW_DOMAINCONST(cfg,dest) do { \
427 if (cfg->opt & MONO_OPT_SHARED) { \
428 /* avoid depending on undefined C behavior in sequence points */ \
429 MonoInst* __domain_var = mono_get_domainvar (cfg); \
430 NEW_TEMPLOAD (cfg, dest, __domain_var->inst_c0); \
432 NEW_PCONST (cfg, dest, (cfg)->domain); \
436 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
438 #define NEW_ARGLOAD(cfg,dest,num) do { \
439 if (arg_array [(num)]->opcode == OP_ICONST) (dest) = arg_array [(num)]; else { \
440 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
441 (dest)->ssa_op = MONO_SSA_LOAD; \
442 (dest)->inst_i0 = arg_array [(num)]; \
443 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
444 type_to_eval_stack_type (param_types [(num)], (dest)); \
445 (dest)->klass = (dest)->inst_i0->klass; \
448 #define NEW_LOCLOAD(cfg,dest,num) do { \
449 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
450 (dest)->ssa_op = MONO_SSA_LOAD; \
451 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
452 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
453 type_to_eval_stack_type (header->locals [(num)], (dest)); \
454 (dest)->klass = (dest)->inst_i0->klass; \
457 #define NEW_LOCLOADA(cfg,dest,num) do { \
458 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
459 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
460 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
461 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
462 (dest)->opcode = OP_LDADDR; \
463 (dest)->type = STACK_MP; \
464 (dest)->klass = (dest)->inst_i0->klass; \
465 if (!MONO_TYPE_ISSTRUCT (header->locals [(num)])) \
466 (cfg)->disable_ssa = TRUE; \
469 #define NEW_RETLOADA(cfg,dest) do { \
470 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
471 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
472 (dest)->inst_i0 = (cfg)->ret; \
473 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
474 (dest)->opcode = cfg->ret_var_is_local ? OP_LDADDR : CEE_LDIND_I; \
475 (dest)->type = STACK_MP; \
476 (dest)->klass = (dest)->inst_i0->klass; \
477 (cfg)->disable_ssa = TRUE; \
480 #define NEW_ARGLOADA(cfg,dest,num) do { \
481 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
482 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
483 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
484 (dest)->inst_i0 = arg_array [(num)]; \
485 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
486 (dest)->opcode = OP_LDADDR; \
487 (dest)->type = STACK_MP; \
488 (dest)->klass = (dest)->inst_i0->klass; \
489 (cfg)->disable_ssa = TRUE; \
492 #define NEW_TEMPLOAD(cfg,dest,num) do { \
493 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
494 (dest)->ssa_op = MONO_SSA_LOAD; \
495 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
496 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
497 type_to_eval_stack_type ((dest)->inst_i0->inst_vtype, (dest)); \
498 (dest)->klass = (dest)->inst_i0->klass; \
501 #define NEW_TEMPLOADA(cfg,dest,num) do { \
502 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
503 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
504 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
505 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
506 (dest)->opcode = OP_LDADDR; \
507 (dest)->type = STACK_MP; \
508 (dest)->klass = (dest)->inst_i0->klass; \
509 if (!MONO_TYPE_ISSTRUCT (cfg->varinfo [(num)]->inst_vtype)) \
510 (cfg)->disable_ssa = TRUE; \
514 #define NEW_INDLOAD(cfg,dest,addr,vtype) do { \
515 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
516 (dest)->inst_left = addr; \
517 (dest)->opcode = mono_type_to_ldind (vtype); \
518 type_to_eval_stack_type (vtype, (dest)); \
519 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
522 #define NEW_INDSTORE(cfg,dest,addr,value,vtype) do { \
523 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
524 (dest)->inst_i0 = addr; \
525 (dest)->opcode = mono_type_to_stind (vtype); \
526 (dest)->inst_i1 = (value); \
527 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
530 #define NEW_TEMPSTORE(cfg,dest,num,inst) do { \
531 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
532 (dest)->ssa_op = MONO_SSA_STORE; \
533 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
534 (dest)->opcode = mono_type_to_stind ((dest)->inst_i0->inst_vtype); \
535 (dest)->inst_i1 = (inst); \
536 (dest)->klass = (dest)->inst_i0->klass; \
539 #define NEW_LOCSTORE(cfg,dest,num,inst) do { \
540 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
541 (dest)->opcode = mono_type_to_stind (header->locals [(num)]); \
542 (dest)->ssa_op = MONO_SSA_STORE; \
543 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
544 (dest)->inst_i1 = (inst); \
545 (dest)->klass = (dest)->inst_i0->klass; \
548 #define NEW_ARGSTORE(cfg,dest,num,inst) do { \
549 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
550 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
551 (dest)->opcode = mono_type_to_stind (param_types [(num)]); \
552 (dest)->ssa_op = MONO_SSA_STORE; \
553 (dest)->inst_i0 = arg_array [(num)]; \
554 (dest)->inst_i1 = (inst); \
555 (dest)->klass = (dest)->inst_i0->klass; \
558 #define NEW_DUMMY_USE(cfg,dest,load) do { \
559 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
560 (dest)->opcode = OP_DUMMY_USE; \
561 (dest)->inst_left = (load); \
564 #define NEW_DUMMY_STORE(cfg,dest,num) do { \
565 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
566 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
567 (dest)->opcode = OP_DUMMY_STORE; \
568 (dest)->klass = (dest)->inst_i0->klass; \
571 #define ADD_BINOP(op) do { \
572 MONO_INST_NEW (cfg, ins, (op)); \
573 ins->cil_code = ip; \
575 ins->inst_i0 = sp [0]; \
576 ins->inst_i1 = sp [1]; \
578 type_from_op (ins); \
582 #define ADD_UNOP(op) do { \
583 MONO_INST_NEW (cfg, ins, (op)); \
584 ins->cil_code = ip; \
586 ins->inst_i0 = sp [0]; \
588 type_from_op (ins); \
592 #define ADD_BINCOND(next_block) do { \
595 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
596 cmp->inst_i0 = sp [0]; \
597 cmp->inst_i1 = sp [1]; \
598 cmp->cil_code = ins->cil_code; \
599 type_from_op (cmp); \
601 ins->inst_i0 = cmp; \
602 MONO_ADD_INS (bblock, ins); \
603 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
604 GET_BBLOCK (cfg, bbhash, tblock, target); \
605 link_bblock (cfg, bblock, tblock); \
606 ins->inst_true_bb = tblock; \
607 CHECK_BBLOCK (target, ip, tblock); \
608 if ((next_block)) { \
609 link_bblock (cfg, bblock, (next_block)); \
610 ins->inst_false_bb = (next_block); \
611 start_new_bblock = 1; \
613 GET_BBLOCK (cfg, bbhash, tblock, ip); \
614 link_bblock (cfg, bblock, tblock); \
615 ins->inst_false_bb = tblock; \
616 start_new_bblock = 2; \
620 /* FIXME: handle float, long ... */
621 #define ADD_UNCOND(istrue) do { \
624 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
625 cmp->inst_i0 = sp [0]; \
626 switch (cmp->inst_i0->type) { \
628 cmp->inst_i1 = zero_int64; break; \
630 cmp->inst_i1 = zero_r8; break; \
633 cmp->inst_i1 = zero_ptr; break; \
635 cmp->inst_i1 = zero_obj; break; \
637 cmp->inst_i1 = zero_int32; \
639 cmp->cil_code = ins->cil_code; \
640 type_from_op (cmp); \
642 ins->inst_i0 = cmp; \
643 ins->opcode = (istrue)? CEE_BNE_UN: CEE_BEQ; \
644 MONO_ADD_INS (bblock, ins); \
645 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
646 GET_BBLOCK (cfg, bbhash, tblock, target); \
647 link_bblock (cfg, bblock, tblock); \
648 ins->inst_true_bb = tblock; \
649 CHECK_BBLOCK (target, ip, tblock); \
650 GET_BBLOCK (cfg, bbhash, tblock, ip); \
651 link_bblock (cfg, bblock, tblock); \
652 ins->inst_false_bb = tblock; \
653 start_new_bblock = 2; \
656 #define NEW_LDELEMA(cfg,dest,sp,k) do { \
657 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
658 (dest)->opcode = CEE_LDELEMA; \
659 (dest)->inst_left = (sp) [0]; \
660 (dest)->inst_right = (sp) [1]; \
661 (dest)->type = STACK_MP; \
662 (dest)->klass = (k); \
663 (cfg)->flags |= MONO_CFG_HAS_LDELEMA; \
666 #define NEW_GROUP(cfg,dest,el1,el2) do { \
667 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
668 (dest)->opcode = OP_GROUP; \
669 (dest)->inst_left = (el1); \
670 (dest)->inst_right = (el2); \
675 compare_bblock (gconstpointer a, gconstpointer b)
677 const MonoBasicBlock *b1 = a;
678 const MonoBasicBlock *b2 = b;
680 return b2->cil_code - b1->cil_code;
685 * link_bblock: Links two basic blocks
687 * links two basic blocks in the control flow graph, the 'from'
688 * argument is the starting block and the 'to' argument is the block
689 * the control flow ends to after 'from'.
692 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
694 MonoBasicBlock **newa;
698 if (from->cil_code) {
700 g_print ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
702 g_print ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
705 g_print ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
707 g_print ("edge from entry to exit\n");
711 for (i = 0; i < from->out_count; ++i) {
712 if (to == from->out_bb [i]) {
718 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
719 for (i = 0; i < from->out_count; ++i) {
720 newa [i] = from->out_bb [i];
728 for (i = 0; i < to->in_count; ++i) {
729 if (from == to->in_bb [i]) {
735 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
736 for (i = 0; i < to->in_count; ++i) {
737 newa [i] = to->in_bb [i];
746 * mono_find_block_region:
748 * We mark each basic block with a region ID. We use that to avoid BB
749 * optimizations when blocks are in different regions.
752 * A region token that encodes where this region is, and information
753 * about the clause owner for this block.
755 * The region encodes the try/catch/filter clause that owns this block
756 * as well as the type. -1 is a special value that represents a block
757 * that is in none of try/catch/filter.
760 mono_find_block_region (MonoCompile *cfg, int offset)
762 MonoMethod *method = cfg->method;
763 MonoMethodHeader *header = mono_method_get_header (method);
764 MonoExceptionClause *clause;
767 /* first search for handlers and filters */
768 for (i = 0; i < header->num_clauses; ++i) {
769 clause = &header->clauses [i];
770 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->data.filter_offset) &&
771 (offset < (clause->handler_offset)))
772 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
774 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
775 if (clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)
776 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
778 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
782 /* search the try blocks */
783 for (i = 0; i < header->num_clauses; ++i) {
784 clause = &header->clauses [i];
785 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
786 return ((i + 1) << 8) | clause->flags;
793 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
795 MonoMethod *method = cfg->method;
796 MonoMethodHeader *header = mono_method_get_header (method);
797 MonoExceptionClause *clause;
798 MonoBasicBlock *handler;
802 for (i = 0; i < header->num_clauses; ++i) {
803 clause = &header->clauses [i];
804 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
805 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
806 if (clause->flags == type) {
807 handler = g_hash_table_lookup (cfg->bb_hash, header->code + clause->handler_offset);
809 res = g_list_append (res, handler);
817 mono_find_spvar_for_region (MonoCompile *cfg, int region)
819 return g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
823 mono_create_spvar_for_region (MonoCompile *cfg, int region)
827 var = g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
831 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
832 /* prevent it from being register allocated */
833 var->flags |= MONO_INST_INDIRECT;
835 g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
839 mono_find_exvar_for_offset (MonoCompile *cfg, int offset)
841 return g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
845 mono_create_exvar_for_offset (MonoCompile *cfg, int offset)
849 var = g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
853 var = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
854 /* prevent it from being register allocated */
855 var->flags |= MONO_INST_INDIRECT;
857 g_hash_table_insert (cfg->exvars, GINT_TO_POINTER (offset), var);
863 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
867 array [*dfn] = start;
868 /*g_print ("visit %d at %p\n", *dfn, start->cil_code);*/
869 for (i = 0; i < start->out_count; ++i) {
870 if (start->out_bb [i]->dfn)
873 start->out_bb [i]->dfn = *dfn;
874 start->out_bb [i]->df_parent = start;
875 array [*dfn] = start->out_bb [i];
876 df_visit (start->out_bb [i], dfn, array);
882 MonoBasicBlock *best;
886 previous_foreach (gconstpointer key, gpointer val, gpointer data)
888 PrevStruct *p = data;
889 MonoBasicBlock *bb = val;
890 //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,
891 //bb->method == p->best->method, bb->cil_code < p->code, bb->cil_code > p->best->cil_code);
893 if (bb->cil_code && bb->cil_code < p->code && bb->cil_code > p->best->cil_code)
897 static MonoBasicBlock*
898 find_previous (GHashTable *bb_hash, MonoBasicBlock *start, const guchar *code) {
904 g_hash_table_foreach (bb_hash, (GHFunc)previous_foreach, &p);
909 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
918 * FIXME: take into account all the details:
919 * second may have been the target of more than one bblock
921 second->out_count = first->out_count;
922 second->out_bb = first->out_bb;
924 for (i = 0; i < first->out_count; ++i) {
925 bb = first->out_bb [i];
926 for (j = 0; j < bb->in_count; ++j) {
927 if (bb->in_bb [j] == first)
928 bb->in_bb [j] = second;
932 first->out_count = 0;
933 first->out_bb = NULL;
934 link_bblock (cfg, first, second);
936 second->last_ins = first->last_ins;
938 /*g_print ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
939 for (inst = first->code; inst && inst->next; inst = inst->next) {
940 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
941 g_print ("found %p: %s", inst->next->cil_code, code);
943 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
944 second->code = inst->next;
946 first->last_ins = inst;
947 second->next_bb = first->next_bb;
948 first->next_bb = second;
953 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
959 reverse_branch_op (guint32 opcode)
961 static const int reverse_map [] = {
962 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
963 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
965 static const int reverse_fmap [] = {
966 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
967 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
969 static const int reverse_lmap [] = {
970 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
971 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
973 static const int reverse_imap [] = {
974 OP_IBNE_UN, OP_IBLT, OP_IBLE, OP_IBGT, OP_IBGE,
975 OP_IBEQ, OP_IBLT_UN, OP_IBLE_UN, OP_IBGT_UN, OP_IBGE_UN
978 if (opcode >= CEE_BEQ && opcode <= CEE_BLT_UN) {
979 opcode = reverse_map [opcode - CEE_BEQ];
980 } else if (opcode >= OP_FBEQ && opcode <= OP_FBLT_UN) {
981 opcode = reverse_fmap [opcode - OP_FBEQ];
982 } else if (opcode >= OP_LBEQ && opcode <= OP_LBLT_UN) {
983 opcode = reverse_lmap [opcode - OP_LBEQ];
984 } else if (opcode >= OP_IBEQ && opcode <= OP_IBLT_UN) {
985 opcode = reverse_imap [opcode - OP_IBEQ];
987 g_assert_not_reached ();
993 mono_type_to_ldind (MonoType *type)
999 switch (type->type) {
1001 return CEE_LDIND_I1;
1003 case MONO_TYPE_BOOLEAN:
1004 return CEE_LDIND_U1;
1006 return CEE_LDIND_I2;
1008 case MONO_TYPE_CHAR:
1009 return CEE_LDIND_U2;
1011 return CEE_LDIND_I4;
1013 return CEE_LDIND_U4;
1017 case MONO_TYPE_FNPTR:
1019 case MONO_TYPE_CLASS:
1020 case MONO_TYPE_STRING:
1021 case MONO_TYPE_OBJECT:
1022 case MONO_TYPE_SZARRAY:
1023 case MONO_TYPE_ARRAY:
1024 return CEE_LDIND_REF;
1027 return CEE_LDIND_I8;
1029 return CEE_LDIND_R4;
1031 return CEE_LDIND_R8;
1032 case MONO_TYPE_VALUETYPE:
1033 if (type->data.klass->enumtype) {
1034 type = type->data.klass->enum_basetype;
1038 case MONO_TYPE_TYPEDBYREF:
1040 case MONO_TYPE_GENERICINST:
1041 type = &type->data.generic_class->container_class->byval_arg;
1044 g_error ("unknown type 0x%02x in type_to_ldind", type->type);
1050 mono_type_to_stind (MonoType *type)
1056 switch (type->type) {
1059 case MONO_TYPE_BOOLEAN:
1060 return CEE_STIND_I1;
1063 case MONO_TYPE_CHAR:
1064 return CEE_STIND_I2;
1067 return CEE_STIND_I4;
1071 case MONO_TYPE_FNPTR:
1073 case MONO_TYPE_CLASS:
1074 case MONO_TYPE_STRING:
1075 case MONO_TYPE_OBJECT:
1076 case MONO_TYPE_SZARRAY:
1077 case MONO_TYPE_ARRAY:
1078 return CEE_STIND_REF;
1081 return CEE_STIND_I8;
1083 return CEE_STIND_R4;
1085 return CEE_STIND_R8;
1086 case MONO_TYPE_VALUETYPE:
1087 if (type->data.klass->enumtype) {
1088 type = type->data.klass->enum_basetype;
1092 case MONO_TYPE_TYPEDBYREF:
1094 case MONO_TYPE_GENERICINST:
1095 type = &type->data.generic_class->container_class->byval_arg;
1098 g_error ("unknown type 0x%02x in type_to_stind", type->type);
1104 * Returns the type used in the eval stack when @type is loaded.
1105 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
1108 type_to_eval_stack_type (MonoType *type, MonoInst *inst)
1113 inst->type = STACK_MP;
1117 klass = mono_class_from_mono_type (type);
1120 switch (type->type) {
1121 case MONO_TYPE_VOID:
1122 inst->type = STACK_INV;
1126 case MONO_TYPE_BOOLEAN:
1129 case MONO_TYPE_CHAR:
1132 inst->type = STACK_I4;
1137 case MONO_TYPE_FNPTR:
1138 inst->type = STACK_PTR;
1140 case MONO_TYPE_CLASS:
1141 case MONO_TYPE_STRING:
1142 case MONO_TYPE_OBJECT:
1143 case MONO_TYPE_SZARRAY:
1144 case MONO_TYPE_ARRAY:
1145 inst->type = STACK_OBJ;
1149 inst->type = STACK_I8;
1153 inst->type = STACK_R8;
1155 case MONO_TYPE_VALUETYPE:
1156 if (type->data.klass->enumtype) {
1157 type = type->data.klass->enum_basetype;
1160 inst->klass = klass;
1161 inst->type = STACK_VTYPE;
1164 case MONO_TYPE_TYPEDBYREF:
1165 inst->klass = mono_defaults.typed_reference_class;
1166 inst->type = STACK_VTYPE;
1168 case MONO_TYPE_GENERICINST:
1169 type = &type->data.generic_class->container_class->byval_arg;
1172 g_error ("unknown type 0x%02x in eval stack type", type->type);
1177 * The following tables are used to quickly validate the IL code in type_from_op ().
1180 bin_num_table [STACK_MAX] [STACK_MAX] = {
1181 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1182 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1183 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1184 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1185 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
1186 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
1187 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1188 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1193 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
1196 /* reduce the size of this table */
1198 bin_int_table [STACK_MAX] [STACK_MAX] = {
1199 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1200 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1201 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1202 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1203 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1204 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1205 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1206 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1210 bin_comp_table [STACK_MAX] [STACK_MAX] = {
1212 {0, 1, 0, 1, 0, 0, 4, 0},
1213 {0, 0, 1, 0, 0, 0, 0, 0},
1214 {0, 1, 0, 1, 0, 2, 4, 0},
1215 {0, 0, 0, 0, 1, 0, 0, 0},
1216 {0, 0, 0, 2, 0, 1, 0, 0},
1217 {0, 4, 0, 4, 0, 0, 3, 0},
1218 {0, 0, 0, 0, 0, 0, 0, 0},
1221 /* reduce the size of this table */
1223 shift_table [STACK_MAX] [STACK_MAX] = {
1224 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1225 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1226 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1227 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1228 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1229 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1230 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1231 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1235 * Tables to map from the non-specific opcode to the matching
1236 * type-specific opcode.
1238 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
1239 static const guint16
1240 binops_op_map [STACK_MAX] = {
1241 0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, OP_PADD-CEE_ADD
1244 /* handles from CEE_NEG to CEE_CONV_U8 */
1245 static const guint16
1246 unops_op_map [STACK_MAX] = {
1247 0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, OP_PNEG-CEE_NEG
1250 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
1251 static const guint16
1252 ovfops_op_map [STACK_MAX] = {
1253 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
1256 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
1257 static const guint16
1258 ovf2ops_op_map [STACK_MAX] = {
1259 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
1262 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
1263 static const guint16
1264 ovf3ops_op_map [STACK_MAX] = {
1265 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
1268 /* handles from CEE_CEQ to CEE_CLT_UN */
1269 static const guint16
1270 ceqops_op_map [STACK_MAX] = {
1271 0, 0, OP_LCEQ-CEE_CEQ, OP_PCEQ-CEE_CEQ, OP_FCEQ-CEE_CEQ, OP_LCEQ-CEE_CEQ
1275 * Sets ins->type (the type on the eval stack) according to the
1276 * type of the opcode and the arguments to it.
1277 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1279 * FIXME: this function sets ins->type unconditionally in some cases, but
1280 * it should set it to invalid for some types (a conv.x on an object)
1283 type_from_op (MonoInst *ins) {
1284 switch (ins->opcode) {
1291 /* FIXME: check unverifiable args for STACK_MP */
1292 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1293 ins->opcode += binops_op_map [ins->type];
1300 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1301 ins->opcode += binops_op_map [ins->type];
1306 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1307 ins->opcode += binops_op_map [ins->type];
1311 /* FIXME: handle some specifics with ins->next->type */
1312 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1313 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))))
1314 ins->opcode = OP_LCOMPARE;
1321 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1322 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1326 ins->type = neg_table [ins->inst_i0->type];
1327 ins->opcode += unops_op_map [ins->type];
1330 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1331 ins->type = ins->inst_i0->type;
1333 ins->type = STACK_INV;
1334 ins->opcode += unops_op_map [ins->type];
1340 ins->type = STACK_I4;
1341 ins->opcode += unops_op_map [ins->inst_i0->type];
1344 ins->type = STACK_R8;
1345 switch (ins->inst_i0->type) {
1350 ins->opcode = OP_LCONV_TO_R_UN;
1354 case CEE_CONV_OVF_I1:
1355 case CEE_CONV_OVF_U1:
1356 case CEE_CONV_OVF_I2:
1357 case CEE_CONV_OVF_U2:
1358 case CEE_CONV_OVF_I4:
1359 case CEE_CONV_OVF_U4:
1360 ins->type = STACK_I4;
1361 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1363 case CEE_CONV_OVF_I_UN:
1364 case CEE_CONV_OVF_U_UN:
1365 ins->type = STACK_PTR;
1366 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1368 case CEE_CONV_OVF_I1_UN:
1369 case CEE_CONV_OVF_I2_UN:
1370 case CEE_CONV_OVF_I4_UN:
1371 case CEE_CONV_OVF_U1_UN:
1372 case CEE_CONV_OVF_U2_UN:
1373 case CEE_CONV_OVF_U4_UN:
1374 ins->type = STACK_I4;
1375 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1378 ins->type = STACK_PTR;
1379 switch (ins->inst_i0->type) {
1385 ins->opcode = OP_LCONV_TO_U;
1388 ins->opcode = OP_FCONV_TO_U;
1394 ins->type = STACK_I8;
1395 ins->opcode += unops_op_map [ins->inst_i0->type];
1397 case CEE_CONV_OVF_I8:
1398 case CEE_CONV_OVF_U8:
1399 ins->type = STACK_I8;
1400 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1402 case CEE_CONV_OVF_U8_UN:
1403 case CEE_CONV_OVF_I8_UN:
1404 ins->type = STACK_I8;
1405 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1409 ins->type = STACK_R8;
1410 ins->opcode += unops_op_map [ins->inst_i0->type];
1413 ins->type = STACK_R8;
1417 ins->type = STACK_I4;
1418 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1421 case CEE_CONV_OVF_I:
1422 case CEE_CONV_OVF_U:
1423 ins->type = STACK_PTR;
1424 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1427 case CEE_ADD_OVF_UN:
1429 case CEE_MUL_OVF_UN:
1431 case CEE_SUB_OVF_UN:
1432 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1433 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1436 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1443 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_MP, STACK_R8, STACK_R8, STACK_OBJ
1446 /* map ldelem.x to the matching ldind.x opcode */
1448 ldelem_to_ldind [] = {
1462 /* map stelem.x to the matching stind.x opcode */
1464 stelem_to_stind [] = {
1478 param_table [STACK_MAX] [STACK_MAX] = {
1483 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1487 switch (args->type) {
1497 for (i = 0; i < sig->param_count; ++i) {
1498 switch (args [i].type) {
1502 if (!sig->params [i]->byref)
1506 if (sig->params [i]->byref)
1508 switch (sig->params [i]->type) {
1509 case MONO_TYPE_CLASS:
1510 case MONO_TYPE_STRING:
1511 case MONO_TYPE_OBJECT:
1512 case MONO_TYPE_SZARRAY:
1513 case MONO_TYPE_ARRAY:
1520 if (sig->params [i]->byref)
1522 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1531 /*if (!param_table [args [i].type] [sig->params [i]->type])
1539 * When we need a pointer to the current domain many times in a method, we
1540 * call mono_domain_get() once and we store the result in a local variable.
1541 * This function returns the variable that represents the MonoDomain*.
1543 inline static MonoInst *
1544 mono_get_domainvar (MonoCompile *cfg)
1546 if (!cfg->domainvar)
1547 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1548 return cfg->domainvar;
1552 * The got_var contains the address of the Global Offset Table when AOT
1555 inline static MonoInst *
1556 mono_get_got_var (MonoCompile *cfg)
1558 #ifdef MONO_ARCH_NEED_GOT_VAR
1559 if (!cfg->compile_aot)
1561 if (!cfg->got_var) {
1562 cfg->got_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1564 return cfg->got_var;
1571 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
1574 int num = cfg->num_varinfo;
1576 if ((num + 1) >= cfg->varinfo_count) {
1577 cfg->varinfo_count = (cfg->varinfo_count + 2) * 2;
1578 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
1579 cfg->vars = (MonoMethodVar **)g_realloc (cfg->vars, sizeof (MonoMethodVar*) * cfg->varinfo_count);
1582 /*g_print ("created temp %d of type 0x%x\n", num, type->type);*/
1583 mono_jit_stats.allocate_var++;
1585 MONO_INST_NEW (cfg, inst, opcode);
1586 inst->inst_c0 = num;
1587 inst->inst_vtype = type;
1588 inst->klass = mono_class_from_mono_type (type);
1589 /* if set to 1 the variable is native */
1592 cfg->varinfo [num] = inst;
1594 cfg->vars [num] = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMethodVar));
1595 MONO_INIT_VARINFO (cfg->vars [num], num);
1598 //g_print ("created temp %d of type %s\n", num, mono_type_get_name (type));
1603 * Transform a MonoInst into a load from the variable of index var_index.
1606 mono_compile_make_var_load (MonoCompile *cfg, MonoInst *dest, gssize var_index) {
1607 memset (dest, 0, sizeof (MonoInst));
1608 dest->ssa_op = MONO_SSA_LOAD;
1609 dest->inst_i0 = cfg->varinfo [var_index];
1610 dest->opcode = mono_type_to_ldind (dest->inst_i0->inst_vtype);
1611 type_to_eval_stack_type (dest->inst_i0->inst_vtype, dest);
1612 dest->klass = dest->inst_i0->klass;
1616 * Create a MonoInst that is a load from the variable of index var_index.
1619 mono_compile_create_var_load (MonoCompile *cfg, gssize var_index) {
1621 NEW_TEMPLOAD (cfg,dest,var_index);
1626 * Create a MonoInst that is a store of the given value into the variable of index var_index.
1629 mono_compile_create_var_store (MonoCompile *cfg, gssize var_index, MonoInst *value) {
1631 NEW_TEMPSTORE (cfg, dest, var_index, value);
1636 type_from_stack_type (MonoInst *ins) {
1637 switch (ins->type) {
1638 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1639 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1640 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1641 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1642 case STACK_MP: return &mono_defaults.int_class->byval_arg;
1643 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1644 case STACK_VTYPE: return &ins->klass->byval_arg;
1646 g_error ("stack type %d to montype not handled\n", ins->type);
1652 mono_type_from_stack_type (MonoInst *ins) {
1653 return type_from_stack_type (ins);
1657 array_access_to_klass (int opcode)
1661 return mono_defaults.byte_class;
1663 return mono_defaults.uint16_class;
1666 return mono_defaults.int_class;
1669 return mono_defaults.sbyte_class;
1672 return mono_defaults.int16_class;
1675 return mono_defaults.int32_class;
1677 return mono_defaults.uint32_class;
1680 return mono_defaults.int64_class;
1683 return mono_defaults.single_class;
1686 return mono_defaults.double_class;
1687 case CEE_LDELEM_REF:
1688 case CEE_STELEM_REF:
1689 return mono_defaults.object_class;
1691 g_assert_not_reached ();
1697 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
1701 MONO_ADD_INS (bb, inst);
1704 switch (bb->last_ins->opcode) {
1718 while (prev->next && prev->next != bb->last_ins)
1720 if (prev == bb->code) {
1721 if (bb->last_ins == bb->code) {
1722 inst->next = bb->code;
1725 inst->next = prev->next;
1729 inst->next = bb->last_ins;
1733 // g_warning ("handle conditional jump in add_ins_to_end ()\n");
1735 MONO_ADD_INS (bb, inst);
1741 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
1743 MonoInst *inst, *load;
1745 NEW_TEMPLOAD (cfg, load, src);
1747 NEW_TEMPSTORE (cfg, inst, dest, load);
1748 if (inst->opcode == CEE_STOBJ) {
1749 NEW_TEMPLOADA (cfg, inst, dest);
1750 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE);
1752 inst->cil_code = NULL;
1753 mono_add_ins_to_end (bb, inst);
1758 * We try to share variables when possible
1761 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1766 /* inlining can result in deeper stacks */
1767 if (slot >= mono_method_get_header (cfg->method)->max_stack)
1768 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1770 pos = ins->type - 1 + slot * STACK_MAX;
1772 switch (ins->type) {
1779 if ((vnum = cfg->intvars [pos]))
1780 return cfg->varinfo [vnum];
1781 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1782 cfg->intvars [pos] = res->inst_c0;
1785 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1791 * This function is called to handle items that are left on the evaluation stack
1792 * at basic block boundaries. What happens is that we save the values to local variables
1793 * and we reload them later when first entering the target basic block (with the
1794 * handle_loaded_temps () function).
1795 * It is also used to handle items on the stack in store opcodes, since it is
1796 * possible that the variable to be stored into is already on the stack, in
1797 * which case its old value should be used.
1798 * A single joint point will use the same variables (stored in the array bb->out_stack or
1799 * bb->in_stack, if the basic block is before or after the joint point).
1802 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count) {
1804 MonoBasicBlock *outb;
1805 MonoInst *inst, **locals;
1810 if (cfg->verbose_level > 3)
1811 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
1812 if (!bb->out_scount) {
1813 bb->out_scount = count;
1814 //g_print ("bblock %d has out:", bb->block_num);
1816 for (i = 0; i < bb->out_count; ++i) {
1817 outb = bb->out_bb [i];
1818 //g_print (" %d", outb->block_num);
1819 if (outb->in_stack) {
1821 bb->out_stack = outb->in_stack;
1827 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1828 for (i = 0; i < count; ++i) {
1830 * try to reuse temps already allocated for this purpouse, if they occupy the same
1831 * stack slot and if they are of the same type.
1832 * This won't cause conflicts since if 'local' is used to
1833 * store one of the values in the in_stack of a bblock, then
1834 * the same variable will be used for the same outgoing stack
1836 * This doesn't work when inlining methods, since the bblocks
1837 * in the inlined methods do not inherit their in_stack from
1838 * the bblock they are inlined to. See bug #58863 for an
1841 if (cfg->inlined_method)
1842 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1844 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1849 for (i = 0; i < bb->out_count; ++i) {
1850 outb = bb->out_bb [i];
1851 if (outb->in_scount)
1852 continue; /* check they are the same locals */
1853 outb->in_scount = count;
1854 outb->in_stack = bb->out_stack;
1857 locals = bb->out_stack;
1858 for (i = 0; i < count; ++i) {
1859 /* add store ops at the end of the bb, before the branch */
1860 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1861 if (inst->opcode == CEE_STOBJ) {
1862 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
1863 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE);
1865 inst->cil_code = sp [i]->cil_code;
1866 mono_add_ins_to_end (bb, inst);
1868 if (cfg->verbose_level > 3)
1869 g_print ("storing %d to temp %d\n", i, (int)locals [i]->inst_c0);
1873 * It is possible that the out bblocks already have in_stack assigned, and
1874 * the in_stacks differ. In this case, we will store to all the different
1881 /* Find a bblock which has a different in_stack */
1883 while (bindex < bb->out_count) {
1884 outb = bb->out_bb [bindex];
1885 if (outb->in_stack != locals) {
1887 * Instead of storing sp [i] to locals [i], we need to store
1888 * locals [i] to <new locals>[i], since the sp [i] tree can't
1889 * be shared between trees.
1891 for (i = 0; i < count; ++i)
1892 mono_add_varcopy_to_end (cfg, bb, locals [i]->inst_c0, outb->in_stack [i]->inst_c0);
1893 locals = outb->in_stack;
1905 ret_type_to_call_opcode (MonoType *type, int calli, int virt)
1908 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1911 switch (type->type) {
1912 case MONO_TYPE_VOID:
1913 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
1916 case MONO_TYPE_BOOLEAN:
1919 case MONO_TYPE_CHAR:
1922 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1926 case MONO_TYPE_FNPTR:
1927 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1928 case MONO_TYPE_CLASS:
1929 case MONO_TYPE_STRING:
1930 case MONO_TYPE_OBJECT:
1931 case MONO_TYPE_SZARRAY:
1932 case MONO_TYPE_ARRAY:
1933 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1936 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
1939 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
1940 case MONO_TYPE_VALUETYPE:
1941 if (type->data.klass->enumtype) {
1942 type = type->data.klass->enum_basetype;
1945 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1946 case MONO_TYPE_TYPEDBYREF:
1947 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1948 case MONO_TYPE_GENERICINST:
1949 type = &type->data.generic_class->container_class->byval_arg;
1952 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
1958 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
1960 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
1961 MonoJumpInfoBBTable *table;
1963 table = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfoBBTable));
1965 table->table_size = num_blocks;
1967 ji->ip.label = label;
1968 ji->type = MONO_PATCH_INFO_SWITCH;
1969 ji->data.table = table;
1970 ji->next = cfg->patch_info;
1971 cfg->patch_info = ji;
1975 * When we add a tree of instructions, we need to ensure the instructions currently
1976 * on the stack are executed before (like, if we load a value from a local).
1977 * We ensure this by saving the currently loaded values to temps and rewriting the
1978 * instructions to load the values.
1979 * This is not done for opcodes that terminate a basic block (because it's handled already
1980 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
1983 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
1985 MonoInst *load, *store, *temp, *ins;
1987 while (stack < sp) {
1989 /* handle also other constants */
1990 if ((ins->opcode != OP_ICONST) &&
1991 /* temps never get written to again, so we can safely avoid duplicating them */
1992 !(ins->ssa_op == MONO_SSA_LOAD && ins->inst_i0->opcode == OP_LOCAL && ins->inst_i0->flags & MONO_INST_IS_TEMP)) {
1993 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1994 temp->flags |= MONO_INST_IS_TEMP;
1995 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1996 store->cil_code = ins->cil_code;
1997 if (store->opcode == CEE_STOBJ) {
1998 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
1999 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE);
2001 MONO_ADD_INS (bblock, store);
2002 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2003 load->cil_code = ins->cil_code;
2011 * Prepare arguments for passing to a function call.
2012 * Return a non-zero value if the arguments can't be passed to the given
2014 * The type checks are not yet complete and some conversions may need
2015 * casts on 32 or 64 bit architectures.
2018 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
2020 MonoType *simple_type;
2024 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
2028 for (i = 0; i < sig->param_count; ++i) {
2029 if (sig->params [i]->byref) {
2030 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
2034 simple_type = sig->params [i];
2036 switch (simple_type->type) {
2037 case MONO_TYPE_VOID:
2042 case MONO_TYPE_BOOLEAN:
2045 case MONO_TYPE_CHAR:
2048 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
2054 case MONO_TYPE_FNPTR:
2055 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
2058 case MONO_TYPE_CLASS:
2059 case MONO_TYPE_STRING:
2060 case MONO_TYPE_OBJECT:
2061 case MONO_TYPE_SZARRAY:
2062 case MONO_TYPE_ARRAY:
2063 if (args [i]->type != STACK_OBJ)
2068 if (args [i]->type != STACK_I8)
2073 if (args [i]->type != STACK_R8)
2076 case MONO_TYPE_VALUETYPE:
2077 if (simple_type->data.klass->enumtype) {
2078 simple_type = simple_type->data.klass->enum_basetype;
2081 if (args [i]->type != STACK_VTYPE)
2084 case MONO_TYPE_TYPEDBYREF:
2085 if (args [i]->type != STACK_VTYPE)
2088 case MONO_TYPE_GENERICINST:
2089 simple_type = &simple_type->data.generic_class->container_class->byval_arg;
2093 g_error ("unknown type 0x%02x in check_call_signature",
2101 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
2102 const guint8 *ip, gboolean to_end)
2104 MonoInst *temp, *store, *ins = (MonoInst*)call;
2105 MonoType *ret = sig->ret;
2107 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
2109 call->inst.type = STACK_OBJ;
2110 call->inst.opcode = CEE_CALL;
2111 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
2113 type_to_eval_stack_type (ret, ins);
2114 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
2117 temp->flags |= MONO_INST_IS_TEMP;
2119 if (MONO_TYPE_ISSTRUCT (ret)) {
2120 MonoInst *loada, *dummy_store;
2123 * Emit a dummy store to the local holding the result so the
2124 * liveness info remains correct.
2126 NEW_DUMMY_STORE (cfg, dummy_store, temp->inst_c0);
2128 mono_add_ins_to_end (bblock, dummy_store);
2130 MONO_ADD_INS (bblock, dummy_store);
2132 /* we use this to allocate native sized structs */
2133 temp->unused = sig->pinvoke;
2135 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
2136 if (call->inst.opcode == OP_VCALL)
2137 ins->inst_left = loada;
2139 ins->inst_right = loada; /* a virtual or indirect call */
2142 mono_add_ins_to_end (bblock, ins);
2144 MONO_ADD_INS (bblock, ins);
2146 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2147 store->cil_code = ip;
2149 mono_add_ins_to_end (bblock, store);
2151 MONO_ADD_INS (bblock, store);
2153 return temp->inst_c0;
2156 mono_add_ins_to_end (bblock, ins);
2158 MONO_ADD_INS (bblock, ins);
2163 inline static MonoCallInst *
2164 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
2165 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
2170 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual));
2172 call->inst.cil_code = ip;
2174 call->signature = sig;
2175 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
2176 type_to_eval_stack_type (sig->ret, &call->inst);
2178 for (arg = call->out_args; arg;) {
2179 MonoInst *narg = arg->next;
2184 mono_add_ins_to_end (bblock, arg);
2186 MONO_ADD_INS (bblock, arg);
2193 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
2194 MonoInst **args, MonoInst *addr, const guint8 *ip)
2196 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
2198 call->inst.inst_i0 = addr;
2200 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
2203 static MonoCallInst*
2204 mono_emit_method_call_full (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
2205 MonoInst **args, const guint8 *ip, MonoInst *this, gboolean to_end)
2207 gboolean virtual = this != NULL;
2210 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, to_end);
2212 if (this && sig->hasthis &&
2213 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
2214 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
2215 call->method = mono_marshal_get_remoting_invoke_with_check (method);
2217 call->method = method;
2219 call->inst.flags |= MONO_INST_HAS_METHOD;
2220 call->inst.inst_left = this;
2223 mono_get_got_var (cfg);
2224 else if (call->method->klass->flags & TYPE_ATTRIBUTE_INTERFACE)
2225 /* Needed by the code generated in inssel.brg */
2226 mono_get_got_var (cfg);
2231 static MonoCallInst*
2232 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
2233 MonoInst **args, const guint8 *ip, MonoInst *this)
2235 return mono_emit_method_call_full (cfg, bblock, method, sig, args, ip, this, FALSE);
2239 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
2240 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
2242 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, signature, args, ip, this);
2244 return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
2248 mono_emit_method_call_spilled_full (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
2249 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this,
2250 gboolean ret_object, gboolean to_end)
2252 MonoCallInst *call = mono_emit_method_call_full (cfg, bblock, method, signature, args, ip, this, to_end);
2254 return mono_spill_call (cfg, bblock, call, signature, ret_object, ip, to_end);
2258 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
2259 MonoInst **args, const guint8 *ip, gboolean ret_object, gboolean to_end)
2265 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
2268 mono_get_got_var (cfg);
2270 return mono_spill_call (cfg, bblock, call, sig, ret_object, ip, to_end);
2274 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
2276 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
2279 g_warning ("unregistered JIT ICall");
2280 g_assert_not_reached ();
2283 mono_get_got_var (cfg);
2284 return mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, args, ip, FALSE, FALSE);
2288 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
2290 MonoInst *ins, *temp = NULL, *store, *load, *begin;
2291 MonoInst *last_arg = NULL;
2295 //g_print ("emulating: ");
2296 //mono_print_tree_nl (tree);
2297 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE));
2298 ins = (MonoInst*)call;
2300 call->inst.cil_code = tree->cil_code;
2302 call->signature = info->sig;
2304 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
2306 mono_get_got_var (cfg);
2308 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2309 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
2310 temp->flags |= MONO_INST_IS_TEMP;
2311 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2312 store->cil_code = tree->cil_code;
2317 nargs = info->sig->param_count + info->sig->hasthis;
2319 for (last_arg = call->out_args; last_arg && last_arg->next; last_arg = last_arg->next) ;
2322 last_arg->next = store;
2325 begin = call->out_args;
2329 if (cfg->prev_ins) {
2331 * This assumes that that in a tree, emulate_opcode is called for a
2332 * node before it is called for its children. dec_foreach needs to
2333 * take this into account.
2335 store->next = cfg->prev_ins->next;
2336 cfg->prev_ins->next = begin;
2338 store->next = cfg->cbb->code;
2339 cfg->cbb->code = begin;
2342 call->fptr = mono_icall_get_wrapper (info);
2344 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2345 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2350 static MonoMethodSignature *
2351 mono_get_element_address_signature (int arity)
2353 static GHashTable *sighash = NULL;
2354 MonoMethodSignature *res;
2357 EnterCriticalSection (&jit_mutex);
2359 sighash = g_hash_table_new (NULL, NULL);
2361 else if ((res = g_hash_table_lookup (sighash, GINT_TO_POINTER (arity)))) {
2362 LeaveCriticalSection (&jit_mutex);
2366 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
2369 #ifdef MONO_ARCH_VARARG_ICALLS
2370 /* Only set this only some archs since not all backends can handle varargs+pinvoke */
2371 res->call_convention = MONO_CALL_VARARG;
2373 res->params [0] = &mono_defaults.array_class->byval_arg;
2375 for (i = 1; i <= arity; i++)
2376 res->params [i] = &mono_defaults.int_class->byval_arg;
2378 res->ret = &mono_defaults.int_class->byval_arg;
2380 g_hash_table_insert (sighash, GINT_TO_POINTER (arity), res);
2381 LeaveCriticalSection (&jit_mutex);
2386 static MonoMethodSignature *
2387 mono_get_array_new_va_signature (int arity)
2389 static GHashTable *sighash = NULL;
2390 MonoMethodSignature *res;
2393 EnterCriticalSection (&jit_mutex);
2395 sighash = g_hash_table_new (NULL, NULL);
2397 else if ((res = g_hash_table_lookup (sighash, GINT_TO_POINTER (arity)))) {
2398 LeaveCriticalSection (&jit_mutex);
2402 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
2405 #ifdef MONO_ARCH_VARARG_ICALLS
2406 /* Only set this only some archs since not all backends can handle varargs+pinvoke */
2407 res->call_convention = MONO_CALL_VARARG;
2410 res->params [0] = &mono_defaults.int_class->byval_arg;
2411 for (i = 0; i < arity; i++)
2412 res->params [i + 1] = &mono_defaults.int_class->byval_arg;
2414 res->ret = &mono_defaults.int_class->byval_arg;
2416 g_hash_table_insert (sighash, GINT_TO_POINTER (arity), res);
2417 LeaveCriticalSection (&jit_mutex);
2423 get_memcpy_method (void)
2425 static MonoMethod *memcpy_method = NULL;
2426 if (!memcpy_method) {
2427 memcpy_method = mono_class_get_method_from_name (mono_defaults.string_class, "memcpy", 3);
2429 g_error ("Old corlib found. Install a new one");
2431 return memcpy_method;
2435 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native) {
2436 MonoInst *iargs [3];
2439 MonoMethod *memcpy_method;
2443 * This check breaks with spilled vars... need to handle it during verification anyway.
2444 * g_assert (klass && klass == src->klass && klass == dest->klass);
2448 n = mono_class_native_size (klass, &align);
2450 n = mono_class_value_size (klass, &align);
2452 if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
2454 if (dest->opcode == OP_LDADDR) {
2455 /* Keep liveness info correct */
2456 NEW_DUMMY_STORE (cfg, inst, dest->inst_i0->inst_c0);
2457 MONO_ADD_INS (bblock, inst);
2459 MONO_INST_NEW (cfg, inst, OP_MEMCPY);
2460 inst->inst_left = dest;
2461 inst->inst_right = src;
2462 inst->cil_code = ip;
2464 MONO_ADD_INS (bblock, inst);
2469 NEW_ICONST (cfg, iargs [2], n);
2471 memcpy_method = get_memcpy_method ();
2472 mono_emit_method_call_spilled_full (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL, FALSE, to_end);
2476 get_memset_method (void)
2478 static MonoMethod *memset_method = NULL;
2479 if (!memset_method) {
2480 memset_method = mono_class_get_method_from_name (mono_defaults.string_class, "memset", 3);
2482 g_error ("Old corlib found. Install a new one");
2484 return memset_method;
2488 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
2490 MonoInst *iargs [3];
2491 MonoInst *ins, *zero_int32;
2493 MonoMethod *memset_method;
2495 NEW_ICONST (cfg, zero_int32, 0);
2497 mono_class_init (klass);
2498 n = mono_class_value_size (klass, NULL);
2499 MONO_INST_NEW (cfg, ins, 0);
2501 ins->inst_left = dest;
2502 ins->inst_right = zero_int32;
2505 ins->opcode = CEE_STIND_I1;
2506 MONO_ADD_INS (bblock, ins);
2509 ins->opcode = CEE_STIND_I2;
2510 MONO_ADD_INS (bblock, ins);
2513 ins->opcode = CEE_STIND_I4;
2514 MONO_ADD_INS (bblock, ins);
2517 if (n <= sizeof (gpointer) * 5) {
2518 ins->opcode = OP_MEMSET;
2521 MONO_ADD_INS (bblock, ins);
2524 memset_method = get_memset_method ();
2525 handle_loaded_temps (cfg, bblock, stack_start, sp);
2527 NEW_ICONST (cfg, iargs [1], 0);
2528 NEW_ICONST (cfg, iargs [2], n);
2529 mono_emit_method_call_spilled (cfg, bblock, memset_method, memset_method->signature, iargs, ip, NULL);
2535 handle_alloc (MonoCompile *cfg, MonoBasicBlock *bblock, MonoClass *klass, gboolean for_box, const guchar *ip)
2537 MonoInst *iargs [2];
2540 if (cfg->opt & MONO_OPT_SHARED) {
2541 NEW_DOMAINCONST (cfg, iargs [0]);
2542 NEW_CLASSCONST (cfg, iargs [1], klass);
2544 alloc_ftn = mono_object_new;
2546 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
2549 alloc_ftn = mono_class_get_allocation_ftn (vtable, for_box, &pass_lw);
2551 guint32 lw = vtable->klass->instance_size;
2552 lw = ((lw + (sizeof (gpointer) - 1)) & ~(sizeof (gpointer) - 1)) / sizeof (gpointer);
2553 NEW_ICONST (cfg, iargs [0], lw);
2554 NEW_VTABLECONST (cfg, iargs [1], vtable);
2557 NEW_VTABLECONST (cfg, iargs [0], vtable);
2560 return mono_emit_jit_icall (cfg, bblock, alloc_ftn, iargs, ip);
2564 handle_box (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *val, const guchar *ip, MonoClass *klass)
2566 MonoInst *dest, *vtoffset, *add, *vstore;
2569 temp = handle_alloc (cfg, bblock, klass, TRUE, ip);
2570 NEW_TEMPLOAD (cfg, dest, temp);
2571 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
2572 MONO_INST_NEW (cfg, add, OP_PADD);
2573 add->inst_left = dest;
2574 add->inst_right = vtoffset;
2577 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
2578 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
2579 vstore->cil_code = ip;
2580 vstore->inst_left = add;
2581 vstore->inst_right = val;
2583 if (vstore->opcode == CEE_STOBJ) {
2584 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
2586 MONO_ADD_INS (bblock, vstore);
2588 NEW_TEMPLOAD (cfg, dest, temp);
2593 handle_array_new (MonoCompile *cfg, MonoBasicBlock *bblock, int rank, MonoInst **sp, unsigned char *ip)
2595 MonoMethodSignature *esig;
2596 char icall_name [256];
2598 MonoJitICallInfo *info;
2600 /* Need to register the icall so it gets an icall wrapper */
2601 sprintf (icall_name, "ves_array_new_va_%d", rank);
2603 info = mono_find_jit_icall_by_name (icall_name);
2605 esig = mono_get_array_new_va_signature (rank);
2606 name = g_strdup (icall_name);
2607 info = mono_register_jit_icall (mono_array_new_va, name, esig, FALSE);
2609 EnterCriticalSection (&jit_mutex);
2610 g_hash_table_insert (jit_icall_name_hash, name, name);
2611 LeaveCriticalSection (&jit_mutex);
2614 cfg->flags |= MONO_CFG_HAS_VARARGS;
2616 return mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, sp, ip, TRUE, FALSE);
2620 mono_emit_load_got_addr (MonoCompile *cfg)
2622 MonoInst *load, *store, *dummy_use;
2625 if (!cfg->got_var || cfg->got_var_allocated)
2628 MONO_INST_NEW (cfg, get_got, OP_LOAD_GOTADDR);
2629 NEW_TEMPSTORE (cfg, store, cfg->got_var->inst_c0, get_got);
2631 /* Add it to the start of the first bblock */
2632 if (cfg->bb_entry->code) {
2633 store->next = cfg->bb_entry->code;
2634 cfg->bb_entry->code = store;
2637 MONO_ADD_INS (cfg->bb_entry, store);
2639 cfg->got_var_allocated = TRUE;
2642 * Add a dummy use to keep the got_var alive, since real uses might
2643 * only be generated in the decompose or instruction selection phases.
2644 * Add it to end_bblock, so the variable's lifetime covers the whole
2647 NEW_TEMPLOAD (cfg, load, cfg->got_var->inst_c0);
2648 NEW_DUMMY_USE (cfg, dummy_use, load);
2649 MONO_ADD_INS (cfg->bb_exit, dummy_use);
2652 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
2655 mini_class_is_system_array (MonoClass *klass)
2657 if (klass->parent == mono_defaults.array_class)
2659 else if (mono_defaults.generic_array_class && klass->parent && klass->parent->generic_class)
2660 return klass->parent->generic_class->container_class == mono_defaults.generic_array_class;
2666 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
2668 MonoMethodHeader *header = mono_method_get_header (method);
2669 MonoMethodSignature *signature = mono_method_signature (method);
2673 #ifdef MONO_ARCH_HAVE_LMF_OPS
2674 if (((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2675 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) &&
2676 !MONO_TYPE_ISSTRUCT (signature->ret) && !mini_class_is_system_array (method->klass))
2680 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2681 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2682 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
2683 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
2684 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2685 (method->klass->marshalbyref) ||
2686 !header || header->num_clauses ||
2687 /* fixme: why cant we inline valuetype returns? */
2688 MONO_TYPE_ISSTRUCT (signature->ret))
2691 /* its not worth to inline methods with valuetype arguments?? */
2692 for (i = 0; i < signature->param_count; i++) {
2693 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
2699 * if we can initialize the class of the method right away, we do,
2700 * otherwise we don't allow inlining if the class needs initialization,
2701 * since it would mean inserting a call to mono_runtime_class_init()
2702 * inside the inlined code
2704 if (!(cfg->opt & MONO_OPT_SHARED)) {
2705 vtable = mono_class_vtable (cfg->domain, method->klass);
2706 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
2707 if (cfg->run_cctors)
2708 mono_runtime_class_init (vtable);
2710 else if (!vtable->initialized && mono_class_needs_cctor_run (method->klass, NULL))
2714 * If we're compiling for shared code
2715 * the cctor will need to be run at aot method load time, for example,
2716 * or at the end of the compilation of the inlining method.
2718 if (mono_class_needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
2721 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
2724 * CAS - do not inline methods with declarative security
2725 * Note: this has to be before any possible return TRUE;
2727 if (mono_method_has_declsec (method))
2730 /* also consider num_locals? */
2731 if (getenv ("MONO_INLINELIMIT")) {
2732 if (header->code_size < atoi (getenv ("MONO_INLINELIMIT"))) {
2735 } else if (header->code_size < 20)
2742 mini_field_access_needs_cctor_run (MonoCompile *cfg, MonoMethod *method, MonoVTable *vtable)
2744 if (vtable->initialized && !cfg->compile_aot)
2747 if (vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)
2750 if (!mono_class_needs_cctor_run (vtable->klass, method))
2753 if (! (method->flags & METHOD_ATTRIBUTE_STATIC) && (vtable->klass == method->klass))
2754 /* The initialization is already done before the method is called */
2761 mini_get_ldelema_ins (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
2765 MonoMethodSignature *esig;
2766 char icall_name [256];
2768 MonoJitICallInfo *info;
2770 rank = mono_method_signature (cmethod)->param_count - (is_set? 1: 0);
2772 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
2774 NEW_GROUP (cfg, indexes, sp [1], sp [2]);
2775 MONO_INST_NEW (cfg, addr, OP_LDELEMA2D);
2776 addr->inst_left = sp [0];
2777 addr->inst_right = indexes;
2778 addr->cil_code = ip;
2779 addr->type = STACK_MP;
2780 addr->klass = cmethod->klass;
2784 /* Need to register the icall so it gets an icall wrapper */
2785 sprintf (icall_name, "ves_array_element_address_%d", rank);
2787 info = mono_find_jit_icall_by_name (icall_name);
2789 esig = mono_get_element_address_signature (rank);
2790 name = g_strdup (icall_name);
2791 info = mono_register_jit_icall (ves_array_element_address, name, esig, FALSE);
2793 EnterCriticalSection (&jit_mutex);
2794 g_hash_table_insert (jit_icall_name_hash, name, name);
2795 LeaveCriticalSection (&jit_mutex);
2798 temp = mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, sp, ip, FALSE, FALSE);
2799 cfg->flags |= MONO_CFG_HAS_VARARGS;
2801 NEW_TEMPLOAD (cfg, addr, temp);
2805 static MonoJitICallInfo **emul_opcode_map = NULL;
2807 static inline MonoJitICallInfo *
2808 mono_find_jit_opcode_emulation (int opcode)
2810 if (emul_opcode_map)
2811 return emul_opcode_map [opcode];
2817 mini_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
2819 MonoInst *ins = NULL;
2821 static MonoClass *runtime_helpers_class = NULL;
2822 if (! runtime_helpers_class)
2823 runtime_helpers_class = mono_class_from_name (mono_defaults.corlib,
2824 "System.Runtime.CompilerServices", "RuntimeHelpers");
2826 if (cmethod->klass == mono_defaults.string_class) {
2827 if (cmethod->name [0] != 'g')
2830 if (strcmp (cmethod->name, "get_Chars") == 0) {
2831 MONO_INST_NEW (cfg, ins, OP_GETCHR);
2832 ins->inst_i0 = args [0];
2833 ins->inst_i1 = args [1];
2835 } else if (strcmp (cmethod->name, "get_Length") == 0) {
2836 MONO_INST_NEW (cfg, ins, OP_STRLEN);
2837 ins->inst_i0 = args [0];
2841 } else if (cmethod->klass == mono_defaults.object_class) {
2842 if (strcmp (cmethod->name, "GetType") == 0) {
2843 MONO_INST_NEW (cfg, ins, OP_GETTYPE);
2844 ins->inst_i0 = args [0];
2846 } else if (strcmp (cmethod->name, "InternalGetHashCode") == 0) {
2847 MONO_INST_NEW (cfg, ins, OP_GETHASHCODE);
2848 ins->inst_i0 = args [0];
2852 } else if (mini_class_is_system_array (cmethod->klass)) {
2853 if (cmethod->name [0] != 'g')
2856 if (strcmp (cmethod->name, "get_Rank") == 0) {
2857 MONO_INST_NEW (cfg, ins, OP_ARRAY_RANK);
2858 ins->inst_i0 = args [0];
2860 } else if (strcmp (cmethod->name, "get_Length") == 0) {
2861 MONO_INST_NEW (cfg, ins, CEE_LDLEN);
2862 ins->inst_i0 = args [0];
2866 } else if (cmethod->klass == runtime_helpers_class) {
2867 if (strcmp (cmethod->name, "get_OffsetToStringData") == 0) {
2868 NEW_ICONST (cfg, ins, G_STRUCT_OFFSET (MonoString, chars));
2872 } else if (cmethod->klass == mono_defaults.thread_class) {
2873 if (strcmp (cmethod->name, "get_CurrentThread") == 0 && (ins = mono_arch_get_thread_intrinsic (cfg)))
2878 return mono_arch_get_inst_for_method (cfg, cmethod, fsig, args);
2882 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
2884 MonoInst *store, *temp;
2887 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
2889 if (!sig->hasthis && sig->param_count == 0)
2893 if (sp [0]->opcode == OP_ICONST) {
2896 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
2898 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2899 store->cil_code = sp [0]->cil_code;
2900 MONO_ADD_INS (bblock, store);
2905 for (i = 0; i < sig->param_count; ++i) {
2906 if (sp [0]->opcode == OP_ICONST) {
2909 temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
2911 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2912 store->cil_code = sp [0]->cil_code;
2913 if (store->opcode == CEE_STOBJ) {
2914 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2915 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE);
2917 MONO_ADD_INS (bblock, store);
2925 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
2926 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b, gboolean inline_allways)
2928 MonoInst *ins, *rvar = NULL;
2929 MonoMethodHeader *cheader;
2930 MonoBasicBlock *ebblock, *sbblock;
2931 int i, costs, new_locals_offset;
2932 MonoMethod *prev_inlined_method;
2934 if (cfg->verbose_level > 2)
2935 g_print ("INLINE START %p %s -> %s\n", cmethod, mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2937 if (!cmethod->inline_info) {
2938 mono_jit_stats.inlineable_methods++;
2939 cmethod->inline_info = 1;
2941 /* allocate space to store the return value */
2942 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2943 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2946 /* allocate local variables */
2947 cheader = mono_method_get_header (cmethod);
2948 new_locals_offset = cfg->num_varinfo;
2949 for (i = 0; i < cheader->num_locals; ++i)
2950 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
2952 /* allocate starte and end blocks */
2953 sbblock = NEW_BBLOCK (cfg);
2954 sbblock->block_num = cfg->num_bblocks++;
2955 sbblock->real_offset = real_offset;
2957 ebblock = NEW_BBLOCK (cfg);
2958 ebblock->block_num = cfg->num_bblocks++;
2959 ebblock->real_offset = real_offset;
2961 prev_inlined_method = cfg->inlined_method;
2962 cfg->inlined_method = cmethod;
2964 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
2966 cfg->inlined_method = prev_inlined_method;
2968 if ((costs >= 0 && costs < 60) || inline_allways) {
2969 if (cfg->verbose_level > 2)
2970 g_print ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2972 mono_jit_stats.inlined_methods++;
2974 /* always add some code to avoid block split failures */
2975 MONO_INST_NEW (cfg, ins, CEE_NOP);
2976 MONO_ADD_INS (bblock, ins);
2979 bblock->next_bb = sbblock;
2980 link_bblock (cfg, bblock, sbblock);
2983 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
2989 if (cfg->verbose_level > 2)
2990 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
2996 * Some of these comments may well be out-of-date.
2997 * Design decisions: we do a single pass over the IL code (and we do bblock
2998 * splitting/merging in the few cases when it's required: a back jump to an IL
2999 * address that was not already seen as bblock starting point).
3000 * Code is validated as we go (full verification is still better left to metadata/verify.c).
3001 * Complex operations are decomposed in simpler ones right away. We need to let the
3002 * arch-specific code peek and poke inside this process somehow (except when the
3003 * optimizations can take advantage of the full semantic info of coarse opcodes).
3004 * All the opcodes of the form opcode.s are 'normalized' to opcode.
3005 * MonoInst->opcode initially is the IL opcode or some simplification of that
3006 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
3007 * opcode with value bigger than OP_LAST.
3008 * At this point the IR can be handed over to an interpreter, a dumb code generator
3009 * or to the optimizing code generator that will translate it to SSA form.
3011 * Profiling directed optimizations.
3012 * We may compile by default with few or no optimizations and instrument the code
3013 * or the user may indicate what methods to optimize the most either in a config file
3014 * or through repeated runs where the compiler applies offline the optimizations to
3015 * each method and then decides if it was worth it.
3018 * * consider using an array instead of an hash table (bb_hash)
3021 #define CHECK_TYPE(ins) if (!(ins)->type) goto unverified
3022 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) goto unverified
3023 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) goto unverified
3024 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) goto unverified
3025 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) goto unverified
3026 #define CHECK_OPSIZE(size) if (ip + size > end) goto unverified
3029 /* offset from br.s -> br like opcodes */
3030 #define BIG_BRANCH_OFFSET 13
3033 ip_in_bb (MonoCompile *cfg, MonoBasicBlock *bb, const guint8* ip)
3035 MonoBasicBlock *b = g_hash_table_lookup (cfg->bb_hash, ip);
3037 return b == NULL || b == bb;
3041 get_basic_blocks (MonoCompile *cfg, GHashTable *bbhash, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
3043 unsigned char *ip = start;
3044 unsigned char *target;
3047 MonoBasicBlock *bblock;
3048 const MonoOpcode *opcode;
3051 cli_addr = ip - start;
3052 i = mono_opcode_value ((const guint8 **)&ip, end);
3055 opcode = &mono_opcodes [i];
3056 switch (opcode->argument) {
3057 case MonoInlineNone:
3060 case MonoInlineString:
3061 case MonoInlineType:
3062 case MonoInlineField:
3063 case MonoInlineMethod:
3066 case MonoShortInlineR:
3073 case MonoShortInlineVar:
3074 case MonoShortInlineI:
3077 case MonoShortInlineBrTarget:
3078 target = start + cli_addr + 2 + (signed char)ip [1];
3079 GET_BBLOCK (cfg, bbhash, bblock, target);
3082 case MonoInlineBrTarget:
3083 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
3084 GET_BBLOCK (cfg, bbhash, bblock, target);
3087 case MonoInlineSwitch: {
3088 guint32 n = read32 (ip + 1);
3091 cli_addr += 5 + 4 * n;
3092 target = start + cli_addr;
3093 GET_BBLOCK (cfg, bbhash, bblock, target);
3095 for (j = 0; j < n; ++j) {
3096 target = start + cli_addr + (gint32)read32 (ip);
3097 GET_BBLOCK (cfg, bbhash, bblock, target);
3107 g_assert_not_reached ();
3117 emit_tree (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *ins, const guint8* ip_next)
3119 MonoInst *store, *temp, *load;
3121 if (ip_in_bb (cfg, bblock, ip_next) &&
3122 (CODE_IS_STLOC (ip_next) || *ip_next == CEE_BRTRUE || *ip_next == CEE_BRFALSE ||
3123 *ip_next == CEE_BRTRUE_S || *ip_next == CEE_BRFALSE_S || *ip_next == CEE_RET))
3126 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3127 temp->flags |= MONO_INST_IS_TEMP;
3128 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3129 store->cil_code = ins->cil_code;
3130 MONO_ADD_INS (bblock, store);
3131 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
3132 load->cil_code = ins->cil_code;
3136 static inline MonoMethod *
3137 mini_get_method (MonoImage *image, guint32 token, MonoClass *klass, MonoGenericContext *context)
3139 MonoMethod *method = mono_get_method_full (image, token, klass, context);
3141 if (method->is_inflated)
3142 method = mono_get_inflated_method (method);
3148 void check_linkdemand (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee, MonoBasicBlock *bblock, unsigned char *ip)
3150 guint32 result = mono_declsec_linkdemand (cfg->domain, caller, callee);
3151 if (result == MONO_JIT_SECURITY_OK)
3154 if (result == MONO_JIT_LINKDEMAND_ECMA) {
3155 /* Generate code to throw a SecurityException before the actual call/link */
3156 MonoAssembly *assembly = mono_image_get_assembly (caller->klass->image);
3157 MonoReflectionAssembly *refass = (MonoReflectionAssembly*) mono_assembly_get_object (cfg->domain, assembly);
3158 MonoReflectionMethod *refmet = mono_method_get_object (cfg->domain, caller, NULL);
3159 MonoSecurityManager *secman = mono_security_manager_get_methods ();
3162 NEW_ICONST (cfg, args [0], 4);
3163 NEW_PCONST (cfg, args [1], refass);
3164 NEW_PCONST (cfg, args [2], refmet);
3165 mono_emit_method_call_spilled (cfg, bblock, secman->linkdemandsecurityexception, mono_method_signature (secman->linkdemandsecurityexception), args, ip, NULL);
3166 } else if (cfg->exception_type == MONO_EXCEPTION_NONE) {
3167 /* don't hide previous results */
3168 cfg->exception_type = MONO_EXCEPTION_SECURITY_LINKDEMAND;
3169 cfg->exception_data = result;
3175 * mono_method_to_ir: translates IL into basic blocks containing trees
3178 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
3179 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
3180 guint inline_offset, gboolean is_virtual_call)
3182 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
3183 MonoInst *ins, **sp, **stack_start;
3184 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
3186 MonoMethod *cmethod;
3187 MonoInst **arg_array;
3188 MonoMethodHeader *header;
3190 guint32 token, ins_flag;
3192 MonoClass *constrained_call = NULL;
3193 unsigned char *ip, *end, *target, *err_pos;
3194 static double r8_0 = 0.0;
3195 MonoMethodSignature *sig;
3196 MonoGenericContext *generic_context = NULL;
3197 MonoGenericContainer *generic_container = NULL;
3198 MonoType **param_types;
3199 GList *bb_recheck = NULL, *tmp;
3200 int i, n, start_new_bblock, align;
3201 int num_calls = 0, inline_costs = 0;
3202 int breakpoint_id = 0;
3203 guint real_offset, num_args;
3204 MonoBoolean security, pinvoke;
3205 MonoSecurityManager* secman = NULL;
3206 MonoDeclSecurityActions actions;
3207 GSList *class_inits = NULL;
3209 image = method->klass->image;
3210 header = mono_method_get_header (method);
3211 generic_container = ((MonoMethodNormal *)method)->generic_container;
3212 sig = mono_method_signature (method);
3213 num_args = sig->hasthis + sig->param_count;
3214 ip = (unsigned char*)header->code;
3215 end = ip + header->code_size;
3216 mono_jit_stats.cil_code_size += header->code_size;
3218 if (sig->is_inflated)
3219 generic_context = ((MonoMethodInflated *) method)->context;
3220 else if (generic_container)
3221 generic_context = &generic_container->context;
3223 g_assert (!sig->has_type_parameters);
3225 if (cfg->method == method) {
3227 bbhash = cfg->bb_hash;
3229 real_offset = inline_offset;
3230 bbhash = g_hash_table_new (g_direct_hash, NULL);
3233 if (cfg->verbose_level > 2)
3234 g_print ("method to IR %s\n", mono_method_full_name (method, TRUE));
3236 dont_inline = g_list_prepend (dont_inline, method);
3237 if (cfg->method == method) {
3239 if (cfg->method->save_lmf)
3240 /* Needed by the prolog code */
3241 mono_get_got_var (cfg);
3243 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
3244 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
3247 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
3248 start_bblock->cil_code = NULL;
3249 start_bblock->cil_length = 0;
3250 start_bblock->block_num = cfg->num_bblocks++;
3253 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
3254 end_bblock->cil_code = NULL;
3255 end_bblock->cil_length = 0;
3256 end_bblock->block_num = cfg->num_bblocks++;
3257 g_assert (cfg->num_bblocks == 2);
3259 arg_array = alloca (sizeof (MonoInst *) * num_args);
3260 for (i = num_args - 1; i >= 0; i--)
3261 arg_array [i] = cfg->varinfo [i];
3263 if (header->num_clauses) {
3264 cfg->spvars = g_hash_table_new (NULL, NULL);
3265 cfg->exvars = g_hash_table_new (NULL, NULL);
3267 /* handle exception clauses */
3268 for (i = 0; i < header->num_clauses; ++i) {
3269 //unsigned char *p = ip;
3270 MonoExceptionClause *clause = &header->clauses [i];
3271 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->try_offset);
3272 tblock->real_offset = clause->try_offset;
3273 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
3274 tblock->real_offset = clause->handler_offset;
3276 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
3277 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
3278 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
3279 MONO_ADD_INS (tblock, ins);
3282 /*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);
3284 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
3286 /* catch and filter blocks get the exception object on the stack */
3287 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
3288 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
3289 MonoInst *load, *dummy_use;
3291 /* mostly like handle_stack_args (), but just sets the input args */
3292 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
3293 tblock->in_scount = 1;
3294 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
3295 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
3298 * Add a dummy use for the exvar so its liveness info will be
3301 NEW_TEMPLOAD (cfg, load, tblock->in_stack [0]->inst_c0);
3302 NEW_DUMMY_USE (cfg, dummy_use, load);
3303 MONO_ADD_INS (tblock, dummy_use);
3305 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
3306 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->data.filter_offset);
3307 tblock->real_offset = clause->data.filter_offset;
3308 tblock->in_scount = 1;
3309 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
3310 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->data.filter_offset);
3311 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
3312 MONO_ADD_INS (tblock, ins);
3317 arg_array = alloca (sizeof (MonoInst *) * num_args);
3318 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
3321 /* FIRST CODE BLOCK */
3322 bblock = NEW_BBLOCK (cfg);
3323 bblock->cil_code = ip;
3325 ADD_BBLOCK (cfg, bbhash, bblock);
3327 if (cfg->method == method) {
3328 breakpoint_id = mono_debugger_method_has_breakpoint (method);
3329 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
3330 MONO_INST_NEW (cfg, ins, CEE_BREAK);
3331 MONO_ADD_INS (bblock, ins);
3335 if (mono_use_security_manager)
3336 secman = mono_security_manager_get_methods ();
3338 security = (secman && mono_method_has_declsec (method));
3339 /* at this point having security doesn't mean we have any code to generate */
3340 if (security && (cfg->method == method)) {
3341 /* Only Demand, NonCasDemand and DemandChoice requires code generation.
3342 * And we do not want to enter the next section (with allocation) if we
3343 * have nothing to generate */
3344 security = mono_declsec_get_demands (method, &actions);
3347 /* we must Demand SecurityPermission.Unmanaged before P/Invoking */
3348 pinvoke = (secman && (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE));
3350 MonoMethod *wrapped = mono_marshal_method_from_wrapper (method);
3351 if (wrapped && (wrapped->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
3352 MonoCustomAttrInfo* custom = mono_custom_attrs_from_method (wrapped);
3354 /* unless the method or it's class has the [SuppressUnmanagedCodeSecurity] attribute */
3355 if (custom && mono_custom_attrs_has_attr (custom, secman->suppressunmanagedcodesecurity)) {
3360 custom = mono_custom_attrs_from_class (wrapped->klass);
3361 if (custom && mono_custom_attrs_has_attr (custom, secman->suppressunmanagedcodesecurity)) {
3366 /* not a P/Invoke after all */
3371 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED))) || mono_compile_aot || security || pinvoke) {
3372 /* we use a separate basic block for the initialization code */
3373 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
3374 init_localsbb->real_offset = real_offset;
3375 start_bblock->next_bb = init_localsbb;
3376 init_localsbb->next_bb = bblock;
3377 link_bblock (cfg, start_bblock, init_localsbb);
3378 link_bblock (cfg, init_localsbb, bblock);
3379 init_localsbb->block_num = cfg->num_bblocks++;
3381 start_bblock->next_bb = bblock;
3382 link_bblock (cfg, start_bblock, bblock);
3385 /* at this point we know, if security is TRUE, that some code needs to be generated */
3386 if (security && (cfg->method == method)) {
3389 mono_jit_stats.cas_demand_generation++;
3391 if (actions.demand.blob) {
3392 /* Add code for SecurityAction.Demand */
3393 NEW_DECLSECCONST (cfg, args[0], image, actions.demand);
3394 NEW_ICONST (cfg, args [1], actions.demand.size);
3395 /* Calls static void SecurityManager.InternalDemand (byte* permissions, int size); */
3396 mono_emit_method_call_spilled (cfg, init_localsbb, secman->demand, mono_method_signature (secman->demand), args, ip, NULL);
3398 if (actions.noncasdemand.blob) {
3399 /* CLR 1.x uses a .noncasdemand (but 2.x doesn't) */
3400 /* For Mono we re-route non-CAS Demand to Demand (as the managed code must deal with it anyway) */
3401 NEW_DECLSECCONST (cfg, args[0], image, actions.noncasdemand);
3402 NEW_ICONST (cfg, args [1], actions.noncasdemand.size);
3403 /* Calls static void SecurityManager.InternalDemand (byte* permissions, int size); */
3404 mono_emit_method_call_spilled (cfg, init_localsbb, secman->demand, mono_method_signature (secman->demand), args, ip, NULL);
3406 if (actions.demandchoice.blob) {
3407 /* New in 2.0, Demand must succeed for one of the permissions (i.e. not all) */
3408 NEW_DECLSECCONST (cfg, args[0], image, actions.demandchoice);
3409 NEW_ICONST (cfg, args [1], actions.demandchoice.size);
3410 /* Calls static void SecurityManager.InternalDemandChoice (byte* permissions, int size); */
3411 mono_emit_method_call_spilled (cfg, init_localsbb, secman->demandchoice, mono_method_signature (secman->demandchoice), args, ip, NULL);
3415 /* we must Demand SecurityPermission.Unmanaged before p/invoking */
3417 mono_emit_method_call_spilled (cfg, init_localsbb, secman->demandunmanaged, mono_method_signature (secman->demandunmanaged), NULL, ip, NULL);
3420 if (get_basic_blocks (cfg, bbhash, header, real_offset, ip, end, &err_pos)) {
3425 if (cfg->method == method)
3426 mono_debug_init_method (cfg, bblock, breakpoint_id);
3428 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
3430 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
3431 for (n = 0; n < sig->param_count; ++n)
3432 param_types [n + sig->hasthis] = sig->params [n];
3435 /* do this somewhere outside - not here */
3436 NEW_ICONST (cfg, zero_int32, 0);
3437 NEW_ICONST (cfg, zero_int64, 0);
3438 zero_int64->type = STACK_I8;
3439 NEW_PCONST (cfg, zero_ptr, 0);
3440 NEW_PCONST (cfg, zero_obj, 0);
3441 zero_obj->type = STACK_OBJ;
3443 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
3444 zero_r8->type = STACK_R8;
3445 zero_r8->inst_p0 = &r8_0;
3447 /* add a check for this != NULL to inlined methods */
3448 if (is_virtual_call) {
3449 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
3450 NEW_ARGLOAD (cfg, ins->inst_left, 0);
3452 MONO_ADD_INS (bblock, ins);
3455 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
3456 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
3459 start_new_bblock = 0;
3462 if (cfg->method == method)
3463 real_offset = ip - header->code;
3465 real_offset = inline_offset;
3467 if (start_new_bblock) {
3468 bblock->cil_length = ip - bblock->cil_code;
3469 if (start_new_bblock == 2) {
3470 g_assert (ip == tblock->cil_code);
3472 GET_BBLOCK (cfg, bbhash, tblock, ip);
3474 bblock->next_bb = tblock;
3476 start_new_bblock = 0;
3477 for (i = 0; i < bblock->in_scount; ++i) {
3478 if (cfg->verbose_level > 3)
3479 g_print ("loading %d from temp %d\n", i, (int)bblock->in_stack [i]->inst_c0);
3480 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
3483 g_slist_free (class_inits);
3486 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
3487 link_bblock (cfg, bblock, tblock);
3488 if (sp != stack_start) {
3489 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3492 bblock->next_bb = tblock;
3494 for (i = 0; i < bblock->in_scount; ++i) {
3495 if (cfg->verbose_level > 3)
3496 g_print ("loading %d from temp %d\n", i, (int)bblock->in_stack [i]->inst_c0);
3497 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
3500 g_slist_free (class_inits);
3505 bblock->real_offset = real_offset;
3507 if ((cfg->method == method) && cfg->coverage_info) {
3508 MonoInst *store, *one;
3509 guint32 cil_offset = ip - header->code;
3510 cfg->coverage_info->data [cil_offset].cil_code = ip;
3512 /* TODO: Use an increment here */
3513 NEW_ICONST (cfg, one, 1);
3516 NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
3519 MONO_INST_NEW (cfg, store, CEE_STIND_I);
3520 store->cil_code = ip;
3521 store->inst_left = ins;
3522 store->inst_right = one;
3524 MONO_ADD_INS (bblock, store);
3527 if (cfg->verbose_level > 3)
3528 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
3535 MONO_INST_NEW (cfg, ins, CEE_BREAK);
3536 ins->cil_code = ip++;
3537 MONO_ADD_INS (bblock, ins);
3543 CHECK_STACK_OVF (1);
3544 n = (*ip)-CEE_LDARG_0;
3546 NEW_ARGLOAD (cfg, ins, n);
3547 ins->cil_code = ip++;
3554 CHECK_STACK_OVF (1);
3555 n = (*ip)-CEE_LDLOC_0;
3557 NEW_LOCLOAD (cfg, ins, n);
3558 ins->cil_code = ip++;
3566 n = (*ip)-CEE_STLOC_0;
3569 handle_loaded_temps (cfg, bblock, stack_start, sp);
3570 NEW_LOCSTORE (cfg, ins, n, *sp);
3572 if (ins->opcode == CEE_STOBJ) {
3573 NEW_LOCLOADA (cfg, ins, n);
3574 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3576 MONO_ADD_INS (bblock, ins);
3582 CHECK_STACK_OVF (1);
3584 NEW_ARGLOAD (cfg, ins, ip [1]);
3591 CHECK_STACK_OVF (1);
3593 NEW_ARGLOADA (cfg, ins, ip [1]);
3603 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
3604 handle_loaded_temps (cfg, bblock, stack_start, sp);
3606 if (ins->opcode == CEE_STOBJ) {
3607 NEW_ARGLOADA (cfg, ins, ip [1]);
3608 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3610 MONO_ADD_INS (bblock, ins);
3615 CHECK_STACK_OVF (1);
3616 CHECK_LOCAL (ip [1]);
3617 NEW_LOCLOAD (cfg, ins, ip [1]);
3624 CHECK_STACK_OVF (1);
3625 CHECK_LOCAL (ip [1]);
3626 NEW_LOCLOADA (cfg, ins, ip [1]);
3635 handle_loaded_temps (cfg, bblock, stack_start, sp);
3636 CHECK_LOCAL (ip [1]);
3637 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
3639 if (ins->opcode == CEE_STOBJ) {
3640 NEW_LOCLOADA (cfg, ins, ip [1]);
3641 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3643 MONO_ADD_INS (bblock, ins);
3648 CHECK_STACK_OVF (1);
3649 NEW_PCONST (cfg, ins, NULL);
3651 ins->type = STACK_OBJ;
3656 CHECK_STACK_OVF (1);
3657 NEW_ICONST (cfg, ins, -1);
3671 CHECK_STACK_OVF (1);
3672 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
3679 CHECK_STACK_OVF (1);
3681 NEW_ICONST (cfg, ins, *((signed char*)ip));
3688 CHECK_STACK_OVF (1);
3689 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
3696 CHECK_STACK_OVF (1);
3697 MONO_INST_NEW (cfg, ins, OP_I8CONST);
3699 ins->type = STACK_I8;
3701 ins->inst_l = (gint64)read64 (ip);
3707 /* we should really allocate this only late in the compilation process */
3708 mono_domain_lock (cfg->domain);
3709 f = mono_mempool_alloc (cfg->domain->mp, sizeof (float));
3710 mono_domain_unlock (cfg->domain);
3712 CHECK_STACK_OVF (1);
3713 MONO_INST_NEW (cfg, ins, OP_R4CONST);
3714 ins->type = STACK_R8;
3725 mono_domain_lock (cfg->domain);
3726 d = mono_mempool_alloc (cfg->domain->mp, sizeof (double));
3727 mono_domain_unlock (cfg->domain);
3729 CHECK_STACK_OVF (1);
3730 MONO_INST_NEW (cfg, ins, OP_R8CONST);
3731 ins->type = STACK_R8;
3741 MonoInst *temp, *store;
3743 CHECK_STACK_OVF (1);
3748 * small optimization: if the loaded value was from a local already,
3749 * just load it twice.
3751 if (ins->ssa_op == MONO_SSA_LOAD &&
3752 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
3754 MONO_INST_NEW (cfg, temp, 0);
3756 temp->cil_code = ip;
3759 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3760 temp->flags |= MONO_INST_IS_TEMP;
3761 temp->cil_code = ip;
3762 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3763 store->cil_code = ip;
3764 if (store->opcode == CEE_STOBJ) {
3765 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
3766 handle_stobj (cfg, bblock, store, sp [0], sp [0]->cil_code, store->klass, TRUE, FALSE);
3768 MONO_ADD_INS (bblock, store);
3770 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3773 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3783 MONO_INST_NEW (cfg, ins, CEE_POP);
3784 MONO_ADD_INS (bblock, ins);
3785 ins->cil_code = ip++;
3791 if (stack_start != sp)
3793 MONO_INST_NEW (cfg, ins, CEE_JMP);
3794 token = read32 (ip + 1);
3795 /* FIXME: check the signature matches */
3796 cmethod = mini_get_method (image, token, NULL, generic_context);
3798 if (mono_use_security_manager) {
3799 check_linkdemand (cfg, method, cmethod, bblock, ip);
3802 ins->inst_p0 = cmethod;
3803 MONO_ADD_INS (bblock, ins);
3805 start_new_bblock = 1;
3809 case CEE_CALLVIRT: {
3810 MonoInst *addr = NULL;
3811 MonoMethodSignature *fsig = NULL;
3812 int temp, array_rank = 0;
3813 int virtual = *ip == CEE_CALLVIRT;
3816 token = read32 (ip + 1);
3818 if (*ip == CEE_CALLI) {
3823 if (method->wrapper_type != MONO_WRAPPER_NONE)
3824 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
3826 fsig = mono_metadata_parse_signature (image, token);
3828 n = fsig->param_count + fsig->hasthis;
3830 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3831 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
3832 } else if (constrained_call) {
3833 cmethod = mono_get_method_constrained (image, token, constrained_call, generic_context);
3834 cmethod = mono_get_inflated_method (cmethod);
3836 cmethod = mini_get_method (image, token, NULL, generic_context);
3841 if (!virtual && (cmethod->flags & METHOD_ATTRIBUTE_ABSTRACT))
3844 if (!cmethod->klass->inited)
3845 mono_class_init (cmethod->klass);
3847 if (mono_method_signature (cmethod)->pinvoke) {
3848 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
3849 fsig = mono_method_signature (wrapper);
3850 } else if (constrained_call) {
3851 fsig = mono_method_signature (cmethod);
3853 fsig = mono_method_get_signature_full (cmethod, image, token, generic_context);
3856 n = fsig->param_count + fsig->hasthis;
3858 if (mono_use_security_manager) {
3859 check_linkdemand (cfg, method, cmethod, bblock, ip);
3862 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
3863 mini_class_is_system_array (cmethod->klass)) {
3864 array_rank = cmethod->klass->rank;
3867 if (cmethod->string_ctor)
3868 g_assert_not_reached ();
3873 mono_get_got_var (cfg);
3875 /* code in inssel.brg might transform a virtual call to a normal call */
3876 if (!(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
3877 ((cmethod->flags & METHOD_ATTRIBUTE_FINAL) &&
3878 cmethod->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK))
3879 mono_get_got_var (cfg);
3882 if (cmethod && cmethod->klass->generic_container) {
3889 //g_assert (!virtual || fsig->hasthis);
3893 if (constrained_call) {
3895 * We have the `constrained.' prefix opcode.
3897 if (constrained_call->valuetype && !cmethod->klass->valuetype) {
3900 * The type parameter is instantiated as a valuetype,
3901 * but that type doesn't override the method we're
3902 * calling, so we need to box `this'.
3903 * sp [0] is a pointer to the data: we need the value
3904 * in handle_box (), so load it here.
3906 MONO_INST_NEW (cfg, load, mono_type_to_ldind (&constrained_call->byval_arg));
3907 type_to_eval_stack_type (&constrained_call->byval_arg, load);
3908 load->cil_code = ip;
3909 load->inst_left = sp [0];
3910 sp [0] = handle_box (cfg, bblock, load, ip, constrained_call);
3911 } else if (!constrained_call->valuetype) {
3915 * The type parameter is instantiated as a reference
3916 * type. We have a managed pointer on the stack, so
3917 * we need to dereference it here.
3920 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3922 ins->inst_i0 = sp [0];
3923 ins->type = STACK_OBJ;
3925 } else if (cmethod->klass->valuetype)
3927 constrained_call = NULL;
3930 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp)) {
3935 if (cmethod && virtual && mono_method_signature (cmethod)->generic_param_count) {
3936 MonoInst *this_temp, *store;
3937 MonoInst *iargs [3];
3939 g_assert (mono_method_signature (cmethod)->is_inflated);
3941 this_temp = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
3942 this_temp->cil_code = ip;
3943 NEW_TEMPSTORE (cfg, store, this_temp->inst_c0, sp [0]);
3945 store->cil_code = ip;
3946 MONO_ADD_INS (bblock, store);
3948 NEW_TEMPLOAD (cfg, iargs [0], this_temp->inst_c0);
3949 NEW_PCONST (cfg, iargs [1], cmethod);
3950 NEW_PCONST (cfg, iargs [2], ((MonoMethodInflated *) cmethod)->context);
3951 temp = mono_emit_jit_icall (cfg, bblock, helper_compile_generic_method, iargs, ip);
3953 NEW_TEMPLOAD (cfg, addr, temp);
3954 NEW_TEMPLOAD (cfg, sp [0], this_temp->inst_c0);
3956 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
3957 NEW_TEMPLOAD (cfg, *sp, temp);
3965 if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL) &&
3966 (mono_metadata_signature_equal (mono_method_signature (method), mono_method_signature (cmethod)))) {
3968 /* FIXME: This assumes the two methods has the same number and type of arguments */
3969 for (i = 0; i < n; ++i) {
3970 /* Check if argument is the same */
3971 NEW_ARGLOAD (cfg, ins, i);
3972 if ((ins->opcode == sp [i]->opcode) && (ins->inst_i0 == sp [i]->inst_i0))
3975 /* Prevent argument from being register allocated */
3976 arg_array [i]->flags |= MONO_INST_VOLATILE;
3977 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3979 if (ins->opcode == CEE_STOBJ) {
3980 NEW_ARGLOADA (cfg, ins, i);
3981 handle_stobj (cfg, bblock, ins, sp [i], sp [i]->cil_code, ins->klass, FALSE, FALSE);
3984 MONO_ADD_INS (bblock, ins);
3986 MONO_INST_NEW (cfg, ins, CEE_JMP);
3988 ins->inst_p0 = cmethod;
3989 ins->inst_p1 = arg_array [0];
3990 MONO_ADD_INS (bblock, ins);
3991 link_bblock (cfg, bblock, end_bblock);
3992 start_new_bblock = 1;
3993 /* skip CEE_RET as well */
3998 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_inst_for_method (cfg, cmethod, fsig, sp))) {
4001 if (MONO_TYPE_IS_VOID (fsig->ret)) {
4002 MONO_ADD_INS (bblock, ins);
4004 type_to_eval_stack_type (fsig->ret, ins);
4013 handle_loaded_temps (cfg, bblock, stack_start, sp);
4015 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
4016 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
4017 mono_method_check_inlining (cfg, cmethod) &&
4018 !g_list_find (dont_inline, cmethod)) {
4020 MonoBasicBlock *ebblock;
4021 gboolean allways = FALSE;
4023 if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
4024 (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
4025 cmethod = mono_marshal_get_native_wrapper (cmethod);
4029 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, allways))) {
4033 GET_BBLOCK (cfg, bbhash, bblock, ip);
4034 ebblock->next_bb = bblock;
4035 link_bblock (cfg, ebblock, bblock);
4037 if (!MONO_TYPE_IS_VOID (fsig->ret))
4040 /* indicates start of a new block, and triggers a load of all
4041 stack arguments at bb boundarie */
4044 inline_costs += costs;
4049 inline_costs += 10 * num_calls++;
4051 /* tail recursion elimination */
4052 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == method && ip [5] == CEE_RET) {
4053 gboolean has_vtargs = FALSE;
4056 /* keep it simple */
4057 for (i = fsig->param_count - 1; i >= 0; i--) {
4058 if (MONO_TYPE_ISSTRUCT (mono_method_signature (cmethod)->params [i]))
4063 for (i = 0; i < n; ++i) {
4064 NEW_ARGSTORE (cfg, ins, i, sp [i]);
4066 MONO_ADD_INS (bblock, ins);
4068 MONO_INST_NEW (cfg, ins, CEE_BR);
4070 MONO_ADD_INS (bblock, ins);
4071 tblock = start_bblock->out_bb [0];
4072 link_bblock (cfg, bblock, tblock);
4073 ins->inst_target_bb = tblock;
4074 start_new_bblock = 1;
4076 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
4077 /* just create a dummy - the value is never used */
4078 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
4079 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
4081 MONO_INST_NEW (cfg, ins, CEE_POP);
4082 MONO_ADD_INS (bblock, ins);
4086 /* skip the CEE_RET, too */
4087 if (ip_in_bb (cfg, bblock, ip + 5))
4096 if (*ip == CEE_CALLI) {
4098 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
4099 NEW_TEMPLOAD (cfg, *sp, temp);
4103 } else if (array_rank) {
4106 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
4107 if (sp [fsig->param_count]->type == STACK_OBJ) {
4108 MonoInst *iargs [2];
4109 MonoInst *array, *to_store, *store;
4111 handle_loaded_temps (cfg, bblock, stack_start, sp);
4113 array = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
4114 NEW_TEMPSTORE (cfg, store, array->inst_c0, sp [0]);
4115 store->cil_code = ip;
4116 MONO_ADD_INS (bblock, store);
4117 NEW_TEMPLOAD (cfg, iargs [0], array->inst_c0);
4119 to_store = mono_compile_create_var (cfg, type_from_stack_type (sp [fsig->param_count]), OP_LOCAL);
4120 NEW_TEMPSTORE (cfg, store, to_store->inst_c0, sp [fsig->param_count]);
4121 store->cil_code = ip;
4122 MONO_ADD_INS (bblock, store);
4123 NEW_TEMPLOAD (cfg, iargs [1], to_store->inst_c0);
4126 * We first save the args for the call so that the args are copied to the stack
4127 * and a new instruction tree for them is created. If we don't do this,
4128 * the same MonoInst is added to two different trees and this is not
4131 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref_check, iargs, ip);
4133 NEW_TEMPLOAD (cfg, sp [0], array->inst_c0);
4134 NEW_TEMPLOAD (cfg, sp [fsig->param_count], to_store->inst_c0);
4137 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
4138 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
4140 if (ins->opcode == CEE_STOBJ) {
4141 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
4143 MONO_ADD_INS (bblock, ins);
4146 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
4147 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
4148 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
4152 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
4153 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
4156 g_assert_not_reached ();
4160 if (ip_in_bb (cfg, bblock, ip + 5)
4161 && (!MONO_TYPE_ISSTRUCT (fsig->ret))
4162 && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)
4163 && (CODE_IS_STLOC (ip + 5) || ip [5] == CEE_POP || ip [5] == CEE_BRTRUE || ip [5] == CEE_BRFALSE ||
4164 ip [5] == CEE_BRTRUE_S || ip [5] == CEE_BRFALSE_S || ip [5] == CEE_RET)) {
4165 /* no need to spill */
4166 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
4169 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) {
4170 NEW_TEMPLOAD (cfg, *sp, temp);
4180 if (cfg->method != method) {
4181 /* return from inlined methode */
4186 //g_assert (returnvar != -1);
4187 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
4188 store->cil_code = sp [0]->cil_code;
4189 if (store->opcode == CEE_STOBJ) {
4190 g_assert_not_reached ();
4191 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
4192 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
4194 MONO_ADD_INS (bblock, store);
4198 g_assert (!return_var);
4201 MONO_INST_NEW (cfg, ins, CEE_NOP);
4202 ins->opcode = mono_type_to_stind (mono_method_signature (method)->ret);
4203 if (ins->opcode == CEE_STOBJ) {
4204 NEW_RETLOADA (cfg, ins);
4205 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4207 ins->opcode = OP_SETRET;
4209 ins->inst_i0 = *sp;;
4210 ins->inst_i1 = NULL;
4211 MONO_ADD_INS (bblock, ins);
4215 if (sp != stack_start)
4217 MONO_INST_NEW (cfg, ins, CEE_BR);
4218 ins->cil_code = ip++;
4219 ins->inst_target_bb = end_bblock;
4220 MONO_ADD_INS (bblock, ins);
4221 link_bblock (cfg, bblock, end_bblock);
4222 start_new_bblock = 1;
4226 MONO_INST_NEW (cfg, ins, CEE_BR);
4227 ins->cil_code = ip++;
4228 MONO_ADD_INS (bblock, ins);
4229 target = ip + 1 + (signed char)(*ip);
4231 GET_BBLOCK (cfg, bbhash, tblock, target);
4232 link_bblock (cfg, bblock, tblock);
4233 CHECK_BBLOCK (target, ip, tblock);
4234 ins->inst_target_bb = tblock;
4235 if (sp != stack_start) {
4236 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4239 start_new_bblock = 1;
4246 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
4247 ins->cil_code = ip++;
4248 target = ip + 1 + *(signed char*)ip;
4250 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
4251 if (sp != stack_start) {
4252 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4269 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
4270 ins->cil_code = ip++;
4271 target = ip + 1 + *(signed char*)ip;
4274 if (sp != stack_start) {
4275 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4282 MONO_INST_NEW (cfg, ins, CEE_BR);
4283 ins->cil_code = ip++;
4284 MONO_ADD_INS (bblock, ins);
4285 target = ip + 4 + (gint32)read32(ip);
4287 GET_BBLOCK (cfg, bbhash, tblock, target);
4288 link_bblock (cfg, bblock, tblock);
4289 CHECK_BBLOCK (target, ip, tblock);
4290 ins->inst_target_bb = tblock;
4291 if (sp != stack_start) {
4292 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4295 start_new_bblock = 1;
4302 MONO_INST_NEW (cfg, ins, *ip);
4303 ins->cil_code = ip++;
4304 target = ip + 4 + (gint32)read32(ip);
4306 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
4307 if (sp != stack_start) {
4308 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4325 MONO_INST_NEW (cfg, ins, *ip);
4326 ins->cil_code = ip++;
4327 target = ip + 4 + (gint32)read32(ip);
4330 if (sp != stack_start) {
4331 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4339 n = read32 (ip + 1);
4340 MONO_INST_NEW (cfg, ins, *ip);
4342 ins->inst_left = *sp;
4343 if ((ins->inst_left->type != STACK_I4) && (ins->inst_left->type != STACK_PTR))
4347 CHECK_OPSIZE (n * sizeof (guint32));
4348 target = ip + n * sizeof (guint32);
4349 MONO_ADD_INS (bblock, ins);
4350 GET_BBLOCK (cfg, bbhash, tblock, target);
4351 link_bblock (cfg, bblock, tblock);
4352 ins->klass = GUINT_TO_POINTER (n);
4353 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
4354 ins->inst_many_bb [n] = tblock;
4356 for (i = 0; i < n; ++i) {
4357 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
4358 link_bblock (cfg, bblock, tblock);
4359 ins->inst_many_bb [i] = tblock;
4362 if (sp != stack_start) {
4363 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4366 /* Needed by the code generated in inssel.brg */
4367 mono_get_got_var (cfg);
4382 MONO_INST_NEW (cfg, ins, *ip);
4387 ins->type = ldind_type [*ip - CEE_LDIND_I1];
4388 ins->flags |= ins_flag;
4400 MONO_INST_NEW (cfg, ins, *ip);
4401 ins->cil_code = ip++;
4403 handle_loaded_temps (cfg, bblock, stack_start, sp);
4404 MONO_ADD_INS (bblock, ins);
4405 ins->inst_i0 = sp [0];
4406 ins->inst_i1 = sp [1];
4407 ins->flags |= ins_flag;
4426 /* special case that gives a nice speedup and happens to workaorund a ppc jit but (for the release)
4427 * later apply the speedup to the left shift as well
4430 if ((ins->opcode == OP_LSHR_UN) && (ins->type == STACK_I8)
4431 && (ins->inst_right->opcode == OP_ICONST) && (ins->inst_right->inst_c0 == 32)) {
4432 ins->opcode = OP_LONG_SHRUN_32;
4433 /*g_print ("applied long shr speedup to %s\n", cfg->method->name);*/
4437 if (mono_find_jit_opcode_emulation (ins->opcode)) {
4439 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
4440 mono_get_got_var (cfg);
4454 case CEE_CONV_OVF_I8:
4455 case CEE_CONV_OVF_U8:
4459 if (mono_find_jit_opcode_emulation (ins->opcode)) {
4461 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
4462 mono_get_got_var (cfg);
4466 case CEE_CONV_OVF_I4:
4467 case CEE_CONV_OVF_I1:
4468 case CEE_CONV_OVF_I2:
4469 case CEE_CONV_OVF_I:
4470 case CEE_CONV_OVF_U:
4473 if (sp [-1]->type == STACK_R8) {
4474 ADD_UNOP (CEE_CONV_OVF_I8);
4482 case CEE_CONV_OVF_U1:
4483 case CEE_CONV_OVF_U2:
4484 case CEE_CONV_OVF_U4:
4487 if (sp [-1]->type == STACK_R8) {
4488 ADD_UNOP (CEE_CONV_OVF_U8);
4496 case CEE_CONV_OVF_I1_UN:
4497 case CEE_CONV_OVF_I2_UN:
4498 case CEE_CONV_OVF_I4_UN:
4499 case CEE_CONV_OVF_I8_UN:
4500 case CEE_CONV_OVF_U1_UN:
4501 case CEE_CONV_OVF_U2_UN:
4502 case CEE_CONV_OVF_U4_UN:
4503 case CEE_CONV_OVF_U8_UN:
4504 case CEE_CONV_OVF_I_UN:
4505 case CEE_CONV_OVF_U_UN:
4513 token = read32 (ip + 1);
4514 if (method->wrapper_type != MONO_WRAPPER_NONE)
4515 klass = mono_method_get_wrapper_data (method, token);
4517 klass = mono_class_get_full (image, token, generic_context);
4519 mono_class_init (klass);
4521 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4522 MonoInst *store, *load;
4523 MONO_INST_NEW (cfg, load, CEE_LDIND_REF);
4524 load->cil_code = ip;
4525 load->inst_i0 = sp [1];
4526 load->type = STACK_OBJ;
4527 load->flags |= ins_flag;
4528 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
4529 store->cil_code = ip;
4530 handle_loaded_temps (cfg, bblock, stack_start, sp);
4531 MONO_ADD_INS (bblock, store);
4532 store->inst_i0 = sp [0];
4533 store->inst_i1 = load;
4534 store->flags |= ins_flag;
4536 n = mono_class_value_size (klass, NULL);
4537 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
4539 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
4540 copy->inst_left = sp [0];
4541 copy->inst_right = sp [1];
4542 copy->cil_code = ip;
4544 MONO_ADD_INS (bblock, copy);
4546 MonoMethod *memcpy_method = get_memcpy_method ();
4547 MonoInst *iargs [3];
4550 NEW_ICONST (cfg, iargs [2], n);
4551 iargs [2]->cil_code = ip;
4553 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
4560 MonoInst *iargs [3];
4566 token = read32 (ip + 1);
4567 if (method->wrapper_type != MONO_WRAPPER_NONE)
4568 klass = mono_method_get_wrapper_data (method, token);
4570 klass = mono_class_get_full (image, token, generic_context);
4572 mono_class_init (klass);
4573 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4574 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
4576 ins->inst_i0 = sp [0];
4577 ins->type = STACK_OBJ;
4578 ins->flags |= ins_flag;
4585 /* Optimize the common ldobj+stloc combination */
4595 loc_index = ip [5] - CEE_STLOC_0;
4602 if ((loc_index != -1) && ip_in_bb (cfg, bblock, ip + 5)) {
4603 CHECK_LOCAL (loc_index);
4604 NEW_LOCSTORE (cfg, ins, loc_index, *sp);
4606 if (ins->opcode == CEE_STOBJ) {
4607 handle_loaded_temps (cfg, bblock, stack_start, sp);
4609 g_assert (ins->opcode == CEE_STOBJ);
4610 NEW_LOCLOADA (cfg, ins, loc_index);
4611 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4618 n = mono_class_value_size (klass, NULL);
4619 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
4620 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
4621 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
4623 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
4624 copy->inst_left = iargs [0];
4625 copy->inst_right = *sp;
4626 copy->cil_code = ip;
4628 MONO_ADD_INS (bblock, copy);
4630 MonoMethod *memcpy_method = get_memcpy_method ();
4632 NEW_ICONST (cfg, iargs [2], n);
4633 iargs [2]->cil_code = ip;
4635 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
4637 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
4645 CHECK_STACK_OVF (1);
4647 n = read32 (ip + 1);
4649 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
4650 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, n));
4652 ins->type = STACK_OBJ;
4655 else if (method->wrapper_type != MONO_WRAPPER_NONE) {
4657 MonoInst *iargs [1];
4659 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
4660 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
4661 NEW_TEMPLOAD (cfg, *sp, temp);
4665 if (cfg->opt & MONO_OPT_SHARED) {
4667 MonoInst *iargs [3];
4668 MonoInst* domain_var;
4670 if (cfg->compile_aot) {
4671 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, GINT_TO_POINTER (n));
4673 /* avoid depending on undefined C behavior in sequence points */
4674 domain_var = mono_get_domainvar (cfg);
4675 NEW_TEMPLOAD (cfg, iargs [0], domain_var->inst_c0);
4676 NEW_IMAGECONST (cfg, iargs [1], image);
4677 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
4678 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
4679 NEW_TEMPLOAD (cfg, *sp, temp);
4680 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
4682 if (cfg->compile_aot)
4683 NEW_LDSTRCONST (cfg, ins, image, n);
4685 NEW_PCONST (cfg, ins, NULL);
4687 ins->type = STACK_OBJ;
4688 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
4698 MonoInst *iargs [2];
4699 MonoMethodSignature *fsig;
4703 token = read32 (ip + 1);
4704 if (method->wrapper_type != MONO_WRAPPER_NONE) {
4705 cmethod = mono_method_get_wrapper_data (method, token);
4707 cmethod = mini_get_method (image, token, NULL, generic_context);
4708 fsig = mono_method_get_signature (cmethod, image, token);
4710 mono_class_init (cmethod->klass);
4712 if (mono_use_security_manager) {
4713 check_linkdemand (cfg, method, cmethod, bblock, ip);
4716 n = fsig->param_count;
4719 /* move the args to allow room for 'this' in the first position */
4725 handle_loaded_temps (cfg, bblock, stack_start, sp);
4727 if (mini_class_is_system_array (cmethod->klass)) {
4728 NEW_METHODCONST (cfg, *sp, cmethod);
4729 temp = handle_array_new (cfg, bblock, fsig->param_count, sp, ip);
4730 } else if (cmethod->string_ctor) {
4731 /* we simply pass a null pointer */
4732 NEW_PCONST (cfg, *sp, NULL);
4733 /* now call the string ctor */
4734 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
4736 MonoInst* callvirt_this_arg = NULL;
4738 if (cmethod->klass->valuetype) {
4739 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
4740 temp = iargs [0]->inst_c0;
4742 NEW_TEMPLOADA (cfg, *sp, temp);
4744 handle_initobj (cfg, bblock, *sp, NULL, cmethod->klass, stack_start, sp);
4746 NEW_TEMPLOADA (cfg, *sp, temp);
4749 * The code generated by mini_emit_virtual_call () expects
4750 * iargs [0] to be a boxed instance, but luckily the vcall
4751 * will be transformed into a normal call there. The AOT
4752 * case needs an already allocate got_var.
4754 mono_get_got_var (cfg);
4756 temp = handle_alloc (cfg, bblock, cmethod->klass, FALSE, ip);
4757 NEW_TEMPLOAD (cfg, *sp, temp);
4760 /* Avoid virtual calls to ctors if possible */
4761 if (cmethod->klass->marshalbyref)
4762 callvirt_this_arg = sp [0];
4764 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
4765 mono_method_check_inlining (cfg, cmethod) &&
4766 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
4767 !g_list_find (dont_inline, cmethod)) {
4769 MonoBasicBlock *ebblock;
4770 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, FALSE))) {
4775 GET_BBLOCK (cfg, bbhash, bblock, ip);
4776 ebblock->next_bb = bblock;
4777 link_bblock (cfg, ebblock, bblock);
4779 NEW_TEMPLOAD (cfg, *sp, temp);
4782 /* indicates start of a new block, and triggers a load
4783 of all stack arguments at bb boundarie */
4786 inline_costs += costs;
4790 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, callvirt_this_arg);
4793 /* now call the actual ctor */
4794 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, callvirt_this_arg);
4798 NEW_TEMPLOAD (cfg, *sp, temp);
4809 token = read32 (ip + 1);
4810 if (method->wrapper_type != MONO_WRAPPER_NONE)
4811 klass = mono_method_get_wrapper_data (method, token);
4813 klass = mono_class_get_full (image, token, generic_context);
4814 mono_class_init (klass);
4816 /* Needed by the code generated in inssel.brg */
4817 mono_get_got_var (cfg);
4819 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4821 MonoMethod *mono_isinst;
4822 MonoInst *iargs [1];
4823 MonoBasicBlock *ebblock;
4827 mono_isinst = mono_marshal_get_isinst (klass);
4830 costs = inline_method (cfg, mono_isinst, mono_method_signature (mono_isinst), bblock,
4831 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4833 g_assert (costs > 0);
4838 GET_BBLOCK (cfg, bbhash, bblock, ip);
4839 ebblock->next_bb = bblock;
4840 link_bblock (cfg, ebblock, bblock);
4842 temp = iargs [0]->inst_i0->inst_c0;
4843 NEW_TEMPLOAD (cfg, *sp, temp);
4847 inline_costs += costs;
4851 MONO_INST_NEW (cfg, ins, *ip);
4852 ins->type = STACK_OBJ;
4853 ins->inst_left = *sp;
4854 ins->inst_newa_class = klass;
4856 *sp++ = emit_tree (cfg, bblock, ins, ip + 5);
4860 case CEE_UNBOX_ANY: {
4861 MonoInst *add, *vtoffset;
4862 MonoInst *iargs [3];
4867 token = read32 (ip + 1);
4868 if (method->wrapper_type != MONO_WRAPPER_NONE)
4869 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4871 klass = mono_class_get_full (image, token, generic_context);
4872 mono_class_init (klass);
4874 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4876 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4877 MonoMethod *mono_castclass;
4878 MonoInst *iargs [1];
4879 MonoBasicBlock *ebblock;
4883 mono_castclass = mono_marshal_get_castclass (klass);
4886 costs = inline_method (cfg, mono_castclass, mono_method_signature (mono_castclass), bblock,
4887 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4889 g_assert (costs > 0);
4894 GET_BBLOCK (cfg, bbhash, bblock, ip);
4895 ebblock->next_bb = bblock;
4896 link_bblock (cfg, ebblock, bblock);
4898 temp = iargs [0]->inst_i0->inst_c0;
4899 NEW_TEMPLOAD (cfg, *sp, temp);
4903 inline_costs += costs;
4906 MONO_INST_NEW (cfg, ins, CEE_CASTCLASS);
4907 ins->type = STACK_OBJ;
4908 ins->inst_left = *sp;
4910 ins->inst_newa_class = klass;
4918 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
4919 ins->type = STACK_OBJ;
4920 ins->inst_left = *sp;
4922 ins->inst_newa_class = klass;
4925 MONO_INST_NEW (cfg, add, OP_PADD);
4926 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4927 add->inst_left = ins;
4928 add->inst_right = vtoffset;
4929 add->type = STACK_MP;
4933 n = mono_class_value_size (klass, NULL);
4934 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
4935 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
4936 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
4938 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
4939 copy->inst_left = iargs [0];
4940 copy->inst_right = *sp;
4941 copy->cil_code = ip;
4943 MONO_ADD_INS (bblock, copy);
4945 MonoMethod *memcpy_method = get_memcpy_method ();
4947 NEW_ICONST (cfg, iargs [2], n);
4948 iargs [2]->cil_code = ip;
4950 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
4952 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
4958 MonoInst *add, *vtoffset;
4963 token = read32 (ip + 1);
4964 if (method->wrapper_type != MONO_WRAPPER_NONE)
4965 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4967 klass = mono_class_get_full (image, token, generic_context);
4968 mono_class_init (klass);
4970 /* Needed by the code generated in inssel.brg */
4971 mono_get_got_var (cfg);
4973 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
4974 ins->type = STACK_OBJ;
4975 ins->inst_left = *sp;
4977 ins->inst_newa_class = klass;
4980 MONO_INST_NEW (cfg, add, OP_PADD);
4981 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4982 add->inst_left = ins;
4983 add->inst_right = vtoffset;
4984 add->type = STACK_MP;
4994 token = read32 (ip + 1);
4995 if (method->wrapper_type != MONO_WRAPPER_NONE)
4996 klass = mono_method_get_wrapper_data (method, token);
4998 klass = mono_class_get_full (image, token, generic_context);
4999 mono_class_init (klass);
5001 /* Needed by the code generated in inssel.brg */
5002 mono_get_got_var (cfg);
5004 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
5006 MonoMethod *mono_castclass;
5007 MonoInst *iargs [1];
5008 MonoBasicBlock *ebblock;
5012 mono_castclass = mono_marshal_get_castclass (klass);
5015 costs = inline_method (cfg, mono_castclass, mono_method_signature (mono_castclass), bblock,
5016 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
5018 g_assert (costs > 0);
5023 GET_BBLOCK (cfg, bbhash, bblock, ip);
5024 ebblock->next_bb = bblock;
5025 link_bblock (cfg, ebblock, bblock);
5027 temp = iargs [0]->inst_i0->inst_c0;
5028 NEW_TEMPLOAD (cfg, *sp, temp);
5032 inline_costs += costs;
5035 MONO_INST_NEW (cfg, ins, *ip);
5036 ins->type = STACK_OBJ;
5037 ins->inst_left = *sp;
5039 ins->inst_newa_class = klass;
5041 *sp++ = emit_tree (cfg, bblock, ins, ip + 5);
5047 MONO_INST_NEW (cfg, ins, *ip);
5049 ins->inst_left = *sp;
5050 ins->cil_code = ip++;
5051 bblock->out_of_line = TRUE;
5052 MONO_ADD_INS (bblock, ins);
5053 MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
5054 ins->cil_code = ip - 1;
5055 MONO_ADD_INS (bblock, ins);
5057 link_bblock (cfg, bblock, end_bblock);
5058 start_new_bblock = 1;
5059 mono_get_got_var (cfg);
5064 MonoInst *offset_ins;
5065 MonoClassField *field;
5066 MonoBasicBlock *ebblock;
5070 if (*ip == CEE_STFLD) {
5077 // FIXME: enable this test later.
5078 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
5081 token = read32 (ip + 1);
5082 field = mono_field_from_token (image, token, &klass, generic_context);
5083 mono_class_init (klass);
5085 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
5086 /* FIXME: mark instructions for use in SSA */
5087 if (*ip == CEE_STFLD) {
5088 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound || klass == mono_defaults.marshalbyrefobject_class) {
5089 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
5090 MonoInst *iargs [5];
5093 NEW_CLASSCONST (cfg, iargs [1], klass);
5094 NEW_FIELDCONST (cfg, iargs [2], field);
5095 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
5099 if (cfg->opt & MONO_OPT_INLINE) {
5100 costs = inline_method (cfg, stfld_wrapper, mono_method_signature (stfld_wrapper), bblock,
5101 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
5102 g_assert (costs > 0);
5107 GET_BBLOCK (cfg, bbhash, bblock, ip);
5108 ebblock->next_bb = bblock;
5109 link_bblock (cfg, ebblock, bblock);
5111 /* indicates start of a new block, and triggers a load
5112 of all stack arguments at bb boundarie */
5115 inline_costs += costs;
5118 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, mono_method_signature (stfld_wrapper), iargs, ip, NULL);
5122 NEW_ICONST (cfg, offset_ins, foffset);
5123 MONO_INST_NEW (cfg, ins, OP_PADD);
5125 ins->inst_left = *sp;
5126 ins->inst_right = offset_ins;
5127 ins->type = STACK_MP;
5129 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
5130 store->cil_code = ip;
5131 store->inst_left = ins;
5132 store->inst_right = sp [1];
5133 handle_loaded_temps (cfg, bblock, stack_start, sp);
5134 store->flags |= ins_flag;
5136 if (store->opcode == CEE_STOBJ) {
5137 handle_stobj (cfg, bblock, ins, sp [1], ip,
5138 mono_class_from_mono_type (field->type), FALSE, FALSE);
5140 MONO_ADD_INS (bblock, store);
5143 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound || klass == mono_defaults.marshalbyrefobject_class) {
5144 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
5145 MonoInst *iargs [4];
5149 NEW_CLASSCONST (cfg, iargs [1], klass);
5150 NEW_FIELDCONST (cfg, iargs [2], field);
5151 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
5152 if ((cfg->opt & MONO_OPT_INLINE) && !MONO_TYPE_ISSTRUCT (mono_method_signature (ldfld_wrapper)->ret)) {
5153 costs = inline_method (cfg, ldfld_wrapper, mono_method_signature (ldfld_wrapper), bblock,
5154 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
5155 g_assert (costs > 0);
5160 GET_BBLOCK (cfg, bbhash, bblock, ip);
5161 ebblock->next_bb = bblock;
5162 link_bblock (cfg, ebblock, bblock);
5164 temp = iargs [0]->inst_i0->inst_c0;
5166 if (*ip == CEE_LDFLDA) {
5167 /* not sure howto handle this */
5168 NEW_TEMPLOADA (cfg, *sp, temp);
5170 NEW_TEMPLOAD (cfg, *sp, temp);
5174 /* indicates start of a new block, and triggers a load of
5175 all stack arguments at bb boundarie */
5178 inline_costs += costs;
5181 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, mono_method_signature (ldfld_wrapper), iargs, ip, NULL);
5182 if (*ip == CEE_LDFLDA) {
5183 /* not sure howto handle this */
5184 NEW_TEMPLOADA (cfg, *sp, temp);
5186 NEW_TEMPLOAD (cfg, *sp, temp);
5191 NEW_ICONST (cfg, offset_ins, foffset);
5192 MONO_INST_NEW (cfg, ins, OP_PADD);
5194 ins->inst_left = *sp;
5195 ins->inst_right = offset_ins;
5196 ins->type = STACK_MP;
5198 if (*ip == CEE_LDFLDA) {
5202 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
5203 type_to_eval_stack_type (field->type, load);
5204 load->cil_code = ip;
5205 load->inst_left = ins;
5206 load->flags |= ins_flag;
5218 MonoClassField *field;
5219 gpointer addr = NULL;
5222 token = read32 (ip + 1);
5224 field = mono_field_from_token (image, token, &klass, generic_context);
5225 mono_class_init (klass);
5227 g_assert (!(field->type->attrs & FIELD_ATTRIBUTE_LITERAL));
5229 if ((*ip) == CEE_STSFLD)
5230 handle_loaded_temps (cfg, bblock, stack_start, sp);
5232 /* The special_static_fields field is init'd in mono_class_vtable, so it needs
5233 * to be called here.
5235 if (!(cfg->opt & MONO_OPT_SHARED))
5236 mono_class_vtable (cfg->domain, klass);
5237 mono_domain_lock (cfg->domain);
5238 if (cfg->domain->special_static_fields)
5239 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
5240 mono_domain_unlock (cfg->domain);
5242 if ((cfg->opt & MONO_OPT_SHARED) || (cfg->compile_aot && addr)) {
5244 MonoInst *iargs [2];
5245 MonoInst *domain_var;
5247 g_assert (field->parent);
5248 /* avoid depending on undefined C behavior in sequence points */
5249 domain_var = mono_get_domainvar (cfg);
5250 NEW_TEMPLOAD (cfg, iargs [0], domain_var->inst_c0);
5251 NEW_FIELDCONST (cfg, iargs [1], field);
5252 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
5253 NEW_TEMPLOAD (cfg, ins, temp);
5256 vtable = mono_class_vtable (cfg->domain, klass);
5258 if (mini_field_access_needs_cctor_run (cfg, method, vtable) && !(g_slist_find (class_inits, vtable))) {
5259 guint8 *tramp = mono_create_class_init_trampoline (vtable);
5260 mono_emit_native_call (cfg, bblock, tramp,
5261 helper_sig_class_init_trampoline,
5262 NULL, ip, FALSE, FALSE);
5263 if (cfg->verbose_level > 2)
5264 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
5265 class_inits = g_slist_prepend (class_inits, vtable);
5267 if (cfg->run_cctors)
5268 mono_runtime_class_init (vtable);
5270 addr = (char*)vtable->data + field->offset;
5272 if (cfg->compile_aot)
5273 NEW_SFLDACONST (cfg, ins, field);
5275 NEW_PCONST (cfg, ins, addr);
5279 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
5280 * This could be later optimized to do just a couple of
5281 * memory dereferences with constant offsets.
5284 MonoInst *iargs [1];
5285 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
5286 temp = mono_emit_jit_icall (cfg, bblock, mono_get_special_static_data, iargs, ip);
5287 NEW_TEMPLOAD (cfg, ins, temp);
5291 /* FIXME: mark instructions for use in SSA */
5292 if (*ip == CEE_LDSFLDA) {
5294 } else if (*ip == CEE_STSFLD) {
5298 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
5299 store->cil_code = ip;
5300 store->inst_left = ins;
5301 store->inst_right = sp [0];
5302 store->flags |= ins_flag;
5305 if (store->opcode == CEE_STOBJ) {
5306 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
5308 MONO_ADD_INS (bblock, store);
5310 gboolean is_const = FALSE;
5311 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
5312 if (!((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) &&
5313 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
5314 gpointer addr = (char*)vtable->data + field->offset;
5315 int ro_type = field->type->type;
5316 if (ro_type == MONO_TYPE_VALUETYPE && field->type->data.klass->enumtype) {
5317 ro_type = field->type->data.klass->enum_basetype->type;
5319 /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
5322 case MONO_TYPE_BOOLEAN:
5324 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
5328 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
5331 case MONO_TYPE_CHAR:
5333 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
5337 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
5342 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
5346 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
5351 case MONO_TYPE_STRING:
5352 case MONO_TYPE_OBJECT:
5353 case MONO_TYPE_CLASS:
5354 case MONO_TYPE_SZARRAY:
5356 case MONO_TYPE_FNPTR:
5357 case MONO_TYPE_ARRAY:
5358 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
5359 type_to_eval_stack_type (field->type, *sp);
5364 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
5365 sp [0]->type = STACK_I8;
5366 sp [0]->inst_l = *((gint64 *)addr);
5371 case MONO_TYPE_VALUETYPE:
5380 CHECK_STACK_OVF (1);
5381 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
5382 type_to_eval_stack_type (field->type, load);
5383 load->cil_code = ip;
5384 load->inst_left = ins;
5386 load->flags |= ins_flag;
5388 /* fixme: dont see the problem why this does not work */
5389 //cfg->disable_aot = TRUE;
5399 token = read32 (ip + 1);
5400 if (method->wrapper_type != MONO_WRAPPER_NONE)
5401 klass = mono_method_get_wrapper_data (method, token);
5403 klass = mono_class_get_full (image, token, generic_context);
5404 mono_class_init (klass);
5405 n = mono_type_to_stind (&klass->byval_arg);
5406 if (n == CEE_STOBJ) {
5407 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
5409 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
5411 MONO_INST_NEW (cfg, store, n);
5412 store->cil_code = ip;
5413 store->inst_left = sp [0];
5414 store->inst_right = sp [1];
5415 store->flags |= ins_flag;
5416 MONO_ADD_INS (bblock, store);
5428 token = read32 (ip + 1);
5429 if (method->wrapper_type != MONO_WRAPPER_NONE)
5430 klass = mono_method_get_wrapper_data (method, token);
5432 klass = mono_class_get_full (image, token, generic_context);
5433 mono_class_init (klass);
5435 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5440 *sp++ = handle_box (cfg, bblock, val, ip, klass);
5447 MONO_INST_NEW (cfg, ins, *ip);
5452 token = read32 (ip + 1);
5454 /* allocate the domainvar - becaus this is used in decompose_foreach */
5455 if (cfg->opt & MONO_OPT_SHARED) {
5456 mono_get_domainvar (cfg);
5457 /* LAME-IR: Mark it as used since otherwise it will be optimized away */
5458 cfg->domainvar->flags |= MONO_INST_VOLATILE;
5462 mono_get_got_var (cfg);
5464 if (method->wrapper_type != MONO_WRAPPER_NONE)
5465 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5467 klass = mono_class_get_full (image, token, generic_context);
5469 mono_class_init (klass);
5470 ins->inst_newa_class = klass;
5471 ins->inst_newa_len = *sp;
5472 ins->type = STACK_OBJ;
5476 * we store the object so calls to create the array are not interleaved
5477 * with the arguments of other calls.
5480 MonoInst *store, *temp, *load;
5482 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
5483 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
5484 store->cil_code = ins->cil_code;
5485 MONO_ADD_INS (bblock, store);
5486 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
5487 load->cil_code = ins->cil_code;
5494 MONO_INST_NEW (cfg, ins, *ip);
5495 ins->cil_code = ip++;
5497 ins->inst_left = *sp;
5498 ins->type = STACK_PTR;
5506 if (method->wrapper_type != MONO_WRAPPER_NONE)
5507 klass = (MonoClass*)mono_method_get_wrapper_data (method, read32 (ip + 1));
5509 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
5511 /* we need to make sure that this array is exactly the type it needs
5512 * to be for correctness. the wrappers are lax with their usage
5513 * so we need to ignore them here
5515 if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE) {
5517 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
5518 check->cil_code = ip;
5519 check->klass = klass;
5520 check->inst_left = sp [0];
5521 check->type = STACK_OBJ;
5525 mono_class_init (klass);
5526 NEW_LDELEMA (cfg, ins, sp, klass);
5531 case CEE_LDELEM_ANY: {
5536 token = read32 (ip + 1);
5537 klass = mono_class_get_full (image, token, generic_context);
5538 mono_class_init (klass);
5539 NEW_LDELEMA (cfg, load, sp, klass);
5540 load->cil_code = ip;
5541 MONO_INST_NEW (cfg, ins, mono_type_to_ldind (&klass->byval_arg));
5543 ins->inst_left = load;
5545 type_to_eval_stack_type (&klass->byval_arg, ins);
5559 case CEE_LDELEM_REF: {
5563 * ldind.x (ldelema (array, index))
5564 * ldelema does the bounds check
5568 klass = array_access_to_klass (*ip);
5569 NEW_LDELEMA (cfg, load, sp, klass);
5570 load->cil_code = ip;
5571 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
5573 ins->inst_left = load;
5575 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
5585 case CEE_STELEM_R8: {
5589 * stind.x (ldelema (array, index), val)
5590 * ldelema does the bounds check
5594 klass = array_access_to_klass (*ip);
5595 NEW_LDELEMA (cfg, load, sp, klass);
5596 load->cil_code = ip;
5597 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
5599 ins->inst_left = load;
5600 ins->inst_right = sp [2];
5602 handle_loaded_temps (cfg, bblock, stack_start, sp);
5603 MONO_ADD_INS (bblock, ins);
5607 case CEE_STELEM_ANY: {
5611 * stind.x (ldelema (array, index), val)
5612 * ldelema does the bounds check
5617 token = read32 (ip + 1);
5618 klass = mono_class_get_full (image, token, generic_context);
5619 mono_class_init (klass);
5620 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5621 MonoMethod* helper = mono_marshal_get_stelemref ();
5622 MonoInst *iargs [3];
5623 handle_loaded_temps (cfg, bblock, stack_start, sp);
5629 mono_emit_method_call_spilled (cfg, bblock, helper, mono_method_signature (helper), iargs, ip, NULL);
5631 NEW_LDELEMA (cfg, load, sp, klass);
5632 load->cil_code = ip;
5634 n = mono_type_to_stind (&klass->byval_arg);
5636 handle_stobj (cfg, bblock, load, sp [2], ip, klass, FALSE, FALSE);
5638 MONO_INST_NEW (cfg, ins, n);
5640 ins->inst_left = load;
5641 ins->inst_right = sp [2];
5642 handle_loaded_temps (cfg, bblock, stack_start, sp);
5643 MONO_ADD_INS (bblock, ins);
5650 case CEE_STELEM_REF: {
5651 MonoInst *iargs [3];
5652 MonoMethod* helper = mono_marshal_get_stelemref ();
5657 handle_loaded_temps (cfg, bblock, stack_start, sp);
5663 mono_emit_method_call_spilled (cfg, bblock, helper, mono_method_signature (helper), iargs, ip, NULL);
5667 NEW_GROUP (cfg, group, sp [0], sp [1]);
5668 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
5670 ins->inst_left = group;
5671 ins->inst_right = sp [2];
5672 MONO_ADD_INS (bblock, ins);
5679 case CEE_CKFINITE: {
5680 MonoInst *store, *temp;
5683 /* this instr. can throw exceptions as side effect,
5684 * so we cant eliminate dead code which contains CKFINITE opdodes.
5685 * Spilling to memory makes sure that we always perform
5689 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
5691 ins->inst_left = sp [-1];
5692 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
5694 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
5695 store->cil_code = ip;
5696 MONO_ADD_INS (bblock, store);
5698 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
5705 MONO_INST_NEW (cfg, ins, *ip);
5708 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
5709 mono_class_init (klass);
5710 ins->type = STACK_MP;
5711 ins->inst_left = *sp;
5713 ins->inst_newa_class = klass;
5718 case CEE_MKREFANY: {
5719 MonoInst *loc, *klassconst;
5722 MONO_INST_NEW (cfg, ins, *ip);
5725 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
5726 mono_class_init (klass);
5729 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
5730 NEW_TEMPLOADA (cfg, ins->inst_right, loc->inst_c0);
5732 NEW_PCONST (cfg, klassconst, klass);
5733 NEW_GROUP (cfg, ins->inst_left, *sp, klassconst);
5735 MONO_ADD_INS (bblock, ins);
5737 NEW_TEMPLOAD (cfg, *sp, loc->inst_c0);
5744 MonoClass *handle_class;
5746 CHECK_STACK_OVF (1);
5749 n = read32 (ip + 1);
5751 handle = mono_ldtoken (image, n, &handle_class, generic_context);
5752 mono_class_init (handle_class);
5754 if (cfg->opt & MONO_OPT_SHARED) {
5756 MonoInst *res, *store, *addr, *vtvar, *iargs [3];
5758 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
5760 NEW_IMAGECONST (cfg, iargs [0], image);
5761 NEW_ICONST (cfg, iargs [1], n);
5762 NEW_PCONST (cfg, iargs [2], generic_context);
5763 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
5764 NEW_TEMPLOAD (cfg, res, temp);
5765 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5766 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
5767 MONO_ADD_INS (bblock, store);
5768 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5770 if ((ip [5] == CEE_CALL) && (cmethod = mini_get_method (image, read32 (ip + 6), NULL, generic_context)) &&
5771 (cmethod->klass == mono_defaults.monotype_class->parent) &&
5772 (strcmp (cmethod->name, "GetTypeFromHandle") == 0) && ip_in_bb (cfg, bblock, ip + 5)) {
5773 MonoClass *tclass = mono_class_from_mono_type (handle);
5774 mono_class_init (tclass);
5775 if (cfg->compile_aot)
5776 NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n);
5778 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
5779 ins->type = STACK_OBJ;
5780 ins->klass = cmethod->klass;
5783 MonoInst *store, *addr, *vtvar;
5785 if (cfg->compile_aot)
5786 NEW_LDTOKENCONST (cfg, ins, image, n);
5788 NEW_PCONST (cfg, ins, handle);
5789 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
5790 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5791 NEW_INDSTORE (cfg, store, addr, ins, &mono_defaults.int_class->byval_arg);
5792 MONO_ADD_INS (bblock, store);
5793 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5809 case CEE_ADD_OVF_UN:
5811 case CEE_MUL_OVF_UN:
5813 case CEE_SUB_OVF_UN:
5816 if (mono_find_jit_opcode_emulation (ins->opcode)) {
5818 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
5819 mono_get_got_var (cfg);
5823 case CEE_ENDFINALLY:
5824 MONO_INST_NEW (cfg, ins, *ip);
5825 MONO_ADD_INS (bblock, ins);
5826 ins->cil_code = ip++;
5827 start_new_bblock = 1;
5830 * Control will leave the method so empty the stack, otherwise
5831 * the next basic block will start with a nonempty stack.
5833 while (sp != stack_start) {
5834 MONO_INST_NEW (cfg, ins, CEE_POP);
5838 MONO_ADD_INS (bblock, ins);
5845 if (*ip == CEE_LEAVE) {
5847 target = ip + 5 + (gint32)read32(ip + 1);
5850 target = ip + 2 + (signed char)(ip [1]);
5853 /* empty the stack */
5854 while (sp != stack_start) {
5855 MONO_INST_NEW (cfg, ins, CEE_POP);
5859 MONO_ADD_INS (bblock, ins);
5863 * If this leave statement is in a catch block, check for a
5864 * pending exception, and rethrow it if necessary.
5866 for (i = 0; i < header->num_clauses; ++i) {
5867 MonoExceptionClause *clause = &header->clauses [i];
5869 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)) {
5873 NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, clause->handler_offset)->inst_c0);
5874 load->cil_code = ip;
5876 temp = mono_emit_jit_icall (cfg, bblock, mono_thread_get_pending_exception, NULL, ip);
5877 NEW_TEMPLOAD (cfg, *sp, temp);
5879 MONO_INST_NEW (cfg, ins, OP_THROW_OR_NULL);
5880 ins->inst_left = *sp;
5881 ins->inst_right = load;
5883 MONO_ADD_INS (bblock, ins);
5887 /* fixme: call fault handler ? */
5889 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
5891 for (tmp = handlers; tmp; tmp = tmp->next) {
5893 link_bblock (cfg, bblock, tblock);
5894 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
5896 ins->inst_target_bb = tblock;
5897 MONO_ADD_INS (bblock, ins);
5899 g_list_free (handlers);
5902 MONO_INST_NEW (cfg, ins, CEE_BR);
5904 MONO_ADD_INS (bblock, ins);
5905 GET_BBLOCK (cfg, bbhash, tblock, target);
5906 link_bblock (cfg, bblock, tblock);
5907 CHECK_BBLOCK (target, ip, tblock);
5908 ins->inst_target_bb = tblock;
5909 start_new_bblock = 1;
5911 if (*ip == CEE_LEAVE)
5920 MONO_INST_NEW (cfg, ins, *ip);
5922 handle_loaded_temps (cfg, bblock, stack_start, sp);
5923 MONO_ADD_INS (bblock, ins);
5924 ins->cil_code = ip++;
5925 ins->inst_i0 = sp [0];
5926 ins->inst_i1 = sp [1];
5934 /* trampoline mono specific opcodes */
5935 case MONO_CUSTOM_PREFIX: {
5937 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
5942 case CEE_MONO_ICALL: {
5945 MonoJitICallInfo *info;
5947 token = read32 (ip + 2);
5948 func = mono_method_get_wrapper_data (method, token);
5949 info = mono_find_jit_icall_by_addr (func);
5952 CHECK_STACK (info->sig->param_count);
5953 sp -= info->sig->param_count;
5955 temp = mono_emit_jit_icall (cfg, bblock, info->func, sp, ip);
5956 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
5957 NEW_TEMPLOAD (cfg, *sp, temp);
5962 inline_costs += 10 * num_calls++;
5966 case CEE_MONO_LDPTR:
5967 CHECK_STACK_OVF (1);
5969 token = read32 (ip + 2);
5970 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
5974 inline_costs += 10 * num_calls++;
5975 /* Can't embed random pointers into AOT code */
5976 cfg->disable_aot = 1;
5978 case CEE_MONO_VTADDR:
5981 MONO_INST_NEW (cfg, ins, OP_VTADDR);
5983 ins->type = STACK_MP;
5984 ins->inst_left = *sp;
5988 case CEE_MONO_NEWOBJ: {
5989 MonoInst *iargs [2];
5991 CHECK_STACK_OVF (1);
5993 token = read32 (ip + 2);
5994 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5995 mono_class_init (klass);
5996 NEW_DOMAINCONST (cfg, iargs [0]);
5997 NEW_CLASSCONST (cfg, iargs [1], klass);
5998 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
5999 NEW_TEMPLOAD (cfg, *sp, temp);
6002 inline_costs += 10 * num_calls++;
6005 case CEE_MONO_OBJADDR:
6008 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
6010 ins->type = STACK_MP;
6011 ins->inst_left = *sp;
6015 case CEE_MONO_LDNATIVEOBJ:
6018 token = read32 (ip + 2);
6019 klass = mono_method_get_wrapper_data (method, token);
6020 g_assert (klass->valuetype);
6021 mono_class_init (klass);
6022 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
6026 case CEE_MONO_RETOBJ:
6027 g_assert (cfg->ret);
6028 g_assert (mono_method_signature (method)->pinvoke);
6033 token = read32 (ip + 2);
6034 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
6036 NEW_RETLOADA (cfg, ins);
6037 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
6039 if (sp != stack_start)
6042 MONO_INST_NEW (cfg, ins, CEE_BR);
6044 ins->inst_target_bb = end_bblock;
6045 MONO_ADD_INS (bblock, ins);
6046 link_bblock (cfg, bblock, end_bblock);
6047 start_new_bblock = 1;
6050 case CEE_MONO_CISINST:
6051 case CEE_MONO_CCASTCLASS: {
6056 token = read32 (ip + 2);
6057 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
6058 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_CISINST) ? OP_CISINST : OP_CCASTCLASS);
6059 ins->type = STACK_I4;
6060 ins->inst_left = *sp;
6061 ins->inst_newa_class = klass;
6063 *sp++ = emit_tree (cfg, bblock, ins, ip + 6);
6067 case CEE_MONO_SAVE_LMF:
6068 case CEE_MONO_RESTORE_LMF:
6069 #ifdef MONO_ARCH_HAVE_LMF_OPS
6070 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_SAVE_LMF) ? OP_SAVE_LMF : OP_RESTORE_LMF);
6071 MONO_ADD_INS (bblock, ins);
6072 cfg->need_lmf_area = TRUE;
6076 case CEE_MONO_CLASSCONST:
6077 CHECK_STACK_OVF (1);
6079 token = read32 (ip + 2);
6080 NEW_CLASSCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
6084 inline_costs += 10 * num_calls++;
6086 case CEE_MONO_NOT_TAKEN:
6087 bblock->out_of_line = TRUE;
6091 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
6100 /* somewhat similar to LDTOKEN */
6101 MonoInst *addr, *vtvar;
6102 CHECK_STACK_OVF (1);
6103 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
6105 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
6106 addr->cil_code = ip;
6107 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
6109 ins->inst_left = addr;
6110 MONO_ADD_INS (bblock, ins);
6111 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
6125 * The following transforms:
6126 * CEE_CEQ into OP_CEQ
6127 * CEE_CGT into OP_CGT
6128 * CEE_CGT_UN into OP_CGT_UN
6129 * CEE_CLT into OP_CLT
6130 * CEE_CLT_UN into OP_CLT_UN
6132 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
6134 MONO_INST_NEW (cfg, ins, cmp->opcode);
6136 cmp->inst_i0 = sp [0];
6137 cmp->inst_i1 = sp [1];
6141 if ((sp [0]->type == STACK_I8) || ((sizeof (gpointer) == 8) && ((sp [0]->type == STACK_PTR) || (sp [0]->type == STACK_OBJ) || (sp [0]->type == STACK_MP))))
6142 cmp->opcode = OP_LCOMPARE;
6144 cmp->opcode = OP_COMPARE;
6146 ins->type = STACK_I4;
6149 /* spill it to reduce the expression complexity
6150 * and workaround bug 54209
6152 if (cmp->inst_left->type == STACK_I8) {
6154 *sp++ = emit_tree (cfg, bblock, ins, ip + 2);
6163 CHECK_STACK_OVF (1);
6165 n = read32 (ip + 2);
6166 if (method->wrapper_type != MONO_WRAPPER_NONE)
6167 cmethod = mono_method_get_wrapper_data (method, n);
6169 cmethod = mini_get_method (image, n, NULL, generic_context);
6172 mono_class_init (cmethod->klass);
6174 if (mono_use_security_manager) {
6175 check_linkdemand (cfg, method, cmethod, bblock, ip);
6178 handle_loaded_temps (cfg, bblock, stack_start, sp);
6180 NEW_METHODCONST (cfg, argconst, cmethod);
6181 if (method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED)
6182 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
6184 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn_nosync, &argconst, ip);
6185 NEW_TEMPLOAD (cfg, *sp, temp);
6189 inline_costs += 10 * num_calls++;
6192 case CEE_LDVIRTFTN: {
6198 n = read32 (ip + 2);
6199 if (method->wrapper_type != MONO_WRAPPER_NONE)
6200 cmethod = mono_method_get_wrapper_data (method, n);
6202 cmethod = mini_get_method (image, n, NULL, generic_context);
6204 mono_class_init (cmethod->klass);
6206 if (mono_use_security_manager) {
6207 check_linkdemand (cfg, method, cmethod, bblock, ip);
6210 handle_loaded_temps (cfg, bblock, stack_start, sp);
6214 NEW_METHODCONST (cfg, args [1], cmethod);
6215 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
6216 NEW_TEMPLOAD (cfg, *sp, temp);
6220 inline_costs += 10 * num_calls++;
6224 CHECK_STACK_OVF (1);
6226 n = read16 (ip + 2);
6228 NEW_ARGLOAD (cfg, ins, n);
6234 CHECK_STACK_OVF (1);
6236 n = read16 (ip + 2);
6238 NEW_ARGLOADA (cfg, ins, n);
6246 handle_loaded_temps (cfg, bblock, stack_start, sp);
6248 n = read16 (ip + 2);
6250 NEW_ARGSTORE (cfg, ins, n, *sp);
6252 if (ins->opcode == CEE_STOBJ) {
6253 NEW_ARGLOADA (cfg, ins, n);
6254 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
6256 MONO_ADD_INS (bblock, ins);
6260 CHECK_STACK_OVF (1);
6262 n = read16 (ip + 2);
6264 NEW_LOCLOAD (cfg, ins, n);
6270 CHECK_STACK_OVF (1);
6272 n = read16 (ip + 2);
6274 NEW_LOCLOADA (cfg, ins, n);
6283 n = read16 (ip + 2);
6285 handle_loaded_temps (cfg, bblock, stack_start, sp);
6286 NEW_LOCSTORE (cfg, ins, n, *sp);
6288 if (ins->opcode == CEE_STOBJ) {
6289 NEW_LOCLOADA (cfg, ins, n);
6290 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
6292 MONO_ADD_INS (bblock, ins);
6299 if (sp != stack_start)
6301 if (cfg->method != method)
6303 * Inlining this into a loop in a parent could lead to
6304 * stack overflows which is different behavior than the
6305 * non-inlined case, thus disable inlining in this case.
6307 goto inline_failure;
6308 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
6309 ins->inst_left = *sp;
6311 ins->type = STACK_MP;
6313 cfg->flags |= MONO_CFG_HAS_ALLOCA;
6314 if (header->init_locals)
6315 ins->flags |= MONO_INST_INIT;
6319 /* FIXME: set init flag if locals init is set in this method */
6321 case CEE_ENDFILTER: {
6322 MonoExceptionClause *clause, *nearest;
6323 int cc, nearest_num;
6327 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
6329 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
6330 ins->inst_left = *sp;
6332 MONO_ADD_INS (bblock, ins);
6333 start_new_bblock = 1;
6338 for (cc = 0; cc < header->num_clauses; ++cc) {
6339 clause = &header->clauses [cc];
6340 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
6341 (!nearest || (clause->data.filter_offset > nearest->data.filter_offset))) {
6347 if ((ip - header->code) != nearest->handler_offset)
6352 case CEE_UNALIGNED_:
6353 ins_flag |= MONO_INST_UNALIGNED;
6354 /* FIXME: record alignment? we can assume 1 for now */
6359 ins_flag |= MONO_INST_VOLATILE;
6363 ins_flag |= MONO_INST_TAILCALL;
6364 cfg->flags |= MONO_CFG_HAS_TAIL;
6365 /* Can't inline tail calls at this time */
6366 inline_costs += 100000;
6373 token = read32 (ip + 2);
6374 if (method->wrapper_type != MONO_WRAPPER_NONE)
6375 klass = mono_method_get_wrapper_data (method, token);
6377 klass = mono_class_get_full (image, token, generic_context);
6378 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
6379 MonoInst *store, *load;
6380 NEW_PCONST (cfg, load, NULL);
6381 load->cil_code = ip;
6382 load->type = STACK_OBJ;
6383 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
6384 store->cil_code = ip;
6385 handle_loaded_temps (cfg, bblock, stack_start, sp);
6386 MONO_ADD_INS (bblock, store);
6387 store->inst_i0 = sp [0];
6388 store->inst_i1 = load;
6390 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
6395 case CEE_CONSTRAINED_:
6396 /* FIXME: implement */
6398 token = read32 (ip + 2);
6399 constrained_call = mono_class_get_full (image, token, generic_context);
6404 MonoInst *iargs [3];
6407 if ((cfg->opt & MONO_OPT_INTRINS) && (ip [1] == CEE_CPBLK) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5)) {
6409 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
6410 copy->inst_left = sp [0];
6411 copy->inst_right = sp [1];
6412 copy->cil_code = ip;
6414 MONO_ADD_INS (bblock, copy);
6421 handle_loaded_temps (cfg, bblock, stack_start, sp);
6422 if (ip [1] == CEE_CPBLK) {
6423 MonoMethod *memcpy_method = get_memcpy_method ();
6424 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
6426 MonoMethod *memset_method = get_memset_method ();
6427 mono_emit_method_call_spilled (cfg, bblock, memset_method, memset_method->signature, iargs, ip, NULL);
6436 ins_flag |= MONO_INST_NOTYPECHECK;
6438 ins_flag |= MONO_INST_NORANGECHECK;
6439 /* we ignore the no-nullcheck for now since we
6440 * really do it explicitly only when doing callvirt->call
6446 int handler_offset = -1;
6448 for (i = 0; i < header->num_clauses; ++i) {
6449 MonoExceptionClause *clause = &header->clauses [i];
6450 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code) && !(clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY))
6451 handler_offset = clause->handler_offset;
6454 g_assert (handler_offset != -1);
6456 NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, handler_offset)->inst_c0);
6457 load->cil_code = ip;
6458 MONO_INST_NEW (cfg, ins, OP_RETHROW);
6459 ins->inst_left = load;
6461 MONO_ADD_INS (bblock, ins);
6463 link_bblock (cfg, bblock, end_bblock);
6464 start_new_bblock = 1;
6466 mono_get_got_var (cfg);
6470 CHECK_STACK_OVF (1);
6472 token = read32 (ip + 2);
6473 /* FIXXME: handle generics. */
6474 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
6475 MonoType *type = mono_type_create_from_typespec (image, token);
6476 token = mono_type_size (type, &align);
6478 MonoClass *szclass = mono_class_get_full (image, token, generic_context);
6479 mono_class_init (szclass);
6480 token = mono_class_value_size (szclass, &align);
6482 NEW_ICONST (cfg, ins, token);
6487 case CEE_REFANYTYPE:
6489 MONO_INST_NEW (cfg, ins, OP_REFANYTYPE);
6491 ins->type = STACK_MP;
6492 ins->inst_left = *sp;
6493 ins->type = STACK_VTYPE;
6494 ins->klass = mono_defaults.typehandle_class;
6503 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
6508 g_error ("opcode 0x%02x not handled", *ip);
6511 if (start_new_bblock != 1)
6514 bblock->cil_length = ip - bblock->cil_code;
6515 bblock->next_bb = end_bblock;
6517 if (cfg->method == method && cfg->domainvar) {
6519 MonoInst *get_domain;
6521 if (! (get_domain = mono_arch_get_domain_intrinsic (cfg))) {
6524 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
6525 call->signature = helper_sig_domain_get;
6526 call->inst.type = STACK_PTR;
6527 call->fptr = mono_domain_get;
6528 get_domain = (MonoInst*)call;
6531 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, get_domain);
6532 MONO_ADD_INS (init_localsbb, store);
6535 if (cfg->method == method && cfg->got_var)
6536 mono_emit_load_got_addr (cfg);
6538 if (header->init_locals) {
6540 for (i = 0; i < header->num_locals; ++i) {
6541 MonoType *ptype = header->locals [i];
6542 int t = ptype->type;
6543 if (t == MONO_TYPE_VALUETYPE && ptype->data.klass->enumtype)
6544 t = ptype->data.klass->enum_basetype->type;
6546 NEW_PCONST (cfg, ins, NULL);
6547 NEW_LOCSTORE (cfg, store, i, ins);
6548 MONO_ADD_INS (init_localsbb, store);
6549 } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
6550 NEW_ICONST (cfg, ins, 0);
6551 NEW_LOCSTORE (cfg, store, i, ins);
6552 MONO_ADD_INS (init_localsbb, store);
6553 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
6554 MONO_INST_NEW (cfg, ins, OP_I8CONST);
6555 ins->type = STACK_I8;
6557 NEW_LOCSTORE (cfg, store, i, ins);
6558 MONO_ADD_INS (init_localsbb, store);
6559 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
6560 MONO_INST_NEW (cfg, ins, OP_R8CONST);
6561 ins->type = STACK_R8;
6562 ins->inst_p0 = (void*)&r8_0;
6563 NEW_LOCSTORE (cfg, store, i, ins);
6564 MONO_ADD_INS (init_localsbb, store);
6565 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
6566 ((t == MONO_TYPE_GENERICINST) && mono_metadata_generic_class_is_valuetype (ptype->data.generic_class))) {
6567 NEW_LOCLOADA (cfg, ins, i);
6568 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (ptype), NULL, NULL);
6571 NEW_PCONST (cfg, ins, NULL);
6572 NEW_LOCSTORE (cfg, store, i, ins);
6573 MONO_ADD_INS (init_localsbb, store);
6578 /* resolve backward branches in the middle of an existing basic block */
6579 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
6581 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
6582 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
6583 if (tblock != start_bblock) {
6585 split_bblock (cfg, tblock, bblock);
6586 l = bblock->cil_code - header->code;
6587 bblock->cil_length = tblock->cil_length - l;
6588 tblock->cil_length = l;
6590 g_print ("recheck failed.\n");
6594 if (cfg->method == method) {
6596 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6597 bb->region = mono_find_block_region (cfg, bb->real_offset);
6599 mono_create_spvar_for_region (cfg, bb->region);
6600 if (cfg->verbose_level > 2)
6601 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
6604 g_hash_table_destroy (bbhash);
6607 g_slist_free (class_inits);
6608 dont_inline = g_list_remove (dont_inline, method);
6609 return inline_costs;
6612 if (cfg->method != method)
6613 g_hash_table_destroy (bbhash);
6614 g_slist_free (class_inits);
6615 dont_inline = g_list_remove (dont_inline, method);
6619 if (cfg->method != method)
6620 g_hash_table_destroy (bbhash);
6621 g_slist_free (class_inits);
6622 g_error ("Invalid IL code at IL%04x in %s: %s\n", (int)(ip - header->code),
6623 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
6624 dont_inline = g_list_remove (dont_inline, method);
6629 mono_print_tree (MonoInst *tree) {
6635 arity = mono_burg_arity [tree->opcode];
6637 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
6639 switch (tree->opcode) {
6641 printf ("[%d]", (int)tree->inst_c0);
6644 printf ("[%lld]", (long long)tree->inst_l);
6647 printf ("[%f]", *(double*)tree->inst_p0);
6650 printf ("[%f]", *(float*)tree->inst_p0);
6654 printf ("[%d]", (int)tree->inst_c0);
6657 if (tree->inst_offset < 0)
6658 printf ("[-0x%x(%s)]", (int)(-tree->inst_offset), mono_arch_regname (tree->inst_basereg));
6660 printf ("[0x%x(%s)]", (int)(tree->inst_offset), mono_arch_regname (tree->inst_basereg));
6663 printf ("[%s]", mono_arch_regname (tree->dreg));
6666 printf ("[%s]", tree->inst_newa_class->name);
6667 mono_print_tree (tree->inst_newa_len);
6678 case OP_VOIDCALLVIRT: {
6679 MonoCallInst *call = (MonoCallInst*)tree;
6681 printf ("[%s]", call->method->name);
6682 else if (call->fptr) {
6683 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (call->fptr);
6685 printf ("[%s]", info->name);
6691 printf ("[%d (", (int)tree->inst_c0);
6692 for (i = 0; i < tree->inst_phi_args [0]; i++) {
6695 printf ("%d", tree->inst_phi_args [i + 1]);
6706 case OP_LOAD_MEMBASE:
6707 case OP_LOADI4_MEMBASE:
6708 case OP_LOADU4_MEMBASE:
6709 case OP_LOADU1_MEMBASE:
6710 case OP_LOADI1_MEMBASE:
6711 case OP_LOADU2_MEMBASE:
6712 case OP_LOADI2_MEMBASE:
6713 printf ("[%s] <- [%s + 0x%x]", mono_arch_regname (tree->dreg), mono_arch_regname (tree->inst_basereg), (int)tree->inst_offset);
6716 case OP_CALL_HANDLER:
6717 printf ("[B%d]", tree->inst_target_bb->block_num);
6727 case OP_VOIDCALL_REG:
6728 mono_print_tree (tree->inst_left);
6740 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
6741 mono_print_tree (tree->inst_left);
6744 if (!mono_arch_print_tree(tree, arity)) {
6746 mono_print_tree (tree->inst_left);
6748 mono_print_tree (tree->inst_right);
6759 mono_print_tree_nl (MonoInst *tree)
6761 mono_print_tree (tree);
6765 #define make_icall_sig mono_create_icall_signature
6768 create_helper_signature (void)
6770 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
6771 helper_sig_newarr = make_icall_sig ("object ptr ptr int32");
6773 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
6774 helper_sig_newarr_specific = make_icall_sig ("object ptr int32");
6776 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
6777 helper_sig_object_new = make_icall_sig ("object ptr ptr");
6779 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
6780 helper_sig_object_new_specific = make_icall_sig ("object ptr");
6782 /* void* mono_method_compile (MonoMethod*) */
6783 helper_sig_compile = make_icall_sig ("ptr ptr");
6785 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
6786 helper_sig_compile_virt = make_icall_sig ("ptr object ptr");
6788 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
6789 helper_sig_ldstr = make_icall_sig ("object ptr ptr int32");
6791 /* MonoDomain *mono_domain_get (void) */
6792 helper_sig_domain_get = make_icall_sig ("ptr");
6794 /* void stelem_ref (MonoArray *, int index, MonoObject *) */
6795 helper_sig_stelem_ref = make_icall_sig ("void ptr int32 object");
6797 /* void stelem_ref_check (MonoArray *, MonoObject *) */
6798 helper_sig_stelem_ref_check = make_icall_sig ("void object object");
6800 /* void *helper_compile_generic_method (MonoObject *, MonoMethod *, MonoGenericContext *) */
6801 helper_sig_compile_generic_method = make_icall_sig ("ptr object ptr ptr");
6803 /* int amethod (int, int) */
6804 helper_sig_int_int_int = make_icall_sig ("int32 int32 int32");
6806 /* long amethod (long, long) */
6807 helper_sig_long_long_long = make_icall_sig ("long long long");
6809 /* object amethod (intptr) */
6810 helper_sig_obj_ptr = make_icall_sig ("object ptr");
6812 helper_sig_obj_ptr_ptr = make_icall_sig ("object ptr ptr");
6814 helper_sig_obj_obj_ptr_ptr = make_icall_sig ("object object ptr ptr");
6816 helper_sig_void_void = make_icall_sig ("void");
6818 /* void amethod (intptr) */
6819 helper_sig_void_ptr = make_icall_sig ("void ptr");
6821 /* void amethod (MonoObject *obj) */
6822 helper_sig_void_obj = make_icall_sig ("void object");
6824 /* void amethod (MonoObject *obj, void *ptr, int i) */
6825 helper_sig_void_obj_ptr_int = make_icall_sig ("void object ptr int");
6827 helper_sig_void_obj_ptr_ptr_obj = make_icall_sig ("void object ptr ptr object");
6829 /* intptr amethod (void) */
6830 helper_sig_ptr_void = make_icall_sig ("ptr");
6832 /* object amethod (void) */
6833 helper_sig_obj_void = make_icall_sig ("object");
6835 /* void amethod (intptr, intptr) */
6836 helper_sig_void_ptr_ptr = make_icall_sig ("void ptr ptr");
6838 /* void amethod (intptr, intptr, intptr) */
6839 helper_sig_void_ptr_ptr_ptr = make_icall_sig ("void ptr ptr ptr");
6841 /* intptr amethod (intptr, intptr) */
6842 helper_sig_ptr_ptr_ptr = make_icall_sig ("ptr ptr ptr");
6844 helper_sig_ptr_ptr_ptr_ptr = make_icall_sig ("ptr ptr ptr ptr");
6846 /* IntPtr amethod (object) */
6847 helper_sig_ptr_obj = make_icall_sig ("ptr object");
6849 /* IntPtr amethod (object, int) */
6850 helper_sig_ptr_obj_int = make_icall_sig ("ptr object int");
6852 /* IntPtr amethod (int) */
6853 helper_sig_ptr_int = make_icall_sig ("ptr int32");
6855 /* long amethod (long, guint32) */
6856 helper_sig_long_long_int = make_icall_sig ("long long int32");
6858 /* ulong amethod (double) */
6859 helper_sig_ulong_double = make_icall_sig ("ulong double");
6861 /* long amethod (double) */
6862 helper_sig_long_double = make_icall_sig ("long double");
6864 /* double amethod (long) */
6865 helper_sig_double_long = make_icall_sig ("double long");
6867 /* double amethod (int) */
6868 helper_sig_double_int = make_icall_sig ("double int32");
6870 /* float amethod (long) */
6871 helper_sig_float_long = make_icall_sig ("float long");
6873 /* double amethod (double, double) */
6874 helper_sig_double_double_double = make_icall_sig ("double double double");
6876 /* uint amethod (double) */
6877 helper_sig_uint_double = make_icall_sig ("uint32 double");
6879 /* int amethod (double) */
6880 helper_sig_int_double = make_icall_sig ("int32 double");
6882 helper_sig_class_init_trampoline = make_icall_sig ("void");
6884 helper_sig_obj_obj_obj_ptr = make_icall_sig ("object object object ptr");
6886 helper_sig_void_obj_obj_ptr = make_icall_sig ("void object object ptr");
6890 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
6893 MonoMethod *wrapper;
6896 if (callinfo->wrapper)
6897 return callinfo->wrapper;
6899 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
6900 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
6901 /* Must be domain neutral since there is only one copy */
6902 code = mono_jit_compile_method_with_opt (wrapper, default_opt | MONO_OPT_SHARED);
6904 if (!callinfo->wrapper) {
6905 callinfo->wrapper = code;
6906 mono_register_jit_icall_wrapper (callinfo, code);
6907 mono_debug_add_icall_wrapper (wrapper, callinfo);
6911 return callinfo->wrapper;
6915 mono_init_trampolines (void)
6917 trampoline_code [MONO_TRAMPOLINE_GENERIC] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_GENERIC);
6918 trampoline_code [MONO_TRAMPOLINE_JUMP] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_JUMP);
6919 trampoline_code [MONO_TRAMPOLINE_CLASS_INIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_CLASS_INIT);
6920 #ifdef MONO_ARCH_HAVE_PIC_AOT
6921 trampoline_code [MONO_TRAMPOLINE_AOT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_AOT);
6924 mono_generic_trampoline_code = trampoline_code [MONO_TRAMPOLINE_GENERIC];
6928 mono_get_trampoline_code (MonoTrampolineType tramp_type)
6930 return trampoline_code [tramp_type];
6934 mono_create_class_init_trampoline (MonoVTable *vtable)
6938 /* previously created trampoline code */
6939 mono_domain_lock (vtable->domain);
6941 g_hash_table_lookup (vtable->domain->class_init_trampoline_hash,
6943 mono_domain_unlock (vtable->domain);
6947 code = mono_arch_create_class_init_trampoline (vtable);
6949 /* store trampoline address */
6950 mono_domain_lock (vtable->domain);
6951 g_hash_table_insert (vtable->domain->class_init_trampoline_hash,
6953 mono_domain_unlock (vtable->domain);
6955 EnterCriticalSection (&jit_mutex);
6956 if (!class_init_hash_addr)
6957 class_init_hash_addr = g_hash_table_new (NULL, NULL);
6958 g_hash_table_insert (class_init_hash_addr, code, vtable);
6959 LeaveCriticalSection (&jit_mutex);
6965 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method,
6966 gboolean add_sync_wrapper)
6971 if (add_sync_wrapper && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
6972 return mono_create_jump_trampoline (domain, mono_marshal_get_synchronized_wrapper (method), FALSE);
6974 code = mono_jit_find_compiled_method (domain, method);
6978 mono_domain_lock (domain);
6979 code = g_hash_table_lookup (domain->jump_trampoline_hash, method);
6980 mono_domain_unlock (domain);
6984 ji = mono_arch_create_jump_trampoline (method);
6987 * mono_delegate_ctor needs to find the method metadata from the
6988 * trampoline address, so we save it here.
6991 mono_jit_info_table_add (mono_get_root_domain (), ji);
6993 mono_domain_lock (domain);
6994 g_hash_table_insert (domain->jump_trampoline_hash, method, ji->code_start);
6995 mono_domain_unlock (domain);
6997 return ji->code_start;
7001 mono_create_jit_trampoline (MonoMethod *method)
7003 MonoDomain *domain = mono_domain_get ();
7006 mono_domain_lock (domain);
7007 tramp = g_hash_table_lookup (domain->jit_trampoline_hash, method);
7008 mono_domain_unlock (domain);
7012 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
7013 return mono_create_jit_trampoline (mono_marshal_get_synchronized_wrapper (method));
7015 tramp = mono_arch_create_jit_trampoline (method);
7017 mono_domain_lock (domain);
7018 g_hash_table_insert (domain->jit_trampoline_hash, method, tramp);
7019 mono_domain_unlock (domain);
7021 mono_jit_stats.method_trampolines++;
7026 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
7028 mono_create_jit_trampoline_from_token (MonoImage *image, guint32 token)
7032 tramp = mono_arch_create_jit_trampoline_from_token (image, token);
7034 mono_jit_stats.method_trampolines++;
7041 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
7045 EnterCriticalSection (&jit_mutex);
7046 if (class_init_hash_addr)
7047 res = g_hash_table_lookup (class_init_hash_addr, addr);
7050 LeaveCriticalSection (&jit_mutex);
7055 mono_dynamic_code_hash_insert (MonoDomain *domain, MonoMethod *method, MonoJitDynamicMethodInfo *ji)
7057 if (!domain->dynamic_code_hash)
7058 domain->dynamic_code_hash = g_hash_table_new (NULL, NULL);
7059 g_hash_table_insert (domain->dynamic_code_hash, method, ji);
7062 static MonoJitDynamicMethodInfo*
7063 mono_dynamic_code_hash_lookup (MonoDomain *domain, MonoMethod *method)
7065 MonoJitDynamicMethodInfo *res;
7067 if (domain->dynamic_code_hash)
7068 res = g_hash_table_lookup (domain->dynamic_code_hash, method);
7081 * mono_allocate_stack_slots:
7083 * Allocate stack slots for all non register allocated variables using a
7084 * linear scan algorithm.
7085 * Returns: an array of stack offsets which the caller should free.
7086 * STACK_SIZE is set to the amount of stack space needed.
7087 * STACK_ALIGN is set to the alignment needed by the locals area.
7090 mono_allocate_stack_slots (MonoCompile *m, guint32 *stack_size, guint32 *stack_align)
7092 int i, slot, offset, size, align;
7096 GList *vars = NULL, *l;
7097 StackSlotInfo *scalar_stack_slots, *vtype_stack_slots, *slot_info;
7101 scalar_stack_slots = g_new0 (StackSlotInfo, MONO_TYPE_PINNED);
7102 vtype_stack_slots = g_new0 (StackSlotInfo, 256);
7105 offsets = g_new (gint32, m->num_varinfo);
7106 for (i = 0; i < m->num_varinfo; ++i)
7109 for (i = m->locals_start; i < m->num_varinfo; i++) {
7110 inst = m->varinfo [i];
7111 vmv = MONO_VARINFO (m, i);
7113 if ((inst->flags & MONO_INST_IS_DEAD) || inst->opcode == OP_REGVAR || inst->opcode == OP_REGOFFSET)
7116 vars = g_list_prepend (vars, vmv);
7119 vars = mono_varlist_sort (m, vars, 0);
7122 for (l = vars; l; l = l->next) {
7124 inst = m->varinfo [vmv->idx];
7126 /* inst->unused indicates native sized value types, this is used by the
7127 * pinvoke wrappers when they call functions returning structures */
7128 if (inst->unused && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
7129 size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
7131 size = mono_type_size (inst->inst_vtype, &align);
7133 t = mono_type_get_underlying_type (inst->inst_vtype);
7135 case MONO_TYPE_VALUETYPE:
7136 for (i = 0; i < nvtypes; ++i)
7137 if (t->data.klass == vtype_stack_slots [i].vtype)
7140 slot_info = &vtype_stack_slots [i];
7142 g_assert (nvtypes < 256);
7143 vtype_stack_slots [nvtypes].vtype = t->data.klass;
7144 slot_info = &vtype_stack_slots [nvtypes];
7149 slot_info = &scalar_stack_slots [t->type];
7153 if (m->comp_done & MONO_COMP_LIVENESS) {
7154 //printf ("START %2d %08x %08x\n", vmv->idx, vmv->range.first_use.abs_pos, vmv->range.last_use.abs_pos);
7156 /* expire old intervals in active */
7157 while (slot_info->active) {
7158 MonoMethodVar *amv = (MonoMethodVar *)slot_info->active->data;
7160 if (amv->range.last_use.abs_pos > vmv->range.first_use.abs_pos)
7163 //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);
7165 slot_info->active = g_list_delete_link (slot_info->active, slot_info->active);
7166 slot_info->slots = g_list_prepend (slot_info->slots, GINT_TO_POINTER (offsets [amv->idx]));
7170 * This also handles the case when the variable is used in an
7171 * exception region, as liveness info is not computed there.
7174 * FIXME: All valuetypes are marked as INDIRECT because of LDADDR
7177 if (! (inst->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
7178 if (slot_info->slots) {
7179 slot = GPOINTER_TO_INT (slot_info->slots->data);
7181 slot_info->slots = g_list_delete_link (slot_info->slots, slot_info->slots);
7184 slot_info->active = mono_varlist_insert_sorted (m, slot_info->active, vmv, TRUE);
7189 static int count = 0;
7193 if (count == atoi (getenv ("COUNT")))
7194 printf ("LAST: %s\n", mono_method_full_name (m->method, TRUE));
7195 if (count > atoi (getenv ("COUNT")))
7198 mono_print_tree_nl (inst);
7202 if (slot == 0xffffff) {
7204 * Allways allocate valuetypes to sizeof (gpointer) to allow more
7205 * efficient copying (and to work around the fact that OP_MEMCPY
7206 * and OP_MEMSET ignores alignment).
7208 if (t->type == MONO_TYPE_VALUETYPE)
7209 align = sizeof (gpointer);
7212 offset += align - 1;
7213 offset &= ~(align - 1);
7216 if (*stack_align == 0)
7217 *stack_align = align;
7220 offsets [vmv->idx] = slot;
7223 for (i = 0; i < MONO_TYPE_PINNED; ++i) {
7224 g_list_free (scalar_stack_slots [i].active);
7225 g_list_free (scalar_stack_slots [i].slots);
7227 for (i = 0; i < nvtypes; ++i) {
7228 g_list_free (vtype_stack_slots [i].active);
7229 g_list_free (vtype_stack_slots [i].slots);
7231 g_free (scalar_stack_slots);
7232 g_free (vtype_stack_slots);
7234 *stack_size = offset;
7239 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func, gboolean no_throw)
7241 MonoJitICallInfo *info;
7243 if (!emul_opcode_map)
7244 emul_opcode_map = g_new0 (MonoJitICallInfo*, OP_LAST + 1);
7246 g_assert (!sig->hasthis);
7247 g_assert (sig->param_count < 3);
7249 info = mono_register_jit_icall (func, name, sig, no_throw);
7251 emul_opcode_map [opcode] = info;
7255 decompose_foreach (MonoInst *tree, gpointer data)
7257 static MonoJitICallInfo *newarr_info = NULL;
7258 static MonoJitICallInfo *newarr_specific_info = NULL;
7259 MonoJitICallInfo *info;
7262 switch (tree->opcode) {
7264 MonoCompile *cfg = data;
7265 MonoInst *iargs [3];
7268 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
7269 g_assert (newarr_info);
7270 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
7271 g_assert (newarr_specific_info);
7274 if (cfg->opt & MONO_OPT_SHARED) {
7275 NEW_DOMAINCONST (cfg, iargs [0]);
7276 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
7277 iargs [2] = tree->inst_newa_len;
7282 MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
7284 NEW_VTABLECONST (cfg, iargs [0], vtable);
7285 iargs [1] = tree->inst_newa_len;
7287 info = newarr_specific_info;
7290 mono_emulate_opcode (cfg, tree, iargs, info);
7292 /* Need to decompose arguments after the the opcode is decomposed */
7293 for (i = 0; i < info->sig->param_count; ++i)
7294 dec_foreach (iargs [i], cfg);
7304 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
7306 switch (mono_burg_arity [tree->opcode]) {
7309 mono_inst_foreach (tree->inst_left, func, data);
7312 mono_inst_foreach (tree->inst_left, func, data);
7313 mono_inst_foreach (tree->inst_right, func, data);
7316 g_assert_not_reached ();
7323 mono_print_bb_code (MonoBasicBlock *bb) {
7325 MonoInst *c = bb->code;
7327 mono_print_tree (c);
7335 print_dfn (MonoCompile *cfg) {
7340 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
7342 for (i = 0; i < cfg->num_bblocks; ++i) {
7343 bb = cfg->bblocks [i];
7344 /*if (bb->cil_code) {
7345 char* code1, *code2;
7346 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
7347 if (bb->last_ins->cil_code)
7348 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
7350 code2 = g_strdup ("");
7352 code1 [strlen (code1) - 1] = 0;
7353 code = g_strdup_printf ("%s -> %s", code1, code2);
7357 code = g_strdup ("\n");
7358 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
7360 MonoInst *c = bb->code;
7362 mono_print_tree (c);
7370 g_print ("\tprev:");
7371 for (j = 0; j < bb->in_count; ++j) {
7372 g_print (" BB%d", bb->in_bb [j]->block_num);
7374 g_print ("\t\tsucc:");
7375 for (j = 0; j < bb->out_count; ++j) {
7376 g_print (" BB%d", bb->out_bb [j]->block_num);
7378 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
7381 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
7384 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
7386 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
7394 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
7398 g_assert (bb->code);
7399 bb->last_ins->next = inst;
7400 bb->last_ins = inst;
7402 bb->last_ins = bb->code = inst;
7407 mono_destroy_compile (MonoCompile *cfg)
7409 //mono_mempool_stats (cfg->mempool);
7410 g_hash_table_destroy (cfg->bb_hash);
7411 mono_free_loop_info (cfg);
7413 mono_regstate_free (cfg->rs);
7415 g_hash_table_destroy (cfg->spvars);
7417 g_hash_table_destroy (cfg->exvars);
7418 mono_mempool_destroy (cfg->mempool);
7419 g_list_free (cfg->ldstr_list);
7421 g_free (cfg->varinfo);
7426 #ifdef HAVE_KW_THREAD
7427 static __thread gpointer mono_lmf_addr MONO_TLS_FAST;
7431 mono_get_jit_tls_key (void)
7433 return mono_jit_tls_id;
7437 mono_get_lmf_tls_offset (void)
7440 MONO_THREAD_VAR_OFFSET(mono_lmf_addr,offset);
7445 mono_get_lmf_addr (void)
7447 #ifdef HAVE_KW_THREAD
7448 return mono_lmf_addr;
7450 MonoJitTlsData *jit_tls;
7452 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
7453 return &jit_tls->lmf;
7455 g_assert_not_reached ();
7460 /* Called by native->managed wrappers */
7462 mono_jit_thread_attach (MonoDomain *domain)
7464 #ifdef HAVE_KW_THREAD
7465 if (!mono_lmf_addr) {
7466 mono_thread_attach (domain);
7469 if (!TlsGetValue (mono_jit_tls_id))
7470 mono_thread_attach (domain);
7475 * mono_thread_abort:
7476 * @obj: exception object
7478 * abort the thread, print exception information and stack trace
7481 mono_thread_abort (MonoObject *obj)
7483 /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
7485 /* handle_remove should be eventually called for this thread, too
7488 mono_thread_exit ();
7492 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
7494 MonoJitTlsData *jit_tls;
7497 jit_tls = TlsGetValue (mono_jit_tls_id);
7501 jit_tls = g_new0 (MonoJitTlsData, 1);
7503 TlsSetValue (mono_jit_tls_id, jit_tls);
7505 jit_tls->abort_func = abort_func;
7506 jit_tls->end_of_stack = stack_start;
7508 lmf = g_new0 (MonoLMF, 1);
7511 jit_tls->lmf = jit_tls->first_lmf = lmf;
7513 #ifdef HAVE_KW_THREAD
7514 mono_lmf_addr = &jit_tls->lmf;
7517 mono_arch_setup_jit_tls_data (jit_tls);
7523 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
7526 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
7527 thread = mono_thread_current ();
7529 thread->jit_data = jit_tls;
7532 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
7535 mono_thread_abort_dummy (MonoObject *obj)
7537 if (mono_thread_attach_aborted_cb)
7538 mono_thread_attach_aborted_cb (obj);
7540 mono_thread_abort (obj);
7544 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
7547 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
7548 thread = mono_thread_current ();
7550 thread->jit_data = jit_tls;
7551 if (mono_profiler_get_events () & MONO_PROFILE_STATISTICAL)
7552 setup_stat_profiler ();
7556 mini_thread_cleanup (MonoThread *thread)
7558 MonoJitTlsData *jit_tls = thread->jit_data;
7561 mono_arch_free_jit_tls_data (jit_tls);
7562 g_free (jit_tls->first_lmf);
7564 thread->jit_data = NULL;
7569 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
7571 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
7575 ji->data.target = target;
7576 ji->next = cfg->patch_info;
7578 cfg->patch_info = ji;
7582 mono_remove_patch_info (MonoCompile *cfg, int ip)
7584 MonoJumpInfo **ji = &cfg->patch_info;
7587 if ((*ji)->ip.i == ip)
7590 ji = &((*ji)->next);
7595 mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors)
7597 unsigned char *ip = patch_info->ip.i + code;
7598 gconstpointer target = NULL;
7600 switch (patch_info->type) {
7601 case MONO_PATCH_INFO_BB:
7602 target = patch_info->data.bb->native_offset + code;
7604 case MONO_PATCH_INFO_ABS:
7605 target = patch_info->data.target;
7607 case MONO_PATCH_INFO_LABEL:
7608 target = patch_info->data.inst->inst_c0 + code;
7610 case MONO_PATCH_INFO_IP:
7613 case MONO_PATCH_INFO_METHOD_REL:
7614 target = code + patch_info->data.offset;
7616 case MONO_PATCH_INFO_INTERNAL_METHOD: {
7617 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
7619 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
7620 g_assert_not_reached ();
7622 target = mono_icall_get_wrapper (mi);
7625 case MONO_PATCH_INFO_METHOD_JUMP: {
7628 /* get the trampoline to the method from the domain */
7629 target = mono_create_jump_trampoline (domain, patch_info->data.method, TRUE);
7630 if (!domain->jump_target_hash)
7631 domain->jump_target_hash = g_hash_table_new (NULL, NULL);
7632 list = g_hash_table_lookup (domain->jump_target_hash, patch_info->data.method);
7633 list = g_slist_prepend (list, ip);
7634 g_hash_table_insert (domain->jump_target_hash, patch_info->data.method, list);
7637 case MONO_PATCH_INFO_METHOD:
7638 if (patch_info->data.method == method) {
7641 /* get the trampoline to the method from the domain */
7642 target = mono_create_jit_trampoline (patch_info->data.method);
7644 case MONO_PATCH_INFO_SWITCH: {
7645 gpointer *jump_table;
7648 if (method && method->dynamic) {
7649 jump_table = mono_code_manager_reserve (mono_dynamic_code_hash_lookup (domain, method)->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
7651 mono_domain_lock (domain);
7652 jump_table = mono_code_manager_reserve (domain->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
7653 mono_domain_unlock (domain);
7656 for (i = 0; i < patch_info->data.table->table_size; i++) {
7657 jump_table [i] = code + GPOINTER_TO_INT (patch_info->data.table->table [i]);
7659 target = jump_table;
7662 case MONO_PATCH_INFO_METHODCONST:
7663 case MONO_PATCH_INFO_CLASS:
7664 case MONO_PATCH_INFO_IMAGE:
7665 case MONO_PATCH_INFO_FIELD:
7666 target = patch_info->data.target;
7668 case MONO_PATCH_INFO_IID:
7669 mono_class_init (patch_info->data.klass);
7670 target = GINT_TO_POINTER ((int)patch_info->data.klass->interface_id);
7672 case MONO_PATCH_INFO_VTABLE:
7673 target = mono_class_vtable (domain, patch_info->data.klass);
7675 case MONO_PATCH_INFO_CLASS_INIT:
7676 target = mono_create_class_init_trampoline (mono_class_vtable (domain, patch_info->data.klass));
7678 case MONO_PATCH_INFO_SFLDA: {
7679 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
7680 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && (method && mono_class_needs_cctor_run (vtable->klass, method)))
7681 /* Done by the generated code */
7685 mono_runtime_class_init (vtable);
7687 target = (char*)vtable->data + patch_info->data.field->offset;
7690 case MONO_PATCH_INFO_R4:
7691 case MONO_PATCH_INFO_R8:
7692 target = patch_info->data.target;
7694 case MONO_PATCH_INFO_EXC_NAME:
7695 target = patch_info->data.name;
7697 case MONO_PATCH_INFO_LDSTR:
7699 mono_ldstr (domain, patch_info->data.token->image,
7700 mono_metadata_token_index (patch_info->data.token->token));
7702 case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
7704 MonoClass *handle_class;
7706 handle = mono_ldtoken (patch_info->data.token->image,
7707 patch_info->data.token->token, &handle_class, NULL);
7708 mono_class_init (handle_class);
7709 mono_class_init (mono_class_from_mono_type (handle));
7712 mono_type_get_object (domain, handle);
7715 case MONO_PATCH_INFO_LDTOKEN: {
7717 MonoClass *handle_class;
7719 handle = mono_ldtoken (patch_info->data.token->image,
7720 patch_info->data.token->token, &handle_class, NULL);
7721 mono_class_init (handle_class);
7726 case MONO_PATCH_INFO_DECLSEC:
7727 target = (mono_metadata_blob_heap (patch_info->data.token->image, patch_info->data.token->token) + 2);
7729 case MONO_PATCH_INFO_BB_OVF:
7730 case MONO_PATCH_INFO_EXC_OVF:
7731 case MONO_PATCH_INFO_GOT_OFFSET:
7732 case MONO_PATCH_INFO_NONE:
7735 g_assert_not_reached ();
7738 return (gpointer)target;
7742 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
7743 MonoJitICallInfo *info;
7745 decompose_foreach (tree, cfg);
7747 switch (mono_burg_arity [tree->opcode]) {
7750 dec_foreach (tree->inst_left, cfg);
7752 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
7753 MonoInst *iargs [2];
7755 iargs [0] = tree->inst_left;
7757 mono_emulate_opcode (cfg, tree, iargs, info);
7763 #ifdef MONO_ARCH_BIGMUL_INTRINS
7764 if (tree->opcode == OP_LMUL
7765 && (cfg->opt & MONO_OPT_INTRINS)
7766 && (tree->inst_left->opcode == CEE_CONV_I8
7767 || tree->inst_left->opcode == CEE_CONV_U8)
7768 && tree->inst_left->inst_left->type == STACK_I4
7769 && (tree->inst_right->opcode == CEE_CONV_I8
7770 || tree->inst_right->opcode == CEE_CONV_U8)
7771 && tree->inst_right->inst_left->type == STACK_I4
7772 && tree->inst_left->opcode == tree->inst_right->opcode) {
7773 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
7774 tree->inst_left = tree->inst_left->inst_left;
7775 tree->inst_right = tree->inst_right->inst_left;
7776 dec_foreach (tree, cfg);
7779 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
7780 MonoInst *iargs [2];
7782 iargs [0] = tree->inst_i0;
7783 iargs [1] = tree->inst_i1;
7785 mono_emulate_opcode (cfg, tree, iargs, info);
7787 dec_foreach (iargs [0], cfg);
7788 dec_foreach (iargs [1], cfg);
7791 dec_foreach (tree->inst_left, cfg);
7792 dec_foreach (tree->inst_right, cfg);
7796 g_assert_not_reached ();
7801 decompose_pass (MonoCompile *cfg) {
7804 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7807 cfg->prev_ins = NULL;
7808 for (tree = cfg->cbb->code; tree; tree = tree->next) {
7809 dec_foreach (tree, cfg);
7810 cfg->prev_ins = tree;
7816 nullify_basic_block (MonoBasicBlock *bb)
7823 bb->code = bb->last_ins = NULL;
7824 bb->cil_code = NULL;
7828 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
7832 for (i = 0; i < bb->out_count; i++) {
7833 MonoBasicBlock *ob = bb->out_bb [i];
7836 if (bb->out_count > 1) {
7837 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
7841 bb->out_bb [i] = repl;
7848 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
7852 for (i = 0; i < bb->in_count; i++) {
7853 MonoBasicBlock *ib = bb->in_bb [i];
7856 if (bb->in_count > 1) {
7857 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
7861 bb->in_bb [i] = repl;
7868 replace_or_add_in_block (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
7870 gboolean found = FALSE;
7873 for (i = 0; i < bb->in_count; i++) {
7874 MonoBasicBlock *ib = bb->in_bb [i];
7877 if (bb->in_count > 1) {
7878 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
7882 bb->in_bb [i] = repl;
7889 MonoBasicBlock **new_in_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (bb->in_count + 1));
7890 for (i = 0; i < bb->in_count; i++) {
7891 new_in_bb [i] = bb->in_bb [i];
7893 new_in_bb [i] = repl;
7895 bb->in_bb = new_in_bb;
7901 replace_out_block_in_code (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl) {
7904 for (inst = bb->code; inst != NULL; inst = inst->next) {
7905 if (inst->opcode == OP_CALL_HANDLER) {
7906 if (inst->inst_target_bb == orig) {
7907 inst->inst_target_bb = repl;
7911 if (bb->last_ins != NULL) {
7912 switch (bb->last_ins->opcode) {
7914 if (bb->last_ins->inst_target_bb == orig) {
7915 bb->last_ins->inst_target_bb = repl;
7920 int n = GPOINTER_TO_INT (bb->last_ins->klass);
7921 for (i = 0; i < n; i++ ) {
7922 if (bb->last_ins->inst_many_bb [i] == orig) {
7923 bb->last_ins->inst_many_bb [i] = repl;
7938 if (bb->last_ins->inst_true_bb == orig) {
7939 bb->last_ins->inst_true_bb = repl;
7941 if (bb->last_ins->inst_false_bb == orig) {
7942 bb->last_ins->inst_false_bb = repl;
7952 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
7956 for (i = 0; i < bb->out_count; i++) {
7957 MonoBasicBlock *ob = bb->out_bb [i];
7958 for (j = 0; j < ob->in_count; j++) {
7959 if (ob->in_bb [j] == orig) {
7960 ob->in_bb [j] = repl;
7968 * Check if a bb is useless (is just made of NOPs and ends with an
7969 * unconditional branch, or nothing).
7970 * If it is so, unlink it from the CFG and nullify it, and return TRUE.
7971 * Otherwise, return FALSE;
7974 remove_block_if_useless (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *previous_bb) {
7975 MonoBasicBlock *target_bb = NULL;
7978 /* Do not touch handlers */
7979 if (bb->region != -1) return FALSE;
7981 for (inst = bb->code; inst != NULL; inst = inst->next) {
7982 switch (inst->opcode) {
7986 target_bb = inst->inst_target_bb;
7993 if (target_bb == NULL) {
7994 if ((bb->out_count == 1) && (bb->out_bb [0] == bb->next_bb)) {
7995 target_bb = bb->next_bb;
7997 /* Do not touch empty BBs that do not "fall through" to their next BB (like the exit BB) */
8002 /* Do not touch BBs following a switch (they are the "default" branch) */
8003 if ((previous_bb->last_ins != NULL) && (previous_bb->last_ins->opcode == CEE_SWITCH)) {
8007 /* Do not touch BBs following the entry BB and jumping to something that is not */
8008 /* thiry "next" bb (the entry BB cannot contain the branch) */
8009 if ((previous_bb == cfg->bb_entry) && (bb->next_bb != target_bb)) {
8013 /* Check that there is a target BB, and that bb is not an empty loop (Bug 75061) */
8014 if ((target_bb != NULL) && (target_bb != bb)) {
8017 if (cfg->verbose_level > 1) {
8018 printf ("remove_block_if_useless %s, removed BB%d\n", mono_method_full_name (cfg->method, TRUE), bb->block_num);
8021 for (i = 0; i < bb->in_count; i++) {
8022 MonoBasicBlock *in_bb = bb->in_bb [i];
8023 replace_out_block (in_bb, bb, target_bb);
8024 replace_out_block_in_code (in_bb, bb, target_bb);
8025 if (bb->in_count == 1) {
8026 replace_in_block (target_bb, bb, in_bb);
8028 replace_or_add_in_block (cfg, target_bb, bb, in_bb);
8032 if ((previous_bb != cfg->bb_entry) &&
8033 (previous_bb->region == bb->region) &&
8034 ((previous_bb->last_ins == NULL) ||
8035 ((previous_bb->last_ins->opcode != CEE_BR) &&
8036 (! (MONO_IS_COND_BRANCH_OP (previous_bb->last_ins))) &&
8037 (previous_bb->last_ins->opcode != CEE_SWITCH)))) {
8038 for (i = 0; i < previous_bb->out_count; i++) {
8039 if (previous_bb->out_bb [i] == target_bb) {
8041 MONO_INST_NEW (cfg, jump, CEE_BR);
8042 MONO_ADD_INS (previous_bb, jump);
8043 jump->cil_code = previous_bb->cil_code;
8044 jump->inst_target_bb = target_bb;
8050 previous_bb->next_bb = bb->next_bb;
8051 nullify_basic_block (bb);
8060 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
8062 bb->out_count = bbn->out_count;
8063 bb->out_bb = bbn->out_bb;
8065 replace_basic_block (bb, bbn, bb);
8069 bb->last_ins->next = bbn->code;
8070 bb->last_ins = bbn->last_ins;
8073 bb->code = bbn->code;
8074 bb->last_ins = bbn->last_ins;
8076 bb->next_bb = bbn->next_bb;
8077 nullify_basic_block (bbn);
8081 move_basic_block_to_end (MonoCompile *cfg, MonoBasicBlock *bb)
8083 MonoBasicBlock *bbn, *next;
8087 /* Find the previous */
8088 for (bbn = cfg->bb_entry; bbn->next_bb && bbn->next_bb != bb; bbn = bbn->next_bb)
8091 bbn->next_bb = bb->next_bb;
8095 for (bbn = cfg->bb_entry; bbn->next_bb; bbn = bbn->next_bb)
8101 if (next && (!bb->last_ins || (bb->last_ins->opcode != OP_NOT_REACHED))) {
8104 MONO_INST_NEW (cfg, ins, CEE_BR);
8105 MONO_ADD_INS (bb, ins);
8106 link_bblock (cfg, bb, next);
8107 ins->inst_target_bb = next;
8112 * Optimizes the branches on the Control Flow Graph
8116 optimize_branches (MonoCompile *cfg)
8118 int i, changed = FALSE;
8119 MonoBasicBlock *bb, *bbn;
8120 guint32 niterations;
8123 * Some crazy loops could cause the code below to go into an infinite
8124 * loop, see bug #53003 for an example. To prevent this, we put an upper
8125 * bound on the number of iterations.
8129 MonoBasicBlock *previous_bb;
8133 /* we skip the entry block (exit is handled specially instead ) */
8134 for (previous_bb = cfg->bb_entry, bb = cfg->bb_entry->next_bb; bb; previous_bb = bb, bb = bb->next_bb) {
8136 /* dont touch code inside exception clauses */
8137 if (bb->region != -1)
8140 if (remove_block_if_useless (cfg, bb, previous_bb)) {
8145 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
8146 if (cfg->verbose_level > 2)
8147 g_print ("nullify block triggered %d\n", bbn->block_num);
8149 bb->next_bb = bbn->next_bb;
8151 for (i = 0; i < bbn->out_count; i++)
8152 replace_in_block (bbn->out_bb [i], bbn, NULL);
8154 nullify_basic_block (bbn);
8158 if (bb->out_count == 1) {
8159 bbn = bb->out_bb [0];
8161 /* conditional branches where true and false targets are the same can be also replaced with CEE_BR */
8162 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins)) {
8164 MONO_INST_NEW (cfg, pop, CEE_POP);
8165 pop->inst_left = bb->last_ins->inst_left->inst_left;
8166 mono_add_ins_to_end (bb, pop);
8167 MONO_INST_NEW (cfg, pop, CEE_POP);
8168 pop->inst_left = bb->last_ins->inst_left->inst_right;
8169 mono_add_ins_to_end (bb, pop);
8170 bb->last_ins->opcode = CEE_BR;
8171 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
8173 if (cfg->verbose_level > 2)
8174 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
8177 if (bb->region == bbn->region && bb->next_bb == bbn) {
8178 /* the block are in sequence anyway ... */
8180 /* branches to the following block can be removed */
8181 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
8182 bb->last_ins->opcode = CEE_NOP;
8184 if (cfg->verbose_level > 2)
8185 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
8188 if (bbn->in_count == 1) {
8190 if (bbn != cfg->bb_exit) {
8191 if (cfg->verbose_level > 2)
8192 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
8193 merge_basic_blocks (bb, bbn);
8197 //mono_print_bb_code (bb);
8202 } while (changed && (niterations > 0));
8209 /* we skip the entry block (exit is handled specially instead ) */
8210 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
8212 /* dont touch code inside exception clauses */
8213 if (bb->region != -1)
8216 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
8217 if (cfg->verbose_level > 2) {
8218 g_print ("nullify block triggered %d\n", bbn->block_num);
8220 bb->next_bb = bbn->next_bb;
8222 for (i = 0; i < bbn->out_count; i++)
8223 replace_in_block (bbn->out_bb [i], bbn, NULL);
8225 nullify_basic_block (bbn);
8231 if (bb->out_count == 1) {
8232 bbn = bb->out_bb [0];
8234 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
8235 bbn = bb->last_ins->inst_target_bb;
8236 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
8237 bbn->code->inst_target_bb->region == bb->region) {
8239 if (cfg->verbose_level > 2)
8240 g_print ("in %s branch to branch triggered %d -> %d -> %d\n", cfg->method->name,
8241 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num);
8243 replace_in_block (bbn, bb, NULL);
8244 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
8245 link_bblock (cfg, bb, bbn->code->inst_target_bb);
8246 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
8251 } else if (bb->out_count == 2) {
8252 if (bb->last_ins && MONO_IS_COND_BRANCH_NOFP (bb->last_ins)) {
8253 int branch_result = mono_eval_cond_branch (bb->last_ins);
8254 MonoBasicBlock *taken_branch_target = NULL, *untaken_branch_target = NULL;
8255 if (branch_result == BRANCH_TAKEN) {
8256 taken_branch_target = bb->last_ins->inst_true_bb;
8257 untaken_branch_target = bb->last_ins->inst_false_bb;
8258 } else if (branch_result == BRANCH_NOT_TAKEN) {
8259 taken_branch_target = bb->last_ins->inst_false_bb;
8260 untaken_branch_target = bb->last_ins->inst_true_bb;
8262 if (taken_branch_target) {
8263 /* if mono_eval_cond_branch () is ever taken to handle
8264 * non-constant values to compare, issue a pop here.
8266 bb->last_ins->opcode = CEE_BR;
8267 bb->last_ins->inst_target_bb = taken_branch_target;
8268 replace_out_block (bb, untaken_branch_target, NULL);
8269 replace_in_block (untaken_branch_target, bb, NULL);
8273 bbn = bb->last_ins->inst_true_bb;
8274 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
8275 bbn->code->inst_target_bb->region == bb->region) {
8276 if (cfg->verbose_level > 2)
8277 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
8278 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
8281 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
8283 replace_in_block (bbn, bb, NULL);
8285 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
8286 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
8288 link_bblock (cfg, bb, bbn->code->inst_target_bb);
8294 bbn = bb->last_ins->inst_false_bb;
8295 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
8296 bbn->code->inst_target_bb->region == bb->region) {
8297 if (cfg->verbose_level > 2)
8298 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
8299 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
8302 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
8304 replace_in_block (bbn, bb, NULL);
8306 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
8307 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
8309 link_bblock (cfg, bb, bbn->code->inst_target_bb);
8316 if (bb->last_ins && MONO_IS_COND_BRANCH_NOFP (bb->last_ins)) {
8317 if (bb->last_ins->inst_false_bb->out_of_line && (bb->region == bb->last_ins->inst_false_bb->region)) {
8318 /* Reverse the branch */
8319 bb->last_ins->opcode = reverse_branch_op (bb->last_ins->opcode);
8320 bbn = bb->last_ins->inst_false_bb;
8321 bb->last_ins->inst_false_bb = bb->last_ins->inst_true_bb;
8322 bb->last_ins->inst_true_bb = bbn;
8324 move_basic_block_to_end (cfg, bb->last_ins->inst_true_bb);
8325 if (cfg->verbose_level > 2)
8326 g_print ("cbranch to throw block triggered %d.\n",
8332 } while (changed && (niterations > 0));
8337 mono_compile_create_vars (MonoCompile *cfg)
8339 MonoMethodSignature *sig;
8340 MonoMethodHeader *header;
8343 header = mono_method_get_header (cfg->method);
8345 sig = mono_method_signature (cfg->method);
8347 if (!MONO_TYPE_IS_VOID (sig->ret)) {
8348 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
8349 cfg->ret->opcode = OP_RETARG;
8350 cfg->ret->inst_vtype = sig->ret;
8351 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
8353 if (cfg->verbose_level > 2)
8354 g_print ("creating vars\n");
8357 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
8359 for (i = 0; i < sig->param_count; ++i) {
8360 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
8361 if (sig->params [i]->byref) {
8362 cfg->disable_ssa = TRUE;
8366 cfg->locals_start = cfg->num_varinfo;
8368 if (cfg->verbose_level > 2)
8369 g_print ("creating locals\n");
8370 for (i = 0; i < header->num_locals; ++i)
8371 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
8372 if (cfg->verbose_level > 2)
8373 g_print ("locals done\n");
8375 #ifdef MONO_ARCH_HAVE_CREATE_VARS
8376 mono_arch_create_vars (cfg);
8381 mono_print_code (MonoCompile *cfg)
8385 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
8386 MonoInst *tree = bb->code;
8391 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
8393 for (; tree; tree = tree->next) {
8394 mono_print_tree (tree);
8399 bb->last_ins->next = NULL;
8403 extern const char * const mono_burg_rule_string [];
8406 emit_state (MonoCompile *cfg, MBState *state, int goal)
8409 int ern = mono_burg_rule (state, goal);
8410 const guint16 *nts = mono_burg_nts [ern];
8413 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
8417 // state->reg1 = state->reg2; /* chain rule */
8419 #ifdef MONO_ARCH_ENABLE_EMIT_STATE_OPT
8422 state->reg1 = mono_regstate_next_int (cfg->rs);
8423 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
8426 state->reg1 = mono_regstate_next_int (cfg->rs);
8427 state->reg2 = mono_regstate_next_int (cfg->rs);
8430 state->reg1 = mono_regstate_next_float (cfg->rs);
8433 #ifdef MONO_ARCH_ENABLE_EMIT_STATE_OPT
8435 * Enabling this might cause bugs to surface in the local register
8436 * allocators on some architectures like x86.
8438 if ((state->tree->ssa_op == MONO_SSA_STORE) && (state->left->tree->opcode == OP_REGVAR)) {
8439 /* Do not optimize away reg-reg moves */
8440 if (! ((state->right->tree->ssa_op == MONO_SSA_LOAD) && (state->right->left->tree->opcode == OP_REGVAR))) {
8441 state->right->reg1 = state->left->tree->dreg;
8450 mono_burg_kids (state, ern, kids);
8452 emit_state (cfg, kids [0], nts [0]);
8454 emit_state (cfg, kids [1], nts [1]);
8456 g_assert (!nts [3]);
8457 emit_state (cfg, kids [2], nts [2]);
8462 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
8463 if ((emit = mono_burg_func [ern]))
8464 emit (state, state->tree, cfg);
8467 #define DEBUG_SELECTION
8470 mini_select_instructions (MonoCompile *cfg)
8474 cfg->state_pool = mono_mempool_new ();
8475 cfg->rs = mono_regstate_new ();
8477 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
8478 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins) &&
8479 bb->next_bb != bb->last_ins->inst_false_bb) {
8481 /* we are careful when inverting, since bugs like #59580
8482 * could show up when dealing with NaNs.
8484 if (MONO_IS_COND_BRANCH_NOFP(bb->last_ins) && bb->next_bb == bb->last_ins->inst_true_bb) {
8485 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
8486 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
8487 bb->last_ins->inst_false_bb = tmp;
8489 bb->last_ins->opcode = reverse_branch_op (bb->last_ins->opcode);
8491 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
8492 inst->opcode = CEE_BR;
8493 inst->inst_target_bb = bb->last_ins->inst_false_bb;
8494 mono_bblock_add_inst (bb, inst);
8499 #ifdef DEBUG_SELECTION
8500 if (cfg->verbose_level >= 4) {
8501 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
8502 MonoInst *tree = bb->code;
8503 g_print ("DUMP BLOCK %d:\n", bb->block_num);
8506 for (; tree; tree = tree->next) {
8507 mono_print_tree (tree);
8514 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
8515 MonoInst *tree = bb->code, *next;
8521 bb->last_ins = NULL;
8524 mono_regstate_reset (cfg->rs);
8526 #ifdef DEBUG_SELECTION
8527 if (cfg->verbose_level >= 3)
8528 g_print ("LABEL BLOCK %d:\n", bb->block_num);
8530 for (; tree; tree = next) {
8532 #ifdef DEBUG_SELECTION
8533 if (cfg->verbose_level >= 3) {
8534 mono_print_tree (tree);
8539 if (!(mbstate = mono_burg_label (tree, cfg))) {
8540 g_warning ("unable to label tree %p", tree);
8541 mono_print_tree (tree);
8543 g_assert_not_reached ();
8545 emit_state (cfg, mbstate, MB_NTERM_stmt);
8547 bb->max_ireg = cfg->rs->next_vireg;
8548 bb->max_freg = cfg->rs->next_vfreg;
8551 bb->last_ins->next = NULL;
8553 mono_mempool_empty (cfg->state_pool);
8555 mono_mempool_destroy (cfg->state_pool);
8559 mono_codegen (MonoCompile *cfg)
8561 MonoJumpInfo *patch_info;
8563 int i, max_epilog_size;
8566 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
8567 cfg->spill_count = 0;
8568 /* we reuse dfn here */
8569 /* bb->dfn = bb_count++; */
8570 mono_arch_local_regalloc (cfg, bb);
8573 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
8574 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
8576 code = mono_arch_emit_prolog (cfg);
8578 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
8579 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
8581 cfg->code_len = code - cfg->native_code;
8582 cfg->prolog_end = cfg->code_len;
8584 mono_debug_open_method (cfg);
8586 /* emit code all basic blocks */
8587 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
8588 bb->native_offset = cfg->code_len;
8589 mono_arch_output_basic_block (cfg, bb);
8591 if (bb == cfg->bb_exit) {
8592 cfg->epilog_begin = cfg->code_len;
8594 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE) {
8595 code = cfg->native_code + cfg->code_len;
8596 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
8597 cfg->code_len = code - cfg->native_code;
8600 mono_arch_emit_epilog (cfg);
8604 mono_arch_emit_exceptions (cfg);
8606 max_epilog_size = 0;
8608 code = cfg->native_code + cfg->code_len;
8610 /* we always allocate code in cfg->domain->code_mp to increase locality */
8611 cfg->code_size = cfg->code_len + max_epilog_size;
8612 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
8614 if (cfg->method->dynamic) {
8615 /* Allocate the code into a separate memory pool so it can be freed */
8616 cfg->dynamic_info = g_new0 (MonoJitDynamicMethodInfo, 1);
8617 cfg->dynamic_info->code_mp = mono_code_manager_new_dynamic ();
8618 mono_domain_lock (cfg->domain);
8619 mono_dynamic_code_hash_insert (cfg->domain, cfg->method, cfg->dynamic_info);
8620 mono_domain_unlock (cfg->domain);
8622 code = mono_code_manager_reserve (cfg->dynamic_info->code_mp, cfg->code_size);
8624 mono_domain_lock (cfg->domain);
8625 code = mono_code_manager_reserve (cfg->domain->code_mp, cfg->code_size);
8626 mono_domain_unlock (cfg->domain);
8629 memcpy (code, cfg->native_code, cfg->code_len);
8630 g_free (cfg->native_code);
8631 cfg->native_code = code;
8632 code = cfg->native_code + cfg->code_len;
8634 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
8635 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
8636 switch (patch_info->type) {
8637 case MONO_PATCH_INFO_ABS: {
8638 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
8640 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
8641 if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) &&
8642 strstr (cfg->method->name, info->name))
8644 * This is an icall wrapper, and this is a call to the
8649 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
8650 patch_info->data.name = info->name;
8654 MonoVTable *vtable = mono_find_class_init_trampoline_by_addr (patch_info->data.target);
8656 patch_info->type = MONO_PATCH_INFO_CLASS_INIT;
8657 patch_info->data.klass = vtable->klass;
8662 case MONO_PATCH_INFO_SWITCH: {
8664 if (cfg->method->dynamic) {
8665 table = mono_code_manager_reserve (cfg->dynamic_info->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
8667 mono_domain_lock (cfg->domain);
8668 table = mono_code_manager_reserve (cfg->domain->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
8669 mono_domain_unlock (cfg->domain);
8672 if (!cfg->compile_aot)
8673 /* In the aot case, the patch already points to the correct location */
8674 patch_info->ip.i = patch_info->ip.label->inst_c0;
8675 for (i = 0; i < patch_info->data.table->table_size; i++) {
8676 table [i] = GINT_TO_POINTER (patch_info->data.table->table [i]->native_offset);
8678 patch_info->data.table->table = (MonoBasicBlock**)table;
8687 if (cfg->verbose_level > 0) {
8688 char* nm = mono_method_full_name (cfg->method, TRUE);
8689 g_print ("Method %s emitted at %p to %p [%s]\n",
8691 cfg->native_code, cfg->native_code + cfg->code_len, cfg->domain->friendly_name);
8695 #ifdef MONO_ARCH_HAVE_SAVE_UNWIND_INFO
8696 mono_arch_save_unwind_info (cfg);
8699 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info, cfg->run_cctors);
8701 if (cfg->method->dynamic) {
8702 mono_code_manager_commit (cfg->dynamic_info->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
8704 mono_domain_lock (cfg->domain);
8705 mono_code_manager_commit (cfg->domain->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
8706 mono_domain_unlock (cfg->domain);
8709 mono_arch_flush_icache (cfg->native_code, cfg->code_len);
8711 mono_debug_close_method (cfg);
8715 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
8720 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
8721 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
8723 if (cp->opcode == OP_ICONST) {
8724 if (cfg->opt & MONO_OPT_CONSPROP) {
8725 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
8729 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
8730 if (cfg->opt & MONO_OPT_COPYPROP) {
8731 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
8737 arity = mono_burg_arity [tree->opcode];
8740 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
8741 if (cfg->opt & MONO_OPT_CFOLD)
8742 mono_constant_fold_inst (tree, NULL);
8743 /* The opcode may have changed */
8744 if (mono_burg_arity [tree->opcode] > 1) {
8745 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
8746 if (cfg->opt & MONO_OPT_CFOLD)
8747 mono_constant_fold_inst (tree, NULL);
8749 mono_constant_fold_inst (tree, NULL);
8755 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
8759 switch (tree->opcode) {
8769 if (tree->ssa_op == MONO_SSA_NOP) {
8770 memset (acp, 0, sizeof (MonoInst *) * acp_size);
8787 case OP_VOIDCALL_REG:
8788 case OP_VOIDCALLVIRT:
8790 MonoCallInst *call = (MonoCallInst *)tree;
8791 MonoMethodSignature *sig = call->signature;
8792 int i, byref = FALSE;
8794 for (i = 0; i < sig->param_count; i++) {
8795 if (sig->params [i]->byref) {
8802 memset (acp, 0, sizeof (MonoInst *) * acp_size);
8810 arity = mono_burg_arity [tree->opcode];
8816 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
8819 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
8820 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
8823 g_assert_not_reached ();
8828 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
8830 MonoInst *tree = bb->code;
8836 for (; tree; tree = tree->next) {
8838 mono_cprop_copy_values (cfg, tree, acp);
8840 mono_cprop_invalidate_values (tree, acp, acp_size);
8842 if (tree->ssa_op == MONO_SSA_STORE &&
8843 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
8844 MonoInst *i1 = tree->inst_i1;
8846 acp [tree->inst_i0->inst_c0] = NULL;
8848 for (i = 0; i < acp_size; i++) {
8849 if (acp [i] && acp [i]->opcode != OP_ICONST &&
8850 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
8855 if (i1->opcode == OP_ICONST) {
8856 acp [tree->inst_i0->inst_c0] = i1;
8857 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
8859 if (i1->ssa_op == MONO_SSA_LOAD &&
8860 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
8861 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
8862 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
8863 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
8868 if (tree->opcode == CEE_BEQ) {
8869 g_assert (tree->inst_i0->opcode == OP_COMPARE);
8870 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
8871 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
8873 tree->opcode = CEE_BR;
8874 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
8875 tree->inst_target_bb = tree->inst_true_bb;
8877 tree->inst_target_bb = tree->inst_false_bb;
8886 mono_local_cprop (MonoCompile *cfg)
8891 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
8893 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
8894 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
8895 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
8900 remove_critical_edges (MonoCompile *cfg) {
8902 MonoBasicBlock *previous_bb;
8904 if (cfg->verbose_level > 3) {
8905 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
8907 printf ("remove_critical_edges %s, BEFORE BB%d (in:", mono_method_full_name (cfg->method, TRUE), bb->block_num);
8908 for (i = 0; i < bb->in_count; i++) {
8909 printf (" %d", bb->in_bb [i]->block_num);
8912 for (i = 0; i < bb->out_count; i++) {
8913 printf (" %d", bb->out_bb [i]->block_num);
8916 if (bb->last_ins != NULL) {
8918 mono_print_tree (bb->last_ins);
8924 for (previous_bb = cfg->bb_entry, bb = previous_bb->next_bb; bb != NULL; previous_bb = previous_bb->next_bb, bb = bb->next_bb) {
8925 if (bb->in_count > 1) {
8927 for (in_bb_index = 0; in_bb_index < bb->in_count; in_bb_index++) {
8928 MonoBasicBlock *in_bb = bb->in_bb [in_bb_index];
8929 if (in_bb->out_count > 1) {
8930 MonoBasicBlock *new_bb = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock));
8931 new_bb->block_num = cfg->num_bblocks++;
8932 // new_bb->real_offset = bb->real_offset;
8933 new_bb->region = bb->region;
8935 /* Do not alter the CFG while altering the BB list */
8936 if (previous_bb->region == bb->region) {
8937 if (previous_bb != cfg->bb_entry) {
8938 /* If previous_bb "followed through" to bb, */
8939 /* keep it linked with a CEE_BR */
8940 if ((previous_bb->last_ins == NULL) ||
8941 ((previous_bb->last_ins->opcode != CEE_BR) &&
8942 (! (MONO_IS_COND_BRANCH_OP (previous_bb->last_ins))) &&
8943 (previous_bb->last_ins->opcode != CEE_SWITCH))) {
8945 /* Make sure previous_bb really falls through bb */
8946 for (i = 0; i < previous_bb->out_count; i++) {
8947 if (previous_bb->out_bb [i] == bb) {
8949 MONO_INST_NEW (cfg, jump, CEE_BR);
8950 MONO_ADD_INS (previous_bb, jump);
8951 jump->cil_code = previous_bb->cil_code;
8952 jump->inst_target_bb = bb;
8958 /* We cannot add any inst to the entry BB, so we must */
8959 /* put a new BB in the middle to hold the CEE_BR */
8961 MonoBasicBlock *new_bb_after_entry = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock));
8962 new_bb_after_entry->block_num = cfg->num_bblocks++;
8963 // new_bb_after_entry->real_offset = bb->real_offset;
8964 new_bb_after_entry->region = bb->region;
8966 MONO_INST_NEW (cfg, jump, CEE_BR);
8967 MONO_ADD_INS (new_bb_after_entry, jump);
8968 jump->cil_code = bb->cil_code;
8969 jump->inst_target_bb = bb;
8971 previous_bb->next_bb = new_bb_after_entry;
8972 previous_bb = new_bb_after_entry;
8974 if (cfg->verbose_level > 2) {
8975 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);
8980 /* Insert new_bb in the BB list */
8981 previous_bb->next_bb = new_bb;
8982 new_bb->next_bb = bb;
8983 previous_bb = new_bb;
8985 /* Setup in_bb and out_bb */
8986 new_bb->in_bb = mono_mempool_alloc ((cfg)->mempool, sizeof (MonoBasicBlock*));
8987 new_bb->in_bb [0] = in_bb;
8988 new_bb->in_count = 1;
8989 new_bb->out_bb = mono_mempool_alloc ((cfg)->mempool, sizeof (MonoBasicBlock*));
8990 new_bb->out_bb [0] = bb;
8991 new_bb->out_count = 1;
8993 /* Relink in_bb and bb to (from) new_bb */
8994 replace_out_block (in_bb, bb, new_bb);
8995 replace_out_block_in_code (in_bb, bb, new_bb);
8996 replace_in_block (bb, in_bb, new_bb);
8998 if (cfg->verbose_level > 2) {
8999 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);
9006 if (cfg->verbose_level > 3) {
9007 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
9009 printf ("remove_critical_edges %s, AFTER BB%d (in:", mono_method_full_name (cfg->method, TRUE), bb->block_num);
9010 for (i = 0; i < bb->in_count; i++) {
9011 printf (" %d", bb->in_bb [i]->block_num);
9014 for (i = 0; i < bb->out_count; i++) {
9015 printf (" %d", bb->out_bb [i]->block_num);
9018 if (bb->last_ins != NULL) {
9020 mono_print_tree (bb->last_ins);
9028 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, gboolean compile_aot, int parts)
9030 MonoMethodHeader *header = mono_method_get_header (method);
9031 guint8 *ip = (guint8 *)header->code;
9034 int dfn = 0, i, code_size_ratio;
9036 mono_jit_stats.methods_compiled++;
9037 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
9038 mono_profiler_method_jit (method);
9040 cfg = g_new0 (MonoCompile, 1);
9041 cfg->method = method;
9042 cfg->mempool = mono_mempool_new ();
9044 cfg->prof_options = mono_profiler_get_events ();
9045 cfg->run_cctors = run_cctors;
9046 cfg->bb_hash = g_hash_table_new (NULL, NULL);
9047 cfg->domain = domain;
9048 cfg->verbose_level = mini_verbose;
9049 cfg->compile_aot = compile_aot;
9050 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
9051 mono_method_get_header (method)->max_stack);
9053 if (cfg->verbose_level > 2)
9054 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
9057 * create MonoInst* which represents arguments and local variables
9059 mono_compile_create_vars (cfg);
9061 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
9062 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
9063 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
9064 mono_destroy_compile (cfg);
9068 mono_jit_stats.basic_blocks += cfg->num_bblocks;
9069 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
9071 if ((cfg->num_varinfo > 2000) && !mono_compile_aot) {
9073 * we disable some optimizations if there are too many variables
9074 * because JIT time may become too expensive. The actual number needs
9075 * to be tweaked and eventually the non-linear algorithms should be fixed.
9077 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
9078 cfg->disable_ssa = TRUE;
9081 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
9083 if (cfg->opt & MONO_OPT_BRANCH)
9084 optimize_branches (cfg);
9086 if (cfg->opt & MONO_OPT_SSAPRE) {
9087 remove_critical_edges (cfg);
9090 /* Depth-first ordering on basic blocks */
9091 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
9093 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
9094 if (cfg->num_bblocks != dfn + 1) {
9097 cfg->num_bblocks = dfn + 1;
9099 if (!header->clauses) {
9100 /* remove unreachable code, because the code in them may be
9101 * inconsistent (access to dead variables for example) */
9102 for (bb = cfg->bb_entry; bb;) {
9103 MonoBasicBlock *bbn = bb->next_bb;
9105 if (bbn && bbn->region == -1 && !bbn->dfn) {
9106 if (cfg->verbose_level > 1)
9107 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
9108 bb->next_bb = bbn->next_bb;
9109 nullify_basic_block (bbn);
9117 if (cfg->opt & MONO_OPT_LOOP) {
9118 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
9119 mono_compute_natural_loops (cfg);
9122 /* after method_to_ir */
9126 //#define DEBUGSSA "logic_run"
9127 #define DEBUGSSA_CLASS "Tests"
9130 if (!header->num_clauses && !cfg->disable_ssa) {
9131 mono_local_cprop (cfg);
9132 mono_ssa_compute (cfg);
9136 /* fixme: add all optimizations which requires SSA */
9137 if (cfg->opt & (MONO_OPT_DEADCE | MONO_OPT_ABCREM | MONO_OPT_SSAPRE)) {
9138 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
9139 mono_local_cprop (cfg);
9140 mono_ssa_compute (cfg);
9142 if (cfg->verbose_level >= 2) {
9149 /* after SSA translation */
9153 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
9154 if (cfg->comp_done & MONO_COMP_SSA) {
9155 mono_ssa_cprop (cfg);
9157 mono_local_cprop (cfg);
9161 if (cfg->comp_done & MONO_COMP_SSA) {
9162 mono_ssa_deadce (cfg);
9164 //mono_ssa_strength_reduction (cfg);
9166 if ((cfg->flags & MONO_CFG_HAS_LDELEMA) && (cfg->opt & MONO_OPT_ABCREM))
9167 mono_perform_abc_removal (cfg);
9169 if (cfg->opt & MONO_OPT_SSAPRE)
9170 mono_perform_ssapre (cfg);
9172 mono_ssa_remove (cfg);
9174 if (cfg->opt & MONO_OPT_BRANCH)
9175 optimize_branches (cfg);
9178 /* after SSA removal */
9182 decompose_pass (cfg);
9187 g_assert (cfg->got_var_allocated);
9190 * Allways allocate the GOT var to a register, because keeping it
9191 * in memory will increase the number of live temporaries in some
9192 * code created by inssel.brg, leading to the well known spills+
9193 * branches problem. Testcase: mcs crash in
9194 * System.MonoCustomAttrs:GetCustomAttributes.
9196 regs = mono_arch_get_global_int_regs (cfg);
9198 cfg->got_var->opcode = OP_REGVAR;
9199 cfg->got_var->dreg = GPOINTER_TO_INT (regs->data);
9200 cfg->used_int_regs |= 1LL << cfg->got_var->dreg;
9205 if (cfg->opt & MONO_OPT_LINEARS) {
9208 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
9209 cfg->comp_done &= ~MONO_COMP_LIVENESS;
9210 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
9211 mono_analyze_liveness (cfg);
9213 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
9214 regs = mono_arch_get_global_int_regs (cfg);
9216 regs = g_list_delete_link (regs, regs);
9217 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
9221 //mono_print_code (cfg);
9225 /* variables are allocated after decompose, since decompose could create temps */
9226 mono_arch_allocate_vars (cfg);
9228 if (cfg->opt & MONO_OPT_CFOLD)
9229 mono_constant_fold (cfg);
9231 mini_select_instructions (cfg);
9234 if (cfg->verbose_level >= 2) {
9235 char *id = mono_method_full_name (cfg->method, FALSE);
9236 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
9240 if (cfg->method->dynamic) {
9241 jinfo = g_malloc0 (sizeof (MonoJitInfo) + (header->num_clauses * sizeof (MonoJitExceptionInfo)));
9243 /* we access cfg->domain->mp */
9244 mono_domain_lock (cfg->domain);
9245 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo) + (header->num_clauses * sizeof (MonoJitExceptionInfo)));
9246 mono_domain_unlock (cfg->domain);
9249 jinfo->method = method;
9250 jinfo->code_start = cfg->native_code;
9251 jinfo->code_size = cfg->code_len;
9252 jinfo->used_regs = cfg->used_int_regs;
9253 jinfo->domain_neutral = (cfg->opt & MONO_OPT_SHARED) != 0;
9254 jinfo->cas_inited = FALSE; /* initialization delayed at the first stalk walk using this method */
9256 if (header->num_clauses) {
9259 jinfo->num_clauses = header->num_clauses;
9261 for (i = 0; i < header->num_clauses; i++) {
9262 MonoExceptionClause *ec = &header->clauses [i];
9263 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
9264 MonoBasicBlock *tblock;
9267 ei->flags = ec->flags;
9269 exvar = mono_find_exvar_for_offset (cfg, ec->handler_offset);
9270 ei->exvar_offset = exvar ? exvar->inst_offset : 0;
9272 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
9273 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->data.filter_offset);
9275 ei->data.filter = cfg->native_code + tblock->native_offset;
9277 ei->data.catch_class = ec->data.catch_class;
9280 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
9282 ei->try_start = cfg->native_code + tblock->native_offset;
9283 g_assert (tblock->native_offset);
9284 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
9286 ei->try_end = cfg->native_code + tblock->native_offset;
9287 g_assert (tblock->native_offset);
9288 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
9290 ei->handler_start = cfg->native_code + tblock->native_offset;
9294 cfg->jit_info = jinfo;
9296 mono_domain_lock (cfg->domain);
9297 mono_jit_info_table_add (cfg->domain, jinfo);
9299 if (cfg->method->dynamic)
9300 mono_dynamic_code_hash_lookup (cfg->domain, cfg->method)->ji = jinfo;
9301 mono_domain_unlock (cfg->domain);
9303 /* collect statistics */
9304 mono_jit_stats.allocated_code_size += cfg->code_len;
9305 code_size_ratio = cfg->code_len;
9306 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
9307 mono_jit_stats.biggest_method_size = code_size_ratio;
9308 mono_jit_stats.biggest_method = method;
9310 code_size_ratio = (code_size_ratio * 100) / mono_method_get_header (method)->code_size;
9311 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
9312 mono_jit_stats.max_code_size_ratio = code_size_ratio;
9313 mono_jit_stats.max_ratio_method = method;
9315 mono_jit_stats.native_code_size += cfg->code_len;
9317 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
9318 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
9320 /* this can only be set if the security manager is active */
9321 if (cfg->exception_type == MONO_EXCEPTION_SECURITY_LINKDEMAND) {
9322 MonoAssembly *assembly = mono_image_get_assembly (method->klass->image);
9323 MonoReflectionAssembly *refass = (MonoReflectionAssembly*) mono_assembly_get_object (domain, assembly);
9324 MonoReflectionMethod *refmet = mono_method_get_object (domain, method, NULL);
9325 MonoSecurityManager* secman = mono_security_manager_get_methods ();
9326 MonoObject *exc = NULL;
9329 args [0] = &cfg->exception_data;
9332 mono_runtime_invoke (secman->linkdemandsecurityexception, NULL, args, &exc);
9334 mono_destroy_compile (cfg);
9337 mono_raise_exception ((MonoException*)exc);
9344 mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain)
9347 GHashTable *jit_code_hash;
9348 gpointer code = NULL;
9354 jit_code_hash = target_domain->jit_code_hash;
9356 method = mono_get_inflated_method (method);
9358 #ifdef MONO_USE_AOT_COMPILER
9359 if (!mono_compile_aot && (opt & MONO_OPT_AOT)) {
9361 MonoDomain *domain = mono_domain_get ();
9363 mono_domain_lock (domain);
9365 mono_class_init (method->klass);
9366 if ((info = mono_aot_get_method (domain, method))) {
9367 g_hash_table_insert (domain->jit_code_hash, method, info);
9368 mono_domain_unlock (domain);
9369 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
9370 return info->code_start;
9373 mono_domain_unlock (domain);
9377 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
9378 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
9380 MonoMethodPInvoke* piinfo = (MonoMethodPInvoke *) method;
9382 if (method->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE)
9383 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);
9385 if (!piinfo->addr) {
9386 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
9387 piinfo->addr = mono_lookup_internal_call (method);
9389 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
9390 mono_lookup_pinvoke_call (method, NULL, NULL);
9392 nm = mono_marshal_get_native_wrapper (method);
9393 return mono_compile_method (nm);
9395 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
9396 //mono_debug_add_wrapper (method, nm);
9397 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
9398 const char *name = method->name;
9401 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
9402 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
9403 MonoJitICallInfo *mi = mono_find_jit_icall_by_name ("mono_delegate_ctor");
9405 return (gpointer)mono_icall_get_wrapper (mi);
9406 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
9407 nm = mono_marshal_get_delegate_invoke (method);
9408 return mono_jit_compile_method (nm);
9409 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
9410 nm = mono_marshal_get_delegate_begin_invoke (method);
9411 return mono_jit_compile_method (nm);
9412 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
9413 nm = mono_marshal_get_delegate_end_invoke (method);
9414 return mono_jit_compile_method (nm);
9420 cfg = mini_method_compile (method, opt, target_domain, TRUE, FALSE, 0);
9422 mono_domain_lock (target_domain);
9424 /* Check if some other thread already did the job. In this case, we can
9425 discard the code this thread generated. */
9427 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
9428 /* We can't use a domain specific method in another domain */
9429 if ((target_domain == mono_domain_get ()) || info->domain_neutral) {
9430 code = info->code_start;
9431 // printf("Discarding code for method %s\n", method->name);
9436 g_hash_table_insert (jit_code_hash, method, cfg->jit_info);
9437 code = cfg->native_code;
9440 mono_destroy_compile (cfg);
9442 if (target_domain->jump_target_hash) {
9443 MonoJumpInfo patch_info;
9445 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
9447 patch_info.next = NULL;
9448 patch_info.ip.i = 0;
9449 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
9450 patch_info.data.method = method;
9451 g_hash_table_remove (target_domain->jump_target_hash, method);
9453 for (tmp = list; tmp; tmp = tmp->next)
9454 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info, TRUE);
9455 g_slist_free (list);
9458 mono_domain_unlock (target_domain);
9460 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
9465 mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt)
9467 /* FIXME: later copy the code from mono */
9468 MonoDomain *target_domain, *domain = mono_domain_get ();
9472 if (opt & MONO_OPT_SHARED)
9473 target_domain = mono_get_root_domain ();
9475 target_domain = domain;
9477 mono_domain_lock (target_domain);
9479 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
9480 /* We can't use a domain specific method in another domain */
9481 if (! ((domain != target_domain) && !info->domain_neutral)) {
9482 mono_domain_unlock (target_domain);
9483 mono_jit_stats.methods_lookups++;
9484 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
9485 return info->code_start;
9489 mono_domain_unlock (target_domain);
9490 p = mono_jit_compile_method_inner (method, target_domain);
9495 mono_jit_compile_method (MonoMethod *method)
9497 return mono_jit_compile_method_with_opt (method, default_opt);
9501 invalidated_delegate_trampoline (char *desc)
9503 g_error ("Unmanaged code called delegate of type %s which was already garbage collected.\n"
9504 "See http://www.go-mono.com/delegate.html for an explanation and ways to fix this.",
9509 * mono_jit_free_method:
9511 * Free all memory allocated by the JIT for METHOD.
9514 mono_jit_free_method (MonoDomain *domain, MonoMethod *method)
9516 MonoJitDynamicMethodInfo *ji;
9517 gboolean destroy = TRUE;
9519 g_assert (method->dynamic);
9521 mono_domain_lock (domain);
9522 ji = mono_dynamic_code_hash_lookup (domain, method);
9523 mono_domain_unlock (domain);
9527 mono_domain_lock (domain);
9528 g_hash_table_remove (domain->dynamic_code_hash, method);
9529 g_hash_table_remove (domain->jit_code_hash, method);
9530 mono_domain_unlock (domain);
9532 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
9533 if (debug_options.keep_delegates && method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
9535 * Instead of freeing the code, change it to call an error routine
9536 * so people can fix their code.
9538 char *type = mono_type_full_name (&method->klass->byval_arg);
9539 char *type_and_method = g_strdup_printf ("%s.%s", type, method->name);
9542 mono_arch_invalidate_method (ji->ji, invalidated_delegate_trampoline, type_and_method);
9548 * This needs to be done before freeing code_mp, since the code address is the
9549 * key in the table, so if we the code_mp first, another thread can grab the
9550 * same code address and replace our entry in the table.
9552 mono_jit_info_table_remove (domain, ji->ji);
9555 mono_code_manager_destroy (ji->code_mp);
9561 mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method)
9563 MonoDomain *target_domain;
9566 if (default_opt & MONO_OPT_SHARED)
9567 target_domain = mono_get_root_domain ();
9569 target_domain = domain;
9571 mono_domain_lock (target_domain);
9573 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
9574 /* We can't use a domain specific method in another domain */
9575 if (! ((domain != target_domain) && !info->domain_neutral)) {
9576 mono_domain_unlock (target_domain);
9577 mono_jit_stats.methods_lookups++;
9578 return info->code_start;
9582 mono_domain_unlock (target_domain);
9588 * mono_jit_runtime_invoke:
9589 * @method: the method to invoke
9590 * @obj: this pointer
9591 * @params: array of parameter values.
9592 * @exc: used to catch exceptions objects
9595 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
9598 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc, void* compiled_method);
9599 void* compiled_method;
9601 if (obj == NULL && !(method->flags & METHOD_ATTRIBUTE_STATIC) && !method->string_ctor && (method->wrapper_type == 0)) {
9602 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
9606 method = mono_get_inflated_method (method);
9607 invoke = mono_marshal_get_runtime_invoke (method);
9608 runtime_invoke = mono_jit_compile_method (invoke);
9610 /* We need this here becuase mono_marshal_get_runtime_invoke can be place
9611 * the helper method in System.Object and not the target class
9613 mono_runtime_class_init (mono_class_vtable (mono_domain_get (), method->klass));
9615 compiled_method = mono_jit_compile_method (method);
9616 return runtime_invoke (obj, params, exc, compiled_method);
9619 #ifdef PLATFORM_WIN32
9620 #define GET_CONTEXT \
9621 struct sigcontext *ctx = (struct sigcontext*)_dummy;
9624 #define GET_CONTEXT \
9625 void *ctx = context;
9626 #elif defined(sun) // Solaris x86
9627 #define GET_CONTEXT \
9628 ucontext_t *uctx = context; \
9629 struct sigcontext *ctx = (struct sigcontext *)&(uctx->uc_mcontext);
9630 #elif defined(__ppc__) || defined (__powerpc__) || defined (__s390__) || defined (MONO_ARCH_USE_SIGACTION)
9631 #define GET_CONTEXT \
9632 void *ctx = context;
9634 #define GET_CONTEXT \
9635 void **_p = (void **)&_dummy; \
9636 struct sigcontext *ctx = (struct sigcontext *)++_p;
9640 #ifdef MONO_ARCH_USE_SIGACTION
9641 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, siginfo_t *info, void *context)
9643 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy)
9647 SIG_HANDLER_SIGNATURE (sigfpe_signal_handler)
9649 MonoException *exc = NULL;
9650 #ifndef MONO_ARCH_USE_SIGACTION
9655 #if defined(MONO_ARCH_HAVE_IS_INT_OVERFLOW)
9656 if (mono_arch_is_int_overflow (ctx, info))
9657 exc = mono_get_exception_arithmetic ();
9659 exc = mono_get_exception_divide_by_zero ();
9661 exc = mono_get_exception_divide_by_zero ();
9664 mono_arch_handle_exception (ctx, exc, FALSE);
9668 SIG_HANDLER_SIGNATURE (sigill_signal_handler)
9672 exc = mono_get_exception_execution_engine ("SIGILL");
9674 mono_arch_handle_exception (ctx, exc, FALSE);
9677 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
9679 #ifndef MONO_ARCH_USE_SIGACTION
9680 #error "Can't use sigaltstack without sigaction"
9686 SIG_HANDLER_SIGNATURE (sigsegv_signal_handler)
9688 MonoException *exc = NULL;
9689 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
9690 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
9694 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
9695 /* Can't allocate memory using Boehm GC on altstack */
9696 if (jit_tls->stack_size &&
9697 ((guint8*)info->si_addr >= (guint8*)jit_tls->end_of_stack - jit_tls->stack_size) &&
9698 ((guint8*)info->si_addr < (guint8*)jit_tls->end_of_stack))
9699 exc = mono_domain_get ()->stack_overflow_ex;
9701 exc = mono_domain_get ()->null_reference_ex;
9704 if (debug_options.abort_on_sigsegv) {
9705 MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx));
9707 fprintf (stderr, "Got SIGSEGV while in unmanaged code, and the 'abort-on-sigsegv' MONO_DEBUG option is set. Aborting...\n");
9708 /* Segfault in unmanaged code */
9713 mono_arch_handle_exception (ctx, exc, FALSE);
9717 SIG_HANDLER_SIGNATURE (sigusr1_signal_handler)
9719 gboolean running_managed;
9727 * This is an async signal, so the code below must not call anything which
9728 * is not async safe. That includes the pthread locking functions. If we
9729 * know that we interrupted managed code, then locking is safe.
9731 ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx));
9732 running_managed = ji != NULL;
9734 exc = mono_thread_request_interruption (running_managed);
9737 mono_arch_handle_exception (ctx, exc, FALSE);
9741 SIG_HANDLER_SIGNATURE (sigprof_signal_handler)
9745 mono_profiler_stat_hit (mono_arch_ip_from_context (ctx), ctx);
9749 SIG_HANDLER_SIGNATURE (sigquit_signal_handler)
9754 exc = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
9756 mono_arch_handle_exception (ctx, exc, FALSE);
9760 SIG_HANDLER_SIGNATURE (sigint_signal_handler)
9765 exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
9767 mono_arch_handle_exception (ctx, exc, FALSE);
9771 SIG_HANDLER_SIGNATURE (sigusr2_signal_handler)
9773 gboolean enabled = mono_trace_is_enabled ();
9775 mono_trace_enable (!enabled);
9778 #ifndef PLATFORM_WIN32
9780 add_signal_handler (int signo, gpointer handler)
9782 struct sigaction sa;
9784 #ifdef MONO_ARCH_USE_SIGACTION
9785 sa.sa_sigaction = handler;
9786 sigemptyset (&sa.sa_mask);
9787 sa.sa_flags = SA_SIGINFO;
9789 sa.sa_handler = handler;
9790 sigemptyset (&sa.sa_mask);
9793 g_assert (sigaction (signo, &sa, NULL) != -1);
9798 mono_runtime_install_handlers (void)
9800 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
9801 struct sigaction sa;
9804 #ifdef PLATFORM_WIN32
9806 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
9807 win32_seh_set_handler(SIGILL, sigill_signal_handler);
9808 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
9809 if (debug_options.handle_sigint)
9810 win32_seh_set_handler(SIGINT, sigint_signal_handler);
9812 #else /* !PLATFORM_WIN32 */
9814 /* libpthreads has its own implementation of sigaction(),
9815 * but it seems to work well with our current exception
9816 * handlers. If not we must call syscall directly instead
9819 if (debug_options.handle_sigint)
9820 add_signal_handler (SIGINT, sigint_signal_handler);
9822 add_signal_handler (SIGFPE, sigfpe_signal_handler);
9823 add_signal_handler (SIGQUIT, sigquit_signal_handler);
9824 add_signal_handler (SIGILL, sigill_signal_handler);
9825 add_signal_handler (SIGBUS, sigsegv_signal_handler);
9826 if (mono_jit_trace_calls != NULL)
9827 add_signal_handler (SIGUSR2, sigusr2_signal_handler);
9829 add_signal_handler (mono_thread_get_abort_signal (), sigusr1_signal_handler);
9832 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
9833 sa.sa_sigaction = sigsegv_signal_handler;
9834 sigemptyset (&sa.sa_mask);
9835 sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
9836 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
9838 add_signal_handler (SIGSEGV, sigsegv_signal_handler);
9841 #endif /* PLATFORM_WIN32 */
9845 #ifdef HAVE_LINUX_RTC_H
9846 #include <linux/rtc.h>
9847 #include <sys/ioctl.h>
9849 static int rtc_fd = -1;
9852 enable_rtc_timer (gboolean enable)
9855 flags = fcntl (rtc_fd, F_GETFL);
9857 perror ("getflags");
9864 if (fcntl (rtc_fd, F_SETFL, flags) == -1) {
9865 perror ("setflags");
9873 setup_stat_profiler (void)
9876 struct itimerval itval;
9877 static int inited = 0;
9878 #ifdef HAVE_LINUX_RTC_H
9879 const char *rtc_freq;
9880 if (!inited && (rtc_freq = g_getenv ("MONO_RTC"))) {
9884 freq = atoi (rtc_freq);
9887 rtc_fd = open ("/dev/rtc", O_RDONLY);
9889 perror ("open /dev/rtc");
9892 add_signal_handler (SIGPROF, sigprof_signal_handler);
9893 if (ioctl (rtc_fd, RTC_IRQP_SET, freq) == -1) {
9894 perror ("set rtc freq");
9897 if (ioctl (rtc_fd, RTC_PIE_ON, 0) == -1) {
9898 perror ("start rtc");
9901 if (fcntl (rtc_fd, F_SETSIG, SIGPROF) == -1) {
9905 if (fcntl (rtc_fd, F_SETOWN, getpid ()) == -1) {
9909 enable_rtc_timer (TRUE);
9916 itval.it_interval.tv_usec = 999;
9917 itval.it_interval.tv_sec = 0;
9918 itval.it_value = itval.it_interval;
9919 setitimer (ITIMER_PROF, &itval, NULL);
9923 add_signal_handler (SIGPROF, sigprof_signal_handler);
9927 /* mono_jit_create_remoting_trampoline:
9928 * @method: pointer to the method info
9930 * Creates a trampoline which calls the remoting functions. This
9931 * is used in the vtable of transparent proxies.
9933 * Returns: a pointer to the newly created code
9936 mono_jit_create_remoting_trampoline (MonoMethod *method, MonoRemotingTarget target)
9939 guint8 *addr = NULL;
9941 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
9942 (mono_method_signature (method)->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
9943 nm = mono_marshal_get_remoting_invoke_for_target (method, target);
9944 addr = mono_compile_method (nm);
9946 addr = mono_compile_method (method);
9952 mini_parse_debug_options (void)
9954 char *options = getenv ("MONO_DEBUG");
9955 gchar **args, **ptr;
9960 args = g_strsplit (options, ",", -1);
9962 for (ptr = args; ptr && *ptr; ptr++) {
9963 const char *arg = *ptr;
9965 if (!strcmp (arg, "handle-sigint"))
9966 debug_options.handle_sigint = TRUE;
9967 else if (!strcmp (arg, "keep-delegates"))
9968 debug_options.keep_delegates = TRUE;
9969 else if (!strcmp (arg, "abort-on-sigsegv"))
9970 debug_options.abort_on_sigsegv = TRUE;
9972 fprintf (stderr, "Invalid option for the MONO_DEBUG env variable: %s\n", arg);
9973 fprintf (stderr, "Available options: 'handle-sigint', 'keep-delegates', 'abort-on-sigsegv'\n");
9980 mini_init (const char *filename)
9984 /* Happens when using the embedding interface */
9985 if (default_opt == 0)
9986 default_opt = mono_parse_default_optimizations (NULL);
9988 InitializeCriticalSection (&jit_mutex);
9990 global_codeman = mono_code_manager_new ();
9991 jit_icall_name_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
9993 mono_arch_cpu_init ();
9995 mono_init_trampolines ();
9997 if (!g_thread_supported ())
9998 g_thread_init (NULL);
10000 if (getenv ("MONO_DEBUG") != NULL)
10001 mini_parse_debug_options ();
10003 MONO_GC_PRE_INIT ();
10005 mono_jit_tls_id = TlsAlloc ();
10006 setup_jit_tls_data ((gpointer)-1, mono_thread_abort);
10010 if (default_opt & MONO_OPT_AOT)
10013 mono_runtime_install_handlers ();
10014 mono_threads_install_cleanup (mini_thread_cleanup);
10016 #define JIT_TRAMPOLINES_WORK
10017 #ifdef JIT_TRAMPOLINES_WORK
10018 mono_install_compile_method (mono_jit_compile_method);
10019 mono_install_free_method (mono_jit_free_method);
10020 mono_install_trampoline (mono_create_jit_trampoline);
10021 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
10023 #define JIT_INVOKE_WORKS
10024 #ifdef JIT_INVOKE_WORKS
10025 mono_install_runtime_invoke (mono_jit_runtime_invoke);
10026 mono_install_handler (mono_arch_get_throw_exception ());
10028 mono_install_stack_walk (mono_jit_walk_stack);
10029 mono_install_init_vtable (mono_aot_init_vtable);
10030 mono_install_get_cached_class_info (mono_aot_get_cached_class_info);
10031 mono_install_jit_info_find_in_aot (mono_aot_find_jit_info);
10033 domain = mono_init_from_assembly (filename, filename);
10034 mono_icall_init ();
10036 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
10037 ves_icall_get_frame_info);
10038 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
10039 ves_icall_get_trace);
10040 mono_add_internal_call ("System.Exception::get_trace",
10041 ves_icall_System_Exception_get_trace);
10042 mono_add_internal_call ("System.Security.SecurityFrame::_GetSecurityFrame",
10043 ves_icall_System_Security_SecurityFrame_GetSecurityFrame);
10044 mono_add_internal_call ("System.Security.SecurityFrame::_GetSecurityStack",
10045 ves_icall_System_Security_SecurityFrame_GetSecurityStack);
10046 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
10047 mono_runtime_install_handlers);
10050 create_helper_signature ();
10052 #define JIT_CALLS_WORK
10053 #ifdef JIT_CALLS_WORK
10054 /* Needs to be called here since register_jit_icall depends on it */
10055 mono_marshal_init ();
10057 mono_arch_register_lowlevel_calls ();
10058 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
10059 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
10060 mono_register_jit_icall (mono_trace_enter_method, "mono_trace_enter_method", NULL, TRUE);
10061 mono_register_jit_icall (mono_trace_leave_method, "mono_trace_leave_method", NULL, TRUE);
10062 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
10063 mono_register_jit_icall (mono_jit_thread_attach, "mono_jit_thread_attach", helper_sig_void_void, TRUE);
10064 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
10066 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
10067 mono_register_jit_icall (mono_arch_get_rethrow_exception (), "mono_arch_rethrow_exception", helper_sig_void_obj, TRUE);
10068 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
10069 helper_sig_void_ptr, TRUE);
10070 #if MONO_ARCH_HAVE_THROW_CORLIB_EXCEPTION
10071 mono_register_jit_icall (mono_arch_get_throw_corlib_exception (), "mono_arch_throw_corlib_exception",
10072 helper_sig_void_ptr, TRUE);
10074 mono_register_jit_icall (mono_thread_get_pending_exception, "mono_thread_get_pending_exception", helper_sig_obj_void, FALSE);
10075 mono_register_jit_icall (mono_thread_interruption_checkpoint, "mono_thread_interruption_checkpoint", helper_sig_void_void, FALSE);
10076 mono_register_jit_icall (mono_thread_force_interruption_checkpoint, "mono_thread_force_interruption_checkpoint", helper_sig_void_void, FALSE);
10077 mono_register_jit_icall (mono_load_remote_field_new, "mono_load_remote_field_new", helper_sig_obj_obj_ptr_ptr, FALSE);
10078 mono_register_jit_icall (mono_store_remote_field_new, "mono_store_remote_field_new", helper_sig_void_obj_ptr_ptr_obj, FALSE);
10081 * NOTE, NOTE, NOTE, NOTE:
10082 * when adding emulation for some opcodes, remember to also add a dummy
10083 * rule to the burg files, because we need the arity information to be correct.
10085 #ifndef MONO_ARCH_NO_EMULATE_LONG_MUL_OPTS
10086 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult, TRUE);
10087 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv, FALSE);
10088 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un, FALSE);
10089 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem, FALSE);
10090 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un, FALSE);
10091 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un, FALSE);
10092 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf, FALSE);
10095 #ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
10096 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl, TRUE);
10097 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr, TRUE);
10098 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un, TRUE);
10101 #ifdef MONO_ARCH_EMULATE_MUL_DIV
10102 mono_register_opcode_emulation (CEE_MUL, "__emul_imul", helper_sig_int_int_int, mono_imul, TRUE);
10103 mono_register_opcode_emulation (CEE_DIV, "__emul_idiv", helper_sig_int_int_int, mono_idiv, TRUE);
10104 mono_register_opcode_emulation (CEE_DIV_UN, "__emul_idiv_un", helper_sig_int_int_int, mono_idiv_un, TRUE);
10105 mono_register_opcode_emulation (CEE_REM, "__emul_irem", helper_sig_int_int_int, mono_irem, TRUE);
10106 mono_register_opcode_emulation (CEE_REM_UN, "__emul_irem_un", helper_sig_int_int_int, mono_irem_un, TRUE);
10107 mono_register_opcode_emulation (CEE_MUL_OVF, "__emul_imul_ovf", helper_sig_int_int_int, mono_imul_ovf, TRUE);
10108 mono_register_opcode_emulation (CEE_MUL_OVF_UN, "__emul_imul_ovf_un", helper_sig_int_int_int, mono_imul_ovf_un, TRUE);
10109 mono_register_opcode_emulation (OP_FDIV, "__emul_fdiv", helper_sig_double_double_double, mono_fdiv, TRUE);
10112 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8, FALSE);
10113 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4, FALSE);
10114 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8, FALSE);
10115 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8, FALSE);
10117 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
10118 mono_register_opcode_emulation (OP_FCONV_TO_I8, "__emul_fconv_to_i8", helper_sig_long_double, mono_fconv_i8, FALSE);
10120 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
10121 mono_register_opcode_emulation (CEE_CONV_R_UN, "__emul_conv_r_un", helper_sig_double_int, mono_conv_to_r8_un, FALSE);
10123 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
10124 mono_register_opcode_emulation (OP_LCONV_TO_R8, "__emul_lconv_to_r8", helper_sig_double_long, mono_lconv_to_r8, FALSE);
10126 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
10127 mono_register_opcode_emulation (OP_LCONV_TO_R4, "__emul_lconv_to_r4", helper_sig_float_long, mono_lconv_to_r4, FALSE);
10129 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
10130 mono_register_opcode_emulation (OP_LCONV_TO_R_UN, "__emul_lconv_to_r8_un", helper_sig_double_long, mono_lconv_to_r8_un, FALSE);
10132 #ifdef MONO_ARCH_EMULATE_FREM
10133 mono_register_opcode_emulation (OP_FREM, "__emul_frem", helper_sig_double_double_double, fmod, FALSE);
10136 #if SIZEOF_VOID_P == 4
10137 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4, TRUE);
10140 /* other jit icalls */
10141 mono_register_jit_icall (mono_delegate_ctor, "mono_delegate_ctor", helper_sig_void_obj_obj_ptr, FALSE);
10142 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
10143 helper_sig_ptr_ptr_ptr, FALSE);
10144 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr_ptr, FALSE);
10145 mono_register_jit_icall (mono_get_special_static_data, "mono_get_special_static_data", helper_sig_ptr_int, FALSE);
10146 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
10147 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
10148 mono_register_jit_icall (helper_stelem_ref_check, "helper_stelem_ref_check", helper_sig_stelem_ref_check, FALSE);
10149 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
10150 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
10151 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
10152 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
10153 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
10154 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
10155 mono_register_jit_icall (mono_ldftn_nosync, "mono_ldftn_nosync", helper_sig_compile, FALSE);
10156 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
10157 mono_register_jit_icall (helper_compile_generic_method, "compile_generic_method", helper_sig_compile_generic_method, FALSE);
10160 #define JIT_RUNTIME_WORKS
10161 #ifdef JIT_RUNTIME_WORKS
10162 mono_install_runtime_cleanup ((MonoDomainFunc)mini_cleanup);
10163 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
10166 mono_thread_attach (domain);
10170 MonoJitStats mono_jit_stats = {0};
10173 print_jit_stats (void)
10175 if (mono_jit_stats.enabled) {
10176 g_print ("Mono Jit statistics\n");
10177 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
10178 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
10179 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
10180 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
10181 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
10182 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
10183 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
10184 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
10185 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
10186 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
10187 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
10188 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
10189 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
10190 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
10191 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
10192 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
10193 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
10194 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
10196 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
10197 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
10198 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
10199 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
10200 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
10202 g_print ("\nGeneric instances: %ld\n", mono_stats.generic_instance_count);
10203 g_print ("Initialized classes: %ld\n", mono_stats.generic_class_count);
10204 g_print ("Inflated methods: %ld / %ld\n", mono_stats.inflated_method_count_2,
10205 mono_stats.inflated_method_count);
10206 g_print ("Inflated types: %ld\n", mono_stats.inflated_type_count);
10207 g_print ("Generics metadata size: %ld\n", mono_stats.generics_metadata_size);
10209 if (mono_use_security_manager) {
10210 g_print ("\nDecl security check : %ld\n", mono_jit_stats.cas_declsec_check);
10211 g_print ("LinkDemand (user) : %ld\n", mono_jit_stats.cas_linkdemand);
10212 g_print ("LinkDemand (icall) : %ld\n", mono_jit_stats.cas_linkdemand_icall);
10213 g_print ("LinkDemand (pinvoke) : %ld\n", mono_jit_stats.cas_linkdemand_pinvoke);
10214 g_print ("LinkDemand (aptc) : %ld\n", mono_jit_stats.cas_linkdemand_aptc);
10215 g_print ("Demand (code gen) : %ld\n", mono_jit_stats.cas_demand_generation);
10221 mini_cleanup (MonoDomain *domain)
10223 #ifdef HAVE_LINUX_RTC_H
10225 enable_rtc_timer (FALSE);
10229 * mono_runtime_cleanup() and mono_domain_finalize () need to
10230 * be called early since they need the execution engine still
10231 * fully working (mono_domain_finalize may invoke managed finalizers
10232 * and mono_runtime_cleanup will wait for other threads to finish).
10234 mono_domain_finalize (domain, 2000);
10236 mono_runtime_cleanup (domain);
10238 mono_profiler_shutdown ();
10240 mono_debug_cleanup ();
10242 mono_icall_cleanup ();
10244 #ifdef PLATFORM_WIN32
10245 win32_seh_cleanup();
10248 mono_domain_free (domain, TRUE);
10250 mono_code_manager_destroy (global_codeman);
10251 g_hash_table_destroy (jit_icall_name_hash);
10252 if (class_init_hash_addr)
10253 g_hash_table_destroy (class_init_hash_addr);
10255 print_jit_stats ();
10259 mono_set_defaults (int verbose_level, guint32 opts)
10261 mini_verbose = verbose_level;
10262 default_opt = opts;
10266 mono_precompile_assembly (MonoAssembly *ass, void *user_data)
10268 GHashTable *assemblies = (GHashTable*)user_data;
10269 MonoImage *image = mono_assembly_get_image (ass);
10270 MonoMethod *method, *invoke;
10273 if (g_hash_table_lookup (assemblies, ass))
10276 g_hash_table_insert (assemblies, ass, ass);
10278 if (mini_verbose > 0)
10279 printf ("PRECOMPILE: %s.\n", mono_image_get_filename (image));
10281 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
10282 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
10283 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
10287 if (mini_verbose > 1) {
10288 char * desc = mono_method_full_name (method, TRUE);
10289 g_print ("Compiling %d %s\n", count, desc);
10292 mono_compile_method (method);
10293 if (strcmp (method->name, "Finalize") == 0) {
10294 invoke = mono_marshal_get_runtime_invoke (method);
10295 mono_compile_method (invoke);
10297 if (method->klass->marshalbyref && mono_method_signature (method)->hasthis) {
10298 invoke = mono_marshal_get_remoting_invoke_with_check (method);
10299 mono_compile_method (invoke);
10303 /* Load and precompile referenced assemblies as well */
10304 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_ASSEMBLYREF); ++i) {
10305 mono_assembly_load_reference (image, i);
10306 if (image->references [i])
10307 mono_precompile_assembly (image->references [i], assemblies);
10311 void mono_precompile_assemblies ()
10313 GHashTable *assemblies = g_hash_table_new (NULL, NULL);
10315 mono_assembly_foreach ((GFunc)mono_precompile_assembly, assemblies);
10317 g_hash_table_destroy (assemblies);