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) {
968 inst->type = STACK_MP;
973 switch (type->type) {
976 case MONO_TYPE_BOOLEAN:
982 inst->type = STACK_I4;
987 case MONO_TYPE_FNPTR:
988 inst->type = STACK_PTR;
990 case MONO_TYPE_CLASS:
991 case MONO_TYPE_STRING:
992 case MONO_TYPE_OBJECT:
993 case MONO_TYPE_SZARRAY:
994 case MONO_TYPE_ARRAY:
995 inst->type = STACK_OBJ;
999 inst->type = STACK_I8;
1003 inst->type = STACK_R8;
1005 case MONO_TYPE_VALUETYPE:
1006 if (type->data.klass->enumtype) {
1007 type = type->data.klass->enum_basetype;
1010 inst->klass = type->data.klass;
1011 inst->type = STACK_VTYPE;
1014 case MONO_TYPE_TYPEDBYREF:
1015 inst->klass = mono_defaults.typed_reference_class;
1016 inst->type = STACK_VTYPE;
1018 case MONO_TYPE_GENERICINST:
1019 type = type->data.generic_inst->generic_type;
1022 g_error ("unknown type 0x%02x in eval stack type", type->type);
1027 * The following tables are used to quickly validate the IL code in type_from_op ().
1030 bin_num_table [STACK_MAX] [STACK_MAX] = {
1031 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1032 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1033 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1034 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1035 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
1036 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
1037 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1038 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1043 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
1046 /* reduce the size of this table */
1048 bin_int_table [STACK_MAX] [STACK_MAX] = {
1049 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1050 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1051 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1052 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1053 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1054 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1055 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1056 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1060 bin_comp_table [STACK_MAX] [STACK_MAX] = {
1062 {0, 1, 0, 1, 0, 0, 4, 0},
1063 {0, 0, 1, 0, 0, 0, 0, 0},
1064 {0, 1, 0, 1, 0, 2, 4, 0},
1065 {0, 0, 0, 0, 1, 0, 0, 0},
1066 {0, 0, 0, 2, 0, 1, 0, 0},
1067 {0, 4, 0, 4, 0, 0, 3, 0},
1068 {0, 0, 0, 0, 0, 0, 0, 0},
1071 /* reduce the size of this table */
1073 shift_table [STACK_MAX] [STACK_MAX] = {
1074 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1075 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1076 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1077 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1078 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1079 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1080 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1081 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1085 * Tables to map from the non-specific opcode to the matching
1086 * type-specific opcode.
1088 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
1089 static const guint16
1090 binops_op_map [STACK_MAX] = {
1091 0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, OP_PADD-CEE_ADD
1094 /* handles from CEE_NEG to CEE_CONV_U8 */
1095 static const guint16
1096 unops_op_map [STACK_MAX] = {
1097 0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, OP_PNEG-CEE_NEG
1100 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
1101 static const guint16
1102 ovfops_op_map [STACK_MAX] = {
1103 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
1106 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
1107 static const guint16
1108 ovf2ops_op_map [STACK_MAX] = {
1109 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
1112 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
1113 static const guint16
1114 ovf3ops_op_map [STACK_MAX] = {
1115 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
1118 /* handles from CEE_CEQ to CEE_CLT_UN */
1119 static const guint16
1120 ceqops_op_map [STACK_MAX] = {
1121 0, 0, OP_LCEQ-CEE_CEQ, OP_PCEQ-CEE_CEQ, OP_FCEQ-CEE_CEQ, OP_LCEQ-CEE_CEQ
1125 * Sets ins->type (the type on the eval stack) according to the
1126 * type of the opcode and the arguments to it.
1127 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1129 * FIXME: this function sets ins->type unconditionally in some cases, but
1130 * it should set it to invalid for some types (a conv.x on an object)
1133 type_from_op (MonoInst *ins) {
1134 switch (ins->opcode) {
1141 /* FIXME: check unverifiable args for STACK_MP */
1142 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1143 ins->opcode += binops_op_map [ins->type];
1150 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1151 ins->opcode += binops_op_map [ins->type];
1156 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1157 ins->opcode += binops_op_map [ins->type];
1161 /* FIXME: handle some specifics with ins->next->type */
1162 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1163 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))))
1164 ins->opcode = OP_LCOMPARE;
1171 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1172 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1176 ins->type = neg_table [ins->inst_i0->type];
1177 ins->opcode += unops_op_map [ins->type];
1180 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1181 ins->type = ins->inst_i0->type;
1183 ins->type = STACK_INV;
1184 ins->opcode += unops_op_map [ins->type];
1190 ins->type = STACK_I4;
1191 ins->opcode += unops_op_map [ins->inst_i0->type];
1194 ins->type = STACK_R8;
1195 switch (ins->inst_i0->type) {
1200 ins->opcode = OP_LCONV_TO_R_UN;
1204 case CEE_CONV_OVF_I1:
1205 case CEE_CONV_OVF_U1:
1206 case CEE_CONV_OVF_I2:
1207 case CEE_CONV_OVF_U2:
1208 case CEE_CONV_OVF_I4:
1209 case CEE_CONV_OVF_U4:
1210 ins->type = STACK_I4;
1211 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1213 case CEE_CONV_OVF_I_UN:
1214 case CEE_CONV_OVF_U_UN:
1215 ins->type = STACK_PTR;
1216 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1218 case CEE_CONV_OVF_I1_UN:
1219 case CEE_CONV_OVF_I2_UN:
1220 case CEE_CONV_OVF_I4_UN:
1221 case CEE_CONV_OVF_U1_UN:
1222 case CEE_CONV_OVF_U2_UN:
1223 case CEE_CONV_OVF_U4_UN:
1224 ins->type = STACK_I4;
1225 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1228 ins->type = STACK_PTR;
1229 switch (ins->inst_i0->type) {
1235 ins->opcode = OP_LCONV_TO_U;
1238 ins->opcode = OP_FCONV_TO_U;
1244 ins->type = STACK_I8;
1245 ins->opcode += unops_op_map [ins->inst_i0->type];
1247 case CEE_CONV_OVF_I8:
1248 case CEE_CONV_OVF_U8:
1249 ins->type = STACK_I8;
1250 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1252 case CEE_CONV_OVF_U8_UN:
1253 case CEE_CONV_OVF_I8_UN:
1254 ins->type = STACK_I8;
1255 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1259 ins->type = STACK_R8;
1260 ins->opcode += unops_op_map [ins->inst_i0->type];
1263 ins->type = STACK_R8;
1267 ins->type = STACK_I4;
1268 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1271 case CEE_CONV_OVF_I:
1272 case CEE_CONV_OVF_U:
1273 ins->type = STACK_PTR;
1274 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1277 case CEE_ADD_OVF_UN:
1279 case CEE_MUL_OVF_UN:
1281 case CEE_SUB_OVF_UN:
1282 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1283 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1286 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1293 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_MP, STACK_R8, STACK_R8, STACK_OBJ
1296 /* map ldelem.x to the matching ldind.x opcode */
1298 ldelem_to_ldind [] = {
1312 /* map stelem.x to the matching stind.x opcode */
1314 stelem_to_stind [] = {
1328 param_table [STACK_MAX] [STACK_MAX] = {
1333 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1337 switch (args->type) {
1347 for (i = 0; i < sig->param_count; ++i) {
1348 switch (args [i].type) {
1352 if (!sig->params [i]->byref)
1356 if (sig->params [i]->byref)
1358 switch (sig->params [i]->type) {
1359 case MONO_TYPE_CLASS:
1360 case MONO_TYPE_STRING:
1361 case MONO_TYPE_OBJECT:
1362 case MONO_TYPE_SZARRAY:
1363 case MONO_TYPE_ARRAY:
1370 if (sig->params [i]->byref)
1372 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1381 /*if (!param_table [args [i].type] [sig->params [i]->type])
1389 * When we need a pointer to the current domain many times in a method, we
1390 * call mono_domain_get() once and we store the result in a local variable.
1391 * This function returns the variable that represents the MonoDomain*.
1393 inline static MonoInst *
1394 mono_get_domainvar (MonoCompile *cfg)
1396 if (!cfg->domainvar)
1397 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1398 return cfg->domainvar;
1402 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
1405 int num = cfg->num_varinfo;
1407 if ((num + 1) >= cfg->varinfo_count) {
1408 cfg->varinfo_count = (cfg->varinfo_count + 2) * 2;
1409 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
1410 cfg->vars = (MonoMethodVar **)g_realloc (cfg->vars, sizeof (MonoMethodVar*) * cfg->varinfo_count);
1413 /*g_print ("created temp %d of type 0x%x\n", num, type->type);*/
1414 mono_jit_stats.allocate_var++;
1416 MONO_INST_NEW (cfg, inst, opcode);
1417 inst->inst_c0 = num;
1418 inst->inst_vtype = type;
1419 inst->klass = mono_class_from_mono_type (type);
1420 /* if set to 1 the variable is native */
1423 cfg->varinfo [num] = inst;
1425 cfg->vars [num] = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMethodVar));
1426 MONO_INIT_VARINFO (cfg->vars [num], num);
1429 //g_print ("created temp %d of type %s\n", num, mono_type_get_name (type));
1434 type_from_stack_type (MonoInst *ins) {
1435 switch (ins->type) {
1436 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1437 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1438 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1439 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1440 case STACK_MP: return &mono_defaults.int_class->byval_arg;
1441 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1442 case STACK_VTYPE: return &ins->klass->byval_arg;
1444 g_error ("stack type %d to montype not handled\n", ins->type);
1450 array_access_to_klass (int opcode)
1454 return mono_defaults.byte_class;
1456 return mono_defaults.uint16_class;
1459 return mono_defaults.int_class;
1462 return mono_defaults.sbyte_class;
1465 return mono_defaults.int16_class;
1468 return mono_defaults.int32_class;
1470 return mono_defaults.uint32_class;
1473 return mono_defaults.int64_class;
1476 return mono_defaults.single_class;
1479 return mono_defaults.double_class;
1480 case CEE_LDELEM_REF:
1481 case CEE_STELEM_REF:
1482 return mono_defaults.object_class;
1484 g_assert_not_reached ();
1490 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
1494 MONO_ADD_INS (bb, inst);
1497 switch (bb->last_ins->opcode) {
1511 while (prev->next && prev->next != bb->last_ins)
1513 if (prev == bb->code) {
1514 if (bb->last_ins == bb->code) {
1515 inst->next = bb->code;
1518 inst->next = prev->next;
1522 inst->next = bb->last_ins;
1526 // g_warning ("handle conditional jump in add_ins_to_end ()\n");
1528 MONO_ADD_INS (bb, inst);
1534 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
1536 MonoInst *inst, *load;
1538 NEW_TEMPLOAD (cfg, load, src);
1540 NEW_TEMPSTORE (cfg, inst, dest, load);
1541 if (inst->opcode == CEE_STOBJ) {
1542 NEW_TEMPLOADA (cfg, inst, dest);
1543 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE);
1545 inst->cil_code = NULL;
1546 mono_add_ins_to_end (bb, inst);
1551 * We try to share variables when possible
1554 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1559 /* inlining can result in deeper stacks */
1560 if (slot >= mono_method_get_header (cfg->method)->max_stack)
1561 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1563 pos = ins->type - 1 + slot * STACK_MAX;
1565 switch (ins->type) {
1572 if ((vnum = cfg->intvars [pos]))
1573 return cfg->varinfo [vnum];
1574 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1575 cfg->intvars [pos] = res->inst_c0;
1578 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1584 * This function is called to handle items that are left on the evaluation stack
1585 * at basic block boundaries. What happens is that we save the values to local variables
1586 * and we reload them later when first entering the target basic block (with the
1587 * handle_loaded_temps () function).
1588 * It is also used to handle items on the stack in store opcodes, since it is
1589 * possible that the variable to be stored into is already on the stack, in
1590 * which case its old value should be used.
1591 * A single joint point will use the same variables (stored in the array bb->out_stack or
1592 * bb->in_stack, if the basic block is before or after the joint point).
1595 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count) {
1597 MonoBasicBlock *outb;
1598 MonoInst *inst, **locals;
1603 if (cfg->verbose_level > 3)
1604 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
1605 if (!bb->out_scount) {
1606 bb->out_scount = count;
1607 //g_print ("bblock %d has out:", bb->block_num);
1609 for (i = 0; i < bb->out_count; ++i) {
1610 outb = bb->out_bb [i];
1611 //g_print (" %d", outb->block_num);
1612 if (outb->in_stack) {
1614 bb->out_stack = outb->in_stack;
1620 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1621 for (i = 0; i < count; ++i) {
1623 * try to reuse temps already allocated for this purpouse, if they occupy the same
1624 * stack slot and if they are of the same type.
1625 * This won't cause conflicts since if 'local' is used to
1626 * store one of the values in the in_stack of a bblock, then
1627 * the same variable will be used for the same outgoing stack
1629 * This doesn't work when inlining methods, since the bblocks
1630 * in the inlined methods do not inherit their in_stack from
1631 * the bblock they are inlined to. See bug #58863 for an
1634 if (cfg->inlined_method)
1635 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1637 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1642 for (i = 0; i < bb->out_count; ++i) {
1643 outb = bb->out_bb [i];
1644 if (outb->in_scount)
1645 continue; /* check they are the same locals */
1646 outb->in_scount = count;
1647 outb->in_stack = bb->out_stack;
1650 locals = bb->out_stack;
1651 for (i = 0; i < count; ++i) {
1652 /* add store ops at the end of the bb, before the branch */
1653 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1654 if (inst->opcode == CEE_STOBJ) {
1655 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
1656 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE);
1658 inst->cil_code = sp [i]->cil_code;
1659 mono_add_ins_to_end (bb, inst);
1661 if (cfg->verbose_level > 3)
1662 g_print ("storing %d to temp %d\n", i, locals [i]->inst_c0);
1666 * It is possible that the out bblocks already have in_stack assigned, and
1667 * the in_stacks differ. In this case, we will store to all the different
1674 /* Find a bblock which has a different in_stack */
1676 while (bindex < bb->out_count) {
1677 outb = bb->out_bb [bindex];
1678 if (outb->in_stack != locals) {
1680 * Instead of storing sp [i] to locals [i], we need to store
1681 * locals [i] to <new locals>[i], since the sp [i] tree can't
1682 * be shared between trees.
1684 for (i = 0; i < count; ++i)
1685 mono_add_varcopy_to_end (cfg, bb, locals [i]->inst_c0, outb->in_stack [i]->inst_c0);
1686 locals = outb->in_stack;
1698 ret_type_to_call_opcode (MonoType *type, int calli, int virt)
1701 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1704 switch (type->type) {
1705 case MONO_TYPE_VOID:
1706 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
1709 case MONO_TYPE_BOOLEAN:
1712 case MONO_TYPE_CHAR:
1715 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1719 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1720 case MONO_TYPE_CLASS:
1721 case MONO_TYPE_STRING:
1722 case MONO_TYPE_OBJECT:
1723 case MONO_TYPE_SZARRAY:
1724 case MONO_TYPE_ARRAY:
1725 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1728 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
1731 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
1732 case MONO_TYPE_VALUETYPE:
1733 if (type->data.klass->enumtype) {
1734 type = type->data.klass->enum_basetype;
1737 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1738 case MONO_TYPE_TYPEDBYREF:
1739 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1740 case MONO_TYPE_GENERICINST:
1741 type = type->data.generic_inst->generic_type;
1744 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
1750 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
1752 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
1754 ji->ip.label = label;
1755 ji->type = MONO_PATCH_INFO_SWITCH;
1756 ji->data.table = bbs;
1757 ji->next = cfg->patch_info;
1758 ji->table_size = num_blocks;
1759 cfg->patch_info = ji;
1763 * When we add a tree of instructions, we need to ensure the instructions currently
1764 * on the stack are executed before (like, if we load a value from a local).
1765 * We ensure this by saving the currently loaded values to temps and rewriting the
1766 * instructions to load the values.
1767 * This is not done for opcodes that terminate a basic block (because it's handled already
1768 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
1771 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
1773 MonoInst *load, *store, *temp, *ins;
1775 while (stack < sp) {
1777 /* handle also other constants */
1778 if (ins->opcode != OP_ICONST) {
1779 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1780 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1781 store->cil_code = ins->cil_code;
1782 if (store->opcode == CEE_STOBJ) {
1783 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
1784 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE);
1786 MONO_ADD_INS (bblock, store);
1787 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1788 load->cil_code = ins->cil_code;
1796 * Prepare arguments for passing to a function call.
1797 * Return a non-zero value if the arguments can't be passed to the given
1799 * The type checks are not yet complete and some conversions may need
1800 * casts on 32 or 64 bit architectures.
1803 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
1805 MonoType *simple_type;
1809 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
1813 for (i = 0; i < sig->param_count; ++i) {
1814 if (sig->params [i]->byref) {
1816 * check the result of ldelema is only passed as an argument if the byref
1817 * type matches exactly the array element type.
1818 * FIXME: if the argument as been saved on the stack as part of the
1819 * interface variable code (the value was on the stack at a basic block boundary)
1820 * we need to add the check in that case, too.
1822 if (args [i]->opcode == CEE_LDELEMA) {
1824 MonoClass *exact_class = mono_class_from_mono_type (sig->params [i]);
1825 if (!exact_class->valuetype) {
1826 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
1827 check->cil_code = args [i]->cil_code;
1828 check->klass = exact_class;
1829 check->inst_left = args [i]->inst_left;
1830 check->type = STACK_OBJ;
1831 args [i]->inst_left = check;
1834 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
1838 simple_type = sig->params [i];
1840 switch (simple_type->type) {
1841 case MONO_TYPE_VOID:
1846 case MONO_TYPE_BOOLEAN:
1849 case MONO_TYPE_CHAR:
1852 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
1858 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
1861 case MONO_TYPE_CLASS:
1862 case MONO_TYPE_STRING:
1863 case MONO_TYPE_OBJECT:
1864 case MONO_TYPE_SZARRAY:
1865 case MONO_TYPE_ARRAY:
1866 if (args [i]->type != STACK_OBJ)
1871 if (args [i]->type != STACK_I8)
1876 if (args [i]->type != STACK_R8)
1879 case MONO_TYPE_VALUETYPE:
1880 if (simple_type->data.klass->enumtype) {
1881 simple_type = simple_type->data.klass->enum_basetype;
1884 if (args [i]->type != STACK_VTYPE)
1887 case MONO_TYPE_TYPEDBYREF:
1888 if (args [i]->type != STACK_VTYPE)
1891 case MONO_TYPE_GENERICINST:
1892 simple_type = simple_type->data.generic_inst->generic_type;
1896 g_error ("unknown type 0x%02x in check_call_signature",
1904 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
1905 const guint8 *ip, gboolean to_end)
1907 MonoInst *temp, *store, *ins = (MonoInst*)call;
1908 MonoType *ret = sig->ret;
1910 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
1912 call->inst.type = STACK_OBJ;
1913 call->inst.opcode = CEE_CALL;
1914 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
1916 type_to_eval_stack_type (ret, ins);
1917 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
1920 if (MONO_TYPE_ISSTRUCT (ret)) {
1923 /* we use this to allocate native sized structs */
1924 temp->unused = sig->pinvoke;
1926 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
1927 if (call->inst.opcode == OP_VCALL)
1928 ins->inst_left = loada;
1930 ins->inst_right = loada; /* a virtual or indirect call */
1933 mono_add_ins_to_end (bblock, ins);
1935 MONO_ADD_INS (bblock, ins);
1937 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1938 store->cil_code = ip;
1940 mono_add_ins_to_end (bblock, store);
1942 MONO_ADD_INS (bblock, store);
1944 return temp->inst_c0;
1947 mono_add_ins_to_end (bblock, ins);
1949 MONO_ADD_INS (bblock, ins);
1954 inline static MonoCallInst *
1955 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1956 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
1961 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual));
1963 call->inst.cil_code = ip;
1965 call->signature = sig;
1966 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
1968 for (arg = call->out_args; arg;) {
1969 MonoInst *narg = arg->next;
1974 mono_add_ins_to_end (bblock, arg);
1976 MONO_ADD_INS (bblock, arg);
1983 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1984 MonoInst **args, MonoInst *addr, const guint8 *ip)
1986 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
1988 call->inst.inst_i0 = addr;
1990 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
1993 static MonoCallInst*
1994 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
1995 MonoInst **args, const guint8 *ip, MonoInst *this)
1997 gboolean virtual = this != NULL;
2000 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, FALSE);
2002 if (this && sig->hasthis &&
2003 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
2004 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
2005 call->method = mono_marshal_get_remoting_invoke_with_check (method);
2007 call->method = method;
2009 call->inst.flags |= MONO_INST_HAS_METHOD;
2010 call->inst.inst_left = this;
2016 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
2017 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
2019 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, signature, args, ip, this);
2021 return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
2025 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
2026 MonoInst **args, const guint8 *ip, gboolean ret_object, gboolean to_end)
2032 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
2034 return mono_spill_call (cfg, bblock, call, sig, ret_object, ip, to_end);
2038 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
2040 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
2043 g_warning ("unregistered JIT ICall");
2044 g_assert_not_reached ();
2047 return mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, args, ip, FALSE, FALSE);
2051 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
2053 MonoInst *ins, *temp = NULL, *store, *load, *begin;
2054 MonoInst *last_arg = NULL;
2058 //g_print ("emulating: ");
2059 //mono_print_tree_nl (tree);
2060 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE));
2061 ins = (MonoInst*)call;
2063 call->inst.cil_code = tree->cil_code;
2065 call->signature = info->sig;
2067 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
2069 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2070 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
2071 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2072 store->cil_code = tree->cil_code;
2077 nargs = info->sig->param_count + info->sig->hasthis;
2079 for (last_arg = call->out_args; last_arg && last_arg->next; last_arg = last_arg->next) ;
2082 last_arg->next = store;
2085 begin = call->out_args;
2089 if (cfg->prev_ins) {
2091 * This assumes that that in a tree, emulate_opcode is called for a
2092 * node before it is called for its children. dec_foreach needs to
2093 * take this into account.
2095 store->next = cfg->prev_ins->next;
2096 cfg->prev_ins->next = begin;
2098 store->next = cfg->cbb->code;
2099 cfg->cbb->code = begin;
2102 call->fptr = mono_icall_get_wrapper (info);
2104 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2105 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2110 static MonoMethodSignature *
2111 mono_get_element_address_signature (int arity)
2113 static GHashTable *sighash = NULL;
2114 MonoMethodSignature *res;
2117 EnterCriticalSection (&jit_mutex);
2119 sighash = g_hash_table_new (NULL, NULL);
2121 else if ((res = g_hash_table_lookup (sighash, (gpointer)arity))) {
2122 LeaveCriticalSection (&jit_mutex);
2126 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
2129 #ifdef MONO_ARCH_VARARG_ICALLS
2130 /* Only set this only some archs since not all backends can handle varargs+pinvoke */
2131 res->call_convention = MONO_CALL_VARARG;
2133 res->params [0] = &mono_defaults.array_class->byval_arg;
2135 for (i = 1; i <= arity; i++)
2136 res->params [i] = &mono_defaults.int_class->byval_arg;
2138 res->ret = &mono_defaults.int_class->byval_arg;
2140 g_hash_table_insert (sighash, (gpointer)arity, res);
2141 LeaveCriticalSection (&jit_mutex);
2146 static MonoMethodSignature *
2147 mono_get_array_new_va_signature (int arity)
2149 static GHashTable *sighash = NULL;
2150 MonoMethodSignature *res;
2153 EnterCriticalSection (&jit_mutex);
2155 sighash = g_hash_table_new (NULL, NULL);
2157 else if ((res = g_hash_table_lookup (sighash, (gpointer)arity))) {
2158 LeaveCriticalSection (&jit_mutex);
2162 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
2165 #ifdef MONO_ARCH_VARARG_ICALLS
2166 /* Only set this only some archs since not all backends can handle varargs+pinvoke */
2167 res->call_convention = MONO_CALL_VARARG;
2170 res->params [0] = &mono_defaults.int_class->byval_arg;
2171 for (i = 0; i < arity; i++)
2172 res->params [i + 1] = &mono_defaults.int_class->byval_arg;
2174 res->ret = &mono_defaults.int_class->byval_arg;
2176 g_hash_table_insert (sighash, (gpointer)arity, res);
2177 LeaveCriticalSection (&jit_mutex);
2183 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native) {
2184 MonoInst *iargs [3];
2190 * This check breaks with spilled vars... need to handle it during verification anyway.
2191 * g_assert (klass && klass == src->klass && klass == dest->klass);
2195 n = mono_class_native_size (klass, &align);
2197 n = mono_class_value_size (klass, &align);
2199 if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
2201 MONO_INST_NEW (cfg, inst, OP_MEMCPY);
2202 inst->inst_left = dest;
2203 inst->inst_right = src;
2204 inst->cil_code = ip;
2206 MONO_ADD_INS (bblock, inst);
2211 NEW_ICONST (cfg, iargs [2], n);
2213 mono_emit_native_call (cfg, bblock, helper_memcpy, helper_sig_memcpy, iargs, ip, FALSE, to_end);
2217 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
2219 MonoInst *iargs [2];
2220 MonoInst *ins, *zero_int32;
2223 NEW_ICONST (cfg, zero_int32, 0);
2225 mono_class_init (klass);
2226 n = mono_class_value_size (klass, NULL);
2227 MONO_INST_NEW (cfg, ins, 0);
2229 ins->inst_left = dest;
2230 ins->inst_right = zero_int32;
2233 ins->opcode = CEE_STIND_I1;
2234 MONO_ADD_INS (bblock, ins);
2237 ins->opcode = CEE_STIND_I2;
2238 MONO_ADD_INS (bblock, ins);
2241 ins->opcode = CEE_STIND_I4;
2242 MONO_ADD_INS (bblock, ins);
2245 if (n <= sizeof (gpointer) * 5) {
2246 ins->opcode = OP_MEMSET;
2249 MONO_ADD_INS (bblock, ins);
2252 handle_loaded_temps (cfg, bblock, stack_start, sp);
2253 NEW_ICONST (cfg, ins, n);
2256 mono_emit_jit_icall (cfg, bblock, helper_initobj, iargs, ip);
2262 handle_alloc (MonoCompile *cfg, MonoBasicBlock *bblock, MonoClass *klass, const guchar *ip)
2264 MonoInst *iargs [2];
2267 if (cfg->opt & MONO_OPT_SHARED) {
2268 NEW_DOMAINCONST (cfg, iargs [0]);
2269 NEW_CLASSCONST (cfg, iargs [1], klass);
2271 alloc_ftn = mono_object_new;
2273 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
2276 alloc_ftn = mono_class_get_allocation_ftn (vtable, &pass_lw);
2278 guint32 lw = vtable->klass->instance_size;
2279 lw = ((lw + (sizeof (gpointer) - 1)) & ~(sizeof (gpointer) - 1)) / sizeof (gpointer);
2280 NEW_ICONST (cfg, iargs [0], lw);
2281 NEW_VTABLECONST (cfg, iargs [1], vtable);
2284 NEW_VTABLECONST (cfg, iargs [0], vtable);
2287 return mono_emit_jit_icall (cfg, bblock, alloc_ftn, iargs, ip);
2291 handle_box (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *val, const guchar *ip, MonoClass *klass)
2293 MonoInst *dest, *vtoffset, *add, *vstore;
2296 temp = handle_alloc (cfg, bblock, klass, ip);
2297 NEW_TEMPLOAD (cfg, dest, temp);
2298 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
2299 MONO_INST_NEW (cfg, add, OP_PADD);
2300 add->inst_left = dest;
2301 add->inst_right = vtoffset;
2304 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
2305 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
2306 vstore->cil_code = ip;
2307 vstore->inst_left = add;
2308 vstore->inst_right = val;
2310 if (vstore->opcode == CEE_STOBJ) {
2311 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
2313 MONO_ADD_INS (bblock, vstore);
2315 NEW_TEMPLOAD (cfg, dest, temp);
2320 handle_array_new (MonoCompile *cfg, MonoBasicBlock *bblock, int rank, MonoInst **sp, unsigned char *ip)
2322 MonoMethodSignature *esig;
2323 char icall_name [256];
2324 MonoJitICallInfo *info;
2326 /* Need to register the icall so it gets an icall wrapper */
2327 sprintf (icall_name, "ves_array_new_va_%d", rank);
2329 info = mono_find_jit_icall_by_name (icall_name);
2331 esig = mono_get_array_new_va_signature (rank);
2332 info = mono_register_jit_icall (mono_array_new_va, g_strdup (icall_name), esig, FALSE);
2335 cfg->flags |= MONO_CFG_HAS_VARARGS;
2337 return mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, sp, ip, TRUE, FALSE);
2340 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
2343 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
2345 MonoMethodHeader *header = mono_method_get_header (method);
2346 MonoMethodSignature *signature = method->signature;
2350 #ifdef MONO_ARCH_HAVE_LMF_OPS
2351 if (((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2352 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) &&
2353 !MONO_TYPE_ISSTRUCT (signature->ret) && (method->klass->parent != mono_defaults.array_class))
2357 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2358 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2359 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
2360 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
2361 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2362 (method->klass->marshalbyref) ||
2363 !header || header->num_clauses ||
2364 /* fixme: why cant we inline valuetype returns? */
2365 MONO_TYPE_ISSTRUCT (signature->ret))
2368 /* its not worth to inline methods with valuetype arguments?? */
2369 for (i = 0; i < signature->param_count; i++) {
2370 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
2376 * if we can initialize the class of the method right away, we do,
2377 * otherwise we don't allow inlining if the class needs initialization,
2378 * since it would mean inserting a call to mono_runtime_class_init()
2379 * inside the inlined code
2381 if (!(cfg->opt & MONO_OPT_SHARED)) {
2382 vtable = mono_class_vtable (cfg->domain, method->klass);
2383 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
2384 if (cfg->run_cctors)
2385 mono_runtime_class_init (vtable);
2387 else if (!vtable->initialized && mono_class_needs_cctor_run (method->klass, NULL))
2391 * If we're compiling for shared code
2392 * the cctor will need to be run at aot method load time, for example,
2393 * or at the end of the compilation of the inlining method.
2395 if (mono_class_needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
2398 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
2401 * CAS - do not inline methods with declarative security
2402 * Note: this has to be before any possible return TRUE;
2404 if (mono_method_has_declsec (method))
2407 /* also consider num_locals? */
2408 if (getenv ("MONO_INLINELIMIT")) {
2409 if (header->code_size < atoi (getenv ("MONO_INLINELIMIT"))) {
2412 } else if (header->code_size < 20)
2419 mini_get_ldelema_ins (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
2423 MonoMethodSignature *esig;
2424 char icall_name [256];
2425 MonoJitICallInfo *info;
2427 rank = cmethod->signature->param_count - (is_set? 1: 0);
2429 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
2431 NEW_GROUP (cfg, indexes, sp [1], sp [2]);
2432 MONO_INST_NEW (cfg, addr, OP_LDELEMA2D);
2433 addr->inst_left = sp [0];
2434 addr->inst_right = indexes;
2435 addr->cil_code = ip;
2436 addr->type = STACK_MP;
2437 addr->klass = cmethod->klass;
2441 /* Need to register the icall so it gets an icall wrapper */
2442 sprintf (icall_name, "ves_array_element_address_%d", rank);
2444 info = mono_find_jit_icall_by_name (icall_name);
2446 esig = mono_get_element_address_signature (rank);
2447 info = mono_register_jit_icall (ves_array_element_address, g_strdup (icall_name), esig, FALSE);
2450 temp = mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, sp, ip, FALSE, FALSE);
2451 cfg->flags |= MONO_CFG_HAS_VARARGS;
2453 NEW_TEMPLOAD (cfg, addr, temp);
2457 static MonoJitICallInfo **emul_opcode_map = NULL;
2459 static inline MonoJitICallInfo *
2460 mono_find_jit_opcode_emulation (int opcode)
2462 if (emul_opcode_map)
2463 return emul_opcode_map [opcode];
2469 mini_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
2474 static MonoClass *runtime_helpers_class = NULL;
2475 if (! runtime_helpers_class)
2476 runtime_helpers_class = mono_class_from_name (mono_defaults.corlib,
2477 "System.Runtime.CompilerServices", "RuntimeHelpers");
2479 if (cmethod->klass == mono_defaults.string_class) {
2480 if (cmethod->name [0] != 'g')
2483 if (strcmp (cmethod->name, "get_Chars") == 0)
2485 else if (strcmp (cmethod->name, "get_Length") == 0)
2488 } else if (cmethod->klass == mono_defaults.object_class) {
2489 if (strcmp (cmethod->name, "GetType") == 0)
2493 } else if (cmethod->klass == mono_defaults.array_class) {
2494 if (strcmp (cmethod->name, "get_Rank") == 0)
2496 else if (strcmp (cmethod->name, "get_Length") == 0)
2500 } else if (cmethod->klass == runtime_helpers_class) {
2501 if (strcmp (cmethod->name, "get_OffsetToStringData") == 0) {
2502 NEW_ICONST (cfg, ins, G_STRUCT_OFFSET (MonoString, chars));
2506 } else if (cmethod->klass == mono_defaults.thread_class) {
2507 if (strcmp (cmethod->name, "get_CurrentThread") == 0 && (ins = mono_arch_get_thread_intrinsic (cfg)))
2511 op = mono_arch_get_opcode_for_method (cfg, cmethod, fsig, args);
2515 pc = fsig->param_count + fsig->hasthis;
2516 MONO_INST_NEW (cfg, ins, op);
2519 ins->inst_i0 = args [0];
2521 ins->inst_i1 = args [1];
2528 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
2530 MonoInst *store, *temp;
2533 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
2535 if (!sig->hasthis && sig->param_count == 0)
2539 if (sp [0]->opcode == OP_ICONST) {
2542 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
2544 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2545 store->cil_code = sp [0]->cil_code;
2546 MONO_ADD_INS (bblock, store);
2551 for (i = 0; i < sig->param_count; ++i) {
2552 if (sp [0]->opcode == OP_ICONST) {
2555 temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
2557 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2558 store->cil_code = sp [0]->cil_code;
2559 if (store->opcode == CEE_STOBJ) {
2560 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2561 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE);
2563 MONO_ADD_INS (bblock, store);
2571 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
2572 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b, gboolean inline_allways)
2574 MonoInst *ins, *rvar = NULL;
2575 MonoMethodHeader *cheader;
2576 MonoBasicBlock *ebblock, *sbblock;
2577 int i, costs, new_locals_offset;
2578 MonoMethod *prev_inlined_method;
2580 if (cfg->verbose_level > 2)
2581 g_print ("INLINE START %p %s -> %s\n", cmethod, mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2583 if (!cmethod->inline_info) {
2584 mono_jit_stats.inlineable_methods++;
2585 cmethod->inline_info = 1;
2587 /* allocate space to store the return value */
2588 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2589 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2592 /* allocate local variables */
2593 cheader = mono_method_get_header (cmethod);
2594 new_locals_offset = cfg->num_varinfo;
2595 for (i = 0; i < cheader->num_locals; ++i)
2596 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
2598 /* allocate starte and end blocks */
2599 sbblock = NEW_BBLOCK (cfg);
2600 sbblock->block_num = cfg->num_bblocks++;
2601 sbblock->real_offset = real_offset;
2603 ebblock = NEW_BBLOCK (cfg);
2604 ebblock->block_num = cfg->num_bblocks++;
2605 ebblock->real_offset = real_offset;
2607 prev_inlined_method = cfg->inlined_method;
2608 cfg->inlined_method = cmethod;
2610 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
2612 cfg->inlined_method = prev_inlined_method;
2614 if ((costs >= 0 && costs < 60) || inline_allways) {
2615 if (cfg->verbose_level > 2)
2616 g_print ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2618 mono_jit_stats.inlined_methods++;
2620 /* always add some code to avoid block split failures */
2621 MONO_INST_NEW (cfg, ins, CEE_NOP);
2622 MONO_ADD_INS (bblock, ins);
2625 bblock->next_bb = sbblock;
2626 link_bblock (cfg, bblock, sbblock);
2629 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
2635 if (cfg->verbose_level > 2)
2636 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
2642 * Some of these comments may well be out-of-date.
2643 * Design decisions: we do a single pass over the IL code (and we do bblock
2644 * splitting/merging in the few cases when it's required: a back jump to an IL
2645 * address that was not already seen as bblock starting point).
2646 * Code is validated as we go (full verification is still better left to metadata/verify.c).
2647 * Complex operations are decomposed in simpler ones right away. We need to let the
2648 * arch-specific code peek and poke inside this process somehow (except when the
2649 * optimizations can take advantage of the full semantic info of coarse opcodes).
2650 * All the opcodes of the form opcode.s are 'normalized' to opcode.
2651 * MonoInst->opcode initially is the IL opcode or some simplification of that
2652 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
2653 * opcode with value bigger than OP_LAST.
2654 * At this point the IR can be handed over to an interpreter, a dumb code generator
2655 * or to the optimizing code generator that will translate it to SSA form.
2657 * Profiling directed optimizations.
2658 * We may compile by default with few or no optimizations and instrument the code
2659 * or the user may indicate what methods to optimize the most either in a config file
2660 * or through repeated runs where the compiler applies offline the optimizations to
2661 * each method and then decides if it was worth it.
2664 * * consider using an array instead of an hash table (bb_hash)
2667 #define CHECK_TYPE(ins) if (!(ins)->type) goto unverified
2668 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) goto unverified
2669 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) goto unverified
2670 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) goto unverified
2671 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) goto unverified
2672 #define CHECK_OPSIZE(size) if (ip + size > end) goto unverified
2675 /* offset from br.s -> br like opcodes */
2676 #define BIG_BRANCH_OFFSET 13
2679 get_basic_blocks (MonoCompile *cfg, GHashTable *bbhash, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
2681 unsigned char *ip = start;
2682 unsigned char *target;
2685 MonoBasicBlock *bblock;
2686 const MonoOpcode *opcode;
2689 cli_addr = ip - start;
2690 i = mono_opcode_value ((const guint8 **)&ip, end);
2693 opcode = &mono_opcodes [i];
2694 switch (opcode->argument) {
2695 case MonoInlineNone:
2698 case MonoInlineString:
2699 case MonoInlineType:
2700 case MonoInlineField:
2701 case MonoInlineMethod:
2704 case MonoShortInlineR:
2711 case MonoShortInlineVar:
2712 case MonoShortInlineI:
2715 case MonoShortInlineBrTarget:
2716 target = start + cli_addr + 2 + (signed char)ip [1];
2717 GET_BBLOCK (cfg, bbhash, bblock, target);
2720 case MonoInlineBrTarget:
2721 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
2722 GET_BBLOCK (cfg, bbhash, bblock, target);
2725 case MonoInlineSwitch: {
2726 guint32 n = read32 (ip + 1);
2729 cli_addr += 5 + 4 * n;
2730 target = start + cli_addr;
2731 GET_BBLOCK (cfg, bbhash, bblock, target);
2733 for (j = 0; j < n; ++j) {
2734 target = start + cli_addr + (gint32)read32 (ip);
2735 GET_BBLOCK (cfg, bbhash, bblock, target);
2745 g_assert_not_reached ();
2755 emit_tree (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *ins)
2757 MonoInst *store, *temp, *load;
2758 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
2759 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2760 store->cil_code = ins->cil_code;
2761 MONO_ADD_INS (bblock, store);
2762 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2763 load->cil_code = ins->cil_code;
2768 * mono_method_to_ir: translates IL into basic blocks containing trees
2771 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
2772 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
2773 guint inline_offset, gboolean is_virtual_call)
2775 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
2776 MonoInst *ins, **sp, **stack_start;
2777 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
2779 MonoMethod *cmethod;
2780 MonoInst **arg_array;
2781 MonoMethodHeader *header;
2783 guint32 token, ins_flag;
2785 MonoClass *constrained_call = NULL;
2786 unsigned char *ip, *end, *target, *err_pos;
2787 static double r8_0 = 0.0;
2788 MonoMethodSignature *sig;
2789 MonoGenericContext *generic_context = NULL;
2790 MonoGenericContainer *generic_container = NULL;
2791 MonoType **param_types;
2792 GList *bb_recheck = NULL, *tmp;
2793 int i, n, start_new_bblock, align;
2794 int num_calls = 0, inline_costs = 0;
2795 int *filter_lengths = NULL;
2796 int breakpoint_id = 0;
2797 guint real_offset, num_args;
2799 image = method->klass->image;
2800 header = mono_method_get_header (method);
2801 generic_container = ((MonoMethodNormal *)method)->generic_container;
2802 sig = method->signature;
2803 num_args = sig->hasthis + sig->param_count;
2804 ip = (unsigned char*)header->code;
2805 end = ip + header->code_size;
2806 mono_jit_stats.cil_code_size += header->code_size;
2808 if (method->signature->is_inflated)
2809 generic_context = ((MonoMethodInflated *) method)->context;
2810 else if (generic_container)
2811 generic_context = generic_container->context;
2813 if (cfg->method == method) {
2815 bbhash = cfg->bb_hash;
2817 real_offset = inline_offset;
2818 bbhash = g_hash_table_new (g_direct_hash, NULL);
2821 if (cfg->verbose_level > 2)
2822 g_print ("method to IR %s\n", mono_method_full_name (method, TRUE));
2824 dont_inline = g_list_prepend (dont_inline, method);
2825 if (cfg->method == method) {
2827 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
2828 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
2831 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
2832 start_bblock->cil_code = NULL;
2833 start_bblock->cil_length = 0;
2834 start_bblock->block_num = cfg->num_bblocks++;
2837 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
2838 end_bblock->cil_code = NULL;
2839 end_bblock->cil_length = 0;
2840 end_bblock->block_num = cfg->num_bblocks++;
2841 g_assert (cfg->num_bblocks == 2);
2843 arg_array = alloca (sizeof (MonoInst *) * num_args);
2844 for (i = num_args - 1; i >= 0; i--)
2845 arg_array [i] = cfg->varinfo [i];
2847 if (header->num_clauses) {
2848 int size = sizeof (int) * header->num_clauses;
2849 filter_lengths = alloca (size);
2850 memset (filter_lengths, 0, size);
2852 cfg->spvars = g_hash_table_new (NULL, NULL);
2854 /* handle exception clauses */
2855 for (i = 0; i < header->num_clauses; ++i) {
2856 //unsigned char *p = ip;
2857 MonoExceptionClause *clause = &header->clauses [i];
2858 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->try_offset);
2859 tblock->real_offset = clause->try_offset;
2860 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
2861 tblock->real_offset = clause->handler_offset;
2863 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
2864 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2865 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2866 MONO_ADD_INS (tblock, ins);
2869 /*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);
2871 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
2873 /* catch and filter blocks get the exception object on the stack */
2874 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
2875 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2876 /* mostly like handle_stack_args (), but just sets the input args */
2877 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
2879 cfg->exvar = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
2880 /* prevent it from being register allocated */
2881 cfg->exvar->flags |= MONO_INST_INDIRECT;
2883 tblock->in_scount = 1;
2884 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2885 tblock->in_stack [0] = cfg->exvar;
2887 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2888 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->data.filter_offset);
2889 tblock->real_offset = clause->data.filter_offset;
2890 tblock->in_scount = 1;
2891 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2892 tblock->in_stack [0] = cfg->exvar;
2893 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2894 MONO_ADD_INS (tblock, ins);
2900 arg_array = alloca (sizeof (MonoInst *) * num_args);
2901 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
2904 /* FIRST CODE BLOCK */
2905 bblock = NEW_BBLOCK (cfg);
2906 bblock->cil_code = ip;
2908 ADD_BBLOCK (cfg, bbhash, bblock);
2910 if (cfg->method == method) {
2911 breakpoint_id = mono_debugger_method_has_breakpoint (method);
2912 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
2913 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2914 MONO_ADD_INS (bblock, ins);
2918 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED))) || mono_compile_aot) {
2919 /* we use a separate basic block for the initialization code */
2920 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
2921 init_localsbb->real_offset = real_offset;
2922 start_bblock->next_bb = init_localsbb;
2923 init_localsbb->next_bb = bblock;
2924 link_bblock (cfg, start_bblock, init_localsbb);
2925 link_bblock (cfg, init_localsbb, bblock);
2926 init_localsbb->block_num = cfg->num_bblocks++;
2928 start_bblock->next_bb = bblock;
2929 link_bblock (cfg, start_bblock, bblock);
2932 if (get_basic_blocks (cfg, bbhash, header, real_offset, ip, end, &err_pos)) {
2937 mono_debug_init_method (cfg, bblock, breakpoint_id);
2939 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
2941 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
2942 for (n = 0; n < sig->param_count; ++n)
2943 param_types [n + sig->hasthis] = sig->params [n];
2945 /* do this somewhere outside - not here */
2946 NEW_ICONST (cfg, zero_int32, 0);
2947 NEW_ICONST (cfg, zero_int64, 0);
2948 zero_int64->type = STACK_I8;
2949 NEW_PCONST (cfg, zero_ptr, 0);
2950 NEW_PCONST (cfg, zero_obj, 0);
2951 zero_obj->type = STACK_OBJ;
2953 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
2954 zero_r8->type = STACK_R8;
2955 zero_r8->inst_p0 = &r8_0;
2957 /* add a check for this != NULL to inlined methods */
2958 if (is_virtual_call) {
2959 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
2960 NEW_ARGLOAD (cfg, ins->inst_left, 0);
2962 MONO_ADD_INS (bblock, ins);
2965 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
2966 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
2969 start_new_bblock = 0;
2972 if (cfg->method == method)
2973 real_offset = ip - header->code;
2975 real_offset = inline_offset;
2977 if (start_new_bblock) {
2978 bblock->cil_length = ip - bblock->cil_code;
2979 if (start_new_bblock == 2) {
2980 g_assert (ip == tblock->cil_code);
2982 GET_BBLOCK (cfg, bbhash, tblock, ip);
2984 bblock->next_bb = tblock;
2986 start_new_bblock = 0;
2987 for (i = 0; i < bblock->in_scount; ++i) {
2988 if (cfg->verbose_level > 3)
2989 g_print ("loading %d from temp %d\n", i, bblock->in_stack [i]->inst_c0);
2990 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2994 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
2995 link_bblock (cfg, bblock, tblock);
2996 if (sp != stack_start) {
2997 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3000 bblock->next_bb = tblock;
3002 for (i = 0; i < bblock->in_scount; ++i) {
3003 if (cfg->verbose_level > 3)
3004 g_print ("loading %d from temp %d\n", i, bblock->in_stack [i]->inst_c0);
3005 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
3011 bblock->real_offset = real_offset;
3013 if ((cfg->method == method) && cfg->coverage_info) {
3014 MonoInst *store, *one;
3015 guint32 cil_offset = ip - header->code;
3016 cfg->coverage_info->data [cil_offset].cil_code = ip;
3018 /* TODO: Use an increment here */
3019 NEW_ICONST (cfg, one, 1);
3022 NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
3025 MONO_INST_NEW (cfg, store, CEE_STIND_I);
3026 store->cil_code = ip;
3027 store->inst_left = ins;
3028 store->inst_right = one;
3030 MONO_ADD_INS (bblock, store);
3033 if (cfg->verbose_level > 3)
3034 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, sp-stack_start, mono_disasm_code_one (NULL, method, ip, NULL));
3041 MONO_INST_NEW (cfg, ins, CEE_BREAK);
3042 ins->cil_code = ip++;
3043 MONO_ADD_INS (bblock, ins);
3049 CHECK_STACK_OVF (1);
3050 n = (*ip)-CEE_LDARG_0;
3052 NEW_ARGLOAD (cfg, ins, n);
3053 ins->cil_code = ip++;
3060 CHECK_STACK_OVF (1);
3061 n = (*ip)-CEE_LDLOC_0;
3063 NEW_LOCLOAD (cfg, ins, n);
3064 ins->cil_code = ip++;
3072 n = (*ip)-CEE_STLOC_0;
3075 handle_loaded_temps (cfg, bblock, stack_start, sp);
3076 NEW_LOCSTORE (cfg, ins, n, *sp);
3078 if (ins->opcode == CEE_STOBJ) {
3079 NEW_LOCLOADA (cfg, ins, n);
3080 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3082 MONO_ADD_INS (bblock, ins);
3088 CHECK_STACK_OVF (1);
3090 NEW_ARGLOAD (cfg, ins, ip [1]);
3097 CHECK_STACK_OVF (1);
3099 NEW_ARGLOADA (cfg, ins, ip [1]);
3109 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
3110 handle_loaded_temps (cfg, bblock, stack_start, sp);
3112 if (ins->opcode == CEE_STOBJ) {
3113 NEW_ARGLOADA (cfg, ins, ip [1]);
3114 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3116 MONO_ADD_INS (bblock, ins);
3121 CHECK_STACK_OVF (1);
3122 CHECK_LOCAL (ip [1]);
3123 NEW_LOCLOAD (cfg, ins, ip [1]);
3130 CHECK_STACK_OVF (1);
3131 CHECK_LOCAL (ip [1]);
3132 NEW_LOCLOADA (cfg, ins, ip [1]);
3141 handle_loaded_temps (cfg, bblock, stack_start, sp);
3142 CHECK_LOCAL (ip [1]);
3143 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
3145 if (ins->opcode == CEE_STOBJ) {
3146 NEW_LOCLOADA (cfg, ins, ip [1]);
3147 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3149 MONO_ADD_INS (bblock, ins);
3154 CHECK_STACK_OVF (1);
3155 NEW_PCONST (cfg, ins, NULL);
3157 ins->type = STACK_OBJ;
3162 CHECK_STACK_OVF (1);
3163 NEW_ICONST (cfg, ins, -1);
3177 CHECK_STACK_OVF (1);
3178 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
3185 CHECK_STACK_OVF (1);
3187 NEW_ICONST (cfg, ins, *((signed char*)ip));
3194 CHECK_STACK_OVF (1);
3195 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
3202 CHECK_STACK_OVF (1);
3203 MONO_INST_NEW (cfg, ins, OP_I8CONST);
3205 ins->type = STACK_I8;
3207 ins->inst_l = (gint64)read64 (ip);
3212 float *f = mono_mempool_alloc (cfg->domain->mp, sizeof (float));
3214 CHECK_STACK_OVF (1);
3215 MONO_INST_NEW (cfg, ins, OP_R4CONST);
3216 ins->type = STACK_R8;
3225 double *d = mono_mempool_alloc (cfg->domain->mp, sizeof (double));
3227 CHECK_STACK_OVF (1);
3228 MONO_INST_NEW (cfg, ins, OP_R8CONST);
3229 ins->type = STACK_R8;
3238 MonoInst *temp, *store;
3240 CHECK_STACK_OVF (1);
3245 * small optimization: if the loaded value was from a local already,
3246 * just load it twice.
3248 if (ins->ssa_op == MONO_SSA_LOAD &&
3249 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
3251 MONO_INST_NEW (cfg, temp, 0);
3253 temp->cil_code = ip;
3256 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3257 temp->cil_code = ip;
3258 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3259 store->cil_code = ip;
3260 MONO_ADD_INS (bblock, store);
3261 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3264 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3274 MONO_INST_NEW (cfg, ins, CEE_POP);
3275 MONO_ADD_INS (bblock, ins);
3276 ins->cil_code = ip++;
3282 if (stack_start != sp)
3284 MONO_INST_NEW (cfg, ins, CEE_JMP);
3285 token = read32 (ip + 1);
3286 /* FIXME: check the signature matches */
3287 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3288 ins->inst_p0 = cmethod;
3289 MONO_ADD_INS (bblock, ins);
3291 start_new_bblock = 1;
3295 case CEE_CALLVIRT: {
3296 MonoInst *addr = NULL;
3297 MonoMethodSignature *fsig = NULL;
3298 int temp, array_rank = 0;
3299 int virtual = *ip == CEE_CALLVIRT;
3302 token = read32 (ip + 1);
3304 if (*ip == CEE_CALLI) {
3309 if (method->wrapper_type != MONO_WRAPPER_NONE)
3310 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
3312 fsig = mono_metadata_parse_signature (image, token);
3314 n = fsig->param_count + fsig->hasthis;
3316 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3317 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
3318 } else if (constrained_call) {
3319 cmethod = mono_get_method_constrained (image, token, constrained_call, generic_context);
3321 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3326 if (!cmethod->klass->inited)
3327 mono_class_init (cmethod->klass);
3329 if (cmethod->signature->pinvoke) {
3330 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
3331 fsig = wrapper->signature;
3333 fsig = mono_method_get_signature (cmethod, image, token);
3336 n = fsig->param_count + fsig->hasthis;
3338 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
3339 cmethod->klass->parent == mono_defaults.array_class) {
3340 array_rank = cmethod->klass->rank;
3343 if (cmethod->string_ctor)
3344 g_assert_not_reached ();
3350 //g_assert (!virtual || fsig->hasthis);
3354 if (constrained_call) {
3356 * We have the `constrained.' prefix opcode.
3358 if (constrained_call->valuetype && !cmethod->klass->valuetype) {
3361 * The type parameter is instantiated as a valuetype,
3362 * but that type doesn't override the method we're
3363 * calling, so we need to box `this'.
3364 * sp [0] is a pointer to the data: we need the value
3365 * in handle_box (), so load it here.
3367 MONO_INST_NEW (cfg, load, mono_type_to_ldind (&constrained_call->byval_arg));
3368 type_to_eval_stack_type (&constrained_call->byval_arg, load);
3369 load->cil_code = ip;
3370 load->inst_left = sp [0];
3371 sp [0] = handle_box (cfg, bblock, load, ip, constrained_call);
3372 } else if (!constrained_call->valuetype) {
3376 * The type parameter is instantiated as a reference
3377 * type. We have a managed pointer on the stack, so
3378 * we need to dereference it here.
3381 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3383 ins->inst_i0 = sp [0];
3384 ins->type = STACK_OBJ;
3386 } else if (cmethod->klass->valuetype)
3388 constrained_call = NULL;
3391 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
3394 if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL) && (mono_metadata_signature_equal (method->signature, cmethod->signature))) {
3396 /* FIXME: This assumes the two methods has the same number and type of arguments */
3397 for (i = 0; i < n; ++i) {
3398 /* Check if argument is the same */
3399 NEW_ARGLOAD (cfg, ins, i);
3400 if ((ins->opcode == sp [i]->opcode) && (ins->inst_i0 == sp [i]->inst_i0))
3403 /* Prevent argument from being register allocated */
3404 arg_array [i]->flags |= MONO_INST_VOLATILE;
3405 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3407 if (ins->opcode == CEE_STOBJ) {
3408 NEW_ARGLOADA (cfg, ins, i);
3409 handle_stobj (cfg, bblock, ins, sp [i], sp [i]->cil_code, ins->klass, FALSE, FALSE);
3412 MONO_ADD_INS (bblock, ins);
3414 MONO_INST_NEW (cfg, ins, CEE_JMP);
3416 ins->inst_p0 = cmethod;
3417 ins->inst_p1 = arg_array [0];
3418 MONO_ADD_INS (bblock, ins);
3419 start_new_bblock = 1;
3420 /* skip CEE_RET as well */
3425 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_opcode_for_method (cfg, cmethod, fsig, sp))) {
3428 if (MONO_TYPE_IS_VOID (fsig->ret)) {
3429 MONO_ADD_INS (bblock, ins);
3431 type_to_eval_stack_type (fsig->ret, ins);
3440 handle_loaded_temps (cfg, bblock, stack_start, sp);
3442 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3443 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
3444 mono_method_check_inlining (cfg, cmethod) &&
3445 !g_list_find (dont_inline, cmethod)) {
3447 MonoBasicBlock *ebblock;
3448 gboolean allways = FALSE;
3450 if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
3451 (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
3452 cmethod = mono_marshal_get_native_wrapper (cmethod);
3456 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, allways))) {
3460 GET_BBLOCK (cfg, bbhash, bblock, ip);
3461 ebblock->next_bb = bblock;
3462 link_bblock (cfg, ebblock, bblock);
3464 if (!MONO_TYPE_IS_VOID (fsig->ret))
3467 /* indicates start of a new block, and triggers a load of all
3468 stack arguments at bb boundarie */
3471 inline_costs += costs;
3476 inline_costs += 10 * num_calls++;
3478 /* tail recursion elimination */
3479 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == method && ip [5] == CEE_RET) {
3480 gboolean has_vtargs = FALSE;
3483 /* keep it simple */
3484 for (i = fsig->param_count - 1; i >= 0; i--) {
3485 if (MONO_TYPE_ISSTRUCT (cmethod->signature->params [i]))
3490 for (i = 0; i < n; ++i) {
3491 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3493 MONO_ADD_INS (bblock, ins);
3495 MONO_INST_NEW (cfg, ins, CEE_BR);
3497 MONO_ADD_INS (bblock, ins);
3498 tblock = start_bblock->out_bb [0];
3499 link_bblock (cfg, bblock, tblock);
3500 ins->inst_target_bb = tblock;
3501 start_new_bblock = 1;
3504 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3505 /* just create a dummy - the value is never used */
3506 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
3507 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3515 if (*ip == CEE_CALLI) {
3517 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
3518 NEW_TEMPLOAD (cfg, *sp, temp);
3522 } else if (array_rank) {
3525 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
3526 if (sp [fsig->param_count]->type == STACK_OBJ) {
3527 MonoInst *iargs [2];
3528 MonoInst *array, *to_store, *store;
3530 handle_loaded_temps (cfg, bblock, stack_start, sp);
3532 array = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
3533 NEW_TEMPSTORE (cfg, store, array->inst_c0, sp [0]);
3534 store->cil_code = ip;
3535 MONO_ADD_INS (bblock, store);
3536 NEW_TEMPLOAD (cfg, iargs [0], array->inst_c0);
3538 to_store = mono_compile_create_var (cfg, type_from_stack_type (sp [fsig->param_count]), OP_LOCAL);
3539 NEW_TEMPSTORE (cfg, store, to_store->inst_c0, sp [fsig->param_count]);
3540 store->cil_code = ip;
3541 MONO_ADD_INS (bblock, store);
3542 NEW_TEMPLOAD (cfg, iargs [1], to_store->inst_c0);
3545 * We first save the args for the call so that the args are copied to the stack
3546 * and a new instruction tree for them is created. If we don't do this,
3547 * the same MonoInst is added to two different trees and this is not
3550 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref_check, iargs, ip);
3552 NEW_TEMPLOAD (cfg, sp [0], array->inst_c0);
3553 NEW_TEMPLOAD (cfg, sp [fsig->param_count], to_store->inst_c0);
3556 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
3557 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
3559 if (ins->opcode == CEE_STOBJ) {
3560 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
3562 MONO_ADD_INS (bblock, ins);
3565 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
3566 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3567 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
3571 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
3572 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3575 g_assert_not_reached ();
3579 if (0 && CODE_IS_STLOC (ip + 5) && (!MONO_TYPE_ISSTRUCT (fsig->ret)) && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)) {
3580 /* no need to spill */
3581 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
3584 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) {
3585 NEW_TEMPLOAD (cfg, *sp, temp);
3595 if (cfg->method != method) {
3596 /* return from inlined methode */
3601 //g_assert (returnvar != -1);
3602 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
3603 store->cil_code = sp [0]->cil_code;
3604 if (store->opcode == CEE_STOBJ) {
3605 g_assert_not_reached ();
3606 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
3607 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
3609 MONO_ADD_INS (bblock, store);
3613 g_assert (!return_var);
3616 MONO_INST_NEW (cfg, ins, CEE_NOP);
3617 ins->opcode = mono_type_to_stind (method->signature->ret);
3618 if (ins->opcode == CEE_STOBJ) {
3619 NEW_RETLOADA (cfg, ins);
3620 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3622 ins->opcode = OP_SETRET;
3624 ins->inst_i0 = *sp;;
3625 ins->inst_i1 = NULL;
3626 MONO_ADD_INS (bblock, ins);
3630 if (sp != stack_start)
3632 MONO_INST_NEW (cfg, ins, CEE_BR);
3633 ins->cil_code = ip++;
3634 ins->inst_target_bb = end_bblock;
3635 MONO_ADD_INS (bblock, ins);
3636 link_bblock (cfg, bblock, end_bblock);
3637 start_new_bblock = 1;
3641 MONO_INST_NEW (cfg, ins, CEE_BR);
3642 ins->cil_code = ip++;
3643 MONO_ADD_INS (bblock, ins);
3644 target = ip + 1 + (signed char)(*ip);
3646 GET_BBLOCK (cfg, bbhash, tblock, target);
3647 link_bblock (cfg, bblock, tblock);
3648 CHECK_BBLOCK (target, ip, tblock);
3649 ins->inst_target_bb = tblock;
3650 if (sp != stack_start) {
3651 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3654 start_new_bblock = 1;
3661 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3662 ins->cil_code = ip++;
3663 target = ip + 1 + *(signed char*)ip;
3665 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
3666 if (sp != stack_start) {
3667 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3684 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3685 ins->cil_code = ip++;
3686 target = ip + 1 + *(signed char*)ip;
3689 if (sp != stack_start) {
3690 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3697 MONO_INST_NEW (cfg, ins, CEE_BR);
3698 ins->cil_code = ip++;
3699 MONO_ADD_INS (bblock, ins);
3700 target = ip + 4 + (gint32)read32(ip);
3702 GET_BBLOCK (cfg, bbhash, tblock, target);
3703 link_bblock (cfg, bblock, tblock);
3704 CHECK_BBLOCK (target, ip, tblock);
3705 ins->inst_target_bb = tblock;
3706 if (sp != stack_start) {
3707 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3710 start_new_bblock = 1;
3717 MONO_INST_NEW (cfg, ins, *ip);
3718 ins->cil_code = ip++;
3719 target = ip + 4 + (gint32)read32(ip);
3721 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
3722 if (sp != stack_start) {
3723 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3740 MONO_INST_NEW (cfg, ins, *ip);
3741 ins->cil_code = ip++;
3742 target = ip + 4 + (gint32)read32(ip);
3745 if (sp != stack_start) {
3746 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3754 n = read32 (ip + 1);
3755 MONO_INST_NEW (cfg, ins, *ip);
3757 ins->inst_left = *sp;
3758 if (ins->inst_left->type != STACK_I4) goto unverified;
3761 CHECK_OPSIZE (n * sizeof (guint32));
3762 target = ip + n * sizeof (guint32);
3763 MONO_ADD_INS (bblock, ins);
3764 GET_BBLOCK (cfg, bbhash, tblock, target);
3765 link_bblock (cfg, bblock, tblock);
3766 ins->klass = GUINT_TO_POINTER (n);
3767 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
3768 ins->inst_many_bb [n] = tblock;
3770 for (i = 0; i < n; ++i) {
3771 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
3772 link_bblock (cfg, bblock, tblock);
3773 ins->inst_many_bb [i] = tblock;
3776 if (sp != stack_start) {
3777 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3794 MONO_INST_NEW (cfg, ins, *ip);
3799 ins->type = ldind_type [*ip - CEE_LDIND_I1];
3800 ins->flags |= ins_flag;
3812 MONO_INST_NEW (cfg, ins, *ip);
3813 ins->cil_code = ip++;
3815 handle_loaded_temps (cfg, bblock, stack_start, sp);
3816 MONO_ADD_INS (bblock, ins);
3817 ins->inst_i0 = sp [0];
3818 ins->inst_i1 = sp [1];
3819 ins->flags |= ins_flag;
3838 /* special case that gives a nice speedup and happens to workaorund a ppc jit but (for the release)
3839 * later apply the speedup to the left shift as well
3842 if ((ins->opcode == OP_LSHR_UN) && (ins->type == STACK_I8)
3843 && (ins->inst_right->opcode == OP_ICONST) && (ins->inst_right->inst_c0 == 32)) {
3844 ins->opcode = OP_LONG_SHRUN_32;
3845 /*g_print ("applied long shr speedup to %s\n", cfg->method->name);*/
3849 if (mono_find_jit_opcode_emulation (ins->opcode)) {
3851 *sp++ = emit_tree (cfg, bblock, ins);
3865 case CEE_CONV_OVF_I8:
3866 case CEE_CONV_OVF_U8:
3870 if (mono_find_jit_opcode_emulation (ins->opcode)) {
3872 *sp++ = emit_tree (cfg, bblock, ins);
3876 case CEE_CONV_OVF_I4:
3877 case CEE_CONV_OVF_I1:
3878 case CEE_CONV_OVF_I2:
3879 case CEE_CONV_OVF_I:
3880 case CEE_CONV_OVF_U:
3883 if (sp [-1]->type == STACK_R8) {
3884 ADD_UNOP (CEE_CONV_OVF_I8);
3892 case CEE_CONV_OVF_U1:
3893 case CEE_CONV_OVF_U2:
3894 case CEE_CONV_OVF_U4:
3897 if (sp [-1]->type == STACK_R8) {
3898 ADD_UNOP (CEE_CONV_OVF_U8);
3906 case CEE_CONV_OVF_I1_UN:
3907 case CEE_CONV_OVF_I2_UN:
3908 case CEE_CONV_OVF_I4_UN:
3909 case CEE_CONV_OVF_I8_UN:
3910 case CEE_CONV_OVF_U1_UN:
3911 case CEE_CONV_OVF_U2_UN:
3912 case CEE_CONV_OVF_U4_UN:
3913 case CEE_CONV_OVF_U8_UN:
3914 case CEE_CONV_OVF_I_UN:
3915 case CEE_CONV_OVF_U_UN:
3923 token = read32 (ip + 1);
3924 if (method->wrapper_type != MONO_WRAPPER_NONE)
3925 klass = mono_method_get_wrapper_data (method, token);
3927 klass = mono_class_get_full (image, token, generic_context);
3929 mono_class_init (klass);
3931 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3932 MonoInst *store, *load;
3933 MONO_INST_NEW (cfg, load, CEE_LDIND_REF);
3934 load->cil_code = ip;
3935 load->inst_i0 = sp [1];
3936 load->type = STACK_OBJ;
3937 load->flags |= ins_flag;
3938 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
3939 store->cil_code = ip;
3940 handle_loaded_temps (cfg, bblock, stack_start, sp);
3941 MONO_ADD_INS (bblock, store);
3942 store->inst_i0 = sp [0];
3943 store->inst_i1 = load;
3944 store->flags |= ins_flag;
3946 n = mono_class_value_size (klass, NULL);
3947 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3949 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3950 copy->inst_left = sp [0];
3951 copy->inst_right = sp [1];
3952 copy->cil_code = ip;
3954 MONO_ADD_INS (bblock, copy);
3956 MonoInst *iargs [3];
3959 NEW_ICONST (cfg, iargs [2], n);
3960 iargs [2]->cil_code = ip;
3962 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3969 MonoInst *iargs [3];
3973 token = read32 (ip + 1);
3974 if (method->wrapper_type != MONO_WRAPPER_NONE)
3975 klass = mono_method_get_wrapper_data (method, token);
3977 klass = mono_class_get_full (image, token, generic_context);
3979 mono_class_init (klass);
3980 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3981 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3983 ins->inst_i0 = sp [0];
3984 ins->type = STACK_OBJ;
3985 ins->flags |= ins_flag;
3991 n = mono_class_value_size (klass, NULL);
3992 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3993 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3994 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3996 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3997 copy->inst_left = iargs [0];
3998 copy->inst_right = *sp;
3999 copy->cil_code = ip;
4001 MONO_ADD_INS (bblock, copy);
4004 NEW_ICONST (cfg, iargs [2], n);
4005 iargs [2]->cil_code = ip;
4007 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
4009 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
4017 CHECK_STACK_OVF (1);
4019 n = read32 (ip + 1);
4021 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
4022 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, n));
4024 ins->type = STACK_OBJ;
4027 else if (method->wrapper_type != MONO_WRAPPER_NONE) {
4029 MonoInst *iargs [1];
4031 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
4032 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
4033 NEW_TEMPLOAD (cfg, *sp, temp);
4037 if (cfg->opt & MONO_OPT_SHARED) {
4039 MonoInst *iargs [3];
4041 if (mono_compile_aot) {
4042 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n);
4045 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
4046 NEW_IMAGECONST (cfg, iargs [1], image);
4047 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
4048 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
4049 NEW_TEMPLOAD (cfg, *sp, temp);
4050 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
4052 if (mono_compile_aot)
4053 NEW_LDSTRCONST (cfg, ins, image, n);
4055 NEW_PCONST (cfg, ins, NULL);
4057 ins->type = STACK_OBJ;
4058 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
4068 MonoInst *iargs [2];
4069 MonoMethodSignature *fsig;
4073 token = read32 (ip + 1);
4074 if (method->wrapper_type != MONO_WRAPPER_NONE) {
4075 cmethod = mono_method_get_wrapper_data (method, token);
4077 cmethod = mono_get_method_full (image, token, NULL, generic_context);
4078 fsig = mono_method_get_signature (cmethod, image, token);
4080 mono_class_init (cmethod->klass);
4082 n = fsig->param_count;
4085 /* move the args to allow room for 'this' in the first position */
4091 handle_loaded_temps (cfg, bblock, stack_start, sp);
4094 if (cmethod->klass->parent == mono_defaults.array_class) {
4095 NEW_METHODCONST (cfg, *sp, cmethod);
4096 temp = handle_array_new (cfg, bblock, fsig->param_count, sp, ip);
4097 } else if (cmethod->string_ctor) {
4098 /* we simply pass a null pointer */
4099 NEW_PCONST (cfg, *sp, NULL);
4100 /* now call the string ctor */
4101 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
4103 if (cmethod->klass->valuetype) {
4104 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
4105 temp = iargs [0]->inst_c0;
4106 NEW_TEMPLOADA (cfg, *sp, temp);
4108 temp = handle_alloc (cfg, bblock, cmethod->klass, ip);
4109 NEW_TEMPLOAD (cfg, *sp, temp);
4112 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
4113 mono_method_check_inlining (cfg, cmethod) &&
4114 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
4115 !g_list_find (dont_inline, cmethod)) {
4117 MonoBasicBlock *ebblock;
4118 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, FALSE))) {
4123 GET_BBLOCK (cfg, bbhash, bblock, ip);
4124 ebblock->next_bb = bblock;
4125 link_bblock (cfg, ebblock, bblock);
4127 NEW_TEMPLOAD (cfg, *sp, temp);
4130 /* indicates start of a new block, and triggers a load
4131 of all stack arguments at bb boundarie */
4134 inline_costs += costs;
4138 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
4141 /* now call the actual ctor */
4142 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
4146 NEW_TEMPLOAD (cfg, *sp, temp);
4157 token = read32 (ip + 1);
4158 if (method->wrapper_type != MONO_WRAPPER_NONE)
4159 klass = mono_method_get_wrapper_data (method, token);
4161 klass = mono_class_get_full (image, token, generic_context);
4162 mono_class_init (klass);
4164 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4166 MonoMethod *mono_isinst;
4167 MonoInst *iargs [1];
4168 MonoBasicBlock *ebblock;
4172 mono_isinst = mono_marshal_get_isinst (klass);
4175 costs = inline_method (cfg, mono_isinst, mono_isinst->signature, bblock,
4176 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4178 g_assert (costs > 0);
4183 GET_BBLOCK (cfg, bbhash, bblock, ip);
4184 ebblock->next_bb = bblock;
4185 link_bblock (cfg, ebblock, bblock);
4187 temp = iargs [0]->inst_i0->inst_c0;
4188 NEW_TEMPLOAD (cfg, *sp, temp);
4192 inline_costs += costs;
4196 MONO_INST_NEW (cfg, ins, *ip);
4197 ins->type = STACK_OBJ;
4198 ins->inst_left = *sp;
4199 ins->inst_newa_class = klass;
4201 *sp++ = emit_tree (cfg, bblock, ins);
4205 case CEE_UNBOX_ANY: {
4206 MonoInst *add, *vtoffset;
4207 MonoInst *iargs [3];
4212 token = read32 (ip + 1);
4213 if (method->wrapper_type != MONO_WRAPPER_NONE)
4214 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4216 klass = mono_class_get_full (image, token, generic_context);
4217 mono_class_init (klass);
4219 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4221 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4222 MonoMethod *mono_castclass;
4223 MonoInst *iargs [1];
4224 MonoBasicBlock *ebblock;
4228 mono_castclass = mono_marshal_get_castclass (klass);
4231 costs = inline_method (cfg, mono_castclass, mono_castclass->signature, bblock,
4232 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4234 g_assert (costs > 0);
4239 GET_BBLOCK (cfg, bbhash, bblock, ip);
4240 ebblock->next_bb = bblock;
4241 link_bblock (cfg, ebblock, bblock);
4243 temp = iargs [0]->inst_i0->inst_c0;
4244 NEW_TEMPLOAD (cfg, *sp, temp);
4248 inline_costs += costs;
4251 MONO_INST_NEW (cfg, ins, CEE_CASTCLASS);
4252 ins->type = STACK_OBJ;
4253 ins->inst_left = *sp;
4255 ins->inst_newa_class = klass;
4263 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
4264 ins->type = STACK_OBJ;
4265 ins->inst_left = *sp;
4267 ins->inst_newa_class = klass;
4270 MONO_INST_NEW (cfg, add, OP_PADD);
4271 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4272 add->inst_left = ins;
4273 add->inst_right = vtoffset;
4274 add->type = STACK_MP;
4278 n = mono_class_value_size (klass, NULL);
4279 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
4280 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
4281 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
4283 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
4284 copy->inst_left = iargs [0];
4285 copy->inst_right = *sp;
4286 copy->cil_code = ip;
4288 MONO_ADD_INS (bblock, copy);
4291 NEW_ICONST (cfg, iargs [2], n);
4292 iargs [2]->cil_code = ip;
4294 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
4296 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
4302 MonoInst *add, *vtoffset;
4307 token = read32 (ip + 1);
4308 if (method->wrapper_type != MONO_WRAPPER_NONE)
4309 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4311 klass = mono_class_get_full (image, token, generic_context);
4312 mono_class_init (klass);
4314 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
4315 ins->type = STACK_OBJ;
4316 ins->inst_left = *sp;
4318 ins->inst_newa_class = klass;
4321 MONO_INST_NEW (cfg, add, OP_PADD);
4322 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4323 add->inst_left = ins;
4324 add->inst_right = vtoffset;
4325 add->type = STACK_MP;
4335 token = read32 (ip + 1);
4336 if (method->wrapper_type != MONO_WRAPPER_NONE)
4337 klass = mono_method_get_wrapper_data (method, token);
4339 klass = mono_class_get_full (image, token, generic_context);
4340 mono_class_init (klass);
4342 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4344 MonoMethod *mono_castclass;
4345 MonoInst *iargs [1];
4346 MonoBasicBlock *ebblock;
4350 mono_castclass = mono_marshal_get_castclass (klass);
4353 costs = inline_method (cfg, mono_castclass, mono_castclass->signature, bblock,
4354 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4356 g_assert (costs > 0);
4361 GET_BBLOCK (cfg, bbhash, bblock, ip);
4362 ebblock->next_bb = bblock;
4363 link_bblock (cfg, ebblock, bblock);
4365 temp = iargs [0]->inst_i0->inst_c0;
4366 NEW_TEMPLOAD (cfg, *sp, temp);
4370 inline_costs += costs;
4373 MONO_INST_NEW (cfg, ins, *ip);
4374 ins->type = STACK_OBJ;
4375 ins->inst_left = *sp;
4377 ins->inst_newa_class = klass;
4379 *sp++ = emit_tree (cfg, bblock, ins);
4385 MONO_INST_NEW (cfg, ins, *ip);
4387 ins->inst_left = *sp;
4388 ins->cil_code = ip++;
4389 MONO_ADD_INS (bblock, ins);
4391 start_new_bblock = 1;
4396 MonoInst *offset_ins;
4397 MonoClassField *field;
4398 MonoBasicBlock *ebblock;
4402 if (*ip == CEE_STFLD) {
4409 // FIXME: enable this test later.
4410 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
4413 token = read32 (ip + 1);
4414 field = mono_field_from_token (image, token, &klass, generic_context);
4415 mono_class_init (klass);
4417 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
4418 /* FIXME: mark instructions for use in SSA */
4419 if (*ip == CEE_STFLD) {
4420 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound || klass == mono_defaults.marshalbyrefobject_class) {
4421 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
4422 MonoInst *iargs [5];
4425 NEW_CLASSCONST (cfg, iargs [1], klass);
4426 NEW_FIELDCONST (cfg, iargs [2], field);
4427 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
4431 if (cfg->opt & MONO_OPT_INLINE) {
4432 costs = inline_method (cfg, stfld_wrapper, stfld_wrapper->signature, bblock,
4433 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4434 g_assert (costs > 0);
4439 GET_BBLOCK (cfg, bbhash, bblock, ip);
4440 ebblock->next_bb = bblock;
4441 link_bblock (cfg, ebblock, bblock);
4443 /* indicates start of a new block, and triggers a load
4444 of all stack arguments at bb boundarie */
4447 inline_costs += costs;
4450 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, stfld_wrapper->signature, iargs, ip, NULL);
4454 NEW_ICONST (cfg, offset_ins, foffset);
4455 MONO_INST_NEW (cfg, ins, OP_PADD);
4457 ins->inst_left = *sp;
4458 ins->inst_right = offset_ins;
4459 ins->type = STACK_MP;
4461 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4462 store->cil_code = ip;
4463 store->inst_left = ins;
4464 store->inst_right = sp [1];
4465 handle_loaded_temps (cfg, bblock, stack_start, sp);
4466 store->flags |= ins_flag;
4468 if (store->opcode == CEE_STOBJ) {
4469 handle_stobj (cfg, bblock, ins, sp [1], ip,
4470 mono_class_from_mono_type (field->type), FALSE, FALSE);
4472 MONO_ADD_INS (bblock, store);
4475 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound || klass == mono_defaults.marshalbyrefobject_class) {
4476 /* fixme: we need to inline that call somehow */
4477 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
4478 MonoInst *iargs [4];
4482 NEW_CLASSCONST (cfg, iargs [1], klass);
4483 NEW_FIELDCONST (cfg, iargs [2], field);
4484 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
4485 if (cfg->opt & MONO_OPT_INLINE) {
4486 costs = inline_method (cfg, ldfld_wrapper, ldfld_wrapper->signature, bblock,
4487 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4488 g_assert (costs > 0);
4493 GET_BBLOCK (cfg, bbhash, bblock, ip);
4494 ebblock->next_bb = bblock;
4495 link_bblock (cfg, ebblock, bblock);
4497 temp = iargs [0]->inst_i0->inst_c0;
4499 if (*ip == CEE_LDFLDA) {
4500 /* not sure howto handle this */
4501 NEW_TEMPLOADA (cfg, *sp, temp);
4503 NEW_TEMPLOAD (cfg, *sp, temp);
4507 /* indicates start of a new block, and triggers a load of
4508 all stack arguments at bb boundarie */
4511 inline_costs += costs;
4514 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, ldfld_wrapper->signature, iargs, ip, NULL);
4515 if (*ip == CEE_LDFLDA) {
4516 /* not sure howto handle this */
4517 NEW_TEMPLOADA (cfg, *sp, temp);
4519 NEW_TEMPLOAD (cfg, *sp, temp);
4524 NEW_ICONST (cfg, offset_ins, foffset);
4525 MONO_INST_NEW (cfg, ins, OP_PADD);
4527 ins->inst_left = *sp;
4528 ins->inst_right = offset_ins;
4529 ins->type = STACK_MP;
4531 if (*ip == CEE_LDFLDA) {
4535 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4536 type_to_eval_stack_type (field->type, load);
4537 load->cil_code = ip;
4538 load->inst_left = ins;
4539 load->flags |= ins_flag;
4551 MonoClassField *field;
4552 gpointer addr = NULL;
4555 token = read32 (ip + 1);
4557 field = mono_field_from_token (image, token, &klass, generic_context);
4558 mono_class_init (klass);
4560 if ((*ip) == CEE_STSFLD)
4561 handle_loaded_temps (cfg, bblock, stack_start, sp);
4563 if (cfg->domain->special_static_fields)
4564 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
4566 if ((cfg->opt & MONO_OPT_SHARED) || (mono_compile_aot && addr)) {
4568 MonoInst *iargs [2];
4569 g_assert (field->parent);
4570 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
4571 NEW_FIELDCONST (cfg, iargs [1], field);
4572 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
4573 NEW_TEMPLOAD (cfg, ins, temp);
4576 vtable = mono_class_vtable (cfg->domain, klass);
4578 if ((!vtable->initialized || mono_compile_aot) && !(klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (klass, method)) {
4579 guint8 *tramp = mono_create_class_init_trampoline (vtable);
4580 mono_emit_native_call (cfg, bblock, tramp,
4581 helper_sig_class_init_trampoline,
4582 NULL, ip, FALSE, FALSE);
4583 if (cfg->verbose_level > 2)
4584 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
4586 if (cfg->run_cctors)
4587 mono_runtime_class_init (vtable);
4589 addr = (char*)vtable->data + field->offset;
4591 if (mono_compile_aot)
4592 NEW_SFLDACONST (cfg, ins, field);
4594 NEW_PCONST (cfg, ins, addr);
4598 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
4599 * This could be later optimized to do just a couple of
4600 * memory dereferences with constant offsets.
4603 MonoInst *iargs [1];
4604 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
4605 temp = mono_emit_jit_icall (cfg, bblock, mono_get_special_static_data, iargs, ip);
4606 NEW_TEMPLOAD (cfg, ins, temp);
4610 /* FIXME: mark instructions for use in SSA */
4611 if (*ip == CEE_LDSFLDA) {
4613 } else if (*ip == CEE_STSFLD) {
4617 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4618 store->cil_code = ip;
4619 store->inst_left = ins;
4620 store->inst_right = sp [0];
4621 store->flags |= ins_flag;
4624 if (store->opcode == CEE_STOBJ) {
4625 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
4627 MONO_ADD_INS (bblock, store);
4629 gboolean is_const = FALSE;
4630 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4631 if (!((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) &&
4632 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
4633 gpointer addr = (char*)vtable->data + field->offset;
4634 /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
4636 switch (field->type->type) {
4637 case MONO_TYPE_BOOLEAN:
4639 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
4643 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
4646 case MONO_TYPE_CHAR:
4648 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
4652 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
4657 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
4661 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
4666 case MONO_TYPE_STRING:
4667 case MONO_TYPE_OBJECT:
4668 case MONO_TYPE_CLASS:
4669 case MONO_TYPE_SZARRAY:
4671 case MONO_TYPE_FNPTR:
4672 case MONO_TYPE_ARRAY:
4673 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
4674 type_to_eval_stack_type (field->type, *sp);
4679 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
4680 sp [0]->type = STACK_I8;
4681 sp [0]->inst_l = *((gint64 *)addr);
4686 case MONO_TYPE_VALUETYPE:
4695 CHECK_STACK_OVF (1);
4696 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4697 type_to_eval_stack_type (field->type, load);
4698 load->cil_code = ip;
4699 load->inst_left = ins;
4701 load->flags |= ins_flag;
4703 /* fixme: dont see the problem why this does not work */
4704 //cfg->disable_aot = TRUE;
4714 token = read32 (ip + 1);
4715 if (method->wrapper_type != MONO_WRAPPER_NONE)
4716 klass = mono_method_get_wrapper_data (method, token);
4718 klass = mono_class_get_full (image, token, generic_context);
4719 mono_class_init (klass);
4720 n = mono_type_to_stind (&klass->byval_arg);
4721 if (n == CEE_STOBJ) {
4722 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
4724 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
4726 MONO_INST_NEW (cfg, store, n);
4727 store->cil_code = ip;
4728 store->inst_left = sp [0];
4729 store->inst_right = sp [1];
4730 store->flags |= ins_flag;
4731 MONO_ADD_INS (bblock, store);
4743 token = read32 (ip + 1);
4744 if (method->wrapper_type != MONO_WRAPPER_NONE)
4745 klass = mono_method_get_wrapper_data (method, token);
4747 klass = mono_class_get_full (image, token, generic_context);
4748 mono_class_init (klass);
4750 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4755 *sp++ = handle_box (cfg, bblock, val, ip, klass);
4762 MONO_INST_NEW (cfg, ins, *ip);
4767 token = read32 (ip + 1);
4769 /* allocate the domainvar - becaus this is used in decompose_foreach */
4770 if (cfg->opt & MONO_OPT_SHARED) {
4771 mono_get_domainvar (cfg);
4772 /* LAME-IR: Mark it as used since otherwise it will be optimized away */
4773 cfg->domainvar->flags |= MONO_INST_VOLATILE;
4776 if (method->wrapper_type != MONO_WRAPPER_NONE)
4777 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4779 klass = mono_class_get_full (image, token, generic_context);
4781 mono_class_init (klass);
4782 ins->inst_newa_class = klass;
4783 ins->inst_newa_len = *sp;
4784 ins->type = STACK_OBJ;
4788 * we store the object so calls to create the array are not interleaved
4789 * with the arguments of other calls.
4792 MonoInst *store, *temp, *load;
4794 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
4795 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4796 store->cil_code = ins->cil_code;
4797 MONO_ADD_INS (bblock, store);
4798 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
4799 load->cil_code = ins->cil_code;
4806 MONO_INST_NEW (cfg, ins, *ip);
4807 ins->cil_code = ip++;
4809 ins->inst_left = *sp;
4810 ins->type = STACK_PTR;
4818 if (method->wrapper_type != MONO_WRAPPER_NONE)
4819 klass = (MonoClass*)mono_method_get_wrapper_data (method, read32 (ip + 1));
4821 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4822 mono_class_init (klass);
4823 NEW_LDELEMA (cfg, ins, sp, klass);
4828 case CEE_LDELEM_ANY: {
4833 token = read32 (ip + 1);
4834 klass = mono_class_get_full (image, token, generic_context);
4835 mono_class_init (klass);
4836 NEW_LDELEMA (cfg, load, sp, klass);
4837 load->cil_code = ip;
4838 MONO_INST_NEW (cfg, ins, mono_type_to_ldind (&klass->byval_arg));
4840 ins->inst_left = load;
4842 type_to_eval_stack_type (&klass->byval_arg, ins);
4856 case CEE_LDELEM_REF: {
4860 * ldind.x (ldelema (array, index))
4861 * ldelema does the bounds check
4865 klass = array_access_to_klass (*ip);
4866 NEW_LDELEMA (cfg, load, sp, klass);
4867 load->cil_code = ip;
4868 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
4870 ins->inst_left = load;
4872 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
4882 case CEE_STELEM_R8: {
4886 * stind.x (ldelema (array, index), val)
4887 * ldelema does the bounds check
4891 klass = array_access_to_klass (*ip);
4892 NEW_LDELEMA (cfg, load, sp, klass);
4893 load->cil_code = ip;
4894 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
4896 ins->inst_left = load;
4897 ins->inst_right = sp [2];
4899 handle_loaded_temps (cfg, bblock, stack_start, sp);
4900 MONO_ADD_INS (bblock, ins);
4904 case CEE_STELEM_ANY: {
4908 * stind.x (ldelema (array, index), val)
4909 * ldelema does the bounds check
4914 token = read32 (ip + 1);
4915 klass = mono_class_get_full (image, token, generic_context);
4916 mono_class_init (klass);
4917 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4918 MonoMethod* helper = mono_marshal_get_stelemref ();
4919 MonoInst *iargs [3];
4920 handle_loaded_temps (cfg, bblock, stack_start, sp);
4926 mono_emit_method_call_spilled (cfg, bblock, helper, helper->signature, iargs, ip, NULL);
4928 NEW_LDELEMA (cfg, load, sp, klass);
4929 load->cil_code = ip;
4931 n = mono_type_to_stind (&klass->byval_arg);
4933 handle_stobj (cfg, bblock, load, sp [2], ip, klass, FALSE, FALSE);
4935 MONO_INST_NEW (cfg, ins, n);
4937 ins->inst_left = load;
4938 ins->inst_right = sp [2];
4939 handle_loaded_temps (cfg, bblock, stack_start, sp);
4940 MONO_ADD_INS (bblock, ins);
4947 case CEE_STELEM_REF: {
4948 MonoInst *iargs [3];
4949 MonoMethod* helper = mono_marshal_get_stelemref ();
4954 handle_loaded_temps (cfg, bblock, stack_start, sp);
4960 mono_emit_method_call_spilled (cfg, bblock, helper, helper->signature, iargs, ip, NULL);
4964 NEW_GROUP (cfg, group, sp [0], sp [1]);
4965 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
4967 ins->inst_left = group;
4968 ins->inst_right = sp [2];
4969 MONO_ADD_INS (bblock, ins);
4976 case CEE_CKFINITE: {
4977 MonoInst *store, *temp;
4980 /* this instr. can throw exceptions as side effect,
4981 * so we cant eliminate dead code which contains CKFINITE opdodes.
4982 * Spilling to memory makes sure that we always perform
4986 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
4988 ins->inst_left = sp [-1];
4989 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
4991 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4992 store->cil_code = ip;
4993 MONO_ADD_INS (bblock, store);
4995 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
5002 MONO_INST_NEW (cfg, ins, *ip);
5005 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
5006 mono_class_init (klass);
5007 ins->type = STACK_MP;
5008 ins->inst_left = *sp;
5010 ins->inst_newa_class = klass;
5015 case CEE_MKREFANY: {
5016 MonoInst *loc, *klassconst;
5019 MONO_INST_NEW (cfg, ins, *ip);
5022 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
5023 mono_class_init (klass);
5026 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
5027 NEW_TEMPLOADA (cfg, ins->inst_right, loc->inst_c0);
5029 NEW_PCONST (cfg, klassconst, klass);
5030 NEW_GROUP (cfg, ins->inst_left, *sp, klassconst);
5032 MONO_ADD_INS (bblock, ins);
5034 NEW_TEMPLOAD (cfg, *sp, loc->inst_c0);
5041 MonoClass *handle_class;
5043 CHECK_STACK_OVF (1);
5046 n = read32 (ip + 1);
5048 handle = mono_ldtoken (image, n, &handle_class, generic_context);
5049 mono_class_init (handle_class);
5051 if (cfg->opt & MONO_OPT_SHARED) {
5053 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
5055 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
5057 NEW_IMAGECONST (cfg, iargs [0], image);
5058 NEW_ICONST (cfg, iargs [1], n);
5059 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
5060 NEW_TEMPLOAD (cfg, res, temp);
5061 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5062 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
5063 MONO_ADD_INS (bblock, store);
5064 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5066 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method_full (image, read32 (ip + 6), NULL, generic_context)) &&
5067 (cmethod->klass == mono_defaults.monotype_class->parent) &&
5068 (strcmp (cmethod->name, "GetTypeFromHandle") == 0) &&
5069 ((g_hash_table_lookup (bbhash, ip + 5) == NULL) ||
5070 (g_hash_table_lookup (bbhash, ip + 5) == bblock))) {
5071 MonoClass *tclass = mono_class_from_mono_type (handle);
5072 mono_class_init (tclass);
5073 if (mono_compile_aot)
5074 NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n);
5076 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
5077 ins->type = STACK_OBJ;
5078 ins->klass = cmethod->klass;
5081 MonoInst *store, *addr, *vtvar;
5083 if (mono_compile_aot)
5084 NEW_LDTOKENCONST (cfg, ins, image, n);
5086 NEW_PCONST (cfg, ins, handle);
5087 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
5088 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5089 NEW_INDSTORE (cfg, store, addr, ins, &mono_defaults.int_class->byval_arg);
5090 MONO_ADD_INS (bblock, store);
5091 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5107 case CEE_ADD_OVF_UN:
5109 case CEE_MUL_OVF_UN:
5111 case CEE_SUB_OVF_UN:
5114 if (mono_find_jit_opcode_emulation (ins->opcode)) {
5116 *sp++ = emit_tree (cfg, bblock, ins);
5120 case CEE_ENDFINALLY:
5121 MONO_INST_NEW (cfg, ins, *ip);
5122 MONO_ADD_INS (bblock, ins);
5123 ins->cil_code = ip++;
5124 start_new_bblock = 1;
5127 * Control will leave the method so empty the stack, otherwise
5128 * the next basic block will start with a nonempty stack.
5130 while (sp != stack_start) {
5131 MONO_INST_NEW (cfg, ins, CEE_POP);
5135 MONO_ADD_INS (bblock, ins);
5141 if (*ip == CEE_LEAVE) {
5143 target = ip + 5 + (gint32)read32(ip + 1);
5146 target = ip + 2 + (signed char)(ip [1]);
5149 /* empty the stack */
5150 while (sp != stack_start) {
5151 MONO_INST_NEW (cfg, ins, CEE_POP);
5155 MONO_ADD_INS (bblock, ins);
5159 * If this leave statement is in a catch block, check for a
5160 * pending exception, and rethrow it if necessary.
5162 for (i = 0; i < header->num_clauses; ++i) {
5163 MonoExceptionClause *clause = &header->clauses [i];
5164 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code)) {
5167 temp = mono_emit_jit_icall (cfg, bblock, mono_thread_get_pending_exception, NULL, ip);
5168 NEW_TEMPLOAD (cfg, *sp, temp);
5170 MONO_INST_NEW (cfg, ins, OP_THROW_OR_NULL);
5171 ins->inst_left = *sp;
5173 MONO_ADD_INS (bblock, ins);
5177 /* fixme: call fault handler ? */
5179 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
5181 for (tmp = handlers; tmp; tmp = tmp->next) {
5183 link_bblock (cfg, bblock, tblock);
5184 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
5186 ins->inst_target_bb = tblock;
5187 MONO_ADD_INS (bblock, ins);
5189 g_list_free (handlers);
5192 MONO_INST_NEW (cfg, ins, CEE_BR);
5194 MONO_ADD_INS (bblock, ins);
5195 GET_BBLOCK (cfg, bbhash, tblock, target);
5196 link_bblock (cfg, bblock, tblock);
5197 CHECK_BBLOCK (target, ip, tblock);
5198 ins->inst_target_bb = tblock;
5199 start_new_bblock = 1;
5201 if (*ip == CEE_LEAVE)
5210 MONO_INST_NEW (cfg, ins, *ip);
5212 handle_loaded_temps (cfg, bblock, stack_start, sp);
5213 MONO_ADD_INS (bblock, ins);
5214 ins->cil_code = ip++;
5215 ins->inst_i0 = sp [0];
5216 ins->inst_i1 = sp [1];
5224 /* trampoline mono specific opcodes */
5225 case MONO_CUSTOM_PREFIX: {
5227 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
5232 case CEE_MONO_ICALL: {
5235 MonoJitICallInfo *info;
5237 token = read32 (ip + 2);
5238 func = mono_method_get_wrapper_data (method, token);
5239 info = mono_find_jit_icall_by_addr (func);
5242 CHECK_STACK (info->sig->param_count);
5243 sp -= info->sig->param_count;
5245 temp = mono_emit_jit_icall (cfg, bblock, info->func, sp, ip);
5246 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
5247 NEW_TEMPLOAD (cfg, *sp, temp);
5252 inline_costs += 10 * num_calls++;
5256 case CEE_MONO_LDPTR:
5257 CHECK_STACK_OVF (1);
5259 token = read32 (ip + 2);
5260 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
5264 inline_costs += 10 * num_calls++;
5266 case CEE_MONO_VTADDR:
5269 MONO_INST_NEW (cfg, ins, OP_VTADDR);
5271 ins->type = STACK_MP;
5272 ins->inst_left = *sp;
5276 case CEE_MONO_NEWOBJ: {
5277 MonoInst *iargs [2];
5279 CHECK_STACK_OVF (1);
5281 token = read32 (ip + 2);
5282 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5283 mono_class_init (klass);
5284 NEW_DOMAINCONST (cfg, iargs [0]);
5285 NEW_CLASSCONST (cfg, iargs [1], klass);
5286 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
5287 NEW_TEMPLOAD (cfg, *sp, temp);
5290 inline_costs += 10 * num_calls++;
5293 case CEE_MONO_OBJADDR:
5296 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
5298 ins->type = STACK_MP;
5299 ins->inst_left = *sp;
5303 case CEE_MONO_LDNATIVEOBJ:
5306 token = read32 (ip + 2);
5307 klass = mono_method_get_wrapper_data (method, token);
5308 g_assert (klass->valuetype);
5309 mono_class_init (klass);
5310 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
5314 case CEE_MONO_RETOBJ:
5315 g_assert (cfg->ret);
5316 g_assert (method->signature->pinvoke);
5321 token = read32 (ip + 2);
5322 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5324 NEW_RETLOADA (cfg, ins);
5325 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
5327 if (sp != stack_start)
5330 MONO_INST_NEW (cfg, ins, CEE_BR);
5332 ins->inst_target_bb = end_bblock;
5333 MONO_ADD_INS (bblock, ins);
5334 link_bblock (cfg, bblock, end_bblock);
5335 start_new_bblock = 1;
5338 case CEE_MONO_CISINST:
5339 case CEE_MONO_CCASTCLASS: {
5344 token = read32 (ip + 2);
5345 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5346 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_CISINST) ? OP_CISINST : OP_CCASTCLASS);
5347 ins->type = STACK_I4;
5348 ins->inst_left = *sp;
5349 ins->inst_newa_class = klass;
5351 *sp++ = emit_tree (cfg, bblock, ins);
5355 case CEE_MONO_SAVE_LMF:
5356 case CEE_MONO_RESTORE_LMF:
5357 #ifdef MONO_ARCH_HAVE_LMF_OPS
5358 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_SAVE_LMF) ? OP_SAVE_LMF : OP_RESTORE_LMF);
5359 MONO_ADD_INS (bblock, ins);
5360 cfg->need_lmf_area = TRUE;
5365 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
5374 /* somewhat similar to LDTOKEN */
5375 MonoInst *addr, *vtvar;
5376 CHECK_STACK_OVF (1);
5377 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
5379 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5380 addr->cil_code = ip;
5381 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
5383 ins->inst_left = addr;
5384 MONO_ADD_INS (bblock, ins);
5385 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5399 * The following transforms:
5400 * CEE_CEQ into OP_CEQ
5401 * CEE_CGT into OP_CGT
5402 * CEE_CGT_UN into OP_CGT_UN
5403 * CEE_CLT into OP_CLT
5404 * CEE_CLT_UN into OP_CLT_UN
5406 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
5408 MONO_INST_NEW (cfg, ins, cmp->opcode);
5410 cmp->inst_i0 = sp [0];
5411 cmp->inst_i1 = sp [1];
5415 if ((sp [0]->type == STACK_I8) || ((sizeof (gpointer) == 8) && ((sp [0]->type == STACK_PTR) || (sp [0]->type == STACK_OBJ) || (sp [0]->type == STACK_MP))))
5416 cmp->opcode = OP_LCOMPARE;
5418 cmp->opcode = OP_COMPARE;
5420 ins->type = STACK_I4;
5423 /* spill it to reduce the expression complexity
5424 * and workaround bug 54209
5426 if (cmp->inst_left->type == STACK_I8) {
5428 *sp++ = emit_tree (cfg, bblock, ins);
5437 CHECK_STACK_OVF (1);
5439 n = read32 (ip + 2);
5440 if (method->wrapper_type != MONO_WRAPPER_NONE)
5441 cmethod = mono_method_get_wrapper_data (method, n);
5443 cmethod = mono_get_method_full (image, n, NULL, generic_context);
5446 mono_class_init (cmethod->klass);
5447 handle_loaded_temps (cfg, bblock, stack_start, sp);
5449 NEW_METHODCONST (cfg, argconst, cmethod);
5450 if (method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED)
5451 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
5453 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn_nosync, &argconst, ip);
5454 NEW_TEMPLOAD (cfg, *sp, temp);
5458 inline_costs += 10 * num_calls++;
5461 case CEE_LDVIRTFTN: {
5467 n = read32 (ip + 2);
5468 if (method->wrapper_type != MONO_WRAPPER_NONE)
5469 cmethod = mono_method_get_wrapper_data (method, n);
5471 cmethod = mono_get_method_full (image, n, NULL, generic_context);
5473 mono_class_init (cmethod->klass);
5474 handle_loaded_temps (cfg, bblock, stack_start, sp);
5478 NEW_METHODCONST (cfg, args [1], cmethod);
5479 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
5480 NEW_TEMPLOAD (cfg, *sp, temp);
5484 inline_costs += 10 * num_calls++;
5488 CHECK_STACK_OVF (1);
5490 n = read16 (ip + 2);
5492 NEW_ARGLOAD (cfg, ins, n);
5498 CHECK_STACK_OVF (1);
5500 n = read16 (ip + 2);
5502 NEW_ARGLOADA (cfg, ins, n);
5510 handle_loaded_temps (cfg, bblock, stack_start, sp);
5512 n = read16 (ip + 2);
5514 NEW_ARGSTORE (cfg, ins, n, *sp);
5516 if (ins->opcode == CEE_STOBJ) {
5517 NEW_ARGLOADA (cfg, ins, n);
5518 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5520 MONO_ADD_INS (bblock, ins);
5524 CHECK_STACK_OVF (1);
5526 n = read16 (ip + 2);
5528 NEW_LOCLOAD (cfg, ins, n);
5534 CHECK_STACK_OVF (1);
5536 n = read16 (ip + 2);
5538 NEW_LOCLOADA (cfg, ins, n);
5547 n = read16 (ip + 2);
5549 handle_loaded_temps (cfg, bblock, stack_start, sp);
5550 NEW_LOCSTORE (cfg, ins, n, *sp);
5552 if (ins->opcode == CEE_STOBJ) {
5553 NEW_LOCLOADA (cfg, ins, n);
5554 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5556 MONO_ADD_INS (bblock, ins);
5563 if (sp != stack_start)
5565 if (cfg->method != method)
5567 * Inlining this into a loop in a parent could lead to
5568 * stack overflows which is different behavior than the
5569 * non-inlined case, thus disable inlining in this case.
5571 goto inline_failure;
5572 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
5573 ins->inst_left = *sp;
5575 ins->type = STACK_MP;
5577 cfg->flags |= MONO_CFG_HAS_ALLOCA;
5578 if (header->init_locals)
5579 ins->flags |= MONO_INST_INIT;
5583 /* FIXME: set init flag if locals init is set in this method */
5585 case CEE_ENDFILTER: {
5586 MonoExceptionClause *clause, *nearest;
5587 int cc, nearest_num;
5591 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
5593 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
5594 ins->inst_left = *sp;
5596 MONO_ADD_INS (bblock, ins);
5597 start_new_bblock = 1;
5602 for (cc = 0; cc < header->num_clauses; ++cc) {
5603 clause = &header->clauses [cc];
5604 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
5605 (!nearest || (clause->data.filter_offset > nearest->data.filter_offset))) {
5611 filter_lengths [nearest_num] = (ip - header->code) - nearest->data.filter_offset;
5615 case CEE_UNALIGNED_:
5616 ins_flag |= MONO_INST_UNALIGNED;
5617 /* FIXME: record alignment? we can assume 1 for now */
5622 ins_flag |= MONO_INST_VOLATILE;
5626 ins_flag |= MONO_INST_TAILCALL;
5627 cfg->flags |= MONO_CFG_HAS_TAIL;
5628 /* Can't inline tail calls at this time */
5629 inline_costs += 100000;
5636 token = read32 (ip + 2);
5637 if (method->wrapper_type != MONO_WRAPPER_NONE)
5638 klass = mono_method_get_wrapper_data (method, token);
5640 klass = mono_class_get_full (image, token, generic_context);
5641 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5642 MonoInst *store, *load;
5643 NEW_PCONST (cfg, load, NULL);
5644 load->cil_code = ip;
5645 load->type = STACK_OBJ;
5646 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
5647 store->cil_code = ip;
5648 handle_loaded_temps (cfg, bblock, stack_start, sp);
5649 MONO_ADD_INS (bblock, store);
5650 store->inst_i0 = sp [0];
5651 store->inst_i1 = load;
5653 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
5658 case CEE_CONSTRAINED_:
5659 /* FIXME: implement */
5661 token = read32 (ip + 2);
5662 constrained_call = mono_class_get_full (image, token, generic_context);
5667 MonoInst *iargs [3];
5670 if ((cfg->opt & MONO_OPT_INTRINS) && (ip [1] == CEE_CPBLK) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5)) {
5672 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
5673 copy->inst_left = sp [0];
5674 copy->inst_right = sp [1];
5675 copy->cil_code = ip;
5677 MONO_ADD_INS (bblock, copy);
5684 handle_loaded_temps (cfg, bblock, stack_start, sp);
5685 if (ip [1] == CEE_CPBLK) {
5686 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
5688 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
5697 ins_flag |= MONO_INST_NOTYPECHECK;
5699 ins_flag |= MONO_INST_NORANGECHECK;
5700 /* we ignore the no-nullcheck for now since we
5701 * really do it explicitly only when doing callvirt->call
5707 /* FIXME: check we are in a catch handler */
5708 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
5709 load->cil_code = ip;
5710 MONO_INST_NEW (cfg, ins, OP_RETHROW);
5711 ins->inst_left = load;
5713 MONO_ADD_INS (bblock, ins);
5715 start_new_bblock = 1;
5720 CHECK_STACK_OVF (1);
5722 token = read32 (ip + 2);
5723 /* FIXXME: handle generics. */
5724 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
5725 MonoType *type = mono_type_create_from_typespec (image, token);
5726 token = mono_type_size (type, &align);
5728 MonoClass *szclass = mono_class_get_full (image, token, generic_context);
5729 mono_class_init (szclass);
5730 token = mono_class_value_size (szclass, &align);
5732 NEW_ICONST (cfg, ins, token);
5737 case CEE_REFANYTYPE:
5739 MONO_INST_NEW (cfg, ins, OP_REFANYTYPE);
5741 ins->type = STACK_MP;
5742 ins->inst_left = *sp;
5743 ins->type = STACK_VTYPE;
5744 ins->klass = mono_defaults.typehandle_class;
5753 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
5758 g_error ("opcode 0x%02x not handled", *ip);
5761 if (start_new_bblock != 1)
5764 bblock->cil_length = ip - bblock->cil_code;
5765 bblock->next_bb = end_bblock;
5766 link_bblock (cfg, bblock, end_bblock);
5768 if (cfg->method == method && cfg->domainvar) {
5772 MonoInst *get_domain;
5774 if (! (get_domain = mono_arch_get_domain_intrinsic (cfg))) {
5777 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
5778 call->signature = helper_sig_domain_get;
5779 call->inst.type = STACK_PTR;
5780 call->fptr = mono_domain_get;
5781 get_domain = (MonoInst*)call;
5784 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, get_domain);
5785 MONO_ADD_INS (init_localsbb, store);
5788 if (header->init_locals) {
5790 for (i = 0; i < header->num_locals; ++i) {
5791 MonoType *ptype = header->locals [i];
5792 int t = ptype->type;
5793 if (t == MONO_TYPE_VALUETYPE && ptype->data.klass->enumtype)
5794 t = ptype->data.klass->enum_basetype->type;
5795 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
5796 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
5797 NEW_ICONST (cfg, ins, 0);
5798 NEW_LOCSTORE (cfg, store, i, ins);
5799 MONO_ADD_INS (init_localsbb, store);
5800 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
5801 MONO_INST_NEW (cfg, ins, OP_I8CONST);
5802 ins->type = STACK_I8;
5804 NEW_LOCSTORE (cfg, store, i, ins);
5805 MONO_ADD_INS (init_localsbb, store);
5806 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
5807 MONO_INST_NEW (cfg, ins, OP_R8CONST);
5808 ins->type = STACK_R8;
5809 ins->inst_p0 = (void*)&r8_0;
5810 NEW_LOCSTORE (cfg, store, i, ins);
5811 MONO_ADD_INS (init_localsbb, store);
5812 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
5813 ((t == MONO_TYPE_GENERICINST) && mono_metadata_generic_inst_is_valuetype (ptype->data.generic_inst))) {
5814 NEW_LOCLOADA (cfg, ins, i);
5815 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (ptype), NULL, NULL);
5818 NEW_PCONST (cfg, ins, NULL);
5819 NEW_LOCSTORE (cfg, store, i, ins);
5820 MONO_ADD_INS (init_localsbb, store);
5826 /* resolve backward branches in the middle of an existing basic block */
5827 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
5829 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
5830 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
5831 if (tblock != start_bblock) {
5833 split_bblock (cfg, tblock, bblock);
5834 l = bblock->cil_code - header->code;
5835 bblock->cil_length = tblock->cil_length - l;
5836 tblock->cil_length = l;
5838 g_print ("recheck failed.\n");
5843 * we compute regions here, because the length of filter clauses is not known in advance.
5844 * It is computed in the CEE_ENDFILTER case in the above switch statement
5846 if (cfg->method == method) {
5848 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5849 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
5851 mono_create_spvar_for_region (cfg, bb->region);
5852 if (cfg->verbose_level > 2)
5853 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
5856 g_hash_table_destroy (bbhash);
5859 dont_inline = g_list_remove (dont_inline, method);
5860 return inline_costs;
5863 if (cfg->method != method)
5864 g_hash_table_destroy (bbhash);
5865 dont_inline = g_list_remove (dont_inline, method);
5869 if (cfg->method != method)
5870 g_hash_table_destroy (bbhash);
5871 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
5872 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
5873 dont_inline = g_list_remove (dont_inline, method);
5878 mono_print_tree (MonoInst *tree) {
5884 arity = mono_burg_arity [tree->opcode];
5886 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
5888 switch (tree->opcode) {
5890 printf ("[%d]", tree->inst_c0);
5893 printf ("[%lld]", tree->inst_l);
5896 printf ("[%f]", *(double*)tree->inst_p0);
5899 printf ("[%f]", *(float*)tree->inst_p0);
5903 printf ("[%d]", tree->inst_c0);
5906 if (tree->inst_offset < 0)
5907 printf ("[-0x%x(%s)]", -tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5909 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5912 printf ("[%s]", mono_arch_regname (tree->dreg));
5915 printf ("[%s]", tree->inst_newa_class->name);
5916 mono_print_tree (tree->inst_newa_len);
5927 case OP_VOIDCALLVIRT: {
5928 MonoCallInst *call = (MonoCallInst*)tree;
5930 printf ("[%s]", call->method->name);
5935 printf ("[%d (", tree->inst_c0);
5936 for (i = 0; i < tree->inst_phi_args [0]; i++) {
5939 printf ("%d", tree->inst_phi_args [i + 1]);
5950 case OP_LOAD_MEMBASE:
5951 case OP_LOADI4_MEMBASE:
5952 case OP_LOADU4_MEMBASE:
5953 case OP_LOADU1_MEMBASE:
5954 case OP_LOADI1_MEMBASE:
5955 case OP_LOADU2_MEMBASE:
5956 case OP_LOADI2_MEMBASE:
5957 printf ("[%s] <- [%s + 0x%x]", mono_arch_regname (tree->dreg), mono_arch_regname (tree->inst_basereg), tree->inst_offset);
5960 printf ("[B%d]", tree->inst_target_bb->block_num);
5970 case OP_VOIDCALL_REG:
5971 mono_print_tree (tree->inst_left);
5983 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
5984 mono_print_tree (tree->inst_left);
5987 if (!mono_arch_print_tree(tree, arity)) {
5989 mono_print_tree (tree->inst_left);
5991 mono_print_tree (tree->inst_right);
6002 mono_print_tree_nl (MonoInst *tree)
6004 mono_print_tree (tree);
6008 #define make_icall_sig mono_create_icall_signature
6011 create_helper_signature (void)
6013 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
6014 helper_sig_newarr = make_icall_sig ("object ptr ptr int32");
6016 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
6017 helper_sig_newarr_specific = make_icall_sig ("object ptr int32");
6019 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
6020 helper_sig_object_new = make_icall_sig ("object ptr ptr");
6022 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
6023 helper_sig_object_new_specific = make_icall_sig ("object ptr");
6025 /* void* mono_method_compile (MonoMethod*) */
6026 helper_sig_compile = make_icall_sig ("ptr ptr");
6028 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
6029 helper_sig_compile_virt = make_icall_sig ("ptr object ptr");
6031 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
6032 helper_sig_ldstr = make_icall_sig ("object ptr ptr int32");
6034 /* MonoDomain *mono_domain_get (void) */
6035 helper_sig_domain_get = make_icall_sig ("ptr");
6037 /* void stelem_ref (MonoArray *, int index, MonoObject *) */
6038 helper_sig_stelem_ref = make_icall_sig ("void ptr int32 object");
6040 /* void stelem_ref_check (MonoArray *, MonoObject *) */
6041 helper_sig_stelem_ref_check = make_icall_sig ("void object object");
6043 /* long amethod (long, long) */
6044 helper_sig_long_long_long = make_icall_sig ("long long long");
6046 /* object amethod (intptr) */
6047 helper_sig_obj_ptr = make_icall_sig ("object ptr");
6049 helper_sig_obj_ptr_ptr = make_icall_sig ("object ptr ptr");
6051 helper_sig_obj_obj_ptr_ptr = make_icall_sig ("object object ptr ptr");
6053 helper_sig_void_void = make_icall_sig ("void");
6055 /* void amethod (intptr) */
6056 helper_sig_void_ptr = make_icall_sig ("void ptr");
6058 /* void amethod (MonoObject *obj) */
6059 helper_sig_void_obj = make_icall_sig ("void object");
6061 /* void amethod (MonoObject *obj, void *ptr, int i) */
6062 helper_sig_void_obj_ptr_int = make_icall_sig ("void object ptr int");
6064 helper_sig_void_obj_ptr_ptr_obj = make_icall_sig ("void object ptr ptr object");
6066 /* intptr amethod (void) */
6067 helper_sig_ptr_void = make_icall_sig ("ptr");
6069 /* object amethod (void) */
6070 helper_sig_obj_void = make_icall_sig ("object");
6072 /* void amethod (intptr, intptr) */
6073 helper_sig_void_ptr_ptr = make_icall_sig ("void ptr ptr");
6075 /* void amethod (intptr, intptr, intptr) */
6076 helper_sig_void_ptr_ptr_ptr = make_icall_sig ("void ptr ptr ptr");
6078 /* intptr amethod (intptr, intptr) */
6079 helper_sig_ptr_ptr_ptr = make_icall_sig ("ptr ptr ptr");
6081 /* IntPtr amethod (object) */
6082 helper_sig_ptr_obj = make_icall_sig ("ptr object");
6084 /* IntPtr amethod (object, int) */
6085 helper_sig_ptr_obj_int = make_icall_sig ("ptr object int");
6087 /* IntPtr amethod (int) */
6088 helper_sig_ptr_int = make_icall_sig ("ptr int32");
6090 /* long amethod (long, guint32) */
6091 helper_sig_long_long_int = make_icall_sig ("long long int32");
6093 /* ulong amethod (double) */
6094 helper_sig_ulong_double = make_icall_sig ("ulong double");
6096 /* long amethod (double) */
6097 helper_sig_long_double = make_icall_sig ("long double");
6099 /* double amethod (long) */
6100 helper_sig_double_long = make_icall_sig ("double long");
6102 /* double amethod (int) */
6103 helper_sig_double_int = make_icall_sig ("double int32");
6105 /* float amethod (long) */
6106 helper_sig_float_long = make_icall_sig ("float long");
6108 /* double amethod (double, double) */
6109 helper_sig_double_double_double = make_icall_sig ("double double double");
6111 /* uint amethod (double) */
6112 helper_sig_uint_double = make_icall_sig ("uint32 double");
6114 /* int amethod (double) */
6115 helper_sig_int_double = make_icall_sig ("int32 double");
6117 /* void initobj (intptr, int size) */
6118 helper_sig_initobj = make_icall_sig ("void ptr int32");
6120 /* void memcpy (intptr, intptr, int size) */
6121 helper_sig_memcpy = make_icall_sig ("void ptr ptr int32");
6123 /* void memset (intptr, int val, int size) */
6124 helper_sig_memset = make_icall_sig ("void ptr int32 int32");
6126 helper_sig_class_init_trampoline = make_icall_sig ("void");
6130 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
6133 MonoMethod *wrapper;
6136 if (callinfo->wrapper)
6137 return callinfo->wrapper;
6139 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
6140 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
6141 /* Must be domain neutral since there is only one copy */
6142 code = mono_jit_compile_method_with_opt (wrapper, default_opt | MONO_OPT_SHARED);
6144 if (!callinfo->wrapper) {
6145 callinfo->wrapper = code;
6146 mono_register_jit_icall_wrapper (callinfo, code);
6147 mono_debug_add_icall_wrapper (wrapper, callinfo);
6151 return callinfo->wrapper;
6155 mono_create_class_init_trampoline (MonoVTable *vtable)
6159 /* previously created trampoline code */
6160 mono_domain_lock (vtable->domain);
6162 mono_g_hash_table_lookup (vtable->domain->class_init_trampoline_hash,
6164 mono_domain_unlock (vtable->domain);
6168 code = mono_arch_create_class_init_trampoline (vtable);
6170 /* store trampoline address */
6171 mono_domain_lock (vtable->domain);
6172 mono_g_hash_table_insert (vtable->domain->class_init_trampoline_hash,
6174 mono_domain_unlock (vtable->domain);
6176 EnterCriticalSection (&jit_mutex);
6177 if (!class_init_hash_addr)
6178 class_init_hash_addr = g_hash_table_new (NULL, NULL);
6179 g_hash_table_insert (class_init_hash_addr, code, vtable);
6180 LeaveCriticalSection (&jit_mutex);
6186 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method,
6187 gboolean add_sync_wrapper)
6192 if (add_sync_wrapper && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
6193 return mono_create_jump_trampoline (domain, mono_marshal_get_synchronized_wrapper (method), FALSE);
6195 code = mono_jit_find_compiled_method (domain, method);
6199 mono_domain_lock (domain);
6200 code = mono_g_hash_table_lookup (domain->jump_trampoline_hash, method);
6201 mono_domain_unlock (domain);
6205 ji = mono_arch_create_jump_trampoline (method);
6208 * mono_delegate_ctor needs to find the method metadata from the
6209 * trampoline address, so we save it here.
6212 mono_jit_info_table_add (mono_get_root_domain (), ji);
6214 mono_domain_lock (domain);
6215 mono_g_hash_table_insert (domain->jump_trampoline_hash, method, ji->code_start);
6216 mono_domain_unlock (domain);
6218 return ji->code_start;
6222 mono_create_jit_trampoline (MonoMethod *method)
6224 MonoDomain *domain = mono_domain_get ();
6227 /* Trampoline are domain specific, so cache only the one used in the root domain */
6228 if ((domain == mono_get_root_domain ()) && method->info)
6229 return method->info;
6231 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
6232 return mono_create_jit_trampoline (mono_marshal_get_synchronized_wrapper (method));
6234 tramp = mono_arch_create_jit_trampoline (method);
6235 if (domain == mono_get_root_domain ())
6236 method->info = tramp;
6242 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
6246 EnterCriticalSection (&jit_mutex);
6247 if (class_init_hash_addr)
6248 res = g_hash_table_lookup (class_init_hash_addr, addr);
6251 LeaveCriticalSection (&jit_mutex);
6256 mono_dynamic_code_hash_insert (MonoDomain *domain, MonoMethod *method, MonoJitDynamicMethodInfo *ji)
6258 if (!domain->dynamic_code_hash)
6259 domain->dynamic_code_hash = g_hash_table_new (NULL, NULL);
6260 g_hash_table_insert (domain->dynamic_code_hash, method, ji);
6263 static MonoJitDynamicMethodInfo*
6264 mono_dynamic_code_hash_lookup (MonoDomain *domain, MonoMethod *method)
6266 MonoJitDynamicMethodInfo *res;
6268 if (domain->dynamic_code_hash)
6269 res = g_hash_table_lookup (domain->dynamic_code_hash, method);
6276 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func, gboolean no_throw)
6278 MonoJitICallInfo *info;
6280 if (!emul_opcode_map)
6281 emul_opcode_map = g_new0 (MonoJitICallInfo*, OP_LAST + 1);
6283 g_assert (!sig->hasthis);
6284 g_assert (sig->param_count < 3);
6286 info = mono_register_jit_icall (func, name, sig, no_throw);
6288 emul_opcode_map [opcode] = info;
6292 decompose_foreach (MonoInst *tree, gpointer data)
6294 static MonoJitICallInfo *newarr_info = NULL;
6295 static MonoJitICallInfo *newarr_specific_info = NULL;
6296 MonoJitICallInfo *info;
6299 switch (tree->opcode) {
6301 MonoCompile *cfg = data;
6302 MonoInst *iargs [3];
6305 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
6306 g_assert (newarr_info);
6307 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
6308 g_assert (newarr_specific_info);
6311 if (cfg->opt & MONO_OPT_SHARED) {
6312 NEW_DOMAINCONST (cfg, iargs [0]);
6313 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
6314 iargs [2] = tree->inst_newa_len;
6319 MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
6321 NEW_VTABLECONST (cfg, iargs [0], vtable);
6322 iargs [1] = tree->inst_newa_len;
6324 info = newarr_specific_info;
6327 mono_emulate_opcode (cfg, tree, iargs, info);
6329 /* Need to decompose arguments after the the opcode is decomposed */
6330 for (i = 0; i < info->sig->param_count; ++i)
6331 dec_foreach (iargs [i], cfg);
6341 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
6343 switch (mono_burg_arity [tree->opcode]) {
6346 mono_inst_foreach (tree->inst_left, func, data);
6349 mono_inst_foreach (tree->inst_left, func, data);
6350 mono_inst_foreach (tree->inst_right, func, data);
6353 g_assert_not_reached ();
6360 mono_print_bb_code (MonoBasicBlock *bb) {
6362 MonoInst *c = bb->code;
6364 mono_print_tree (c);
6372 print_dfn (MonoCompile *cfg) {
6377 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
6379 for (i = 0; i < cfg->num_bblocks; ++i) {
6380 bb = cfg->bblocks [i];
6381 /*if (bb->cil_code) {
6382 char* code1, *code2;
6383 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
6384 if (bb->last_ins->cil_code)
6385 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
6387 code2 = g_strdup ("");
6389 code1 [strlen (code1) - 1] = 0;
6390 code = g_strdup_printf ("%s -> %s", code1, code2);
6394 code = g_strdup ("\n");
6395 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
6397 MonoInst *c = bb->code;
6399 mono_print_tree (c);
6407 g_print ("\tprev:");
6408 for (j = 0; j < bb->in_count; ++j) {
6409 g_print (" BB%d", bb->in_bb [j]->block_num);
6411 g_print ("\t\tsucc:");
6412 for (j = 0; j < bb->out_count; ++j) {
6413 g_print (" BB%d", bb->out_bb [j]->block_num);
6415 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
6418 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
6421 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
6423 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
6431 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
6435 g_assert (bb->code);
6436 bb->last_ins->next = inst;
6437 bb->last_ins = inst;
6439 bb->last_ins = bb->code = inst;
6444 mono_destroy_compile (MonoCompile *cfg)
6446 //mono_mempool_stats (cfg->mempool);
6447 g_hash_table_destroy (cfg->bb_hash);
6448 mono_free_loop_info (cfg);
6450 mono_regstate_free (cfg->rs);
6452 g_hash_table_destroy (cfg->spvars);
6453 mono_mempool_destroy (cfg->mempool);
6454 g_list_free (cfg->ldstr_list);
6456 g_free (cfg->varinfo);
6461 #ifdef HAVE_KW_THREAD
6462 static __thread gpointer mono_lmf_addr;
6466 mono_get_lmf_addr (void)
6468 #ifdef HAVE_KW_THREAD
6469 return mono_lmf_addr;
6471 MonoJitTlsData *jit_tls;
6473 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
6474 return &jit_tls->lmf;
6476 g_assert_not_reached ();
6482 * mono_thread_abort:
6483 * @obj: exception object
6485 * abort the thread, print exception information and stack trace
6488 mono_thread_abort (MonoObject *obj)
6490 /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
6492 /* handle_remove should be eventually called for this thread, too
6495 mono_thread_exit ();
6499 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
6501 MonoJitTlsData *jit_tls;
6504 jit_tls = TlsGetValue (mono_jit_tls_id);
6508 jit_tls = g_new0 (MonoJitTlsData, 1);
6510 TlsSetValue (mono_jit_tls_id, jit_tls);
6512 jit_tls->abort_func = abort_func;
6513 jit_tls->end_of_stack = stack_start;
6515 lmf = g_new0 (MonoLMF, 1);
6518 jit_tls->lmf = jit_tls->first_lmf = lmf;
6520 #ifdef HAVE_KW_THREAD
6521 mono_lmf_addr = &jit_tls->lmf;
6524 mono_arch_setup_jit_tls_data (jit_tls);
6530 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
6533 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
6534 thread = mono_thread_current ();
6536 thread->jit_data = jit_tls;
6539 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
6542 mono_thread_abort_dummy (MonoObject *obj)
6544 if (mono_thread_attach_aborted_cb)
6545 mono_thread_attach_aborted_cb (obj);
6547 mono_thread_abort (obj);
6551 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
6554 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
6555 thread = mono_thread_current ();
6557 thread->jit_data = jit_tls;
6561 mini_thread_cleanup (MonoThread *thread)
6563 MonoJitTlsData *jit_tls = thread->jit_data;
6566 mono_arch_free_jit_tls_data (jit_tls);
6567 g_free (jit_tls->first_lmf);
6569 thread->jit_data = NULL;
6574 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
6576 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
6580 ji->data.target = target;
6581 ji->next = cfg->patch_info;
6583 cfg->patch_info = ji;
6587 mono_remove_patch_info (MonoCompile *cfg, int ip)
6589 MonoJumpInfo **ji = &cfg->patch_info;
6592 if ((*ji)->ip.i == ip)
6595 ji = &((*ji)->next);
6600 mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors)
6602 unsigned char *ip = patch_info->ip.i + code;
6603 gconstpointer target = NULL;
6605 switch (patch_info->type) {
6606 case MONO_PATCH_INFO_BB:
6607 target = patch_info->data.bb->native_offset + code;
6609 case MONO_PATCH_INFO_ABS:
6610 target = patch_info->data.target;
6612 case MONO_PATCH_INFO_LABEL:
6613 target = patch_info->data.inst->inst_c0 + code;
6615 case MONO_PATCH_INFO_IP:
6618 case MONO_PATCH_INFO_METHOD_REL:
6619 target = code + patch_info->data.offset;
6621 case MONO_PATCH_INFO_INTERNAL_METHOD: {
6622 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
6624 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
6625 g_assert_not_reached ();
6627 target = mono_icall_get_wrapper (mi);
6630 case MONO_PATCH_INFO_METHOD_JUMP: {
6633 /* get the trampoline to the method from the domain */
6634 target = mono_create_jump_trampoline (domain, patch_info->data.method, TRUE);
6635 if (!domain->jump_target_hash)
6636 domain->jump_target_hash = g_hash_table_new (NULL, NULL);
6637 list = g_hash_table_lookup (domain->jump_target_hash, patch_info->data.method);
6638 list = g_slist_prepend (list, ip);
6639 g_hash_table_insert (domain->jump_target_hash, patch_info->data.method, list);
6642 case MONO_PATCH_INFO_METHOD:
6643 if (patch_info->data.method == method) {
6646 /* get the trampoline to the method from the domain */
6647 target = mono_create_jit_trampoline (patch_info->data.method);
6649 case MONO_PATCH_INFO_SWITCH: {
6650 gpointer *jump_table;
6653 if (method->dynamic) {
6654 jump_table = mono_code_manager_reserve (mono_dynamic_code_hash_lookup (domain, method)->code_mp, sizeof (gpointer) * patch_info->table_size);
6656 mono_domain_lock (domain);
6657 jump_table = mono_code_manager_reserve (domain->code_mp, sizeof (gpointer) * patch_info->table_size);
6658 mono_domain_unlock (domain);
6661 for (i = 0; i < patch_info->table_size; i++) {
6662 jump_table [i] = code + (int)patch_info->data.table [i];
6664 target = jump_table;
6667 case MONO_PATCH_INFO_METHODCONST:
6668 case MONO_PATCH_INFO_CLASS:
6669 case MONO_PATCH_INFO_IMAGE:
6670 case MONO_PATCH_INFO_FIELD:
6671 target = patch_info->data.target;
6673 case MONO_PATCH_INFO_IID:
6674 mono_class_init (patch_info->data.klass);
6675 target = (gpointer)patch_info->data.klass->interface_id;
6677 case MONO_PATCH_INFO_VTABLE:
6678 target = mono_class_vtable (domain, patch_info->data.klass);
6680 case MONO_PATCH_INFO_CLASS_INIT:
6681 target = mono_create_class_init_trampoline (mono_class_vtable (domain, patch_info->data.klass));
6683 case MONO_PATCH_INFO_SFLDA: {
6684 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
6685 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (vtable->klass, method))
6686 /* Done by the generated code */
6690 mono_runtime_class_init (vtable);
6692 target = (char*)vtable->data + patch_info->data.field->offset;
6695 case MONO_PATCH_INFO_R4:
6696 case MONO_PATCH_INFO_R8:
6697 target = patch_info->data.target;
6699 case MONO_PATCH_INFO_EXC_NAME:
6700 target = patch_info->data.name;
6702 case MONO_PATCH_INFO_LDSTR:
6704 mono_ldstr (domain, patch_info->data.token->image,
6705 mono_metadata_token_index (patch_info->data.token->token));
6707 case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
6709 MonoClass *handle_class;
6711 handle = mono_ldtoken (patch_info->data.token->image,
6712 patch_info->data.token->token, &handle_class, NULL);
6713 mono_class_init (handle_class);
6714 mono_class_init (mono_class_from_mono_type (handle));
6717 mono_type_get_object (domain, handle);
6720 case MONO_PATCH_INFO_LDTOKEN: {
6722 MonoClass *handle_class;
6724 handle = mono_ldtoken (patch_info->data.token->image,
6725 patch_info->data.token->token, &handle_class, NULL);
6726 mono_class_init (handle_class);
6731 case MONO_PATCH_INFO_BB_OVF:
6732 case MONO_PATCH_INFO_EXC_OVF:
6735 g_assert_not_reached ();
6738 return (gpointer)target;
6742 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
6743 MonoJitICallInfo *info;
6745 decompose_foreach (tree, cfg);
6747 switch (mono_burg_arity [tree->opcode]) {
6750 dec_foreach (tree->inst_left, cfg);
6752 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6753 MonoInst *iargs [2];
6755 iargs [0] = tree->inst_left;
6757 mono_emulate_opcode (cfg, tree, iargs, info);
6763 #ifdef MONO_ARCH_BIGMUL_INTRINS
6764 if (tree->opcode == OP_LMUL
6765 && (cfg->opt & MONO_OPT_INTRINS)
6766 && (tree->inst_left->opcode == CEE_CONV_I8
6767 || tree->inst_left->opcode == CEE_CONV_U8)
6768 && tree->inst_left->inst_left->type == STACK_I4
6769 && (tree->inst_right->opcode == CEE_CONV_I8
6770 || tree->inst_right->opcode == CEE_CONV_U8)
6771 && tree->inst_right->inst_left->type == STACK_I4
6772 && tree->inst_left->opcode == tree->inst_right->opcode) {
6773 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
6774 tree->inst_left = tree->inst_left->inst_left;
6775 tree->inst_right = tree->inst_right->inst_left;
6776 dec_foreach (tree, cfg);
6779 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6780 MonoInst *iargs [2];
6782 iargs [0] = tree->inst_i0;
6783 iargs [1] = tree->inst_i1;
6785 mono_emulate_opcode (cfg, tree, iargs, info);
6787 dec_foreach (iargs [0], cfg);
6788 dec_foreach (iargs [1], cfg);
6791 dec_foreach (tree->inst_left, cfg);
6792 dec_foreach (tree->inst_right, cfg);
6796 g_assert_not_reached ();
6801 decompose_pass (MonoCompile *cfg) {
6804 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6807 cfg->prev_ins = NULL;
6808 for (tree = cfg->cbb->code; tree; tree = tree->next) {
6809 dec_foreach (tree, cfg);
6810 cfg->prev_ins = tree;
6816 nullify_basic_block (MonoBasicBlock *bb)
6823 bb->code = bb->last_ins = NULL;
6824 bb->cil_code = NULL;
6828 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6832 for (i = 0; i < bb->out_count; i++) {
6833 MonoBasicBlock *ob = bb->out_bb [i];
6836 if (bb->out_count > 1) {
6837 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
6841 bb->out_bb [i] = repl;
6848 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6852 for (i = 0; i < bb->in_count; i++) {
6853 MonoBasicBlock *ib = bb->in_bb [i];
6856 if (bb->in_count > 1) {
6857 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
6861 bb->in_bb [i] = repl;
6868 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6872 for (i = 0; i < bb->out_count; i++) {
6873 MonoBasicBlock *ob = bb->out_bb [i];
6874 for (j = 0; j < ob->in_count; j++) {
6875 if (ob->in_bb [j] == orig) {
6876 ob->in_bb [j] = repl;
6885 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
6887 bb->out_count = bbn->out_count;
6888 bb->out_bb = bbn->out_bb;
6890 replace_basic_block (bb, bbn, bb);
6894 bb->last_ins->next = bbn->code;
6895 bb->last_ins = bbn->last_ins;
6898 bb->code = bbn->code;
6899 bb->last_ins = bbn->last_ins;
6901 bb->next_bb = bbn->next_bb;
6902 nullify_basic_block (bbn);
6906 * Optimizes the branches on the Control Flow Graph
6910 optimize_branches (MonoCompile *cfg)
6912 int i, changed = FALSE;
6913 MonoBasicBlock *bb, *bbn;
6914 guint32 niterations;
6917 * Some crazy loops could cause the code below to go into an infinite
6918 * loop, see bug #53003 for an example. To prevent this, we put an upper
6919 * bound on the number of iterations.
6926 /* we skip the entry block (exit is handled specially instead ) */
6927 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
6929 /* dont touch code inside exception clauses */
6930 if (bb->region != -1)
6933 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
6934 if (cfg->verbose_level > 2)
6935 g_print ("nullify block triggered %d\n", bbn->block_num);
6937 bb->next_bb = bbn->next_bb;
6939 for (i = 0; i < bbn->out_count; i++)
6940 replace_in_block (bbn->out_bb [i], bbn, NULL);
6942 nullify_basic_block (bbn);
6946 if (bb->out_count == 1) {
6947 bbn = bb->out_bb [0];
6949 /* conditional branches where true and false targets are the same can be also replaced with CEE_BR */
6950 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins)) {
6951 bb->last_ins->opcode = CEE_BR;
6952 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
6954 if (cfg->verbose_level > 2)
6955 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
6958 if (bb->region == bbn->region && bb->next_bb == bbn) {
6959 /* the block are in sequence anyway ... */
6961 /* branches to the following block can be removed */
6962 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
6963 bb->last_ins->opcode = CEE_NOP;
6965 if (cfg->verbose_level > 2)
6966 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
6969 if (bbn->in_count == 1) {
6971 if (bbn != cfg->bb_exit) {
6972 if (cfg->verbose_level > 2)
6973 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
6974 merge_basic_blocks (bb, bbn);
6978 //mono_print_bb_code (bb);
6983 } while (changed && (niterations > 0));
6990 /* we skip the entry block (exit is handled specially instead ) */
6991 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
6993 /* dont touch code inside exception clauses */
6994 if (bb->region != -1)
6997 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
6998 if (cfg->verbose_level > 2) {
6999 g_print ("nullify block triggered %d\n", bbn->block_num);
7001 bb->next_bb = bbn->next_bb;
7003 for (i = 0; i < bbn->out_count; i++)
7004 replace_in_block (bbn->out_bb [i], bbn, NULL);
7006 nullify_basic_block (bbn);
7012 if (bb->out_count == 1) {
7013 bbn = bb->out_bb [0];
7015 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
7016 bbn = bb->last_ins->inst_target_bb;
7017 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7018 bbn->code->inst_target_bb->region == bb->region) {
7020 if (cfg->verbose_level > 2)
7021 g_print ("in %s branch to branch triggered %d -> %d -> %d\n", cfg->method->name,
7022 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num);
7024 replace_in_block (bbn, bb, NULL);
7025 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7026 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7027 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
7032 } else if (bb->out_count == 2) {
7033 if (bb->last_ins && MONO_IS_COND_BRANCH_NOFP (bb->last_ins)) {
7034 bbn = bb->last_ins->inst_true_bb;
7035 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7036 bbn->code->inst_target_bb->region == bb->region) {
7037 if (cfg->verbose_level > 2)
7038 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
7039 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
7042 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
7044 replace_in_block (bbn, bb, NULL);
7046 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
7047 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7049 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7055 bbn = bb->last_ins->inst_false_bb;
7056 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7057 bbn->code->inst_target_bb->region == bb->region) {
7058 if (cfg->verbose_level > 2)
7059 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
7060 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
7063 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
7065 replace_in_block (bbn, bb, NULL);
7067 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
7068 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7070 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7078 } while (changed && (niterations > 0));
7083 mono_compile_create_vars (MonoCompile *cfg)
7085 MonoMethodSignature *sig;
7086 MonoMethodHeader *header;
7089 header = mono_method_get_header (cfg->method);
7091 sig = cfg->method->signature;
7093 if (!MONO_TYPE_IS_VOID (sig->ret)) {
7094 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
7095 cfg->ret->opcode = OP_RETARG;
7096 cfg->ret->inst_vtype = sig->ret;
7097 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
7099 if (cfg->verbose_level > 2)
7100 g_print ("creating vars\n");
7103 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
7105 for (i = 0; i < sig->param_count; ++i)
7106 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
7108 cfg->locals_start = cfg->num_varinfo;
7110 if (cfg->verbose_level > 2)
7111 g_print ("creating locals\n");
7112 for (i = 0; i < header->num_locals; ++i)
7113 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
7114 if (cfg->verbose_level > 2)
7115 g_print ("locals done\n");
7119 mono_print_code (MonoCompile *cfg)
7123 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7124 MonoInst *tree = bb->code;
7129 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
7131 for (; tree; tree = tree->next) {
7132 mono_print_tree (tree);
7137 bb->last_ins->next = NULL;
7141 extern const char * const mono_burg_rule_string [];
7144 emit_state (MonoCompile *cfg, MBState *state, int goal)
7147 int ern = mono_burg_rule (state, goal);
7148 const guint16 *nts = mono_burg_nts [ern];
7151 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
7155 // state->reg1 = state->reg2; /* chain rule */
7157 #ifdef MONO_ARCH_ENABLE_EMIT_STATE_OPT
7160 state->reg1 = mono_regstate_next_int (cfg->rs);
7161 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
7164 state->reg1 = mono_regstate_next_int (cfg->rs);
7165 state->reg2 = mono_regstate_next_int (cfg->rs);
7168 state->reg1 = mono_regstate_next_float (cfg->rs);
7171 #ifdef MONO_ARCH_ENABLE_EMIT_STATE_OPT
7173 * Enabling this might cause bugs to surface in the local register
7174 * allocators on some architectures like x86.
7176 if ((state->tree->ssa_op == MONO_SSA_STORE) && (state->left->tree->opcode == OP_REGVAR)) {
7177 /* Do not optimize away reg-reg moves */
7178 if (! ((state->right->tree->ssa_op == MONO_SSA_LOAD) && (state->right->left->tree->opcode == OP_REGVAR))) {
7179 state->right->reg1 = state->left->tree->dreg;
7188 mono_burg_kids (state, ern, kids);
7190 emit_state (cfg, kids [0], nts [0]);
7192 emit_state (cfg, kids [1], nts [1]);
7194 g_assert (!nts [3]);
7195 emit_state (cfg, kids [2], nts [2]);
7200 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
7201 if ((emit = mono_burg_func [ern]))
7202 emit (state, state->tree, cfg);
7205 #define DEBUG_SELECTION
7208 mini_select_instructions (MonoCompile *cfg)
7210 static const int reverse_map [] = {
7211 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
7212 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
7214 static const int reverse_fmap [] = {
7215 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
7216 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
7218 static const int reverse_lmap [] = {
7219 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
7220 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
7222 static const int reverse_imap [] = {
7223 OP_IBNE_UN, OP_IBLT, OP_IBLE, OP_IBGT, OP_IBGE,
7224 OP_IBEQ, OP_IBLT_UN, OP_IBLE_UN, OP_IBGT_UN, OP_IBGE_UN
7229 cfg->state_pool = mono_mempool_new ();
7230 cfg->rs = mono_regstate_new ();
7232 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7233 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins) &&
7234 bb->next_bb != bb->last_ins->inst_false_bb) {
7236 /* we are careful when inverting, since bugs like #59580
7237 * could show up when dealing with NaNs.
7239 if (MONO_IS_COND_BRANCH_NOFP(bb->last_ins) && bb->next_bb == bb->last_ins->inst_true_bb) {
7240 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
7241 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
7242 bb->last_ins->inst_false_bb = tmp;
7244 if (bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
7245 bb->last_ins->opcode = reverse_map [bb->last_ins->opcode - CEE_BEQ];
7246 } else if (bb->last_ins->opcode >= OP_FBEQ && bb->last_ins->opcode <= OP_FBLT_UN) {
7247 bb->last_ins->opcode = reverse_fmap [bb->last_ins->opcode - OP_FBEQ];
7248 } else if (bb->last_ins->opcode >= OP_LBEQ && bb->last_ins->opcode <= OP_LBLT_UN) {
7249 bb->last_ins->opcode = reverse_lmap [bb->last_ins->opcode - OP_LBEQ];
7250 } else if (bb->last_ins->opcode >= OP_IBEQ && bb->last_ins->opcode <= OP_IBLT_UN) {
7251 bb->last_ins->opcode = reverse_imap [bb->last_ins->opcode - OP_IBEQ];
7254 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
7255 inst->opcode = CEE_BR;
7256 inst->inst_target_bb = bb->last_ins->inst_false_bb;
7257 mono_bblock_add_inst (bb, inst);
7262 #ifdef DEBUG_SELECTION
7263 if (cfg->verbose_level >= 4) {
7264 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7265 MonoInst *tree = bb->code;
7266 g_print ("DUMP BLOCK %d:\n", bb->block_num);
7269 for (; tree; tree = tree->next) {
7270 mono_print_tree (tree);
7277 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7278 MonoInst *tree = bb->code, *next;
7284 bb->last_ins = NULL;
7287 mono_regstate_reset (cfg->rs);
7289 #ifdef DEBUG_SELECTION
7290 if (cfg->verbose_level >= 3)
7291 g_print ("LABEL BLOCK %d:\n", bb->block_num);
7293 for (; tree; tree = next) {
7295 #ifdef DEBUG_SELECTION
7296 if (cfg->verbose_level >= 3) {
7297 mono_print_tree (tree);
7302 if (!(mbstate = mono_burg_label (tree, cfg))) {
7303 g_warning ("unable to label tree %p", tree);
7304 mono_print_tree (tree);
7306 g_assert_not_reached ();
7308 emit_state (cfg, mbstate, MB_NTERM_stmt);
7310 bb->max_ireg = cfg->rs->next_vireg;
7311 bb->max_freg = cfg->rs->next_vfreg;
7314 bb->last_ins->next = NULL;
7316 mono_mempool_empty (cfg->state_pool);
7318 mono_mempool_destroy (cfg->state_pool);
7322 mono_codegen (MonoCompile *cfg)
7324 MonoJumpInfo *patch_info;
7326 int i, max_epilog_size;
7329 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7330 cfg->spill_count = 0;
7331 /* we reuse dfn here */
7332 /* bb->dfn = bb_count++; */
7333 mono_arch_local_regalloc (cfg, bb);
7336 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
7337 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
7339 code = mono_arch_emit_prolog (cfg);
7341 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7342 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
7344 cfg->code_len = code - cfg->native_code;
7345 cfg->prolog_end = cfg->code_len;
7347 mono_debug_open_method (cfg);
7349 /* emit code all basic blocks */
7350 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7351 bb->native_offset = cfg->code_len;
7352 mono_arch_output_basic_block (cfg, bb);
7354 cfg->bb_exit->native_offset = cfg->code_len;
7356 code = cfg->native_code + cfg->code_len;
7358 max_epilog_size = mono_arch_max_epilog_size (cfg);
7360 /* we always allocate code in cfg->domain->code_mp to increase locality */
7361 cfg->code_size = cfg->code_len + max_epilog_size;
7362 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
7364 if (cfg->method->dynamic) {
7365 /* Allocate the code into a separate memory pool so it can be freed */
7366 cfg->dynamic_info = g_new0 (MonoJitDynamicMethodInfo, 1);
7367 cfg->dynamic_info->code_mp = mono_code_manager_new_dynamic ();
7368 mono_domain_lock (cfg->domain);
7369 mono_dynamic_code_hash_insert (cfg->domain, cfg->method, cfg->dynamic_info);
7370 mono_domain_unlock (cfg->domain);
7372 code = mono_code_manager_reserve (cfg->dynamic_info->code_mp, cfg->code_size);
7374 mono_domain_lock (cfg->domain);
7375 code = mono_code_manager_reserve (cfg->domain->code_mp, cfg->code_size);
7376 mono_domain_unlock (cfg->domain);
7379 memcpy (code, cfg->native_code, cfg->code_len);
7380 g_free (cfg->native_code);
7381 cfg->native_code = code;
7382 code = cfg->native_code + cfg->code_len;
7384 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
7386 cfg->epilog_begin = cfg->code_len;
7388 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7389 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
7391 cfg->code_len = code - cfg->native_code;
7393 mono_arch_emit_epilog (cfg);
7395 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7396 switch (patch_info->type) {
7397 case MONO_PATCH_INFO_ABS: {
7398 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
7400 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
7401 if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) &&
7402 strstr (cfg->method->name, info->name))
7404 * This is an icall wrapper, and this is a call to the
7409 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
7410 patch_info->data.name = info->name;
7414 MonoVTable *vtable = mono_find_class_init_trampoline_by_addr (patch_info->data.target);
7416 patch_info->type = MONO_PATCH_INFO_CLASS_INIT;
7417 patch_info->data.klass = vtable->klass;
7422 case MONO_PATCH_INFO_SWITCH: {
7424 if (cfg->method->dynamic) {
7425 table = mono_code_manager_reserve (cfg->dynamic_info->code_mp, sizeof (gpointer) * patch_info->table_size);
7427 mono_domain_lock (cfg->domain);
7428 table = mono_code_manager_reserve (cfg->domain->code_mp, sizeof (gpointer) * patch_info->table_size);
7429 mono_domain_unlock (cfg->domain);
7432 patch_info->ip.i = patch_info->ip.label->inst_c0;
7433 for (i = 0; i < patch_info->table_size; i++) {
7434 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
7436 patch_info->data.target = table;
7445 if (cfg->verbose_level > 0)
7446 g_print ("Method %s emitted at %p to %p [%s]\n",
7447 mono_method_full_name (cfg->method, TRUE),
7448 cfg->native_code, cfg->native_code + cfg->code_len, cfg->domain->friendly_name);
7450 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info, cfg->run_cctors);
7452 if (cfg->method->dynamic) {
7453 mono_code_manager_commit (cfg->dynamic_info->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
7455 mono_domain_lock (cfg->domain);
7456 mono_code_manager_commit (cfg->domain->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
7457 mono_domain_unlock (cfg->domain);
7460 mono_arch_flush_icache (cfg->native_code, cfg->code_len);
7462 mono_debug_close_method (cfg);
7466 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
7471 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
7472 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
7474 if (cp->opcode == OP_ICONST) {
7475 if (cfg->opt & MONO_OPT_CONSPROP) {
7476 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
7480 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
7481 if (cfg->opt & MONO_OPT_COPYPROP) {
7482 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
7488 arity = mono_burg_arity [tree->opcode];
7491 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
7492 if (cfg->opt & MONO_OPT_CFOLD)
7493 mono_constant_fold_inst (tree, NULL);
7494 /* The opcode may have changed */
7495 if (mono_burg_arity [tree->opcode] > 1) {
7496 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
7497 if (cfg->opt & MONO_OPT_CFOLD)
7498 mono_constant_fold_inst (tree, NULL);
7500 mono_constant_fold_inst (tree, NULL);
7506 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
7510 switch (tree->opcode) {
7520 if (tree->ssa_op == MONO_SSA_NOP) {
7521 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7538 case OP_VOIDCALL_REG:
7539 case OP_VOIDCALLVIRT:
7541 MonoCallInst *call = (MonoCallInst *)tree;
7542 MonoMethodSignature *sig = call->signature;
7543 int i, byref = FALSE;
7545 for (i = 0; i < sig->param_count; i++) {
7546 if (sig->params [i]->byref) {
7553 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7561 arity = mono_burg_arity [tree->opcode];
7567 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7570 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7571 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
7574 g_assert_not_reached ();
7579 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
7581 MonoInst *tree = bb->code;
7587 for (; tree; tree = tree->next) {
7589 mono_cprop_copy_values (cfg, tree, acp);
7591 mono_cprop_invalidate_values (tree, acp, acp_size);
7593 if (tree->ssa_op == MONO_SSA_STORE &&
7594 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
7595 MonoInst *i1 = tree->inst_i1;
7597 acp [tree->inst_i0->inst_c0] = NULL;
7599 for (i = 0; i < acp_size; i++) {
7600 if (acp [i] && acp [i]->opcode != OP_ICONST &&
7601 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
7606 if (i1->opcode == OP_ICONST) {
7607 acp [tree->inst_i0->inst_c0] = i1;
7608 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
7610 if (i1->ssa_op == MONO_SSA_LOAD &&
7611 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
7612 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
7613 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
7614 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
7619 if (tree->opcode == CEE_BEQ) {
7620 g_assert (tree->inst_i0->opcode == OP_COMPARE);
7621 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
7622 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
7624 tree->opcode = CEE_BR;
7625 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
7626 tree->inst_target_bb = tree->inst_true_bb;
7628 tree->inst_target_bb = tree->inst_false_bb;
7637 mono_local_cprop (MonoCompile *cfg)
7642 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
7644 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7645 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
7646 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
7651 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, int parts)
7653 MonoMethodHeader *header = mono_method_get_header (method);
7654 guint8 *ip = (guint8 *)header->code;
7657 int dfn = 0, i, code_size_ratio;
7659 mono_jit_stats.methods_compiled++;
7660 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
7661 mono_profiler_method_jit (method);
7663 cfg = g_new0 (MonoCompile, 1);
7664 cfg->method = method;
7665 cfg->mempool = mono_mempool_new ();
7667 cfg->prof_options = mono_profiler_get_events ();
7668 cfg->run_cctors = run_cctors;
7669 cfg->bb_hash = g_hash_table_new (NULL, NULL);
7670 cfg->domain = domain;
7671 cfg->verbose_level = mini_verbose;
7672 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
7673 mono_method_get_header (method)->max_stack);
7675 if (cfg->verbose_level > 2)
7676 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
7679 * create MonoInst* which represents arguments and local variables
7681 mono_compile_create_vars (cfg);
7683 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
7684 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7685 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
7686 mono_destroy_compile (cfg);
7690 mono_jit_stats.basic_blocks += cfg->num_bblocks;
7691 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
7693 if (cfg->num_varinfo > 2000) {
7695 * we disable some optimizations if there are too many variables
7696 * because JIT time may become too expensive. The actual number needs
7697 * to be tweaked and eventually the non-linear algorithms should be fixed.
7699 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
7700 cfg->disable_ssa = TRUE;
7702 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
7704 /* Depth-first ordering on basic blocks */
7705 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
7707 if (cfg->opt & MONO_OPT_BRANCH)
7708 optimize_branches (cfg);
7710 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
7711 if (cfg->num_bblocks != dfn + 1) {
7714 cfg->num_bblocks = dfn + 1;
7716 if (!header->clauses) {
7717 /* remove unreachable code, because the code in them may be
7718 * inconsistent (access to dead variables for example) */
7719 for (bb = cfg->bb_entry; bb;) {
7720 MonoBasicBlock *bbn = bb->next_bb;
7722 if (bbn && bbn->region == -1 && !bbn->dfn) {
7723 if (cfg->verbose_level > 1)
7724 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
7725 bb->next_bb = bbn->next_bb;
7726 nullify_basic_block (bbn);
7734 if (cfg->opt & MONO_OPT_LOOP) {
7735 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
7736 mono_compute_natural_loops (cfg);
7739 /* after method_to_ir */
7743 //#define DEBUGSSA "logic_run"
7744 #define DEBUGSSA_CLASS "Tests"
7747 if (!header->num_clauses && !cfg->disable_ssa) {
7748 mono_local_cprop (cfg);
7749 mono_ssa_compute (cfg);
7753 /* fixme: add all optimizations which requires SSA */
7754 if (cfg->opt & (MONO_OPT_DEADCE | MONO_OPT_ABCREM)) {
7755 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
7756 mono_local_cprop (cfg);
7757 mono_ssa_compute (cfg);
7759 if (cfg->verbose_level >= 2) {
7766 /* after SSA translation */
7770 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
7771 if (cfg->comp_done & MONO_COMP_SSA) {
7772 mono_ssa_cprop (cfg);
7774 mono_local_cprop (cfg);
7778 if (cfg->comp_done & MONO_COMP_SSA) {
7779 mono_ssa_deadce (cfg);
7781 //mono_ssa_strength_reduction (cfg);
7783 if ((cfg->flags & MONO_CFG_HAS_LDELEMA) && (cfg->opt & MONO_OPT_ABCREM))
7784 mono_perform_abc_removal (cfg);
7786 mono_ssa_remove (cfg);
7788 if (cfg->opt & MONO_OPT_BRANCH)
7789 optimize_branches (cfg);
7792 /* after SSA removal */
7796 decompose_pass (cfg);
7798 if (cfg->opt & MONO_OPT_LINEARS) {
7801 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
7802 cfg->comp_done &= ~MONO_COMP_LIVENESS;
7803 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
7804 mono_analyze_liveness (cfg);
7806 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
7807 regs = mono_arch_get_global_int_regs (cfg);
7808 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
7812 //mono_print_code (cfg);
7816 /* variables are allocated after decompose, since decompose could create temps */
7817 mono_arch_allocate_vars (cfg);
7819 if (cfg->opt & MONO_OPT_CFOLD)
7820 mono_constant_fold (cfg);
7822 mini_select_instructions (cfg);
7825 if (cfg->verbose_level >= 2) {
7826 char *id = mono_method_full_name (cfg->method, FALSE);
7827 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
7831 if (cfg->method->dynamic)
7832 jinfo = g_new0 (MonoJitInfo, 1);
7834 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
7836 jinfo->method = method;
7837 jinfo->code_start = cfg->native_code;
7838 jinfo->code_size = cfg->code_len;
7839 jinfo->used_regs = cfg->used_int_regs;
7840 jinfo->domain_neutral = (cfg->opt & MONO_OPT_SHARED) != 0;
7842 if (header->num_clauses) {
7845 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
7846 jinfo->num_clauses = header->num_clauses;
7847 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
7848 sizeof (MonoJitExceptionInfo) * header->num_clauses);
7850 for (i = 0; i < header->num_clauses; i++) {
7851 MonoExceptionClause *ec = &header->clauses [i];
7852 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
7853 MonoBasicBlock *tblock;
7855 ei->flags = ec->flags;
7857 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7858 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->data.filter_offset);
7860 ei->data.filter = cfg->native_code + tblock->native_offset;
7862 ei->data.catch_class = ec->data.catch_class;
7865 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
7867 ei->try_start = cfg->native_code + tblock->native_offset;
7868 g_assert (tblock->native_offset);
7869 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
7871 ei->try_end = cfg->native_code + tblock->native_offset;
7872 g_assert (tblock->native_offset);
7873 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
7875 ei->handler_start = cfg->native_code + tblock->native_offset;
7879 cfg->jit_info = jinfo;
7881 mono_jit_info_table_add (cfg->domain, jinfo);
7883 if (cfg->method->dynamic)
7884 mono_dynamic_code_hash_lookup (cfg->domain, cfg->method)->ji = jinfo;
7886 /* collect statistics */
7887 mono_jit_stats.allocated_code_size += cfg->code_len;
7888 code_size_ratio = cfg->code_len;
7889 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
7890 mono_jit_stats.biggest_method_size = code_size_ratio;
7891 mono_jit_stats.biggest_method = method;
7893 code_size_ratio = (code_size_ratio * 100) / mono_method_get_header (method)->code_size;
7894 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
7895 mono_jit_stats.max_code_size_ratio = code_size_ratio;
7896 mono_jit_stats.max_ratio_method = method;
7898 mono_jit_stats.native_code_size += cfg->code_len;
7900 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7901 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
7907 mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain)
7910 GHashTable *jit_code_hash;
7911 gpointer code = NULL;
7917 jit_code_hash = target_domain->jit_code_hash;
7919 #ifdef MONO_USE_AOT_COMPILER
7920 if (!mono_compile_aot && (opt & MONO_OPT_AOT)) {
7922 MonoDomain *domain = mono_domain_get ();
7924 mono_domain_lock (domain);
7926 mono_class_init (method->klass);
7927 if ((info = mono_aot_get_method (domain, method))) {
7928 g_hash_table_insert (domain->jit_code_hash, method, info);
7929 mono_domain_unlock (domain);
7930 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
7931 return info->code_start;
7934 mono_domain_unlock (domain);
7938 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
7939 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
7941 MonoMethodPInvoke* piinfo = (MonoMethodPInvoke *) method;
7943 if (!piinfo->addr) {
7944 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
7945 piinfo->addr = mono_lookup_internal_call (method);
7947 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
7948 mono_lookup_pinvoke_call (method, NULL, NULL);
7950 nm = mono_marshal_get_native_wrapper (method);
7951 return mono_compile_method (nm);
7953 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
7954 //mono_debug_add_wrapper (method, nm);
7955 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
7956 const char *name = method->name;
7959 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
7960 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
7961 /* FIXME: uhm, we need a wrapper to handle exceptions? */
7962 return (gpointer)mono_delegate_ctor;
7963 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
7964 nm = mono_marshal_get_delegate_invoke (method);
7965 return mono_jit_compile_method (nm);
7966 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
7967 nm = mono_marshal_get_delegate_begin_invoke (method);
7968 return mono_jit_compile_method (nm);
7969 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
7970 nm = mono_marshal_get_delegate_end_invoke (method);
7971 return mono_jit_compile_method (nm);
7977 cfg = mini_method_compile (method, opt, target_domain, TRUE, 0);
7979 mono_domain_lock (target_domain);
7981 /* Check if some other thread already did the job. In this case, we can
7982 discard the code this thread generated. */
7984 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
7985 /* We can't use a domain specific method in another domain */
7986 if ((target_domain == mono_domain_get ()) || info->domain_neutral) {
7987 code = info->code_start;
7988 // printf("Discarding code for method %s\n", method->name);
7993 g_hash_table_insert (jit_code_hash, method, cfg->jit_info);
7994 code = cfg->native_code;
7997 mono_destroy_compile (cfg);
7999 if (target_domain->jump_target_hash) {
8000 MonoJumpInfo patch_info;
8002 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
8004 patch_info.next = NULL;
8005 patch_info.ip.i = 0;
8006 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
8007 patch_info.data.method = method;
8008 g_hash_table_remove (target_domain->jump_target_hash, method);
8010 for (tmp = list; tmp; tmp = tmp->next)
8011 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info, TRUE);
8012 g_slist_free (list);
8015 mono_domain_unlock (target_domain);
8017 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
8022 mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt)
8024 /* FIXME: later copy the code from mono */
8025 MonoDomain *target_domain, *domain = mono_domain_get ();
8029 if (opt & MONO_OPT_SHARED)
8030 target_domain = mono_get_root_domain ();
8032 target_domain = domain;
8034 mono_domain_lock (target_domain);
8036 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
8037 /* We can't use a domain specific method in another domain */
8038 if (! ((domain != target_domain) && !info->domain_neutral)) {
8039 mono_domain_unlock (target_domain);
8040 mono_jit_stats.methods_lookups++;
8041 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
8042 return info->code_start;
8046 mono_domain_unlock (target_domain);
8047 p = mono_jit_compile_method_inner (method, target_domain);
8052 mono_jit_compile_method (MonoMethod *method)
8054 return mono_jit_compile_method_with_opt (method, default_opt);
8058 invalidated_delegate_trampoline (MonoClass *klass)
8060 g_error ("Unmanaged code called delegate of type %s which was already garbage collected.\n"
8061 "See http://www.go-mono.com/delegate.html for an explanation and ways to fix this.",
8062 mono_type_full_name (&klass->byval_arg));
8066 * mono_jit_free_method:
8068 * Free all memory allocated by the JIT for METHOD.
8071 mono_jit_free_method (MonoDomain *domain, MonoMethod *method)
8073 MonoJitDynamicMethodInfo *ji;
8075 g_assert (method->dynamic);
8077 mono_domain_lock (domain);
8078 ji = mono_dynamic_code_hash_lookup (domain, method);
8079 mono_domain_unlock (domain);
8080 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
8081 /* FIXME: only enable this with a env var */
8082 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
8084 * Instead of freeing the code, change it to call an error routine
8085 * so people can fix their code.
8088 char *type = mono_type_full_name (&method->klass->byval_arg);
8089 char *type_and_method = g_strdup_printf ("%s.%s", type, method->name);
8092 mono_arch_invalidate_method (ji->ji, invalidated_delegate_trampoline, type_and_method);
8100 mono_domain_lock (domain);
8101 g_hash_table_remove (domain->dynamic_code_hash, ji);
8102 mono_domain_unlock (domain);
8104 mono_code_manager_destroy (ji->code_mp);
8105 mono_jit_info_table_remove (domain, ji->ji);
8111 mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method)
8113 MonoDomain *target_domain;
8116 if (default_opt & MONO_OPT_SHARED)
8117 target_domain = mono_get_root_domain ();
8119 target_domain = domain;
8121 mono_domain_lock (target_domain);
8123 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
8124 /* We can't use a domain specific method in another domain */
8125 if (! ((domain != target_domain) && !info->domain_neutral)) {
8126 mono_domain_unlock (target_domain);
8127 mono_jit_stats.methods_lookups++;
8128 return info->code_start;
8132 mono_domain_unlock (target_domain);
8138 * mono_jit_runtime_invoke:
8139 * @method: the method to invoke
8140 * @obj: this pointer
8141 * @params: array of parameter values.
8142 * @exc: used to catch exceptions objects
8145 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
8148 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc, void* compiled_method);
8149 void* compiled_method;
8151 if (obj == NULL && !(method->flags & METHOD_ATTRIBUTE_STATIC) && !method->string_ctor && (method->wrapper_type == 0)) {
8152 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
8156 invoke = mono_marshal_get_runtime_invoke (method);
8157 runtime_invoke = mono_jit_compile_method (invoke);
8159 /* We need this here becuase mono_marshal_get_runtime_invoke can be place
8160 * the helper method in System.Object and not the target class
8162 mono_runtime_class_init (mono_class_vtable (mono_domain_get (), method->klass));
8164 compiled_method = mono_jit_compile_method (method);
8165 return runtime_invoke (obj, params, exc, compiled_method);
8168 #ifdef PLATFORM_WIN32
8169 #define GET_CONTEXT \
8170 struct sigcontext *ctx = (struct sigcontext*)_dummy;
8173 #define GET_CONTEXT \
8174 void *ctx = context;
8175 #elif defined(sun) // Solaris x86
8176 #define GET_CONTEXT \
8177 ucontext_t *uctx = context; \
8178 struct sigcontext *ctx = (struct sigcontext *)&(uctx->uc_mcontext);
8179 #elif defined(__ppc__) || defined (__powerpc__) || defined (__s390__) || defined (MONO_ARCH_USE_SIGACTION)
8180 #define GET_CONTEXT \
8181 void *ctx = context;
8183 #define GET_CONTEXT \
8184 void **_p = (void **)&_dummy; \
8185 struct sigcontext *ctx = (struct sigcontext *)++_p;
8189 #ifdef MONO_ARCH_USE_SIGACTION
8190 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, siginfo_t *info, void *context)
8192 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy)
8196 SIG_HANDLER_SIGNATURE (sigfpe_signal_handler)
8198 MonoException *exc = NULL;
8199 #ifndef MONO_ARCH_USE_SIGACTION
8204 #if defined(MONO_ARCH_HAVE_IS_INT_OVERFLOW)
8205 if (mono_arch_is_int_overflow (ctx, info))
8206 exc = mono_get_exception_arithmetic ();
8208 exc = mono_get_exception_divide_by_zero ();
8210 exc = mono_get_exception_divide_by_zero ();
8213 mono_arch_handle_exception (ctx, exc, FALSE);
8217 SIG_HANDLER_SIGNATURE (sigill_signal_handler)
8221 exc = mono_get_exception_execution_engine ("SIGILL");
8223 mono_arch_handle_exception (ctx, exc, FALSE);
8226 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8229 sigsegv_signal_handler (int _dummy, siginfo_t *info, void *context)
8232 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
8233 struct sigcontext *ctx = (struct sigcontext *)&(((ucontext_t*)context)->uc_mcontext);
8235 /* Can't allocate memory using Boehm GC on altstack */
8236 if (jit_tls->stack_size &&
8237 ((guint8*)info->si_addr >= (guint8*)jit_tls->end_of_stack - jit_tls->stack_size) &&
8238 ((guint8*)info->si_addr < (guint8*)jit_tls->end_of_stack))
8239 exc = mono_domain_get ()->stack_overflow_ex;
8241 exc = mono_domain_get ()->null_reference_ex;
8243 mono_arch_handle_exception (ctx, exc, FALSE);
8249 SIG_HANDLER_SIGNATURE (sigsegv_signal_handler)
8253 mono_arch_handle_exception (ctx, NULL, FALSE);
8259 SIG_HANDLER_SIGNATURE (sigusr1_signal_handler)
8261 gboolean running_managed;
8266 running_managed = (mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx)) != NULL);
8268 exc = mono_thread_request_interruption (running_managed);
8271 mono_arch_handle_exception (ctx, exc, FALSE);
8275 SIG_HANDLER_SIGNATURE (sigquit_signal_handler)
8280 exc = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
8282 mono_arch_handle_exception (ctx, exc, FALSE);
8286 SIG_HANDLER_SIGNATURE (sigint_signal_handler)
8291 exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
8293 mono_arch_handle_exception (ctx, exc, FALSE);
8296 #ifndef PLATFORM_WIN32
8298 add_signal_handler (int signo, gpointer handler)
8300 struct sigaction sa;
8302 #ifdef MONO_ARCH_USE_SIGACTION
8303 sa.sa_sigaction = handler;
8304 sigemptyset (&sa.sa_mask);
8305 sa.sa_flags = SA_SIGINFO;
8307 sa.sa_handler = handler;
8308 sigemptyset (&sa.sa_mask);
8311 g_assert (sigaction (signo, &sa, NULL) != -1);
8316 mono_runtime_install_handlers (void)
8318 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8319 struct sigaction sa;
8322 #ifdef PLATFORM_WIN32
8324 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
8325 win32_seh_set_handler(SIGILL, sigill_signal_handler);
8326 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
8327 if (getenv ("MONO_DEBUG"))
8328 win32_seh_set_handler(SIGINT, sigint_signal_handler);
8329 #else /* !PLATFORM_WIN32 */
8331 /* libpthreads has its own implementation of sigaction(),
8332 * but it seems to work well with our current exception
8333 * handlers. If not we must call syscall directly instead
8336 if (getenv ("MONO_DEBUG")) {
8337 add_signal_handler (SIGINT, sigint_signal_handler);
8340 add_signal_handler (SIGFPE, sigfpe_signal_handler);
8341 add_signal_handler (SIGQUIT, sigquit_signal_handler);
8342 add_signal_handler (SIGILL, sigill_signal_handler);
8343 add_signal_handler (SIGBUS, sigsegv_signal_handler);
8344 add_signal_handler (mono_thread_get_abort_signal (), sigusr1_signal_handler);
8347 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8348 sa.sa_sigaction = sigsegv_signal_handler;
8349 sigemptyset (&sa.sa_mask);
8350 sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
8351 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
8353 add_signal_handler (SIGSEGV, sigsegv_signal_handler);
8356 #endif /* PLATFORM_WIN32 */
8359 /* mono_jit_create_remoting_trampoline:
8360 * @method: pointer to the method info
8362 * Creates a trampoline which calls the remoting functions. This
8363 * is used in the vtable of transparent proxies.
8365 * Returns: a pointer to the newly created code
8368 mono_jit_create_remoting_trampoline (MonoMethod *method, MonoRemotingTarget target)
8371 guint8 *addr = NULL;
8373 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
8374 (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
8375 nm = mono_marshal_get_remoting_invoke_for_target (method, target);
8376 addr = mono_compile_method (nm);
8378 addr = mono_compile_method (method);
8384 mini_init (const char *filename)
8388 mono_arch_cpu_init ();
8390 if (!g_thread_supported ())
8391 g_thread_init (NULL);
8393 MONO_GC_PRE_INIT ();
8395 mono_jit_tls_id = TlsAlloc ();
8396 setup_jit_tls_data ((gpointer)-1, mono_thread_abort);
8398 InitializeCriticalSection (&jit_mutex);
8402 if (default_opt & MONO_OPT_AOT)
8405 mono_runtime_install_handlers ();
8406 mono_threads_install_cleanup (mini_thread_cleanup);
8408 #define JIT_TRAMPOLINES_WORK
8409 #ifdef JIT_TRAMPOLINES_WORK
8410 mono_install_compile_method (mono_jit_compile_method);
8411 mono_install_free_method (mono_jit_free_method);
8412 mono_install_trampoline (mono_create_jit_trampoline);
8413 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
8415 #define JIT_INVOKE_WORKS
8416 #ifdef JIT_INVOKE_WORKS
8417 mono_install_runtime_invoke (mono_jit_runtime_invoke);
8418 mono_install_handler (mono_arch_get_throw_exception ());
8420 mono_install_stack_walk (mono_jit_walk_stack);
8422 domain = mono_init_from_assembly (filename, filename);
8425 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
8426 ves_icall_get_frame_info);
8427 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
8428 ves_icall_get_trace);
8429 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
8430 mono_runtime_install_handlers);
8433 create_helper_signature ();
8435 #define JIT_CALLS_WORK
8436 #ifdef JIT_CALLS_WORK
8437 /* Needs to be called here since register_jit_icall depends on it */
8438 mono_marshal_init ();
8440 mono_arch_register_lowlevel_calls ();
8441 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
8442 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
8443 mono_register_jit_icall (mono_trace_enter_method, "mono_trace_enter_method", NULL, TRUE);
8444 mono_register_jit_icall (mono_trace_leave_method, "mono_trace_leave_method", NULL, TRUE);
8445 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
8446 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
8448 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
8449 mono_register_jit_icall (mono_arch_get_rethrow_exception (), "mono_arch_rethrow_exception", helper_sig_void_obj, TRUE);
8450 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
8451 helper_sig_void_ptr, TRUE);
8452 mono_register_jit_icall (mono_thread_get_pending_exception, "mono_thread_get_pending_exception", helper_sig_obj_void, FALSE);
8453 mono_register_jit_icall (mono_thread_interruption_checkpoint, "mono_thread_interruption_checkpoint", helper_sig_void_void, FALSE);
8454 mono_register_jit_icall (mono_load_remote_field_new, "mono_load_remote_field_new", helper_sig_obj_obj_ptr_ptr, FALSE);
8455 mono_register_jit_icall (mono_store_remote_field_new, "mono_store_remote_field_new", helper_sig_void_obj_ptr_ptr_obj, FALSE);
8458 * NOTE, NOTE, NOTE, NOTE:
8459 * when adding emulation for some opcodes, remember to also add a dummy
8460 * rule to the burg files, because we need the arity information to be correct.
8462 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult, TRUE);
8463 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un, FALSE);
8464 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf, FALSE);
8465 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv, FALSE);
8466 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un, FALSE);
8467 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem, FALSE);
8468 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un, FALSE);
8470 #ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
8471 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl, TRUE);
8472 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr, TRUE);
8473 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un, TRUE);
8476 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8, FALSE);
8477 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4, FALSE);
8478 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8, FALSE);
8479 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8, FALSE);
8481 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
8482 mono_register_opcode_emulation (OP_FCONV_TO_I8, "__emul_fconv_to_i8", helper_sig_long_double, mono_fconv_i8, FALSE);
8484 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
8485 mono_register_opcode_emulation (CEE_CONV_R_UN, "__emul_conv_r_un", helper_sig_double_int, mono_conv_to_r8_un, FALSE);
8487 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
8488 mono_register_opcode_emulation (OP_LCONV_TO_R8, "__emul_lconv_to_r8", helper_sig_double_long, mono_lconv_to_r8, FALSE);
8490 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
8491 mono_register_opcode_emulation (OP_LCONV_TO_R4, "__emul_lconv_to_r4", helper_sig_float_long, mono_lconv_to_r4, FALSE);
8493 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
8494 mono_register_opcode_emulation (OP_LCONV_TO_R_UN, "__emul_lconv_to_r8_un", helper_sig_double_long, mono_lconv_to_r8_un, FALSE);
8496 #ifdef MONO_ARCH_EMULATE_FREM
8497 mono_register_opcode_emulation (OP_FREM, "__emul_frem", helper_sig_double_double_double, fmod, FALSE);
8500 #if SIZEOF_VOID_P == 4
8501 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4, TRUE);
8504 #warning "fixme: add opcode emulation"
8508 /* other jit icalls */
8509 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
8510 helper_sig_ptr_ptr_ptr, FALSE);
8511 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
8512 mono_register_jit_icall (mono_get_special_static_data, "mono_get_special_static_data", helper_sig_ptr_int, FALSE);
8513 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
8514 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
8515 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
8516 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
8517 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
8518 mono_register_jit_icall (helper_stelem_ref_check, "helper_stelem_ref_check", helper_sig_stelem_ref_check, FALSE);
8519 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
8520 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
8521 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
8522 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
8523 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
8524 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
8525 mono_register_jit_icall (mono_ldftn_nosync, "mono_ldftn_nosync", helper_sig_compile, FALSE);
8526 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
8529 #define JIT_RUNTIME_WORKS
8530 #ifdef JIT_RUNTIME_WORKS
8531 mono_runtime_install_cleanup ((MonoDomainFunc)mini_cleanup);
8532 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
8535 mono_thread_attach (domain);
8539 MonoJitStats mono_jit_stats = {0};
8542 print_jit_stats (void)
8544 if (mono_jit_stats.enabled) {
8545 g_print ("Mono Jit statistics\n");
8546 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
8547 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
8548 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
8549 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
8550 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
8551 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
8552 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
8553 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
8554 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
8555 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
8556 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
8557 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
8558 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
8559 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
8560 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
8561 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
8562 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
8563 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
8565 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
8566 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
8567 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
8568 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
8569 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
8571 g_print ("\nGeneric instances: %ld\n", mono_stats.generic_instance_count);
8572 g_print ("Inflated methods: %ld\n", mono_stats.inflated_method_count);
8573 g_print ("Inflated types: %ld\n", mono_stats.inflated_type_count);
8574 g_print ("Generics metadata size: %ld\n", mono_stats.generics_metadata_size);
8579 mini_cleanup (MonoDomain *domain)
8582 * mono_runtime_cleanup() and mono_domain_finalize () need to
8583 * be called early since they need the execution engine still
8584 * fully working (mono_domain_finalize may invoke managed finalizers
8585 * and mono_runtime_cleanup will wait for other threads to finish).
8587 mono_domain_finalize (domain, 2000);
8589 mono_runtime_cleanup (domain);
8591 mono_profiler_shutdown ();
8593 mono_debug_cleanup ();
8595 #ifdef PLATFORM_WIN32
8596 win32_seh_cleanup();
8599 mono_domain_free (domain, TRUE);
8605 mono_set_defaults (int verbose_level, guint32 opts)
8607 mini_verbose = verbose_level;
8612 mono_precompile_assembly (MonoAssembly *ass, void *user_data)
8614 MonoImage *image = mono_assembly_get_image (ass);
8615 MonoMethod *method, *invoke;
8618 if (mini_verbose > 0)
8619 printf ("PRECOMPILE: %s.\n", mono_image_get_filename (image));
8621 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
8622 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
8623 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
8627 if (mini_verbose > 1) {
8628 char * desc = mono_method_full_name (method, TRUE);
8629 g_print ("Compiling %d %s\n", count, desc);
8632 mono_compile_method (method);
8633 if (strcmp (method->name, "Finalize") == 0) {
8634 invoke = mono_marshal_get_runtime_invoke (method);
8635 mono_compile_method (invoke);
8637 if (method->klass->marshalbyref && method->signature->hasthis) {
8638 invoke = mono_marshal_get_remoting_invoke_with_check (method);
8639 mono_compile_method (invoke);
8644 void mono_precompile_assemblies ()
8646 mono_assembly_foreach ((GFunc)mono_precompile_assembly, NULL);