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>
56 #include "jit-icalls.c"
59 * this is used to determine when some branch optimizations are possible: we exclude FP compares
60 * because they have weird semantics with NaNs.
62 #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))
63 #define MONO_IS_COND_BRANCH_NOFP(ins) (MONO_IS_COND_BRANCH_OP(ins) && (ins)->inst_left->inst_left->type != STACK_R8)
65 #define MONO_CHECK_THIS(ins) (cfg->method->signature->hasthis && (ins)->ssa_op == MONO_SSA_LOAD && (ins)->inst_left->inst_c0 == 0)
67 gboolean mono_arch_print_tree(MonoInst *tree, int arity);
68 static gpointer mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt);
69 static gpointer mono_jit_compile_method (MonoMethod *method);
70 static gpointer mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method);
72 static void handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src,
73 const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native);
75 static void dec_foreach (MonoInst *tree, MonoCompile *cfg);
77 static int mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
78 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
79 guint inline_offset, gboolean is_virtual_call);
81 extern guint8 mono_burg_arity [];
82 /* helper methods signature */
83 static MonoMethodSignature *helper_sig_long_long_long = NULL;
84 static MonoMethodSignature *helper_sig_long_long_int = NULL;
85 static MonoMethodSignature *helper_sig_newarr = NULL;
86 static MonoMethodSignature *helper_sig_newarr_specific = NULL;
87 static MonoMethodSignature *helper_sig_ldstr = NULL;
88 static MonoMethodSignature *helper_sig_domain_get = NULL;
89 static MonoMethodSignature *helper_sig_object_new = NULL;
90 static MonoMethodSignature *helper_sig_object_new_specific = NULL;
91 static MonoMethodSignature *helper_sig_compile = NULL;
92 static MonoMethodSignature *helper_sig_compile_virt = NULL;
93 static MonoMethodSignature *helper_sig_obj_ptr = NULL;
94 static MonoMethodSignature *helper_sig_obj_ptr_ptr = NULL;
95 static MonoMethodSignature *helper_sig_obj_obj_ptr_ptr = NULL;
96 static MonoMethodSignature *helper_sig_obj_void = NULL;
97 static MonoMethodSignature *helper_sig_ptr_void = NULL;
98 static MonoMethodSignature *helper_sig_void_void = NULL;
99 static MonoMethodSignature *helper_sig_void_ptr = NULL;
100 static MonoMethodSignature *helper_sig_void_obj = NULL;
101 static MonoMethodSignature *helper_sig_void_obj_ptr_int = NULL;
102 static MonoMethodSignature *helper_sig_void_obj_ptr_ptr_obj = NULL;
103 static MonoMethodSignature *helper_sig_void_ptr_ptr = NULL;
104 static MonoMethodSignature *helper_sig_void_ptr_ptr_ptr = NULL;
105 static MonoMethodSignature *helper_sig_ptr_ptr_ptr = NULL;
106 static MonoMethodSignature *helper_sig_ptr_obj = NULL;
107 static MonoMethodSignature *helper_sig_ptr_obj_int = NULL;
108 static MonoMethodSignature *helper_sig_ptr_int = NULL;
109 static MonoMethodSignature *helper_sig_initobj = NULL;
110 static MonoMethodSignature *helper_sig_memcpy = NULL;
111 static MonoMethodSignature *helper_sig_memset = NULL;
112 static MonoMethodSignature *helper_sig_ulong_double = NULL;
113 static MonoMethodSignature *helper_sig_long_double = NULL;
114 static MonoMethodSignature *helper_sig_double_long = NULL;
115 static MonoMethodSignature *helper_sig_double_int = NULL;
116 static MonoMethodSignature *helper_sig_float_long = NULL;
117 static MonoMethodSignature *helper_sig_double_double_double = NULL;
118 static MonoMethodSignature *helper_sig_uint_double = NULL;
119 static MonoMethodSignature *helper_sig_int_double = NULL;
120 static MonoMethodSignature *helper_sig_stelem_ref = NULL;
121 static MonoMethodSignature *helper_sig_stelem_ref_check = NULL;
122 static MonoMethodSignature *helper_sig_class_init_trampoline = NULL;
124 static guint32 default_opt = MONO_OPT_PEEPHOLE;
126 guint32 mono_jit_tls_id = -1;
127 MonoTraceSpec *mono_jit_trace_calls = NULL;
128 gboolean mono_break_on_exc = FALSE;
129 gboolean mono_compile_aot = FALSE;
131 static int mini_verbose = 0;
133 static CRITICAL_SECTION trampoline_hash_mutex;
135 static GHashTable *class_init_hash_addr = NULL;
137 static GHashTable *jump_trampoline_hash = NULL;
140 mono_running_on_valgrind (void)
142 #ifdef HAVE_VALGRIND_MEMCHECK_H
143 if (RUNNING_ON_VALGRIND)
153 G_GNUC_UNUSED static void
154 print_method_from_ip (void *ip)
159 MonoDomain *domain = mono_domain_get ();
161 ji = mono_jit_info_table_find (domain, ip);
163 g_print ("No method at %p\n", ip);
166 method = mono_method_full_name (ji->method, TRUE);
167 source = mono_debug_source_location_from_address (ji->method, (int) ip, NULL, domain);
169 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);
172 g_print ("%s\n", source);
180 mono_print_method_from_ip (void *ip)
182 print_method_from_ip (ip);
186 * mono_method_same_domain:
188 * Determine whenever two compiled methods are in the same domain, thus
189 * the address of the callee can be embedded in the caller.
191 gboolean mono_method_same_domain (MonoJitInfo *caller, MonoJitInfo *callee)
193 if (!caller || !callee)
197 * If the call was made from domain-neutral to domain-specific
198 * code, we can't patch the call site.
200 if (caller->domain_neutral && !callee->domain_neutral)
203 if ((caller->method->klass == mono_defaults.appdomain_class) &&
204 (strstr (caller->method->name, "InvokeInDomain"))) {
205 /* The InvokeInDomain methods change the current appdomain */
213 mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token)
215 MonoJumpInfoToken *res = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoToken));
222 #define MONO_INIT_VARINFO(vi,id) do { \
223 (vi)->range.first_use.pos.bid = 0xffff; \
229 * Basic blocks have two numeric identifiers:
230 * dfn: Depth First Number
231 * block_num: unique ID assigned at bblock creation
233 #define NEW_BBLOCK(cfg) (mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)))
234 #define ADD_BBLOCK(cfg,bbhash,b) do { \
235 g_hash_table_insert (bbhash, (b)->cil_code, (b)); \
236 (b)->block_num = cfg->num_bblocks++; \
237 (b)->real_offset = real_offset; \
240 #define GET_BBLOCK(cfg,bbhash,tblock,ip) do { \
241 (tblock) = g_hash_table_lookup (bbhash, (ip)); \
243 if ((ip) >= end || (ip) < header->code) goto unverified; \
244 (tblock) = NEW_BBLOCK (cfg); \
245 (tblock)->cil_code = (ip); \
246 ADD_BBLOCK (cfg, (bbhash), (tblock)); \
250 #define CHECK_BBLOCK(target,ip,tblock) do { \
251 if ((target) < (ip) && !(tblock)->code) { \
252 bb_recheck = g_list_prepend (bb_recheck, (tblock)); \
253 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); \
257 #define NEW_ICONST(cfg,dest,val) do { \
258 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
259 (dest)->opcode = OP_ICONST; \
260 (dest)->inst_c0 = (val); \
261 (dest)->type = STACK_I4; \
264 #if SIZEOF_VOID_P == 8
265 #define NEW_PCONST(cfg,dest,val) do { \
266 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
267 (dest)->opcode = OP_I8CONST; \
268 (dest)->inst_p0 = (val); \
269 (dest)->type = STACK_PTR; \
272 #define NEW_PCONST(cfg,dest,val) do { \
273 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
274 (dest)->opcode = OP_ICONST; \
275 (dest)->inst_p0 = (val); \
276 (dest)->type = STACK_PTR; \
280 #if SIZEOF_VOID_P == 8
281 #define OP_PCONST OP_I8CONST
283 #define OP_PCONST OP_ICONST
286 #define NEW_CLASSCONST(cfg,dest,val) do { \
287 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
288 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_PCONST; \
289 (dest)->inst_p0 = (val); \
290 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_CLASS; \
291 (dest)->type = STACK_PTR; \
294 #define NEW_IMAGECONST(cfg,dest,val) do { \
295 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
296 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_PCONST; \
297 (dest)->inst_p0 = (val); \
298 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_IMAGE; \
299 (dest)->type = STACK_PTR; \
302 #define NEW_FIELDCONST(cfg,dest,field) do { \
303 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
304 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_PCONST; \
305 (dest)->inst_p0 = (field); \
306 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_FIELD; \
307 (dest)->type = STACK_PTR; \
310 #define NEW_METHODCONST(cfg,dest,val) do { \
311 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
312 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_PCONST; \
313 (dest)->inst_p0 = (val); \
314 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_METHODCONST; \
315 (dest)->type = STACK_PTR; \
318 #define NEW_VTABLECONST(cfg,dest,vtable) do { \
319 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
320 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_PCONST; \
321 (dest)->inst_p0 = mono_compile_aot ? (gpointer)((vtable)->klass) : (vtable); \
322 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_VTABLE; \
323 (dest)->type = STACK_PTR; \
326 #define NEW_SFLDACONST(cfg,dest,field) do { \
327 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
328 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_PCONST; \
329 (dest)->inst_p0 = (field); \
330 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_SFLDA; \
331 (dest)->type = STACK_PTR; \
334 #define NEW_LDSTRCONST(cfg,dest,image,token) do { \
335 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
336 (dest)->opcode = OP_AOTCONST; \
337 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
338 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_LDSTR; \
339 (dest)->type = STACK_OBJ; \
342 #define NEW_TYPE_FROM_HANDLE_CONST(cfg,dest,image,token) do { \
343 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
344 (dest)->opcode = OP_AOTCONST; \
345 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
346 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_TYPE_FROM_HANDLE; \
347 (dest)->type = STACK_OBJ; \
350 #define NEW_LDTOKENCONST(cfg,dest,image,token) do { \
351 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
352 (dest)->opcode = OP_AOTCONST; \
353 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
354 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_LDTOKEN; \
355 (dest)->type = STACK_PTR; \
358 #define NEW_DOMAINCONST(cfg,dest) do { \
359 if (cfg->opt & MONO_OPT_SHARED) { \
360 NEW_TEMPLOAD (cfg, dest, mono_get_domainvar (cfg)->inst_c0); \
362 NEW_PCONST (cfg, dest, (cfg)->domain); \
366 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
368 #define NEW_ARGLOAD(cfg,dest,num) do { \
369 if (arg_array [(num)]->opcode == OP_ICONST) (dest) = arg_array [(num)]; else { \
370 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
371 (dest)->ssa_op = MONO_SSA_LOAD; \
372 (dest)->inst_i0 = arg_array [(num)]; \
373 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
374 type_to_eval_stack_type (param_types [(num)], (dest)); \
375 (dest)->klass = (dest)->inst_i0->klass; \
378 #define NEW_LOCLOAD(cfg,dest,num) do { \
379 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
380 (dest)->ssa_op = MONO_SSA_LOAD; \
381 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
382 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
383 type_to_eval_stack_type (header->locals [(num)], (dest)); \
384 (dest)->klass = (dest)->inst_i0->klass; \
387 #define NEW_LOCLOADA(cfg,dest,num) do { \
388 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
389 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
390 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
391 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
392 (dest)->opcode = OP_LDADDR; \
393 (dest)->type = STACK_MP; \
394 (dest)->klass = (dest)->inst_i0->klass; \
395 if (!MONO_TYPE_ISSTRUCT (header->locals [(num)])) \
396 (cfg)->disable_ssa = TRUE; \
399 #define NEW_RETLOADA(cfg,dest) do { \
400 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
401 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
402 (dest)->inst_i0 = (cfg)->ret; \
403 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
404 (dest)->opcode = CEE_LDIND_I; \
405 (dest)->type = STACK_MP; \
406 (dest)->klass = (dest)->inst_i0->klass; \
407 (cfg)->disable_ssa = TRUE; \
410 #define NEW_ARGLOADA(cfg,dest,num) do { \
411 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
412 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
413 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
414 (dest)->inst_i0 = arg_array [(num)]; \
415 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
416 (dest)->opcode = OP_LDADDR; \
417 (dest)->type = STACK_MP; \
418 (dest)->klass = (dest)->inst_i0->klass; \
419 (cfg)->disable_ssa = TRUE; \
422 #define NEW_TEMPLOAD(cfg,dest,num) do { \
423 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
424 (dest)->ssa_op = MONO_SSA_LOAD; \
425 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
426 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
427 type_to_eval_stack_type ((dest)->inst_i0->inst_vtype, (dest)); \
428 (dest)->klass = (dest)->inst_i0->klass; \
431 #define NEW_TEMPLOADA(cfg,dest,num) do { \
432 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
433 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
434 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
435 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
436 (dest)->opcode = OP_LDADDR; \
437 (dest)->type = STACK_MP; \
438 (dest)->klass = (dest)->inst_i0->klass; \
439 if (!MONO_TYPE_ISSTRUCT (cfg->varinfo [(num)]->inst_vtype)) \
440 (cfg)->disable_ssa = TRUE; \
444 #define NEW_INDLOAD(cfg,dest,addr,vtype) do { \
445 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
446 (dest)->inst_left = addr; \
447 (dest)->opcode = mono_type_to_ldind (vtype); \
448 type_to_eval_stack_type (vtype, (dest)); \
449 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
452 #define NEW_INDSTORE(cfg,dest,addr,value,vtype) do { \
453 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
454 (dest)->inst_i0 = addr; \
455 (dest)->opcode = mono_type_to_stind (vtype); \
456 (dest)->inst_i1 = (value); \
457 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
460 #define NEW_TEMPSTORE(cfg,dest,num,inst) do { \
461 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
462 (dest)->ssa_op = MONO_SSA_STORE; \
463 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
464 (dest)->opcode = mono_type_to_stind ((dest)->inst_i0->inst_vtype); \
465 (dest)->inst_i1 = (inst); \
466 (dest)->klass = (dest)->inst_i0->klass; \
469 #define NEW_LOCSTORE(cfg,dest,num,inst) do { \
470 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
471 (dest)->opcode = mono_type_to_stind (header->locals [(num)]); \
472 (dest)->ssa_op = MONO_SSA_STORE; \
473 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
474 (dest)->inst_i1 = (inst); \
475 (dest)->klass = (dest)->inst_i0->klass; \
478 #define NEW_ARGSTORE(cfg,dest,num,inst) do { \
479 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
480 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
481 (dest)->opcode = mono_type_to_stind (param_types [(num)]); \
482 (dest)->ssa_op = MONO_SSA_STORE; \
483 (dest)->inst_i0 = arg_array [(num)]; \
484 (dest)->inst_i1 = (inst); \
485 (dest)->klass = (dest)->inst_i0->klass; \
488 #define ADD_BINOP(op) do { \
489 MONO_INST_NEW (cfg, ins, (op)); \
490 ins->cil_code = ip; \
492 ins->inst_i0 = sp [0]; \
493 ins->inst_i1 = sp [1]; \
495 type_from_op (ins); \
499 #define ADD_UNOP(op) do { \
500 MONO_INST_NEW (cfg, ins, (op)); \
501 ins->cil_code = ip; \
503 ins->inst_i0 = sp [0]; \
505 type_from_op (ins); \
509 #define ADD_BINCOND(next_block) do { \
512 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
513 cmp->inst_i0 = sp [0]; \
514 cmp->inst_i1 = sp [1]; \
515 cmp->cil_code = ins->cil_code; \
516 type_from_op (cmp); \
518 ins->inst_i0 = cmp; \
519 MONO_ADD_INS (bblock, ins); \
520 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
521 GET_BBLOCK (cfg, bbhash, tblock, target); \
522 link_bblock (cfg, bblock, tblock); \
523 ins->inst_true_bb = tblock; \
524 CHECK_BBLOCK (target, ip, tblock); \
525 if ((next_block)) { \
526 link_bblock (cfg, bblock, (next_block)); \
527 ins->inst_false_bb = (next_block); \
528 start_new_bblock = 1; \
530 GET_BBLOCK (cfg, bbhash, tblock, ip); \
531 link_bblock (cfg, bblock, tblock); \
532 ins->inst_false_bb = tblock; \
533 start_new_bblock = 2; \
537 /* FIXME: handle float, long ... */
538 #define ADD_UNCOND(istrue) do { \
541 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
542 cmp->inst_i0 = sp [0]; \
543 switch (cmp->inst_i0->type) { \
545 cmp->inst_i1 = zero_int64; break; \
547 cmp->inst_i1 = zero_r8; break; \
550 cmp->inst_i1 = zero_ptr; break; \
552 cmp->inst_i1 = zero_obj; break; \
554 cmp->inst_i1 = zero_int32; \
556 cmp->cil_code = ins->cil_code; \
557 type_from_op (cmp); \
559 ins->inst_i0 = cmp; \
560 ins->opcode = (istrue)? CEE_BNE_UN: CEE_BEQ; \
561 MONO_ADD_INS (bblock, ins); \
562 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
563 GET_BBLOCK (cfg, bbhash, tblock, target); \
564 link_bblock (cfg, bblock, tblock); \
565 ins->inst_true_bb = tblock; \
566 CHECK_BBLOCK (target, ip, tblock); \
567 GET_BBLOCK (cfg, bbhash, tblock, ip); \
568 link_bblock (cfg, bblock, tblock); \
569 ins->inst_false_bb = tblock; \
570 start_new_bblock = 2; \
573 #define NEW_LDELEMA(cfg,dest,sp,k) do { \
574 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
575 (dest)->opcode = CEE_LDELEMA; \
576 (dest)->inst_left = (sp) [0]; \
577 (dest)->inst_right = (sp) [1]; \
578 (dest)->type = STACK_MP; \
579 (dest)->klass = (k); \
580 (cfg)->flags |= MONO_CFG_HAS_LDELEMA; \
583 #define NEW_GROUP(cfg,dest,el1,el2) do { \
584 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
585 (dest)->opcode = OP_GROUP; \
586 (dest)->inst_left = (el1); \
587 (dest)->inst_right = (el2); \
592 compare_bblock (gconstpointer a, gconstpointer b)
594 const MonoBasicBlock *b1 = a;
595 const MonoBasicBlock *b2 = b;
597 return b2->cil_code - b1->cil_code;
602 * link_bblock: Links two basic blocks
604 * links two basic blocks in the control flow graph, the 'from'
605 * argument is the starting block and the 'to' argument is the block
606 * the control flow ends to after 'from'.
609 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
611 MonoBasicBlock **newa;
615 if (from->cil_code) {
617 g_print ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
619 g_print ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
622 g_print ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
624 g_print ("edge from entry to exit\n");
628 for (i = 0; i < from->out_count; ++i) {
629 if (to == from->out_bb [i]) {
635 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
636 for (i = 0; i < from->out_count; ++i) {
637 newa [i] = from->out_bb [i];
645 for (i = 0; i < to->in_count; ++i) {
646 if (from == to->in_bb [i]) {
652 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
653 for (i = 0; i < to->in_count; ++i) {
654 newa [i] = to->in_bb [i];
663 * mono_find_block_region:
665 * We mark each basic block with a region ID. We use that to avoid BB
666 * optimizations when blocks are in different regions.
669 * A region token that encodes where this region is, and information
670 * about the clause owner for this block.
672 * The region encodes the try/catch/filter clause that owns this block
673 * as well as the type. -1 is a special value that represents a block
674 * that is in none of try/catch/filter.
677 mono_find_block_region (MonoCompile *cfg, int offset, int *filter_lengths)
679 MonoMethod *method = cfg->method;
680 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
681 MonoExceptionClause *clause;
684 /* first search for handlers and filters */
685 for (i = 0; i < header->num_clauses; ++i) {
686 clause = &header->clauses [i];
687 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->token_or_filter) &&
688 (offset < (clause->token_or_filter + filter_lengths [i])))
689 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
691 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
692 if (clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)
693 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
695 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
699 /* search the try blocks */
700 for (i = 0; i < header->num_clauses; ++i) {
701 clause = &header->clauses [i];
702 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
703 return ((i + 1) << 8) | clause->flags;
710 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
712 MonoMethod *method = cfg->method;
713 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
714 MonoExceptionClause *clause;
715 MonoBasicBlock *handler;
719 for (i = 0; i < header->num_clauses; ++i) {
720 clause = &header->clauses [i];
721 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
722 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
723 if (clause->flags == type) {
724 handler = g_hash_table_lookup (cfg->bb_hash, header->code + clause->handler_offset);
726 res = g_list_append (res, handler);
734 mono_find_spvar_for_region (MonoCompile *cfg, int region)
736 return g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
740 mono_create_spvar_for_region (MonoCompile *cfg, int region)
744 var = g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
748 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
749 /* prevent it from being register allocated */
750 var->flags |= MONO_INST_INDIRECT;
752 g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
756 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
760 array [*dfn] = start;
761 /*g_print ("visit %d at %p\n", *dfn, start->cil_code);*/
762 for (i = 0; i < start->out_count; ++i) {
763 if (start->out_bb [i]->dfn)
766 start->out_bb [i]->dfn = *dfn;
767 start->out_bb [i]->df_parent = start;
768 array [*dfn] = start->out_bb [i];
769 df_visit (start->out_bb [i], dfn, array);
775 MonoBasicBlock *best;
779 previous_foreach (gconstpointer key, gpointer val, gpointer data)
781 PrevStruct *p = data;
782 MonoBasicBlock *bb = val;
783 //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,
784 //bb->method == p->best->method, bb->cil_code < p->code, bb->cil_code > p->best->cil_code);
786 if (bb->cil_code && bb->cil_code < p->code && bb->cil_code > p->best->cil_code)
790 static MonoBasicBlock*
791 find_previous (GHashTable *bb_hash, MonoBasicBlock *start, const guchar *code) {
797 g_hash_table_foreach (bb_hash, (GHFunc)previous_foreach, &p);
802 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
811 * FIXME: take into account all the details:
812 * second may have been the target of more than one bblock
814 second->out_count = first->out_count;
815 second->out_bb = first->out_bb;
817 for (i = 0; i < first->out_count; ++i) {
818 bb = first->out_bb [i];
819 for (j = 0; j < bb->in_count; ++j) {
820 if (bb->in_bb [j] == first)
821 bb->in_bb [j] = second;
825 first->out_count = 0;
826 first->out_bb = NULL;
827 link_bblock (cfg, first, second);
829 second->last_ins = first->last_ins;
831 /*g_print ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
832 for (inst = first->code; inst && inst->next; inst = inst->next) {
833 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
834 g_print ("found %p: %s", inst->next->cil_code, code);
836 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
837 second->code = inst->next;
839 first->last_ins = inst;
840 second->next_bb = first->next_bb;
841 first->next_bb = second;
846 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
852 mono_type_to_ldind (MonoType *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 t = type->data.klass->enum_basetype->type;
899 case MONO_TYPE_TYPEDBYREF:
901 case MONO_TYPE_GENERICINST:
902 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE)
904 return CEE_LDIND_REF;
906 g_error ("unknown type 0x%02x in type_to_ldind", type->type);
912 mono_type_to_stind (MonoType *type)
923 case MONO_TYPE_BOOLEAN:
935 case MONO_TYPE_FNPTR:
937 case MONO_TYPE_CLASS:
938 case MONO_TYPE_STRING:
939 case MONO_TYPE_OBJECT:
940 case MONO_TYPE_SZARRAY:
941 case MONO_TYPE_ARRAY:
942 return CEE_STIND_REF;
950 case MONO_TYPE_VALUETYPE:
951 if (type->data.klass->enumtype) {
952 t = type->data.klass->enum_basetype->type;
956 case MONO_TYPE_TYPEDBYREF:
958 case MONO_TYPE_GENERICINST:
959 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE)
961 return CEE_STIND_REF;
963 g_error ("unknown type 0x%02x in type_to_stind", type->type);
969 * Returns the type used in the eval stack when @type is loaded.
970 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
973 type_to_eval_stack_type (MonoType *type, MonoInst *inst) {
977 inst->type = STACK_MP;
985 case MONO_TYPE_BOOLEAN:
991 inst->type = STACK_I4;
996 case MONO_TYPE_FNPTR:
997 inst->type = STACK_PTR;
999 case MONO_TYPE_CLASS:
1000 case MONO_TYPE_STRING:
1001 case MONO_TYPE_OBJECT:
1002 case MONO_TYPE_SZARRAY:
1003 case MONO_TYPE_ARRAY:
1004 inst->type = STACK_OBJ;
1008 inst->type = STACK_I8;
1012 inst->type = STACK_R8;
1014 case MONO_TYPE_VALUETYPE:
1015 if (type->data.klass->enumtype) {
1016 t = type->data.klass->enum_basetype->type;
1019 inst->klass = type->data.klass;
1020 inst->type = STACK_VTYPE;
1023 case MONO_TYPE_TYPEDBYREF:
1024 inst->klass = mono_defaults.typed_reference_class;
1025 inst->type = STACK_VTYPE;
1027 case MONO_TYPE_GENERICINST:
1028 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE) {
1029 inst->klass = mono_class_from_mono_type (type);
1030 inst->type = STACK_VTYPE;
1032 inst->type = STACK_OBJ;
1036 g_error ("unknown type 0x%02x in eval stack type", type->type);
1041 * The following tables are used to quickly validate the IL code in type_from_op ().
1044 bin_num_table [STACK_MAX] [STACK_MAX] = {
1045 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1046 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1047 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1048 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1049 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
1050 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
1051 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1052 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1057 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
1060 /* reduce the size of this table */
1062 bin_int_table [STACK_MAX] [STACK_MAX] = {
1063 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1064 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1065 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1066 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1067 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1068 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1069 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1070 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1074 bin_comp_table [STACK_MAX] [STACK_MAX] = {
1076 {0, 1, 0, 1, 0, 0, 4, 0},
1077 {0, 0, 1, 0, 0, 0, 0, 0},
1078 {0, 1, 0, 1, 0, 2, 4, 0},
1079 {0, 0, 0, 0, 1, 0, 0, 0},
1080 {0, 0, 0, 2, 0, 1, 0, 0},
1081 {0, 4, 0, 4, 0, 0, 3, 0},
1082 {0, 0, 0, 0, 0, 0, 0, 0},
1085 /* reduce the size of this table */
1087 shift_table [STACK_MAX] [STACK_MAX] = {
1088 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1089 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1090 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1091 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1092 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1093 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1094 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1095 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1099 * Tables to map from the non-specific opcode to the matching
1100 * type-specific opcode.
1102 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
1103 static const guint16
1104 binops_op_map [STACK_MAX] = {
1105 0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, 0
1108 /* handles from CEE_NEG to CEE_CONV_U8 */
1109 static const guint16
1110 unops_op_map [STACK_MAX] = {
1111 0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, 0
1114 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
1115 static const guint16
1116 ovfops_op_map [STACK_MAX] = {
1117 0, 0, OP_LCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_FCONV_TO_U2-CEE_CONV_U2, 0
1120 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
1121 static const guint16
1122 ovf2ops_op_map [STACK_MAX] = {
1123 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, 0
1126 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
1127 static const guint16
1128 ovf3ops_op_map [STACK_MAX] = {
1129 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, 0
1132 /* handles from CEE_CEQ to CEE_CLT_UN */
1133 static const guint16
1134 ceqops_op_map [STACK_MAX] = {
1135 0, 0, OP_LCEQ-CEE_CEQ, OP_PCEQ-CEE_CEQ, OP_FCEQ-CEE_CEQ, 0
1139 * Sets ins->type (the type on the eval stack) according to the
1140 * type of the opcode and the arguments to it.
1141 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1143 * FIXME: this function sets ins->type unconditionally in some cases, but
1144 * it should set it to invalid for some types (a conv.x on an object)
1147 type_from_op (MonoInst *ins) {
1148 switch (ins->opcode) {
1155 /* FIXME: check unverifiable args for STACK_MP */
1156 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1157 ins->opcode += binops_op_map [ins->type];
1164 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1165 ins->opcode += binops_op_map [ins->type];
1170 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1171 ins->opcode += binops_op_map [ins->type];
1175 /* FIXME: handle some specifics with ins->next->type */
1176 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1177 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))))
1178 ins->opcode = OP_LCOMPARE;
1185 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1186 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1190 ins->type = neg_table [ins->inst_i0->type];
1191 ins->opcode += unops_op_map [ins->type];
1194 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1195 ins->type = ins->inst_i0->type;
1197 ins->type = STACK_INV;
1198 ins->opcode += unops_op_map [ins->type];
1204 ins->type = STACK_I4;
1205 ins->opcode += unops_op_map [ins->inst_i0->type];
1208 ins->type = STACK_R8;
1209 switch (ins->inst_i0->type) {
1214 ins->opcode = OP_LCONV_TO_R_UN;
1218 case CEE_CONV_OVF_I1:
1219 case CEE_CONV_OVF_U1:
1220 case CEE_CONV_OVF_I2:
1221 case CEE_CONV_OVF_U2:
1222 case CEE_CONV_OVF_I4:
1223 case CEE_CONV_OVF_U4:
1224 ins->type = STACK_I4;
1225 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1227 case CEE_CONV_OVF_I_UN:
1228 case CEE_CONV_OVF_U_UN:
1229 ins->type = STACK_PTR;
1230 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1232 case CEE_CONV_OVF_I1_UN:
1233 case CEE_CONV_OVF_I2_UN:
1234 case CEE_CONV_OVF_I4_UN:
1235 case CEE_CONV_OVF_U1_UN:
1236 case CEE_CONV_OVF_U2_UN:
1237 case CEE_CONV_OVF_U4_UN:
1238 ins->type = STACK_I4;
1239 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1242 ins->type = STACK_PTR;
1243 switch (ins->inst_i0->type) {
1249 ins->opcode = OP_LCONV_TO_U;
1252 ins->opcode = OP_FCONV_TO_U;
1258 ins->type = STACK_I8;
1259 ins->opcode += unops_op_map [ins->inst_i0->type];
1261 case CEE_CONV_OVF_I8:
1262 case CEE_CONV_OVF_U8:
1263 ins->type = STACK_I8;
1264 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1266 case CEE_CONV_OVF_U8_UN:
1267 case CEE_CONV_OVF_I8_UN:
1268 ins->type = STACK_I8;
1269 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1273 ins->type = STACK_R8;
1274 ins->opcode += unops_op_map [ins->inst_i0->type];
1277 ins->type = STACK_R8;
1281 ins->type = STACK_I4;
1282 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1285 case CEE_CONV_OVF_I:
1286 case CEE_CONV_OVF_U:
1287 ins->type = STACK_PTR;
1288 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1291 case CEE_ADD_OVF_UN:
1293 case CEE_MUL_OVF_UN:
1295 case CEE_SUB_OVF_UN:
1296 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1297 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1300 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1307 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_MP, STACK_R8, STACK_R8, STACK_OBJ
1310 /* map ldelem.x to the matching ldind.x opcode */
1312 ldelem_to_ldind [] = {
1326 /* map stelem.x to the matching stind.x opcode */
1328 stelem_to_stind [] = {
1342 param_table [STACK_MAX] [STACK_MAX] = {
1347 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1351 switch (args->type) {
1361 for (i = 0; i < sig->param_count; ++i) {
1362 switch (args [i].type) {
1366 if (!sig->params [i]->byref)
1370 if (sig->params [i]->byref)
1372 switch (sig->params [i]->type) {
1373 case MONO_TYPE_CLASS:
1374 case MONO_TYPE_STRING:
1375 case MONO_TYPE_OBJECT:
1376 case MONO_TYPE_SZARRAY:
1377 case MONO_TYPE_ARRAY:
1384 if (sig->params [i]->byref)
1386 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1395 /*if (!param_table [args [i].type] [sig->params [i]->type])
1403 * When we need a pointer to the current domain many times in a method, we
1404 * call mono_domain_get() once and we store the result in a local variable.
1405 * This function returns the variable that represents the MonoDomain*.
1407 inline static MonoInst *
1408 mono_get_domainvar (MonoCompile *cfg)
1410 if (!cfg->domainvar)
1411 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1412 return cfg->domainvar;
1416 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
1419 int num = cfg->num_varinfo;
1421 if ((num + 1) >= cfg->varinfo_count) {
1422 cfg->varinfo_count = (cfg->varinfo_count + 2) * 2;
1423 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
1424 cfg->vars = (MonoMethodVar **)g_realloc (cfg->vars, sizeof (MonoMethodVar*) * cfg->varinfo_count);
1427 /*g_print ("created temp %d of type 0x%x\n", num, type->type);*/
1428 mono_jit_stats.allocate_var++;
1430 MONO_INST_NEW (cfg, inst, opcode);
1431 inst->inst_c0 = num;
1432 inst->inst_vtype = type;
1433 inst->klass = mono_class_from_mono_type (type);
1434 /* if set to 1 the variable is native */
1437 cfg->varinfo [num] = inst;
1439 cfg->vars [num] = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMethodVar));
1440 MONO_INIT_VARINFO (cfg->vars [num], num);
1443 //g_print ("created temp %d of type %s\n", num, mono_type_get_name (type));
1448 type_from_stack_type (MonoInst *ins) {
1449 switch (ins->type) {
1450 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1451 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1452 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1453 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1454 case STACK_MP: return &mono_defaults.int_class->byval_arg;
1455 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1456 case STACK_VTYPE: return &ins->klass->byval_arg;
1458 g_error ("stack type %d to montype not handled\n", ins->type);
1464 array_access_to_klass (int opcode)
1468 return mono_defaults.byte_class;
1470 return mono_defaults.uint16_class;
1473 return mono_defaults.int_class;
1476 return mono_defaults.sbyte_class;
1479 return mono_defaults.int16_class;
1482 return mono_defaults.int32_class;
1484 return mono_defaults.uint32_class;
1487 return mono_defaults.int64_class;
1490 return mono_defaults.single_class;
1493 return mono_defaults.double_class;
1494 case CEE_LDELEM_REF:
1495 case CEE_STELEM_REF:
1496 return mono_defaults.object_class;
1498 g_assert_not_reached ();
1504 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
1508 MONO_ADD_INS (bb, inst);
1511 switch (bb->last_ins->opcode) {
1525 while (prev->next && prev->next != bb->last_ins)
1527 if (prev == bb->code) {
1528 if (bb->last_ins == bb->code) {
1529 inst->next = bb->code;
1532 inst->next = prev->next;
1536 inst->next = bb->last_ins;
1540 // g_warning ("handle conditional jump in add_ins_to_end ()\n");
1542 MONO_ADD_INS (bb, inst);
1548 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
1550 MonoInst *inst, *load;
1552 NEW_TEMPLOAD (cfg, load, src);
1554 NEW_TEMPSTORE (cfg, inst, dest, load);
1555 if (inst->opcode == CEE_STOBJ) {
1556 NEW_TEMPLOADA (cfg, inst, dest);
1557 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE);
1559 inst->cil_code = NULL;
1560 mono_add_ins_to_end (bb, inst);
1565 * We try to share variables when possible
1568 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1573 /* inlining can result in deeper stacks */
1574 if (slot >= ((MonoMethodNormal *)cfg->method)->header->max_stack)
1575 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1577 pos = ins->type - 1 + slot * STACK_MAX;
1579 switch (ins->type) {
1586 if ((vnum = cfg->intvars [pos]))
1587 return cfg->varinfo [vnum];
1588 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1589 cfg->intvars [pos] = res->inst_c0;
1592 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1598 * This function is called to handle items that are left on the evaluation stack
1599 * at basic block boundaries. What happens is that we save the values to local variables
1600 * and we reload them later when first entering the target basic block (with the
1601 * handle_loaded_temps () function).
1602 * It is also used to handle items on the stack in store opcodes, since it is
1603 * possible that the variable to be stored into is already on the stack, in
1604 * which case its old value should be used.
1605 * A single joint point will use the same variables (stored in the array bb->out_stack or
1606 * bb->in_stack, if the basic block is before or after the joint point).
1609 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count) {
1611 MonoBasicBlock *outb;
1612 MonoInst *inst, **locals;
1617 if (cfg->verbose_level > 3)
1618 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
1619 if (!bb->out_scount) {
1620 bb->out_scount = count;
1621 //g_print ("bblock %d has out:", bb->block_num);
1623 for (i = 0; i < bb->out_count; ++i) {
1624 outb = bb->out_bb [i];
1625 //g_print (" %d", outb->block_num);
1626 if (outb->in_stack) {
1628 bb->out_stack = outb->in_stack;
1634 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1635 for (i = 0; i < count; ++i) {
1636 /* see bug#58863, but removing this code causes regressions in gtk-sharp build
1637 * (SEGV running Method::Initialize() in gapi_codegen.exe)
1639 gboolean bug = FALSE;
1641 if (strstr (cfg->method->klass->name, "Method") && strstr (cfg->method->name, "Initialize")) {
1646 * try to reuse temps already allocated for this purpouse, if they occupy the same
1647 * stack slot and if they are of the same type.
1648 * This won't cause conflicts since if 'local' is used to
1649 * store one of the values in the in_stack of a bblock, then
1650 * the same variable will be used for the same outgoing stack
1652 * This doesn't work when inlining methods, since the bblocks
1653 * in the inlined methods do not inherit their in_stack from
1654 * the bblock they are inlined to. See bug #58863 for an
1657 if (cfg->inlined_method)
1658 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1660 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1665 for (i = 0; i < bb->out_count; ++i) {
1666 outb = bb->out_bb [i];
1667 if (outb->in_scount)
1668 continue; /* check they are the same locals */
1669 outb->in_scount = count;
1670 outb->in_stack = bb->out_stack;
1673 locals = bb->out_stack;
1674 for (i = 0; i < count; ++i) {
1675 /* add store ops at the end of the bb, before the branch */
1676 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1677 if (inst->opcode == CEE_STOBJ) {
1678 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
1679 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE);
1681 inst->cil_code = sp [i]->cil_code;
1682 mono_add_ins_to_end (bb, inst);
1684 if (cfg->verbose_level > 3)
1685 g_print ("storing %d to temp %d\n", i, locals [i]->inst_c0);
1689 * It is possible that the out bblocks already have in_stack assigned, and
1690 * the in_stacks differ. In this case, we will store to all the different
1697 /* Find a bblock which has a different in_stack */
1699 while (bindex < bb->out_count) {
1700 outb = bb->out_bb [bindex];
1701 if (outb->in_stack != locals) {
1703 * Instead of storing sp [i] to locals [i], we need to store
1704 * locals [i] to <new locals>[i], since the sp [i] tree can't
1705 * be shared between trees.
1707 for (i = 0; i < count; ++i)
1708 mono_add_varcopy_to_end (cfg, bb, locals [i]->inst_c0, outb->in_stack [i]->inst_c0);
1709 locals = outb->in_stack;
1721 ret_type_to_call_opcode (MonoType *type, int calli, int virt)
1726 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1730 case MONO_TYPE_VOID:
1731 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
1734 case MONO_TYPE_BOOLEAN:
1737 case MONO_TYPE_CHAR:
1740 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1744 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1745 case MONO_TYPE_CLASS:
1746 case MONO_TYPE_STRING:
1747 case MONO_TYPE_OBJECT:
1748 case MONO_TYPE_SZARRAY:
1749 case MONO_TYPE_ARRAY:
1750 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1753 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
1756 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
1757 case MONO_TYPE_VALUETYPE:
1758 if (type->data.klass->enumtype) {
1759 t = type->data.klass->enum_basetype->type;
1762 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1763 case MONO_TYPE_TYPEDBYREF:
1764 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1765 case MONO_TYPE_GENERICINST:
1766 t = type->data.generic_inst->generic_type->type;
1769 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
1775 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
1777 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
1779 ji->ip.label = label;
1780 ji->type = MONO_PATCH_INFO_SWITCH;
1781 ji->data.table = bbs;
1782 ji->next = cfg->patch_info;
1783 ji->table_size = num_blocks;
1784 cfg->patch_info = ji;
1788 * When we add a tree of instructions, we need to ensure the instructions currently
1789 * on the stack are executed before (like, if we load a value from a local).
1790 * We ensure this by saving the currently loaded values to temps and rewriting the
1791 * instructions to load the values.
1792 * This is not done for opcodes that terminate a basic block (because it's handled already
1793 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
1796 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
1798 MonoInst *load, *store, *temp, *ins;
1800 while (stack < sp) {
1802 /* handle also other constants */
1803 if (ins->opcode != OP_ICONST) {
1804 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1805 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1806 store->cil_code = ins->cil_code;
1807 if (store->opcode == CEE_STOBJ) {
1808 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
1809 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE);
1811 MONO_ADD_INS (bblock, store);
1812 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1813 load->cil_code = ins->cil_code;
1821 * Prepare arguments for passing to a function call.
1822 * Return a non-zero value if the arguments can't be passed to the given
1824 * The type checks are not yet complete and some conversions may need
1825 * casts on 32 or 64 bit architectures.
1828 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
1833 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
1837 for (i = 0; i < sig->param_count; ++i) {
1838 if (sig->params [i]->byref) {
1840 * check the result of ldelema is only passed as an argument if the byref
1841 * type matches exactly the array element type.
1842 * FIXME: if the argument as been saved on the stack as part of the
1843 * interface variable code (the value was on the stack at a basic block boundary)
1844 * we need to add the check in that case, too.
1846 if (args [i]->opcode == CEE_LDELEMA) {
1848 MonoClass *exact_class = mono_class_from_mono_type (sig->params [i]);
1849 if (!exact_class->valuetype) {
1850 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
1851 check->cil_code = args [i]->cil_code;
1852 check->klass = exact_class;
1853 check->inst_left = args [i]->inst_left;
1854 check->type = STACK_OBJ;
1855 args [i]->inst_left = check;
1858 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
1862 simple_type = sig->params [i]->type;
1864 switch (simple_type) {
1865 case MONO_TYPE_VOID:
1870 case MONO_TYPE_BOOLEAN:
1873 case MONO_TYPE_CHAR:
1876 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
1882 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
1885 case MONO_TYPE_CLASS:
1886 case MONO_TYPE_STRING:
1887 case MONO_TYPE_OBJECT:
1888 case MONO_TYPE_SZARRAY:
1889 case MONO_TYPE_ARRAY:
1890 if (args [i]->type != STACK_OBJ)
1895 if (args [i]->type != STACK_I8)
1900 if (args [i]->type != STACK_R8)
1903 case MONO_TYPE_VALUETYPE:
1904 if (sig->params [i]->data.klass->enumtype) {
1905 simple_type = sig->params [i]->data.klass->enum_basetype->type;
1908 if (args [i]->type != STACK_VTYPE)
1911 case MONO_TYPE_TYPEDBYREF:
1912 if (args [i]->type != STACK_VTYPE)
1915 case MONO_TYPE_GENERICINST:
1916 simple_type = sig->params [i]->data.generic_inst->generic_type->type;
1920 g_error ("unknown type 0x%02x in check_call_signature", simple_type);
1927 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
1928 const guint8 *ip, gboolean to_end)
1930 MonoInst *temp, *store, *ins = (MonoInst*)call;
1931 MonoType *ret = sig->ret;
1933 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
1935 call->inst.type = STACK_OBJ;
1936 call->inst.opcode = CEE_CALL;
1937 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
1939 type_to_eval_stack_type (ret, ins);
1940 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
1943 if (MONO_TYPE_ISSTRUCT (ret)) {
1946 /* we use this to allocate native sized structs */
1947 temp->unused = sig->pinvoke;
1949 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
1950 if (call->inst.opcode == OP_VCALL)
1951 ins->inst_left = loada;
1953 ins->inst_right = loada; /* a virtual or indirect call */
1956 mono_add_ins_to_end (bblock, ins);
1958 MONO_ADD_INS (bblock, ins);
1960 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1961 store->cil_code = ip;
1963 mono_add_ins_to_end (bblock, store);
1965 MONO_ADD_INS (bblock, store);
1967 return temp->inst_c0;
1970 mono_add_ins_to_end (bblock, ins);
1972 MONO_ADD_INS (bblock, ins);
1977 inline static MonoCallInst *
1978 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1979 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
1984 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual));
1986 call->inst.cil_code = ip;
1988 call->signature = sig;
1989 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
1991 for (arg = call->out_args; arg;) {
1992 MonoInst *narg = arg->next;
1997 mono_add_ins_to_end (bblock, arg);
1999 MONO_ADD_INS (bblock, arg);
2006 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
2007 MonoInst **args, MonoInst *addr, const guint8 *ip)
2009 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
2011 call->inst.inst_i0 = addr;
2013 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
2016 static MonoCallInst*
2017 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
2018 MonoInst **args, const guint8 *ip, MonoInst *this)
2020 gboolean virtual = this != NULL;
2023 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, FALSE);
2025 if (this && sig->hasthis &&
2026 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
2027 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
2028 call->method = mono_marshal_get_remoting_invoke_with_check (method);
2030 call->method = method;
2032 call->inst.flags |= MONO_INST_HAS_METHOD;
2033 call->inst.inst_left = this;
2039 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
2040 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
2042 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, signature, args, ip, this);
2044 return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
2048 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
2049 MonoInst **args, const guint8 *ip, gboolean to_end)
2055 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
2057 return mono_spill_call (cfg, bblock, call, sig, func == mono_array_new_va, ip, to_end);
2061 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
2063 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
2066 g_warning ("unregistered JIT ICall");
2067 g_assert_not_reached ();
2070 return mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, args, ip, FALSE);
2074 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
2076 MonoInst *ins, *temp = NULL, *store, *load, *begin;
2077 MonoInst *last_arg = NULL;
2081 //g_print ("emulating: ");
2082 //mono_print_tree_nl (tree);
2083 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE));
2084 ins = (MonoInst*)call;
2086 call->inst.cil_code = tree->cil_code;
2088 call->signature = info->sig;
2090 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
2092 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2093 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
2094 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2095 store->cil_code = tree->cil_code;
2100 nargs = info->sig->param_count + info->sig->hasthis;
2102 for (last_arg = call->out_args; last_arg && last_arg->next; last_arg = last_arg->next) ;
2105 last_arg->next = store;
2108 begin = call->out_args;
2112 if (cfg->prev_ins) {
2114 * This assumes that that in a tree, emulate_opcode is called for a
2115 * node before it is called for its children. dec_foreach needs to
2116 * take this into account.
2118 store->next = cfg->prev_ins->next;
2119 cfg->prev_ins->next = begin;
2121 store->next = cfg->cbb->code;
2122 cfg->cbb->code = begin;
2125 call->fptr = mono_icall_get_wrapper (info);
2127 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2128 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2133 static MonoMethodSignature *
2134 mono_get_element_address_signature (int arity)
2136 static GHashTable *sighash = NULL;
2137 MonoMethodSignature *res;
2140 EnterCriticalSection (&trampoline_hash_mutex);
2142 sighash = g_hash_table_new (NULL, NULL);
2144 else if ((res = g_hash_table_lookup (sighash, (gpointer)arity))) {
2145 LeaveCriticalSection (&trampoline_hash_mutex);
2149 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
2152 res->params [0] = &mono_defaults.array_class->byval_arg;
2154 for (i = 1; i <= arity; i++)
2155 res->params [i] = &mono_defaults.int_class->byval_arg;
2157 res->ret = &mono_defaults.int_class->byval_arg;
2159 g_hash_table_insert (sighash, (gpointer)arity, res);
2160 LeaveCriticalSection (&trampoline_hash_mutex);
2166 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native) {
2167 MonoInst *iargs [3];
2173 * This check breaks with spilled vars... need to handle it during verification anyway.
2174 * g_assert (klass && klass == src->klass && klass == dest->klass);
2178 n = mono_class_native_size (klass, &align);
2180 n = mono_class_value_size (klass, &align);
2182 if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
2184 MONO_INST_NEW (cfg, inst, OP_MEMCPY);
2185 inst->inst_left = dest;
2186 inst->inst_right = src;
2187 inst->cil_code = ip;
2189 MONO_ADD_INS (bblock, inst);
2194 NEW_ICONST (cfg, iargs [2], n);
2196 mono_emit_native_call (cfg, bblock, helper_memcpy, helper_sig_memcpy, iargs, ip, to_end);
2200 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
2202 MonoInst *iargs [2];
2203 MonoInst *ins, *zero_int32;
2206 NEW_ICONST (cfg, zero_int32, 0);
2208 mono_class_init (klass);
2209 n = mono_class_value_size (klass, NULL);
2210 MONO_INST_NEW (cfg, ins, 0);
2212 ins->inst_left = dest;
2213 ins->inst_right = zero_int32;
2216 ins->opcode = CEE_STIND_I1;
2217 MONO_ADD_INS (bblock, ins);
2220 ins->opcode = CEE_STIND_I2;
2221 MONO_ADD_INS (bblock, ins);
2224 ins->opcode = CEE_STIND_I4;
2225 MONO_ADD_INS (bblock, ins);
2228 if (n <= sizeof (gpointer) * 5) {
2229 ins->opcode = OP_MEMSET;
2232 MONO_ADD_INS (bblock, ins);
2235 handle_loaded_temps (cfg, bblock, stack_start, sp);
2236 NEW_ICONST (cfg, ins, n);
2239 mono_emit_jit_icall (cfg, bblock, helper_initobj, iargs, ip);
2245 handle_box (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *val, const guchar *ip, MonoClass *klass)
2247 MonoInst *iargs [2];
2248 MonoInst *dest, *vtoffset, *add, *vstore;
2251 /* much like NEWOBJ */
2252 if (cfg->opt & MONO_OPT_SHARED) {
2253 NEW_DOMAINCONST (cfg, iargs [0]);
2254 NEW_CLASSCONST (cfg, iargs [1], klass);
2256 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
2258 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
2259 NEW_VTABLECONST (cfg, iargs [0], vtable);
2260 if (klass->has_finalize || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
2261 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
2263 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
2265 NEW_TEMPLOAD (cfg, dest, temp);
2266 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
2267 MONO_INST_NEW (cfg, add, CEE_ADD);
2268 add->inst_left = dest;
2269 add->inst_right = vtoffset;
2272 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
2273 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
2274 vstore->cil_code = ip;
2275 vstore->inst_left = add;
2276 vstore->inst_right = val;
2278 if (vstore->opcode == CEE_STOBJ) {
2279 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
2281 MONO_ADD_INS (bblock, vstore);
2283 NEW_TEMPLOAD (cfg, dest, temp);
2287 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
2290 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
2292 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
2293 MonoMethodSignature *signature = method->signature;
2297 #ifdef MONO_ARCH_HAVE_LMF_OPS
2298 if (((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2299 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) &&
2300 !MONO_TYPE_ISSTRUCT (signature->ret) && (method->klass->parent != mono_defaults.array_class))
2304 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2305 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2306 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
2307 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
2308 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2309 (method->klass->marshalbyref) ||
2310 !header || header->num_clauses ||
2311 /* fixme: why cant we inline valuetype returns? */
2312 MONO_TYPE_ISSTRUCT (signature->ret))
2315 /* its not worth to inline methods with valuetype arguments?? */
2316 for (i = 0; i < signature->param_count; i++) {
2317 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
2323 * if we can initialize the class of the method right away, we do,
2324 * otherwise we don't allow inlining if the class needs initialization,
2325 * since it would mean inserting a call to mono_runtime_class_init()
2326 * inside the inlined code
2328 if (!(cfg->opt & MONO_OPT_SHARED)) {
2329 vtable = mono_class_vtable (cfg->domain, method->klass);
2330 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
2331 if (cfg->run_cctors)
2332 mono_runtime_class_init (vtable);
2334 else if (!vtable->initialized && mono_class_needs_cctor_run (method->klass, NULL))
2338 * If we're compiling for shared code
2339 * the cctor will need to be run at aot method load time, for example,
2340 * or at the end of the compilation of the inlining method.
2342 if (mono_class_needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
2345 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
2347 /* also consider num_locals? */
2348 if (getenv ("MONO_INLINELIMIT"))
2349 if (header->code_size < atoi (getenv ("MONO_INLINELIMIT"))) {
2353 if (header->code_size < 20)
2360 mini_get_ldelema_ins (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
2364 MonoMethodSignature *esig;
2365 char icall_name [256];
2366 MonoJitICallInfo *info;
2368 rank = cmethod->signature->param_count - (is_set? 1: 0);
2370 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
2372 NEW_GROUP (cfg, indexes, sp [1], sp [2]);
2373 MONO_INST_NEW (cfg, addr, OP_LDELEMA2D);
2374 addr->inst_left = sp [0];
2375 addr->inst_right = indexes;
2376 addr->cil_code = ip;
2377 addr->type = STACK_MP;
2378 addr->klass = cmethod->klass;
2382 /* Need to register the icall so it gets an icall wrapper */
2383 sprintf (icall_name, "ves_array_element_address_%d", rank);
2385 info = mono_find_jit_icall_by_name (icall_name);
2387 esig = mono_get_element_address_signature (rank);
2388 info = mono_register_jit_icall (ves_array_element_address, g_strdup (icall_name), esig, FALSE);
2391 temp = mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, sp, ip, FALSE);
2392 cfg->flags |= MONO_CFG_HAS_VARARGS;
2394 NEW_TEMPLOAD (cfg, addr, temp);
2398 static MonoJitICallInfo **emul_opcode_map = NULL;
2400 static inline MonoJitICallInfo *
2401 mono_find_jit_opcode_emulation (int opcode)
2403 if (emul_opcode_map)
2404 return emul_opcode_map [opcode];
2410 mini_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
2415 static MonoClass *runtime_helpers_class = NULL;
2416 if (! runtime_helpers_class)
2417 runtime_helpers_class = mono_class_from_name (mono_defaults.corlib,
2418 "System.Runtime.CompilerServices", "RuntimeHelpers");
2420 if (cmethod->klass == mono_defaults.string_class) {
2421 if (cmethod->name [0] != 'g')
2424 if (strcmp (cmethod->name, "get_Chars") == 0)
2426 else if (strcmp (cmethod->name, "get_Length") == 0)
2429 } else if (cmethod->klass == mono_defaults.object_class) {
2430 if (strcmp (cmethod->name, "GetType") == 0)
2434 } else if (cmethod->klass == mono_defaults.array_class) {
2435 if (strcmp (cmethod->name, "get_Rank") == 0)
2437 else if (strcmp (cmethod->name, "get_Length") == 0)
2441 } else if (cmethod->klass == runtime_helpers_class) {
2442 if (strcmp (cmethod->name, "get_OffsetToStringData") == 0) {
2443 NEW_ICONST (cfg, ins, G_STRUCT_OFFSET (MonoString, chars));
2448 op = mono_arch_get_opcode_for_method (cfg, cmethod, fsig, args);
2452 pc = fsig->param_count + fsig->hasthis;
2453 MONO_INST_NEW (cfg, ins, op);
2456 ins->inst_i0 = args [0];
2458 ins->inst_i1 = args [1];
2465 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
2467 MonoInst *store, *temp;
2470 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
2472 if (!sig->hasthis && sig->param_count == 0)
2476 if (sp [0]->opcode == OP_ICONST) {
2479 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
2481 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2482 store->cil_code = sp [0]->cil_code;
2483 MONO_ADD_INS (bblock, store);
2488 for (i = 0; i < sig->param_count; ++i) {
2489 if (sp [0]->opcode == OP_ICONST) {
2492 temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
2494 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2495 store->cil_code = sp [0]->cil_code;
2496 if (store->opcode == CEE_STOBJ) {
2497 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2498 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE);
2500 MONO_ADD_INS (bblock, store);
2508 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
2509 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b, gboolean inline_allways)
2511 MonoInst *ins, *rvar = NULL;
2512 MonoMethodHeader *cheader;
2513 MonoBasicBlock *ebblock, *sbblock;
2514 int i, costs, new_locals_offset;
2515 MonoMethod *prev_inlined_method;
2517 if (cfg->verbose_level > 2)
2518 g_print ("INLINE START %p %s -> %s\n", cmethod, mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2520 if (!cmethod->inline_info) {
2521 mono_jit_stats.inlineable_methods++;
2522 cmethod->inline_info = 1;
2524 /* allocate space to store the return value */
2525 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2526 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2529 /* allocate local variables */
2530 cheader = ((MonoMethodNormal *)cmethod)->header;
2531 new_locals_offset = cfg->num_varinfo;
2532 for (i = 0; i < cheader->num_locals; ++i)
2533 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
2535 /* allocate starte and end blocks */
2536 sbblock = NEW_BBLOCK (cfg);
2537 sbblock->block_num = cfg->num_bblocks++;
2538 sbblock->real_offset = real_offset;
2540 ebblock = NEW_BBLOCK (cfg);
2541 ebblock->block_num = cfg->num_bblocks++;
2542 ebblock->real_offset = real_offset;
2544 prev_inlined_method = cfg->inlined_method;
2545 cfg->inlined_method = cmethod;
2547 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
2549 cfg->inlined_method = prev_inlined_method;
2551 if ((costs >= 0 && costs < 60) || inline_allways) {
2552 if (cfg->verbose_level > 2)
2553 g_print ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2555 mono_jit_stats.inlined_methods++;
2557 /* always add some code to avoid block split failures */
2558 MONO_INST_NEW (cfg, ins, CEE_NOP);
2559 MONO_ADD_INS (bblock, ins);
2562 bblock->next_bb = sbblock;
2563 link_bblock (cfg, bblock, sbblock);
2566 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
2572 if (cfg->verbose_level > 2)
2573 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
2579 * Some of these comments may well be out-of-date.
2580 * Design decisions: we do a single pass over the IL code (and we do bblock
2581 * splitting/merging in the few cases when it's required: a back jump to an IL
2582 * address that was not already seen as bblock starting point).
2583 * Code is validated as we go (full verification is still better left to metadata/verify.c).
2584 * Complex operations are decomposed in simpler ones right away. We need to let the
2585 * arch-specific code peek and poke inside this process somehow (except when the
2586 * optimizations can take advantage of the full semantic info of coarse opcodes).
2587 * All the opcodes of the form opcode.s are 'normalized' to opcode.
2588 * MonoInst->opcode initially is the IL opcode or some simplification of that
2589 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
2590 * opcode with value bigger than OP_LAST.
2591 * At this point the IR can be handed over to an interpreter, a dumb code generator
2592 * or to the optimizing code generator that will translate it to SSA form.
2594 * Profiling directed optimizations.
2595 * We may compile by default with few or no optimizations and instrument the code
2596 * or the user may indicate what methods to optimize the most either in a config file
2597 * or through repeated runs where the compiler applies offline the optimizations to
2598 * each method and then decides if it was worth it.
2601 * * consider using an array instead of an hash table (bb_hash)
2604 #define CHECK_TYPE(ins) if (!(ins)->type) goto unverified
2605 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) goto unverified
2606 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) goto unverified
2607 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) goto unverified
2608 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) goto unverified
2609 #define CHECK_OPSIZE(size) if (ip + size > end) goto unverified
2612 /* offset from br.s -> br like opcodes */
2613 #define BIG_BRANCH_OFFSET 13
2616 get_basic_blocks (MonoCompile *cfg, GHashTable *bbhash, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
2618 unsigned char *ip = start;
2619 unsigned char *target;
2622 MonoBasicBlock *bblock;
2623 const MonoOpcode *opcode;
2626 cli_addr = ip - start;
2627 i = mono_opcode_value ((const guint8 **)&ip, end);
2630 opcode = &mono_opcodes [i];
2631 switch (opcode->argument) {
2632 case MonoInlineNone:
2635 case MonoInlineString:
2636 case MonoInlineType:
2637 case MonoInlineField:
2638 case MonoInlineMethod:
2641 case MonoShortInlineR:
2648 case MonoShortInlineVar:
2649 case MonoShortInlineI:
2652 case MonoShortInlineBrTarget:
2653 target = start + cli_addr + 2 + (signed char)ip [1];
2654 GET_BBLOCK (cfg, bbhash, bblock, target);
2657 case MonoInlineBrTarget:
2658 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
2659 GET_BBLOCK (cfg, bbhash, bblock, target);
2662 case MonoInlineSwitch: {
2663 guint32 n = read32 (ip + 1);
2666 cli_addr += 5 + 4 * n;
2667 target = start + cli_addr;
2668 GET_BBLOCK (cfg, bbhash, bblock, target);
2670 for (j = 0; j < n; ++j) {
2671 target = start + cli_addr + (gint32)read32 (ip);
2672 GET_BBLOCK (cfg, bbhash, bblock, target);
2682 g_assert_not_reached ();
2692 emit_tree (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *ins)
2694 MonoInst *store, *temp, *load;
2695 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
2696 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2697 store->cil_code = ins->cil_code;
2698 MONO_ADD_INS (bblock, store);
2699 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2700 load->cil_code = ins->cil_code;
2705 * mono_method_to_ir: translates IL into basic blocks containing trees
2708 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
2709 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
2710 guint inline_offset, gboolean is_virtual_call)
2712 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
2713 MonoInst *ins, **sp, **stack_start;
2714 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
2716 MonoMethod *cmethod;
2717 MonoInst **arg_array;
2718 MonoMethodHeader *header;
2720 guint32 token, ins_flag;
2722 MonoClass *constrained_call = NULL;
2723 unsigned char *ip, *end, *target, *err_pos;
2724 static double r8_0 = 0.0;
2725 MonoMethodSignature *sig;
2726 MonoGenericContext *generic_context = NULL;
2727 MonoType **param_types;
2728 GList *bb_recheck = NULL, *tmp;
2729 int i, n, start_new_bblock, align;
2730 int num_calls = 0, inline_costs = 0;
2731 int *filter_lengths = NULL;
2732 int breakpoint_id = 0;
2733 guint real_offset, num_args;
2735 image = method->klass->image;
2736 header = ((MonoMethodNormal *)method)->header;
2737 sig = method->signature;
2738 num_args = sig->hasthis + sig->param_count;
2739 ip = (unsigned char*)header->code;
2740 end = ip + header->code_size;
2741 mono_jit_stats.cil_code_size += header->code_size;
2743 if (method->signature->is_inflated)
2744 generic_context = ((MonoMethodInflated *) method)->context;
2746 if (cfg->method == method) {
2748 bbhash = cfg->bb_hash;
2750 real_offset = inline_offset;
2751 bbhash = g_hash_table_new (g_direct_hash, NULL);
2754 if (cfg->verbose_level > 2)
2755 g_print ("method to IR %s\n", mono_method_full_name (method, TRUE));
2757 dont_inline = g_list_prepend (dont_inline, method);
2758 if (cfg->method == method) {
2760 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
2761 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
2764 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
2765 start_bblock->cil_code = NULL;
2766 start_bblock->cil_length = 0;
2767 start_bblock->block_num = cfg->num_bblocks++;
2770 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
2771 end_bblock->cil_code = NULL;
2772 end_bblock->cil_length = 0;
2773 end_bblock->block_num = cfg->num_bblocks++;
2774 g_assert (cfg->num_bblocks == 2);
2776 arg_array = alloca (sizeof (MonoInst *) * num_args);
2777 for (i = num_args - 1; i >= 0; i--)
2778 arg_array [i] = cfg->varinfo [i];
2780 if (header->num_clauses) {
2781 int size = sizeof (int) * header->num_clauses;
2782 filter_lengths = alloca (size);
2783 memset (filter_lengths, 0, size);
2785 cfg->spvars = g_hash_table_new (NULL, NULL);
2787 /* handle exception clauses */
2788 for (i = 0; i < header->num_clauses; ++i) {
2789 //unsigned char *p = ip;
2790 MonoExceptionClause *clause = &header->clauses [i];
2791 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->try_offset);
2792 tblock->real_offset = clause->try_offset;
2793 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
2794 tblock->real_offset = clause->handler_offset;
2796 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
2797 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2798 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2799 MONO_ADD_INS (tblock, ins);
2802 /*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);
2804 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
2806 /* catch and filter blocks get the exception object on the stack */
2807 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
2808 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2809 /* mostly like handle_stack_args (), but just sets the input args */
2810 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
2812 cfg->exvar = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
2813 /* prevent it from being register allocated */
2814 cfg->exvar->flags |= MONO_INST_INDIRECT;
2816 tblock->in_scount = 1;
2817 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2818 tblock->in_stack [0] = cfg->exvar;
2820 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2821 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->token_or_filter);
2822 tblock->real_offset = clause->token_or_filter;
2823 tblock->in_scount = 1;
2824 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2825 tblock->in_stack [0] = cfg->exvar;
2826 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2827 MONO_ADD_INS (tblock, ins);
2833 arg_array = alloca (sizeof (MonoInst *) * num_args);
2834 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
2837 /* FIRST CODE BLOCK */
2838 bblock = NEW_BBLOCK (cfg);
2839 bblock->cil_code = ip;
2841 ADD_BBLOCK (cfg, bbhash, bblock);
2843 if (cfg->method == method) {
2844 breakpoint_id = mono_debugger_method_has_breakpoint (method);
2845 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
2846 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2847 MONO_ADD_INS (bblock, ins);
2851 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED))) || mono_compile_aot) {
2852 /* we use a separate basic block for the initialization code */
2853 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
2854 init_localsbb->real_offset = real_offset;
2855 start_bblock->next_bb = init_localsbb;
2856 init_localsbb->next_bb = bblock;
2857 link_bblock (cfg, start_bblock, init_localsbb);
2858 link_bblock (cfg, init_localsbb, bblock);
2859 init_localsbb->block_num = cfg->num_bblocks++;
2861 start_bblock->next_bb = bblock;
2862 link_bblock (cfg, start_bblock, bblock);
2865 if (get_basic_blocks (cfg, bbhash, header, real_offset, ip, end, &err_pos)) {
2870 mono_debug_init_method (cfg, bblock, breakpoint_id);
2872 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
2874 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
2875 for (n = 0; n < sig->param_count; ++n)
2876 param_types [n + sig->hasthis] = sig->params [n];
2878 /* do this somewhere outside - not here */
2879 NEW_ICONST (cfg, zero_int32, 0);
2880 NEW_ICONST (cfg, zero_int64, 0);
2881 zero_int64->type = STACK_I8;
2882 NEW_PCONST (cfg, zero_ptr, 0);
2883 NEW_PCONST (cfg, zero_obj, 0);
2884 zero_obj->type = STACK_OBJ;
2886 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
2887 zero_r8->type = STACK_R8;
2888 zero_r8->inst_p0 = &r8_0;
2890 /* add a check for this != NULL to inlined methods */
2891 if (is_virtual_call) {
2892 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
2893 NEW_ARGLOAD (cfg, ins->inst_left, 0);
2895 MONO_ADD_INS (bblock, ins);
2898 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
2899 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
2902 start_new_bblock = 0;
2905 if (cfg->method == method)
2906 real_offset = ip - header->code;
2908 real_offset = inline_offset;
2910 if (start_new_bblock) {
2911 bblock->cil_length = ip - bblock->cil_code;
2912 if (start_new_bblock == 2) {
2913 g_assert (ip == tblock->cil_code);
2915 GET_BBLOCK (cfg, bbhash, tblock, ip);
2917 bblock->next_bb = tblock;
2919 start_new_bblock = 0;
2920 for (i = 0; i < bblock->in_scount; ++i) {
2921 if (cfg->verbose_level > 3)
2922 g_print ("loading %d from temp %d\n", i, bblock->in_stack [i]->inst_c0);
2923 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2927 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
2928 link_bblock (cfg, bblock, tblock);
2929 if (sp != stack_start) {
2930 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2933 bblock->next_bb = tblock;
2935 for (i = 0; i < bblock->in_scount; ++i) {
2936 if (cfg->verbose_level > 3)
2937 g_print ("loading %d from temp %d\n", i, bblock->in_stack [i]->inst_c0);
2938 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2944 bblock->real_offset = real_offset;
2946 if ((cfg->method == method) && cfg->coverage_info) {
2947 MonoInst *store, *one;
2948 guint32 cil_offset = ip - header->code;
2949 cfg->coverage_info->data [cil_offset].cil_code = ip;
2951 /* TODO: Use an increment here */
2952 NEW_ICONST (cfg, one, 1);
2955 NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
2958 MONO_INST_NEW (cfg, store, CEE_STIND_I);
2959 store->cil_code = ip;
2960 store->inst_left = ins;
2961 store->inst_right = one;
2963 MONO_ADD_INS (bblock, store);
2966 if (cfg->verbose_level > 3)
2967 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, sp-stack_start, mono_disasm_code_one (NULL, method, ip, NULL));
2974 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2975 ins->cil_code = ip++;
2976 MONO_ADD_INS (bblock, ins);
2982 CHECK_STACK_OVF (1);
2983 n = (*ip)-CEE_LDARG_0;
2985 NEW_ARGLOAD (cfg, ins, n);
2986 ins->cil_code = ip++;
2993 CHECK_STACK_OVF (1);
2994 n = (*ip)-CEE_LDLOC_0;
2996 NEW_LOCLOAD (cfg, ins, n);
2997 ins->cil_code = ip++;
3005 n = (*ip)-CEE_STLOC_0;
3008 handle_loaded_temps (cfg, bblock, stack_start, sp);
3009 NEW_LOCSTORE (cfg, ins, n, *sp);
3011 if (ins->opcode == CEE_STOBJ) {
3012 NEW_LOCLOADA (cfg, ins, n);
3013 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3015 MONO_ADD_INS (bblock, ins);
3021 CHECK_STACK_OVF (1);
3023 NEW_ARGLOAD (cfg, ins, ip [1]);
3030 CHECK_STACK_OVF (1);
3032 NEW_ARGLOADA (cfg, ins, ip [1]);
3042 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
3043 handle_loaded_temps (cfg, bblock, stack_start, sp);
3045 if (ins->opcode == CEE_STOBJ) {
3046 NEW_ARGLOADA (cfg, ins, ip [1]);
3047 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3049 MONO_ADD_INS (bblock, ins);
3054 CHECK_STACK_OVF (1);
3055 CHECK_LOCAL (ip [1]);
3056 NEW_LOCLOAD (cfg, ins, ip [1]);
3063 CHECK_STACK_OVF (1);
3064 CHECK_LOCAL (ip [1]);
3065 NEW_LOCLOADA (cfg, ins, ip [1]);
3074 handle_loaded_temps (cfg, bblock, stack_start, sp);
3075 CHECK_LOCAL (ip [1]);
3076 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
3078 if (ins->opcode == CEE_STOBJ) {
3079 NEW_LOCLOADA (cfg, ins, ip [1]);
3080 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3082 MONO_ADD_INS (bblock, ins);
3087 CHECK_STACK_OVF (1);
3088 NEW_PCONST (cfg, ins, NULL);
3090 ins->type = STACK_OBJ;
3095 CHECK_STACK_OVF (1);
3096 NEW_ICONST (cfg, ins, -1);
3110 CHECK_STACK_OVF (1);
3111 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
3118 CHECK_STACK_OVF (1);
3120 NEW_ICONST (cfg, ins, *((signed char*)ip));
3127 CHECK_STACK_OVF (1);
3128 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
3135 CHECK_STACK_OVF (1);
3136 MONO_INST_NEW (cfg, ins, OP_I8CONST);
3138 ins->type = STACK_I8;
3140 ins->inst_l = (gint64)read64 (ip);
3145 float *f = mono_mempool_alloc (cfg->domain->mp, sizeof (float));
3147 CHECK_STACK_OVF (1);
3148 MONO_INST_NEW (cfg, ins, OP_R4CONST);
3149 ins->type = STACK_R8;
3158 double *d = mono_mempool_alloc (cfg->domain->mp, sizeof (double));
3160 CHECK_STACK_OVF (1);
3161 MONO_INST_NEW (cfg, ins, OP_R8CONST);
3162 ins->type = STACK_R8;
3171 MonoInst *temp, *store;
3173 CHECK_STACK_OVF (1);
3178 * small optimization: if the loaded value was from a local already,
3179 * just load it twice.
3181 if (ins->ssa_op == MONO_SSA_LOAD &&
3182 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
3184 MONO_INST_NEW (cfg, temp, 0);
3186 temp->cil_code = ip;
3189 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3190 temp->cil_code = ip;
3191 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3192 store->cil_code = ip;
3193 MONO_ADD_INS (bblock, store);
3194 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3197 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3207 MONO_INST_NEW (cfg, ins, CEE_POP);
3208 MONO_ADD_INS (bblock, ins);
3209 ins->cil_code = ip++;
3215 if (stack_start != sp)
3217 MONO_INST_NEW (cfg, ins, CEE_JMP);
3218 token = read32 (ip + 1);
3219 /* FIXME: check the signature matches */
3220 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3221 ins->inst_p0 = cmethod;
3222 MONO_ADD_INS (bblock, ins);
3224 start_new_bblock = 1;
3228 case CEE_CALLVIRT: {
3229 MonoInst *addr = NULL;
3230 MonoMethodSignature *fsig = NULL;
3231 int temp, array_rank = 0;
3232 int virtual = *ip == CEE_CALLVIRT;
3235 token = read32 (ip + 1);
3237 if (*ip == CEE_CALLI) {
3242 if (method->wrapper_type != MONO_WRAPPER_NONE)
3243 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
3245 fsig = mono_metadata_parse_signature (image, token);
3247 n = fsig->param_count + fsig->hasthis;
3249 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3250 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
3251 } else if (constrained_call) {
3252 cmethod = mono_get_method_constrained (image, token, constrained_call, generic_context);
3254 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3259 if (!cmethod->klass->inited)
3260 mono_class_init (cmethod->klass);
3262 if (cmethod->signature->pinvoke) {
3263 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
3264 fsig = wrapper->signature;
3266 fsig = mono_method_get_signature (cmethod, image, token);
3269 n = fsig->param_count + fsig->hasthis;
3271 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
3272 cmethod->klass->parent == mono_defaults.array_class) {
3273 array_rank = cmethod->klass->rank;
3276 if (cmethod->string_ctor)
3277 g_assert_not_reached ();
3283 //g_assert (!virtual || fsig->hasthis);
3287 if (constrained_call) {
3289 * We have the `constrained.' prefix opcode.
3291 if (constrained_call->valuetype && !cmethod->klass->valuetype) {
3293 * The type parameter is instantiated as a valuetype,
3294 * but that type doesn't override the method we're
3295 * calling, so we need to box `this'.
3297 sp [0] = handle_box (cfg, bblock, sp [0], ip, constrained_call);
3298 } else if (!constrained_call->valuetype) {
3302 * The type parameter is instantiated as a reference
3303 * type. We have a managed pointer on the stack, so
3304 * we need to dereference it here.
3307 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3309 ins->inst_i0 = sp [0];
3310 ins->type = STACK_OBJ;
3312 } else if (cmethod->klass->valuetype)
3314 constrained_call = NULL;
3317 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
3320 if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL) && (mono_metadata_signature_equal (method->signature, cmethod->signature))) {
3322 /* FIXME: This assumes the two methods has the same number and type of arguments */
3323 for (i = 0; i < n; ++i) {
3324 /* Check if argument is the same */
3325 NEW_ARGLOAD (cfg, ins, i);
3326 if ((ins->opcode == sp [i]->opcode) && (ins->inst_i0 == sp [i]->inst_i0))
3329 /* Prevent argument from being register allocated */
3330 arg_array [i]->flags |= MONO_INST_VOLATILE;
3331 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3333 if (ins->opcode == CEE_STOBJ) {
3334 NEW_ARGLOADA (cfg, ins, i);
3335 handle_stobj (cfg, bblock, ins, sp [i], sp [i]->cil_code, ins->klass, FALSE, FALSE);
3338 MONO_ADD_INS (bblock, ins);
3340 MONO_INST_NEW (cfg, ins, CEE_JMP);
3342 ins->inst_p0 = cmethod;
3343 ins->inst_p1 = arg_array [0];
3344 MONO_ADD_INS (bblock, ins);
3345 start_new_bblock = 1;
3346 /* skip CEE_RET as well */
3351 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_opcode_for_method (cfg, cmethod, fsig, sp))) {
3354 if (MONO_TYPE_IS_VOID (fsig->ret)) {
3355 MONO_ADD_INS (bblock, ins);
3357 type_to_eval_stack_type (fsig->ret, ins);
3366 handle_loaded_temps (cfg, bblock, stack_start, sp);
3368 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3369 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
3370 mono_method_check_inlining (cfg, cmethod) &&
3371 !g_list_find (dont_inline, cmethod)) {
3373 MonoBasicBlock *ebblock;
3374 gboolean allways = FALSE;
3376 if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
3377 (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
3378 cmethod = mono_marshal_get_native_wrapper (cmethod);
3382 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, allways))) {
3386 GET_BBLOCK (cfg, bbhash, bblock, ip);
3387 ebblock->next_bb = bblock;
3388 link_bblock (cfg, ebblock, bblock);
3390 if (!MONO_TYPE_IS_VOID (fsig->ret))
3393 /* indicates start of a new block, and triggers a load of all
3394 stack arguments at bb boundarie */
3397 inline_costs += costs;
3402 inline_costs += 10 * num_calls++;
3404 /* tail recursion elimination */
3405 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == method && ip [5] == CEE_RET) {
3406 gboolean has_vtargs = FALSE;
3409 /* keep it simple */
3410 for (i = fsig->param_count - 1; i >= 0; i--) {
3411 if (MONO_TYPE_ISSTRUCT (cmethod->signature->params [i]))
3416 for (i = 0; i < n; ++i) {
3417 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3419 MONO_ADD_INS (bblock, ins);
3421 MONO_INST_NEW (cfg, ins, CEE_BR);
3423 MONO_ADD_INS (bblock, ins);
3424 tblock = start_bblock->out_bb [0];
3425 link_bblock (cfg, bblock, tblock);
3426 ins->inst_target_bb = tblock;
3427 start_new_bblock = 1;
3430 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3431 /* just create a dummy - the value is never used */
3432 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
3433 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3441 if (*ip == CEE_CALLI) {
3443 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
3444 NEW_TEMPLOAD (cfg, *sp, temp);
3448 } else if (array_rank) {
3451 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
3452 if (sp [fsig->param_count]->type == STACK_OBJ) {
3453 MonoInst *iargs [2];
3454 MonoInst *array, *to_store, *store;
3456 handle_loaded_temps (cfg, bblock, stack_start, sp);
3458 array = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
3459 NEW_TEMPSTORE (cfg, store, array->inst_c0, sp [0]);
3460 store->cil_code = ip;
3461 MONO_ADD_INS (bblock, store);
3462 NEW_TEMPLOAD (cfg, iargs [0], array->inst_c0);
3464 to_store = mono_compile_create_var (cfg, type_from_stack_type (sp [fsig->param_count]), OP_LOCAL);
3465 NEW_TEMPSTORE (cfg, store, to_store->inst_c0, sp [fsig->param_count]);
3466 store->cil_code = ip;
3467 MONO_ADD_INS (bblock, store);
3468 NEW_TEMPLOAD (cfg, iargs [1], to_store->inst_c0);
3471 * We first save the args for the call so that the args are copied to the stack
3472 * and a new instruction tree for them is created. If we don't do this,
3473 * the same MonoInst is added to two different trees and this is not
3476 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref_check, iargs, ip);
3478 NEW_TEMPLOAD (cfg, sp [0], array->inst_c0);
3479 NEW_TEMPLOAD (cfg, sp [fsig->param_count], to_store->inst_c0);
3482 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
3483 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
3485 if (ins->opcode == CEE_STOBJ) {
3486 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
3488 MONO_ADD_INS (bblock, ins);
3491 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
3492 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3493 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
3497 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
3498 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3501 g_assert_not_reached ();
3505 if (0 && CODE_IS_STLOC (ip + 5) && (!MONO_TYPE_ISSTRUCT (fsig->ret)) && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)) {
3506 /* no need to spill */
3507 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
3510 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) {
3511 NEW_TEMPLOAD (cfg, *sp, temp);
3521 if (cfg->method != method) {
3522 /* return from inlined methode */
3527 //g_assert (returnvar != -1);
3528 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
3529 store->cil_code = sp [0]->cil_code;
3530 if (store->opcode == CEE_STOBJ) {
3531 g_assert_not_reached ();
3532 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
3533 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
3535 MONO_ADD_INS (bblock, store);
3539 g_assert (!return_var);
3542 MONO_INST_NEW (cfg, ins, CEE_NOP);
3543 ins->opcode = mono_type_to_stind (method->signature->ret);
3544 if (ins->opcode == CEE_STOBJ) {
3545 NEW_RETLOADA (cfg, ins);
3546 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3548 ins->opcode = OP_SETRET;
3550 ins->inst_i0 = *sp;;
3551 ins->inst_i1 = NULL;
3552 MONO_ADD_INS (bblock, ins);
3556 if (sp != stack_start)
3558 MONO_INST_NEW (cfg, ins, CEE_BR);
3559 ins->cil_code = ip++;
3560 ins->inst_target_bb = end_bblock;
3561 MONO_ADD_INS (bblock, ins);
3562 link_bblock (cfg, bblock, end_bblock);
3563 start_new_bblock = 1;
3567 MONO_INST_NEW (cfg, ins, CEE_BR);
3568 ins->cil_code = ip++;
3569 MONO_ADD_INS (bblock, ins);
3570 target = ip + 1 + (signed char)(*ip);
3572 GET_BBLOCK (cfg, bbhash, tblock, target);
3573 link_bblock (cfg, bblock, tblock);
3574 CHECK_BBLOCK (target, ip, tblock);
3575 ins->inst_target_bb = tblock;
3576 if (sp != stack_start) {
3577 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3580 start_new_bblock = 1;
3587 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3588 ins->cil_code = ip++;
3589 target = ip + 1 + *(signed char*)ip;
3591 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
3592 if (sp != stack_start) {
3593 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3610 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3611 ins->cil_code = ip++;
3612 target = ip + 1 + *(signed char*)ip;
3615 if (sp != stack_start) {
3616 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3623 MONO_INST_NEW (cfg, ins, CEE_BR);
3624 ins->cil_code = ip++;
3625 MONO_ADD_INS (bblock, ins);
3626 target = ip + 4 + (gint32)read32(ip);
3628 GET_BBLOCK (cfg, bbhash, tblock, target);
3629 link_bblock (cfg, bblock, tblock);
3630 CHECK_BBLOCK (target, ip, tblock);
3631 ins->inst_target_bb = tblock;
3632 if (sp != stack_start) {
3633 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3636 start_new_bblock = 1;
3643 MONO_INST_NEW (cfg, ins, *ip);
3644 ins->cil_code = ip++;
3645 target = ip + 4 + (gint32)read32(ip);
3647 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
3648 if (sp != stack_start) {
3649 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3666 MONO_INST_NEW (cfg, ins, *ip);
3667 ins->cil_code = ip++;
3668 target = ip + 4 + (gint32)read32(ip);
3671 if (sp != stack_start) {
3672 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3680 n = read32 (ip + 1);
3681 MONO_INST_NEW (cfg, ins, *ip);
3683 ins->inst_left = *sp;
3684 if (ins->inst_left->type != STACK_I4) goto unverified;
3687 CHECK_OPSIZE (n * sizeof (guint32));
3688 target = ip + n * sizeof (guint32);
3689 MONO_ADD_INS (bblock, ins);
3690 GET_BBLOCK (cfg, bbhash, tblock, target);
3691 link_bblock (cfg, bblock, tblock);
3692 ins->klass = GUINT_TO_POINTER (n);
3693 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
3694 ins->inst_many_bb [n] = tblock;
3696 for (i = 0; i < n; ++i) {
3697 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
3698 link_bblock (cfg, bblock, tblock);
3699 ins->inst_many_bb [i] = tblock;
3702 if (sp != stack_start) {
3703 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3720 MONO_INST_NEW (cfg, ins, *ip);
3725 ins->type = ldind_type [*ip - CEE_LDIND_I1];
3726 ins->flags |= ins_flag;
3738 MONO_INST_NEW (cfg, ins, *ip);
3739 ins->cil_code = ip++;
3741 handle_loaded_temps (cfg, bblock, stack_start, sp);
3742 MONO_ADD_INS (bblock, ins);
3743 ins->inst_i0 = sp [0];
3744 ins->inst_i1 = sp [1];
3745 ins->flags |= ins_flag;
3764 /* special case that gives a nice speedup and happens to workaorund a ppc jit but (for the release)
3765 * later apply the speedup to the left shift as well
3768 if ((ins->opcode == OP_LSHR_UN) && (ins->type == STACK_I8)
3769 && (ins->inst_right->opcode == OP_ICONST) && (ins->inst_right->inst_c0 == 32)) {
3770 ins->opcode = OP_LONG_SHRUN_32;
3771 /*g_print ("applied long shr speedup to %s\n", cfg->method->name);*/
3775 if (mono_find_jit_opcode_emulation (ins->opcode)) {
3777 *sp++ = emit_tree (cfg, bblock, ins);
3791 case CEE_CONV_OVF_I8:
3792 case CEE_CONV_OVF_U8:
3796 if (mono_find_jit_opcode_emulation (ins->opcode)) {
3798 *sp++ = emit_tree (cfg, bblock, ins);
3802 case CEE_CONV_OVF_I4:
3803 case CEE_CONV_OVF_I1:
3804 case CEE_CONV_OVF_I2:
3805 case CEE_CONV_OVF_I:
3806 case CEE_CONV_OVF_U:
3809 if (sp [-1]->type == STACK_R8) {
3810 ADD_UNOP (CEE_CONV_OVF_I8);
3818 case CEE_CONV_OVF_U1:
3819 case CEE_CONV_OVF_U2:
3820 case CEE_CONV_OVF_U4:
3823 if (sp [-1]->type == STACK_R8) {
3824 ADD_UNOP (CEE_CONV_OVF_U8);
3832 case CEE_CONV_OVF_I1_UN:
3833 case CEE_CONV_OVF_I2_UN:
3834 case CEE_CONV_OVF_I4_UN:
3835 case CEE_CONV_OVF_I8_UN:
3836 case CEE_CONV_OVF_U1_UN:
3837 case CEE_CONV_OVF_U2_UN:
3838 case CEE_CONV_OVF_U4_UN:
3839 case CEE_CONV_OVF_U8_UN:
3840 case CEE_CONV_OVF_I_UN:
3841 case CEE_CONV_OVF_U_UN:
3849 token = read32 (ip + 1);
3850 if (method->wrapper_type != MONO_WRAPPER_NONE)
3851 klass = mono_method_get_wrapper_data (method, token);
3853 klass = mono_class_get_full (image, token, generic_context);
3855 mono_class_init (klass);
3857 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3858 MonoInst *store, *load;
3859 MONO_INST_NEW (cfg, load, CEE_LDIND_REF);
3860 load->cil_code = ip;
3861 load->inst_i0 = sp [1];
3862 load->type = ldind_type [CEE_LDIND_REF];
3863 load->flags |= ins_flag;
3864 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
3865 store->cil_code = ip;
3866 handle_loaded_temps (cfg, bblock, stack_start, sp);
3867 MONO_ADD_INS (bblock, store);
3868 store->inst_i0 = sp [0];
3869 store->inst_i1 = load;
3870 store->flags |= ins_flag;
3872 n = mono_class_value_size (klass, NULL);
3873 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3875 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3876 copy->inst_left = sp [0];
3877 copy->inst_right = sp [1];
3878 copy->cil_code = ip;
3880 MONO_ADD_INS (bblock, copy);
3882 MonoInst *iargs [3];
3885 NEW_ICONST (cfg, iargs [2], n);
3886 iargs [2]->cil_code = ip;
3888 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3895 MonoInst *iargs [3];
3899 token = read32 (ip + 1);
3900 if (method->wrapper_type != MONO_WRAPPER_NONE)
3901 klass = mono_method_get_wrapper_data (method, token);
3903 klass = mono_class_get_full (image, token, generic_context);
3905 mono_class_init (klass);
3906 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3907 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3909 ins->inst_i0 = sp [0];
3910 ins->type = ldind_type [CEE_LDIND_REF];
3911 ins->flags |= ins_flag;
3917 n = mono_class_value_size (klass, NULL);
3918 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3919 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3920 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3922 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3923 copy->inst_left = iargs [0];
3924 copy->inst_right = *sp;
3925 copy->cil_code = ip;
3927 MONO_ADD_INS (bblock, copy);
3930 NEW_ICONST (cfg, iargs [2], n);
3931 iargs [2]->cil_code = ip;
3933 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3935 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3943 CHECK_STACK_OVF (1);
3945 n = read32 (ip + 1);
3947 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3949 MonoInst *iargs [1];
3951 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
3952 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
3953 NEW_TEMPLOAD (cfg, *sp, temp);
3957 if (cfg->opt & MONO_OPT_SHARED) {
3959 MonoInst *iargs [3];
3961 if (mono_compile_aot) {
3962 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n);
3965 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3966 NEW_IMAGECONST (cfg, iargs [1], image);
3967 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
3968 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
3969 NEW_TEMPLOAD (cfg, *sp, temp);
3970 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3972 if (mono_compile_aot)
3973 NEW_LDSTRCONST (cfg, ins, image, n);
3975 NEW_PCONST (cfg, ins, NULL);
3977 ins->type = STACK_OBJ;
3978 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3988 MonoInst *iargs [2];
3989 MonoMethodSignature *fsig;
3993 token = read32 (ip + 1);
3994 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3995 cmethod = mono_method_get_wrapper_data (method, token);
3997 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3998 fsig = mono_method_get_signature (cmethod, image, token);
4000 mono_class_init (cmethod->klass);
4002 n = fsig->param_count;
4005 /* move the args to allow room for 'this' in the first position */
4011 handle_loaded_temps (cfg, bblock, stack_start, sp);
4014 if (cmethod->klass->parent == mono_defaults.array_class) {
4015 NEW_METHODCONST (cfg, *sp, cmethod);
4016 temp = mono_emit_native_call (cfg, bblock, mono_array_new_va, fsig, sp, ip, FALSE);
4017 cfg->flags |= MONO_CFG_HAS_VARARGS;
4019 } else if (cmethod->string_ctor) {
4020 /* we simply pass a null pointer */
4021 NEW_PCONST (cfg, *sp, NULL);
4022 /* now call the string ctor */
4023 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
4025 if (cmethod->klass->valuetype) {
4026 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
4027 temp = iargs [0]->inst_c0;
4028 NEW_TEMPLOADA (cfg, *sp, temp);
4030 if (cfg->opt & MONO_OPT_SHARED) {
4031 NEW_DOMAINCONST (cfg, iargs [0]);
4032 NEW_CLASSCONST (cfg, iargs [1], cmethod->klass);
4034 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
4036 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
4037 NEW_VTABLECONST (cfg, iargs [0], vtable);
4038 if (cmethod->klass->has_finalize || cmethod->klass->marshalbyref || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
4039 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
4041 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
4043 NEW_TEMPLOAD (cfg, *sp, temp);
4046 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
4047 mono_method_check_inlining (cfg, cmethod) &&
4048 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
4049 !g_list_find (dont_inline, cmethod)) {
4051 MonoBasicBlock *ebblock;
4052 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, FALSE))) {
4057 GET_BBLOCK (cfg, bbhash, bblock, ip);
4058 ebblock->next_bb = bblock;
4059 link_bblock (cfg, ebblock, bblock);
4061 NEW_TEMPLOAD (cfg, *sp, temp);
4064 /* indicates start of a new block, and triggers a load
4065 of all stack arguments at bb boundarie */
4068 inline_costs += costs;
4072 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
4075 /* now call the actual ctor */
4076 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
4080 NEW_TEMPLOAD (cfg, *sp, temp);
4091 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4092 mono_class_init (klass);
4094 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4096 MonoMethod *mono_isinst;
4097 MonoInst *iargs [1];
4098 MonoBasicBlock *ebblock;
4102 mono_isinst = mono_marshal_get_isinst (klass);
4105 costs = inline_method (cfg, mono_isinst, mono_isinst->signature, bblock,
4106 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4108 g_assert (costs > 0);
4113 GET_BBLOCK (cfg, bbhash, bblock, ip);
4114 ebblock->next_bb = bblock;
4115 link_bblock (cfg, ebblock, bblock);
4117 temp = iargs [0]->inst_i0->inst_c0;
4118 NEW_TEMPLOAD (cfg, *sp, temp);
4122 inline_costs += costs;
4126 MONO_INST_NEW (cfg, ins, *ip);
4127 ins->type = STACK_OBJ;
4128 ins->inst_left = *sp;
4129 ins->inst_newa_class = klass;
4131 *sp++ = emit_tree (cfg, bblock, ins);
4135 case CEE_UNBOX_ANY: {
4136 MonoInst *add, *vtoffset;
4137 MonoInst *iargs [3];
4142 token = read32 (ip + 1);
4143 if (method->wrapper_type != MONO_WRAPPER_NONE)
4144 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4146 klass = mono_class_get_full (image, token, generic_context);
4147 mono_class_init (klass);
4149 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4151 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4152 MonoMethod *mono_castclass;
4153 MonoInst *iargs [1];
4154 MonoBasicBlock *ebblock;
4158 mono_castclass = mono_marshal_get_castclass (klass);
4161 costs = inline_method (cfg, mono_castclass, mono_castclass->signature, bblock,
4162 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4164 g_assert (costs > 0);
4169 GET_BBLOCK (cfg, bbhash, bblock, ip);
4170 ebblock->next_bb = bblock;
4171 link_bblock (cfg, ebblock, bblock);
4173 temp = iargs [0]->inst_i0->inst_c0;
4174 NEW_TEMPLOAD (cfg, *sp, temp);
4178 inline_costs += costs;
4181 MONO_INST_NEW (cfg, ins, CEE_CASTCLASS);
4182 ins->type = STACK_OBJ;
4183 ins->inst_left = *sp;
4185 ins->inst_newa_class = klass;
4193 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
4194 ins->type = STACK_OBJ;
4195 ins->inst_left = *sp;
4197 ins->inst_newa_class = klass;
4200 MONO_INST_NEW (cfg, add, CEE_ADD);
4201 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4202 add->inst_left = ins;
4203 add->inst_right = vtoffset;
4204 add->type = STACK_MP;
4208 n = mono_class_value_size (klass, NULL);
4209 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
4210 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
4211 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
4213 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
4214 copy->inst_left = iargs [0];
4215 copy->inst_right = *sp;
4216 copy->cil_code = ip;
4218 MONO_ADD_INS (bblock, copy);
4221 NEW_ICONST (cfg, iargs [2], n);
4222 iargs [2]->cil_code = ip;
4224 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
4226 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
4232 MonoInst *add, *vtoffset;
4237 token = read32 (ip + 1);
4238 if (method->wrapper_type != MONO_WRAPPER_NONE)
4239 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4241 klass = mono_class_get_full (image, token, generic_context);
4242 mono_class_init (klass);
4244 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
4245 ins->type = STACK_OBJ;
4246 ins->inst_left = *sp;
4248 ins->inst_newa_class = klass;
4251 MONO_INST_NEW (cfg, add, CEE_ADD);
4252 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4253 add->inst_left = ins;
4254 add->inst_right = vtoffset;
4255 add->type = STACK_MP;
4265 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4266 mono_class_init (klass);
4268 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4270 MonoMethod *mono_castclass;
4271 MonoInst *iargs [1];
4272 MonoBasicBlock *ebblock;
4276 mono_castclass = mono_marshal_get_castclass (klass);
4279 costs = inline_method (cfg, mono_castclass, mono_castclass->signature, bblock,
4280 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4282 g_assert (costs > 0);
4287 GET_BBLOCK (cfg, bbhash, bblock, ip);
4288 ebblock->next_bb = bblock;
4289 link_bblock (cfg, ebblock, bblock);
4291 temp = iargs [0]->inst_i0->inst_c0;
4292 NEW_TEMPLOAD (cfg, *sp, temp);
4296 inline_costs += costs;
4299 MONO_INST_NEW (cfg, ins, *ip);
4300 ins->type = STACK_OBJ;
4301 ins->inst_left = *sp;
4303 ins->inst_newa_class = klass;
4305 *sp++ = emit_tree (cfg, bblock, ins);
4311 MONO_INST_NEW (cfg, ins, *ip);
4313 ins->inst_left = *sp;
4314 ins->cil_code = ip++;
4315 MONO_ADD_INS (bblock, ins);
4317 start_new_bblock = 1;
4322 MonoInst *offset_ins;
4323 MonoClassField *field;
4324 MonoBasicBlock *ebblock;
4328 if (*ip == CEE_STFLD) {
4335 // FIXME: enable this test later.
4336 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
4339 token = read32 (ip + 1);
4340 field = mono_field_from_token (image, token, &klass, generic_context);
4341 mono_class_init (klass);
4343 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
4344 /* FIXME: mark instructions for use in SSA */
4345 if (*ip == CEE_STFLD) {
4346 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound) {
4347 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
4348 MonoInst *iargs [5];
4351 NEW_CLASSCONST (cfg, iargs [1], klass);
4352 NEW_FIELDCONST (cfg, iargs [2], field);
4353 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
4357 if (cfg->opt & MONO_OPT_INLINE) {
4358 costs = inline_method (cfg, stfld_wrapper, stfld_wrapper->signature, bblock,
4359 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4360 g_assert (costs > 0);
4365 GET_BBLOCK (cfg, bbhash, bblock, ip);
4366 ebblock->next_bb = bblock;
4367 link_bblock (cfg, ebblock, bblock);
4369 /* indicates start of a new block, and triggers a load
4370 of all stack arguments at bb boundarie */
4373 inline_costs += costs;
4376 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, stfld_wrapper->signature, iargs, ip, NULL);
4380 NEW_ICONST (cfg, offset_ins, foffset);
4381 MONO_INST_NEW (cfg, ins, CEE_ADD);
4383 ins->inst_left = *sp;
4384 ins->inst_right = offset_ins;
4385 ins->type = STACK_MP;
4387 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4388 store->cil_code = ip;
4389 store->inst_left = ins;
4390 store->inst_right = sp [1];
4391 handle_loaded_temps (cfg, bblock, stack_start, sp);
4392 store->flags |= ins_flag;
4394 if (store->opcode == CEE_STOBJ) {
4395 handle_stobj (cfg, bblock, ins, sp [1], ip,
4396 mono_class_from_mono_type (field->type), FALSE, FALSE);
4398 MONO_ADD_INS (bblock, store);
4401 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound) {
4402 /* fixme: we need to inline that call somehow */
4403 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
4404 MonoInst *iargs [4];
4408 NEW_CLASSCONST (cfg, iargs [1], klass);
4409 NEW_FIELDCONST (cfg, iargs [2], field);
4410 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
4411 if (cfg->opt & MONO_OPT_INLINE) {
4412 costs = inline_method (cfg, ldfld_wrapper, ldfld_wrapper->signature, bblock,
4413 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4414 g_assert (costs > 0);
4419 GET_BBLOCK (cfg, bbhash, bblock, ip);
4420 ebblock->next_bb = bblock;
4421 link_bblock (cfg, ebblock, bblock);
4423 temp = iargs [0]->inst_i0->inst_c0;
4425 if (*ip == CEE_LDFLDA) {
4426 /* not sure howto handle this */
4427 NEW_TEMPLOADA (cfg, *sp, temp);
4429 NEW_TEMPLOAD (cfg, *sp, temp);
4433 /* indicates start of a new block, and triggers a load of
4434 all stack arguments at bb boundarie */
4437 inline_costs += costs;
4440 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, ldfld_wrapper->signature, iargs, ip, NULL);
4441 if (*ip == CEE_LDFLDA) {
4442 /* not sure howto handle this */
4443 NEW_TEMPLOADA (cfg, *sp, temp);
4445 NEW_TEMPLOAD (cfg, *sp, temp);
4450 NEW_ICONST (cfg, offset_ins, foffset);
4451 MONO_INST_NEW (cfg, ins, CEE_ADD);
4453 ins->inst_left = *sp;
4454 ins->inst_right = offset_ins;
4455 ins->type = STACK_MP;
4457 if (*ip == CEE_LDFLDA) {
4461 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4462 type_to_eval_stack_type (field->type, load);
4463 load->cil_code = ip;
4464 load->inst_left = ins;
4465 load->flags |= ins_flag;
4477 MonoClassField *field;
4478 gpointer addr = NULL;
4481 token = read32 (ip + 1);
4483 field = mono_field_from_token (image, token, &klass, generic_context);
4484 mono_class_init (klass);
4486 if ((*ip) == CEE_STSFLD)
4487 handle_loaded_temps (cfg, bblock, stack_start, sp);
4489 if (cfg->domain->special_static_fields)
4490 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
4492 if ((cfg->opt & MONO_OPT_SHARED) || (mono_compile_aot && addr)) {
4494 MonoInst *iargs [2];
4495 g_assert (field->parent);
4496 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
4497 NEW_FIELDCONST (cfg, iargs [1], field);
4498 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
4499 NEW_TEMPLOAD (cfg, ins, temp);
4502 vtable = mono_class_vtable (cfg->domain, klass);
4504 if ((!vtable->initialized || mono_compile_aot) && !(klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (klass, method)) {
4505 guint8 *tramp = mono_create_class_init_trampoline (vtable);
4506 mono_emit_native_call (cfg, bblock, tramp,
4507 helper_sig_class_init_trampoline,
4509 if (cfg->verbose_level > 2)
4510 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
4512 if (cfg->run_cctors)
4513 mono_runtime_class_init (vtable);
4515 addr = (char*)vtable->data + field->offset;
4517 if (mono_compile_aot)
4518 NEW_SFLDACONST (cfg, ins, field);
4520 NEW_PCONST (cfg, ins, addr);
4524 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
4525 * This could be later optimized to do just a couple of
4526 * memory dereferences with constant offsets.
4529 MonoInst *iargs [1];
4530 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
4531 temp = mono_emit_jit_icall (cfg, bblock, mono_get_special_static_data, iargs, ip);
4532 NEW_TEMPLOAD (cfg, ins, temp);
4536 /* FIXME: mark instructions for use in SSA */
4537 if (*ip == CEE_LDSFLDA) {
4539 } else if (*ip == CEE_STSFLD) {
4543 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4544 store->cil_code = ip;
4545 store->inst_left = ins;
4546 store->inst_right = sp [0];
4547 store->flags |= ins_flag;
4550 if (store->opcode == CEE_STOBJ) {
4551 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
4553 MONO_ADD_INS (bblock, store);
4555 gboolean is_const = FALSE;
4556 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4557 if (!((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) &&
4558 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
4559 gpointer addr = (char*)vtable->data + field->offset;
4560 /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
4562 switch (field->type->type) {
4563 case MONO_TYPE_BOOLEAN:
4565 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
4569 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
4572 case MONO_TYPE_CHAR:
4574 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
4578 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
4583 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
4587 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
4592 case MONO_TYPE_STRING:
4593 case MONO_TYPE_OBJECT:
4594 case MONO_TYPE_CLASS:
4595 case MONO_TYPE_SZARRAY:
4597 case MONO_TYPE_FNPTR:
4598 case MONO_TYPE_ARRAY:
4599 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
4600 type_to_eval_stack_type (field->type, *sp);
4605 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
4606 sp [0]->type = STACK_I8;
4607 sp [0]->inst_l = *((gint64 *)addr);
4612 case MONO_TYPE_VALUETYPE:
4621 CHECK_STACK_OVF (1);
4622 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4623 type_to_eval_stack_type (field->type, load);
4624 load->cil_code = ip;
4625 load->inst_left = ins;
4627 load->flags |= ins_flag;
4629 /* fixme: dont see the problem why this does not work */
4630 //cfg->disable_aot = TRUE;
4640 token = read32 (ip + 1);
4641 if (method->wrapper_type != MONO_WRAPPER_NONE)
4642 klass = mono_method_get_wrapper_data (method, token);
4644 klass = mono_class_get_full (image, token, generic_context);
4645 mono_class_init (klass);
4646 n = mono_type_to_stind (&klass->byval_arg);
4647 if (n == CEE_STOBJ) {
4648 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
4650 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
4652 MONO_INST_NEW (cfg, store, n);
4653 store->cil_code = ip;
4654 store->inst_left = sp [0];
4655 store->inst_right = sp [1];
4656 store->flags |= ins_flag;
4657 MONO_ADD_INS (bblock, store);
4669 token = read32 (ip + 1);
4670 if (method->wrapper_type != MONO_WRAPPER_NONE)
4671 klass = mono_method_get_wrapper_data (method, token);
4673 klass = mono_class_get_full (image, token, generic_context);
4674 mono_class_init (klass);
4676 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4681 *sp++ = handle_box (cfg, bblock, val, ip, klass);
4688 MONO_INST_NEW (cfg, ins, *ip);
4693 token = read32 (ip + 1);
4695 /* allocate the domainvar - becaus this is used in decompose_foreach */
4696 if (cfg->opt & MONO_OPT_SHARED)
4697 mono_get_domainvar (cfg);
4699 if (method->wrapper_type != MONO_WRAPPER_NONE)
4700 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4702 klass = mono_class_get_full (image, token, generic_context);
4704 mono_class_init (klass);
4705 ins->inst_newa_class = klass;
4706 ins->inst_newa_len = *sp;
4707 ins->type = STACK_OBJ;
4711 * we store the object so calls to create the array are not interleaved
4712 * with the arguments of other calls.
4715 MonoInst *store, *temp, *load;
4717 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
4718 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4719 store->cil_code = ins->cil_code;
4720 MONO_ADD_INS (bblock, store);
4721 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
4722 load->cil_code = ins->cil_code;
4729 MONO_INST_NEW (cfg, ins, *ip);
4730 ins->cil_code = ip++;
4732 ins->inst_left = *sp;
4733 ins->type = STACK_PTR;
4741 if (method->wrapper_type != MONO_WRAPPER_NONE)
4742 klass = (MonoClass*)mono_method_get_wrapper_data (method, read32 (ip + 1));
4744 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4745 mono_class_init (klass);
4746 NEW_LDELEMA (cfg, ins, sp, klass);
4756 token = read32 (ip + 1);
4757 klass = mono_class_get_full (image, token, generic_context);
4758 mono_class_init (klass);
4759 NEW_LDELEMA (cfg, load, sp, klass);
4760 load->cil_code = ip;
4761 MONO_INST_NEW (cfg, ins, mono_type_to_ldind (&klass->byval_arg));
4763 ins->inst_left = load;
4765 type_to_eval_stack_type (&klass->byval_arg, ins);
4779 case CEE_LDELEM_REF: {
4783 * ldind.x (ldelema (array, index))
4784 * ldelema does the bounds check
4788 klass = array_access_to_klass (*ip);
4789 NEW_LDELEMA (cfg, load, sp, klass);
4790 load->cil_code = ip;
4791 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
4793 ins->inst_left = load;
4795 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
4805 case CEE_STELEM_R8: {
4809 * stind.x (ldelema (array, index), val)
4810 * ldelema does the bounds check
4814 klass = array_access_to_klass (*ip);
4815 NEW_LDELEMA (cfg, load, sp, klass);
4816 load->cil_code = ip;
4817 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
4819 ins->inst_left = load;
4820 ins->inst_right = sp [2];
4822 handle_loaded_temps (cfg, bblock, stack_start, sp);
4823 MONO_ADD_INS (bblock, ins);
4831 * stind.x (ldelema (array, index), val)
4832 * ldelema does the bounds check
4837 token = read32 (ip + 1);
4838 klass = mono_class_get_full (image, token, generic_context);
4839 mono_class_init (klass);
4840 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4841 MonoInst *iargs [3];
4842 handle_loaded_temps (cfg, bblock, stack_start, sp);
4848 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4850 NEW_LDELEMA (cfg, load, sp, klass);
4851 load->cil_code = ip;
4853 n = mono_type_to_stind (&klass->byval_arg);
4855 handle_stobj (cfg, bblock, load, sp [2], ip, klass, FALSE, FALSE);
4857 MONO_INST_NEW (cfg, ins, n);
4859 ins->inst_left = load;
4860 ins->inst_right = sp [2];
4861 handle_loaded_temps (cfg, bblock, stack_start, sp);
4862 MONO_ADD_INS (bblock, ins);
4869 case CEE_STELEM_REF: {
4870 MonoInst *iargs [3];
4875 handle_loaded_temps (cfg, bblock, stack_start, sp);
4881 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4885 NEW_GROUP (cfg, group, sp [0], sp [1]);
4886 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
4888 ins->inst_left = group;
4889 ins->inst_right = sp [2];
4890 MONO_ADD_INS (bblock, ins);
4897 case CEE_CKFINITE: {
4898 MonoInst *store, *temp;
4901 /* this instr. can throw exceptions as side effect,
4902 * so we cant eliminate dead code which contains CKFINITE opdodes.
4903 * Spilling to memory makes sure that we always perform
4907 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
4909 ins->inst_left = sp [-1];
4910 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
4912 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4913 store->cil_code = ip;
4914 MONO_ADD_INS (bblock, store);
4916 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
4923 MONO_INST_NEW (cfg, ins, *ip);
4926 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4927 mono_class_init (klass);
4928 ins->type = STACK_MP;
4929 ins->inst_left = *sp;
4931 ins->inst_newa_class = klass;
4936 case CEE_MKREFANY: {
4937 MonoInst *loc, *klassconst;
4940 MONO_INST_NEW (cfg, ins, *ip);
4943 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4944 mono_class_init (klass);
4947 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
4948 NEW_TEMPLOADA (cfg, ins->inst_right, loc->inst_c0);
4950 NEW_PCONST (cfg, klassconst, klass);
4951 NEW_GROUP (cfg, ins->inst_left, *sp, klassconst);
4953 MONO_ADD_INS (bblock, ins);
4955 NEW_TEMPLOAD (cfg, *sp, loc->inst_c0);
4962 MonoClass *handle_class;
4964 CHECK_STACK_OVF (1);
4967 n = read32 (ip + 1);
4969 handle = mono_ldtoken (image, n, &handle_class, generic_context);
4970 mono_class_init (handle_class);
4972 if (cfg->opt & MONO_OPT_SHARED) {
4974 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
4976 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
4978 NEW_IMAGECONST (cfg, iargs [0], image);
4979 NEW_ICONST (cfg, iargs [1], n);
4980 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
4981 NEW_TEMPLOAD (cfg, res, temp);
4982 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4983 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
4984 MONO_ADD_INS (bblock, store);
4985 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4987 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method_full (image, read32 (ip + 6), NULL, generic_context)) &&
4988 (cmethod->klass == mono_defaults.monotype_class->parent) &&
4989 (strcmp (cmethod->name, "GetTypeFromHandle") == 0) &&
4990 ((g_hash_table_lookup (bbhash, ip + 5) == NULL) ||
4991 (g_hash_table_lookup (bbhash, ip + 5) == bblock))) {
4992 MonoClass *tclass = mono_class_from_mono_type (handle);
4993 mono_class_init (tclass);
4994 if (mono_compile_aot)
4995 NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n);
4997 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
4998 ins->type = STACK_OBJ;
4999 ins->klass = cmethod->klass;
5002 MonoInst *store, *addr, *vtvar;
5004 if (mono_compile_aot)
5005 NEW_LDTOKENCONST (cfg, ins, image, n);
5007 NEW_PCONST (cfg, ins, handle);
5008 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
5009 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5010 NEW_INDSTORE (cfg, store, addr, ins, &mono_defaults.int_class->byval_arg);
5011 MONO_ADD_INS (bblock, store);
5012 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5028 case CEE_ADD_OVF_UN:
5030 case CEE_MUL_OVF_UN:
5032 case CEE_SUB_OVF_UN:
5035 if (mono_find_jit_opcode_emulation (ins->opcode)) {
5037 *sp++ = emit_tree (cfg, bblock, ins);
5041 case CEE_ENDFINALLY:
5042 MONO_INST_NEW (cfg, ins, *ip);
5043 MONO_ADD_INS (bblock, ins);
5044 ins->cil_code = ip++;
5045 start_new_bblock = 1;
5048 * Control will leave the method so empty the stack, otherwise
5049 * the next basic block will start with a nonempty stack.
5051 while (sp != stack_start) {
5052 MONO_INST_NEW (cfg, ins, CEE_POP);
5056 MONO_ADD_INS (bblock, ins);
5062 if (*ip == CEE_LEAVE) {
5064 target = ip + 5 + (gint32)read32(ip + 1);
5067 target = ip + 2 + (signed char)(ip [1]);
5070 /* empty the stack */
5071 while (sp != stack_start) {
5072 MONO_INST_NEW (cfg, ins, CEE_POP);
5076 MONO_ADD_INS (bblock, ins);
5080 * If this leave statement is in a catch block, check for a
5081 * pending exception, and rethrow it if necessary.
5083 for (i = 0; i < header->num_clauses; ++i) {
5084 MonoExceptionClause *clause = &header->clauses [i];
5085 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code)) {
5088 temp = mono_emit_jit_icall (cfg, bblock, mono_thread_get_pending_exception, NULL, ip);
5089 NEW_TEMPLOAD (cfg, *sp, temp);
5091 MONO_INST_NEW (cfg, ins, OP_THROW_OR_NULL);
5092 ins->inst_left = *sp;
5094 MONO_ADD_INS (bblock, ins);
5098 /* fixme: call fault handler ? */
5100 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
5102 for (tmp = handlers; tmp; tmp = tmp->next) {
5104 link_bblock (cfg, bblock, tblock);
5105 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
5107 ins->inst_target_bb = tblock;
5108 MONO_ADD_INS (bblock, ins);
5110 g_list_free (handlers);
5113 MONO_INST_NEW (cfg, ins, CEE_BR);
5115 MONO_ADD_INS (bblock, ins);
5116 GET_BBLOCK (cfg, bbhash, tblock, target);
5117 link_bblock (cfg, bblock, tblock);
5118 CHECK_BBLOCK (target, ip, tblock);
5119 ins->inst_target_bb = tblock;
5120 start_new_bblock = 1;
5122 if (*ip == CEE_LEAVE)
5131 MONO_INST_NEW (cfg, ins, *ip);
5133 handle_loaded_temps (cfg, bblock, stack_start, sp);
5134 MONO_ADD_INS (bblock, ins);
5135 ins->cil_code = ip++;
5136 ins->inst_i0 = sp [0];
5137 ins->inst_i1 = sp [1];
5145 /* trampoline mono specific opcodes */
5146 case MONO_CUSTOM_PREFIX: {
5148 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
5153 case CEE_MONO_ICALL: {
5156 MonoJitICallInfo *info;
5158 token = read32 (ip + 2);
5159 func = mono_method_get_wrapper_data (method, token);
5160 info = mono_find_jit_icall_by_addr (func);
5163 CHECK_STACK (info->sig->param_count);
5164 sp -= info->sig->param_count;
5166 temp = mono_emit_jit_icall (cfg, bblock, info->func, sp, ip);
5167 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
5168 NEW_TEMPLOAD (cfg, *sp, temp);
5173 inline_costs += 10 * num_calls++;
5177 case CEE_MONO_LDPTR:
5178 CHECK_STACK_OVF (1);
5180 token = read32 (ip + 2);
5181 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
5185 inline_costs += 10 * num_calls++;
5187 case CEE_MONO_VTADDR:
5190 MONO_INST_NEW (cfg, ins, OP_VTADDR);
5192 ins->type = STACK_MP;
5193 ins->inst_left = *sp;
5197 case CEE_MONO_NEWOBJ: {
5198 MonoInst *iargs [2];
5200 CHECK_STACK_OVF (1);
5202 token = read32 (ip + 2);
5203 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5204 mono_class_init (klass);
5205 NEW_DOMAINCONST (cfg, iargs [0]);
5206 NEW_CLASSCONST (cfg, iargs [1], klass);
5207 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
5208 NEW_TEMPLOAD (cfg, *sp, temp);
5211 inline_costs += 10 * num_calls++;
5214 case CEE_MONO_OBJADDR:
5217 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
5219 ins->type = STACK_MP;
5220 ins->inst_left = *sp;
5224 case CEE_MONO_LDNATIVEOBJ:
5227 token = read32 (ip + 2);
5228 klass = mono_method_get_wrapper_data (method, token);
5229 g_assert (klass->valuetype);
5230 mono_class_init (klass);
5231 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
5235 case CEE_MONO_RETOBJ:
5236 g_assert (cfg->ret);
5237 g_assert (method->signature->pinvoke);
5242 token = read32 (ip + 2);
5243 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5245 NEW_RETLOADA (cfg, ins);
5246 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
5248 if (sp != stack_start)
5251 MONO_INST_NEW (cfg, ins, CEE_BR);
5253 ins->inst_target_bb = end_bblock;
5254 MONO_ADD_INS (bblock, ins);
5255 link_bblock (cfg, bblock, end_bblock);
5256 start_new_bblock = 1;
5259 case CEE_MONO_CISINST:
5260 case CEE_MONO_CCASTCLASS: {
5265 token = read32 (ip + 2);
5266 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5267 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_CISINST) ? OP_CISINST : OP_CCASTCLASS);
5268 ins->type = STACK_I4;
5269 ins->inst_left = *sp;
5270 ins->inst_newa_class = klass;
5272 *sp++ = emit_tree (cfg, bblock, ins);
5276 case CEE_MONO_SAVE_LMF:
5277 case CEE_MONO_RESTORE_LMF:
5278 #ifdef MONO_ARCH_HAVE_LMF_OPS
5279 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_SAVE_LMF) ? OP_SAVE_LMF : OP_RESTORE_LMF);
5280 MONO_ADD_INS (bblock, ins);
5281 cfg->need_lmf_area = TRUE;
5286 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
5295 /* somewhat similar to LDTOKEN */
5296 MonoInst *addr, *vtvar;
5297 CHECK_STACK_OVF (1);
5298 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
5300 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5301 addr->cil_code = ip;
5302 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
5304 ins->inst_left = addr;
5305 MONO_ADD_INS (bblock, ins);
5306 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5320 * The following transforms:
5321 * CEE_CEQ into OP_CEQ
5322 * CEE_CGT into OP_CGT
5323 * CEE_CGT_UN into OP_CGT_UN
5324 * CEE_CLT into OP_CLT
5325 * CEE_CLT_UN into OP_CLT_UN
5327 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
5329 MONO_INST_NEW (cfg, ins, cmp->opcode);
5331 cmp->inst_i0 = sp [0];
5332 cmp->inst_i1 = sp [1];
5336 if ((sp [0]->type == STACK_I8) || ((sizeof (gpointer) == 8) && ((sp [0]->type == STACK_PTR) || (sp [0]->type == STACK_OBJ) || (sp [0]->type == STACK_MP))))
5337 cmp->opcode = OP_LCOMPARE;
5339 cmp->opcode = OP_COMPARE;
5341 ins->type = STACK_I4;
5344 /* spill it to reduce the expression complexity
5345 * and workaround bug 54209
5347 if (cmp->inst_left->type == STACK_I8) {
5349 *sp++ = emit_tree (cfg, bblock, ins);
5358 CHECK_STACK_OVF (1);
5360 n = read32 (ip + 2);
5361 if (method->wrapper_type != MONO_WRAPPER_NONE)
5362 cmethod = mono_method_get_wrapper_data (method, n);
5364 cmethod = mono_get_method_full (image, n, NULL, generic_context);
5367 mono_class_init (cmethod->klass);
5368 handle_loaded_temps (cfg, bblock, stack_start, sp);
5370 NEW_METHODCONST (cfg, argconst, cmethod);
5371 if (method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED)
5372 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
5374 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn_nosync, &argconst, ip);
5375 NEW_TEMPLOAD (cfg, *sp, temp);
5379 inline_costs += 10 * num_calls++;
5382 case CEE_LDVIRTFTN: {
5388 n = read32 (ip + 2);
5389 if (method->wrapper_type != MONO_WRAPPER_NONE)
5390 cmethod = mono_method_get_wrapper_data (method, n);
5392 cmethod = mono_get_method_full (image, n, NULL, generic_context);
5394 mono_class_init (cmethod->klass);
5395 handle_loaded_temps (cfg, bblock, stack_start, sp);
5399 NEW_METHODCONST (cfg, args [1], cmethod);
5400 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
5401 NEW_TEMPLOAD (cfg, *sp, temp);
5405 inline_costs += 10 * num_calls++;
5409 CHECK_STACK_OVF (1);
5411 n = read16 (ip + 2);
5413 NEW_ARGLOAD (cfg, ins, n);
5419 CHECK_STACK_OVF (1);
5421 n = read16 (ip + 2);
5423 NEW_ARGLOADA (cfg, ins, n);
5431 handle_loaded_temps (cfg, bblock, stack_start, sp);
5433 n = read16 (ip + 2);
5435 NEW_ARGSTORE (cfg, ins, n, *sp);
5437 if (ins->opcode == CEE_STOBJ) {
5438 NEW_ARGLOADA (cfg, ins, n);
5439 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5441 MONO_ADD_INS (bblock, ins);
5445 CHECK_STACK_OVF (1);
5447 n = read16 (ip + 2);
5449 NEW_LOCLOAD (cfg, ins, n);
5455 CHECK_STACK_OVF (1);
5457 n = read16 (ip + 2);
5459 NEW_LOCLOADA (cfg, ins, n);
5468 n = read16 (ip + 2);
5470 handle_loaded_temps (cfg, bblock, stack_start, sp);
5471 NEW_LOCSTORE (cfg, ins, n, *sp);
5473 if (ins->opcode == CEE_STOBJ) {
5474 NEW_LOCLOADA (cfg, ins, n);
5475 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5477 MONO_ADD_INS (bblock, ins);
5484 if (sp != stack_start)
5486 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
5487 ins->inst_left = *sp;
5490 cfg->flags |= MONO_CFG_HAS_ALLOCA;
5491 if (header->init_locals)
5492 ins->flags |= MONO_INST_INIT;
5496 /* FIXME: set init flag if locals init is set in this method */
5498 case CEE_ENDFILTER: {
5499 MonoExceptionClause *clause, *nearest;
5500 int cc, nearest_num;
5504 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
5506 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
5507 ins->inst_left = *sp;
5509 MONO_ADD_INS (bblock, ins);
5510 start_new_bblock = 1;
5515 for (cc = 0; cc < header->num_clauses; ++cc) {
5516 clause = &header->clauses [cc];
5517 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
5518 (!nearest || (clause->token_or_filter > nearest->token_or_filter))) {
5524 filter_lengths [nearest_num] = (ip - header->code) - nearest->token_or_filter;
5528 case CEE_UNALIGNED_:
5529 ins_flag |= MONO_INST_UNALIGNED;
5530 /* FIXME: record alignment? we can assume 1 for now */
5535 ins_flag |= MONO_INST_VOLATILE;
5539 ins_flag |= MONO_INST_TAILCALL;
5540 /* Can't inline tail calls at this time */
5541 inline_costs += 100000;
5548 token = read32 (ip + 2);
5549 if (method->wrapper_type != MONO_WRAPPER_NONE)
5550 klass = mono_method_get_wrapper_data (method, token);
5552 klass = mono_class_get_full (image, token, generic_context);
5553 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5554 MonoInst *store, *load;
5555 NEW_PCONST (cfg, load, NULL);
5556 load->cil_code = ip;
5557 load->type = STACK_OBJ;
5558 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
5559 store->cil_code = ip;
5560 handle_loaded_temps (cfg, bblock, stack_start, sp);
5561 MONO_ADD_INS (bblock, store);
5562 store->inst_i0 = sp [0];
5563 store->inst_i1 = load;
5565 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
5570 case CEE_CONSTRAINED_:
5571 /* FIXME: implement */
5573 token = read32 (ip + 2);
5574 constrained_call = mono_class_get_full (image, token, generic_context);
5579 MonoInst *iargs [3];
5582 if ((cfg->opt & MONO_OPT_INTRINS) && (ip [1] == CEE_CPBLK) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5)) {
5584 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
5585 copy->inst_left = sp [0];
5586 copy->inst_right = sp [1];
5587 copy->cil_code = ip;
5589 MONO_ADD_INS (bblock, copy);
5596 handle_loaded_temps (cfg, bblock, stack_start, sp);
5597 if (ip [1] == CEE_CPBLK) {
5598 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
5600 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
5609 ins_flag |= MONO_INST_NOTYPECHECK;
5611 ins_flag |= MONO_INST_NORANGECHECK;
5612 /* we ignore the no-nullcheck for now since we
5613 * really do it explicitly only when doing callvirt->call
5619 /* FIXME: check we are in a catch handler */
5620 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
5621 load->cil_code = ip;
5622 MONO_INST_NEW (cfg, ins, CEE_THROW);
5623 ins->inst_left = load;
5625 MONO_ADD_INS (bblock, ins);
5627 start_new_bblock = 1;
5632 CHECK_STACK_OVF (1);
5634 token = read32 (ip + 2);
5635 /* FIXXME: handle generics. */
5636 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
5637 MonoType *type = mono_type_create_from_typespec (image, token);
5638 token = mono_type_size (type, &align);
5640 MonoClass *szclass = mono_class_get_full (image, token, generic_context);
5641 mono_class_init (szclass);
5642 token = mono_class_value_size (szclass, &align);
5644 NEW_ICONST (cfg, ins, token);
5649 case CEE_REFANYTYPE:
5651 MONO_INST_NEW (cfg, ins, OP_REFANYTYPE);
5653 ins->type = STACK_MP;
5654 ins->inst_left = *sp;
5655 ins->type = STACK_VTYPE;
5656 ins->klass = mono_defaults.typehandle_class;
5665 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
5670 g_error ("opcode 0x%02x not handled", *ip);
5673 if (start_new_bblock != 1)
5676 bblock->cil_length = ip - bblock->cil_code;
5677 bblock->next_bb = end_bblock;
5678 link_bblock (cfg, bblock, end_bblock);
5680 if (cfg->method == method && cfg->domainvar) {
5684 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
5685 call->signature = helper_sig_domain_get;
5686 call->inst.type = STACK_PTR;
5687 call->fptr = mono_domain_get;
5688 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, (MonoInst*)call);
5690 MONO_ADD_INS (init_localsbb, store);
5693 if (header->init_locals) {
5695 for (i = 0; i < header->num_locals; ++i) {
5696 int t = header->locals [i]->type;
5697 if (t == MONO_TYPE_VALUETYPE && header->locals [i]->data.klass->enumtype)
5698 t = header->locals [i]->data.klass->enum_basetype->type;
5699 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
5700 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
5701 NEW_ICONST (cfg, ins, 0);
5702 NEW_LOCSTORE (cfg, store, i, ins);
5703 MONO_ADD_INS (init_localsbb, store);
5704 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
5705 MONO_INST_NEW (cfg, ins, OP_I8CONST);
5706 ins->type = STACK_I8;
5708 NEW_LOCSTORE (cfg, store, i, ins);
5709 MONO_ADD_INS (init_localsbb, store);
5710 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
5711 MONO_INST_NEW (cfg, ins, OP_R8CONST);
5712 ins->type = STACK_R8;
5713 ins->inst_p0 = (void*)&r8_0;
5714 NEW_LOCSTORE (cfg, store, i, ins);
5715 MONO_ADD_INS (init_localsbb, store);
5716 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF)) {
5717 NEW_LOCLOADA (cfg, ins, i);
5718 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (header->locals [i]), NULL, NULL);
5721 NEW_PCONST (cfg, ins, NULL);
5722 NEW_LOCSTORE (cfg, store, i, ins);
5723 MONO_ADD_INS (init_localsbb, store);
5729 /* resolve backward branches in the middle of an existing basic block */
5730 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
5732 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
5733 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
5734 if (tblock != start_bblock) {
5736 split_bblock (cfg, tblock, bblock);
5737 l = bblock->cil_code - header->code;
5738 bblock->cil_length = tblock->cil_length - l;
5739 tblock->cil_length = l;
5741 g_print ("recheck failed.\n");
5746 * we compute regions here, because the length of filter clauses is not known in advance.
5747 * It is computed in the CEE_ENDFILTER case in the above switch statement
5749 if (cfg->method == method) {
5751 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5752 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
5754 mono_create_spvar_for_region (cfg, bb->region);
5755 if (cfg->verbose_level > 2)
5756 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
5759 g_hash_table_destroy (bbhash);
5762 dont_inline = g_list_remove (dont_inline, method);
5763 return inline_costs;
5766 if (cfg->method != method)
5767 g_hash_table_destroy (bbhash);
5768 dont_inline = g_list_remove (dont_inline, method);
5772 if (cfg->method != method)
5773 g_hash_table_destroy (bbhash);
5774 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
5775 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
5776 dont_inline = g_list_remove (dont_inline, method);
5781 mono_print_tree (MonoInst *tree) {
5787 arity = mono_burg_arity [tree->opcode];
5789 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
5791 switch (tree->opcode) {
5793 printf ("[%d]", tree->inst_c0);
5796 printf ("[%lld]", tree->inst_l);
5799 printf ("[%f]", *(double*)tree->inst_p0);
5802 printf ("[%f]", *(float*)tree->inst_p0);
5806 printf ("[%d]", tree->inst_c0);
5809 if (tree->inst_offset < 0)
5810 printf ("[-0x%x(%s)]", -tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5812 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5815 printf ("[%s]", mono_arch_regname (tree->dreg));
5818 printf ("[%s]", tree->inst_newa_class->name);
5819 mono_print_tree (tree->inst_newa_len);
5830 case OP_VOIDCALLVIRT: {
5831 MonoCallInst *call = (MonoCallInst*)tree;
5833 printf ("[%s]", call->method->name);
5838 printf ("[%d (", tree->inst_c0);
5839 for (i = 0; i < tree->inst_phi_args [0]; i++) {
5842 printf ("%d", tree->inst_phi_args [i + 1]);
5853 case OP_LOAD_MEMBASE:
5854 case OP_LOADI4_MEMBASE:
5855 case OP_LOADU4_MEMBASE:
5856 case OP_LOADU1_MEMBASE:
5857 case OP_LOADI1_MEMBASE:
5858 case OP_LOADU2_MEMBASE:
5859 case OP_LOADI2_MEMBASE:
5860 printf ("[%s] <- [%s + 0x%x]", mono_arch_regname (tree->dreg), mono_arch_regname (tree->inst_basereg), tree->inst_offset);
5863 printf ("[B%d]", tree->inst_target_bb->block_num);
5873 case OP_VOIDCALL_REG:
5874 mono_print_tree (tree->inst_left);
5886 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
5887 mono_print_tree (tree->inst_left);
5890 if (!mono_arch_print_tree(tree, arity)) {
5892 mono_print_tree (tree->inst_left);
5894 mono_print_tree (tree->inst_right);
5905 mono_print_tree_nl (MonoInst *tree)
5907 mono_print_tree (tree);
5911 #define make_icall_sig mono_create_icall_signature
5914 create_helper_signature (void)
5916 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
5917 helper_sig_newarr = make_icall_sig ("object ptr ptr int32");
5919 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
5920 helper_sig_newarr_specific = make_icall_sig ("object ptr int32");
5922 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
5923 helper_sig_object_new = make_icall_sig ("object ptr ptr");
5925 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
5926 helper_sig_object_new_specific = make_icall_sig ("object ptr");
5928 /* void* mono_method_compile (MonoMethod*) */
5929 helper_sig_compile = make_icall_sig ("ptr ptr");
5931 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
5932 helper_sig_compile_virt = make_icall_sig ("ptr object ptr");
5934 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
5935 helper_sig_ldstr = make_icall_sig ("object ptr ptr int32");
5937 /* MonoDomain *mono_domain_get (void) */
5938 helper_sig_domain_get = make_icall_sig ("ptr");
5940 /* void stelem_ref (MonoArray *, int index, MonoObject *) */
5941 helper_sig_stelem_ref = make_icall_sig ("void ptr int32 object");
5943 /* void stelem_ref_check (MonoArray *, MonoObject *) */
5944 helper_sig_stelem_ref_check = make_icall_sig ("void object object");
5946 /* long amethod (long, long) */
5947 helper_sig_long_long_long = make_icall_sig ("long long long");
5949 /* object amethod (intptr) */
5950 helper_sig_obj_ptr = make_icall_sig ("object ptr");
5952 helper_sig_obj_ptr_ptr = make_icall_sig ("object ptr ptr");
5954 helper_sig_obj_obj_ptr_ptr = make_icall_sig ("object object ptr ptr");
5956 helper_sig_void_void = make_icall_sig ("void");
5958 /* void amethod (intptr) */
5959 helper_sig_void_ptr = make_icall_sig ("void ptr");
5961 /* void amethod (MonoObject *obj) */
5962 helper_sig_void_obj = make_icall_sig ("void object");
5964 /* void amethod (MonoObject *obj, void *ptr, int i) */
5965 helper_sig_void_obj_ptr_int = make_icall_sig ("void object ptr int");
5967 helper_sig_void_obj_ptr_ptr_obj = make_icall_sig ("void object ptr ptr object");
5969 /* intptr amethod (void) */
5970 helper_sig_ptr_void = make_icall_sig ("ptr");
5972 /* object amethod (void) */
5973 helper_sig_obj_void = make_icall_sig ("object");
5975 /* void amethod (intptr, intptr) */
5976 helper_sig_void_ptr_ptr = make_icall_sig ("void ptr ptr");
5978 /* void amethod (intptr, intptr, intptr) */
5979 helper_sig_void_ptr_ptr_ptr = make_icall_sig ("void ptr ptr ptr");
5981 /* intptr amethod (intptr, intptr) */
5982 helper_sig_ptr_ptr_ptr = make_icall_sig ("ptr ptr ptr");
5984 /* IntPtr amethod (object) */
5985 helper_sig_ptr_obj = make_icall_sig ("ptr object");
5987 /* IntPtr amethod (object, int) */
5988 helper_sig_ptr_obj_int = make_icall_sig ("ptr object int");
5990 /* IntPtr amethod (int) */
5991 helper_sig_ptr_int = make_icall_sig ("ptr int32");
5993 /* long amethod (long, guint32) */
5994 helper_sig_long_long_int = make_icall_sig ("long long int32");
5996 /* ulong amethod (double) */
5997 helper_sig_ulong_double = make_icall_sig ("ulong double");
5999 /* long amethod (double) */
6000 helper_sig_long_double = make_icall_sig ("long double");
6002 /* double amethod (long) */
6003 helper_sig_double_long = make_icall_sig ("double long");
6005 /* double amethod (int) */
6006 helper_sig_double_int = make_icall_sig ("double int32");
6008 /* float amethod (long) */
6009 helper_sig_float_long = make_icall_sig ("float long");
6011 /* double amethod (double, double) */
6012 helper_sig_double_double_double = make_icall_sig ("double double double");
6014 /* uint amethod (double) */
6015 helper_sig_uint_double = make_icall_sig ("uint32 double");
6017 /* int amethod (double) */
6018 helper_sig_int_double = make_icall_sig ("int32 double");
6020 /* void initobj (intptr, int size) */
6021 helper_sig_initobj = make_icall_sig ("void ptr int32");
6023 /* void memcpy (intptr, intptr, int size) */
6024 helper_sig_memcpy = make_icall_sig ("void ptr ptr int32");
6026 /* void memset (intptr, int val, int size) */
6027 helper_sig_memset = make_icall_sig ("void ptr int32 int32");
6029 helper_sig_class_init_trampoline = make_icall_sig ("void");
6033 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
6036 MonoMethod *wrapper;
6039 if (callinfo->wrapper)
6040 return callinfo->wrapper;
6042 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
6043 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
6044 /* Must be domain neutral since there is only one copy */
6045 code = mono_jit_compile_method_with_opt (wrapper, default_opt | MONO_OPT_SHARED);
6047 if (!callinfo->wrapper) {
6048 callinfo->wrapper = code;
6049 mono_register_jit_icall_wrapper (callinfo, code);
6050 mono_debug_add_icall_wrapper (wrapper, callinfo);
6054 return callinfo->wrapper;
6058 mono_create_class_init_trampoline (MonoVTable *vtable)
6062 /* previously created trampoline code */
6063 mono_domain_lock (vtable->domain);
6065 mono_g_hash_table_lookup (vtable->domain->class_init_trampoline_hash,
6067 mono_domain_unlock (vtable->domain);
6071 code = mono_arch_create_class_init_trampoline (vtable);
6073 /* store trampoline address */
6074 mono_domain_lock (vtable->domain);
6075 mono_g_hash_table_insert (vtable->domain->class_init_trampoline_hash,
6077 mono_domain_unlock (vtable->domain);
6079 EnterCriticalSection (&trampoline_hash_mutex);
6080 if (!class_init_hash_addr)
6081 class_init_hash_addr = g_hash_table_new (NULL, NULL);
6082 g_hash_table_insert (class_init_hash_addr, code, vtable);
6083 LeaveCriticalSection (&trampoline_hash_mutex);
6089 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method,
6090 gboolean add_sync_wrapper)
6095 if (add_sync_wrapper && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
6096 return mono_create_jump_trampoline (domain, mono_marshal_get_synchronized_wrapper (method), FALSE);
6098 code = mono_jit_find_compiled_method (domain, method);
6102 EnterCriticalSection (&trampoline_hash_mutex);
6104 if (jump_trampoline_hash) {
6105 code = g_hash_table_lookup (jump_trampoline_hash, method);
6107 LeaveCriticalSection (&trampoline_hash_mutex);
6112 ji = mono_arch_create_jump_trampoline (method);
6115 * mono_delegate_ctor needs to find the method metadata from the
6116 * trampoline address, so we save it here.
6119 mono_jit_info_table_add (mono_get_root_domain (), ji);
6121 if (!jump_trampoline_hash)
6122 jump_trampoline_hash = g_hash_table_new (NULL, NULL);
6123 g_hash_table_insert (jump_trampoline_hash, method, ji->code_start);
6125 LeaveCriticalSection (&trampoline_hash_mutex);
6127 return ji->code_start;
6131 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
6135 EnterCriticalSection (&trampoline_hash_mutex);
6136 if (class_init_hash_addr)
6137 res = g_hash_table_lookup (class_init_hash_addr, addr);
6140 LeaveCriticalSection (&trampoline_hash_mutex);
6145 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func, gboolean no_throw)
6147 MonoJitICallInfo *info;
6149 if (!emul_opcode_map)
6150 emul_opcode_map = g_new0 (MonoJitICallInfo*, OP_LAST + 1);
6152 g_assert (!sig->hasthis);
6153 g_assert (sig->param_count < 3);
6155 info = mono_register_jit_icall (func, name, sig, no_throw);
6157 emul_opcode_map [opcode] = info;
6161 decompose_foreach (MonoInst *tree, gpointer data)
6163 static MonoJitICallInfo *newarr_info = NULL;
6164 static MonoJitICallInfo *newarr_specific_info = NULL;
6165 MonoJitICallInfo *info;
6168 switch (tree->opcode) {
6170 MonoCompile *cfg = data;
6171 MonoInst *iargs [3];
6174 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
6175 g_assert (newarr_info);
6176 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
6177 g_assert (newarr_specific_info);
6180 if (cfg->opt & MONO_OPT_SHARED) {
6181 NEW_DOMAINCONST (cfg, iargs [0]);
6182 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
6183 iargs [2] = tree->inst_newa_len;
6188 MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
6190 NEW_VTABLECONST (cfg, iargs [0], vtable);
6191 iargs [1] = tree->inst_newa_len;
6193 info = newarr_specific_info;
6196 mono_emulate_opcode (cfg, tree, iargs, info);
6198 /* Need to decompose arguments after the the opcode is decomposed */
6199 for (i = 0; i < info->sig->param_count; ++i)
6200 dec_foreach (iargs [i], cfg);
6210 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
6212 switch (mono_burg_arity [tree->opcode]) {
6215 mono_inst_foreach (tree->inst_left, func, data);
6218 mono_inst_foreach (tree->inst_left, func, data);
6219 mono_inst_foreach (tree->inst_right, func, data);
6222 g_assert_not_reached ();
6229 mono_print_bb_code (MonoBasicBlock *bb) {
6231 MonoInst *c = bb->code;
6233 mono_print_tree (c);
6241 print_dfn (MonoCompile *cfg) {
6246 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
6248 for (i = 0; i < cfg->num_bblocks; ++i) {
6249 bb = cfg->bblocks [i];
6251 char* code1, *code2;
6252 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
6253 if (bb->last_ins->cil_code)
6254 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
6256 code2 = g_strdup ("");
6258 code1 [strlen (code1) - 1] = 0;
6259 code = g_strdup_printf ("%s -> %s", code1, code2);
6263 code = g_strdup ("\n");
6264 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
6266 MonoInst *c = bb->code;
6268 mono_print_tree (c);
6276 g_print ("\tprev:");
6277 for (j = 0; j < bb->in_count; ++j) {
6278 g_print (" BB%d", bb->in_bb [j]->block_num);
6280 g_print ("\t\tsucc:");
6281 for (j = 0; j < bb->out_count; ++j) {
6282 g_print (" BB%d", bb->out_bb [j]->block_num);
6284 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
6287 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
6290 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
6292 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
6300 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
6304 g_assert (bb->code);
6305 bb->last_ins->next = inst;
6306 bb->last_ins = inst;
6308 bb->last_ins = bb->code = inst;
6313 mono_destroy_compile (MonoCompile *cfg)
6315 //mono_mempool_stats (cfg->mempool);
6316 g_hash_table_destroy (cfg->bb_hash);
6317 mono_free_loop_info (cfg);
6319 mono_regstate_free (cfg->rs);
6321 g_hash_table_destroy (cfg->spvars);
6322 mono_mempool_destroy (cfg->mempool);
6323 g_list_free (cfg->ldstr_list);
6325 g_free (cfg->varinfo);
6330 #ifdef HAVE_KW_THREAD
6331 static __thread gpointer mono_lmf_addr;
6335 mono_get_lmf_addr (void)
6337 #ifdef HAVE_KW_THREAD
6338 return mono_lmf_addr;
6340 MonoJitTlsData *jit_tls;
6342 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
6343 return &jit_tls->lmf;
6345 g_assert_not_reached ();
6351 * mono_thread_abort:
6352 * @obj: exception object
6354 * abort the thread, print exception information and stack trace
6357 mono_thread_abort (MonoObject *obj)
6359 /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
6361 /* handle_remove should be eventually called for this thread, too
6368 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
6370 MonoJitTlsData *jit_tls;
6373 jit_tls = TlsGetValue (mono_jit_tls_id);
6377 jit_tls = g_new0 (MonoJitTlsData, 1);
6379 TlsSetValue (mono_jit_tls_id, jit_tls);
6381 jit_tls->abort_func = abort_func;
6382 jit_tls->end_of_stack = stack_start;
6384 lmf = g_new0 (MonoLMF, 1);
6387 jit_tls->lmf = jit_tls->first_lmf = lmf;
6389 #ifdef HAVE_KW_THREAD
6390 mono_lmf_addr = &jit_tls->lmf;
6393 mono_arch_setup_jit_tls_data (jit_tls);
6399 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
6402 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
6403 thread = mono_thread_current ();
6405 thread->jit_data = jit_tls;
6408 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
6411 mono_thread_abort_dummy (MonoObject *obj)
6413 if (mono_thread_attach_aborted_cb)
6414 mono_thread_attach_aborted_cb (obj);
6416 mono_thread_abort (obj);
6420 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
6423 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
6424 thread = mono_thread_current ();
6426 thread->jit_data = jit_tls;
6430 mini_thread_cleanup (MonoThread *thread)
6432 MonoJitTlsData *jit_tls = thread->jit_data;
6435 mono_arch_free_jit_tls_data (jit_tls);
6436 g_free (jit_tls->first_lmf);
6438 thread->jit_data = NULL;
6443 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
6445 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
6449 ji->data.target = target;
6450 ji->next = cfg->patch_info;
6452 cfg->patch_info = ji;
6456 mono_remove_patch_info (MonoCompile *cfg, int ip)
6458 MonoJumpInfo **ji = &cfg->patch_info;
6461 if ((*ji)->ip.i == ip)
6464 ji = &((*ji)->next);
6469 mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors)
6471 unsigned char *ip = patch_info->ip.i + code;
6472 gconstpointer target = NULL;
6474 switch (patch_info->type) {
6475 case MONO_PATCH_INFO_BB:
6476 target = patch_info->data.bb->native_offset + code;
6478 case MONO_PATCH_INFO_ABS:
6479 target = patch_info->data.target;
6481 case MONO_PATCH_INFO_LABEL:
6482 target = patch_info->data.inst->inst_c0 + code;
6484 case MONO_PATCH_INFO_IP:
6487 case MONO_PATCH_INFO_METHOD_REL:
6488 target = code + patch_info->data.offset;
6490 case MONO_PATCH_INFO_INTERNAL_METHOD: {
6491 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
6493 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
6494 g_assert_not_reached ();
6496 target = mono_icall_get_wrapper (mi);
6499 case MONO_PATCH_INFO_METHOD_JUMP: {
6502 /* get the trampoline to the method from the domain */
6503 target = mono_create_jump_trampoline (domain, patch_info->data.method, TRUE);
6504 if (!domain->jump_target_hash)
6505 domain->jump_target_hash = g_hash_table_new (NULL, NULL);
6506 list = g_hash_table_lookup (domain->jump_target_hash, patch_info->data.method);
6507 list = g_slist_prepend (list, ip);
6508 g_hash_table_insert (domain->jump_target_hash, patch_info->data.method, list);
6511 case MONO_PATCH_INFO_METHOD:
6512 if (patch_info->data.method == method) {
6515 /* get the trampoline to the method from the domain */
6516 target = mono_arch_create_jit_trampoline (patch_info->data.method);
6518 case MONO_PATCH_INFO_SWITCH: {
6519 gpointer *jump_table;
6522 mono_domain_lock (domain);
6523 jump_table = mono_code_manager_reserve (domain->code_mp, sizeof (gpointer) * patch_info->table_size);
6524 mono_domain_unlock (domain);
6526 for (i = 0; i < patch_info->table_size; i++) {
6527 jump_table [i] = code + (int)patch_info->data.table [i];
6529 target = jump_table;
6532 case MONO_PATCH_INFO_METHODCONST:
6533 case MONO_PATCH_INFO_CLASS:
6534 case MONO_PATCH_INFO_IMAGE:
6535 case MONO_PATCH_INFO_FIELD:
6536 target = patch_info->data.target;
6538 case MONO_PATCH_INFO_IID:
6539 mono_class_init (patch_info->data.klass);
6540 target = (gpointer)patch_info->data.klass->interface_id;
6542 case MONO_PATCH_INFO_VTABLE:
6543 target = mono_class_vtable (domain, patch_info->data.klass);
6545 case MONO_PATCH_INFO_CLASS_INIT:
6546 target = mono_create_class_init_trampoline (mono_class_vtable (domain, patch_info->data.klass));
6548 case MONO_PATCH_INFO_SFLDA: {
6549 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
6550 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (vtable->klass, method))
6551 /* Done by the generated code */
6555 mono_runtime_class_init (vtable);
6557 target = (char*)vtable->data + patch_info->data.field->offset;
6560 case MONO_PATCH_INFO_R4:
6561 case MONO_PATCH_INFO_R8:
6562 target = patch_info->data.target;
6564 case MONO_PATCH_INFO_EXC_NAME:
6565 target = patch_info->data.name;
6567 case MONO_PATCH_INFO_LDSTR:
6569 mono_ldstr (domain, patch_info->data.token->image,
6570 mono_metadata_token_index (patch_info->data.token->token));
6572 case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
6574 MonoClass *handle_class;
6576 handle = mono_ldtoken (patch_info->data.token->image,
6577 patch_info->data.token->token, &handle_class, NULL);
6578 mono_class_init (handle_class);
6579 mono_class_init (mono_class_from_mono_type (handle));
6582 mono_type_get_object (domain, handle);
6585 case MONO_PATCH_INFO_LDTOKEN: {
6587 MonoClass *handle_class;
6589 handle = mono_ldtoken (patch_info->data.token->image,
6590 patch_info->data.token->token, &handle_class, NULL);
6591 mono_class_init (handle_class);
6596 case MONO_PATCH_INFO_BB_OVF:
6597 case MONO_PATCH_INFO_EXC_OVF:
6600 g_assert_not_reached ();
6603 return (gpointer)target;
6607 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
6608 MonoJitICallInfo *info;
6610 decompose_foreach (tree, cfg);
6612 switch (mono_burg_arity [tree->opcode]) {
6615 dec_foreach (tree->inst_left, cfg);
6617 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6618 MonoInst *iargs [2];
6620 iargs [0] = tree->inst_left;
6622 mono_emulate_opcode (cfg, tree, iargs, info);
6628 #ifdef MONO_ARCH_BIGMUL_INTRINS
6629 if (tree->opcode == OP_LMUL
6630 && (cfg->opt & MONO_OPT_INTRINS)
6631 && (tree->inst_left->opcode == CEE_CONV_I8
6632 || tree->inst_left->opcode == CEE_CONV_U8)
6633 && tree->inst_left->inst_left->type == STACK_I4
6634 && (tree->inst_right->opcode == CEE_CONV_I8
6635 || tree->inst_right->opcode == CEE_CONV_U8)
6636 && tree->inst_right->inst_left->type == STACK_I4
6637 && tree->inst_left->opcode == tree->inst_right->opcode) {
6638 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
6639 tree->inst_left = tree->inst_left->inst_left;
6640 tree->inst_right = tree->inst_right->inst_left;
6641 dec_foreach (tree, cfg);
6644 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6645 MonoInst *iargs [2];
6647 iargs [0] = tree->inst_i0;
6648 iargs [1] = tree->inst_i1;
6650 mono_emulate_opcode (cfg, tree, iargs, info);
6652 dec_foreach (iargs [0], cfg);
6653 dec_foreach (iargs [1], cfg);
6656 dec_foreach (tree->inst_left, cfg);
6657 dec_foreach (tree->inst_right, cfg);
6661 g_assert_not_reached ();
6666 decompose_pass (MonoCompile *cfg) {
6669 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6672 cfg->prev_ins = NULL;
6673 for (tree = cfg->cbb->code; tree; tree = tree->next) {
6674 dec_foreach (tree, cfg);
6675 cfg->prev_ins = tree;
6681 nullify_basic_block (MonoBasicBlock *bb)
6688 bb->code = bb->last_ins = NULL;
6689 bb->cil_code = NULL;
6693 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6697 for (i = 0; i < bb->out_count; i++) {
6698 MonoBasicBlock *ob = bb->out_bb [i];
6701 if (bb->out_count > 1) {
6702 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
6706 bb->out_bb [i] = repl;
6713 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6717 for (i = 0; i < bb->in_count; i++) {
6718 MonoBasicBlock *ib = bb->in_bb [i];
6721 if (bb->in_count > 1) {
6722 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
6726 bb->in_bb [i] = repl;
6733 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6737 for (i = 0; i < bb->out_count; i++) {
6738 MonoBasicBlock *ob = bb->out_bb [i];
6739 for (j = 0; j < ob->in_count; j++) {
6740 if (ob->in_bb [j] == orig) {
6741 ob->in_bb [j] = repl;
6750 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
6752 bb->out_count = bbn->out_count;
6753 bb->out_bb = bbn->out_bb;
6755 replace_basic_block (bb, bbn, bb);
6759 bb->last_ins->next = bbn->code;
6760 bb->last_ins = bbn->last_ins;
6763 bb->code = bbn->code;
6764 bb->last_ins = bbn->last_ins;
6766 bb->next_bb = bbn->next_bb;
6767 nullify_basic_block (bbn);
6771 * Optimizes the branches on the Control Flow Graph
6775 optimize_branches (MonoCompile *cfg)
6777 int i, changed = FALSE;
6778 MonoBasicBlock *bb, *bbn;
6779 guint32 niterations;
6782 * Some crazy loops could cause the code below to go into an infinite
6783 * loop, see bug #53003 for an example. To prevent this, we put an upper
6784 * bound on the number of iterations.
6791 /* we skip the entry block (exit is handled specially instead ) */
6792 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
6794 /* dont touch code inside exception clauses */
6795 if (bb->region != -1)
6798 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
6799 if (cfg->verbose_level > 2)
6800 g_print ("nullify block triggered %d\n", bbn->block_num);
6802 bb->next_bb = bbn->next_bb;
6804 for (i = 0; i < bbn->out_count; i++)
6805 replace_in_block (bbn->out_bb [i], bbn, NULL);
6807 nullify_basic_block (bbn);
6811 if (bb->out_count == 1) {
6812 bbn = bb->out_bb [0];
6814 /* conditional branches where true and false targets are the same can be also replaced with CEE_BR */
6815 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins)) {
6816 bb->last_ins->opcode = CEE_BR;
6817 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
6819 if (cfg->verbose_level > 2)
6820 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
6823 if (bb->region == bbn->region && bb->next_bb == bbn) {
6824 /* the block are in sequence anyway ... */
6826 /* branches to the following block can be removed */
6827 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
6828 bb->last_ins->opcode = CEE_NOP;
6830 if (cfg->verbose_level > 2)
6831 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
6834 if (bbn->in_count == 1) {
6836 if (bbn != cfg->bb_exit) {
6837 if (cfg->verbose_level > 2)
6838 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
6839 merge_basic_blocks (bb, bbn);
6843 //mono_print_bb_code (bb);
6848 } while (changed && (niterations > 0));
6855 /* we skip the entry block (exit is handled specially instead ) */
6856 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
6858 /* dont touch code inside exception clauses */
6859 if (bb->region != -1)
6862 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
6863 if (cfg->verbose_level > 2) {
6864 g_print ("nullify block triggered %d\n", bbn->block_num);
6866 bb->next_bb = bbn->next_bb;
6868 for (i = 0; i < bbn->out_count; i++)
6869 replace_in_block (bbn->out_bb [i], bbn, NULL);
6871 nullify_basic_block (bbn);
6877 if (bb->out_count == 1) {
6878 bbn = bb->out_bb [0];
6880 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
6881 bbn = bb->last_ins->inst_target_bb;
6882 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
6883 bbn->code->inst_target_bb->region == bb->region) {
6885 if (cfg->verbose_level > 2)
6886 g_print ("in %s branch to branch triggered %d -> %d -> %d\n", cfg->method->name,
6887 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num);
6889 replace_in_block (bbn, bb, NULL);
6890 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
6891 link_bblock (cfg, bb, bbn->code->inst_target_bb);
6892 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
6897 } else if (bb->out_count == 2) {
6898 if (bb->last_ins && MONO_IS_COND_BRANCH_NOFP (bb->last_ins)) {
6899 bbn = bb->last_ins->inst_true_bb;
6900 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
6901 bbn->code->inst_target_bb->region == bb->region) {
6902 if (cfg->verbose_level > 2)
6903 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
6904 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
6907 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
6909 replace_in_block (bbn, bb, NULL);
6911 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
6912 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
6914 link_bblock (cfg, bb, bbn->code->inst_target_bb);
6920 bbn = bb->last_ins->inst_false_bb;
6921 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
6922 bbn->code->inst_target_bb->region == bb->region) {
6923 if (cfg->verbose_level > 2)
6924 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
6925 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
6928 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
6930 replace_in_block (bbn, bb, NULL);
6932 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
6933 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
6935 link_bblock (cfg, bb, bbn->code->inst_target_bb);
6943 } while (changed && (niterations > 0));
6948 mono_compile_create_vars (MonoCompile *cfg)
6950 MonoMethodSignature *sig;
6951 MonoMethodHeader *header;
6954 header = ((MonoMethodNormal *)cfg->method)->header;
6956 sig = cfg->method->signature;
6958 if (!MONO_TYPE_IS_VOID (sig->ret)) {
6959 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
6960 cfg->ret->opcode = OP_RETARG;
6961 cfg->ret->inst_vtype = sig->ret;
6962 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
6964 if (cfg->verbose_level > 2)
6965 g_print ("creating vars\n");
6968 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
6970 for (i = 0; i < sig->param_count; ++i)
6971 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
6973 cfg->locals_start = cfg->num_varinfo;
6975 if (cfg->verbose_level > 2)
6976 g_print ("creating locals\n");
6977 for (i = 0; i < header->num_locals; ++i)
6978 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
6979 if (cfg->verbose_level > 2)
6980 g_print ("locals done\n");
6984 mono_print_code (MonoCompile *cfg)
6988 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6989 MonoInst *tree = bb->code;
6994 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
6996 for (; tree; tree = tree->next) {
6997 mono_print_tree (tree);
7002 bb->last_ins->next = NULL;
7006 extern const char * const mono_burg_rule_string [];
7009 emit_state (MonoCompile *cfg, MBState *state, int goal)
7012 int ern = mono_burg_rule (state, goal);
7013 const guint16 *nts = mono_burg_nts [ern];
7016 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
7020 // state->reg1 = state->reg2; /* chain rule */
7022 state->reg1 = mono_regstate_next_int (cfg->rs);
7023 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
7026 state->reg1 = mono_regstate_next_int (cfg->rs);
7027 state->reg2 = mono_regstate_next_int (cfg->rs);
7030 state->reg1 = mono_regstate_next_float (cfg->rs);
7037 mono_burg_kids (state, ern, kids);
7039 emit_state (cfg, kids [0], nts [0]);
7041 emit_state (cfg, kids [1], nts [1]);
7043 g_assert (!nts [3]);
7044 emit_state (cfg, kids [2], nts [2]);
7049 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
7050 if ((emit = mono_burg_func [ern]))
7051 emit (state, state->tree, cfg);
7054 #define DEBUG_SELECTION
7057 mini_select_instructions (MonoCompile *cfg)
7059 static const int reverse_map [] = {
7060 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
7061 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
7063 static const int reverse_fmap [] = {
7064 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
7065 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
7067 static const int reverse_lmap [] = {
7068 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
7069 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
7071 static const int reverse_imap [] = {
7072 OP_IBNE_UN, OP_IBLT, OP_IBLE, OP_IBGT, OP_IBGE,
7073 OP_IBEQ, OP_IBLT_UN, OP_IBLE_UN, OP_IBGT_UN, OP_IBGE_UN
7078 cfg->state_pool = mono_mempool_new ();
7079 cfg->rs = mono_regstate_new ();
7081 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7082 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins) &&
7083 bb->next_bb != bb->last_ins->inst_false_bb) {
7085 /* we are careful when inverting, since bugs like #59580
7086 * could show up when dealing with NaNs.
7088 if (MONO_IS_COND_BRANCH_NOFP(bb->last_ins) && bb->next_bb == bb->last_ins->inst_true_bb) {
7089 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
7090 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
7091 bb->last_ins->inst_false_bb = tmp;
7093 if (bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
7094 bb->last_ins->opcode = reverse_map [bb->last_ins->opcode - CEE_BEQ];
7095 } else if (bb->last_ins->opcode >= OP_FBEQ && bb->last_ins->opcode <= OP_FBLT_UN) {
7096 bb->last_ins->opcode = reverse_fmap [bb->last_ins->opcode - OP_FBEQ];
7097 } else if (bb->last_ins->opcode >= OP_LBEQ && bb->last_ins->opcode <= OP_LBLT_UN) {
7098 bb->last_ins->opcode = reverse_lmap [bb->last_ins->opcode - OP_LBEQ];
7099 } else if (bb->last_ins->opcode >= OP_IBEQ && bb->last_ins->opcode <= OP_IBLT_UN) {
7100 bb->last_ins->opcode = reverse_imap [bb->last_ins->opcode - OP_IBEQ];
7103 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
7104 inst->opcode = CEE_BR;
7105 inst->inst_target_bb = bb->last_ins->inst_false_bb;
7106 mono_bblock_add_inst (bb, inst);
7111 #ifdef DEBUG_SELECTION
7112 if (cfg->verbose_level >= 4) {
7113 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7114 MonoInst *tree = bb->code;
7115 g_print ("DUMP BLOCK %d:\n", bb->block_num);
7118 for (; tree; tree = tree->next) {
7119 mono_print_tree (tree);
7126 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7127 MonoInst *tree = bb->code, *next;
7133 bb->last_ins = NULL;
7136 mono_regstate_reset (cfg->rs);
7138 #ifdef DEBUG_SELECTION
7139 if (cfg->verbose_level >= 3)
7140 g_print ("LABEL BLOCK %d:\n", bb->block_num);
7142 for (; tree; tree = next) {
7144 #ifdef DEBUG_SELECTION
7145 if (cfg->verbose_level >= 3) {
7146 mono_print_tree (tree);
7151 if (!(mbstate = mono_burg_label (tree, cfg))) {
7152 g_warning ("unable to label tree %p", tree);
7153 mono_print_tree (tree);
7155 g_assert_not_reached ();
7157 emit_state (cfg, mbstate, MB_NTERM_stmt);
7159 bb->max_ireg = cfg->rs->next_vireg;
7160 bb->max_freg = cfg->rs->next_vfreg;
7163 bb->last_ins->next = NULL;
7165 mono_mempool_empty (cfg->state_pool);
7167 mono_mempool_destroy (cfg->state_pool);
7171 mono_codegen (MonoCompile *cfg)
7173 MonoJumpInfo *patch_info;
7175 int i, max_epilog_size;
7178 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7179 cfg->spill_count = 0;
7180 /* we reuse dfn here */
7181 /* bb->dfn = bb_count++; */
7182 mono_arch_local_regalloc (cfg, bb);
7185 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
7186 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
7188 code = mono_arch_emit_prolog (cfg);
7190 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7191 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
7193 cfg->code_len = code - cfg->native_code;
7194 cfg->prolog_end = cfg->code_len;
7196 mono_debug_open_method (cfg);
7198 /* emit code all basic blocks */
7199 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7200 bb->native_offset = cfg->code_len;
7201 mono_arch_output_basic_block (cfg, bb);
7203 cfg->bb_exit->native_offset = cfg->code_len;
7205 code = cfg->native_code + cfg->code_len;
7207 max_epilog_size = mono_arch_max_epilog_size (cfg);
7209 /* we always allocate code in cfg->domain->code_mp to increase locality */
7210 cfg->code_size = cfg->code_len + max_epilog_size;
7211 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
7213 mono_domain_lock (cfg->domain);
7214 code = mono_code_manager_reserve (cfg->domain->code_mp, cfg->code_size);
7215 mono_domain_unlock (cfg->domain);
7217 memcpy (code, cfg->native_code, cfg->code_len);
7218 g_free (cfg->native_code);
7219 cfg->native_code = code;
7220 code = cfg->native_code + cfg->code_len;
7222 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
7224 cfg->epilog_begin = cfg->code_len;
7226 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7227 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
7229 cfg->code_len = code - cfg->native_code;
7231 mono_arch_emit_epilog (cfg);
7233 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7234 switch (patch_info->type) {
7235 case MONO_PATCH_INFO_ABS: {
7236 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
7238 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
7239 if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) &&
7240 strstr (cfg->method->name, info->name))
7242 * This is an icall wrapper, and this is a call to the
7247 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
7248 patch_info->data.name = info->name;
7252 MonoVTable *vtable = mono_find_class_init_trampoline_by_addr (patch_info->data.target);
7254 patch_info->type = MONO_PATCH_INFO_CLASS_INIT;
7255 patch_info->data.klass = vtable->klass;
7260 case MONO_PATCH_INFO_SWITCH: {
7262 mono_domain_lock (cfg->domain);
7263 table = mono_code_manager_reserve (cfg->domain->code_mp, sizeof (gpointer) * patch_info->table_size);
7264 mono_domain_unlock (cfg->domain);
7266 patch_info->ip.i = patch_info->ip.label->inst_c0;
7267 for (i = 0; i < patch_info->table_size; i++) {
7268 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
7270 patch_info->data.target = table;
7279 if (cfg->verbose_level > 0)
7280 g_print ("Method %s emitted at %p to %p [%s]\n",
7281 mono_method_full_name (cfg->method, TRUE),
7282 cfg->native_code, cfg->native_code + cfg->code_len, cfg->domain->friendly_name);
7284 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info, cfg->run_cctors);
7286 mono_domain_lock (cfg->domain);
7287 mono_code_manager_commit (cfg->domain->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
7288 mono_domain_unlock (cfg->domain);
7290 mono_arch_flush_icache (cfg->native_code, cfg->code_len);
7292 mono_debug_close_method (cfg);
7296 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
7301 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
7302 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
7304 if (cp->opcode == OP_ICONST) {
7305 if (cfg->opt & MONO_OPT_CONSPROP) {
7306 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
7310 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
7311 if (cfg->opt & MONO_OPT_COPYPROP) {
7312 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
7318 arity = mono_burg_arity [tree->opcode];
7321 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
7322 if (cfg->opt & MONO_OPT_CFOLD)
7323 mono_constant_fold_inst (tree, NULL);
7324 /* The opcode may have changed */
7325 if (mono_burg_arity [tree->opcode] > 1) {
7326 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
7327 if (cfg->opt & MONO_OPT_CFOLD)
7328 mono_constant_fold_inst (tree, NULL);
7330 mono_constant_fold_inst (tree, NULL);
7336 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
7340 switch (tree->opcode) {
7350 if (tree->ssa_op == MONO_SSA_NOP) {
7351 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7368 case OP_VOIDCALL_REG:
7369 case OP_VOIDCALLVIRT:
7371 MonoCallInst *call = (MonoCallInst *)tree;
7372 MonoMethodSignature *sig = call->signature;
7373 int i, byref = FALSE;
7375 for (i = 0; i < sig->param_count; i++) {
7376 if (sig->params [i]->byref) {
7383 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7391 arity = mono_burg_arity [tree->opcode];
7397 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7400 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7401 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
7404 g_assert_not_reached ();
7409 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
7411 MonoInst *tree = bb->code;
7417 for (; tree; tree = tree->next) {
7419 mono_cprop_copy_values (cfg, tree, acp);
7421 mono_cprop_invalidate_values (tree, acp, acp_size);
7423 if (tree->ssa_op == MONO_SSA_STORE &&
7424 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
7425 MonoInst *i1 = tree->inst_i1;
7427 acp [tree->inst_i0->inst_c0] = NULL;
7429 for (i = 0; i < acp_size; i++) {
7430 if (acp [i] && acp [i]->opcode != OP_ICONST &&
7431 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
7436 if (i1->opcode == OP_ICONST) {
7437 acp [tree->inst_i0->inst_c0] = i1;
7438 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
7440 if (i1->ssa_op == MONO_SSA_LOAD &&
7441 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
7442 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
7443 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
7444 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
7449 if (tree->opcode == CEE_BEQ) {
7450 g_assert (tree->inst_i0->opcode == OP_COMPARE);
7451 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
7452 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
7454 tree->opcode = CEE_BR;
7455 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
7456 tree->inst_target_bb = tree->inst_true_bb;
7458 tree->inst_target_bb = tree->inst_false_bb;
7467 mono_local_cprop (MonoCompile *cfg)
7472 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
7474 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7475 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
7476 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
7481 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, int parts)
7483 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
7484 guint8 *ip = (guint8 *)header->code;
7487 int dfn = 0, i, code_size_ratio;
7489 mono_jit_stats.methods_compiled++;
7490 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
7491 mono_profiler_method_jit (method);
7493 cfg = g_new0 (MonoCompile, 1);
7494 cfg->method = method;
7495 cfg->mempool = mono_mempool_new ();
7497 cfg->prof_options = mono_profiler_get_events ();
7498 cfg->run_cctors = run_cctors;
7499 cfg->bb_hash = g_hash_table_new (NULL, NULL);
7500 cfg->domain = domain;
7501 cfg->verbose_level = mini_verbose;
7502 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
7503 ((MonoMethodNormal *)method)->header->max_stack);
7505 if (cfg->verbose_level > 2)
7506 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
7509 * create MonoInst* which represents arguments and local variables
7511 mono_compile_create_vars (cfg);
7513 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
7514 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7515 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
7516 mono_destroy_compile (cfg);
7520 mono_jit_stats.basic_blocks += cfg->num_bblocks;
7521 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
7523 if (cfg->num_varinfo > 2000) {
7525 * we disable some optimizations if there are too many variables
7526 * because JIT time may become too expensive. The actual number needs
7527 * to be tweaked and eventually the non-linear algorithms should be fixed.
7529 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
7530 cfg->disable_ssa = TRUE;
7532 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
7534 /* Depth-first ordering on basic blocks */
7535 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
7537 if (cfg->opt & MONO_OPT_BRANCH)
7538 optimize_branches (cfg);
7540 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
7541 if (cfg->num_bblocks != dfn + 1) {
7544 cfg->num_bblocks = dfn + 1;
7546 if (!header->clauses) {
7547 /* remove unreachable code, because the code in them may be
7548 * inconsistent (access to dead variables for example) */
7549 for (bb = cfg->bb_entry; bb;) {
7550 MonoBasicBlock *bbn = bb->next_bb;
7552 if (bbn && bbn->region == -1 && !bbn->dfn) {
7553 if (cfg->verbose_level > 1)
7554 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
7555 bb->next_bb = bbn->next_bb;
7556 nullify_basic_block (bbn);
7564 if (cfg->opt & MONO_OPT_LOOP) {
7565 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
7566 mono_compute_natural_loops (cfg);
7569 /* after method_to_ir */
7573 //#define DEBUGSSA "logic_run"
7574 #define DEBUGSSA_CLASS "Tests"
7577 if (!header->num_clauses && !cfg->disable_ssa) {
7578 mono_local_cprop (cfg);
7579 mono_ssa_compute (cfg);
7583 /* fixme: add all optimizations which requires SSA */
7584 if (cfg->opt & (MONO_OPT_DEADCE | MONO_OPT_ABCREM)) {
7585 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
7586 mono_local_cprop (cfg);
7587 mono_ssa_compute (cfg);
7589 if (cfg->verbose_level >= 2) {
7596 /* after SSA translation */
7600 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
7601 if (cfg->comp_done & MONO_COMP_SSA) {
7602 mono_ssa_cprop (cfg);
7604 mono_local_cprop (cfg);
7608 if (cfg->comp_done & MONO_COMP_SSA) {
7609 mono_ssa_deadce (cfg);
7611 //mono_ssa_strength_reduction (cfg);
7613 if ((cfg->flags & MONO_CFG_HAS_LDELEMA) && (cfg->opt & MONO_OPT_ABCREM))
7614 mono_perform_abc_removal (cfg);
7616 mono_ssa_remove (cfg);
7618 if (cfg->opt & MONO_OPT_BRANCH)
7619 optimize_branches (cfg);
7622 /* after SSA removal */
7626 decompose_pass (cfg);
7628 if (cfg->opt & MONO_OPT_LINEARS) {
7631 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
7632 cfg->comp_done &= ~MONO_COMP_LIVENESS;
7633 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
7634 mono_analyze_liveness (cfg);
7636 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
7637 regs = mono_arch_get_global_int_regs (cfg);
7638 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
7642 //mono_print_code (cfg);
7646 /* variables are allocated after decompose, since decompose could create temps */
7647 mono_arch_allocate_vars (cfg);
7649 if (cfg->opt & MONO_OPT_CFOLD)
7650 mono_constant_fold (cfg);
7652 mini_select_instructions (cfg);
7655 if (cfg->verbose_level >= 2) {
7656 char *id = mono_method_full_name (cfg->method, FALSE);
7657 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
7661 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
7663 jinfo = g_new0 (MonoJitInfo, 1);
7664 jinfo->method = method;
7665 jinfo->code_start = cfg->native_code;
7666 jinfo->code_size = cfg->code_len;
7667 jinfo->used_regs = cfg->used_int_regs;
7668 jinfo->domain_neutral = (cfg->opt & MONO_OPT_SHARED) != 0;
7670 if (header->num_clauses) {
7673 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
7674 jinfo->num_clauses = header->num_clauses;
7675 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
7676 sizeof (MonoJitExceptionInfo) * header->num_clauses);
7678 for (i = 0; i < header->num_clauses; i++) {
7679 MonoExceptionClause *ec = &header->clauses [i];
7680 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
7681 MonoBasicBlock *tblock;
7683 ei->flags = ec->flags;
7685 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7686 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->token_or_filter);
7688 ei->data.filter = cfg->native_code + tblock->native_offset;
7690 ei->data.token = ec->token_or_filter;
7693 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
7695 ei->try_start = cfg->native_code + tblock->native_offset;
7696 g_assert (tblock->native_offset);
7697 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
7699 ei->try_end = cfg->native_code + tblock->native_offset;
7700 g_assert (tblock->native_offset);
7701 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
7703 ei->handler_start = cfg->native_code + tblock->native_offset;
7707 cfg->jit_info = jinfo;
7709 mono_jit_info_table_add (cfg->domain, jinfo);
7711 /* collect statistics */
7712 mono_jit_stats.allocated_code_size += cfg->code_len;
7713 code_size_ratio = cfg->code_len;
7714 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
7715 mono_jit_stats.biggest_method_size = code_size_ratio;
7716 mono_jit_stats.biggest_method = method;
7718 code_size_ratio = (code_size_ratio * 100) / ((MonoMethodNormal *)method)->header->code_size;
7719 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
7720 mono_jit_stats.max_code_size_ratio = code_size_ratio;
7721 mono_jit_stats.max_ratio_method = method;
7723 mono_jit_stats.native_code_size += cfg->code_len;
7725 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7726 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
7732 mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain)
7735 GHashTable *jit_code_hash;
7736 gpointer code = NULL;
7742 jit_code_hash = target_domain->jit_code_hash;
7744 #ifdef MONO_USE_AOT_COMPILER
7745 if (!mono_compile_aot && (opt & MONO_OPT_AOT)) {
7747 MonoDomain *domain = mono_domain_get ();
7749 mono_domain_lock (domain);
7751 mono_class_init (method->klass);
7752 if ((info = mono_aot_get_method (domain, method))) {
7753 g_hash_table_insert (domain->jit_code_hash, method, info);
7754 mono_domain_unlock (domain);
7755 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
7756 return info->code_start;
7759 mono_domain_unlock (domain);
7763 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
7764 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
7767 if (!method->addr) {
7768 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
7769 method->addr = mono_lookup_internal_call (method);
7771 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
7772 mono_lookup_pinvoke_call (method, NULL, NULL);
7774 nm = mono_marshal_get_native_wrapper (method);
7775 return mono_compile_method (nm);
7777 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
7778 //mono_debug_add_wrapper (method, nm);
7779 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
7780 const char *name = method->name;
7783 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
7784 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
7785 /* FIXME: uhm, we need a wrapper to handle exceptions? */
7786 return (gpointer)mono_delegate_ctor;
7787 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
7788 nm = mono_marshal_get_delegate_invoke (method);
7789 return mono_jit_compile_method (nm);
7790 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
7791 nm = mono_marshal_get_delegate_begin_invoke (method);
7792 return mono_jit_compile_method (nm);
7793 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
7794 nm = mono_marshal_get_delegate_end_invoke (method);
7795 return mono_jit_compile_method (nm);
7801 cfg = mini_method_compile (method, opt, target_domain, TRUE, 0);
7803 mono_domain_lock (target_domain);
7805 /* Check if some other thread already did the job. In this case, we can
7806 discard the code this thread generated. */
7808 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
7809 /* We can't use a domain specific method in another domain */
7810 if ((target_domain == mono_domain_get ()) || info->domain_neutral) {
7811 code = info->code_start;
7812 // printf("Discarding code for method %s\n", method->name);
7817 g_hash_table_insert (jit_code_hash, method, cfg->jit_info);
7818 code = cfg->native_code;
7821 mono_destroy_compile (cfg);
7823 if (target_domain->jump_target_hash) {
7824 MonoJumpInfo patch_info;
7826 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
7828 patch_info.next = NULL;
7829 patch_info.ip.i = 0;
7830 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
7831 patch_info.data.method = method;
7832 g_hash_table_remove (target_domain->jump_target_hash, method);
7834 for (tmp = list; tmp; tmp = tmp->next)
7835 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info, TRUE);
7836 g_slist_free (list);
7839 mono_domain_unlock (target_domain);
7841 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
7846 mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt)
7848 /* FIXME: later copy the code from mono */
7849 MonoDomain *target_domain, *domain = mono_domain_get ();
7853 if (opt & MONO_OPT_SHARED)
7854 target_domain = mono_get_root_domain ();
7856 target_domain = domain;
7858 mono_domain_lock (target_domain);
7860 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
7861 /* We can't use a domain specific method in another domain */
7862 if (! ((domain != target_domain) && !info->domain_neutral)) {
7863 mono_domain_unlock (target_domain);
7864 mono_jit_stats.methods_lookups++;
7865 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
7866 return info->code_start;
7870 mono_domain_unlock (target_domain);
7871 p = mono_jit_compile_method_inner (method, target_domain);
7876 mono_jit_compile_method (MonoMethod *method)
7878 return mono_jit_compile_method_with_opt (method, default_opt);
7882 mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method)
7884 MonoDomain *target_domain;
7887 if (default_opt & MONO_OPT_SHARED)
7888 target_domain = mono_get_root_domain ();
7890 target_domain = domain;
7892 mono_domain_lock (target_domain);
7894 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
7895 /* We can't use a domain specific method in another domain */
7896 if (! ((domain != target_domain) && !info->domain_neutral)) {
7897 mono_domain_unlock (target_domain);
7898 mono_jit_stats.methods_lookups++;
7899 return info->code_start;
7903 mono_domain_unlock (target_domain);
7909 * mono_jit_runtime_invoke:
7910 * @method: the method to invoke
7911 * @obj: this pointer
7912 * @params: array of parameter values.
7913 * @exc: used to catch exceptions objects
7916 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
7919 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc, void* compiled_method);
7920 void* compiled_method;
7922 if (obj == NULL && !(method->flags & METHOD_ATTRIBUTE_STATIC) && !method->string_ctor && (method->wrapper_type == 0)) {
7923 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
7927 invoke = mono_marshal_get_runtime_invoke (method);
7928 runtime_invoke = mono_jit_compile_method (invoke);
7930 /* We need this here becuase mono_marshal_get_runtime_invoke can be place
7931 * the helper method in System.Object and not the target class
7933 mono_runtime_class_init (mono_class_vtable (mono_domain_get (), method->klass));
7935 compiled_method = mono_jit_compile_method (method);
7936 return runtime_invoke (obj, params, exc, compiled_method);
7939 #ifdef PLATFORM_WIN32
7940 #define GET_CONTEXT \
7941 struct sigcontext *ctx = (struct sigcontext*)_dummy;
7944 #define GET_CONTEXT \
7945 void *ctx = context;
7946 #elif defined(sun) // Solaris x86
7947 #define GET_CONTEXT \
7948 ucontext_t *uctx = context; \
7949 struct sigcontext *ctx = (struct sigcontext *)&(uctx->uc_mcontext);
7950 #elif defined(__ppc__) || defined (__powerpc__) || defined (__s390__)
7951 #define GET_CONTEXT \
7952 void *ctx = context;
7954 #define GET_CONTEXT \
7955 void **_p = (void **)&_dummy; \
7956 struct sigcontext *ctx = (struct sigcontext *)++_p;
7960 #ifdef MONO_ARCH_USE_SIGACTION
7961 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, siginfo_t *info, void *context)
7963 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy)
7967 SIG_HANDLER_SIGNATURE (sigfpe_signal_handler)
7972 exc = mono_get_exception_divide_by_zero ();
7974 mono_arch_handle_exception (ctx, exc, FALSE);
7978 SIG_HANDLER_SIGNATURE (sigill_signal_handler)
7982 exc = mono_get_exception_execution_engine ("SIGILL");
7984 mono_arch_handle_exception (ctx, exc, FALSE);
7987 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
7990 sigsegv_signal_handler (int _dummy, siginfo_t *info, void *context)
7993 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
7994 struct sigcontext *ctx = (struct sigcontext *)&(((ucontext_t*)context)->uc_mcontext);
7996 /* Can't allocate memory using Boehm GC on altstack */
7997 if (jit_tls->stack_size &&
7998 ((guint8*)info->si_addr >= (guint8*)jit_tls->end_of_stack - jit_tls->stack_size) &&
7999 ((guint8*)info->si_addr < (guint8*)jit_tls->end_of_stack))
8000 exc = mono_domain_get ()->stack_overflow_ex;
8002 exc = mono_domain_get ()->null_reference_ex;
8004 mono_arch_handle_exception (ctx, exc, FALSE);
8010 SIG_HANDLER_SIGNATURE (sigsegv_signal_handler)
8014 mono_arch_handle_exception (ctx, NULL, FALSE);
8020 SIG_HANDLER_SIGNATURE (sigusr1_signal_handler)
8022 gboolean running_managed;
8027 running_managed = (mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx)) != NULL);
8029 exc = mono_thread_request_interruption (running_managed);
8032 mono_arch_handle_exception (ctx, exc, FALSE);
8036 SIG_HANDLER_SIGNATURE (sigquit_signal_handler)
8041 exc = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
8043 mono_arch_handle_exception (ctx, exc, FALSE);
8047 SIG_HANDLER_SIGNATURE (sigint_signal_handler)
8052 exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
8054 mono_arch_handle_exception (ctx, exc, FALSE);
8057 #ifndef PLATFORM_WIN32
8059 add_signal_handler (int signo, gpointer handler)
8061 struct sigaction sa;
8063 #ifdef MONO_ARCH_USE_SIGACTION
8064 sa.sa_sigaction = handler;
8065 sigemptyset (&sa.sa_mask);
8066 sa.sa_flags = SA_SIGINFO;
8068 sa.sa_handler = handler;
8069 sigemptyset (&sa.sa_mask);
8072 g_assert (sigaction (signo, &sa, NULL) != -1);
8077 mono_runtime_install_handlers (void)
8079 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8080 struct sigaction sa;
8083 #ifdef PLATFORM_WIN32
8085 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
8086 win32_seh_set_handler(SIGILL, sigill_signal_handler);
8087 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
8088 if (getenv ("MONO_DEBUG"))
8089 win32_seh_set_handler(SIGINT, sigint_signal_handler);
8090 #else /* !PLATFORM_WIN32 */
8092 /* libpthreads has its own implementation of sigaction(),
8093 * but it seems to work well with our current exception
8094 * handlers. If not we must call syscall directly instead
8097 if (getenv ("MONO_DEBUG")) {
8098 add_signal_handler (SIGINT, sigint_signal_handler);
8101 add_signal_handler (SIGFPE, sigfpe_signal_handler);
8102 add_signal_handler (SIGQUIT, sigquit_signal_handler);
8103 add_signal_handler (SIGILL, sigill_signal_handler);
8104 add_signal_handler (SIGBUS, sigsegv_signal_handler);
8105 add_signal_handler (mono_thread_get_abort_signal (), sigusr1_signal_handler);
8108 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8109 sa.sa_sigaction = sigsegv_signal_handler;
8110 sigemptyset (&sa.sa_mask);
8111 sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
8112 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
8114 add_signal_handler (SIGSEGV, sigsegv_signal_handler);
8117 #endif /* PLATFORM_WIN32 */
8120 /* mono_jit_create_remoting_trampoline:
8121 * @method: pointer to the method info
8123 * Creates a trampoline which calls the remoting functions. This
8124 * is used in the vtable of transparent proxies.
8126 * Returns: a pointer to the newly created code
8129 mono_jit_create_remoting_trampoline (MonoMethod *method)
8132 guint8 *addr = NULL;
8134 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
8135 (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
8136 nm = mono_marshal_get_remoting_invoke (method);
8137 addr = mono_compile_method (nm);
8139 addr = mono_compile_method (method);
8145 mini_init (const char *filename)
8149 mono_arch_cpu_init ();
8151 if (!g_thread_supported ())
8152 g_thread_init (NULL);
8154 mono_jit_tls_id = TlsAlloc ();
8155 setup_jit_tls_data ((gpointer)-1, mono_thread_abort);
8157 InitializeCriticalSection (&trampoline_hash_mutex);
8161 if (default_opt & MONO_OPT_AOT)
8164 mono_runtime_install_handlers ();
8165 mono_threads_install_cleanup (mini_thread_cleanup);
8167 #define JIT_TRAMPOLINES_WORK
8168 #ifdef JIT_TRAMPOLINES_WORK
8169 mono_install_compile_method (mono_jit_compile_method);
8170 mono_install_trampoline (mono_arch_create_jit_trampoline);
8171 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
8173 #define JIT_INVOKE_WORKS
8174 #ifdef JIT_INVOKE_WORKS
8175 mono_install_runtime_invoke (mono_jit_runtime_invoke);
8176 mono_install_handler (mono_arch_get_throw_exception ());
8178 mono_install_stack_walk (mono_jit_walk_stack);
8180 domain = mono_init (filename);
8183 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
8184 ves_icall_get_frame_info);
8185 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
8186 ves_icall_get_trace);
8187 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
8188 mono_runtime_install_handlers);
8191 create_helper_signature ();
8193 #define JIT_CALLS_WORK
8194 #ifdef JIT_CALLS_WORK
8195 /* Needs to be called here since register_jit_icall depends on it */
8196 mono_marshal_init ();
8198 mono_arch_register_lowlevel_calls ();
8199 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
8200 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
8201 mono_register_jit_icall (mono_trace_enter_method, "mono_trace_enter_method", NULL, TRUE);
8202 mono_register_jit_icall (mono_trace_leave_method, "mono_trace_leave_method", NULL, TRUE);
8203 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
8204 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
8206 /* fixme: we cant handle vararg methods this way, because the signature is not constant */
8207 //mono_register_jit_icall (ves_array_element_address, "ves_array_element_address", NULL);
8208 //mono_register_jit_icall (mono_array_new_va, "mono_array_new_va", NULL);
8210 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
8211 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
8212 helper_sig_void_ptr, TRUE);
8213 mono_register_jit_icall (mono_thread_get_pending_exception, "mono_thread_get_pending_exception", helper_sig_obj_void, FALSE);
8214 mono_register_jit_icall (mono_thread_interruption_checkpoint, "mono_thread_interruption_checkpoint", helper_sig_void_void, FALSE);
8215 mono_register_jit_icall (mono_load_remote_field_new, "mono_load_remote_field_new", helper_sig_obj_obj_ptr_ptr, FALSE);
8216 mono_register_jit_icall (mono_store_remote_field_new, "mono_store_remote_field_new", helper_sig_void_obj_ptr_ptr_obj, FALSE);
8219 * NOTE, NOTE, NOTE, NOTE:
8220 * when adding emulation for some opcodes, remember to also add a dummy
8221 * rule to the burg files, because we need the arity information to be correct.
8223 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult, TRUE);
8224 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un, FALSE);
8225 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf, FALSE);
8226 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv, FALSE);
8227 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un, FALSE);
8228 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem, FALSE);
8229 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un, FALSE);
8231 #ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
8232 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl, TRUE);
8233 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr, TRUE);
8234 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un, TRUE);
8237 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8, FALSE);
8238 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4, FALSE);
8239 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8, FALSE);
8240 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8, FALSE);
8242 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
8243 mono_register_opcode_emulation (OP_FCONV_TO_I8, "__emul_fconv_to_i8", helper_sig_long_double, mono_fconv_i8, FALSE);
8245 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
8246 mono_register_opcode_emulation (CEE_CONV_R_UN, "__emul_conv_r_un", helper_sig_double_int, mono_conv_to_r8_un, FALSE);
8248 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
8249 mono_register_opcode_emulation (OP_LCONV_TO_R8, "__emul_lconv_to_r8", helper_sig_double_long, mono_lconv_to_r8, FALSE);
8251 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
8252 mono_register_opcode_emulation (OP_LCONV_TO_R4, "__emul_lconv_to_r4", helper_sig_float_long, mono_lconv_to_r4, FALSE);
8254 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
8255 mono_register_opcode_emulation (OP_LCONV_TO_R_UN, "__emul_lconv_to_r8_un", helper_sig_double_long, mono_lconv_to_r8_un, FALSE);
8257 #ifdef MONO_ARCH_EMULATE_FREM
8258 mono_register_opcode_emulation (OP_FREM, "__emul_frem", helper_sig_double_double_double, fmod, FALSE);
8261 #if SIZEOF_VOID_P == 4
8262 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4, TRUE);
8265 #warning "fixme: add opcode emulation"
8269 /* other jit icalls */
8270 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
8271 helper_sig_ptr_ptr_ptr, FALSE);
8272 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
8273 mono_register_jit_icall (mono_get_special_static_data, "mono_get_special_static_data", helper_sig_ptr_int, FALSE);
8274 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
8275 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
8276 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
8277 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
8278 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
8279 mono_register_jit_icall (helper_stelem_ref_check, "helper_stelem_ref_check", helper_sig_stelem_ref_check, FALSE);
8280 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
8281 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
8282 mono_register_jit_icall (mono_object_new_fast, "mono_object_new_fast", helper_sig_object_new_specific, FALSE);
8283 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
8284 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
8285 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
8286 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
8287 mono_register_jit_icall (mono_ldftn_nosync, "mono_ldftn_nosync", helper_sig_compile, FALSE);
8288 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
8291 #define JIT_RUNTIME_WORKS
8292 #ifdef JIT_RUNTIME_WORKS
8293 mono_runtime_install_cleanup ((MonoDomainFunc)mini_cleanup);
8294 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
8297 mono_thread_attach (domain);
8301 MonoJitStats mono_jit_stats = {0};
8304 print_jit_stats (void)
8306 if (mono_jit_stats.enabled) {
8307 g_print ("Mono Jit statistics\n");
8308 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
8309 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
8310 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
8311 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
8312 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
8313 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
8314 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
8315 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
8316 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
8317 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
8318 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
8319 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
8320 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
8321 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
8322 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
8323 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
8324 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
8325 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
8327 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
8328 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
8329 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
8330 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
8331 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
8333 g_print ("\nGeneric instances: %ld\n", mono_stats.generic_instance_count);
8334 g_print ("Inflated methods: %ld\n", mono_stats.inflated_method_count);
8335 g_print ("Inflated types: %ld\n", mono_stats.inflated_type_count);
8336 g_print ("Generics metadata size: %ld\n", mono_stats.generics_metadata_size);
8341 mini_cleanup (MonoDomain *domain)
8344 * mono_runtime_cleanup() and mono_domain_finalize () need to
8345 * be called early since they need the execution engine still
8346 * fully working (mono_domain_finalize may invoke managed finalizers
8347 * and mono_runtime_cleanup will wait for other threads to finish).
8349 mono_domain_finalize (domain, 2000);
8351 mono_runtime_cleanup (domain);
8353 mono_profiler_shutdown ();
8355 mono_debug_cleanup ();
8357 #ifdef PLATFORM_WIN32
8358 win32_seh_cleanup();
8361 mono_domain_free (domain, TRUE);
8367 mono_set_defaults (int verbose_level, guint32 opts)
8369 mini_verbose = verbose_level;
8374 mono_precompile_assembly (MonoAssembly *ass, void *user_data)
8376 MonoImage *image = mono_assembly_get_image (ass);
8377 MonoMethod *method, *invoke;
8380 if (mini_verbose > 0)
8381 printf ("PRECOMPILE: %s.\n", mono_image_get_filename (image));
8383 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
8384 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
8385 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
8389 if (mini_verbose > 1) {
8390 char * desc = mono_method_full_name (method, TRUE);
8391 g_print ("Compiling %d %s\n", count, desc);
8394 mono_compile_method (method);
8395 if (strcmp (method->name, "Finalize") == 0) {
8396 invoke = mono_marshal_get_runtime_invoke (method);
8397 mono_compile_method (invoke);
8399 if (method->klass->marshalbyref && method->signature->hasthis) {
8400 invoke = mono_marshal_get_remoting_invoke_with_check (method);
8401 mono_compile_method (invoke);
8406 void mono_precompile_assemblies ()
8408 mono_assembly_foreach ((GFunc)mono_precompile_assembly, NULL);