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_long_long_long = NULL;
90 static MonoMethodSignature *helper_sig_long_long_int = NULL;
91 static MonoMethodSignature *helper_sig_newarr = NULL;
92 static MonoMethodSignature *helper_sig_newarr_specific = NULL;
93 static MonoMethodSignature *helper_sig_ldstr = NULL;
94 static MonoMethodSignature *helper_sig_domain_get = NULL;
95 static MonoMethodSignature *helper_sig_object_new = NULL;
96 static MonoMethodSignature *helper_sig_object_new_specific = NULL;
97 static MonoMethodSignature *helper_sig_compile = NULL;
98 static MonoMethodSignature *helper_sig_compile_virt = NULL;
99 static MonoMethodSignature *helper_sig_obj_ptr = NULL;
100 static MonoMethodSignature *helper_sig_obj_ptr_ptr = NULL;
101 static MonoMethodSignature *helper_sig_obj_obj_ptr_ptr = NULL;
102 static MonoMethodSignature *helper_sig_obj_obj_obj_ptr = NULL;
103 static MonoMethodSignature *helper_sig_void_obj_obj_ptr = NULL;
104 static MonoMethodSignature *helper_sig_obj_void = NULL;
105 static MonoMethodSignature *helper_sig_ptr_void = NULL;
106 static MonoMethodSignature *helper_sig_void_void = NULL;
107 static MonoMethodSignature *helper_sig_void_ptr = NULL;
108 static MonoMethodSignature *helper_sig_void_obj = NULL;
109 static MonoMethodSignature *helper_sig_void_obj_ptr_int = NULL;
110 static MonoMethodSignature *helper_sig_void_obj_ptr_ptr_obj = NULL;
111 static MonoMethodSignature *helper_sig_void_ptr_ptr = NULL;
112 static MonoMethodSignature *helper_sig_void_ptr_ptr_ptr = NULL;
113 static MonoMethodSignature *helper_sig_ptr_ptr_ptr = NULL;
114 static MonoMethodSignature *helper_sig_ptr_ptr_ptr_ptr = NULL;
115 static MonoMethodSignature *helper_sig_ptr_obj = NULL;
116 static MonoMethodSignature *helper_sig_ptr_obj_int = NULL;
117 static MonoMethodSignature *helper_sig_ptr_int = NULL;
118 static MonoMethodSignature *helper_sig_ulong_double = NULL;
119 static MonoMethodSignature *helper_sig_long_double = NULL;
120 static MonoMethodSignature *helper_sig_double_long = NULL;
121 static MonoMethodSignature *helper_sig_double_int = NULL;
122 static MonoMethodSignature *helper_sig_float_long = NULL;
123 static MonoMethodSignature *helper_sig_double_double_double = NULL;
124 static MonoMethodSignature *helper_sig_uint_double = NULL;
125 static MonoMethodSignature *helper_sig_int_double = NULL;
126 static MonoMethodSignature *helper_sig_stelem_ref = NULL;
127 static MonoMethodSignature *helper_sig_stelem_ref_check = NULL;
128 static MonoMethodSignature *helper_sig_class_init_trampoline = NULL;
129 static MonoMethodSignature *helper_sig_compile_generic_method = NULL;
131 static guint32 default_opt = MONO_OPT_PEEPHOLE;
133 guint32 mono_jit_tls_id = -1;
134 MonoTraceSpec *mono_jit_trace_calls = NULL;
135 gboolean mono_break_on_exc = FALSE;
137 gboolean mono_compile_aot = FALSE;
139 gboolean mono_use_security_manager = FALSE;
141 static int mini_verbose = 0;
143 static CRITICAL_SECTION jit_mutex;
145 static GHashTable *class_init_hash_addr = NULL;
147 static MonoCodeManager *global_codeman = NULL;
149 static GHashTable *jit_icall_name_hash = NULL;
151 static MonoDebugOptions debug_options;
154 * Address of the trampoline code. This is used by the debugger to check
155 * whether a method is a trampoline.
157 guint8 *mono_generic_trampoline_code = NULL;
159 static guint8* trampoline_code [MONO_TRAMPOLINE_NUM];
162 mono_running_on_valgrind (void)
164 #ifdef HAVE_VALGRIND_MEMCHECK_H
165 if (RUNNING_ON_VALGRIND)
175 G_GNUC_UNUSED static char*
176 get_method_from_ip (void *ip)
182 MonoDomain *domain = mono_domain_get ();
184 ji = mono_jit_info_table_find (domain, ip);
188 method = mono_method_full_name (ji->method, TRUE);
189 source = mono_debug_source_location_from_address (ji->method, (guint32)((guint8*)ip - (guint8*)ji->code_start), NULL, domain);
191 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);
200 G_GNUC_UNUSED static void
201 print_method_from_ip (void *ip)
206 MonoDomain *domain = mono_domain_get ();
208 ji = mono_jit_info_table_find (domain, ip);
210 g_print ("No method at %p\n", ip);
213 method = mono_method_full_name (ji->method, TRUE);
214 source = mono_debug_source_location_from_address (ji->method, (guint32)((guint8*)ip - (guint8*)ji->code_start), NULL, domain);
216 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);
219 g_print ("%s\n", source);
226 mono_print_method_from_ip (void *ip)
228 print_method_from_ip (ip);
232 * mono_method_same_domain:
234 * Determine whenever two compiled methods are in the same domain, thus
235 * the address of the callee can be embedded in the caller.
237 gboolean mono_method_same_domain (MonoJitInfo *caller, MonoJitInfo *callee)
239 if (!caller || !callee)
243 * If the call was made from domain-neutral to domain-specific
244 * code, we can't patch the call site.
246 if (caller->domain_neutral && !callee->domain_neutral)
249 if ((caller->method->klass == mono_defaults.appdomain_class) &&
250 (strstr (caller->method->name, "InvokeInDomain"))) {
251 /* The InvokeInDomain methods change the current appdomain */
259 * mono_global_codeman_reserve:
261 * Allocate code memory from the global code manager.
263 void *mono_global_codeman_reserve (int size)
267 if (!global_codeman) {
268 /* This can happen during startup */
269 global_codeman = mono_code_manager_new ();
270 return mono_code_manager_reserve (global_codeman, size);
273 EnterCriticalSection (&jit_mutex);
274 ptr = mono_code_manager_reserve (global_codeman, size);
275 LeaveCriticalSection (&jit_mutex);
281 mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token)
283 MonoJumpInfoToken *res = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoToken));
290 #define MONO_INIT_VARINFO(vi,id) do { \
291 (vi)->range.first_use.pos.bid = 0xffff; \
297 * Basic blocks have two numeric identifiers:
298 * dfn: Depth First Number
299 * block_num: unique ID assigned at bblock creation
301 #define NEW_BBLOCK(cfg) (mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)))
302 #define ADD_BBLOCK(cfg,bbhash,b) do { \
303 g_hash_table_insert (bbhash, (b)->cil_code, (b)); \
304 (b)->block_num = cfg->num_bblocks++; \
305 (b)->real_offset = real_offset; \
308 #define GET_BBLOCK(cfg,bbhash,tblock,ip) do { \
309 (tblock) = g_hash_table_lookup (bbhash, (ip)); \
311 if ((ip) >= end || (ip) < header->code) goto unverified; \
312 (tblock) = NEW_BBLOCK (cfg); \
313 (tblock)->cil_code = (ip); \
314 ADD_BBLOCK (cfg, (bbhash), (tblock)); \
318 #define CHECK_BBLOCK(target,ip,tblock) do { \
319 if ((target) < (ip) && !(tblock)->code) { \
320 bb_recheck = g_list_prepend (bb_recheck, (tblock)); \
321 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)); \
325 #define NEW_ICONST(cfg,dest,val) do { \
326 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
327 (dest)->opcode = OP_ICONST; \
328 (dest)->inst_c0 = (val); \
329 (dest)->type = STACK_I4; \
332 #define NEW_PCONST(cfg,dest,val) do { \
333 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
334 (dest)->opcode = OP_PCONST; \
335 (dest)->inst_p0 = (val); \
336 (dest)->type = STACK_PTR; \
340 #ifdef MONO_ARCH_NEED_GOT_VAR
342 #define NEW_PATCH_INFO(cfg,dest,el1,el2) do { \
343 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
344 (dest)->opcode = OP_PATCH_INFO; \
345 (dest)->inst_left = (gpointer)(el1); \
346 (dest)->inst_right = (gpointer)(el2); \
349 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do { \
350 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
351 (dest)->opcode = cfg->compile_aot ? OP_GOT_ENTRY : OP_PCONST; \
352 if (cfg->compile_aot) { \
353 MonoInst *group, *got_var, *got_loc; \
354 got_loc = mono_get_got_var (cfg); \
355 NEW_TEMPLOAD ((cfg), got_var, got_loc->inst_c0); \
356 NEW_PATCH_INFO ((cfg), group, cons, patch_type); \
357 (dest)->inst_p0 = got_var; \
358 (dest)->inst_p1 = group; \
360 (dest)->inst_p0 = (cons); \
361 (dest)->inst_i1 = (gpointer)(patch_type); \
363 (dest)->type = STACK_PTR; \
366 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,stack_type) do { \
367 MonoInst *group, *got_var, *got_loc; \
368 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
369 (dest)->opcode = OP_GOT_ENTRY; \
370 got_loc = mono_get_got_var (cfg); \
371 NEW_TEMPLOAD ((cfg), got_var, got_loc->inst_c0); \
372 NEW_PATCH_INFO ((cfg), group, NULL, patch_type); \
373 group->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
374 (dest)->inst_p0 = got_var; \
375 (dest)->inst_p1 = group; \
376 (dest)->type = (stack_type); \
381 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do { \
382 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
383 (dest)->opcode = cfg->compile_aot ? OP_AOTCONST : OP_PCONST; \
384 (dest)->inst_p0 = (cons); \
385 (dest)->inst_i1 = (gpointer)(patch_type); \
386 (dest)->type = STACK_PTR; \
389 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,stack_type) do { \
390 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
391 (dest)->opcode = OP_AOTCONST; \
392 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
393 (dest)->inst_p1 = (gpointer)(patch_type); \
394 (dest)->type = (stack_type); \
399 #define NEW_CLASSCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_CLASS, (val))
401 #define NEW_IMAGECONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_IMAGE, (val))
403 #define NEW_FIELDCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_FIELD, (val))
405 #define NEW_METHODCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHODCONST, (val))
407 #define NEW_VTABLECONST(cfg,dest,vtable) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_VTABLE, cfg->compile_aot ? (gpointer)((vtable)->klass) : (vtable))
409 #define NEW_SFLDACONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_SFLDA, (val))
411 #define NEW_LDSTRCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDSTR, (image), (token), STACK_OBJ)
413 #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)
415 #define NEW_LDTOKENCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDTOKEN, (image), (token), STACK_PTR)
417 #define NEW_DECLSECCONST(cfg,dest,image,entry) do { \
418 if (cfg->compile_aot) { \
419 NEW_AOTCONST_TOKEN (cfg, dest, MONO_PATCH_INFO_DECLSEC, image, (entry).index, STACK_OBJ); \
421 NEW_PCONST (cfg, args [0], (entry).blob); \
425 #define NEW_DOMAINCONST(cfg,dest) do { \
426 if (cfg->opt & MONO_OPT_SHARED) { \
427 /* avoid depending on undefined C behavior in sequence points */ \
428 MonoInst* __domain_var = mono_get_domainvar (cfg); \
429 NEW_TEMPLOAD (cfg, dest, __domain_var->inst_c0); \
431 NEW_PCONST (cfg, dest, (cfg)->domain); \
435 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
437 #define NEW_ARGLOAD(cfg,dest,num) do { \
438 if (arg_array [(num)]->opcode == OP_ICONST) (dest) = arg_array [(num)]; else { \
439 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
440 (dest)->ssa_op = MONO_SSA_LOAD; \
441 (dest)->inst_i0 = arg_array [(num)]; \
442 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
443 type_to_eval_stack_type (param_types [(num)], (dest)); \
444 (dest)->klass = (dest)->inst_i0->klass; \
447 #define NEW_LOCLOAD(cfg,dest,num) do { \
448 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
449 (dest)->ssa_op = MONO_SSA_LOAD; \
450 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
451 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
452 type_to_eval_stack_type (header->locals [(num)], (dest)); \
453 (dest)->klass = (dest)->inst_i0->klass; \
456 #define NEW_LOCLOADA(cfg,dest,num) do { \
457 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
458 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
459 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
460 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
461 (dest)->opcode = OP_LDADDR; \
462 (dest)->type = STACK_MP; \
463 (dest)->klass = (dest)->inst_i0->klass; \
464 if (!MONO_TYPE_ISSTRUCT (header->locals [(num)])) \
465 (cfg)->disable_ssa = TRUE; \
468 #define NEW_RETLOADA(cfg,dest) do { \
469 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
470 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
471 (dest)->inst_i0 = (cfg)->ret; \
472 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
473 (dest)->opcode = cfg->ret_var_is_local ? OP_LDADDR : CEE_LDIND_I; \
474 (dest)->type = STACK_MP; \
475 (dest)->klass = (dest)->inst_i0->klass; \
476 (cfg)->disable_ssa = TRUE; \
479 #define NEW_ARGLOADA(cfg,dest,num) do { \
480 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
481 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
482 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
483 (dest)->inst_i0 = arg_array [(num)]; \
484 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
485 (dest)->opcode = OP_LDADDR; \
486 (dest)->type = STACK_MP; \
487 (dest)->klass = (dest)->inst_i0->klass; \
488 (cfg)->disable_ssa = TRUE; \
491 #define NEW_TEMPLOAD(cfg,dest,num) do { \
492 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
493 (dest)->ssa_op = MONO_SSA_LOAD; \
494 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
495 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
496 type_to_eval_stack_type ((dest)->inst_i0->inst_vtype, (dest)); \
497 (dest)->klass = (dest)->inst_i0->klass; \
500 #define NEW_TEMPLOADA(cfg,dest,num) do { \
501 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
502 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
503 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
504 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
505 (dest)->opcode = OP_LDADDR; \
506 (dest)->type = STACK_MP; \
507 (dest)->klass = (dest)->inst_i0->klass; \
508 if (!MONO_TYPE_ISSTRUCT (cfg->varinfo [(num)]->inst_vtype)) \
509 (cfg)->disable_ssa = TRUE; \
513 #define NEW_INDLOAD(cfg,dest,addr,vtype) do { \
514 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
515 (dest)->inst_left = addr; \
516 (dest)->opcode = mono_type_to_ldind (vtype); \
517 type_to_eval_stack_type (vtype, (dest)); \
518 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
521 #define NEW_INDSTORE(cfg,dest,addr,value,vtype) do { \
522 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
523 (dest)->inst_i0 = addr; \
524 (dest)->opcode = mono_type_to_stind (vtype); \
525 (dest)->inst_i1 = (value); \
526 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
529 #define NEW_TEMPSTORE(cfg,dest,num,inst) do { \
530 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
531 (dest)->ssa_op = MONO_SSA_STORE; \
532 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
533 (dest)->opcode = mono_type_to_stind ((dest)->inst_i0->inst_vtype); \
534 (dest)->inst_i1 = (inst); \
535 (dest)->klass = (dest)->inst_i0->klass; \
538 #define NEW_LOCSTORE(cfg,dest,num,inst) do { \
539 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
540 (dest)->opcode = mono_type_to_stind (header->locals [(num)]); \
541 (dest)->ssa_op = MONO_SSA_STORE; \
542 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
543 (dest)->inst_i1 = (inst); \
544 (dest)->klass = (dest)->inst_i0->klass; \
547 #define NEW_ARGSTORE(cfg,dest,num,inst) do { \
548 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
549 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
550 (dest)->opcode = mono_type_to_stind (param_types [(num)]); \
551 (dest)->ssa_op = MONO_SSA_STORE; \
552 (dest)->inst_i0 = arg_array [(num)]; \
553 (dest)->inst_i1 = (inst); \
554 (dest)->klass = (dest)->inst_i0->klass; \
557 #define NEW_DUMMY_USE(cfg,dest,load) do { \
558 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
559 (dest)->opcode = OP_DUMMY_USE; \
560 (dest)->inst_left = (load); \
563 #define NEW_DUMMY_STORE(cfg,dest,num) do { \
564 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
565 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
566 (dest)->opcode = OP_DUMMY_STORE; \
567 (dest)->klass = (dest)->inst_i0->klass; \
570 #define ADD_BINOP(op) do { \
571 MONO_INST_NEW (cfg, ins, (op)); \
572 ins->cil_code = ip; \
574 ins->inst_i0 = sp [0]; \
575 ins->inst_i1 = sp [1]; \
577 type_from_op (ins); \
581 #define ADD_UNOP(op) do { \
582 MONO_INST_NEW (cfg, ins, (op)); \
583 ins->cil_code = ip; \
585 ins->inst_i0 = sp [0]; \
587 type_from_op (ins); \
591 #define ADD_BINCOND(next_block) do { \
594 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
595 cmp->inst_i0 = sp [0]; \
596 cmp->inst_i1 = sp [1]; \
597 cmp->cil_code = ins->cil_code; \
598 type_from_op (cmp); \
600 ins->inst_i0 = cmp; \
601 MONO_ADD_INS (bblock, ins); \
602 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
603 GET_BBLOCK (cfg, bbhash, tblock, target); \
604 link_bblock (cfg, bblock, tblock); \
605 ins->inst_true_bb = tblock; \
606 CHECK_BBLOCK (target, ip, tblock); \
607 if ((next_block)) { \
608 link_bblock (cfg, bblock, (next_block)); \
609 ins->inst_false_bb = (next_block); \
610 start_new_bblock = 1; \
612 GET_BBLOCK (cfg, bbhash, tblock, ip); \
613 link_bblock (cfg, bblock, tblock); \
614 ins->inst_false_bb = tblock; \
615 start_new_bblock = 2; \
619 /* FIXME: handle float, long ... */
620 #define ADD_UNCOND(istrue) do { \
623 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
624 cmp->inst_i0 = sp [0]; \
625 switch (cmp->inst_i0->type) { \
627 cmp->inst_i1 = zero_int64; break; \
629 cmp->inst_i1 = zero_r8; break; \
632 cmp->inst_i1 = zero_ptr; break; \
634 cmp->inst_i1 = zero_obj; break; \
636 cmp->inst_i1 = zero_int32; \
638 cmp->cil_code = ins->cil_code; \
639 type_from_op (cmp); \
641 ins->inst_i0 = cmp; \
642 ins->opcode = (istrue)? CEE_BNE_UN: CEE_BEQ; \
643 MONO_ADD_INS (bblock, ins); \
644 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
645 GET_BBLOCK (cfg, bbhash, tblock, target); \
646 link_bblock (cfg, bblock, tblock); \
647 ins->inst_true_bb = tblock; \
648 CHECK_BBLOCK (target, ip, tblock); \
649 GET_BBLOCK (cfg, bbhash, tblock, ip); \
650 link_bblock (cfg, bblock, tblock); \
651 ins->inst_false_bb = tblock; \
652 start_new_bblock = 2; \
655 #define NEW_LDELEMA(cfg,dest,sp,k) do { \
656 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
657 (dest)->opcode = CEE_LDELEMA; \
658 (dest)->inst_left = (sp) [0]; \
659 (dest)->inst_right = (sp) [1]; \
660 (dest)->type = STACK_MP; \
661 (dest)->klass = (k); \
662 (cfg)->flags |= MONO_CFG_HAS_LDELEMA; \
665 #define NEW_GROUP(cfg,dest,el1,el2) do { \
666 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
667 (dest)->opcode = OP_GROUP; \
668 (dest)->inst_left = (el1); \
669 (dest)->inst_right = (el2); \
674 compare_bblock (gconstpointer a, gconstpointer b)
676 const MonoBasicBlock *b1 = a;
677 const MonoBasicBlock *b2 = b;
679 return b2->cil_code - b1->cil_code;
684 * link_bblock: Links two basic blocks
686 * links two basic blocks in the control flow graph, the 'from'
687 * argument is the starting block and the 'to' argument is the block
688 * the control flow ends to after 'from'.
691 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
693 MonoBasicBlock **newa;
697 if (from->cil_code) {
699 g_print ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
701 g_print ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
704 g_print ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
706 g_print ("edge from entry to exit\n");
710 for (i = 0; i < from->out_count; ++i) {
711 if (to == from->out_bb [i]) {
717 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
718 for (i = 0; i < from->out_count; ++i) {
719 newa [i] = from->out_bb [i];
727 for (i = 0; i < to->in_count; ++i) {
728 if (from == to->in_bb [i]) {
734 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
735 for (i = 0; i < to->in_count; ++i) {
736 newa [i] = to->in_bb [i];
745 * mono_find_block_region:
747 * We mark each basic block with a region ID. We use that to avoid BB
748 * optimizations when blocks are in different regions.
751 * A region token that encodes where this region is, and information
752 * about the clause owner for this block.
754 * The region encodes the try/catch/filter clause that owns this block
755 * as well as the type. -1 is a special value that represents a block
756 * that is in none of try/catch/filter.
759 mono_find_block_region (MonoCompile *cfg, int offset)
761 MonoMethod *method = cfg->method;
762 MonoMethodHeader *header = mono_method_get_header (method);
763 MonoExceptionClause *clause;
766 /* first search for handlers and filters */
767 for (i = 0; i < header->num_clauses; ++i) {
768 clause = &header->clauses [i];
769 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->data.filter_offset) &&
770 (offset < (clause->handler_offset)))
771 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
773 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
774 if (clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)
775 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
777 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
781 /* search the try blocks */
782 for (i = 0; i < header->num_clauses; ++i) {
783 clause = &header->clauses [i];
784 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
785 return ((i + 1) << 8) | clause->flags;
792 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
794 MonoMethod *method = cfg->method;
795 MonoMethodHeader *header = mono_method_get_header (method);
796 MonoExceptionClause *clause;
797 MonoBasicBlock *handler;
801 for (i = 0; i < header->num_clauses; ++i) {
802 clause = &header->clauses [i];
803 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
804 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
805 if (clause->flags == type) {
806 handler = g_hash_table_lookup (cfg->bb_hash, header->code + clause->handler_offset);
808 res = g_list_append (res, handler);
816 mono_find_spvar_for_region (MonoCompile *cfg, int region)
818 return g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
822 mono_create_spvar_for_region (MonoCompile *cfg, int region)
826 var = g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
830 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
831 /* prevent it from being register allocated */
832 var->flags |= MONO_INST_INDIRECT;
834 g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
838 mono_find_exvar_for_offset (MonoCompile *cfg, int offset)
840 return g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
844 mono_create_exvar_for_offset (MonoCompile *cfg, int offset)
848 var = g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
852 var = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
853 /* prevent it from being register allocated */
854 var->flags |= MONO_INST_INDIRECT;
856 g_hash_table_insert (cfg->exvars, GINT_TO_POINTER (offset), var);
862 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
866 array [*dfn] = start;
867 /*g_print ("visit %d at %p\n", *dfn, start->cil_code);*/
868 for (i = 0; i < start->out_count; ++i) {
869 if (start->out_bb [i]->dfn)
872 start->out_bb [i]->dfn = *dfn;
873 start->out_bb [i]->df_parent = start;
874 array [*dfn] = start->out_bb [i];
875 df_visit (start->out_bb [i], dfn, array);
881 MonoBasicBlock *best;
885 previous_foreach (gconstpointer key, gpointer val, gpointer data)
887 PrevStruct *p = data;
888 MonoBasicBlock *bb = val;
889 //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,
890 //bb->method == p->best->method, bb->cil_code < p->code, bb->cil_code > p->best->cil_code);
892 if (bb->cil_code && bb->cil_code < p->code && bb->cil_code > p->best->cil_code)
896 static MonoBasicBlock*
897 find_previous (GHashTable *bb_hash, MonoBasicBlock *start, const guchar *code) {
903 g_hash_table_foreach (bb_hash, (GHFunc)previous_foreach, &p);
908 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
917 * FIXME: take into account all the details:
918 * second may have been the target of more than one bblock
920 second->out_count = first->out_count;
921 second->out_bb = first->out_bb;
923 for (i = 0; i < first->out_count; ++i) {
924 bb = first->out_bb [i];
925 for (j = 0; j < bb->in_count; ++j) {
926 if (bb->in_bb [j] == first)
927 bb->in_bb [j] = second;
931 first->out_count = 0;
932 first->out_bb = NULL;
933 link_bblock (cfg, first, second);
935 second->last_ins = first->last_ins;
937 /*g_print ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
938 for (inst = first->code; inst && inst->next; inst = inst->next) {
939 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
940 g_print ("found %p: %s", inst->next->cil_code, code);
942 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
943 second->code = inst->next;
945 first->last_ins = inst;
946 second->next_bb = first->next_bb;
947 first->next_bb = second;
952 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
958 reverse_branch_op (guint32 opcode)
960 static const int reverse_map [] = {
961 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
962 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
964 static const int reverse_fmap [] = {
965 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
966 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
968 static const int reverse_lmap [] = {
969 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
970 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
972 static const int reverse_imap [] = {
973 OP_IBNE_UN, OP_IBLT, OP_IBLE, OP_IBGT, OP_IBGE,
974 OP_IBEQ, OP_IBLT_UN, OP_IBLE_UN, OP_IBGT_UN, OP_IBGE_UN
977 if (opcode >= CEE_BEQ && opcode <= CEE_BLT_UN) {
978 opcode = reverse_map [opcode - CEE_BEQ];
979 } else if (opcode >= OP_FBEQ && opcode <= OP_FBLT_UN) {
980 opcode = reverse_fmap [opcode - OP_FBEQ];
981 } else if (opcode >= OP_LBEQ && opcode <= OP_LBLT_UN) {
982 opcode = reverse_lmap [opcode - OP_LBEQ];
983 } else if (opcode >= OP_IBEQ && opcode <= OP_IBLT_UN) {
984 opcode = reverse_imap [opcode - OP_IBEQ];
986 g_assert_not_reached ();
992 mono_type_to_ldind (MonoType *type)
998 switch (type->type) {
1000 return CEE_LDIND_I1;
1002 case MONO_TYPE_BOOLEAN:
1003 return CEE_LDIND_U1;
1005 return CEE_LDIND_I2;
1007 case MONO_TYPE_CHAR:
1008 return CEE_LDIND_U2;
1010 return CEE_LDIND_I4;
1012 return CEE_LDIND_U4;
1016 case MONO_TYPE_FNPTR:
1018 case MONO_TYPE_CLASS:
1019 case MONO_TYPE_STRING:
1020 case MONO_TYPE_OBJECT:
1021 case MONO_TYPE_SZARRAY:
1022 case MONO_TYPE_ARRAY:
1023 return CEE_LDIND_REF;
1026 return CEE_LDIND_I8;
1028 return CEE_LDIND_R4;
1030 return CEE_LDIND_R8;
1031 case MONO_TYPE_VALUETYPE:
1032 if (type->data.klass->enumtype) {
1033 type = type->data.klass->enum_basetype;
1037 case MONO_TYPE_TYPEDBYREF:
1039 case MONO_TYPE_GENERICINST:
1040 type = &type->data.generic_class->container_class->byval_arg;
1043 g_error ("unknown type 0x%02x in type_to_ldind", type->type);
1049 mono_type_to_stind (MonoType *type)
1055 switch (type->type) {
1058 case MONO_TYPE_BOOLEAN:
1059 return CEE_STIND_I1;
1062 case MONO_TYPE_CHAR:
1063 return CEE_STIND_I2;
1066 return CEE_STIND_I4;
1070 case MONO_TYPE_FNPTR:
1072 case MONO_TYPE_CLASS:
1073 case MONO_TYPE_STRING:
1074 case MONO_TYPE_OBJECT:
1075 case MONO_TYPE_SZARRAY:
1076 case MONO_TYPE_ARRAY:
1077 return CEE_STIND_REF;
1080 return CEE_STIND_I8;
1082 return CEE_STIND_R4;
1084 return CEE_STIND_R8;
1085 case MONO_TYPE_VALUETYPE:
1086 if (type->data.klass->enumtype) {
1087 type = type->data.klass->enum_basetype;
1091 case MONO_TYPE_TYPEDBYREF:
1093 case MONO_TYPE_GENERICINST:
1094 type = &type->data.generic_class->container_class->byval_arg;
1097 g_error ("unknown type 0x%02x in type_to_stind", type->type);
1103 * Returns the type used in the eval stack when @type is loaded.
1104 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
1107 type_to_eval_stack_type (MonoType *type, MonoInst *inst)
1112 inst->type = STACK_MP;
1116 klass = mono_class_from_mono_type (type);
1119 switch (type->type) {
1120 case MONO_TYPE_VOID:
1121 inst->type = STACK_INV;
1125 case MONO_TYPE_BOOLEAN:
1128 case MONO_TYPE_CHAR:
1131 inst->type = STACK_I4;
1136 case MONO_TYPE_FNPTR:
1137 inst->type = STACK_PTR;
1139 case MONO_TYPE_CLASS:
1140 case MONO_TYPE_STRING:
1141 case MONO_TYPE_OBJECT:
1142 case MONO_TYPE_SZARRAY:
1143 case MONO_TYPE_ARRAY:
1144 inst->type = STACK_OBJ;
1148 inst->type = STACK_I8;
1152 inst->type = STACK_R8;
1154 case MONO_TYPE_VALUETYPE:
1155 if (type->data.klass->enumtype) {
1156 type = type->data.klass->enum_basetype;
1159 inst->klass = klass;
1160 inst->type = STACK_VTYPE;
1163 case MONO_TYPE_TYPEDBYREF:
1164 inst->klass = mono_defaults.typed_reference_class;
1165 inst->type = STACK_VTYPE;
1167 case MONO_TYPE_GENERICINST:
1168 type = &type->data.generic_class->container_class->byval_arg;
1171 g_error ("unknown type 0x%02x in eval stack type", type->type);
1176 * The following tables are used to quickly validate the IL code in type_from_op ().
1179 bin_num_table [STACK_MAX] [STACK_MAX] = {
1180 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1181 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1182 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1183 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1184 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
1185 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
1186 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1187 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1192 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
1195 /* reduce the size of this table */
1197 bin_int_table [STACK_MAX] [STACK_MAX] = {
1198 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1199 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1200 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1201 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1202 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, 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}
1209 bin_comp_table [STACK_MAX] [STACK_MAX] = {
1211 {0, 1, 0, 1, 0, 0, 4, 0},
1212 {0, 0, 1, 0, 0, 0, 0, 0},
1213 {0, 1, 0, 1, 0, 2, 4, 0},
1214 {0, 0, 0, 0, 1, 0, 0, 0},
1215 {0, 0, 0, 2, 0, 1, 0, 0},
1216 {0, 4, 0, 4, 0, 0, 3, 0},
1217 {0, 0, 0, 0, 0, 0, 0, 0},
1220 /* reduce the size of this table */
1222 shift_table [STACK_MAX] [STACK_MAX] = {
1223 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1224 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1225 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1226 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1227 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, 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}
1234 * Tables to map from the non-specific opcode to the matching
1235 * type-specific opcode.
1237 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
1238 static const guint16
1239 binops_op_map [STACK_MAX] = {
1240 0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, OP_PADD-CEE_ADD
1243 /* handles from CEE_NEG to CEE_CONV_U8 */
1244 static const guint16
1245 unops_op_map [STACK_MAX] = {
1246 0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, OP_PNEG-CEE_NEG
1249 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
1250 static const guint16
1251 ovfops_op_map [STACK_MAX] = {
1252 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
1255 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
1256 static const guint16
1257 ovf2ops_op_map [STACK_MAX] = {
1258 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
1261 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
1262 static const guint16
1263 ovf3ops_op_map [STACK_MAX] = {
1264 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
1267 /* handles from CEE_CEQ to CEE_CLT_UN */
1268 static const guint16
1269 ceqops_op_map [STACK_MAX] = {
1270 0, 0, OP_LCEQ-CEE_CEQ, OP_PCEQ-CEE_CEQ, OP_FCEQ-CEE_CEQ, OP_LCEQ-CEE_CEQ
1274 * Sets ins->type (the type on the eval stack) according to the
1275 * type of the opcode and the arguments to it.
1276 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1278 * FIXME: this function sets ins->type unconditionally in some cases, but
1279 * it should set it to invalid for some types (a conv.x on an object)
1282 type_from_op (MonoInst *ins) {
1283 switch (ins->opcode) {
1290 /* FIXME: check unverifiable args for STACK_MP */
1291 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1292 ins->opcode += binops_op_map [ins->type];
1299 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1300 ins->opcode += binops_op_map [ins->type];
1305 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1306 ins->opcode += binops_op_map [ins->type];
1310 /* FIXME: handle some specifics with ins->next->type */
1311 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1312 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))))
1313 ins->opcode = OP_LCOMPARE;
1320 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1321 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1325 ins->type = neg_table [ins->inst_i0->type];
1326 ins->opcode += unops_op_map [ins->type];
1329 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1330 ins->type = ins->inst_i0->type;
1332 ins->type = STACK_INV;
1333 ins->opcode += unops_op_map [ins->type];
1339 ins->type = STACK_I4;
1340 ins->opcode += unops_op_map [ins->inst_i0->type];
1343 ins->type = STACK_R8;
1344 switch (ins->inst_i0->type) {
1349 ins->opcode = OP_LCONV_TO_R_UN;
1353 case CEE_CONV_OVF_I1:
1354 case CEE_CONV_OVF_U1:
1355 case CEE_CONV_OVF_I2:
1356 case CEE_CONV_OVF_U2:
1357 case CEE_CONV_OVF_I4:
1358 case CEE_CONV_OVF_U4:
1359 ins->type = STACK_I4;
1360 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1362 case CEE_CONV_OVF_I_UN:
1363 case CEE_CONV_OVF_U_UN:
1364 ins->type = STACK_PTR;
1365 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1367 case CEE_CONV_OVF_I1_UN:
1368 case CEE_CONV_OVF_I2_UN:
1369 case CEE_CONV_OVF_I4_UN:
1370 case CEE_CONV_OVF_U1_UN:
1371 case CEE_CONV_OVF_U2_UN:
1372 case CEE_CONV_OVF_U4_UN:
1373 ins->type = STACK_I4;
1374 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1377 ins->type = STACK_PTR;
1378 switch (ins->inst_i0->type) {
1384 ins->opcode = OP_LCONV_TO_U;
1387 ins->opcode = OP_FCONV_TO_U;
1393 ins->type = STACK_I8;
1394 ins->opcode += unops_op_map [ins->inst_i0->type];
1396 case CEE_CONV_OVF_I8:
1397 case CEE_CONV_OVF_U8:
1398 ins->type = STACK_I8;
1399 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1401 case CEE_CONV_OVF_U8_UN:
1402 case CEE_CONV_OVF_I8_UN:
1403 ins->type = STACK_I8;
1404 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1408 ins->type = STACK_R8;
1409 ins->opcode += unops_op_map [ins->inst_i0->type];
1412 ins->type = STACK_R8;
1416 ins->type = STACK_I4;
1417 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1420 case CEE_CONV_OVF_I:
1421 case CEE_CONV_OVF_U:
1422 ins->type = STACK_PTR;
1423 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1426 case CEE_ADD_OVF_UN:
1428 case CEE_MUL_OVF_UN:
1430 case CEE_SUB_OVF_UN:
1431 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1432 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1435 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1442 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_MP, STACK_R8, STACK_R8, STACK_OBJ
1445 /* map ldelem.x to the matching ldind.x opcode */
1447 ldelem_to_ldind [] = {
1461 /* map stelem.x to the matching stind.x opcode */
1463 stelem_to_stind [] = {
1477 param_table [STACK_MAX] [STACK_MAX] = {
1482 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1486 switch (args->type) {
1496 for (i = 0; i < sig->param_count; ++i) {
1497 switch (args [i].type) {
1501 if (!sig->params [i]->byref)
1505 if (sig->params [i]->byref)
1507 switch (sig->params [i]->type) {
1508 case MONO_TYPE_CLASS:
1509 case MONO_TYPE_STRING:
1510 case MONO_TYPE_OBJECT:
1511 case MONO_TYPE_SZARRAY:
1512 case MONO_TYPE_ARRAY:
1519 if (sig->params [i]->byref)
1521 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1530 /*if (!param_table [args [i].type] [sig->params [i]->type])
1538 * When we need a pointer to the current domain many times in a method, we
1539 * call mono_domain_get() once and we store the result in a local variable.
1540 * This function returns the variable that represents the MonoDomain*.
1542 inline static MonoInst *
1543 mono_get_domainvar (MonoCompile *cfg)
1545 if (!cfg->domainvar)
1546 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1547 return cfg->domainvar;
1551 * The got_var contains the address of the Global Offset Table when AOT
1554 inline static MonoInst *
1555 mono_get_got_var (MonoCompile *cfg)
1557 #ifdef MONO_ARCH_NEED_GOT_VAR
1558 if (!cfg->compile_aot)
1560 if (!cfg->got_var) {
1561 cfg->got_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1563 return cfg->got_var;
1570 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
1573 int num = cfg->num_varinfo;
1575 if ((num + 1) >= cfg->varinfo_count) {
1576 cfg->varinfo_count = (cfg->varinfo_count + 2) * 2;
1577 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
1578 cfg->vars = (MonoMethodVar **)g_realloc (cfg->vars, sizeof (MonoMethodVar*) * cfg->varinfo_count);
1581 /*g_print ("created temp %d of type 0x%x\n", num, type->type);*/
1582 mono_jit_stats.allocate_var++;
1584 MONO_INST_NEW (cfg, inst, opcode);
1585 inst->inst_c0 = num;
1586 inst->inst_vtype = type;
1587 inst->klass = mono_class_from_mono_type (type);
1588 /* if set to 1 the variable is native */
1591 cfg->varinfo [num] = inst;
1593 cfg->vars [num] = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMethodVar));
1594 MONO_INIT_VARINFO (cfg->vars [num], num);
1597 //g_print ("created temp %d of type %s\n", num, mono_type_get_name (type));
1602 * Transform a MonoInst into a load from the variable of index var_index.
1605 mono_compile_make_var_load (MonoCompile *cfg, MonoInst *dest, gssize var_index) {
1606 memset (dest, 0, sizeof (MonoInst));
1607 dest->ssa_op = MONO_SSA_LOAD;
1608 dest->inst_i0 = cfg->varinfo [var_index];
1609 dest->opcode = mono_type_to_ldind (dest->inst_i0->inst_vtype);
1610 type_to_eval_stack_type (dest->inst_i0->inst_vtype, dest);
1611 dest->klass = dest->inst_i0->klass;
1615 * Create a MonoInst that is a load from the variable of index var_index.
1618 mono_compile_create_var_load (MonoCompile *cfg, gssize var_index) {
1620 NEW_TEMPLOAD (cfg,dest,var_index);
1625 * Create a MonoInst that is a store of the given value into the variable of index var_index.
1628 mono_compile_create_var_store (MonoCompile *cfg, gssize var_index, MonoInst *value) {
1630 NEW_TEMPSTORE (cfg, dest, var_index, value);
1635 type_from_stack_type (MonoInst *ins) {
1636 switch (ins->type) {
1637 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1638 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1639 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1640 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1641 case STACK_MP: return &mono_defaults.int_class->byval_arg;
1642 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1643 case STACK_VTYPE: return &ins->klass->byval_arg;
1645 g_error ("stack type %d to montype not handled\n", ins->type);
1651 mono_type_from_stack_type (MonoInst *ins) {
1652 return type_from_stack_type (ins);
1656 array_access_to_klass (int opcode)
1660 return mono_defaults.byte_class;
1662 return mono_defaults.uint16_class;
1665 return mono_defaults.int_class;
1668 return mono_defaults.sbyte_class;
1671 return mono_defaults.int16_class;
1674 return mono_defaults.int32_class;
1676 return mono_defaults.uint32_class;
1679 return mono_defaults.int64_class;
1682 return mono_defaults.single_class;
1685 return mono_defaults.double_class;
1686 case CEE_LDELEM_REF:
1687 case CEE_STELEM_REF:
1688 return mono_defaults.object_class;
1690 g_assert_not_reached ();
1696 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
1700 MONO_ADD_INS (bb, inst);
1703 switch (bb->last_ins->opcode) {
1717 while (prev->next && prev->next != bb->last_ins)
1719 if (prev == bb->code) {
1720 if (bb->last_ins == bb->code) {
1721 inst->next = bb->code;
1724 inst->next = prev->next;
1728 inst->next = bb->last_ins;
1732 // g_warning ("handle conditional jump in add_ins_to_end ()\n");
1734 MONO_ADD_INS (bb, inst);
1740 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
1742 MonoInst *inst, *load;
1744 NEW_TEMPLOAD (cfg, load, src);
1746 NEW_TEMPSTORE (cfg, inst, dest, load);
1747 if (inst->opcode == CEE_STOBJ) {
1748 NEW_TEMPLOADA (cfg, inst, dest);
1749 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE);
1751 inst->cil_code = NULL;
1752 mono_add_ins_to_end (bb, inst);
1757 * We try to share variables when possible
1760 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1765 /* inlining can result in deeper stacks */
1766 if (slot >= mono_method_get_header (cfg->method)->max_stack)
1767 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1769 pos = ins->type - 1 + slot * STACK_MAX;
1771 switch (ins->type) {
1778 if ((vnum = cfg->intvars [pos]))
1779 return cfg->varinfo [vnum];
1780 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1781 cfg->intvars [pos] = res->inst_c0;
1784 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1790 * This function is called to handle items that are left on the evaluation stack
1791 * at basic block boundaries. What happens is that we save the values to local variables
1792 * and we reload them later when first entering the target basic block (with the
1793 * handle_loaded_temps () function).
1794 * It is also used to handle items on the stack in store opcodes, since it is
1795 * possible that the variable to be stored into is already on the stack, in
1796 * which case its old value should be used.
1797 * A single joint point will use the same variables (stored in the array bb->out_stack or
1798 * bb->in_stack, if the basic block is before or after the joint point).
1801 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count) {
1803 MonoBasicBlock *outb;
1804 MonoInst *inst, **locals;
1809 if (cfg->verbose_level > 3)
1810 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
1811 if (!bb->out_scount) {
1812 bb->out_scount = count;
1813 //g_print ("bblock %d has out:", bb->block_num);
1815 for (i = 0; i < bb->out_count; ++i) {
1816 outb = bb->out_bb [i];
1817 //g_print (" %d", outb->block_num);
1818 if (outb->in_stack) {
1820 bb->out_stack = outb->in_stack;
1826 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1827 for (i = 0; i < count; ++i) {
1829 * try to reuse temps already allocated for this purpouse, if they occupy the same
1830 * stack slot and if they are of the same type.
1831 * This won't cause conflicts since if 'local' is used to
1832 * store one of the values in the in_stack of a bblock, then
1833 * the same variable will be used for the same outgoing stack
1835 * This doesn't work when inlining methods, since the bblocks
1836 * in the inlined methods do not inherit their in_stack from
1837 * the bblock they are inlined to. See bug #58863 for an
1840 if (cfg->inlined_method)
1841 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1843 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1848 for (i = 0; i < bb->out_count; ++i) {
1849 outb = bb->out_bb [i];
1850 if (outb->in_scount)
1851 continue; /* check they are the same locals */
1852 outb->in_scount = count;
1853 outb->in_stack = bb->out_stack;
1856 locals = bb->out_stack;
1857 for (i = 0; i < count; ++i) {
1858 /* add store ops at the end of the bb, before the branch */
1859 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1860 if (inst->opcode == CEE_STOBJ) {
1861 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
1862 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE);
1864 inst->cil_code = sp [i]->cil_code;
1865 mono_add_ins_to_end (bb, inst);
1867 if (cfg->verbose_level > 3)
1868 g_print ("storing %d to temp %d\n", i, (int)locals [i]->inst_c0);
1872 * It is possible that the out bblocks already have in_stack assigned, and
1873 * the in_stacks differ. In this case, we will store to all the different
1880 /* Find a bblock which has a different in_stack */
1882 while (bindex < bb->out_count) {
1883 outb = bb->out_bb [bindex];
1884 if (outb->in_stack != locals) {
1886 * Instead of storing sp [i] to locals [i], we need to store
1887 * locals [i] to <new locals>[i], since the sp [i] tree can't
1888 * be shared between trees.
1890 for (i = 0; i < count; ++i)
1891 mono_add_varcopy_to_end (cfg, bb, locals [i]->inst_c0, outb->in_stack [i]->inst_c0);
1892 locals = outb->in_stack;
1904 ret_type_to_call_opcode (MonoType *type, int calli, int virt)
1907 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1910 switch (type->type) {
1911 case MONO_TYPE_VOID:
1912 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
1915 case MONO_TYPE_BOOLEAN:
1918 case MONO_TYPE_CHAR:
1921 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1925 case MONO_TYPE_FNPTR:
1926 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1927 case MONO_TYPE_CLASS:
1928 case MONO_TYPE_STRING:
1929 case MONO_TYPE_OBJECT:
1930 case MONO_TYPE_SZARRAY:
1931 case MONO_TYPE_ARRAY:
1932 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1935 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
1938 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
1939 case MONO_TYPE_VALUETYPE:
1940 if (type->data.klass->enumtype) {
1941 type = type->data.klass->enum_basetype;
1944 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1945 case MONO_TYPE_TYPEDBYREF:
1946 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1947 case MONO_TYPE_GENERICINST:
1948 type = &type->data.generic_class->container_class->byval_arg;
1951 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
1957 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
1959 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
1960 MonoJumpInfoBBTable *table;
1962 table = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfoBBTable));
1964 table->table_size = num_blocks;
1966 ji->ip.label = label;
1967 ji->type = MONO_PATCH_INFO_SWITCH;
1968 ji->data.table = table;
1969 ji->next = cfg->patch_info;
1970 cfg->patch_info = ji;
1974 * When we add a tree of instructions, we need to ensure the instructions currently
1975 * on the stack are executed before (like, if we load a value from a local).
1976 * We ensure this by saving the currently loaded values to temps and rewriting the
1977 * instructions to load the values.
1978 * This is not done for opcodes that terminate a basic block (because it's handled already
1979 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
1982 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
1984 MonoInst *load, *store, *temp, *ins;
1986 while (stack < sp) {
1988 /* handle also other constants */
1989 if ((ins->opcode != OP_ICONST) &&
1990 /* temps never get written to again, so we can safely avoid duplicating them */
1991 !(ins->ssa_op == MONO_SSA_LOAD && ins->inst_i0->opcode == OP_LOCAL && ins->inst_i0->flags & MONO_INST_IS_TEMP)) {
1992 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1993 temp->flags |= MONO_INST_IS_TEMP;
1994 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1995 store->cil_code = ins->cil_code;
1996 if (store->opcode == CEE_STOBJ) {
1997 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
1998 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE);
2000 MONO_ADD_INS (bblock, store);
2001 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2002 load->cil_code = ins->cil_code;
2010 * Prepare arguments for passing to a function call.
2011 * Return a non-zero value if the arguments can't be passed to the given
2013 * The type checks are not yet complete and some conversions may need
2014 * casts on 32 or 64 bit architectures.
2017 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
2019 MonoType *simple_type;
2023 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
2027 for (i = 0; i < sig->param_count; ++i) {
2028 if (sig->params [i]->byref) {
2029 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
2033 simple_type = sig->params [i];
2035 switch (simple_type->type) {
2036 case MONO_TYPE_VOID:
2041 case MONO_TYPE_BOOLEAN:
2044 case MONO_TYPE_CHAR:
2047 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
2053 case MONO_TYPE_FNPTR:
2054 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
2057 case MONO_TYPE_CLASS:
2058 case MONO_TYPE_STRING:
2059 case MONO_TYPE_OBJECT:
2060 case MONO_TYPE_SZARRAY:
2061 case MONO_TYPE_ARRAY:
2062 if (args [i]->type != STACK_OBJ)
2067 if (args [i]->type != STACK_I8)
2072 if (args [i]->type != STACK_R8)
2075 case MONO_TYPE_VALUETYPE:
2076 if (simple_type->data.klass->enumtype) {
2077 simple_type = simple_type->data.klass->enum_basetype;
2080 if (args [i]->type != STACK_VTYPE)
2083 case MONO_TYPE_TYPEDBYREF:
2084 if (args [i]->type != STACK_VTYPE)
2087 case MONO_TYPE_GENERICINST:
2088 simple_type = &simple_type->data.generic_class->container_class->byval_arg;
2092 g_error ("unknown type 0x%02x in check_call_signature",
2100 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
2101 const guint8 *ip, gboolean to_end)
2103 MonoInst *temp, *store, *ins = (MonoInst*)call;
2104 MonoType *ret = sig->ret;
2106 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
2108 call->inst.type = STACK_OBJ;
2109 call->inst.opcode = CEE_CALL;
2110 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
2112 type_to_eval_stack_type (ret, ins);
2113 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
2116 temp->flags |= MONO_INST_IS_TEMP;
2118 if (MONO_TYPE_ISSTRUCT (ret)) {
2119 MonoInst *loada, *dummy_store;
2122 * Emit a dummy store to the local holding the result so the
2123 * liveness info remains correct.
2125 NEW_DUMMY_STORE (cfg, dummy_store, temp->inst_c0);
2127 mono_add_ins_to_end (bblock, dummy_store);
2129 MONO_ADD_INS (bblock, dummy_store);
2131 /* we use this to allocate native sized structs */
2132 temp->unused = sig->pinvoke;
2134 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
2135 if (call->inst.opcode == OP_VCALL)
2136 ins->inst_left = loada;
2138 ins->inst_right = loada; /* a virtual or indirect call */
2141 mono_add_ins_to_end (bblock, ins);
2143 MONO_ADD_INS (bblock, ins);
2145 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2146 store->cil_code = ip;
2148 mono_add_ins_to_end (bblock, store);
2150 MONO_ADD_INS (bblock, store);
2152 return temp->inst_c0;
2155 mono_add_ins_to_end (bblock, ins);
2157 MONO_ADD_INS (bblock, ins);
2162 inline static MonoCallInst *
2163 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
2164 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
2169 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual));
2171 call->inst.cil_code = ip;
2173 call->signature = sig;
2174 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
2175 type_to_eval_stack_type (sig->ret, &call->inst);
2177 for (arg = call->out_args; arg;) {
2178 MonoInst *narg = arg->next;
2183 mono_add_ins_to_end (bblock, arg);
2185 MONO_ADD_INS (bblock, arg);
2192 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
2193 MonoInst **args, MonoInst *addr, const guint8 *ip)
2195 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
2197 call->inst.inst_i0 = addr;
2199 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
2202 static MonoCallInst*
2203 mono_emit_method_call_full (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
2204 MonoInst **args, const guint8 *ip, MonoInst *this, gboolean to_end)
2206 gboolean virtual = this != NULL;
2209 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, to_end);
2211 if (this && sig->hasthis &&
2212 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
2213 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
2214 call->method = mono_marshal_get_remoting_invoke_with_check (method);
2216 call->method = method;
2218 call->inst.flags |= MONO_INST_HAS_METHOD;
2219 call->inst.inst_left = this;
2222 mono_get_got_var (cfg);
2223 else if (call->method->klass->flags & TYPE_ATTRIBUTE_INTERFACE)
2224 /* Needed by the code generated in inssel.brg */
2225 mono_get_got_var (cfg);
2230 static MonoCallInst*
2231 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
2232 MonoInst **args, const guint8 *ip, MonoInst *this)
2234 return mono_emit_method_call_full (cfg, bblock, method, sig, args, ip, this, FALSE);
2238 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
2239 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
2241 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, signature, args, ip, this);
2243 return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
2247 mono_emit_method_call_spilled_full (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
2248 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this,
2249 gboolean ret_object, gboolean to_end)
2251 MonoCallInst *call = mono_emit_method_call_full (cfg, bblock, method, signature, args, ip, this, to_end);
2253 return mono_spill_call (cfg, bblock, call, signature, ret_object, ip, to_end);
2257 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
2258 MonoInst **args, const guint8 *ip, gboolean ret_object, gboolean to_end)
2264 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
2267 mono_get_got_var (cfg);
2269 return mono_spill_call (cfg, bblock, call, sig, ret_object, ip, to_end);
2273 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
2275 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
2278 g_warning ("unregistered JIT ICall");
2279 g_assert_not_reached ();
2282 mono_get_got_var (cfg);
2283 return mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, args, ip, FALSE, FALSE);
2287 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
2289 MonoInst *ins, *temp = NULL, *store, *load, *begin;
2290 MonoInst *last_arg = NULL;
2294 //g_print ("emulating: ");
2295 //mono_print_tree_nl (tree);
2296 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE));
2297 ins = (MonoInst*)call;
2299 call->inst.cil_code = tree->cil_code;
2301 call->signature = info->sig;
2303 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
2305 mono_get_got_var (cfg);
2307 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2308 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
2309 temp->flags |= MONO_INST_IS_TEMP;
2310 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2311 store->cil_code = tree->cil_code;
2316 nargs = info->sig->param_count + info->sig->hasthis;
2318 for (last_arg = call->out_args; last_arg && last_arg->next; last_arg = last_arg->next) ;
2321 last_arg->next = store;
2324 begin = call->out_args;
2328 if (cfg->prev_ins) {
2330 * This assumes that that in a tree, emulate_opcode is called for a
2331 * node before it is called for its children. dec_foreach needs to
2332 * take this into account.
2334 store->next = cfg->prev_ins->next;
2335 cfg->prev_ins->next = begin;
2337 store->next = cfg->cbb->code;
2338 cfg->cbb->code = begin;
2341 call->fptr = mono_icall_get_wrapper (info);
2343 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2344 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2349 static MonoMethodSignature *
2350 mono_get_element_address_signature (int arity)
2352 static GHashTable *sighash = NULL;
2353 MonoMethodSignature *res;
2356 EnterCriticalSection (&jit_mutex);
2358 sighash = g_hash_table_new (NULL, NULL);
2360 else if ((res = g_hash_table_lookup (sighash, GINT_TO_POINTER (arity)))) {
2361 LeaveCriticalSection (&jit_mutex);
2365 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
2368 #ifdef MONO_ARCH_VARARG_ICALLS
2369 /* Only set this only some archs since not all backends can handle varargs+pinvoke */
2370 res->call_convention = MONO_CALL_VARARG;
2372 res->params [0] = &mono_defaults.array_class->byval_arg;
2374 for (i = 1; i <= arity; i++)
2375 res->params [i] = &mono_defaults.int_class->byval_arg;
2377 res->ret = &mono_defaults.int_class->byval_arg;
2379 g_hash_table_insert (sighash, GINT_TO_POINTER (arity), res);
2380 LeaveCriticalSection (&jit_mutex);
2385 static MonoMethodSignature *
2386 mono_get_array_new_va_signature (int arity)
2388 static GHashTable *sighash = NULL;
2389 MonoMethodSignature *res;
2392 EnterCriticalSection (&jit_mutex);
2394 sighash = g_hash_table_new (NULL, NULL);
2396 else if ((res = g_hash_table_lookup (sighash, GINT_TO_POINTER (arity)))) {
2397 LeaveCriticalSection (&jit_mutex);
2401 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
2404 #ifdef MONO_ARCH_VARARG_ICALLS
2405 /* Only set this only some archs since not all backends can handle varargs+pinvoke */
2406 res->call_convention = MONO_CALL_VARARG;
2409 res->params [0] = &mono_defaults.int_class->byval_arg;
2410 for (i = 0; i < arity; i++)
2411 res->params [i + 1] = &mono_defaults.int_class->byval_arg;
2413 res->ret = &mono_defaults.int_class->byval_arg;
2415 g_hash_table_insert (sighash, GINT_TO_POINTER (arity), res);
2416 LeaveCriticalSection (&jit_mutex);
2422 get_memcpy_method (void)
2424 static MonoMethod *memcpy_method = NULL;
2425 if (!memcpy_method) {
2426 memcpy_method = mono_class_get_method_from_name (mono_defaults.string_class, "memcpy", 3);
2428 g_error ("Old corlib found. Install a new one");
2430 return memcpy_method;
2434 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native) {
2435 MonoInst *iargs [3];
2438 MonoMethod *memcpy_method;
2442 * This check breaks with spilled vars... need to handle it during verification anyway.
2443 * g_assert (klass && klass == src->klass && klass == dest->klass);
2447 n = mono_class_native_size (klass, &align);
2449 n = mono_class_value_size (klass, &align);
2451 if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
2453 if (dest->opcode == OP_LDADDR) {
2454 /* Keep liveness info correct */
2455 NEW_DUMMY_STORE (cfg, inst, dest->inst_i0->inst_c0);
2456 MONO_ADD_INS (bblock, inst);
2458 MONO_INST_NEW (cfg, inst, OP_MEMCPY);
2459 inst->inst_left = dest;
2460 inst->inst_right = src;
2461 inst->cil_code = ip;
2463 MONO_ADD_INS (bblock, inst);
2468 NEW_ICONST (cfg, iargs [2], n);
2470 memcpy_method = get_memcpy_method ();
2471 mono_emit_method_call_spilled_full (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL, FALSE, to_end);
2475 get_memset_method (void)
2477 static MonoMethod *memset_method = NULL;
2478 if (!memset_method) {
2479 memset_method = mono_class_get_method_from_name (mono_defaults.string_class, "memset", 3);
2481 g_error ("Old corlib found. Install a new one");
2483 return memset_method;
2487 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
2489 MonoInst *iargs [3];
2490 MonoInst *ins, *zero_int32;
2492 MonoMethod *memset_method;
2494 NEW_ICONST (cfg, zero_int32, 0);
2496 mono_class_init (klass);
2497 n = mono_class_value_size (klass, NULL);
2498 MONO_INST_NEW (cfg, ins, 0);
2500 ins->inst_left = dest;
2501 ins->inst_right = zero_int32;
2504 ins->opcode = CEE_STIND_I1;
2505 MONO_ADD_INS (bblock, ins);
2508 ins->opcode = CEE_STIND_I2;
2509 MONO_ADD_INS (bblock, ins);
2512 ins->opcode = CEE_STIND_I4;
2513 MONO_ADD_INS (bblock, ins);
2516 if (n <= sizeof (gpointer) * 5) {
2517 ins->opcode = OP_MEMSET;
2520 MONO_ADD_INS (bblock, ins);
2523 memset_method = get_memset_method ();
2524 handle_loaded_temps (cfg, bblock, stack_start, sp);
2526 NEW_ICONST (cfg, iargs [1], 0);
2527 NEW_ICONST (cfg, iargs [2], n);
2528 mono_emit_method_call_spilled (cfg, bblock, memset_method, memset_method->signature, iargs, ip, NULL);
2534 handle_alloc (MonoCompile *cfg, MonoBasicBlock *bblock, MonoClass *klass, gboolean for_box, const guchar *ip)
2536 MonoInst *iargs [2];
2539 if (cfg->opt & MONO_OPT_SHARED) {
2540 NEW_DOMAINCONST (cfg, iargs [0]);
2541 NEW_CLASSCONST (cfg, iargs [1], klass);
2543 alloc_ftn = mono_object_new;
2545 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
2548 alloc_ftn = mono_class_get_allocation_ftn (vtable, for_box, &pass_lw);
2550 guint32 lw = vtable->klass->instance_size;
2551 lw = ((lw + (sizeof (gpointer) - 1)) & ~(sizeof (gpointer) - 1)) / sizeof (gpointer);
2552 NEW_ICONST (cfg, iargs [0], lw);
2553 NEW_VTABLECONST (cfg, iargs [1], vtable);
2556 NEW_VTABLECONST (cfg, iargs [0], vtable);
2559 return mono_emit_jit_icall (cfg, bblock, alloc_ftn, iargs, ip);
2563 handle_box (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *val, const guchar *ip, MonoClass *klass)
2565 MonoInst *dest, *vtoffset, *add, *vstore;
2568 temp = handle_alloc (cfg, bblock, klass, TRUE, ip);
2569 NEW_TEMPLOAD (cfg, dest, temp);
2570 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
2571 MONO_INST_NEW (cfg, add, OP_PADD);
2572 add->inst_left = dest;
2573 add->inst_right = vtoffset;
2576 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
2577 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
2578 vstore->cil_code = ip;
2579 vstore->inst_left = add;
2580 vstore->inst_right = val;
2582 if (vstore->opcode == CEE_STOBJ) {
2583 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
2585 MONO_ADD_INS (bblock, vstore);
2587 NEW_TEMPLOAD (cfg, dest, temp);
2592 handle_array_new (MonoCompile *cfg, MonoBasicBlock *bblock, int rank, MonoInst **sp, unsigned char *ip)
2594 MonoMethodSignature *esig;
2595 char icall_name [256];
2597 MonoJitICallInfo *info;
2599 /* Need to register the icall so it gets an icall wrapper */
2600 sprintf (icall_name, "ves_array_new_va_%d", rank);
2602 info = mono_find_jit_icall_by_name (icall_name);
2604 esig = mono_get_array_new_va_signature (rank);
2605 name = g_strdup (icall_name);
2606 info = mono_register_jit_icall (mono_array_new_va, name, esig, FALSE);
2608 EnterCriticalSection (&jit_mutex);
2609 g_hash_table_insert (jit_icall_name_hash, name, name);
2610 LeaveCriticalSection (&jit_mutex);
2613 cfg->flags |= MONO_CFG_HAS_VARARGS;
2615 return mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, sp, ip, TRUE, FALSE);
2619 mono_emit_load_got_addr (MonoCompile *cfg)
2621 MonoInst *load, *store, *dummy_use;
2624 if (!cfg->got_var || cfg->got_var_allocated)
2627 MONO_INST_NEW (cfg, get_got, OP_LOAD_GOTADDR);
2628 NEW_TEMPSTORE (cfg, store, cfg->got_var->inst_c0, get_got);
2630 /* Add it to the start of the first bblock */
2631 if (cfg->bb_entry->code) {
2632 store->next = cfg->bb_entry->code;
2633 cfg->bb_entry->code = store;
2636 MONO_ADD_INS (cfg->bb_entry, store);
2638 cfg->got_var_allocated = TRUE;
2641 * Add a dummy use to keep the got_var alive, since real uses might
2642 * only be generated in the decompose or instruction selection phases.
2643 * Add it to end_bblock, so the variable's lifetime covers the whole
2646 NEW_TEMPLOAD (cfg, load, cfg->got_var->inst_c0);
2647 NEW_DUMMY_USE (cfg, dummy_use, load);
2648 MONO_ADD_INS (cfg->bb_exit, dummy_use);
2651 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
2654 mini_class_is_system_array (MonoClass *klass)
2656 if (klass->parent == mono_defaults.array_class)
2658 else if (mono_defaults.generic_array_class && klass->parent && klass->parent->generic_class)
2659 return klass->parent->generic_class->container_class == mono_defaults.generic_array_class;
2665 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
2667 MonoMethodHeader *header = mono_method_get_header (method);
2668 MonoMethodSignature *signature = mono_method_signature (method);
2672 #ifdef MONO_ARCH_HAVE_LMF_OPS
2673 if (((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2674 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) &&
2675 !MONO_TYPE_ISSTRUCT (signature->ret) && !mini_class_is_system_array (method->klass))
2679 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2680 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2681 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
2682 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
2683 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2684 (method->klass->marshalbyref) ||
2685 !header || header->num_clauses ||
2686 /* fixme: why cant we inline valuetype returns? */
2687 MONO_TYPE_ISSTRUCT (signature->ret))
2690 /* its not worth to inline methods with valuetype arguments?? */
2691 for (i = 0; i < signature->param_count; i++) {
2692 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
2698 * if we can initialize the class of the method right away, we do,
2699 * otherwise we don't allow inlining if the class needs initialization,
2700 * since it would mean inserting a call to mono_runtime_class_init()
2701 * inside the inlined code
2703 if (!(cfg->opt & MONO_OPT_SHARED)) {
2704 vtable = mono_class_vtable (cfg->domain, method->klass);
2705 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
2706 if (cfg->run_cctors)
2707 mono_runtime_class_init (vtable);
2709 else if (!vtable->initialized && mono_class_needs_cctor_run (method->klass, NULL))
2713 * If we're compiling for shared code
2714 * the cctor will need to be run at aot method load time, for example,
2715 * or at the end of the compilation of the inlining method.
2717 if (mono_class_needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
2720 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
2723 * CAS - do not inline methods with declarative security
2724 * Note: this has to be before any possible return TRUE;
2726 if (mono_method_has_declsec (method))
2729 /* also consider num_locals? */
2730 if (getenv ("MONO_INLINELIMIT")) {
2731 if (header->code_size < atoi (getenv ("MONO_INLINELIMIT"))) {
2734 } else if (header->code_size < 20)
2741 mini_field_access_needs_cctor_run (MonoCompile *cfg, MonoMethod *method, MonoVTable *vtable)
2743 if (vtable->initialized && !cfg->compile_aot)
2746 if (vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)
2749 if (!mono_class_needs_cctor_run (vtable->klass, method))
2752 if (! (method->flags & METHOD_ATTRIBUTE_STATIC) && (vtable->klass == method->klass))
2753 /* The initialization is already done before the method is called */
2760 mini_get_ldelema_ins (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
2764 MonoMethodSignature *esig;
2765 char icall_name [256];
2767 MonoJitICallInfo *info;
2769 rank = mono_method_signature (cmethod)->param_count - (is_set? 1: 0);
2771 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
2773 NEW_GROUP (cfg, indexes, sp [1], sp [2]);
2774 MONO_INST_NEW (cfg, addr, OP_LDELEMA2D);
2775 addr->inst_left = sp [0];
2776 addr->inst_right = indexes;
2777 addr->cil_code = ip;
2778 addr->type = STACK_MP;
2779 addr->klass = cmethod->klass;
2783 /* Need to register the icall so it gets an icall wrapper */
2784 sprintf (icall_name, "ves_array_element_address_%d", rank);
2786 info = mono_find_jit_icall_by_name (icall_name);
2788 esig = mono_get_element_address_signature (rank);
2789 name = g_strdup (icall_name);
2790 info = mono_register_jit_icall (ves_array_element_address, name, esig, FALSE);
2792 EnterCriticalSection (&jit_mutex);
2793 g_hash_table_insert (jit_icall_name_hash, name, name);
2794 LeaveCriticalSection (&jit_mutex);
2797 temp = mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, sp, ip, FALSE, FALSE);
2798 cfg->flags |= MONO_CFG_HAS_VARARGS;
2800 NEW_TEMPLOAD (cfg, addr, temp);
2804 static MonoJitICallInfo **emul_opcode_map = NULL;
2806 static inline MonoJitICallInfo *
2807 mono_find_jit_opcode_emulation (int opcode)
2809 if (emul_opcode_map)
2810 return emul_opcode_map [opcode];
2816 mini_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
2818 MonoInst *ins = NULL;
2820 static MonoClass *runtime_helpers_class = NULL;
2821 if (! runtime_helpers_class)
2822 runtime_helpers_class = mono_class_from_name (mono_defaults.corlib,
2823 "System.Runtime.CompilerServices", "RuntimeHelpers");
2825 if (cmethod->klass == mono_defaults.string_class) {
2826 if (cmethod->name [0] != 'g')
2829 if (strcmp (cmethod->name, "get_Chars") == 0) {
2830 MONO_INST_NEW (cfg, ins, OP_GETCHR);
2831 ins->inst_i0 = args [0];
2832 ins->inst_i1 = args [1];
2834 } else if (strcmp (cmethod->name, "get_Length") == 0) {
2835 MONO_INST_NEW (cfg, ins, OP_STRLEN);
2836 ins->inst_i0 = args [0];
2840 } else if (cmethod->klass == mono_defaults.object_class) {
2841 if (strcmp (cmethod->name, "GetType") == 0) {
2842 MONO_INST_NEW (cfg, ins, OP_GETTYPE);
2843 ins->inst_i0 = args [0];
2845 } else if (strcmp (cmethod->name, "InternalGetHashCode") == 0) {
2846 MONO_INST_NEW (cfg, ins, OP_GETHASHCODE);
2847 ins->inst_i0 = args [0];
2851 } else if (mini_class_is_system_array (cmethod->klass)) {
2852 if (cmethod->name [0] != 'g')
2855 if (strcmp (cmethod->name, "get_Rank") == 0) {
2856 MONO_INST_NEW (cfg, ins, OP_ARRAY_RANK);
2857 ins->inst_i0 = args [0];
2859 } else if (strcmp (cmethod->name, "get_Length") == 0) {
2860 MONO_INST_NEW (cfg, ins, CEE_LDLEN);
2861 ins->inst_i0 = args [0];
2865 } else if (cmethod->klass == runtime_helpers_class) {
2866 if (strcmp (cmethod->name, "get_OffsetToStringData") == 0) {
2867 NEW_ICONST (cfg, ins, G_STRUCT_OFFSET (MonoString, chars));
2871 } else if (cmethod->klass == mono_defaults.thread_class) {
2872 if (strcmp (cmethod->name, "get_CurrentThread") == 0 && (ins = mono_arch_get_thread_intrinsic (cfg)))
2877 return mono_arch_get_inst_for_method (cfg, cmethod, fsig, args);
2881 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
2883 MonoInst *store, *temp;
2886 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
2888 if (!sig->hasthis && sig->param_count == 0)
2892 if (sp [0]->opcode == OP_ICONST) {
2895 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
2897 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2898 store->cil_code = sp [0]->cil_code;
2899 MONO_ADD_INS (bblock, store);
2904 for (i = 0; i < sig->param_count; ++i) {
2905 if (sp [0]->opcode == OP_ICONST) {
2908 temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
2910 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2911 store->cil_code = sp [0]->cil_code;
2912 if (store->opcode == CEE_STOBJ) {
2913 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2914 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE);
2916 MONO_ADD_INS (bblock, store);
2924 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
2925 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b, gboolean inline_allways)
2927 MonoInst *ins, *rvar = NULL;
2928 MonoMethodHeader *cheader;
2929 MonoBasicBlock *ebblock, *sbblock;
2930 int i, costs, new_locals_offset;
2931 MonoMethod *prev_inlined_method;
2933 if (cfg->verbose_level > 2)
2934 g_print ("INLINE START %p %s -> %s\n", cmethod, mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2936 if (!cmethod->inline_info) {
2937 mono_jit_stats.inlineable_methods++;
2938 cmethod->inline_info = 1;
2940 /* allocate space to store the return value */
2941 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2942 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2945 /* allocate local variables */
2946 cheader = mono_method_get_header (cmethod);
2947 new_locals_offset = cfg->num_varinfo;
2948 for (i = 0; i < cheader->num_locals; ++i)
2949 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
2951 /* allocate starte and end blocks */
2952 sbblock = NEW_BBLOCK (cfg);
2953 sbblock->block_num = cfg->num_bblocks++;
2954 sbblock->real_offset = real_offset;
2956 ebblock = NEW_BBLOCK (cfg);
2957 ebblock->block_num = cfg->num_bblocks++;
2958 ebblock->real_offset = real_offset;
2960 prev_inlined_method = cfg->inlined_method;
2961 cfg->inlined_method = cmethod;
2963 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
2965 cfg->inlined_method = prev_inlined_method;
2967 if ((costs >= 0 && costs < 60) || inline_allways) {
2968 if (cfg->verbose_level > 2)
2969 g_print ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2971 mono_jit_stats.inlined_methods++;
2973 /* always add some code to avoid block split failures */
2974 MONO_INST_NEW (cfg, ins, CEE_NOP);
2975 MONO_ADD_INS (bblock, ins);
2978 bblock->next_bb = sbblock;
2979 link_bblock (cfg, bblock, sbblock);
2982 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
2988 if (cfg->verbose_level > 2)
2989 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
2995 * Some of these comments may well be out-of-date.
2996 * Design decisions: we do a single pass over the IL code (and we do bblock
2997 * splitting/merging in the few cases when it's required: a back jump to an IL
2998 * address that was not already seen as bblock starting point).
2999 * Code is validated as we go (full verification is still better left to metadata/verify.c).
3000 * Complex operations are decomposed in simpler ones right away. We need to let the
3001 * arch-specific code peek and poke inside this process somehow (except when the
3002 * optimizations can take advantage of the full semantic info of coarse opcodes).
3003 * All the opcodes of the form opcode.s are 'normalized' to opcode.
3004 * MonoInst->opcode initially is the IL opcode or some simplification of that
3005 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
3006 * opcode with value bigger than OP_LAST.
3007 * At this point the IR can be handed over to an interpreter, a dumb code generator
3008 * or to the optimizing code generator that will translate it to SSA form.
3010 * Profiling directed optimizations.
3011 * We may compile by default with few or no optimizations and instrument the code
3012 * or the user may indicate what methods to optimize the most either in a config file
3013 * or through repeated runs where the compiler applies offline the optimizations to
3014 * each method and then decides if it was worth it.
3017 * * consider using an array instead of an hash table (bb_hash)
3020 #define CHECK_TYPE(ins) if (!(ins)->type) goto unverified
3021 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) goto unverified
3022 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) goto unverified
3023 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) goto unverified
3024 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) goto unverified
3025 #define CHECK_OPSIZE(size) if (ip + size > end) goto unverified
3028 /* offset from br.s -> br like opcodes */
3029 #define BIG_BRANCH_OFFSET 13
3032 ip_in_bb (MonoCompile *cfg, MonoBasicBlock *bb, const guint8* ip)
3034 MonoBasicBlock *b = g_hash_table_lookup (cfg->bb_hash, ip);
3036 return b == NULL || b == bb;
3040 get_basic_blocks (MonoCompile *cfg, GHashTable *bbhash, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
3042 unsigned char *ip = start;
3043 unsigned char *target;
3046 MonoBasicBlock *bblock;
3047 const MonoOpcode *opcode;
3050 cli_addr = ip - start;
3051 i = mono_opcode_value ((const guint8 **)&ip, end);
3054 opcode = &mono_opcodes [i];
3055 switch (opcode->argument) {
3056 case MonoInlineNone:
3059 case MonoInlineString:
3060 case MonoInlineType:
3061 case MonoInlineField:
3062 case MonoInlineMethod:
3065 case MonoShortInlineR:
3072 case MonoShortInlineVar:
3073 case MonoShortInlineI:
3076 case MonoShortInlineBrTarget:
3077 target = start + cli_addr + 2 + (signed char)ip [1];
3078 GET_BBLOCK (cfg, bbhash, bblock, target);
3081 case MonoInlineBrTarget:
3082 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
3083 GET_BBLOCK (cfg, bbhash, bblock, target);
3086 case MonoInlineSwitch: {
3087 guint32 n = read32 (ip + 1);
3090 cli_addr += 5 + 4 * n;
3091 target = start + cli_addr;
3092 GET_BBLOCK (cfg, bbhash, bblock, target);
3094 for (j = 0; j < n; ++j) {
3095 target = start + cli_addr + (gint32)read32 (ip);
3096 GET_BBLOCK (cfg, bbhash, bblock, target);
3106 g_assert_not_reached ();
3116 emit_tree (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *ins, const guint8* ip_next)
3118 MonoInst *store, *temp, *load;
3120 if (ip_in_bb (cfg, bblock, ip_next) &&
3121 (CODE_IS_STLOC (ip_next) || *ip_next == CEE_BRTRUE || *ip_next == CEE_BRFALSE ||
3122 *ip_next == CEE_BRTRUE_S || *ip_next == CEE_BRFALSE_S || *ip_next == CEE_RET))
3125 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3126 temp->flags |= MONO_INST_IS_TEMP;
3127 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3128 store->cil_code = ins->cil_code;
3129 MONO_ADD_INS (bblock, store);
3130 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
3131 load->cil_code = ins->cil_code;
3135 static inline MonoMethod *
3136 mini_get_method (MonoImage *image, guint32 token, MonoClass *klass, MonoGenericContext *context)
3138 MonoMethod *method = mono_get_method_full (image, token, klass, context);
3140 if (method->is_inflated)
3141 method = mono_get_inflated_method (method);
3147 void check_linkdemand (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee, MonoBasicBlock *bblock, unsigned char *ip)
3149 guint32 result = mono_declsec_linkdemand (cfg->domain, caller, callee);
3150 if (result == MONO_JIT_SECURITY_OK)
3153 if (result == MONO_JIT_LINKDEMAND_ECMA) {
3154 /* Generate code to throw a SecurityException before the actual call/link */
3155 MonoAssembly *assembly = mono_image_get_assembly (caller->klass->image);
3156 MonoReflectionAssembly *refass = (MonoReflectionAssembly*) mono_assembly_get_object (cfg->domain, assembly);
3157 MonoReflectionMethod *refmet = mono_method_get_object (cfg->domain, caller, NULL);
3158 MonoSecurityManager *secman = mono_security_manager_get_methods ();
3161 NEW_ICONST (cfg, args [0], 4);
3162 NEW_PCONST (cfg, args [1], refass);
3163 NEW_PCONST (cfg, args [2], refmet);
3164 mono_emit_method_call_spilled (cfg, bblock, secman->linkdemandsecurityexception, mono_method_signature (secman->linkdemandsecurityexception), args, ip, NULL);
3165 } else if (cfg->exception_type == MONO_EXCEPTION_NONE) {
3166 /* don't hide previous results */
3167 cfg->exception_type = MONO_EXCEPTION_SECURITY_LINKDEMAND;
3168 cfg->exception_data = result;
3174 * mono_method_to_ir: translates IL into basic blocks containing trees
3177 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
3178 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
3179 guint inline_offset, gboolean is_virtual_call)
3181 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
3182 MonoInst *ins, **sp, **stack_start;
3183 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
3185 MonoMethod *cmethod;
3186 MonoInst **arg_array;
3187 MonoMethodHeader *header;
3189 guint32 token, ins_flag;
3191 MonoClass *constrained_call = NULL;
3192 unsigned char *ip, *end, *target, *err_pos;
3193 static double r8_0 = 0.0;
3194 MonoMethodSignature *sig;
3195 MonoGenericContext *generic_context = NULL;
3196 MonoGenericContainer *generic_container = NULL;
3197 MonoType **param_types;
3198 GList *bb_recheck = NULL, *tmp;
3199 int i, n, start_new_bblock, align;
3200 int num_calls = 0, inline_costs = 0;
3201 int breakpoint_id = 0;
3202 guint real_offset, num_args;
3203 MonoBoolean security, pinvoke;
3204 MonoSecurityManager* secman = NULL;
3205 MonoDeclSecurityActions actions;
3206 GSList *class_inits;
3208 image = method->klass->image;
3209 header = mono_method_get_header (method);
3210 generic_container = ((MonoMethodNormal *)method)->generic_container;
3211 sig = mono_method_signature (method);
3212 num_args = sig->hasthis + sig->param_count;
3213 ip = (unsigned char*)header->code;
3214 end = ip + header->code_size;
3215 mono_jit_stats.cil_code_size += header->code_size;
3217 if (sig->is_inflated)
3218 generic_context = ((MonoMethodInflated *) method)->context;
3219 else if (generic_container)
3220 generic_context = &generic_container->context;
3222 g_assert (!sig->has_type_parameters);
3224 if (cfg->method == method) {
3226 bbhash = cfg->bb_hash;
3228 real_offset = inline_offset;
3229 bbhash = g_hash_table_new (g_direct_hash, NULL);
3232 if (cfg->verbose_level > 2)
3233 g_print ("method to IR %s\n", mono_method_full_name (method, TRUE));
3235 dont_inline = g_list_prepend (dont_inline, method);
3236 if (cfg->method == method) {
3238 if (cfg->method->save_lmf)
3239 /* Needed by the prolog code */
3240 mono_get_got_var (cfg);
3242 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
3243 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
3246 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
3247 start_bblock->cil_code = NULL;
3248 start_bblock->cil_length = 0;
3249 start_bblock->block_num = cfg->num_bblocks++;
3252 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
3253 end_bblock->cil_code = NULL;
3254 end_bblock->cil_length = 0;
3255 end_bblock->block_num = cfg->num_bblocks++;
3256 g_assert (cfg->num_bblocks == 2);
3258 arg_array = alloca (sizeof (MonoInst *) * num_args);
3259 for (i = num_args - 1; i >= 0; i--)
3260 arg_array [i] = cfg->varinfo [i];
3262 if (header->num_clauses) {
3263 cfg->spvars = g_hash_table_new (NULL, NULL);
3264 cfg->exvars = g_hash_table_new (NULL, NULL);
3266 /* handle exception clauses */
3267 for (i = 0; i < header->num_clauses; ++i) {
3268 //unsigned char *p = ip;
3269 MonoExceptionClause *clause = &header->clauses [i];
3270 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->try_offset);
3271 tblock->real_offset = clause->try_offset;
3272 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
3273 tblock->real_offset = clause->handler_offset;
3275 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
3276 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
3277 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
3278 MONO_ADD_INS (tblock, ins);
3281 /*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);
3283 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
3285 /* catch and filter blocks get the exception object on the stack */
3286 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
3287 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
3288 MonoInst *load, *dummy_use;
3290 /* mostly like handle_stack_args (), but just sets the input args */
3291 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
3292 tblock->in_scount = 1;
3293 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
3294 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
3297 * Add a dummy use for the exvar so its liveness info will be
3300 NEW_TEMPLOAD (cfg, load, tblock->in_stack [0]->inst_c0);
3301 NEW_DUMMY_USE (cfg, dummy_use, load);
3302 MONO_ADD_INS (tblock, dummy_use);
3304 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
3305 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->data.filter_offset);
3306 tblock->real_offset = clause->data.filter_offset;
3307 tblock->in_scount = 1;
3308 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
3309 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->data.filter_offset);
3310 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
3311 MONO_ADD_INS (tblock, ins);
3316 arg_array = alloca (sizeof (MonoInst *) * num_args);
3317 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
3320 /* FIRST CODE BLOCK */
3321 bblock = NEW_BBLOCK (cfg);
3322 bblock->cil_code = ip;
3324 ADD_BBLOCK (cfg, bbhash, bblock);
3326 if (cfg->method == method) {
3327 breakpoint_id = mono_debugger_method_has_breakpoint (method);
3328 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
3329 MONO_INST_NEW (cfg, ins, CEE_BREAK);
3330 MONO_ADD_INS (bblock, ins);
3334 if (mono_use_security_manager)
3335 secman = mono_security_manager_get_methods ();
3337 security = (secman && mono_method_has_declsec (method));
3338 /* at this point having security doesn't mean we have any code to generate */
3339 if (security && (cfg->method == method)) {
3340 /* Only Demand, NonCasDemand and DemandChoice requires code generation.
3341 * And we do not want to enter the next section (with allocation) if we
3342 * have nothing to generate */
3343 security = mono_declsec_get_demands (method, &actions);
3346 /* we must Demand SecurityPermission.Unmanaged before P/Invoking */
3347 pinvoke = (secman && (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE));
3349 MonoMethod *wrapped = mono_marshal_method_from_wrapper (method);
3350 if (wrapped && (wrapped->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
3351 MonoCustomAttrInfo* custom = mono_custom_attrs_from_method (wrapped);
3353 /* unless the method or it's class has the [SuppressUnmanagedCodeSecurity] attribute */
3354 if (custom && mono_custom_attrs_has_attr (custom, secman->suppressunmanagedcodesecurity)) {
3359 custom = mono_custom_attrs_from_class (wrapped->klass);
3360 if (custom && mono_custom_attrs_has_attr (custom, secman->suppressunmanagedcodesecurity)) {
3365 /* not a P/Invoke after all */
3370 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED))) || mono_compile_aot || security || pinvoke) {
3371 /* we use a separate basic block for the initialization code */
3372 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
3373 init_localsbb->real_offset = real_offset;
3374 start_bblock->next_bb = init_localsbb;
3375 init_localsbb->next_bb = bblock;
3376 link_bblock (cfg, start_bblock, init_localsbb);
3377 link_bblock (cfg, init_localsbb, bblock);
3378 init_localsbb->block_num = cfg->num_bblocks++;
3380 start_bblock->next_bb = bblock;
3381 link_bblock (cfg, start_bblock, bblock);
3384 /* at this point we know, if security is TRUE, that some code needs to be generated */
3385 if (security && (cfg->method == method)) {
3388 mono_jit_stats.cas_demand_generation++;
3390 if (actions.demand.blob) {
3391 /* Add code for SecurityAction.Demand */
3392 NEW_DECLSECCONST (cfg, args[0], image, actions.demand);
3393 NEW_ICONST (cfg, args [1], actions.demand.size);
3394 /* Calls static void SecurityManager.InternalDemand (byte* permissions, int size); */
3395 mono_emit_method_call_spilled (cfg, init_localsbb, secman->demand, mono_method_signature (secman->demand), args, ip, NULL);
3397 if (actions.noncasdemand.blob) {
3398 /* CLR 1.x uses a .noncasdemand (but 2.x doesn't) */
3399 /* For Mono we re-route non-CAS Demand to Demand (as the managed code must deal with it anyway) */
3400 NEW_DECLSECCONST (cfg, args[0], image, actions.noncasdemand);
3401 NEW_ICONST (cfg, args [1], actions.noncasdemand.size);
3402 /* Calls static void SecurityManager.InternalDemand (byte* permissions, int size); */
3403 mono_emit_method_call_spilled (cfg, init_localsbb, secman->demand, mono_method_signature (secman->demand), args, ip, NULL);
3405 if (actions.demandchoice.blob) {
3406 /* New in 2.0, Demand must succeed for one of the permissions (i.e. not all) */
3407 NEW_DECLSECCONST (cfg, args[0], image, actions.demandchoice);
3408 NEW_ICONST (cfg, args [1], actions.demandchoice.size);
3409 /* Calls static void SecurityManager.InternalDemandChoice (byte* permissions, int size); */
3410 mono_emit_method_call_spilled (cfg, init_localsbb, secman->demandchoice, mono_method_signature (secman->demandchoice), args, ip, NULL);
3414 /* we must Demand SecurityPermission.Unmanaged before p/invoking */
3416 mono_emit_method_call_spilled (cfg, init_localsbb, secman->demandunmanaged, mono_method_signature (secman->demandunmanaged), NULL, ip, NULL);
3419 if (get_basic_blocks (cfg, bbhash, header, real_offset, ip, end, &err_pos)) {
3424 if (cfg->method == method)
3425 mono_debug_init_method (cfg, bblock, breakpoint_id);
3427 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
3429 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
3430 for (n = 0; n < sig->param_count; ++n)
3431 param_types [n + sig->hasthis] = sig->params [n];
3434 /* do this somewhere outside - not here */
3435 NEW_ICONST (cfg, zero_int32, 0);
3436 NEW_ICONST (cfg, zero_int64, 0);
3437 zero_int64->type = STACK_I8;
3438 NEW_PCONST (cfg, zero_ptr, 0);
3439 NEW_PCONST (cfg, zero_obj, 0);
3440 zero_obj->type = STACK_OBJ;
3442 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
3443 zero_r8->type = STACK_R8;
3444 zero_r8->inst_p0 = &r8_0;
3446 /* add a check for this != NULL to inlined methods */
3447 if (is_virtual_call) {
3448 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
3449 NEW_ARGLOAD (cfg, ins->inst_left, 0);
3451 MONO_ADD_INS (bblock, ins);
3454 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
3455 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
3458 start_new_bblock = 0;
3461 if (cfg->method == method)
3462 real_offset = ip - header->code;
3464 real_offset = inline_offset;
3466 if (start_new_bblock) {
3467 bblock->cil_length = ip - bblock->cil_code;
3468 if (start_new_bblock == 2) {
3469 g_assert (ip == tblock->cil_code);
3471 GET_BBLOCK (cfg, bbhash, tblock, ip);
3473 bblock->next_bb = tblock;
3475 start_new_bblock = 0;
3476 for (i = 0; i < bblock->in_scount; ++i) {
3477 if (cfg->verbose_level > 3)
3478 g_print ("loading %d from temp %d\n", i, (int)bblock->in_stack [i]->inst_c0);
3479 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
3482 g_slist_free (class_inits);
3485 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
3486 link_bblock (cfg, bblock, tblock);
3487 if (sp != stack_start) {
3488 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3491 bblock->next_bb = tblock;
3493 for (i = 0; i < bblock->in_scount; ++i) {
3494 if (cfg->verbose_level > 3)
3495 g_print ("loading %d from temp %d\n", i, (int)bblock->in_stack [i]->inst_c0);
3496 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
3499 g_slist_free (class_inits);
3504 bblock->real_offset = real_offset;
3506 if ((cfg->method == method) && cfg->coverage_info) {
3507 MonoInst *store, *one;
3508 guint32 cil_offset = ip - header->code;
3509 cfg->coverage_info->data [cil_offset].cil_code = ip;
3511 /* TODO: Use an increment here */
3512 NEW_ICONST (cfg, one, 1);
3515 NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
3518 MONO_INST_NEW (cfg, store, CEE_STIND_I);
3519 store->cil_code = ip;
3520 store->inst_left = ins;
3521 store->inst_right = one;
3523 MONO_ADD_INS (bblock, store);
3526 if (cfg->verbose_level > 3)
3527 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
3534 MONO_INST_NEW (cfg, ins, CEE_BREAK);
3535 ins->cil_code = ip++;
3536 MONO_ADD_INS (bblock, ins);
3542 CHECK_STACK_OVF (1);
3543 n = (*ip)-CEE_LDARG_0;
3545 NEW_ARGLOAD (cfg, ins, n);
3546 ins->cil_code = ip++;
3553 CHECK_STACK_OVF (1);
3554 n = (*ip)-CEE_LDLOC_0;
3556 NEW_LOCLOAD (cfg, ins, n);
3557 ins->cil_code = ip++;
3565 n = (*ip)-CEE_STLOC_0;
3568 handle_loaded_temps (cfg, bblock, stack_start, sp);
3569 NEW_LOCSTORE (cfg, ins, n, *sp);
3571 if (ins->opcode == CEE_STOBJ) {
3572 NEW_LOCLOADA (cfg, ins, n);
3573 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3575 MONO_ADD_INS (bblock, ins);
3581 CHECK_STACK_OVF (1);
3583 NEW_ARGLOAD (cfg, ins, ip [1]);
3590 CHECK_STACK_OVF (1);
3592 NEW_ARGLOADA (cfg, ins, ip [1]);
3602 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
3603 handle_loaded_temps (cfg, bblock, stack_start, sp);
3605 if (ins->opcode == CEE_STOBJ) {
3606 NEW_ARGLOADA (cfg, ins, ip [1]);
3607 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3609 MONO_ADD_INS (bblock, ins);
3614 CHECK_STACK_OVF (1);
3615 CHECK_LOCAL (ip [1]);
3616 NEW_LOCLOAD (cfg, ins, ip [1]);
3623 CHECK_STACK_OVF (1);
3624 CHECK_LOCAL (ip [1]);
3625 NEW_LOCLOADA (cfg, ins, ip [1]);
3634 handle_loaded_temps (cfg, bblock, stack_start, sp);
3635 CHECK_LOCAL (ip [1]);
3636 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
3638 if (ins->opcode == CEE_STOBJ) {
3639 NEW_LOCLOADA (cfg, ins, ip [1]);
3640 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3642 MONO_ADD_INS (bblock, ins);
3647 CHECK_STACK_OVF (1);
3648 NEW_PCONST (cfg, ins, NULL);
3650 ins->type = STACK_OBJ;
3655 CHECK_STACK_OVF (1);
3656 NEW_ICONST (cfg, ins, -1);
3670 CHECK_STACK_OVF (1);
3671 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
3678 CHECK_STACK_OVF (1);
3680 NEW_ICONST (cfg, ins, *((signed char*)ip));
3687 CHECK_STACK_OVF (1);
3688 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
3695 CHECK_STACK_OVF (1);
3696 MONO_INST_NEW (cfg, ins, OP_I8CONST);
3698 ins->type = STACK_I8;
3700 ins->inst_l = (gint64)read64 (ip);
3706 /* we should really allocate this only late in the compilation process */
3707 mono_domain_lock (cfg->domain);
3708 f = mono_mempool_alloc (cfg->domain->mp, sizeof (float));
3709 mono_domain_unlock (cfg->domain);
3711 CHECK_STACK_OVF (1);
3712 MONO_INST_NEW (cfg, ins, OP_R4CONST);
3713 ins->type = STACK_R8;
3724 mono_domain_lock (cfg->domain);
3725 d = mono_mempool_alloc (cfg->domain->mp, sizeof (double));
3726 mono_domain_unlock (cfg->domain);
3728 CHECK_STACK_OVF (1);
3729 MONO_INST_NEW (cfg, ins, OP_R8CONST);
3730 ins->type = STACK_R8;
3740 MonoInst *temp, *store;
3742 CHECK_STACK_OVF (1);
3747 * small optimization: if the loaded value was from a local already,
3748 * just load it twice.
3750 if (ins->ssa_op == MONO_SSA_LOAD &&
3751 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
3753 MONO_INST_NEW (cfg, temp, 0);
3755 temp->cil_code = ip;
3758 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3759 temp->flags |= MONO_INST_IS_TEMP;
3760 temp->cil_code = ip;
3761 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3762 store->cil_code = ip;
3763 if (store->opcode == CEE_STOBJ) {
3764 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
3765 handle_stobj (cfg, bblock, store, sp [0], sp [0]->cil_code, store->klass, TRUE, FALSE);
3767 MONO_ADD_INS (bblock, store);
3769 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3772 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3782 MONO_INST_NEW (cfg, ins, CEE_POP);
3783 MONO_ADD_INS (bblock, ins);
3784 ins->cil_code = ip++;
3790 if (stack_start != sp)
3792 MONO_INST_NEW (cfg, ins, CEE_JMP);
3793 token = read32 (ip + 1);
3794 /* FIXME: check the signature matches */
3795 cmethod = mini_get_method (image, token, NULL, generic_context);
3797 if (mono_use_security_manager) {
3798 check_linkdemand (cfg, method, cmethod, bblock, ip);
3801 ins->inst_p0 = cmethod;
3802 MONO_ADD_INS (bblock, ins);
3804 start_new_bblock = 1;
3808 case CEE_CALLVIRT: {
3809 MonoInst *addr = NULL;
3810 MonoMethodSignature *fsig = NULL;
3811 int temp, array_rank = 0;
3812 int virtual = *ip == CEE_CALLVIRT;
3815 token = read32 (ip + 1);
3817 if (*ip == CEE_CALLI) {
3822 if (method->wrapper_type != MONO_WRAPPER_NONE)
3823 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
3825 fsig = mono_metadata_parse_signature (image, token);
3827 n = fsig->param_count + fsig->hasthis;
3829 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3830 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
3831 } else if (constrained_call) {
3832 cmethod = mono_get_method_constrained (image, token, constrained_call, generic_context);
3833 cmethod = mono_get_inflated_method (cmethod);
3835 cmethod = mini_get_method (image, token, NULL, generic_context);
3840 if (!virtual && (cmethod->flags & METHOD_ATTRIBUTE_ABSTRACT))
3843 if (!cmethod->klass->inited)
3844 mono_class_init (cmethod->klass);
3846 if (mono_method_signature (cmethod)->pinvoke) {
3847 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
3848 fsig = mono_method_signature (wrapper);
3849 } else if (constrained_call) {
3850 fsig = mono_method_signature (cmethod);
3852 fsig = mono_method_get_signature_full (cmethod, image, token, generic_context);
3855 n = fsig->param_count + fsig->hasthis;
3857 if (mono_use_security_manager) {
3858 check_linkdemand (cfg, method, cmethod, bblock, ip);
3861 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
3862 mini_class_is_system_array (cmethod->klass)) {
3863 array_rank = cmethod->klass->rank;
3866 if (cmethod->string_ctor)
3867 g_assert_not_reached ();
3872 mono_get_got_var (cfg);
3874 /* code in inssel.brg might transform a virtual call to a normal call */
3875 if (!(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
3876 ((cmethod->flags & METHOD_ATTRIBUTE_FINAL) &&
3877 cmethod->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK))
3878 mono_get_got_var (cfg);
3881 if (cmethod && cmethod->klass->generic_container) {
3888 //g_assert (!virtual || fsig->hasthis);
3892 if (constrained_call) {
3894 * We have the `constrained.' prefix opcode.
3896 if (constrained_call->valuetype && !cmethod->klass->valuetype) {
3899 * The type parameter is instantiated as a valuetype,
3900 * but that type doesn't override the method we're
3901 * calling, so we need to box `this'.
3902 * sp [0] is a pointer to the data: we need the value
3903 * in handle_box (), so load it here.
3905 MONO_INST_NEW (cfg, load, mono_type_to_ldind (&constrained_call->byval_arg));
3906 type_to_eval_stack_type (&constrained_call->byval_arg, load);
3907 load->cil_code = ip;
3908 load->inst_left = sp [0];
3909 sp [0] = handle_box (cfg, bblock, load, ip, constrained_call);
3910 } else if (!constrained_call->valuetype) {
3914 * The type parameter is instantiated as a reference
3915 * type. We have a managed pointer on the stack, so
3916 * we need to dereference it here.
3919 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3921 ins->inst_i0 = sp [0];
3922 ins->type = STACK_OBJ;
3924 } else if (cmethod->klass->valuetype)
3926 constrained_call = NULL;
3929 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp)) {
3934 if (cmethod && virtual && mono_method_signature (cmethod)->generic_param_count) {
3935 MonoInst *this_temp, *store;
3936 MonoInst *iargs [3];
3938 g_assert (mono_method_signature (cmethod)->is_inflated);
3940 this_temp = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
3941 this_temp->cil_code = ip;
3942 NEW_TEMPSTORE (cfg, store, this_temp->inst_c0, sp [0]);
3944 store->cil_code = ip;
3945 MONO_ADD_INS (bblock, store);
3947 NEW_TEMPLOAD (cfg, iargs [0], this_temp->inst_c0);
3948 NEW_PCONST (cfg, iargs [1], cmethod);
3949 NEW_PCONST (cfg, iargs [2], ((MonoMethodInflated *) cmethod)->context);
3950 temp = mono_emit_jit_icall (cfg, bblock, helper_compile_generic_method, iargs, ip);
3952 NEW_TEMPLOAD (cfg, addr, temp);
3953 NEW_TEMPLOAD (cfg, sp [0], this_temp->inst_c0);
3955 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
3956 NEW_TEMPLOAD (cfg, *sp, temp);
3964 if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL) &&
3965 (mono_metadata_signature_equal (mono_method_signature (method), mono_method_signature (cmethod)))) {
3967 /* FIXME: This assumes the two methods has the same number and type of arguments */
3968 for (i = 0; i < n; ++i) {
3969 /* Check if argument is the same */
3970 NEW_ARGLOAD (cfg, ins, i);
3971 if ((ins->opcode == sp [i]->opcode) && (ins->inst_i0 == sp [i]->inst_i0))
3974 /* Prevent argument from being register allocated */
3975 arg_array [i]->flags |= MONO_INST_VOLATILE;
3976 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3978 if (ins->opcode == CEE_STOBJ) {
3979 NEW_ARGLOADA (cfg, ins, i);
3980 handle_stobj (cfg, bblock, ins, sp [i], sp [i]->cil_code, ins->klass, FALSE, FALSE);
3983 MONO_ADD_INS (bblock, ins);
3985 MONO_INST_NEW (cfg, ins, CEE_JMP);
3987 ins->inst_p0 = cmethod;
3988 ins->inst_p1 = arg_array [0];
3989 MONO_ADD_INS (bblock, ins);
3990 link_bblock (cfg, bblock, end_bblock);
3991 start_new_bblock = 1;
3992 /* skip CEE_RET as well */
3997 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_inst_for_method (cfg, cmethod, fsig, sp))) {
4000 if (MONO_TYPE_IS_VOID (fsig->ret)) {
4001 MONO_ADD_INS (bblock, ins);
4003 type_to_eval_stack_type (fsig->ret, ins);
4012 handle_loaded_temps (cfg, bblock, stack_start, sp);
4014 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
4015 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
4016 mono_method_check_inlining (cfg, cmethod) &&
4017 !g_list_find (dont_inline, cmethod)) {
4019 MonoBasicBlock *ebblock;
4020 gboolean allways = FALSE;
4022 if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
4023 (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
4024 cmethod = mono_marshal_get_native_wrapper (cmethod);
4028 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, allways))) {
4032 GET_BBLOCK (cfg, bbhash, bblock, ip);
4033 ebblock->next_bb = bblock;
4034 link_bblock (cfg, ebblock, bblock);
4036 if (!MONO_TYPE_IS_VOID (fsig->ret))
4039 /* indicates start of a new block, and triggers a load of all
4040 stack arguments at bb boundarie */
4043 inline_costs += costs;
4048 inline_costs += 10 * num_calls++;
4050 /* tail recursion elimination */
4051 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == method && ip [5] == CEE_RET) {
4052 gboolean has_vtargs = FALSE;
4055 /* keep it simple */
4056 for (i = fsig->param_count - 1; i >= 0; i--) {
4057 if (MONO_TYPE_ISSTRUCT (mono_method_signature (cmethod)->params [i]))
4062 for (i = 0; i < n; ++i) {
4063 NEW_ARGSTORE (cfg, ins, i, sp [i]);
4065 MONO_ADD_INS (bblock, ins);
4067 MONO_INST_NEW (cfg, ins, CEE_BR);
4069 MONO_ADD_INS (bblock, ins);
4070 tblock = start_bblock->out_bb [0];
4071 link_bblock (cfg, bblock, tblock);
4072 ins->inst_target_bb = tblock;
4073 start_new_bblock = 1;
4075 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
4076 /* just create a dummy - the value is never used */
4077 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
4078 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
4080 MONO_INST_NEW (cfg, ins, CEE_POP);
4081 MONO_ADD_INS (bblock, ins);
4085 /* skip the CEE_RET, too */
4086 if (ip_in_bb (cfg, bblock, ip + 5))
4095 if (*ip == CEE_CALLI) {
4097 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
4098 NEW_TEMPLOAD (cfg, *sp, temp);
4102 } else if (array_rank) {
4105 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
4106 if (sp [fsig->param_count]->type == STACK_OBJ) {
4107 MonoInst *iargs [2];
4108 MonoInst *array, *to_store, *store;
4110 handle_loaded_temps (cfg, bblock, stack_start, sp);
4112 array = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
4113 NEW_TEMPSTORE (cfg, store, array->inst_c0, sp [0]);
4114 store->cil_code = ip;
4115 MONO_ADD_INS (bblock, store);
4116 NEW_TEMPLOAD (cfg, iargs [0], array->inst_c0);
4118 to_store = mono_compile_create_var (cfg, type_from_stack_type (sp [fsig->param_count]), OP_LOCAL);
4119 NEW_TEMPSTORE (cfg, store, to_store->inst_c0, sp [fsig->param_count]);
4120 store->cil_code = ip;
4121 MONO_ADD_INS (bblock, store);
4122 NEW_TEMPLOAD (cfg, iargs [1], to_store->inst_c0);
4125 * We first save the args for the call so that the args are copied to the stack
4126 * and a new instruction tree for them is created. If we don't do this,
4127 * the same MonoInst is added to two different trees and this is not
4130 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref_check, iargs, ip);
4132 NEW_TEMPLOAD (cfg, sp [0], array->inst_c0);
4133 NEW_TEMPLOAD (cfg, sp [fsig->param_count], to_store->inst_c0);
4136 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
4137 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
4139 if (ins->opcode == CEE_STOBJ) {
4140 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
4142 MONO_ADD_INS (bblock, ins);
4145 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
4146 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
4147 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
4151 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
4152 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
4155 g_assert_not_reached ();
4159 if (ip_in_bb (cfg, bblock, ip + 5)
4160 && (!MONO_TYPE_ISSTRUCT (fsig->ret))
4161 && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)
4162 && (CODE_IS_STLOC (ip + 5) || ip [5] == CEE_POP || ip [5] == CEE_BRTRUE || ip [5] == CEE_BRFALSE ||
4163 ip [5] == CEE_BRTRUE_S || ip [5] == CEE_BRFALSE_S || ip [5] == CEE_RET)) {
4164 /* no need to spill */
4165 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
4168 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) {
4169 NEW_TEMPLOAD (cfg, *sp, temp);
4179 if (cfg->method != method) {
4180 /* return from inlined methode */
4185 //g_assert (returnvar != -1);
4186 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
4187 store->cil_code = sp [0]->cil_code;
4188 if (store->opcode == CEE_STOBJ) {
4189 g_assert_not_reached ();
4190 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
4191 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
4193 MONO_ADD_INS (bblock, store);
4197 g_assert (!return_var);
4200 MONO_INST_NEW (cfg, ins, CEE_NOP);
4201 ins->opcode = mono_type_to_stind (mono_method_signature (method)->ret);
4202 if (ins->opcode == CEE_STOBJ) {
4203 NEW_RETLOADA (cfg, ins);
4204 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4206 ins->opcode = OP_SETRET;
4208 ins->inst_i0 = *sp;;
4209 ins->inst_i1 = NULL;
4210 MONO_ADD_INS (bblock, ins);
4214 if (sp != stack_start)
4216 MONO_INST_NEW (cfg, ins, CEE_BR);
4217 ins->cil_code = ip++;
4218 ins->inst_target_bb = end_bblock;
4219 MONO_ADD_INS (bblock, ins);
4220 link_bblock (cfg, bblock, end_bblock);
4221 start_new_bblock = 1;
4225 MONO_INST_NEW (cfg, ins, CEE_BR);
4226 ins->cil_code = ip++;
4227 MONO_ADD_INS (bblock, ins);
4228 target = ip + 1 + (signed char)(*ip);
4230 GET_BBLOCK (cfg, bbhash, tblock, target);
4231 link_bblock (cfg, bblock, tblock);
4232 CHECK_BBLOCK (target, ip, tblock);
4233 ins->inst_target_bb = tblock;
4234 if (sp != stack_start) {
4235 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4238 start_new_bblock = 1;
4245 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
4246 ins->cil_code = ip++;
4247 target = ip + 1 + *(signed char*)ip;
4249 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
4250 if (sp != stack_start) {
4251 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4268 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
4269 ins->cil_code = ip++;
4270 target = ip + 1 + *(signed char*)ip;
4273 if (sp != stack_start) {
4274 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4281 MONO_INST_NEW (cfg, ins, CEE_BR);
4282 ins->cil_code = ip++;
4283 MONO_ADD_INS (bblock, ins);
4284 target = ip + 4 + (gint32)read32(ip);
4286 GET_BBLOCK (cfg, bbhash, tblock, target);
4287 link_bblock (cfg, bblock, tblock);
4288 CHECK_BBLOCK (target, ip, tblock);
4289 ins->inst_target_bb = tblock;
4290 if (sp != stack_start) {
4291 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4294 start_new_bblock = 1;
4301 MONO_INST_NEW (cfg, ins, *ip);
4302 ins->cil_code = ip++;
4303 target = ip + 4 + (gint32)read32(ip);
4305 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
4306 if (sp != stack_start) {
4307 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4324 MONO_INST_NEW (cfg, ins, *ip);
4325 ins->cil_code = ip++;
4326 target = ip + 4 + (gint32)read32(ip);
4329 if (sp != stack_start) {
4330 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4338 n = read32 (ip + 1);
4339 MONO_INST_NEW (cfg, ins, *ip);
4341 ins->inst_left = *sp;
4342 if ((ins->inst_left->type != STACK_I4) && (ins->inst_left->type != STACK_PTR))
4346 CHECK_OPSIZE (n * sizeof (guint32));
4347 target = ip + n * sizeof (guint32);
4348 MONO_ADD_INS (bblock, ins);
4349 GET_BBLOCK (cfg, bbhash, tblock, target);
4350 link_bblock (cfg, bblock, tblock);
4351 ins->klass = GUINT_TO_POINTER (n);
4352 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
4353 ins->inst_many_bb [n] = tblock;
4355 for (i = 0; i < n; ++i) {
4356 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
4357 link_bblock (cfg, bblock, tblock);
4358 ins->inst_many_bb [i] = tblock;
4361 if (sp != stack_start) {
4362 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4365 /* Needed by the code generated in inssel.brg */
4366 mono_get_got_var (cfg);
4381 MONO_INST_NEW (cfg, ins, *ip);
4386 ins->type = ldind_type [*ip - CEE_LDIND_I1];
4387 ins->flags |= ins_flag;
4399 MONO_INST_NEW (cfg, ins, *ip);
4400 ins->cil_code = ip++;
4402 handle_loaded_temps (cfg, bblock, stack_start, sp);
4403 MONO_ADD_INS (bblock, ins);
4404 ins->inst_i0 = sp [0];
4405 ins->inst_i1 = sp [1];
4406 ins->flags |= ins_flag;
4425 /* special case that gives a nice speedup and happens to workaorund a ppc jit but (for the release)
4426 * later apply the speedup to the left shift as well
4429 if ((ins->opcode == OP_LSHR_UN) && (ins->type == STACK_I8)
4430 && (ins->inst_right->opcode == OP_ICONST) && (ins->inst_right->inst_c0 == 32)) {
4431 ins->opcode = OP_LONG_SHRUN_32;
4432 /*g_print ("applied long shr speedup to %s\n", cfg->method->name);*/
4436 if (mono_find_jit_opcode_emulation (ins->opcode)) {
4438 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
4439 mono_get_got_var (cfg);
4453 case CEE_CONV_OVF_I8:
4454 case CEE_CONV_OVF_U8:
4458 if (mono_find_jit_opcode_emulation (ins->opcode)) {
4460 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
4461 mono_get_got_var (cfg);
4465 case CEE_CONV_OVF_I4:
4466 case CEE_CONV_OVF_I1:
4467 case CEE_CONV_OVF_I2:
4468 case CEE_CONV_OVF_I:
4469 case CEE_CONV_OVF_U:
4472 if (sp [-1]->type == STACK_R8) {
4473 ADD_UNOP (CEE_CONV_OVF_I8);
4481 case CEE_CONV_OVF_U1:
4482 case CEE_CONV_OVF_U2:
4483 case CEE_CONV_OVF_U4:
4486 if (sp [-1]->type == STACK_R8) {
4487 ADD_UNOP (CEE_CONV_OVF_U8);
4495 case CEE_CONV_OVF_I1_UN:
4496 case CEE_CONV_OVF_I2_UN:
4497 case CEE_CONV_OVF_I4_UN:
4498 case CEE_CONV_OVF_I8_UN:
4499 case CEE_CONV_OVF_U1_UN:
4500 case CEE_CONV_OVF_U2_UN:
4501 case CEE_CONV_OVF_U4_UN:
4502 case CEE_CONV_OVF_U8_UN:
4503 case CEE_CONV_OVF_I_UN:
4504 case CEE_CONV_OVF_U_UN:
4512 token = read32 (ip + 1);
4513 if (method->wrapper_type != MONO_WRAPPER_NONE)
4514 klass = mono_method_get_wrapper_data (method, token);
4516 klass = mono_class_get_full (image, token, generic_context);
4518 mono_class_init (klass);
4520 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4521 MonoInst *store, *load;
4522 MONO_INST_NEW (cfg, load, CEE_LDIND_REF);
4523 load->cil_code = ip;
4524 load->inst_i0 = sp [1];
4525 load->type = STACK_OBJ;
4526 load->flags |= ins_flag;
4527 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
4528 store->cil_code = ip;
4529 handle_loaded_temps (cfg, bblock, stack_start, sp);
4530 MONO_ADD_INS (bblock, store);
4531 store->inst_i0 = sp [0];
4532 store->inst_i1 = load;
4533 store->flags |= ins_flag;
4535 n = mono_class_value_size (klass, NULL);
4536 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
4538 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
4539 copy->inst_left = sp [0];
4540 copy->inst_right = sp [1];
4541 copy->cil_code = ip;
4543 MONO_ADD_INS (bblock, copy);
4545 MonoMethod *memcpy_method = get_memcpy_method ();
4546 MonoInst *iargs [3];
4549 NEW_ICONST (cfg, iargs [2], n);
4550 iargs [2]->cil_code = ip;
4552 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
4559 MonoInst *iargs [3];
4565 token = read32 (ip + 1);
4566 if (method->wrapper_type != MONO_WRAPPER_NONE)
4567 klass = mono_method_get_wrapper_data (method, token);
4569 klass = mono_class_get_full (image, token, generic_context);
4571 mono_class_init (klass);
4572 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4573 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
4575 ins->inst_i0 = sp [0];
4576 ins->type = STACK_OBJ;
4577 ins->flags |= ins_flag;
4584 /* Optimize the common ldobj+stloc combination */
4594 loc_index = ip [5] - CEE_STLOC_0;
4601 if ((loc_index != -1) && ip_in_bb (cfg, bblock, ip + 5)) {
4602 CHECK_LOCAL (loc_index);
4603 NEW_LOCSTORE (cfg, ins, loc_index, *sp);
4605 if (ins->opcode == CEE_STOBJ) {
4606 handle_loaded_temps (cfg, bblock, stack_start, sp);
4608 g_assert (ins->opcode == CEE_STOBJ);
4609 NEW_LOCLOADA (cfg, ins, loc_index);
4610 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4617 n = mono_class_value_size (klass, NULL);
4618 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
4619 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
4620 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
4622 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
4623 copy->inst_left = iargs [0];
4624 copy->inst_right = *sp;
4625 copy->cil_code = ip;
4627 MONO_ADD_INS (bblock, copy);
4629 MonoMethod *memcpy_method = get_memcpy_method ();
4631 NEW_ICONST (cfg, iargs [2], n);
4632 iargs [2]->cil_code = ip;
4634 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
4636 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
4644 CHECK_STACK_OVF (1);
4646 n = read32 (ip + 1);
4648 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
4649 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, n));
4651 ins->type = STACK_OBJ;
4654 else if (method->wrapper_type != MONO_WRAPPER_NONE) {
4656 MonoInst *iargs [1];
4658 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
4659 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
4660 NEW_TEMPLOAD (cfg, *sp, temp);
4664 if (cfg->opt & MONO_OPT_SHARED) {
4666 MonoInst *iargs [3];
4667 MonoInst* domain_var;
4669 if (cfg->compile_aot) {
4670 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, GINT_TO_POINTER (n));
4672 /* avoid depending on undefined C behavior in sequence points */
4673 domain_var = mono_get_domainvar (cfg);
4674 NEW_TEMPLOAD (cfg, iargs [0], domain_var->inst_c0);
4675 NEW_IMAGECONST (cfg, iargs [1], image);
4676 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
4677 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
4678 NEW_TEMPLOAD (cfg, *sp, temp);
4679 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
4681 if (cfg->compile_aot)
4682 NEW_LDSTRCONST (cfg, ins, image, n);
4684 NEW_PCONST (cfg, ins, NULL);
4686 ins->type = STACK_OBJ;
4687 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
4697 MonoInst *iargs [2];
4698 MonoMethodSignature *fsig;
4702 token = read32 (ip + 1);
4703 if (method->wrapper_type != MONO_WRAPPER_NONE) {
4704 cmethod = mono_method_get_wrapper_data (method, token);
4706 cmethod = mini_get_method (image, token, NULL, generic_context);
4707 fsig = mono_method_get_signature (cmethod, image, token);
4709 mono_class_init (cmethod->klass);
4711 if (mono_use_security_manager) {
4712 check_linkdemand (cfg, method, cmethod, bblock, ip);
4715 n = fsig->param_count;
4718 /* move the args to allow room for 'this' in the first position */
4724 handle_loaded_temps (cfg, bblock, stack_start, sp);
4726 if (mini_class_is_system_array (cmethod->klass)) {
4727 NEW_METHODCONST (cfg, *sp, cmethod);
4728 temp = handle_array_new (cfg, bblock, fsig->param_count, sp, ip);
4729 } else if (cmethod->string_ctor) {
4730 /* we simply pass a null pointer */
4731 NEW_PCONST (cfg, *sp, NULL);
4732 /* now call the string ctor */
4733 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
4735 MonoInst* callvirt_this_arg = NULL;
4737 if (cmethod->klass->valuetype) {
4738 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
4739 temp = iargs [0]->inst_c0;
4741 NEW_TEMPLOADA (cfg, *sp, temp);
4743 handle_initobj (cfg, bblock, *sp, NULL, cmethod->klass, stack_start, sp);
4745 NEW_TEMPLOADA (cfg, *sp, temp);
4748 * The code generated by mini_emit_virtual_call () expects
4749 * iargs [0] to be a boxed instance, but luckily the vcall
4750 * will be transformed into a normal call there. The AOT
4751 * case needs an already allocate got_var.
4753 mono_get_got_var (cfg);
4755 temp = handle_alloc (cfg, bblock, cmethod->klass, FALSE, ip);
4756 NEW_TEMPLOAD (cfg, *sp, temp);
4759 /* Avoid virtual calls to ctors if possible */
4760 if (cmethod->klass->marshalbyref)
4761 callvirt_this_arg = sp [0];
4763 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
4764 mono_method_check_inlining (cfg, cmethod) &&
4765 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
4766 !g_list_find (dont_inline, cmethod)) {
4768 MonoBasicBlock *ebblock;
4769 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, FALSE))) {
4774 GET_BBLOCK (cfg, bbhash, bblock, ip);
4775 ebblock->next_bb = bblock;
4776 link_bblock (cfg, ebblock, bblock);
4778 NEW_TEMPLOAD (cfg, *sp, temp);
4781 /* indicates start of a new block, and triggers a load
4782 of all stack arguments at bb boundarie */
4785 inline_costs += costs;
4789 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, callvirt_this_arg);
4792 /* now call the actual ctor */
4793 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, callvirt_this_arg);
4797 NEW_TEMPLOAD (cfg, *sp, temp);
4808 token = read32 (ip + 1);
4809 if (method->wrapper_type != MONO_WRAPPER_NONE)
4810 klass = mono_method_get_wrapper_data (method, token);
4812 klass = mono_class_get_full (image, token, generic_context);
4813 mono_class_init (klass);
4815 /* Needed by the code generated in inssel.brg */
4816 mono_get_got_var (cfg);
4818 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4820 MonoMethod *mono_isinst;
4821 MonoInst *iargs [1];
4822 MonoBasicBlock *ebblock;
4826 mono_isinst = mono_marshal_get_isinst (klass);
4829 costs = inline_method (cfg, mono_isinst, mono_method_signature (mono_isinst), bblock,
4830 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4832 g_assert (costs > 0);
4837 GET_BBLOCK (cfg, bbhash, bblock, ip);
4838 ebblock->next_bb = bblock;
4839 link_bblock (cfg, ebblock, bblock);
4841 temp = iargs [0]->inst_i0->inst_c0;
4842 NEW_TEMPLOAD (cfg, *sp, temp);
4846 inline_costs += costs;
4850 MONO_INST_NEW (cfg, ins, *ip);
4851 ins->type = STACK_OBJ;
4852 ins->inst_left = *sp;
4853 ins->inst_newa_class = klass;
4855 *sp++ = emit_tree (cfg, bblock, ins, ip + 5);
4859 case CEE_UNBOX_ANY: {
4860 MonoInst *add, *vtoffset;
4861 MonoInst *iargs [3];
4866 token = read32 (ip + 1);
4867 if (method->wrapper_type != MONO_WRAPPER_NONE)
4868 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4870 klass = mono_class_get_full (image, token, generic_context);
4871 mono_class_init (klass);
4873 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4875 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4876 MonoMethod *mono_castclass;
4877 MonoInst *iargs [1];
4878 MonoBasicBlock *ebblock;
4882 mono_castclass = mono_marshal_get_castclass (klass);
4885 costs = inline_method (cfg, mono_castclass, mono_method_signature (mono_castclass), bblock,
4886 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4888 g_assert (costs > 0);
4893 GET_BBLOCK (cfg, bbhash, bblock, ip);
4894 ebblock->next_bb = bblock;
4895 link_bblock (cfg, ebblock, bblock);
4897 temp = iargs [0]->inst_i0->inst_c0;
4898 NEW_TEMPLOAD (cfg, *sp, temp);
4902 inline_costs += costs;
4905 MONO_INST_NEW (cfg, ins, CEE_CASTCLASS);
4906 ins->type = STACK_OBJ;
4907 ins->inst_left = *sp;
4909 ins->inst_newa_class = klass;
4917 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
4918 ins->type = STACK_OBJ;
4919 ins->inst_left = *sp;
4921 ins->inst_newa_class = klass;
4924 MONO_INST_NEW (cfg, add, OP_PADD);
4925 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4926 add->inst_left = ins;
4927 add->inst_right = vtoffset;
4928 add->type = STACK_MP;
4932 n = mono_class_value_size (klass, NULL);
4933 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
4934 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
4935 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
4937 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
4938 copy->inst_left = iargs [0];
4939 copy->inst_right = *sp;
4940 copy->cil_code = ip;
4942 MONO_ADD_INS (bblock, copy);
4944 MonoMethod *memcpy_method = get_memcpy_method ();
4946 NEW_ICONST (cfg, iargs [2], n);
4947 iargs [2]->cil_code = ip;
4949 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
4951 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
4957 MonoInst *add, *vtoffset;
4962 token = read32 (ip + 1);
4963 if (method->wrapper_type != MONO_WRAPPER_NONE)
4964 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4966 klass = mono_class_get_full (image, token, generic_context);
4967 mono_class_init (klass);
4969 /* Needed by the code generated in inssel.brg */
4970 mono_get_got_var (cfg);
4972 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
4973 ins->type = STACK_OBJ;
4974 ins->inst_left = *sp;
4976 ins->inst_newa_class = klass;
4979 MONO_INST_NEW (cfg, add, OP_PADD);
4980 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4981 add->inst_left = ins;
4982 add->inst_right = vtoffset;
4983 add->type = STACK_MP;
4993 token = read32 (ip + 1);
4994 if (method->wrapper_type != MONO_WRAPPER_NONE)
4995 klass = mono_method_get_wrapper_data (method, token);
4997 klass = mono_class_get_full (image, token, generic_context);
4998 mono_class_init (klass);
5000 /* Needed by the code generated in inssel.brg */
5001 mono_get_got_var (cfg);
5003 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
5005 MonoMethod *mono_castclass;
5006 MonoInst *iargs [1];
5007 MonoBasicBlock *ebblock;
5011 mono_castclass = mono_marshal_get_castclass (klass);
5014 costs = inline_method (cfg, mono_castclass, mono_method_signature (mono_castclass), bblock,
5015 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
5017 g_assert (costs > 0);
5022 GET_BBLOCK (cfg, bbhash, bblock, ip);
5023 ebblock->next_bb = bblock;
5024 link_bblock (cfg, ebblock, bblock);
5026 temp = iargs [0]->inst_i0->inst_c0;
5027 NEW_TEMPLOAD (cfg, *sp, temp);
5031 inline_costs += costs;
5034 MONO_INST_NEW (cfg, ins, *ip);
5035 ins->type = STACK_OBJ;
5036 ins->inst_left = *sp;
5038 ins->inst_newa_class = klass;
5040 *sp++ = emit_tree (cfg, bblock, ins, ip + 5);
5046 MONO_INST_NEW (cfg, ins, *ip);
5048 ins->inst_left = *sp;
5049 ins->cil_code = ip++;
5050 bblock->out_of_line = TRUE;
5051 MONO_ADD_INS (bblock, ins);
5052 MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
5053 ins->cil_code = ip - 1;
5054 MONO_ADD_INS (bblock, ins);
5056 link_bblock (cfg, bblock, end_bblock);
5057 start_new_bblock = 1;
5058 mono_get_got_var (cfg);
5063 MonoInst *offset_ins;
5064 MonoClassField *field;
5065 MonoBasicBlock *ebblock;
5069 if (*ip == CEE_STFLD) {
5076 // FIXME: enable this test later.
5077 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
5080 token = read32 (ip + 1);
5081 field = mono_field_from_token (image, token, &klass, generic_context);
5082 mono_class_init (klass);
5084 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
5085 /* FIXME: mark instructions for use in SSA */
5086 if (*ip == CEE_STFLD) {
5087 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound || klass == mono_defaults.marshalbyrefobject_class) {
5088 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
5089 MonoInst *iargs [5];
5092 NEW_CLASSCONST (cfg, iargs [1], klass);
5093 NEW_FIELDCONST (cfg, iargs [2], field);
5094 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
5098 if (cfg->opt & MONO_OPT_INLINE) {
5099 costs = inline_method (cfg, stfld_wrapper, mono_method_signature (stfld_wrapper), bblock,
5100 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
5101 g_assert (costs > 0);
5106 GET_BBLOCK (cfg, bbhash, bblock, ip);
5107 ebblock->next_bb = bblock;
5108 link_bblock (cfg, ebblock, bblock);
5110 /* indicates start of a new block, and triggers a load
5111 of all stack arguments at bb boundarie */
5114 inline_costs += costs;
5117 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, mono_method_signature (stfld_wrapper), iargs, ip, NULL);
5121 NEW_ICONST (cfg, offset_ins, foffset);
5122 MONO_INST_NEW (cfg, ins, OP_PADD);
5124 ins->inst_left = *sp;
5125 ins->inst_right = offset_ins;
5126 ins->type = STACK_MP;
5128 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
5129 store->cil_code = ip;
5130 store->inst_left = ins;
5131 store->inst_right = sp [1];
5132 handle_loaded_temps (cfg, bblock, stack_start, sp);
5133 store->flags |= ins_flag;
5135 if (store->opcode == CEE_STOBJ) {
5136 handle_stobj (cfg, bblock, ins, sp [1], ip,
5137 mono_class_from_mono_type (field->type), FALSE, FALSE);
5139 MONO_ADD_INS (bblock, store);
5142 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound || klass == mono_defaults.marshalbyrefobject_class) {
5143 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
5144 MonoInst *iargs [4];
5148 NEW_CLASSCONST (cfg, iargs [1], klass);
5149 NEW_FIELDCONST (cfg, iargs [2], field);
5150 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
5151 if ((cfg->opt & MONO_OPT_INLINE) && !MONO_TYPE_ISSTRUCT (mono_method_signature (ldfld_wrapper)->ret)) {
5152 costs = inline_method (cfg, ldfld_wrapper, mono_method_signature (ldfld_wrapper), bblock,
5153 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
5154 g_assert (costs > 0);
5159 GET_BBLOCK (cfg, bbhash, bblock, ip);
5160 ebblock->next_bb = bblock;
5161 link_bblock (cfg, ebblock, bblock);
5163 temp = iargs [0]->inst_i0->inst_c0;
5165 if (*ip == CEE_LDFLDA) {
5166 /* not sure howto handle this */
5167 NEW_TEMPLOADA (cfg, *sp, temp);
5169 NEW_TEMPLOAD (cfg, *sp, temp);
5173 /* indicates start of a new block, and triggers a load of
5174 all stack arguments at bb boundarie */
5177 inline_costs += costs;
5180 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, mono_method_signature (ldfld_wrapper), iargs, ip, NULL);
5181 if (*ip == CEE_LDFLDA) {
5182 /* not sure howto handle this */
5183 NEW_TEMPLOADA (cfg, *sp, temp);
5185 NEW_TEMPLOAD (cfg, *sp, temp);
5190 NEW_ICONST (cfg, offset_ins, foffset);
5191 MONO_INST_NEW (cfg, ins, OP_PADD);
5193 ins->inst_left = *sp;
5194 ins->inst_right = offset_ins;
5195 ins->type = STACK_MP;
5197 if (*ip == CEE_LDFLDA) {
5201 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
5202 type_to_eval_stack_type (field->type, load);
5203 load->cil_code = ip;
5204 load->inst_left = ins;
5205 load->flags |= ins_flag;
5217 MonoClassField *field;
5218 gpointer addr = NULL;
5221 token = read32 (ip + 1);
5223 field = mono_field_from_token (image, token, &klass, generic_context);
5224 mono_class_init (klass);
5226 g_assert (!(field->type->attrs & FIELD_ATTRIBUTE_LITERAL));
5228 if ((*ip) == CEE_STSFLD)
5229 handle_loaded_temps (cfg, bblock, stack_start, sp);
5231 /* The special_static_fields field is init'd in mono_class_vtable, so it needs
5232 * to be called here.
5234 if (!(cfg->opt & MONO_OPT_SHARED))
5235 mono_class_vtable (cfg->domain, klass);
5236 mono_domain_lock (cfg->domain);
5237 if (cfg->domain->special_static_fields)
5238 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
5239 mono_domain_unlock (cfg->domain);
5241 if ((cfg->opt & MONO_OPT_SHARED) || (cfg->compile_aot && addr)) {
5243 MonoInst *iargs [2];
5244 MonoInst *domain_var;
5246 g_assert (field->parent);
5247 /* avoid depending on undefined C behavior in sequence points */
5248 domain_var = mono_get_domainvar (cfg);
5249 NEW_TEMPLOAD (cfg, iargs [0], domain_var->inst_c0);
5250 NEW_FIELDCONST (cfg, iargs [1], field);
5251 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
5252 NEW_TEMPLOAD (cfg, ins, temp);
5255 vtable = mono_class_vtable (cfg->domain, klass);
5257 if (mini_field_access_needs_cctor_run (cfg, method, vtable) && !(g_slist_find (class_inits, vtable))) {
5258 guint8 *tramp = mono_create_class_init_trampoline (vtable);
5259 mono_emit_native_call (cfg, bblock, tramp,
5260 helper_sig_class_init_trampoline,
5261 NULL, ip, FALSE, FALSE);
5262 if (cfg->verbose_level > 2)
5263 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
5264 class_inits = g_slist_prepend (class_inits, vtable);
5266 if (cfg->run_cctors)
5267 mono_runtime_class_init (vtable);
5269 addr = (char*)vtable->data + field->offset;
5271 if (cfg->compile_aot)
5272 NEW_SFLDACONST (cfg, ins, field);
5274 NEW_PCONST (cfg, ins, addr);
5278 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
5279 * This could be later optimized to do just a couple of
5280 * memory dereferences with constant offsets.
5283 MonoInst *iargs [1];
5284 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
5285 temp = mono_emit_jit_icall (cfg, bblock, mono_get_special_static_data, iargs, ip);
5286 NEW_TEMPLOAD (cfg, ins, temp);
5290 /* FIXME: mark instructions for use in SSA */
5291 if (*ip == CEE_LDSFLDA) {
5293 } else if (*ip == CEE_STSFLD) {
5297 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
5298 store->cil_code = ip;
5299 store->inst_left = ins;
5300 store->inst_right = sp [0];
5301 store->flags |= ins_flag;
5304 if (store->opcode == CEE_STOBJ) {
5305 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
5307 MONO_ADD_INS (bblock, store);
5309 gboolean is_const = FALSE;
5310 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
5311 if (!((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) &&
5312 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
5313 gpointer addr = (char*)vtable->data + field->offset;
5314 int ro_type = field->type->type;
5315 if (ro_type == MONO_TYPE_VALUETYPE && field->type->data.klass->enumtype) {
5316 ro_type = field->type->data.klass->enum_basetype->type;
5318 /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
5321 case MONO_TYPE_BOOLEAN:
5323 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
5327 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
5330 case MONO_TYPE_CHAR:
5332 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
5336 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
5341 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
5345 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
5350 case MONO_TYPE_STRING:
5351 case MONO_TYPE_OBJECT:
5352 case MONO_TYPE_CLASS:
5353 case MONO_TYPE_SZARRAY:
5355 case MONO_TYPE_FNPTR:
5356 case MONO_TYPE_ARRAY:
5357 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
5358 type_to_eval_stack_type (field->type, *sp);
5363 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
5364 sp [0]->type = STACK_I8;
5365 sp [0]->inst_l = *((gint64 *)addr);
5370 case MONO_TYPE_VALUETYPE:
5379 CHECK_STACK_OVF (1);
5380 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
5381 type_to_eval_stack_type (field->type, load);
5382 load->cil_code = ip;
5383 load->inst_left = ins;
5385 load->flags |= ins_flag;
5387 /* fixme: dont see the problem why this does not work */
5388 //cfg->disable_aot = TRUE;
5398 token = read32 (ip + 1);
5399 if (method->wrapper_type != MONO_WRAPPER_NONE)
5400 klass = mono_method_get_wrapper_data (method, token);
5402 klass = mono_class_get_full (image, token, generic_context);
5403 mono_class_init (klass);
5404 n = mono_type_to_stind (&klass->byval_arg);
5405 if (n == CEE_STOBJ) {
5406 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
5408 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
5410 MONO_INST_NEW (cfg, store, n);
5411 store->cil_code = ip;
5412 store->inst_left = sp [0];
5413 store->inst_right = sp [1];
5414 store->flags |= ins_flag;
5415 MONO_ADD_INS (bblock, store);
5427 token = read32 (ip + 1);
5428 if (method->wrapper_type != MONO_WRAPPER_NONE)
5429 klass = mono_method_get_wrapper_data (method, token);
5431 klass = mono_class_get_full (image, token, generic_context);
5432 mono_class_init (klass);
5434 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5439 *sp++ = handle_box (cfg, bblock, val, ip, klass);
5446 MONO_INST_NEW (cfg, ins, *ip);
5451 token = read32 (ip + 1);
5453 /* allocate the domainvar - becaus this is used in decompose_foreach */
5454 if (cfg->opt & MONO_OPT_SHARED) {
5455 mono_get_domainvar (cfg);
5456 /* LAME-IR: Mark it as used since otherwise it will be optimized away */
5457 cfg->domainvar->flags |= MONO_INST_VOLATILE;
5461 mono_get_got_var (cfg);
5463 if (method->wrapper_type != MONO_WRAPPER_NONE)
5464 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5466 klass = mono_class_get_full (image, token, generic_context);
5468 mono_class_init (klass);
5469 ins->inst_newa_class = klass;
5470 ins->inst_newa_len = *sp;
5471 ins->type = STACK_OBJ;
5475 * we store the object so calls to create the array are not interleaved
5476 * with the arguments of other calls.
5479 MonoInst *store, *temp, *load;
5481 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
5482 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
5483 store->cil_code = ins->cil_code;
5484 MONO_ADD_INS (bblock, store);
5485 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
5486 load->cil_code = ins->cil_code;
5493 MONO_INST_NEW (cfg, ins, *ip);
5494 ins->cil_code = ip++;
5496 ins->inst_left = *sp;
5497 ins->type = STACK_PTR;
5505 if (method->wrapper_type != MONO_WRAPPER_NONE)
5506 klass = (MonoClass*)mono_method_get_wrapper_data (method, read32 (ip + 1));
5508 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
5510 /* we need to make sure that this array is exactly the type it needs
5511 * to be for correctness. the wrappers are lax with their usage
5512 * so we need to ignore them here
5514 if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE) {
5516 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
5517 check->cil_code = ip;
5518 check->klass = klass;
5519 check->inst_left = sp [0];
5520 check->type = STACK_OBJ;
5524 mono_class_init (klass);
5525 NEW_LDELEMA (cfg, ins, sp, klass);
5530 case CEE_LDELEM_ANY: {
5535 token = read32 (ip + 1);
5536 klass = mono_class_get_full (image, token, generic_context);
5537 mono_class_init (klass);
5538 NEW_LDELEMA (cfg, load, sp, klass);
5539 load->cil_code = ip;
5540 MONO_INST_NEW (cfg, ins, mono_type_to_ldind (&klass->byval_arg));
5542 ins->inst_left = load;
5544 type_to_eval_stack_type (&klass->byval_arg, ins);
5558 case CEE_LDELEM_REF: {
5562 * ldind.x (ldelema (array, index))
5563 * ldelema does the bounds check
5567 klass = array_access_to_klass (*ip);
5568 NEW_LDELEMA (cfg, load, sp, klass);
5569 load->cil_code = ip;
5570 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
5572 ins->inst_left = load;
5574 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
5584 case CEE_STELEM_R8: {
5588 * stind.x (ldelema (array, index), val)
5589 * ldelema does the bounds check
5593 klass = array_access_to_klass (*ip);
5594 NEW_LDELEMA (cfg, load, sp, klass);
5595 load->cil_code = ip;
5596 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
5598 ins->inst_left = load;
5599 ins->inst_right = sp [2];
5601 handle_loaded_temps (cfg, bblock, stack_start, sp);
5602 MONO_ADD_INS (bblock, ins);
5606 case CEE_STELEM_ANY: {
5610 * stind.x (ldelema (array, index), val)
5611 * ldelema does the bounds check
5616 token = read32 (ip + 1);
5617 klass = mono_class_get_full (image, token, generic_context);
5618 mono_class_init (klass);
5619 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5620 MonoMethod* helper = mono_marshal_get_stelemref ();
5621 MonoInst *iargs [3];
5622 handle_loaded_temps (cfg, bblock, stack_start, sp);
5628 mono_emit_method_call_spilled (cfg, bblock, helper, mono_method_signature (helper), iargs, ip, NULL);
5630 NEW_LDELEMA (cfg, load, sp, klass);
5631 load->cil_code = ip;
5633 n = mono_type_to_stind (&klass->byval_arg);
5635 handle_stobj (cfg, bblock, load, sp [2], ip, klass, FALSE, FALSE);
5637 MONO_INST_NEW (cfg, ins, n);
5639 ins->inst_left = load;
5640 ins->inst_right = sp [2];
5641 handle_loaded_temps (cfg, bblock, stack_start, sp);
5642 MONO_ADD_INS (bblock, ins);
5649 case CEE_STELEM_REF: {
5650 MonoInst *iargs [3];
5651 MonoMethod* helper = mono_marshal_get_stelemref ();
5656 handle_loaded_temps (cfg, bblock, stack_start, sp);
5662 mono_emit_method_call_spilled (cfg, bblock, helper, mono_method_signature (helper), iargs, ip, NULL);
5666 NEW_GROUP (cfg, group, sp [0], sp [1]);
5667 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
5669 ins->inst_left = group;
5670 ins->inst_right = sp [2];
5671 MONO_ADD_INS (bblock, ins);
5678 case CEE_CKFINITE: {
5679 MonoInst *store, *temp;
5682 /* this instr. can throw exceptions as side effect,
5683 * so we cant eliminate dead code which contains CKFINITE opdodes.
5684 * Spilling to memory makes sure that we always perform
5688 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
5690 ins->inst_left = sp [-1];
5691 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
5693 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
5694 store->cil_code = ip;
5695 MONO_ADD_INS (bblock, store);
5697 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
5704 MONO_INST_NEW (cfg, ins, *ip);
5707 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
5708 mono_class_init (klass);
5709 ins->type = STACK_MP;
5710 ins->inst_left = *sp;
5712 ins->inst_newa_class = klass;
5717 case CEE_MKREFANY: {
5718 MonoInst *loc, *klassconst;
5721 MONO_INST_NEW (cfg, ins, *ip);
5724 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
5725 mono_class_init (klass);
5728 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
5729 NEW_TEMPLOADA (cfg, ins->inst_right, loc->inst_c0);
5731 NEW_PCONST (cfg, klassconst, klass);
5732 NEW_GROUP (cfg, ins->inst_left, *sp, klassconst);
5734 MONO_ADD_INS (bblock, ins);
5736 NEW_TEMPLOAD (cfg, *sp, loc->inst_c0);
5743 MonoClass *handle_class;
5745 CHECK_STACK_OVF (1);
5748 n = read32 (ip + 1);
5750 handle = mono_ldtoken (image, n, &handle_class, generic_context);
5751 mono_class_init (handle_class);
5753 if (cfg->opt & MONO_OPT_SHARED) {
5755 MonoInst *res, *store, *addr, *vtvar, *iargs [3];
5757 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
5759 NEW_IMAGECONST (cfg, iargs [0], image);
5760 NEW_ICONST (cfg, iargs [1], n);
5761 NEW_PCONST (cfg, iargs [2], generic_context);
5762 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
5763 NEW_TEMPLOAD (cfg, res, temp);
5764 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5765 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
5766 MONO_ADD_INS (bblock, store);
5767 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5769 if ((ip [5] == CEE_CALL) && (cmethod = mini_get_method (image, read32 (ip + 6), NULL, generic_context)) &&
5770 (cmethod->klass == mono_defaults.monotype_class->parent) &&
5771 (strcmp (cmethod->name, "GetTypeFromHandle") == 0) && ip_in_bb (cfg, bblock, ip + 5)) {
5772 MonoClass *tclass = mono_class_from_mono_type (handle);
5773 mono_class_init (tclass);
5774 if (cfg->compile_aot)
5775 NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n);
5777 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
5778 ins->type = STACK_OBJ;
5779 ins->klass = cmethod->klass;
5782 MonoInst *store, *addr, *vtvar;
5784 if (cfg->compile_aot)
5785 NEW_LDTOKENCONST (cfg, ins, image, n);
5787 NEW_PCONST (cfg, ins, handle);
5788 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
5789 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5790 NEW_INDSTORE (cfg, store, addr, ins, &mono_defaults.int_class->byval_arg);
5791 MONO_ADD_INS (bblock, store);
5792 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5808 case CEE_ADD_OVF_UN:
5810 case CEE_MUL_OVF_UN:
5812 case CEE_SUB_OVF_UN:
5815 if (mono_find_jit_opcode_emulation (ins->opcode)) {
5817 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
5818 mono_get_got_var (cfg);
5822 case CEE_ENDFINALLY:
5823 MONO_INST_NEW (cfg, ins, *ip);
5824 MONO_ADD_INS (bblock, ins);
5825 ins->cil_code = ip++;
5826 start_new_bblock = 1;
5829 * Control will leave the method so empty the stack, otherwise
5830 * the next basic block will start with a nonempty stack.
5832 while (sp != stack_start) {
5833 MONO_INST_NEW (cfg, ins, CEE_POP);
5837 MONO_ADD_INS (bblock, ins);
5844 if (*ip == CEE_LEAVE) {
5846 target = ip + 5 + (gint32)read32(ip + 1);
5849 target = ip + 2 + (signed char)(ip [1]);
5852 /* empty the stack */
5853 while (sp != stack_start) {
5854 MONO_INST_NEW (cfg, ins, CEE_POP);
5858 MONO_ADD_INS (bblock, ins);
5862 * If this leave statement is in a catch block, check for a
5863 * pending exception, and rethrow it if necessary.
5865 for (i = 0; i < header->num_clauses; ++i) {
5866 MonoExceptionClause *clause = &header->clauses [i];
5868 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)) {
5872 NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, clause->handler_offset)->inst_c0);
5873 load->cil_code = ip;
5875 temp = mono_emit_jit_icall (cfg, bblock, mono_thread_get_pending_exception, NULL, ip);
5876 NEW_TEMPLOAD (cfg, *sp, temp);
5878 MONO_INST_NEW (cfg, ins, OP_THROW_OR_NULL);
5879 ins->inst_left = *sp;
5880 ins->inst_right = load;
5882 MONO_ADD_INS (bblock, ins);
5886 /* fixme: call fault handler ? */
5888 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
5890 for (tmp = handlers; tmp; tmp = tmp->next) {
5892 link_bblock (cfg, bblock, tblock);
5893 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
5895 ins->inst_target_bb = tblock;
5896 MONO_ADD_INS (bblock, ins);
5898 g_list_free (handlers);
5901 MONO_INST_NEW (cfg, ins, CEE_BR);
5903 MONO_ADD_INS (bblock, ins);
5904 GET_BBLOCK (cfg, bbhash, tblock, target);
5905 link_bblock (cfg, bblock, tblock);
5906 CHECK_BBLOCK (target, ip, tblock);
5907 ins->inst_target_bb = tblock;
5908 start_new_bblock = 1;
5910 if (*ip == CEE_LEAVE)
5919 MONO_INST_NEW (cfg, ins, *ip);
5921 handle_loaded_temps (cfg, bblock, stack_start, sp);
5922 MONO_ADD_INS (bblock, ins);
5923 ins->cil_code = ip++;
5924 ins->inst_i0 = sp [0];
5925 ins->inst_i1 = sp [1];
5933 /* trampoline mono specific opcodes */
5934 case MONO_CUSTOM_PREFIX: {
5936 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
5941 case CEE_MONO_ICALL: {
5944 MonoJitICallInfo *info;
5946 token = read32 (ip + 2);
5947 func = mono_method_get_wrapper_data (method, token);
5948 info = mono_find_jit_icall_by_addr (func);
5951 CHECK_STACK (info->sig->param_count);
5952 sp -= info->sig->param_count;
5954 temp = mono_emit_jit_icall (cfg, bblock, info->func, sp, ip);
5955 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
5956 NEW_TEMPLOAD (cfg, *sp, temp);
5961 inline_costs += 10 * num_calls++;
5965 case CEE_MONO_LDPTR:
5966 CHECK_STACK_OVF (1);
5968 token = read32 (ip + 2);
5969 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
5973 inline_costs += 10 * num_calls++;
5974 /* Can't embed random pointers into AOT code */
5975 cfg->disable_aot = 1;
5977 case CEE_MONO_VTADDR:
5980 MONO_INST_NEW (cfg, ins, OP_VTADDR);
5982 ins->type = STACK_MP;
5983 ins->inst_left = *sp;
5987 case CEE_MONO_NEWOBJ: {
5988 MonoInst *iargs [2];
5990 CHECK_STACK_OVF (1);
5992 token = read32 (ip + 2);
5993 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5994 mono_class_init (klass);
5995 NEW_DOMAINCONST (cfg, iargs [0]);
5996 NEW_CLASSCONST (cfg, iargs [1], klass);
5997 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
5998 NEW_TEMPLOAD (cfg, *sp, temp);
6001 inline_costs += 10 * num_calls++;
6004 case CEE_MONO_OBJADDR:
6007 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
6009 ins->type = STACK_MP;
6010 ins->inst_left = *sp;
6014 case CEE_MONO_LDNATIVEOBJ:
6017 token = read32 (ip + 2);
6018 klass = mono_method_get_wrapper_data (method, token);
6019 g_assert (klass->valuetype);
6020 mono_class_init (klass);
6021 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
6025 case CEE_MONO_RETOBJ:
6026 g_assert (cfg->ret);
6027 g_assert (mono_method_signature (method)->pinvoke);
6032 token = read32 (ip + 2);
6033 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
6035 NEW_RETLOADA (cfg, ins);
6036 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
6038 if (sp != stack_start)
6041 MONO_INST_NEW (cfg, ins, CEE_BR);
6043 ins->inst_target_bb = end_bblock;
6044 MONO_ADD_INS (bblock, ins);
6045 link_bblock (cfg, bblock, end_bblock);
6046 start_new_bblock = 1;
6049 case CEE_MONO_CISINST:
6050 case CEE_MONO_CCASTCLASS: {
6055 token = read32 (ip + 2);
6056 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
6057 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_CISINST) ? OP_CISINST : OP_CCASTCLASS);
6058 ins->type = STACK_I4;
6059 ins->inst_left = *sp;
6060 ins->inst_newa_class = klass;
6062 *sp++ = emit_tree (cfg, bblock, ins, ip + 6);
6066 case CEE_MONO_SAVE_LMF:
6067 case CEE_MONO_RESTORE_LMF:
6068 #ifdef MONO_ARCH_HAVE_LMF_OPS
6069 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_SAVE_LMF) ? OP_SAVE_LMF : OP_RESTORE_LMF);
6070 MONO_ADD_INS (bblock, ins);
6071 cfg->need_lmf_area = TRUE;
6075 case CEE_MONO_CLASSCONST:
6076 CHECK_STACK_OVF (1);
6078 token = read32 (ip + 2);
6079 NEW_CLASSCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
6083 inline_costs += 10 * num_calls++;
6085 case CEE_MONO_NOT_TAKEN:
6086 bblock->out_of_line = TRUE;
6090 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
6099 /* somewhat similar to LDTOKEN */
6100 MonoInst *addr, *vtvar;
6101 CHECK_STACK_OVF (1);
6102 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
6104 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
6105 addr->cil_code = ip;
6106 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
6108 ins->inst_left = addr;
6109 MONO_ADD_INS (bblock, ins);
6110 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
6124 * The following transforms:
6125 * CEE_CEQ into OP_CEQ
6126 * CEE_CGT into OP_CGT
6127 * CEE_CGT_UN into OP_CGT_UN
6128 * CEE_CLT into OP_CLT
6129 * CEE_CLT_UN into OP_CLT_UN
6131 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
6133 MONO_INST_NEW (cfg, ins, cmp->opcode);
6135 cmp->inst_i0 = sp [0];
6136 cmp->inst_i1 = sp [1];
6140 if ((sp [0]->type == STACK_I8) || ((sizeof (gpointer) == 8) && ((sp [0]->type == STACK_PTR) || (sp [0]->type == STACK_OBJ) || (sp [0]->type == STACK_MP))))
6141 cmp->opcode = OP_LCOMPARE;
6143 cmp->opcode = OP_COMPARE;
6145 ins->type = STACK_I4;
6148 /* spill it to reduce the expression complexity
6149 * and workaround bug 54209
6151 if (cmp->inst_left->type == STACK_I8) {
6153 *sp++ = emit_tree (cfg, bblock, ins, ip + 2);
6162 CHECK_STACK_OVF (1);
6164 n = read32 (ip + 2);
6165 if (method->wrapper_type != MONO_WRAPPER_NONE)
6166 cmethod = mono_method_get_wrapper_data (method, n);
6168 cmethod = mini_get_method (image, n, NULL, generic_context);
6171 mono_class_init (cmethod->klass);
6173 if (mono_use_security_manager) {
6174 check_linkdemand (cfg, method, cmethod, bblock, ip);
6177 handle_loaded_temps (cfg, bblock, stack_start, sp);
6179 NEW_METHODCONST (cfg, argconst, cmethod);
6180 if (method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED)
6181 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
6183 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn_nosync, &argconst, ip);
6184 NEW_TEMPLOAD (cfg, *sp, temp);
6188 inline_costs += 10 * num_calls++;
6191 case CEE_LDVIRTFTN: {
6197 n = read32 (ip + 2);
6198 if (method->wrapper_type != MONO_WRAPPER_NONE)
6199 cmethod = mono_method_get_wrapper_data (method, n);
6201 cmethod = mini_get_method (image, n, NULL, generic_context);
6203 mono_class_init (cmethod->klass);
6205 if (mono_use_security_manager) {
6206 check_linkdemand (cfg, method, cmethod, bblock, ip);
6209 handle_loaded_temps (cfg, bblock, stack_start, sp);
6213 NEW_METHODCONST (cfg, args [1], cmethod);
6214 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
6215 NEW_TEMPLOAD (cfg, *sp, temp);
6219 inline_costs += 10 * num_calls++;
6223 CHECK_STACK_OVF (1);
6225 n = read16 (ip + 2);
6227 NEW_ARGLOAD (cfg, ins, n);
6233 CHECK_STACK_OVF (1);
6235 n = read16 (ip + 2);
6237 NEW_ARGLOADA (cfg, ins, n);
6245 handle_loaded_temps (cfg, bblock, stack_start, sp);
6247 n = read16 (ip + 2);
6249 NEW_ARGSTORE (cfg, ins, n, *sp);
6251 if (ins->opcode == CEE_STOBJ) {
6252 NEW_ARGLOADA (cfg, ins, n);
6253 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
6255 MONO_ADD_INS (bblock, ins);
6259 CHECK_STACK_OVF (1);
6261 n = read16 (ip + 2);
6263 NEW_LOCLOAD (cfg, ins, n);
6269 CHECK_STACK_OVF (1);
6271 n = read16 (ip + 2);
6273 NEW_LOCLOADA (cfg, ins, n);
6282 n = read16 (ip + 2);
6284 handle_loaded_temps (cfg, bblock, stack_start, sp);
6285 NEW_LOCSTORE (cfg, ins, n, *sp);
6287 if (ins->opcode == CEE_STOBJ) {
6288 NEW_LOCLOADA (cfg, ins, n);
6289 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
6291 MONO_ADD_INS (bblock, ins);
6298 if (sp != stack_start)
6300 if (cfg->method != method)
6302 * Inlining this into a loop in a parent could lead to
6303 * stack overflows which is different behavior than the
6304 * non-inlined case, thus disable inlining in this case.
6306 goto inline_failure;
6307 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
6308 ins->inst_left = *sp;
6310 ins->type = STACK_MP;
6312 cfg->flags |= MONO_CFG_HAS_ALLOCA;
6313 if (header->init_locals)
6314 ins->flags |= MONO_INST_INIT;
6318 /* FIXME: set init flag if locals init is set in this method */
6320 case CEE_ENDFILTER: {
6321 MonoExceptionClause *clause, *nearest;
6322 int cc, nearest_num;
6326 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
6328 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
6329 ins->inst_left = *sp;
6331 MONO_ADD_INS (bblock, ins);
6332 start_new_bblock = 1;
6337 for (cc = 0; cc < header->num_clauses; ++cc) {
6338 clause = &header->clauses [cc];
6339 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
6340 (!nearest || (clause->data.filter_offset > nearest->data.filter_offset))) {
6346 if ((ip - header->code) != nearest->handler_offset)
6351 case CEE_UNALIGNED_:
6352 ins_flag |= MONO_INST_UNALIGNED;
6353 /* FIXME: record alignment? we can assume 1 for now */
6358 ins_flag |= MONO_INST_VOLATILE;
6362 ins_flag |= MONO_INST_TAILCALL;
6363 cfg->flags |= MONO_CFG_HAS_TAIL;
6364 /* Can't inline tail calls at this time */
6365 inline_costs += 100000;
6372 token = read32 (ip + 2);
6373 if (method->wrapper_type != MONO_WRAPPER_NONE)
6374 klass = mono_method_get_wrapper_data (method, token);
6376 klass = mono_class_get_full (image, token, generic_context);
6377 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
6378 MonoInst *store, *load;
6379 NEW_PCONST (cfg, load, NULL);
6380 load->cil_code = ip;
6381 load->type = STACK_OBJ;
6382 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
6383 store->cil_code = ip;
6384 handle_loaded_temps (cfg, bblock, stack_start, sp);
6385 MONO_ADD_INS (bblock, store);
6386 store->inst_i0 = sp [0];
6387 store->inst_i1 = load;
6389 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
6394 case CEE_CONSTRAINED_:
6395 /* FIXME: implement */
6397 token = read32 (ip + 2);
6398 constrained_call = mono_class_get_full (image, token, generic_context);
6403 MonoInst *iargs [3];
6406 if ((cfg->opt & MONO_OPT_INTRINS) && (ip [1] == CEE_CPBLK) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5)) {
6408 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
6409 copy->inst_left = sp [0];
6410 copy->inst_right = sp [1];
6411 copy->cil_code = ip;
6413 MONO_ADD_INS (bblock, copy);
6420 handle_loaded_temps (cfg, bblock, stack_start, sp);
6421 if (ip [1] == CEE_CPBLK) {
6422 MonoMethod *memcpy_method = get_memcpy_method ();
6423 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
6425 MonoMethod *memset_method = get_memset_method ();
6426 mono_emit_method_call_spilled (cfg, bblock, memset_method, memset_method->signature, iargs, ip, NULL);
6435 ins_flag |= MONO_INST_NOTYPECHECK;
6437 ins_flag |= MONO_INST_NORANGECHECK;
6438 /* we ignore the no-nullcheck for now since we
6439 * really do it explicitly only when doing callvirt->call
6445 int handler_offset = -1;
6447 for (i = 0; i < header->num_clauses; ++i) {
6448 MonoExceptionClause *clause = &header->clauses [i];
6449 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code) && !(clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY))
6450 handler_offset = clause->handler_offset;
6453 g_assert (handler_offset != -1);
6455 NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, handler_offset)->inst_c0);
6456 load->cil_code = ip;
6457 MONO_INST_NEW (cfg, ins, OP_RETHROW);
6458 ins->inst_left = load;
6460 MONO_ADD_INS (bblock, ins);
6462 link_bblock (cfg, bblock, end_bblock);
6463 start_new_bblock = 1;
6465 mono_get_got_var (cfg);
6469 CHECK_STACK_OVF (1);
6471 token = read32 (ip + 2);
6472 /* FIXXME: handle generics. */
6473 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
6474 MonoType *type = mono_type_create_from_typespec (image, token);
6475 token = mono_type_size (type, &align);
6477 MonoClass *szclass = mono_class_get_full (image, token, generic_context);
6478 mono_class_init (szclass);
6479 token = mono_class_value_size (szclass, &align);
6481 NEW_ICONST (cfg, ins, token);
6486 case CEE_REFANYTYPE:
6488 MONO_INST_NEW (cfg, ins, OP_REFANYTYPE);
6490 ins->type = STACK_MP;
6491 ins->inst_left = *sp;
6492 ins->type = STACK_VTYPE;
6493 ins->klass = mono_defaults.typehandle_class;
6502 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
6507 g_error ("opcode 0x%02x not handled", *ip);
6510 if (start_new_bblock != 1)
6513 bblock->cil_length = ip - bblock->cil_code;
6514 bblock->next_bb = end_bblock;
6516 if (cfg->method == method && cfg->domainvar) {
6518 MonoInst *get_domain;
6520 if (! (get_domain = mono_arch_get_domain_intrinsic (cfg))) {
6523 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
6524 call->signature = helper_sig_domain_get;
6525 call->inst.type = STACK_PTR;
6526 call->fptr = mono_domain_get;
6527 get_domain = (MonoInst*)call;
6530 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, get_domain);
6531 MONO_ADD_INS (init_localsbb, store);
6534 if (cfg->method == method && cfg->got_var)
6535 mono_emit_load_got_addr (cfg);
6537 if (header->init_locals) {
6539 for (i = 0; i < header->num_locals; ++i) {
6540 MonoType *ptype = header->locals [i];
6541 int t = ptype->type;
6542 if (t == MONO_TYPE_VALUETYPE && ptype->data.klass->enumtype)
6543 t = ptype->data.klass->enum_basetype->type;
6545 NEW_PCONST (cfg, ins, NULL);
6546 NEW_LOCSTORE (cfg, store, i, ins);
6547 MONO_ADD_INS (init_localsbb, store);
6548 } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
6549 NEW_ICONST (cfg, ins, 0);
6550 NEW_LOCSTORE (cfg, store, i, ins);
6551 MONO_ADD_INS (init_localsbb, store);
6552 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
6553 MONO_INST_NEW (cfg, ins, OP_I8CONST);
6554 ins->type = STACK_I8;
6556 NEW_LOCSTORE (cfg, store, i, ins);
6557 MONO_ADD_INS (init_localsbb, store);
6558 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
6559 MONO_INST_NEW (cfg, ins, OP_R8CONST);
6560 ins->type = STACK_R8;
6561 ins->inst_p0 = (void*)&r8_0;
6562 NEW_LOCSTORE (cfg, store, i, ins);
6563 MONO_ADD_INS (init_localsbb, store);
6564 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
6565 ((t == MONO_TYPE_GENERICINST) && mono_metadata_generic_class_is_valuetype (ptype->data.generic_class))) {
6566 NEW_LOCLOADA (cfg, ins, i);
6567 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (ptype), NULL, NULL);
6570 NEW_PCONST (cfg, ins, NULL);
6571 NEW_LOCSTORE (cfg, store, i, ins);
6572 MONO_ADD_INS (init_localsbb, store);
6577 /* resolve backward branches in the middle of an existing basic block */
6578 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
6580 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
6581 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
6582 if (tblock != start_bblock) {
6584 split_bblock (cfg, tblock, bblock);
6585 l = bblock->cil_code - header->code;
6586 bblock->cil_length = tblock->cil_length - l;
6587 tblock->cil_length = l;
6589 g_print ("recheck failed.\n");
6593 if (cfg->method == method) {
6595 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6596 bb->region = mono_find_block_region (cfg, bb->real_offset);
6598 mono_create_spvar_for_region (cfg, bb->region);
6599 if (cfg->verbose_level > 2)
6600 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
6603 g_hash_table_destroy (bbhash);
6606 g_slist_free (class_inits);
6607 dont_inline = g_list_remove (dont_inline, method);
6608 return inline_costs;
6611 if (cfg->method != method)
6612 g_hash_table_destroy (bbhash);
6613 g_slist_free (class_inits);
6614 dont_inline = g_list_remove (dont_inline, method);
6618 if (cfg->method != method)
6619 g_hash_table_destroy (bbhash);
6620 g_slist_free (class_inits);
6621 g_error ("Invalid IL code at IL%04x in %s: %s\n", (int)(ip - header->code),
6622 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
6623 dont_inline = g_list_remove (dont_inline, method);
6628 mono_print_tree (MonoInst *tree) {
6634 arity = mono_burg_arity [tree->opcode];
6636 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
6638 switch (tree->opcode) {
6640 printf ("[%d]", (int)tree->inst_c0);
6643 printf ("[%lld]", (long long)tree->inst_l);
6646 printf ("[%f]", *(double*)tree->inst_p0);
6649 printf ("[%f]", *(float*)tree->inst_p0);
6653 printf ("[%d]", (int)tree->inst_c0);
6656 if (tree->inst_offset < 0)
6657 printf ("[-0x%x(%s)]", (int)(-tree->inst_offset), mono_arch_regname (tree->inst_basereg));
6659 printf ("[0x%x(%s)]", (int)(tree->inst_offset), mono_arch_regname (tree->inst_basereg));
6662 printf ("[%s]", mono_arch_regname (tree->dreg));
6665 printf ("[%s]", tree->inst_newa_class->name);
6666 mono_print_tree (tree->inst_newa_len);
6677 case OP_VOIDCALLVIRT: {
6678 MonoCallInst *call = (MonoCallInst*)tree;
6680 printf ("[%s]", call->method->name);
6681 else if (call->fptr) {
6682 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (call->fptr);
6684 printf ("[%s]", info->name);
6690 printf ("[%d (", (int)tree->inst_c0);
6691 for (i = 0; i < tree->inst_phi_args [0]; i++) {
6694 printf ("%d", tree->inst_phi_args [i + 1]);
6705 case OP_LOAD_MEMBASE:
6706 case OP_LOADI4_MEMBASE:
6707 case OP_LOADU4_MEMBASE:
6708 case OP_LOADU1_MEMBASE:
6709 case OP_LOADI1_MEMBASE:
6710 case OP_LOADU2_MEMBASE:
6711 case OP_LOADI2_MEMBASE:
6712 printf ("[%s] <- [%s + 0x%x]", mono_arch_regname (tree->dreg), mono_arch_regname (tree->inst_basereg), (int)tree->inst_offset);
6715 case OP_CALL_HANDLER:
6716 printf ("[B%d]", tree->inst_target_bb->block_num);
6726 case OP_VOIDCALL_REG:
6727 mono_print_tree (tree->inst_left);
6739 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
6740 mono_print_tree (tree->inst_left);
6743 if (!mono_arch_print_tree(tree, arity)) {
6745 mono_print_tree (tree->inst_left);
6747 mono_print_tree (tree->inst_right);
6758 mono_print_tree_nl (MonoInst *tree)
6760 mono_print_tree (tree);
6764 #define make_icall_sig mono_create_icall_signature
6767 create_helper_signature (void)
6769 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
6770 helper_sig_newarr = make_icall_sig ("object ptr ptr int32");
6772 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
6773 helper_sig_newarr_specific = make_icall_sig ("object ptr int32");
6775 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
6776 helper_sig_object_new = make_icall_sig ("object ptr ptr");
6778 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
6779 helper_sig_object_new_specific = make_icall_sig ("object ptr");
6781 /* void* mono_method_compile (MonoMethod*) */
6782 helper_sig_compile = make_icall_sig ("ptr ptr");
6784 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
6785 helper_sig_compile_virt = make_icall_sig ("ptr object ptr");
6787 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
6788 helper_sig_ldstr = make_icall_sig ("object ptr ptr int32");
6790 /* MonoDomain *mono_domain_get (void) */
6791 helper_sig_domain_get = make_icall_sig ("ptr");
6793 /* void stelem_ref (MonoArray *, int index, MonoObject *) */
6794 helper_sig_stelem_ref = make_icall_sig ("void ptr int32 object");
6796 /* void stelem_ref_check (MonoArray *, MonoObject *) */
6797 helper_sig_stelem_ref_check = make_icall_sig ("void object object");
6799 /* void *helper_compile_generic_method (MonoObject *, MonoMethod *, MonoGenericContext *) */
6800 helper_sig_compile_generic_method = make_icall_sig ("ptr object ptr ptr");
6802 /* long amethod (long, long) */
6803 helper_sig_long_long_long = make_icall_sig ("long long long");
6805 /* object amethod (intptr) */
6806 helper_sig_obj_ptr = make_icall_sig ("object ptr");
6808 helper_sig_obj_ptr_ptr = make_icall_sig ("object ptr ptr");
6810 helper_sig_obj_obj_ptr_ptr = make_icall_sig ("object object ptr ptr");
6812 helper_sig_void_void = make_icall_sig ("void");
6814 /* void amethod (intptr) */
6815 helper_sig_void_ptr = make_icall_sig ("void ptr");
6817 /* void amethod (MonoObject *obj) */
6818 helper_sig_void_obj = make_icall_sig ("void object");
6820 /* void amethod (MonoObject *obj, void *ptr, int i) */
6821 helper_sig_void_obj_ptr_int = make_icall_sig ("void object ptr int");
6823 helper_sig_void_obj_ptr_ptr_obj = make_icall_sig ("void object ptr ptr object");
6825 /* intptr amethod (void) */
6826 helper_sig_ptr_void = make_icall_sig ("ptr");
6828 /* object amethod (void) */
6829 helper_sig_obj_void = make_icall_sig ("object");
6831 /* void amethod (intptr, intptr) */
6832 helper_sig_void_ptr_ptr = make_icall_sig ("void ptr ptr");
6834 /* void amethod (intptr, intptr, intptr) */
6835 helper_sig_void_ptr_ptr_ptr = make_icall_sig ("void ptr ptr ptr");
6837 /* intptr amethod (intptr, intptr) */
6838 helper_sig_ptr_ptr_ptr = make_icall_sig ("ptr ptr ptr");
6840 helper_sig_ptr_ptr_ptr_ptr = make_icall_sig ("ptr ptr ptr ptr");
6842 /* IntPtr amethod (object) */
6843 helper_sig_ptr_obj = make_icall_sig ("ptr object");
6845 /* IntPtr amethod (object, int) */
6846 helper_sig_ptr_obj_int = make_icall_sig ("ptr object int");
6848 /* IntPtr amethod (int) */
6849 helper_sig_ptr_int = make_icall_sig ("ptr int32");
6851 /* long amethod (long, guint32) */
6852 helper_sig_long_long_int = make_icall_sig ("long long int32");
6854 /* ulong amethod (double) */
6855 helper_sig_ulong_double = make_icall_sig ("ulong double");
6857 /* long amethod (double) */
6858 helper_sig_long_double = make_icall_sig ("long double");
6860 /* double amethod (long) */
6861 helper_sig_double_long = make_icall_sig ("double long");
6863 /* double amethod (int) */
6864 helper_sig_double_int = make_icall_sig ("double int32");
6866 /* float amethod (long) */
6867 helper_sig_float_long = make_icall_sig ("float long");
6869 /* double amethod (double, double) */
6870 helper_sig_double_double_double = make_icall_sig ("double double double");
6872 /* uint amethod (double) */
6873 helper_sig_uint_double = make_icall_sig ("uint32 double");
6875 /* int amethod (double) */
6876 helper_sig_int_double = make_icall_sig ("int32 double");
6878 helper_sig_class_init_trampoline = make_icall_sig ("void");
6880 helper_sig_obj_obj_obj_ptr = make_icall_sig ("object object object ptr");
6882 helper_sig_void_obj_obj_ptr = make_icall_sig ("void object object ptr");
6886 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
6889 MonoMethod *wrapper;
6892 if (callinfo->wrapper)
6893 return callinfo->wrapper;
6895 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
6896 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
6897 /* Must be domain neutral since there is only one copy */
6898 code = mono_jit_compile_method_with_opt (wrapper, default_opt | MONO_OPT_SHARED);
6900 if (!callinfo->wrapper) {
6901 callinfo->wrapper = code;
6902 mono_register_jit_icall_wrapper (callinfo, code);
6903 mono_debug_add_icall_wrapper (wrapper, callinfo);
6907 return callinfo->wrapper;
6911 mono_init_trampolines (void)
6913 trampoline_code [MONO_TRAMPOLINE_GENERIC] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_GENERIC);
6914 trampoline_code [MONO_TRAMPOLINE_JUMP] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_JUMP);
6915 trampoline_code [MONO_TRAMPOLINE_CLASS_INIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_CLASS_INIT);
6916 #ifdef MONO_ARCH_HAVE_PIC_AOT
6917 trampoline_code [MONO_TRAMPOLINE_AOT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_AOT);
6920 mono_generic_trampoline_code = trampoline_code [MONO_TRAMPOLINE_GENERIC];
6924 mono_get_trampoline_code (MonoTrampolineType tramp_type)
6926 return trampoline_code [tramp_type];
6930 mono_create_class_init_trampoline (MonoVTable *vtable)
6934 /* previously created trampoline code */
6935 mono_domain_lock (vtable->domain);
6937 g_hash_table_lookup (vtable->domain->class_init_trampoline_hash,
6939 mono_domain_unlock (vtable->domain);
6943 code = mono_arch_create_class_init_trampoline (vtable);
6945 /* store trampoline address */
6946 mono_domain_lock (vtable->domain);
6947 g_hash_table_insert (vtable->domain->class_init_trampoline_hash,
6949 mono_domain_unlock (vtable->domain);
6951 EnterCriticalSection (&jit_mutex);
6952 if (!class_init_hash_addr)
6953 class_init_hash_addr = g_hash_table_new (NULL, NULL);
6954 g_hash_table_insert (class_init_hash_addr, code, vtable);
6955 LeaveCriticalSection (&jit_mutex);
6961 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method,
6962 gboolean add_sync_wrapper)
6967 if (add_sync_wrapper && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
6968 return mono_create_jump_trampoline (domain, mono_marshal_get_synchronized_wrapper (method), FALSE);
6970 code = mono_jit_find_compiled_method (domain, method);
6974 mono_domain_lock (domain);
6975 code = g_hash_table_lookup (domain->jump_trampoline_hash, method);
6976 mono_domain_unlock (domain);
6980 ji = mono_arch_create_jump_trampoline (method);
6983 * mono_delegate_ctor needs to find the method metadata from the
6984 * trampoline address, so we save it here.
6987 mono_jit_info_table_add (mono_get_root_domain (), ji);
6989 mono_domain_lock (domain);
6990 g_hash_table_insert (domain->jump_trampoline_hash, method, ji->code_start);
6991 mono_domain_unlock (domain);
6993 return ji->code_start;
6997 mono_create_jit_trampoline (MonoMethod *method)
6999 MonoDomain *domain = mono_domain_get ();
7002 mono_domain_lock (domain);
7003 tramp = g_hash_table_lookup (domain->jit_trampoline_hash, method);
7004 mono_domain_unlock (domain);
7008 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
7009 return mono_create_jit_trampoline (mono_marshal_get_synchronized_wrapper (method));
7011 tramp = mono_arch_create_jit_trampoline (method);
7013 mono_domain_lock (domain);
7014 g_hash_table_insert (domain->jit_trampoline_hash, method, tramp);
7015 mono_domain_unlock (domain);
7017 mono_jit_stats.method_trampolines++;
7022 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
7024 mono_create_jit_trampoline_from_token (MonoImage *image, guint32 token)
7028 tramp = mono_arch_create_jit_trampoline_from_token (image, token);
7030 mono_jit_stats.method_trampolines++;
7037 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
7041 EnterCriticalSection (&jit_mutex);
7042 if (class_init_hash_addr)
7043 res = g_hash_table_lookup (class_init_hash_addr, addr);
7046 LeaveCriticalSection (&jit_mutex);
7051 mono_dynamic_code_hash_insert (MonoDomain *domain, MonoMethod *method, MonoJitDynamicMethodInfo *ji)
7053 if (!domain->dynamic_code_hash)
7054 domain->dynamic_code_hash = g_hash_table_new (NULL, NULL);
7055 g_hash_table_insert (domain->dynamic_code_hash, method, ji);
7058 static MonoJitDynamicMethodInfo*
7059 mono_dynamic_code_hash_lookup (MonoDomain *domain, MonoMethod *method)
7061 MonoJitDynamicMethodInfo *res;
7063 if (domain->dynamic_code_hash)
7064 res = g_hash_table_lookup (domain->dynamic_code_hash, method);
7077 * mono_allocate_stack_slots:
7079 * Allocate stack slots for all non register allocated variables using a
7080 * linear scan algorithm.
7081 * Returns: an array of stack offsets which the caller should free.
7082 * STACK_SIZE is set to the amount of stack space needed.
7083 * STACK_ALIGN is set to the alignment needed by the locals area.
7086 mono_allocate_stack_slots (MonoCompile *m, guint32 *stack_size, guint32 *stack_align)
7088 int i, slot, offset, size, align;
7092 GList *vars = NULL, *l;
7093 StackSlotInfo *scalar_stack_slots, *vtype_stack_slots, *slot_info;
7097 scalar_stack_slots = g_new0 (StackSlotInfo, MONO_TYPE_PINNED);
7098 vtype_stack_slots = g_new0 (StackSlotInfo, 256);
7101 offsets = g_new (gint32, m->num_varinfo);
7102 for (i = 0; i < m->num_varinfo; ++i)
7105 for (i = m->locals_start; i < m->num_varinfo; i++) {
7106 inst = m->varinfo [i];
7107 vmv = MONO_VARINFO (m, i);
7109 if ((inst->flags & MONO_INST_IS_DEAD) || inst->opcode == OP_REGVAR || inst->opcode == OP_REGOFFSET)
7112 vars = g_list_prepend (vars, vmv);
7115 vars = mono_varlist_sort (m, vars, 0);
7118 for (l = vars; l; l = l->next) {
7120 inst = m->varinfo [vmv->idx];
7122 /* inst->unused indicates native sized value types, this is used by the
7123 * pinvoke wrappers when they call functions returning structures */
7124 if (inst->unused && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
7125 size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
7127 size = mono_type_size (inst->inst_vtype, &align);
7129 t = mono_type_get_underlying_type (inst->inst_vtype);
7131 case MONO_TYPE_VALUETYPE:
7132 for (i = 0; i < nvtypes; ++i)
7133 if (t->data.klass == vtype_stack_slots [i].vtype)
7136 slot_info = &vtype_stack_slots [i];
7138 g_assert (nvtypes < 256);
7139 vtype_stack_slots [nvtypes].vtype = t->data.klass;
7140 slot_info = &vtype_stack_slots [nvtypes];
7145 slot_info = &scalar_stack_slots [t->type];
7149 if (m->comp_done & MONO_COMP_LIVENESS) {
7150 //printf ("START %2d %08x %08x\n", vmv->idx, vmv->range.first_use.abs_pos, vmv->range.last_use.abs_pos);
7152 /* expire old intervals in active */
7153 while (slot_info->active) {
7154 MonoMethodVar *amv = (MonoMethodVar *)slot_info->active->data;
7156 if (amv->range.last_use.abs_pos > vmv->range.first_use.abs_pos)
7159 //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);
7161 slot_info->active = g_list_delete_link (slot_info->active, slot_info->active);
7162 slot_info->slots = g_list_prepend (slot_info->slots, GINT_TO_POINTER (offsets [amv->idx]));
7166 * This also handles the case when the variable is used in an
7167 * exception region, as liveness info is not computed there.
7170 * FIXME: All valuetypes are marked as INDIRECT because of LDADDR
7173 if (! (inst->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
7174 if (slot_info->slots) {
7175 slot = GPOINTER_TO_INT (slot_info->slots->data);
7177 slot_info->slots = g_list_delete_link (slot_info->slots, slot_info->slots);
7180 slot_info->active = mono_varlist_insert_sorted (m, slot_info->active, vmv, TRUE);
7185 static int count = 0;
7189 if (count == atoi (getenv ("COUNT")))
7190 printf ("LAST: %s\n", mono_method_full_name (m->method, TRUE));
7191 if (count > atoi (getenv ("COUNT")))
7194 mono_print_tree_nl (inst);
7198 if (slot == 0xffffff) {
7200 offset += align - 1;
7201 offset &= ~(align - 1);
7204 if (*stack_align == 0)
7205 *stack_align = align;
7208 offsets [vmv->idx] = slot;
7211 for (i = 0; i < MONO_TYPE_PINNED; ++i) {
7212 g_list_free (scalar_stack_slots [i].active);
7213 g_list_free (scalar_stack_slots [i].slots);
7215 for (i = 0; i < nvtypes; ++i) {
7216 g_list_free (vtype_stack_slots [i].active);
7217 g_list_free (vtype_stack_slots [i].slots);
7219 g_free (scalar_stack_slots);
7220 g_free (vtype_stack_slots);
7222 *stack_size = offset;
7227 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func, gboolean no_throw)
7229 MonoJitICallInfo *info;
7231 if (!emul_opcode_map)
7232 emul_opcode_map = g_new0 (MonoJitICallInfo*, OP_LAST + 1);
7234 g_assert (!sig->hasthis);
7235 g_assert (sig->param_count < 3);
7237 info = mono_register_jit_icall (func, name, sig, no_throw);
7239 emul_opcode_map [opcode] = info;
7243 decompose_foreach (MonoInst *tree, gpointer data)
7245 static MonoJitICallInfo *newarr_info = NULL;
7246 static MonoJitICallInfo *newarr_specific_info = NULL;
7247 MonoJitICallInfo *info;
7250 switch (tree->opcode) {
7252 MonoCompile *cfg = data;
7253 MonoInst *iargs [3];
7256 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
7257 g_assert (newarr_info);
7258 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
7259 g_assert (newarr_specific_info);
7262 if (cfg->opt & MONO_OPT_SHARED) {
7263 NEW_DOMAINCONST (cfg, iargs [0]);
7264 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
7265 iargs [2] = tree->inst_newa_len;
7270 MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
7272 NEW_VTABLECONST (cfg, iargs [0], vtable);
7273 iargs [1] = tree->inst_newa_len;
7275 info = newarr_specific_info;
7278 mono_emulate_opcode (cfg, tree, iargs, info);
7280 /* Need to decompose arguments after the the opcode is decomposed */
7281 for (i = 0; i < info->sig->param_count; ++i)
7282 dec_foreach (iargs [i], cfg);
7292 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
7294 switch (mono_burg_arity [tree->opcode]) {
7297 mono_inst_foreach (tree->inst_left, func, data);
7300 mono_inst_foreach (tree->inst_left, func, data);
7301 mono_inst_foreach (tree->inst_right, func, data);
7304 g_assert_not_reached ();
7311 mono_print_bb_code (MonoBasicBlock *bb) {
7313 MonoInst *c = bb->code;
7315 mono_print_tree (c);
7323 print_dfn (MonoCompile *cfg) {
7328 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
7330 for (i = 0; i < cfg->num_bblocks; ++i) {
7331 bb = cfg->bblocks [i];
7332 /*if (bb->cil_code) {
7333 char* code1, *code2;
7334 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
7335 if (bb->last_ins->cil_code)
7336 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
7338 code2 = g_strdup ("");
7340 code1 [strlen (code1) - 1] = 0;
7341 code = g_strdup_printf ("%s -> %s", code1, code2);
7345 code = g_strdup ("\n");
7346 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
7348 MonoInst *c = bb->code;
7350 mono_print_tree (c);
7358 g_print ("\tprev:");
7359 for (j = 0; j < bb->in_count; ++j) {
7360 g_print (" BB%d", bb->in_bb [j]->block_num);
7362 g_print ("\t\tsucc:");
7363 for (j = 0; j < bb->out_count; ++j) {
7364 g_print (" BB%d", bb->out_bb [j]->block_num);
7366 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
7369 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
7372 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
7374 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
7382 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
7386 g_assert (bb->code);
7387 bb->last_ins->next = inst;
7388 bb->last_ins = inst;
7390 bb->last_ins = bb->code = inst;
7395 mono_destroy_compile (MonoCompile *cfg)
7397 //mono_mempool_stats (cfg->mempool);
7398 g_hash_table_destroy (cfg->bb_hash);
7399 mono_free_loop_info (cfg);
7401 mono_regstate_free (cfg->rs);
7403 g_hash_table_destroy (cfg->spvars);
7405 g_hash_table_destroy (cfg->exvars);
7406 mono_mempool_destroy (cfg->mempool);
7407 g_list_free (cfg->ldstr_list);
7409 g_free (cfg->varinfo);
7414 #ifdef HAVE_KW_THREAD
7415 static __thread gpointer mono_lmf_addr MONO_TLS_FAST;
7419 mono_get_jit_tls_key (void)
7421 return mono_jit_tls_id;
7425 mono_get_lmf_tls_offset (void)
7428 MONO_THREAD_VAR_OFFSET(mono_lmf_addr,offset);
7433 mono_get_lmf_addr (void)
7435 #ifdef HAVE_KW_THREAD
7436 return mono_lmf_addr;
7438 MonoJitTlsData *jit_tls;
7440 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
7441 return &jit_tls->lmf;
7443 g_assert_not_reached ();
7448 /* Called by native->managed wrappers */
7450 mono_jit_thread_attach (MonoDomain *domain)
7452 #ifdef HAVE_KW_THREAD
7453 if (!mono_lmf_addr) {
7454 mono_thread_attach (domain);
7457 if (!TlsGetValue (mono_jit_tls_id))
7458 mono_thread_attach (domain);
7463 * mono_thread_abort:
7464 * @obj: exception object
7466 * abort the thread, print exception information and stack trace
7469 mono_thread_abort (MonoObject *obj)
7471 /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
7473 /* handle_remove should be eventually called for this thread, too
7476 mono_thread_exit ();
7480 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
7482 MonoJitTlsData *jit_tls;
7485 jit_tls = TlsGetValue (mono_jit_tls_id);
7489 jit_tls = g_new0 (MonoJitTlsData, 1);
7491 TlsSetValue (mono_jit_tls_id, jit_tls);
7493 jit_tls->abort_func = abort_func;
7494 jit_tls->end_of_stack = stack_start;
7496 lmf = g_new0 (MonoLMF, 1);
7499 jit_tls->lmf = jit_tls->first_lmf = lmf;
7501 #ifdef HAVE_KW_THREAD
7502 mono_lmf_addr = &jit_tls->lmf;
7505 mono_arch_setup_jit_tls_data (jit_tls);
7511 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
7514 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
7515 thread = mono_thread_current ();
7517 thread->jit_data = jit_tls;
7520 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
7523 mono_thread_abort_dummy (MonoObject *obj)
7525 if (mono_thread_attach_aborted_cb)
7526 mono_thread_attach_aborted_cb (obj);
7528 mono_thread_abort (obj);
7532 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
7535 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
7536 thread = mono_thread_current ();
7538 thread->jit_data = jit_tls;
7539 if (mono_profiler_get_events () & MONO_PROFILE_STATISTICAL)
7540 setup_stat_profiler ();
7544 mini_thread_cleanup (MonoThread *thread)
7546 MonoJitTlsData *jit_tls = thread->jit_data;
7549 mono_arch_free_jit_tls_data (jit_tls);
7550 g_free (jit_tls->first_lmf);
7552 thread->jit_data = NULL;
7557 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
7559 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
7563 ji->data.target = target;
7564 ji->next = cfg->patch_info;
7566 cfg->patch_info = ji;
7570 mono_remove_patch_info (MonoCompile *cfg, int ip)
7572 MonoJumpInfo **ji = &cfg->patch_info;
7575 if ((*ji)->ip.i == ip)
7578 ji = &((*ji)->next);
7583 mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors)
7585 unsigned char *ip = patch_info->ip.i + code;
7586 gconstpointer target = NULL;
7588 switch (patch_info->type) {
7589 case MONO_PATCH_INFO_BB:
7590 target = patch_info->data.bb->native_offset + code;
7592 case MONO_PATCH_INFO_ABS:
7593 target = patch_info->data.target;
7595 case MONO_PATCH_INFO_LABEL:
7596 target = patch_info->data.inst->inst_c0 + code;
7598 case MONO_PATCH_INFO_IP:
7601 case MONO_PATCH_INFO_METHOD_REL:
7602 target = code + patch_info->data.offset;
7604 case MONO_PATCH_INFO_INTERNAL_METHOD: {
7605 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
7607 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
7608 g_assert_not_reached ();
7610 target = mono_icall_get_wrapper (mi);
7613 case MONO_PATCH_INFO_METHOD_JUMP: {
7616 /* get the trampoline to the method from the domain */
7617 target = mono_create_jump_trampoline (domain, patch_info->data.method, TRUE);
7618 if (!domain->jump_target_hash)
7619 domain->jump_target_hash = g_hash_table_new (NULL, NULL);
7620 list = g_hash_table_lookup (domain->jump_target_hash, patch_info->data.method);
7621 list = g_slist_prepend (list, ip);
7622 g_hash_table_insert (domain->jump_target_hash, patch_info->data.method, list);
7625 case MONO_PATCH_INFO_METHOD:
7626 if (patch_info->data.method == method) {
7629 /* get the trampoline to the method from the domain */
7630 target = mono_create_jit_trampoline (patch_info->data.method);
7632 case MONO_PATCH_INFO_SWITCH: {
7633 gpointer *jump_table;
7636 if (method && method->dynamic) {
7637 jump_table = mono_code_manager_reserve (mono_dynamic_code_hash_lookup (domain, method)->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
7639 mono_domain_lock (domain);
7640 jump_table = mono_code_manager_reserve (domain->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
7641 mono_domain_unlock (domain);
7644 for (i = 0; i < patch_info->data.table->table_size; i++) {
7645 jump_table [i] = code + GPOINTER_TO_INT (patch_info->data.table->table [i]);
7647 target = jump_table;
7650 case MONO_PATCH_INFO_METHODCONST:
7651 case MONO_PATCH_INFO_CLASS:
7652 case MONO_PATCH_INFO_IMAGE:
7653 case MONO_PATCH_INFO_FIELD:
7654 target = patch_info->data.target;
7656 case MONO_PATCH_INFO_IID:
7657 mono_class_init (patch_info->data.klass);
7658 target = GINT_TO_POINTER ((int)patch_info->data.klass->interface_id);
7660 case MONO_PATCH_INFO_VTABLE:
7661 target = mono_class_vtable (domain, patch_info->data.klass);
7663 case MONO_PATCH_INFO_CLASS_INIT:
7664 target = mono_create_class_init_trampoline (mono_class_vtable (domain, patch_info->data.klass));
7666 case MONO_PATCH_INFO_SFLDA: {
7667 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
7668 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && (method && mono_class_needs_cctor_run (vtable->klass, method)))
7669 /* Done by the generated code */
7673 mono_runtime_class_init (vtable);
7675 target = (char*)vtable->data + patch_info->data.field->offset;
7678 case MONO_PATCH_INFO_R4:
7679 case MONO_PATCH_INFO_R8:
7680 target = patch_info->data.target;
7682 case MONO_PATCH_INFO_EXC_NAME:
7683 target = patch_info->data.name;
7685 case MONO_PATCH_INFO_LDSTR:
7687 mono_ldstr (domain, patch_info->data.token->image,
7688 mono_metadata_token_index (patch_info->data.token->token));
7690 case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
7692 MonoClass *handle_class;
7694 handle = mono_ldtoken (patch_info->data.token->image,
7695 patch_info->data.token->token, &handle_class, NULL);
7696 mono_class_init (handle_class);
7697 mono_class_init (mono_class_from_mono_type (handle));
7700 mono_type_get_object (domain, handle);
7703 case MONO_PATCH_INFO_LDTOKEN: {
7705 MonoClass *handle_class;
7707 handle = mono_ldtoken (patch_info->data.token->image,
7708 patch_info->data.token->token, &handle_class, NULL);
7709 mono_class_init (handle_class);
7714 case MONO_PATCH_INFO_DECLSEC:
7715 target = (mono_metadata_blob_heap (patch_info->data.token->image, patch_info->data.token->token) + 2);
7717 case MONO_PATCH_INFO_BB_OVF:
7718 case MONO_PATCH_INFO_EXC_OVF:
7719 case MONO_PATCH_INFO_GOT_OFFSET:
7720 case MONO_PATCH_INFO_NONE:
7723 g_assert_not_reached ();
7726 return (gpointer)target;
7730 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
7731 MonoJitICallInfo *info;
7733 decompose_foreach (tree, cfg);
7735 switch (mono_burg_arity [tree->opcode]) {
7738 dec_foreach (tree->inst_left, cfg);
7740 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
7741 MonoInst *iargs [2];
7743 iargs [0] = tree->inst_left;
7745 mono_emulate_opcode (cfg, tree, iargs, info);
7751 #ifdef MONO_ARCH_BIGMUL_INTRINS
7752 if (tree->opcode == OP_LMUL
7753 && (cfg->opt & MONO_OPT_INTRINS)
7754 && (tree->inst_left->opcode == CEE_CONV_I8
7755 || tree->inst_left->opcode == CEE_CONV_U8)
7756 && tree->inst_left->inst_left->type == STACK_I4
7757 && (tree->inst_right->opcode == CEE_CONV_I8
7758 || tree->inst_right->opcode == CEE_CONV_U8)
7759 && tree->inst_right->inst_left->type == STACK_I4
7760 && tree->inst_left->opcode == tree->inst_right->opcode) {
7761 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
7762 tree->inst_left = tree->inst_left->inst_left;
7763 tree->inst_right = tree->inst_right->inst_left;
7764 dec_foreach (tree, cfg);
7767 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
7768 MonoInst *iargs [2];
7770 iargs [0] = tree->inst_i0;
7771 iargs [1] = tree->inst_i1;
7773 mono_emulate_opcode (cfg, tree, iargs, info);
7775 dec_foreach (iargs [0], cfg);
7776 dec_foreach (iargs [1], cfg);
7779 dec_foreach (tree->inst_left, cfg);
7780 dec_foreach (tree->inst_right, cfg);
7784 g_assert_not_reached ();
7789 decompose_pass (MonoCompile *cfg) {
7792 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7795 cfg->prev_ins = NULL;
7796 for (tree = cfg->cbb->code; tree; tree = tree->next) {
7797 dec_foreach (tree, cfg);
7798 cfg->prev_ins = tree;
7804 nullify_basic_block (MonoBasicBlock *bb)
7811 bb->code = bb->last_ins = NULL;
7812 bb->cil_code = NULL;
7816 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
7820 for (i = 0; i < bb->out_count; i++) {
7821 MonoBasicBlock *ob = bb->out_bb [i];
7824 if (bb->out_count > 1) {
7825 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
7829 bb->out_bb [i] = repl;
7836 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
7840 for (i = 0; i < bb->in_count; i++) {
7841 MonoBasicBlock *ib = bb->in_bb [i];
7844 if (bb->in_count > 1) {
7845 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
7849 bb->in_bb [i] = repl;
7856 replace_or_add_in_block (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
7858 gboolean found = FALSE;
7861 for (i = 0; i < bb->in_count; i++) {
7862 MonoBasicBlock *ib = bb->in_bb [i];
7865 if (bb->in_count > 1) {
7866 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
7870 bb->in_bb [i] = repl;
7877 MonoBasicBlock **new_in_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (bb->in_count + 1));
7878 for (i = 0; i < bb->in_count; i++) {
7879 new_in_bb [i] = bb->in_bb [i];
7881 new_in_bb [i] = repl;
7883 bb->in_bb = new_in_bb;
7889 replace_out_block_in_code (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl) {
7892 for (inst = bb->code; inst != NULL; inst = inst->next) {
7893 if (inst->opcode == OP_CALL_HANDLER) {
7894 if (inst->inst_target_bb == orig) {
7895 inst->inst_target_bb = repl;
7899 if (bb->last_ins != NULL) {
7900 switch (bb->last_ins->opcode) {
7902 if (bb->last_ins->inst_target_bb == orig) {
7903 bb->last_ins->inst_target_bb = repl;
7908 int n = GPOINTER_TO_INT (bb->last_ins->klass);
7909 for (i = 0; i < n; i++ ) {
7910 if (bb->last_ins->inst_many_bb [i] == orig) {
7911 bb->last_ins->inst_many_bb [i] = repl;
7926 if (bb->last_ins->inst_true_bb == orig) {
7927 bb->last_ins->inst_true_bb = repl;
7929 if (bb->last_ins->inst_false_bb == orig) {
7930 bb->last_ins->inst_false_bb = repl;
7940 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
7944 for (i = 0; i < bb->out_count; i++) {
7945 MonoBasicBlock *ob = bb->out_bb [i];
7946 for (j = 0; j < ob->in_count; j++) {
7947 if (ob->in_bb [j] == orig) {
7948 ob->in_bb [j] = repl;
7956 * Check if a bb is useless (is just made of NOPs and ends with an
7957 * unconditional branch, or nothing).
7958 * If it is so, unlink it from the CFG and nullify it, and return TRUE.
7959 * Otherwise, return FALSE;
7962 remove_block_if_useless (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *previous_bb) {
7963 MonoBasicBlock *target_bb = NULL;
7966 /* Do not touch handlers */
7967 if (bb->region != -1) return FALSE;
7969 for (inst = bb->code; inst != NULL; inst = inst->next) {
7970 switch (inst->opcode) {
7974 target_bb = inst->inst_target_bb;
7981 if (target_bb == NULL) {
7982 if ((bb->out_count == 1) && (bb->out_bb [0] == bb->next_bb)) {
7983 target_bb = bb->next_bb;
7985 /* Do not touch empty BBs that do not "fall through" to their next BB (like the exit BB) */
7990 /* Do not touch BBs following a switch (they are the "default" branch) */
7991 if ((previous_bb->last_ins != NULL) && (previous_bb->last_ins->opcode == CEE_SWITCH)) {
7995 /* Do not touch BBs following the entry BB and jumping to something that is not */
7996 /* thiry "next" bb (the entry BB cannot contain the branch) */
7997 if ((previous_bb == cfg->bb_entry) && (bb->next_bb != target_bb)) {
8001 /* Check that there is a target BB, and that bb is not an empty loop (Bug 75061) */
8002 if ((target_bb != NULL) && (target_bb != bb)) {
8005 if (cfg->verbose_level > 1) {
8006 printf ("remove_block_if_useless %s, removed BB%d\n", mono_method_full_name (cfg->method, TRUE), bb->block_num);
8009 for (i = 0; i < bb->in_count; i++) {
8010 MonoBasicBlock *in_bb = bb->in_bb [i];
8011 replace_out_block (in_bb, bb, target_bb);
8012 replace_out_block_in_code (in_bb, bb, target_bb);
8013 if (bb->in_count == 1) {
8014 replace_in_block (target_bb, bb, in_bb);
8016 replace_or_add_in_block (cfg, target_bb, bb, in_bb);
8020 if ((previous_bb != cfg->bb_entry) &&
8021 (previous_bb->region == bb->region) &&
8022 ((previous_bb->last_ins == NULL) ||
8023 ((previous_bb->last_ins->opcode != CEE_BR) &&
8024 (! (MONO_IS_COND_BRANCH_OP (previous_bb->last_ins))) &&
8025 (previous_bb->last_ins->opcode != CEE_SWITCH)))) {
8026 for (i = 0; i < previous_bb->out_count; i++) {
8027 if (previous_bb->out_bb [i] == target_bb) {
8029 MONO_INST_NEW (cfg, jump, CEE_BR);
8030 MONO_ADD_INS (previous_bb, jump);
8031 jump->cil_code = previous_bb->cil_code;
8032 jump->inst_target_bb = target_bb;
8038 previous_bb->next_bb = bb->next_bb;
8039 nullify_basic_block (bb);
8048 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
8050 bb->out_count = bbn->out_count;
8051 bb->out_bb = bbn->out_bb;
8053 replace_basic_block (bb, bbn, bb);
8057 bb->last_ins->next = bbn->code;
8058 bb->last_ins = bbn->last_ins;
8061 bb->code = bbn->code;
8062 bb->last_ins = bbn->last_ins;
8064 bb->next_bb = bbn->next_bb;
8065 nullify_basic_block (bbn);
8069 move_basic_block_to_end (MonoCompile *cfg, MonoBasicBlock *bb)
8071 MonoBasicBlock *bbn, *next;
8075 /* Find the previous */
8076 for (bbn = cfg->bb_entry; bbn->next_bb && bbn->next_bb != bb; bbn = bbn->next_bb)
8079 bbn->next_bb = bb->next_bb;
8083 for (bbn = cfg->bb_entry; bbn->next_bb; bbn = bbn->next_bb)
8089 if (next && (!bb->last_ins || (bb->last_ins->opcode != OP_NOT_REACHED))) {
8092 MONO_INST_NEW (cfg, ins, CEE_BR);
8093 MONO_ADD_INS (bb, ins);
8094 link_bblock (cfg, bb, next);
8095 ins->inst_target_bb = next;
8100 * Optimizes the branches on the Control Flow Graph
8104 optimize_branches (MonoCompile *cfg)
8106 int i, changed = FALSE;
8107 MonoBasicBlock *bb, *bbn;
8108 guint32 niterations;
8111 * Some crazy loops could cause the code below to go into an infinite
8112 * loop, see bug #53003 for an example. To prevent this, we put an upper
8113 * bound on the number of iterations.
8117 MonoBasicBlock *previous_bb;
8121 /* we skip the entry block (exit is handled specially instead ) */
8122 for (previous_bb = cfg->bb_entry, bb = cfg->bb_entry->next_bb; bb; previous_bb = bb, bb = bb->next_bb) {
8124 /* dont touch code inside exception clauses */
8125 if (bb->region != -1)
8128 if (remove_block_if_useless (cfg, bb, previous_bb)) {
8133 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
8134 if (cfg->verbose_level > 2)
8135 g_print ("nullify block triggered %d\n", bbn->block_num);
8137 bb->next_bb = bbn->next_bb;
8139 for (i = 0; i < bbn->out_count; i++)
8140 replace_in_block (bbn->out_bb [i], bbn, NULL);
8142 nullify_basic_block (bbn);
8146 if (bb->out_count == 1) {
8147 bbn = bb->out_bb [0];
8149 /* conditional branches where true and false targets are the same can be also replaced with CEE_BR */
8150 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins)) {
8152 MONO_INST_NEW (cfg, pop, CEE_POP);
8153 pop->inst_left = bb->last_ins->inst_left->inst_left;
8154 mono_add_ins_to_end (bb, pop);
8155 MONO_INST_NEW (cfg, pop, CEE_POP);
8156 pop->inst_left = bb->last_ins->inst_left->inst_right;
8157 mono_add_ins_to_end (bb, pop);
8158 bb->last_ins->opcode = CEE_BR;
8159 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
8161 if (cfg->verbose_level > 2)
8162 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
8165 if (bb->region == bbn->region && bb->next_bb == bbn) {
8166 /* the block are in sequence anyway ... */
8168 /* branches to the following block can be removed */
8169 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
8170 bb->last_ins->opcode = CEE_NOP;
8172 if (cfg->verbose_level > 2)
8173 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
8176 if (bbn->in_count == 1) {
8178 if (bbn != cfg->bb_exit) {
8179 if (cfg->verbose_level > 2)
8180 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
8181 merge_basic_blocks (bb, bbn);
8185 //mono_print_bb_code (bb);
8190 } while (changed && (niterations > 0));
8197 /* we skip the entry block (exit is handled specially instead ) */
8198 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
8200 /* dont touch code inside exception clauses */
8201 if (bb->region != -1)
8204 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
8205 if (cfg->verbose_level > 2) {
8206 g_print ("nullify block triggered %d\n", bbn->block_num);
8208 bb->next_bb = bbn->next_bb;
8210 for (i = 0; i < bbn->out_count; i++)
8211 replace_in_block (bbn->out_bb [i], bbn, NULL);
8213 nullify_basic_block (bbn);
8219 if (bb->out_count == 1) {
8220 bbn = bb->out_bb [0];
8222 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
8223 bbn = bb->last_ins->inst_target_bb;
8224 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
8225 bbn->code->inst_target_bb->region == bb->region) {
8227 if (cfg->verbose_level > 2)
8228 g_print ("in %s branch to branch triggered %d -> %d -> %d\n", cfg->method->name,
8229 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num);
8231 replace_in_block (bbn, bb, NULL);
8232 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
8233 link_bblock (cfg, bb, bbn->code->inst_target_bb);
8234 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
8239 } else if (bb->out_count == 2) {
8240 if (bb->last_ins && MONO_IS_COND_BRANCH_NOFP (bb->last_ins)) {
8241 int branch_result = mono_eval_cond_branch (bb->last_ins);
8242 MonoBasicBlock *taken_branch_target = NULL, *untaken_branch_target = NULL;
8243 if (branch_result == BRANCH_TAKEN) {
8244 taken_branch_target = bb->last_ins->inst_true_bb;
8245 untaken_branch_target = bb->last_ins->inst_false_bb;
8246 } else if (branch_result == BRANCH_NOT_TAKEN) {
8247 taken_branch_target = bb->last_ins->inst_false_bb;
8248 untaken_branch_target = bb->last_ins->inst_true_bb;
8250 if (taken_branch_target) {
8251 /* if mono_eval_cond_branch () is ever taken to handle
8252 * non-constant values to compare, issue a pop here.
8254 bb->last_ins->opcode = CEE_BR;
8255 bb->last_ins->inst_target_bb = taken_branch_target;
8256 replace_out_block (bb, untaken_branch_target, NULL);
8257 replace_in_block (untaken_branch_target, bb, NULL);
8261 bbn = bb->last_ins->inst_true_bb;
8262 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
8263 bbn->code->inst_target_bb->region == bb->region) {
8264 if (cfg->verbose_level > 2)
8265 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
8266 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
8269 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
8271 replace_in_block (bbn, bb, NULL);
8273 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
8274 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
8276 link_bblock (cfg, bb, bbn->code->inst_target_bb);
8282 bbn = bb->last_ins->inst_false_bb;
8283 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
8284 bbn->code->inst_target_bb->region == bb->region) {
8285 if (cfg->verbose_level > 2)
8286 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
8287 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
8290 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
8292 replace_in_block (bbn, bb, NULL);
8294 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
8295 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
8297 link_bblock (cfg, bb, bbn->code->inst_target_bb);
8304 if (bb->last_ins && MONO_IS_COND_BRANCH_NOFP (bb->last_ins)) {
8305 if (bb->last_ins->inst_false_bb->out_of_line && (bb->region == bb->last_ins->inst_false_bb->region)) {
8306 /* Reverse the branch */
8307 bb->last_ins->opcode = reverse_branch_op (bb->last_ins->opcode);
8308 bbn = bb->last_ins->inst_false_bb;
8309 bb->last_ins->inst_false_bb = bb->last_ins->inst_true_bb;
8310 bb->last_ins->inst_true_bb = bbn;
8312 move_basic_block_to_end (cfg, bb->last_ins->inst_true_bb);
8313 if (cfg->verbose_level > 2)
8314 g_print ("cbranch to throw block triggered %d.\n",
8320 } while (changed && (niterations > 0));
8325 mono_compile_create_vars (MonoCompile *cfg)
8327 MonoMethodSignature *sig;
8328 MonoMethodHeader *header;
8331 header = mono_method_get_header (cfg->method);
8333 sig = mono_method_signature (cfg->method);
8335 if (!MONO_TYPE_IS_VOID (sig->ret)) {
8336 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
8337 cfg->ret->opcode = OP_RETARG;
8338 cfg->ret->inst_vtype = sig->ret;
8339 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
8341 if (cfg->verbose_level > 2)
8342 g_print ("creating vars\n");
8345 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
8347 for (i = 0; i < sig->param_count; ++i) {
8348 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
8349 if (sig->params [i]->byref) {
8350 cfg->disable_ssa = TRUE;
8354 cfg->locals_start = cfg->num_varinfo;
8356 if (cfg->verbose_level > 2)
8357 g_print ("creating locals\n");
8358 for (i = 0; i < header->num_locals; ++i)
8359 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
8360 if (cfg->verbose_level > 2)
8361 g_print ("locals done\n");
8363 #ifdef MONO_ARCH_HAVE_CREATE_VARS
8364 mono_arch_create_vars (cfg);
8369 mono_print_code (MonoCompile *cfg)
8373 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
8374 MonoInst *tree = bb->code;
8379 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
8381 for (; tree; tree = tree->next) {
8382 mono_print_tree (tree);
8387 bb->last_ins->next = NULL;
8391 extern const char * const mono_burg_rule_string [];
8394 emit_state (MonoCompile *cfg, MBState *state, int goal)
8397 int ern = mono_burg_rule (state, goal);
8398 const guint16 *nts = mono_burg_nts [ern];
8401 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
8405 // state->reg1 = state->reg2; /* chain rule */
8407 #ifdef MONO_ARCH_ENABLE_EMIT_STATE_OPT
8410 state->reg1 = mono_regstate_next_int (cfg->rs);
8411 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
8414 state->reg1 = mono_regstate_next_int (cfg->rs);
8415 state->reg2 = mono_regstate_next_int (cfg->rs);
8418 state->reg1 = mono_regstate_next_float (cfg->rs);
8421 #ifdef MONO_ARCH_ENABLE_EMIT_STATE_OPT
8423 * Enabling this might cause bugs to surface in the local register
8424 * allocators on some architectures like x86.
8426 if ((state->tree->ssa_op == MONO_SSA_STORE) && (state->left->tree->opcode == OP_REGVAR)) {
8427 /* Do not optimize away reg-reg moves */
8428 if (! ((state->right->tree->ssa_op == MONO_SSA_LOAD) && (state->right->left->tree->opcode == OP_REGVAR))) {
8429 state->right->reg1 = state->left->tree->dreg;
8438 mono_burg_kids (state, ern, kids);
8440 emit_state (cfg, kids [0], nts [0]);
8442 emit_state (cfg, kids [1], nts [1]);
8444 g_assert (!nts [3]);
8445 emit_state (cfg, kids [2], nts [2]);
8450 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
8451 if ((emit = mono_burg_func [ern]))
8452 emit (state, state->tree, cfg);
8455 #define DEBUG_SELECTION
8458 mini_select_instructions (MonoCompile *cfg)
8462 cfg->state_pool = mono_mempool_new ();
8463 cfg->rs = mono_regstate_new ();
8465 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
8466 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins) &&
8467 bb->next_bb != bb->last_ins->inst_false_bb) {
8469 /* we are careful when inverting, since bugs like #59580
8470 * could show up when dealing with NaNs.
8472 if (MONO_IS_COND_BRANCH_NOFP(bb->last_ins) && bb->next_bb == bb->last_ins->inst_true_bb) {
8473 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
8474 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
8475 bb->last_ins->inst_false_bb = tmp;
8477 bb->last_ins->opcode = reverse_branch_op (bb->last_ins->opcode);
8479 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
8480 inst->opcode = CEE_BR;
8481 inst->inst_target_bb = bb->last_ins->inst_false_bb;
8482 mono_bblock_add_inst (bb, inst);
8487 #ifdef DEBUG_SELECTION
8488 if (cfg->verbose_level >= 4) {
8489 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
8490 MonoInst *tree = bb->code;
8491 g_print ("DUMP BLOCK %d:\n", bb->block_num);
8494 for (; tree; tree = tree->next) {
8495 mono_print_tree (tree);
8502 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
8503 MonoInst *tree = bb->code, *next;
8509 bb->last_ins = NULL;
8512 mono_regstate_reset (cfg->rs);
8514 #ifdef DEBUG_SELECTION
8515 if (cfg->verbose_level >= 3)
8516 g_print ("LABEL BLOCK %d:\n", bb->block_num);
8518 for (; tree; tree = next) {
8520 #ifdef DEBUG_SELECTION
8521 if (cfg->verbose_level >= 3) {
8522 mono_print_tree (tree);
8527 if (!(mbstate = mono_burg_label (tree, cfg))) {
8528 g_warning ("unable to label tree %p", tree);
8529 mono_print_tree (tree);
8531 g_assert_not_reached ();
8533 emit_state (cfg, mbstate, MB_NTERM_stmt);
8535 bb->max_ireg = cfg->rs->next_vireg;
8536 bb->max_freg = cfg->rs->next_vfreg;
8539 bb->last_ins->next = NULL;
8541 mono_mempool_empty (cfg->state_pool);
8543 mono_mempool_destroy (cfg->state_pool);
8547 mono_codegen (MonoCompile *cfg)
8549 MonoJumpInfo *patch_info;
8551 int i, max_epilog_size;
8554 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
8555 cfg->spill_count = 0;
8556 /* we reuse dfn here */
8557 /* bb->dfn = bb_count++; */
8558 mono_arch_local_regalloc (cfg, bb);
8561 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
8562 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
8564 code = mono_arch_emit_prolog (cfg);
8566 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
8567 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
8569 cfg->code_len = code - cfg->native_code;
8570 cfg->prolog_end = cfg->code_len;
8572 mono_debug_open_method (cfg);
8574 /* emit code all basic blocks */
8575 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
8576 bb->native_offset = cfg->code_len;
8577 mono_arch_output_basic_block (cfg, bb);
8579 if (bb == cfg->bb_exit) {
8580 cfg->epilog_begin = cfg->code_len;
8582 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE) {
8583 code = cfg->native_code + cfg->code_len;
8584 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
8585 cfg->code_len = code - cfg->native_code;
8588 mono_arch_emit_epilog (cfg);
8592 mono_arch_emit_exceptions (cfg);
8594 max_epilog_size = 0;
8596 code = cfg->native_code + cfg->code_len;
8598 /* we always allocate code in cfg->domain->code_mp to increase locality */
8599 cfg->code_size = cfg->code_len + max_epilog_size;
8600 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
8602 if (cfg->method->dynamic) {
8603 /* Allocate the code into a separate memory pool so it can be freed */
8604 cfg->dynamic_info = g_new0 (MonoJitDynamicMethodInfo, 1);
8605 cfg->dynamic_info->code_mp = mono_code_manager_new_dynamic ();
8606 mono_domain_lock (cfg->domain);
8607 mono_dynamic_code_hash_insert (cfg->domain, cfg->method, cfg->dynamic_info);
8608 mono_domain_unlock (cfg->domain);
8610 code = mono_code_manager_reserve (cfg->dynamic_info->code_mp, cfg->code_size);
8612 mono_domain_lock (cfg->domain);
8613 code = mono_code_manager_reserve (cfg->domain->code_mp, cfg->code_size);
8614 mono_domain_unlock (cfg->domain);
8617 memcpy (code, cfg->native_code, cfg->code_len);
8618 g_free (cfg->native_code);
8619 cfg->native_code = code;
8620 code = cfg->native_code + cfg->code_len;
8622 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
8623 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
8624 switch (patch_info->type) {
8625 case MONO_PATCH_INFO_ABS: {
8626 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
8628 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
8629 if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) &&
8630 strstr (cfg->method->name, info->name))
8632 * This is an icall wrapper, and this is a call to the
8637 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
8638 patch_info->data.name = info->name;
8642 MonoVTable *vtable = mono_find_class_init_trampoline_by_addr (patch_info->data.target);
8644 patch_info->type = MONO_PATCH_INFO_CLASS_INIT;
8645 patch_info->data.klass = vtable->klass;
8650 case MONO_PATCH_INFO_SWITCH: {
8652 if (cfg->method->dynamic) {
8653 table = mono_code_manager_reserve (cfg->dynamic_info->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
8655 mono_domain_lock (cfg->domain);
8656 table = mono_code_manager_reserve (cfg->domain->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
8657 mono_domain_unlock (cfg->domain);
8660 if (!cfg->compile_aot)
8661 /* In the aot case, the patch already points to the correct location */
8662 patch_info->ip.i = patch_info->ip.label->inst_c0;
8663 for (i = 0; i < patch_info->data.table->table_size; i++) {
8664 table [i] = GINT_TO_POINTER (patch_info->data.table->table [i]->native_offset);
8666 patch_info->data.table->table = (MonoBasicBlock**)table;
8675 if (cfg->verbose_level > 0) {
8676 char* nm = mono_method_full_name (cfg->method, TRUE);
8677 g_print ("Method %s emitted at %p to %p [%s]\n",
8679 cfg->native_code, cfg->native_code + cfg->code_len, cfg->domain->friendly_name);
8683 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info, cfg->run_cctors);
8685 if (cfg->method->dynamic) {
8686 mono_code_manager_commit (cfg->dynamic_info->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
8688 mono_domain_lock (cfg->domain);
8689 mono_code_manager_commit (cfg->domain->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
8690 mono_domain_unlock (cfg->domain);
8693 mono_arch_flush_icache (cfg->native_code, cfg->code_len);
8695 mono_debug_close_method (cfg);
8699 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
8704 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
8705 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
8707 if (cp->opcode == OP_ICONST) {
8708 if (cfg->opt & MONO_OPT_CONSPROP) {
8709 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
8713 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
8714 if (cfg->opt & MONO_OPT_COPYPROP) {
8715 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
8721 arity = mono_burg_arity [tree->opcode];
8724 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
8725 if (cfg->opt & MONO_OPT_CFOLD)
8726 mono_constant_fold_inst (tree, NULL);
8727 /* The opcode may have changed */
8728 if (mono_burg_arity [tree->opcode] > 1) {
8729 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
8730 if (cfg->opt & MONO_OPT_CFOLD)
8731 mono_constant_fold_inst (tree, NULL);
8733 mono_constant_fold_inst (tree, NULL);
8739 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
8743 switch (tree->opcode) {
8753 if (tree->ssa_op == MONO_SSA_NOP) {
8754 memset (acp, 0, sizeof (MonoInst *) * acp_size);
8771 case OP_VOIDCALL_REG:
8772 case OP_VOIDCALLVIRT:
8774 MonoCallInst *call = (MonoCallInst *)tree;
8775 MonoMethodSignature *sig = call->signature;
8776 int i, byref = FALSE;
8778 for (i = 0; i < sig->param_count; i++) {
8779 if (sig->params [i]->byref) {
8786 memset (acp, 0, sizeof (MonoInst *) * acp_size);
8794 arity = mono_burg_arity [tree->opcode];
8800 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
8803 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
8804 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
8807 g_assert_not_reached ();
8812 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
8814 MonoInst *tree = bb->code;
8820 for (; tree; tree = tree->next) {
8822 mono_cprop_copy_values (cfg, tree, acp);
8824 mono_cprop_invalidate_values (tree, acp, acp_size);
8826 if (tree->ssa_op == MONO_SSA_STORE &&
8827 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
8828 MonoInst *i1 = tree->inst_i1;
8830 acp [tree->inst_i0->inst_c0] = NULL;
8832 for (i = 0; i < acp_size; i++) {
8833 if (acp [i] && acp [i]->opcode != OP_ICONST &&
8834 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
8839 if (i1->opcode == OP_ICONST) {
8840 acp [tree->inst_i0->inst_c0] = i1;
8841 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
8843 if (i1->ssa_op == MONO_SSA_LOAD &&
8844 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
8845 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
8846 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
8847 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
8852 if (tree->opcode == CEE_BEQ) {
8853 g_assert (tree->inst_i0->opcode == OP_COMPARE);
8854 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
8855 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
8857 tree->opcode = CEE_BR;
8858 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
8859 tree->inst_target_bb = tree->inst_true_bb;
8861 tree->inst_target_bb = tree->inst_false_bb;
8870 mono_local_cprop (MonoCompile *cfg)
8875 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
8877 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
8878 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
8879 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
8884 remove_critical_edges (MonoCompile *cfg) {
8886 MonoBasicBlock *previous_bb;
8888 if (cfg->verbose_level > 3) {
8889 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
8891 printf ("remove_critical_edges %s, BEFORE BB%d (in:", mono_method_full_name (cfg->method, TRUE), bb->block_num);
8892 for (i = 0; i < bb->in_count; i++) {
8893 printf (" %d", bb->in_bb [i]->block_num);
8896 for (i = 0; i < bb->out_count; i++) {
8897 printf (" %d", bb->out_bb [i]->block_num);
8900 if (bb->last_ins != NULL) {
8902 mono_print_tree (bb->last_ins);
8908 for (previous_bb = cfg->bb_entry, bb = previous_bb->next_bb; bb != NULL; previous_bb = previous_bb->next_bb, bb = bb->next_bb) {
8909 if (bb->in_count > 1) {
8911 for (in_bb_index = 0; in_bb_index < bb->in_count; in_bb_index++) {
8912 MonoBasicBlock *in_bb = bb->in_bb [in_bb_index];
8913 if (in_bb->out_count > 1) {
8914 MonoBasicBlock *new_bb = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock));
8915 new_bb->block_num = cfg->num_bblocks++;
8916 // new_bb->real_offset = bb->real_offset;
8917 new_bb->region = bb->region;
8919 /* Do not alter the CFG while altering the BB list */
8920 if (previous_bb->region == bb->region) {
8921 if (previous_bb != cfg->bb_entry) {
8922 /* If previous_bb "followed through" to bb, */
8923 /* keep it linked with a CEE_BR */
8924 if ((previous_bb->last_ins == NULL) ||
8925 ((previous_bb->last_ins->opcode != CEE_BR) &&
8926 (! (MONO_IS_COND_BRANCH_OP (previous_bb->last_ins))) &&
8927 (previous_bb->last_ins->opcode != CEE_SWITCH))) {
8929 /* Make sure previous_bb really falls through bb */
8930 for (i = 0; i < previous_bb->out_count; i++) {
8931 if (previous_bb->out_bb [i] == bb) {
8933 MONO_INST_NEW (cfg, jump, CEE_BR);
8934 MONO_ADD_INS (previous_bb, jump);
8935 jump->cil_code = previous_bb->cil_code;
8936 jump->inst_target_bb = bb;
8942 /* We cannot add any inst to the entry BB, so we must */
8943 /* put a new BB in the middle to hold the CEE_BR */
8945 MonoBasicBlock *new_bb_after_entry = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock));
8946 new_bb_after_entry->block_num = cfg->num_bblocks++;
8947 // new_bb_after_entry->real_offset = bb->real_offset;
8948 new_bb_after_entry->region = bb->region;
8950 MONO_INST_NEW (cfg, jump, CEE_BR);
8951 MONO_ADD_INS (new_bb_after_entry, jump);
8952 jump->cil_code = bb->cil_code;
8953 jump->inst_target_bb = bb;
8955 previous_bb->next_bb = new_bb_after_entry;
8956 previous_bb = new_bb_after_entry;
8958 if (cfg->verbose_level > 2) {
8959 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);
8964 /* Insert new_bb in the BB list */
8965 previous_bb->next_bb = new_bb;
8966 new_bb->next_bb = bb;
8967 previous_bb = new_bb;
8969 /* Setup in_bb and out_bb */
8970 new_bb->in_bb = mono_mempool_alloc ((cfg)->mempool, sizeof (MonoBasicBlock*));
8971 new_bb->in_bb [0] = in_bb;
8972 new_bb->in_count = 1;
8973 new_bb->out_bb = mono_mempool_alloc ((cfg)->mempool, sizeof (MonoBasicBlock*));
8974 new_bb->out_bb [0] = bb;
8975 new_bb->out_count = 1;
8977 /* Relink in_bb and bb to (from) new_bb */
8978 replace_out_block (in_bb, bb, new_bb);
8979 replace_out_block_in_code (in_bb, bb, new_bb);
8980 replace_in_block (bb, in_bb, new_bb);
8982 if (cfg->verbose_level > 2) {
8983 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);
8990 if (cfg->verbose_level > 3) {
8991 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
8993 printf ("remove_critical_edges %s, AFTER BB%d (in:", mono_method_full_name (cfg->method, TRUE), bb->block_num);
8994 for (i = 0; i < bb->in_count; i++) {
8995 printf (" %d", bb->in_bb [i]->block_num);
8998 for (i = 0; i < bb->out_count; i++) {
8999 printf (" %d", bb->out_bb [i]->block_num);
9002 if (bb->last_ins != NULL) {
9004 mono_print_tree (bb->last_ins);
9012 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, gboolean compile_aot, int parts)
9014 MonoMethodHeader *header = mono_method_get_header (method);
9015 guint8 *ip = (guint8 *)header->code;
9018 int dfn = 0, i, code_size_ratio;
9020 mono_jit_stats.methods_compiled++;
9021 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
9022 mono_profiler_method_jit (method);
9024 cfg = g_new0 (MonoCompile, 1);
9025 cfg->method = method;
9026 cfg->mempool = mono_mempool_new ();
9028 cfg->prof_options = mono_profiler_get_events ();
9029 cfg->run_cctors = run_cctors;
9030 cfg->bb_hash = g_hash_table_new (NULL, NULL);
9031 cfg->domain = domain;
9032 cfg->verbose_level = mini_verbose;
9033 cfg->compile_aot = compile_aot;
9034 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
9035 mono_method_get_header (method)->max_stack);
9037 if (cfg->verbose_level > 2)
9038 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
9041 * create MonoInst* which represents arguments and local variables
9043 mono_compile_create_vars (cfg);
9045 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
9046 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
9047 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
9048 mono_destroy_compile (cfg);
9052 mono_jit_stats.basic_blocks += cfg->num_bblocks;
9053 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
9055 if ((cfg->num_varinfo > 2000) && !mono_compile_aot) {
9057 * we disable some optimizations if there are too many variables
9058 * because JIT time may become too expensive. The actual number needs
9059 * to be tweaked and eventually the non-linear algorithms should be fixed.
9061 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
9062 cfg->disable_ssa = TRUE;
9065 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
9067 if (cfg->opt & MONO_OPT_BRANCH)
9068 optimize_branches (cfg);
9070 if (cfg->opt & MONO_OPT_SSAPRE) {
9071 remove_critical_edges (cfg);
9074 /* Depth-first ordering on basic blocks */
9075 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
9077 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
9078 if (cfg->num_bblocks != dfn + 1) {
9081 cfg->num_bblocks = dfn + 1;
9083 if (!header->clauses) {
9084 /* remove unreachable code, because the code in them may be
9085 * inconsistent (access to dead variables for example) */
9086 for (bb = cfg->bb_entry; bb;) {
9087 MonoBasicBlock *bbn = bb->next_bb;
9089 if (bbn && bbn->region == -1 && !bbn->dfn) {
9090 if (cfg->verbose_level > 1)
9091 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
9092 bb->next_bb = bbn->next_bb;
9093 nullify_basic_block (bbn);
9101 if (cfg->opt & MONO_OPT_LOOP) {
9102 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
9103 mono_compute_natural_loops (cfg);
9106 /* after method_to_ir */
9110 //#define DEBUGSSA "logic_run"
9111 #define DEBUGSSA_CLASS "Tests"
9114 if (!header->num_clauses && !cfg->disable_ssa) {
9115 mono_local_cprop (cfg);
9116 mono_ssa_compute (cfg);
9120 /* fixme: add all optimizations which requires SSA */
9121 if (cfg->opt & (MONO_OPT_DEADCE | MONO_OPT_ABCREM | MONO_OPT_SSAPRE)) {
9122 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
9123 mono_local_cprop (cfg);
9124 mono_ssa_compute (cfg);
9126 if (cfg->verbose_level >= 2) {
9133 /* after SSA translation */
9137 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
9138 if (cfg->comp_done & MONO_COMP_SSA) {
9139 mono_ssa_cprop (cfg);
9141 mono_local_cprop (cfg);
9145 if (cfg->comp_done & MONO_COMP_SSA) {
9146 mono_ssa_deadce (cfg);
9148 //mono_ssa_strength_reduction (cfg);
9150 if ((cfg->flags & MONO_CFG_HAS_LDELEMA) && (cfg->opt & MONO_OPT_ABCREM))
9151 mono_perform_abc_removal (cfg);
9153 if (cfg->opt & MONO_OPT_SSAPRE)
9154 mono_perform_ssapre (cfg);
9156 mono_ssa_remove (cfg);
9158 if (cfg->opt & MONO_OPT_BRANCH)
9159 optimize_branches (cfg);
9162 /* after SSA removal */
9166 decompose_pass (cfg);
9171 g_assert (cfg->got_var_allocated);
9174 * Allways allocate the GOT var to a register, because keeping it
9175 * in memory will increase the number of live temporaries in some
9176 * code created by inssel.brg, leading to the well known spills+
9177 * branches problem. Testcase: mcs crash in
9178 * System.MonoCustomAttrs:GetCustomAttributes.
9180 regs = mono_arch_get_global_int_regs (cfg);
9182 cfg->got_var->opcode = OP_REGVAR;
9183 cfg->got_var->dreg = GPOINTER_TO_INT (regs->data);
9184 cfg->used_int_regs |= 1LL << cfg->got_var->dreg;
9189 if (cfg->opt & MONO_OPT_LINEARS) {
9192 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
9193 cfg->comp_done &= ~MONO_COMP_LIVENESS;
9194 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
9195 mono_analyze_liveness (cfg);
9197 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
9198 regs = mono_arch_get_global_int_regs (cfg);
9200 regs = g_list_delete_link (regs, regs);
9201 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
9205 //mono_print_code (cfg);
9209 /* variables are allocated after decompose, since decompose could create temps */
9210 mono_arch_allocate_vars (cfg);
9212 if (cfg->opt & MONO_OPT_CFOLD)
9213 mono_constant_fold (cfg);
9215 mini_select_instructions (cfg);
9218 if (cfg->verbose_level >= 2) {
9219 char *id = mono_method_full_name (cfg->method, FALSE);
9220 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
9224 if (cfg->method->dynamic) {
9225 jinfo = g_malloc0 (sizeof (MonoJitInfo) + (header->num_clauses * sizeof (MonoJitExceptionInfo)));
9227 /* we access cfg->domain->mp */
9228 mono_domain_lock (cfg->domain);
9229 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo) + (header->num_clauses * sizeof (MonoJitExceptionInfo)));
9230 mono_domain_unlock (cfg->domain);
9233 jinfo->method = method;
9234 jinfo->code_start = cfg->native_code;
9235 jinfo->code_size = cfg->code_len;
9236 jinfo->used_regs = cfg->used_int_regs;
9237 jinfo->domain_neutral = (cfg->opt & MONO_OPT_SHARED) != 0;
9238 jinfo->cas_inited = FALSE; /* initialization delayed at the first stalk walk using this method */
9240 if (header->num_clauses) {
9243 jinfo->num_clauses = header->num_clauses;
9245 for (i = 0; i < header->num_clauses; i++) {
9246 MonoExceptionClause *ec = &header->clauses [i];
9247 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
9248 MonoBasicBlock *tblock;
9251 ei->flags = ec->flags;
9253 exvar = mono_find_exvar_for_offset (cfg, ec->handler_offset);
9254 ei->exvar_offset = exvar ? exvar->inst_offset : 0;
9256 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
9257 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->data.filter_offset);
9259 ei->data.filter = cfg->native_code + tblock->native_offset;
9261 ei->data.catch_class = ec->data.catch_class;
9264 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
9266 ei->try_start = cfg->native_code + tblock->native_offset;
9267 g_assert (tblock->native_offset);
9268 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
9270 ei->try_end = cfg->native_code + tblock->native_offset;
9271 g_assert (tblock->native_offset);
9272 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
9274 ei->handler_start = cfg->native_code + tblock->native_offset;
9278 cfg->jit_info = jinfo;
9280 mono_domain_lock (cfg->domain);
9281 mono_jit_info_table_add (cfg->domain, jinfo);
9283 if (cfg->method->dynamic)
9284 mono_dynamic_code_hash_lookup (cfg->domain, cfg->method)->ji = jinfo;
9285 mono_domain_unlock (cfg->domain);
9287 /* collect statistics */
9288 mono_jit_stats.allocated_code_size += cfg->code_len;
9289 code_size_ratio = cfg->code_len;
9290 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
9291 mono_jit_stats.biggest_method_size = code_size_ratio;
9292 mono_jit_stats.biggest_method = method;
9294 code_size_ratio = (code_size_ratio * 100) / mono_method_get_header (method)->code_size;
9295 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
9296 mono_jit_stats.max_code_size_ratio = code_size_ratio;
9297 mono_jit_stats.max_ratio_method = method;
9299 mono_jit_stats.native_code_size += cfg->code_len;
9301 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
9302 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
9304 /* this can only be set if the security manager is active */
9305 if (cfg->exception_type == MONO_EXCEPTION_SECURITY_LINKDEMAND) {
9306 MonoAssembly *assembly = mono_image_get_assembly (method->klass->image);
9307 MonoReflectionAssembly *refass = (MonoReflectionAssembly*) mono_assembly_get_object (domain, assembly);
9308 MonoReflectionMethod *refmet = mono_method_get_object (domain, method, NULL);
9309 MonoSecurityManager* secman = mono_security_manager_get_methods ();
9310 MonoObject *exc = NULL;
9313 args [0] = &cfg->exception_data;
9316 mono_runtime_invoke (secman->linkdemandsecurityexception, NULL, args, &exc);
9318 mono_destroy_compile (cfg);
9321 mono_raise_exception ((MonoException*)exc);
9328 mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain)
9331 GHashTable *jit_code_hash;
9332 gpointer code = NULL;
9338 jit_code_hash = target_domain->jit_code_hash;
9340 #ifdef MONO_USE_AOT_COMPILER
9341 if (!mono_compile_aot && (opt & MONO_OPT_AOT)) {
9343 MonoDomain *domain = mono_domain_get ();
9345 mono_domain_lock (domain);
9347 mono_class_init (method->klass);
9348 if ((info = mono_aot_get_method (domain, method))) {
9349 g_hash_table_insert (domain->jit_code_hash, method, info);
9350 mono_domain_unlock (domain);
9351 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
9352 return info->code_start;
9355 mono_domain_unlock (domain);
9359 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
9360 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
9362 MonoMethodPInvoke* piinfo = (MonoMethodPInvoke *) method;
9364 if (method->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE)
9365 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);
9367 if (!piinfo->addr) {
9368 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
9369 piinfo->addr = mono_lookup_internal_call (method);
9371 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
9372 mono_lookup_pinvoke_call (method, NULL, NULL);
9374 nm = mono_marshal_get_native_wrapper (method);
9375 return mono_compile_method (nm);
9377 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
9378 //mono_debug_add_wrapper (method, nm);
9379 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
9380 const char *name = method->name;
9383 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
9384 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
9385 MonoJitICallInfo *mi = mono_find_jit_icall_by_name ("mono_delegate_ctor");
9387 return (gpointer)mono_icall_get_wrapper (mi);
9388 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
9389 nm = mono_marshal_get_delegate_invoke (method);
9390 return mono_jit_compile_method (nm);
9391 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
9392 nm = mono_marshal_get_delegate_begin_invoke (method);
9393 return mono_jit_compile_method (nm);
9394 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
9395 nm = mono_marshal_get_delegate_end_invoke (method);
9396 return mono_jit_compile_method (nm);
9402 cfg = mini_method_compile (method, opt, target_domain, TRUE, FALSE, 0);
9404 mono_domain_lock (target_domain);
9406 /* Check if some other thread already did the job. In this case, we can
9407 discard the code this thread generated. */
9409 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
9410 /* We can't use a domain specific method in another domain */
9411 if ((target_domain == mono_domain_get ()) || info->domain_neutral) {
9412 code = info->code_start;
9413 // printf("Discarding code for method %s\n", method->name);
9418 g_hash_table_insert (jit_code_hash, method, cfg->jit_info);
9419 code = cfg->native_code;
9422 mono_destroy_compile (cfg);
9424 if (target_domain->jump_target_hash) {
9425 MonoJumpInfo patch_info;
9427 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
9429 patch_info.next = NULL;
9430 patch_info.ip.i = 0;
9431 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
9432 patch_info.data.method = method;
9433 g_hash_table_remove (target_domain->jump_target_hash, method);
9435 for (tmp = list; tmp; tmp = tmp->next)
9436 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info, TRUE);
9437 g_slist_free (list);
9440 mono_domain_unlock (target_domain);
9442 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
9447 mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt)
9449 /* FIXME: later copy the code from mono */
9450 MonoDomain *target_domain, *domain = mono_domain_get ();
9454 if (opt & MONO_OPT_SHARED)
9455 target_domain = mono_get_root_domain ();
9457 target_domain = domain;
9459 mono_domain_lock (target_domain);
9461 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
9462 /* We can't use a domain specific method in another domain */
9463 if (! ((domain != target_domain) && !info->domain_neutral)) {
9464 mono_domain_unlock (target_domain);
9465 mono_jit_stats.methods_lookups++;
9466 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
9467 return info->code_start;
9471 mono_domain_unlock (target_domain);
9472 p = mono_jit_compile_method_inner (method, target_domain);
9477 mono_jit_compile_method (MonoMethod *method)
9479 return mono_jit_compile_method_with_opt (method, default_opt);
9483 invalidated_delegate_trampoline (char *desc)
9485 g_error ("Unmanaged code called delegate of type %s which was already garbage collected.\n"
9486 "See http://www.go-mono.com/delegate.html for an explanation and ways to fix this.",
9491 * mono_jit_free_method:
9493 * Free all memory allocated by the JIT for METHOD.
9496 mono_jit_free_method (MonoDomain *domain, MonoMethod *method)
9498 MonoJitDynamicMethodInfo *ji;
9499 gboolean destroy = TRUE;
9501 g_assert (method->dynamic);
9503 mono_domain_lock (domain);
9504 ji = mono_dynamic_code_hash_lookup (domain, method);
9505 mono_domain_unlock (domain);
9509 mono_domain_lock (domain);
9510 g_hash_table_remove (domain->dynamic_code_hash, method);
9511 g_hash_table_remove (domain->jit_code_hash, method);
9512 mono_domain_unlock (domain);
9514 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
9515 if (debug_options.keep_delegates && method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
9517 * Instead of freeing the code, change it to call an error routine
9518 * so people can fix their code.
9520 char *type = mono_type_full_name (&method->klass->byval_arg);
9521 char *type_and_method = g_strdup_printf ("%s.%s", type, method->name);
9524 mono_arch_invalidate_method (ji->ji, invalidated_delegate_trampoline, type_and_method);
9530 * This needs to be done before freeing code_mp, since the code address is the
9531 * key in the table, so if we the code_mp first, another thread can grab the
9532 * same code address and replace our entry in the table.
9534 mono_jit_info_table_remove (domain, ji->ji);
9537 mono_code_manager_destroy (ji->code_mp);
9543 mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method)
9545 MonoDomain *target_domain;
9548 if (default_opt & MONO_OPT_SHARED)
9549 target_domain = mono_get_root_domain ();
9551 target_domain = domain;
9553 mono_domain_lock (target_domain);
9555 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
9556 /* We can't use a domain specific method in another domain */
9557 if (! ((domain != target_domain) && !info->domain_neutral)) {
9558 mono_domain_unlock (target_domain);
9559 mono_jit_stats.methods_lookups++;
9560 return info->code_start;
9564 mono_domain_unlock (target_domain);
9570 * mono_jit_runtime_invoke:
9571 * @method: the method to invoke
9572 * @obj: this pointer
9573 * @params: array of parameter values.
9574 * @exc: used to catch exceptions objects
9577 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
9580 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc, void* compiled_method);
9581 void* compiled_method;
9583 if (obj == NULL && !(method->flags & METHOD_ATTRIBUTE_STATIC) && !method->string_ctor && (method->wrapper_type == 0)) {
9584 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
9588 invoke = mono_marshal_get_runtime_invoke (method);
9589 runtime_invoke = mono_jit_compile_method (invoke);
9591 /* We need this here becuase mono_marshal_get_runtime_invoke can be place
9592 * the helper method in System.Object and not the target class
9594 mono_runtime_class_init (mono_class_vtable (mono_domain_get (), method->klass));
9596 compiled_method = mono_jit_compile_method (method);
9597 return runtime_invoke (obj, params, exc, compiled_method);
9600 #ifdef PLATFORM_WIN32
9601 #define GET_CONTEXT \
9602 struct sigcontext *ctx = (struct sigcontext*)_dummy;
9605 #define GET_CONTEXT \
9606 void *ctx = context;
9607 #elif defined(sun) // Solaris x86
9608 #define GET_CONTEXT \
9609 ucontext_t *uctx = context; \
9610 struct sigcontext *ctx = (struct sigcontext *)&(uctx->uc_mcontext);
9611 #elif defined(__ppc__) || defined (__powerpc__) || defined (__s390__) || defined (MONO_ARCH_USE_SIGACTION)
9612 #define GET_CONTEXT \
9613 void *ctx = context;
9615 #define GET_CONTEXT \
9616 void **_p = (void **)&_dummy; \
9617 struct sigcontext *ctx = (struct sigcontext *)++_p;
9621 #ifdef MONO_ARCH_USE_SIGACTION
9622 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, siginfo_t *info, void *context)
9624 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy)
9628 SIG_HANDLER_SIGNATURE (sigfpe_signal_handler)
9630 MonoException *exc = NULL;
9631 #ifndef MONO_ARCH_USE_SIGACTION
9636 #if defined(MONO_ARCH_HAVE_IS_INT_OVERFLOW)
9637 if (mono_arch_is_int_overflow (ctx, info))
9638 exc = mono_get_exception_arithmetic ();
9640 exc = mono_get_exception_divide_by_zero ();
9642 exc = mono_get_exception_divide_by_zero ();
9645 mono_arch_handle_exception (ctx, exc, FALSE);
9649 SIG_HANDLER_SIGNATURE (sigill_signal_handler)
9653 exc = mono_get_exception_execution_engine ("SIGILL");
9655 mono_arch_handle_exception (ctx, exc, FALSE);
9658 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
9660 #ifndef MONO_ARCH_USE_SIGACTION
9661 #error "Can't use sigaltstack without sigaction"
9667 SIG_HANDLER_SIGNATURE (sigsegv_signal_handler)
9669 MonoException *exc = NULL;
9670 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
9671 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
9675 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
9676 /* Can't allocate memory using Boehm GC on altstack */
9677 if (jit_tls->stack_size &&
9678 ((guint8*)info->si_addr >= (guint8*)jit_tls->end_of_stack - jit_tls->stack_size) &&
9679 ((guint8*)info->si_addr < (guint8*)jit_tls->end_of_stack))
9680 exc = mono_domain_get ()->stack_overflow_ex;
9682 exc = mono_domain_get ()->null_reference_ex;
9685 if (debug_options.abort_on_sigsegv) {
9686 MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx));
9688 fprintf (stderr, "Got SIGSEGV while in unmanaged code, and the 'abort-on-sigsegv' MONO_DEBUG option is set. Aborting...\n");
9689 /* Segfault in unmanaged code */
9694 mono_arch_handle_exception (ctx, exc, FALSE);
9698 SIG_HANDLER_SIGNATURE (sigusr1_signal_handler)
9700 gboolean running_managed;
9708 * This is an async signal, so the code below must not call anything which
9709 * is not async safe. That includes the pthread locking functions. If we
9710 * know that we interrupted managed code, then locking is safe.
9712 ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx));
9713 running_managed = ji != NULL;
9715 exc = mono_thread_request_interruption (running_managed);
9718 mono_arch_handle_exception (ctx, exc, FALSE);
9722 SIG_HANDLER_SIGNATURE (sigprof_signal_handler)
9726 mono_profiler_stat_hit (mono_arch_ip_from_context (ctx), ctx);
9730 SIG_HANDLER_SIGNATURE (sigquit_signal_handler)
9735 exc = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
9737 mono_arch_handle_exception (ctx, exc, FALSE);
9741 SIG_HANDLER_SIGNATURE (sigint_signal_handler)
9746 exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
9748 mono_arch_handle_exception (ctx, exc, FALSE);
9752 SIG_HANDLER_SIGNATURE (sigusr2_signal_handler)
9754 gboolean enabled = mono_trace_is_enabled ();
9756 mono_trace_enable (!enabled);
9759 #ifndef PLATFORM_WIN32
9761 add_signal_handler (int signo, gpointer handler)
9763 struct sigaction sa;
9765 #ifdef MONO_ARCH_USE_SIGACTION
9766 sa.sa_sigaction = handler;
9767 sigemptyset (&sa.sa_mask);
9768 sa.sa_flags = SA_SIGINFO;
9770 sa.sa_handler = handler;
9771 sigemptyset (&sa.sa_mask);
9774 g_assert (sigaction (signo, &sa, NULL) != -1);
9779 mono_runtime_install_handlers (void)
9781 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
9782 struct sigaction sa;
9785 #ifdef PLATFORM_WIN32
9787 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
9788 win32_seh_set_handler(SIGILL, sigill_signal_handler);
9789 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
9790 if (debug_options.handle_sigint)
9791 win32_seh_set_handler(SIGINT, sigint_signal_handler);
9793 #else /* !PLATFORM_WIN32 */
9795 /* libpthreads has its own implementation of sigaction(),
9796 * but it seems to work well with our current exception
9797 * handlers. If not we must call syscall directly instead
9800 if (debug_options.handle_sigint)
9801 add_signal_handler (SIGINT, sigint_signal_handler);
9803 add_signal_handler (SIGFPE, sigfpe_signal_handler);
9804 add_signal_handler (SIGQUIT, sigquit_signal_handler);
9805 add_signal_handler (SIGILL, sigill_signal_handler);
9806 add_signal_handler (SIGBUS, sigsegv_signal_handler);
9807 if (mono_jit_trace_calls != NULL)
9808 add_signal_handler (SIGUSR2, sigusr2_signal_handler);
9810 add_signal_handler (mono_thread_get_abort_signal (), sigusr1_signal_handler);
9813 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
9814 sa.sa_sigaction = sigsegv_signal_handler;
9815 sigemptyset (&sa.sa_mask);
9816 sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
9817 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
9819 add_signal_handler (SIGSEGV, sigsegv_signal_handler);
9822 #endif /* PLATFORM_WIN32 */
9826 #ifdef HAVE_LINUX_RTC_H
9827 #include <linux/rtc.h>
9828 #include <sys/ioctl.h>
9830 static int rtc_fd = -1;
9833 enable_rtc_timer (gboolean enable)
9836 flags = fcntl (rtc_fd, F_GETFL);
9838 perror ("getflags");
9845 if (fcntl (rtc_fd, F_SETFL, flags) == -1) {
9846 perror ("setflags");
9854 setup_stat_profiler (void)
9857 struct itimerval itval;
9858 static int inited = 0;
9859 #ifdef HAVE_LINUX_RTC_H
9860 const char *rtc_freq;
9861 if (!inited && (rtc_freq = g_getenv ("MONO_RTC"))) {
9865 freq = atoi (rtc_freq);
9868 rtc_fd = open ("/dev/rtc", O_RDONLY);
9870 perror ("open /dev/rtc");
9873 add_signal_handler (SIGPROF, sigprof_signal_handler);
9874 if (ioctl (rtc_fd, RTC_IRQP_SET, freq) == -1) {
9875 perror ("set rtc freq");
9878 if (ioctl (rtc_fd, RTC_PIE_ON, 0) == -1) {
9879 perror ("start rtc");
9882 if (fcntl (rtc_fd, F_SETSIG, SIGPROF) == -1) {
9886 if (fcntl (rtc_fd, F_SETOWN, getpid ()) == -1) {
9890 enable_rtc_timer (TRUE);
9897 itval.it_interval.tv_usec = 999;
9898 itval.it_interval.tv_sec = 0;
9899 itval.it_value = itval.it_interval;
9900 setitimer (ITIMER_PROF, &itval, NULL);
9904 add_signal_handler (SIGPROF, sigprof_signal_handler);
9908 /* mono_jit_create_remoting_trampoline:
9909 * @method: pointer to the method info
9911 * Creates a trampoline which calls the remoting functions. This
9912 * is used in the vtable of transparent proxies.
9914 * Returns: a pointer to the newly created code
9917 mono_jit_create_remoting_trampoline (MonoMethod *method, MonoRemotingTarget target)
9920 guint8 *addr = NULL;
9922 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
9923 (mono_method_signature (method)->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
9924 nm = mono_marshal_get_remoting_invoke_for_target (method, target);
9925 addr = mono_compile_method (nm);
9927 addr = mono_compile_method (method);
9933 mini_parse_debug_options (void)
9935 char *options = getenv ("MONO_DEBUG");
9936 gchar **args, **ptr;
9941 args = g_strsplit (options, ",", -1);
9943 for (ptr = args; ptr && *ptr; ptr++) {
9944 const char *arg = *ptr;
9946 if (!strcmp (arg, "handle-sigint"))
9947 debug_options.handle_sigint = TRUE;
9948 else if (!strcmp (arg, "keep-delegates"))
9949 debug_options.keep_delegates = TRUE;
9950 else if (!strcmp (arg, "abort-on-sigsegv"))
9951 debug_options.abort_on_sigsegv = TRUE;
9953 fprintf (stderr, "Invalid option for the MONO_DEBUG env variable: %s\n", arg);
9954 fprintf (stderr, "Available options: 'handle-sigint', 'keep-delegates', 'abort-on-sigsegv'\n");
9961 mini_init (const char *filename)
9965 InitializeCriticalSection (&jit_mutex);
9967 global_codeman = mono_code_manager_new ();
9968 jit_icall_name_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
9970 mono_arch_cpu_init ();
9972 mono_init_trampolines ();
9974 if (!g_thread_supported ())
9975 g_thread_init (NULL);
9977 if (getenv ("MONO_DEBUG") != NULL)
9978 mini_parse_debug_options ();
9980 MONO_GC_PRE_INIT ();
9982 mono_jit_tls_id = TlsAlloc ();
9983 setup_jit_tls_data ((gpointer)-1, mono_thread_abort);
9987 if (default_opt & MONO_OPT_AOT)
9990 mono_runtime_install_handlers ();
9991 mono_threads_install_cleanup (mini_thread_cleanup);
9993 #define JIT_TRAMPOLINES_WORK
9994 #ifdef JIT_TRAMPOLINES_WORK
9995 mono_install_compile_method (mono_jit_compile_method);
9996 mono_install_free_method (mono_jit_free_method);
9997 mono_install_trampoline (mono_create_jit_trampoline);
9998 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
10000 #define JIT_INVOKE_WORKS
10001 #ifdef JIT_INVOKE_WORKS
10002 mono_install_runtime_invoke (mono_jit_runtime_invoke);
10003 mono_install_handler (mono_arch_get_throw_exception ());
10005 mono_install_stack_walk (mono_jit_walk_stack);
10006 mono_install_init_vtable (mono_aot_init_vtable);
10007 mono_install_get_cached_class_info (mono_aot_get_cached_class_info);
10008 mono_install_jit_info_find_in_aot (mono_aot_find_jit_info);
10010 domain = mono_init_from_assembly (filename, filename);
10011 mono_icall_init ();
10013 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
10014 ves_icall_get_frame_info);
10015 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
10016 ves_icall_get_trace);
10017 mono_add_internal_call ("System.Exception::get_trace",
10018 ves_icall_System_Exception_get_trace);
10019 mono_add_internal_call ("System.Security.SecurityFrame::_GetSecurityFrame",
10020 ves_icall_System_Security_SecurityFrame_GetSecurityFrame);
10021 mono_add_internal_call ("System.Security.SecurityFrame::_GetSecurityStack",
10022 ves_icall_System_Security_SecurityFrame_GetSecurityStack);
10023 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
10024 mono_runtime_install_handlers);
10027 create_helper_signature ();
10029 #define JIT_CALLS_WORK
10030 #ifdef JIT_CALLS_WORK
10031 /* Needs to be called here since register_jit_icall depends on it */
10032 mono_marshal_init ();
10034 mono_arch_register_lowlevel_calls ();
10035 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
10036 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
10037 mono_register_jit_icall (mono_trace_enter_method, "mono_trace_enter_method", NULL, TRUE);
10038 mono_register_jit_icall (mono_trace_leave_method, "mono_trace_leave_method", NULL, TRUE);
10039 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
10040 mono_register_jit_icall (mono_jit_thread_attach, "mono_jit_thread_attach", helper_sig_void_void, TRUE);
10041 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
10043 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
10044 mono_register_jit_icall (mono_arch_get_rethrow_exception (), "mono_arch_rethrow_exception", helper_sig_void_obj, TRUE);
10045 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
10046 helper_sig_void_ptr, TRUE);
10047 #if MONO_ARCH_HAVE_THROW_CORLIB_EXCEPTION
10048 mono_register_jit_icall (mono_arch_get_throw_corlib_exception (), "mono_arch_throw_corlib_exception",
10049 helper_sig_void_ptr, TRUE);
10051 mono_register_jit_icall (mono_thread_get_pending_exception, "mono_thread_get_pending_exception", helper_sig_obj_void, FALSE);
10052 mono_register_jit_icall (mono_thread_interruption_checkpoint, "mono_thread_interruption_checkpoint", helper_sig_void_void, FALSE);
10053 mono_register_jit_icall (mono_thread_force_interruption_checkpoint, "mono_thread_force_interruption_checkpoint", helper_sig_void_void, FALSE);
10054 mono_register_jit_icall (mono_load_remote_field_new, "mono_load_remote_field_new", helper_sig_obj_obj_ptr_ptr, FALSE);
10055 mono_register_jit_icall (mono_store_remote_field_new, "mono_store_remote_field_new", helper_sig_void_obj_ptr_ptr_obj, FALSE);
10058 * NOTE, NOTE, NOTE, NOTE:
10059 * when adding emulation for some opcodes, remember to also add a dummy
10060 * rule to the burg files, because we need the arity information to be correct.
10062 #ifndef MONO_ARCH_NO_EMULATE_LONG_MUL_OPTS
10063 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult, TRUE);
10064 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv, FALSE);
10065 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un, FALSE);
10066 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem, FALSE);
10067 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un, FALSE);
10068 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un, FALSE);
10069 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf, FALSE);
10072 #ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
10073 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl, TRUE);
10074 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr, TRUE);
10075 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un, TRUE);
10078 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8, FALSE);
10079 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4, FALSE);
10080 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8, FALSE);
10081 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8, FALSE);
10083 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
10084 mono_register_opcode_emulation (OP_FCONV_TO_I8, "__emul_fconv_to_i8", helper_sig_long_double, mono_fconv_i8, FALSE);
10086 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
10087 mono_register_opcode_emulation (CEE_CONV_R_UN, "__emul_conv_r_un", helper_sig_double_int, mono_conv_to_r8_un, FALSE);
10089 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
10090 mono_register_opcode_emulation (OP_LCONV_TO_R8, "__emul_lconv_to_r8", helper_sig_double_long, mono_lconv_to_r8, FALSE);
10092 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
10093 mono_register_opcode_emulation (OP_LCONV_TO_R4, "__emul_lconv_to_r4", helper_sig_float_long, mono_lconv_to_r4, FALSE);
10095 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
10096 mono_register_opcode_emulation (OP_LCONV_TO_R_UN, "__emul_lconv_to_r8_un", helper_sig_double_long, mono_lconv_to_r8_un, FALSE);
10098 #ifdef MONO_ARCH_EMULATE_FREM
10099 mono_register_opcode_emulation (OP_FREM, "__emul_frem", helper_sig_double_double_double, fmod, FALSE);
10102 #if SIZEOF_VOID_P == 4
10103 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4, TRUE);
10106 /* other jit icalls */
10107 mono_register_jit_icall (mono_delegate_ctor, "mono_delegate_ctor", helper_sig_void_obj_obj_ptr, FALSE);
10108 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
10109 helper_sig_ptr_ptr_ptr, FALSE);
10110 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr_ptr, FALSE);
10111 mono_register_jit_icall (mono_get_special_static_data, "mono_get_special_static_data", helper_sig_ptr_int, FALSE);
10112 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
10113 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
10114 mono_register_jit_icall (helper_stelem_ref_check, "helper_stelem_ref_check", helper_sig_stelem_ref_check, FALSE);
10115 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
10116 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
10117 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
10118 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
10119 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
10120 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
10121 mono_register_jit_icall (mono_ldftn_nosync, "mono_ldftn_nosync", helper_sig_compile, FALSE);
10122 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
10123 mono_register_jit_icall (helper_compile_generic_method, "compile_generic_method", helper_sig_compile_generic_method, FALSE);
10126 #define JIT_RUNTIME_WORKS
10127 #ifdef JIT_RUNTIME_WORKS
10128 mono_install_runtime_cleanup ((MonoDomainFunc)mini_cleanup);
10129 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
10132 mono_thread_attach (domain);
10136 MonoJitStats mono_jit_stats = {0};
10139 print_jit_stats (void)
10141 if (mono_jit_stats.enabled) {
10142 g_print ("Mono Jit statistics\n");
10143 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
10144 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
10145 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
10146 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
10147 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
10148 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
10149 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
10150 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
10151 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
10152 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
10153 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
10154 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
10155 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
10156 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
10157 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
10158 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
10159 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
10160 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
10162 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
10163 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
10164 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
10165 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
10166 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
10168 g_print ("\nGeneric instances: %ld\n", mono_stats.generic_instance_count);
10169 g_print ("Initialized classes: %ld\n", mono_stats.generic_class_count);
10170 g_print ("Inflated methods: %ld / %ld\n", mono_stats.inflated_method_count_2,
10171 mono_stats.inflated_method_count);
10172 g_print ("Inflated types: %ld\n", mono_stats.inflated_type_count);
10173 g_print ("Generics metadata size: %ld\n", mono_stats.generics_metadata_size);
10175 if (mono_use_security_manager) {
10176 g_print ("\nDecl security check : %ld\n", mono_jit_stats.cas_declsec_check);
10177 g_print ("LinkDemand (user) : %ld\n", mono_jit_stats.cas_linkdemand);
10178 g_print ("LinkDemand (icall) : %ld\n", mono_jit_stats.cas_linkdemand_icall);
10179 g_print ("LinkDemand (pinvoke) : %ld\n", mono_jit_stats.cas_linkdemand_pinvoke);
10180 g_print ("LinkDemand (aptc) : %ld\n", mono_jit_stats.cas_linkdemand_aptc);
10181 g_print ("Demand (code gen) : %ld\n", mono_jit_stats.cas_demand_generation);
10187 mini_cleanup (MonoDomain *domain)
10189 #ifdef HAVE_LINUX_RTC_H
10191 enable_rtc_timer (FALSE);
10195 * mono_runtime_cleanup() and mono_domain_finalize () need to
10196 * be called early since they need the execution engine still
10197 * fully working (mono_domain_finalize may invoke managed finalizers
10198 * and mono_runtime_cleanup will wait for other threads to finish).
10200 mono_domain_finalize (domain, 2000);
10202 mono_runtime_cleanup (domain);
10204 mono_profiler_shutdown ();
10206 mono_debug_cleanup ();
10208 mono_icall_cleanup ();
10210 #ifdef PLATFORM_WIN32
10211 win32_seh_cleanup();
10214 mono_domain_free (domain, TRUE);
10216 mono_code_manager_destroy (global_codeman);
10217 g_hash_table_destroy (jit_icall_name_hash);
10218 if (class_init_hash_addr)
10219 g_hash_table_destroy (class_init_hash_addr);
10221 print_jit_stats ();
10225 mono_set_defaults (int verbose_level, guint32 opts)
10227 mini_verbose = verbose_level;
10228 default_opt = opts;
10232 mono_precompile_assembly (MonoAssembly *ass, void *user_data)
10234 GHashTable *assemblies = (GHashTable*)user_data;
10235 MonoImage *image = mono_assembly_get_image (ass);
10236 MonoMethod *method, *invoke;
10239 if (g_hash_table_lookup (assemblies, ass))
10242 g_hash_table_insert (assemblies, ass, ass);
10244 if (mini_verbose > 0)
10245 printf ("PRECOMPILE: %s.\n", mono_image_get_filename (image));
10247 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
10248 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
10249 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
10253 if (mini_verbose > 1) {
10254 char * desc = mono_method_full_name (method, TRUE);
10255 g_print ("Compiling %d %s\n", count, desc);
10258 mono_compile_method (method);
10259 if (strcmp (method->name, "Finalize") == 0) {
10260 invoke = mono_marshal_get_runtime_invoke (method);
10261 mono_compile_method (invoke);
10263 if (method->klass->marshalbyref && mono_method_signature (method)->hasthis) {
10264 invoke = mono_marshal_get_remoting_invoke_with_check (method);
10265 mono_compile_method (invoke);
10269 /* Load and precompile referenced assemblies as well */
10270 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_ASSEMBLYREF); ++i) {
10271 mono_assembly_load_reference (image, i);
10272 if (image->references [i])
10273 mono_precompile_assembly (image->references [i], assemblies);
10277 void mono_precompile_assemblies ()
10279 GHashTable *assemblies = g_hash_table_new (NULL, NULL);
10281 mono_assembly_foreach ((GFunc)mono_precompile_assembly, assemblies);
10283 g_hash_table_destroy (assemblies);