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->has_type_parameters);
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 if (store->opcode == CEE_STOBJ) {
3268 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
3269 handle_stobj (cfg, bblock, store, ins, ins->cil_code, store->klass, TRUE, FALSE);
3271 MONO_ADD_INS (bblock, store);
3272 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3275 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3285 MONO_INST_NEW (cfg, ins, CEE_POP);
3286 MONO_ADD_INS (bblock, ins);
3287 ins->cil_code = ip++;
3293 if (stack_start != sp)
3295 MONO_INST_NEW (cfg, ins, CEE_JMP);
3296 token = read32 (ip + 1);
3297 /* FIXME: check the signature matches */
3298 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3299 ins->inst_p0 = cmethod;
3300 MONO_ADD_INS (bblock, ins);
3302 start_new_bblock = 1;
3306 case CEE_CALLVIRT: {
3307 MonoInst *addr = NULL;
3308 MonoMethodSignature *fsig = NULL;
3309 int temp, array_rank = 0;
3310 int virtual = *ip == CEE_CALLVIRT;
3313 token = read32 (ip + 1);
3315 if (*ip == CEE_CALLI) {
3320 if (method->wrapper_type != MONO_WRAPPER_NONE)
3321 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
3323 fsig = mono_metadata_parse_signature (image, token);
3325 n = fsig->param_count + fsig->hasthis;
3327 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3328 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
3329 } else if (constrained_call) {
3330 cmethod = mono_get_method_constrained (image, token, constrained_call, generic_context);
3332 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3337 if (!cmethod->klass->inited)
3338 mono_class_init (cmethod->klass);
3340 if (cmethod->signature->pinvoke) {
3341 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
3342 fsig = wrapper->signature;
3344 fsig = mono_method_get_signature (cmethod, image, token);
3347 n = fsig->param_count + fsig->hasthis;
3349 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
3350 cmethod->klass->parent == mono_defaults.array_class) {
3351 array_rank = cmethod->klass->rank;
3354 if (cmethod->string_ctor)
3355 g_assert_not_reached ();
3359 if (cmethod && cmethod->klass->generic_container)
3364 //g_assert (!virtual || fsig->hasthis);
3368 if (constrained_call) {
3370 * We have the `constrained.' prefix opcode.
3372 if (constrained_call->valuetype && !cmethod->klass->valuetype) {
3375 * The type parameter is instantiated as a valuetype,
3376 * but that type doesn't override the method we're
3377 * calling, so we need to box `this'.
3378 * sp [0] is a pointer to the data: we need the value
3379 * in handle_box (), so load it here.
3381 MONO_INST_NEW (cfg, load, mono_type_to_ldind (&constrained_call->byval_arg));
3382 type_to_eval_stack_type (&constrained_call->byval_arg, load);
3383 load->cil_code = ip;
3384 load->inst_left = sp [0];
3385 sp [0] = handle_box (cfg, bblock, load, ip, constrained_call);
3386 } else if (!constrained_call->valuetype) {
3390 * The type parameter is instantiated as a reference
3391 * type. We have a managed pointer on the stack, so
3392 * we need to dereference it here.
3395 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3397 ins->inst_i0 = sp [0];
3398 ins->type = STACK_OBJ;
3400 } else if (cmethod->klass->valuetype)
3402 constrained_call = NULL;
3405 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
3408 if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL) && (mono_metadata_signature_equal (method->signature, cmethod->signature))) {
3410 /* FIXME: This assumes the two methods has the same number and type of arguments */
3411 for (i = 0; i < n; ++i) {
3412 /* Check if argument is the same */
3413 NEW_ARGLOAD (cfg, ins, i);
3414 if ((ins->opcode == sp [i]->opcode) && (ins->inst_i0 == sp [i]->inst_i0))
3417 /* Prevent argument from being register allocated */
3418 arg_array [i]->flags |= MONO_INST_VOLATILE;
3419 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3421 if (ins->opcode == CEE_STOBJ) {
3422 NEW_ARGLOADA (cfg, ins, i);
3423 handle_stobj (cfg, bblock, ins, sp [i], sp [i]->cil_code, ins->klass, FALSE, FALSE);
3426 MONO_ADD_INS (bblock, ins);
3428 MONO_INST_NEW (cfg, ins, CEE_JMP);
3430 ins->inst_p0 = cmethod;
3431 ins->inst_p1 = arg_array [0];
3432 MONO_ADD_INS (bblock, ins);
3433 start_new_bblock = 1;
3434 /* skip CEE_RET as well */
3439 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_opcode_for_method (cfg, cmethod, fsig, sp))) {
3442 if (MONO_TYPE_IS_VOID (fsig->ret)) {
3443 MONO_ADD_INS (bblock, ins);
3445 type_to_eval_stack_type (fsig->ret, ins);
3454 handle_loaded_temps (cfg, bblock, stack_start, sp);
3456 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3457 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
3458 mono_method_check_inlining (cfg, cmethod) &&
3459 !g_list_find (dont_inline, cmethod)) {
3461 MonoBasicBlock *ebblock;
3462 gboolean allways = FALSE;
3464 if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
3465 (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
3466 cmethod = mono_marshal_get_native_wrapper (cmethod);
3470 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, allways))) {
3474 GET_BBLOCK (cfg, bbhash, bblock, ip);
3475 ebblock->next_bb = bblock;
3476 link_bblock (cfg, ebblock, bblock);
3478 if (!MONO_TYPE_IS_VOID (fsig->ret))
3481 /* indicates start of a new block, and triggers a load of all
3482 stack arguments at bb boundarie */
3485 inline_costs += costs;
3490 inline_costs += 10 * num_calls++;
3492 /* tail recursion elimination */
3493 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == method && ip [5] == CEE_RET) {
3494 gboolean has_vtargs = FALSE;
3497 /* keep it simple */
3498 for (i = fsig->param_count - 1; i >= 0; i--) {
3499 if (MONO_TYPE_ISSTRUCT (cmethod->signature->params [i]))
3504 for (i = 0; i < n; ++i) {
3505 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3507 MONO_ADD_INS (bblock, ins);
3509 MONO_INST_NEW (cfg, ins, CEE_BR);
3511 MONO_ADD_INS (bblock, ins);
3512 tblock = start_bblock->out_bb [0];
3513 link_bblock (cfg, bblock, tblock);
3514 ins->inst_target_bb = tblock;
3515 start_new_bblock = 1;
3518 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3519 /* just create a dummy - the value is never used */
3520 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
3521 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3529 if (*ip == CEE_CALLI) {
3531 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
3532 NEW_TEMPLOAD (cfg, *sp, temp);
3536 } else if (array_rank) {
3539 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
3540 if (sp [fsig->param_count]->type == STACK_OBJ) {
3541 MonoInst *iargs [2];
3542 MonoInst *array, *to_store, *store;
3544 handle_loaded_temps (cfg, bblock, stack_start, sp);
3546 array = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
3547 NEW_TEMPSTORE (cfg, store, array->inst_c0, sp [0]);
3548 store->cil_code = ip;
3549 MONO_ADD_INS (bblock, store);
3550 NEW_TEMPLOAD (cfg, iargs [0], array->inst_c0);
3552 to_store = mono_compile_create_var (cfg, type_from_stack_type (sp [fsig->param_count]), OP_LOCAL);
3553 NEW_TEMPSTORE (cfg, store, to_store->inst_c0, sp [fsig->param_count]);
3554 store->cil_code = ip;
3555 MONO_ADD_INS (bblock, store);
3556 NEW_TEMPLOAD (cfg, iargs [1], to_store->inst_c0);
3559 * We first save the args for the call so that the args are copied to the stack
3560 * and a new instruction tree for them is created. If we don't do this,
3561 * the same MonoInst is added to two different trees and this is not
3564 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref_check, iargs, ip);
3566 NEW_TEMPLOAD (cfg, sp [0], array->inst_c0);
3567 NEW_TEMPLOAD (cfg, sp [fsig->param_count], to_store->inst_c0);
3570 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
3571 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
3573 if (ins->opcode == CEE_STOBJ) {
3574 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
3576 MONO_ADD_INS (bblock, ins);
3579 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
3580 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3581 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
3585 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
3586 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3589 g_assert_not_reached ();
3593 if (0 && CODE_IS_STLOC (ip + 5) && (!MONO_TYPE_ISSTRUCT (fsig->ret)) && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)) {
3594 /* no need to spill */
3595 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
3598 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) {
3599 NEW_TEMPLOAD (cfg, *sp, temp);
3609 if (cfg->method != method) {
3610 /* return from inlined methode */
3615 //g_assert (returnvar != -1);
3616 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
3617 store->cil_code = sp [0]->cil_code;
3618 if (store->opcode == CEE_STOBJ) {
3619 g_assert_not_reached ();
3620 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
3621 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
3623 MONO_ADD_INS (bblock, store);
3627 g_assert (!return_var);
3630 MONO_INST_NEW (cfg, ins, CEE_NOP);
3631 ins->opcode = mono_type_to_stind (method->signature->ret);
3632 if (ins->opcode == CEE_STOBJ) {
3633 NEW_RETLOADA (cfg, ins);
3634 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3636 ins->opcode = OP_SETRET;
3638 ins->inst_i0 = *sp;;
3639 ins->inst_i1 = NULL;
3640 MONO_ADD_INS (bblock, ins);
3644 if (sp != stack_start)
3646 MONO_INST_NEW (cfg, ins, CEE_BR);
3647 ins->cil_code = ip++;
3648 ins->inst_target_bb = end_bblock;
3649 MONO_ADD_INS (bblock, ins);
3650 link_bblock (cfg, bblock, end_bblock);
3651 start_new_bblock = 1;
3655 MONO_INST_NEW (cfg, ins, CEE_BR);
3656 ins->cil_code = ip++;
3657 MONO_ADD_INS (bblock, ins);
3658 target = ip + 1 + (signed char)(*ip);
3660 GET_BBLOCK (cfg, bbhash, tblock, target);
3661 link_bblock (cfg, bblock, tblock);
3662 CHECK_BBLOCK (target, ip, tblock);
3663 ins->inst_target_bb = tblock;
3664 if (sp != stack_start) {
3665 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3668 start_new_bblock = 1;
3675 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3676 ins->cil_code = ip++;
3677 target = ip + 1 + *(signed char*)ip;
3679 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
3680 if (sp != stack_start) {
3681 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3698 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3699 ins->cil_code = ip++;
3700 target = ip + 1 + *(signed char*)ip;
3703 if (sp != stack_start) {
3704 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3711 MONO_INST_NEW (cfg, ins, CEE_BR);
3712 ins->cil_code = ip++;
3713 MONO_ADD_INS (bblock, ins);
3714 target = ip + 4 + (gint32)read32(ip);
3716 GET_BBLOCK (cfg, bbhash, tblock, target);
3717 link_bblock (cfg, bblock, tblock);
3718 CHECK_BBLOCK (target, ip, tblock);
3719 ins->inst_target_bb = tblock;
3720 if (sp != stack_start) {
3721 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3724 start_new_bblock = 1;
3731 MONO_INST_NEW (cfg, ins, *ip);
3732 ins->cil_code = ip++;
3733 target = ip + 4 + (gint32)read32(ip);
3735 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
3736 if (sp != stack_start) {
3737 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3754 MONO_INST_NEW (cfg, ins, *ip);
3755 ins->cil_code = ip++;
3756 target = ip + 4 + (gint32)read32(ip);
3759 if (sp != stack_start) {
3760 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3768 n = read32 (ip + 1);
3769 MONO_INST_NEW (cfg, ins, *ip);
3771 ins->inst_left = *sp;
3772 if (ins->inst_left->type != STACK_I4) goto unverified;
3775 CHECK_OPSIZE (n * sizeof (guint32));
3776 target = ip + n * sizeof (guint32);
3777 MONO_ADD_INS (bblock, ins);
3778 GET_BBLOCK (cfg, bbhash, tblock, target);
3779 link_bblock (cfg, bblock, tblock);
3780 ins->klass = GUINT_TO_POINTER (n);
3781 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
3782 ins->inst_many_bb [n] = tblock;
3784 for (i = 0; i < n; ++i) {
3785 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
3786 link_bblock (cfg, bblock, tblock);
3787 ins->inst_many_bb [i] = tblock;
3790 if (sp != stack_start) {
3791 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3808 MONO_INST_NEW (cfg, ins, *ip);
3813 ins->type = ldind_type [*ip - CEE_LDIND_I1];
3814 ins->flags |= ins_flag;
3826 MONO_INST_NEW (cfg, ins, *ip);
3827 ins->cil_code = ip++;
3829 handle_loaded_temps (cfg, bblock, stack_start, sp);
3830 MONO_ADD_INS (bblock, ins);
3831 ins->inst_i0 = sp [0];
3832 ins->inst_i1 = sp [1];
3833 ins->flags |= ins_flag;
3852 /* special case that gives a nice speedup and happens to workaorund a ppc jit but (for the release)
3853 * later apply the speedup to the left shift as well
3856 if ((ins->opcode == OP_LSHR_UN) && (ins->type == STACK_I8)
3857 && (ins->inst_right->opcode == OP_ICONST) && (ins->inst_right->inst_c0 == 32)) {
3858 ins->opcode = OP_LONG_SHRUN_32;
3859 /*g_print ("applied long shr speedup to %s\n", cfg->method->name);*/
3863 if (mono_find_jit_opcode_emulation (ins->opcode)) {
3865 *sp++ = emit_tree (cfg, bblock, ins);
3879 case CEE_CONV_OVF_I8:
3880 case CEE_CONV_OVF_U8:
3884 if (mono_find_jit_opcode_emulation (ins->opcode)) {
3886 *sp++ = emit_tree (cfg, bblock, ins);
3890 case CEE_CONV_OVF_I4:
3891 case CEE_CONV_OVF_I1:
3892 case CEE_CONV_OVF_I2:
3893 case CEE_CONV_OVF_I:
3894 case CEE_CONV_OVF_U:
3897 if (sp [-1]->type == STACK_R8) {
3898 ADD_UNOP (CEE_CONV_OVF_I8);
3906 case CEE_CONV_OVF_U1:
3907 case CEE_CONV_OVF_U2:
3908 case CEE_CONV_OVF_U4:
3911 if (sp [-1]->type == STACK_R8) {
3912 ADD_UNOP (CEE_CONV_OVF_U8);
3920 case CEE_CONV_OVF_I1_UN:
3921 case CEE_CONV_OVF_I2_UN:
3922 case CEE_CONV_OVF_I4_UN:
3923 case CEE_CONV_OVF_I8_UN:
3924 case CEE_CONV_OVF_U1_UN:
3925 case CEE_CONV_OVF_U2_UN:
3926 case CEE_CONV_OVF_U4_UN:
3927 case CEE_CONV_OVF_U8_UN:
3928 case CEE_CONV_OVF_I_UN:
3929 case CEE_CONV_OVF_U_UN:
3937 token = read32 (ip + 1);
3938 if (method->wrapper_type != MONO_WRAPPER_NONE)
3939 klass = mono_method_get_wrapper_data (method, token);
3941 klass = mono_class_get_full (image, token, generic_context);
3943 mono_class_init (klass);
3945 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3946 MonoInst *store, *load;
3947 MONO_INST_NEW (cfg, load, CEE_LDIND_REF);
3948 load->cil_code = ip;
3949 load->inst_i0 = sp [1];
3950 load->type = STACK_OBJ;
3951 load->flags |= ins_flag;
3952 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
3953 store->cil_code = ip;
3954 handle_loaded_temps (cfg, bblock, stack_start, sp);
3955 MONO_ADD_INS (bblock, store);
3956 store->inst_i0 = sp [0];
3957 store->inst_i1 = load;
3958 store->flags |= ins_flag;
3960 n = mono_class_value_size (klass, NULL);
3961 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3963 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3964 copy->inst_left = sp [0];
3965 copy->inst_right = sp [1];
3966 copy->cil_code = ip;
3968 MONO_ADD_INS (bblock, copy);
3970 MonoInst *iargs [3];
3973 NEW_ICONST (cfg, iargs [2], n);
3974 iargs [2]->cil_code = ip;
3976 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3983 MonoInst *iargs [3];
3987 token = read32 (ip + 1);
3988 if (method->wrapper_type != MONO_WRAPPER_NONE)
3989 klass = mono_method_get_wrapper_data (method, token);
3991 klass = mono_class_get_full (image, token, generic_context);
3993 mono_class_init (klass);
3994 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3995 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3997 ins->inst_i0 = sp [0];
3998 ins->type = STACK_OBJ;
3999 ins->flags |= ins_flag;
4005 n = mono_class_value_size (klass, NULL);
4006 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
4007 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
4008 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
4010 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
4011 copy->inst_left = iargs [0];
4012 copy->inst_right = *sp;
4013 copy->cil_code = ip;
4015 MONO_ADD_INS (bblock, copy);
4018 NEW_ICONST (cfg, iargs [2], n);
4019 iargs [2]->cil_code = ip;
4021 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
4023 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
4031 CHECK_STACK_OVF (1);
4033 n = read32 (ip + 1);
4035 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
4036 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, n));
4038 ins->type = STACK_OBJ;
4041 else if (method->wrapper_type != MONO_WRAPPER_NONE) {
4043 MonoInst *iargs [1];
4045 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
4046 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
4047 NEW_TEMPLOAD (cfg, *sp, temp);
4051 if (cfg->opt & MONO_OPT_SHARED) {
4053 MonoInst *iargs [3];
4055 if (mono_compile_aot) {
4056 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n);
4059 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
4060 NEW_IMAGECONST (cfg, iargs [1], image);
4061 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
4062 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
4063 NEW_TEMPLOAD (cfg, *sp, temp);
4064 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
4066 if (mono_compile_aot)
4067 NEW_LDSTRCONST (cfg, ins, image, n);
4069 NEW_PCONST (cfg, ins, NULL);
4071 ins->type = STACK_OBJ;
4072 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
4082 MonoInst *iargs [2];
4083 MonoMethodSignature *fsig;
4087 token = read32 (ip + 1);
4088 if (method->wrapper_type != MONO_WRAPPER_NONE) {
4089 cmethod = mono_method_get_wrapper_data (method, token);
4091 cmethod = mono_get_method_full (image, token, NULL, generic_context);
4092 fsig = mono_method_get_signature (cmethod, image, token);
4094 mono_class_init (cmethod->klass);
4096 n = fsig->param_count;
4099 /* move the args to allow room for 'this' in the first position */
4105 handle_loaded_temps (cfg, bblock, stack_start, sp);
4108 if (cmethod->klass->parent == mono_defaults.array_class) {
4109 NEW_METHODCONST (cfg, *sp, cmethod);
4110 temp = handle_array_new (cfg, bblock, fsig->param_count, sp, ip);
4111 } else if (cmethod->string_ctor) {
4112 /* we simply pass a null pointer */
4113 NEW_PCONST (cfg, *sp, NULL);
4114 /* now call the string ctor */
4115 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
4117 if (cmethod->klass->valuetype) {
4118 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
4119 temp = iargs [0]->inst_c0;
4120 NEW_TEMPLOADA (cfg, *sp, temp);
4122 temp = handle_alloc (cfg, bblock, cmethod->klass, ip);
4123 NEW_TEMPLOAD (cfg, *sp, temp);
4126 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
4127 mono_method_check_inlining (cfg, cmethod) &&
4128 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
4129 !g_list_find (dont_inline, cmethod)) {
4131 MonoBasicBlock *ebblock;
4132 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, FALSE))) {
4137 GET_BBLOCK (cfg, bbhash, bblock, ip);
4138 ebblock->next_bb = bblock;
4139 link_bblock (cfg, ebblock, bblock);
4141 NEW_TEMPLOAD (cfg, *sp, temp);
4144 /* indicates start of a new block, and triggers a load
4145 of all stack arguments at bb boundarie */
4148 inline_costs += costs;
4152 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
4155 /* now call the actual ctor */
4156 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
4160 NEW_TEMPLOAD (cfg, *sp, temp);
4171 token = read32 (ip + 1);
4172 if (method->wrapper_type != MONO_WRAPPER_NONE)
4173 klass = mono_method_get_wrapper_data (method, token);
4175 klass = mono_class_get_full (image, token, generic_context);
4176 mono_class_init (klass);
4178 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4180 MonoMethod *mono_isinst;
4181 MonoInst *iargs [1];
4182 MonoBasicBlock *ebblock;
4186 mono_isinst = mono_marshal_get_isinst (klass);
4189 costs = inline_method (cfg, mono_isinst, mono_isinst->signature, bblock,
4190 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4192 g_assert (costs > 0);
4197 GET_BBLOCK (cfg, bbhash, bblock, ip);
4198 ebblock->next_bb = bblock;
4199 link_bblock (cfg, ebblock, bblock);
4201 temp = iargs [0]->inst_i0->inst_c0;
4202 NEW_TEMPLOAD (cfg, *sp, temp);
4206 inline_costs += costs;
4210 MONO_INST_NEW (cfg, ins, *ip);
4211 ins->type = STACK_OBJ;
4212 ins->inst_left = *sp;
4213 ins->inst_newa_class = klass;
4215 *sp++ = emit_tree (cfg, bblock, ins);
4219 case CEE_UNBOX_ANY: {
4220 MonoInst *add, *vtoffset;
4221 MonoInst *iargs [3];
4226 token = read32 (ip + 1);
4227 if (method->wrapper_type != MONO_WRAPPER_NONE)
4228 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4230 klass = mono_class_get_full (image, token, generic_context);
4231 mono_class_init (klass);
4233 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4235 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4236 MonoMethod *mono_castclass;
4237 MonoInst *iargs [1];
4238 MonoBasicBlock *ebblock;
4242 mono_castclass = mono_marshal_get_castclass (klass);
4245 costs = inline_method (cfg, mono_castclass, mono_castclass->signature, bblock,
4246 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4248 g_assert (costs > 0);
4253 GET_BBLOCK (cfg, bbhash, bblock, ip);
4254 ebblock->next_bb = bblock;
4255 link_bblock (cfg, ebblock, bblock);
4257 temp = iargs [0]->inst_i0->inst_c0;
4258 NEW_TEMPLOAD (cfg, *sp, temp);
4262 inline_costs += costs;
4265 MONO_INST_NEW (cfg, ins, CEE_CASTCLASS);
4266 ins->type = STACK_OBJ;
4267 ins->inst_left = *sp;
4269 ins->inst_newa_class = klass;
4277 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
4278 ins->type = STACK_OBJ;
4279 ins->inst_left = *sp;
4281 ins->inst_newa_class = klass;
4284 MONO_INST_NEW (cfg, add, OP_PADD);
4285 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4286 add->inst_left = ins;
4287 add->inst_right = vtoffset;
4288 add->type = STACK_MP;
4292 n = mono_class_value_size (klass, NULL);
4293 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
4294 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
4295 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
4297 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
4298 copy->inst_left = iargs [0];
4299 copy->inst_right = *sp;
4300 copy->cil_code = ip;
4302 MONO_ADD_INS (bblock, copy);
4305 NEW_ICONST (cfg, iargs [2], n);
4306 iargs [2]->cil_code = ip;
4308 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
4310 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
4316 MonoInst *add, *vtoffset;
4321 token = read32 (ip + 1);
4322 if (method->wrapper_type != MONO_WRAPPER_NONE)
4323 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4325 klass = mono_class_get_full (image, token, generic_context);
4326 mono_class_init (klass);
4328 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
4329 ins->type = STACK_OBJ;
4330 ins->inst_left = *sp;
4332 ins->inst_newa_class = klass;
4335 MONO_INST_NEW (cfg, add, OP_PADD);
4336 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4337 add->inst_left = ins;
4338 add->inst_right = vtoffset;
4339 add->type = STACK_MP;
4349 token = read32 (ip + 1);
4350 if (method->wrapper_type != MONO_WRAPPER_NONE)
4351 klass = mono_method_get_wrapper_data (method, token);
4353 klass = mono_class_get_full (image, token, generic_context);
4354 mono_class_init (klass);
4356 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4358 MonoMethod *mono_castclass;
4359 MonoInst *iargs [1];
4360 MonoBasicBlock *ebblock;
4364 mono_castclass = mono_marshal_get_castclass (klass);
4367 costs = inline_method (cfg, mono_castclass, mono_castclass->signature, bblock,
4368 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4370 g_assert (costs > 0);
4375 GET_BBLOCK (cfg, bbhash, bblock, ip);
4376 ebblock->next_bb = bblock;
4377 link_bblock (cfg, ebblock, bblock);
4379 temp = iargs [0]->inst_i0->inst_c0;
4380 NEW_TEMPLOAD (cfg, *sp, temp);
4384 inline_costs += costs;
4387 MONO_INST_NEW (cfg, ins, *ip);
4388 ins->type = STACK_OBJ;
4389 ins->inst_left = *sp;
4391 ins->inst_newa_class = klass;
4393 *sp++ = emit_tree (cfg, bblock, ins);
4399 MONO_INST_NEW (cfg, ins, *ip);
4401 ins->inst_left = *sp;
4402 ins->cil_code = ip++;
4403 MONO_ADD_INS (bblock, ins);
4405 start_new_bblock = 1;
4410 MonoInst *offset_ins;
4411 MonoClassField *field;
4412 MonoBasicBlock *ebblock;
4416 if (*ip == CEE_STFLD) {
4423 // FIXME: enable this test later.
4424 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
4427 token = read32 (ip + 1);
4428 field = mono_field_from_token (image, token, &klass, generic_context);
4429 mono_class_init (klass);
4431 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
4432 /* FIXME: mark instructions for use in SSA */
4433 if (*ip == CEE_STFLD) {
4434 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound || klass == mono_defaults.marshalbyrefobject_class) {
4435 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
4436 MonoInst *iargs [5];
4439 NEW_CLASSCONST (cfg, iargs [1], klass);
4440 NEW_FIELDCONST (cfg, iargs [2], field);
4441 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
4445 if (cfg->opt & MONO_OPT_INLINE) {
4446 costs = inline_method (cfg, stfld_wrapper, stfld_wrapper->signature, bblock,
4447 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4448 g_assert (costs > 0);
4453 GET_BBLOCK (cfg, bbhash, bblock, ip);
4454 ebblock->next_bb = bblock;
4455 link_bblock (cfg, ebblock, bblock);
4457 /* indicates start of a new block, and triggers a load
4458 of all stack arguments at bb boundarie */
4461 inline_costs += costs;
4464 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, stfld_wrapper->signature, iargs, ip, NULL);
4468 NEW_ICONST (cfg, offset_ins, foffset);
4469 MONO_INST_NEW (cfg, ins, OP_PADD);
4471 ins->inst_left = *sp;
4472 ins->inst_right = offset_ins;
4473 ins->type = STACK_MP;
4475 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4476 store->cil_code = ip;
4477 store->inst_left = ins;
4478 store->inst_right = sp [1];
4479 handle_loaded_temps (cfg, bblock, stack_start, sp);
4480 store->flags |= ins_flag;
4482 if (store->opcode == CEE_STOBJ) {
4483 handle_stobj (cfg, bblock, ins, sp [1], ip,
4484 mono_class_from_mono_type (field->type), FALSE, FALSE);
4486 MONO_ADD_INS (bblock, store);
4489 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound || klass == mono_defaults.marshalbyrefobject_class) {
4490 /* fixme: we need to inline that call somehow */
4491 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
4492 MonoInst *iargs [4];
4496 NEW_CLASSCONST (cfg, iargs [1], klass);
4497 NEW_FIELDCONST (cfg, iargs [2], field);
4498 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
4499 if (cfg->opt & MONO_OPT_INLINE) {
4500 costs = inline_method (cfg, ldfld_wrapper, ldfld_wrapper->signature, bblock,
4501 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4502 g_assert (costs > 0);
4507 GET_BBLOCK (cfg, bbhash, bblock, ip);
4508 ebblock->next_bb = bblock;
4509 link_bblock (cfg, ebblock, bblock);
4511 temp = iargs [0]->inst_i0->inst_c0;
4513 if (*ip == CEE_LDFLDA) {
4514 /* not sure howto handle this */
4515 NEW_TEMPLOADA (cfg, *sp, temp);
4517 NEW_TEMPLOAD (cfg, *sp, temp);
4521 /* indicates start of a new block, and triggers a load of
4522 all stack arguments at bb boundarie */
4525 inline_costs += costs;
4528 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, ldfld_wrapper->signature, iargs, ip, NULL);
4529 if (*ip == CEE_LDFLDA) {
4530 /* not sure howto handle this */
4531 NEW_TEMPLOADA (cfg, *sp, temp);
4533 NEW_TEMPLOAD (cfg, *sp, temp);
4538 NEW_ICONST (cfg, offset_ins, foffset);
4539 MONO_INST_NEW (cfg, ins, OP_PADD);
4541 ins->inst_left = *sp;
4542 ins->inst_right = offset_ins;
4543 ins->type = STACK_MP;
4545 if (*ip == CEE_LDFLDA) {
4549 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4550 type_to_eval_stack_type (field->type, load);
4551 load->cil_code = ip;
4552 load->inst_left = ins;
4553 load->flags |= ins_flag;
4565 MonoClassField *field;
4566 gpointer addr = NULL;
4569 token = read32 (ip + 1);
4571 field = mono_field_from_token (image, token, &klass, generic_context);
4572 mono_class_init (klass);
4574 if ((*ip) == CEE_STSFLD)
4575 handle_loaded_temps (cfg, bblock, stack_start, sp);
4577 if (cfg->domain->special_static_fields)
4578 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
4580 if ((cfg->opt & MONO_OPT_SHARED) || (mono_compile_aot && addr)) {
4582 MonoInst *iargs [2];
4583 g_assert (field->parent);
4584 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
4585 NEW_FIELDCONST (cfg, iargs [1], field);
4586 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
4587 NEW_TEMPLOAD (cfg, ins, temp);
4590 vtable = mono_class_vtable (cfg->domain, klass);
4592 if ((!vtable->initialized || mono_compile_aot) && !(klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (klass, method)) {
4593 guint8 *tramp = mono_create_class_init_trampoline (vtable);
4594 mono_emit_native_call (cfg, bblock, tramp,
4595 helper_sig_class_init_trampoline,
4596 NULL, ip, FALSE, FALSE);
4597 if (cfg->verbose_level > 2)
4598 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
4600 if (cfg->run_cctors)
4601 mono_runtime_class_init (vtable);
4603 addr = (char*)vtable->data + field->offset;
4605 if (mono_compile_aot)
4606 NEW_SFLDACONST (cfg, ins, field);
4608 NEW_PCONST (cfg, ins, addr);
4612 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
4613 * This could be later optimized to do just a couple of
4614 * memory dereferences with constant offsets.
4617 MonoInst *iargs [1];
4618 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
4619 temp = mono_emit_jit_icall (cfg, bblock, mono_get_special_static_data, iargs, ip);
4620 NEW_TEMPLOAD (cfg, ins, temp);
4624 /* FIXME: mark instructions for use in SSA */
4625 if (*ip == CEE_LDSFLDA) {
4627 } else if (*ip == CEE_STSFLD) {
4631 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4632 store->cil_code = ip;
4633 store->inst_left = ins;
4634 store->inst_right = sp [0];
4635 store->flags |= ins_flag;
4638 if (store->opcode == CEE_STOBJ) {
4639 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
4641 MONO_ADD_INS (bblock, store);
4643 gboolean is_const = FALSE;
4644 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4645 if (!((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) &&
4646 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
4647 gpointer addr = (char*)vtable->data + field->offset;
4648 /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
4650 switch (field->type->type) {
4651 case MONO_TYPE_BOOLEAN:
4653 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
4657 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
4660 case MONO_TYPE_CHAR:
4662 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
4666 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
4671 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
4675 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
4680 case MONO_TYPE_STRING:
4681 case MONO_TYPE_OBJECT:
4682 case MONO_TYPE_CLASS:
4683 case MONO_TYPE_SZARRAY:
4685 case MONO_TYPE_FNPTR:
4686 case MONO_TYPE_ARRAY:
4687 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
4688 type_to_eval_stack_type (field->type, *sp);
4693 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
4694 sp [0]->type = STACK_I8;
4695 sp [0]->inst_l = *((gint64 *)addr);
4700 case MONO_TYPE_VALUETYPE:
4709 CHECK_STACK_OVF (1);
4710 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4711 type_to_eval_stack_type (field->type, load);
4712 load->cil_code = ip;
4713 load->inst_left = ins;
4715 load->flags |= ins_flag;
4717 /* fixme: dont see the problem why this does not work */
4718 //cfg->disable_aot = TRUE;
4728 token = read32 (ip + 1);
4729 if (method->wrapper_type != MONO_WRAPPER_NONE)
4730 klass = mono_method_get_wrapper_data (method, token);
4732 klass = mono_class_get_full (image, token, generic_context);
4733 mono_class_init (klass);
4734 n = mono_type_to_stind (&klass->byval_arg);
4735 if (n == CEE_STOBJ) {
4736 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
4738 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
4740 MONO_INST_NEW (cfg, store, n);
4741 store->cil_code = ip;
4742 store->inst_left = sp [0];
4743 store->inst_right = sp [1];
4744 store->flags |= ins_flag;
4745 MONO_ADD_INS (bblock, store);
4757 token = read32 (ip + 1);
4758 if (method->wrapper_type != MONO_WRAPPER_NONE)
4759 klass = mono_method_get_wrapper_data (method, token);
4761 klass = mono_class_get_full (image, token, generic_context);
4762 mono_class_init (klass);
4764 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4769 *sp++ = handle_box (cfg, bblock, val, ip, klass);
4776 MONO_INST_NEW (cfg, ins, *ip);
4781 token = read32 (ip + 1);
4783 /* allocate the domainvar - becaus this is used in decompose_foreach */
4784 if (cfg->opt & MONO_OPT_SHARED) {
4785 mono_get_domainvar (cfg);
4786 /* LAME-IR: Mark it as used since otherwise it will be optimized away */
4787 cfg->domainvar->flags |= MONO_INST_VOLATILE;
4790 if (method->wrapper_type != MONO_WRAPPER_NONE)
4791 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4793 klass = mono_class_get_full (image, token, generic_context);
4795 mono_class_init (klass);
4796 ins->inst_newa_class = klass;
4797 ins->inst_newa_len = *sp;
4798 ins->type = STACK_OBJ;
4802 * we store the object so calls to create the array are not interleaved
4803 * with the arguments of other calls.
4806 MonoInst *store, *temp, *load;
4808 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
4809 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4810 store->cil_code = ins->cil_code;
4811 MONO_ADD_INS (bblock, store);
4812 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
4813 load->cil_code = ins->cil_code;
4820 MONO_INST_NEW (cfg, ins, *ip);
4821 ins->cil_code = ip++;
4823 ins->inst_left = *sp;
4824 ins->type = STACK_PTR;
4832 if (method->wrapper_type != MONO_WRAPPER_NONE)
4833 klass = (MonoClass*)mono_method_get_wrapper_data (method, read32 (ip + 1));
4835 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4836 mono_class_init (klass);
4837 NEW_LDELEMA (cfg, ins, sp, klass);
4842 case CEE_LDELEM_ANY: {
4847 token = read32 (ip + 1);
4848 klass = mono_class_get_full (image, token, generic_context);
4849 mono_class_init (klass);
4850 NEW_LDELEMA (cfg, load, sp, klass);
4851 load->cil_code = ip;
4852 MONO_INST_NEW (cfg, ins, mono_type_to_ldind (&klass->byval_arg));
4854 ins->inst_left = load;
4856 type_to_eval_stack_type (&klass->byval_arg, ins);
4870 case CEE_LDELEM_REF: {
4874 * ldind.x (ldelema (array, index))
4875 * ldelema does the bounds check
4879 klass = array_access_to_klass (*ip);
4880 NEW_LDELEMA (cfg, load, sp, klass);
4881 load->cil_code = ip;
4882 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
4884 ins->inst_left = load;
4886 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
4896 case CEE_STELEM_R8: {
4900 * stind.x (ldelema (array, index), val)
4901 * ldelema does the bounds check
4905 klass = array_access_to_klass (*ip);
4906 NEW_LDELEMA (cfg, load, sp, klass);
4907 load->cil_code = ip;
4908 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
4910 ins->inst_left = load;
4911 ins->inst_right = sp [2];
4913 handle_loaded_temps (cfg, bblock, stack_start, sp);
4914 MONO_ADD_INS (bblock, ins);
4918 case CEE_STELEM_ANY: {
4922 * stind.x (ldelema (array, index), val)
4923 * ldelema does the bounds check
4928 token = read32 (ip + 1);
4929 klass = mono_class_get_full (image, token, generic_context);
4930 mono_class_init (klass);
4931 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4932 MonoMethod* helper = mono_marshal_get_stelemref ();
4933 MonoInst *iargs [3];
4934 handle_loaded_temps (cfg, bblock, stack_start, sp);
4940 mono_emit_method_call_spilled (cfg, bblock, helper, helper->signature, iargs, ip, NULL);
4942 NEW_LDELEMA (cfg, load, sp, klass);
4943 load->cil_code = ip;
4945 n = mono_type_to_stind (&klass->byval_arg);
4947 handle_stobj (cfg, bblock, load, sp [2], ip, klass, FALSE, FALSE);
4949 MONO_INST_NEW (cfg, ins, n);
4951 ins->inst_left = load;
4952 ins->inst_right = sp [2];
4953 handle_loaded_temps (cfg, bblock, stack_start, sp);
4954 MONO_ADD_INS (bblock, ins);
4961 case CEE_STELEM_REF: {
4962 MonoInst *iargs [3];
4963 MonoMethod* helper = mono_marshal_get_stelemref ();
4968 handle_loaded_temps (cfg, bblock, stack_start, sp);
4974 mono_emit_method_call_spilled (cfg, bblock, helper, helper->signature, iargs, ip, NULL);
4978 NEW_GROUP (cfg, group, sp [0], sp [1]);
4979 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
4981 ins->inst_left = group;
4982 ins->inst_right = sp [2];
4983 MONO_ADD_INS (bblock, ins);
4990 case CEE_CKFINITE: {
4991 MonoInst *store, *temp;
4994 /* this instr. can throw exceptions as side effect,
4995 * so we cant eliminate dead code which contains CKFINITE opdodes.
4996 * Spilling to memory makes sure that we always perform
5000 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
5002 ins->inst_left = sp [-1];
5003 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
5005 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
5006 store->cil_code = ip;
5007 MONO_ADD_INS (bblock, store);
5009 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
5016 MONO_INST_NEW (cfg, ins, *ip);
5019 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
5020 mono_class_init (klass);
5021 ins->type = STACK_MP;
5022 ins->inst_left = *sp;
5024 ins->inst_newa_class = klass;
5029 case CEE_MKREFANY: {
5030 MonoInst *loc, *klassconst;
5033 MONO_INST_NEW (cfg, ins, *ip);
5036 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
5037 mono_class_init (klass);
5040 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
5041 NEW_TEMPLOADA (cfg, ins->inst_right, loc->inst_c0);
5043 NEW_PCONST (cfg, klassconst, klass);
5044 NEW_GROUP (cfg, ins->inst_left, *sp, klassconst);
5046 MONO_ADD_INS (bblock, ins);
5048 NEW_TEMPLOAD (cfg, *sp, loc->inst_c0);
5055 MonoClass *handle_class;
5057 CHECK_STACK_OVF (1);
5060 n = read32 (ip + 1);
5062 handle = mono_ldtoken (image, n, &handle_class, generic_context);
5063 mono_class_init (handle_class);
5065 if (cfg->opt & MONO_OPT_SHARED) {
5067 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
5069 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
5071 NEW_IMAGECONST (cfg, iargs [0], image);
5072 NEW_ICONST (cfg, iargs [1], n);
5073 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
5074 NEW_TEMPLOAD (cfg, res, temp);
5075 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5076 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
5077 MONO_ADD_INS (bblock, store);
5078 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5080 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method_full (image, read32 (ip + 6), NULL, generic_context)) &&
5081 (cmethod->klass == mono_defaults.monotype_class->parent) &&
5082 (strcmp (cmethod->name, "GetTypeFromHandle") == 0) &&
5083 ((g_hash_table_lookup (bbhash, ip + 5) == NULL) ||
5084 (g_hash_table_lookup (bbhash, ip + 5) == bblock))) {
5085 MonoClass *tclass = mono_class_from_mono_type (handle);
5086 mono_class_init (tclass);
5087 if (mono_compile_aot)
5088 NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n);
5090 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
5091 ins->type = STACK_OBJ;
5092 ins->klass = cmethod->klass;
5095 MonoInst *store, *addr, *vtvar;
5097 if (mono_compile_aot)
5098 NEW_LDTOKENCONST (cfg, ins, image, n);
5100 NEW_PCONST (cfg, ins, handle);
5101 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
5102 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5103 NEW_INDSTORE (cfg, store, addr, ins, &mono_defaults.int_class->byval_arg);
5104 MONO_ADD_INS (bblock, store);
5105 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5121 case CEE_ADD_OVF_UN:
5123 case CEE_MUL_OVF_UN:
5125 case CEE_SUB_OVF_UN:
5128 if (mono_find_jit_opcode_emulation (ins->opcode)) {
5130 *sp++ = emit_tree (cfg, bblock, ins);
5134 case CEE_ENDFINALLY:
5135 MONO_INST_NEW (cfg, ins, *ip);
5136 MONO_ADD_INS (bblock, ins);
5137 ins->cil_code = ip++;
5138 start_new_bblock = 1;
5141 * Control will leave the method so empty the stack, otherwise
5142 * the next basic block will start with a nonempty stack.
5144 while (sp != stack_start) {
5145 MONO_INST_NEW (cfg, ins, CEE_POP);
5149 MONO_ADD_INS (bblock, ins);
5155 if (*ip == CEE_LEAVE) {
5157 target = ip + 5 + (gint32)read32(ip + 1);
5160 target = ip + 2 + (signed char)(ip [1]);
5163 /* empty the stack */
5164 while (sp != stack_start) {
5165 MONO_INST_NEW (cfg, ins, CEE_POP);
5169 MONO_ADD_INS (bblock, ins);
5173 * If this leave statement is in a catch block, check for a
5174 * pending exception, and rethrow it if necessary.
5176 for (i = 0; i < header->num_clauses; ++i) {
5177 MonoExceptionClause *clause = &header->clauses [i];
5178 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code)) {
5181 temp = mono_emit_jit_icall (cfg, bblock, mono_thread_get_pending_exception, NULL, ip);
5182 NEW_TEMPLOAD (cfg, *sp, temp);
5184 MONO_INST_NEW (cfg, ins, OP_THROW_OR_NULL);
5185 ins->inst_left = *sp;
5187 MONO_ADD_INS (bblock, ins);
5191 /* fixme: call fault handler ? */
5193 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
5195 for (tmp = handlers; tmp; tmp = tmp->next) {
5197 link_bblock (cfg, bblock, tblock);
5198 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
5200 ins->inst_target_bb = tblock;
5201 MONO_ADD_INS (bblock, ins);
5203 g_list_free (handlers);
5206 MONO_INST_NEW (cfg, ins, CEE_BR);
5208 MONO_ADD_INS (bblock, ins);
5209 GET_BBLOCK (cfg, bbhash, tblock, target);
5210 link_bblock (cfg, bblock, tblock);
5211 CHECK_BBLOCK (target, ip, tblock);
5212 ins->inst_target_bb = tblock;
5213 start_new_bblock = 1;
5215 if (*ip == CEE_LEAVE)
5224 MONO_INST_NEW (cfg, ins, *ip);
5226 handle_loaded_temps (cfg, bblock, stack_start, sp);
5227 MONO_ADD_INS (bblock, ins);
5228 ins->cil_code = ip++;
5229 ins->inst_i0 = sp [0];
5230 ins->inst_i1 = sp [1];
5238 /* trampoline mono specific opcodes */
5239 case MONO_CUSTOM_PREFIX: {
5241 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
5246 case CEE_MONO_ICALL: {
5249 MonoJitICallInfo *info;
5251 token = read32 (ip + 2);
5252 func = mono_method_get_wrapper_data (method, token);
5253 info = mono_find_jit_icall_by_addr (func);
5256 CHECK_STACK (info->sig->param_count);
5257 sp -= info->sig->param_count;
5259 temp = mono_emit_jit_icall (cfg, bblock, info->func, sp, ip);
5260 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
5261 NEW_TEMPLOAD (cfg, *sp, temp);
5266 inline_costs += 10 * num_calls++;
5270 case CEE_MONO_LDPTR:
5271 CHECK_STACK_OVF (1);
5273 token = read32 (ip + 2);
5274 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
5278 inline_costs += 10 * num_calls++;
5280 case CEE_MONO_VTADDR:
5283 MONO_INST_NEW (cfg, ins, OP_VTADDR);
5285 ins->type = STACK_MP;
5286 ins->inst_left = *sp;
5290 case CEE_MONO_NEWOBJ: {
5291 MonoInst *iargs [2];
5293 CHECK_STACK_OVF (1);
5295 token = read32 (ip + 2);
5296 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5297 mono_class_init (klass);
5298 NEW_DOMAINCONST (cfg, iargs [0]);
5299 NEW_CLASSCONST (cfg, iargs [1], klass);
5300 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
5301 NEW_TEMPLOAD (cfg, *sp, temp);
5304 inline_costs += 10 * num_calls++;
5307 case CEE_MONO_OBJADDR:
5310 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
5312 ins->type = STACK_MP;
5313 ins->inst_left = *sp;
5317 case CEE_MONO_LDNATIVEOBJ:
5320 token = read32 (ip + 2);
5321 klass = mono_method_get_wrapper_data (method, token);
5322 g_assert (klass->valuetype);
5323 mono_class_init (klass);
5324 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
5328 case CEE_MONO_RETOBJ:
5329 g_assert (cfg->ret);
5330 g_assert (method->signature->pinvoke);
5335 token = read32 (ip + 2);
5336 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5338 NEW_RETLOADA (cfg, ins);
5339 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
5341 if (sp != stack_start)
5344 MONO_INST_NEW (cfg, ins, CEE_BR);
5346 ins->inst_target_bb = end_bblock;
5347 MONO_ADD_INS (bblock, ins);
5348 link_bblock (cfg, bblock, end_bblock);
5349 start_new_bblock = 1;
5352 case CEE_MONO_CISINST:
5353 case CEE_MONO_CCASTCLASS: {
5358 token = read32 (ip + 2);
5359 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5360 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_CISINST) ? OP_CISINST : OP_CCASTCLASS);
5361 ins->type = STACK_I4;
5362 ins->inst_left = *sp;
5363 ins->inst_newa_class = klass;
5365 *sp++ = emit_tree (cfg, bblock, ins);
5369 case CEE_MONO_SAVE_LMF:
5370 case CEE_MONO_RESTORE_LMF:
5371 #ifdef MONO_ARCH_HAVE_LMF_OPS
5372 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_SAVE_LMF) ? OP_SAVE_LMF : OP_RESTORE_LMF);
5373 MONO_ADD_INS (bblock, ins);
5374 cfg->need_lmf_area = TRUE;
5379 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
5388 /* somewhat similar to LDTOKEN */
5389 MonoInst *addr, *vtvar;
5390 CHECK_STACK_OVF (1);
5391 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
5393 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5394 addr->cil_code = ip;
5395 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
5397 ins->inst_left = addr;
5398 MONO_ADD_INS (bblock, ins);
5399 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5413 * The following transforms:
5414 * CEE_CEQ into OP_CEQ
5415 * CEE_CGT into OP_CGT
5416 * CEE_CGT_UN into OP_CGT_UN
5417 * CEE_CLT into OP_CLT
5418 * CEE_CLT_UN into OP_CLT_UN
5420 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
5422 MONO_INST_NEW (cfg, ins, cmp->opcode);
5424 cmp->inst_i0 = sp [0];
5425 cmp->inst_i1 = sp [1];
5429 if ((sp [0]->type == STACK_I8) || ((sizeof (gpointer) == 8) && ((sp [0]->type == STACK_PTR) || (sp [0]->type == STACK_OBJ) || (sp [0]->type == STACK_MP))))
5430 cmp->opcode = OP_LCOMPARE;
5432 cmp->opcode = OP_COMPARE;
5434 ins->type = STACK_I4;
5437 /* spill it to reduce the expression complexity
5438 * and workaround bug 54209
5440 if (cmp->inst_left->type == STACK_I8) {
5442 *sp++ = emit_tree (cfg, bblock, ins);
5451 CHECK_STACK_OVF (1);
5453 n = read32 (ip + 2);
5454 if (method->wrapper_type != MONO_WRAPPER_NONE)
5455 cmethod = mono_method_get_wrapper_data (method, n);
5457 cmethod = mono_get_method_full (image, n, NULL, generic_context);
5460 mono_class_init (cmethod->klass);
5461 handle_loaded_temps (cfg, bblock, stack_start, sp);
5463 NEW_METHODCONST (cfg, argconst, cmethod);
5464 if (method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED)
5465 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
5467 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn_nosync, &argconst, ip);
5468 NEW_TEMPLOAD (cfg, *sp, temp);
5472 inline_costs += 10 * num_calls++;
5475 case CEE_LDVIRTFTN: {
5481 n = read32 (ip + 2);
5482 if (method->wrapper_type != MONO_WRAPPER_NONE)
5483 cmethod = mono_method_get_wrapper_data (method, n);
5485 cmethod = mono_get_method_full (image, n, NULL, generic_context);
5487 mono_class_init (cmethod->klass);
5488 handle_loaded_temps (cfg, bblock, stack_start, sp);
5492 NEW_METHODCONST (cfg, args [1], cmethod);
5493 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
5494 NEW_TEMPLOAD (cfg, *sp, temp);
5498 inline_costs += 10 * num_calls++;
5502 CHECK_STACK_OVF (1);
5504 n = read16 (ip + 2);
5506 NEW_ARGLOAD (cfg, ins, n);
5512 CHECK_STACK_OVF (1);
5514 n = read16 (ip + 2);
5516 NEW_ARGLOADA (cfg, ins, n);
5524 handle_loaded_temps (cfg, bblock, stack_start, sp);
5526 n = read16 (ip + 2);
5528 NEW_ARGSTORE (cfg, ins, n, *sp);
5530 if (ins->opcode == CEE_STOBJ) {
5531 NEW_ARGLOADA (cfg, ins, n);
5532 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5534 MONO_ADD_INS (bblock, ins);
5538 CHECK_STACK_OVF (1);
5540 n = read16 (ip + 2);
5542 NEW_LOCLOAD (cfg, ins, n);
5548 CHECK_STACK_OVF (1);
5550 n = read16 (ip + 2);
5552 NEW_LOCLOADA (cfg, ins, n);
5561 n = read16 (ip + 2);
5563 handle_loaded_temps (cfg, bblock, stack_start, sp);
5564 NEW_LOCSTORE (cfg, ins, n, *sp);
5566 if (ins->opcode == CEE_STOBJ) {
5567 NEW_LOCLOADA (cfg, ins, n);
5568 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5570 MONO_ADD_INS (bblock, ins);
5577 if (sp != stack_start)
5579 if (cfg->method != method)
5581 * Inlining this into a loop in a parent could lead to
5582 * stack overflows which is different behavior than the
5583 * non-inlined case, thus disable inlining in this case.
5585 goto inline_failure;
5586 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
5587 ins->inst_left = *sp;
5589 ins->type = STACK_MP;
5591 cfg->flags |= MONO_CFG_HAS_ALLOCA;
5592 if (header->init_locals)
5593 ins->flags |= MONO_INST_INIT;
5597 /* FIXME: set init flag if locals init is set in this method */
5599 case CEE_ENDFILTER: {
5600 MonoExceptionClause *clause, *nearest;
5601 int cc, nearest_num;
5605 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
5607 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
5608 ins->inst_left = *sp;
5610 MONO_ADD_INS (bblock, ins);
5611 start_new_bblock = 1;
5616 for (cc = 0; cc < header->num_clauses; ++cc) {
5617 clause = &header->clauses [cc];
5618 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
5619 (!nearest || (clause->data.filter_offset > nearest->data.filter_offset))) {
5625 filter_lengths [nearest_num] = (ip - header->code) - nearest->data.filter_offset;
5629 case CEE_UNALIGNED_:
5630 ins_flag |= MONO_INST_UNALIGNED;
5631 /* FIXME: record alignment? we can assume 1 for now */
5636 ins_flag |= MONO_INST_VOLATILE;
5640 ins_flag |= MONO_INST_TAILCALL;
5641 cfg->flags |= MONO_CFG_HAS_TAIL;
5642 /* Can't inline tail calls at this time */
5643 inline_costs += 100000;
5650 token = read32 (ip + 2);
5651 if (method->wrapper_type != MONO_WRAPPER_NONE)
5652 klass = mono_method_get_wrapper_data (method, token);
5654 klass = mono_class_get_full (image, token, generic_context);
5655 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5656 MonoInst *store, *load;
5657 NEW_PCONST (cfg, load, NULL);
5658 load->cil_code = ip;
5659 load->type = STACK_OBJ;
5660 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
5661 store->cil_code = ip;
5662 handle_loaded_temps (cfg, bblock, stack_start, sp);
5663 MONO_ADD_INS (bblock, store);
5664 store->inst_i0 = sp [0];
5665 store->inst_i1 = load;
5667 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
5672 case CEE_CONSTRAINED_:
5673 /* FIXME: implement */
5675 token = read32 (ip + 2);
5676 constrained_call = mono_class_get_full (image, token, generic_context);
5681 MonoInst *iargs [3];
5684 if ((cfg->opt & MONO_OPT_INTRINS) && (ip [1] == CEE_CPBLK) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5)) {
5686 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
5687 copy->inst_left = sp [0];
5688 copy->inst_right = sp [1];
5689 copy->cil_code = ip;
5691 MONO_ADD_INS (bblock, copy);
5698 handle_loaded_temps (cfg, bblock, stack_start, sp);
5699 if (ip [1] == CEE_CPBLK) {
5700 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
5702 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
5711 ins_flag |= MONO_INST_NOTYPECHECK;
5713 ins_flag |= MONO_INST_NORANGECHECK;
5714 /* we ignore the no-nullcheck for now since we
5715 * really do it explicitly only when doing callvirt->call
5721 /* FIXME: check we are in a catch handler */
5722 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
5723 load->cil_code = ip;
5724 MONO_INST_NEW (cfg, ins, OP_RETHROW);
5725 ins->inst_left = load;
5727 MONO_ADD_INS (bblock, ins);
5729 start_new_bblock = 1;
5734 CHECK_STACK_OVF (1);
5736 token = read32 (ip + 2);
5737 /* FIXXME: handle generics. */
5738 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
5739 MonoType *type = mono_type_create_from_typespec (image, token);
5740 token = mono_type_size (type, &align);
5742 MonoClass *szclass = mono_class_get_full (image, token, generic_context);
5743 mono_class_init (szclass);
5744 token = mono_class_value_size (szclass, &align);
5746 NEW_ICONST (cfg, ins, token);
5751 case CEE_REFANYTYPE:
5753 MONO_INST_NEW (cfg, ins, OP_REFANYTYPE);
5755 ins->type = STACK_MP;
5756 ins->inst_left = *sp;
5757 ins->type = STACK_VTYPE;
5758 ins->klass = mono_defaults.typehandle_class;
5767 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
5772 g_error ("opcode 0x%02x not handled", *ip);
5775 if (start_new_bblock != 1)
5778 bblock->cil_length = ip - bblock->cil_code;
5779 bblock->next_bb = end_bblock;
5780 link_bblock (cfg, bblock, end_bblock);
5782 if (cfg->method == method && cfg->domainvar) {
5786 MonoInst *get_domain;
5788 if (! (get_domain = mono_arch_get_domain_intrinsic (cfg))) {
5791 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
5792 call->signature = helper_sig_domain_get;
5793 call->inst.type = STACK_PTR;
5794 call->fptr = mono_domain_get;
5795 get_domain = (MonoInst*)call;
5798 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, get_domain);
5799 MONO_ADD_INS (init_localsbb, store);
5802 if (header->init_locals) {
5804 for (i = 0; i < header->num_locals; ++i) {
5805 MonoType *ptype = header->locals [i];
5806 int t = ptype->type;
5807 if (t == MONO_TYPE_VALUETYPE && ptype->data.klass->enumtype)
5808 t = ptype->data.klass->enum_basetype->type;
5809 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
5810 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
5811 NEW_ICONST (cfg, ins, 0);
5812 NEW_LOCSTORE (cfg, store, i, ins);
5813 MONO_ADD_INS (init_localsbb, store);
5814 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
5815 MONO_INST_NEW (cfg, ins, OP_I8CONST);
5816 ins->type = STACK_I8;
5818 NEW_LOCSTORE (cfg, store, i, ins);
5819 MONO_ADD_INS (init_localsbb, store);
5820 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
5821 MONO_INST_NEW (cfg, ins, OP_R8CONST);
5822 ins->type = STACK_R8;
5823 ins->inst_p0 = (void*)&r8_0;
5824 NEW_LOCSTORE (cfg, store, i, ins);
5825 MONO_ADD_INS (init_localsbb, store);
5826 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
5827 ((t == MONO_TYPE_GENERICINST) && mono_metadata_generic_inst_is_valuetype (ptype->data.generic_inst))) {
5828 NEW_LOCLOADA (cfg, ins, i);
5829 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (ptype), NULL, NULL);
5832 NEW_PCONST (cfg, ins, NULL);
5833 NEW_LOCSTORE (cfg, store, i, ins);
5834 MONO_ADD_INS (init_localsbb, store);
5840 /* resolve backward branches in the middle of an existing basic block */
5841 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
5843 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
5844 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
5845 if (tblock != start_bblock) {
5847 split_bblock (cfg, tblock, bblock);
5848 l = bblock->cil_code - header->code;
5849 bblock->cil_length = tblock->cil_length - l;
5850 tblock->cil_length = l;
5852 g_print ("recheck failed.\n");
5857 * we compute regions here, because the length of filter clauses is not known in advance.
5858 * It is computed in the CEE_ENDFILTER case in the above switch statement
5860 if (cfg->method == method) {
5862 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5863 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
5865 mono_create_spvar_for_region (cfg, bb->region);
5866 if (cfg->verbose_level > 2)
5867 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
5870 g_hash_table_destroy (bbhash);
5873 dont_inline = g_list_remove (dont_inline, method);
5874 return inline_costs;
5877 if (cfg->method != method)
5878 g_hash_table_destroy (bbhash);
5879 dont_inline = g_list_remove (dont_inline, method);
5883 if (cfg->method != method)
5884 g_hash_table_destroy (bbhash);
5885 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
5886 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
5887 dont_inline = g_list_remove (dont_inline, method);
5892 mono_print_tree (MonoInst *tree) {
5898 arity = mono_burg_arity [tree->opcode];
5900 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
5902 switch (tree->opcode) {
5904 printf ("[%d]", tree->inst_c0);
5907 printf ("[%lld]", tree->inst_l);
5910 printf ("[%f]", *(double*)tree->inst_p0);
5913 printf ("[%f]", *(float*)tree->inst_p0);
5917 printf ("[%d]", tree->inst_c0);
5920 if (tree->inst_offset < 0)
5921 printf ("[-0x%x(%s)]", -tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5923 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5926 printf ("[%s]", mono_arch_regname (tree->dreg));
5929 printf ("[%s]", tree->inst_newa_class->name);
5930 mono_print_tree (tree->inst_newa_len);
5941 case OP_VOIDCALLVIRT: {
5942 MonoCallInst *call = (MonoCallInst*)tree;
5944 printf ("[%s]", call->method->name);
5949 printf ("[%d (", tree->inst_c0);
5950 for (i = 0; i < tree->inst_phi_args [0]; i++) {
5953 printf ("%d", tree->inst_phi_args [i + 1]);
5964 case OP_LOAD_MEMBASE:
5965 case OP_LOADI4_MEMBASE:
5966 case OP_LOADU4_MEMBASE:
5967 case OP_LOADU1_MEMBASE:
5968 case OP_LOADI1_MEMBASE:
5969 case OP_LOADU2_MEMBASE:
5970 case OP_LOADI2_MEMBASE:
5971 printf ("[%s] <- [%s + 0x%x]", mono_arch_regname (tree->dreg), mono_arch_regname (tree->inst_basereg), tree->inst_offset);
5974 printf ("[B%d]", tree->inst_target_bb->block_num);
5984 case OP_VOIDCALL_REG:
5985 mono_print_tree (tree->inst_left);
5997 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
5998 mono_print_tree (tree->inst_left);
6001 if (!mono_arch_print_tree(tree, arity)) {
6003 mono_print_tree (tree->inst_left);
6005 mono_print_tree (tree->inst_right);
6016 mono_print_tree_nl (MonoInst *tree)
6018 mono_print_tree (tree);
6022 #define make_icall_sig mono_create_icall_signature
6025 create_helper_signature (void)
6027 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
6028 helper_sig_newarr = make_icall_sig ("object ptr ptr int32");
6030 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
6031 helper_sig_newarr_specific = make_icall_sig ("object ptr int32");
6033 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
6034 helper_sig_object_new = make_icall_sig ("object ptr ptr");
6036 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
6037 helper_sig_object_new_specific = make_icall_sig ("object ptr");
6039 /* void* mono_method_compile (MonoMethod*) */
6040 helper_sig_compile = make_icall_sig ("ptr ptr");
6042 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
6043 helper_sig_compile_virt = make_icall_sig ("ptr object ptr");
6045 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
6046 helper_sig_ldstr = make_icall_sig ("object ptr ptr int32");
6048 /* MonoDomain *mono_domain_get (void) */
6049 helper_sig_domain_get = make_icall_sig ("ptr");
6051 /* void stelem_ref (MonoArray *, int index, MonoObject *) */
6052 helper_sig_stelem_ref = make_icall_sig ("void ptr int32 object");
6054 /* void stelem_ref_check (MonoArray *, MonoObject *) */
6055 helper_sig_stelem_ref_check = make_icall_sig ("void object object");
6057 /* long amethod (long, long) */
6058 helper_sig_long_long_long = make_icall_sig ("long long long");
6060 /* object amethod (intptr) */
6061 helper_sig_obj_ptr = make_icall_sig ("object ptr");
6063 helper_sig_obj_ptr_ptr = make_icall_sig ("object ptr ptr");
6065 helper_sig_obj_obj_ptr_ptr = make_icall_sig ("object object ptr ptr");
6067 helper_sig_void_void = make_icall_sig ("void");
6069 /* void amethod (intptr) */
6070 helper_sig_void_ptr = make_icall_sig ("void ptr");
6072 /* void amethod (MonoObject *obj) */
6073 helper_sig_void_obj = make_icall_sig ("void object");
6075 /* void amethod (MonoObject *obj, void *ptr, int i) */
6076 helper_sig_void_obj_ptr_int = make_icall_sig ("void object ptr int");
6078 helper_sig_void_obj_ptr_ptr_obj = make_icall_sig ("void object ptr ptr object");
6080 /* intptr amethod (void) */
6081 helper_sig_ptr_void = make_icall_sig ("ptr");
6083 /* object amethod (void) */
6084 helper_sig_obj_void = make_icall_sig ("object");
6086 /* void amethod (intptr, intptr) */
6087 helper_sig_void_ptr_ptr = make_icall_sig ("void ptr ptr");
6089 /* void amethod (intptr, intptr, intptr) */
6090 helper_sig_void_ptr_ptr_ptr = make_icall_sig ("void ptr ptr ptr");
6092 /* intptr amethod (intptr, intptr) */
6093 helper_sig_ptr_ptr_ptr = make_icall_sig ("ptr ptr ptr");
6095 /* IntPtr amethod (object) */
6096 helper_sig_ptr_obj = make_icall_sig ("ptr object");
6098 /* IntPtr amethod (object, int) */
6099 helper_sig_ptr_obj_int = make_icall_sig ("ptr object int");
6101 /* IntPtr amethod (int) */
6102 helper_sig_ptr_int = make_icall_sig ("ptr int32");
6104 /* long amethod (long, guint32) */
6105 helper_sig_long_long_int = make_icall_sig ("long long int32");
6107 /* ulong amethod (double) */
6108 helper_sig_ulong_double = make_icall_sig ("ulong double");
6110 /* long amethod (double) */
6111 helper_sig_long_double = make_icall_sig ("long double");
6113 /* double amethod (long) */
6114 helper_sig_double_long = make_icall_sig ("double long");
6116 /* double amethod (int) */
6117 helper_sig_double_int = make_icall_sig ("double int32");
6119 /* float amethod (long) */
6120 helper_sig_float_long = make_icall_sig ("float long");
6122 /* double amethod (double, double) */
6123 helper_sig_double_double_double = make_icall_sig ("double double double");
6125 /* uint amethod (double) */
6126 helper_sig_uint_double = make_icall_sig ("uint32 double");
6128 /* int amethod (double) */
6129 helper_sig_int_double = make_icall_sig ("int32 double");
6131 /* void initobj (intptr, int size) */
6132 helper_sig_initobj = make_icall_sig ("void ptr int32");
6134 /* void memcpy (intptr, intptr, int size) */
6135 helper_sig_memcpy = make_icall_sig ("void ptr ptr int32");
6137 /* void memset (intptr, int val, int size) */
6138 helper_sig_memset = make_icall_sig ("void ptr int32 int32");
6140 helper_sig_class_init_trampoline = make_icall_sig ("void");
6144 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
6147 MonoMethod *wrapper;
6150 if (callinfo->wrapper)
6151 return callinfo->wrapper;
6153 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
6154 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
6155 /* Must be domain neutral since there is only one copy */
6156 code = mono_jit_compile_method_with_opt (wrapper, default_opt | MONO_OPT_SHARED);
6158 if (!callinfo->wrapper) {
6159 callinfo->wrapper = code;
6160 mono_register_jit_icall_wrapper (callinfo, code);
6161 mono_debug_add_icall_wrapper (wrapper, callinfo);
6165 return callinfo->wrapper;
6169 mono_create_class_init_trampoline (MonoVTable *vtable)
6173 /* previously created trampoline code */
6174 mono_domain_lock (vtable->domain);
6176 mono_g_hash_table_lookup (vtable->domain->class_init_trampoline_hash,
6178 mono_domain_unlock (vtable->domain);
6182 code = mono_arch_create_class_init_trampoline (vtable);
6184 /* store trampoline address */
6185 mono_domain_lock (vtable->domain);
6186 mono_g_hash_table_insert (vtable->domain->class_init_trampoline_hash,
6188 mono_domain_unlock (vtable->domain);
6190 EnterCriticalSection (&jit_mutex);
6191 if (!class_init_hash_addr)
6192 class_init_hash_addr = g_hash_table_new (NULL, NULL);
6193 g_hash_table_insert (class_init_hash_addr, code, vtable);
6194 LeaveCriticalSection (&jit_mutex);
6200 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method,
6201 gboolean add_sync_wrapper)
6206 if (add_sync_wrapper && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
6207 return mono_create_jump_trampoline (domain, mono_marshal_get_synchronized_wrapper (method), FALSE);
6209 code = mono_jit_find_compiled_method (domain, method);
6213 mono_domain_lock (domain);
6214 code = mono_g_hash_table_lookup (domain->jump_trampoline_hash, method);
6215 mono_domain_unlock (domain);
6219 ji = mono_arch_create_jump_trampoline (method);
6222 * mono_delegate_ctor needs to find the method metadata from the
6223 * trampoline address, so we save it here.
6226 mono_jit_info_table_add (mono_get_root_domain (), ji);
6228 mono_domain_lock (domain);
6229 mono_g_hash_table_insert (domain->jump_trampoline_hash, method, ji->code_start);
6230 mono_domain_unlock (domain);
6232 return ji->code_start;
6236 mono_create_jit_trampoline (MonoMethod *method)
6238 MonoDomain *domain = mono_domain_get ();
6241 /* Trampoline are domain specific, so cache only the one used in the root domain */
6242 if ((domain == mono_get_root_domain ()) && method->info)
6243 return method->info;
6245 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
6246 return mono_create_jit_trampoline (mono_marshal_get_synchronized_wrapper (method));
6248 tramp = mono_arch_create_jit_trampoline (method);
6249 if (domain == mono_get_root_domain ())
6250 method->info = tramp;
6256 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
6260 EnterCriticalSection (&jit_mutex);
6261 if (class_init_hash_addr)
6262 res = g_hash_table_lookup (class_init_hash_addr, addr);
6265 LeaveCriticalSection (&jit_mutex);
6270 mono_dynamic_code_hash_insert (MonoDomain *domain, MonoMethod *method, MonoJitDynamicMethodInfo *ji)
6272 if (!domain->dynamic_code_hash)
6273 domain->dynamic_code_hash = g_hash_table_new (NULL, NULL);
6274 g_hash_table_insert (domain->dynamic_code_hash, method, ji);
6277 static MonoJitDynamicMethodInfo*
6278 mono_dynamic_code_hash_lookup (MonoDomain *domain, MonoMethod *method)
6280 MonoJitDynamicMethodInfo *res;
6282 if (domain->dynamic_code_hash)
6283 res = g_hash_table_lookup (domain->dynamic_code_hash, method);
6290 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func, gboolean no_throw)
6292 MonoJitICallInfo *info;
6294 if (!emul_opcode_map)
6295 emul_opcode_map = g_new0 (MonoJitICallInfo*, OP_LAST + 1);
6297 g_assert (!sig->hasthis);
6298 g_assert (sig->param_count < 3);
6300 info = mono_register_jit_icall (func, name, sig, no_throw);
6302 emul_opcode_map [opcode] = info;
6306 decompose_foreach (MonoInst *tree, gpointer data)
6308 static MonoJitICallInfo *newarr_info = NULL;
6309 static MonoJitICallInfo *newarr_specific_info = NULL;
6310 MonoJitICallInfo *info;
6313 switch (tree->opcode) {
6315 MonoCompile *cfg = data;
6316 MonoInst *iargs [3];
6319 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
6320 g_assert (newarr_info);
6321 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
6322 g_assert (newarr_specific_info);
6325 if (cfg->opt & MONO_OPT_SHARED) {
6326 NEW_DOMAINCONST (cfg, iargs [0]);
6327 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
6328 iargs [2] = tree->inst_newa_len;
6333 MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
6335 NEW_VTABLECONST (cfg, iargs [0], vtable);
6336 iargs [1] = tree->inst_newa_len;
6338 info = newarr_specific_info;
6341 mono_emulate_opcode (cfg, tree, iargs, info);
6343 /* Need to decompose arguments after the the opcode is decomposed */
6344 for (i = 0; i < info->sig->param_count; ++i)
6345 dec_foreach (iargs [i], cfg);
6355 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
6357 switch (mono_burg_arity [tree->opcode]) {
6360 mono_inst_foreach (tree->inst_left, func, data);
6363 mono_inst_foreach (tree->inst_left, func, data);
6364 mono_inst_foreach (tree->inst_right, func, data);
6367 g_assert_not_reached ();
6374 mono_print_bb_code (MonoBasicBlock *bb) {
6376 MonoInst *c = bb->code;
6378 mono_print_tree (c);
6386 print_dfn (MonoCompile *cfg) {
6391 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
6393 for (i = 0; i < cfg->num_bblocks; ++i) {
6394 bb = cfg->bblocks [i];
6395 /*if (bb->cil_code) {
6396 char* code1, *code2;
6397 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
6398 if (bb->last_ins->cil_code)
6399 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
6401 code2 = g_strdup ("");
6403 code1 [strlen (code1) - 1] = 0;
6404 code = g_strdup_printf ("%s -> %s", code1, code2);
6408 code = g_strdup ("\n");
6409 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
6411 MonoInst *c = bb->code;
6413 mono_print_tree (c);
6421 g_print ("\tprev:");
6422 for (j = 0; j < bb->in_count; ++j) {
6423 g_print (" BB%d", bb->in_bb [j]->block_num);
6425 g_print ("\t\tsucc:");
6426 for (j = 0; j < bb->out_count; ++j) {
6427 g_print (" BB%d", bb->out_bb [j]->block_num);
6429 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
6432 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
6435 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
6437 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
6445 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
6449 g_assert (bb->code);
6450 bb->last_ins->next = inst;
6451 bb->last_ins = inst;
6453 bb->last_ins = bb->code = inst;
6458 mono_destroy_compile (MonoCompile *cfg)
6460 //mono_mempool_stats (cfg->mempool);
6461 g_hash_table_destroy (cfg->bb_hash);
6462 mono_free_loop_info (cfg);
6464 mono_regstate_free (cfg->rs);
6466 g_hash_table_destroy (cfg->spvars);
6467 mono_mempool_destroy (cfg->mempool);
6468 g_list_free (cfg->ldstr_list);
6470 g_free (cfg->varinfo);
6475 #ifdef HAVE_KW_THREAD
6476 static __thread gpointer mono_lmf_addr;
6480 mono_get_lmf_addr (void)
6482 #ifdef HAVE_KW_THREAD
6483 return mono_lmf_addr;
6485 MonoJitTlsData *jit_tls;
6487 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
6488 return &jit_tls->lmf;
6490 g_assert_not_reached ();
6496 * mono_thread_abort:
6497 * @obj: exception object
6499 * abort the thread, print exception information and stack trace
6502 mono_thread_abort (MonoObject *obj)
6504 /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
6506 /* handle_remove should be eventually called for this thread, too
6509 mono_thread_exit ();
6513 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
6515 MonoJitTlsData *jit_tls;
6518 jit_tls = TlsGetValue (mono_jit_tls_id);
6522 jit_tls = g_new0 (MonoJitTlsData, 1);
6524 TlsSetValue (mono_jit_tls_id, jit_tls);
6526 jit_tls->abort_func = abort_func;
6527 jit_tls->end_of_stack = stack_start;
6529 lmf = g_new0 (MonoLMF, 1);
6532 jit_tls->lmf = jit_tls->first_lmf = lmf;
6534 #ifdef HAVE_KW_THREAD
6535 mono_lmf_addr = &jit_tls->lmf;
6538 mono_arch_setup_jit_tls_data (jit_tls);
6544 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
6547 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
6548 thread = mono_thread_current ();
6550 thread->jit_data = jit_tls;
6553 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
6556 mono_thread_abort_dummy (MonoObject *obj)
6558 if (mono_thread_attach_aborted_cb)
6559 mono_thread_attach_aborted_cb (obj);
6561 mono_thread_abort (obj);
6565 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
6568 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
6569 thread = mono_thread_current ();
6571 thread->jit_data = jit_tls;
6575 mini_thread_cleanup (MonoThread *thread)
6577 MonoJitTlsData *jit_tls = thread->jit_data;
6580 mono_arch_free_jit_tls_data (jit_tls);
6581 g_free (jit_tls->first_lmf);
6583 thread->jit_data = NULL;
6588 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
6590 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
6594 ji->data.target = target;
6595 ji->next = cfg->patch_info;
6597 cfg->patch_info = ji;
6601 mono_remove_patch_info (MonoCompile *cfg, int ip)
6603 MonoJumpInfo **ji = &cfg->patch_info;
6606 if ((*ji)->ip.i == ip)
6609 ji = &((*ji)->next);
6614 mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors)
6616 unsigned char *ip = patch_info->ip.i + code;
6617 gconstpointer target = NULL;
6619 switch (patch_info->type) {
6620 case MONO_PATCH_INFO_BB:
6621 target = patch_info->data.bb->native_offset + code;
6623 case MONO_PATCH_INFO_ABS:
6624 target = patch_info->data.target;
6626 case MONO_PATCH_INFO_LABEL:
6627 target = patch_info->data.inst->inst_c0 + code;
6629 case MONO_PATCH_INFO_IP:
6632 case MONO_PATCH_INFO_METHOD_REL:
6633 target = code + patch_info->data.offset;
6635 case MONO_PATCH_INFO_INTERNAL_METHOD: {
6636 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
6638 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
6639 g_assert_not_reached ();
6641 target = mono_icall_get_wrapper (mi);
6644 case MONO_PATCH_INFO_METHOD_JUMP: {
6647 /* get the trampoline to the method from the domain */
6648 target = mono_create_jump_trampoline (domain, patch_info->data.method, TRUE);
6649 if (!domain->jump_target_hash)
6650 domain->jump_target_hash = g_hash_table_new (NULL, NULL);
6651 list = g_hash_table_lookup (domain->jump_target_hash, patch_info->data.method);
6652 list = g_slist_prepend (list, ip);
6653 g_hash_table_insert (domain->jump_target_hash, patch_info->data.method, list);
6656 case MONO_PATCH_INFO_METHOD:
6657 if (patch_info->data.method == method) {
6660 /* get the trampoline to the method from the domain */
6661 target = mono_create_jit_trampoline (patch_info->data.method);
6663 case MONO_PATCH_INFO_SWITCH: {
6664 gpointer *jump_table;
6667 if (method->dynamic) {
6668 jump_table = mono_code_manager_reserve (mono_dynamic_code_hash_lookup (domain, method)->code_mp, sizeof (gpointer) * patch_info->table_size);
6670 mono_domain_lock (domain);
6671 jump_table = mono_code_manager_reserve (domain->code_mp, sizeof (gpointer) * patch_info->table_size);
6672 mono_domain_unlock (domain);
6675 for (i = 0; i < patch_info->table_size; i++) {
6676 jump_table [i] = code + (int)patch_info->data.table [i];
6678 target = jump_table;
6681 case MONO_PATCH_INFO_METHODCONST:
6682 case MONO_PATCH_INFO_CLASS:
6683 case MONO_PATCH_INFO_IMAGE:
6684 case MONO_PATCH_INFO_FIELD:
6685 target = patch_info->data.target;
6687 case MONO_PATCH_INFO_IID:
6688 mono_class_init (patch_info->data.klass);
6689 target = (gpointer)patch_info->data.klass->interface_id;
6691 case MONO_PATCH_INFO_VTABLE:
6692 target = mono_class_vtable (domain, patch_info->data.klass);
6694 case MONO_PATCH_INFO_CLASS_INIT:
6695 target = mono_create_class_init_trampoline (mono_class_vtable (domain, patch_info->data.klass));
6697 case MONO_PATCH_INFO_SFLDA: {
6698 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
6699 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (vtable->klass, method))
6700 /* Done by the generated code */
6704 mono_runtime_class_init (vtable);
6706 target = (char*)vtable->data + patch_info->data.field->offset;
6709 case MONO_PATCH_INFO_R4:
6710 case MONO_PATCH_INFO_R8:
6711 target = patch_info->data.target;
6713 case MONO_PATCH_INFO_EXC_NAME:
6714 target = patch_info->data.name;
6716 case MONO_PATCH_INFO_LDSTR:
6718 mono_ldstr (domain, patch_info->data.token->image,
6719 mono_metadata_token_index (patch_info->data.token->token));
6721 case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
6723 MonoClass *handle_class;
6725 handle = mono_ldtoken (patch_info->data.token->image,
6726 patch_info->data.token->token, &handle_class, NULL);
6727 mono_class_init (handle_class);
6728 mono_class_init (mono_class_from_mono_type (handle));
6731 mono_type_get_object (domain, handle);
6734 case MONO_PATCH_INFO_LDTOKEN: {
6736 MonoClass *handle_class;
6738 handle = mono_ldtoken (patch_info->data.token->image,
6739 patch_info->data.token->token, &handle_class, NULL);
6740 mono_class_init (handle_class);
6745 case MONO_PATCH_INFO_BB_OVF:
6746 case MONO_PATCH_INFO_EXC_OVF:
6749 g_assert_not_reached ();
6752 return (gpointer)target;
6756 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
6757 MonoJitICallInfo *info;
6759 decompose_foreach (tree, cfg);
6761 switch (mono_burg_arity [tree->opcode]) {
6764 dec_foreach (tree->inst_left, cfg);
6766 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6767 MonoInst *iargs [2];
6769 iargs [0] = tree->inst_left;
6771 mono_emulate_opcode (cfg, tree, iargs, info);
6777 #ifdef MONO_ARCH_BIGMUL_INTRINS
6778 if (tree->opcode == OP_LMUL
6779 && (cfg->opt & MONO_OPT_INTRINS)
6780 && (tree->inst_left->opcode == CEE_CONV_I8
6781 || tree->inst_left->opcode == CEE_CONV_U8)
6782 && tree->inst_left->inst_left->type == STACK_I4
6783 && (tree->inst_right->opcode == CEE_CONV_I8
6784 || tree->inst_right->opcode == CEE_CONV_U8)
6785 && tree->inst_right->inst_left->type == STACK_I4
6786 && tree->inst_left->opcode == tree->inst_right->opcode) {
6787 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
6788 tree->inst_left = tree->inst_left->inst_left;
6789 tree->inst_right = tree->inst_right->inst_left;
6790 dec_foreach (tree, cfg);
6793 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6794 MonoInst *iargs [2];
6796 iargs [0] = tree->inst_i0;
6797 iargs [1] = tree->inst_i1;
6799 mono_emulate_opcode (cfg, tree, iargs, info);
6801 dec_foreach (iargs [0], cfg);
6802 dec_foreach (iargs [1], cfg);
6805 dec_foreach (tree->inst_left, cfg);
6806 dec_foreach (tree->inst_right, cfg);
6810 g_assert_not_reached ();
6815 decompose_pass (MonoCompile *cfg) {
6818 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6821 cfg->prev_ins = NULL;
6822 for (tree = cfg->cbb->code; tree; tree = tree->next) {
6823 dec_foreach (tree, cfg);
6824 cfg->prev_ins = tree;
6830 nullify_basic_block (MonoBasicBlock *bb)
6837 bb->code = bb->last_ins = NULL;
6838 bb->cil_code = NULL;
6842 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6846 for (i = 0; i < bb->out_count; i++) {
6847 MonoBasicBlock *ob = bb->out_bb [i];
6850 if (bb->out_count > 1) {
6851 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
6855 bb->out_bb [i] = repl;
6862 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6866 for (i = 0; i < bb->in_count; i++) {
6867 MonoBasicBlock *ib = bb->in_bb [i];
6870 if (bb->in_count > 1) {
6871 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
6875 bb->in_bb [i] = repl;
6882 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6886 for (i = 0; i < bb->out_count; i++) {
6887 MonoBasicBlock *ob = bb->out_bb [i];
6888 for (j = 0; j < ob->in_count; j++) {
6889 if (ob->in_bb [j] == orig) {
6890 ob->in_bb [j] = repl;
6899 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
6901 bb->out_count = bbn->out_count;
6902 bb->out_bb = bbn->out_bb;
6904 replace_basic_block (bb, bbn, bb);
6908 bb->last_ins->next = bbn->code;
6909 bb->last_ins = bbn->last_ins;
6912 bb->code = bbn->code;
6913 bb->last_ins = bbn->last_ins;
6915 bb->next_bb = bbn->next_bb;
6916 nullify_basic_block (bbn);
6920 * Optimizes the branches on the Control Flow Graph
6924 optimize_branches (MonoCompile *cfg)
6926 int i, changed = FALSE;
6927 MonoBasicBlock *bb, *bbn;
6928 guint32 niterations;
6931 * Some crazy loops could cause the code below to go into an infinite
6932 * loop, see bug #53003 for an example. To prevent this, we put an upper
6933 * bound on the number of iterations.
6940 /* we skip the entry block (exit is handled specially instead ) */
6941 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
6943 /* dont touch code inside exception clauses */
6944 if (bb->region != -1)
6947 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
6948 if (cfg->verbose_level > 2)
6949 g_print ("nullify block triggered %d\n", bbn->block_num);
6951 bb->next_bb = bbn->next_bb;
6953 for (i = 0; i < bbn->out_count; i++)
6954 replace_in_block (bbn->out_bb [i], bbn, NULL);
6956 nullify_basic_block (bbn);
6960 if (bb->out_count == 1) {
6961 bbn = bb->out_bb [0];
6963 /* conditional branches where true and false targets are the same can be also replaced with CEE_BR */
6964 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins)) {
6965 bb->last_ins->opcode = CEE_BR;
6966 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
6968 if (cfg->verbose_level > 2)
6969 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
6972 if (bb->region == bbn->region && bb->next_bb == bbn) {
6973 /* the block are in sequence anyway ... */
6975 /* branches to the following block can be removed */
6976 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
6977 bb->last_ins->opcode = CEE_NOP;
6979 if (cfg->verbose_level > 2)
6980 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
6983 if (bbn->in_count == 1) {
6985 if (bbn != cfg->bb_exit) {
6986 if (cfg->verbose_level > 2)
6987 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
6988 merge_basic_blocks (bb, bbn);
6992 //mono_print_bb_code (bb);
6997 } while (changed && (niterations > 0));
7004 /* we skip the entry block (exit is handled specially instead ) */
7005 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
7007 /* dont touch code inside exception clauses */
7008 if (bb->region != -1)
7011 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
7012 if (cfg->verbose_level > 2) {
7013 g_print ("nullify block triggered %d\n", bbn->block_num);
7015 bb->next_bb = bbn->next_bb;
7017 for (i = 0; i < bbn->out_count; i++)
7018 replace_in_block (bbn->out_bb [i], bbn, NULL);
7020 nullify_basic_block (bbn);
7026 if (bb->out_count == 1) {
7027 bbn = bb->out_bb [0];
7029 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
7030 bbn = bb->last_ins->inst_target_bb;
7031 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7032 bbn->code->inst_target_bb->region == bb->region) {
7034 if (cfg->verbose_level > 2)
7035 g_print ("in %s branch to branch triggered %d -> %d -> %d\n", cfg->method->name,
7036 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num);
7038 replace_in_block (bbn, bb, NULL);
7039 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7040 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7041 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
7046 } else if (bb->out_count == 2) {
7047 if (bb->last_ins && MONO_IS_COND_BRANCH_NOFP (bb->last_ins)) {
7048 bbn = bb->last_ins->inst_true_bb;
7049 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7050 bbn->code->inst_target_bb->region == bb->region) {
7051 if (cfg->verbose_level > 2)
7052 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
7053 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
7056 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
7058 replace_in_block (bbn, bb, NULL);
7060 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
7061 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7063 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7069 bbn = bb->last_ins->inst_false_bb;
7070 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7071 bbn->code->inst_target_bb->region == bb->region) {
7072 if (cfg->verbose_level > 2)
7073 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
7074 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
7077 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
7079 replace_in_block (bbn, bb, NULL);
7081 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
7082 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7084 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7092 } while (changed && (niterations > 0));
7097 mono_compile_create_vars (MonoCompile *cfg)
7099 MonoMethodSignature *sig;
7100 MonoMethodHeader *header;
7103 header = mono_method_get_header (cfg->method);
7105 sig = cfg->method->signature;
7107 if (!MONO_TYPE_IS_VOID (sig->ret)) {
7108 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
7109 cfg->ret->opcode = OP_RETARG;
7110 cfg->ret->inst_vtype = sig->ret;
7111 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
7113 if (cfg->verbose_level > 2)
7114 g_print ("creating vars\n");
7117 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
7119 for (i = 0; i < sig->param_count; ++i)
7120 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
7122 cfg->locals_start = cfg->num_varinfo;
7124 if (cfg->verbose_level > 2)
7125 g_print ("creating locals\n");
7126 for (i = 0; i < header->num_locals; ++i)
7127 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
7128 if (cfg->verbose_level > 2)
7129 g_print ("locals done\n");
7133 mono_print_code (MonoCompile *cfg)
7137 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7138 MonoInst *tree = bb->code;
7143 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
7145 for (; tree; tree = tree->next) {
7146 mono_print_tree (tree);
7151 bb->last_ins->next = NULL;
7155 extern const char * const mono_burg_rule_string [];
7158 emit_state (MonoCompile *cfg, MBState *state, int goal)
7161 int ern = mono_burg_rule (state, goal);
7162 const guint16 *nts = mono_burg_nts [ern];
7165 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
7169 // state->reg1 = state->reg2; /* chain rule */
7171 #ifdef MONO_ARCH_ENABLE_EMIT_STATE_OPT
7174 state->reg1 = mono_regstate_next_int (cfg->rs);
7175 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
7178 state->reg1 = mono_regstate_next_int (cfg->rs);
7179 state->reg2 = mono_regstate_next_int (cfg->rs);
7182 state->reg1 = mono_regstate_next_float (cfg->rs);
7185 #ifdef MONO_ARCH_ENABLE_EMIT_STATE_OPT
7187 * Enabling this might cause bugs to surface in the local register
7188 * allocators on some architectures like x86.
7190 if ((state->tree->ssa_op == MONO_SSA_STORE) && (state->left->tree->opcode == OP_REGVAR)) {
7191 /* Do not optimize away reg-reg moves */
7192 if (! ((state->right->tree->ssa_op == MONO_SSA_LOAD) && (state->right->left->tree->opcode == OP_REGVAR))) {
7193 state->right->reg1 = state->left->tree->dreg;
7202 mono_burg_kids (state, ern, kids);
7204 emit_state (cfg, kids [0], nts [0]);
7206 emit_state (cfg, kids [1], nts [1]);
7208 g_assert (!nts [3]);
7209 emit_state (cfg, kids [2], nts [2]);
7214 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
7215 if ((emit = mono_burg_func [ern]))
7216 emit (state, state->tree, cfg);
7219 #define DEBUG_SELECTION
7222 mini_select_instructions (MonoCompile *cfg)
7224 static const int reverse_map [] = {
7225 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
7226 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
7228 static const int reverse_fmap [] = {
7229 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
7230 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
7232 static const int reverse_lmap [] = {
7233 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
7234 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
7236 static const int reverse_imap [] = {
7237 OP_IBNE_UN, OP_IBLT, OP_IBLE, OP_IBGT, OP_IBGE,
7238 OP_IBEQ, OP_IBLT_UN, OP_IBLE_UN, OP_IBGT_UN, OP_IBGE_UN
7243 cfg->state_pool = mono_mempool_new ();
7244 cfg->rs = mono_regstate_new ();
7246 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7247 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins) &&
7248 bb->next_bb != bb->last_ins->inst_false_bb) {
7250 /* we are careful when inverting, since bugs like #59580
7251 * could show up when dealing with NaNs.
7253 if (MONO_IS_COND_BRANCH_NOFP(bb->last_ins) && bb->next_bb == bb->last_ins->inst_true_bb) {
7254 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
7255 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
7256 bb->last_ins->inst_false_bb = tmp;
7258 if (bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
7259 bb->last_ins->opcode = reverse_map [bb->last_ins->opcode - CEE_BEQ];
7260 } else if (bb->last_ins->opcode >= OP_FBEQ && bb->last_ins->opcode <= OP_FBLT_UN) {
7261 bb->last_ins->opcode = reverse_fmap [bb->last_ins->opcode - OP_FBEQ];
7262 } else if (bb->last_ins->opcode >= OP_LBEQ && bb->last_ins->opcode <= OP_LBLT_UN) {
7263 bb->last_ins->opcode = reverse_lmap [bb->last_ins->opcode - OP_LBEQ];
7264 } else if (bb->last_ins->opcode >= OP_IBEQ && bb->last_ins->opcode <= OP_IBLT_UN) {
7265 bb->last_ins->opcode = reverse_imap [bb->last_ins->opcode - OP_IBEQ];
7268 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
7269 inst->opcode = CEE_BR;
7270 inst->inst_target_bb = bb->last_ins->inst_false_bb;
7271 mono_bblock_add_inst (bb, inst);
7276 #ifdef DEBUG_SELECTION
7277 if (cfg->verbose_level >= 4) {
7278 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7279 MonoInst *tree = bb->code;
7280 g_print ("DUMP BLOCK %d:\n", bb->block_num);
7283 for (; tree; tree = tree->next) {
7284 mono_print_tree (tree);
7291 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7292 MonoInst *tree = bb->code, *next;
7298 bb->last_ins = NULL;
7301 mono_regstate_reset (cfg->rs);
7303 #ifdef DEBUG_SELECTION
7304 if (cfg->verbose_level >= 3)
7305 g_print ("LABEL BLOCK %d:\n", bb->block_num);
7307 for (; tree; tree = next) {
7309 #ifdef DEBUG_SELECTION
7310 if (cfg->verbose_level >= 3) {
7311 mono_print_tree (tree);
7316 if (!(mbstate = mono_burg_label (tree, cfg))) {
7317 g_warning ("unable to label tree %p", tree);
7318 mono_print_tree (tree);
7320 g_assert_not_reached ();
7322 emit_state (cfg, mbstate, MB_NTERM_stmt);
7324 bb->max_ireg = cfg->rs->next_vireg;
7325 bb->max_freg = cfg->rs->next_vfreg;
7328 bb->last_ins->next = NULL;
7330 mono_mempool_empty (cfg->state_pool);
7332 mono_mempool_destroy (cfg->state_pool);
7336 mono_codegen (MonoCompile *cfg)
7338 MonoJumpInfo *patch_info;
7340 int i, max_epilog_size;
7343 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7344 cfg->spill_count = 0;
7345 /* we reuse dfn here */
7346 /* bb->dfn = bb_count++; */
7347 mono_arch_local_regalloc (cfg, bb);
7350 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
7351 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
7353 code = mono_arch_emit_prolog (cfg);
7355 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7356 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
7358 cfg->code_len = code - cfg->native_code;
7359 cfg->prolog_end = cfg->code_len;
7361 mono_debug_open_method (cfg);
7363 /* emit code all basic blocks */
7364 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7365 bb->native_offset = cfg->code_len;
7366 mono_arch_output_basic_block (cfg, bb);
7368 cfg->bb_exit->native_offset = cfg->code_len;
7370 code = cfg->native_code + cfg->code_len;
7372 max_epilog_size = mono_arch_max_epilog_size (cfg);
7374 /* we always allocate code in cfg->domain->code_mp to increase locality */
7375 cfg->code_size = cfg->code_len + max_epilog_size;
7376 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
7378 if (cfg->method->dynamic) {
7379 /* Allocate the code into a separate memory pool so it can be freed */
7380 cfg->dynamic_info = g_new0 (MonoJitDynamicMethodInfo, 1);
7381 cfg->dynamic_info->code_mp = mono_code_manager_new_dynamic ();
7382 mono_domain_lock (cfg->domain);
7383 mono_dynamic_code_hash_insert (cfg->domain, cfg->method, cfg->dynamic_info);
7384 mono_domain_unlock (cfg->domain);
7386 code = mono_code_manager_reserve (cfg->dynamic_info->code_mp, cfg->code_size);
7388 mono_domain_lock (cfg->domain);
7389 code = mono_code_manager_reserve (cfg->domain->code_mp, cfg->code_size);
7390 mono_domain_unlock (cfg->domain);
7393 memcpy (code, cfg->native_code, cfg->code_len);
7394 g_free (cfg->native_code);
7395 cfg->native_code = code;
7396 code = cfg->native_code + cfg->code_len;
7398 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
7400 cfg->epilog_begin = cfg->code_len;
7402 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7403 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
7405 cfg->code_len = code - cfg->native_code;
7407 mono_arch_emit_epilog (cfg);
7409 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7410 switch (patch_info->type) {
7411 case MONO_PATCH_INFO_ABS: {
7412 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
7414 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
7415 if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) &&
7416 strstr (cfg->method->name, info->name))
7418 * This is an icall wrapper, and this is a call to the
7423 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
7424 patch_info->data.name = info->name;
7428 MonoVTable *vtable = mono_find_class_init_trampoline_by_addr (patch_info->data.target);
7430 patch_info->type = MONO_PATCH_INFO_CLASS_INIT;
7431 patch_info->data.klass = vtable->klass;
7436 case MONO_PATCH_INFO_SWITCH: {
7438 if (cfg->method->dynamic) {
7439 table = mono_code_manager_reserve (cfg->dynamic_info->code_mp, sizeof (gpointer) * patch_info->table_size);
7441 mono_domain_lock (cfg->domain);
7442 table = mono_code_manager_reserve (cfg->domain->code_mp, sizeof (gpointer) * patch_info->table_size);
7443 mono_domain_unlock (cfg->domain);
7446 patch_info->ip.i = patch_info->ip.label->inst_c0;
7447 for (i = 0; i < patch_info->table_size; i++) {
7448 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
7450 patch_info->data.target = table;
7459 if (cfg->verbose_level > 0)
7460 g_print ("Method %s emitted at %p to %p [%s]\n",
7461 mono_method_full_name (cfg->method, TRUE),
7462 cfg->native_code, cfg->native_code + cfg->code_len, cfg->domain->friendly_name);
7464 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info, cfg->run_cctors);
7466 if (cfg->method->dynamic) {
7467 mono_code_manager_commit (cfg->dynamic_info->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
7469 mono_domain_lock (cfg->domain);
7470 mono_code_manager_commit (cfg->domain->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
7471 mono_domain_unlock (cfg->domain);
7474 mono_arch_flush_icache (cfg->native_code, cfg->code_len);
7476 mono_debug_close_method (cfg);
7480 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
7485 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
7486 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
7488 if (cp->opcode == OP_ICONST) {
7489 if (cfg->opt & MONO_OPT_CONSPROP) {
7490 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
7494 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
7495 if (cfg->opt & MONO_OPT_COPYPROP) {
7496 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
7502 arity = mono_burg_arity [tree->opcode];
7505 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
7506 if (cfg->opt & MONO_OPT_CFOLD)
7507 mono_constant_fold_inst (tree, NULL);
7508 /* The opcode may have changed */
7509 if (mono_burg_arity [tree->opcode] > 1) {
7510 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
7511 if (cfg->opt & MONO_OPT_CFOLD)
7512 mono_constant_fold_inst (tree, NULL);
7514 mono_constant_fold_inst (tree, NULL);
7520 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
7524 switch (tree->opcode) {
7534 if (tree->ssa_op == MONO_SSA_NOP) {
7535 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7552 case OP_VOIDCALL_REG:
7553 case OP_VOIDCALLVIRT:
7555 MonoCallInst *call = (MonoCallInst *)tree;
7556 MonoMethodSignature *sig = call->signature;
7557 int i, byref = FALSE;
7559 for (i = 0; i < sig->param_count; i++) {
7560 if (sig->params [i]->byref) {
7567 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7575 arity = mono_burg_arity [tree->opcode];
7581 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7584 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7585 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
7588 g_assert_not_reached ();
7593 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
7595 MonoInst *tree = bb->code;
7601 for (; tree; tree = tree->next) {
7603 mono_cprop_copy_values (cfg, tree, acp);
7605 mono_cprop_invalidate_values (tree, acp, acp_size);
7607 if (tree->ssa_op == MONO_SSA_STORE &&
7608 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
7609 MonoInst *i1 = tree->inst_i1;
7611 acp [tree->inst_i0->inst_c0] = NULL;
7613 for (i = 0; i < acp_size; i++) {
7614 if (acp [i] && acp [i]->opcode != OP_ICONST &&
7615 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
7620 if (i1->opcode == OP_ICONST) {
7621 acp [tree->inst_i0->inst_c0] = i1;
7622 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
7624 if (i1->ssa_op == MONO_SSA_LOAD &&
7625 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
7626 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
7627 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
7628 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
7633 if (tree->opcode == CEE_BEQ) {
7634 g_assert (tree->inst_i0->opcode == OP_COMPARE);
7635 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
7636 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
7638 tree->opcode = CEE_BR;
7639 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
7640 tree->inst_target_bb = tree->inst_true_bb;
7642 tree->inst_target_bb = tree->inst_false_bb;
7651 mono_local_cprop (MonoCompile *cfg)
7656 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
7658 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7659 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
7660 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
7665 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, int parts)
7667 MonoMethodHeader *header = mono_method_get_header (method);
7668 guint8 *ip = (guint8 *)header->code;
7671 int dfn = 0, i, code_size_ratio;
7673 mono_jit_stats.methods_compiled++;
7674 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
7675 mono_profiler_method_jit (method);
7677 cfg = g_new0 (MonoCompile, 1);
7678 cfg->method = method;
7679 cfg->mempool = mono_mempool_new ();
7681 cfg->prof_options = mono_profiler_get_events ();
7682 cfg->run_cctors = run_cctors;
7683 cfg->bb_hash = g_hash_table_new (NULL, NULL);
7684 cfg->domain = domain;
7685 cfg->verbose_level = mini_verbose;
7686 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
7687 mono_method_get_header (method)->max_stack);
7689 if (cfg->verbose_level > 2)
7690 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
7693 * create MonoInst* which represents arguments and local variables
7695 mono_compile_create_vars (cfg);
7697 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
7698 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7699 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
7700 mono_destroy_compile (cfg);
7704 mono_jit_stats.basic_blocks += cfg->num_bblocks;
7705 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
7707 if (cfg->num_varinfo > 2000) {
7709 * we disable some optimizations if there are too many variables
7710 * because JIT time may become too expensive. The actual number needs
7711 * to be tweaked and eventually the non-linear algorithms should be fixed.
7713 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
7714 cfg->disable_ssa = TRUE;
7716 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
7718 /* Depth-first ordering on basic blocks */
7719 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
7721 if (cfg->opt & MONO_OPT_BRANCH)
7722 optimize_branches (cfg);
7724 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
7725 if (cfg->num_bblocks != dfn + 1) {
7728 cfg->num_bblocks = dfn + 1;
7730 if (!header->clauses) {
7731 /* remove unreachable code, because the code in them may be
7732 * inconsistent (access to dead variables for example) */
7733 for (bb = cfg->bb_entry; bb;) {
7734 MonoBasicBlock *bbn = bb->next_bb;
7736 if (bbn && bbn->region == -1 && !bbn->dfn) {
7737 if (cfg->verbose_level > 1)
7738 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
7739 bb->next_bb = bbn->next_bb;
7740 nullify_basic_block (bbn);
7748 if (cfg->opt & MONO_OPT_LOOP) {
7749 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
7750 mono_compute_natural_loops (cfg);
7753 /* after method_to_ir */
7757 //#define DEBUGSSA "logic_run"
7758 #define DEBUGSSA_CLASS "Tests"
7761 if (!header->num_clauses && !cfg->disable_ssa) {
7762 mono_local_cprop (cfg);
7763 mono_ssa_compute (cfg);
7767 /* fixme: add all optimizations which requires SSA */
7768 if (cfg->opt & (MONO_OPT_DEADCE | MONO_OPT_ABCREM)) {
7769 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
7770 mono_local_cprop (cfg);
7771 mono_ssa_compute (cfg);
7773 if (cfg->verbose_level >= 2) {
7780 /* after SSA translation */
7784 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
7785 if (cfg->comp_done & MONO_COMP_SSA) {
7786 mono_ssa_cprop (cfg);
7788 mono_local_cprop (cfg);
7792 if (cfg->comp_done & MONO_COMP_SSA) {
7793 mono_ssa_deadce (cfg);
7795 //mono_ssa_strength_reduction (cfg);
7797 if ((cfg->flags & MONO_CFG_HAS_LDELEMA) && (cfg->opt & MONO_OPT_ABCREM))
7798 mono_perform_abc_removal (cfg);
7800 mono_ssa_remove (cfg);
7802 if (cfg->opt & MONO_OPT_BRANCH)
7803 optimize_branches (cfg);
7806 /* after SSA removal */
7810 decompose_pass (cfg);
7812 if (cfg->opt & MONO_OPT_LINEARS) {
7815 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
7816 cfg->comp_done &= ~MONO_COMP_LIVENESS;
7817 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
7818 mono_analyze_liveness (cfg);
7820 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
7821 regs = mono_arch_get_global_int_regs (cfg);
7822 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
7826 //mono_print_code (cfg);
7830 /* variables are allocated after decompose, since decompose could create temps */
7831 mono_arch_allocate_vars (cfg);
7833 if (cfg->opt & MONO_OPT_CFOLD)
7834 mono_constant_fold (cfg);
7836 mini_select_instructions (cfg);
7839 if (cfg->verbose_level >= 2) {
7840 char *id = mono_method_full_name (cfg->method, FALSE);
7841 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
7845 if (cfg->method->dynamic)
7846 jinfo = g_new0 (MonoJitInfo, 1);
7848 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
7850 jinfo->method = method;
7851 jinfo->code_start = cfg->native_code;
7852 jinfo->code_size = cfg->code_len;
7853 jinfo->used_regs = cfg->used_int_regs;
7854 jinfo->domain_neutral = (cfg->opt & MONO_OPT_SHARED) != 0;
7856 if (header->num_clauses) {
7859 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
7860 jinfo->num_clauses = header->num_clauses;
7861 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
7862 sizeof (MonoJitExceptionInfo) * header->num_clauses);
7864 for (i = 0; i < header->num_clauses; i++) {
7865 MonoExceptionClause *ec = &header->clauses [i];
7866 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
7867 MonoBasicBlock *tblock;
7869 ei->flags = ec->flags;
7871 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7872 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->data.filter_offset);
7874 ei->data.filter = cfg->native_code + tblock->native_offset;
7876 ei->data.catch_class = ec->data.catch_class;
7879 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
7881 ei->try_start = cfg->native_code + tblock->native_offset;
7882 g_assert (tblock->native_offset);
7883 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
7885 ei->try_end = cfg->native_code + tblock->native_offset;
7886 g_assert (tblock->native_offset);
7887 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
7889 ei->handler_start = cfg->native_code + tblock->native_offset;
7893 cfg->jit_info = jinfo;
7895 mono_jit_info_table_add (cfg->domain, jinfo);
7897 if (cfg->method->dynamic)
7898 mono_dynamic_code_hash_lookup (cfg->domain, cfg->method)->ji = jinfo;
7900 /* collect statistics */
7901 mono_jit_stats.allocated_code_size += cfg->code_len;
7902 code_size_ratio = cfg->code_len;
7903 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
7904 mono_jit_stats.biggest_method_size = code_size_ratio;
7905 mono_jit_stats.biggest_method = method;
7907 code_size_ratio = (code_size_ratio * 100) / mono_method_get_header (method)->code_size;
7908 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
7909 mono_jit_stats.max_code_size_ratio = code_size_ratio;
7910 mono_jit_stats.max_ratio_method = method;
7912 mono_jit_stats.native_code_size += cfg->code_len;
7914 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7915 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
7921 mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain)
7924 GHashTable *jit_code_hash;
7925 gpointer code = NULL;
7931 jit_code_hash = target_domain->jit_code_hash;
7933 #ifdef MONO_USE_AOT_COMPILER
7934 if (!mono_compile_aot && (opt & MONO_OPT_AOT)) {
7936 MonoDomain *domain = mono_domain_get ();
7938 mono_domain_lock (domain);
7940 mono_class_init (method->klass);
7941 if ((info = mono_aot_get_method (domain, method))) {
7942 g_hash_table_insert (domain->jit_code_hash, method, info);
7943 mono_domain_unlock (domain);
7944 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
7945 return info->code_start;
7948 mono_domain_unlock (domain);
7952 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
7953 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
7955 MonoMethodPInvoke* piinfo = (MonoMethodPInvoke *) method;
7957 if (!piinfo->addr) {
7958 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
7959 piinfo->addr = mono_lookup_internal_call (method);
7961 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
7962 mono_lookup_pinvoke_call (method, NULL, NULL);
7964 nm = mono_marshal_get_native_wrapper (method);
7965 return mono_compile_method (nm);
7967 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
7968 //mono_debug_add_wrapper (method, nm);
7969 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
7970 const char *name = method->name;
7973 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
7974 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
7975 /* FIXME: uhm, we need a wrapper to handle exceptions? */
7976 return (gpointer)mono_delegate_ctor;
7977 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
7978 nm = mono_marshal_get_delegate_invoke (method);
7979 return mono_jit_compile_method (nm);
7980 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
7981 nm = mono_marshal_get_delegate_begin_invoke (method);
7982 return mono_jit_compile_method (nm);
7983 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
7984 nm = mono_marshal_get_delegate_end_invoke (method);
7985 return mono_jit_compile_method (nm);
7991 cfg = mini_method_compile (method, opt, target_domain, TRUE, 0);
7993 mono_domain_lock (target_domain);
7995 /* Check if some other thread already did the job. In this case, we can
7996 discard the code this thread generated. */
7998 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
7999 /* We can't use a domain specific method in another domain */
8000 if ((target_domain == mono_domain_get ()) || info->domain_neutral) {
8001 code = info->code_start;
8002 // printf("Discarding code for method %s\n", method->name);
8007 g_hash_table_insert (jit_code_hash, method, cfg->jit_info);
8008 code = cfg->native_code;
8011 mono_destroy_compile (cfg);
8013 if (target_domain->jump_target_hash) {
8014 MonoJumpInfo patch_info;
8016 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
8018 patch_info.next = NULL;
8019 patch_info.ip.i = 0;
8020 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
8021 patch_info.data.method = method;
8022 g_hash_table_remove (target_domain->jump_target_hash, method);
8024 for (tmp = list; tmp; tmp = tmp->next)
8025 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info, TRUE);
8026 g_slist_free (list);
8029 mono_domain_unlock (target_domain);
8031 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
8036 mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt)
8038 /* FIXME: later copy the code from mono */
8039 MonoDomain *target_domain, *domain = mono_domain_get ();
8043 if (opt & MONO_OPT_SHARED)
8044 target_domain = mono_get_root_domain ();
8046 target_domain = domain;
8048 mono_domain_lock (target_domain);
8050 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
8051 /* We can't use a domain specific method in another domain */
8052 if (! ((domain != target_domain) && !info->domain_neutral)) {
8053 mono_domain_unlock (target_domain);
8054 mono_jit_stats.methods_lookups++;
8055 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
8056 return info->code_start;
8060 mono_domain_unlock (target_domain);
8061 p = mono_jit_compile_method_inner (method, target_domain);
8066 mono_jit_compile_method (MonoMethod *method)
8068 return mono_jit_compile_method_with_opt (method, default_opt);
8072 invalidated_delegate_trampoline (MonoClass *klass)
8074 g_error ("Unmanaged code called delegate of type %s which was already garbage collected.\n"
8075 "See http://www.go-mono.com/delegate.html for an explanation and ways to fix this.",
8076 mono_type_full_name (&klass->byval_arg));
8080 * mono_jit_free_method:
8082 * Free all memory allocated by the JIT for METHOD.
8085 mono_jit_free_method (MonoDomain *domain, MonoMethod *method)
8087 MonoJitDynamicMethodInfo *ji;
8089 g_assert (method->dynamic);
8091 mono_domain_lock (domain);
8092 ji = mono_dynamic_code_hash_lookup (domain, method);
8093 mono_domain_unlock (domain);
8094 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
8095 /* FIXME: only enable this with a env var */
8096 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
8098 * Instead of freeing the code, change it to call an error routine
8099 * so people can fix their code.
8102 char *type = mono_type_full_name (&method->klass->byval_arg);
8103 char *type_and_method = g_strdup_printf ("%s.%s", type, method->name);
8106 mono_arch_invalidate_method (ji->ji, invalidated_delegate_trampoline, type_and_method);
8114 mono_domain_lock (domain);
8115 g_hash_table_remove (domain->dynamic_code_hash, ji);
8116 mono_domain_unlock (domain);
8118 mono_code_manager_destroy (ji->code_mp);
8119 mono_jit_info_table_remove (domain, ji->ji);
8125 mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method)
8127 MonoDomain *target_domain;
8130 if (default_opt & MONO_OPT_SHARED)
8131 target_domain = mono_get_root_domain ();
8133 target_domain = domain;
8135 mono_domain_lock (target_domain);
8137 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
8138 /* We can't use a domain specific method in another domain */
8139 if (! ((domain != target_domain) && !info->domain_neutral)) {
8140 mono_domain_unlock (target_domain);
8141 mono_jit_stats.methods_lookups++;
8142 return info->code_start;
8146 mono_domain_unlock (target_domain);
8152 * mono_jit_runtime_invoke:
8153 * @method: the method to invoke
8154 * @obj: this pointer
8155 * @params: array of parameter values.
8156 * @exc: used to catch exceptions objects
8159 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
8162 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc, void* compiled_method);
8163 void* compiled_method;
8165 if (obj == NULL && !(method->flags & METHOD_ATTRIBUTE_STATIC) && !method->string_ctor && (method->wrapper_type == 0)) {
8166 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
8170 invoke = mono_marshal_get_runtime_invoke (method);
8171 runtime_invoke = mono_jit_compile_method (invoke);
8173 /* We need this here becuase mono_marshal_get_runtime_invoke can be place
8174 * the helper method in System.Object and not the target class
8176 mono_runtime_class_init (mono_class_vtable (mono_domain_get (), method->klass));
8178 compiled_method = mono_jit_compile_method (method);
8179 return runtime_invoke (obj, params, exc, compiled_method);
8182 #ifdef PLATFORM_WIN32
8183 #define GET_CONTEXT \
8184 struct sigcontext *ctx = (struct sigcontext*)_dummy;
8187 #define GET_CONTEXT \
8188 void *ctx = context;
8189 #elif defined(sun) // Solaris x86
8190 #define GET_CONTEXT \
8191 ucontext_t *uctx = context; \
8192 struct sigcontext *ctx = (struct sigcontext *)&(uctx->uc_mcontext);
8193 #elif defined(__ppc__) || defined (__powerpc__) || defined (__s390__) || defined (MONO_ARCH_USE_SIGACTION)
8194 #define GET_CONTEXT \
8195 void *ctx = context;
8197 #define GET_CONTEXT \
8198 void **_p = (void **)&_dummy; \
8199 struct sigcontext *ctx = (struct sigcontext *)++_p;
8203 #ifdef MONO_ARCH_USE_SIGACTION
8204 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, siginfo_t *info, void *context)
8206 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy)
8210 SIG_HANDLER_SIGNATURE (sigfpe_signal_handler)
8212 MonoException *exc = NULL;
8213 #ifndef MONO_ARCH_USE_SIGACTION
8218 #if defined(MONO_ARCH_HAVE_IS_INT_OVERFLOW)
8219 if (mono_arch_is_int_overflow (ctx, info))
8220 exc = mono_get_exception_arithmetic ();
8222 exc = mono_get_exception_divide_by_zero ();
8224 exc = mono_get_exception_divide_by_zero ();
8227 mono_arch_handle_exception (ctx, exc, FALSE);
8231 SIG_HANDLER_SIGNATURE (sigill_signal_handler)
8235 exc = mono_get_exception_execution_engine ("SIGILL");
8237 mono_arch_handle_exception (ctx, exc, FALSE);
8240 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8243 sigsegv_signal_handler (int _dummy, siginfo_t *info, void *context)
8246 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
8247 struct sigcontext *ctx = (struct sigcontext *)&(((ucontext_t*)context)->uc_mcontext);
8249 /* Can't allocate memory using Boehm GC on altstack */
8250 if (jit_tls->stack_size &&
8251 ((guint8*)info->si_addr >= (guint8*)jit_tls->end_of_stack - jit_tls->stack_size) &&
8252 ((guint8*)info->si_addr < (guint8*)jit_tls->end_of_stack))
8253 exc = mono_domain_get ()->stack_overflow_ex;
8255 exc = mono_domain_get ()->null_reference_ex;
8257 mono_arch_handle_exception (ctx, exc, FALSE);
8263 SIG_HANDLER_SIGNATURE (sigsegv_signal_handler)
8267 mono_arch_handle_exception (ctx, NULL, FALSE);
8273 SIG_HANDLER_SIGNATURE (sigusr1_signal_handler)
8275 gboolean running_managed;
8280 running_managed = (mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx)) != NULL);
8282 exc = mono_thread_request_interruption (running_managed);
8285 mono_arch_handle_exception (ctx, exc, FALSE);
8289 SIG_HANDLER_SIGNATURE (sigquit_signal_handler)
8294 exc = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
8296 mono_arch_handle_exception (ctx, exc, FALSE);
8300 SIG_HANDLER_SIGNATURE (sigint_signal_handler)
8305 exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
8307 mono_arch_handle_exception (ctx, exc, FALSE);
8310 #ifndef PLATFORM_WIN32
8312 add_signal_handler (int signo, gpointer handler)
8314 struct sigaction sa;
8316 #ifdef MONO_ARCH_USE_SIGACTION
8317 sa.sa_sigaction = handler;
8318 sigemptyset (&sa.sa_mask);
8319 sa.sa_flags = SA_SIGINFO;
8321 sa.sa_handler = handler;
8322 sigemptyset (&sa.sa_mask);
8325 g_assert (sigaction (signo, &sa, NULL) != -1);
8330 mono_runtime_install_handlers (void)
8332 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8333 struct sigaction sa;
8336 #ifdef PLATFORM_WIN32
8338 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
8339 win32_seh_set_handler(SIGILL, sigill_signal_handler);
8340 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
8341 if (getenv ("MONO_DEBUG"))
8342 win32_seh_set_handler(SIGINT, sigint_signal_handler);
8343 #else /* !PLATFORM_WIN32 */
8345 /* libpthreads has its own implementation of sigaction(),
8346 * but it seems to work well with our current exception
8347 * handlers. If not we must call syscall directly instead
8350 if (getenv ("MONO_DEBUG")) {
8351 add_signal_handler (SIGINT, sigint_signal_handler);
8354 add_signal_handler (SIGFPE, sigfpe_signal_handler);
8355 add_signal_handler (SIGQUIT, sigquit_signal_handler);
8356 add_signal_handler (SIGILL, sigill_signal_handler);
8357 add_signal_handler (SIGBUS, sigsegv_signal_handler);
8358 add_signal_handler (mono_thread_get_abort_signal (), sigusr1_signal_handler);
8361 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8362 sa.sa_sigaction = sigsegv_signal_handler;
8363 sigemptyset (&sa.sa_mask);
8364 sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
8365 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
8367 add_signal_handler (SIGSEGV, sigsegv_signal_handler);
8370 #endif /* PLATFORM_WIN32 */
8373 /* mono_jit_create_remoting_trampoline:
8374 * @method: pointer to the method info
8376 * Creates a trampoline which calls the remoting functions. This
8377 * is used in the vtable of transparent proxies.
8379 * Returns: a pointer to the newly created code
8382 mono_jit_create_remoting_trampoline (MonoMethod *method, MonoRemotingTarget target)
8385 guint8 *addr = NULL;
8387 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
8388 (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
8389 nm = mono_marshal_get_remoting_invoke_for_target (method, target);
8390 addr = mono_compile_method (nm);
8392 addr = mono_compile_method (method);
8398 mini_init (const char *filename)
8402 mono_arch_cpu_init ();
8404 if (!g_thread_supported ())
8405 g_thread_init (NULL);
8407 MONO_GC_PRE_INIT ();
8409 mono_jit_tls_id = TlsAlloc ();
8410 setup_jit_tls_data ((gpointer)-1, mono_thread_abort);
8412 InitializeCriticalSection (&jit_mutex);
8416 if (default_opt & MONO_OPT_AOT)
8419 mono_runtime_install_handlers ();
8420 mono_threads_install_cleanup (mini_thread_cleanup);
8422 #define JIT_TRAMPOLINES_WORK
8423 #ifdef JIT_TRAMPOLINES_WORK
8424 mono_install_compile_method (mono_jit_compile_method);
8425 mono_install_free_method (mono_jit_free_method);
8426 mono_install_trampoline (mono_create_jit_trampoline);
8427 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
8429 #define JIT_INVOKE_WORKS
8430 #ifdef JIT_INVOKE_WORKS
8431 mono_install_runtime_invoke (mono_jit_runtime_invoke);
8432 mono_install_handler (mono_arch_get_throw_exception ());
8434 mono_install_stack_walk (mono_jit_walk_stack);
8436 domain = mono_init_from_assembly (filename, filename);
8439 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
8440 ves_icall_get_frame_info);
8441 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
8442 ves_icall_get_trace);
8443 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
8444 mono_runtime_install_handlers);
8447 create_helper_signature ();
8449 #define JIT_CALLS_WORK
8450 #ifdef JIT_CALLS_WORK
8451 /* Needs to be called here since register_jit_icall depends on it */
8452 mono_marshal_init ();
8454 mono_arch_register_lowlevel_calls ();
8455 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
8456 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
8457 mono_register_jit_icall (mono_trace_enter_method, "mono_trace_enter_method", NULL, TRUE);
8458 mono_register_jit_icall (mono_trace_leave_method, "mono_trace_leave_method", NULL, TRUE);
8459 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
8460 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
8462 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
8463 mono_register_jit_icall (mono_arch_get_rethrow_exception (), "mono_arch_rethrow_exception", helper_sig_void_obj, TRUE);
8464 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
8465 helper_sig_void_ptr, TRUE);
8466 mono_register_jit_icall (mono_thread_get_pending_exception, "mono_thread_get_pending_exception", helper_sig_obj_void, FALSE);
8467 mono_register_jit_icall (mono_thread_interruption_checkpoint, "mono_thread_interruption_checkpoint", helper_sig_void_void, FALSE);
8468 mono_register_jit_icall (mono_load_remote_field_new, "mono_load_remote_field_new", helper_sig_obj_obj_ptr_ptr, FALSE);
8469 mono_register_jit_icall (mono_store_remote_field_new, "mono_store_remote_field_new", helper_sig_void_obj_ptr_ptr_obj, FALSE);
8472 * NOTE, NOTE, NOTE, NOTE:
8473 * when adding emulation for some opcodes, remember to also add a dummy
8474 * rule to the burg files, because we need the arity information to be correct.
8476 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult, TRUE);
8477 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un, FALSE);
8478 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf, FALSE);
8479 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv, FALSE);
8480 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un, FALSE);
8481 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem, FALSE);
8482 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un, FALSE);
8484 #ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
8485 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl, TRUE);
8486 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr, TRUE);
8487 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un, TRUE);
8490 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8, FALSE);
8491 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4, FALSE);
8492 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8, FALSE);
8493 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8, FALSE);
8495 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
8496 mono_register_opcode_emulation (OP_FCONV_TO_I8, "__emul_fconv_to_i8", helper_sig_long_double, mono_fconv_i8, FALSE);
8498 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
8499 mono_register_opcode_emulation (CEE_CONV_R_UN, "__emul_conv_r_un", helper_sig_double_int, mono_conv_to_r8_un, FALSE);
8501 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
8502 mono_register_opcode_emulation (OP_LCONV_TO_R8, "__emul_lconv_to_r8", helper_sig_double_long, mono_lconv_to_r8, FALSE);
8504 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
8505 mono_register_opcode_emulation (OP_LCONV_TO_R4, "__emul_lconv_to_r4", helper_sig_float_long, mono_lconv_to_r4, FALSE);
8507 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
8508 mono_register_opcode_emulation (OP_LCONV_TO_R_UN, "__emul_lconv_to_r8_un", helper_sig_double_long, mono_lconv_to_r8_un, FALSE);
8510 #ifdef MONO_ARCH_EMULATE_FREM
8511 mono_register_opcode_emulation (OP_FREM, "__emul_frem", helper_sig_double_double_double, fmod, FALSE);
8514 #if SIZEOF_VOID_P == 4
8515 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4, TRUE);
8518 #warning "fixme: add opcode emulation"
8522 /* other jit icalls */
8523 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
8524 helper_sig_ptr_ptr_ptr, FALSE);
8525 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
8526 mono_register_jit_icall (mono_get_special_static_data, "mono_get_special_static_data", helper_sig_ptr_int, FALSE);
8527 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
8528 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
8529 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
8530 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
8531 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
8532 mono_register_jit_icall (helper_stelem_ref_check, "helper_stelem_ref_check", helper_sig_stelem_ref_check, FALSE);
8533 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
8534 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
8535 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
8536 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
8537 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
8538 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
8539 mono_register_jit_icall (mono_ldftn_nosync, "mono_ldftn_nosync", helper_sig_compile, FALSE);
8540 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
8543 #define JIT_RUNTIME_WORKS
8544 #ifdef JIT_RUNTIME_WORKS
8545 mono_install_runtime_cleanup ((MonoDomainFunc)mini_cleanup);
8546 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
8549 mono_thread_attach (domain);
8553 MonoJitStats mono_jit_stats = {0};
8556 print_jit_stats (void)
8558 if (mono_jit_stats.enabled) {
8559 g_print ("Mono Jit statistics\n");
8560 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
8561 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
8562 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
8563 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
8564 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
8565 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
8566 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
8567 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
8568 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
8569 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
8570 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
8571 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
8572 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
8573 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
8574 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
8575 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
8576 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
8577 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
8579 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
8580 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
8581 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
8582 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
8583 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
8585 g_print ("\nGeneric instances: %ld\n", mono_stats.generic_instance_count);
8586 g_print ("Inflated methods: %ld\n", mono_stats.inflated_method_count);
8587 g_print ("Inflated types: %ld\n", mono_stats.inflated_type_count);
8588 g_print ("Generics metadata size: %ld\n", mono_stats.generics_metadata_size);
8593 mini_cleanup (MonoDomain *domain)
8596 * mono_runtime_cleanup() and mono_domain_finalize () need to
8597 * be called early since they need the execution engine still
8598 * fully working (mono_domain_finalize may invoke managed finalizers
8599 * and mono_runtime_cleanup will wait for other threads to finish).
8601 mono_domain_finalize (domain, 2000);
8603 mono_runtime_cleanup (domain);
8605 mono_profiler_shutdown ();
8607 mono_debug_cleanup ();
8609 #ifdef PLATFORM_WIN32
8610 win32_seh_cleanup();
8613 mono_domain_free (domain, TRUE);
8619 mono_set_defaults (int verbose_level, guint32 opts)
8621 mini_verbose = verbose_level;
8626 mono_precompile_assembly (MonoAssembly *ass, void *user_data)
8628 MonoImage *image = mono_assembly_get_image (ass);
8629 MonoMethod *method, *invoke;
8632 if (mini_verbose > 0)
8633 printf ("PRECOMPILE: %s.\n", mono_image_get_filename (image));
8635 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
8636 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
8637 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
8641 if (mini_verbose > 1) {
8642 char * desc = mono_method_full_name (method, TRUE);
8643 g_print ("Compiling %d %s\n", count, desc);
8646 mono_compile_method (method);
8647 if (strcmp (method->name, "Finalize") == 0) {
8648 invoke = mono_marshal_get_runtime_invoke (method);
8649 mono_compile_method (invoke);
8651 if (method->klass->marshalbyref && method->signature->hasthis) {
8652 invoke = mono_marshal_get_remoting_invoke_with_check (method);
8653 mono_compile_method (invoke);
8658 void mono_precompile_assemblies ()
8660 mono_assembly_foreach ((GFunc)mono_precompile_assembly, NULL);
8664 mini_compile_generic_method (MonoMethod *method, MonoGenericContext *context)
8666 MonoMethod *inflated;
8669 inflated = mono_class_inflate_generic_method (method, context, NULL);
8670 addr = mono_compile_method (inflated);