2 * mini.c: The new Mono code generator.
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2002 Ximian, Inc.
16 #ifdef sun // Solaris x86
17 #include <sys/types.h>
18 #include <sys/ucontext.h>
21 #ifdef HAVE_VALGRIND_MEMCHECK_H
22 #include <valgrind/memcheck.h>
25 #include <mono/metadata/assembly.h>
26 #include <mono/metadata/loader.h>
27 #include <mono/metadata/cil-coff.h>
28 #include <mono/metadata/tabledefs.h>
29 #include <mono/metadata/class.h>
30 #include <mono/metadata/object.h>
31 #include <mono/metadata/exception.h>
32 #include <mono/metadata/opcodes.h>
33 #include <mono/metadata/mono-endian.h>
34 #include <mono/metadata/tokentype.h>
35 #include <mono/metadata/tabledefs.h>
36 #include <mono/metadata/threads.h>
37 #include <mono/metadata/marshal.h>
38 #include <mono/metadata/socket-io.h>
39 #include <mono/metadata/appdomain.h>
40 #include <mono/metadata/debug-helpers.h>
41 #include <mono/io-layer/io-layer.h>
42 #include "mono/metadata/profiler.h"
43 #include <mono/metadata/profiler-private.h>
44 #include <mono/metadata/mono-config.h>
45 #include <mono/metadata/environment.h>
46 #include <mono/metadata/mono-debug.h>
47 #include <mono/metadata/mono-debug-debugger.h>
48 #include <mono/metadata/monitor.h>
49 #include <mono/os/gc_wrapper.h>
57 #include "jit-icalls.c"
60 * this is used to determine when some branch optimizations are possible: we exclude FP compares
61 * because they have weird semantics with NaNs.
63 #define MONO_IS_COND_BRANCH_OP(ins) (((ins)->opcode >= CEE_BEQ && (ins)->opcode <= CEE_BLT_UN) || ((ins)->opcode >= OP_LBEQ && (ins)->opcode <= OP_LBLT_UN) || ((ins)->opcode >= OP_FBEQ && (ins)->opcode <= OP_FBLT_UN) || ((ins)->opcode >= OP_IBEQ && (ins)->opcode <= OP_IBLT_UN))
64 #define MONO_IS_COND_BRANCH_NOFP(ins) (MONO_IS_COND_BRANCH_OP(ins) && (ins)->inst_left->inst_left->type != STACK_R8)
66 #define MONO_CHECK_THIS(ins) (cfg->method->signature->hasthis && (ins)->ssa_op == MONO_SSA_LOAD && (ins)->inst_left->inst_c0 == 0)
68 gboolean mono_arch_print_tree(MonoInst *tree, int arity);
69 static gpointer mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt);
70 static gpointer mono_jit_compile_method (MonoMethod *method);
71 static gpointer mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method);
73 static void handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src,
74 const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native);
76 static void dec_foreach (MonoInst *tree, MonoCompile *cfg);
78 static int mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
79 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
80 guint inline_offset, gboolean is_virtual_call);
82 extern guint8 mono_burg_arity [];
83 /* helper methods signature */
84 static MonoMethodSignature *helper_sig_long_long_long = NULL;
85 static MonoMethodSignature *helper_sig_long_long_int = NULL;
86 static MonoMethodSignature *helper_sig_newarr = NULL;
87 static MonoMethodSignature *helper_sig_newarr_specific = NULL;
88 static MonoMethodSignature *helper_sig_ldstr = NULL;
89 static MonoMethodSignature *helper_sig_domain_get = NULL;
90 static MonoMethodSignature *helper_sig_object_new = NULL;
91 static MonoMethodSignature *helper_sig_object_new_specific = NULL;
92 static MonoMethodSignature *helper_sig_compile = NULL;
93 static MonoMethodSignature *helper_sig_compile_virt = NULL;
94 static MonoMethodSignature *helper_sig_obj_ptr = NULL;
95 static MonoMethodSignature *helper_sig_obj_ptr_ptr = NULL;
96 static MonoMethodSignature *helper_sig_obj_obj_ptr_ptr = NULL;
97 static MonoMethodSignature *helper_sig_obj_void = NULL;
98 static MonoMethodSignature *helper_sig_ptr_void = NULL;
99 static MonoMethodSignature *helper_sig_void_void = NULL;
100 static MonoMethodSignature *helper_sig_void_ptr = NULL;
101 static MonoMethodSignature *helper_sig_void_obj = NULL;
102 static MonoMethodSignature *helper_sig_void_obj_ptr_int = NULL;
103 static MonoMethodSignature *helper_sig_void_obj_ptr_ptr_obj = NULL;
104 static MonoMethodSignature *helper_sig_void_ptr_ptr = NULL;
105 static MonoMethodSignature *helper_sig_void_ptr_ptr_ptr = NULL;
106 static MonoMethodSignature *helper_sig_ptr_ptr_ptr = NULL;
107 static MonoMethodSignature *helper_sig_ptr_obj = NULL;
108 static MonoMethodSignature *helper_sig_ptr_obj_int = NULL;
109 static MonoMethodSignature *helper_sig_ptr_int = NULL;
110 static MonoMethodSignature *helper_sig_initobj = NULL;
111 static MonoMethodSignature *helper_sig_memcpy = NULL;
112 static MonoMethodSignature *helper_sig_memset = NULL;
113 static MonoMethodSignature *helper_sig_ulong_double = NULL;
114 static MonoMethodSignature *helper_sig_long_double = NULL;
115 static MonoMethodSignature *helper_sig_double_long = NULL;
116 static MonoMethodSignature *helper_sig_double_int = NULL;
117 static MonoMethodSignature *helper_sig_float_long = NULL;
118 static MonoMethodSignature *helper_sig_double_double_double = NULL;
119 static MonoMethodSignature *helper_sig_uint_double = NULL;
120 static MonoMethodSignature *helper_sig_int_double = NULL;
121 static MonoMethodSignature *helper_sig_stelem_ref = NULL;
122 static MonoMethodSignature *helper_sig_stelem_ref_check = NULL;
123 static MonoMethodSignature *helper_sig_class_init_trampoline = NULL;
125 static guint32 default_opt = MONO_OPT_PEEPHOLE;
127 guint32 mono_jit_tls_id = -1;
128 MonoTraceSpec *mono_jit_trace_calls = NULL;
129 gboolean mono_break_on_exc = FALSE;
130 gboolean mono_compile_aot = FALSE;
132 static int mini_verbose = 0;
134 static CRITICAL_SECTION jit_mutex;
136 static GHashTable *class_init_hash_addr = NULL;
139 mono_running_on_valgrind (void)
141 #ifdef HAVE_VALGRIND_MEMCHECK_H
142 if (RUNNING_ON_VALGRIND)
152 G_GNUC_UNUSED static void
153 print_method_from_ip (void *ip)
158 MonoDomain *domain = mono_domain_get ();
160 ji = mono_jit_info_table_find (domain, ip);
162 g_print ("No method at %p\n", ip);
165 method = mono_method_full_name (ji->method, TRUE);
166 source = mono_debug_source_location_from_address (ji->method, (int) ip, NULL, domain);
168 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);
171 g_print ("%s\n", source);
179 mono_print_method_from_ip (void *ip)
181 print_method_from_ip (ip);
185 * mono_method_same_domain:
187 * Determine whenever two compiled methods are in the same domain, thus
188 * the address of the callee can be embedded in the caller.
190 gboolean mono_method_same_domain (MonoJitInfo *caller, MonoJitInfo *callee)
192 if (!caller || !callee)
196 * If the call was made from domain-neutral to domain-specific
197 * code, we can't patch the call site.
199 if (caller->domain_neutral && !callee->domain_neutral)
202 if ((caller->method->klass == mono_defaults.appdomain_class) &&
203 (strstr (caller->method->name, "InvokeInDomain"))) {
204 /* The InvokeInDomain methods change the current appdomain */
212 mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token)
214 MonoJumpInfoToken *res = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoToken));
221 #define MONO_INIT_VARINFO(vi,id) do { \
222 (vi)->range.first_use.pos.bid = 0xffff; \
228 * Basic blocks have two numeric identifiers:
229 * dfn: Depth First Number
230 * block_num: unique ID assigned at bblock creation
232 #define NEW_BBLOCK(cfg) (mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)))
233 #define ADD_BBLOCK(cfg,bbhash,b) do { \
234 g_hash_table_insert (bbhash, (b)->cil_code, (b)); \
235 (b)->block_num = cfg->num_bblocks++; \
236 (b)->real_offset = real_offset; \
239 #define GET_BBLOCK(cfg,bbhash,tblock,ip) do { \
240 (tblock) = g_hash_table_lookup (bbhash, (ip)); \
242 if ((ip) >= end || (ip) < header->code) goto unverified; \
243 (tblock) = NEW_BBLOCK (cfg); \
244 (tblock)->cil_code = (ip); \
245 ADD_BBLOCK (cfg, (bbhash), (tblock)); \
249 #define CHECK_BBLOCK(target,ip,tblock) do { \
250 if ((target) < (ip) && !(tblock)->code) { \
251 bb_recheck = g_list_prepend (bb_recheck, (tblock)); \
252 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); \
256 #define NEW_ICONST(cfg,dest,val) do { \
257 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
258 (dest)->opcode = OP_ICONST; \
259 (dest)->inst_c0 = (val); \
260 (dest)->type = STACK_I4; \
263 #if SIZEOF_VOID_P == 8
264 #define NEW_PCONST(cfg,dest,val) do { \
265 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
266 (dest)->opcode = OP_I8CONST; \
267 (dest)->inst_p0 = (val); \
268 (dest)->type = STACK_PTR; \
271 #define NEW_PCONST(cfg,dest,val) do { \
272 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
273 (dest)->opcode = OP_ICONST; \
274 (dest)->inst_p0 = (val); \
275 (dest)->type = STACK_PTR; \
279 #if SIZEOF_VOID_P == 8
280 #define OP_PCONST OP_I8CONST
282 #define OP_PCONST OP_ICONST
285 #define NEW_CLASSCONST(cfg,dest,val) do { \
286 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
287 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_PCONST; \
288 (dest)->inst_p0 = (val); \
289 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_CLASS; \
290 (dest)->type = STACK_PTR; \
293 #define NEW_IMAGECONST(cfg,dest,val) do { \
294 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
295 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_PCONST; \
296 (dest)->inst_p0 = (val); \
297 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_IMAGE; \
298 (dest)->type = STACK_PTR; \
301 #define NEW_FIELDCONST(cfg,dest,field) do { \
302 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
303 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_PCONST; \
304 (dest)->inst_p0 = (field); \
305 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_FIELD; \
306 (dest)->type = STACK_PTR; \
309 #define NEW_METHODCONST(cfg,dest,val) do { \
310 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
311 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_PCONST; \
312 (dest)->inst_p0 = (val); \
313 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_METHODCONST; \
314 (dest)->type = STACK_PTR; \
317 #define NEW_VTABLECONST(cfg,dest,vtable) do { \
318 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
319 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_PCONST; \
320 (dest)->inst_p0 = mono_compile_aot ? (gpointer)((vtable)->klass) : (vtable); \
321 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_VTABLE; \
322 (dest)->type = STACK_PTR; \
325 #define NEW_SFLDACONST(cfg,dest,field) do { \
326 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
327 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_PCONST; \
328 (dest)->inst_p0 = (field); \
329 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_SFLDA; \
330 (dest)->type = STACK_PTR; \
333 #define NEW_LDSTRCONST(cfg,dest,image,token) do { \
334 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
335 (dest)->opcode = OP_AOTCONST; \
336 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
337 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_LDSTR; \
338 (dest)->type = STACK_OBJ; \
341 #define NEW_TYPE_FROM_HANDLE_CONST(cfg,dest,image,token) do { \
342 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
343 (dest)->opcode = OP_AOTCONST; \
344 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
345 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_TYPE_FROM_HANDLE; \
346 (dest)->type = STACK_OBJ; \
349 #define NEW_LDTOKENCONST(cfg,dest,image,token) do { \
350 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
351 (dest)->opcode = OP_AOTCONST; \
352 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
353 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_LDTOKEN; \
354 (dest)->type = STACK_PTR; \
357 #define NEW_DOMAINCONST(cfg,dest) do { \
358 if (cfg->opt & MONO_OPT_SHARED) { \
359 NEW_TEMPLOAD (cfg, dest, mono_get_domainvar (cfg)->inst_c0); \
361 NEW_PCONST (cfg, dest, (cfg)->domain); \
365 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
367 #define NEW_ARGLOAD(cfg,dest,num) do { \
368 if (arg_array [(num)]->opcode == OP_ICONST) (dest) = arg_array [(num)]; else { \
369 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
370 (dest)->ssa_op = MONO_SSA_LOAD; \
371 (dest)->inst_i0 = arg_array [(num)]; \
372 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
373 type_to_eval_stack_type (param_types [(num)], (dest)); \
374 (dest)->klass = (dest)->inst_i0->klass; \
377 #define NEW_LOCLOAD(cfg,dest,num) do { \
378 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
379 (dest)->ssa_op = MONO_SSA_LOAD; \
380 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
381 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
382 type_to_eval_stack_type (header->locals [(num)], (dest)); \
383 (dest)->klass = (dest)->inst_i0->klass; \
386 #define NEW_LOCLOADA(cfg,dest,num) do { \
387 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
388 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
389 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
390 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
391 (dest)->opcode = OP_LDADDR; \
392 (dest)->type = STACK_MP; \
393 (dest)->klass = (dest)->inst_i0->klass; \
394 if (!MONO_TYPE_ISSTRUCT (header->locals [(num)])) \
395 (cfg)->disable_ssa = TRUE; \
398 #define NEW_RETLOADA(cfg,dest) do { \
399 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
400 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
401 (dest)->inst_i0 = (cfg)->ret; \
402 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
403 (dest)->opcode = CEE_LDIND_I; \
404 (dest)->type = STACK_MP; \
405 (dest)->klass = (dest)->inst_i0->klass; \
406 (cfg)->disable_ssa = TRUE; \
409 #define NEW_ARGLOADA(cfg,dest,num) do { \
410 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
411 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
412 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
413 (dest)->inst_i0 = arg_array [(num)]; \
414 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
415 (dest)->opcode = OP_LDADDR; \
416 (dest)->type = STACK_MP; \
417 (dest)->klass = (dest)->inst_i0->klass; \
418 (cfg)->disable_ssa = TRUE; \
421 #define NEW_TEMPLOAD(cfg,dest,num) do { \
422 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
423 (dest)->ssa_op = MONO_SSA_LOAD; \
424 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
425 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
426 type_to_eval_stack_type ((dest)->inst_i0->inst_vtype, (dest)); \
427 (dest)->klass = (dest)->inst_i0->klass; \
430 #define NEW_TEMPLOADA(cfg,dest,num) do { \
431 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
432 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
433 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
434 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
435 (dest)->opcode = OP_LDADDR; \
436 (dest)->type = STACK_MP; \
437 (dest)->klass = (dest)->inst_i0->klass; \
438 if (!MONO_TYPE_ISSTRUCT (cfg->varinfo [(num)]->inst_vtype)) \
439 (cfg)->disable_ssa = TRUE; \
443 #define NEW_INDLOAD(cfg,dest,addr,vtype) do { \
444 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
445 (dest)->inst_left = addr; \
446 (dest)->opcode = mono_type_to_ldind (vtype); \
447 type_to_eval_stack_type (vtype, (dest)); \
448 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
451 #define NEW_INDSTORE(cfg,dest,addr,value,vtype) do { \
452 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
453 (dest)->inst_i0 = addr; \
454 (dest)->opcode = mono_type_to_stind (vtype); \
455 (dest)->inst_i1 = (value); \
456 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
459 #define NEW_TEMPSTORE(cfg,dest,num,inst) do { \
460 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
461 (dest)->ssa_op = MONO_SSA_STORE; \
462 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
463 (dest)->opcode = mono_type_to_stind ((dest)->inst_i0->inst_vtype); \
464 (dest)->inst_i1 = (inst); \
465 (dest)->klass = (dest)->inst_i0->klass; \
468 #define NEW_LOCSTORE(cfg,dest,num,inst) do { \
469 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
470 (dest)->opcode = mono_type_to_stind (header->locals [(num)]); \
471 (dest)->ssa_op = MONO_SSA_STORE; \
472 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
473 (dest)->inst_i1 = (inst); \
474 (dest)->klass = (dest)->inst_i0->klass; \
477 #define NEW_ARGSTORE(cfg,dest,num,inst) do { \
478 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
479 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
480 (dest)->opcode = mono_type_to_stind (param_types [(num)]); \
481 (dest)->ssa_op = MONO_SSA_STORE; \
482 (dest)->inst_i0 = arg_array [(num)]; \
483 (dest)->inst_i1 = (inst); \
484 (dest)->klass = (dest)->inst_i0->klass; \
487 #define ADD_BINOP(op) do { \
488 MONO_INST_NEW (cfg, ins, (op)); \
489 ins->cil_code = ip; \
491 ins->inst_i0 = sp [0]; \
492 ins->inst_i1 = sp [1]; \
494 type_from_op (ins); \
498 #define ADD_UNOP(op) do { \
499 MONO_INST_NEW (cfg, ins, (op)); \
500 ins->cil_code = ip; \
502 ins->inst_i0 = sp [0]; \
504 type_from_op (ins); \
508 #define ADD_BINCOND(next_block) do { \
511 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
512 cmp->inst_i0 = sp [0]; \
513 cmp->inst_i1 = sp [1]; \
514 cmp->cil_code = ins->cil_code; \
515 type_from_op (cmp); \
517 ins->inst_i0 = cmp; \
518 MONO_ADD_INS (bblock, ins); \
519 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
520 GET_BBLOCK (cfg, bbhash, tblock, target); \
521 link_bblock (cfg, bblock, tblock); \
522 ins->inst_true_bb = tblock; \
523 CHECK_BBLOCK (target, ip, tblock); \
524 if ((next_block)) { \
525 link_bblock (cfg, bblock, (next_block)); \
526 ins->inst_false_bb = (next_block); \
527 start_new_bblock = 1; \
529 GET_BBLOCK (cfg, bbhash, tblock, ip); \
530 link_bblock (cfg, bblock, tblock); \
531 ins->inst_false_bb = tblock; \
532 start_new_bblock = 2; \
536 /* FIXME: handle float, long ... */
537 #define ADD_UNCOND(istrue) do { \
540 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
541 cmp->inst_i0 = sp [0]; \
542 switch (cmp->inst_i0->type) { \
544 cmp->inst_i1 = zero_int64; break; \
546 cmp->inst_i1 = zero_r8; break; \
549 cmp->inst_i1 = zero_ptr; break; \
551 cmp->inst_i1 = zero_obj; break; \
553 cmp->inst_i1 = zero_int32; \
555 cmp->cil_code = ins->cil_code; \
556 type_from_op (cmp); \
558 ins->inst_i0 = cmp; \
559 ins->opcode = (istrue)? CEE_BNE_UN: CEE_BEQ; \
560 MONO_ADD_INS (bblock, ins); \
561 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
562 GET_BBLOCK (cfg, bbhash, tblock, target); \
563 link_bblock (cfg, bblock, tblock); \
564 ins->inst_true_bb = tblock; \
565 CHECK_BBLOCK (target, ip, tblock); \
566 GET_BBLOCK (cfg, bbhash, tblock, ip); \
567 link_bblock (cfg, bblock, tblock); \
568 ins->inst_false_bb = tblock; \
569 start_new_bblock = 2; \
572 #define NEW_LDELEMA(cfg,dest,sp,k) do { \
573 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
574 (dest)->opcode = CEE_LDELEMA; \
575 (dest)->inst_left = (sp) [0]; \
576 (dest)->inst_right = (sp) [1]; \
577 (dest)->type = STACK_MP; \
578 (dest)->klass = (k); \
579 (cfg)->flags |= MONO_CFG_HAS_LDELEMA; \
582 #define NEW_GROUP(cfg,dest,el1,el2) do { \
583 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
584 (dest)->opcode = OP_GROUP; \
585 (dest)->inst_left = (el1); \
586 (dest)->inst_right = (el2); \
591 compare_bblock (gconstpointer a, gconstpointer b)
593 const MonoBasicBlock *b1 = a;
594 const MonoBasicBlock *b2 = b;
596 return b2->cil_code - b1->cil_code;
601 * link_bblock: Links two basic blocks
603 * links two basic blocks in the control flow graph, the 'from'
604 * argument is the starting block and the 'to' argument is the block
605 * the control flow ends to after 'from'.
608 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
610 MonoBasicBlock **newa;
614 if (from->cil_code) {
616 g_print ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
618 g_print ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
621 g_print ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
623 g_print ("edge from entry to exit\n");
627 for (i = 0; i < from->out_count; ++i) {
628 if (to == from->out_bb [i]) {
634 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
635 for (i = 0; i < from->out_count; ++i) {
636 newa [i] = from->out_bb [i];
644 for (i = 0; i < to->in_count; ++i) {
645 if (from == to->in_bb [i]) {
651 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
652 for (i = 0; i < to->in_count; ++i) {
653 newa [i] = to->in_bb [i];
662 * mono_find_block_region:
664 * We mark each basic block with a region ID. We use that to avoid BB
665 * optimizations when blocks are in different regions.
668 * A region token that encodes where this region is, and information
669 * about the clause owner for this block.
671 * The region encodes the try/catch/filter clause that owns this block
672 * as well as the type. -1 is a special value that represents a block
673 * that is in none of try/catch/filter.
676 mono_find_block_region (MonoCompile *cfg, int offset, int *filter_lengths)
678 MonoMethod *method = cfg->method;
679 MonoMethodHeader *header = mono_method_get_header (method);
680 MonoExceptionClause *clause;
683 /* first search for handlers and filters */
684 for (i = 0; i < header->num_clauses; ++i) {
685 clause = &header->clauses [i];
686 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->data.filter_offset) &&
687 (offset < (clause->data.filter_offset + filter_lengths [i])))
688 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
690 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
691 if (clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)
692 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
694 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
698 /* search the try blocks */
699 for (i = 0; i < header->num_clauses; ++i) {
700 clause = &header->clauses [i];
701 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
702 return ((i + 1) << 8) | clause->flags;
709 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
711 MonoMethod *method = cfg->method;
712 MonoMethodHeader *header = mono_method_get_header (method);
713 MonoExceptionClause *clause;
714 MonoBasicBlock *handler;
718 for (i = 0; i < header->num_clauses; ++i) {
719 clause = &header->clauses [i];
720 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
721 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
722 if (clause->flags == type) {
723 handler = g_hash_table_lookup (cfg->bb_hash, header->code + clause->handler_offset);
725 res = g_list_append (res, handler);
733 mono_find_spvar_for_region (MonoCompile *cfg, int region)
735 return g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
739 mono_create_spvar_for_region (MonoCompile *cfg, int region)
743 var = g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
747 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
748 /* prevent it from being register allocated */
749 var->flags |= MONO_INST_INDIRECT;
751 g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
755 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
759 array [*dfn] = start;
760 /*g_print ("visit %d at %p\n", *dfn, start->cil_code);*/
761 for (i = 0; i < start->out_count; ++i) {
762 if (start->out_bb [i]->dfn)
765 start->out_bb [i]->dfn = *dfn;
766 start->out_bb [i]->df_parent = start;
767 array [*dfn] = start->out_bb [i];
768 df_visit (start->out_bb [i], dfn, array);
774 MonoBasicBlock *best;
778 previous_foreach (gconstpointer key, gpointer val, gpointer data)
780 PrevStruct *p = data;
781 MonoBasicBlock *bb = val;
782 //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,
783 //bb->method == p->best->method, bb->cil_code < p->code, bb->cil_code > p->best->cil_code);
785 if (bb->cil_code && bb->cil_code < p->code && bb->cil_code > p->best->cil_code)
789 static MonoBasicBlock*
790 find_previous (GHashTable *bb_hash, MonoBasicBlock *start, const guchar *code) {
796 g_hash_table_foreach (bb_hash, (GHFunc)previous_foreach, &p);
801 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
810 * FIXME: take into account all the details:
811 * second may have been the target of more than one bblock
813 second->out_count = first->out_count;
814 second->out_bb = first->out_bb;
816 for (i = 0; i < first->out_count; ++i) {
817 bb = first->out_bb [i];
818 for (j = 0; j < bb->in_count; ++j) {
819 if (bb->in_bb [j] == first)
820 bb->in_bb [j] = second;
824 first->out_count = 0;
825 first->out_bb = NULL;
826 link_bblock (cfg, first, second);
828 second->last_ins = first->last_ins;
830 /*g_print ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
831 for (inst = first->code; inst && inst->next; inst = inst->next) {
832 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
833 g_print ("found %p: %s", inst->next->cil_code, code);
835 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
836 second->code = inst->next;
838 first->last_ins = inst;
839 second->next_bb = first->next_bb;
840 first->next_bb = second;
845 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
851 mono_type_to_ldind (MonoType *type)
857 switch (type->type) {
861 case MONO_TYPE_BOOLEAN:
875 case MONO_TYPE_FNPTR:
877 case MONO_TYPE_CLASS:
878 case MONO_TYPE_STRING:
879 case MONO_TYPE_OBJECT:
880 case MONO_TYPE_SZARRAY:
881 case MONO_TYPE_ARRAY:
882 return CEE_LDIND_REF;
890 case MONO_TYPE_VALUETYPE:
891 if (type->data.klass->enumtype) {
892 type = type->data.klass->enum_basetype;
896 case MONO_TYPE_TYPEDBYREF:
898 case MONO_TYPE_GENERICINST:
899 type = type->data.generic_inst->generic_type;
902 g_error ("unknown type 0x%02x in type_to_ldind", type->type);
908 mono_type_to_stind (MonoType *type)
914 switch (type->type) {
917 case MONO_TYPE_BOOLEAN:
929 case MONO_TYPE_FNPTR:
931 case MONO_TYPE_CLASS:
932 case MONO_TYPE_STRING:
933 case MONO_TYPE_OBJECT:
934 case MONO_TYPE_SZARRAY:
935 case MONO_TYPE_ARRAY:
936 return CEE_STIND_REF;
944 case MONO_TYPE_VALUETYPE:
945 if (type->data.klass->enumtype) {
946 type = type->data.klass->enum_basetype;
950 case MONO_TYPE_TYPEDBYREF:
952 case MONO_TYPE_GENERICINST:
953 type = type->data.generic_inst->generic_type;
956 g_error ("unknown type 0x%02x in type_to_stind", type->type);
962 * Returns the type used in the eval stack when @type is loaded.
963 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
966 type_to_eval_stack_type (MonoType *type, MonoInst *inst)
971 inst->type = STACK_MP;
975 klass = mono_class_from_mono_type (type);
978 switch (type->type) {
981 case MONO_TYPE_BOOLEAN:
987 inst->type = STACK_I4;
992 case MONO_TYPE_FNPTR:
993 inst->type = STACK_PTR;
995 case MONO_TYPE_CLASS:
996 case MONO_TYPE_STRING:
997 case MONO_TYPE_OBJECT:
998 case MONO_TYPE_SZARRAY:
999 case MONO_TYPE_ARRAY:
1000 inst->type = STACK_OBJ;
1004 inst->type = STACK_I8;
1008 inst->type = STACK_R8;
1010 case MONO_TYPE_VALUETYPE:
1011 if (type->data.klass->enumtype) {
1012 type = type->data.klass->enum_basetype;
1015 inst->klass = klass;
1016 inst->type = STACK_VTYPE;
1019 case MONO_TYPE_TYPEDBYREF:
1020 inst->klass = mono_defaults.typed_reference_class;
1021 inst->type = STACK_VTYPE;
1023 case MONO_TYPE_GENERICINST:
1024 type = type->data.generic_inst->generic_type;
1027 g_error ("unknown type 0x%02x in eval stack type", type->type);
1032 * The following tables are used to quickly validate the IL code in type_from_op ().
1035 bin_num_table [STACK_MAX] [STACK_MAX] = {
1036 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1037 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1038 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1039 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1040 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
1041 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
1042 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1043 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1048 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
1051 /* reduce the size of this table */
1053 bin_int_table [STACK_MAX] [STACK_MAX] = {
1054 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1055 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1056 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1057 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1058 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1059 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1060 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1061 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1065 bin_comp_table [STACK_MAX] [STACK_MAX] = {
1067 {0, 1, 0, 1, 0, 0, 4, 0},
1068 {0, 0, 1, 0, 0, 0, 0, 0},
1069 {0, 1, 0, 1, 0, 2, 4, 0},
1070 {0, 0, 0, 0, 1, 0, 0, 0},
1071 {0, 0, 0, 2, 0, 1, 0, 0},
1072 {0, 4, 0, 4, 0, 0, 3, 0},
1073 {0, 0, 0, 0, 0, 0, 0, 0},
1076 /* reduce the size of this table */
1078 shift_table [STACK_MAX] [STACK_MAX] = {
1079 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1080 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1081 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1082 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1083 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1084 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1085 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1086 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1090 * Tables to map from the non-specific opcode to the matching
1091 * type-specific opcode.
1093 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
1094 static const guint16
1095 binops_op_map [STACK_MAX] = {
1096 0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, OP_PADD-CEE_ADD
1099 /* handles from CEE_NEG to CEE_CONV_U8 */
1100 static const guint16
1101 unops_op_map [STACK_MAX] = {
1102 0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, OP_PNEG-CEE_NEG
1105 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
1106 static const guint16
1107 ovfops_op_map [STACK_MAX] = {
1108 0, 0, OP_LCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_FCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2
1111 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
1112 static const guint16
1113 ovf2ops_op_map [STACK_MAX] = {
1114 0, 0, OP_LCONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN, OP_PCONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN, OP_FCONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN, OP_PCONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN
1117 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
1118 static const guint16
1119 ovf3ops_op_map [STACK_MAX] = {
1120 0, 0, OP_LCONV_TO_OVF_I1-CEE_CONV_OVF_I1, OP_PCONV_TO_OVF_I1-CEE_CONV_OVF_I1, OP_FCONV_TO_OVF_I1-CEE_CONV_OVF_I1, OP_PCONV_TO_OVF_I1-CEE_CONV_OVF_I1
1123 /* handles from CEE_CEQ to CEE_CLT_UN */
1124 static const guint16
1125 ceqops_op_map [STACK_MAX] = {
1126 0, 0, OP_LCEQ-CEE_CEQ, OP_PCEQ-CEE_CEQ, OP_FCEQ-CEE_CEQ, OP_LCEQ-CEE_CEQ
1130 * Sets ins->type (the type on the eval stack) according to the
1131 * type of the opcode and the arguments to it.
1132 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1134 * FIXME: this function sets ins->type unconditionally in some cases, but
1135 * it should set it to invalid for some types (a conv.x on an object)
1138 type_from_op (MonoInst *ins) {
1139 switch (ins->opcode) {
1146 /* FIXME: check unverifiable args for STACK_MP */
1147 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1148 ins->opcode += binops_op_map [ins->type];
1155 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1156 ins->opcode += binops_op_map [ins->type];
1161 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1162 ins->opcode += binops_op_map [ins->type];
1166 /* FIXME: handle some specifics with ins->next->type */
1167 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1168 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))))
1169 ins->opcode = OP_LCOMPARE;
1176 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1177 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1181 ins->type = neg_table [ins->inst_i0->type];
1182 ins->opcode += unops_op_map [ins->type];
1185 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1186 ins->type = ins->inst_i0->type;
1188 ins->type = STACK_INV;
1189 ins->opcode += unops_op_map [ins->type];
1195 ins->type = STACK_I4;
1196 ins->opcode += unops_op_map [ins->inst_i0->type];
1199 ins->type = STACK_R8;
1200 switch (ins->inst_i0->type) {
1205 ins->opcode = OP_LCONV_TO_R_UN;
1209 case CEE_CONV_OVF_I1:
1210 case CEE_CONV_OVF_U1:
1211 case CEE_CONV_OVF_I2:
1212 case CEE_CONV_OVF_U2:
1213 case CEE_CONV_OVF_I4:
1214 case CEE_CONV_OVF_U4:
1215 ins->type = STACK_I4;
1216 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1218 case CEE_CONV_OVF_I_UN:
1219 case CEE_CONV_OVF_U_UN:
1220 ins->type = STACK_PTR;
1221 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1223 case CEE_CONV_OVF_I1_UN:
1224 case CEE_CONV_OVF_I2_UN:
1225 case CEE_CONV_OVF_I4_UN:
1226 case CEE_CONV_OVF_U1_UN:
1227 case CEE_CONV_OVF_U2_UN:
1228 case CEE_CONV_OVF_U4_UN:
1229 ins->type = STACK_I4;
1230 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1233 ins->type = STACK_PTR;
1234 switch (ins->inst_i0->type) {
1240 ins->opcode = OP_LCONV_TO_U;
1243 ins->opcode = OP_FCONV_TO_U;
1249 ins->type = STACK_I8;
1250 ins->opcode += unops_op_map [ins->inst_i0->type];
1252 case CEE_CONV_OVF_I8:
1253 case CEE_CONV_OVF_U8:
1254 ins->type = STACK_I8;
1255 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1257 case CEE_CONV_OVF_U8_UN:
1258 case CEE_CONV_OVF_I8_UN:
1259 ins->type = STACK_I8;
1260 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1264 ins->type = STACK_R8;
1265 ins->opcode += unops_op_map [ins->inst_i0->type];
1268 ins->type = STACK_R8;
1272 ins->type = STACK_I4;
1273 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1276 case CEE_CONV_OVF_I:
1277 case CEE_CONV_OVF_U:
1278 ins->type = STACK_PTR;
1279 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1282 case CEE_ADD_OVF_UN:
1284 case CEE_MUL_OVF_UN:
1286 case CEE_SUB_OVF_UN:
1287 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1288 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1291 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1298 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_MP, STACK_R8, STACK_R8, STACK_OBJ
1301 /* map ldelem.x to the matching ldind.x opcode */
1303 ldelem_to_ldind [] = {
1317 /* map stelem.x to the matching stind.x opcode */
1319 stelem_to_stind [] = {
1333 param_table [STACK_MAX] [STACK_MAX] = {
1338 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1342 switch (args->type) {
1352 for (i = 0; i < sig->param_count; ++i) {
1353 switch (args [i].type) {
1357 if (!sig->params [i]->byref)
1361 if (sig->params [i]->byref)
1363 switch (sig->params [i]->type) {
1364 case MONO_TYPE_CLASS:
1365 case MONO_TYPE_STRING:
1366 case MONO_TYPE_OBJECT:
1367 case MONO_TYPE_SZARRAY:
1368 case MONO_TYPE_ARRAY:
1375 if (sig->params [i]->byref)
1377 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1386 /*if (!param_table [args [i].type] [sig->params [i]->type])
1394 * When we need a pointer to the current domain many times in a method, we
1395 * call mono_domain_get() once and we store the result in a local variable.
1396 * This function returns the variable that represents the MonoDomain*.
1398 inline static MonoInst *
1399 mono_get_domainvar (MonoCompile *cfg)
1401 if (!cfg->domainvar)
1402 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1403 return cfg->domainvar;
1407 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
1410 int num = cfg->num_varinfo;
1412 if ((num + 1) >= cfg->varinfo_count) {
1413 cfg->varinfo_count = (cfg->varinfo_count + 2) * 2;
1414 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
1415 cfg->vars = (MonoMethodVar **)g_realloc (cfg->vars, sizeof (MonoMethodVar*) * cfg->varinfo_count);
1418 /*g_print ("created temp %d of type 0x%x\n", num, type->type);*/
1419 mono_jit_stats.allocate_var++;
1421 MONO_INST_NEW (cfg, inst, opcode);
1422 inst->inst_c0 = num;
1423 inst->inst_vtype = type;
1424 inst->klass = mono_class_from_mono_type (type);
1425 /* if set to 1 the variable is native */
1428 cfg->varinfo [num] = inst;
1430 cfg->vars [num] = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMethodVar));
1431 MONO_INIT_VARINFO (cfg->vars [num], num);
1434 //g_print ("created temp %d of type %s\n", num, mono_type_get_name (type));
1439 type_from_stack_type (MonoInst *ins) {
1440 switch (ins->type) {
1441 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1442 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1443 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1444 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1445 case STACK_MP: return &mono_defaults.int_class->byval_arg;
1446 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1447 case STACK_VTYPE: return &ins->klass->byval_arg;
1449 g_error ("stack type %d to montype not handled\n", ins->type);
1455 array_access_to_klass (int opcode)
1459 return mono_defaults.byte_class;
1461 return mono_defaults.uint16_class;
1464 return mono_defaults.int_class;
1467 return mono_defaults.sbyte_class;
1470 return mono_defaults.int16_class;
1473 return mono_defaults.int32_class;
1475 return mono_defaults.uint32_class;
1478 return mono_defaults.int64_class;
1481 return mono_defaults.single_class;
1484 return mono_defaults.double_class;
1485 case CEE_LDELEM_REF:
1486 case CEE_STELEM_REF:
1487 return mono_defaults.object_class;
1489 g_assert_not_reached ();
1495 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
1499 MONO_ADD_INS (bb, inst);
1502 switch (bb->last_ins->opcode) {
1516 while (prev->next && prev->next != bb->last_ins)
1518 if (prev == bb->code) {
1519 if (bb->last_ins == bb->code) {
1520 inst->next = bb->code;
1523 inst->next = prev->next;
1527 inst->next = bb->last_ins;
1531 // g_warning ("handle conditional jump in add_ins_to_end ()\n");
1533 MONO_ADD_INS (bb, inst);
1539 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
1541 MonoInst *inst, *load;
1543 NEW_TEMPLOAD (cfg, load, src);
1545 NEW_TEMPSTORE (cfg, inst, dest, load);
1546 if (inst->opcode == CEE_STOBJ) {
1547 NEW_TEMPLOADA (cfg, inst, dest);
1548 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE);
1550 inst->cil_code = NULL;
1551 mono_add_ins_to_end (bb, inst);
1556 * We try to share variables when possible
1559 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1564 /* inlining can result in deeper stacks */
1565 if (slot >= mono_method_get_header (cfg->method)->max_stack)
1566 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1568 pos = ins->type - 1 + slot * STACK_MAX;
1570 switch (ins->type) {
1577 if ((vnum = cfg->intvars [pos]))
1578 return cfg->varinfo [vnum];
1579 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1580 cfg->intvars [pos] = res->inst_c0;
1583 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1589 * This function is called to handle items that are left on the evaluation stack
1590 * at basic block boundaries. What happens is that we save the values to local variables
1591 * and we reload them later when first entering the target basic block (with the
1592 * handle_loaded_temps () function).
1593 * It is also used to handle items on the stack in store opcodes, since it is
1594 * possible that the variable to be stored into is already on the stack, in
1595 * which case its old value should be used.
1596 * A single joint point will use the same variables (stored in the array bb->out_stack or
1597 * bb->in_stack, if the basic block is before or after the joint point).
1600 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count) {
1602 MonoBasicBlock *outb;
1603 MonoInst *inst, **locals;
1608 if (cfg->verbose_level > 3)
1609 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
1610 if (!bb->out_scount) {
1611 bb->out_scount = count;
1612 //g_print ("bblock %d has out:", bb->block_num);
1614 for (i = 0; i < bb->out_count; ++i) {
1615 outb = bb->out_bb [i];
1616 //g_print (" %d", outb->block_num);
1617 if (outb->in_stack) {
1619 bb->out_stack = outb->in_stack;
1625 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1626 for (i = 0; i < count; ++i) {
1628 * try to reuse temps already allocated for this purpouse, if they occupy the same
1629 * stack slot and if they are of the same type.
1630 * This won't cause conflicts since if 'local' is used to
1631 * store one of the values in the in_stack of a bblock, then
1632 * the same variable will be used for the same outgoing stack
1634 * This doesn't work when inlining methods, since the bblocks
1635 * in the inlined methods do not inherit their in_stack from
1636 * the bblock they are inlined to. See bug #58863 for an
1639 if (cfg->inlined_method)
1640 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1642 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1647 for (i = 0; i < bb->out_count; ++i) {
1648 outb = bb->out_bb [i];
1649 if (outb->in_scount)
1650 continue; /* check they are the same locals */
1651 outb->in_scount = count;
1652 outb->in_stack = bb->out_stack;
1655 locals = bb->out_stack;
1656 for (i = 0; i < count; ++i) {
1657 /* add store ops at the end of the bb, before the branch */
1658 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1659 if (inst->opcode == CEE_STOBJ) {
1660 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
1661 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE);
1663 inst->cil_code = sp [i]->cil_code;
1664 mono_add_ins_to_end (bb, inst);
1666 if (cfg->verbose_level > 3)
1667 g_print ("storing %d to temp %d\n", i, locals [i]->inst_c0);
1671 * It is possible that the out bblocks already have in_stack assigned, and
1672 * the in_stacks differ. In this case, we will store to all the different
1679 /* Find a bblock which has a different in_stack */
1681 while (bindex < bb->out_count) {
1682 outb = bb->out_bb [bindex];
1683 if (outb->in_stack != locals) {
1685 * Instead of storing sp [i] to locals [i], we need to store
1686 * locals [i] to <new locals>[i], since the sp [i] tree can't
1687 * be shared between trees.
1689 for (i = 0; i < count; ++i)
1690 mono_add_varcopy_to_end (cfg, bb, locals [i]->inst_c0, outb->in_stack [i]->inst_c0);
1691 locals = outb->in_stack;
1703 ret_type_to_call_opcode (MonoType *type, int calli, int virt)
1706 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1709 switch (type->type) {
1710 case MONO_TYPE_VOID:
1711 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
1714 case MONO_TYPE_BOOLEAN:
1717 case MONO_TYPE_CHAR:
1720 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1724 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1725 case MONO_TYPE_CLASS:
1726 case MONO_TYPE_STRING:
1727 case MONO_TYPE_OBJECT:
1728 case MONO_TYPE_SZARRAY:
1729 case MONO_TYPE_ARRAY:
1730 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1733 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
1736 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
1737 case MONO_TYPE_VALUETYPE:
1738 if (type->data.klass->enumtype) {
1739 type = type->data.klass->enum_basetype;
1742 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1743 case MONO_TYPE_TYPEDBYREF:
1744 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1745 case MONO_TYPE_GENERICINST:
1746 type = type->data.generic_inst->generic_type;
1749 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
1755 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
1757 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
1759 ji->ip.label = label;
1760 ji->type = MONO_PATCH_INFO_SWITCH;
1761 ji->data.table = bbs;
1762 ji->next = cfg->patch_info;
1763 ji->table_size = num_blocks;
1764 cfg->patch_info = ji;
1768 * When we add a tree of instructions, we need to ensure the instructions currently
1769 * on the stack are executed before (like, if we load a value from a local).
1770 * We ensure this by saving the currently loaded values to temps and rewriting the
1771 * instructions to load the values.
1772 * This is not done for opcodes that terminate a basic block (because it's handled already
1773 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
1776 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
1778 MonoInst *load, *store, *temp, *ins;
1780 while (stack < sp) {
1782 /* handle also other constants */
1783 if (ins->opcode != OP_ICONST) {
1784 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1785 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1786 store->cil_code = ins->cil_code;
1787 if (store->opcode == CEE_STOBJ) {
1788 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
1789 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE);
1791 MONO_ADD_INS (bblock, store);
1792 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1793 load->cil_code = ins->cil_code;
1801 * Prepare arguments for passing to a function call.
1802 * Return a non-zero value if the arguments can't be passed to the given
1804 * The type checks are not yet complete and some conversions may need
1805 * casts on 32 or 64 bit architectures.
1808 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
1810 MonoType *simple_type;
1814 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
1818 for (i = 0; i < sig->param_count; ++i) {
1819 if (sig->params [i]->byref) {
1821 * check the result of ldelema is only passed as an argument if the byref
1822 * type matches exactly the array element type.
1823 * FIXME: if the argument as been saved on the stack as part of the
1824 * interface variable code (the value was on the stack at a basic block boundary)
1825 * we need to add the check in that case, too.
1827 if (args [i]->opcode == CEE_LDELEMA) {
1829 MonoClass *exact_class = mono_class_from_mono_type (sig->params [i]);
1830 if (!exact_class->valuetype) {
1831 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
1832 check->cil_code = args [i]->cil_code;
1833 check->klass = exact_class;
1834 check->inst_left = args [i]->inst_left;
1835 check->type = STACK_OBJ;
1836 args [i]->inst_left = check;
1839 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
1843 simple_type = sig->params [i];
1845 switch (simple_type->type) {
1846 case MONO_TYPE_VOID:
1851 case MONO_TYPE_BOOLEAN:
1854 case MONO_TYPE_CHAR:
1857 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
1863 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
1866 case MONO_TYPE_CLASS:
1867 case MONO_TYPE_STRING:
1868 case MONO_TYPE_OBJECT:
1869 case MONO_TYPE_SZARRAY:
1870 case MONO_TYPE_ARRAY:
1871 if (args [i]->type != STACK_OBJ)
1876 if (args [i]->type != STACK_I8)
1881 if (args [i]->type != STACK_R8)
1884 case MONO_TYPE_VALUETYPE:
1885 if (simple_type->data.klass->enumtype) {
1886 simple_type = simple_type->data.klass->enum_basetype;
1889 if (args [i]->type != STACK_VTYPE)
1892 case MONO_TYPE_TYPEDBYREF:
1893 if (args [i]->type != STACK_VTYPE)
1896 case MONO_TYPE_GENERICINST:
1897 simple_type = simple_type->data.generic_inst->generic_type;
1901 g_error ("unknown type 0x%02x in check_call_signature",
1909 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
1910 const guint8 *ip, gboolean to_end)
1912 MonoInst *temp, *store, *ins = (MonoInst*)call;
1913 MonoType *ret = sig->ret;
1915 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
1917 call->inst.type = STACK_OBJ;
1918 call->inst.opcode = CEE_CALL;
1919 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
1921 type_to_eval_stack_type (ret, ins);
1922 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
1925 if (MONO_TYPE_ISSTRUCT (ret)) {
1928 /* we use this to allocate native sized structs */
1929 temp->unused = sig->pinvoke;
1931 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
1932 if (call->inst.opcode == OP_VCALL)
1933 ins->inst_left = loada;
1935 ins->inst_right = loada; /* a virtual or indirect call */
1938 mono_add_ins_to_end (bblock, ins);
1940 MONO_ADD_INS (bblock, ins);
1942 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1943 store->cil_code = ip;
1945 mono_add_ins_to_end (bblock, store);
1947 MONO_ADD_INS (bblock, store);
1949 return temp->inst_c0;
1952 mono_add_ins_to_end (bblock, ins);
1954 MONO_ADD_INS (bblock, ins);
1959 inline static MonoCallInst *
1960 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1961 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
1966 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual));
1968 call->inst.cil_code = ip;
1970 call->signature = sig;
1971 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
1973 for (arg = call->out_args; arg;) {
1974 MonoInst *narg = arg->next;
1979 mono_add_ins_to_end (bblock, arg);
1981 MONO_ADD_INS (bblock, arg);
1988 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1989 MonoInst **args, MonoInst *addr, const guint8 *ip)
1991 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
1993 call->inst.inst_i0 = addr;
1995 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
1998 static MonoCallInst*
1999 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
2000 MonoInst **args, const guint8 *ip, MonoInst *this)
2002 gboolean virtual = this != NULL;
2005 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, FALSE);
2007 if (this && sig->hasthis &&
2008 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
2009 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
2010 call->method = mono_marshal_get_remoting_invoke_with_check (method);
2012 call->method = method;
2014 call->inst.flags |= MONO_INST_HAS_METHOD;
2015 call->inst.inst_left = this;
2021 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
2022 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
2024 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, signature, args, ip, this);
2026 return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
2030 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
2031 MonoInst **args, const guint8 *ip, gboolean ret_object, gboolean to_end)
2037 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
2039 return mono_spill_call (cfg, bblock, call, sig, ret_object, ip, to_end);
2043 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
2045 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
2048 g_warning ("unregistered JIT ICall");
2049 g_assert_not_reached ();
2052 return mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, args, ip, FALSE, FALSE);
2056 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
2058 MonoInst *ins, *temp = NULL, *store, *load, *begin;
2059 MonoInst *last_arg = NULL;
2063 //g_print ("emulating: ");
2064 //mono_print_tree_nl (tree);
2065 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE));
2066 ins = (MonoInst*)call;
2068 call->inst.cil_code = tree->cil_code;
2070 call->signature = info->sig;
2072 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
2074 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2075 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
2076 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2077 store->cil_code = tree->cil_code;
2082 nargs = info->sig->param_count + info->sig->hasthis;
2084 for (last_arg = call->out_args; last_arg && last_arg->next; last_arg = last_arg->next) ;
2087 last_arg->next = store;
2090 begin = call->out_args;
2094 if (cfg->prev_ins) {
2096 * This assumes that that in a tree, emulate_opcode is called for a
2097 * node before it is called for its children. dec_foreach needs to
2098 * take this into account.
2100 store->next = cfg->prev_ins->next;
2101 cfg->prev_ins->next = begin;
2103 store->next = cfg->cbb->code;
2104 cfg->cbb->code = begin;
2107 call->fptr = mono_icall_get_wrapper (info);
2109 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2110 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2115 static MonoMethodSignature *
2116 mono_get_element_address_signature (int arity)
2118 static GHashTable *sighash = NULL;
2119 MonoMethodSignature *res;
2122 EnterCriticalSection (&jit_mutex);
2124 sighash = g_hash_table_new (NULL, NULL);
2126 else if ((res = g_hash_table_lookup (sighash, (gpointer)arity))) {
2127 LeaveCriticalSection (&jit_mutex);
2131 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
2134 #ifdef MONO_ARCH_VARARG_ICALLS
2135 /* Only set this only some archs since not all backends can handle varargs+pinvoke */
2136 res->call_convention = MONO_CALL_VARARG;
2138 res->params [0] = &mono_defaults.array_class->byval_arg;
2140 for (i = 1; i <= arity; i++)
2141 res->params [i] = &mono_defaults.int_class->byval_arg;
2143 res->ret = &mono_defaults.int_class->byval_arg;
2145 g_hash_table_insert (sighash, (gpointer)arity, res);
2146 LeaveCriticalSection (&jit_mutex);
2151 static MonoMethodSignature *
2152 mono_get_array_new_va_signature (int arity)
2154 static GHashTable *sighash = NULL;
2155 MonoMethodSignature *res;
2158 EnterCriticalSection (&jit_mutex);
2160 sighash = g_hash_table_new (NULL, NULL);
2162 else if ((res = g_hash_table_lookup (sighash, (gpointer)arity))) {
2163 LeaveCriticalSection (&jit_mutex);
2167 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
2170 #ifdef MONO_ARCH_VARARG_ICALLS
2171 /* Only set this only some archs since not all backends can handle varargs+pinvoke */
2172 res->call_convention = MONO_CALL_VARARG;
2175 res->params [0] = &mono_defaults.int_class->byval_arg;
2176 for (i = 0; i < arity; i++)
2177 res->params [i + 1] = &mono_defaults.int_class->byval_arg;
2179 res->ret = &mono_defaults.int_class->byval_arg;
2181 g_hash_table_insert (sighash, (gpointer)arity, res);
2182 LeaveCriticalSection (&jit_mutex);
2188 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native) {
2189 MonoInst *iargs [3];
2195 * This check breaks with spilled vars... need to handle it during verification anyway.
2196 * g_assert (klass && klass == src->klass && klass == dest->klass);
2200 n = mono_class_native_size (klass, &align);
2202 n = mono_class_value_size (klass, &align);
2204 if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
2206 MONO_INST_NEW (cfg, inst, OP_MEMCPY);
2207 inst->inst_left = dest;
2208 inst->inst_right = src;
2209 inst->cil_code = ip;
2211 MONO_ADD_INS (bblock, inst);
2216 NEW_ICONST (cfg, iargs [2], n);
2218 mono_emit_native_call (cfg, bblock, helper_memcpy, helper_sig_memcpy, iargs, ip, FALSE, to_end);
2222 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
2224 MonoInst *iargs [2];
2225 MonoInst *ins, *zero_int32;
2228 NEW_ICONST (cfg, zero_int32, 0);
2230 mono_class_init (klass);
2231 n = mono_class_value_size (klass, NULL);
2232 MONO_INST_NEW (cfg, ins, 0);
2234 ins->inst_left = dest;
2235 ins->inst_right = zero_int32;
2238 ins->opcode = CEE_STIND_I1;
2239 MONO_ADD_INS (bblock, ins);
2242 ins->opcode = CEE_STIND_I2;
2243 MONO_ADD_INS (bblock, ins);
2246 ins->opcode = CEE_STIND_I4;
2247 MONO_ADD_INS (bblock, ins);
2250 if (n <= sizeof (gpointer) * 5) {
2251 ins->opcode = OP_MEMSET;
2254 MONO_ADD_INS (bblock, ins);
2257 handle_loaded_temps (cfg, bblock, stack_start, sp);
2258 NEW_ICONST (cfg, ins, n);
2261 mono_emit_jit_icall (cfg, bblock, helper_initobj, iargs, ip);
2267 handle_alloc (MonoCompile *cfg, MonoBasicBlock *bblock, MonoClass *klass, const guchar *ip)
2269 MonoInst *iargs [2];
2272 if (cfg->opt & MONO_OPT_SHARED) {
2273 NEW_DOMAINCONST (cfg, iargs [0]);
2274 NEW_CLASSCONST (cfg, iargs [1], klass);
2276 alloc_ftn = mono_object_new;
2278 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
2281 alloc_ftn = mono_class_get_allocation_ftn (vtable, &pass_lw);
2283 guint32 lw = vtable->klass->instance_size;
2284 lw = ((lw + (sizeof (gpointer) - 1)) & ~(sizeof (gpointer) - 1)) / sizeof (gpointer);
2285 NEW_ICONST (cfg, iargs [0], lw);
2286 NEW_VTABLECONST (cfg, iargs [1], vtable);
2289 NEW_VTABLECONST (cfg, iargs [0], vtable);
2292 return mono_emit_jit_icall (cfg, bblock, alloc_ftn, iargs, ip);
2296 handle_box (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *val, const guchar *ip, MonoClass *klass)
2298 MonoInst *dest, *vtoffset, *add, *vstore;
2301 temp = handle_alloc (cfg, bblock, klass, ip);
2302 NEW_TEMPLOAD (cfg, dest, temp);
2303 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
2304 MONO_INST_NEW (cfg, add, OP_PADD);
2305 add->inst_left = dest;
2306 add->inst_right = vtoffset;
2309 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
2310 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
2311 vstore->cil_code = ip;
2312 vstore->inst_left = add;
2313 vstore->inst_right = val;
2315 if (vstore->opcode == CEE_STOBJ) {
2316 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
2318 MONO_ADD_INS (bblock, vstore);
2320 NEW_TEMPLOAD (cfg, dest, temp);
2325 handle_array_new (MonoCompile *cfg, MonoBasicBlock *bblock, int rank, MonoInst **sp, unsigned char *ip)
2327 MonoMethodSignature *esig;
2328 char icall_name [256];
2329 MonoJitICallInfo *info;
2331 /* Need to register the icall so it gets an icall wrapper */
2332 sprintf (icall_name, "ves_array_new_va_%d", rank);
2334 info = mono_find_jit_icall_by_name (icall_name);
2336 esig = mono_get_array_new_va_signature (rank);
2337 info = mono_register_jit_icall (mono_array_new_va, g_strdup (icall_name), esig, FALSE);
2340 cfg->flags |= MONO_CFG_HAS_VARARGS;
2342 return mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, sp, ip, TRUE, FALSE);
2345 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
2348 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
2350 MonoMethodHeader *header = mono_method_get_header (method);
2351 MonoMethodSignature *signature = method->signature;
2355 #ifdef MONO_ARCH_HAVE_LMF_OPS
2356 if (((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2357 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) &&
2358 !MONO_TYPE_ISSTRUCT (signature->ret) && (method->klass->parent != mono_defaults.array_class))
2362 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2363 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2364 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
2365 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
2366 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2367 (method->klass->marshalbyref) ||
2368 !header || header->num_clauses ||
2369 /* fixme: why cant we inline valuetype returns? */
2370 MONO_TYPE_ISSTRUCT (signature->ret))
2373 /* its not worth to inline methods with valuetype arguments?? */
2374 for (i = 0; i < signature->param_count; i++) {
2375 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
2381 * if we can initialize the class of the method right away, we do,
2382 * otherwise we don't allow inlining if the class needs initialization,
2383 * since it would mean inserting a call to mono_runtime_class_init()
2384 * inside the inlined code
2386 if (!(cfg->opt & MONO_OPT_SHARED)) {
2387 vtable = mono_class_vtable (cfg->domain, method->klass);
2388 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
2389 if (cfg->run_cctors)
2390 mono_runtime_class_init (vtable);
2392 else if (!vtable->initialized && mono_class_needs_cctor_run (method->klass, NULL))
2396 * If we're compiling for shared code
2397 * the cctor will need to be run at aot method load time, for example,
2398 * or at the end of the compilation of the inlining method.
2400 if (mono_class_needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
2403 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
2406 * CAS - do not inline methods with declarative security
2407 * Note: this has to be before any possible return TRUE;
2409 if (mono_method_has_declsec (method))
2412 /* also consider num_locals? */
2413 if (getenv ("MONO_INLINELIMIT")) {
2414 if (header->code_size < atoi (getenv ("MONO_INLINELIMIT"))) {
2417 } else if (header->code_size < 20)
2424 mini_get_ldelema_ins (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
2428 MonoMethodSignature *esig;
2429 char icall_name [256];
2430 MonoJitICallInfo *info;
2432 rank = cmethod->signature->param_count - (is_set? 1: 0);
2434 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
2436 NEW_GROUP (cfg, indexes, sp [1], sp [2]);
2437 MONO_INST_NEW (cfg, addr, OP_LDELEMA2D);
2438 addr->inst_left = sp [0];
2439 addr->inst_right = indexes;
2440 addr->cil_code = ip;
2441 addr->type = STACK_MP;
2442 addr->klass = cmethod->klass;
2446 /* Need to register the icall so it gets an icall wrapper */
2447 sprintf (icall_name, "ves_array_element_address_%d", rank);
2449 info = mono_find_jit_icall_by_name (icall_name);
2451 esig = mono_get_element_address_signature (rank);
2452 info = mono_register_jit_icall (ves_array_element_address, g_strdup (icall_name), esig, FALSE);
2455 temp = mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, sp, ip, FALSE, FALSE);
2456 cfg->flags |= MONO_CFG_HAS_VARARGS;
2458 NEW_TEMPLOAD (cfg, addr, temp);
2462 static MonoJitICallInfo **emul_opcode_map = NULL;
2464 static inline MonoJitICallInfo *
2465 mono_find_jit_opcode_emulation (int opcode)
2467 if (emul_opcode_map)
2468 return emul_opcode_map [opcode];
2474 mini_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
2479 static MonoClass *runtime_helpers_class = NULL;
2480 if (! runtime_helpers_class)
2481 runtime_helpers_class = mono_class_from_name (mono_defaults.corlib,
2482 "System.Runtime.CompilerServices", "RuntimeHelpers");
2484 if (cmethod->klass == mono_defaults.string_class) {
2485 if (cmethod->name [0] != 'g')
2488 if (strcmp (cmethod->name, "get_Chars") == 0)
2490 else if (strcmp (cmethod->name, "get_Length") == 0)
2493 } else if (cmethod->klass == mono_defaults.object_class) {
2494 if (strcmp (cmethod->name, "GetType") == 0)
2498 } else if (cmethod->klass == mono_defaults.array_class) {
2499 if (strcmp (cmethod->name, "get_Rank") == 0)
2501 else if (strcmp (cmethod->name, "get_Length") == 0)
2505 } else if (cmethod->klass == runtime_helpers_class) {
2506 if (strcmp (cmethod->name, "get_OffsetToStringData") == 0) {
2507 NEW_ICONST (cfg, ins, G_STRUCT_OFFSET (MonoString, chars));
2511 } else if (cmethod->klass == mono_defaults.thread_class) {
2512 if (strcmp (cmethod->name, "get_CurrentThread") == 0 && (ins = mono_arch_get_thread_intrinsic (cfg)))
2516 op = mono_arch_get_opcode_for_method (cfg, cmethod, fsig, args);
2520 pc = fsig->param_count + fsig->hasthis;
2521 MONO_INST_NEW (cfg, ins, op);
2524 ins->inst_i0 = args [0];
2526 ins->inst_i1 = args [1];
2533 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
2535 MonoInst *store, *temp;
2538 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
2540 if (!sig->hasthis && sig->param_count == 0)
2544 if (sp [0]->opcode == OP_ICONST) {
2547 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
2549 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2550 store->cil_code = sp [0]->cil_code;
2551 MONO_ADD_INS (bblock, store);
2556 for (i = 0; i < sig->param_count; ++i) {
2557 if (sp [0]->opcode == OP_ICONST) {
2560 temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
2562 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2563 store->cil_code = sp [0]->cil_code;
2564 if (store->opcode == CEE_STOBJ) {
2565 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2566 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE);
2568 MONO_ADD_INS (bblock, store);
2576 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
2577 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b, gboolean inline_allways)
2579 MonoInst *ins, *rvar = NULL;
2580 MonoMethodHeader *cheader;
2581 MonoBasicBlock *ebblock, *sbblock;
2582 int i, costs, new_locals_offset;
2583 MonoMethod *prev_inlined_method;
2585 if (cfg->verbose_level > 2)
2586 g_print ("INLINE START %p %s -> %s\n", cmethod, mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2588 if (!cmethod->inline_info) {
2589 mono_jit_stats.inlineable_methods++;
2590 cmethod->inline_info = 1;
2592 /* allocate space to store the return value */
2593 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2594 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2597 /* allocate local variables */
2598 cheader = mono_method_get_header (cmethod);
2599 new_locals_offset = cfg->num_varinfo;
2600 for (i = 0; i < cheader->num_locals; ++i)
2601 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
2603 /* allocate starte and end blocks */
2604 sbblock = NEW_BBLOCK (cfg);
2605 sbblock->block_num = cfg->num_bblocks++;
2606 sbblock->real_offset = real_offset;
2608 ebblock = NEW_BBLOCK (cfg);
2609 ebblock->block_num = cfg->num_bblocks++;
2610 ebblock->real_offset = real_offset;
2612 prev_inlined_method = cfg->inlined_method;
2613 cfg->inlined_method = cmethod;
2615 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
2617 cfg->inlined_method = prev_inlined_method;
2619 if ((costs >= 0 && costs < 60) || inline_allways) {
2620 if (cfg->verbose_level > 2)
2621 g_print ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2623 mono_jit_stats.inlined_methods++;
2625 /* always add some code to avoid block split failures */
2626 MONO_INST_NEW (cfg, ins, CEE_NOP);
2627 MONO_ADD_INS (bblock, ins);
2630 bblock->next_bb = sbblock;
2631 link_bblock (cfg, bblock, sbblock);
2634 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
2640 if (cfg->verbose_level > 2)
2641 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
2647 * Some of these comments may well be out-of-date.
2648 * Design decisions: we do a single pass over the IL code (and we do bblock
2649 * splitting/merging in the few cases when it's required: a back jump to an IL
2650 * address that was not already seen as bblock starting point).
2651 * Code is validated as we go (full verification is still better left to metadata/verify.c).
2652 * Complex operations are decomposed in simpler ones right away. We need to let the
2653 * arch-specific code peek and poke inside this process somehow (except when the
2654 * optimizations can take advantage of the full semantic info of coarse opcodes).
2655 * All the opcodes of the form opcode.s are 'normalized' to opcode.
2656 * MonoInst->opcode initially is the IL opcode or some simplification of that
2657 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
2658 * opcode with value bigger than OP_LAST.
2659 * At this point the IR can be handed over to an interpreter, a dumb code generator
2660 * or to the optimizing code generator that will translate it to SSA form.
2662 * Profiling directed optimizations.
2663 * We may compile by default with few or no optimizations and instrument the code
2664 * or the user may indicate what methods to optimize the most either in a config file
2665 * or through repeated runs where the compiler applies offline the optimizations to
2666 * each method and then decides if it was worth it.
2669 * * consider using an array instead of an hash table (bb_hash)
2672 #define CHECK_TYPE(ins) if (!(ins)->type) goto unverified
2673 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) goto unverified
2674 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) goto unverified
2675 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) goto unverified
2676 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) goto unverified
2677 #define CHECK_OPSIZE(size) if (ip + size > end) goto unverified
2680 /* offset from br.s -> br like opcodes */
2681 #define BIG_BRANCH_OFFSET 13
2684 get_basic_blocks (MonoCompile *cfg, GHashTable *bbhash, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
2686 unsigned char *ip = start;
2687 unsigned char *target;
2690 MonoBasicBlock *bblock;
2691 const MonoOpcode *opcode;
2694 cli_addr = ip - start;
2695 i = mono_opcode_value ((const guint8 **)&ip, end);
2698 opcode = &mono_opcodes [i];
2699 switch (opcode->argument) {
2700 case MonoInlineNone:
2703 case MonoInlineString:
2704 case MonoInlineType:
2705 case MonoInlineField:
2706 case MonoInlineMethod:
2709 case MonoShortInlineR:
2716 case MonoShortInlineVar:
2717 case MonoShortInlineI:
2720 case MonoShortInlineBrTarget:
2721 target = start + cli_addr + 2 + (signed char)ip [1];
2722 GET_BBLOCK (cfg, bbhash, bblock, target);
2725 case MonoInlineBrTarget:
2726 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
2727 GET_BBLOCK (cfg, bbhash, bblock, target);
2730 case MonoInlineSwitch: {
2731 guint32 n = read32 (ip + 1);
2734 cli_addr += 5 + 4 * n;
2735 target = start + cli_addr;
2736 GET_BBLOCK (cfg, bbhash, bblock, target);
2738 for (j = 0; j < n; ++j) {
2739 target = start + cli_addr + (gint32)read32 (ip);
2740 GET_BBLOCK (cfg, bbhash, bblock, target);
2750 g_assert_not_reached ();
2760 emit_tree (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *ins)
2762 MonoInst *store, *temp, *load;
2763 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
2764 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2765 store->cil_code = ins->cil_code;
2766 MONO_ADD_INS (bblock, store);
2767 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2768 load->cil_code = ins->cil_code;
2773 * mono_method_to_ir: translates IL into basic blocks containing trees
2776 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
2777 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
2778 guint inline_offset, gboolean is_virtual_call)
2780 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
2781 MonoInst *ins, **sp, **stack_start;
2782 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
2784 MonoMethod *cmethod;
2785 MonoInst **arg_array;
2786 MonoMethodHeader *header;
2788 guint32 token, ins_flag;
2790 MonoClass *constrained_call = NULL;
2791 unsigned char *ip, *end, *target, *err_pos;
2792 static double r8_0 = 0.0;
2793 MonoMethodSignature *sig;
2794 MonoGenericContext *generic_context = NULL;
2795 MonoGenericContainer *generic_container = NULL;
2796 MonoType **param_types;
2797 GList *bb_recheck = NULL, *tmp;
2798 int i, n, start_new_bblock, align;
2799 int num_calls = 0, inline_costs = 0;
2800 int *filter_lengths = NULL;
2801 int breakpoint_id = 0;
2802 guint real_offset, num_args;
2804 image = method->klass->image;
2805 header = mono_method_get_header (method);
2806 generic_container = ((MonoMethodNormal *)method)->generic_container;
2807 sig = method->signature;
2808 num_args = sig->hasthis + sig->param_count;
2809 ip = (unsigned char*)header->code;
2810 end = ip + header->code_size;
2811 mono_jit_stats.cil_code_size += header->code_size;
2813 if (method->signature->is_inflated)
2814 generic_context = ((MonoMethodInflated *) method)->context;
2815 else if (generic_container)
2816 generic_context = generic_container->context;
2818 g_assert (!method->signature->generic_param_count || (generic_context && generic_context->gmethod));
2820 if (cfg->method == method) {
2822 bbhash = cfg->bb_hash;
2824 real_offset = inline_offset;
2825 bbhash = g_hash_table_new (g_direct_hash, NULL);
2828 if (cfg->verbose_level > 2)
2829 g_print ("method to IR %s\n", mono_method_full_name (method, TRUE));
2831 dont_inline = g_list_prepend (dont_inline, method);
2832 if (cfg->method == method) {
2834 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
2835 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
2838 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
2839 start_bblock->cil_code = NULL;
2840 start_bblock->cil_length = 0;
2841 start_bblock->block_num = cfg->num_bblocks++;
2844 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
2845 end_bblock->cil_code = NULL;
2846 end_bblock->cil_length = 0;
2847 end_bblock->block_num = cfg->num_bblocks++;
2848 g_assert (cfg->num_bblocks == 2);
2850 arg_array = alloca (sizeof (MonoInst *) * num_args);
2851 for (i = num_args - 1; i >= 0; i--)
2852 arg_array [i] = cfg->varinfo [i];
2854 if (header->num_clauses) {
2855 int size = sizeof (int) * header->num_clauses;
2856 filter_lengths = alloca (size);
2857 memset (filter_lengths, 0, size);
2859 cfg->spvars = g_hash_table_new (NULL, NULL);
2861 /* handle exception clauses */
2862 for (i = 0; i < header->num_clauses; ++i) {
2863 //unsigned char *p = ip;
2864 MonoExceptionClause *clause = &header->clauses [i];
2865 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->try_offset);
2866 tblock->real_offset = clause->try_offset;
2867 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
2868 tblock->real_offset = clause->handler_offset;
2870 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
2871 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2872 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2873 MONO_ADD_INS (tblock, ins);
2876 /*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);
2878 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
2880 /* catch and filter blocks get the exception object on the stack */
2881 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
2882 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2883 /* mostly like handle_stack_args (), but just sets the input args */
2884 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
2886 cfg->exvar = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
2887 /* prevent it from being register allocated */
2888 cfg->exvar->flags |= MONO_INST_INDIRECT;
2890 tblock->in_scount = 1;
2891 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2892 tblock->in_stack [0] = cfg->exvar;
2894 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2895 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->data.filter_offset);
2896 tblock->real_offset = clause->data.filter_offset;
2897 tblock->in_scount = 1;
2898 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2899 tblock->in_stack [0] = cfg->exvar;
2900 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2901 MONO_ADD_INS (tblock, ins);
2907 arg_array = alloca (sizeof (MonoInst *) * num_args);
2908 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
2911 /* FIRST CODE BLOCK */
2912 bblock = NEW_BBLOCK (cfg);
2913 bblock->cil_code = ip;
2915 ADD_BBLOCK (cfg, bbhash, bblock);
2917 if (cfg->method == method) {
2918 breakpoint_id = mono_debugger_method_has_breakpoint (method);
2919 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
2920 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2921 MONO_ADD_INS (bblock, ins);
2925 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED))) || mono_compile_aot) {
2926 /* we use a separate basic block for the initialization code */
2927 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
2928 init_localsbb->real_offset = real_offset;
2929 start_bblock->next_bb = init_localsbb;
2930 init_localsbb->next_bb = bblock;
2931 link_bblock (cfg, start_bblock, init_localsbb);
2932 link_bblock (cfg, init_localsbb, bblock);
2933 init_localsbb->block_num = cfg->num_bblocks++;
2935 start_bblock->next_bb = bblock;
2936 link_bblock (cfg, start_bblock, bblock);
2939 if (get_basic_blocks (cfg, bbhash, header, real_offset, ip, end, &err_pos)) {
2944 mono_debug_init_method (cfg, bblock, breakpoint_id);
2946 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
2948 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
2949 for (n = 0; n < sig->param_count; ++n)
2950 param_types [n + sig->hasthis] = sig->params [n];
2952 /* do this somewhere outside - not here */
2953 NEW_ICONST (cfg, zero_int32, 0);
2954 NEW_ICONST (cfg, zero_int64, 0);
2955 zero_int64->type = STACK_I8;
2956 NEW_PCONST (cfg, zero_ptr, 0);
2957 NEW_PCONST (cfg, zero_obj, 0);
2958 zero_obj->type = STACK_OBJ;
2960 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
2961 zero_r8->type = STACK_R8;
2962 zero_r8->inst_p0 = &r8_0;
2964 /* add a check for this != NULL to inlined methods */
2965 if (is_virtual_call) {
2966 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
2967 NEW_ARGLOAD (cfg, ins->inst_left, 0);
2969 MONO_ADD_INS (bblock, ins);
2972 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
2973 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
2976 start_new_bblock = 0;
2979 if (cfg->method == method)
2980 real_offset = ip - header->code;
2982 real_offset = inline_offset;
2984 if (start_new_bblock) {
2985 bblock->cil_length = ip - bblock->cil_code;
2986 if (start_new_bblock == 2) {
2987 g_assert (ip == tblock->cil_code);
2989 GET_BBLOCK (cfg, bbhash, tblock, ip);
2991 bblock->next_bb = tblock;
2993 start_new_bblock = 0;
2994 for (i = 0; i < bblock->in_scount; ++i) {
2995 if (cfg->verbose_level > 3)
2996 g_print ("loading %d from temp %d\n", i, bblock->in_stack [i]->inst_c0);
2997 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
3001 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
3002 link_bblock (cfg, bblock, tblock);
3003 if (sp != stack_start) {
3004 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3007 bblock->next_bb = tblock;
3009 for (i = 0; i < bblock->in_scount; ++i) {
3010 if (cfg->verbose_level > 3)
3011 g_print ("loading %d from temp %d\n", i, bblock->in_stack [i]->inst_c0);
3012 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
3018 bblock->real_offset = real_offset;
3020 if ((cfg->method == method) && cfg->coverage_info) {
3021 MonoInst *store, *one;
3022 guint32 cil_offset = ip - header->code;
3023 cfg->coverage_info->data [cil_offset].cil_code = ip;
3025 /* TODO: Use an increment here */
3026 NEW_ICONST (cfg, one, 1);
3029 NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
3032 MONO_INST_NEW (cfg, store, CEE_STIND_I);
3033 store->cil_code = ip;
3034 store->inst_left = ins;
3035 store->inst_right = one;
3037 MONO_ADD_INS (bblock, store);
3040 if (cfg->verbose_level > 3)
3041 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, sp-stack_start, mono_disasm_code_one (NULL, method, ip, NULL));
3048 MONO_INST_NEW (cfg, ins, CEE_BREAK);
3049 ins->cil_code = ip++;
3050 MONO_ADD_INS (bblock, ins);
3056 CHECK_STACK_OVF (1);
3057 n = (*ip)-CEE_LDARG_0;
3059 NEW_ARGLOAD (cfg, ins, n);
3060 ins->cil_code = ip++;
3067 CHECK_STACK_OVF (1);
3068 n = (*ip)-CEE_LDLOC_0;
3070 NEW_LOCLOAD (cfg, ins, n);
3071 ins->cil_code = ip++;
3079 n = (*ip)-CEE_STLOC_0;
3082 handle_loaded_temps (cfg, bblock, stack_start, sp);
3083 NEW_LOCSTORE (cfg, ins, n, *sp);
3085 if (ins->opcode == CEE_STOBJ) {
3086 NEW_LOCLOADA (cfg, ins, n);
3087 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3089 MONO_ADD_INS (bblock, ins);
3095 CHECK_STACK_OVF (1);
3097 NEW_ARGLOAD (cfg, ins, ip [1]);
3104 CHECK_STACK_OVF (1);
3106 NEW_ARGLOADA (cfg, ins, ip [1]);
3116 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
3117 handle_loaded_temps (cfg, bblock, stack_start, sp);
3119 if (ins->opcode == CEE_STOBJ) {
3120 NEW_ARGLOADA (cfg, ins, ip [1]);
3121 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3123 MONO_ADD_INS (bblock, ins);
3128 CHECK_STACK_OVF (1);
3129 CHECK_LOCAL (ip [1]);
3130 NEW_LOCLOAD (cfg, ins, ip [1]);
3137 CHECK_STACK_OVF (1);
3138 CHECK_LOCAL (ip [1]);
3139 NEW_LOCLOADA (cfg, ins, ip [1]);
3148 handle_loaded_temps (cfg, bblock, stack_start, sp);
3149 CHECK_LOCAL (ip [1]);
3150 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
3152 if (ins->opcode == CEE_STOBJ) {
3153 NEW_LOCLOADA (cfg, ins, ip [1]);
3154 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3156 MONO_ADD_INS (bblock, ins);
3161 CHECK_STACK_OVF (1);
3162 NEW_PCONST (cfg, ins, NULL);
3164 ins->type = STACK_OBJ;
3169 CHECK_STACK_OVF (1);
3170 NEW_ICONST (cfg, ins, -1);
3184 CHECK_STACK_OVF (1);
3185 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
3192 CHECK_STACK_OVF (1);
3194 NEW_ICONST (cfg, ins, *((signed char*)ip));
3201 CHECK_STACK_OVF (1);
3202 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
3209 CHECK_STACK_OVF (1);
3210 MONO_INST_NEW (cfg, ins, OP_I8CONST);
3212 ins->type = STACK_I8;
3214 ins->inst_l = (gint64)read64 (ip);
3219 float *f = mono_mempool_alloc (cfg->domain->mp, sizeof (float));
3221 CHECK_STACK_OVF (1);
3222 MONO_INST_NEW (cfg, ins, OP_R4CONST);
3223 ins->type = STACK_R8;
3232 double *d = mono_mempool_alloc (cfg->domain->mp, sizeof (double));
3234 CHECK_STACK_OVF (1);
3235 MONO_INST_NEW (cfg, ins, OP_R8CONST);
3236 ins->type = STACK_R8;
3245 MonoInst *temp, *store;
3247 CHECK_STACK_OVF (1);
3252 * small optimization: if the loaded value was from a local already,
3253 * just load it twice.
3255 if (ins->ssa_op == MONO_SSA_LOAD &&
3256 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
3258 MONO_INST_NEW (cfg, temp, 0);
3260 temp->cil_code = ip;
3263 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3264 temp->cil_code = ip;
3265 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3266 store->cil_code = ip;
3267 MONO_ADD_INS (bblock, store);
3268 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3271 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3281 MONO_INST_NEW (cfg, ins, CEE_POP);
3282 MONO_ADD_INS (bblock, ins);
3283 ins->cil_code = ip++;
3289 if (stack_start != sp)
3291 MONO_INST_NEW (cfg, ins, CEE_JMP);
3292 token = read32 (ip + 1);
3293 /* FIXME: check the signature matches */
3294 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3295 ins->inst_p0 = cmethod;
3296 MONO_ADD_INS (bblock, ins);
3298 start_new_bblock = 1;
3302 case CEE_CALLVIRT: {
3303 MonoInst *addr = NULL;
3304 MonoMethodSignature *fsig = NULL;
3305 int temp, array_rank = 0;
3306 int virtual = *ip == CEE_CALLVIRT;
3309 token = read32 (ip + 1);
3311 if (*ip == CEE_CALLI) {
3316 if (method->wrapper_type != MONO_WRAPPER_NONE)
3317 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
3319 fsig = mono_metadata_parse_signature (image, token);
3321 n = fsig->param_count + fsig->hasthis;
3323 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3324 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
3325 } else if (constrained_call) {
3326 cmethod = mono_get_method_constrained (image, token, constrained_call, generic_context);
3328 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3333 if (!cmethod->klass->inited)
3334 mono_class_init (cmethod->klass);
3336 if (cmethod->signature->pinvoke) {
3337 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
3338 fsig = wrapper->signature;
3340 fsig = mono_method_get_signature (cmethod, image, token);
3343 n = fsig->param_count + fsig->hasthis;
3345 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
3346 cmethod->klass->parent == mono_defaults.array_class) {
3347 array_rank = cmethod->klass->rank;
3350 if (cmethod->string_ctor)
3351 g_assert_not_reached ();
3355 if (cmethod && cmethod->klass->generic_container)
3360 //g_assert (!virtual || fsig->hasthis);
3364 if (constrained_call) {
3366 * We have the `constrained.' prefix opcode.
3368 if (constrained_call->valuetype && !cmethod->klass->valuetype) {
3371 * The type parameter is instantiated as a valuetype,
3372 * but that type doesn't override the method we're
3373 * calling, so we need to box `this'.
3374 * sp [0] is a pointer to the data: we need the value
3375 * in handle_box (), so load it here.
3377 MONO_INST_NEW (cfg, load, mono_type_to_ldind (&constrained_call->byval_arg));
3378 type_to_eval_stack_type (&constrained_call->byval_arg, load);
3379 load->cil_code = ip;
3380 load->inst_left = sp [0];
3381 sp [0] = handle_box (cfg, bblock, load, ip, constrained_call);
3382 } else if (!constrained_call->valuetype) {
3386 * The type parameter is instantiated as a reference
3387 * type. We have a managed pointer on the stack, so
3388 * we need to dereference it here.
3391 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3393 ins->inst_i0 = sp [0];
3394 ins->type = STACK_OBJ;
3396 } else if (cmethod->klass->valuetype)
3398 constrained_call = NULL;
3401 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
3404 if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL) && (mono_metadata_signature_equal (method->signature, cmethod->signature))) {
3406 /* FIXME: This assumes the two methods has the same number and type of arguments */
3407 for (i = 0; i < n; ++i) {
3408 /* Check if argument is the same */
3409 NEW_ARGLOAD (cfg, ins, i);
3410 if ((ins->opcode == sp [i]->opcode) && (ins->inst_i0 == sp [i]->inst_i0))
3413 /* Prevent argument from being register allocated */
3414 arg_array [i]->flags |= MONO_INST_VOLATILE;
3415 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3417 if (ins->opcode == CEE_STOBJ) {
3418 NEW_ARGLOADA (cfg, ins, i);
3419 handle_stobj (cfg, bblock, ins, sp [i], sp [i]->cil_code, ins->klass, FALSE, FALSE);
3422 MONO_ADD_INS (bblock, ins);
3424 MONO_INST_NEW (cfg, ins, CEE_JMP);
3426 ins->inst_p0 = cmethod;
3427 ins->inst_p1 = arg_array [0];
3428 MONO_ADD_INS (bblock, ins);
3429 start_new_bblock = 1;
3430 /* skip CEE_RET as well */
3435 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_opcode_for_method (cfg, cmethod, fsig, sp))) {
3438 if (MONO_TYPE_IS_VOID (fsig->ret)) {
3439 MONO_ADD_INS (bblock, ins);
3441 type_to_eval_stack_type (fsig->ret, ins);
3450 handle_loaded_temps (cfg, bblock, stack_start, sp);
3452 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3453 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
3454 mono_method_check_inlining (cfg, cmethod) &&
3455 !g_list_find (dont_inline, cmethod)) {
3457 MonoBasicBlock *ebblock;
3458 gboolean allways = FALSE;
3460 if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
3461 (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
3462 cmethod = mono_marshal_get_native_wrapper (cmethod);
3466 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, allways))) {
3470 GET_BBLOCK (cfg, bbhash, bblock, ip);
3471 ebblock->next_bb = bblock;
3472 link_bblock (cfg, ebblock, bblock);
3474 if (!MONO_TYPE_IS_VOID (fsig->ret))
3477 /* indicates start of a new block, and triggers a load of all
3478 stack arguments at bb boundarie */
3481 inline_costs += costs;
3486 inline_costs += 10 * num_calls++;
3488 /* tail recursion elimination */
3489 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == method && ip [5] == CEE_RET) {
3490 gboolean has_vtargs = FALSE;
3493 /* keep it simple */
3494 for (i = fsig->param_count - 1; i >= 0; i--) {
3495 if (MONO_TYPE_ISSTRUCT (cmethod->signature->params [i]))
3500 for (i = 0; i < n; ++i) {
3501 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3503 MONO_ADD_INS (bblock, ins);
3505 MONO_INST_NEW (cfg, ins, CEE_BR);
3507 MONO_ADD_INS (bblock, ins);
3508 tblock = start_bblock->out_bb [0];
3509 link_bblock (cfg, bblock, tblock);
3510 ins->inst_target_bb = tblock;
3511 start_new_bblock = 1;
3514 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3515 /* just create a dummy - the value is never used */
3516 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
3517 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3525 if (*ip == CEE_CALLI) {
3527 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
3528 NEW_TEMPLOAD (cfg, *sp, temp);
3532 } else if (array_rank) {
3535 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
3536 if (sp [fsig->param_count]->type == STACK_OBJ) {
3537 MonoInst *iargs [2];
3538 MonoInst *array, *to_store, *store;
3540 handle_loaded_temps (cfg, bblock, stack_start, sp);
3542 array = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
3543 NEW_TEMPSTORE (cfg, store, array->inst_c0, sp [0]);
3544 store->cil_code = ip;
3545 MONO_ADD_INS (bblock, store);
3546 NEW_TEMPLOAD (cfg, iargs [0], array->inst_c0);
3548 to_store = mono_compile_create_var (cfg, type_from_stack_type (sp [fsig->param_count]), OP_LOCAL);
3549 NEW_TEMPSTORE (cfg, store, to_store->inst_c0, sp [fsig->param_count]);
3550 store->cil_code = ip;
3551 MONO_ADD_INS (bblock, store);
3552 NEW_TEMPLOAD (cfg, iargs [1], to_store->inst_c0);
3555 * We first save the args for the call so that the args are copied to the stack
3556 * and a new instruction tree for them is created. If we don't do this,
3557 * the same MonoInst is added to two different trees and this is not
3560 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref_check, iargs, ip);
3562 NEW_TEMPLOAD (cfg, sp [0], array->inst_c0);
3563 NEW_TEMPLOAD (cfg, sp [fsig->param_count], to_store->inst_c0);
3566 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
3567 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
3569 if (ins->opcode == CEE_STOBJ) {
3570 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
3572 MONO_ADD_INS (bblock, ins);
3575 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
3576 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3577 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
3581 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
3582 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3585 g_assert_not_reached ();
3589 if (0 && CODE_IS_STLOC (ip + 5) && (!MONO_TYPE_ISSTRUCT (fsig->ret)) && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)) {
3590 /* no need to spill */
3591 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
3594 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) {
3595 NEW_TEMPLOAD (cfg, *sp, temp);
3605 if (cfg->method != method) {
3606 /* return from inlined methode */
3611 //g_assert (returnvar != -1);
3612 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
3613 store->cil_code = sp [0]->cil_code;
3614 if (store->opcode == CEE_STOBJ) {
3615 g_assert_not_reached ();
3616 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
3617 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
3619 MONO_ADD_INS (bblock, store);
3623 g_assert (!return_var);
3626 MONO_INST_NEW (cfg, ins, CEE_NOP);
3627 ins->opcode = mono_type_to_stind (method->signature->ret);
3628 if (ins->opcode == CEE_STOBJ) {
3629 NEW_RETLOADA (cfg, ins);
3630 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3632 ins->opcode = OP_SETRET;
3634 ins->inst_i0 = *sp;;
3635 ins->inst_i1 = NULL;
3636 MONO_ADD_INS (bblock, ins);
3640 if (sp != stack_start)
3642 MONO_INST_NEW (cfg, ins, CEE_BR);
3643 ins->cil_code = ip++;
3644 ins->inst_target_bb = end_bblock;
3645 MONO_ADD_INS (bblock, ins);
3646 link_bblock (cfg, bblock, end_bblock);
3647 start_new_bblock = 1;
3651 MONO_INST_NEW (cfg, ins, CEE_BR);
3652 ins->cil_code = ip++;
3653 MONO_ADD_INS (bblock, ins);
3654 target = ip + 1 + (signed char)(*ip);
3656 GET_BBLOCK (cfg, bbhash, tblock, target);
3657 link_bblock (cfg, bblock, tblock);
3658 CHECK_BBLOCK (target, ip, tblock);
3659 ins->inst_target_bb = tblock;
3660 if (sp != stack_start) {
3661 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3664 start_new_bblock = 1;
3671 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3672 ins->cil_code = ip++;
3673 target = ip + 1 + *(signed char*)ip;
3675 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
3676 if (sp != stack_start) {
3677 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3694 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3695 ins->cil_code = ip++;
3696 target = ip + 1 + *(signed char*)ip;
3699 if (sp != stack_start) {
3700 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3707 MONO_INST_NEW (cfg, ins, CEE_BR);
3708 ins->cil_code = ip++;
3709 MONO_ADD_INS (bblock, ins);
3710 target = ip + 4 + (gint32)read32(ip);
3712 GET_BBLOCK (cfg, bbhash, tblock, target);
3713 link_bblock (cfg, bblock, tblock);
3714 CHECK_BBLOCK (target, ip, tblock);
3715 ins->inst_target_bb = tblock;
3716 if (sp != stack_start) {
3717 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3720 start_new_bblock = 1;
3727 MONO_INST_NEW (cfg, ins, *ip);
3728 ins->cil_code = ip++;
3729 target = ip + 4 + (gint32)read32(ip);
3731 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
3732 if (sp != stack_start) {
3733 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3750 MONO_INST_NEW (cfg, ins, *ip);
3751 ins->cil_code = ip++;
3752 target = ip + 4 + (gint32)read32(ip);
3755 if (sp != stack_start) {
3756 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3764 n = read32 (ip + 1);
3765 MONO_INST_NEW (cfg, ins, *ip);
3767 ins->inst_left = *sp;
3768 if (ins->inst_left->type != STACK_I4) goto unverified;
3771 CHECK_OPSIZE (n * sizeof (guint32));
3772 target = ip + n * sizeof (guint32);
3773 MONO_ADD_INS (bblock, ins);
3774 GET_BBLOCK (cfg, bbhash, tblock, target);
3775 link_bblock (cfg, bblock, tblock);
3776 ins->klass = GUINT_TO_POINTER (n);
3777 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
3778 ins->inst_many_bb [n] = tblock;
3780 for (i = 0; i < n; ++i) {
3781 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
3782 link_bblock (cfg, bblock, tblock);
3783 ins->inst_many_bb [i] = tblock;
3786 if (sp != stack_start) {
3787 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3804 MONO_INST_NEW (cfg, ins, *ip);
3809 ins->type = ldind_type [*ip - CEE_LDIND_I1];
3810 ins->flags |= ins_flag;
3822 MONO_INST_NEW (cfg, ins, *ip);
3823 ins->cil_code = ip++;
3825 handle_loaded_temps (cfg, bblock, stack_start, sp);
3826 MONO_ADD_INS (bblock, ins);
3827 ins->inst_i0 = sp [0];
3828 ins->inst_i1 = sp [1];
3829 ins->flags |= ins_flag;
3848 /* special case that gives a nice speedup and happens to workaorund a ppc jit but (for the release)
3849 * later apply the speedup to the left shift as well
3852 if ((ins->opcode == OP_LSHR_UN) && (ins->type == STACK_I8)
3853 && (ins->inst_right->opcode == OP_ICONST) && (ins->inst_right->inst_c0 == 32)) {
3854 ins->opcode = OP_LONG_SHRUN_32;
3855 /*g_print ("applied long shr speedup to %s\n", cfg->method->name);*/
3859 if (mono_find_jit_opcode_emulation (ins->opcode)) {
3861 *sp++ = emit_tree (cfg, bblock, ins);
3875 case CEE_CONV_OVF_I8:
3876 case CEE_CONV_OVF_U8:
3880 if (mono_find_jit_opcode_emulation (ins->opcode)) {
3882 *sp++ = emit_tree (cfg, bblock, ins);
3886 case CEE_CONV_OVF_I4:
3887 case CEE_CONV_OVF_I1:
3888 case CEE_CONV_OVF_I2:
3889 case CEE_CONV_OVF_I:
3890 case CEE_CONV_OVF_U:
3893 if (sp [-1]->type == STACK_R8) {
3894 ADD_UNOP (CEE_CONV_OVF_I8);
3902 case CEE_CONV_OVF_U1:
3903 case CEE_CONV_OVF_U2:
3904 case CEE_CONV_OVF_U4:
3907 if (sp [-1]->type == STACK_R8) {
3908 ADD_UNOP (CEE_CONV_OVF_U8);
3916 case CEE_CONV_OVF_I1_UN:
3917 case CEE_CONV_OVF_I2_UN:
3918 case CEE_CONV_OVF_I4_UN:
3919 case CEE_CONV_OVF_I8_UN:
3920 case CEE_CONV_OVF_U1_UN:
3921 case CEE_CONV_OVF_U2_UN:
3922 case CEE_CONV_OVF_U4_UN:
3923 case CEE_CONV_OVF_U8_UN:
3924 case CEE_CONV_OVF_I_UN:
3925 case CEE_CONV_OVF_U_UN:
3933 token = read32 (ip + 1);
3934 if (method->wrapper_type != MONO_WRAPPER_NONE)
3935 klass = mono_method_get_wrapper_data (method, token);
3937 klass = mono_class_get_full (image, token, generic_context);
3939 mono_class_init (klass);
3941 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3942 MonoInst *store, *load;
3943 MONO_INST_NEW (cfg, load, CEE_LDIND_REF);
3944 load->cil_code = ip;
3945 load->inst_i0 = sp [1];
3946 load->type = STACK_OBJ;
3947 load->flags |= ins_flag;
3948 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
3949 store->cil_code = ip;
3950 handle_loaded_temps (cfg, bblock, stack_start, sp);
3951 MONO_ADD_INS (bblock, store);
3952 store->inst_i0 = sp [0];
3953 store->inst_i1 = load;
3954 store->flags |= ins_flag;
3956 n = mono_class_value_size (klass, NULL);
3957 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3959 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3960 copy->inst_left = sp [0];
3961 copy->inst_right = sp [1];
3962 copy->cil_code = ip;
3964 MONO_ADD_INS (bblock, copy);
3966 MonoInst *iargs [3];
3969 NEW_ICONST (cfg, iargs [2], n);
3970 iargs [2]->cil_code = ip;
3972 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3979 MonoInst *iargs [3];
3983 token = read32 (ip + 1);
3984 if (method->wrapper_type != MONO_WRAPPER_NONE)
3985 klass = mono_method_get_wrapper_data (method, token);
3987 klass = mono_class_get_full (image, token, generic_context);
3989 mono_class_init (klass);
3990 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3991 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3993 ins->inst_i0 = sp [0];
3994 ins->type = STACK_OBJ;
3995 ins->flags |= ins_flag;
4001 n = mono_class_value_size (klass, NULL);
4002 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
4003 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
4004 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
4006 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
4007 copy->inst_left = iargs [0];
4008 copy->inst_right = *sp;
4009 copy->cil_code = ip;
4011 MONO_ADD_INS (bblock, copy);
4014 NEW_ICONST (cfg, iargs [2], n);
4015 iargs [2]->cil_code = ip;
4017 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
4019 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
4027 CHECK_STACK_OVF (1);
4029 n = read32 (ip + 1);
4031 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
4032 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, n));
4034 ins->type = STACK_OBJ;
4037 else if (method->wrapper_type != MONO_WRAPPER_NONE) {
4039 MonoInst *iargs [1];
4041 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
4042 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
4043 NEW_TEMPLOAD (cfg, *sp, temp);
4047 if (cfg->opt & MONO_OPT_SHARED) {
4049 MonoInst *iargs [3];
4051 if (mono_compile_aot) {
4052 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n);
4055 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
4056 NEW_IMAGECONST (cfg, iargs [1], image);
4057 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
4058 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
4059 NEW_TEMPLOAD (cfg, *sp, temp);
4060 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
4062 if (mono_compile_aot)
4063 NEW_LDSTRCONST (cfg, ins, image, n);
4065 NEW_PCONST (cfg, ins, NULL);
4067 ins->type = STACK_OBJ;
4068 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
4078 MonoInst *iargs [2];
4079 MonoMethodSignature *fsig;
4083 token = read32 (ip + 1);
4084 if (method->wrapper_type != MONO_WRAPPER_NONE) {
4085 cmethod = mono_method_get_wrapper_data (method, token);
4087 cmethod = mono_get_method_full (image, token, NULL, generic_context);
4088 fsig = mono_method_get_signature (cmethod, image, token);
4090 mono_class_init (cmethod->klass);
4092 n = fsig->param_count;
4095 /* move the args to allow room for 'this' in the first position */
4101 handle_loaded_temps (cfg, bblock, stack_start, sp);
4104 if (cmethod->klass->parent == mono_defaults.array_class) {
4105 NEW_METHODCONST (cfg, *sp, cmethod);
4106 temp = handle_array_new (cfg, bblock, fsig->param_count, sp, ip);
4107 } else if (cmethod->string_ctor) {
4108 /* we simply pass a null pointer */
4109 NEW_PCONST (cfg, *sp, NULL);
4110 /* now call the string ctor */
4111 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
4113 if (cmethod->klass->valuetype) {
4114 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
4115 temp = iargs [0]->inst_c0;
4116 NEW_TEMPLOADA (cfg, *sp, temp);
4118 temp = handle_alloc (cfg, bblock, cmethod->klass, ip);
4119 NEW_TEMPLOAD (cfg, *sp, temp);
4122 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
4123 mono_method_check_inlining (cfg, cmethod) &&
4124 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
4125 !g_list_find (dont_inline, cmethod)) {
4127 MonoBasicBlock *ebblock;
4128 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, FALSE))) {
4133 GET_BBLOCK (cfg, bbhash, bblock, ip);
4134 ebblock->next_bb = bblock;
4135 link_bblock (cfg, ebblock, bblock);
4137 NEW_TEMPLOAD (cfg, *sp, temp);
4140 /* indicates start of a new block, and triggers a load
4141 of all stack arguments at bb boundarie */
4144 inline_costs += costs;
4148 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
4151 /* now call the actual ctor */
4152 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
4156 NEW_TEMPLOAD (cfg, *sp, temp);
4167 token = read32 (ip + 1);
4168 if (method->wrapper_type != MONO_WRAPPER_NONE)
4169 klass = mono_method_get_wrapper_data (method, token);
4171 klass = mono_class_get_full (image, token, generic_context);
4172 mono_class_init (klass);
4174 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4176 MonoMethod *mono_isinst;
4177 MonoInst *iargs [1];
4178 MonoBasicBlock *ebblock;
4182 mono_isinst = mono_marshal_get_isinst (klass);
4185 costs = inline_method (cfg, mono_isinst, mono_isinst->signature, bblock,
4186 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4188 g_assert (costs > 0);
4193 GET_BBLOCK (cfg, bbhash, bblock, ip);
4194 ebblock->next_bb = bblock;
4195 link_bblock (cfg, ebblock, bblock);
4197 temp = iargs [0]->inst_i0->inst_c0;
4198 NEW_TEMPLOAD (cfg, *sp, temp);
4202 inline_costs += costs;
4206 MONO_INST_NEW (cfg, ins, *ip);
4207 ins->type = STACK_OBJ;
4208 ins->inst_left = *sp;
4209 ins->inst_newa_class = klass;
4211 *sp++ = emit_tree (cfg, bblock, ins);
4215 case CEE_UNBOX_ANY: {
4216 MonoInst *add, *vtoffset;
4217 MonoInst *iargs [3];
4222 token = read32 (ip + 1);
4223 if (method->wrapper_type != MONO_WRAPPER_NONE)
4224 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4226 klass = mono_class_get_full (image, token, generic_context);
4227 mono_class_init (klass);
4229 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4231 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4232 MonoMethod *mono_castclass;
4233 MonoInst *iargs [1];
4234 MonoBasicBlock *ebblock;
4238 mono_castclass = mono_marshal_get_castclass (klass);
4241 costs = inline_method (cfg, mono_castclass, mono_castclass->signature, bblock,
4242 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4244 g_assert (costs > 0);
4249 GET_BBLOCK (cfg, bbhash, bblock, ip);
4250 ebblock->next_bb = bblock;
4251 link_bblock (cfg, ebblock, bblock);
4253 temp = iargs [0]->inst_i0->inst_c0;
4254 NEW_TEMPLOAD (cfg, *sp, temp);
4258 inline_costs += costs;
4261 MONO_INST_NEW (cfg, ins, CEE_CASTCLASS);
4262 ins->type = STACK_OBJ;
4263 ins->inst_left = *sp;
4265 ins->inst_newa_class = klass;
4273 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
4274 ins->type = STACK_OBJ;
4275 ins->inst_left = *sp;
4277 ins->inst_newa_class = klass;
4280 MONO_INST_NEW (cfg, add, OP_PADD);
4281 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4282 add->inst_left = ins;
4283 add->inst_right = vtoffset;
4284 add->type = STACK_MP;
4288 n = mono_class_value_size (klass, NULL);
4289 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
4290 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
4291 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
4293 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
4294 copy->inst_left = iargs [0];
4295 copy->inst_right = *sp;
4296 copy->cil_code = ip;
4298 MONO_ADD_INS (bblock, copy);
4301 NEW_ICONST (cfg, iargs [2], n);
4302 iargs [2]->cil_code = ip;
4304 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
4306 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
4312 MonoInst *add, *vtoffset;
4317 token = read32 (ip + 1);
4318 if (method->wrapper_type != MONO_WRAPPER_NONE)
4319 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4321 klass = mono_class_get_full (image, token, generic_context);
4322 mono_class_init (klass);
4324 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
4325 ins->type = STACK_OBJ;
4326 ins->inst_left = *sp;
4328 ins->inst_newa_class = klass;
4331 MONO_INST_NEW (cfg, add, OP_PADD);
4332 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4333 add->inst_left = ins;
4334 add->inst_right = vtoffset;
4335 add->type = STACK_MP;
4345 token = read32 (ip + 1);
4346 if (method->wrapper_type != MONO_WRAPPER_NONE)
4347 klass = mono_method_get_wrapper_data (method, token);
4349 klass = mono_class_get_full (image, token, generic_context);
4350 mono_class_init (klass);
4352 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4354 MonoMethod *mono_castclass;
4355 MonoInst *iargs [1];
4356 MonoBasicBlock *ebblock;
4360 mono_castclass = mono_marshal_get_castclass (klass);
4363 costs = inline_method (cfg, mono_castclass, mono_castclass->signature, bblock,
4364 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4366 g_assert (costs > 0);
4371 GET_BBLOCK (cfg, bbhash, bblock, ip);
4372 ebblock->next_bb = bblock;
4373 link_bblock (cfg, ebblock, bblock);
4375 temp = iargs [0]->inst_i0->inst_c0;
4376 NEW_TEMPLOAD (cfg, *sp, temp);
4380 inline_costs += costs;
4383 MONO_INST_NEW (cfg, ins, *ip);
4384 ins->type = STACK_OBJ;
4385 ins->inst_left = *sp;
4387 ins->inst_newa_class = klass;
4389 *sp++ = emit_tree (cfg, bblock, ins);
4395 MONO_INST_NEW (cfg, ins, *ip);
4397 ins->inst_left = *sp;
4398 ins->cil_code = ip++;
4399 MONO_ADD_INS (bblock, ins);
4401 start_new_bblock = 1;
4406 MonoInst *offset_ins;
4407 MonoClassField *field;
4408 MonoBasicBlock *ebblock;
4412 if (*ip == CEE_STFLD) {
4419 // FIXME: enable this test later.
4420 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
4423 token = read32 (ip + 1);
4424 field = mono_field_from_token (image, token, &klass, generic_context);
4425 mono_class_init (klass);
4427 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
4428 /* FIXME: mark instructions for use in SSA */
4429 if (*ip == CEE_STFLD) {
4430 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound || klass == mono_defaults.marshalbyrefobject_class) {
4431 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
4432 MonoInst *iargs [5];
4435 NEW_CLASSCONST (cfg, iargs [1], klass);
4436 NEW_FIELDCONST (cfg, iargs [2], field);
4437 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
4441 if (cfg->opt & MONO_OPT_INLINE) {
4442 costs = inline_method (cfg, stfld_wrapper, stfld_wrapper->signature, bblock,
4443 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4444 g_assert (costs > 0);
4449 GET_BBLOCK (cfg, bbhash, bblock, ip);
4450 ebblock->next_bb = bblock;
4451 link_bblock (cfg, ebblock, bblock);
4453 /* indicates start of a new block, and triggers a load
4454 of all stack arguments at bb boundarie */
4457 inline_costs += costs;
4460 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, stfld_wrapper->signature, iargs, ip, NULL);
4464 NEW_ICONST (cfg, offset_ins, foffset);
4465 MONO_INST_NEW (cfg, ins, OP_PADD);
4467 ins->inst_left = *sp;
4468 ins->inst_right = offset_ins;
4469 ins->type = STACK_MP;
4471 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4472 store->cil_code = ip;
4473 store->inst_left = ins;
4474 store->inst_right = sp [1];
4475 handle_loaded_temps (cfg, bblock, stack_start, sp);
4476 store->flags |= ins_flag;
4478 if (store->opcode == CEE_STOBJ) {
4479 handle_stobj (cfg, bblock, ins, sp [1], ip,
4480 mono_class_from_mono_type (field->type), FALSE, FALSE);
4482 MONO_ADD_INS (bblock, store);
4485 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound || klass == mono_defaults.marshalbyrefobject_class) {
4486 /* fixme: we need to inline that call somehow */
4487 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
4488 MonoInst *iargs [4];
4492 NEW_CLASSCONST (cfg, iargs [1], klass);
4493 NEW_FIELDCONST (cfg, iargs [2], field);
4494 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
4495 if (cfg->opt & MONO_OPT_INLINE) {
4496 costs = inline_method (cfg, ldfld_wrapper, ldfld_wrapper->signature, bblock,
4497 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4498 g_assert (costs > 0);
4503 GET_BBLOCK (cfg, bbhash, bblock, ip);
4504 ebblock->next_bb = bblock;
4505 link_bblock (cfg, ebblock, bblock);
4507 temp = iargs [0]->inst_i0->inst_c0;
4509 if (*ip == CEE_LDFLDA) {
4510 /* not sure howto handle this */
4511 NEW_TEMPLOADA (cfg, *sp, temp);
4513 NEW_TEMPLOAD (cfg, *sp, temp);
4517 /* indicates start of a new block, and triggers a load of
4518 all stack arguments at bb boundarie */
4521 inline_costs += costs;
4524 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, ldfld_wrapper->signature, iargs, ip, NULL);
4525 if (*ip == CEE_LDFLDA) {
4526 /* not sure howto handle this */
4527 NEW_TEMPLOADA (cfg, *sp, temp);
4529 NEW_TEMPLOAD (cfg, *sp, temp);
4534 NEW_ICONST (cfg, offset_ins, foffset);
4535 MONO_INST_NEW (cfg, ins, OP_PADD);
4537 ins->inst_left = *sp;
4538 ins->inst_right = offset_ins;
4539 ins->type = STACK_MP;
4541 if (*ip == CEE_LDFLDA) {
4545 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4546 type_to_eval_stack_type (field->type, load);
4547 load->cil_code = ip;
4548 load->inst_left = ins;
4549 load->flags |= ins_flag;
4561 MonoClassField *field;
4562 gpointer addr = NULL;
4565 token = read32 (ip + 1);
4567 field = mono_field_from_token (image, token, &klass, generic_context);
4568 mono_class_init (klass);
4570 if ((*ip) == CEE_STSFLD)
4571 handle_loaded_temps (cfg, bblock, stack_start, sp);
4573 if (cfg->domain->special_static_fields)
4574 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
4576 if ((cfg->opt & MONO_OPT_SHARED) || (mono_compile_aot && addr)) {
4578 MonoInst *iargs [2];
4579 g_assert (field->parent);
4580 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
4581 NEW_FIELDCONST (cfg, iargs [1], field);
4582 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
4583 NEW_TEMPLOAD (cfg, ins, temp);
4586 vtable = mono_class_vtable (cfg->domain, klass);
4588 if ((!vtable->initialized || mono_compile_aot) && !(klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (klass, method)) {
4589 guint8 *tramp = mono_create_class_init_trampoline (vtable);
4590 mono_emit_native_call (cfg, bblock, tramp,
4591 helper_sig_class_init_trampoline,
4592 NULL, ip, FALSE, FALSE);
4593 if (cfg->verbose_level > 2)
4594 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
4596 if (cfg->run_cctors)
4597 mono_runtime_class_init (vtable);
4599 addr = (char*)vtable->data + field->offset;
4601 if (mono_compile_aot)
4602 NEW_SFLDACONST (cfg, ins, field);
4604 NEW_PCONST (cfg, ins, addr);
4608 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
4609 * This could be later optimized to do just a couple of
4610 * memory dereferences with constant offsets.
4613 MonoInst *iargs [1];
4614 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
4615 temp = mono_emit_jit_icall (cfg, bblock, mono_get_special_static_data, iargs, ip);
4616 NEW_TEMPLOAD (cfg, ins, temp);
4620 /* FIXME: mark instructions for use in SSA */
4621 if (*ip == CEE_LDSFLDA) {
4623 } else if (*ip == CEE_STSFLD) {
4627 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4628 store->cil_code = ip;
4629 store->inst_left = ins;
4630 store->inst_right = sp [0];
4631 store->flags |= ins_flag;
4634 if (store->opcode == CEE_STOBJ) {
4635 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
4637 MONO_ADD_INS (bblock, store);
4639 gboolean is_const = FALSE;
4640 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4641 if (!((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) &&
4642 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
4643 gpointer addr = (char*)vtable->data + field->offset;
4644 /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
4646 switch (field->type->type) {
4647 case MONO_TYPE_BOOLEAN:
4649 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
4653 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
4656 case MONO_TYPE_CHAR:
4658 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
4662 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
4667 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
4671 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
4676 case MONO_TYPE_STRING:
4677 case MONO_TYPE_OBJECT:
4678 case MONO_TYPE_CLASS:
4679 case MONO_TYPE_SZARRAY:
4681 case MONO_TYPE_FNPTR:
4682 case MONO_TYPE_ARRAY:
4683 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
4684 type_to_eval_stack_type (field->type, *sp);
4689 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
4690 sp [0]->type = STACK_I8;
4691 sp [0]->inst_l = *((gint64 *)addr);
4696 case MONO_TYPE_VALUETYPE:
4705 CHECK_STACK_OVF (1);
4706 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4707 type_to_eval_stack_type (field->type, load);
4708 load->cil_code = ip;
4709 load->inst_left = ins;
4711 load->flags |= ins_flag;
4713 /* fixme: dont see the problem why this does not work */
4714 //cfg->disable_aot = TRUE;
4724 token = read32 (ip + 1);
4725 if (method->wrapper_type != MONO_WRAPPER_NONE)
4726 klass = mono_method_get_wrapper_data (method, token);
4728 klass = mono_class_get_full (image, token, generic_context);
4729 mono_class_init (klass);
4730 n = mono_type_to_stind (&klass->byval_arg);
4731 if (n == CEE_STOBJ) {
4732 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
4734 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
4736 MONO_INST_NEW (cfg, store, n);
4737 store->cil_code = ip;
4738 store->inst_left = sp [0];
4739 store->inst_right = sp [1];
4740 store->flags |= ins_flag;
4741 MONO_ADD_INS (bblock, store);
4753 token = read32 (ip + 1);
4754 if (method->wrapper_type != MONO_WRAPPER_NONE)
4755 klass = mono_method_get_wrapper_data (method, token);
4757 klass = mono_class_get_full (image, token, generic_context);
4758 mono_class_init (klass);
4760 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4765 *sp++ = handle_box (cfg, bblock, val, ip, klass);
4772 MONO_INST_NEW (cfg, ins, *ip);
4777 token = read32 (ip + 1);
4779 /* allocate the domainvar - becaus this is used in decompose_foreach */
4780 if (cfg->opt & MONO_OPT_SHARED) {
4781 mono_get_domainvar (cfg);
4782 /* LAME-IR: Mark it as used since otherwise it will be optimized away */
4783 cfg->domainvar->flags |= MONO_INST_VOLATILE;
4786 if (method->wrapper_type != MONO_WRAPPER_NONE)
4787 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4789 klass = mono_class_get_full (image, token, generic_context);
4791 mono_class_init (klass);
4792 ins->inst_newa_class = klass;
4793 ins->inst_newa_len = *sp;
4794 ins->type = STACK_OBJ;
4798 * we store the object so calls to create the array are not interleaved
4799 * with the arguments of other calls.
4802 MonoInst *store, *temp, *load;
4804 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
4805 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4806 store->cil_code = ins->cil_code;
4807 MONO_ADD_INS (bblock, store);
4808 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
4809 load->cil_code = ins->cil_code;
4816 MONO_INST_NEW (cfg, ins, *ip);
4817 ins->cil_code = ip++;
4819 ins->inst_left = *sp;
4820 ins->type = STACK_PTR;
4828 if (method->wrapper_type != MONO_WRAPPER_NONE)
4829 klass = (MonoClass*)mono_method_get_wrapper_data (method, read32 (ip + 1));
4831 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4832 mono_class_init (klass);
4833 NEW_LDELEMA (cfg, ins, sp, klass);
4838 case CEE_LDELEM_ANY: {
4843 token = read32 (ip + 1);
4844 klass = mono_class_get_full (image, token, generic_context);
4845 mono_class_init (klass);
4846 NEW_LDELEMA (cfg, load, sp, klass);
4847 load->cil_code = ip;
4848 MONO_INST_NEW (cfg, ins, mono_type_to_ldind (&klass->byval_arg));
4850 ins->inst_left = load;
4852 type_to_eval_stack_type (&klass->byval_arg, ins);
4866 case CEE_LDELEM_REF: {
4870 * ldind.x (ldelema (array, index))
4871 * ldelema does the bounds check
4875 klass = array_access_to_klass (*ip);
4876 NEW_LDELEMA (cfg, load, sp, klass);
4877 load->cil_code = ip;
4878 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
4880 ins->inst_left = load;
4882 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
4892 case CEE_STELEM_R8: {
4896 * stind.x (ldelema (array, index), val)
4897 * ldelema does the bounds check
4901 klass = array_access_to_klass (*ip);
4902 NEW_LDELEMA (cfg, load, sp, klass);
4903 load->cil_code = ip;
4904 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
4906 ins->inst_left = load;
4907 ins->inst_right = sp [2];
4909 handle_loaded_temps (cfg, bblock, stack_start, sp);
4910 MONO_ADD_INS (bblock, ins);
4914 case CEE_STELEM_ANY: {
4918 * stind.x (ldelema (array, index), val)
4919 * ldelema does the bounds check
4924 token = read32 (ip + 1);
4925 klass = mono_class_get_full (image, token, generic_context);
4926 mono_class_init (klass);
4927 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4928 MonoMethod* helper = mono_marshal_get_stelemref ();
4929 MonoInst *iargs [3];
4930 handle_loaded_temps (cfg, bblock, stack_start, sp);
4936 mono_emit_method_call_spilled (cfg, bblock, helper, helper->signature, iargs, ip, NULL);
4938 NEW_LDELEMA (cfg, load, sp, klass);
4939 load->cil_code = ip;
4941 n = mono_type_to_stind (&klass->byval_arg);
4943 handle_stobj (cfg, bblock, load, sp [2], ip, klass, FALSE, FALSE);
4945 MONO_INST_NEW (cfg, ins, n);
4947 ins->inst_left = load;
4948 ins->inst_right = sp [2];
4949 handle_loaded_temps (cfg, bblock, stack_start, sp);
4950 MONO_ADD_INS (bblock, ins);
4957 case CEE_STELEM_REF: {
4958 MonoInst *iargs [3];
4959 MonoMethod* helper = mono_marshal_get_stelemref ();
4964 handle_loaded_temps (cfg, bblock, stack_start, sp);
4970 mono_emit_method_call_spilled (cfg, bblock, helper, helper->signature, iargs, ip, NULL);
4974 NEW_GROUP (cfg, group, sp [0], sp [1]);
4975 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
4977 ins->inst_left = group;
4978 ins->inst_right = sp [2];
4979 MONO_ADD_INS (bblock, ins);
4986 case CEE_CKFINITE: {
4987 MonoInst *store, *temp;
4990 /* this instr. can throw exceptions as side effect,
4991 * so we cant eliminate dead code which contains CKFINITE opdodes.
4992 * Spilling to memory makes sure that we always perform
4996 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
4998 ins->inst_left = sp [-1];
4999 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
5001 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
5002 store->cil_code = ip;
5003 MONO_ADD_INS (bblock, store);
5005 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
5012 MONO_INST_NEW (cfg, ins, *ip);
5015 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
5016 mono_class_init (klass);
5017 ins->type = STACK_MP;
5018 ins->inst_left = *sp;
5020 ins->inst_newa_class = klass;
5025 case CEE_MKREFANY: {
5026 MonoInst *loc, *klassconst;
5029 MONO_INST_NEW (cfg, ins, *ip);
5032 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
5033 mono_class_init (klass);
5036 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
5037 NEW_TEMPLOADA (cfg, ins->inst_right, loc->inst_c0);
5039 NEW_PCONST (cfg, klassconst, klass);
5040 NEW_GROUP (cfg, ins->inst_left, *sp, klassconst);
5042 MONO_ADD_INS (bblock, ins);
5044 NEW_TEMPLOAD (cfg, *sp, loc->inst_c0);
5051 MonoClass *handle_class;
5053 CHECK_STACK_OVF (1);
5056 n = read32 (ip + 1);
5058 handle = mono_ldtoken (image, n, &handle_class, generic_context);
5059 mono_class_init (handle_class);
5061 if (cfg->opt & MONO_OPT_SHARED) {
5063 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
5065 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
5067 NEW_IMAGECONST (cfg, iargs [0], image);
5068 NEW_ICONST (cfg, iargs [1], n);
5069 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
5070 NEW_TEMPLOAD (cfg, res, temp);
5071 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5072 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
5073 MONO_ADD_INS (bblock, store);
5074 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5076 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method_full (image, read32 (ip + 6), NULL, generic_context)) &&
5077 (cmethod->klass == mono_defaults.monotype_class->parent) &&
5078 (strcmp (cmethod->name, "GetTypeFromHandle") == 0) &&
5079 ((g_hash_table_lookup (bbhash, ip + 5) == NULL) ||
5080 (g_hash_table_lookup (bbhash, ip + 5) == bblock))) {
5081 MonoClass *tclass = mono_class_from_mono_type (handle);
5082 mono_class_init (tclass);
5083 if (mono_compile_aot)
5084 NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n);
5086 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
5087 ins->type = STACK_OBJ;
5088 ins->klass = cmethod->klass;
5091 MonoInst *store, *addr, *vtvar;
5093 if (mono_compile_aot)
5094 NEW_LDTOKENCONST (cfg, ins, image, n);
5096 NEW_PCONST (cfg, ins, handle);
5097 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
5098 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5099 NEW_INDSTORE (cfg, store, addr, ins, &mono_defaults.int_class->byval_arg);
5100 MONO_ADD_INS (bblock, store);
5101 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5117 case CEE_ADD_OVF_UN:
5119 case CEE_MUL_OVF_UN:
5121 case CEE_SUB_OVF_UN:
5124 if (mono_find_jit_opcode_emulation (ins->opcode)) {
5126 *sp++ = emit_tree (cfg, bblock, ins);
5130 case CEE_ENDFINALLY:
5131 MONO_INST_NEW (cfg, ins, *ip);
5132 MONO_ADD_INS (bblock, ins);
5133 ins->cil_code = ip++;
5134 start_new_bblock = 1;
5137 * Control will leave the method so empty the stack, otherwise
5138 * the next basic block will start with a nonempty stack.
5140 while (sp != stack_start) {
5141 MONO_INST_NEW (cfg, ins, CEE_POP);
5145 MONO_ADD_INS (bblock, ins);
5151 if (*ip == CEE_LEAVE) {
5153 target = ip + 5 + (gint32)read32(ip + 1);
5156 target = ip + 2 + (signed char)(ip [1]);
5159 /* empty the stack */
5160 while (sp != stack_start) {
5161 MONO_INST_NEW (cfg, ins, CEE_POP);
5165 MONO_ADD_INS (bblock, ins);
5169 * If this leave statement is in a catch block, check for a
5170 * pending exception, and rethrow it if necessary.
5172 for (i = 0; i < header->num_clauses; ++i) {
5173 MonoExceptionClause *clause = &header->clauses [i];
5174 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code)) {
5177 temp = mono_emit_jit_icall (cfg, bblock, mono_thread_get_pending_exception, NULL, ip);
5178 NEW_TEMPLOAD (cfg, *sp, temp);
5180 MONO_INST_NEW (cfg, ins, OP_THROW_OR_NULL);
5181 ins->inst_left = *sp;
5183 MONO_ADD_INS (bblock, ins);
5187 /* fixme: call fault handler ? */
5189 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
5191 for (tmp = handlers; tmp; tmp = tmp->next) {
5193 link_bblock (cfg, bblock, tblock);
5194 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
5196 ins->inst_target_bb = tblock;
5197 MONO_ADD_INS (bblock, ins);
5199 g_list_free (handlers);
5202 MONO_INST_NEW (cfg, ins, CEE_BR);
5204 MONO_ADD_INS (bblock, ins);
5205 GET_BBLOCK (cfg, bbhash, tblock, target);
5206 link_bblock (cfg, bblock, tblock);
5207 CHECK_BBLOCK (target, ip, tblock);
5208 ins->inst_target_bb = tblock;
5209 start_new_bblock = 1;
5211 if (*ip == CEE_LEAVE)
5220 MONO_INST_NEW (cfg, ins, *ip);
5222 handle_loaded_temps (cfg, bblock, stack_start, sp);
5223 MONO_ADD_INS (bblock, ins);
5224 ins->cil_code = ip++;
5225 ins->inst_i0 = sp [0];
5226 ins->inst_i1 = sp [1];
5234 /* trampoline mono specific opcodes */
5235 case MONO_CUSTOM_PREFIX: {
5237 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
5242 case CEE_MONO_ICALL: {
5245 MonoJitICallInfo *info;
5247 token = read32 (ip + 2);
5248 func = mono_method_get_wrapper_data (method, token);
5249 info = mono_find_jit_icall_by_addr (func);
5252 CHECK_STACK (info->sig->param_count);
5253 sp -= info->sig->param_count;
5255 temp = mono_emit_jit_icall (cfg, bblock, info->func, sp, ip);
5256 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
5257 NEW_TEMPLOAD (cfg, *sp, temp);
5262 inline_costs += 10 * num_calls++;
5266 case CEE_MONO_LDPTR:
5267 CHECK_STACK_OVF (1);
5269 token = read32 (ip + 2);
5270 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
5274 inline_costs += 10 * num_calls++;
5276 case CEE_MONO_VTADDR:
5279 MONO_INST_NEW (cfg, ins, OP_VTADDR);
5281 ins->type = STACK_MP;
5282 ins->inst_left = *sp;
5286 case CEE_MONO_NEWOBJ: {
5287 MonoInst *iargs [2];
5289 CHECK_STACK_OVF (1);
5291 token = read32 (ip + 2);
5292 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5293 mono_class_init (klass);
5294 NEW_DOMAINCONST (cfg, iargs [0]);
5295 NEW_CLASSCONST (cfg, iargs [1], klass);
5296 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
5297 NEW_TEMPLOAD (cfg, *sp, temp);
5300 inline_costs += 10 * num_calls++;
5303 case CEE_MONO_OBJADDR:
5306 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
5308 ins->type = STACK_MP;
5309 ins->inst_left = *sp;
5313 case CEE_MONO_LDNATIVEOBJ:
5316 token = read32 (ip + 2);
5317 klass = mono_method_get_wrapper_data (method, token);
5318 g_assert (klass->valuetype);
5319 mono_class_init (klass);
5320 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
5324 case CEE_MONO_RETOBJ:
5325 g_assert (cfg->ret);
5326 g_assert (method->signature->pinvoke);
5331 token = read32 (ip + 2);
5332 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5334 NEW_RETLOADA (cfg, ins);
5335 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
5337 if (sp != stack_start)
5340 MONO_INST_NEW (cfg, ins, CEE_BR);
5342 ins->inst_target_bb = end_bblock;
5343 MONO_ADD_INS (bblock, ins);
5344 link_bblock (cfg, bblock, end_bblock);
5345 start_new_bblock = 1;
5348 case CEE_MONO_CISINST:
5349 case CEE_MONO_CCASTCLASS: {
5354 token = read32 (ip + 2);
5355 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5356 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_CISINST) ? OP_CISINST : OP_CCASTCLASS);
5357 ins->type = STACK_I4;
5358 ins->inst_left = *sp;
5359 ins->inst_newa_class = klass;
5361 *sp++ = emit_tree (cfg, bblock, ins);
5365 case CEE_MONO_SAVE_LMF:
5366 case CEE_MONO_RESTORE_LMF:
5367 #ifdef MONO_ARCH_HAVE_LMF_OPS
5368 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_SAVE_LMF) ? OP_SAVE_LMF : OP_RESTORE_LMF);
5369 MONO_ADD_INS (bblock, ins);
5370 cfg->need_lmf_area = TRUE;
5375 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
5384 /* somewhat similar to LDTOKEN */
5385 MonoInst *addr, *vtvar;
5386 CHECK_STACK_OVF (1);
5387 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
5389 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5390 addr->cil_code = ip;
5391 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
5393 ins->inst_left = addr;
5394 MONO_ADD_INS (bblock, ins);
5395 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5409 * The following transforms:
5410 * CEE_CEQ into OP_CEQ
5411 * CEE_CGT into OP_CGT
5412 * CEE_CGT_UN into OP_CGT_UN
5413 * CEE_CLT into OP_CLT
5414 * CEE_CLT_UN into OP_CLT_UN
5416 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
5418 MONO_INST_NEW (cfg, ins, cmp->opcode);
5420 cmp->inst_i0 = sp [0];
5421 cmp->inst_i1 = sp [1];
5425 if ((sp [0]->type == STACK_I8) || ((sizeof (gpointer) == 8) && ((sp [0]->type == STACK_PTR) || (sp [0]->type == STACK_OBJ) || (sp [0]->type == STACK_MP))))
5426 cmp->opcode = OP_LCOMPARE;
5428 cmp->opcode = OP_COMPARE;
5430 ins->type = STACK_I4;
5433 /* spill it to reduce the expression complexity
5434 * and workaround bug 54209
5436 if (cmp->inst_left->type == STACK_I8) {
5438 *sp++ = emit_tree (cfg, bblock, ins);
5447 CHECK_STACK_OVF (1);
5449 n = read32 (ip + 2);
5450 if (method->wrapper_type != MONO_WRAPPER_NONE)
5451 cmethod = mono_method_get_wrapper_data (method, n);
5453 cmethod = mono_get_method_full (image, n, NULL, generic_context);
5456 mono_class_init (cmethod->klass);
5457 handle_loaded_temps (cfg, bblock, stack_start, sp);
5459 NEW_METHODCONST (cfg, argconst, cmethod);
5460 if (method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED)
5461 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
5463 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn_nosync, &argconst, ip);
5464 NEW_TEMPLOAD (cfg, *sp, temp);
5468 inline_costs += 10 * num_calls++;
5471 case CEE_LDVIRTFTN: {
5477 n = read32 (ip + 2);
5478 if (method->wrapper_type != MONO_WRAPPER_NONE)
5479 cmethod = mono_method_get_wrapper_data (method, n);
5481 cmethod = mono_get_method_full (image, n, NULL, generic_context);
5483 mono_class_init (cmethod->klass);
5484 handle_loaded_temps (cfg, bblock, stack_start, sp);
5488 NEW_METHODCONST (cfg, args [1], cmethod);
5489 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
5490 NEW_TEMPLOAD (cfg, *sp, temp);
5494 inline_costs += 10 * num_calls++;
5498 CHECK_STACK_OVF (1);
5500 n = read16 (ip + 2);
5502 NEW_ARGLOAD (cfg, ins, n);
5508 CHECK_STACK_OVF (1);
5510 n = read16 (ip + 2);
5512 NEW_ARGLOADA (cfg, ins, n);
5520 handle_loaded_temps (cfg, bblock, stack_start, sp);
5522 n = read16 (ip + 2);
5524 NEW_ARGSTORE (cfg, ins, n, *sp);
5526 if (ins->opcode == CEE_STOBJ) {
5527 NEW_ARGLOADA (cfg, ins, n);
5528 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5530 MONO_ADD_INS (bblock, ins);
5534 CHECK_STACK_OVF (1);
5536 n = read16 (ip + 2);
5538 NEW_LOCLOAD (cfg, ins, n);
5544 CHECK_STACK_OVF (1);
5546 n = read16 (ip + 2);
5548 NEW_LOCLOADA (cfg, ins, n);
5557 n = read16 (ip + 2);
5559 handle_loaded_temps (cfg, bblock, stack_start, sp);
5560 NEW_LOCSTORE (cfg, ins, n, *sp);
5562 if (ins->opcode == CEE_STOBJ) {
5563 NEW_LOCLOADA (cfg, ins, n);
5564 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5566 MONO_ADD_INS (bblock, ins);
5573 if (sp != stack_start)
5575 if (cfg->method != method)
5577 * Inlining this into a loop in a parent could lead to
5578 * stack overflows which is different behavior than the
5579 * non-inlined case, thus disable inlining in this case.
5581 goto inline_failure;
5582 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
5583 ins->inst_left = *sp;
5585 ins->type = STACK_MP;
5587 cfg->flags |= MONO_CFG_HAS_ALLOCA;
5588 if (header->init_locals)
5589 ins->flags |= MONO_INST_INIT;
5593 /* FIXME: set init flag if locals init is set in this method */
5595 case CEE_ENDFILTER: {
5596 MonoExceptionClause *clause, *nearest;
5597 int cc, nearest_num;
5601 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
5603 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
5604 ins->inst_left = *sp;
5606 MONO_ADD_INS (bblock, ins);
5607 start_new_bblock = 1;
5612 for (cc = 0; cc < header->num_clauses; ++cc) {
5613 clause = &header->clauses [cc];
5614 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
5615 (!nearest || (clause->data.filter_offset > nearest->data.filter_offset))) {
5621 filter_lengths [nearest_num] = (ip - header->code) - nearest->data.filter_offset;
5625 case CEE_UNALIGNED_:
5626 ins_flag |= MONO_INST_UNALIGNED;
5627 /* FIXME: record alignment? we can assume 1 for now */
5632 ins_flag |= MONO_INST_VOLATILE;
5636 ins_flag |= MONO_INST_TAILCALL;
5637 cfg->flags |= MONO_CFG_HAS_TAIL;
5638 /* Can't inline tail calls at this time */
5639 inline_costs += 100000;
5646 token = read32 (ip + 2);
5647 if (method->wrapper_type != MONO_WRAPPER_NONE)
5648 klass = mono_method_get_wrapper_data (method, token);
5650 klass = mono_class_get_full (image, token, generic_context);
5651 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5652 MonoInst *store, *load;
5653 NEW_PCONST (cfg, load, NULL);
5654 load->cil_code = ip;
5655 load->type = STACK_OBJ;
5656 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
5657 store->cil_code = ip;
5658 handle_loaded_temps (cfg, bblock, stack_start, sp);
5659 MONO_ADD_INS (bblock, store);
5660 store->inst_i0 = sp [0];
5661 store->inst_i1 = load;
5663 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
5668 case CEE_CONSTRAINED_:
5669 /* FIXME: implement */
5671 token = read32 (ip + 2);
5672 constrained_call = mono_class_get_full (image, token, generic_context);
5677 MonoInst *iargs [3];
5680 if ((cfg->opt & MONO_OPT_INTRINS) && (ip [1] == CEE_CPBLK) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5)) {
5682 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
5683 copy->inst_left = sp [0];
5684 copy->inst_right = sp [1];
5685 copy->cil_code = ip;
5687 MONO_ADD_INS (bblock, copy);
5694 handle_loaded_temps (cfg, bblock, stack_start, sp);
5695 if (ip [1] == CEE_CPBLK) {
5696 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
5698 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
5707 ins_flag |= MONO_INST_NOTYPECHECK;
5709 ins_flag |= MONO_INST_NORANGECHECK;
5710 /* we ignore the no-nullcheck for now since we
5711 * really do it explicitly only when doing callvirt->call
5717 /* FIXME: check we are in a catch handler */
5718 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
5719 load->cil_code = ip;
5720 MONO_INST_NEW (cfg, ins, OP_RETHROW);
5721 ins->inst_left = load;
5723 MONO_ADD_INS (bblock, ins);
5725 start_new_bblock = 1;
5730 CHECK_STACK_OVF (1);
5732 token = read32 (ip + 2);
5733 /* FIXXME: handle generics. */
5734 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
5735 MonoType *type = mono_type_create_from_typespec (image, token);
5736 token = mono_type_size (type, &align);
5738 MonoClass *szclass = mono_class_get_full (image, token, generic_context);
5739 mono_class_init (szclass);
5740 token = mono_class_value_size (szclass, &align);
5742 NEW_ICONST (cfg, ins, token);
5747 case CEE_REFANYTYPE:
5749 MONO_INST_NEW (cfg, ins, OP_REFANYTYPE);
5751 ins->type = STACK_MP;
5752 ins->inst_left = *sp;
5753 ins->type = STACK_VTYPE;
5754 ins->klass = mono_defaults.typehandle_class;
5763 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
5768 g_error ("opcode 0x%02x not handled", *ip);
5771 if (start_new_bblock != 1)
5774 bblock->cil_length = ip - bblock->cil_code;
5775 bblock->next_bb = end_bblock;
5776 link_bblock (cfg, bblock, end_bblock);
5778 if (cfg->method == method && cfg->domainvar) {
5782 MonoInst *get_domain;
5784 if (! (get_domain = mono_arch_get_domain_intrinsic (cfg))) {
5787 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
5788 call->signature = helper_sig_domain_get;
5789 call->inst.type = STACK_PTR;
5790 call->fptr = mono_domain_get;
5791 get_domain = (MonoInst*)call;
5794 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, get_domain);
5795 MONO_ADD_INS (init_localsbb, store);
5798 if (header->init_locals) {
5800 for (i = 0; i < header->num_locals; ++i) {
5801 MonoType *ptype = header->locals [i];
5802 int t = ptype->type;
5803 if (t == MONO_TYPE_VALUETYPE && ptype->data.klass->enumtype)
5804 t = ptype->data.klass->enum_basetype->type;
5805 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
5806 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
5807 NEW_ICONST (cfg, ins, 0);
5808 NEW_LOCSTORE (cfg, store, i, ins);
5809 MONO_ADD_INS (init_localsbb, store);
5810 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
5811 MONO_INST_NEW (cfg, ins, OP_I8CONST);
5812 ins->type = STACK_I8;
5814 NEW_LOCSTORE (cfg, store, i, ins);
5815 MONO_ADD_INS (init_localsbb, store);
5816 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
5817 MONO_INST_NEW (cfg, ins, OP_R8CONST);
5818 ins->type = STACK_R8;
5819 ins->inst_p0 = (void*)&r8_0;
5820 NEW_LOCSTORE (cfg, store, i, ins);
5821 MONO_ADD_INS (init_localsbb, store);
5822 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
5823 ((t == MONO_TYPE_GENERICINST) && mono_metadata_generic_inst_is_valuetype (ptype->data.generic_inst))) {
5824 NEW_LOCLOADA (cfg, ins, i);
5825 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (ptype), NULL, NULL);
5828 NEW_PCONST (cfg, ins, NULL);
5829 NEW_LOCSTORE (cfg, store, i, ins);
5830 MONO_ADD_INS (init_localsbb, store);
5836 /* resolve backward branches in the middle of an existing basic block */
5837 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
5839 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
5840 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
5841 if (tblock != start_bblock) {
5843 split_bblock (cfg, tblock, bblock);
5844 l = bblock->cil_code - header->code;
5845 bblock->cil_length = tblock->cil_length - l;
5846 tblock->cil_length = l;
5848 g_print ("recheck failed.\n");
5853 * we compute regions here, because the length of filter clauses is not known in advance.
5854 * It is computed in the CEE_ENDFILTER case in the above switch statement
5856 if (cfg->method == method) {
5858 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5859 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
5861 mono_create_spvar_for_region (cfg, bb->region);
5862 if (cfg->verbose_level > 2)
5863 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
5866 g_hash_table_destroy (bbhash);
5869 dont_inline = g_list_remove (dont_inline, method);
5870 return inline_costs;
5873 if (cfg->method != method)
5874 g_hash_table_destroy (bbhash);
5875 dont_inline = g_list_remove (dont_inline, method);
5879 if (cfg->method != method)
5880 g_hash_table_destroy (bbhash);
5881 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
5882 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
5883 dont_inline = g_list_remove (dont_inline, method);
5888 mono_print_tree (MonoInst *tree) {
5894 arity = mono_burg_arity [tree->opcode];
5896 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
5898 switch (tree->opcode) {
5900 printf ("[%d]", tree->inst_c0);
5903 printf ("[%lld]", tree->inst_l);
5906 printf ("[%f]", *(double*)tree->inst_p0);
5909 printf ("[%f]", *(float*)tree->inst_p0);
5913 printf ("[%d]", tree->inst_c0);
5916 if (tree->inst_offset < 0)
5917 printf ("[-0x%x(%s)]", -tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5919 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5922 printf ("[%s]", mono_arch_regname (tree->dreg));
5925 printf ("[%s]", tree->inst_newa_class->name);
5926 mono_print_tree (tree->inst_newa_len);
5937 case OP_VOIDCALLVIRT: {
5938 MonoCallInst *call = (MonoCallInst*)tree;
5940 printf ("[%s]", call->method->name);
5945 printf ("[%d (", tree->inst_c0);
5946 for (i = 0; i < tree->inst_phi_args [0]; i++) {
5949 printf ("%d", tree->inst_phi_args [i + 1]);
5960 case OP_LOAD_MEMBASE:
5961 case OP_LOADI4_MEMBASE:
5962 case OP_LOADU4_MEMBASE:
5963 case OP_LOADU1_MEMBASE:
5964 case OP_LOADI1_MEMBASE:
5965 case OP_LOADU2_MEMBASE:
5966 case OP_LOADI2_MEMBASE:
5967 printf ("[%s] <- [%s + 0x%x]", mono_arch_regname (tree->dreg), mono_arch_regname (tree->inst_basereg), tree->inst_offset);
5970 printf ("[B%d]", tree->inst_target_bb->block_num);
5980 case OP_VOIDCALL_REG:
5981 mono_print_tree (tree->inst_left);
5993 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
5994 mono_print_tree (tree->inst_left);
5997 if (!mono_arch_print_tree(tree, arity)) {
5999 mono_print_tree (tree->inst_left);
6001 mono_print_tree (tree->inst_right);
6012 mono_print_tree_nl (MonoInst *tree)
6014 mono_print_tree (tree);
6018 #define make_icall_sig mono_create_icall_signature
6021 create_helper_signature (void)
6023 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
6024 helper_sig_newarr = make_icall_sig ("object ptr ptr int32");
6026 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
6027 helper_sig_newarr_specific = make_icall_sig ("object ptr int32");
6029 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
6030 helper_sig_object_new = make_icall_sig ("object ptr ptr");
6032 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
6033 helper_sig_object_new_specific = make_icall_sig ("object ptr");
6035 /* void* mono_method_compile (MonoMethod*) */
6036 helper_sig_compile = make_icall_sig ("ptr ptr");
6038 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
6039 helper_sig_compile_virt = make_icall_sig ("ptr object ptr");
6041 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
6042 helper_sig_ldstr = make_icall_sig ("object ptr ptr int32");
6044 /* MonoDomain *mono_domain_get (void) */
6045 helper_sig_domain_get = make_icall_sig ("ptr");
6047 /* void stelem_ref (MonoArray *, int index, MonoObject *) */
6048 helper_sig_stelem_ref = make_icall_sig ("void ptr int32 object");
6050 /* void stelem_ref_check (MonoArray *, MonoObject *) */
6051 helper_sig_stelem_ref_check = make_icall_sig ("void object object");
6053 /* long amethod (long, long) */
6054 helper_sig_long_long_long = make_icall_sig ("long long long");
6056 /* object amethod (intptr) */
6057 helper_sig_obj_ptr = make_icall_sig ("object ptr");
6059 helper_sig_obj_ptr_ptr = make_icall_sig ("object ptr ptr");
6061 helper_sig_obj_obj_ptr_ptr = make_icall_sig ("object object ptr ptr");
6063 helper_sig_void_void = make_icall_sig ("void");
6065 /* void amethod (intptr) */
6066 helper_sig_void_ptr = make_icall_sig ("void ptr");
6068 /* void amethod (MonoObject *obj) */
6069 helper_sig_void_obj = make_icall_sig ("void object");
6071 /* void amethod (MonoObject *obj, void *ptr, int i) */
6072 helper_sig_void_obj_ptr_int = make_icall_sig ("void object ptr int");
6074 helper_sig_void_obj_ptr_ptr_obj = make_icall_sig ("void object ptr ptr object");
6076 /* intptr amethod (void) */
6077 helper_sig_ptr_void = make_icall_sig ("ptr");
6079 /* object amethod (void) */
6080 helper_sig_obj_void = make_icall_sig ("object");
6082 /* void amethod (intptr, intptr) */
6083 helper_sig_void_ptr_ptr = make_icall_sig ("void ptr ptr");
6085 /* void amethod (intptr, intptr, intptr) */
6086 helper_sig_void_ptr_ptr_ptr = make_icall_sig ("void ptr ptr ptr");
6088 /* intptr amethod (intptr, intptr) */
6089 helper_sig_ptr_ptr_ptr = make_icall_sig ("ptr ptr ptr");
6091 /* IntPtr amethod (object) */
6092 helper_sig_ptr_obj = make_icall_sig ("ptr object");
6094 /* IntPtr amethod (object, int) */
6095 helper_sig_ptr_obj_int = make_icall_sig ("ptr object int");
6097 /* IntPtr amethod (int) */
6098 helper_sig_ptr_int = make_icall_sig ("ptr int32");
6100 /* long amethod (long, guint32) */
6101 helper_sig_long_long_int = make_icall_sig ("long long int32");
6103 /* ulong amethod (double) */
6104 helper_sig_ulong_double = make_icall_sig ("ulong double");
6106 /* long amethod (double) */
6107 helper_sig_long_double = make_icall_sig ("long double");
6109 /* double amethod (long) */
6110 helper_sig_double_long = make_icall_sig ("double long");
6112 /* double amethod (int) */
6113 helper_sig_double_int = make_icall_sig ("double int32");
6115 /* float amethod (long) */
6116 helper_sig_float_long = make_icall_sig ("float long");
6118 /* double amethod (double, double) */
6119 helper_sig_double_double_double = make_icall_sig ("double double double");
6121 /* uint amethod (double) */
6122 helper_sig_uint_double = make_icall_sig ("uint32 double");
6124 /* int amethod (double) */
6125 helper_sig_int_double = make_icall_sig ("int32 double");
6127 /* void initobj (intptr, int size) */
6128 helper_sig_initobj = make_icall_sig ("void ptr int32");
6130 /* void memcpy (intptr, intptr, int size) */
6131 helper_sig_memcpy = make_icall_sig ("void ptr ptr int32");
6133 /* void memset (intptr, int val, int size) */
6134 helper_sig_memset = make_icall_sig ("void ptr int32 int32");
6136 helper_sig_class_init_trampoline = make_icall_sig ("void");
6140 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
6143 MonoMethod *wrapper;
6146 if (callinfo->wrapper)
6147 return callinfo->wrapper;
6149 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
6150 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
6151 /* Must be domain neutral since there is only one copy */
6152 code = mono_jit_compile_method_with_opt (wrapper, default_opt | MONO_OPT_SHARED);
6154 if (!callinfo->wrapper) {
6155 callinfo->wrapper = code;
6156 mono_register_jit_icall_wrapper (callinfo, code);
6157 mono_debug_add_icall_wrapper (wrapper, callinfo);
6161 return callinfo->wrapper;
6165 mono_create_class_init_trampoline (MonoVTable *vtable)
6169 /* previously created trampoline code */
6170 mono_domain_lock (vtable->domain);
6172 mono_g_hash_table_lookup (vtable->domain->class_init_trampoline_hash,
6174 mono_domain_unlock (vtable->domain);
6178 code = mono_arch_create_class_init_trampoline (vtable);
6180 /* store trampoline address */
6181 mono_domain_lock (vtable->domain);
6182 mono_g_hash_table_insert (vtable->domain->class_init_trampoline_hash,
6184 mono_domain_unlock (vtable->domain);
6186 EnterCriticalSection (&jit_mutex);
6187 if (!class_init_hash_addr)
6188 class_init_hash_addr = g_hash_table_new (NULL, NULL);
6189 g_hash_table_insert (class_init_hash_addr, code, vtable);
6190 LeaveCriticalSection (&jit_mutex);
6196 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method,
6197 gboolean add_sync_wrapper)
6202 if (add_sync_wrapper && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
6203 return mono_create_jump_trampoline (domain, mono_marshal_get_synchronized_wrapper (method), FALSE);
6205 code = mono_jit_find_compiled_method (domain, method);
6209 mono_domain_lock (domain);
6210 code = mono_g_hash_table_lookup (domain->jump_trampoline_hash, method);
6211 mono_domain_unlock (domain);
6215 ji = mono_arch_create_jump_trampoline (method);
6218 * mono_delegate_ctor needs to find the method metadata from the
6219 * trampoline address, so we save it here.
6222 mono_jit_info_table_add (mono_get_root_domain (), ji);
6224 mono_domain_lock (domain);
6225 mono_g_hash_table_insert (domain->jump_trampoline_hash, method, ji->code_start);
6226 mono_domain_unlock (domain);
6228 return ji->code_start;
6232 mono_create_jit_trampoline (MonoMethod *method)
6234 MonoDomain *domain = mono_domain_get ();
6237 /* Trampoline are domain specific, so cache only the one used in the root domain */
6238 if ((domain == mono_get_root_domain ()) && method->info)
6239 return method->info;
6241 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
6242 return mono_create_jit_trampoline (mono_marshal_get_synchronized_wrapper (method));
6244 tramp = mono_arch_create_jit_trampoline (method);
6245 if (domain == mono_get_root_domain ())
6246 method->info = tramp;
6252 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
6256 EnterCriticalSection (&jit_mutex);
6257 if (class_init_hash_addr)
6258 res = g_hash_table_lookup (class_init_hash_addr, addr);
6261 LeaveCriticalSection (&jit_mutex);
6266 mono_dynamic_code_hash_insert (MonoDomain *domain, MonoMethod *method, MonoJitDynamicMethodInfo *ji)
6268 if (!domain->dynamic_code_hash)
6269 domain->dynamic_code_hash = g_hash_table_new (NULL, NULL);
6270 g_hash_table_insert (domain->dynamic_code_hash, method, ji);
6273 static MonoJitDynamicMethodInfo*
6274 mono_dynamic_code_hash_lookup (MonoDomain *domain, MonoMethod *method)
6276 MonoJitDynamicMethodInfo *res;
6278 if (domain->dynamic_code_hash)
6279 res = g_hash_table_lookup (domain->dynamic_code_hash, method);
6286 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func, gboolean no_throw)
6288 MonoJitICallInfo *info;
6290 if (!emul_opcode_map)
6291 emul_opcode_map = g_new0 (MonoJitICallInfo*, OP_LAST + 1);
6293 g_assert (!sig->hasthis);
6294 g_assert (sig->param_count < 3);
6296 info = mono_register_jit_icall (func, name, sig, no_throw);
6298 emul_opcode_map [opcode] = info;
6302 decompose_foreach (MonoInst *tree, gpointer data)
6304 static MonoJitICallInfo *newarr_info = NULL;
6305 static MonoJitICallInfo *newarr_specific_info = NULL;
6306 MonoJitICallInfo *info;
6309 switch (tree->opcode) {
6311 MonoCompile *cfg = data;
6312 MonoInst *iargs [3];
6315 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
6316 g_assert (newarr_info);
6317 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
6318 g_assert (newarr_specific_info);
6321 if (cfg->opt & MONO_OPT_SHARED) {
6322 NEW_DOMAINCONST (cfg, iargs [0]);
6323 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
6324 iargs [2] = tree->inst_newa_len;
6329 MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
6331 NEW_VTABLECONST (cfg, iargs [0], vtable);
6332 iargs [1] = tree->inst_newa_len;
6334 info = newarr_specific_info;
6337 mono_emulate_opcode (cfg, tree, iargs, info);
6339 /* Need to decompose arguments after the the opcode is decomposed */
6340 for (i = 0; i < info->sig->param_count; ++i)
6341 dec_foreach (iargs [i], cfg);
6351 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
6353 switch (mono_burg_arity [tree->opcode]) {
6356 mono_inst_foreach (tree->inst_left, func, data);
6359 mono_inst_foreach (tree->inst_left, func, data);
6360 mono_inst_foreach (tree->inst_right, func, data);
6363 g_assert_not_reached ();
6370 mono_print_bb_code (MonoBasicBlock *bb) {
6372 MonoInst *c = bb->code;
6374 mono_print_tree (c);
6382 print_dfn (MonoCompile *cfg) {
6387 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
6389 for (i = 0; i < cfg->num_bblocks; ++i) {
6390 bb = cfg->bblocks [i];
6391 /*if (bb->cil_code) {
6392 char* code1, *code2;
6393 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
6394 if (bb->last_ins->cil_code)
6395 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
6397 code2 = g_strdup ("");
6399 code1 [strlen (code1) - 1] = 0;
6400 code = g_strdup_printf ("%s -> %s", code1, code2);
6404 code = g_strdup ("\n");
6405 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
6407 MonoInst *c = bb->code;
6409 mono_print_tree (c);
6417 g_print ("\tprev:");
6418 for (j = 0; j < bb->in_count; ++j) {
6419 g_print (" BB%d", bb->in_bb [j]->block_num);
6421 g_print ("\t\tsucc:");
6422 for (j = 0; j < bb->out_count; ++j) {
6423 g_print (" BB%d", bb->out_bb [j]->block_num);
6425 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
6428 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
6431 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
6433 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
6441 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
6445 g_assert (bb->code);
6446 bb->last_ins->next = inst;
6447 bb->last_ins = inst;
6449 bb->last_ins = bb->code = inst;
6454 mono_destroy_compile (MonoCompile *cfg)
6456 //mono_mempool_stats (cfg->mempool);
6457 g_hash_table_destroy (cfg->bb_hash);
6458 mono_free_loop_info (cfg);
6460 mono_regstate_free (cfg->rs);
6462 g_hash_table_destroy (cfg->spvars);
6463 mono_mempool_destroy (cfg->mempool);
6464 g_list_free (cfg->ldstr_list);
6466 g_free (cfg->varinfo);
6471 #ifdef HAVE_KW_THREAD
6472 static __thread gpointer mono_lmf_addr;
6476 mono_get_lmf_addr (void)
6478 #ifdef HAVE_KW_THREAD
6479 return mono_lmf_addr;
6481 MonoJitTlsData *jit_tls;
6483 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
6484 return &jit_tls->lmf;
6486 g_assert_not_reached ();
6492 * mono_thread_abort:
6493 * @obj: exception object
6495 * abort the thread, print exception information and stack trace
6498 mono_thread_abort (MonoObject *obj)
6500 /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
6502 /* handle_remove should be eventually called for this thread, too
6505 mono_thread_exit ();
6509 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
6511 MonoJitTlsData *jit_tls;
6514 jit_tls = TlsGetValue (mono_jit_tls_id);
6518 jit_tls = g_new0 (MonoJitTlsData, 1);
6520 TlsSetValue (mono_jit_tls_id, jit_tls);
6522 jit_tls->abort_func = abort_func;
6523 jit_tls->end_of_stack = stack_start;
6525 lmf = g_new0 (MonoLMF, 1);
6528 jit_tls->lmf = jit_tls->first_lmf = lmf;
6530 #ifdef HAVE_KW_THREAD
6531 mono_lmf_addr = &jit_tls->lmf;
6534 mono_arch_setup_jit_tls_data (jit_tls);
6540 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
6543 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
6544 thread = mono_thread_current ();
6546 thread->jit_data = jit_tls;
6549 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
6552 mono_thread_abort_dummy (MonoObject *obj)
6554 if (mono_thread_attach_aborted_cb)
6555 mono_thread_attach_aborted_cb (obj);
6557 mono_thread_abort (obj);
6561 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
6564 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
6565 thread = mono_thread_current ();
6567 thread->jit_data = jit_tls;
6571 mini_thread_cleanup (MonoThread *thread)
6573 MonoJitTlsData *jit_tls = thread->jit_data;
6576 mono_arch_free_jit_tls_data (jit_tls);
6577 g_free (jit_tls->first_lmf);
6579 thread->jit_data = NULL;
6584 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
6586 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
6590 ji->data.target = target;
6591 ji->next = cfg->patch_info;
6593 cfg->patch_info = ji;
6597 mono_remove_patch_info (MonoCompile *cfg, int ip)
6599 MonoJumpInfo **ji = &cfg->patch_info;
6602 if ((*ji)->ip.i == ip)
6605 ji = &((*ji)->next);
6610 mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors)
6612 unsigned char *ip = patch_info->ip.i + code;
6613 gconstpointer target = NULL;
6615 switch (patch_info->type) {
6616 case MONO_PATCH_INFO_BB:
6617 target = patch_info->data.bb->native_offset + code;
6619 case MONO_PATCH_INFO_ABS:
6620 target = patch_info->data.target;
6622 case MONO_PATCH_INFO_LABEL:
6623 target = patch_info->data.inst->inst_c0 + code;
6625 case MONO_PATCH_INFO_IP:
6628 case MONO_PATCH_INFO_METHOD_REL:
6629 target = code + patch_info->data.offset;
6631 case MONO_PATCH_INFO_INTERNAL_METHOD: {
6632 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
6634 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
6635 g_assert_not_reached ();
6637 target = mono_icall_get_wrapper (mi);
6640 case MONO_PATCH_INFO_METHOD_JUMP: {
6643 /* get the trampoline to the method from the domain */
6644 target = mono_create_jump_trampoline (domain, patch_info->data.method, TRUE);
6645 if (!domain->jump_target_hash)
6646 domain->jump_target_hash = g_hash_table_new (NULL, NULL);
6647 list = g_hash_table_lookup (domain->jump_target_hash, patch_info->data.method);
6648 list = g_slist_prepend (list, ip);
6649 g_hash_table_insert (domain->jump_target_hash, patch_info->data.method, list);
6652 case MONO_PATCH_INFO_METHOD:
6653 if (patch_info->data.method == method) {
6656 /* get the trampoline to the method from the domain */
6657 target = mono_create_jit_trampoline (patch_info->data.method);
6659 case MONO_PATCH_INFO_SWITCH: {
6660 gpointer *jump_table;
6663 if (method->dynamic) {
6664 jump_table = mono_code_manager_reserve (mono_dynamic_code_hash_lookup (domain, method)->code_mp, sizeof (gpointer) * patch_info->table_size);
6666 mono_domain_lock (domain);
6667 jump_table = mono_code_manager_reserve (domain->code_mp, sizeof (gpointer) * patch_info->table_size);
6668 mono_domain_unlock (domain);
6671 for (i = 0; i < patch_info->table_size; i++) {
6672 jump_table [i] = code + (int)patch_info->data.table [i];
6674 target = jump_table;
6677 case MONO_PATCH_INFO_METHODCONST:
6678 case MONO_PATCH_INFO_CLASS:
6679 case MONO_PATCH_INFO_IMAGE:
6680 case MONO_PATCH_INFO_FIELD:
6681 target = patch_info->data.target;
6683 case MONO_PATCH_INFO_IID:
6684 mono_class_init (patch_info->data.klass);
6685 target = (gpointer)patch_info->data.klass->interface_id;
6687 case MONO_PATCH_INFO_VTABLE:
6688 target = mono_class_vtable (domain, patch_info->data.klass);
6690 case MONO_PATCH_INFO_CLASS_INIT:
6691 target = mono_create_class_init_trampoline (mono_class_vtable (domain, patch_info->data.klass));
6693 case MONO_PATCH_INFO_SFLDA: {
6694 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
6695 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (vtable->klass, method))
6696 /* Done by the generated code */
6700 mono_runtime_class_init (vtable);
6702 target = (char*)vtable->data + patch_info->data.field->offset;
6705 case MONO_PATCH_INFO_R4:
6706 case MONO_PATCH_INFO_R8:
6707 target = patch_info->data.target;
6709 case MONO_PATCH_INFO_EXC_NAME:
6710 target = patch_info->data.name;
6712 case MONO_PATCH_INFO_LDSTR:
6714 mono_ldstr (domain, patch_info->data.token->image,
6715 mono_metadata_token_index (patch_info->data.token->token));
6717 case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
6719 MonoClass *handle_class;
6721 handle = mono_ldtoken (patch_info->data.token->image,
6722 patch_info->data.token->token, &handle_class, NULL);
6723 mono_class_init (handle_class);
6724 mono_class_init (mono_class_from_mono_type (handle));
6727 mono_type_get_object (domain, handle);
6730 case MONO_PATCH_INFO_LDTOKEN: {
6732 MonoClass *handle_class;
6734 handle = mono_ldtoken (patch_info->data.token->image,
6735 patch_info->data.token->token, &handle_class, NULL);
6736 mono_class_init (handle_class);
6741 case MONO_PATCH_INFO_BB_OVF:
6742 case MONO_PATCH_INFO_EXC_OVF:
6745 g_assert_not_reached ();
6748 return (gpointer)target;
6752 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
6753 MonoJitICallInfo *info;
6755 decompose_foreach (tree, cfg);
6757 switch (mono_burg_arity [tree->opcode]) {
6760 dec_foreach (tree->inst_left, cfg);
6762 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6763 MonoInst *iargs [2];
6765 iargs [0] = tree->inst_left;
6767 mono_emulate_opcode (cfg, tree, iargs, info);
6773 #ifdef MONO_ARCH_BIGMUL_INTRINS
6774 if (tree->opcode == OP_LMUL
6775 && (cfg->opt & MONO_OPT_INTRINS)
6776 && (tree->inst_left->opcode == CEE_CONV_I8
6777 || tree->inst_left->opcode == CEE_CONV_U8)
6778 && tree->inst_left->inst_left->type == STACK_I4
6779 && (tree->inst_right->opcode == CEE_CONV_I8
6780 || tree->inst_right->opcode == CEE_CONV_U8)
6781 && tree->inst_right->inst_left->type == STACK_I4
6782 && tree->inst_left->opcode == tree->inst_right->opcode) {
6783 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
6784 tree->inst_left = tree->inst_left->inst_left;
6785 tree->inst_right = tree->inst_right->inst_left;
6786 dec_foreach (tree, cfg);
6789 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6790 MonoInst *iargs [2];
6792 iargs [0] = tree->inst_i0;
6793 iargs [1] = tree->inst_i1;
6795 mono_emulate_opcode (cfg, tree, iargs, info);
6797 dec_foreach (iargs [0], cfg);
6798 dec_foreach (iargs [1], cfg);
6801 dec_foreach (tree->inst_left, cfg);
6802 dec_foreach (tree->inst_right, cfg);
6806 g_assert_not_reached ();
6811 decompose_pass (MonoCompile *cfg) {
6814 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6817 cfg->prev_ins = NULL;
6818 for (tree = cfg->cbb->code; tree; tree = tree->next) {
6819 dec_foreach (tree, cfg);
6820 cfg->prev_ins = tree;
6826 nullify_basic_block (MonoBasicBlock *bb)
6833 bb->code = bb->last_ins = NULL;
6834 bb->cil_code = NULL;
6838 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6842 for (i = 0; i < bb->out_count; i++) {
6843 MonoBasicBlock *ob = bb->out_bb [i];
6846 if (bb->out_count > 1) {
6847 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
6851 bb->out_bb [i] = repl;
6858 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6862 for (i = 0; i < bb->in_count; i++) {
6863 MonoBasicBlock *ib = bb->in_bb [i];
6866 if (bb->in_count > 1) {
6867 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
6871 bb->in_bb [i] = repl;
6878 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6882 for (i = 0; i < bb->out_count; i++) {
6883 MonoBasicBlock *ob = bb->out_bb [i];
6884 for (j = 0; j < ob->in_count; j++) {
6885 if (ob->in_bb [j] == orig) {
6886 ob->in_bb [j] = repl;
6895 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
6897 bb->out_count = bbn->out_count;
6898 bb->out_bb = bbn->out_bb;
6900 replace_basic_block (bb, bbn, bb);
6904 bb->last_ins->next = bbn->code;
6905 bb->last_ins = bbn->last_ins;
6908 bb->code = bbn->code;
6909 bb->last_ins = bbn->last_ins;
6911 bb->next_bb = bbn->next_bb;
6912 nullify_basic_block (bbn);
6916 * Optimizes the branches on the Control Flow Graph
6920 optimize_branches (MonoCompile *cfg)
6922 int i, changed = FALSE;
6923 MonoBasicBlock *bb, *bbn;
6924 guint32 niterations;
6927 * Some crazy loops could cause the code below to go into an infinite
6928 * loop, see bug #53003 for an example. To prevent this, we put an upper
6929 * bound on the number of iterations.
6936 /* we skip the entry block (exit is handled specially instead ) */
6937 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
6939 /* dont touch code inside exception clauses */
6940 if (bb->region != -1)
6943 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
6944 if (cfg->verbose_level > 2)
6945 g_print ("nullify block triggered %d\n", bbn->block_num);
6947 bb->next_bb = bbn->next_bb;
6949 for (i = 0; i < bbn->out_count; i++)
6950 replace_in_block (bbn->out_bb [i], bbn, NULL);
6952 nullify_basic_block (bbn);
6956 if (bb->out_count == 1) {
6957 bbn = bb->out_bb [0];
6959 /* conditional branches where true and false targets are the same can be also replaced with CEE_BR */
6960 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins)) {
6961 bb->last_ins->opcode = CEE_BR;
6962 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
6964 if (cfg->verbose_level > 2)
6965 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
6968 if (bb->region == bbn->region && bb->next_bb == bbn) {
6969 /* the block are in sequence anyway ... */
6971 /* branches to the following block can be removed */
6972 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
6973 bb->last_ins->opcode = CEE_NOP;
6975 if (cfg->verbose_level > 2)
6976 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
6979 if (bbn->in_count == 1) {
6981 if (bbn != cfg->bb_exit) {
6982 if (cfg->verbose_level > 2)
6983 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
6984 merge_basic_blocks (bb, bbn);
6988 //mono_print_bb_code (bb);
6993 } while (changed && (niterations > 0));
7000 /* we skip the entry block (exit is handled specially instead ) */
7001 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
7003 /* dont touch code inside exception clauses */
7004 if (bb->region != -1)
7007 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
7008 if (cfg->verbose_level > 2) {
7009 g_print ("nullify block triggered %d\n", bbn->block_num);
7011 bb->next_bb = bbn->next_bb;
7013 for (i = 0; i < bbn->out_count; i++)
7014 replace_in_block (bbn->out_bb [i], bbn, NULL);
7016 nullify_basic_block (bbn);
7022 if (bb->out_count == 1) {
7023 bbn = bb->out_bb [0];
7025 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
7026 bbn = bb->last_ins->inst_target_bb;
7027 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7028 bbn->code->inst_target_bb->region == bb->region) {
7030 if (cfg->verbose_level > 2)
7031 g_print ("in %s branch to branch triggered %d -> %d -> %d\n", cfg->method->name,
7032 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num);
7034 replace_in_block (bbn, bb, NULL);
7035 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7036 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7037 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
7042 } else if (bb->out_count == 2) {
7043 if (bb->last_ins && MONO_IS_COND_BRANCH_NOFP (bb->last_ins)) {
7044 bbn = bb->last_ins->inst_true_bb;
7045 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7046 bbn->code->inst_target_bb->region == bb->region) {
7047 if (cfg->verbose_level > 2)
7048 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
7049 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
7052 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
7054 replace_in_block (bbn, bb, NULL);
7056 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
7057 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7059 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7065 bbn = bb->last_ins->inst_false_bb;
7066 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7067 bbn->code->inst_target_bb->region == bb->region) {
7068 if (cfg->verbose_level > 2)
7069 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
7070 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
7073 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
7075 replace_in_block (bbn, bb, NULL);
7077 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
7078 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7080 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7088 } while (changed && (niterations > 0));
7093 mono_compile_create_vars (MonoCompile *cfg)
7095 MonoMethodSignature *sig;
7096 MonoMethodHeader *header;
7099 header = mono_method_get_header (cfg->method);
7101 sig = cfg->method->signature;
7103 if (!MONO_TYPE_IS_VOID (sig->ret)) {
7104 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
7105 cfg->ret->opcode = OP_RETARG;
7106 cfg->ret->inst_vtype = sig->ret;
7107 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
7109 if (cfg->verbose_level > 2)
7110 g_print ("creating vars\n");
7113 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
7115 for (i = 0; i < sig->param_count; ++i)
7116 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
7118 cfg->locals_start = cfg->num_varinfo;
7120 if (cfg->verbose_level > 2)
7121 g_print ("creating locals\n");
7122 for (i = 0; i < header->num_locals; ++i)
7123 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
7124 if (cfg->verbose_level > 2)
7125 g_print ("locals done\n");
7129 mono_print_code (MonoCompile *cfg)
7133 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7134 MonoInst *tree = bb->code;
7139 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
7141 for (; tree; tree = tree->next) {
7142 mono_print_tree (tree);
7147 bb->last_ins->next = NULL;
7151 extern const char * const mono_burg_rule_string [];
7154 emit_state (MonoCompile *cfg, MBState *state, int goal)
7157 int ern = mono_burg_rule (state, goal);
7158 const guint16 *nts = mono_burg_nts [ern];
7161 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
7165 // state->reg1 = state->reg2; /* chain rule */
7167 #ifdef MONO_ARCH_ENABLE_EMIT_STATE_OPT
7170 state->reg1 = mono_regstate_next_int (cfg->rs);
7171 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
7174 state->reg1 = mono_regstate_next_int (cfg->rs);
7175 state->reg2 = mono_regstate_next_int (cfg->rs);
7178 state->reg1 = mono_regstate_next_float (cfg->rs);
7181 #ifdef MONO_ARCH_ENABLE_EMIT_STATE_OPT
7183 * Enabling this might cause bugs to surface in the local register
7184 * allocators on some architectures like x86.
7186 if ((state->tree->ssa_op == MONO_SSA_STORE) && (state->left->tree->opcode == OP_REGVAR)) {
7187 /* Do not optimize away reg-reg moves */
7188 if (! ((state->right->tree->ssa_op == MONO_SSA_LOAD) && (state->right->left->tree->opcode == OP_REGVAR))) {
7189 state->right->reg1 = state->left->tree->dreg;
7198 mono_burg_kids (state, ern, kids);
7200 emit_state (cfg, kids [0], nts [0]);
7202 emit_state (cfg, kids [1], nts [1]);
7204 g_assert (!nts [3]);
7205 emit_state (cfg, kids [2], nts [2]);
7210 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
7211 if ((emit = mono_burg_func [ern]))
7212 emit (state, state->tree, cfg);
7215 #define DEBUG_SELECTION
7218 mini_select_instructions (MonoCompile *cfg)
7220 static const int reverse_map [] = {
7221 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
7222 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
7224 static const int reverse_fmap [] = {
7225 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
7226 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
7228 static const int reverse_lmap [] = {
7229 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
7230 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
7232 static const int reverse_imap [] = {
7233 OP_IBNE_UN, OP_IBLT, OP_IBLE, OP_IBGT, OP_IBGE,
7234 OP_IBEQ, OP_IBLT_UN, OP_IBLE_UN, OP_IBGT_UN, OP_IBGE_UN
7239 cfg->state_pool = mono_mempool_new ();
7240 cfg->rs = mono_regstate_new ();
7242 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7243 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins) &&
7244 bb->next_bb != bb->last_ins->inst_false_bb) {
7246 /* we are careful when inverting, since bugs like #59580
7247 * could show up when dealing with NaNs.
7249 if (MONO_IS_COND_BRANCH_NOFP(bb->last_ins) && bb->next_bb == bb->last_ins->inst_true_bb) {
7250 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
7251 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
7252 bb->last_ins->inst_false_bb = tmp;
7254 if (bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
7255 bb->last_ins->opcode = reverse_map [bb->last_ins->opcode - CEE_BEQ];
7256 } else if (bb->last_ins->opcode >= OP_FBEQ && bb->last_ins->opcode <= OP_FBLT_UN) {
7257 bb->last_ins->opcode = reverse_fmap [bb->last_ins->opcode - OP_FBEQ];
7258 } else if (bb->last_ins->opcode >= OP_LBEQ && bb->last_ins->opcode <= OP_LBLT_UN) {
7259 bb->last_ins->opcode = reverse_lmap [bb->last_ins->opcode - OP_LBEQ];
7260 } else if (bb->last_ins->opcode >= OP_IBEQ && bb->last_ins->opcode <= OP_IBLT_UN) {
7261 bb->last_ins->opcode = reverse_imap [bb->last_ins->opcode - OP_IBEQ];
7264 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
7265 inst->opcode = CEE_BR;
7266 inst->inst_target_bb = bb->last_ins->inst_false_bb;
7267 mono_bblock_add_inst (bb, inst);
7272 #ifdef DEBUG_SELECTION
7273 if (cfg->verbose_level >= 4) {
7274 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7275 MonoInst *tree = bb->code;
7276 g_print ("DUMP BLOCK %d:\n", bb->block_num);
7279 for (; tree; tree = tree->next) {
7280 mono_print_tree (tree);
7287 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7288 MonoInst *tree = bb->code, *next;
7294 bb->last_ins = NULL;
7297 mono_regstate_reset (cfg->rs);
7299 #ifdef DEBUG_SELECTION
7300 if (cfg->verbose_level >= 3)
7301 g_print ("LABEL BLOCK %d:\n", bb->block_num);
7303 for (; tree; tree = next) {
7305 #ifdef DEBUG_SELECTION
7306 if (cfg->verbose_level >= 3) {
7307 mono_print_tree (tree);
7312 if (!(mbstate = mono_burg_label (tree, cfg))) {
7313 g_warning ("unable to label tree %p", tree);
7314 mono_print_tree (tree);
7316 g_assert_not_reached ();
7318 emit_state (cfg, mbstate, MB_NTERM_stmt);
7320 bb->max_ireg = cfg->rs->next_vireg;
7321 bb->max_freg = cfg->rs->next_vfreg;
7324 bb->last_ins->next = NULL;
7326 mono_mempool_empty (cfg->state_pool);
7328 mono_mempool_destroy (cfg->state_pool);
7332 mono_codegen (MonoCompile *cfg)
7334 MonoJumpInfo *patch_info;
7336 int i, max_epilog_size;
7339 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7340 cfg->spill_count = 0;
7341 /* we reuse dfn here */
7342 /* bb->dfn = bb_count++; */
7343 mono_arch_local_regalloc (cfg, bb);
7346 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
7347 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
7349 code = mono_arch_emit_prolog (cfg);
7351 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7352 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
7354 cfg->code_len = code - cfg->native_code;
7355 cfg->prolog_end = cfg->code_len;
7357 mono_debug_open_method (cfg);
7359 /* emit code all basic blocks */
7360 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7361 bb->native_offset = cfg->code_len;
7362 mono_arch_output_basic_block (cfg, bb);
7364 cfg->bb_exit->native_offset = cfg->code_len;
7366 code = cfg->native_code + cfg->code_len;
7368 max_epilog_size = mono_arch_max_epilog_size (cfg);
7370 /* we always allocate code in cfg->domain->code_mp to increase locality */
7371 cfg->code_size = cfg->code_len + max_epilog_size;
7372 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
7374 if (cfg->method->dynamic) {
7375 /* Allocate the code into a separate memory pool so it can be freed */
7376 cfg->dynamic_info = g_new0 (MonoJitDynamicMethodInfo, 1);
7377 cfg->dynamic_info->code_mp = mono_code_manager_new_dynamic ();
7378 mono_domain_lock (cfg->domain);
7379 mono_dynamic_code_hash_insert (cfg->domain, cfg->method, cfg->dynamic_info);
7380 mono_domain_unlock (cfg->domain);
7382 code = mono_code_manager_reserve (cfg->dynamic_info->code_mp, cfg->code_size);
7384 mono_domain_lock (cfg->domain);
7385 code = mono_code_manager_reserve (cfg->domain->code_mp, cfg->code_size);
7386 mono_domain_unlock (cfg->domain);
7389 memcpy (code, cfg->native_code, cfg->code_len);
7390 g_free (cfg->native_code);
7391 cfg->native_code = code;
7392 code = cfg->native_code + cfg->code_len;
7394 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
7396 cfg->epilog_begin = cfg->code_len;
7398 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7399 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
7401 cfg->code_len = code - cfg->native_code;
7403 mono_arch_emit_epilog (cfg);
7405 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7406 switch (patch_info->type) {
7407 case MONO_PATCH_INFO_ABS: {
7408 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
7410 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
7411 if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) &&
7412 strstr (cfg->method->name, info->name))
7414 * This is an icall wrapper, and this is a call to the
7419 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
7420 patch_info->data.name = info->name;
7424 MonoVTable *vtable = mono_find_class_init_trampoline_by_addr (patch_info->data.target);
7426 patch_info->type = MONO_PATCH_INFO_CLASS_INIT;
7427 patch_info->data.klass = vtable->klass;
7432 case MONO_PATCH_INFO_SWITCH: {
7434 if (cfg->method->dynamic) {
7435 table = mono_code_manager_reserve (cfg->dynamic_info->code_mp, sizeof (gpointer) * patch_info->table_size);
7437 mono_domain_lock (cfg->domain);
7438 table = mono_code_manager_reserve (cfg->domain->code_mp, sizeof (gpointer) * patch_info->table_size);
7439 mono_domain_unlock (cfg->domain);
7442 patch_info->ip.i = patch_info->ip.label->inst_c0;
7443 for (i = 0; i < patch_info->table_size; i++) {
7444 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
7446 patch_info->data.target = table;
7455 if (cfg->verbose_level > 0)
7456 g_print ("Method %s emitted at %p to %p [%s]\n",
7457 mono_method_full_name (cfg->method, TRUE),
7458 cfg->native_code, cfg->native_code + cfg->code_len, cfg->domain->friendly_name);
7460 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info, cfg->run_cctors);
7462 if (cfg->method->dynamic) {
7463 mono_code_manager_commit (cfg->dynamic_info->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
7465 mono_domain_lock (cfg->domain);
7466 mono_code_manager_commit (cfg->domain->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
7467 mono_domain_unlock (cfg->domain);
7470 mono_arch_flush_icache (cfg->native_code, cfg->code_len);
7472 mono_debug_close_method (cfg);
7476 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
7481 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
7482 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
7484 if (cp->opcode == OP_ICONST) {
7485 if (cfg->opt & MONO_OPT_CONSPROP) {
7486 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
7490 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
7491 if (cfg->opt & MONO_OPT_COPYPROP) {
7492 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
7498 arity = mono_burg_arity [tree->opcode];
7501 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
7502 if (cfg->opt & MONO_OPT_CFOLD)
7503 mono_constant_fold_inst (tree, NULL);
7504 /* The opcode may have changed */
7505 if (mono_burg_arity [tree->opcode] > 1) {
7506 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
7507 if (cfg->opt & MONO_OPT_CFOLD)
7508 mono_constant_fold_inst (tree, NULL);
7510 mono_constant_fold_inst (tree, NULL);
7516 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
7520 switch (tree->opcode) {
7530 if (tree->ssa_op == MONO_SSA_NOP) {
7531 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7548 case OP_VOIDCALL_REG:
7549 case OP_VOIDCALLVIRT:
7551 MonoCallInst *call = (MonoCallInst *)tree;
7552 MonoMethodSignature *sig = call->signature;
7553 int i, byref = FALSE;
7555 for (i = 0; i < sig->param_count; i++) {
7556 if (sig->params [i]->byref) {
7563 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7571 arity = mono_burg_arity [tree->opcode];
7577 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7580 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7581 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
7584 g_assert_not_reached ();
7589 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
7591 MonoInst *tree = bb->code;
7597 for (; tree; tree = tree->next) {
7599 mono_cprop_copy_values (cfg, tree, acp);
7601 mono_cprop_invalidate_values (tree, acp, acp_size);
7603 if (tree->ssa_op == MONO_SSA_STORE &&
7604 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
7605 MonoInst *i1 = tree->inst_i1;
7607 acp [tree->inst_i0->inst_c0] = NULL;
7609 for (i = 0; i < acp_size; i++) {
7610 if (acp [i] && acp [i]->opcode != OP_ICONST &&
7611 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
7616 if (i1->opcode == OP_ICONST) {
7617 acp [tree->inst_i0->inst_c0] = i1;
7618 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
7620 if (i1->ssa_op == MONO_SSA_LOAD &&
7621 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
7622 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
7623 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
7624 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
7629 if (tree->opcode == CEE_BEQ) {
7630 g_assert (tree->inst_i0->opcode == OP_COMPARE);
7631 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
7632 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
7634 tree->opcode = CEE_BR;
7635 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
7636 tree->inst_target_bb = tree->inst_true_bb;
7638 tree->inst_target_bb = tree->inst_false_bb;
7647 mono_local_cprop (MonoCompile *cfg)
7652 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
7654 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7655 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
7656 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
7661 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, int parts)
7663 MonoMethodHeader *header = mono_method_get_header (method);
7664 guint8 *ip = (guint8 *)header->code;
7667 int dfn = 0, i, code_size_ratio;
7669 mono_jit_stats.methods_compiled++;
7670 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
7671 mono_profiler_method_jit (method);
7673 cfg = g_new0 (MonoCompile, 1);
7674 cfg->method = method;
7675 cfg->mempool = mono_mempool_new ();
7677 cfg->prof_options = mono_profiler_get_events ();
7678 cfg->run_cctors = run_cctors;
7679 cfg->bb_hash = g_hash_table_new (NULL, NULL);
7680 cfg->domain = domain;
7681 cfg->verbose_level = mini_verbose;
7682 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
7683 mono_method_get_header (method)->max_stack);
7685 if (cfg->verbose_level > 2)
7686 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
7689 * create MonoInst* which represents arguments and local variables
7691 mono_compile_create_vars (cfg);
7693 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
7694 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7695 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
7696 mono_destroy_compile (cfg);
7700 mono_jit_stats.basic_blocks += cfg->num_bblocks;
7701 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
7703 if (cfg->num_varinfo > 2000) {
7705 * we disable some optimizations if there are too many variables
7706 * because JIT time may become too expensive. The actual number needs
7707 * to be tweaked and eventually the non-linear algorithms should be fixed.
7709 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
7710 cfg->disable_ssa = TRUE;
7712 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
7714 /* Depth-first ordering on basic blocks */
7715 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
7717 if (cfg->opt & MONO_OPT_BRANCH)
7718 optimize_branches (cfg);
7720 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
7721 if (cfg->num_bblocks != dfn + 1) {
7724 cfg->num_bblocks = dfn + 1;
7726 if (!header->clauses) {
7727 /* remove unreachable code, because the code in them may be
7728 * inconsistent (access to dead variables for example) */
7729 for (bb = cfg->bb_entry; bb;) {
7730 MonoBasicBlock *bbn = bb->next_bb;
7732 if (bbn && bbn->region == -1 && !bbn->dfn) {
7733 if (cfg->verbose_level > 1)
7734 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
7735 bb->next_bb = bbn->next_bb;
7736 nullify_basic_block (bbn);
7744 if (cfg->opt & MONO_OPT_LOOP) {
7745 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
7746 mono_compute_natural_loops (cfg);
7749 /* after method_to_ir */
7753 //#define DEBUGSSA "logic_run"
7754 #define DEBUGSSA_CLASS "Tests"
7757 if (!header->num_clauses && !cfg->disable_ssa) {
7758 mono_local_cprop (cfg);
7759 mono_ssa_compute (cfg);
7763 /* fixme: add all optimizations which requires SSA */
7764 if (cfg->opt & (MONO_OPT_DEADCE | MONO_OPT_ABCREM)) {
7765 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
7766 mono_local_cprop (cfg);
7767 mono_ssa_compute (cfg);
7769 if (cfg->verbose_level >= 2) {
7776 /* after SSA translation */
7780 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
7781 if (cfg->comp_done & MONO_COMP_SSA) {
7782 mono_ssa_cprop (cfg);
7784 mono_local_cprop (cfg);
7788 if (cfg->comp_done & MONO_COMP_SSA) {
7789 mono_ssa_deadce (cfg);
7791 //mono_ssa_strength_reduction (cfg);
7793 if ((cfg->flags & MONO_CFG_HAS_LDELEMA) && (cfg->opt & MONO_OPT_ABCREM))
7794 mono_perform_abc_removal (cfg);
7796 mono_ssa_remove (cfg);
7798 if (cfg->opt & MONO_OPT_BRANCH)
7799 optimize_branches (cfg);
7802 /* after SSA removal */
7806 decompose_pass (cfg);
7808 if (cfg->opt & MONO_OPT_LINEARS) {
7811 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
7812 cfg->comp_done &= ~MONO_COMP_LIVENESS;
7813 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
7814 mono_analyze_liveness (cfg);
7816 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
7817 regs = mono_arch_get_global_int_regs (cfg);
7818 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
7822 //mono_print_code (cfg);
7826 /* variables are allocated after decompose, since decompose could create temps */
7827 mono_arch_allocate_vars (cfg);
7829 if (cfg->opt & MONO_OPT_CFOLD)
7830 mono_constant_fold (cfg);
7832 mini_select_instructions (cfg);
7835 if (cfg->verbose_level >= 2) {
7836 char *id = mono_method_full_name (cfg->method, FALSE);
7837 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
7841 if (cfg->method->dynamic)
7842 jinfo = g_new0 (MonoJitInfo, 1);
7844 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
7846 jinfo->method = method;
7847 jinfo->code_start = cfg->native_code;
7848 jinfo->code_size = cfg->code_len;
7849 jinfo->used_regs = cfg->used_int_regs;
7850 jinfo->domain_neutral = (cfg->opt & MONO_OPT_SHARED) != 0;
7852 if (header->num_clauses) {
7855 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
7856 jinfo->num_clauses = header->num_clauses;
7857 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
7858 sizeof (MonoJitExceptionInfo) * header->num_clauses);
7860 for (i = 0; i < header->num_clauses; i++) {
7861 MonoExceptionClause *ec = &header->clauses [i];
7862 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
7863 MonoBasicBlock *tblock;
7865 ei->flags = ec->flags;
7867 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7868 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->data.filter_offset);
7870 ei->data.filter = cfg->native_code + tblock->native_offset;
7872 ei->data.catch_class = ec->data.catch_class;
7875 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
7877 ei->try_start = cfg->native_code + tblock->native_offset;
7878 g_assert (tblock->native_offset);
7879 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
7881 ei->try_end = cfg->native_code + tblock->native_offset;
7882 g_assert (tblock->native_offset);
7883 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
7885 ei->handler_start = cfg->native_code + tblock->native_offset;
7889 cfg->jit_info = jinfo;
7891 mono_jit_info_table_add (cfg->domain, jinfo);
7893 if (cfg->method->dynamic)
7894 mono_dynamic_code_hash_lookup (cfg->domain, cfg->method)->ji = jinfo;
7896 /* collect statistics */
7897 mono_jit_stats.allocated_code_size += cfg->code_len;
7898 code_size_ratio = cfg->code_len;
7899 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
7900 mono_jit_stats.biggest_method_size = code_size_ratio;
7901 mono_jit_stats.biggest_method = method;
7903 code_size_ratio = (code_size_ratio * 100) / mono_method_get_header (method)->code_size;
7904 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
7905 mono_jit_stats.max_code_size_ratio = code_size_ratio;
7906 mono_jit_stats.max_ratio_method = method;
7908 mono_jit_stats.native_code_size += cfg->code_len;
7910 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7911 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
7917 mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain)
7920 GHashTable *jit_code_hash;
7921 gpointer code = NULL;
7927 jit_code_hash = target_domain->jit_code_hash;
7929 #ifdef MONO_USE_AOT_COMPILER
7930 if (!mono_compile_aot && (opt & MONO_OPT_AOT)) {
7932 MonoDomain *domain = mono_domain_get ();
7934 mono_domain_lock (domain);
7936 mono_class_init (method->klass);
7937 if ((info = mono_aot_get_method (domain, method))) {
7938 g_hash_table_insert (domain->jit_code_hash, method, info);
7939 mono_domain_unlock (domain);
7940 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
7941 return info->code_start;
7944 mono_domain_unlock (domain);
7948 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
7949 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
7951 MonoMethodPInvoke* piinfo = (MonoMethodPInvoke *) method;
7953 if (!piinfo->addr) {
7954 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
7955 piinfo->addr = mono_lookup_internal_call (method);
7957 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
7958 mono_lookup_pinvoke_call (method, NULL, NULL);
7960 nm = mono_marshal_get_native_wrapper (method);
7961 return mono_compile_method (nm);
7963 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
7964 //mono_debug_add_wrapper (method, nm);
7965 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
7966 const char *name = method->name;
7969 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
7970 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
7971 /* FIXME: uhm, we need a wrapper to handle exceptions? */
7972 return (gpointer)mono_delegate_ctor;
7973 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
7974 nm = mono_marshal_get_delegate_invoke (method);
7975 return mono_jit_compile_method (nm);
7976 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
7977 nm = mono_marshal_get_delegate_begin_invoke (method);
7978 return mono_jit_compile_method (nm);
7979 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
7980 nm = mono_marshal_get_delegate_end_invoke (method);
7981 return mono_jit_compile_method (nm);
7987 cfg = mini_method_compile (method, opt, target_domain, TRUE, 0);
7989 mono_domain_lock (target_domain);
7991 /* Check if some other thread already did the job. In this case, we can
7992 discard the code this thread generated. */
7994 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
7995 /* We can't use a domain specific method in another domain */
7996 if ((target_domain == mono_domain_get ()) || info->domain_neutral) {
7997 code = info->code_start;
7998 // printf("Discarding code for method %s\n", method->name);
8003 g_hash_table_insert (jit_code_hash, method, cfg->jit_info);
8004 code = cfg->native_code;
8007 mono_destroy_compile (cfg);
8009 if (target_domain->jump_target_hash) {
8010 MonoJumpInfo patch_info;
8012 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
8014 patch_info.next = NULL;
8015 patch_info.ip.i = 0;
8016 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
8017 patch_info.data.method = method;
8018 g_hash_table_remove (target_domain->jump_target_hash, method);
8020 for (tmp = list; tmp; tmp = tmp->next)
8021 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info, TRUE);
8022 g_slist_free (list);
8025 mono_domain_unlock (target_domain);
8027 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
8032 mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt)
8034 /* FIXME: later copy the code from mono */
8035 MonoDomain *target_domain, *domain = mono_domain_get ();
8039 if (opt & MONO_OPT_SHARED)
8040 target_domain = mono_get_root_domain ();
8042 target_domain = domain;
8044 mono_domain_lock (target_domain);
8046 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
8047 /* We can't use a domain specific method in another domain */
8048 if (! ((domain != target_domain) && !info->domain_neutral)) {
8049 mono_domain_unlock (target_domain);
8050 mono_jit_stats.methods_lookups++;
8051 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
8052 return info->code_start;
8056 mono_domain_unlock (target_domain);
8057 p = mono_jit_compile_method_inner (method, target_domain);
8062 mono_jit_compile_method (MonoMethod *method)
8064 return mono_jit_compile_method_with_opt (method, default_opt);
8068 invalidated_delegate_trampoline (MonoClass *klass)
8070 g_error ("Unmanaged code called delegate of type %s which was already garbage collected.\n"
8071 "See http://www.go-mono.com/delegate.html for an explanation and ways to fix this.",
8072 mono_type_full_name (&klass->byval_arg));
8076 * mono_jit_free_method:
8078 * Free all memory allocated by the JIT for METHOD.
8081 mono_jit_free_method (MonoDomain *domain, MonoMethod *method)
8083 MonoJitDynamicMethodInfo *ji;
8085 g_assert (method->dynamic);
8087 mono_domain_lock (domain);
8088 ji = mono_dynamic_code_hash_lookup (domain, method);
8089 mono_domain_unlock (domain);
8090 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
8091 /* FIXME: only enable this with a env var */
8092 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
8094 * Instead of freeing the code, change it to call an error routine
8095 * so people can fix their code.
8098 char *type = mono_type_full_name (&method->klass->byval_arg);
8099 char *type_and_method = g_strdup_printf ("%s.%s", type, method->name);
8102 mono_arch_invalidate_method (ji->ji, invalidated_delegate_trampoline, type_and_method);
8110 mono_domain_lock (domain);
8111 g_hash_table_remove (domain->dynamic_code_hash, ji);
8112 mono_domain_unlock (domain);
8114 mono_code_manager_destroy (ji->code_mp);
8115 mono_jit_info_table_remove (domain, ji->ji);
8121 mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method)
8123 MonoDomain *target_domain;
8126 if (default_opt & MONO_OPT_SHARED)
8127 target_domain = mono_get_root_domain ();
8129 target_domain = domain;
8131 mono_domain_lock (target_domain);
8133 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
8134 /* We can't use a domain specific method in another domain */
8135 if (! ((domain != target_domain) && !info->domain_neutral)) {
8136 mono_domain_unlock (target_domain);
8137 mono_jit_stats.methods_lookups++;
8138 return info->code_start;
8142 mono_domain_unlock (target_domain);
8148 * mono_jit_runtime_invoke:
8149 * @method: the method to invoke
8150 * @obj: this pointer
8151 * @params: array of parameter values.
8152 * @exc: used to catch exceptions objects
8155 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
8158 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc, void* compiled_method);
8159 void* compiled_method;
8161 if (obj == NULL && !(method->flags & METHOD_ATTRIBUTE_STATIC) && !method->string_ctor && (method->wrapper_type == 0)) {
8162 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
8166 invoke = mono_marshal_get_runtime_invoke (method);
8167 runtime_invoke = mono_jit_compile_method (invoke);
8169 /* We need this here becuase mono_marshal_get_runtime_invoke can be place
8170 * the helper method in System.Object and not the target class
8172 mono_runtime_class_init (mono_class_vtable (mono_domain_get (), method->klass));
8174 compiled_method = mono_jit_compile_method (method);
8175 return runtime_invoke (obj, params, exc, compiled_method);
8178 #ifdef PLATFORM_WIN32
8179 #define GET_CONTEXT \
8180 struct sigcontext *ctx = (struct sigcontext*)_dummy;
8183 #define GET_CONTEXT \
8184 void *ctx = context;
8185 #elif defined(sun) // Solaris x86
8186 #define GET_CONTEXT \
8187 ucontext_t *uctx = context; \
8188 struct sigcontext *ctx = (struct sigcontext *)&(uctx->uc_mcontext);
8189 #elif defined(__ppc__) || defined (__powerpc__) || defined (__s390__) || defined (MONO_ARCH_USE_SIGACTION)
8190 #define GET_CONTEXT \
8191 void *ctx = context;
8193 #define GET_CONTEXT \
8194 void **_p = (void **)&_dummy; \
8195 struct sigcontext *ctx = (struct sigcontext *)++_p;
8199 #ifdef MONO_ARCH_USE_SIGACTION
8200 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, siginfo_t *info, void *context)
8202 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy)
8206 SIG_HANDLER_SIGNATURE (sigfpe_signal_handler)
8208 MonoException *exc = NULL;
8209 #ifndef MONO_ARCH_USE_SIGACTION
8214 #if defined(MONO_ARCH_HAVE_IS_INT_OVERFLOW)
8215 if (mono_arch_is_int_overflow (ctx, info))
8216 exc = mono_get_exception_arithmetic ();
8218 exc = mono_get_exception_divide_by_zero ();
8220 exc = mono_get_exception_divide_by_zero ();
8223 mono_arch_handle_exception (ctx, exc, FALSE);
8227 SIG_HANDLER_SIGNATURE (sigill_signal_handler)
8231 exc = mono_get_exception_execution_engine ("SIGILL");
8233 mono_arch_handle_exception (ctx, exc, FALSE);
8236 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8239 sigsegv_signal_handler (int _dummy, siginfo_t *info, void *context)
8242 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
8243 struct sigcontext *ctx = (struct sigcontext *)&(((ucontext_t*)context)->uc_mcontext);
8245 /* Can't allocate memory using Boehm GC on altstack */
8246 if (jit_tls->stack_size &&
8247 ((guint8*)info->si_addr >= (guint8*)jit_tls->end_of_stack - jit_tls->stack_size) &&
8248 ((guint8*)info->si_addr < (guint8*)jit_tls->end_of_stack))
8249 exc = mono_domain_get ()->stack_overflow_ex;
8251 exc = mono_domain_get ()->null_reference_ex;
8253 mono_arch_handle_exception (ctx, exc, FALSE);
8259 SIG_HANDLER_SIGNATURE (sigsegv_signal_handler)
8263 mono_arch_handle_exception (ctx, NULL, FALSE);
8269 SIG_HANDLER_SIGNATURE (sigusr1_signal_handler)
8271 gboolean running_managed;
8276 running_managed = (mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx)) != NULL);
8278 exc = mono_thread_request_interruption (running_managed);
8281 mono_arch_handle_exception (ctx, exc, FALSE);
8285 SIG_HANDLER_SIGNATURE (sigquit_signal_handler)
8290 exc = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
8292 mono_arch_handle_exception (ctx, exc, FALSE);
8296 SIG_HANDLER_SIGNATURE (sigint_signal_handler)
8301 exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
8303 mono_arch_handle_exception (ctx, exc, FALSE);
8306 #ifndef PLATFORM_WIN32
8308 add_signal_handler (int signo, gpointer handler)
8310 struct sigaction sa;
8312 #ifdef MONO_ARCH_USE_SIGACTION
8313 sa.sa_sigaction = handler;
8314 sigemptyset (&sa.sa_mask);
8315 sa.sa_flags = SA_SIGINFO;
8317 sa.sa_handler = handler;
8318 sigemptyset (&sa.sa_mask);
8321 g_assert (sigaction (signo, &sa, NULL) != -1);
8326 mono_runtime_install_handlers (void)
8328 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8329 struct sigaction sa;
8332 #ifdef PLATFORM_WIN32
8334 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
8335 win32_seh_set_handler(SIGILL, sigill_signal_handler);
8336 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
8337 if (getenv ("MONO_DEBUG"))
8338 win32_seh_set_handler(SIGINT, sigint_signal_handler);
8339 #else /* !PLATFORM_WIN32 */
8341 /* libpthreads has its own implementation of sigaction(),
8342 * but it seems to work well with our current exception
8343 * handlers. If not we must call syscall directly instead
8346 if (getenv ("MONO_DEBUG")) {
8347 add_signal_handler (SIGINT, sigint_signal_handler);
8350 add_signal_handler (SIGFPE, sigfpe_signal_handler);
8351 add_signal_handler (SIGQUIT, sigquit_signal_handler);
8352 add_signal_handler (SIGILL, sigill_signal_handler);
8353 add_signal_handler (SIGBUS, sigsegv_signal_handler);
8354 add_signal_handler (mono_thread_get_abort_signal (), sigusr1_signal_handler);
8357 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8358 sa.sa_sigaction = sigsegv_signal_handler;
8359 sigemptyset (&sa.sa_mask);
8360 sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
8361 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
8363 add_signal_handler (SIGSEGV, sigsegv_signal_handler);
8366 #endif /* PLATFORM_WIN32 */
8369 /* mono_jit_create_remoting_trampoline:
8370 * @method: pointer to the method info
8372 * Creates a trampoline which calls the remoting functions. This
8373 * is used in the vtable of transparent proxies.
8375 * Returns: a pointer to the newly created code
8378 mono_jit_create_remoting_trampoline (MonoMethod *method, MonoRemotingTarget target)
8381 guint8 *addr = NULL;
8383 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
8384 (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
8385 nm = mono_marshal_get_remoting_invoke_for_target (method, target);
8386 addr = mono_compile_method (nm);
8388 addr = mono_compile_method (method);
8394 mini_init (const char *filename)
8398 mono_arch_cpu_init ();
8400 if (!g_thread_supported ())
8401 g_thread_init (NULL);
8403 MONO_GC_PRE_INIT ();
8405 mono_jit_tls_id = TlsAlloc ();
8406 setup_jit_tls_data ((gpointer)-1, mono_thread_abort);
8408 InitializeCriticalSection (&jit_mutex);
8412 if (default_opt & MONO_OPT_AOT)
8415 mono_runtime_install_handlers ();
8416 mono_threads_install_cleanup (mini_thread_cleanup);
8418 #define JIT_TRAMPOLINES_WORK
8419 #ifdef JIT_TRAMPOLINES_WORK
8420 mono_install_compile_method (mono_jit_compile_method);
8421 mono_install_free_method (mono_jit_free_method);
8422 mono_install_trampoline (mono_create_jit_trampoline);
8423 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
8425 #define JIT_INVOKE_WORKS
8426 #ifdef JIT_INVOKE_WORKS
8427 mono_install_runtime_invoke (mono_jit_runtime_invoke);
8428 mono_install_handler (mono_arch_get_throw_exception ());
8430 mono_install_stack_walk (mono_jit_walk_stack);
8432 domain = mono_init_from_assembly (filename, filename);
8435 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
8436 ves_icall_get_frame_info);
8437 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
8438 ves_icall_get_trace);
8439 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
8440 mono_runtime_install_handlers);
8443 create_helper_signature ();
8445 #define JIT_CALLS_WORK
8446 #ifdef JIT_CALLS_WORK
8447 /* Needs to be called here since register_jit_icall depends on it */
8448 mono_marshal_init ();
8450 mono_arch_register_lowlevel_calls ();
8451 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
8452 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
8453 mono_register_jit_icall (mono_trace_enter_method, "mono_trace_enter_method", NULL, TRUE);
8454 mono_register_jit_icall (mono_trace_leave_method, "mono_trace_leave_method", NULL, TRUE);
8455 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
8456 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
8458 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
8459 mono_register_jit_icall (mono_arch_get_rethrow_exception (), "mono_arch_rethrow_exception", helper_sig_void_obj, TRUE);
8460 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
8461 helper_sig_void_ptr, TRUE);
8462 mono_register_jit_icall (mono_thread_get_pending_exception, "mono_thread_get_pending_exception", helper_sig_obj_void, FALSE);
8463 mono_register_jit_icall (mono_thread_interruption_checkpoint, "mono_thread_interruption_checkpoint", helper_sig_void_void, FALSE);
8464 mono_register_jit_icall (mono_load_remote_field_new, "mono_load_remote_field_new", helper_sig_obj_obj_ptr_ptr, FALSE);
8465 mono_register_jit_icall (mono_store_remote_field_new, "mono_store_remote_field_new", helper_sig_void_obj_ptr_ptr_obj, FALSE);
8468 * NOTE, NOTE, NOTE, NOTE:
8469 * when adding emulation for some opcodes, remember to also add a dummy
8470 * rule to the burg files, because we need the arity information to be correct.
8472 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult, TRUE);
8473 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un, FALSE);
8474 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf, FALSE);
8475 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv, FALSE);
8476 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un, FALSE);
8477 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem, FALSE);
8478 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un, FALSE);
8480 #ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
8481 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl, TRUE);
8482 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr, TRUE);
8483 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un, TRUE);
8486 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8, FALSE);
8487 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4, FALSE);
8488 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8, FALSE);
8489 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8, FALSE);
8491 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
8492 mono_register_opcode_emulation (OP_FCONV_TO_I8, "__emul_fconv_to_i8", helper_sig_long_double, mono_fconv_i8, FALSE);
8494 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
8495 mono_register_opcode_emulation (CEE_CONV_R_UN, "__emul_conv_r_un", helper_sig_double_int, mono_conv_to_r8_un, FALSE);
8497 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
8498 mono_register_opcode_emulation (OP_LCONV_TO_R8, "__emul_lconv_to_r8", helper_sig_double_long, mono_lconv_to_r8, FALSE);
8500 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
8501 mono_register_opcode_emulation (OP_LCONV_TO_R4, "__emul_lconv_to_r4", helper_sig_float_long, mono_lconv_to_r4, FALSE);
8503 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
8504 mono_register_opcode_emulation (OP_LCONV_TO_R_UN, "__emul_lconv_to_r8_un", helper_sig_double_long, mono_lconv_to_r8_un, FALSE);
8506 #ifdef MONO_ARCH_EMULATE_FREM
8507 mono_register_opcode_emulation (OP_FREM, "__emul_frem", helper_sig_double_double_double, fmod, FALSE);
8510 #if SIZEOF_VOID_P == 4
8511 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4, TRUE);
8514 #warning "fixme: add opcode emulation"
8518 /* other jit icalls */
8519 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
8520 helper_sig_ptr_ptr_ptr, FALSE);
8521 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
8522 mono_register_jit_icall (mono_get_special_static_data, "mono_get_special_static_data", helper_sig_ptr_int, FALSE);
8523 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
8524 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
8525 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
8526 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
8527 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
8528 mono_register_jit_icall (helper_stelem_ref_check, "helper_stelem_ref_check", helper_sig_stelem_ref_check, FALSE);
8529 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
8530 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
8531 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
8532 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
8533 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
8534 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
8535 mono_register_jit_icall (mono_ldftn_nosync, "mono_ldftn_nosync", helper_sig_compile, FALSE);
8536 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
8539 #define JIT_RUNTIME_WORKS
8540 #ifdef JIT_RUNTIME_WORKS
8541 mono_runtime_install_cleanup ((MonoDomainFunc)mini_cleanup);
8542 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
8545 mono_thread_attach (domain);
8549 MonoJitStats mono_jit_stats = {0};
8552 print_jit_stats (void)
8554 if (mono_jit_stats.enabled) {
8555 g_print ("Mono Jit statistics\n");
8556 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
8557 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
8558 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
8559 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
8560 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
8561 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
8562 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
8563 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
8564 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
8565 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
8566 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
8567 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
8568 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
8569 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
8570 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
8571 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
8572 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
8573 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
8575 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
8576 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
8577 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
8578 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
8579 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
8581 g_print ("\nGeneric instances: %ld\n", mono_stats.generic_instance_count);
8582 g_print ("Inflated methods: %ld\n", mono_stats.inflated_method_count);
8583 g_print ("Inflated types: %ld\n", mono_stats.inflated_type_count);
8584 g_print ("Generics metadata size: %ld\n", mono_stats.generics_metadata_size);
8589 mini_cleanup (MonoDomain *domain)
8592 * mono_runtime_cleanup() and mono_domain_finalize () need to
8593 * be called early since they need the execution engine still
8594 * fully working (mono_domain_finalize may invoke managed finalizers
8595 * and mono_runtime_cleanup will wait for other threads to finish).
8597 mono_domain_finalize (domain, 2000);
8599 mono_runtime_cleanup (domain);
8601 mono_profiler_shutdown ();
8603 mono_debug_cleanup ();
8605 #ifdef PLATFORM_WIN32
8606 win32_seh_cleanup();
8609 mono_domain_free (domain, TRUE);
8615 mono_set_defaults (int verbose_level, guint32 opts)
8617 mini_verbose = verbose_level;
8622 mono_precompile_assembly (MonoAssembly *ass, void *user_data)
8624 MonoImage *image = mono_assembly_get_image (ass);
8625 MonoMethod *method, *invoke;
8628 if (mini_verbose > 0)
8629 printf ("PRECOMPILE: %s.\n", mono_image_get_filename (image));
8631 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
8632 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
8633 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
8637 if (mini_verbose > 1) {
8638 char * desc = mono_method_full_name (method, TRUE);
8639 g_print ("Compiling %d %s\n", count, desc);
8642 mono_compile_method (method);
8643 if (strcmp (method->name, "Finalize") == 0) {
8644 invoke = mono_marshal_get_runtime_invoke (method);
8645 mono_compile_method (invoke);
8647 if (method->klass->marshalbyref && method->signature->hasthis) {
8648 invoke = mono_marshal_get_remoting_invoke_with_check (method);
8649 mono_compile_method (invoke);
8654 void mono_precompile_assemblies ()
8656 mono_assembly_foreach ((GFunc)mono_precompile_assembly, NULL);