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.
16 #ifdef sun // Solaris x86
17 #include <sys/types.h>
18 #include <sys/ucontext.h>
21 #ifdef HAVE_VALGRIND_MEMCHECK_H
22 #include <valgrind/memcheck.h>
25 #include <mono/metadata/assembly.h>
26 #include <mono/metadata/loader.h>
27 #include <mono/metadata/cil-coff.h>
28 #include <mono/metadata/tabledefs.h>
29 #include <mono/metadata/class.h>
30 #include <mono/metadata/object.h>
31 #include <mono/metadata/exception.h>
32 #include <mono/metadata/opcodes.h>
33 #include <mono/metadata/mono-endian.h>
34 #include <mono/metadata/tokentype.h>
35 #include <mono/metadata/tabledefs.h>
36 #include <mono/metadata/threads.h>
37 #include <mono/metadata/marshal.h>
38 #include <mono/metadata/socket-io.h>
39 #include <mono/metadata/appdomain.h>
40 #include <mono/metadata/debug-helpers.h>
41 #include <mono/io-layer/io-layer.h>
42 #include "mono/metadata/profiler.h"
43 #include <mono/metadata/profiler-private.h>
44 #include <mono/metadata/mono-config.h>
45 #include <mono/metadata/environment.h>
46 #include <mono/metadata/mono-debug.h>
47 #include <mono/metadata/mono-debug-debugger.h>
48 #include <mono/metadata/monitor.h>
49 #include <mono/os/gc_wrapper.h>
57 #include "jit-icalls.c"
60 * this is used to determine when some branch optimizations are possible: we exclude FP compares
61 * because they have weird semantics with NaNs.
63 #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))
64 #define MONO_IS_COND_BRANCH_NOFP(ins) (MONO_IS_COND_BRANCH_OP(ins) && (ins)->inst_left->inst_left->type != STACK_R8)
66 #define MONO_CHECK_THIS(ins) (cfg->method->signature->hasthis && (ins)->ssa_op == MONO_SSA_LOAD && (ins)->inst_left->inst_c0 == 0)
68 gboolean mono_arch_print_tree(MonoInst *tree, int arity);
69 static gpointer mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt);
70 static gpointer mono_jit_compile_method (MonoMethod *method);
71 static gpointer mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method);
73 static void handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src,
74 const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native);
76 static void dec_foreach (MonoInst *tree, MonoCompile *cfg);
78 static int mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
79 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
80 guint inline_offset, gboolean is_virtual_call);
82 extern guint8 mono_burg_arity [];
83 /* helper methods signature */
84 static MonoMethodSignature *helper_sig_long_long_long = NULL;
85 static MonoMethodSignature *helper_sig_long_long_int = NULL;
86 static MonoMethodSignature *helper_sig_newarr = NULL;
87 static MonoMethodSignature *helper_sig_newarr_specific = NULL;
88 static MonoMethodSignature *helper_sig_ldstr = NULL;
89 static MonoMethodSignature *helper_sig_domain_get = NULL;
90 static MonoMethodSignature *helper_sig_object_new = NULL;
91 static MonoMethodSignature *helper_sig_object_new_specific = NULL;
92 static MonoMethodSignature *helper_sig_compile = NULL;
93 static MonoMethodSignature *helper_sig_compile_virt = NULL;
94 static MonoMethodSignature *helper_sig_obj_ptr = NULL;
95 static MonoMethodSignature *helper_sig_obj_ptr_ptr = NULL;
96 static MonoMethodSignature *helper_sig_obj_obj_ptr_ptr = NULL;
97 static MonoMethodSignature *helper_sig_obj_void = NULL;
98 static MonoMethodSignature *helper_sig_ptr_void = NULL;
99 static MonoMethodSignature *helper_sig_void_void = NULL;
100 static MonoMethodSignature *helper_sig_void_ptr = NULL;
101 static MonoMethodSignature *helper_sig_void_obj = NULL;
102 static MonoMethodSignature *helper_sig_void_obj_ptr_int = NULL;
103 static MonoMethodSignature *helper_sig_void_obj_ptr_ptr_obj = NULL;
104 static MonoMethodSignature *helper_sig_void_ptr_ptr = NULL;
105 static MonoMethodSignature *helper_sig_void_ptr_ptr_ptr = NULL;
106 static MonoMethodSignature *helper_sig_ptr_ptr_ptr = NULL;
107 static MonoMethodSignature *helper_sig_ptr_obj = NULL;
108 static MonoMethodSignature *helper_sig_ptr_obj_int = NULL;
109 static MonoMethodSignature *helper_sig_ptr_int = NULL;
110 static MonoMethodSignature *helper_sig_initobj = NULL;
111 static MonoMethodSignature *helper_sig_memcpy = NULL;
112 static MonoMethodSignature *helper_sig_memset = NULL;
113 static MonoMethodSignature *helper_sig_ulong_double = NULL;
114 static MonoMethodSignature *helper_sig_long_double = NULL;
115 static MonoMethodSignature *helper_sig_double_long = NULL;
116 static MonoMethodSignature *helper_sig_double_int = NULL;
117 static MonoMethodSignature *helper_sig_float_long = NULL;
118 static MonoMethodSignature *helper_sig_double_double_double = NULL;
119 static MonoMethodSignature *helper_sig_uint_double = NULL;
120 static MonoMethodSignature *helper_sig_int_double = NULL;
121 static MonoMethodSignature *helper_sig_stelem_ref = NULL;
122 static MonoMethodSignature *helper_sig_stelem_ref_check = NULL;
123 static MonoMethodSignature *helper_sig_class_init_trampoline = NULL;
125 static guint32 default_opt = MONO_OPT_PEEPHOLE;
127 guint32 mono_jit_tls_id = -1;
128 MonoTraceSpec *mono_jit_trace_calls = NULL;
129 gboolean mono_break_on_exc = FALSE;
130 gboolean mono_compile_aot = FALSE;
132 static int mini_verbose = 0;
134 static CRITICAL_SECTION jit_mutex;
136 static GHashTable *class_init_hash_addr = NULL;
138 /* MonoMethod -> MonoJitDynamicMethodInfo */
139 static GHashTable *dynamic_code_hash = NULL;
142 mono_running_on_valgrind (void)
144 #ifdef HAVE_VALGRIND_MEMCHECK_H
145 if (RUNNING_ON_VALGRIND)
155 G_GNUC_UNUSED static void
156 print_method_from_ip (void *ip)
161 MonoDomain *domain = mono_domain_get ();
163 ji = mono_jit_info_table_find (domain, ip);
165 g_print ("No method at %p\n", ip);
168 method = mono_method_full_name (ji->method, TRUE);
169 source = mono_debug_source_location_from_address (ji->method, (int) ip, NULL, domain);
171 g_print ("IP %p at offset 0x%x of method %s (%p %p)\n", ip, (char*)ip - (char*)ji->code_start, method, ji->code_start, (char*)ji->code_start + ji->code_size);
174 g_print ("%s\n", source);
182 mono_print_method_from_ip (void *ip)
184 print_method_from_ip (ip);
188 * mono_method_same_domain:
190 * Determine whenever two compiled methods are in the same domain, thus
191 * the address of the callee can be embedded in the caller.
193 gboolean mono_method_same_domain (MonoJitInfo *caller, MonoJitInfo *callee)
195 if (!caller || !callee)
199 * If the call was made from domain-neutral to domain-specific
200 * code, we can't patch the call site.
202 if (caller->domain_neutral && !callee->domain_neutral)
205 if ((caller->method->klass == mono_defaults.appdomain_class) &&
206 (strstr (caller->method->name, "InvokeInDomain"))) {
207 /* The InvokeInDomain methods change the current appdomain */
215 mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token)
217 MonoJumpInfoToken *res = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoToken));
224 #define MONO_INIT_VARINFO(vi,id) do { \
225 (vi)->range.first_use.pos.bid = 0xffff; \
231 * Basic blocks have two numeric identifiers:
232 * dfn: Depth First Number
233 * block_num: unique ID assigned at bblock creation
235 #define NEW_BBLOCK(cfg) (mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)))
236 #define ADD_BBLOCK(cfg,bbhash,b) do { \
237 g_hash_table_insert (bbhash, (b)->cil_code, (b)); \
238 (b)->block_num = cfg->num_bblocks++; \
239 (b)->real_offset = real_offset; \
242 #define GET_BBLOCK(cfg,bbhash,tblock,ip) do { \
243 (tblock) = g_hash_table_lookup (bbhash, (ip)); \
245 if ((ip) >= end || (ip) < header->code) goto unverified; \
246 (tblock) = NEW_BBLOCK (cfg); \
247 (tblock)->cil_code = (ip); \
248 ADD_BBLOCK (cfg, (bbhash), (tblock)); \
252 #define CHECK_BBLOCK(target,ip,tblock) do { \
253 if ((target) < (ip) && !(tblock)->code) { \
254 bb_recheck = g_list_prepend (bb_recheck, (tblock)); \
255 if (cfg->verbose_level > 2) g_print ("queued block %d for check at IL%04x from IL%04x\n", (tblock)->block_num, (target) - header->code, (ip) - header->code); \
259 #define NEW_ICONST(cfg,dest,val) do { \
260 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
261 (dest)->opcode = OP_ICONST; \
262 (dest)->inst_c0 = (val); \
263 (dest)->type = STACK_I4; \
266 #if SIZEOF_VOID_P == 8
267 #define NEW_PCONST(cfg,dest,val) do { \
268 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
269 (dest)->opcode = OP_I8CONST; \
270 (dest)->inst_p0 = (val); \
271 (dest)->type = STACK_PTR; \
274 #define NEW_PCONST(cfg,dest,val) do { \
275 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
276 (dest)->opcode = OP_ICONST; \
277 (dest)->inst_p0 = (val); \
278 (dest)->type = STACK_PTR; \
282 #if SIZEOF_VOID_P == 8
283 #define OP_PCONST OP_I8CONST
285 #define OP_PCONST OP_ICONST
288 #define NEW_CLASSCONST(cfg,dest,val) do { \
289 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
290 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_PCONST; \
291 (dest)->inst_p0 = (val); \
292 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_CLASS; \
293 (dest)->type = STACK_PTR; \
296 #define NEW_IMAGECONST(cfg,dest,val) do { \
297 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
298 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_PCONST; \
299 (dest)->inst_p0 = (val); \
300 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_IMAGE; \
301 (dest)->type = STACK_PTR; \
304 #define NEW_FIELDCONST(cfg,dest,field) do { \
305 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
306 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_PCONST; \
307 (dest)->inst_p0 = (field); \
308 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_FIELD; \
309 (dest)->type = STACK_PTR; \
312 #define NEW_METHODCONST(cfg,dest,val) do { \
313 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
314 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_PCONST; \
315 (dest)->inst_p0 = (val); \
316 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_METHODCONST; \
317 (dest)->type = STACK_PTR; \
320 #define NEW_VTABLECONST(cfg,dest,vtable) do { \
321 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
322 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_PCONST; \
323 (dest)->inst_p0 = mono_compile_aot ? (gpointer)((vtable)->klass) : (vtable); \
324 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_VTABLE; \
325 (dest)->type = STACK_PTR; \
328 #define NEW_SFLDACONST(cfg,dest,field) do { \
329 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
330 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_PCONST; \
331 (dest)->inst_p0 = (field); \
332 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_SFLDA; \
333 (dest)->type = STACK_PTR; \
336 #define NEW_LDSTRCONST(cfg,dest,image,token) do { \
337 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
338 (dest)->opcode = OP_AOTCONST; \
339 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
340 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_LDSTR; \
341 (dest)->type = STACK_OBJ; \
344 #define NEW_TYPE_FROM_HANDLE_CONST(cfg,dest,image,token) do { \
345 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
346 (dest)->opcode = OP_AOTCONST; \
347 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
348 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_TYPE_FROM_HANDLE; \
349 (dest)->type = STACK_OBJ; \
352 #define NEW_LDTOKENCONST(cfg,dest,image,token) do { \
353 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
354 (dest)->opcode = OP_AOTCONST; \
355 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
356 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_LDTOKEN; \
357 (dest)->type = STACK_PTR; \
360 #define NEW_DOMAINCONST(cfg,dest) do { \
361 if (cfg->opt & MONO_OPT_SHARED) { \
362 NEW_TEMPLOAD (cfg, dest, mono_get_domainvar (cfg)->inst_c0); \
364 NEW_PCONST (cfg, dest, (cfg)->domain); \
368 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
370 #define NEW_ARGLOAD(cfg,dest,num) do { \
371 if (arg_array [(num)]->opcode == OP_ICONST) (dest) = arg_array [(num)]; else { \
372 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
373 (dest)->ssa_op = MONO_SSA_LOAD; \
374 (dest)->inst_i0 = arg_array [(num)]; \
375 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
376 type_to_eval_stack_type (param_types [(num)], (dest)); \
377 (dest)->klass = (dest)->inst_i0->klass; \
380 #define NEW_LOCLOAD(cfg,dest,num) do { \
381 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
382 (dest)->ssa_op = MONO_SSA_LOAD; \
383 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
384 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
385 type_to_eval_stack_type (header->locals [(num)], (dest)); \
386 (dest)->klass = (dest)->inst_i0->klass; \
389 #define NEW_LOCLOADA(cfg,dest,num) do { \
390 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
391 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
392 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
393 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
394 (dest)->opcode = OP_LDADDR; \
395 (dest)->type = STACK_MP; \
396 (dest)->klass = (dest)->inst_i0->klass; \
397 if (!MONO_TYPE_ISSTRUCT (header->locals [(num)])) \
398 (cfg)->disable_ssa = TRUE; \
401 #define NEW_RETLOADA(cfg,dest) do { \
402 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
403 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
404 (dest)->inst_i0 = (cfg)->ret; \
405 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
406 (dest)->opcode = CEE_LDIND_I; \
407 (dest)->type = STACK_MP; \
408 (dest)->klass = (dest)->inst_i0->klass; \
409 (cfg)->disable_ssa = TRUE; \
412 #define NEW_ARGLOADA(cfg,dest,num) do { \
413 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
414 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
415 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
416 (dest)->inst_i0 = arg_array [(num)]; \
417 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
418 (dest)->opcode = OP_LDADDR; \
419 (dest)->type = STACK_MP; \
420 (dest)->klass = (dest)->inst_i0->klass; \
421 (cfg)->disable_ssa = TRUE; \
424 #define NEW_TEMPLOAD(cfg,dest,num) do { \
425 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
426 (dest)->ssa_op = MONO_SSA_LOAD; \
427 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
428 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
429 type_to_eval_stack_type ((dest)->inst_i0->inst_vtype, (dest)); \
430 (dest)->klass = (dest)->inst_i0->klass; \
433 #define NEW_TEMPLOADA(cfg,dest,num) do { \
434 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
435 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
436 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
437 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
438 (dest)->opcode = OP_LDADDR; \
439 (dest)->type = STACK_MP; \
440 (dest)->klass = (dest)->inst_i0->klass; \
441 if (!MONO_TYPE_ISSTRUCT (cfg->varinfo [(num)]->inst_vtype)) \
442 (cfg)->disable_ssa = TRUE; \
446 #define NEW_INDLOAD(cfg,dest,addr,vtype) do { \
447 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
448 (dest)->inst_left = addr; \
449 (dest)->opcode = mono_type_to_ldind (vtype); \
450 type_to_eval_stack_type (vtype, (dest)); \
451 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
454 #define NEW_INDSTORE(cfg,dest,addr,value,vtype) do { \
455 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
456 (dest)->inst_i0 = addr; \
457 (dest)->opcode = mono_type_to_stind (vtype); \
458 (dest)->inst_i1 = (value); \
459 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
462 #define NEW_TEMPSTORE(cfg,dest,num,inst) do { \
463 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
464 (dest)->ssa_op = MONO_SSA_STORE; \
465 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
466 (dest)->opcode = mono_type_to_stind ((dest)->inst_i0->inst_vtype); \
467 (dest)->inst_i1 = (inst); \
468 (dest)->klass = (dest)->inst_i0->klass; \
471 #define NEW_LOCSTORE(cfg,dest,num,inst) do { \
472 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
473 (dest)->opcode = mono_type_to_stind (header->locals [(num)]); \
474 (dest)->ssa_op = MONO_SSA_STORE; \
475 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
476 (dest)->inst_i1 = (inst); \
477 (dest)->klass = (dest)->inst_i0->klass; \
480 #define NEW_ARGSTORE(cfg,dest,num,inst) do { \
481 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
482 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
483 (dest)->opcode = mono_type_to_stind (param_types [(num)]); \
484 (dest)->ssa_op = MONO_SSA_STORE; \
485 (dest)->inst_i0 = arg_array [(num)]; \
486 (dest)->inst_i1 = (inst); \
487 (dest)->klass = (dest)->inst_i0->klass; \
490 #define ADD_BINOP(op) do { \
491 MONO_INST_NEW (cfg, ins, (op)); \
492 ins->cil_code = ip; \
494 ins->inst_i0 = sp [0]; \
495 ins->inst_i1 = sp [1]; \
497 type_from_op (ins); \
501 #define ADD_UNOP(op) do { \
502 MONO_INST_NEW (cfg, ins, (op)); \
503 ins->cil_code = ip; \
505 ins->inst_i0 = sp [0]; \
507 type_from_op (ins); \
511 #define ADD_BINCOND(next_block) do { \
514 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
515 cmp->inst_i0 = sp [0]; \
516 cmp->inst_i1 = sp [1]; \
517 cmp->cil_code = ins->cil_code; \
518 type_from_op (cmp); \
520 ins->inst_i0 = cmp; \
521 MONO_ADD_INS (bblock, ins); \
522 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
523 GET_BBLOCK (cfg, bbhash, tblock, target); \
524 link_bblock (cfg, bblock, tblock); \
525 ins->inst_true_bb = tblock; \
526 CHECK_BBLOCK (target, ip, tblock); \
527 if ((next_block)) { \
528 link_bblock (cfg, bblock, (next_block)); \
529 ins->inst_false_bb = (next_block); \
530 start_new_bblock = 1; \
532 GET_BBLOCK (cfg, bbhash, tblock, ip); \
533 link_bblock (cfg, bblock, tblock); \
534 ins->inst_false_bb = tblock; \
535 start_new_bblock = 2; \
539 /* FIXME: handle float, long ... */
540 #define ADD_UNCOND(istrue) do { \
543 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
544 cmp->inst_i0 = sp [0]; \
545 switch (cmp->inst_i0->type) { \
547 cmp->inst_i1 = zero_int64; break; \
549 cmp->inst_i1 = zero_r8; break; \
552 cmp->inst_i1 = zero_ptr; break; \
554 cmp->inst_i1 = zero_obj; break; \
556 cmp->inst_i1 = zero_int32; \
558 cmp->cil_code = ins->cil_code; \
559 type_from_op (cmp); \
561 ins->inst_i0 = cmp; \
562 ins->opcode = (istrue)? CEE_BNE_UN: CEE_BEQ; \
563 MONO_ADD_INS (bblock, ins); \
564 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
565 GET_BBLOCK (cfg, bbhash, tblock, target); \
566 link_bblock (cfg, bblock, tblock); \
567 ins->inst_true_bb = tblock; \
568 CHECK_BBLOCK (target, ip, tblock); \
569 GET_BBLOCK (cfg, bbhash, tblock, ip); \
570 link_bblock (cfg, bblock, tblock); \
571 ins->inst_false_bb = tblock; \
572 start_new_bblock = 2; \
575 #define NEW_LDELEMA(cfg,dest,sp,k) do { \
576 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
577 (dest)->opcode = CEE_LDELEMA; \
578 (dest)->inst_left = (sp) [0]; \
579 (dest)->inst_right = (sp) [1]; \
580 (dest)->type = STACK_MP; \
581 (dest)->klass = (k); \
582 (cfg)->flags |= MONO_CFG_HAS_LDELEMA; \
585 #define NEW_GROUP(cfg,dest,el1,el2) do { \
586 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
587 (dest)->opcode = OP_GROUP; \
588 (dest)->inst_left = (el1); \
589 (dest)->inst_right = (el2); \
594 compare_bblock (gconstpointer a, gconstpointer b)
596 const MonoBasicBlock *b1 = a;
597 const MonoBasicBlock *b2 = b;
599 return b2->cil_code - b1->cil_code;
604 * link_bblock: Links two basic blocks
606 * links two basic blocks in the control flow graph, the 'from'
607 * argument is the starting block and the 'to' argument is the block
608 * the control flow ends to after 'from'.
611 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
613 MonoBasicBlock **newa;
617 if (from->cil_code) {
619 g_print ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
621 g_print ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
624 g_print ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
626 g_print ("edge from entry to exit\n");
630 for (i = 0; i < from->out_count; ++i) {
631 if (to == from->out_bb [i]) {
637 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
638 for (i = 0; i < from->out_count; ++i) {
639 newa [i] = from->out_bb [i];
647 for (i = 0; i < to->in_count; ++i) {
648 if (from == to->in_bb [i]) {
654 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
655 for (i = 0; i < to->in_count; ++i) {
656 newa [i] = to->in_bb [i];
665 * mono_find_block_region:
667 * We mark each basic block with a region ID. We use that to avoid BB
668 * optimizations when blocks are in different regions.
671 * A region token that encodes where this region is, and information
672 * about the clause owner for this block.
674 * The region encodes the try/catch/filter clause that owns this block
675 * as well as the type. -1 is a special value that represents a block
676 * that is in none of try/catch/filter.
679 mono_find_block_region (MonoCompile *cfg, int offset, int *filter_lengths)
681 MonoMethod *method = cfg->method;
682 MonoMethodHeader *header = mono_method_get_header (method);
683 MonoExceptionClause *clause;
686 /* first search for handlers and filters */
687 for (i = 0; i < header->num_clauses; ++i) {
688 clause = &header->clauses [i];
689 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->data.filter_offset) &&
690 (offset < (clause->data.filter_offset + filter_lengths [i])))
691 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
693 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
694 if (clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)
695 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
697 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
701 /* search the try blocks */
702 for (i = 0; i < header->num_clauses; ++i) {
703 clause = &header->clauses [i];
704 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
705 return ((i + 1) << 8) | clause->flags;
712 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
714 MonoMethod *method = cfg->method;
715 MonoMethodHeader *header = mono_method_get_header (method);
716 MonoExceptionClause *clause;
717 MonoBasicBlock *handler;
721 for (i = 0; i < header->num_clauses; ++i) {
722 clause = &header->clauses [i];
723 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
724 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
725 if (clause->flags == type) {
726 handler = g_hash_table_lookup (cfg->bb_hash, header->code + clause->handler_offset);
728 res = g_list_append (res, handler);
736 mono_find_spvar_for_region (MonoCompile *cfg, int region)
738 return g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
742 mono_create_spvar_for_region (MonoCompile *cfg, int region)
746 var = g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
750 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
751 /* prevent it from being register allocated */
752 var->flags |= MONO_INST_INDIRECT;
754 g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
758 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
762 array [*dfn] = start;
763 /*g_print ("visit %d at %p\n", *dfn, start->cil_code);*/
764 for (i = 0; i < start->out_count; ++i) {
765 if (start->out_bb [i]->dfn)
768 start->out_bb [i]->dfn = *dfn;
769 start->out_bb [i]->df_parent = start;
770 array [*dfn] = start->out_bb [i];
771 df_visit (start->out_bb [i], dfn, array);
777 MonoBasicBlock *best;
781 previous_foreach (gconstpointer key, gpointer val, gpointer data)
783 PrevStruct *p = data;
784 MonoBasicBlock *bb = val;
785 //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,
786 //bb->method == p->best->method, bb->cil_code < p->code, bb->cil_code > p->best->cil_code);
788 if (bb->cil_code && bb->cil_code < p->code && bb->cil_code > p->best->cil_code)
792 static MonoBasicBlock*
793 find_previous (GHashTable *bb_hash, MonoBasicBlock *start, const guchar *code) {
799 g_hash_table_foreach (bb_hash, (GHFunc)previous_foreach, &p);
804 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
813 * FIXME: take into account all the details:
814 * second may have been the target of more than one bblock
816 second->out_count = first->out_count;
817 second->out_bb = first->out_bb;
819 for (i = 0; i < first->out_count; ++i) {
820 bb = first->out_bb [i];
821 for (j = 0; j < bb->in_count; ++j) {
822 if (bb->in_bb [j] == first)
823 bb->in_bb [j] = second;
827 first->out_count = 0;
828 first->out_bb = NULL;
829 link_bblock (cfg, first, second);
831 second->last_ins = first->last_ins;
833 /*g_print ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
834 for (inst = first->code; inst && inst->next; inst = inst->next) {
835 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
836 g_print ("found %p: %s", inst->next->cil_code, code);
838 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
839 second->code = inst->next;
841 first->last_ins = inst;
842 second->next_bb = first->next_bb;
843 first->next_bb = second;
848 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
854 mono_type_to_ldind (MonoType *type)
860 switch (type->type) {
864 case MONO_TYPE_BOOLEAN:
878 case MONO_TYPE_FNPTR:
880 case MONO_TYPE_CLASS:
881 case MONO_TYPE_STRING:
882 case MONO_TYPE_OBJECT:
883 case MONO_TYPE_SZARRAY:
884 case MONO_TYPE_ARRAY:
885 return CEE_LDIND_REF;
893 case MONO_TYPE_VALUETYPE:
894 if (type->data.klass->enumtype) {
895 type = type->data.klass->enum_basetype;
899 case MONO_TYPE_TYPEDBYREF:
901 case MONO_TYPE_GENERICINST:
902 type = type->data.generic_inst->generic_type;
905 g_error ("unknown type 0x%02x in type_to_ldind", type->type);
911 mono_type_to_stind (MonoType *type)
917 switch (type->type) {
920 case MONO_TYPE_BOOLEAN:
932 case MONO_TYPE_FNPTR:
934 case MONO_TYPE_CLASS:
935 case MONO_TYPE_STRING:
936 case MONO_TYPE_OBJECT:
937 case MONO_TYPE_SZARRAY:
938 case MONO_TYPE_ARRAY:
939 return CEE_STIND_REF;
947 case MONO_TYPE_VALUETYPE:
948 if (type->data.klass->enumtype) {
949 type = type->data.klass->enum_basetype;
953 case MONO_TYPE_TYPEDBYREF:
955 case MONO_TYPE_GENERICINST:
956 type = type->data.generic_inst->generic_type;
959 g_error ("unknown type 0x%02x in type_to_stind", type->type);
965 * Returns the type used in the eval stack when @type is loaded.
966 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
969 type_to_eval_stack_type (MonoType *type, MonoInst *inst) {
971 inst->type = STACK_MP;
976 switch (type->type) {
979 case MONO_TYPE_BOOLEAN:
985 inst->type = STACK_I4;
990 case MONO_TYPE_FNPTR:
991 inst->type = STACK_PTR;
993 case MONO_TYPE_CLASS:
994 case MONO_TYPE_STRING:
995 case MONO_TYPE_OBJECT:
996 case MONO_TYPE_SZARRAY:
997 case MONO_TYPE_ARRAY:
998 inst->type = STACK_OBJ;
1002 inst->type = STACK_I8;
1006 inst->type = STACK_R8;
1008 case MONO_TYPE_VALUETYPE:
1009 if (type->data.klass->enumtype) {
1010 type = type->data.klass->enum_basetype;
1013 inst->klass = type->data.klass;
1014 inst->type = STACK_VTYPE;
1017 case MONO_TYPE_TYPEDBYREF:
1018 inst->klass = mono_defaults.typed_reference_class;
1019 inst->type = STACK_VTYPE;
1021 case MONO_TYPE_GENERICINST:
1022 type = type->data.generic_inst->generic_type;
1025 g_error ("unknown type 0x%02x in eval stack type", type->type);
1030 * The following tables are used to quickly validate the IL code in type_from_op ().
1033 bin_num_table [STACK_MAX] [STACK_MAX] = {
1034 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1035 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1036 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1037 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1038 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
1039 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
1040 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1041 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1046 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
1049 /* reduce the size of this table */
1051 bin_int_table [STACK_MAX] [STACK_MAX] = {
1052 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1053 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1054 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1055 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1056 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1057 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1058 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1059 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1063 bin_comp_table [STACK_MAX] [STACK_MAX] = {
1065 {0, 1, 0, 1, 0, 0, 4, 0},
1066 {0, 0, 1, 0, 0, 0, 0, 0},
1067 {0, 1, 0, 1, 0, 2, 4, 0},
1068 {0, 0, 0, 0, 1, 0, 0, 0},
1069 {0, 0, 0, 2, 0, 1, 0, 0},
1070 {0, 4, 0, 4, 0, 0, 3, 0},
1071 {0, 0, 0, 0, 0, 0, 0, 0},
1074 /* reduce the size of this table */
1076 shift_table [STACK_MAX] [STACK_MAX] = {
1077 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1078 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1079 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1080 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1081 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1082 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1083 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1084 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1088 * Tables to map from the non-specific opcode to the matching
1089 * type-specific opcode.
1091 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
1092 static const guint16
1093 binops_op_map [STACK_MAX] = {
1094 0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, OP_PADD-CEE_ADD
1097 /* handles from CEE_NEG to CEE_CONV_U8 */
1098 static const guint16
1099 unops_op_map [STACK_MAX] = {
1100 0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, OP_PNEG-CEE_NEG
1103 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
1104 static const guint16
1105 ovfops_op_map [STACK_MAX] = {
1106 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
1109 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
1110 static const guint16
1111 ovf2ops_op_map [STACK_MAX] = {
1112 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
1115 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
1116 static const guint16
1117 ovf3ops_op_map [STACK_MAX] = {
1118 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
1121 /* handles from CEE_CEQ to CEE_CLT_UN */
1122 static const guint16
1123 ceqops_op_map [STACK_MAX] = {
1124 0, 0, OP_LCEQ-CEE_CEQ, OP_PCEQ-CEE_CEQ, OP_FCEQ-CEE_CEQ, OP_LCEQ-CEE_CEQ
1128 * Sets ins->type (the type on the eval stack) according to the
1129 * type of the opcode and the arguments to it.
1130 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1132 * FIXME: this function sets ins->type unconditionally in some cases, but
1133 * it should set it to invalid for some types (a conv.x on an object)
1136 type_from_op (MonoInst *ins) {
1137 switch (ins->opcode) {
1144 /* FIXME: check unverifiable args for STACK_MP */
1145 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1146 ins->opcode += binops_op_map [ins->type];
1153 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1154 ins->opcode += binops_op_map [ins->type];
1159 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1160 ins->opcode += binops_op_map [ins->type];
1164 /* FIXME: handle some specifics with ins->next->type */
1165 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1166 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))))
1167 ins->opcode = OP_LCOMPARE;
1174 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1175 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1179 ins->type = neg_table [ins->inst_i0->type];
1180 ins->opcode += unops_op_map [ins->type];
1183 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1184 ins->type = ins->inst_i0->type;
1186 ins->type = STACK_INV;
1187 ins->opcode += unops_op_map [ins->type];
1193 ins->type = STACK_I4;
1194 ins->opcode += unops_op_map [ins->inst_i0->type];
1197 ins->type = STACK_R8;
1198 switch (ins->inst_i0->type) {
1203 ins->opcode = OP_LCONV_TO_R_UN;
1207 case CEE_CONV_OVF_I1:
1208 case CEE_CONV_OVF_U1:
1209 case CEE_CONV_OVF_I2:
1210 case CEE_CONV_OVF_U2:
1211 case CEE_CONV_OVF_I4:
1212 case CEE_CONV_OVF_U4:
1213 ins->type = STACK_I4;
1214 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1216 case CEE_CONV_OVF_I_UN:
1217 case CEE_CONV_OVF_U_UN:
1218 ins->type = STACK_PTR;
1219 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1221 case CEE_CONV_OVF_I1_UN:
1222 case CEE_CONV_OVF_I2_UN:
1223 case CEE_CONV_OVF_I4_UN:
1224 case CEE_CONV_OVF_U1_UN:
1225 case CEE_CONV_OVF_U2_UN:
1226 case CEE_CONV_OVF_U4_UN:
1227 ins->type = STACK_I4;
1228 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1231 ins->type = STACK_PTR;
1232 switch (ins->inst_i0->type) {
1238 ins->opcode = OP_LCONV_TO_U;
1241 ins->opcode = OP_FCONV_TO_U;
1247 ins->type = STACK_I8;
1248 ins->opcode += unops_op_map [ins->inst_i0->type];
1250 case CEE_CONV_OVF_I8:
1251 case CEE_CONV_OVF_U8:
1252 ins->type = STACK_I8;
1253 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1255 case CEE_CONV_OVF_U8_UN:
1256 case CEE_CONV_OVF_I8_UN:
1257 ins->type = STACK_I8;
1258 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1262 ins->type = STACK_R8;
1263 ins->opcode += unops_op_map [ins->inst_i0->type];
1266 ins->type = STACK_R8;
1270 ins->type = STACK_I4;
1271 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1274 case CEE_CONV_OVF_I:
1275 case CEE_CONV_OVF_U:
1276 ins->type = STACK_PTR;
1277 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1280 case CEE_ADD_OVF_UN:
1282 case CEE_MUL_OVF_UN:
1284 case CEE_SUB_OVF_UN:
1285 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1286 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1289 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1296 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_MP, STACK_R8, STACK_R8, STACK_OBJ
1299 /* map ldelem.x to the matching ldind.x opcode */
1301 ldelem_to_ldind [] = {
1315 /* map stelem.x to the matching stind.x opcode */
1317 stelem_to_stind [] = {
1331 param_table [STACK_MAX] [STACK_MAX] = {
1336 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1340 switch (args->type) {
1350 for (i = 0; i < sig->param_count; ++i) {
1351 switch (args [i].type) {
1355 if (!sig->params [i]->byref)
1359 if (sig->params [i]->byref)
1361 switch (sig->params [i]->type) {
1362 case MONO_TYPE_CLASS:
1363 case MONO_TYPE_STRING:
1364 case MONO_TYPE_OBJECT:
1365 case MONO_TYPE_SZARRAY:
1366 case MONO_TYPE_ARRAY:
1373 if (sig->params [i]->byref)
1375 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1384 /*if (!param_table [args [i].type] [sig->params [i]->type])
1392 * When we need a pointer to the current domain many times in a method, we
1393 * call mono_domain_get() once and we store the result in a local variable.
1394 * This function returns the variable that represents the MonoDomain*.
1396 inline static MonoInst *
1397 mono_get_domainvar (MonoCompile *cfg)
1399 if (!cfg->domainvar)
1400 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1401 return cfg->domainvar;
1405 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
1408 int num = cfg->num_varinfo;
1410 if ((num + 1) >= cfg->varinfo_count) {
1411 cfg->varinfo_count = (cfg->varinfo_count + 2) * 2;
1412 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
1413 cfg->vars = (MonoMethodVar **)g_realloc (cfg->vars, sizeof (MonoMethodVar*) * cfg->varinfo_count);
1416 /*g_print ("created temp %d of type 0x%x\n", num, type->type);*/
1417 mono_jit_stats.allocate_var++;
1419 MONO_INST_NEW (cfg, inst, opcode);
1420 inst->inst_c0 = num;
1421 inst->inst_vtype = type;
1422 inst->klass = mono_class_from_mono_type (type);
1423 /* if set to 1 the variable is native */
1426 cfg->varinfo [num] = inst;
1428 cfg->vars [num] = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMethodVar));
1429 MONO_INIT_VARINFO (cfg->vars [num], num);
1432 //g_print ("created temp %d of type %s\n", num, mono_type_get_name (type));
1437 type_from_stack_type (MonoInst *ins) {
1438 switch (ins->type) {
1439 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1440 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1441 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1442 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1443 case STACK_MP: return &mono_defaults.int_class->byval_arg;
1444 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1445 case STACK_VTYPE: return &ins->klass->byval_arg;
1447 g_error ("stack type %d to montype not handled\n", ins->type);
1453 array_access_to_klass (int opcode)
1457 return mono_defaults.byte_class;
1459 return mono_defaults.uint16_class;
1462 return mono_defaults.int_class;
1465 return mono_defaults.sbyte_class;
1468 return mono_defaults.int16_class;
1471 return mono_defaults.int32_class;
1473 return mono_defaults.uint32_class;
1476 return mono_defaults.int64_class;
1479 return mono_defaults.single_class;
1482 return mono_defaults.double_class;
1483 case CEE_LDELEM_REF:
1484 case CEE_STELEM_REF:
1485 return mono_defaults.object_class;
1487 g_assert_not_reached ();
1493 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
1497 MONO_ADD_INS (bb, inst);
1500 switch (bb->last_ins->opcode) {
1514 while (prev->next && prev->next != bb->last_ins)
1516 if (prev == bb->code) {
1517 if (bb->last_ins == bb->code) {
1518 inst->next = bb->code;
1521 inst->next = prev->next;
1525 inst->next = bb->last_ins;
1529 // g_warning ("handle conditional jump in add_ins_to_end ()\n");
1531 MONO_ADD_INS (bb, inst);
1537 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
1539 MonoInst *inst, *load;
1541 NEW_TEMPLOAD (cfg, load, src);
1543 NEW_TEMPSTORE (cfg, inst, dest, load);
1544 if (inst->opcode == CEE_STOBJ) {
1545 NEW_TEMPLOADA (cfg, inst, dest);
1546 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE);
1548 inst->cil_code = NULL;
1549 mono_add_ins_to_end (bb, inst);
1554 * We try to share variables when possible
1557 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1562 /* inlining can result in deeper stacks */
1563 if (slot >= mono_method_get_header (cfg->method)->max_stack)
1564 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1566 pos = ins->type - 1 + slot * STACK_MAX;
1568 switch (ins->type) {
1575 if ((vnum = cfg->intvars [pos]))
1576 return cfg->varinfo [vnum];
1577 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1578 cfg->intvars [pos] = res->inst_c0;
1581 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1587 * This function is called to handle items that are left on the evaluation stack
1588 * at basic block boundaries. What happens is that we save the values to local variables
1589 * and we reload them later when first entering the target basic block (with the
1590 * handle_loaded_temps () function).
1591 * It is also used to handle items on the stack in store opcodes, since it is
1592 * possible that the variable to be stored into is already on the stack, in
1593 * which case its old value should be used.
1594 * A single joint point will use the same variables (stored in the array bb->out_stack or
1595 * bb->in_stack, if the basic block is before or after the joint point).
1598 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count) {
1600 MonoBasicBlock *outb;
1601 MonoInst *inst, **locals;
1606 if (cfg->verbose_level > 3)
1607 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
1608 if (!bb->out_scount) {
1609 bb->out_scount = count;
1610 //g_print ("bblock %d has out:", bb->block_num);
1612 for (i = 0; i < bb->out_count; ++i) {
1613 outb = bb->out_bb [i];
1614 //g_print (" %d", outb->block_num);
1615 if (outb->in_stack) {
1617 bb->out_stack = outb->in_stack;
1623 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1624 for (i = 0; i < count; ++i) {
1626 * try to reuse temps already allocated for this purpouse, if they occupy the same
1627 * stack slot and if they are of the same type.
1628 * This won't cause conflicts since if 'local' is used to
1629 * store one of the values in the in_stack of a bblock, then
1630 * the same variable will be used for the same outgoing stack
1632 * This doesn't work when inlining methods, since the bblocks
1633 * in the inlined methods do not inherit their in_stack from
1634 * the bblock they are inlined to. See bug #58863 for an
1637 if (cfg->inlined_method)
1638 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1640 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1645 for (i = 0; i < bb->out_count; ++i) {
1646 outb = bb->out_bb [i];
1647 if (outb->in_scount)
1648 continue; /* check they are the same locals */
1649 outb->in_scount = count;
1650 outb->in_stack = bb->out_stack;
1653 locals = bb->out_stack;
1654 for (i = 0; i < count; ++i) {
1655 /* add store ops at the end of the bb, before the branch */
1656 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1657 if (inst->opcode == CEE_STOBJ) {
1658 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
1659 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE);
1661 inst->cil_code = sp [i]->cil_code;
1662 mono_add_ins_to_end (bb, inst);
1664 if (cfg->verbose_level > 3)
1665 g_print ("storing %d to temp %d\n", i, locals [i]->inst_c0);
1669 * It is possible that the out bblocks already have in_stack assigned, and
1670 * the in_stacks differ. In this case, we will store to all the different
1677 /* Find a bblock which has a different in_stack */
1679 while (bindex < bb->out_count) {
1680 outb = bb->out_bb [bindex];
1681 if (outb->in_stack != locals) {
1683 * Instead of storing sp [i] to locals [i], we need to store
1684 * locals [i] to <new locals>[i], since the sp [i] tree can't
1685 * be shared between trees.
1687 for (i = 0; i < count; ++i)
1688 mono_add_varcopy_to_end (cfg, bb, locals [i]->inst_c0, outb->in_stack [i]->inst_c0);
1689 locals = outb->in_stack;
1701 ret_type_to_call_opcode (MonoType *type, int calli, int virt)
1704 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1707 switch (type->type) {
1708 case MONO_TYPE_VOID:
1709 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
1712 case MONO_TYPE_BOOLEAN:
1715 case MONO_TYPE_CHAR:
1718 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1722 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1723 case MONO_TYPE_CLASS:
1724 case MONO_TYPE_STRING:
1725 case MONO_TYPE_OBJECT:
1726 case MONO_TYPE_SZARRAY:
1727 case MONO_TYPE_ARRAY:
1728 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1731 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
1734 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
1735 case MONO_TYPE_VALUETYPE:
1736 if (type->data.klass->enumtype) {
1737 type = type->data.klass->enum_basetype;
1740 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1741 case MONO_TYPE_TYPEDBYREF:
1742 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1743 case MONO_TYPE_GENERICINST:
1744 type = type->data.generic_inst->generic_type;
1747 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
1753 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
1755 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
1757 ji->ip.label = label;
1758 ji->type = MONO_PATCH_INFO_SWITCH;
1759 ji->data.table = bbs;
1760 ji->next = cfg->patch_info;
1761 ji->table_size = num_blocks;
1762 cfg->patch_info = ji;
1766 * When we add a tree of instructions, we need to ensure the instructions currently
1767 * on the stack are executed before (like, if we load a value from a local).
1768 * We ensure this by saving the currently loaded values to temps and rewriting the
1769 * instructions to load the values.
1770 * This is not done for opcodes that terminate a basic block (because it's handled already
1771 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
1774 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
1776 MonoInst *load, *store, *temp, *ins;
1778 while (stack < sp) {
1780 /* handle also other constants */
1781 if (ins->opcode != OP_ICONST) {
1782 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1783 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1784 store->cil_code = ins->cil_code;
1785 if (store->opcode == CEE_STOBJ) {
1786 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
1787 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE);
1789 MONO_ADD_INS (bblock, store);
1790 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1791 load->cil_code = ins->cil_code;
1799 * Prepare arguments for passing to a function call.
1800 * Return a non-zero value if the arguments can't be passed to the given
1802 * The type checks are not yet complete and some conversions may need
1803 * casts on 32 or 64 bit architectures.
1806 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
1808 MonoType *simple_type;
1812 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
1816 for (i = 0; i < sig->param_count; ++i) {
1817 if (sig->params [i]->byref) {
1819 * check the result of ldelema is only passed as an argument if the byref
1820 * type matches exactly the array element type.
1821 * FIXME: if the argument as been saved on the stack as part of the
1822 * interface variable code (the value was on the stack at a basic block boundary)
1823 * we need to add the check in that case, too.
1825 if (args [i]->opcode == CEE_LDELEMA) {
1827 MonoClass *exact_class = mono_class_from_mono_type (sig->params [i]);
1828 if (!exact_class->valuetype) {
1829 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
1830 check->cil_code = args [i]->cil_code;
1831 check->klass = exact_class;
1832 check->inst_left = args [i]->inst_left;
1833 check->type = STACK_OBJ;
1834 args [i]->inst_left = check;
1837 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
1841 simple_type = sig->params [i];
1843 switch (simple_type->type) {
1844 case MONO_TYPE_VOID:
1849 case MONO_TYPE_BOOLEAN:
1852 case MONO_TYPE_CHAR:
1855 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
1861 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
1864 case MONO_TYPE_CLASS:
1865 case MONO_TYPE_STRING:
1866 case MONO_TYPE_OBJECT:
1867 case MONO_TYPE_SZARRAY:
1868 case MONO_TYPE_ARRAY:
1869 if (args [i]->type != STACK_OBJ)
1874 if (args [i]->type != STACK_I8)
1879 if (args [i]->type != STACK_R8)
1882 case MONO_TYPE_VALUETYPE:
1883 if (simple_type->data.klass->enumtype) {
1884 simple_type = simple_type->data.klass->enum_basetype;
1887 if (args [i]->type != STACK_VTYPE)
1890 case MONO_TYPE_TYPEDBYREF:
1891 if (args [i]->type != STACK_VTYPE)
1894 case MONO_TYPE_GENERICINST:
1895 simple_type = simple_type->data.generic_inst->generic_type;
1899 g_error ("unknown type 0x%02x in check_call_signature",
1907 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
1908 const guint8 *ip, gboolean to_end)
1910 MonoInst *temp, *store, *ins = (MonoInst*)call;
1911 MonoType *ret = sig->ret;
1913 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
1915 call->inst.type = STACK_OBJ;
1916 call->inst.opcode = CEE_CALL;
1917 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
1919 type_to_eval_stack_type (ret, ins);
1920 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
1923 if (MONO_TYPE_ISSTRUCT (ret)) {
1926 /* we use this to allocate native sized structs */
1927 temp->unused = sig->pinvoke;
1929 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
1930 if (call->inst.opcode == OP_VCALL)
1931 ins->inst_left = loada;
1933 ins->inst_right = loada; /* a virtual or indirect call */
1936 mono_add_ins_to_end (bblock, ins);
1938 MONO_ADD_INS (bblock, ins);
1940 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1941 store->cil_code = ip;
1943 mono_add_ins_to_end (bblock, store);
1945 MONO_ADD_INS (bblock, store);
1947 return temp->inst_c0;
1950 mono_add_ins_to_end (bblock, ins);
1952 MONO_ADD_INS (bblock, ins);
1957 inline static MonoCallInst *
1958 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1959 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
1964 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual));
1966 call->inst.cil_code = ip;
1968 call->signature = sig;
1969 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
1971 for (arg = call->out_args; arg;) {
1972 MonoInst *narg = arg->next;
1977 mono_add_ins_to_end (bblock, arg);
1979 MONO_ADD_INS (bblock, arg);
1986 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1987 MonoInst **args, MonoInst *addr, const guint8 *ip)
1989 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
1991 call->inst.inst_i0 = addr;
1993 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
1996 static MonoCallInst*
1997 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
1998 MonoInst **args, const guint8 *ip, MonoInst *this)
2000 gboolean virtual = this != NULL;
2003 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, FALSE);
2005 if (this && sig->hasthis &&
2006 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
2007 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
2008 call->method = mono_marshal_get_remoting_invoke_with_check (method);
2010 call->method = method;
2012 call->inst.flags |= MONO_INST_HAS_METHOD;
2013 call->inst.inst_left = this;
2019 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
2020 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
2022 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, signature, args, ip, this);
2024 return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
2028 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
2029 MonoInst **args, const guint8 *ip, gboolean ret_object, gboolean to_end)
2035 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
2037 return mono_spill_call (cfg, bblock, call, sig, ret_object, ip, to_end);
2041 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
2043 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
2046 g_warning ("unregistered JIT ICall");
2047 g_assert_not_reached ();
2050 return mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, args, ip, FALSE, FALSE);
2054 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
2056 MonoInst *ins, *temp = NULL, *store, *load, *begin;
2057 MonoInst *last_arg = NULL;
2061 //g_print ("emulating: ");
2062 //mono_print_tree_nl (tree);
2063 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE));
2064 ins = (MonoInst*)call;
2066 call->inst.cil_code = tree->cil_code;
2068 call->signature = info->sig;
2070 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
2072 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2073 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
2074 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2075 store->cil_code = tree->cil_code;
2080 nargs = info->sig->param_count + info->sig->hasthis;
2082 for (last_arg = call->out_args; last_arg && last_arg->next; last_arg = last_arg->next) ;
2085 last_arg->next = store;
2088 begin = call->out_args;
2092 if (cfg->prev_ins) {
2094 * This assumes that that in a tree, emulate_opcode is called for a
2095 * node before it is called for its children. dec_foreach needs to
2096 * take this into account.
2098 store->next = cfg->prev_ins->next;
2099 cfg->prev_ins->next = begin;
2101 store->next = cfg->cbb->code;
2102 cfg->cbb->code = begin;
2105 call->fptr = mono_icall_get_wrapper (info);
2107 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2108 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2113 static MonoMethodSignature *
2114 mono_get_element_address_signature (int arity)
2116 static GHashTable *sighash = NULL;
2117 MonoMethodSignature *res;
2120 EnterCriticalSection (&jit_mutex);
2122 sighash = g_hash_table_new (NULL, NULL);
2124 else if ((res = g_hash_table_lookup (sighash, (gpointer)arity))) {
2125 LeaveCriticalSection (&jit_mutex);
2129 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
2132 #ifdef MONO_ARCH_VARARG_ICALLS
2133 /* Only set this only some archs since not all backends can handle varargs+pinvoke */
2134 res->call_convention = MONO_CALL_VARARG;
2136 res->params [0] = &mono_defaults.array_class->byval_arg;
2138 for (i = 1; i <= arity; i++)
2139 res->params [i] = &mono_defaults.int_class->byval_arg;
2141 res->ret = &mono_defaults.int_class->byval_arg;
2143 g_hash_table_insert (sighash, (gpointer)arity, res);
2144 LeaveCriticalSection (&jit_mutex);
2149 static MonoMethodSignature *
2150 mono_get_array_new_va_signature (int arity)
2152 static GHashTable *sighash = NULL;
2153 MonoMethodSignature *res;
2156 EnterCriticalSection (&jit_mutex);
2158 sighash = g_hash_table_new (NULL, NULL);
2160 else if ((res = g_hash_table_lookup (sighash, (gpointer)arity))) {
2161 LeaveCriticalSection (&jit_mutex);
2165 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
2168 #ifdef MONO_ARCH_VARARG_ICALLS
2169 /* Only set this only some archs since not all backends can handle varargs+pinvoke */
2170 res->call_convention = MONO_CALL_VARARG;
2173 res->params [0] = &mono_defaults.int_class->byval_arg;
2174 for (i = 0; i < arity; i++)
2175 res->params [i + 1] = &mono_defaults.int_class->byval_arg;
2177 res->ret = &mono_defaults.int_class->byval_arg;
2179 g_hash_table_insert (sighash, (gpointer)arity, res);
2180 LeaveCriticalSection (&jit_mutex);
2186 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native) {
2187 MonoInst *iargs [3];
2193 * This check breaks with spilled vars... need to handle it during verification anyway.
2194 * g_assert (klass && klass == src->klass && klass == dest->klass);
2198 n = mono_class_native_size (klass, &align);
2200 n = mono_class_value_size (klass, &align);
2202 if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
2204 MONO_INST_NEW (cfg, inst, OP_MEMCPY);
2205 inst->inst_left = dest;
2206 inst->inst_right = src;
2207 inst->cil_code = ip;
2209 MONO_ADD_INS (bblock, inst);
2214 NEW_ICONST (cfg, iargs [2], n);
2216 mono_emit_native_call (cfg, bblock, helper_memcpy, helper_sig_memcpy, iargs, ip, FALSE, to_end);
2220 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
2222 MonoInst *iargs [2];
2223 MonoInst *ins, *zero_int32;
2226 NEW_ICONST (cfg, zero_int32, 0);
2228 mono_class_init (klass);
2229 n = mono_class_value_size (klass, NULL);
2230 MONO_INST_NEW (cfg, ins, 0);
2232 ins->inst_left = dest;
2233 ins->inst_right = zero_int32;
2236 ins->opcode = CEE_STIND_I1;
2237 MONO_ADD_INS (bblock, ins);
2240 ins->opcode = CEE_STIND_I2;
2241 MONO_ADD_INS (bblock, ins);
2244 ins->opcode = CEE_STIND_I4;
2245 MONO_ADD_INS (bblock, ins);
2248 if (n <= sizeof (gpointer) * 5) {
2249 ins->opcode = OP_MEMSET;
2252 MONO_ADD_INS (bblock, ins);
2255 handle_loaded_temps (cfg, bblock, stack_start, sp);
2256 NEW_ICONST (cfg, ins, n);
2259 mono_emit_jit_icall (cfg, bblock, helper_initobj, iargs, ip);
2265 handle_alloc (MonoCompile *cfg, MonoBasicBlock *bblock, MonoClass *klass, const guchar *ip)
2267 MonoInst *iargs [2];
2270 if (cfg->opt & MONO_OPT_SHARED) {
2271 NEW_DOMAINCONST (cfg, iargs [0]);
2272 NEW_CLASSCONST (cfg, iargs [1], klass);
2274 alloc_ftn = mono_object_new;
2276 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
2279 alloc_ftn = mono_class_get_allocation_ftn (vtable, &pass_lw);
2281 guint32 lw = vtable->klass->instance_size;
2282 lw = ((lw + (sizeof (gpointer) - 1)) & ~(sizeof (gpointer) - 1)) / sizeof (gpointer);
2283 NEW_ICONST (cfg, iargs [0], lw);
2284 NEW_VTABLECONST (cfg, iargs [1], vtable);
2287 NEW_VTABLECONST (cfg, iargs [0], vtable);
2290 return mono_emit_jit_icall (cfg, bblock, alloc_ftn, iargs, ip);
2294 handle_box (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *val, const guchar *ip, MonoClass *klass)
2296 MonoInst *dest, *vtoffset, *add, *vstore;
2299 temp = handle_alloc (cfg, bblock, klass, ip);
2300 NEW_TEMPLOAD (cfg, dest, temp);
2301 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
2302 MONO_INST_NEW (cfg, add, OP_PADD);
2303 add->inst_left = dest;
2304 add->inst_right = vtoffset;
2307 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
2308 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
2309 vstore->cil_code = ip;
2310 vstore->inst_left = add;
2311 vstore->inst_right = val;
2313 if (vstore->opcode == CEE_STOBJ) {
2314 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
2316 MONO_ADD_INS (bblock, vstore);
2318 NEW_TEMPLOAD (cfg, dest, temp);
2323 handle_array_new (MonoCompile *cfg, MonoBasicBlock *bblock, int rank, MonoInst **sp, unsigned char *ip)
2325 MonoMethodSignature *esig;
2326 char icall_name [256];
2327 MonoJitICallInfo *info;
2329 /* Need to register the icall so it gets an icall wrapper */
2330 sprintf (icall_name, "ves_array_new_va_%d", rank);
2332 info = mono_find_jit_icall_by_name (icall_name);
2334 esig = mono_get_array_new_va_signature (rank);
2335 info = mono_register_jit_icall (mono_array_new_va, g_strdup (icall_name), esig, FALSE);
2338 cfg->flags |= MONO_CFG_HAS_VARARGS;
2340 return mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, sp, ip, TRUE, FALSE);
2343 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
2346 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
2348 MonoMethodHeader *header = mono_method_get_header (method);
2349 MonoMethodSignature *signature = method->signature;
2353 #ifdef MONO_ARCH_HAVE_LMF_OPS
2354 if (((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2355 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) &&
2356 !MONO_TYPE_ISSTRUCT (signature->ret) && (method->klass->parent != mono_defaults.array_class))
2360 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2361 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2362 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
2363 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
2364 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2365 (method->klass->marshalbyref) ||
2366 !header || header->num_clauses ||
2367 /* fixme: why cant we inline valuetype returns? */
2368 MONO_TYPE_ISSTRUCT (signature->ret))
2371 /* its not worth to inline methods with valuetype arguments?? */
2372 for (i = 0; i < signature->param_count; i++) {
2373 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
2379 * if we can initialize the class of the method right away, we do,
2380 * otherwise we don't allow inlining if the class needs initialization,
2381 * since it would mean inserting a call to mono_runtime_class_init()
2382 * inside the inlined code
2384 if (!(cfg->opt & MONO_OPT_SHARED)) {
2385 vtable = mono_class_vtable (cfg->domain, method->klass);
2386 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
2387 if (cfg->run_cctors)
2388 mono_runtime_class_init (vtable);
2390 else if (!vtable->initialized && mono_class_needs_cctor_run (method->klass, NULL))
2394 * If we're compiling for shared code
2395 * the cctor will need to be run at aot method load time, for example,
2396 * or at the end of the compilation of the inlining method.
2398 if (mono_class_needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
2401 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
2403 /* also consider num_locals? */
2404 if (getenv ("MONO_INLINELIMIT")) {
2405 if (header->code_size < atoi (getenv ("MONO_INLINELIMIT"))) {
2408 } else if (header->code_size < 20)
2415 mini_get_ldelema_ins (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
2419 MonoMethodSignature *esig;
2420 char icall_name [256];
2421 MonoJitICallInfo *info;
2423 rank = cmethod->signature->param_count - (is_set? 1: 0);
2425 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
2427 NEW_GROUP (cfg, indexes, sp [1], sp [2]);
2428 MONO_INST_NEW (cfg, addr, OP_LDELEMA2D);
2429 addr->inst_left = sp [0];
2430 addr->inst_right = indexes;
2431 addr->cil_code = ip;
2432 addr->type = STACK_MP;
2433 addr->klass = cmethod->klass;
2437 /* Need to register the icall so it gets an icall wrapper */
2438 sprintf (icall_name, "ves_array_element_address_%d", rank);
2440 info = mono_find_jit_icall_by_name (icall_name);
2442 esig = mono_get_element_address_signature (rank);
2443 info = mono_register_jit_icall (ves_array_element_address, g_strdup (icall_name), esig, FALSE);
2446 temp = mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, sp, ip, FALSE, FALSE);
2447 cfg->flags |= MONO_CFG_HAS_VARARGS;
2449 NEW_TEMPLOAD (cfg, addr, temp);
2453 static MonoJitICallInfo **emul_opcode_map = NULL;
2455 static inline MonoJitICallInfo *
2456 mono_find_jit_opcode_emulation (int opcode)
2458 if (emul_opcode_map)
2459 return emul_opcode_map [opcode];
2465 mini_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
2470 static MonoClass *runtime_helpers_class = NULL;
2471 if (! runtime_helpers_class)
2472 runtime_helpers_class = mono_class_from_name (mono_defaults.corlib,
2473 "System.Runtime.CompilerServices", "RuntimeHelpers");
2475 if (cmethod->klass == mono_defaults.string_class) {
2476 if (cmethod->name [0] != 'g')
2479 if (strcmp (cmethod->name, "get_Chars") == 0)
2481 else if (strcmp (cmethod->name, "get_Length") == 0)
2484 } else if (cmethod->klass == mono_defaults.object_class) {
2485 if (strcmp (cmethod->name, "GetType") == 0)
2489 } else if (cmethod->klass == mono_defaults.array_class) {
2490 if (strcmp (cmethod->name, "get_Rank") == 0)
2492 else if (strcmp (cmethod->name, "get_Length") == 0)
2496 } else if (cmethod->klass == runtime_helpers_class) {
2497 if (strcmp (cmethod->name, "get_OffsetToStringData") == 0) {
2498 NEW_ICONST (cfg, ins, G_STRUCT_OFFSET (MonoString, chars));
2502 } else if (cmethod->klass == mono_defaults.thread_class) {
2503 if (strcmp (cmethod->name, "get_CurrentThread") == 0 && (ins = mono_arch_get_thread_intrinsic (cfg)))
2507 op = mono_arch_get_opcode_for_method (cfg, cmethod, fsig, args);
2511 pc = fsig->param_count + fsig->hasthis;
2512 MONO_INST_NEW (cfg, ins, op);
2515 ins->inst_i0 = args [0];
2517 ins->inst_i1 = args [1];
2524 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
2526 MonoInst *store, *temp;
2529 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
2531 if (!sig->hasthis && sig->param_count == 0)
2535 if (sp [0]->opcode == OP_ICONST) {
2538 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
2540 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2541 store->cil_code = sp [0]->cil_code;
2542 MONO_ADD_INS (bblock, store);
2547 for (i = 0; i < sig->param_count; ++i) {
2548 if (sp [0]->opcode == OP_ICONST) {
2551 temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
2553 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2554 store->cil_code = sp [0]->cil_code;
2555 if (store->opcode == CEE_STOBJ) {
2556 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2557 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE);
2559 MONO_ADD_INS (bblock, store);
2567 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
2568 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b, gboolean inline_allways)
2570 MonoInst *ins, *rvar = NULL;
2571 MonoMethodHeader *cheader;
2572 MonoBasicBlock *ebblock, *sbblock;
2573 int i, costs, new_locals_offset;
2574 MonoMethod *prev_inlined_method;
2576 if (cfg->verbose_level > 2)
2577 g_print ("INLINE START %p %s -> %s\n", cmethod, mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2579 if (!cmethod->inline_info) {
2580 mono_jit_stats.inlineable_methods++;
2581 cmethod->inline_info = 1;
2583 /* allocate space to store the return value */
2584 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2585 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2588 /* allocate local variables */
2589 cheader = mono_method_get_header (cmethod);
2590 new_locals_offset = cfg->num_varinfo;
2591 for (i = 0; i < cheader->num_locals; ++i)
2592 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
2594 /* allocate starte and end blocks */
2595 sbblock = NEW_BBLOCK (cfg);
2596 sbblock->block_num = cfg->num_bblocks++;
2597 sbblock->real_offset = real_offset;
2599 ebblock = NEW_BBLOCK (cfg);
2600 ebblock->block_num = cfg->num_bblocks++;
2601 ebblock->real_offset = real_offset;
2603 prev_inlined_method = cfg->inlined_method;
2604 cfg->inlined_method = cmethod;
2606 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
2608 cfg->inlined_method = prev_inlined_method;
2610 if ((costs >= 0 && costs < 60) || inline_allways) {
2611 if (cfg->verbose_level > 2)
2612 g_print ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2614 mono_jit_stats.inlined_methods++;
2616 /* always add some code to avoid block split failures */
2617 MONO_INST_NEW (cfg, ins, CEE_NOP);
2618 MONO_ADD_INS (bblock, ins);
2621 bblock->next_bb = sbblock;
2622 link_bblock (cfg, bblock, sbblock);
2625 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
2631 if (cfg->verbose_level > 2)
2632 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
2638 * Some of these comments may well be out-of-date.
2639 * Design decisions: we do a single pass over the IL code (and we do bblock
2640 * splitting/merging in the few cases when it's required: a back jump to an IL
2641 * address that was not already seen as bblock starting point).
2642 * Code is validated as we go (full verification is still better left to metadata/verify.c).
2643 * Complex operations are decomposed in simpler ones right away. We need to let the
2644 * arch-specific code peek and poke inside this process somehow (except when the
2645 * optimizations can take advantage of the full semantic info of coarse opcodes).
2646 * All the opcodes of the form opcode.s are 'normalized' to opcode.
2647 * MonoInst->opcode initially is the IL opcode or some simplification of that
2648 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
2649 * opcode with value bigger than OP_LAST.
2650 * At this point the IR can be handed over to an interpreter, a dumb code generator
2651 * or to the optimizing code generator that will translate it to SSA form.
2653 * Profiling directed optimizations.
2654 * We may compile by default with few or no optimizations and instrument the code
2655 * or the user may indicate what methods to optimize the most either in a config file
2656 * or through repeated runs where the compiler applies offline the optimizations to
2657 * each method and then decides if it was worth it.
2660 * * consider using an array instead of an hash table (bb_hash)
2663 #define CHECK_TYPE(ins) if (!(ins)->type) goto unverified
2664 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) goto unverified
2665 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) goto unverified
2666 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) goto unverified
2667 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) goto unverified
2668 #define CHECK_OPSIZE(size) if (ip + size > end) goto unverified
2671 /* offset from br.s -> br like opcodes */
2672 #define BIG_BRANCH_OFFSET 13
2675 get_basic_blocks (MonoCompile *cfg, GHashTable *bbhash, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
2677 unsigned char *ip = start;
2678 unsigned char *target;
2681 MonoBasicBlock *bblock;
2682 const MonoOpcode *opcode;
2685 cli_addr = ip - start;
2686 i = mono_opcode_value ((const guint8 **)&ip, end);
2689 opcode = &mono_opcodes [i];
2690 switch (opcode->argument) {
2691 case MonoInlineNone:
2694 case MonoInlineString:
2695 case MonoInlineType:
2696 case MonoInlineField:
2697 case MonoInlineMethod:
2700 case MonoShortInlineR:
2707 case MonoShortInlineVar:
2708 case MonoShortInlineI:
2711 case MonoShortInlineBrTarget:
2712 target = start + cli_addr + 2 + (signed char)ip [1];
2713 GET_BBLOCK (cfg, bbhash, bblock, target);
2716 case MonoInlineBrTarget:
2717 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
2718 GET_BBLOCK (cfg, bbhash, bblock, target);
2721 case MonoInlineSwitch: {
2722 guint32 n = read32 (ip + 1);
2725 cli_addr += 5 + 4 * n;
2726 target = start + cli_addr;
2727 GET_BBLOCK (cfg, bbhash, bblock, target);
2729 for (j = 0; j < n; ++j) {
2730 target = start + cli_addr + (gint32)read32 (ip);
2731 GET_BBLOCK (cfg, bbhash, bblock, target);
2741 g_assert_not_reached ();
2751 emit_tree (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *ins)
2753 MonoInst *store, *temp, *load;
2754 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
2755 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2756 store->cil_code = ins->cil_code;
2757 MONO_ADD_INS (bblock, store);
2758 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2759 load->cil_code = ins->cil_code;
2764 * mono_method_to_ir: translates IL into basic blocks containing trees
2767 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
2768 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
2769 guint inline_offset, gboolean is_virtual_call)
2771 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
2772 MonoInst *ins, **sp, **stack_start;
2773 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
2775 MonoMethod *cmethod;
2776 MonoInst **arg_array;
2777 MonoMethodHeader *header;
2779 guint32 token, ins_flag;
2781 MonoClass *constrained_call = NULL;
2782 unsigned char *ip, *end, *target, *err_pos;
2783 static double r8_0 = 0.0;
2784 MonoMethodSignature *sig;
2785 MonoGenericContext *generic_context = NULL;
2786 MonoGenericContainer *generic_container = NULL;
2787 MonoType **param_types;
2788 GList *bb_recheck = NULL, *tmp;
2789 int i, n, start_new_bblock, align;
2790 int num_calls = 0, inline_costs = 0;
2791 int *filter_lengths = NULL;
2792 int breakpoint_id = 0;
2793 guint real_offset, num_args;
2795 image = method->klass->image;
2796 header = mono_method_get_header (method);
2797 generic_container = ((MonoMethodNormal *)method)->generic_container;
2798 sig = method->signature;
2799 num_args = sig->hasthis + sig->param_count;
2800 ip = (unsigned char*)header->code;
2801 end = ip + header->code_size;
2802 mono_jit_stats.cil_code_size += header->code_size;
2804 if (method->signature->is_inflated)
2805 generic_context = ((MonoMethodInflated *) method)->context;
2806 else if (generic_container)
2807 generic_context = generic_container->context;
2809 if (cfg->method == method) {
2811 bbhash = cfg->bb_hash;
2813 real_offset = inline_offset;
2814 bbhash = g_hash_table_new (g_direct_hash, NULL);
2817 if (cfg->verbose_level > 2)
2818 g_print ("method to IR %s\n", mono_method_full_name (method, TRUE));
2820 dont_inline = g_list_prepend (dont_inline, method);
2821 if (cfg->method == method) {
2823 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
2824 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
2827 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
2828 start_bblock->cil_code = NULL;
2829 start_bblock->cil_length = 0;
2830 start_bblock->block_num = cfg->num_bblocks++;
2833 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
2834 end_bblock->cil_code = NULL;
2835 end_bblock->cil_length = 0;
2836 end_bblock->block_num = cfg->num_bblocks++;
2837 g_assert (cfg->num_bblocks == 2);
2839 arg_array = alloca (sizeof (MonoInst *) * num_args);
2840 for (i = num_args - 1; i >= 0; i--)
2841 arg_array [i] = cfg->varinfo [i];
2843 if (header->num_clauses) {
2844 int size = sizeof (int) * header->num_clauses;
2845 filter_lengths = alloca (size);
2846 memset (filter_lengths, 0, size);
2848 cfg->spvars = g_hash_table_new (NULL, NULL);
2850 /* handle exception clauses */
2851 for (i = 0; i < header->num_clauses; ++i) {
2852 //unsigned char *p = ip;
2853 MonoExceptionClause *clause = &header->clauses [i];
2854 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->try_offset);
2855 tblock->real_offset = clause->try_offset;
2856 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
2857 tblock->real_offset = clause->handler_offset;
2859 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
2860 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2861 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2862 MONO_ADD_INS (tblock, ins);
2865 /*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);
2867 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
2869 /* catch and filter blocks get the exception object on the stack */
2870 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
2871 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2872 /* mostly like handle_stack_args (), but just sets the input args */
2873 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
2875 cfg->exvar = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
2876 /* prevent it from being register allocated */
2877 cfg->exvar->flags |= MONO_INST_INDIRECT;
2879 tblock->in_scount = 1;
2880 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2881 tblock->in_stack [0] = cfg->exvar;
2883 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2884 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->data.filter_offset);
2885 tblock->real_offset = clause->data.filter_offset;
2886 tblock->in_scount = 1;
2887 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2888 tblock->in_stack [0] = cfg->exvar;
2889 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2890 MONO_ADD_INS (tblock, ins);
2896 arg_array = alloca (sizeof (MonoInst *) * num_args);
2897 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
2900 /* FIRST CODE BLOCK */
2901 bblock = NEW_BBLOCK (cfg);
2902 bblock->cil_code = ip;
2904 ADD_BBLOCK (cfg, bbhash, bblock);
2906 if (cfg->method == method) {
2907 breakpoint_id = mono_debugger_method_has_breakpoint (method);
2908 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
2909 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2910 MONO_ADD_INS (bblock, ins);
2914 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED))) || mono_compile_aot) {
2915 /* we use a separate basic block for the initialization code */
2916 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
2917 init_localsbb->real_offset = real_offset;
2918 start_bblock->next_bb = init_localsbb;
2919 init_localsbb->next_bb = bblock;
2920 link_bblock (cfg, start_bblock, init_localsbb);
2921 link_bblock (cfg, init_localsbb, bblock);
2922 init_localsbb->block_num = cfg->num_bblocks++;
2924 start_bblock->next_bb = bblock;
2925 link_bblock (cfg, start_bblock, bblock);
2928 if (get_basic_blocks (cfg, bbhash, header, real_offset, ip, end, &err_pos)) {
2933 mono_debug_init_method (cfg, bblock, breakpoint_id);
2935 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
2937 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
2938 for (n = 0; n < sig->param_count; ++n)
2939 param_types [n + sig->hasthis] = sig->params [n];
2941 /* do this somewhere outside - not here */
2942 NEW_ICONST (cfg, zero_int32, 0);
2943 NEW_ICONST (cfg, zero_int64, 0);
2944 zero_int64->type = STACK_I8;
2945 NEW_PCONST (cfg, zero_ptr, 0);
2946 NEW_PCONST (cfg, zero_obj, 0);
2947 zero_obj->type = STACK_OBJ;
2949 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
2950 zero_r8->type = STACK_R8;
2951 zero_r8->inst_p0 = &r8_0;
2953 /* add a check for this != NULL to inlined methods */
2954 if (is_virtual_call) {
2955 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
2956 NEW_ARGLOAD (cfg, ins->inst_left, 0);
2958 MONO_ADD_INS (bblock, ins);
2961 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
2962 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
2965 start_new_bblock = 0;
2968 if (cfg->method == method)
2969 real_offset = ip - header->code;
2971 real_offset = inline_offset;
2973 if (start_new_bblock) {
2974 bblock->cil_length = ip - bblock->cil_code;
2975 if (start_new_bblock == 2) {
2976 g_assert (ip == tblock->cil_code);
2978 GET_BBLOCK (cfg, bbhash, tblock, ip);
2980 bblock->next_bb = tblock;
2982 start_new_bblock = 0;
2983 for (i = 0; i < bblock->in_scount; ++i) {
2984 if (cfg->verbose_level > 3)
2985 g_print ("loading %d from temp %d\n", i, bblock->in_stack [i]->inst_c0);
2986 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2990 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
2991 link_bblock (cfg, bblock, tblock);
2992 if (sp != stack_start) {
2993 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2996 bblock->next_bb = tblock;
2998 for (i = 0; i < bblock->in_scount; ++i) {
2999 if (cfg->verbose_level > 3)
3000 g_print ("loading %d from temp %d\n", i, bblock->in_stack [i]->inst_c0);
3001 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
3007 bblock->real_offset = real_offset;
3009 if ((cfg->method == method) && cfg->coverage_info) {
3010 MonoInst *store, *one;
3011 guint32 cil_offset = ip - header->code;
3012 cfg->coverage_info->data [cil_offset].cil_code = ip;
3014 /* TODO: Use an increment here */
3015 NEW_ICONST (cfg, one, 1);
3018 NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
3021 MONO_INST_NEW (cfg, store, CEE_STIND_I);
3022 store->cil_code = ip;
3023 store->inst_left = ins;
3024 store->inst_right = one;
3026 MONO_ADD_INS (bblock, store);
3029 if (cfg->verbose_level > 3)
3030 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, sp-stack_start, mono_disasm_code_one (NULL, method, ip, NULL));
3037 MONO_INST_NEW (cfg, ins, CEE_BREAK);
3038 ins->cil_code = ip++;
3039 MONO_ADD_INS (bblock, ins);
3045 CHECK_STACK_OVF (1);
3046 n = (*ip)-CEE_LDARG_0;
3048 NEW_ARGLOAD (cfg, ins, n);
3049 ins->cil_code = ip++;
3056 CHECK_STACK_OVF (1);
3057 n = (*ip)-CEE_LDLOC_0;
3059 NEW_LOCLOAD (cfg, ins, n);
3060 ins->cil_code = ip++;
3068 n = (*ip)-CEE_STLOC_0;
3071 handle_loaded_temps (cfg, bblock, stack_start, sp);
3072 NEW_LOCSTORE (cfg, ins, n, *sp);
3074 if (ins->opcode == CEE_STOBJ) {
3075 NEW_LOCLOADA (cfg, ins, n);
3076 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3078 MONO_ADD_INS (bblock, ins);
3084 CHECK_STACK_OVF (1);
3086 NEW_ARGLOAD (cfg, ins, ip [1]);
3093 CHECK_STACK_OVF (1);
3095 NEW_ARGLOADA (cfg, ins, ip [1]);
3105 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
3106 handle_loaded_temps (cfg, bblock, stack_start, sp);
3108 if (ins->opcode == CEE_STOBJ) {
3109 NEW_ARGLOADA (cfg, ins, ip [1]);
3110 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3112 MONO_ADD_INS (bblock, ins);
3117 CHECK_STACK_OVF (1);
3118 CHECK_LOCAL (ip [1]);
3119 NEW_LOCLOAD (cfg, ins, ip [1]);
3126 CHECK_STACK_OVF (1);
3127 CHECK_LOCAL (ip [1]);
3128 NEW_LOCLOADA (cfg, ins, ip [1]);
3137 handle_loaded_temps (cfg, bblock, stack_start, sp);
3138 CHECK_LOCAL (ip [1]);
3139 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
3141 if (ins->opcode == CEE_STOBJ) {
3142 NEW_LOCLOADA (cfg, ins, ip [1]);
3143 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3145 MONO_ADD_INS (bblock, ins);
3150 CHECK_STACK_OVF (1);
3151 NEW_PCONST (cfg, ins, NULL);
3153 ins->type = STACK_OBJ;
3158 CHECK_STACK_OVF (1);
3159 NEW_ICONST (cfg, ins, -1);
3173 CHECK_STACK_OVF (1);
3174 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
3181 CHECK_STACK_OVF (1);
3183 NEW_ICONST (cfg, ins, *((signed char*)ip));
3190 CHECK_STACK_OVF (1);
3191 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
3198 CHECK_STACK_OVF (1);
3199 MONO_INST_NEW (cfg, ins, OP_I8CONST);
3201 ins->type = STACK_I8;
3203 ins->inst_l = (gint64)read64 (ip);
3208 float *f = mono_mempool_alloc (cfg->domain->mp, sizeof (float));
3210 CHECK_STACK_OVF (1);
3211 MONO_INST_NEW (cfg, ins, OP_R4CONST);
3212 ins->type = STACK_R8;
3221 double *d = mono_mempool_alloc (cfg->domain->mp, sizeof (double));
3223 CHECK_STACK_OVF (1);
3224 MONO_INST_NEW (cfg, ins, OP_R8CONST);
3225 ins->type = STACK_R8;
3234 MonoInst *temp, *store;
3236 CHECK_STACK_OVF (1);
3241 * small optimization: if the loaded value was from a local already,
3242 * just load it twice.
3244 if (ins->ssa_op == MONO_SSA_LOAD &&
3245 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
3247 MONO_INST_NEW (cfg, temp, 0);
3249 temp->cil_code = ip;
3252 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3253 temp->cil_code = ip;
3254 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3255 store->cil_code = ip;
3256 MONO_ADD_INS (bblock, store);
3257 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3260 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3270 MONO_INST_NEW (cfg, ins, CEE_POP);
3271 MONO_ADD_INS (bblock, ins);
3272 ins->cil_code = ip++;
3278 if (stack_start != sp)
3280 MONO_INST_NEW (cfg, ins, CEE_JMP);
3281 token = read32 (ip + 1);
3282 /* FIXME: check the signature matches */
3283 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3284 ins->inst_p0 = cmethod;
3285 MONO_ADD_INS (bblock, ins);
3287 start_new_bblock = 1;
3291 case CEE_CALLVIRT: {
3292 MonoInst *addr = NULL;
3293 MonoMethodSignature *fsig = NULL;
3294 int temp, array_rank = 0;
3295 int virtual = *ip == CEE_CALLVIRT;
3298 token = read32 (ip + 1);
3300 if (*ip == CEE_CALLI) {
3305 if (method->wrapper_type != MONO_WRAPPER_NONE)
3306 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
3308 fsig = mono_metadata_parse_signature (image, token);
3310 n = fsig->param_count + fsig->hasthis;
3312 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3313 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
3314 } else if (constrained_call) {
3315 cmethod = mono_get_method_constrained (image, token, constrained_call, generic_context);
3317 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3322 if (!cmethod->klass->inited)
3323 mono_class_init (cmethod->klass);
3325 if (cmethod->signature->pinvoke) {
3326 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
3327 fsig = wrapper->signature;
3329 fsig = mono_method_get_signature (cmethod, image, token);
3332 n = fsig->param_count + fsig->hasthis;
3334 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
3335 cmethod->klass->parent == mono_defaults.array_class) {
3336 array_rank = cmethod->klass->rank;
3339 if (cmethod->string_ctor)
3340 g_assert_not_reached ();
3346 //g_assert (!virtual || fsig->hasthis);
3350 if (constrained_call) {
3352 * We have the `constrained.' prefix opcode.
3354 if (constrained_call->valuetype && !cmethod->klass->valuetype) {
3357 * The type parameter is instantiated as a valuetype,
3358 * but that type doesn't override the method we're
3359 * calling, so we need to box `this'.
3360 * sp [0] is a pointer to the data: we need the value
3361 * in handle_box (), so load it here.
3363 MONO_INST_NEW (cfg, load, mono_type_to_ldind (&constrained_call->byval_arg));
3364 type_to_eval_stack_type (&constrained_call->byval_arg, load);
3365 load->cil_code = ip;
3366 load->inst_left = sp [0];
3367 sp [0] = handle_box (cfg, bblock, load, ip, constrained_call);
3368 } else if (!constrained_call->valuetype) {
3372 * The type parameter is instantiated as a reference
3373 * type. We have a managed pointer on the stack, so
3374 * we need to dereference it here.
3377 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3379 ins->inst_i0 = sp [0];
3380 ins->type = STACK_OBJ;
3382 } else if (cmethod->klass->valuetype)
3384 constrained_call = NULL;
3387 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
3390 if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL) && (mono_metadata_signature_equal (method->signature, cmethod->signature))) {
3392 /* FIXME: This assumes the two methods has the same number and type of arguments */
3393 for (i = 0; i < n; ++i) {
3394 /* Check if argument is the same */
3395 NEW_ARGLOAD (cfg, ins, i);
3396 if ((ins->opcode == sp [i]->opcode) && (ins->inst_i0 == sp [i]->inst_i0))
3399 /* Prevent argument from being register allocated */
3400 arg_array [i]->flags |= MONO_INST_VOLATILE;
3401 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3403 if (ins->opcode == CEE_STOBJ) {
3404 NEW_ARGLOADA (cfg, ins, i);
3405 handle_stobj (cfg, bblock, ins, sp [i], sp [i]->cil_code, ins->klass, FALSE, FALSE);
3408 MONO_ADD_INS (bblock, ins);
3410 MONO_INST_NEW (cfg, ins, CEE_JMP);
3412 ins->inst_p0 = cmethod;
3413 ins->inst_p1 = arg_array [0];
3414 MONO_ADD_INS (bblock, ins);
3415 start_new_bblock = 1;
3416 /* skip CEE_RET as well */
3421 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_opcode_for_method (cfg, cmethod, fsig, sp))) {
3424 if (MONO_TYPE_IS_VOID (fsig->ret)) {
3425 MONO_ADD_INS (bblock, ins);
3427 type_to_eval_stack_type (fsig->ret, ins);
3436 handle_loaded_temps (cfg, bblock, stack_start, sp);
3438 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3439 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
3440 mono_method_check_inlining (cfg, cmethod) &&
3441 !g_list_find (dont_inline, cmethod)) {
3443 MonoBasicBlock *ebblock;
3444 gboolean allways = FALSE;
3446 if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
3447 (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
3448 cmethod = mono_marshal_get_native_wrapper (cmethod);
3452 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, allways))) {
3456 GET_BBLOCK (cfg, bbhash, bblock, ip);
3457 ebblock->next_bb = bblock;
3458 link_bblock (cfg, ebblock, bblock);
3460 if (!MONO_TYPE_IS_VOID (fsig->ret))
3463 /* indicates start of a new block, and triggers a load of all
3464 stack arguments at bb boundarie */
3467 inline_costs += costs;
3472 inline_costs += 10 * num_calls++;
3474 /* tail recursion elimination */
3475 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == method && ip [5] == CEE_RET) {
3476 gboolean has_vtargs = FALSE;
3479 /* keep it simple */
3480 for (i = fsig->param_count - 1; i >= 0; i--) {
3481 if (MONO_TYPE_ISSTRUCT (cmethod->signature->params [i]))
3486 for (i = 0; i < n; ++i) {
3487 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3489 MONO_ADD_INS (bblock, ins);
3491 MONO_INST_NEW (cfg, ins, CEE_BR);
3493 MONO_ADD_INS (bblock, ins);
3494 tblock = start_bblock->out_bb [0];
3495 link_bblock (cfg, bblock, tblock);
3496 ins->inst_target_bb = tblock;
3497 start_new_bblock = 1;
3500 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3501 /* just create a dummy - the value is never used */
3502 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
3503 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3511 if (*ip == CEE_CALLI) {
3513 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
3514 NEW_TEMPLOAD (cfg, *sp, temp);
3518 } else if (array_rank) {
3521 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
3522 if (sp [fsig->param_count]->type == STACK_OBJ) {
3523 MonoInst *iargs [2];
3524 MonoInst *array, *to_store, *store;
3526 handle_loaded_temps (cfg, bblock, stack_start, sp);
3528 array = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
3529 NEW_TEMPSTORE (cfg, store, array->inst_c0, sp [0]);
3530 store->cil_code = ip;
3531 MONO_ADD_INS (bblock, store);
3532 NEW_TEMPLOAD (cfg, iargs [0], array->inst_c0);
3534 to_store = mono_compile_create_var (cfg, type_from_stack_type (sp [fsig->param_count]), OP_LOCAL);
3535 NEW_TEMPSTORE (cfg, store, to_store->inst_c0, sp [fsig->param_count]);
3536 store->cil_code = ip;
3537 MONO_ADD_INS (bblock, store);
3538 NEW_TEMPLOAD (cfg, iargs [1], to_store->inst_c0);
3541 * We first save the args for the call so that the args are copied to the stack
3542 * and a new instruction tree for them is created. If we don't do this,
3543 * the same MonoInst is added to two different trees and this is not
3546 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref_check, iargs, ip);
3548 NEW_TEMPLOAD (cfg, sp [0], array->inst_c0);
3549 NEW_TEMPLOAD (cfg, sp [fsig->param_count], to_store->inst_c0);
3552 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
3553 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
3555 if (ins->opcode == CEE_STOBJ) {
3556 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
3558 MONO_ADD_INS (bblock, ins);
3561 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
3562 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3563 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
3567 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
3568 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3571 g_assert_not_reached ();
3575 if (0 && CODE_IS_STLOC (ip + 5) && (!MONO_TYPE_ISSTRUCT (fsig->ret)) && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)) {
3576 /* no need to spill */
3577 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
3580 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) {
3581 NEW_TEMPLOAD (cfg, *sp, temp);
3591 if (cfg->method != method) {
3592 /* return from inlined methode */
3597 //g_assert (returnvar != -1);
3598 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
3599 store->cil_code = sp [0]->cil_code;
3600 if (store->opcode == CEE_STOBJ) {
3601 g_assert_not_reached ();
3602 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
3603 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
3605 MONO_ADD_INS (bblock, store);
3609 g_assert (!return_var);
3612 MONO_INST_NEW (cfg, ins, CEE_NOP);
3613 ins->opcode = mono_type_to_stind (method->signature->ret);
3614 if (ins->opcode == CEE_STOBJ) {
3615 NEW_RETLOADA (cfg, ins);
3616 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3618 ins->opcode = OP_SETRET;
3620 ins->inst_i0 = *sp;;
3621 ins->inst_i1 = NULL;
3622 MONO_ADD_INS (bblock, ins);
3626 if (sp != stack_start)
3628 MONO_INST_NEW (cfg, ins, CEE_BR);
3629 ins->cil_code = ip++;
3630 ins->inst_target_bb = end_bblock;
3631 MONO_ADD_INS (bblock, ins);
3632 link_bblock (cfg, bblock, end_bblock);
3633 start_new_bblock = 1;
3637 MONO_INST_NEW (cfg, ins, CEE_BR);
3638 ins->cil_code = ip++;
3639 MONO_ADD_INS (bblock, ins);
3640 target = ip + 1 + (signed char)(*ip);
3642 GET_BBLOCK (cfg, bbhash, tblock, target);
3643 link_bblock (cfg, bblock, tblock);
3644 CHECK_BBLOCK (target, ip, tblock);
3645 ins->inst_target_bb = tblock;
3646 if (sp != stack_start) {
3647 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3650 start_new_bblock = 1;
3657 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3658 ins->cil_code = ip++;
3659 target = ip + 1 + *(signed char*)ip;
3661 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
3662 if (sp != stack_start) {
3663 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3680 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3681 ins->cil_code = ip++;
3682 target = ip + 1 + *(signed char*)ip;
3685 if (sp != stack_start) {
3686 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3693 MONO_INST_NEW (cfg, ins, CEE_BR);
3694 ins->cil_code = ip++;
3695 MONO_ADD_INS (bblock, ins);
3696 target = ip + 4 + (gint32)read32(ip);
3698 GET_BBLOCK (cfg, bbhash, tblock, target);
3699 link_bblock (cfg, bblock, tblock);
3700 CHECK_BBLOCK (target, ip, tblock);
3701 ins->inst_target_bb = tblock;
3702 if (sp != stack_start) {
3703 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3706 start_new_bblock = 1;
3713 MONO_INST_NEW (cfg, ins, *ip);
3714 ins->cil_code = ip++;
3715 target = ip + 4 + (gint32)read32(ip);
3717 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
3718 if (sp != stack_start) {
3719 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3736 MONO_INST_NEW (cfg, ins, *ip);
3737 ins->cil_code = ip++;
3738 target = ip + 4 + (gint32)read32(ip);
3741 if (sp != stack_start) {
3742 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3750 n = read32 (ip + 1);
3751 MONO_INST_NEW (cfg, ins, *ip);
3753 ins->inst_left = *sp;
3754 if (ins->inst_left->type != STACK_I4) goto unverified;
3757 CHECK_OPSIZE (n * sizeof (guint32));
3758 target = ip + n * sizeof (guint32);
3759 MONO_ADD_INS (bblock, ins);
3760 GET_BBLOCK (cfg, bbhash, tblock, target);
3761 link_bblock (cfg, bblock, tblock);
3762 ins->klass = GUINT_TO_POINTER (n);
3763 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
3764 ins->inst_many_bb [n] = tblock;
3766 for (i = 0; i < n; ++i) {
3767 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
3768 link_bblock (cfg, bblock, tblock);
3769 ins->inst_many_bb [i] = tblock;
3772 if (sp != stack_start) {
3773 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3790 MONO_INST_NEW (cfg, ins, *ip);
3795 ins->type = ldind_type [*ip - CEE_LDIND_I1];
3796 ins->flags |= ins_flag;
3808 MONO_INST_NEW (cfg, ins, *ip);
3809 ins->cil_code = ip++;
3811 handle_loaded_temps (cfg, bblock, stack_start, sp);
3812 MONO_ADD_INS (bblock, ins);
3813 ins->inst_i0 = sp [0];
3814 ins->inst_i1 = sp [1];
3815 ins->flags |= ins_flag;
3834 /* special case that gives a nice speedup and happens to workaorund a ppc jit but (for the release)
3835 * later apply the speedup to the left shift as well
3838 if ((ins->opcode == OP_LSHR_UN) && (ins->type == STACK_I8)
3839 && (ins->inst_right->opcode == OP_ICONST) && (ins->inst_right->inst_c0 == 32)) {
3840 ins->opcode = OP_LONG_SHRUN_32;
3841 /*g_print ("applied long shr speedup to %s\n", cfg->method->name);*/
3845 if (mono_find_jit_opcode_emulation (ins->opcode)) {
3847 *sp++ = emit_tree (cfg, bblock, ins);
3861 case CEE_CONV_OVF_I8:
3862 case CEE_CONV_OVF_U8:
3866 if (mono_find_jit_opcode_emulation (ins->opcode)) {
3868 *sp++ = emit_tree (cfg, bblock, ins);
3872 case CEE_CONV_OVF_I4:
3873 case CEE_CONV_OVF_I1:
3874 case CEE_CONV_OVF_I2:
3875 case CEE_CONV_OVF_I:
3876 case CEE_CONV_OVF_U:
3879 if (sp [-1]->type == STACK_R8) {
3880 ADD_UNOP (CEE_CONV_OVF_I8);
3888 case CEE_CONV_OVF_U1:
3889 case CEE_CONV_OVF_U2:
3890 case CEE_CONV_OVF_U4:
3893 if (sp [-1]->type == STACK_R8) {
3894 ADD_UNOP (CEE_CONV_OVF_U8);
3902 case CEE_CONV_OVF_I1_UN:
3903 case CEE_CONV_OVF_I2_UN:
3904 case CEE_CONV_OVF_I4_UN:
3905 case CEE_CONV_OVF_I8_UN:
3906 case CEE_CONV_OVF_U1_UN:
3907 case CEE_CONV_OVF_U2_UN:
3908 case CEE_CONV_OVF_U4_UN:
3909 case CEE_CONV_OVF_U8_UN:
3910 case CEE_CONV_OVF_I_UN:
3911 case CEE_CONV_OVF_U_UN:
3919 token = read32 (ip + 1);
3920 if (method->wrapper_type != MONO_WRAPPER_NONE)
3921 klass = mono_method_get_wrapper_data (method, token);
3923 klass = mono_class_get_full (image, token, generic_context);
3925 mono_class_init (klass);
3927 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3928 MonoInst *store, *load;
3929 MONO_INST_NEW (cfg, load, CEE_LDIND_REF);
3930 load->cil_code = ip;
3931 load->inst_i0 = sp [1];
3932 load->type = ldind_type [CEE_LDIND_REF];
3933 load->flags |= ins_flag;
3934 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
3935 store->cil_code = ip;
3936 handle_loaded_temps (cfg, bblock, stack_start, sp);
3937 MONO_ADD_INS (bblock, store);
3938 store->inst_i0 = sp [0];
3939 store->inst_i1 = load;
3940 store->flags |= ins_flag;
3942 n = mono_class_value_size (klass, NULL);
3943 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3945 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3946 copy->inst_left = sp [0];
3947 copy->inst_right = sp [1];
3948 copy->cil_code = ip;
3950 MONO_ADD_INS (bblock, copy);
3952 MonoInst *iargs [3];
3955 NEW_ICONST (cfg, iargs [2], n);
3956 iargs [2]->cil_code = ip;
3958 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3965 MonoInst *iargs [3];
3969 token = read32 (ip + 1);
3970 if (method->wrapper_type != MONO_WRAPPER_NONE)
3971 klass = mono_method_get_wrapper_data (method, token);
3973 klass = mono_class_get_full (image, token, generic_context);
3975 mono_class_init (klass);
3976 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3977 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3979 ins->inst_i0 = sp [0];
3980 ins->type = ldind_type [CEE_LDIND_REF];
3981 ins->flags |= ins_flag;
3987 n = mono_class_value_size (klass, NULL);
3988 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3989 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3990 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3992 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3993 copy->inst_left = iargs [0];
3994 copy->inst_right = *sp;
3995 copy->cil_code = ip;
3997 MONO_ADD_INS (bblock, copy);
4000 NEW_ICONST (cfg, iargs [2], n);
4001 iargs [2]->cil_code = ip;
4003 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
4005 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
4013 CHECK_STACK_OVF (1);
4015 n = read32 (ip + 1);
4017 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
4018 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, n));
4020 ins->type = STACK_OBJ;
4023 else if (method->wrapper_type != MONO_WRAPPER_NONE) {
4025 MonoInst *iargs [1];
4027 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
4028 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
4029 NEW_TEMPLOAD (cfg, *sp, temp);
4033 if (cfg->opt & MONO_OPT_SHARED) {
4035 MonoInst *iargs [3];
4037 if (mono_compile_aot) {
4038 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n);
4041 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
4042 NEW_IMAGECONST (cfg, iargs [1], image);
4043 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
4044 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
4045 NEW_TEMPLOAD (cfg, *sp, temp);
4046 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
4048 if (mono_compile_aot)
4049 NEW_LDSTRCONST (cfg, ins, image, n);
4051 NEW_PCONST (cfg, ins, NULL);
4053 ins->type = STACK_OBJ;
4054 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
4064 MonoInst *iargs [2];
4065 MonoMethodSignature *fsig;
4069 token = read32 (ip + 1);
4070 if (method->wrapper_type != MONO_WRAPPER_NONE) {
4071 cmethod = mono_method_get_wrapper_data (method, token);
4073 cmethod = mono_get_method_full (image, token, NULL, generic_context);
4074 fsig = mono_method_get_signature (cmethod, image, token);
4076 mono_class_init (cmethod->klass);
4078 n = fsig->param_count;
4081 /* move the args to allow room for 'this' in the first position */
4087 handle_loaded_temps (cfg, bblock, stack_start, sp);
4090 if (cmethod->klass->parent == mono_defaults.array_class) {
4091 NEW_METHODCONST (cfg, *sp, cmethod);
4092 temp = handle_array_new (cfg, bblock, fsig->param_count, sp, ip);
4093 } else if (cmethod->string_ctor) {
4094 /* we simply pass a null pointer */
4095 NEW_PCONST (cfg, *sp, NULL);
4096 /* now call the string ctor */
4097 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
4099 if (cmethod->klass->valuetype) {
4100 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
4101 temp = iargs [0]->inst_c0;
4102 NEW_TEMPLOADA (cfg, *sp, temp);
4104 temp = handle_alloc (cfg, bblock, cmethod->klass, ip);
4105 NEW_TEMPLOAD (cfg, *sp, temp);
4108 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
4109 mono_method_check_inlining (cfg, cmethod) &&
4110 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
4111 !g_list_find (dont_inline, cmethod)) {
4113 MonoBasicBlock *ebblock;
4114 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, FALSE))) {
4119 GET_BBLOCK (cfg, bbhash, bblock, ip);
4120 ebblock->next_bb = bblock;
4121 link_bblock (cfg, ebblock, bblock);
4123 NEW_TEMPLOAD (cfg, *sp, temp);
4126 /* indicates start of a new block, and triggers a load
4127 of all stack arguments at bb boundarie */
4130 inline_costs += costs;
4134 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
4137 /* now call the actual ctor */
4138 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
4142 NEW_TEMPLOAD (cfg, *sp, temp);
4153 token = read32 (ip + 1);
4154 if (method->wrapper_type != MONO_WRAPPER_NONE)
4155 klass = mono_method_get_wrapper_data (method, token);
4157 klass = mono_class_get_full (image, token, generic_context);
4158 mono_class_init (klass);
4160 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4162 MonoMethod *mono_isinst;
4163 MonoInst *iargs [1];
4164 MonoBasicBlock *ebblock;
4168 mono_isinst = mono_marshal_get_isinst (klass);
4171 costs = inline_method (cfg, mono_isinst, mono_isinst->signature, bblock,
4172 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4174 g_assert (costs > 0);
4179 GET_BBLOCK (cfg, bbhash, bblock, ip);
4180 ebblock->next_bb = bblock;
4181 link_bblock (cfg, ebblock, bblock);
4183 temp = iargs [0]->inst_i0->inst_c0;
4184 NEW_TEMPLOAD (cfg, *sp, temp);
4188 inline_costs += costs;
4192 MONO_INST_NEW (cfg, ins, *ip);
4193 ins->type = STACK_OBJ;
4194 ins->inst_left = *sp;
4195 ins->inst_newa_class = klass;
4197 *sp++ = emit_tree (cfg, bblock, ins);
4201 case CEE_UNBOX_ANY: {
4202 MonoInst *add, *vtoffset;
4203 MonoInst *iargs [3];
4208 token = read32 (ip + 1);
4209 if (method->wrapper_type != MONO_WRAPPER_NONE)
4210 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4212 klass = mono_class_get_full (image, token, generic_context);
4213 mono_class_init (klass);
4215 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4217 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4218 MonoMethod *mono_castclass;
4219 MonoInst *iargs [1];
4220 MonoBasicBlock *ebblock;
4224 mono_castclass = mono_marshal_get_castclass (klass);
4227 costs = inline_method (cfg, mono_castclass, mono_castclass->signature, bblock,
4228 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4230 g_assert (costs > 0);
4235 GET_BBLOCK (cfg, bbhash, bblock, ip);
4236 ebblock->next_bb = bblock;
4237 link_bblock (cfg, ebblock, bblock);
4239 temp = iargs [0]->inst_i0->inst_c0;
4240 NEW_TEMPLOAD (cfg, *sp, temp);
4244 inline_costs += costs;
4247 MONO_INST_NEW (cfg, ins, CEE_CASTCLASS);
4248 ins->type = STACK_OBJ;
4249 ins->inst_left = *sp;
4251 ins->inst_newa_class = klass;
4259 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
4260 ins->type = STACK_OBJ;
4261 ins->inst_left = *sp;
4263 ins->inst_newa_class = klass;
4266 MONO_INST_NEW (cfg, add, OP_PADD);
4267 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4268 add->inst_left = ins;
4269 add->inst_right = vtoffset;
4270 add->type = STACK_MP;
4274 n = mono_class_value_size (klass, NULL);
4275 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
4276 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
4277 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
4279 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
4280 copy->inst_left = iargs [0];
4281 copy->inst_right = *sp;
4282 copy->cil_code = ip;
4284 MONO_ADD_INS (bblock, copy);
4287 NEW_ICONST (cfg, iargs [2], n);
4288 iargs [2]->cil_code = ip;
4290 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
4292 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
4298 MonoInst *add, *vtoffset;
4303 token = read32 (ip + 1);
4304 if (method->wrapper_type != MONO_WRAPPER_NONE)
4305 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4307 klass = mono_class_get_full (image, token, generic_context);
4308 mono_class_init (klass);
4310 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
4311 ins->type = STACK_OBJ;
4312 ins->inst_left = *sp;
4314 ins->inst_newa_class = klass;
4317 MONO_INST_NEW (cfg, add, OP_PADD);
4318 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4319 add->inst_left = ins;
4320 add->inst_right = vtoffset;
4321 add->type = STACK_MP;
4331 token = read32 (ip + 1);
4332 if (method->wrapper_type != MONO_WRAPPER_NONE)
4333 klass = mono_method_get_wrapper_data (method, token);
4335 klass = mono_class_get_full (image, token, generic_context);
4336 mono_class_init (klass);
4338 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4340 MonoMethod *mono_castclass;
4341 MonoInst *iargs [1];
4342 MonoBasicBlock *ebblock;
4346 mono_castclass = mono_marshal_get_castclass (klass);
4349 costs = inline_method (cfg, mono_castclass, mono_castclass->signature, bblock,
4350 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4352 g_assert (costs > 0);
4357 GET_BBLOCK (cfg, bbhash, bblock, ip);
4358 ebblock->next_bb = bblock;
4359 link_bblock (cfg, ebblock, bblock);
4361 temp = iargs [0]->inst_i0->inst_c0;
4362 NEW_TEMPLOAD (cfg, *sp, temp);
4366 inline_costs += costs;
4369 MONO_INST_NEW (cfg, ins, *ip);
4370 ins->type = STACK_OBJ;
4371 ins->inst_left = *sp;
4373 ins->inst_newa_class = klass;
4375 *sp++ = emit_tree (cfg, bblock, ins);
4381 MONO_INST_NEW (cfg, ins, *ip);
4383 ins->inst_left = *sp;
4384 ins->cil_code = ip++;
4385 MONO_ADD_INS (bblock, ins);
4387 start_new_bblock = 1;
4392 MonoInst *offset_ins;
4393 MonoClassField *field;
4394 MonoBasicBlock *ebblock;
4398 if (*ip == CEE_STFLD) {
4405 // FIXME: enable this test later.
4406 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
4409 token = read32 (ip + 1);
4410 field = mono_field_from_token (image, token, &klass, generic_context);
4411 mono_class_init (klass);
4413 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
4414 /* FIXME: mark instructions for use in SSA */
4415 if (*ip == CEE_STFLD) {
4416 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound || klass == mono_defaults.marshalbyrefobject_class) {
4417 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
4418 MonoInst *iargs [5];
4421 NEW_CLASSCONST (cfg, iargs [1], klass);
4422 NEW_FIELDCONST (cfg, iargs [2], field);
4423 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
4427 if (cfg->opt & MONO_OPT_INLINE) {
4428 costs = inline_method (cfg, stfld_wrapper, stfld_wrapper->signature, bblock,
4429 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4430 g_assert (costs > 0);
4435 GET_BBLOCK (cfg, bbhash, bblock, ip);
4436 ebblock->next_bb = bblock;
4437 link_bblock (cfg, ebblock, bblock);
4439 /* indicates start of a new block, and triggers a load
4440 of all stack arguments at bb boundarie */
4443 inline_costs += costs;
4446 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, stfld_wrapper->signature, iargs, ip, NULL);
4450 NEW_ICONST (cfg, offset_ins, foffset);
4451 MONO_INST_NEW (cfg, ins, OP_PADD);
4453 ins->inst_left = *sp;
4454 ins->inst_right = offset_ins;
4455 ins->type = STACK_MP;
4457 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4458 store->cil_code = ip;
4459 store->inst_left = ins;
4460 store->inst_right = sp [1];
4461 handle_loaded_temps (cfg, bblock, stack_start, sp);
4462 store->flags |= ins_flag;
4464 if (store->opcode == CEE_STOBJ) {
4465 handle_stobj (cfg, bblock, ins, sp [1], ip,
4466 mono_class_from_mono_type (field->type), FALSE, FALSE);
4468 MONO_ADD_INS (bblock, store);
4471 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound || klass == mono_defaults.marshalbyrefobject_class) {
4472 /* fixme: we need to inline that call somehow */
4473 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
4474 MonoInst *iargs [4];
4478 NEW_CLASSCONST (cfg, iargs [1], klass);
4479 NEW_FIELDCONST (cfg, iargs [2], field);
4480 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
4481 if (cfg->opt & MONO_OPT_INLINE) {
4482 costs = inline_method (cfg, ldfld_wrapper, ldfld_wrapper->signature, bblock,
4483 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4484 g_assert (costs > 0);
4489 GET_BBLOCK (cfg, bbhash, bblock, ip);
4490 ebblock->next_bb = bblock;
4491 link_bblock (cfg, ebblock, bblock);
4493 temp = iargs [0]->inst_i0->inst_c0;
4495 if (*ip == CEE_LDFLDA) {
4496 /* not sure howto handle this */
4497 NEW_TEMPLOADA (cfg, *sp, temp);
4499 NEW_TEMPLOAD (cfg, *sp, temp);
4503 /* indicates start of a new block, and triggers a load of
4504 all stack arguments at bb boundarie */
4507 inline_costs += costs;
4510 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, ldfld_wrapper->signature, iargs, ip, NULL);
4511 if (*ip == CEE_LDFLDA) {
4512 /* not sure howto handle this */
4513 NEW_TEMPLOADA (cfg, *sp, temp);
4515 NEW_TEMPLOAD (cfg, *sp, temp);
4520 NEW_ICONST (cfg, offset_ins, foffset);
4521 MONO_INST_NEW (cfg, ins, OP_PADD);
4523 ins->inst_left = *sp;
4524 ins->inst_right = offset_ins;
4525 ins->type = STACK_MP;
4527 if (*ip == CEE_LDFLDA) {
4531 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4532 type_to_eval_stack_type (field->type, load);
4533 load->cil_code = ip;
4534 load->inst_left = ins;
4535 load->flags |= ins_flag;
4547 MonoClassField *field;
4548 gpointer addr = NULL;
4551 token = read32 (ip + 1);
4553 field = mono_field_from_token (image, token, &klass, generic_context);
4554 mono_class_init (klass);
4556 if ((*ip) == CEE_STSFLD)
4557 handle_loaded_temps (cfg, bblock, stack_start, sp);
4559 if (cfg->domain->special_static_fields)
4560 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
4562 if ((cfg->opt & MONO_OPT_SHARED) || (mono_compile_aot && addr)) {
4564 MonoInst *iargs [2];
4565 g_assert (field->parent);
4566 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
4567 NEW_FIELDCONST (cfg, iargs [1], field);
4568 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
4569 NEW_TEMPLOAD (cfg, ins, temp);
4572 vtable = mono_class_vtable (cfg->domain, klass);
4574 if ((!vtable->initialized || mono_compile_aot) && !(klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (klass, method)) {
4575 guint8 *tramp = mono_create_class_init_trampoline (vtable);
4576 mono_emit_native_call (cfg, bblock, tramp,
4577 helper_sig_class_init_trampoline,
4578 NULL, ip, FALSE, FALSE);
4579 if (cfg->verbose_level > 2)
4580 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
4582 if (cfg->run_cctors)
4583 mono_runtime_class_init (vtable);
4585 addr = (char*)vtable->data + field->offset;
4587 if (mono_compile_aot)
4588 NEW_SFLDACONST (cfg, ins, field);
4590 NEW_PCONST (cfg, ins, addr);
4594 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
4595 * This could be later optimized to do just a couple of
4596 * memory dereferences with constant offsets.
4599 MonoInst *iargs [1];
4600 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
4601 temp = mono_emit_jit_icall (cfg, bblock, mono_get_special_static_data, iargs, ip);
4602 NEW_TEMPLOAD (cfg, ins, temp);
4606 /* FIXME: mark instructions for use in SSA */
4607 if (*ip == CEE_LDSFLDA) {
4609 } else if (*ip == CEE_STSFLD) {
4613 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4614 store->cil_code = ip;
4615 store->inst_left = ins;
4616 store->inst_right = sp [0];
4617 store->flags |= ins_flag;
4620 if (store->opcode == CEE_STOBJ) {
4621 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
4623 MONO_ADD_INS (bblock, store);
4625 gboolean is_const = FALSE;
4626 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4627 if (!((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) &&
4628 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
4629 gpointer addr = (char*)vtable->data + field->offset;
4630 /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
4632 switch (field->type->type) {
4633 case MONO_TYPE_BOOLEAN:
4635 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
4639 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
4642 case MONO_TYPE_CHAR:
4644 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
4648 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
4653 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
4657 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
4662 case MONO_TYPE_STRING:
4663 case MONO_TYPE_OBJECT:
4664 case MONO_TYPE_CLASS:
4665 case MONO_TYPE_SZARRAY:
4667 case MONO_TYPE_FNPTR:
4668 case MONO_TYPE_ARRAY:
4669 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
4670 type_to_eval_stack_type (field->type, *sp);
4675 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
4676 sp [0]->type = STACK_I8;
4677 sp [0]->inst_l = *((gint64 *)addr);
4682 case MONO_TYPE_VALUETYPE:
4691 CHECK_STACK_OVF (1);
4692 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4693 type_to_eval_stack_type (field->type, load);
4694 load->cil_code = ip;
4695 load->inst_left = ins;
4697 load->flags |= ins_flag;
4699 /* fixme: dont see the problem why this does not work */
4700 //cfg->disable_aot = TRUE;
4710 token = read32 (ip + 1);
4711 if (method->wrapper_type != MONO_WRAPPER_NONE)
4712 klass = mono_method_get_wrapper_data (method, token);
4714 klass = mono_class_get_full (image, token, generic_context);
4715 mono_class_init (klass);
4716 n = mono_type_to_stind (&klass->byval_arg);
4717 if (n == CEE_STOBJ) {
4718 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
4720 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
4722 MONO_INST_NEW (cfg, store, n);
4723 store->cil_code = ip;
4724 store->inst_left = sp [0];
4725 store->inst_right = sp [1];
4726 store->flags |= ins_flag;
4727 MONO_ADD_INS (bblock, store);
4739 token = read32 (ip + 1);
4740 if (method->wrapper_type != MONO_WRAPPER_NONE)
4741 klass = mono_method_get_wrapper_data (method, token);
4743 klass = mono_class_get_full (image, token, generic_context);
4744 mono_class_init (klass);
4746 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4751 *sp++ = handle_box (cfg, bblock, val, ip, klass);
4758 MONO_INST_NEW (cfg, ins, *ip);
4763 token = read32 (ip + 1);
4765 /* allocate the domainvar - becaus this is used in decompose_foreach */
4766 if (cfg->opt & MONO_OPT_SHARED) {
4767 mono_get_domainvar (cfg);
4768 /* LAME-IR: Mark it as used since otherwise it will be optimized away */
4769 cfg->domainvar->flags |= MONO_INST_VOLATILE;
4772 if (method->wrapper_type != MONO_WRAPPER_NONE)
4773 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4775 klass = mono_class_get_full (image, token, generic_context);
4777 mono_class_init (klass);
4778 ins->inst_newa_class = klass;
4779 ins->inst_newa_len = *sp;
4780 ins->type = STACK_OBJ;
4784 * we store the object so calls to create the array are not interleaved
4785 * with the arguments of other calls.
4788 MonoInst *store, *temp, *load;
4790 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
4791 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4792 store->cil_code = ins->cil_code;
4793 MONO_ADD_INS (bblock, store);
4794 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
4795 load->cil_code = ins->cil_code;
4802 MONO_INST_NEW (cfg, ins, *ip);
4803 ins->cil_code = ip++;
4805 ins->inst_left = *sp;
4806 ins->type = STACK_PTR;
4814 if (method->wrapper_type != MONO_WRAPPER_NONE)
4815 klass = (MonoClass*)mono_method_get_wrapper_data (method, read32 (ip + 1));
4817 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4818 mono_class_init (klass);
4819 NEW_LDELEMA (cfg, ins, sp, klass);
4824 case CEE_LDELEM_ANY: {
4829 token = read32 (ip + 1);
4830 klass = mono_class_get_full (image, token, generic_context);
4831 mono_class_init (klass);
4832 NEW_LDELEMA (cfg, load, sp, klass);
4833 load->cil_code = ip;
4834 MONO_INST_NEW (cfg, ins, mono_type_to_ldind (&klass->byval_arg));
4836 ins->inst_left = load;
4838 type_to_eval_stack_type (&klass->byval_arg, ins);
4852 case CEE_LDELEM_REF: {
4856 * ldind.x (ldelema (array, index))
4857 * ldelema does the bounds check
4861 klass = array_access_to_klass (*ip);
4862 NEW_LDELEMA (cfg, load, sp, klass);
4863 load->cil_code = ip;
4864 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
4866 ins->inst_left = load;
4868 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
4878 case CEE_STELEM_R8: {
4882 * stind.x (ldelema (array, index), val)
4883 * ldelema does the bounds check
4887 klass = array_access_to_klass (*ip);
4888 NEW_LDELEMA (cfg, load, sp, klass);
4889 load->cil_code = ip;
4890 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
4892 ins->inst_left = load;
4893 ins->inst_right = sp [2];
4895 handle_loaded_temps (cfg, bblock, stack_start, sp);
4896 MONO_ADD_INS (bblock, ins);
4900 case CEE_STELEM_ANY: {
4904 * stind.x (ldelema (array, index), val)
4905 * ldelema does the bounds check
4910 token = read32 (ip + 1);
4911 klass = mono_class_get_full (image, token, generic_context);
4912 mono_class_init (klass);
4913 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4914 MonoMethod* helper = mono_marshal_get_stelemref ();
4915 MonoInst *iargs [3];
4916 handle_loaded_temps (cfg, bblock, stack_start, sp);
4922 mono_emit_method_call_spilled (cfg, bblock, helper, helper->signature, iargs, ip, NULL);
4924 NEW_LDELEMA (cfg, load, sp, klass);
4925 load->cil_code = ip;
4927 n = mono_type_to_stind (&klass->byval_arg);
4929 handle_stobj (cfg, bblock, load, sp [2], ip, klass, FALSE, FALSE);
4931 MONO_INST_NEW (cfg, ins, n);
4933 ins->inst_left = load;
4934 ins->inst_right = sp [2];
4935 handle_loaded_temps (cfg, bblock, stack_start, sp);
4936 MONO_ADD_INS (bblock, ins);
4943 case CEE_STELEM_REF: {
4944 MonoInst *iargs [3];
4945 MonoMethod* helper = mono_marshal_get_stelemref ();
4950 handle_loaded_temps (cfg, bblock, stack_start, sp);
4956 mono_emit_method_call_spilled (cfg, bblock, helper, helper->signature, iargs, ip, NULL);
4960 NEW_GROUP (cfg, group, sp [0], sp [1]);
4961 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
4963 ins->inst_left = group;
4964 ins->inst_right = sp [2];
4965 MONO_ADD_INS (bblock, ins);
4972 case CEE_CKFINITE: {
4973 MonoInst *store, *temp;
4976 /* this instr. can throw exceptions as side effect,
4977 * so we cant eliminate dead code which contains CKFINITE opdodes.
4978 * Spilling to memory makes sure that we always perform
4982 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
4984 ins->inst_left = sp [-1];
4985 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
4987 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4988 store->cil_code = ip;
4989 MONO_ADD_INS (bblock, store);
4991 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
4998 MONO_INST_NEW (cfg, ins, *ip);
5001 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
5002 mono_class_init (klass);
5003 ins->type = STACK_MP;
5004 ins->inst_left = *sp;
5006 ins->inst_newa_class = klass;
5011 case CEE_MKREFANY: {
5012 MonoInst *loc, *klassconst;
5015 MONO_INST_NEW (cfg, ins, *ip);
5018 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
5019 mono_class_init (klass);
5022 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
5023 NEW_TEMPLOADA (cfg, ins->inst_right, loc->inst_c0);
5025 NEW_PCONST (cfg, klassconst, klass);
5026 NEW_GROUP (cfg, ins->inst_left, *sp, klassconst);
5028 MONO_ADD_INS (bblock, ins);
5030 NEW_TEMPLOAD (cfg, *sp, loc->inst_c0);
5037 MonoClass *handle_class;
5039 CHECK_STACK_OVF (1);
5042 n = read32 (ip + 1);
5044 handle = mono_ldtoken (image, n, &handle_class, generic_context);
5045 mono_class_init (handle_class);
5047 if (cfg->opt & MONO_OPT_SHARED) {
5049 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
5051 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
5053 NEW_IMAGECONST (cfg, iargs [0], image);
5054 NEW_ICONST (cfg, iargs [1], n);
5055 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
5056 NEW_TEMPLOAD (cfg, res, temp);
5057 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5058 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
5059 MONO_ADD_INS (bblock, store);
5060 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5062 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method_full (image, read32 (ip + 6), NULL, generic_context)) &&
5063 (cmethod->klass == mono_defaults.monotype_class->parent) &&
5064 (strcmp (cmethod->name, "GetTypeFromHandle") == 0) &&
5065 ((g_hash_table_lookup (bbhash, ip + 5) == NULL) ||
5066 (g_hash_table_lookup (bbhash, ip + 5) == bblock))) {
5067 MonoClass *tclass = mono_class_from_mono_type (handle);
5068 mono_class_init (tclass);
5069 if (mono_compile_aot)
5070 NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n);
5072 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
5073 ins->type = STACK_OBJ;
5074 ins->klass = cmethod->klass;
5077 MonoInst *store, *addr, *vtvar;
5079 if (mono_compile_aot)
5080 NEW_LDTOKENCONST (cfg, ins, image, n);
5082 NEW_PCONST (cfg, ins, handle);
5083 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
5084 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5085 NEW_INDSTORE (cfg, store, addr, ins, &mono_defaults.int_class->byval_arg);
5086 MONO_ADD_INS (bblock, store);
5087 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5103 case CEE_ADD_OVF_UN:
5105 case CEE_MUL_OVF_UN:
5107 case CEE_SUB_OVF_UN:
5110 if (mono_find_jit_opcode_emulation (ins->opcode)) {
5112 *sp++ = emit_tree (cfg, bblock, ins);
5116 case CEE_ENDFINALLY:
5117 MONO_INST_NEW (cfg, ins, *ip);
5118 MONO_ADD_INS (bblock, ins);
5119 ins->cil_code = ip++;
5120 start_new_bblock = 1;
5123 * Control will leave the method so empty the stack, otherwise
5124 * the next basic block will start with a nonempty stack.
5126 while (sp != stack_start) {
5127 MONO_INST_NEW (cfg, ins, CEE_POP);
5131 MONO_ADD_INS (bblock, ins);
5137 if (*ip == CEE_LEAVE) {
5139 target = ip + 5 + (gint32)read32(ip + 1);
5142 target = ip + 2 + (signed char)(ip [1]);
5145 /* empty the stack */
5146 while (sp != stack_start) {
5147 MONO_INST_NEW (cfg, ins, CEE_POP);
5151 MONO_ADD_INS (bblock, ins);
5155 * If this leave statement is in a catch block, check for a
5156 * pending exception, and rethrow it if necessary.
5158 for (i = 0; i < header->num_clauses; ++i) {
5159 MonoExceptionClause *clause = &header->clauses [i];
5160 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code)) {
5163 temp = mono_emit_jit_icall (cfg, bblock, mono_thread_get_pending_exception, NULL, ip);
5164 NEW_TEMPLOAD (cfg, *sp, temp);
5166 MONO_INST_NEW (cfg, ins, OP_THROW_OR_NULL);
5167 ins->inst_left = *sp;
5169 MONO_ADD_INS (bblock, ins);
5173 /* fixme: call fault handler ? */
5175 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
5177 for (tmp = handlers; tmp; tmp = tmp->next) {
5179 link_bblock (cfg, bblock, tblock);
5180 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
5182 ins->inst_target_bb = tblock;
5183 MONO_ADD_INS (bblock, ins);
5185 g_list_free (handlers);
5188 MONO_INST_NEW (cfg, ins, CEE_BR);
5190 MONO_ADD_INS (bblock, ins);
5191 GET_BBLOCK (cfg, bbhash, tblock, target);
5192 link_bblock (cfg, bblock, tblock);
5193 CHECK_BBLOCK (target, ip, tblock);
5194 ins->inst_target_bb = tblock;
5195 start_new_bblock = 1;
5197 if (*ip == CEE_LEAVE)
5206 MONO_INST_NEW (cfg, ins, *ip);
5208 handle_loaded_temps (cfg, bblock, stack_start, sp);
5209 MONO_ADD_INS (bblock, ins);
5210 ins->cil_code = ip++;
5211 ins->inst_i0 = sp [0];
5212 ins->inst_i1 = sp [1];
5220 /* trampoline mono specific opcodes */
5221 case MONO_CUSTOM_PREFIX: {
5223 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
5228 case CEE_MONO_ICALL: {
5231 MonoJitICallInfo *info;
5233 token = read32 (ip + 2);
5234 func = mono_method_get_wrapper_data (method, token);
5235 info = mono_find_jit_icall_by_addr (func);
5238 CHECK_STACK (info->sig->param_count);
5239 sp -= info->sig->param_count;
5241 temp = mono_emit_jit_icall (cfg, bblock, info->func, sp, ip);
5242 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
5243 NEW_TEMPLOAD (cfg, *sp, temp);
5248 inline_costs += 10 * num_calls++;
5252 case CEE_MONO_LDPTR:
5253 CHECK_STACK_OVF (1);
5255 token = read32 (ip + 2);
5256 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
5260 inline_costs += 10 * num_calls++;
5262 case CEE_MONO_VTADDR:
5265 MONO_INST_NEW (cfg, ins, OP_VTADDR);
5267 ins->type = STACK_MP;
5268 ins->inst_left = *sp;
5272 case CEE_MONO_NEWOBJ: {
5273 MonoInst *iargs [2];
5275 CHECK_STACK_OVF (1);
5277 token = read32 (ip + 2);
5278 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5279 mono_class_init (klass);
5280 NEW_DOMAINCONST (cfg, iargs [0]);
5281 NEW_CLASSCONST (cfg, iargs [1], klass);
5282 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
5283 NEW_TEMPLOAD (cfg, *sp, temp);
5286 inline_costs += 10 * num_calls++;
5289 case CEE_MONO_OBJADDR:
5292 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
5294 ins->type = STACK_MP;
5295 ins->inst_left = *sp;
5299 case CEE_MONO_LDNATIVEOBJ:
5302 token = read32 (ip + 2);
5303 klass = mono_method_get_wrapper_data (method, token);
5304 g_assert (klass->valuetype);
5305 mono_class_init (klass);
5306 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
5310 case CEE_MONO_RETOBJ:
5311 g_assert (cfg->ret);
5312 g_assert (method->signature->pinvoke);
5317 token = read32 (ip + 2);
5318 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5320 NEW_RETLOADA (cfg, ins);
5321 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
5323 if (sp != stack_start)
5326 MONO_INST_NEW (cfg, ins, CEE_BR);
5328 ins->inst_target_bb = end_bblock;
5329 MONO_ADD_INS (bblock, ins);
5330 link_bblock (cfg, bblock, end_bblock);
5331 start_new_bblock = 1;
5334 case CEE_MONO_CISINST:
5335 case CEE_MONO_CCASTCLASS: {
5340 token = read32 (ip + 2);
5341 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5342 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_CISINST) ? OP_CISINST : OP_CCASTCLASS);
5343 ins->type = STACK_I4;
5344 ins->inst_left = *sp;
5345 ins->inst_newa_class = klass;
5347 *sp++ = emit_tree (cfg, bblock, ins);
5351 case CEE_MONO_SAVE_LMF:
5352 case CEE_MONO_RESTORE_LMF:
5353 #ifdef MONO_ARCH_HAVE_LMF_OPS
5354 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_SAVE_LMF) ? OP_SAVE_LMF : OP_RESTORE_LMF);
5355 MONO_ADD_INS (bblock, ins);
5356 cfg->need_lmf_area = TRUE;
5361 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
5370 /* somewhat similar to LDTOKEN */
5371 MonoInst *addr, *vtvar;
5372 CHECK_STACK_OVF (1);
5373 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
5375 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5376 addr->cil_code = ip;
5377 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
5379 ins->inst_left = addr;
5380 MONO_ADD_INS (bblock, ins);
5381 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5395 * The following transforms:
5396 * CEE_CEQ into OP_CEQ
5397 * CEE_CGT into OP_CGT
5398 * CEE_CGT_UN into OP_CGT_UN
5399 * CEE_CLT into OP_CLT
5400 * CEE_CLT_UN into OP_CLT_UN
5402 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
5404 MONO_INST_NEW (cfg, ins, cmp->opcode);
5406 cmp->inst_i0 = sp [0];
5407 cmp->inst_i1 = sp [1];
5411 if ((sp [0]->type == STACK_I8) || ((sizeof (gpointer) == 8) && ((sp [0]->type == STACK_PTR) || (sp [0]->type == STACK_OBJ) || (sp [0]->type == STACK_MP))))
5412 cmp->opcode = OP_LCOMPARE;
5414 cmp->opcode = OP_COMPARE;
5416 ins->type = STACK_I4;
5419 /* spill it to reduce the expression complexity
5420 * and workaround bug 54209
5422 if (cmp->inst_left->type == STACK_I8) {
5424 *sp++ = emit_tree (cfg, bblock, ins);
5433 CHECK_STACK_OVF (1);
5435 n = read32 (ip + 2);
5436 if (method->wrapper_type != MONO_WRAPPER_NONE)
5437 cmethod = mono_method_get_wrapper_data (method, n);
5439 cmethod = mono_get_method_full (image, n, NULL, generic_context);
5442 mono_class_init (cmethod->klass);
5443 handle_loaded_temps (cfg, bblock, stack_start, sp);
5445 NEW_METHODCONST (cfg, argconst, cmethod);
5446 if (method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED)
5447 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
5449 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn_nosync, &argconst, ip);
5450 NEW_TEMPLOAD (cfg, *sp, temp);
5454 inline_costs += 10 * num_calls++;
5457 case CEE_LDVIRTFTN: {
5463 n = read32 (ip + 2);
5464 if (method->wrapper_type != MONO_WRAPPER_NONE)
5465 cmethod = mono_method_get_wrapper_data (method, n);
5467 cmethod = mono_get_method_full (image, n, NULL, generic_context);
5469 mono_class_init (cmethod->klass);
5470 handle_loaded_temps (cfg, bblock, stack_start, sp);
5474 NEW_METHODCONST (cfg, args [1], cmethod);
5475 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
5476 NEW_TEMPLOAD (cfg, *sp, temp);
5480 inline_costs += 10 * num_calls++;
5484 CHECK_STACK_OVF (1);
5486 n = read16 (ip + 2);
5488 NEW_ARGLOAD (cfg, ins, n);
5494 CHECK_STACK_OVF (1);
5496 n = read16 (ip + 2);
5498 NEW_ARGLOADA (cfg, ins, n);
5506 handle_loaded_temps (cfg, bblock, stack_start, sp);
5508 n = read16 (ip + 2);
5510 NEW_ARGSTORE (cfg, ins, n, *sp);
5512 if (ins->opcode == CEE_STOBJ) {
5513 NEW_ARGLOADA (cfg, ins, n);
5514 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5516 MONO_ADD_INS (bblock, ins);
5520 CHECK_STACK_OVF (1);
5522 n = read16 (ip + 2);
5524 NEW_LOCLOAD (cfg, ins, n);
5530 CHECK_STACK_OVF (1);
5532 n = read16 (ip + 2);
5534 NEW_LOCLOADA (cfg, ins, n);
5543 n = read16 (ip + 2);
5545 handle_loaded_temps (cfg, bblock, stack_start, sp);
5546 NEW_LOCSTORE (cfg, ins, n, *sp);
5548 if (ins->opcode == CEE_STOBJ) {
5549 NEW_LOCLOADA (cfg, ins, n);
5550 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5552 MONO_ADD_INS (bblock, ins);
5559 if (sp != stack_start)
5561 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
5562 ins->inst_left = *sp;
5564 ins->type = STACK_MP;
5566 cfg->flags |= MONO_CFG_HAS_ALLOCA;
5567 if (header->init_locals)
5568 ins->flags |= MONO_INST_INIT;
5572 /* FIXME: set init flag if locals init is set in this method */
5574 case CEE_ENDFILTER: {
5575 MonoExceptionClause *clause, *nearest;
5576 int cc, nearest_num;
5580 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
5582 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
5583 ins->inst_left = *sp;
5585 MONO_ADD_INS (bblock, ins);
5586 start_new_bblock = 1;
5591 for (cc = 0; cc < header->num_clauses; ++cc) {
5592 clause = &header->clauses [cc];
5593 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
5594 (!nearest || (clause->data.filter_offset > nearest->data.filter_offset))) {
5600 filter_lengths [nearest_num] = (ip - header->code) - nearest->data.filter_offset;
5604 case CEE_UNALIGNED_:
5605 ins_flag |= MONO_INST_UNALIGNED;
5606 /* FIXME: record alignment? we can assume 1 for now */
5611 ins_flag |= MONO_INST_VOLATILE;
5615 ins_flag |= MONO_INST_TAILCALL;
5616 cfg->flags |= MONO_CFG_HAS_TAIL;
5617 /* Can't inline tail calls at this time */
5618 inline_costs += 100000;
5625 token = read32 (ip + 2);
5626 if (method->wrapper_type != MONO_WRAPPER_NONE)
5627 klass = mono_method_get_wrapper_data (method, token);
5629 klass = mono_class_get_full (image, token, generic_context);
5630 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5631 MonoInst *store, *load;
5632 NEW_PCONST (cfg, load, NULL);
5633 load->cil_code = ip;
5634 load->type = STACK_OBJ;
5635 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
5636 store->cil_code = ip;
5637 handle_loaded_temps (cfg, bblock, stack_start, sp);
5638 MONO_ADD_INS (bblock, store);
5639 store->inst_i0 = sp [0];
5640 store->inst_i1 = load;
5642 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
5647 case CEE_CONSTRAINED_:
5648 /* FIXME: implement */
5650 token = read32 (ip + 2);
5651 constrained_call = mono_class_get_full (image, token, generic_context);
5656 MonoInst *iargs [3];
5659 if ((cfg->opt & MONO_OPT_INTRINS) && (ip [1] == CEE_CPBLK) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5)) {
5661 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
5662 copy->inst_left = sp [0];
5663 copy->inst_right = sp [1];
5664 copy->cil_code = ip;
5666 MONO_ADD_INS (bblock, copy);
5673 handle_loaded_temps (cfg, bblock, stack_start, sp);
5674 if (ip [1] == CEE_CPBLK) {
5675 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
5677 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
5686 ins_flag |= MONO_INST_NOTYPECHECK;
5688 ins_flag |= MONO_INST_NORANGECHECK;
5689 /* we ignore the no-nullcheck for now since we
5690 * really do it explicitly only when doing callvirt->call
5696 /* FIXME: check we are in a catch handler */
5697 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
5698 load->cil_code = ip;
5699 #ifdef MONO_ARCH_HAVE_RETHROW
5700 MONO_INST_NEW (cfg, ins, OP_RETHROW);
5702 MONO_INST_NEW (cfg, ins, CEE_THROW);
5704 ins->inst_left = load;
5706 MONO_ADD_INS (bblock, ins);
5708 start_new_bblock = 1;
5713 CHECK_STACK_OVF (1);
5715 token = read32 (ip + 2);
5716 /* FIXXME: handle generics. */
5717 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
5718 MonoType *type = mono_type_create_from_typespec (image, token);
5719 token = mono_type_size (type, &align);
5721 MonoClass *szclass = mono_class_get_full (image, token, generic_context);
5722 mono_class_init (szclass);
5723 token = mono_class_value_size (szclass, &align);
5725 NEW_ICONST (cfg, ins, token);
5730 case CEE_REFANYTYPE:
5732 MONO_INST_NEW (cfg, ins, OP_REFANYTYPE);
5734 ins->type = STACK_MP;
5735 ins->inst_left = *sp;
5736 ins->type = STACK_VTYPE;
5737 ins->klass = mono_defaults.typehandle_class;
5746 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
5751 g_error ("opcode 0x%02x not handled", *ip);
5754 if (start_new_bblock != 1)
5757 bblock->cil_length = ip - bblock->cil_code;
5758 bblock->next_bb = end_bblock;
5759 link_bblock (cfg, bblock, end_bblock);
5761 if (cfg->method == method && cfg->domainvar) {
5765 MonoInst *get_domain;
5767 if (! (get_domain = mono_arch_get_domain_intrinsic (cfg))) {
5770 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
5771 call->signature = helper_sig_domain_get;
5772 call->inst.type = STACK_PTR;
5773 call->fptr = mono_domain_get;
5774 get_domain = (MonoInst*)call;
5777 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, get_domain);
5778 MONO_ADD_INS (init_localsbb, store);
5781 if (header->init_locals) {
5783 for (i = 0; i < header->num_locals; ++i) {
5784 MonoType *ptype = header->locals [i];
5785 int t = ptype->type;
5786 if (t == MONO_TYPE_VALUETYPE && ptype->data.klass->enumtype)
5787 t = ptype->data.klass->enum_basetype->type;
5788 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
5789 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
5790 NEW_ICONST (cfg, ins, 0);
5791 NEW_LOCSTORE (cfg, store, i, ins);
5792 MONO_ADD_INS (init_localsbb, store);
5793 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
5794 MONO_INST_NEW (cfg, ins, OP_I8CONST);
5795 ins->type = STACK_I8;
5797 NEW_LOCSTORE (cfg, store, i, ins);
5798 MONO_ADD_INS (init_localsbb, store);
5799 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
5800 MONO_INST_NEW (cfg, ins, OP_R8CONST);
5801 ins->type = STACK_R8;
5802 ins->inst_p0 = (void*)&r8_0;
5803 NEW_LOCSTORE (cfg, store, i, ins);
5804 MONO_ADD_INS (init_localsbb, store);
5805 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
5806 ((t == MONO_TYPE_GENERICINST) && mono_metadata_generic_inst_is_valuetype (ptype->data.generic_inst))) {
5807 NEW_LOCLOADA (cfg, ins, i);
5808 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (ptype), NULL, NULL);
5811 NEW_PCONST (cfg, ins, NULL);
5812 NEW_LOCSTORE (cfg, store, i, ins);
5813 MONO_ADD_INS (init_localsbb, store);
5819 /* resolve backward branches in the middle of an existing basic block */
5820 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
5822 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
5823 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
5824 if (tblock != start_bblock) {
5826 split_bblock (cfg, tblock, bblock);
5827 l = bblock->cil_code - header->code;
5828 bblock->cil_length = tblock->cil_length - l;
5829 tblock->cil_length = l;
5831 g_print ("recheck failed.\n");
5836 * we compute regions here, because the length of filter clauses is not known in advance.
5837 * It is computed in the CEE_ENDFILTER case in the above switch statement
5839 if (cfg->method == method) {
5841 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5842 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
5844 mono_create_spvar_for_region (cfg, bb->region);
5845 if (cfg->verbose_level > 2)
5846 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
5849 g_hash_table_destroy (bbhash);
5852 dont_inline = g_list_remove (dont_inline, method);
5853 return inline_costs;
5856 if (cfg->method != method)
5857 g_hash_table_destroy (bbhash);
5858 dont_inline = g_list_remove (dont_inline, method);
5862 if (cfg->method != method)
5863 g_hash_table_destroy (bbhash);
5864 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
5865 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
5866 dont_inline = g_list_remove (dont_inline, method);
5871 mono_print_tree (MonoInst *tree) {
5877 arity = mono_burg_arity [tree->opcode];
5879 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
5881 switch (tree->opcode) {
5883 printf ("[%d]", tree->inst_c0);
5886 printf ("[%lld]", tree->inst_l);
5889 printf ("[%f]", *(double*)tree->inst_p0);
5892 printf ("[%f]", *(float*)tree->inst_p0);
5896 printf ("[%d]", tree->inst_c0);
5899 if (tree->inst_offset < 0)
5900 printf ("[-0x%x(%s)]", -tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5902 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5905 printf ("[%s]", mono_arch_regname (tree->dreg));
5908 printf ("[%s]", tree->inst_newa_class->name);
5909 mono_print_tree (tree->inst_newa_len);
5920 case OP_VOIDCALLVIRT: {
5921 MonoCallInst *call = (MonoCallInst*)tree;
5923 printf ("[%s]", call->method->name);
5928 printf ("[%d (", tree->inst_c0);
5929 for (i = 0; i < tree->inst_phi_args [0]; i++) {
5932 printf ("%d", tree->inst_phi_args [i + 1]);
5943 case OP_LOAD_MEMBASE:
5944 case OP_LOADI4_MEMBASE:
5945 case OP_LOADU4_MEMBASE:
5946 case OP_LOADU1_MEMBASE:
5947 case OP_LOADI1_MEMBASE:
5948 case OP_LOADU2_MEMBASE:
5949 case OP_LOADI2_MEMBASE:
5950 printf ("[%s] <- [%s + 0x%x]", mono_arch_regname (tree->dreg), mono_arch_regname (tree->inst_basereg), tree->inst_offset);
5953 printf ("[B%d]", tree->inst_target_bb->block_num);
5963 case OP_VOIDCALL_REG:
5964 mono_print_tree (tree->inst_left);
5976 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
5977 mono_print_tree (tree->inst_left);
5980 if (!mono_arch_print_tree(tree, arity)) {
5982 mono_print_tree (tree->inst_left);
5984 mono_print_tree (tree->inst_right);
5995 mono_print_tree_nl (MonoInst *tree)
5997 mono_print_tree (tree);
6001 #define make_icall_sig mono_create_icall_signature
6004 create_helper_signature (void)
6006 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
6007 helper_sig_newarr = make_icall_sig ("object ptr ptr int32");
6009 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
6010 helper_sig_newarr_specific = make_icall_sig ("object ptr int32");
6012 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
6013 helper_sig_object_new = make_icall_sig ("object ptr ptr");
6015 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
6016 helper_sig_object_new_specific = make_icall_sig ("object ptr");
6018 /* void* mono_method_compile (MonoMethod*) */
6019 helper_sig_compile = make_icall_sig ("ptr ptr");
6021 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
6022 helper_sig_compile_virt = make_icall_sig ("ptr object ptr");
6024 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
6025 helper_sig_ldstr = make_icall_sig ("object ptr ptr int32");
6027 /* MonoDomain *mono_domain_get (void) */
6028 helper_sig_domain_get = make_icall_sig ("ptr");
6030 /* void stelem_ref (MonoArray *, int index, MonoObject *) */
6031 helper_sig_stelem_ref = make_icall_sig ("void ptr int32 object");
6033 /* void stelem_ref_check (MonoArray *, MonoObject *) */
6034 helper_sig_stelem_ref_check = make_icall_sig ("void object object");
6036 /* long amethod (long, long) */
6037 helper_sig_long_long_long = make_icall_sig ("long long long");
6039 /* object amethod (intptr) */
6040 helper_sig_obj_ptr = make_icall_sig ("object ptr");
6042 helper_sig_obj_ptr_ptr = make_icall_sig ("object ptr ptr");
6044 helper_sig_obj_obj_ptr_ptr = make_icall_sig ("object object ptr ptr");
6046 helper_sig_void_void = make_icall_sig ("void");
6048 /* void amethod (intptr) */
6049 helper_sig_void_ptr = make_icall_sig ("void ptr");
6051 /* void amethod (MonoObject *obj) */
6052 helper_sig_void_obj = make_icall_sig ("void object");
6054 /* void amethod (MonoObject *obj, void *ptr, int i) */
6055 helper_sig_void_obj_ptr_int = make_icall_sig ("void object ptr int");
6057 helper_sig_void_obj_ptr_ptr_obj = make_icall_sig ("void object ptr ptr object");
6059 /* intptr amethod (void) */
6060 helper_sig_ptr_void = make_icall_sig ("ptr");
6062 /* object amethod (void) */
6063 helper_sig_obj_void = make_icall_sig ("object");
6065 /* void amethod (intptr, intptr) */
6066 helper_sig_void_ptr_ptr = make_icall_sig ("void ptr ptr");
6068 /* void amethod (intptr, intptr, intptr) */
6069 helper_sig_void_ptr_ptr_ptr = make_icall_sig ("void ptr ptr ptr");
6071 /* intptr amethod (intptr, intptr) */
6072 helper_sig_ptr_ptr_ptr = make_icall_sig ("ptr ptr ptr");
6074 /* IntPtr amethod (object) */
6075 helper_sig_ptr_obj = make_icall_sig ("ptr object");
6077 /* IntPtr amethod (object, int) */
6078 helper_sig_ptr_obj_int = make_icall_sig ("ptr object int");
6080 /* IntPtr amethod (int) */
6081 helper_sig_ptr_int = make_icall_sig ("ptr int32");
6083 /* long amethod (long, guint32) */
6084 helper_sig_long_long_int = make_icall_sig ("long long int32");
6086 /* ulong amethod (double) */
6087 helper_sig_ulong_double = make_icall_sig ("ulong double");
6089 /* long amethod (double) */
6090 helper_sig_long_double = make_icall_sig ("long double");
6092 /* double amethod (long) */
6093 helper_sig_double_long = make_icall_sig ("double long");
6095 /* double amethod (int) */
6096 helper_sig_double_int = make_icall_sig ("double int32");
6098 /* float amethod (long) */
6099 helper_sig_float_long = make_icall_sig ("float long");
6101 /* double amethod (double, double) */
6102 helper_sig_double_double_double = make_icall_sig ("double double double");
6104 /* uint amethod (double) */
6105 helper_sig_uint_double = make_icall_sig ("uint32 double");
6107 /* int amethod (double) */
6108 helper_sig_int_double = make_icall_sig ("int32 double");
6110 /* void initobj (intptr, int size) */
6111 helper_sig_initobj = make_icall_sig ("void ptr int32");
6113 /* void memcpy (intptr, intptr, int size) */
6114 helper_sig_memcpy = make_icall_sig ("void ptr ptr int32");
6116 /* void memset (intptr, int val, int size) */
6117 helper_sig_memset = make_icall_sig ("void ptr int32 int32");
6119 helper_sig_class_init_trampoline = make_icall_sig ("void");
6123 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
6126 MonoMethod *wrapper;
6129 if (callinfo->wrapper)
6130 return callinfo->wrapper;
6132 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
6133 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
6134 /* Must be domain neutral since there is only one copy */
6135 code = mono_jit_compile_method_with_opt (wrapper, default_opt | MONO_OPT_SHARED);
6137 if (!callinfo->wrapper) {
6138 callinfo->wrapper = code;
6139 mono_register_jit_icall_wrapper (callinfo, code);
6140 mono_debug_add_icall_wrapper (wrapper, callinfo);
6144 return callinfo->wrapper;
6148 mono_create_class_init_trampoline (MonoVTable *vtable)
6152 /* previously created trampoline code */
6153 mono_domain_lock (vtable->domain);
6155 mono_g_hash_table_lookup (vtable->domain->class_init_trampoline_hash,
6157 mono_domain_unlock (vtable->domain);
6161 code = mono_arch_create_class_init_trampoline (vtable);
6163 /* store trampoline address */
6164 mono_domain_lock (vtable->domain);
6165 mono_g_hash_table_insert (vtable->domain->class_init_trampoline_hash,
6167 mono_domain_unlock (vtable->domain);
6169 EnterCriticalSection (&jit_mutex);
6170 if (!class_init_hash_addr)
6171 class_init_hash_addr = g_hash_table_new (NULL, NULL);
6172 g_hash_table_insert (class_init_hash_addr, code, vtable);
6173 LeaveCriticalSection (&jit_mutex);
6179 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method,
6180 gboolean add_sync_wrapper)
6185 if (add_sync_wrapper && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
6186 return mono_create_jump_trampoline (domain, mono_marshal_get_synchronized_wrapper (method), FALSE);
6188 code = mono_jit_find_compiled_method (domain, method);
6192 mono_domain_lock (domain);
6193 code = mono_g_hash_table_lookup (domain->jump_trampoline_hash, method);
6194 mono_domain_unlock (domain);
6198 ji = mono_arch_create_jump_trampoline (method);
6201 * mono_delegate_ctor needs to find the method metadata from the
6202 * trampoline address, so we save it here.
6205 mono_jit_info_table_add (mono_get_root_domain (), ji);
6207 mono_domain_lock (domain);
6208 mono_g_hash_table_insert (domain->jump_trampoline_hash, method, ji->code_start);
6209 mono_domain_unlock (domain);
6211 return ji->code_start;
6215 mono_create_jit_trampoline (MonoMethod *method)
6217 MonoDomain *domain = mono_domain_get ();
6220 /* Trampoline are domain specific, so cache only the one used in the root domain */
6221 if ((domain == mono_get_root_domain ()) && method->info)
6222 return method->info;
6224 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
6225 return mono_create_jit_trampoline (mono_marshal_get_synchronized_wrapper (method));
6227 tramp = mono_arch_create_jit_trampoline (method);
6228 if (domain == mono_get_root_domain ())
6229 method->info = tramp;
6235 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
6239 EnterCriticalSection (&jit_mutex);
6240 if (class_init_hash_addr)
6241 res = g_hash_table_lookup (class_init_hash_addr, addr);
6244 LeaveCriticalSection (&jit_mutex);
6249 mono_dynamic_code_hash_insert (MonoMethod *method, MonoJitDynamicMethodInfo *ji)
6251 EnterCriticalSection (&jit_mutex);
6252 if (!dynamic_code_hash)
6253 dynamic_code_hash = g_hash_table_new (NULL, NULL);
6254 g_hash_table_insert (dynamic_code_hash, method, ji);
6255 LeaveCriticalSection (&jit_mutex);
6258 static MonoJitDynamicMethodInfo*
6259 mono_dynamic_code_hash_lookup (MonoMethod *method)
6261 MonoJitDynamicMethodInfo *res;
6263 EnterCriticalSection (&jit_mutex);
6264 if (dynamic_code_hash)
6265 res = g_hash_table_lookup (dynamic_code_hash, method);
6268 LeaveCriticalSection (&jit_mutex);
6273 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func, gboolean no_throw)
6275 MonoJitICallInfo *info;
6277 if (!emul_opcode_map)
6278 emul_opcode_map = g_new0 (MonoJitICallInfo*, OP_LAST + 1);
6280 g_assert (!sig->hasthis);
6281 g_assert (sig->param_count < 3);
6283 info = mono_register_jit_icall (func, name, sig, no_throw);
6285 emul_opcode_map [opcode] = info;
6289 decompose_foreach (MonoInst *tree, gpointer data)
6291 static MonoJitICallInfo *newarr_info = NULL;
6292 static MonoJitICallInfo *newarr_specific_info = NULL;
6293 MonoJitICallInfo *info;
6296 switch (tree->opcode) {
6298 MonoCompile *cfg = data;
6299 MonoInst *iargs [3];
6302 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
6303 g_assert (newarr_info);
6304 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
6305 g_assert (newarr_specific_info);
6308 if (cfg->opt & MONO_OPT_SHARED) {
6309 NEW_DOMAINCONST (cfg, iargs [0]);
6310 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
6311 iargs [2] = tree->inst_newa_len;
6316 MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
6318 NEW_VTABLECONST (cfg, iargs [0], vtable);
6319 iargs [1] = tree->inst_newa_len;
6321 info = newarr_specific_info;
6324 mono_emulate_opcode (cfg, tree, iargs, info);
6326 /* Need to decompose arguments after the the opcode is decomposed */
6327 for (i = 0; i < info->sig->param_count; ++i)
6328 dec_foreach (iargs [i], cfg);
6338 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
6340 switch (mono_burg_arity [tree->opcode]) {
6343 mono_inst_foreach (tree->inst_left, func, data);
6346 mono_inst_foreach (tree->inst_left, func, data);
6347 mono_inst_foreach (tree->inst_right, func, data);
6350 g_assert_not_reached ();
6357 mono_print_bb_code (MonoBasicBlock *bb) {
6359 MonoInst *c = bb->code;
6361 mono_print_tree (c);
6369 print_dfn (MonoCompile *cfg) {
6374 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
6376 for (i = 0; i < cfg->num_bblocks; ++i) {
6377 bb = cfg->bblocks [i];
6378 /*if (bb->cil_code) {
6379 char* code1, *code2;
6380 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
6381 if (bb->last_ins->cil_code)
6382 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
6384 code2 = g_strdup ("");
6386 code1 [strlen (code1) - 1] = 0;
6387 code = g_strdup_printf ("%s -> %s", code1, code2);
6391 code = g_strdup ("\n");
6392 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
6394 MonoInst *c = bb->code;
6396 mono_print_tree (c);
6404 g_print ("\tprev:");
6405 for (j = 0; j < bb->in_count; ++j) {
6406 g_print (" BB%d", bb->in_bb [j]->block_num);
6408 g_print ("\t\tsucc:");
6409 for (j = 0; j < bb->out_count; ++j) {
6410 g_print (" BB%d", bb->out_bb [j]->block_num);
6412 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
6415 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
6418 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
6420 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
6428 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
6432 g_assert (bb->code);
6433 bb->last_ins->next = inst;
6434 bb->last_ins = inst;
6436 bb->last_ins = bb->code = inst;
6441 mono_destroy_compile (MonoCompile *cfg)
6443 //mono_mempool_stats (cfg->mempool);
6444 g_hash_table_destroy (cfg->bb_hash);
6445 mono_free_loop_info (cfg);
6447 mono_regstate_free (cfg->rs);
6449 g_hash_table_destroy (cfg->spvars);
6450 mono_mempool_destroy (cfg->mempool);
6451 g_list_free (cfg->ldstr_list);
6453 g_free (cfg->varinfo);
6458 #ifdef HAVE_KW_THREAD
6459 static __thread gpointer mono_lmf_addr;
6463 mono_get_lmf_addr (void)
6465 #ifdef HAVE_KW_THREAD
6466 return mono_lmf_addr;
6468 MonoJitTlsData *jit_tls;
6470 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
6471 return &jit_tls->lmf;
6473 g_assert_not_reached ();
6479 * mono_thread_abort:
6480 * @obj: exception object
6482 * abort the thread, print exception information and stack trace
6485 mono_thread_abort (MonoObject *obj)
6487 /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
6489 /* handle_remove should be eventually called for this thread, too
6492 mono_thread_exit ();
6496 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
6498 MonoJitTlsData *jit_tls;
6501 jit_tls = TlsGetValue (mono_jit_tls_id);
6505 jit_tls = g_new0 (MonoJitTlsData, 1);
6507 TlsSetValue (mono_jit_tls_id, jit_tls);
6509 jit_tls->abort_func = abort_func;
6510 jit_tls->end_of_stack = stack_start;
6512 lmf = g_new0 (MonoLMF, 1);
6515 jit_tls->lmf = jit_tls->first_lmf = lmf;
6517 #ifdef HAVE_KW_THREAD
6518 mono_lmf_addr = &jit_tls->lmf;
6521 mono_arch_setup_jit_tls_data (jit_tls);
6527 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
6530 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
6531 thread = mono_thread_current ();
6533 thread->jit_data = jit_tls;
6536 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
6539 mono_thread_abort_dummy (MonoObject *obj)
6541 if (mono_thread_attach_aborted_cb)
6542 mono_thread_attach_aborted_cb (obj);
6544 mono_thread_abort (obj);
6548 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
6551 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
6552 thread = mono_thread_current ();
6554 thread->jit_data = jit_tls;
6558 mini_thread_cleanup (MonoThread *thread)
6560 MonoJitTlsData *jit_tls = thread->jit_data;
6563 mono_arch_free_jit_tls_data (jit_tls);
6564 g_free (jit_tls->first_lmf);
6566 thread->jit_data = NULL;
6571 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
6573 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
6577 ji->data.target = target;
6578 ji->next = cfg->patch_info;
6580 cfg->patch_info = ji;
6584 mono_remove_patch_info (MonoCompile *cfg, int ip)
6586 MonoJumpInfo **ji = &cfg->patch_info;
6589 if ((*ji)->ip.i == ip)
6592 ji = &((*ji)->next);
6597 mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors)
6599 unsigned char *ip = patch_info->ip.i + code;
6600 gconstpointer target = NULL;
6602 switch (patch_info->type) {
6603 case MONO_PATCH_INFO_BB:
6604 target = patch_info->data.bb->native_offset + code;
6606 case MONO_PATCH_INFO_ABS:
6607 target = patch_info->data.target;
6609 case MONO_PATCH_INFO_LABEL:
6610 target = patch_info->data.inst->inst_c0 + code;
6612 case MONO_PATCH_INFO_IP:
6615 case MONO_PATCH_INFO_METHOD_REL:
6616 target = code + patch_info->data.offset;
6618 case MONO_PATCH_INFO_INTERNAL_METHOD: {
6619 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
6621 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
6622 g_assert_not_reached ();
6624 target = mono_icall_get_wrapper (mi);
6627 case MONO_PATCH_INFO_METHOD_JUMP: {
6630 /* get the trampoline to the method from the domain */
6631 target = mono_create_jump_trampoline (domain, patch_info->data.method, TRUE);
6632 if (!domain->jump_target_hash)
6633 domain->jump_target_hash = g_hash_table_new (NULL, NULL);
6634 list = g_hash_table_lookup (domain->jump_target_hash, patch_info->data.method);
6635 list = g_slist_prepend (list, ip);
6636 g_hash_table_insert (domain->jump_target_hash, patch_info->data.method, list);
6639 case MONO_PATCH_INFO_METHOD:
6640 if (patch_info->data.method == method) {
6643 /* get the trampoline to the method from the domain */
6644 target = mono_create_jit_trampoline (patch_info->data.method);
6646 case MONO_PATCH_INFO_SWITCH: {
6647 gpointer *jump_table;
6650 if (method->dynamic) {
6651 jump_table = mono_code_manager_reserve (mono_dynamic_code_hash_lookup (method)->code_mp, sizeof (gpointer) * patch_info->table_size);
6654 mono_domain_lock (domain);
6655 jump_table = mono_code_manager_reserve (domain->code_mp, sizeof (gpointer) * patch_info->table_size);
6656 mono_domain_unlock (domain);
6659 for (i = 0; i < patch_info->table_size; i++) {
6660 jump_table [i] = code + (int)patch_info->data.table [i];
6662 target = jump_table;
6665 case MONO_PATCH_INFO_METHODCONST:
6666 case MONO_PATCH_INFO_CLASS:
6667 case MONO_PATCH_INFO_IMAGE:
6668 case MONO_PATCH_INFO_FIELD:
6669 target = patch_info->data.target;
6671 case MONO_PATCH_INFO_IID:
6672 mono_class_init (patch_info->data.klass);
6673 target = (gpointer)patch_info->data.klass->interface_id;
6675 case MONO_PATCH_INFO_VTABLE:
6676 target = mono_class_vtable (domain, patch_info->data.klass);
6678 case MONO_PATCH_INFO_CLASS_INIT:
6679 target = mono_create_class_init_trampoline (mono_class_vtable (domain, patch_info->data.klass));
6681 case MONO_PATCH_INFO_SFLDA: {
6682 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
6683 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (vtable->klass, method))
6684 /* Done by the generated code */
6688 mono_runtime_class_init (vtable);
6690 target = (char*)vtable->data + patch_info->data.field->offset;
6693 case MONO_PATCH_INFO_R4:
6694 case MONO_PATCH_INFO_R8:
6695 target = patch_info->data.target;
6697 case MONO_PATCH_INFO_EXC_NAME:
6698 target = patch_info->data.name;
6700 case MONO_PATCH_INFO_LDSTR:
6702 mono_ldstr (domain, patch_info->data.token->image,
6703 mono_metadata_token_index (patch_info->data.token->token));
6705 case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
6707 MonoClass *handle_class;
6709 handle = mono_ldtoken (patch_info->data.token->image,
6710 patch_info->data.token->token, &handle_class, NULL);
6711 mono_class_init (handle_class);
6712 mono_class_init (mono_class_from_mono_type (handle));
6715 mono_type_get_object (domain, handle);
6718 case MONO_PATCH_INFO_LDTOKEN: {
6720 MonoClass *handle_class;
6722 handle = mono_ldtoken (patch_info->data.token->image,
6723 patch_info->data.token->token, &handle_class, NULL);
6724 mono_class_init (handle_class);
6729 case MONO_PATCH_INFO_BB_OVF:
6730 case MONO_PATCH_INFO_EXC_OVF:
6733 g_assert_not_reached ();
6736 return (gpointer)target;
6740 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
6741 MonoJitICallInfo *info;
6743 decompose_foreach (tree, cfg);
6745 switch (mono_burg_arity [tree->opcode]) {
6748 dec_foreach (tree->inst_left, cfg);
6750 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6751 MonoInst *iargs [2];
6753 iargs [0] = tree->inst_left;
6755 mono_emulate_opcode (cfg, tree, iargs, info);
6761 #ifdef MONO_ARCH_BIGMUL_INTRINS
6762 if (tree->opcode == OP_LMUL
6763 && (cfg->opt & MONO_OPT_INTRINS)
6764 && (tree->inst_left->opcode == CEE_CONV_I8
6765 || tree->inst_left->opcode == CEE_CONV_U8)
6766 && tree->inst_left->inst_left->type == STACK_I4
6767 && (tree->inst_right->opcode == CEE_CONV_I8
6768 || tree->inst_right->opcode == CEE_CONV_U8)
6769 && tree->inst_right->inst_left->type == STACK_I4
6770 && tree->inst_left->opcode == tree->inst_right->opcode) {
6771 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
6772 tree->inst_left = tree->inst_left->inst_left;
6773 tree->inst_right = tree->inst_right->inst_left;
6774 dec_foreach (tree, cfg);
6777 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6778 MonoInst *iargs [2];
6780 iargs [0] = tree->inst_i0;
6781 iargs [1] = tree->inst_i1;
6783 mono_emulate_opcode (cfg, tree, iargs, info);
6785 dec_foreach (iargs [0], cfg);
6786 dec_foreach (iargs [1], cfg);
6789 dec_foreach (tree->inst_left, cfg);
6790 dec_foreach (tree->inst_right, cfg);
6794 g_assert_not_reached ();
6799 decompose_pass (MonoCompile *cfg) {
6802 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6805 cfg->prev_ins = NULL;
6806 for (tree = cfg->cbb->code; tree; tree = tree->next) {
6807 dec_foreach (tree, cfg);
6808 cfg->prev_ins = tree;
6814 nullify_basic_block (MonoBasicBlock *bb)
6821 bb->code = bb->last_ins = NULL;
6822 bb->cil_code = NULL;
6826 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6830 for (i = 0; i < bb->out_count; i++) {
6831 MonoBasicBlock *ob = bb->out_bb [i];
6834 if (bb->out_count > 1) {
6835 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
6839 bb->out_bb [i] = repl;
6846 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6850 for (i = 0; i < bb->in_count; i++) {
6851 MonoBasicBlock *ib = bb->in_bb [i];
6854 if (bb->in_count > 1) {
6855 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
6859 bb->in_bb [i] = repl;
6866 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6870 for (i = 0; i < bb->out_count; i++) {
6871 MonoBasicBlock *ob = bb->out_bb [i];
6872 for (j = 0; j < ob->in_count; j++) {
6873 if (ob->in_bb [j] == orig) {
6874 ob->in_bb [j] = repl;
6883 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
6885 bb->out_count = bbn->out_count;
6886 bb->out_bb = bbn->out_bb;
6888 replace_basic_block (bb, bbn, bb);
6892 bb->last_ins->next = bbn->code;
6893 bb->last_ins = bbn->last_ins;
6896 bb->code = bbn->code;
6897 bb->last_ins = bbn->last_ins;
6899 bb->next_bb = bbn->next_bb;
6900 nullify_basic_block (bbn);
6904 * Optimizes the branches on the Control Flow Graph
6908 optimize_branches (MonoCompile *cfg)
6910 int i, changed = FALSE;
6911 MonoBasicBlock *bb, *bbn;
6912 guint32 niterations;
6915 * Some crazy loops could cause the code below to go into an infinite
6916 * loop, see bug #53003 for an example. To prevent this, we put an upper
6917 * bound on the number of iterations.
6924 /* we skip the entry block (exit is handled specially instead ) */
6925 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
6927 /* dont touch code inside exception clauses */
6928 if (bb->region != -1)
6931 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
6932 if (cfg->verbose_level > 2)
6933 g_print ("nullify block triggered %d\n", bbn->block_num);
6935 bb->next_bb = bbn->next_bb;
6937 for (i = 0; i < bbn->out_count; i++)
6938 replace_in_block (bbn->out_bb [i], bbn, NULL);
6940 nullify_basic_block (bbn);
6944 if (bb->out_count == 1) {
6945 bbn = bb->out_bb [0];
6947 /* conditional branches where true and false targets are the same can be also replaced with CEE_BR */
6948 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins)) {
6949 bb->last_ins->opcode = CEE_BR;
6950 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
6952 if (cfg->verbose_level > 2)
6953 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
6956 if (bb->region == bbn->region && bb->next_bb == bbn) {
6957 /* the block are in sequence anyway ... */
6959 /* branches to the following block can be removed */
6960 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
6961 bb->last_ins->opcode = CEE_NOP;
6963 if (cfg->verbose_level > 2)
6964 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
6967 if (bbn->in_count == 1) {
6969 if (bbn != cfg->bb_exit) {
6970 if (cfg->verbose_level > 2)
6971 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
6972 merge_basic_blocks (bb, bbn);
6976 //mono_print_bb_code (bb);
6981 } while (changed && (niterations > 0));
6988 /* we skip the entry block (exit is handled specially instead ) */
6989 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
6991 /* dont touch code inside exception clauses */
6992 if (bb->region != -1)
6995 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
6996 if (cfg->verbose_level > 2) {
6997 g_print ("nullify block triggered %d\n", bbn->block_num);
6999 bb->next_bb = bbn->next_bb;
7001 for (i = 0; i < bbn->out_count; i++)
7002 replace_in_block (bbn->out_bb [i], bbn, NULL);
7004 nullify_basic_block (bbn);
7010 if (bb->out_count == 1) {
7011 bbn = bb->out_bb [0];
7013 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
7014 bbn = bb->last_ins->inst_target_bb;
7015 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7016 bbn->code->inst_target_bb->region == bb->region) {
7018 if (cfg->verbose_level > 2)
7019 g_print ("in %s branch to branch triggered %d -> %d -> %d\n", cfg->method->name,
7020 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num);
7022 replace_in_block (bbn, bb, NULL);
7023 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7024 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7025 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
7030 } else if (bb->out_count == 2) {
7031 if (bb->last_ins && MONO_IS_COND_BRANCH_NOFP (bb->last_ins)) {
7032 bbn = bb->last_ins->inst_true_bb;
7033 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7034 bbn->code->inst_target_bb->region == bb->region) {
7035 if (cfg->verbose_level > 2)
7036 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
7037 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
7040 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
7042 replace_in_block (bbn, bb, NULL);
7044 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
7045 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7047 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7053 bbn = bb->last_ins->inst_false_bb;
7054 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7055 bbn->code->inst_target_bb->region == bb->region) {
7056 if (cfg->verbose_level > 2)
7057 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
7058 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
7061 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
7063 replace_in_block (bbn, bb, NULL);
7065 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
7066 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7068 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7076 } while (changed && (niterations > 0));
7081 mono_compile_create_vars (MonoCompile *cfg)
7083 MonoMethodSignature *sig;
7084 MonoMethodHeader *header;
7087 header = mono_method_get_header (cfg->method);
7089 sig = cfg->method->signature;
7091 if (!MONO_TYPE_IS_VOID (sig->ret)) {
7092 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
7093 cfg->ret->opcode = OP_RETARG;
7094 cfg->ret->inst_vtype = sig->ret;
7095 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
7097 if (cfg->verbose_level > 2)
7098 g_print ("creating vars\n");
7101 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
7103 for (i = 0; i < sig->param_count; ++i)
7104 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
7106 cfg->locals_start = cfg->num_varinfo;
7108 if (cfg->verbose_level > 2)
7109 g_print ("creating locals\n");
7110 for (i = 0; i < header->num_locals; ++i)
7111 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
7112 if (cfg->verbose_level > 2)
7113 g_print ("locals done\n");
7117 mono_print_code (MonoCompile *cfg)
7121 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7122 MonoInst *tree = bb->code;
7127 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
7129 for (; tree; tree = tree->next) {
7130 mono_print_tree (tree);
7135 bb->last_ins->next = NULL;
7139 extern const char * const mono_burg_rule_string [];
7142 emit_state (MonoCompile *cfg, MBState *state, int goal)
7145 int ern = mono_burg_rule (state, goal);
7146 const guint16 *nts = mono_burg_nts [ern];
7149 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
7153 // state->reg1 = state->reg2; /* chain rule */
7155 #ifdef MONO_ARCH_ENABLE_EMIT_STATE_OPT
7158 state->reg1 = mono_regstate_next_int (cfg->rs);
7159 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
7162 state->reg1 = mono_regstate_next_int (cfg->rs);
7163 state->reg2 = mono_regstate_next_int (cfg->rs);
7166 state->reg1 = mono_regstate_next_float (cfg->rs);
7169 #ifdef MONO_ARCH_ENABLE_EMIT_STATE_OPT
7171 * Enabling this might cause bugs to surface in the local register
7172 * allocators on some architectures like x86.
7174 if ((state->tree->ssa_op == MONO_SSA_STORE) && (state->left->tree->opcode == OP_REGVAR)) {
7175 /* Do not optimize away reg-reg moves */
7176 if (! ((state->right->tree->ssa_op == MONO_SSA_LOAD) && (state->right->left->tree->opcode == OP_REGVAR))) {
7177 state->right->reg1 = state->left->tree->dreg;
7186 mono_burg_kids (state, ern, kids);
7188 emit_state (cfg, kids [0], nts [0]);
7190 emit_state (cfg, kids [1], nts [1]);
7192 g_assert (!nts [3]);
7193 emit_state (cfg, kids [2], nts [2]);
7198 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
7199 if ((emit = mono_burg_func [ern]))
7200 emit (state, state->tree, cfg);
7203 #define DEBUG_SELECTION
7206 mini_select_instructions (MonoCompile *cfg)
7208 static const int reverse_map [] = {
7209 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
7210 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
7212 static const int reverse_fmap [] = {
7213 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
7214 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
7216 static const int reverse_lmap [] = {
7217 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
7218 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
7220 static const int reverse_imap [] = {
7221 OP_IBNE_UN, OP_IBLT, OP_IBLE, OP_IBGT, OP_IBGE,
7222 OP_IBEQ, OP_IBLT_UN, OP_IBLE_UN, OP_IBGT_UN, OP_IBGE_UN
7227 cfg->state_pool = mono_mempool_new ();
7228 cfg->rs = mono_regstate_new ();
7230 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7231 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins) &&
7232 bb->next_bb != bb->last_ins->inst_false_bb) {
7234 /* we are careful when inverting, since bugs like #59580
7235 * could show up when dealing with NaNs.
7237 if (MONO_IS_COND_BRANCH_NOFP(bb->last_ins) && bb->next_bb == bb->last_ins->inst_true_bb) {
7238 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
7239 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
7240 bb->last_ins->inst_false_bb = tmp;
7242 if (bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
7243 bb->last_ins->opcode = reverse_map [bb->last_ins->opcode - CEE_BEQ];
7244 } else if (bb->last_ins->opcode >= OP_FBEQ && bb->last_ins->opcode <= OP_FBLT_UN) {
7245 bb->last_ins->opcode = reverse_fmap [bb->last_ins->opcode - OP_FBEQ];
7246 } else if (bb->last_ins->opcode >= OP_LBEQ && bb->last_ins->opcode <= OP_LBLT_UN) {
7247 bb->last_ins->opcode = reverse_lmap [bb->last_ins->opcode - OP_LBEQ];
7248 } else if (bb->last_ins->opcode >= OP_IBEQ && bb->last_ins->opcode <= OP_IBLT_UN) {
7249 bb->last_ins->opcode = reverse_imap [bb->last_ins->opcode - OP_IBEQ];
7252 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
7253 inst->opcode = CEE_BR;
7254 inst->inst_target_bb = bb->last_ins->inst_false_bb;
7255 mono_bblock_add_inst (bb, inst);
7260 #ifdef DEBUG_SELECTION
7261 if (cfg->verbose_level >= 4) {
7262 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7263 MonoInst *tree = bb->code;
7264 g_print ("DUMP BLOCK %d:\n", bb->block_num);
7267 for (; tree; tree = tree->next) {
7268 mono_print_tree (tree);
7275 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7276 MonoInst *tree = bb->code, *next;
7282 bb->last_ins = NULL;
7285 mono_regstate_reset (cfg->rs);
7287 #ifdef DEBUG_SELECTION
7288 if (cfg->verbose_level >= 3)
7289 g_print ("LABEL BLOCK %d:\n", bb->block_num);
7291 for (; tree; tree = next) {
7293 #ifdef DEBUG_SELECTION
7294 if (cfg->verbose_level >= 3) {
7295 mono_print_tree (tree);
7300 if (!(mbstate = mono_burg_label (tree, cfg))) {
7301 g_warning ("unable to label tree %p", tree);
7302 mono_print_tree (tree);
7304 g_assert_not_reached ();
7306 emit_state (cfg, mbstate, MB_NTERM_stmt);
7308 bb->max_ireg = cfg->rs->next_vireg;
7309 bb->max_freg = cfg->rs->next_vfreg;
7312 bb->last_ins->next = NULL;
7314 mono_mempool_empty (cfg->state_pool);
7316 mono_mempool_destroy (cfg->state_pool);
7320 mono_codegen (MonoCompile *cfg)
7322 MonoJumpInfo *patch_info;
7324 int i, max_epilog_size;
7327 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7328 cfg->spill_count = 0;
7329 /* we reuse dfn here */
7330 /* bb->dfn = bb_count++; */
7331 mono_arch_local_regalloc (cfg, bb);
7334 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
7335 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
7337 code = mono_arch_emit_prolog (cfg);
7339 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7340 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
7342 cfg->code_len = code - cfg->native_code;
7343 cfg->prolog_end = cfg->code_len;
7345 mono_debug_open_method (cfg);
7347 /* emit code all basic blocks */
7348 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7349 bb->native_offset = cfg->code_len;
7350 mono_arch_output_basic_block (cfg, bb);
7352 cfg->bb_exit->native_offset = cfg->code_len;
7354 code = cfg->native_code + cfg->code_len;
7356 max_epilog_size = mono_arch_max_epilog_size (cfg);
7358 /* we always allocate code in cfg->domain->code_mp to increase locality */
7359 cfg->code_size = cfg->code_len + max_epilog_size;
7360 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
7362 if (cfg->method->dynamic) {
7363 MonoJitDynamicMethodInfo *dyn_ji;
7365 /* Allocate the code into a separate memory pool so it can be freed */
7366 dyn_ji = g_new0 (MonoJitDynamicMethodInfo, 1);
7367 dyn_ji->code_mp = mono_code_manager_new_dynamic ();
7368 mono_dynamic_code_hash_insert (cfg->method, dyn_ji);
7370 code = mono_code_manager_reserve (dyn_ji->code_mp, cfg->code_size);
7373 mono_domain_lock (cfg->domain);
7374 code = mono_code_manager_reserve (cfg->domain->code_mp, cfg->code_size);
7375 mono_domain_unlock (cfg->domain);
7378 memcpy (code, cfg->native_code, cfg->code_len);
7379 g_free (cfg->native_code);
7380 cfg->native_code = code;
7381 code = cfg->native_code + cfg->code_len;
7383 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
7385 cfg->epilog_begin = cfg->code_len;
7387 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7388 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
7390 cfg->code_len = code - cfg->native_code;
7392 mono_arch_emit_epilog (cfg);
7394 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7395 switch (patch_info->type) {
7396 case MONO_PATCH_INFO_ABS: {
7397 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
7399 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
7400 if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) &&
7401 strstr (cfg->method->name, info->name))
7403 * This is an icall wrapper, and this is a call to the
7408 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
7409 patch_info->data.name = info->name;
7413 MonoVTable *vtable = mono_find_class_init_trampoline_by_addr (patch_info->data.target);
7415 patch_info->type = MONO_PATCH_INFO_CLASS_INIT;
7416 patch_info->data.klass = vtable->klass;
7421 case MONO_PATCH_INFO_SWITCH: {
7423 if (cfg->method->dynamic)
7424 table = mono_code_manager_reserve (mono_dynamic_code_hash_lookup (cfg->method)->code_mp, sizeof (gpointer) * patch_info->table_size);
7426 mono_domain_lock (cfg->domain);
7427 table = mono_code_manager_reserve (cfg->domain->code_mp, sizeof (gpointer) * patch_info->table_size);
7428 mono_domain_unlock (cfg->domain);
7431 patch_info->ip.i = patch_info->ip.label->inst_c0;
7432 for (i = 0; i < patch_info->table_size; i++) {
7433 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
7435 patch_info->data.target = table;
7444 if (cfg->verbose_level > 0)
7445 g_print ("Method %s emitted at %p to %p [%s]\n",
7446 mono_method_full_name (cfg->method, TRUE),
7447 cfg->native_code, cfg->native_code + cfg->code_len, cfg->domain->friendly_name);
7449 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info, cfg->run_cctors);
7451 if (cfg->method->dynamic) {
7452 mono_code_manager_commit (mono_dynamic_code_hash_lookup (cfg->method)->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
7455 mono_domain_lock (cfg->domain);
7456 mono_code_manager_commit (cfg->domain->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
7457 mono_domain_unlock (cfg->domain);
7460 mono_arch_flush_icache (cfg->native_code, cfg->code_len);
7462 mono_debug_close_method (cfg);
7466 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
7471 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
7472 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
7474 if (cp->opcode == OP_ICONST) {
7475 if (cfg->opt & MONO_OPT_CONSPROP) {
7476 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
7480 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
7481 if (cfg->opt & MONO_OPT_COPYPROP) {
7482 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
7488 arity = mono_burg_arity [tree->opcode];
7491 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
7492 if (cfg->opt & MONO_OPT_CFOLD)
7493 mono_constant_fold_inst (tree, NULL);
7494 /* The opcode may have changed */
7495 if (mono_burg_arity [tree->opcode] > 1) {
7496 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
7497 if (cfg->opt & MONO_OPT_CFOLD)
7498 mono_constant_fold_inst (tree, NULL);
7500 mono_constant_fold_inst (tree, NULL);
7506 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
7510 switch (tree->opcode) {
7520 if (tree->ssa_op == MONO_SSA_NOP) {
7521 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7538 case OP_VOIDCALL_REG:
7539 case OP_VOIDCALLVIRT:
7541 MonoCallInst *call = (MonoCallInst *)tree;
7542 MonoMethodSignature *sig = call->signature;
7543 int i, byref = FALSE;
7545 for (i = 0; i < sig->param_count; i++) {
7546 if (sig->params [i]->byref) {
7553 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7561 arity = mono_burg_arity [tree->opcode];
7567 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7570 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7571 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
7574 g_assert_not_reached ();
7579 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
7581 MonoInst *tree = bb->code;
7587 for (; tree; tree = tree->next) {
7589 mono_cprop_copy_values (cfg, tree, acp);
7591 mono_cprop_invalidate_values (tree, acp, acp_size);
7593 if (tree->ssa_op == MONO_SSA_STORE &&
7594 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
7595 MonoInst *i1 = tree->inst_i1;
7597 acp [tree->inst_i0->inst_c0] = NULL;
7599 for (i = 0; i < acp_size; i++) {
7600 if (acp [i] && acp [i]->opcode != OP_ICONST &&
7601 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
7606 if (i1->opcode == OP_ICONST) {
7607 acp [tree->inst_i0->inst_c0] = i1;
7608 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
7610 if (i1->ssa_op == MONO_SSA_LOAD &&
7611 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
7612 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
7613 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
7614 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
7619 if (tree->opcode == CEE_BEQ) {
7620 g_assert (tree->inst_i0->opcode == OP_COMPARE);
7621 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
7622 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
7624 tree->opcode = CEE_BR;
7625 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
7626 tree->inst_target_bb = tree->inst_true_bb;
7628 tree->inst_target_bb = tree->inst_false_bb;
7637 mono_local_cprop (MonoCompile *cfg)
7642 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
7644 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7645 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
7646 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
7651 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, int parts)
7653 MonoMethodHeader *header = mono_method_get_header (method);
7654 guint8 *ip = (guint8 *)header->code;
7657 int dfn = 0, i, code_size_ratio;
7659 mono_jit_stats.methods_compiled++;
7660 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
7661 mono_profiler_method_jit (method);
7663 cfg = g_new0 (MonoCompile, 1);
7664 cfg->method = method;
7665 cfg->mempool = mono_mempool_new ();
7667 cfg->prof_options = mono_profiler_get_events ();
7668 cfg->run_cctors = run_cctors;
7669 cfg->bb_hash = g_hash_table_new (NULL, NULL);
7670 cfg->domain = domain;
7671 cfg->verbose_level = mini_verbose;
7672 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
7673 mono_method_get_header (method)->max_stack);
7675 if (cfg->verbose_level > 2)
7676 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
7679 * create MonoInst* which represents arguments and local variables
7681 mono_compile_create_vars (cfg);
7683 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
7684 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7685 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
7686 mono_destroy_compile (cfg);
7690 mono_jit_stats.basic_blocks += cfg->num_bblocks;
7691 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
7693 if (cfg->num_varinfo > 2000) {
7695 * we disable some optimizations if there are too many variables
7696 * because JIT time may become too expensive. The actual number needs
7697 * to be tweaked and eventually the non-linear algorithms should be fixed.
7699 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
7700 cfg->disable_ssa = TRUE;
7702 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
7704 /* Depth-first ordering on basic blocks */
7705 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
7707 if (cfg->opt & MONO_OPT_BRANCH)
7708 optimize_branches (cfg);
7710 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
7711 if (cfg->num_bblocks != dfn + 1) {
7714 cfg->num_bblocks = dfn + 1;
7716 if (!header->clauses) {
7717 /* remove unreachable code, because the code in them may be
7718 * inconsistent (access to dead variables for example) */
7719 for (bb = cfg->bb_entry; bb;) {
7720 MonoBasicBlock *bbn = bb->next_bb;
7722 if (bbn && bbn->region == -1 && !bbn->dfn) {
7723 if (cfg->verbose_level > 1)
7724 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
7725 bb->next_bb = bbn->next_bb;
7726 nullify_basic_block (bbn);
7734 if (cfg->opt & MONO_OPT_LOOP) {
7735 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
7736 mono_compute_natural_loops (cfg);
7739 /* after method_to_ir */
7743 //#define DEBUGSSA "logic_run"
7744 #define DEBUGSSA_CLASS "Tests"
7747 if (!header->num_clauses && !cfg->disable_ssa) {
7748 mono_local_cprop (cfg);
7749 mono_ssa_compute (cfg);
7753 /* fixme: add all optimizations which requires SSA */
7754 if (cfg->opt & (MONO_OPT_DEADCE | MONO_OPT_ABCREM)) {
7755 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
7756 mono_local_cprop (cfg);
7757 mono_ssa_compute (cfg);
7759 if (cfg->verbose_level >= 2) {
7766 /* after SSA translation */
7770 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
7771 if (cfg->comp_done & MONO_COMP_SSA) {
7772 mono_ssa_cprop (cfg);
7774 mono_local_cprop (cfg);
7778 if (cfg->comp_done & MONO_COMP_SSA) {
7779 mono_ssa_deadce (cfg);
7781 //mono_ssa_strength_reduction (cfg);
7783 if ((cfg->flags & MONO_CFG_HAS_LDELEMA) && (cfg->opt & MONO_OPT_ABCREM))
7784 mono_perform_abc_removal (cfg);
7786 mono_ssa_remove (cfg);
7788 if (cfg->opt & MONO_OPT_BRANCH)
7789 optimize_branches (cfg);
7792 /* after SSA removal */
7796 decompose_pass (cfg);
7798 if (cfg->opt & MONO_OPT_LINEARS) {
7801 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
7802 cfg->comp_done &= ~MONO_COMP_LIVENESS;
7803 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
7804 mono_analyze_liveness (cfg);
7806 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
7807 regs = mono_arch_get_global_int_regs (cfg);
7808 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
7812 //mono_print_code (cfg);
7816 /* variables are allocated after decompose, since decompose could create temps */
7817 mono_arch_allocate_vars (cfg);
7819 if (cfg->opt & MONO_OPT_CFOLD)
7820 mono_constant_fold (cfg);
7822 mini_select_instructions (cfg);
7825 if (cfg->verbose_level >= 2) {
7826 char *id = mono_method_full_name (cfg->method, FALSE);
7827 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
7831 if (cfg->method->dynamic)
7832 jinfo = g_new0 (MonoJitInfo, 1);
7834 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
7836 jinfo->method = method;
7837 jinfo->code_start = cfg->native_code;
7838 jinfo->code_size = cfg->code_len;
7839 jinfo->used_regs = cfg->used_int_regs;
7840 jinfo->domain_neutral = (cfg->opt & MONO_OPT_SHARED) != 0;
7842 if (header->num_clauses) {
7845 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
7846 jinfo->num_clauses = header->num_clauses;
7847 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
7848 sizeof (MonoJitExceptionInfo) * header->num_clauses);
7850 for (i = 0; i < header->num_clauses; i++) {
7851 MonoExceptionClause *ec = &header->clauses [i];
7852 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
7853 MonoBasicBlock *tblock;
7855 ei->flags = ec->flags;
7857 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7858 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->data.filter_offset);
7860 ei->data.filter = cfg->native_code + tblock->native_offset;
7862 ei->data.catch_class = ec->data.catch_class;
7865 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
7867 ei->try_start = cfg->native_code + tblock->native_offset;
7868 g_assert (tblock->native_offset);
7869 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
7871 ei->try_end = cfg->native_code + tblock->native_offset;
7872 g_assert (tblock->native_offset);
7873 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
7875 ei->handler_start = cfg->native_code + tblock->native_offset;
7879 cfg->jit_info = jinfo;
7881 mono_jit_info_table_add (cfg->domain, jinfo);
7883 if (cfg->method->dynamic)
7884 mono_dynamic_code_hash_lookup (cfg->method)->ji = jinfo;
7886 /* collect statistics */
7887 mono_jit_stats.allocated_code_size += cfg->code_len;
7888 code_size_ratio = cfg->code_len;
7889 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
7890 mono_jit_stats.biggest_method_size = code_size_ratio;
7891 mono_jit_stats.biggest_method = method;
7893 code_size_ratio = (code_size_ratio * 100) / mono_method_get_header (method)->code_size;
7894 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
7895 mono_jit_stats.max_code_size_ratio = code_size_ratio;
7896 mono_jit_stats.max_ratio_method = method;
7898 mono_jit_stats.native_code_size += cfg->code_len;
7900 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7901 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
7907 mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain)
7910 GHashTable *jit_code_hash;
7911 gpointer code = NULL;
7917 jit_code_hash = target_domain->jit_code_hash;
7919 #ifdef MONO_USE_AOT_COMPILER
7920 if (!mono_compile_aot && (opt & MONO_OPT_AOT)) {
7922 MonoDomain *domain = mono_domain_get ();
7924 mono_domain_lock (domain);
7926 mono_class_init (method->klass);
7927 if ((info = mono_aot_get_method (domain, method))) {
7928 g_hash_table_insert (domain->jit_code_hash, method, info);
7929 mono_domain_unlock (domain);
7930 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
7931 return info->code_start;
7934 mono_domain_unlock (domain);
7938 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
7939 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
7941 MonoMethodPInvoke* piinfo = (MonoMethodPInvoke *) method;
7943 if (!piinfo->addr) {
7944 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
7945 piinfo->addr = mono_lookup_internal_call (method);
7947 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
7948 mono_lookup_pinvoke_call (method, NULL, NULL);
7950 nm = mono_marshal_get_native_wrapper (method);
7951 return mono_compile_method (nm);
7953 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
7954 //mono_debug_add_wrapper (method, nm);
7955 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
7956 const char *name = method->name;
7959 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
7960 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
7961 /* FIXME: uhm, we need a wrapper to handle exceptions? */
7962 return (gpointer)mono_delegate_ctor;
7963 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
7964 nm = mono_marshal_get_delegate_invoke (method);
7965 return mono_jit_compile_method (nm);
7966 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
7967 nm = mono_marshal_get_delegate_begin_invoke (method);
7968 return mono_jit_compile_method (nm);
7969 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
7970 nm = mono_marshal_get_delegate_end_invoke (method);
7971 return mono_jit_compile_method (nm);
7977 cfg = mini_method_compile (method, opt, target_domain, TRUE, 0);
7979 mono_domain_lock (target_domain);
7981 /* Check if some other thread already did the job. In this case, we can
7982 discard the code this thread generated. */
7984 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
7985 /* We can't use a domain specific method in another domain */
7986 if ((target_domain == mono_domain_get ()) || info->domain_neutral) {
7987 code = info->code_start;
7988 // printf("Discarding code for method %s\n", method->name);
7993 g_hash_table_insert (jit_code_hash, method, cfg->jit_info);
7994 code = cfg->native_code;
7997 mono_destroy_compile (cfg);
7999 if (target_domain->jump_target_hash) {
8000 MonoJumpInfo patch_info;
8002 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
8004 patch_info.next = NULL;
8005 patch_info.ip.i = 0;
8006 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
8007 patch_info.data.method = method;
8008 g_hash_table_remove (target_domain->jump_target_hash, method);
8010 for (tmp = list; tmp; tmp = tmp->next)
8011 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info, TRUE);
8012 g_slist_free (list);
8015 mono_domain_unlock (target_domain);
8017 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
8022 mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt)
8024 /* FIXME: later copy the code from mono */
8025 MonoDomain *target_domain, *domain = mono_domain_get ();
8029 if (opt & MONO_OPT_SHARED)
8030 target_domain = mono_get_root_domain ();
8032 target_domain = domain;
8034 mono_domain_lock (target_domain);
8036 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
8037 /* We can't use a domain specific method in another domain */
8038 if (! ((domain != target_domain) && !info->domain_neutral)) {
8039 mono_domain_unlock (target_domain);
8040 mono_jit_stats.methods_lookups++;
8041 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
8042 return info->code_start;
8046 mono_domain_unlock (target_domain);
8047 p = mono_jit_compile_method_inner (method, target_domain);
8052 mono_jit_compile_method (MonoMethod *method)
8054 return mono_jit_compile_method_with_opt (method, default_opt);
8058 invalidated_delegate_trampoline (MonoClass *klass)
8060 g_error ("Unmanaged code called delegate of type %s which was already garbage collected.\n"
8061 "See http://www.go-mono.com/delegate.html for an explanation and ways to fix this.",
8062 mono_type_full_name (&klass->byval_arg));
8066 * mono_jit_free_method:
8068 * Free all memory allocated by the JIT for METHOD.
8071 mono_jit_free_method (MonoMethod *method)
8073 MonoJitDynamicMethodInfo *ji;
8075 g_assert (method->dynamic);
8077 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
8078 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
8080 * Instead of freeing the code, change it to call an error routine
8081 * so people can fix their code.
8083 EnterCriticalSection (&jit_mutex);
8084 ji = g_hash_table_lookup (dynamic_code_hash, method);
8085 LeaveCriticalSection (&jit_mutex);
8087 char *type = mono_type_full_name (&method->klass->byval_arg);
8088 char *type_and_method = g_strdup_printf ("%s.%s", type, method->name);
8091 mono_arch_invalidate_method (ji->ji, invalidated_delegate_trampoline, type_and_method);
8097 EnterCriticalSection (&jit_mutex);
8098 ji = g_hash_table_lookup (dynamic_code_hash, method);
8100 LeaveCriticalSection (&jit_mutex);
8103 g_hash_table_remove (dynamic_code_hash, ji);
8104 LeaveCriticalSection (&jit_mutex);
8106 mono_code_manager_destroy (ji->code_mp);
8107 mono_jit_info_table_remove (mono_domain_get (), ji->ji);
8113 mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method)
8115 MonoDomain *target_domain;
8118 if (default_opt & MONO_OPT_SHARED)
8119 target_domain = mono_get_root_domain ();
8121 target_domain = domain;
8123 mono_domain_lock (target_domain);
8125 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
8126 /* We can't use a domain specific method in another domain */
8127 if (! ((domain != target_domain) && !info->domain_neutral)) {
8128 mono_domain_unlock (target_domain);
8129 mono_jit_stats.methods_lookups++;
8130 return info->code_start;
8134 mono_domain_unlock (target_domain);
8140 * mono_jit_runtime_invoke:
8141 * @method: the method to invoke
8142 * @obj: this pointer
8143 * @params: array of parameter values.
8144 * @exc: used to catch exceptions objects
8147 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
8150 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc, void* compiled_method);
8151 void* compiled_method;
8153 if (obj == NULL && !(method->flags & METHOD_ATTRIBUTE_STATIC) && !method->string_ctor && (method->wrapper_type == 0)) {
8154 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
8158 invoke = mono_marshal_get_runtime_invoke (method);
8159 runtime_invoke = mono_jit_compile_method (invoke);
8161 /* We need this here becuase mono_marshal_get_runtime_invoke can be place
8162 * the helper method in System.Object and not the target class
8164 mono_runtime_class_init (mono_class_vtable (mono_domain_get (), method->klass));
8166 compiled_method = mono_jit_compile_method (method);
8167 return runtime_invoke (obj, params, exc, compiled_method);
8170 #ifdef PLATFORM_WIN32
8171 #define GET_CONTEXT \
8172 struct sigcontext *ctx = (struct sigcontext*)_dummy;
8175 #define GET_CONTEXT \
8176 void *ctx = context;
8177 #elif defined(sun) // Solaris x86
8178 #define GET_CONTEXT \
8179 ucontext_t *uctx = context; \
8180 struct sigcontext *ctx = (struct sigcontext *)&(uctx->uc_mcontext);
8181 #elif defined(__ppc__) || defined (__powerpc__) || defined (__s390__) || defined (MONO_ARCH_USE_SIGACTION)
8182 #define GET_CONTEXT \
8183 void *ctx = context;
8185 #define GET_CONTEXT \
8186 void **_p = (void **)&_dummy; \
8187 struct sigcontext *ctx = (struct sigcontext *)++_p;
8191 #ifdef MONO_ARCH_USE_SIGACTION
8192 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, siginfo_t *info, void *context)
8194 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy)
8198 SIG_HANDLER_SIGNATURE (sigfpe_signal_handler)
8200 MonoException *exc = NULL;
8203 #if defined(MONO_ARCH_HAVE_IS_INT_OVERFLOW)
8204 if (mono_arch_is_int_overflow (ctx))
8205 exc = mono_get_exception_arithmetic ();
8207 exc = mono_get_exception_divide_by_zero ();
8209 exc = mono_get_exception_divide_by_zero ();
8212 mono_arch_handle_exception (ctx, exc, FALSE);
8216 SIG_HANDLER_SIGNATURE (sigill_signal_handler)
8220 exc = mono_get_exception_execution_engine ("SIGILL");
8222 mono_arch_handle_exception (ctx, exc, FALSE);
8225 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8228 sigsegv_signal_handler (int _dummy, siginfo_t *info, void *context)
8231 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
8232 struct sigcontext *ctx = (struct sigcontext *)&(((ucontext_t*)context)->uc_mcontext);
8234 /* Can't allocate memory using Boehm GC on altstack */
8235 if (jit_tls->stack_size &&
8236 ((guint8*)info->si_addr >= (guint8*)jit_tls->end_of_stack - jit_tls->stack_size) &&
8237 ((guint8*)info->si_addr < (guint8*)jit_tls->end_of_stack))
8238 exc = mono_domain_get ()->stack_overflow_ex;
8240 exc = mono_domain_get ()->null_reference_ex;
8242 mono_arch_handle_exception (ctx, exc, FALSE);
8248 SIG_HANDLER_SIGNATURE (sigsegv_signal_handler)
8252 mono_arch_handle_exception (ctx, NULL, FALSE);
8258 SIG_HANDLER_SIGNATURE (sigusr1_signal_handler)
8260 gboolean running_managed;
8265 running_managed = (mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx)) != NULL);
8267 exc = mono_thread_request_interruption (running_managed);
8270 mono_arch_handle_exception (ctx, exc, FALSE);
8274 SIG_HANDLER_SIGNATURE (sigquit_signal_handler)
8279 exc = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
8281 mono_arch_handle_exception (ctx, exc, FALSE);
8285 SIG_HANDLER_SIGNATURE (sigint_signal_handler)
8290 exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
8292 mono_arch_handle_exception (ctx, exc, FALSE);
8295 #ifndef PLATFORM_WIN32
8297 add_signal_handler (int signo, gpointer handler)
8299 struct sigaction sa;
8301 #ifdef MONO_ARCH_USE_SIGACTION
8302 sa.sa_sigaction = handler;
8303 sigemptyset (&sa.sa_mask);
8304 sa.sa_flags = SA_SIGINFO;
8306 sa.sa_handler = handler;
8307 sigemptyset (&sa.sa_mask);
8310 g_assert (sigaction (signo, &sa, NULL) != -1);
8315 mono_runtime_install_handlers (void)
8317 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8318 struct sigaction sa;
8321 #ifdef PLATFORM_WIN32
8323 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
8324 win32_seh_set_handler(SIGILL, sigill_signal_handler);
8325 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
8326 if (getenv ("MONO_DEBUG"))
8327 win32_seh_set_handler(SIGINT, sigint_signal_handler);
8328 #else /* !PLATFORM_WIN32 */
8330 /* libpthreads has its own implementation of sigaction(),
8331 * but it seems to work well with our current exception
8332 * handlers. If not we must call syscall directly instead
8335 if (getenv ("MONO_DEBUG")) {
8336 add_signal_handler (SIGINT, sigint_signal_handler);
8339 add_signal_handler (SIGFPE, sigfpe_signal_handler);
8340 add_signal_handler (SIGQUIT, sigquit_signal_handler);
8341 add_signal_handler (SIGILL, sigill_signal_handler);
8342 add_signal_handler (SIGBUS, sigsegv_signal_handler);
8343 add_signal_handler (mono_thread_get_abort_signal (), sigusr1_signal_handler);
8346 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8347 sa.sa_sigaction = sigsegv_signal_handler;
8348 sigemptyset (&sa.sa_mask);
8349 sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
8350 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
8352 add_signal_handler (SIGSEGV, sigsegv_signal_handler);
8355 #endif /* PLATFORM_WIN32 */
8358 /* mono_jit_create_remoting_trampoline:
8359 * @method: pointer to the method info
8361 * Creates a trampoline which calls the remoting functions. This
8362 * is used in the vtable of transparent proxies.
8364 * Returns: a pointer to the newly created code
8367 mono_jit_create_remoting_trampoline (MonoMethod *method, MonoRemotingTarget target)
8370 guint8 *addr = NULL;
8372 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
8373 (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
8374 nm = mono_marshal_get_remoting_invoke_for_target (method, target);
8375 addr = mono_compile_method (nm);
8377 addr = mono_compile_method (method);
8383 mini_init (const char *filename)
8387 mono_arch_cpu_init ();
8389 if (!g_thread_supported ())
8390 g_thread_init (NULL);
8392 MONO_GC_PRE_INIT ();
8394 mono_jit_tls_id = TlsAlloc ();
8395 setup_jit_tls_data ((gpointer)-1, mono_thread_abort);
8397 InitializeCriticalSection (&jit_mutex);
8401 if (default_opt & MONO_OPT_AOT)
8404 mono_runtime_install_handlers ();
8405 mono_threads_install_cleanup (mini_thread_cleanup);
8407 #define JIT_TRAMPOLINES_WORK
8408 #ifdef JIT_TRAMPOLINES_WORK
8409 mono_install_compile_method (mono_jit_compile_method);
8410 mono_install_free_method (mono_jit_free_method);
8411 mono_install_trampoline (mono_create_jit_trampoline);
8412 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
8414 #define JIT_INVOKE_WORKS
8415 #ifdef JIT_INVOKE_WORKS
8416 mono_install_runtime_invoke (mono_jit_runtime_invoke);
8417 mono_install_handler (mono_arch_get_throw_exception ());
8419 mono_install_stack_walk (mono_jit_walk_stack);
8421 domain = mono_init_from_assembly (filename, filename);
8424 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
8425 ves_icall_get_frame_info);
8426 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
8427 ves_icall_get_trace);
8428 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
8429 mono_runtime_install_handlers);
8432 create_helper_signature ();
8434 #define JIT_CALLS_WORK
8435 #ifdef JIT_CALLS_WORK
8436 /* Needs to be called here since register_jit_icall depends on it */
8437 mono_marshal_init ();
8439 mono_arch_register_lowlevel_calls ();
8440 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
8441 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
8442 mono_register_jit_icall (mono_trace_enter_method, "mono_trace_enter_method", NULL, TRUE);
8443 mono_register_jit_icall (mono_trace_leave_method, "mono_trace_leave_method", NULL, TRUE);
8444 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
8445 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
8447 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
8448 #ifdef MONO_ARCH_HAVE_RETHROW
8449 mono_register_jit_icall (mono_arch_get_rethrow_exception (), "mono_arch_rethrow_exception", helper_sig_void_obj, TRUE);
8451 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
8452 helper_sig_void_ptr, TRUE);
8453 mono_register_jit_icall (mono_thread_get_pending_exception, "mono_thread_get_pending_exception", helper_sig_obj_void, FALSE);
8454 mono_register_jit_icall (mono_thread_interruption_checkpoint, "mono_thread_interruption_checkpoint", helper_sig_void_void, FALSE);
8455 mono_register_jit_icall (mono_load_remote_field_new, "mono_load_remote_field_new", helper_sig_obj_obj_ptr_ptr, FALSE);
8456 mono_register_jit_icall (mono_store_remote_field_new, "mono_store_remote_field_new", helper_sig_void_obj_ptr_ptr_obj, FALSE);
8459 * NOTE, NOTE, NOTE, NOTE:
8460 * when adding emulation for some opcodes, remember to also add a dummy
8461 * rule to the burg files, because we need the arity information to be correct.
8463 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult, TRUE);
8464 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un, FALSE);
8465 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf, FALSE);
8466 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv, FALSE);
8467 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un, FALSE);
8468 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem, FALSE);
8469 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un, FALSE);
8471 #ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
8472 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl, TRUE);
8473 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr, TRUE);
8474 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un, TRUE);
8477 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8, FALSE);
8478 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4, FALSE);
8479 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8, FALSE);
8480 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8, FALSE);
8482 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
8483 mono_register_opcode_emulation (OP_FCONV_TO_I8, "__emul_fconv_to_i8", helper_sig_long_double, mono_fconv_i8, FALSE);
8485 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
8486 mono_register_opcode_emulation (CEE_CONV_R_UN, "__emul_conv_r_un", helper_sig_double_int, mono_conv_to_r8_un, FALSE);
8488 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
8489 mono_register_opcode_emulation (OP_LCONV_TO_R8, "__emul_lconv_to_r8", helper_sig_double_long, mono_lconv_to_r8, FALSE);
8491 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
8492 mono_register_opcode_emulation (OP_LCONV_TO_R4, "__emul_lconv_to_r4", helper_sig_float_long, mono_lconv_to_r4, FALSE);
8494 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
8495 mono_register_opcode_emulation (OP_LCONV_TO_R_UN, "__emul_lconv_to_r8_un", helper_sig_double_long, mono_lconv_to_r8_un, FALSE);
8497 #ifdef MONO_ARCH_EMULATE_FREM
8498 mono_register_opcode_emulation (OP_FREM, "__emul_frem", helper_sig_double_double_double, fmod, FALSE);
8501 #if SIZEOF_VOID_P == 4
8502 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4, TRUE);
8505 #warning "fixme: add opcode emulation"
8509 /* other jit icalls */
8510 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
8511 helper_sig_ptr_ptr_ptr, FALSE);
8512 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
8513 mono_register_jit_icall (mono_get_special_static_data, "mono_get_special_static_data", helper_sig_ptr_int, FALSE);
8514 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
8515 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
8516 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
8517 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
8518 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
8519 mono_register_jit_icall (helper_stelem_ref_check, "helper_stelem_ref_check", helper_sig_stelem_ref_check, FALSE);
8520 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
8521 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
8522 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
8523 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
8524 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
8525 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
8526 mono_register_jit_icall (mono_ldftn_nosync, "mono_ldftn_nosync", helper_sig_compile, FALSE);
8527 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
8530 #define JIT_RUNTIME_WORKS
8531 #ifdef JIT_RUNTIME_WORKS
8532 mono_runtime_install_cleanup ((MonoDomainFunc)mini_cleanup);
8533 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
8536 mono_thread_attach (domain);
8540 MonoJitStats mono_jit_stats = {0};
8543 print_jit_stats (void)
8545 if (mono_jit_stats.enabled) {
8546 g_print ("Mono Jit statistics\n");
8547 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
8548 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
8549 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
8550 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
8551 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
8552 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
8553 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
8554 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
8555 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
8556 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
8557 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
8558 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
8559 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
8560 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
8561 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
8562 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
8563 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
8564 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
8566 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
8567 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
8568 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
8569 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
8570 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
8572 g_print ("\nGeneric instances: %ld\n", mono_stats.generic_instance_count);
8573 g_print ("Inflated methods: %ld\n", mono_stats.inflated_method_count);
8574 g_print ("Inflated types: %ld\n", mono_stats.inflated_type_count);
8575 g_print ("Generics metadata size: %ld\n", mono_stats.generics_metadata_size);
8580 mini_cleanup (MonoDomain *domain)
8583 * mono_runtime_cleanup() and mono_domain_finalize () need to
8584 * be called early since they need the execution engine still
8585 * fully working (mono_domain_finalize may invoke managed finalizers
8586 * and mono_runtime_cleanup will wait for other threads to finish).
8588 mono_domain_finalize (domain, 2000);
8590 mono_runtime_cleanup (domain);
8592 mono_profiler_shutdown ();
8594 mono_debug_cleanup ();
8596 #ifdef PLATFORM_WIN32
8597 win32_seh_cleanup();
8600 mono_domain_free (domain, TRUE);
8606 mono_set_defaults (int verbose_level, guint32 opts)
8608 mini_verbose = verbose_level;
8613 mono_precompile_assembly (MonoAssembly *ass, void *user_data)
8615 MonoImage *image = mono_assembly_get_image (ass);
8616 MonoMethod *method, *invoke;
8619 if (mini_verbose > 0)
8620 printf ("PRECOMPILE: %s.\n", mono_image_get_filename (image));
8622 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
8623 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
8624 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
8628 if (mini_verbose > 1) {
8629 char * desc = mono_method_full_name (method, TRUE);
8630 g_print ("Compiling %d %s\n", count, desc);
8633 mono_compile_method (method);
8634 if (strcmp (method->name, "Finalize") == 0) {
8635 invoke = mono_marshal_get_runtime_invoke (method);
8636 mono_compile_method (invoke);
8638 if (method->klass->marshalbyref && method->signature->hasthis) {
8639 invoke = mono_marshal_get_remoting_invoke_with_check (method);
8640 mono_compile_method (invoke);
8645 void mono_precompile_assemblies ()
8647 mono_assembly_foreach ((GFunc)mono_precompile_assembly, NULL);