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 HAVE_VALGRIND_MEMCHECK_H
17 #include <valgrind/memcheck.h>
20 #include <mono/metadata/assembly.h>
21 #include <mono/metadata/loader.h>
22 #include <mono/metadata/cil-coff.h>
23 #include <mono/metadata/tabledefs.h>
24 #include <mono/metadata/class.h>
25 #include <mono/metadata/object.h>
26 #include <mono/metadata/exception.h>
27 #include <mono/metadata/opcodes.h>
28 #include <mono/metadata/mono-endian.h>
29 #include <mono/metadata/tokentype.h>
30 #include <mono/metadata/tabledefs.h>
31 #include <mono/metadata/threads.h>
32 #include <mono/metadata/marshal.h>
33 #include <mono/metadata/socket-io.h>
34 #include <mono/metadata/appdomain.h>
35 #include <mono/metadata/debug-helpers.h>
36 #include <mono/io-layer/io-layer.h>
37 #include "mono/metadata/profiler.h"
38 #include <mono/metadata/profiler-private.h>
39 #include <mono/metadata/mono-config.h>
40 #include <mono/metadata/environment.h>
41 #include <mono/metadata/mono-debug.h>
42 #include <mono/metadata/mono-debug-debugger.h>
43 #include <mono/metadata/monitor.h>
51 #include "jit-icalls.c"
53 #define MONO_IS_COND_BRANCH(op) ((op >= CEE_BEQ && op <= CEE_BLT_UN) || (op >= OP_LBEQ && op <= OP_LBLT_UN) || (op >= OP_FBEQ && op <= OP_FBLT_UN))
55 #define MONO_CHECK_THIS(ins) (cfg->method->signature->hasthis && (ins)->ssa_op == MONO_SSA_LOAD && (ins)->inst_left->inst_c0 == 0)
57 gboolean mono_arch_print_tree(MonoInst *tree, int arity);
58 static gpointer mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt);
59 static gpointer mono_jit_compile_method (MonoMethod *method);
60 static gpointer mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method);
62 static void handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src,
63 const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native);
65 static void dec_foreach (MonoInst *tree, MonoCompile *cfg);
67 static int mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
68 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
69 guint inline_offset, gboolean is_virtual_call);
71 extern guint8 mono_burg_arity [];
72 /* helper methods signature */
73 static MonoMethodSignature *helper_sig_long_long_long = NULL;
74 static MonoMethodSignature *helper_sig_long_long_int = NULL;
75 static MonoMethodSignature *helper_sig_newarr = NULL;
76 static MonoMethodSignature *helper_sig_newarr_specific = NULL;
77 static MonoMethodSignature *helper_sig_ldstr = NULL;
78 static MonoMethodSignature *helper_sig_domain_get = NULL;
79 static MonoMethodSignature *helper_sig_object_new = NULL;
80 static MonoMethodSignature *helper_sig_object_new_specific = NULL;
81 static MonoMethodSignature *helper_sig_compile = NULL;
82 static MonoMethodSignature *helper_sig_compile_virt = NULL;
83 static MonoMethodSignature *helper_sig_obj_ptr = NULL;
84 static MonoMethodSignature *helper_sig_obj_ptr_ptr = NULL;
85 static MonoMethodSignature *helper_sig_obj_void = NULL;
86 static MonoMethodSignature *helper_sig_ptr_void = NULL;
87 static MonoMethodSignature *helper_sig_void_ptr = NULL;
88 static MonoMethodSignature *helper_sig_void_obj = NULL;
89 static MonoMethodSignature *helper_sig_void_obj_ptr_int = NULL;
90 static MonoMethodSignature *helper_sig_void_ptr_ptr = NULL;
91 static MonoMethodSignature *helper_sig_void_ptr_ptr_ptr = NULL;
92 static MonoMethodSignature *helper_sig_ptr_ptr_ptr = NULL;
93 static MonoMethodSignature *helper_sig_ptr_obj = NULL;
94 static MonoMethodSignature *helper_sig_ptr_obj_int = NULL;
95 static MonoMethodSignature *helper_sig_ptr_int = NULL;
96 static MonoMethodSignature *helper_sig_initobj = NULL;
97 static MonoMethodSignature *helper_sig_memcpy = NULL;
98 static MonoMethodSignature *helper_sig_memset = NULL;
99 static MonoMethodSignature *helper_sig_ulong_double = NULL;
100 static MonoMethodSignature *helper_sig_long_double = NULL;
101 static MonoMethodSignature *helper_sig_double_long = NULL;
102 static MonoMethodSignature *helper_sig_double_int = NULL;
103 static MonoMethodSignature *helper_sig_float_long = NULL;
104 static MonoMethodSignature *helper_sig_double_double_double = NULL;
105 static MonoMethodSignature *helper_sig_uint_double = NULL;
106 static MonoMethodSignature *helper_sig_int_double = NULL;
107 static MonoMethodSignature *helper_sig_stelem_ref = NULL;
108 static MonoMethodSignature *helper_sig_stelem_ref_check = NULL;
109 static MonoMethodSignature *helper_sig_class_init_trampoline = NULL;
111 static guint32 default_opt = MONO_OPT_PEEPHOLE;
113 guint32 mono_jit_tls_id = -1;
114 MonoTraceSpec *mono_jit_trace_calls = NULL;
115 gboolean mono_break_on_exc = FALSE;
116 gboolean mono_compile_aot = FALSE;
118 static int mini_verbose = 0;
120 static CRITICAL_SECTION trampoline_hash_mutex;
122 static GHashTable *class_init_hash_addr = NULL;
124 static GHashTable *jump_trampoline_hash = NULL;
127 mono_running_on_valgrind (void)
129 #ifdef HAVE_VALGRIND_MEMCHECK_H
130 if (RUNNING_ON_VALGRIND)
139 #ifdef MONO_USE_EXC_TABLES
141 mono_type_blittable (MonoType *type)
161 case MONO_TYPE_VALUETYPE:
162 case MONO_TYPE_CLASS:
163 return type->data.klass->blittable;
173 mono_method_blittable (MonoMethod *method)
175 MonoMethodSignature *sig;
181 if (!mono_arch_has_unwind_info (method->addr)) {
185 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
188 sig = method->signature;
190 if (!mono_type_blittable (sig->ret))
193 for (i = 0; i < sig->param_count; i++)
194 if (!mono_type_blittable (sig->params [i]))
197 if (mono_method_has_marshal_info (method))
200 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && ((MonoMethodPInvoke*)method)->piflags & PINVOKE_ATTRIBUTE_SUPPORTS_LAST_ERROR)
208 G_GNUC_UNUSED static void
209 print_method_from_ip (void *ip)
214 MonoDomain *domain = mono_domain_get ();
216 ji = mono_jit_info_table_find (domain, ip);
218 g_print ("No method at %p\n", ip);
221 method = mono_method_full_name (ji->method, TRUE);
222 source = mono_debug_source_location_from_address (ji->method, (int) ip, NULL, domain);
224 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);
227 g_print ("%s\n", source);
235 * mono_method_same_domain:
237 * Determine whenever two compiled methods are in the same domain, thus
238 * the address of the callee can be embedded in the caller.
240 gboolean mono_method_same_domain (MonoJitInfo *caller, MonoJitInfo *callee)
242 if (!caller || !callee)
246 * If the call was made from domain-neutral to domain-specific
247 * code, we can't patch the call site.
249 if (caller->domain_neutral && !callee->domain_neutral)
252 if ((caller->method->klass == mono_defaults.appdomain_class) &&
253 (strstr (caller->method->name, "InvokeInDomain"))) {
254 /* The InvokeInDomain methods change the current appdomain */
262 mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token)
264 MonoJumpInfoToken *res = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoToken));
271 #define MONO_INIT_VARINFO(vi,id) do { \
272 (vi)->range.first_use.pos.bid = 0xffff; \
278 * Basic blocks have two numeric identifiers:
279 * dfn: Depth First Number
280 * block_num: unique ID assigned at bblock creation
282 #define NEW_BBLOCK(cfg) (mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)))
283 #define ADD_BBLOCK(cfg,bbhash,b) do { \
284 g_hash_table_insert (bbhash, (b)->cil_code, (b)); \
285 (b)->block_num = cfg->num_bblocks++; \
286 (b)->real_offset = real_offset; \
289 #define GET_BBLOCK(cfg,bbhash,tblock,ip) do { \
290 (tblock) = g_hash_table_lookup (bbhash, (ip)); \
292 if ((ip) >= end || (ip) < header->code) goto unverified; \
293 (tblock) = NEW_BBLOCK (cfg); \
294 (tblock)->cil_code = (ip); \
295 ADD_BBLOCK (cfg, (bbhash), (tblock)); \
299 #define CHECK_BBLOCK(target,ip,tblock) do { \
300 if ((target) < (ip) && !(tblock)->code) { \
301 bb_recheck = g_list_prepend (bb_recheck, (tblock)); \
302 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); \
306 #define NEW_ICONST(cfg,dest,val) do { \
307 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
308 (dest)->opcode = OP_ICONST; \
309 (dest)->inst_c0 = (val); \
310 (dest)->type = STACK_I4; \
313 /* FIXME: have a different definition of NEW_PCONST for 64 bit systems */
314 #define NEW_PCONST(cfg,dest,val) do { \
315 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
316 (dest)->opcode = OP_ICONST; \
317 (dest)->inst_p0 = (val); \
318 (dest)->type = STACK_PTR; \
321 #define NEW_CLASSCONST(cfg,dest,val) do { \
322 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
323 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
324 (dest)->inst_p0 = (val); \
325 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_CLASS; \
326 (dest)->type = STACK_PTR; \
329 #define NEW_IMAGECONST(cfg,dest,val) do { \
330 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
331 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
332 (dest)->inst_p0 = (val); \
333 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_IMAGE; \
334 (dest)->type = STACK_PTR; \
337 #define NEW_FIELDCONST(cfg,dest,field) do { \
338 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
339 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
340 (dest)->inst_p0 = (field); \
341 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_FIELD; \
342 (dest)->type = STACK_PTR; \
345 #define NEW_METHODCONST(cfg,dest,val) do { \
346 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
347 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
348 (dest)->inst_p0 = (val); \
349 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_METHODCONST; \
350 (dest)->type = STACK_PTR; \
353 #define NEW_VTABLECONST(cfg,dest,vtable) do { \
354 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
355 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
356 (dest)->inst_p0 = mono_compile_aot ? (gpointer)((vtable)->klass) : (vtable); \
357 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_VTABLE; \
358 (dest)->type = STACK_PTR; \
361 #define NEW_SFLDACONST(cfg,dest,field) do { \
362 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
363 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
364 (dest)->inst_p0 = (field); \
365 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_SFLDA; \
366 (dest)->type = STACK_PTR; \
369 #define NEW_LDSTRCONST(cfg,dest,image,token) do { \
370 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
371 (dest)->opcode = OP_AOTCONST; \
372 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
373 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_LDSTR; \
374 (dest)->type = STACK_OBJ; \
377 #define NEW_TYPE_FROM_HANDLE_CONST(cfg,dest,image,token) do { \
378 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
379 (dest)->opcode = OP_AOTCONST; \
380 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
381 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_TYPE_FROM_HANDLE; \
382 (dest)->type = STACK_OBJ; \
385 #define NEW_LDTOKENCONST(cfg,dest,image,token) do { \
386 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
387 (dest)->opcode = OP_AOTCONST; \
388 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
389 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_LDTOKEN; \
390 (dest)->type = STACK_PTR; \
393 #define NEW_DOMAINCONST(cfg,dest) do { \
394 if (cfg->opt & MONO_OPT_SHARED) { \
395 NEW_TEMPLOAD (cfg, dest, mono_get_domainvar (cfg)->inst_c0); \
397 NEW_PCONST (cfg, dest, (cfg)->domain); \
401 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
403 #define NEW_ARGLOAD(cfg,dest,num) do { \
404 if (arg_array [(num)]->opcode == OP_ICONST) (dest) = arg_array [(num)]; else { \
405 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
406 (dest)->ssa_op = MONO_SSA_LOAD; \
407 (dest)->inst_i0 = arg_array [(num)]; \
408 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
409 type_to_eval_stack_type (param_types [(num)], (dest)); \
410 (dest)->klass = (dest)->inst_i0->klass; \
413 #define NEW_LOCLOAD(cfg,dest,num) do { \
414 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
415 (dest)->ssa_op = MONO_SSA_LOAD; \
416 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
417 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
418 type_to_eval_stack_type (header->locals [(num)], (dest)); \
419 (dest)->klass = (dest)->inst_i0->klass; \
422 #define NEW_LOCLOADA(cfg,dest,num) do { \
423 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
424 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
425 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
426 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
427 (dest)->opcode = OP_LDADDR; \
428 (dest)->type = STACK_MP; \
429 (dest)->klass = (dest)->inst_i0->klass; \
430 if (!MONO_TYPE_ISSTRUCT (header->locals [(num)])) \
431 (cfg)->disable_ssa = TRUE; \
434 #define NEW_RETLOADA(cfg,dest) do { \
435 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
436 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
437 (dest)->inst_i0 = (cfg)->ret; \
438 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
439 (dest)->opcode = CEE_LDIND_I; \
440 (dest)->type = STACK_MP; \
441 (dest)->klass = (dest)->inst_i0->klass; \
442 (cfg)->disable_ssa = TRUE; \
445 #define NEW_ARGLOADA(cfg,dest,num) do { \
446 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
447 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
448 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
449 (dest)->inst_i0 = arg_array [(num)]; \
450 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
451 (dest)->opcode = OP_LDADDR; \
452 (dest)->type = STACK_MP; \
453 (dest)->klass = (dest)->inst_i0->klass; \
454 (cfg)->disable_ssa = TRUE; \
457 #define NEW_TEMPLOAD(cfg,dest,num) do { \
458 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
459 (dest)->ssa_op = MONO_SSA_LOAD; \
460 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
461 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
462 type_to_eval_stack_type ((dest)->inst_i0->inst_vtype, (dest)); \
463 (dest)->klass = (dest)->inst_i0->klass; \
466 #define NEW_TEMPLOADA(cfg,dest,num) do { \
467 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
468 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
469 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
470 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
471 (dest)->opcode = OP_LDADDR; \
472 (dest)->type = STACK_MP; \
473 (dest)->klass = (dest)->inst_i0->klass; \
474 if (!MONO_TYPE_ISSTRUCT (cfg->varinfo [(num)]->inst_vtype)) \
475 (cfg)->disable_ssa = TRUE; \
479 #define NEW_INDLOAD(cfg,dest,addr,vtype) do { \
480 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
481 (dest)->inst_left = addr; \
482 (dest)->opcode = mono_type_to_ldind (vtype); \
483 type_to_eval_stack_type (vtype, (dest)); \
484 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
487 #define NEW_INDSTORE(cfg,dest,addr,value,vtype) do { \
488 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
489 (dest)->inst_i0 = addr; \
490 (dest)->opcode = mono_type_to_stind (vtype); \
491 (dest)->inst_i1 = (value); \
492 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
495 #define NEW_TEMPSTORE(cfg,dest,num,inst) do { \
496 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
497 (dest)->ssa_op = MONO_SSA_STORE; \
498 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
499 (dest)->opcode = mono_type_to_stind ((dest)->inst_i0->inst_vtype); \
500 (dest)->inst_i1 = (inst); \
501 (dest)->klass = (dest)->inst_i0->klass; \
504 #define NEW_LOCSTORE(cfg,dest,num,inst) do { \
505 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
506 (dest)->opcode = mono_type_to_stind (header->locals [(num)]); \
507 (dest)->ssa_op = MONO_SSA_STORE; \
508 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
509 (dest)->inst_i1 = (inst); \
510 (dest)->klass = (dest)->inst_i0->klass; \
513 #define NEW_ARGSTORE(cfg,dest,num,inst) do { \
514 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
515 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
516 (dest)->opcode = mono_type_to_stind (param_types [(num)]); \
517 (dest)->ssa_op = MONO_SSA_STORE; \
518 (dest)->inst_i0 = arg_array [(num)]; \
519 (dest)->inst_i1 = (inst); \
520 (dest)->klass = (dest)->inst_i0->klass; \
523 #define ADD_BINOP(op) do { \
524 MONO_INST_NEW (cfg, ins, (op)); \
525 ins->cil_code = ip; \
527 ins->inst_i0 = sp [0]; \
528 ins->inst_i1 = sp [1]; \
530 type_from_op (ins); \
534 #define ADD_UNOP(op) do { \
535 MONO_INST_NEW (cfg, ins, (op)); \
536 ins->cil_code = ip; \
538 ins->inst_i0 = sp [0]; \
540 type_from_op (ins); \
544 #define ADD_BINCOND(next_block) do { \
546 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
548 cmp->inst_i0 = sp [0]; \
549 cmp->inst_i1 = sp [1]; \
550 cmp->cil_code = ins->cil_code; \
551 type_from_op (cmp); \
553 ins->inst_i0 = cmp; \
554 MONO_ADD_INS (bblock, ins); \
555 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
556 GET_BBLOCK (cfg, bbhash, tblock, target); \
557 link_bblock (cfg, bblock, tblock); \
558 ins->inst_true_bb = tblock; \
559 CHECK_BBLOCK (target, ip, tblock); \
560 if ((next_block)) { \
561 link_bblock (cfg, bblock, (next_block)); \
562 ins->inst_false_bb = (next_block); \
563 start_new_bblock = 1; \
565 GET_BBLOCK (cfg, bbhash, tblock, ip); \
566 link_bblock (cfg, bblock, tblock); \
567 ins->inst_false_bb = tblock; \
568 start_new_bblock = 2; \
572 /* FIXME: handle float, long ... */
573 #define ADD_UNCOND(istrue) do { \
575 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
577 cmp->inst_i0 = sp [0]; \
578 switch (cmp->inst_i0->type) { \
580 cmp->inst_i1 = zero_int64; break; \
582 cmp->inst_i1 = zero_r8; break; \
585 cmp->inst_i1 = zero_ptr; break; \
587 cmp->inst_i1 = zero_obj; break; \
589 cmp->inst_i1 = zero_int32; \
591 cmp->cil_code = ins->cil_code; \
592 type_from_op (cmp); \
594 ins->inst_i0 = cmp; \
595 ins->opcode = (istrue)? CEE_BNE_UN: CEE_BEQ; \
596 MONO_ADD_INS (bblock, ins); \
597 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
598 GET_BBLOCK (cfg, bbhash, tblock, target); \
599 link_bblock (cfg, bblock, tblock); \
600 ins->inst_true_bb = tblock; \
601 CHECK_BBLOCK (target, ip, tblock); \
602 GET_BBLOCK (cfg, bbhash, tblock, ip); \
603 link_bblock (cfg, bblock, tblock); \
604 ins->inst_false_bb = tblock; \
605 start_new_bblock = 2; \
608 #define NEW_LDELEMA(cfg,dest,sp,k) do { \
609 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
610 (dest)->opcode = CEE_LDELEMA; \
611 (dest)->inst_left = (sp) [0]; \
612 (dest)->inst_right = (sp) [1]; \
613 (dest)->type = STACK_MP; \
614 (dest)->klass = (k); \
615 (cfg)->flags |= MONO_CFG_HAS_LDELEMA; \
618 #define NEW_GROUP(cfg,dest,el1,el2) do { \
619 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
620 (dest)->opcode = OP_GROUP; \
621 (dest)->inst_left = (el1); \
622 (dest)->inst_right = (el2); \
627 compare_bblock (gconstpointer a, gconstpointer b)
629 const MonoBasicBlock *b1 = a;
630 const MonoBasicBlock *b2 = b;
632 return b2->cil_code - b1->cil_code;
637 * link_bblock: Links two basic blocks
639 * links two basic blocks in the control flow graph, the 'from'
640 * argument is the starting block and the 'to' argument is the block
641 * the control flow ends to after 'from'.
644 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
646 MonoBasicBlock **newa;
650 if (from->cil_code) {
652 g_print ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
654 g_print ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
657 g_print ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
659 g_print ("edge from entry to exit\n");
663 for (i = 0; i < from->out_count; ++i) {
664 if (to == from->out_bb [i]) {
670 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
671 for (i = 0; i < from->out_count; ++i) {
672 newa [i] = from->out_bb [i];
680 for (i = 0; i < to->in_count; ++i) {
681 if (from == to->in_bb [i]) {
687 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
688 for (i = 0; i < to->in_count; ++i) {
689 newa [i] = to->in_bb [i];
698 * mono_find_block_region:
700 * We mark each basic block with a region ID. We use that to avoid BB
701 * optimizations when blocks are in different regions.
704 * A region token that encodes where this region is, and information
705 * about the clause owner for this block.
707 * The region encodes the try/catch/filter clause that owns this block
708 * as well as the type. -1 is a special value that represents a block
709 * that is in none of try/catch/filter.
712 mono_find_block_region (MonoCompile *cfg, int offset, int *filter_lengths)
714 MonoMethod *method = cfg->method;
715 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
716 MonoExceptionClause *clause;
719 /* first search for handlers and filters */
720 for (i = 0; i < header->num_clauses; ++i) {
721 clause = &header->clauses [i];
722 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->token_or_filter) &&
723 (offset < (clause->token_or_filter + filter_lengths [i])))
724 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
726 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
727 if (clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)
728 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
730 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
734 /* search the try blocks */
735 for (i = 0; i < header->num_clauses; ++i) {
736 clause = &header->clauses [i];
737 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
738 return ((i + 1) << 8) | clause->flags;
745 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
747 MonoMethod *method = cfg->method;
748 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
749 MonoExceptionClause *clause;
750 MonoBasicBlock *handler;
754 for (i = 0; i < header->num_clauses; ++i) {
755 clause = &header->clauses [i];
756 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
757 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
758 if (clause->flags == type) {
759 handler = g_hash_table_lookup (cfg->bb_hash, header->code + clause->handler_offset);
761 res = g_list_append (res, handler);
769 mono_find_spvar_for_region (MonoCompile *cfg, int region)
771 return g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
775 mono_create_spvar_for_region (MonoCompile *cfg, int region)
779 var = g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
783 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
784 /* prevent it from being register allocated */
785 var->flags |= MONO_INST_INDIRECT;
787 g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
791 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
795 array [*dfn] = start;
796 /*g_print ("visit %d at %p\n", *dfn, start->cil_code);*/
797 for (i = 0; i < start->out_count; ++i) {
798 if (start->out_bb [i]->dfn)
801 start->out_bb [i]->dfn = *dfn;
802 start->out_bb [i]->df_parent = start;
803 array [*dfn] = start->out_bb [i];
804 df_visit (start->out_bb [i], dfn, array);
810 MonoBasicBlock *best;
814 previous_foreach (gconstpointer key, gpointer val, gpointer data)
816 PrevStruct *p = data;
817 MonoBasicBlock *bb = val;
818 //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,
819 //bb->method == p->best->method, bb->cil_code < p->code, bb->cil_code > p->best->cil_code);
821 if (bb->cil_code && bb->cil_code < p->code && bb->cil_code > p->best->cil_code)
825 static MonoBasicBlock*
826 find_previous (GHashTable *bb_hash, MonoBasicBlock *start, const guchar *code) {
832 g_hash_table_foreach (bb_hash, (GHFunc)previous_foreach, &p);
837 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
846 * FIXME: take into account all the details:
847 * second may have been the target of more than one bblock
849 second->out_count = first->out_count;
850 second->out_bb = first->out_bb;
852 for (i = 0; i < first->out_count; ++i) {
853 bb = first->out_bb [i];
854 for (j = 0; j < bb->in_count; ++j) {
855 if (bb->in_bb [j] == first)
856 bb->in_bb [j] = second;
860 first->out_count = 0;
861 first->out_bb = NULL;
862 link_bblock (cfg, first, second);
864 second->last_ins = first->last_ins;
866 /*g_print ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
867 for (inst = first->code; inst && inst->next; inst = inst->next) {
868 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
869 g_print ("found %p: %s", inst->next->cil_code, code);
871 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
872 second->code = inst->next;
874 first->last_ins = inst;
875 second->next_bb = first->next_bb;
876 first->next_bb = second;
881 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
887 mono_type_to_ldind (MonoType *type)
899 case MONO_TYPE_BOOLEAN:
913 case MONO_TYPE_FNPTR:
915 case MONO_TYPE_CLASS:
916 case MONO_TYPE_STRING:
917 case MONO_TYPE_OBJECT:
918 case MONO_TYPE_SZARRAY:
919 case MONO_TYPE_ARRAY:
920 return CEE_LDIND_REF;
928 case MONO_TYPE_VALUETYPE:
929 if (type->data.klass->enumtype) {
930 t = type->data.klass->enum_basetype->type;
934 case MONO_TYPE_TYPEDBYREF:
936 case MONO_TYPE_GENERICINST:
937 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE)
939 return CEE_LDIND_REF;
941 g_error ("unknown type 0x%02x in type_to_ldind", type->type);
947 mono_type_to_stind (MonoType *type)
958 case MONO_TYPE_BOOLEAN:
970 case MONO_TYPE_FNPTR:
972 case MONO_TYPE_CLASS:
973 case MONO_TYPE_STRING:
974 case MONO_TYPE_OBJECT:
975 case MONO_TYPE_SZARRAY:
976 case MONO_TYPE_ARRAY:
977 return CEE_STIND_REF;
985 case MONO_TYPE_VALUETYPE:
986 if (type->data.klass->enumtype) {
987 t = type->data.klass->enum_basetype->type;
991 case MONO_TYPE_TYPEDBYREF:
993 case MONO_TYPE_GENERICINST:
994 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE)
996 return CEE_STIND_REF;
998 g_error ("unknown type 0x%02x in type_to_stind", type->type);
1004 * Returns the type used in the eval stack when @type is loaded.
1005 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
1008 type_to_eval_stack_type (MonoType *type, MonoInst *inst) {
1012 inst->type = STACK_MP;
1020 case MONO_TYPE_BOOLEAN:
1023 case MONO_TYPE_CHAR:
1026 inst->type = STACK_I4;
1031 case MONO_TYPE_FNPTR:
1032 inst->type = STACK_PTR;
1034 case MONO_TYPE_CLASS:
1035 case MONO_TYPE_STRING:
1036 case MONO_TYPE_OBJECT:
1037 case MONO_TYPE_SZARRAY:
1038 case MONO_TYPE_ARRAY:
1039 inst->type = STACK_OBJ;
1043 inst->type = STACK_I8;
1047 inst->type = STACK_R8;
1049 case MONO_TYPE_VALUETYPE:
1050 if (type->data.klass->enumtype) {
1051 t = type->data.klass->enum_basetype->type;
1054 inst->klass = type->data.klass;
1055 inst->type = STACK_VTYPE;
1058 case MONO_TYPE_TYPEDBYREF:
1059 inst->klass = mono_defaults.typed_reference_class;
1060 inst->type = STACK_VTYPE;
1062 case MONO_TYPE_GENERICINST:
1063 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE) {
1064 inst->klass = mono_class_from_mono_type (type);
1065 inst->type = STACK_VTYPE;
1067 inst->type = STACK_OBJ;
1071 g_error ("unknown type 0x%02x in eval stack type", type->type);
1076 * The following tables are used to quickly validate the IL code in type_from_op ().
1079 bin_num_table [STACK_MAX] [STACK_MAX] = {
1080 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1081 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1082 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1083 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1084 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
1085 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
1086 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1087 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1092 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
1095 /* reduce the size of this table */
1097 bin_int_table [STACK_MAX] [STACK_MAX] = {
1098 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1099 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1100 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1101 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1102 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1103 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1104 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1105 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1109 bin_comp_table [STACK_MAX] [STACK_MAX] = {
1111 {0, 1, 0, 1, 0, 0, 4, 0},
1112 {0, 0, 1, 0, 0, 0, 0, 0},
1113 {0, 1, 0, 1, 0, 2, 4, 0},
1114 {0, 0, 0, 0, 1, 0, 0, 0},
1115 {0, 0, 0, 2, 0, 1, 0, 0},
1116 {0, 4, 0, 4, 0, 0, 3, 0},
1117 {0, 0, 0, 0, 0, 0, 0, 0},
1120 /* reduce the size of this table */
1122 shift_table [STACK_MAX] [STACK_MAX] = {
1123 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1124 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1125 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1126 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1127 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1128 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1129 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1130 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1134 * Tables to map from the non-specific opcode to the matching
1135 * type-specific opcode.
1137 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
1138 static const guint16
1139 binops_op_map [STACK_MAX] = {
1140 0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, 0
1143 /* handles from CEE_NEG to CEE_CONV_U8 */
1144 static const guint16
1145 unops_op_map [STACK_MAX] = {
1146 0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, 0
1149 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
1150 static const guint16
1151 ovfops_op_map [STACK_MAX] = {
1152 0, 0, OP_LCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_FCONV_TO_U2-CEE_CONV_U2, 0
1155 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
1156 static const guint16
1157 ovf2ops_op_map [STACK_MAX] = {
1158 0, 0, OP_LCONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN, OP_PCONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN, OP_FCONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN, 0
1161 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
1162 static const guint16
1163 ovf3ops_op_map [STACK_MAX] = {
1164 0, 0, OP_LCONV_TO_OVF_I1-CEE_CONV_OVF_I1, OP_PCONV_TO_OVF_I1-CEE_CONV_OVF_I1, OP_FCONV_TO_OVF_I1-CEE_CONV_OVF_I1, 0
1167 /* handles from CEE_CEQ to CEE_CLT_UN */
1168 static const guint16
1169 ceqops_op_map [STACK_MAX] = {
1170 0, 0, OP_LCEQ-CEE_CEQ, OP_PCEQ-CEE_CEQ, OP_FCEQ-CEE_CEQ, 0
1174 * Sets ins->type (the type on the eval stack) according to the
1175 * type of the opcode and the arguments to it.
1176 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1178 * FIXME: this function sets ins->type unconditionally in some cases, but
1179 * it should set it to invalid for some types (a conv.x on an object)
1182 type_from_op (MonoInst *ins) {
1183 switch (ins->opcode) {
1190 /* FIXME: check unverifiable args for STACK_MP */
1191 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1192 ins->opcode += binops_op_map [ins->type];
1199 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1200 ins->opcode += binops_op_map [ins->type];
1205 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1206 ins->opcode += binops_op_map [ins->type];
1209 /* FIXME: handle some specifics with ins->next->type */
1210 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1217 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1218 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1222 ins->type = neg_table [ins->inst_i0->type];
1223 ins->opcode += unops_op_map [ins->type];
1226 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1227 ins->type = ins->inst_i0->type;
1229 ins->type = STACK_INV;
1230 ins->opcode += unops_op_map [ins->type];
1236 ins->type = STACK_I4;
1237 ins->opcode += unops_op_map [ins->inst_i0->type];
1240 ins->type = STACK_R8;
1241 switch (ins->inst_i0->type) {
1246 ins->opcode = OP_LCONV_TO_R_UN;
1250 case CEE_CONV_OVF_I1:
1251 case CEE_CONV_OVF_U1:
1252 case CEE_CONV_OVF_I2:
1253 case CEE_CONV_OVF_U2:
1254 case CEE_CONV_OVF_I4:
1255 case CEE_CONV_OVF_U4:
1256 ins->type = STACK_I4;
1257 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1259 case CEE_CONV_OVF_I_UN:
1260 case CEE_CONV_OVF_U_UN:
1261 ins->type = STACK_PTR;
1262 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1264 case CEE_CONV_OVF_I1_UN:
1265 case CEE_CONV_OVF_I2_UN:
1266 case CEE_CONV_OVF_I4_UN:
1267 case CEE_CONV_OVF_U1_UN:
1268 case CEE_CONV_OVF_U2_UN:
1269 case CEE_CONV_OVF_U4_UN:
1270 ins->type = STACK_I4;
1271 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1274 ins->type = STACK_PTR;
1275 switch (ins->inst_i0->type) {
1281 ins->opcode = OP_LCONV_TO_U;
1284 ins->opcode = OP_FCONV_TO_U;
1290 ins->type = STACK_I8;
1291 ins->opcode += unops_op_map [ins->inst_i0->type];
1293 case CEE_CONV_OVF_I8:
1294 case CEE_CONV_OVF_U8:
1295 ins->type = STACK_I8;
1296 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1298 case CEE_CONV_OVF_U8_UN:
1299 case CEE_CONV_OVF_I8_UN:
1300 ins->type = STACK_I8;
1301 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1305 ins->type = STACK_R8;
1306 ins->opcode += unops_op_map [ins->inst_i0->type];
1309 ins->type = STACK_R8;
1313 ins->type = STACK_I4;
1314 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1317 case CEE_CONV_OVF_I:
1318 case CEE_CONV_OVF_U:
1319 ins->type = STACK_PTR;
1320 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1323 case CEE_ADD_OVF_UN:
1325 case CEE_MUL_OVF_UN:
1327 case CEE_SUB_OVF_UN:
1328 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1329 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1332 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1339 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_MP, STACK_R8, STACK_R8, STACK_OBJ
1342 /* map ldelem.x to the matching ldind.x opcode */
1344 ldelem_to_ldind [] = {
1358 /* map stelem.x to the matching stind.x opcode */
1360 stelem_to_stind [] = {
1374 param_table [STACK_MAX] [STACK_MAX] = {
1379 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1383 switch (args->type) {
1393 for (i = 0; i < sig->param_count; ++i) {
1394 switch (args [i].type) {
1398 if (!sig->params [i]->byref)
1402 if (sig->params [i]->byref)
1404 switch (sig->params [i]->type) {
1405 case MONO_TYPE_CLASS:
1406 case MONO_TYPE_STRING:
1407 case MONO_TYPE_OBJECT:
1408 case MONO_TYPE_SZARRAY:
1409 case MONO_TYPE_ARRAY:
1416 if (sig->params [i]->byref)
1418 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1427 /*if (!param_table [args [i].type] [sig->params [i]->type])
1435 * When we need a pointer to the current domain many times in a method, we
1436 * call mono_domain_get() once and we store the result in a local variable.
1437 * This function returns the variable that represents the MonoDomain*.
1439 inline static MonoInst *
1440 mono_get_domainvar (MonoCompile *cfg)
1442 if (!cfg->domainvar)
1443 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1444 return cfg->domainvar;
1448 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
1451 int num = cfg->num_varinfo;
1453 if ((num + 1) >= cfg->varinfo_count) {
1454 cfg->varinfo_count = (cfg->varinfo_count + 2) * 2;
1455 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
1456 cfg->vars = (MonoMethodVar **)g_realloc (cfg->vars, sizeof (MonoMethodVar*) * cfg->varinfo_count);
1459 /*g_print ("created temp %d of type 0x%x\n", num, type->type);*/
1460 mono_jit_stats.allocate_var++;
1462 MONO_INST_NEW (cfg, inst, opcode);
1463 inst->inst_c0 = num;
1464 inst->inst_vtype = type;
1465 inst->klass = mono_class_from_mono_type (type);
1466 /* if set to 1 the variable is native */
1469 cfg->varinfo [num] = inst;
1471 cfg->vars [num] = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMethodVar));
1472 MONO_INIT_VARINFO (cfg->vars [num], num);
1475 //g_print ("created temp %d of type %s\n", num, mono_type_get_name (type));
1480 type_from_stack_type (MonoInst *ins) {
1481 switch (ins->type) {
1482 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1483 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1484 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1485 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1486 case STACK_MP: return &mono_defaults.int_class->byval_arg;
1487 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1488 case STACK_VTYPE: return &ins->klass->byval_arg;
1490 g_error ("stack type %d to montype not handled\n", ins->type);
1496 array_access_to_klass (int opcode)
1500 return mono_defaults.byte_class;
1502 return mono_defaults.uint16_class;
1505 return mono_defaults.int_class;
1508 return mono_defaults.sbyte_class;
1511 return mono_defaults.int16_class;
1514 return mono_defaults.int32_class;
1516 return mono_defaults.uint32_class;
1519 return mono_defaults.int64_class;
1522 return mono_defaults.single_class;
1525 return mono_defaults.double_class;
1526 case CEE_LDELEM_REF:
1527 case CEE_STELEM_REF:
1528 return mono_defaults.object_class;
1530 g_assert_not_reached ();
1536 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
1540 MONO_ADD_INS (bb, inst);
1543 switch (bb->last_ins->opcode) {
1557 while (prev->next && prev->next != bb->last_ins)
1559 if (prev == bb->code) {
1560 if (bb->last_ins == bb->code) {
1561 inst->next = bb->code;
1564 inst->next = prev->next;
1568 inst->next = bb->last_ins;
1572 // g_warning ("handle conditional jump in add_ins_to_end ()\n");
1574 MONO_ADD_INS (bb, inst);
1580 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
1582 MonoInst *inst, *load;
1584 NEW_TEMPLOAD (cfg, load, src);
1586 NEW_TEMPSTORE (cfg, inst, dest, load);
1587 if (inst->opcode == CEE_STOBJ) {
1588 NEW_TEMPLOADA (cfg, inst, dest);
1589 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE);
1591 inst->cil_code = NULL;
1592 mono_add_ins_to_end (bb, inst);
1597 * We try to share variables when possible
1600 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1605 /* inlining can result in deeper stacks */
1606 if (slot >= ((MonoMethodNormal *)cfg->method)->header->max_stack)
1607 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1609 pos = ins->type - 1 + slot * STACK_MAX;
1611 switch (ins->type) {
1618 if ((vnum = cfg->intvars [pos]))
1619 return cfg->varinfo [vnum];
1620 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1621 cfg->intvars [pos] = res->inst_c0;
1624 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1630 * This function is called to handle items that are left on the evaluation stack
1631 * at basic block boundaries. What happens is that we save the values to local variables
1632 * and we reload them later when first entering the target basic block (with the
1633 * handle_loaded_temps () function).
1634 * It is also used to handle items on the stack in store opcodes, since it is
1635 * possible that the variable to be stored into is already on the stack, in
1636 * which case its old value should be used.
1637 * A single joint point will use the same variables (stored in the array bb->out_stack or
1638 * bb->in_stack, if the basic block is before or after the joint point).
1641 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count) {
1643 MonoBasicBlock *outb;
1644 MonoInst *inst, **locals;
1648 if (cfg->verbose_level > 3)
1649 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
1650 if (!bb->out_scount) {
1652 bb->out_scount = count;
1653 //g_print ("bblock %d has out:", bb->block_num);
1654 for (i = 0; i < bb->out_count; ++i) {
1655 outb = bb->out_bb [i];
1656 //g_print (" %d", outb->block_num);
1657 if (outb->in_stack) {
1659 bb->out_stack = outb->in_stack;
1665 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1666 for (i = 0; i < count; ++i) {
1668 /* try to reuse temps already allocated for this purpouse, if they occupy the same
1669 * stack slot and if they are of the same type. */
1670 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1672 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1677 locals = bb->out_stack;
1678 for (i = 0; i < count; ++i) {
1679 /* add store ops at the end of the bb, before the branch */
1680 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1681 if (inst->opcode == CEE_STOBJ) {
1682 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
1683 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE);
1685 inst->cil_code = sp [i]->cil_code;
1686 mono_add_ins_to_end (bb, inst);
1688 if (cfg->verbose_level > 3)
1689 g_print ("storing %d to temp %d\n", i, locals [i]->inst_c0);
1692 for (i = 0; i < bb->out_count; ++i) {
1693 outb = bb->out_bb [i];
1694 if (outb->in_scount)
1695 continue; /* check they are the same locals */
1696 outb->in_scount = count;
1697 outb->in_stack = locals;
1703 ret_type_to_call_opcode (MonoType *type, int calli, int virt)
1708 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1712 case MONO_TYPE_VOID:
1713 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
1716 case MONO_TYPE_BOOLEAN:
1719 case MONO_TYPE_CHAR:
1722 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1726 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1727 case MONO_TYPE_CLASS:
1728 case MONO_TYPE_STRING:
1729 case MONO_TYPE_OBJECT:
1730 case MONO_TYPE_SZARRAY:
1731 case MONO_TYPE_ARRAY:
1732 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1735 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
1738 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
1739 case MONO_TYPE_VALUETYPE:
1740 if (type->data.klass->enumtype) {
1741 t = type->data.klass->enum_basetype->type;
1744 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1745 case MONO_TYPE_TYPEDBYREF:
1746 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1747 case MONO_TYPE_GENERICINST:
1748 t = type->data.generic_inst->generic_type->type;
1751 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
1757 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
1759 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
1761 ji->ip.label = label;
1762 ji->type = MONO_PATCH_INFO_SWITCH;
1763 ji->data.table = bbs;
1764 ji->next = cfg->patch_info;
1765 ji->table_size = num_blocks;
1766 cfg->patch_info = ji;
1770 * When we add a tree of instructions, we need to ensure the instructions currently
1771 * on the stack are executed before (like, if we load a value from a local).
1772 * We ensure this by saving the currently loaded values to temps and rewriting the
1773 * instructions to load the values.
1774 * This is not done for opcodes that terminate a basic block (because it's handled already
1775 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
1778 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
1780 MonoInst *load, *store, *temp, *ins;
1782 while (stack < sp) {
1784 /* handle also other constants */
1785 if (ins->opcode != OP_ICONST) {
1786 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1787 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1788 store->cil_code = ins->cil_code;
1789 if (store->opcode == CEE_STOBJ) {
1790 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
1791 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE);
1793 MONO_ADD_INS (bblock, store);
1794 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1795 load->cil_code = ins->cil_code;
1803 * Prepare arguments for passing to a function call.
1804 * Return a non-zero value if the arguments can't be passed to the given
1806 * The type checks are not yet complete and some conversions may need
1807 * casts on 32 or 64 bit architectures.
1810 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
1815 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
1819 for (i = 0; i < sig->param_count; ++i) {
1820 if (sig->params [i]->byref) {
1822 * check the result of ldelema is only passed as an argument if the byref
1823 * type matches exactly the array element type.
1824 * FIXME: if the argument as been saved on the stack as part of the
1825 * interface variable code (the value was on the stack at a basic block boundary)
1826 * we need to add the check in that case, too.
1828 if (args [i]->opcode == CEE_LDELEMA) {
1830 MonoClass *exact_class = mono_class_from_mono_type (sig->params [i]);
1831 if (!exact_class->valuetype) {
1832 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
1833 check->cil_code = args [i]->cil_code;
1834 check->klass = exact_class;
1835 check->inst_left = args [i]->inst_left;
1836 check->type = STACK_OBJ;
1837 args [i]->inst_left = check;
1840 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
1844 simple_type = sig->params [i]->type;
1846 switch (simple_type) {
1847 case MONO_TYPE_VOID:
1852 case MONO_TYPE_BOOLEAN:
1855 case MONO_TYPE_CHAR:
1858 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
1864 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
1867 case MONO_TYPE_CLASS:
1868 case MONO_TYPE_STRING:
1869 case MONO_TYPE_OBJECT:
1870 case MONO_TYPE_SZARRAY:
1871 case MONO_TYPE_ARRAY:
1872 if (args [i]->type != STACK_OBJ)
1877 if (args [i]->type != STACK_I8)
1882 if (args [i]->type != STACK_R8)
1885 case MONO_TYPE_VALUETYPE:
1886 if (sig->params [i]->data.klass->enumtype) {
1887 simple_type = sig->params [i]->data.klass->enum_basetype->type;
1890 if (args [i]->type != STACK_VTYPE)
1893 case MONO_TYPE_TYPEDBYREF:
1894 if (args [i]->type != STACK_VTYPE)
1897 case MONO_TYPE_GENERICINST:
1898 simple_type = sig->params [i]->data.generic_inst->generic_type->type;
1902 g_error ("unknown type 0x%02x in check_call_signature", simple_type);
1909 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
1910 const guint8 *ip, gboolean to_end)
1912 MonoInst *temp, *store, *ins = (MonoInst*)call;
1913 MonoType *ret = sig->ret;
1915 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
1917 call->inst.type = STACK_OBJ;
1918 call->inst.opcode = CEE_CALL;
1919 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
1921 type_to_eval_stack_type (ret, ins);
1922 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
1925 if (MONO_TYPE_ISSTRUCT (ret)) {
1928 /* we use this to allocate native sized structs */
1929 temp->unused = sig->pinvoke;
1931 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
1932 if (call->inst.opcode == OP_VCALL)
1933 ins->inst_left = loada;
1935 ins->inst_right = loada; /* a virtual or indirect call */
1938 mono_add_ins_to_end (bblock, ins);
1940 MONO_ADD_INS (bblock, ins);
1942 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1943 store->cil_code = ip;
1945 mono_add_ins_to_end (bblock, store);
1947 MONO_ADD_INS (bblock, store);
1949 return temp->inst_c0;
1952 mono_add_ins_to_end (bblock, ins);
1954 MONO_ADD_INS (bblock, ins);
1959 inline static MonoCallInst *
1960 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1961 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
1966 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual));
1968 call->inst.cil_code = ip;
1970 call->signature = sig;
1971 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
1973 for (arg = call->out_args; arg;) {
1974 MonoInst *narg = arg->next;
1979 mono_add_ins_to_end (bblock, arg);
1981 MONO_ADD_INS (bblock, arg);
1988 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1989 MonoInst **args, MonoInst *addr, const guint8 *ip)
1991 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
1993 call->inst.inst_i0 = addr;
1995 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
1998 static MonoCallInst*
1999 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
2000 MonoInst **args, const guint8 *ip, MonoInst *this)
2002 gboolean virtual = this != NULL;
2005 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, FALSE);
2007 if (this && sig->hasthis &&
2008 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
2009 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
2010 call->method = mono_marshal_get_remoting_invoke_with_check (method);
2012 call->method = method;
2014 call->inst.flags |= MONO_INST_HAS_METHOD;
2015 call->inst.inst_left = this;
2021 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
2022 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
2024 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, signature, args, ip, this);
2026 return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
2030 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
2031 MonoInst **args, const guint8 *ip, gboolean to_end)
2037 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
2039 return mono_spill_call (cfg, bblock, call, sig, func == mono_array_new_va, ip, to_end);
2043 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
2045 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
2048 g_warning ("unregistered JIT ICall");
2049 g_assert_not_reached ();
2052 return mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, args, ip, FALSE);
2056 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
2058 MonoInst *ins, *temp = NULL, *store, *load, *begin;
2059 MonoInst *last_arg = NULL;
2063 //g_print ("emulating: ");
2064 //mono_print_tree_nl (tree);
2065 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE));
2066 ins = (MonoInst*)call;
2068 call->inst.cil_code = tree->cil_code;
2070 call->signature = info->sig;
2072 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
2074 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2075 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
2076 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2077 store->cil_code = tree->cil_code;
2082 nargs = info->sig->param_count + info->sig->hasthis;
2084 for (last_arg = call->out_args; last_arg && last_arg->next; last_arg = last_arg->next) ;
2087 last_arg->next = store;
2090 begin = call->out_args;
2094 if (cfg->prev_ins) {
2096 * This assumes that that in a tree, emulate_opcode is called for a
2097 * node before it is called for its children. dec_foreach needs to
2098 * take this into account.
2100 store->next = cfg->prev_ins->next;
2101 cfg->prev_ins->next = begin;
2103 store->next = cfg->cbb->code;
2104 cfg->cbb->code = begin;
2107 call->fptr = mono_icall_get_wrapper (info);
2109 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2110 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2115 static MonoMethodSignature *
2116 mono_get_element_address_signature (int arity)
2118 static GHashTable *sighash = NULL;
2119 MonoMethodSignature *res;
2122 EnterCriticalSection (&trampoline_hash_mutex);
2124 sighash = g_hash_table_new (NULL, NULL);
2126 else if ((res = g_hash_table_lookup (sighash, (gpointer)arity))) {
2127 LeaveCriticalSection (&trampoline_hash_mutex);
2131 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
2134 res->params [0] = &mono_defaults.array_class->byval_arg;
2136 for (i = 1; i <= arity; i++)
2137 res->params [i] = &mono_defaults.int_class->byval_arg;
2139 res->ret = &mono_defaults.int_class->byval_arg;
2141 g_hash_table_insert (sighash, (gpointer)arity, res);
2142 LeaveCriticalSection (&trampoline_hash_mutex);
2148 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native) {
2149 MonoInst *iargs [3];
2155 * This check breaks with spilled vars... need to handle it during verification anyway.
2156 * g_assert (klass && klass == src->klass && klass == dest->klass);
2160 n = mono_class_native_size (klass, &align);
2162 n = mono_class_value_size (klass, &align);
2164 if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
2166 MONO_INST_NEW (cfg, inst, OP_MEMCPY);
2167 inst->inst_left = dest;
2168 inst->inst_right = src;
2169 inst->cil_code = ip;
2171 MONO_ADD_INS (bblock, inst);
2176 NEW_ICONST (cfg, iargs [2], n);
2178 mono_emit_native_call (cfg, bblock, helper_memcpy, helper_sig_memcpy, iargs, ip, to_end);
2182 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
2184 MonoInst *iargs [2];
2185 MonoInst *ins, *zero_int32;
2188 NEW_ICONST (cfg, zero_int32, 0);
2190 mono_class_init (klass);
2191 n = mono_class_value_size (klass, NULL);
2192 MONO_INST_NEW (cfg, ins, 0);
2194 ins->inst_left = dest;
2195 ins->inst_right = zero_int32;
2198 ins->opcode = CEE_STIND_I1;
2199 MONO_ADD_INS (bblock, ins);
2202 ins->opcode = CEE_STIND_I2;
2203 MONO_ADD_INS (bblock, ins);
2206 ins->opcode = CEE_STIND_I4;
2207 MONO_ADD_INS (bblock, ins);
2210 if (n <= sizeof (gpointer) * 5) {
2211 ins->opcode = OP_MEMSET;
2214 MONO_ADD_INS (bblock, ins);
2217 handle_loaded_temps (cfg, bblock, stack_start, sp);
2218 NEW_ICONST (cfg, ins, n);
2221 mono_emit_jit_icall (cfg, bblock, helper_initobj, iargs, ip);
2227 handle_box (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *val, const guchar *ip, MonoClass *klass)
2229 MonoInst *iargs [2];
2230 MonoInst *dest, *vtoffset, *add, *vstore;
2233 /* much like NEWOBJ */
2234 if (cfg->opt & MONO_OPT_SHARED) {
2235 NEW_DOMAINCONST (cfg, iargs [0]);
2236 NEW_CLASSCONST (cfg, iargs [1], klass);
2238 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
2240 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
2241 NEW_VTABLECONST (cfg, iargs [0], vtable);
2242 if (klass->has_finalize || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
2243 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
2245 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
2247 NEW_TEMPLOAD (cfg, dest, temp);
2248 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
2249 MONO_INST_NEW (cfg, add, CEE_ADD);
2250 add->inst_left = dest;
2251 add->inst_right = vtoffset;
2254 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
2255 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
2256 vstore->cil_code = ip;
2257 vstore->inst_left = add;
2258 vstore->inst_right = val;
2260 if (vstore->opcode == CEE_STOBJ) {
2261 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
2263 MONO_ADD_INS (bblock, vstore);
2265 NEW_TEMPLOAD (cfg, dest, temp);
2269 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
2272 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
2274 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
2275 MonoMethodSignature *signature = method->signature;
2279 #ifdef MONO_ARCH_HAVE_LMF_OPS
2280 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
2281 !MONO_TYPE_ISSTRUCT (signature->ret) && (method->klass->parent != mono_defaults.array_class))
2285 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2286 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2287 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
2288 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
2289 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2290 (method->klass->marshalbyref) ||
2291 !header || header->num_clauses ||
2292 /* fixme: why cant we inline valuetype returns? */
2293 MONO_TYPE_ISSTRUCT (signature->ret))
2296 /* its not worth to inline methods with valuetype arguments?? */
2297 for (i = 0; i < signature->param_count; i++) {
2298 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
2304 * if we can initialize the class of the method right away, we do,
2305 * otherwise we don't allow inlining if the class needs initialization,
2306 * since it would mean inserting a call to mono_runtime_class_init()
2307 * inside the inlined code
2309 if (!(cfg->opt & MONO_OPT_SHARED)) {
2310 vtable = mono_class_vtable (cfg->domain, method->klass);
2311 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
2312 if (cfg->run_cctors)
2313 mono_runtime_class_init (vtable);
2315 else if (!vtable->initialized && mono_class_needs_cctor_run (method->klass, NULL))
2319 * If we're compiling for shared code
2320 * the cctor will need to be run at aot method load time, for example,
2321 * or at the end of the compilation of the inlining method.
2323 if (mono_class_needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
2326 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
2328 /* also consider num_locals? */
2329 if (getenv ("MONO_INLINELIMIT"))
2330 if (header->code_size < atoi (getenv ("MONO_INLINELIMIT"))) {
2334 if (header->code_size < 20)
2341 mini_get_ldelema_ins (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
2345 MonoMethodSignature *esig;
2346 char icall_name [256];
2347 MonoJitICallInfo *info;
2349 rank = cmethod->signature->param_count - (is_set? 1: 0);
2351 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
2353 NEW_GROUP (cfg, indexes, sp [1], sp [2]);
2354 MONO_INST_NEW (cfg, addr, OP_LDELEMA2D);
2355 addr->inst_left = sp [0];
2356 addr->inst_right = indexes;
2357 addr->cil_code = ip;
2358 addr->type = STACK_MP;
2359 addr->klass = cmethod->klass;
2363 /* Need to register the icall so it gets an icall wrapper */
2364 sprintf (icall_name, "ves_array_element_address_%d", rank);
2366 info = mono_find_jit_icall_by_name (icall_name);
2368 esig = mono_get_element_address_signature (rank);
2369 info = mono_register_jit_icall (ves_array_element_address, g_strdup (icall_name), esig, FALSE);
2372 temp = mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, sp, ip, FALSE);
2374 NEW_TEMPLOAD (cfg, addr, temp);
2378 static MonoJitICallInfo **emul_opcode_map = NULL;
2380 static inline MonoJitICallInfo *
2381 mono_find_jit_opcode_emulation (int opcode)
2383 if (emul_opcode_map)
2384 return emul_opcode_map [opcode];
2390 mini_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
2395 if (cmethod->klass == mono_defaults.string_class) {
2396 if (cmethod->name [0] != 'g')
2399 if (strcmp (cmethod->name, "get_Chars") == 0)
2401 else if (strcmp (cmethod->name, "get_Length") == 0)
2404 } else if (cmethod->klass == mono_defaults.object_class) {
2405 if (strcmp (cmethod->name, "GetType") == 0)
2409 } else if (cmethod->klass == mono_defaults.array_class) {
2410 if (strcmp (cmethod->name, "get_Rank") == 0)
2412 else if (strcmp (cmethod->name, "get_Length") == 0)
2417 op = mono_arch_get_opcode_for_method (cfg, cmethod, fsig, args);
2421 pc = fsig->param_count + fsig->hasthis;
2422 MONO_INST_NEW (cfg, ins, op);
2425 ins->inst_i0 = args [0];
2427 ins->inst_i1 = args [1];
2434 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
2436 MonoInst *store, *temp;
2439 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
2441 if (!sig->hasthis && sig->param_count == 0)
2445 if (sp [0]->opcode == OP_ICONST) {
2448 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
2450 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2451 store->cil_code = sp [0]->cil_code;
2452 MONO_ADD_INS (bblock, store);
2457 for (i = 0; i < sig->param_count; ++i) {
2458 if (sp [0]->opcode == OP_ICONST) {
2461 temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
2463 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2464 store->cil_code = sp [0]->cil_code;
2465 if (store->opcode == CEE_STOBJ) {
2466 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2467 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE);
2469 MONO_ADD_INS (bblock, store);
2477 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
2478 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b, gboolean inline_allways)
2480 MonoInst *ins, *rvar = NULL;
2481 MonoMethodHeader *cheader;
2482 MonoBasicBlock *ebblock, *sbblock;
2483 int i, costs, new_locals_offset;
2485 if (cfg->verbose_level > 2)
2486 g_print ("INLINE START %p %s -> %s\n", cmethod, mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2488 if (!cmethod->inline_info) {
2489 mono_jit_stats.inlineable_methods++;
2490 cmethod->inline_info = 1;
2492 /* allocate space to store the return value */
2493 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2494 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2497 /* allocate local variables */
2498 cheader = ((MonoMethodNormal *)cmethod)->header;
2499 new_locals_offset = cfg->num_varinfo;
2500 for (i = 0; i < cheader->num_locals; ++i)
2501 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
2503 /* allocate starte and end blocks */
2504 sbblock = NEW_BBLOCK (cfg);
2505 sbblock->block_num = cfg->num_bblocks++;
2506 sbblock->real_offset = real_offset;
2508 ebblock = NEW_BBLOCK (cfg);
2509 ebblock->block_num = cfg->num_bblocks++;
2510 ebblock->real_offset = real_offset;
2512 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
2514 if ((costs >= 0 && costs < 60) || inline_allways) {
2515 if (cfg->verbose_level > 2)
2516 g_print ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2518 mono_jit_stats.inlined_methods++;
2520 /* always add some code to avoid block split failures */
2521 MONO_INST_NEW (cfg, ins, CEE_NOP);
2522 MONO_ADD_INS (bblock, ins);
2525 bblock->next_bb = sbblock;
2526 link_bblock (cfg, bblock, sbblock);
2529 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
2535 if (cfg->verbose_level > 2)
2536 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
2542 * Some of these comments may well be out-of-date.
2543 * Design decisions: we do a single pass over the IL code (and we do bblock
2544 * splitting/merging in the few cases when it's required: a back jump to an IL
2545 * address that was not already seen as bblock starting point).
2546 * Code is validated as we go (full verification is still better left to metadata/verify.c).
2547 * Complex operations are decomposed in simpler ones right away. We need to let the
2548 * arch-specific code peek and poke inside this process somehow (except when the
2549 * optimizations can take advantage of the full semantic info of coarse opcodes).
2550 * All the opcodes of the form opcode.s are 'normalized' to opcode.
2551 * MonoInst->opcode initially is the IL opcode or some simplification of that
2552 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
2553 * opcode with value bigger than OP_LAST.
2554 * At this point the IR can be handed over to an interpreter, a dumb code generator
2555 * or to the optimizing code generator that will translate it to SSA form.
2557 * Profiling directed optimizations.
2558 * We may compile by default with few or no optimizations and instrument the code
2559 * or the user may indicate what methods to optimize the most either in a config file
2560 * or through repeated runs where the compiler applies offline the optimizations to
2561 * each method and then decides if it was worth it.
2564 * * consider using an array instead of an hash table (bb_hash)
2567 #define CHECK_TYPE(ins) if (!(ins)->type) goto unverified
2568 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) goto unverified
2569 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) goto unverified
2570 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) goto unverified
2571 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) goto unverified
2572 #define CHECK_OPSIZE(size) if (ip + size > end) goto unverified
2575 /* offset from br.s -> br like opcodes */
2576 #define BIG_BRANCH_OFFSET 13
2579 get_basic_blocks (MonoCompile *cfg, GHashTable *bbhash, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
2581 unsigned char *ip = start;
2582 unsigned char *target;
2585 MonoBasicBlock *bblock;
2586 const MonoOpcode *opcode;
2589 cli_addr = ip - start;
2590 i = mono_opcode_value ((const guint8 **)&ip);
2591 opcode = &mono_opcodes [i];
2592 switch (opcode->argument) {
2593 case MonoInlineNone:
2596 case MonoInlineString:
2597 case MonoInlineType:
2598 case MonoInlineField:
2599 case MonoInlineMethod:
2602 case MonoShortInlineR:
2609 case MonoShortInlineVar:
2610 case MonoShortInlineI:
2613 case MonoShortInlineBrTarget:
2614 target = start + cli_addr + 2 + (signed char)ip [1];
2615 GET_BBLOCK (cfg, bbhash, bblock, target);
2618 case MonoInlineBrTarget:
2619 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
2620 GET_BBLOCK (cfg, bbhash, bblock, target);
2623 case MonoInlineSwitch: {
2624 guint32 n = read32 (ip + 1);
2627 cli_addr += 5 + 4 * n;
2628 target = start + cli_addr;
2629 GET_BBLOCK (cfg, bbhash, bblock, target);
2631 for (j = 0; j < n; ++j) {
2632 target = start + cli_addr + (gint32)read32 (ip);
2633 GET_BBLOCK (cfg, bbhash, bblock, target);
2643 g_assert_not_reached ();
2653 * mono_method_to_ir: translates IL into basic blocks containing trees
2656 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
2657 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
2658 guint inline_offset, gboolean is_virtual_call)
2660 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
2661 MonoInst *ins, **sp, **stack_start;
2662 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
2664 MonoMethod *cmethod;
2665 MonoInst **arg_array;
2666 MonoMethodHeader *header;
2668 guint32 token, ins_flag;
2670 MonoClass *constrained_call = NULL;
2671 unsigned char *ip, *end, *target, *err_pos;
2672 static double r8_0 = 0.0;
2673 MonoMethodSignature *sig;
2674 MonoGenericContext *generic_context = NULL;
2675 MonoType **param_types;
2676 GList *bb_recheck = NULL, *tmp;
2677 int i, n, start_new_bblock, align;
2678 int num_calls = 0, inline_costs = 0;
2679 int *filter_lengths = NULL;
2680 int breakpoint_id = 0;
2681 guint real_offset, num_args;
2683 image = method->klass->image;
2684 header = ((MonoMethodNormal *)method)->header;
2685 sig = method->signature;
2686 num_args = sig->hasthis + sig->param_count;
2687 ip = (unsigned char*)header->code;
2688 end = ip + header->code_size;
2689 mono_jit_stats.cil_code_size += header->code_size;
2691 if (method->signature->is_inflated)
2692 generic_context = ((MonoMethodInflated *) method)->context;
2694 if (cfg->method == method) {
2696 bbhash = cfg->bb_hash;
2698 real_offset = inline_offset;
2699 bbhash = g_hash_table_new (g_direct_hash, NULL);
2702 if (cfg->verbose_level > 2)
2703 g_print ("method to IR %s\n", mono_method_full_name (method, TRUE));
2705 dont_inline = g_list_prepend (dont_inline, method);
2706 if (cfg->method == method) {
2708 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
2709 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
2712 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
2713 start_bblock->cil_code = NULL;
2714 start_bblock->cil_length = 0;
2715 start_bblock->block_num = cfg->num_bblocks++;
2718 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
2719 end_bblock->cil_code = NULL;
2720 end_bblock->cil_length = 0;
2721 end_bblock->block_num = cfg->num_bblocks++;
2722 g_assert (cfg->num_bblocks == 2);
2724 arg_array = alloca (sizeof (MonoInst *) * num_args);
2725 for (i = num_args - 1; i >= 0; i--)
2726 arg_array [i] = cfg->varinfo [i];
2728 if (header->num_clauses) {
2729 int size = sizeof (int) * header->num_clauses;
2730 filter_lengths = alloca (size);
2731 memset (filter_lengths, 0, size);
2733 cfg->spvars = g_hash_table_new (NULL, NULL);
2735 /* handle exception clauses */
2736 for (i = 0; i < header->num_clauses; ++i) {
2737 //unsigned char *p = ip;
2738 MonoExceptionClause *clause = &header->clauses [i];
2739 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->try_offset);
2740 tblock->real_offset = clause->try_offset;
2741 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
2742 tblock->real_offset = clause->handler_offset;
2744 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
2745 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2746 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2747 MONO_ADD_INS (tblock, ins);
2750 /*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);
2752 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
2754 /* catch and filter blocks get the exception object on the stack */
2755 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
2756 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2757 /* mostly like handle_stack_args (), but just sets the input args */
2758 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
2760 cfg->exvar = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
2761 /* prevent it from being register allocated */
2762 cfg->exvar->flags |= MONO_INST_INDIRECT;
2764 tblock->in_scount = 1;
2765 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2766 tblock->in_stack [0] = cfg->exvar;
2768 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2769 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->token_or_filter);
2770 tblock->real_offset = clause->token_or_filter;
2771 tblock->in_scount = 1;
2772 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2773 tblock->in_stack [0] = cfg->exvar;
2774 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2775 MONO_ADD_INS (tblock, ins);
2781 arg_array = alloca (sizeof (MonoInst *) * num_args);
2782 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
2785 /* FIRST CODE BLOCK */
2786 bblock = NEW_BBLOCK (cfg);
2787 bblock->cil_code = ip;
2789 ADD_BBLOCK (cfg, bbhash, bblock);
2791 if (cfg->method == method) {
2792 breakpoint_id = mono_debugger_method_has_breakpoint (method);
2793 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
2794 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2795 MONO_ADD_INS (bblock, ins);
2799 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED))) || mono_compile_aot) {
2800 /* we use a separate basic block for the initialization code */
2801 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
2802 init_localsbb->real_offset = real_offset;
2803 start_bblock->next_bb = init_localsbb;
2804 init_localsbb->next_bb = bblock;
2805 link_bblock (cfg, start_bblock, init_localsbb);
2806 link_bblock (cfg, init_localsbb, bblock);
2807 init_localsbb->block_num = cfg->num_bblocks++;
2809 start_bblock->next_bb = bblock;
2810 link_bblock (cfg, start_bblock, bblock);
2813 if (get_basic_blocks (cfg, bbhash, header, real_offset, ip, end, &err_pos)) {
2818 mono_debug_init_method (cfg, bblock, breakpoint_id);
2820 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
2822 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
2823 for (n = 0; n < sig->param_count; ++n)
2824 param_types [n + sig->hasthis] = sig->params [n];
2826 /* do this somewhere outside - not here */
2827 NEW_ICONST (cfg, zero_int32, 0);
2828 NEW_ICONST (cfg, zero_int64, 0);
2829 zero_int64->type = STACK_I8;
2830 NEW_PCONST (cfg, zero_ptr, 0);
2831 NEW_PCONST (cfg, zero_obj, 0);
2832 zero_obj->type = STACK_OBJ;
2834 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
2835 zero_r8->type = STACK_R8;
2836 zero_r8->inst_p0 = &r8_0;
2838 /* add a check for this != NULL to inlined methods */
2839 if (is_virtual_call) {
2840 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
2841 NEW_ARGLOAD (cfg, ins->inst_left, 0);
2843 MONO_ADD_INS (bblock, ins);
2846 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
2847 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
2850 start_new_bblock = 0;
2853 if (cfg->method == method)
2854 real_offset = ip - header->code;
2856 real_offset = inline_offset;
2858 if (start_new_bblock) {
2859 bblock->cil_length = ip - bblock->cil_code;
2860 if (start_new_bblock == 2) {
2861 g_assert (ip == tblock->cil_code);
2863 GET_BBLOCK (cfg, bbhash, tblock, ip);
2865 bblock->next_bb = tblock;
2867 start_new_bblock = 0;
2868 for (i = 0; i < bblock->in_scount; ++i) {
2869 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2873 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
2874 link_bblock (cfg, bblock, tblock);
2875 if (sp != stack_start) {
2876 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2879 bblock->next_bb = tblock;
2881 for (i = 0; i < bblock->in_scount; ++i) {
2882 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2888 bblock->real_offset = real_offset;
2890 if ((cfg->method == method) && cfg->coverage_info) {
2891 MonoInst *store, *one;
2892 guint32 cil_offset = ip - header->code;
2893 cfg->coverage_info->data [cil_offset].cil_code = ip;
2895 /* TODO: Use an increment here */
2896 NEW_ICONST (cfg, one, 1);
2899 NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
2902 MONO_INST_NEW (cfg, store, CEE_STIND_I);
2903 store->cil_code = ip;
2904 store->inst_left = ins;
2905 store->inst_right = one;
2907 MONO_ADD_INS (bblock, store);
2910 if (cfg->verbose_level > 3)
2911 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, sp-stack_start, mono_disasm_code_one (NULL, method, ip, NULL));
2918 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2919 ins->cil_code = ip++;
2920 MONO_ADD_INS (bblock, ins);
2926 CHECK_STACK_OVF (1);
2927 n = (*ip)-CEE_LDARG_0;
2929 NEW_ARGLOAD (cfg, ins, n);
2930 ins->cil_code = ip++;
2937 CHECK_STACK_OVF (1);
2938 n = (*ip)-CEE_LDLOC_0;
2940 NEW_LOCLOAD (cfg, ins, n);
2941 ins->cil_code = ip++;
2949 n = (*ip)-CEE_STLOC_0;
2952 handle_loaded_temps (cfg, bblock, stack_start, sp);
2953 NEW_LOCSTORE (cfg, ins, n, *sp);
2955 if (ins->opcode == CEE_STOBJ) {
2956 NEW_LOCLOADA (cfg, ins, n);
2957 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2959 MONO_ADD_INS (bblock, ins);
2965 CHECK_STACK_OVF (1);
2967 NEW_ARGLOAD (cfg, ins, ip [1]);
2974 CHECK_STACK_OVF (1);
2976 NEW_ARGLOADA (cfg, ins, ip [1]);
2986 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
2987 handle_loaded_temps (cfg, bblock, stack_start, sp);
2989 if (ins->opcode == CEE_STOBJ) {
2990 NEW_ARGLOADA (cfg, ins, ip [1]);
2991 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2993 MONO_ADD_INS (bblock, ins);
2998 CHECK_STACK_OVF (1);
2999 CHECK_LOCAL (ip [1]);
3000 NEW_LOCLOAD (cfg, ins, ip [1]);
3007 CHECK_STACK_OVF (1);
3008 CHECK_LOCAL (ip [1]);
3009 NEW_LOCLOADA (cfg, ins, ip [1]);
3018 handle_loaded_temps (cfg, bblock, stack_start, sp);
3019 CHECK_LOCAL (ip [1]);
3020 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
3022 if (ins->opcode == CEE_STOBJ) {
3023 NEW_LOCLOADA (cfg, ins, ip [1]);
3024 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3026 MONO_ADD_INS (bblock, ins);
3031 CHECK_STACK_OVF (1);
3032 NEW_PCONST (cfg, ins, NULL);
3034 ins->type = STACK_OBJ;
3039 CHECK_STACK_OVF (1);
3040 NEW_ICONST (cfg, ins, -1);
3054 CHECK_STACK_OVF (1);
3055 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
3062 CHECK_STACK_OVF (1);
3064 NEW_ICONST (cfg, ins, *((signed char*)ip));
3071 CHECK_STACK_OVF (1);
3072 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
3079 CHECK_STACK_OVF (1);
3080 MONO_INST_NEW (cfg, ins, OP_I8CONST);
3082 ins->type = STACK_I8;
3084 ins->inst_l = (gint64)read64 (ip);
3089 float *f = g_malloc (sizeof (float));
3091 CHECK_STACK_OVF (1);
3092 MONO_INST_NEW (cfg, ins, OP_R4CONST);
3093 ins->type = STACK_R8;
3102 double *d = g_malloc (sizeof (double));
3104 CHECK_STACK_OVF (1);
3105 MONO_INST_NEW (cfg, ins, OP_R8CONST);
3106 ins->type = STACK_R8;
3115 MonoInst *temp, *store;
3117 CHECK_STACK_OVF (1);
3122 * small optimization: if the loaded value was from a local already,
3123 * just load it twice.
3125 if (ins->ssa_op == MONO_SSA_LOAD &&
3126 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
3128 MONO_INST_NEW (cfg, temp, 0);
3130 temp->cil_code = ip;
3133 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3134 temp->cil_code = ip;
3135 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3136 store->cil_code = ip;
3137 MONO_ADD_INS (bblock, store);
3138 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3141 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3151 MONO_INST_NEW (cfg, ins, CEE_POP);
3152 MONO_ADD_INS (bblock, ins);
3153 ins->cil_code = ip++;
3159 if (stack_start != sp)
3161 MONO_INST_NEW (cfg, ins, CEE_JMP);
3162 token = read32 (ip + 1);
3163 /* FIXME: check the signature matches */
3164 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3165 ins->inst_p0 = cmethod;
3166 MONO_ADD_INS (bblock, ins);
3168 start_new_bblock = 1;
3172 case CEE_CALLVIRT: {
3173 MonoInst *addr = NULL;
3174 MonoMethodSignature *fsig = NULL;
3175 int temp, array_rank = 0;
3176 int virtual = *ip == CEE_CALLVIRT;
3179 token = read32 (ip + 1);
3181 if (*ip == CEE_CALLI) {
3186 if (method->wrapper_type != MONO_WRAPPER_NONE)
3187 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
3189 fsig = mono_metadata_parse_signature (image, token);
3191 n = fsig->param_count + fsig->hasthis;
3193 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3194 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
3195 } else if (constrained_call) {
3196 cmethod = mono_get_method_constrained (image, token, constrained_call, generic_context);
3198 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3203 if (!cmethod->klass->inited)
3204 mono_class_init (cmethod->klass);
3206 if (cmethod->signature->pinvoke) {
3207 #ifdef MONO_USE_EXC_TABLES
3208 if (mono_method_blittable (cmethod)) {
3209 fsig = cmethod->signature;
3212 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
3213 fsig = wrapper->signature;
3214 #ifdef MONO_USE_EXC_TABLES
3218 fsig = mono_method_get_signature (cmethod, image, token);
3221 n = fsig->param_count + fsig->hasthis;
3223 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
3224 cmethod->klass->parent == mono_defaults.array_class) {
3225 array_rank = cmethod->klass->rank;
3228 if (cmethod->string_ctor)
3229 g_assert_not_reached ();
3235 //g_assert (!virtual || fsig->hasthis);
3239 if (constrained_call) {
3241 * We have the `constrained.' prefix opcode.
3243 if (constrained_call->valuetype && !cmethod->klass->valuetype) {
3245 * The type parameter is instantiated as a valuetype,
3246 * but that type doesn't override the method we're
3247 * calling, so we need to box `this'.
3249 sp [0] = handle_box (cfg, bblock, sp [0], ip, constrained_call);
3250 } else if (!constrained_call->valuetype) {
3254 * The type parameter is instantiated as a reference
3255 * type. We have a managed pointer on the stack, so
3256 * we need to dereference it here.
3259 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3261 ins->inst_i0 = sp [0];
3262 ins->type = STACK_OBJ;
3264 } else if (cmethod->klass->valuetype)
3266 constrained_call = NULL;
3269 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
3272 if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL) && (mono_metadata_signature_equal (method->signature, cmethod->signature))) {
3274 /* FIXME: This assumes the two methods has the same number and type of arguments */
3275 for (i = 0; i < n; ++i) {
3276 /* Check if argument is the same */
3277 NEW_ARGLOAD (cfg, ins, i);
3278 if ((ins->opcode == sp [i]->opcode) && (ins->inst_i0 == sp [i]->inst_i0))
3281 /* Prevent argument from being register allocated */
3282 arg_array [i]->flags |= MONO_INST_VOLATILE;
3283 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3285 if (ins->opcode == CEE_STOBJ) {
3286 NEW_ARGLOADA (cfg, ins, i);
3287 handle_stobj (cfg, bblock, ins, sp [i], sp [i]->cil_code, ins->klass, FALSE, FALSE);
3290 MONO_ADD_INS (bblock, ins);
3292 MONO_INST_NEW (cfg, ins, CEE_JMP);
3294 ins->inst_p0 = cmethod;
3295 ins->inst_p1 = arg_array [0];
3296 MONO_ADD_INS (bblock, ins);
3297 start_new_bblock = 1;
3298 /* skip CEE_RET as well */
3303 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_opcode_for_method (cfg, cmethod, fsig, sp))) {
3306 if (MONO_TYPE_IS_VOID (fsig->ret)) {
3307 MONO_ADD_INS (bblock, ins);
3309 type_to_eval_stack_type (fsig->ret, ins);
3318 handle_loaded_temps (cfg, bblock, stack_start, sp);
3320 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3321 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
3322 mono_method_check_inlining (cfg, cmethod) &&
3323 !g_list_find (dont_inline, cmethod)) {
3325 MonoBasicBlock *ebblock;
3327 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
3328 cmethod = mono_marshal_get_native_wrapper (cmethod);
3330 if (costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, FALSE)) {
3334 GET_BBLOCK (cfg, bbhash, bblock, ip);
3335 ebblock->next_bb = bblock;
3336 link_bblock (cfg, ebblock, bblock);
3338 if (!MONO_TYPE_IS_VOID (fsig->ret))
3341 /* indicates start of a new block, and triggers a load of all
3342 stack arguments at bb boundarie */
3345 inline_costs += costs;
3350 inline_costs += 10 * num_calls++;
3352 /* tail recursion elimination */
3353 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == method && ip [5] == CEE_RET) {
3354 gboolean has_vtargs = FALSE;
3357 /* keep it simple */
3358 for (i = fsig->param_count - 1; i >= 0; i--) {
3359 if (MONO_TYPE_ISSTRUCT (cmethod->signature->params [i]))
3364 for (i = 0; i < n; ++i) {
3365 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3367 MONO_ADD_INS (bblock, ins);
3369 MONO_INST_NEW (cfg, ins, CEE_BR);
3371 MONO_ADD_INS (bblock, ins);
3372 tblock = start_bblock->out_bb [0];
3373 link_bblock (cfg, bblock, tblock);
3374 ins->inst_target_bb = tblock;
3375 start_new_bblock = 1;
3378 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3379 /* just create a dummy - the value is never used */
3380 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
3381 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3389 if (*ip == CEE_CALLI) {
3391 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
3392 NEW_TEMPLOAD (cfg, *sp, temp);
3396 } else if (array_rank) {
3399 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
3400 if (sp [fsig->param_count]->type == STACK_OBJ) {
3401 MonoInst *iargs [2];
3402 MonoInst *array, *to_store, *store;
3404 handle_loaded_temps (cfg, bblock, stack_start, sp);
3406 array = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
3407 NEW_TEMPSTORE (cfg, store, array->inst_c0, sp [0]);
3408 store->cil_code = ip;
3409 MONO_ADD_INS (bblock, store);
3410 NEW_TEMPLOAD (cfg, iargs [0], array->inst_c0);
3412 to_store = mono_compile_create_var (cfg, type_from_stack_type (sp [fsig->param_count]), OP_LOCAL);
3413 NEW_TEMPSTORE (cfg, store, to_store->inst_c0, sp [fsig->param_count]);
3414 store->cil_code = ip;
3415 MONO_ADD_INS (bblock, store);
3416 NEW_TEMPLOAD (cfg, iargs [1], to_store->inst_c0);
3419 * We first save the args for the call so that the args are copied to the stack
3420 * and a new instruction tree for them is created. If we don't do this,
3421 * the same MonoInst is added to two different trees and this is not
3424 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref_check, iargs, ip);
3426 NEW_TEMPLOAD (cfg, sp [0], array->inst_c0);
3427 NEW_TEMPLOAD (cfg, sp [fsig->param_count], to_store->inst_c0);
3430 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
3431 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
3433 if (ins->opcode == CEE_STOBJ) {
3434 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
3436 MONO_ADD_INS (bblock, ins);
3439 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
3440 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3441 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
3445 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
3446 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3449 g_assert_not_reached ();
3453 if (0 && CODE_IS_STLOC (ip + 5) && (!MONO_TYPE_ISSTRUCT (fsig->ret)) && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)) {
3454 /* no need to spill */
3455 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
3458 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) {
3459 NEW_TEMPLOAD (cfg, *sp, temp);
3469 if (cfg->method != method) {
3470 /* return from inlined methode */
3475 //g_assert (returnvar != -1);
3476 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
3477 store->cil_code = sp [0]->cil_code;
3478 if (store->opcode == CEE_STOBJ) {
3479 g_assert_not_reached ();
3480 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
3481 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
3483 MONO_ADD_INS (bblock, store);
3487 g_assert (!return_var);
3490 MONO_INST_NEW (cfg, ins, CEE_NOP);
3491 ins->opcode = mono_type_to_stind (method->signature->ret);
3492 if (ins->opcode == CEE_STOBJ) {
3493 NEW_RETLOADA (cfg, ins);
3494 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3496 ins->opcode = OP_SETRET;
3498 ins->inst_i0 = *sp;;
3499 ins->inst_i1 = NULL;
3500 MONO_ADD_INS (bblock, ins);
3504 if (sp != stack_start)
3506 MONO_INST_NEW (cfg, ins, CEE_BR);
3507 ins->cil_code = ip++;
3508 ins->inst_target_bb = end_bblock;
3509 MONO_ADD_INS (bblock, ins);
3510 link_bblock (cfg, bblock, end_bblock);
3511 start_new_bblock = 1;
3515 MONO_INST_NEW (cfg, ins, CEE_BR);
3516 ins->cil_code = ip++;
3517 MONO_ADD_INS (bblock, ins);
3518 target = ip + 1 + (signed char)(*ip);
3520 GET_BBLOCK (cfg, bbhash, tblock, target);
3521 link_bblock (cfg, bblock, tblock);
3522 CHECK_BBLOCK (target, ip, tblock);
3523 ins->inst_target_bb = tblock;
3524 if (sp != stack_start) {
3525 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3528 start_new_bblock = 1;
3535 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3536 ins->cil_code = ip++;
3537 target = ip + 1 + *(signed char*)ip;
3539 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
3540 if (sp != stack_start) {
3541 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3558 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3559 ins->cil_code = ip++;
3560 target = ip + 1 + *(signed char*)ip;
3563 if (sp != stack_start) {
3564 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3571 MONO_INST_NEW (cfg, ins, CEE_BR);
3572 ins->cil_code = ip++;
3573 MONO_ADD_INS (bblock, ins);
3574 target = ip + 4 + (gint32)read32(ip);
3576 GET_BBLOCK (cfg, bbhash, tblock, target);
3577 link_bblock (cfg, bblock, tblock);
3578 CHECK_BBLOCK (target, ip, tblock);
3579 ins->inst_target_bb = tblock;
3580 if (sp != stack_start) {
3581 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3584 start_new_bblock = 1;
3591 MONO_INST_NEW (cfg, ins, *ip);
3592 ins->cil_code = ip++;
3593 target = ip + 4 + (gint32)read32(ip);
3595 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
3596 if (sp != stack_start) {
3597 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3614 MONO_INST_NEW (cfg, ins, *ip);
3615 ins->cil_code = ip++;
3616 target = ip + 4 + (gint32)read32(ip);
3619 if (sp != stack_start) {
3620 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3628 n = read32 (ip + 1);
3629 MONO_INST_NEW (cfg, ins, *ip);
3631 ins->inst_left = *sp;
3632 if (ins->inst_left->type != STACK_I4) goto unverified;
3635 CHECK_OPSIZE (n * sizeof (guint32));
3636 target = ip + n * sizeof (guint32);
3637 MONO_ADD_INS (bblock, ins);
3638 GET_BBLOCK (cfg, bbhash, tblock, target);
3639 link_bblock (cfg, bblock, tblock);
3640 ins->klass = GUINT_TO_POINTER (n);
3641 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
3642 ins->inst_many_bb [n] = tblock;
3644 for (i = 0; i < n; ++i) {
3645 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
3646 link_bblock (cfg, bblock, tblock);
3647 ins->inst_many_bb [i] = tblock;
3650 if (sp != stack_start) {
3651 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3668 MONO_INST_NEW (cfg, ins, *ip);
3673 ins->type = ldind_type [*ip - CEE_LDIND_I1];
3674 ins->flags |= ins_flag;
3686 MONO_INST_NEW (cfg, ins, *ip);
3687 ins->cil_code = ip++;
3689 handle_loaded_temps (cfg, bblock, stack_start, sp);
3690 MONO_ADD_INS (bblock, ins);
3691 ins->inst_i0 = sp [0];
3692 ins->inst_i1 = sp [1];
3693 ins->flags |= ins_flag;
3712 /* special case that gives a nice speedup and happens to workaorund a ppc jit but (for the release)
3713 * later apply the speedup to the left shift as well
3716 if ((ins->opcode == OP_LSHR_UN) && (ins->type == STACK_I8)
3717 && (ins->inst_right->opcode == OP_ICONST) && (ins->inst_right->inst_c0 == 32)) {
3718 ins->opcode = OP_LONG_SHRUN_32;
3719 /*g_print ("applied long shr speedup to %s\n", cfg->method->name);*/
3723 if (mono_find_jit_opcode_emulation (ins->opcode)) {
3724 MonoInst *store, *temp, *load;
3726 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3727 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3728 store->cil_code = ins->cil_code;
3729 MONO_ADD_INS (bblock, store);
3730 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
3731 load->cil_code = ins->cil_code;
3733 /*g_print ("found emulation for %d\n", ins->opcode);*/
3747 case CEE_CONV_OVF_I8:
3748 case CEE_CONV_OVF_U8:
3752 if (mono_find_jit_opcode_emulation (ins->opcode)) {
3753 MonoInst *store, *temp, *load;
3755 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3756 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3757 store->cil_code = ins->cil_code;
3758 MONO_ADD_INS (bblock, store);
3759 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
3760 load->cil_code = ins->cil_code;
3762 /*g_print ("found emulation for %d\n", ins->opcode);*/
3766 case CEE_CONV_OVF_I4:
3767 case CEE_CONV_OVF_I1:
3768 case CEE_CONV_OVF_I2:
3769 case CEE_CONV_OVF_I:
3770 case CEE_CONV_OVF_U:
3773 if (sp [-1]->type == STACK_R8) {
3774 ADD_UNOP (CEE_CONV_OVF_I8);
3782 case CEE_CONV_OVF_U1:
3783 case CEE_CONV_OVF_U2:
3784 case CEE_CONV_OVF_U4:
3787 if (sp [-1]->type == STACK_R8) {
3788 ADD_UNOP (CEE_CONV_OVF_U8);
3796 case CEE_CONV_OVF_I1_UN:
3797 case CEE_CONV_OVF_I2_UN:
3798 case CEE_CONV_OVF_I4_UN:
3799 case CEE_CONV_OVF_I8_UN:
3800 case CEE_CONV_OVF_U1_UN:
3801 case CEE_CONV_OVF_U2_UN:
3802 case CEE_CONV_OVF_U4_UN:
3803 case CEE_CONV_OVF_U8_UN:
3804 case CEE_CONV_OVF_I_UN:
3805 case CEE_CONV_OVF_U_UN:
3813 token = read32 (ip + 1);
3814 if (method->wrapper_type != MONO_WRAPPER_NONE)
3815 klass = mono_method_get_wrapper_data (method, token);
3817 klass = mono_class_get_full (image, token, generic_context);
3819 mono_class_init (klass);
3821 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3822 MonoInst *store, *load;
3823 MONO_INST_NEW (cfg, load, CEE_LDIND_REF);
3824 load->cil_code = ip;
3825 load->inst_i0 = sp [1];
3826 load->type = ldind_type [CEE_LDIND_REF];
3827 load->flags |= ins_flag;
3828 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
3829 store->cil_code = ip;
3830 handle_loaded_temps (cfg, bblock, stack_start, sp);
3831 MONO_ADD_INS (bblock, store);
3832 store->inst_i0 = sp [0];
3833 store->inst_i1 = load;
3834 store->flags |= ins_flag;
3836 n = mono_class_value_size (klass, NULL);
3837 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3839 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3840 copy->inst_left = sp [0];
3841 copy->inst_right = sp [1];
3842 copy->cil_code = ip;
3844 MONO_ADD_INS (bblock, copy);
3846 MonoInst *iargs [3];
3849 NEW_ICONST (cfg, iargs [2], n);
3850 iargs [2]->cil_code = ip;
3852 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3859 MonoInst *iargs [3];
3863 token = read32 (ip + 1);
3864 if (method->wrapper_type != MONO_WRAPPER_NONE)
3865 klass = mono_method_get_wrapper_data (method, token);
3867 klass = mono_class_get_full (image, token, generic_context);
3869 mono_class_init (klass);
3870 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3871 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3873 ins->inst_i0 = sp [0];
3874 ins->type = ldind_type [CEE_LDIND_REF];
3875 ins->flags |= ins_flag;
3881 n = mono_class_value_size (klass, NULL);
3882 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3883 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3884 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3886 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3887 copy->inst_left = iargs [0];
3888 copy->inst_right = *sp;
3889 copy->cil_code = ip;
3891 MONO_ADD_INS (bblock, copy);
3894 NEW_ICONST (cfg, iargs [2], n);
3895 iargs [2]->cil_code = ip;
3897 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3899 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3907 CHECK_STACK_OVF (1);
3909 n = read32 (ip + 1);
3911 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3913 MonoInst *iargs [1];
3915 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
3916 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
3917 NEW_TEMPLOAD (cfg, *sp, temp);
3921 if (cfg->opt & MONO_OPT_SHARED) {
3923 MonoInst *iargs [3];
3925 if (mono_compile_aot) {
3926 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n);
3929 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3930 NEW_IMAGECONST (cfg, iargs [1], image);
3931 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
3932 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
3933 NEW_TEMPLOAD (cfg, *sp, temp);
3934 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3936 if (mono_compile_aot)
3937 NEW_LDSTRCONST (cfg, ins, image, n);
3939 NEW_PCONST (cfg, ins, NULL);
3941 ins->type = STACK_OBJ;
3942 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3952 MonoInst *iargs [2];
3953 MonoMethodSignature *fsig;
3957 token = read32 (ip + 1);
3958 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3959 cmethod = mono_method_get_wrapper_data (method, token);
3961 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3962 fsig = mono_method_get_signature (cmethod, image, token);
3964 mono_class_init (cmethod->klass);
3966 n = fsig->param_count;
3969 /* move the args to allow room for 'this' in the first position */
3975 handle_loaded_temps (cfg, bblock, stack_start, sp);
3978 if (cmethod->klass->parent == mono_defaults.array_class) {
3979 NEW_METHODCONST (cfg, *sp, cmethod);
3980 temp = mono_emit_native_call (cfg, bblock, mono_array_new_va, fsig, sp, ip, FALSE);
3982 } else if (cmethod->string_ctor) {
3983 /* we simply pass a null pointer */
3984 NEW_PCONST (cfg, *sp, NULL);
3985 /* now call the string ctor */
3986 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
3988 if (cmethod->klass->valuetype) {
3989 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
3990 temp = iargs [0]->inst_c0;
3991 NEW_TEMPLOADA (cfg, *sp, temp);
3993 if (cfg->opt & MONO_OPT_SHARED) {
3994 NEW_DOMAINCONST (cfg, iargs [0]);
3995 NEW_CLASSCONST (cfg, iargs [1], cmethod->klass);
3997 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3999 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
4000 NEW_VTABLECONST (cfg, iargs [0], vtable);
4001 if (cmethod->klass->has_finalize || cmethod->klass->marshalbyref || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
4002 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
4004 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
4006 NEW_TEMPLOAD (cfg, *sp, temp);
4009 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
4010 mono_method_check_inlining (cfg, cmethod) &&
4011 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
4012 !g_list_find (dont_inline, cmethod)) {
4014 MonoBasicBlock *ebblock;
4015 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, FALSE))) {
4020 GET_BBLOCK (cfg, bbhash, bblock, ip);
4021 ebblock->next_bb = bblock;
4022 link_bblock (cfg, ebblock, bblock);
4024 NEW_TEMPLOAD (cfg, *sp, temp);
4027 /* indicates start of a new block, and triggers a load
4028 of all stack arguments at bb boundarie */
4031 inline_costs += costs;
4035 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
4038 /* now call the actual ctor */
4039 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
4043 NEW_TEMPLOAD (cfg, *sp, temp);
4054 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4055 mono_class_init (klass);
4057 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4059 MonoMethod *mono_isinst;
4060 MonoInst *iargs [1];
4061 MonoBasicBlock *ebblock;
4065 mono_isinst = mono_marshal_get_isinst (klass);
4068 costs = inline_method (cfg, mono_isinst, mono_isinst->signature, bblock,
4069 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4071 g_assert (costs > 0);
4076 GET_BBLOCK (cfg, bbhash, bblock, ip);
4077 ebblock->next_bb = bblock;
4078 link_bblock (cfg, ebblock, bblock);
4080 temp = iargs [0]->inst_i0->inst_c0;
4081 NEW_TEMPLOAD (cfg, *sp, temp);
4085 inline_costs += costs;
4089 MONO_INST_NEW (cfg, ins, *ip);
4090 ins->type = STACK_OBJ;
4091 ins->inst_left = *sp;
4092 ins->inst_newa_class = klass;
4098 case CEE_UNBOX_ANY: {
4099 MonoInst *add, *vtoffset;
4100 MonoInst *iargs [3];
4105 token = read32 (ip + 1);
4106 if (method->wrapper_type != MONO_WRAPPER_NONE)
4107 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4109 klass = mono_class_get_full (image, token, generic_context);
4110 mono_class_init (klass);
4112 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4114 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4115 MonoMethod *mono_castclass;
4116 MonoInst *iargs [1];
4117 MonoBasicBlock *ebblock;
4121 mono_castclass = mono_marshal_get_castclass (klass);
4124 costs = inline_method (cfg, mono_castclass, mono_castclass->signature, bblock,
4125 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4127 g_assert (costs > 0);
4132 GET_BBLOCK (cfg, bbhash, bblock, ip);
4133 ebblock->next_bb = bblock;
4134 link_bblock (cfg, ebblock, bblock);
4136 temp = iargs [0]->inst_i0->inst_c0;
4137 NEW_TEMPLOAD (cfg, *sp, temp);
4141 inline_costs += costs;
4144 MONO_INST_NEW (cfg, ins, CEE_CASTCLASS);
4145 ins->type = STACK_OBJ;
4146 ins->inst_left = *sp;
4148 ins->inst_newa_class = klass;
4156 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
4157 ins->type = STACK_OBJ;
4158 ins->inst_left = *sp;
4160 ins->inst_newa_class = klass;
4163 MONO_INST_NEW (cfg, add, CEE_ADD);
4164 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4165 add->inst_left = ins;
4166 add->inst_right = vtoffset;
4167 add->type = STACK_MP;
4171 n = mono_class_value_size (klass, NULL);
4172 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
4173 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
4174 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
4176 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
4177 copy->inst_left = iargs [0];
4178 copy->inst_right = *sp;
4179 copy->cil_code = ip;
4181 MONO_ADD_INS (bblock, copy);
4184 NEW_ICONST (cfg, iargs [2], n);
4185 iargs [2]->cil_code = ip;
4187 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
4189 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
4195 MonoInst *add, *vtoffset;
4200 token = read32 (ip + 1);
4201 if (method->wrapper_type != MONO_WRAPPER_NONE)
4202 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4204 klass = mono_class_get_full (image, token, generic_context);
4205 mono_class_init (klass);
4207 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
4208 ins->type = STACK_OBJ;
4209 ins->inst_left = *sp;
4211 ins->inst_newa_class = klass;
4214 MONO_INST_NEW (cfg, add, CEE_ADD);
4215 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4216 add->inst_left = ins;
4217 add->inst_right = vtoffset;
4218 add->type = STACK_MP;
4228 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4229 mono_class_init (klass);
4231 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4233 MonoMethod *mono_castclass;
4234 MonoInst *iargs [1];
4235 MonoBasicBlock *ebblock;
4239 mono_castclass = mono_marshal_get_castclass (klass);
4242 costs = inline_method (cfg, mono_castclass, mono_castclass->signature, bblock,
4243 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4245 g_assert (costs > 0);
4250 GET_BBLOCK (cfg, bbhash, bblock, ip);
4251 ebblock->next_bb = bblock;
4252 link_bblock (cfg, ebblock, bblock);
4254 temp = iargs [0]->inst_i0->inst_c0;
4255 NEW_TEMPLOAD (cfg, *sp, temp);
4259 inline_costs += costs;
4262 MONO_INST_NEW (cfg, ins, *ip);
4263 ins->type = STACK_OBJ;
4264 ins->inst_left = *sp;
4266 ins->inst_newa_class = klass;
4274 MONO_INST_NEW (cfg, ins, *ip);
4276 ins->inst_left = *sp;
4277 ins->cil_code = ip++;
4278 MONO_ADD_INS (bblock, ins);
4280 start_new_bblock = 1;
4285 MonoInst *offset_ins;
4286 MonoClassField *field;
4287 MonoBasicBlock *ebblock;
4291 if (*ip == CEE_STFLD) {
4298 // FIXME: enable this test later.
4299 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
4302 token = read32 (ip + 1);
4303 field = mono_field_from_token (image, token, &klass, generic_context);
4304 mono_class_init (klass);
4306 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
4307 /* FIXME: mark instructions for use in SSA */
4308 if (*ip == CEE_STFLD) {
4309 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound) {
4310 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
4311 MonoInst *iargs [5];
4314 NEW_CLASSCONST (cfg, iargs [1], klass);
4315 NEW_FIELDCONST (cfg, iargs [2], field);
4316 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
4320 if (cfg->opt & MONO_OPT_INLINE) {
4321 costs = inline_method (cfg, stfld_wrapper, stfld_wrapper->signature, bblock,
4322 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4323 g_assert (costs > 0);
4328 GET_BBLOCK (cfg, bbhash, bblock, ip);
4329 ebblock->next_bb = bblock;
4330 link_bblock (cfg, ebblock, bblock);
4332 /* indicates start of a new block, and triggers a load
4333 of all stack arguments at bb boundarie */
4336 inline_costs += costs;
4339 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, stfld_wrapper->signature, iargs, ip, NULL);
4343 NEW_ICONST (cfg, offset_ins, foffset);
4344 MONO_INST_NEW (cfg, ins, CEE_ADD);
4346 ins->inst_left = *sp;
4347 ins->inst_right = offset_ins;
4348 ins->type = STACK_MP;
4350 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4351 store->cil_code = ip;
4352 store->inst_left = ins;
4353 store->inst_right = sp [1];
4354 handle_loaded_temps (cfg, bblock, stack_start, sp);
4355 store->flags |= ins_flag;
4357 if (store->opcode == CEE_STOBJ) {
4358 handle_stobj (cfg, bblock, ins, sp [1], ip,
4359 mono_class_from_mono_type (field->type), FALSE, FALSE);
4361 MONO_ADD_INS (bblock, store);
4364 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound) {
4365 /* fixme: we need to inline that call somehow */
4366 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
4367 MonoInst *iargs [4];
4371 NEW_CLASSCONST (cfg, iargs [1], klass);
4372 NEW_FIELDCONST (cfg, iargs [2], field);
4373 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
4374 if (cfg->opt & MONO_OPT_INLINE) {
4375 costs = inline_method (cfg, ldfld_wrapper, ldfld_wrapper->signature, bblock,
4376 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4377 g_assert (costs > 0);
4382 GET_BBLOCK (cfg, bbhash, bblock, ip);
4383 ebblock->next_bb = bblock;
4384 link_bblock (cfg, ebblock, bblock);
4386 temp = iargs [0]->inst_i0->inst_c0;
4388 if (*ip == CEE_LDFLDA) {
4389 /* not sure howto handle this */
4390 NEW_TEMPLOADA (cfg, *sp, temp);
4392 NEW_TEMPLOAD (cfg, *sp, temp);
4396 /* indicates start of a new block, and triggers a load of
4397 all stack arguments at bb boundarie */
4400 inline_costs += costs;
4403 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, ldfld_wrapper->signature, iargs, ip, NULL);
4404 if (*ip == CEE_LDFLDA) {
4405 /* not sure howto handle this */
4406 NEW_TEMPLOADA (cfg, *sp, temp);
4408 NEW_TEMPLOAD (cfg, *sp, temp);
4413 NEW_ICONST (cfg, offset_ins, foffset);
4414 MONO_INST_NEW (cfg, ins, CEE_ADD);
4416 ins->inst_left = *sp;
4417 ins->inst_right = offset_ins;
4418 ins->type = STACK_MP;
4420 if (*ip == CEE_LDFLDA) {
4424 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4425 type_to_eval_stack_type (field->type, load);
4426 load->cil_code = ip;
4427 load->inst_left = ins;
4428 load->flags |= ins_flag;
4440 MonoClassField *field;
4441 gpointer addr = NULL;
4444 token = read32 (ip + 1);
4446 field = mono_field_from_token (image, token, &klass, generic_context);
4447 mono_class_init (klass);
4449 if ((*ip) == CEE_STSFLD)
4450 handle_loaded_temps (cfg, bblock, stack_start, sp);
4452 if (cfg->domain->special_static_fields)
4453 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
4455 if ((cfg->opt & MONO_OPT_SHARED) || (mono_compile_aot && addr)) {
4457 MonoInst *iargs [2];
4458 g_assert (field->parent);
4459 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
4460 NEW_FIELDCONST (cfg, iargs [1], field);
4461 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
4462 NEW_TEMPLOAD (cfg, ins, temp);
4465 vtable = mono_class_vtable (cfg->domain, klass);
4467 if ((!vtable->initialized || mono_compile_aot) && !(klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (klass, method)) {
4468 guint8 *tramp = mono_create_class_init_trampoline (vtable);
4469 mono_emit_native_call (cfg, bblock, tramp,
4470 helper_sig_class_init_trampoline,
4472 if (cfg->verbose_level > 2)
4473 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
4475 if (cfg->run_cctors)
4476 mono_runtime_class_init (vtable);
4478 addr = (char*)vtable->data + field->offset;
4480 if (mono_compile_aot)
4481 NEW_SFLDACONST (cfg, ins, field);
4483 NEW_PCONST (cfg, ins, addr);
4487 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
4488 * This could be later optimized to do just a couple of
4489 * memory dereferences with constant offsets.
4492 MonoInst *iargs [1];
4493 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
4494 temp = mono_emit_jit_icall (cfg, bblock, mono_get_special_static_data, iargs, ip);
4495 NEW_TEMPLOAD (cfg, ins, temp);
4499 /* FIXME: mark instructions for use in SSA */
4500 if (*ip == CEE_LDSFLDA) {
4502 } else if (*ip == CEE_STSFLD) {
4506 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4507 store->cil_code = ip;
4508 store->inst_left = ins;
4509 store->inst_right = sp [0];
4510 store->flags |= ins_flag;
4513 if (store->opcode == CEE_STOBJ) {
4514 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
4516 MONO_ADD_INS (bblock, store);
4518 gboolean is_const = FALSE;
4519 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4520 if (!((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) &&
4521 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
4522 gpointer addr = (char*)vtable->data + field->offset;
4523 /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
4525 switch (field->type->type) {
4526 case MONO_TYPE_BOOLEAN:
4528 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
4532 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
4535 case MONO_TYPE_CHAR:
4537 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
4541 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
4546 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
4550 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
4555 case MONO_TYPE_STRING:
4556 case MONO_TYPE_OBJECT:
4557 case MONO_TYPE_CLASS:
4558 case MONO_TYPE_SZARRAY:
4560 case MONO_TYPE_FNPTR:
4561 case MONO_TYPE_ARRAY:
4562 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
4563 type_to_eval_stack_type (field->type, *sp);
4568 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
4569 sp [0]->type = STACK_I8;
4570 sp [0]->inst_l = *((gint64 *)addr);
4575 case MONO_TYPE_VALUETYPE:
4584 CHECK_STACK_OVF (1);
4585 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4586 type_to_eval_stack_type (field->type, load);
4587 load->cil_code = ip;
4588 load->inst_left = ins;
4590 load->flags |= ins_flag;
4592 /* fixme: dont see the problem why this does not work */
4593 //cfg->disable_aot = TRUE;
4603 token = read32 (ip + 1);
4604 if (method->wrapper_type != MONO_WRAPPER_NONE)
4605 klass = mono_method_get_wrapper_data (method, token);
4607 klass = mono_class_get_full (image, token, generic_context);
4608 mono_class_init (klass);
4609 n = mono_type_to_stind (&klass->byval_arg);
4610 if (n == CEE_STOBJ) {
4611 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
4613 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
4615 MONO_INST_NEW (cfg, store, n);
4616 store->cil_code = ip;
4617 store->inst_left = sp [0];
4618 store->inst_right = sp [1];
4619 store->flags |= ins_flag;
4620 MONO_ADD_INS (bblock, store);
4632 token = read32 (ip + 1);
4633 if (method->wrapper_type != MONO_WRAPPER_NONE)
4634 klass = mono_method_get_wrapper_data (method, token);
4636 klass = mono_class_get_full (image, token, generic_context);
4637 mono_class_init (klass);
4639 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4644 *sp++ = handle_box (cfg, bblock, val, ip, klass);
4651 MONO_INST_NEW (cfg, ins, *ip);
4656 token = read32 (ip + 1);
4658 /* allocate the domainvar - becaus this is used in decompose_foreach */
4659 if (cfg->opt & MONO_OPT_SHARED)
4660 mono_get_domainvar (cfg);
4662 if (method->wrapper_type != MONO_WRAPPER_NONE)
4663 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4665 klass = mono_class_get_full (image, token, generic_context);
4667 mono_class_init (klass);
4668 ins->inst_newa_class = klass;
4669 ins->inst_newa_len = *sp;
4670 ins->type = STACK_OBJ;
4674 * we store the object so calls to create the array are not interleaved
4675 * with the arguments of other calls.
4678 MonoInst *store, *temp, *load;
4680 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
4681 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4682 store->cil_code = ins->cil_code;
4683 MONO_ADD_INS (bblock, store);
4684 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
4685 load->cil_code = ins->cil_code;
4692 MONO_INST_NEW (cfg, ins, *ip);
4693 ins->cil_code = ip++;
4695 ins->inst_left = *sp;
4696 ins->type = STACK_PTR;
4704 if (method->wrapper_type != MONO_WRAPPER_NONE)
4705 klass = (MonoClass*)mono_method_get_wrapper_data (method, read32 (ip + 1));
4707 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4708 mono_class_init (klass);
4709 NEW_LDELEMA (cfg, ins, sp, klass);
4719 token = read32 (ip + 1);
4720 klass = mono_class_get_full (image, token, generic_context);
4721 mono_class_init (klass);
4722 NEW_LDELEMA (cfg, load, sp, klass);
4723 load->cil_code = ip;
4724 MONO_INST_NEW (cfg, ins, mono_type_to_ldind (&klass->byval_arg));
4726 ins->inst_left = load;
4728 type_to_eval_stack_type (&klass->byval_arg, ins);
4742 case CEE_LDELEM_REF: {
4746 * ldind.x (ldelema (array, index))
4747 * ldelema does the bounds check
4751 klass = array_access_to_klass (*ip);
4752 NEW_LDELEMA (cfg, load, sp, klass);
4753 load->cil_code = ip;
4754 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
4756 ins->inst_left = load;
4758 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
4768 case CEE_STELEM_R8: {
4772 * stind.x (ldelema (array, index), val)
4773 * ldelema does the bounds check
4777 klass = array_access_to_klass (*ip);
4778 NEW_LDELEMA (cfg, load, sp, klass);
4779 load->cil_code = ip;
4780 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
4782 ins->inst_left = load;
4783 ins->inst_right = sp [2];
4785 handle_loaded_temps (cfg, bblock, stack_start, sp);
4786 MONO_ADD_INS (bblock, ins);
4794 * stind.x (ldelema (array, index), val)
4795 * ldelema does the bounds check
4800 token = read32 (ip + 1);
4801 klass = mono_class_get_full (image, token, generic_context);
4802 mono_class_init (klass);
4803 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4804 MonoInst *iargs [3];
4805 handle_loaded_temps (cfg, bblock, stack_start, sp);
4811 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4813 NEW_LDELEMA (cfg, load, sp, klass);
4814 load->cil_code = ip;
4816 n = mono_type_to_stind (&klass->byval_arg);
4818 handle_stobj (cfg, bblock, load, sp [2], ip, klass, FALSE, FALSE);
4820 MONO_INST_NEW (cfg, ins, n);
4822 ins->inst_left = load;
4823 ins->inst_right = sp [2];
4824 handle_loaded_temps (cfg, bblock, stack_start, sp);
4825 MONO_ADD_INS (bblock, ins);
4832 case CEE_STELEM_REF: {
4833 MonoInst *iargs [3];
4838 handle_loaded_temps (cfg, bblock, stack_start, sp);
4844 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4848 NEW_GROUP (cfg, group, sp [0], sp [1]);
4849 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
4851 ins->inst_left = group;
4852 ins->inst_right = sp [2];
4853 MONO_ADD_INS (bblock, ins);
4860 case CEE_CKFINITE: {
4861 MonoInst *store, *temp;
4864 /* this instr. can throw exceptions as side effect,
4865 * so we cant eliminate dead code which contains CKFINITE opdodes.
4866 * Spilling to memory makes sure that we always perform
4870 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
4872 ins->inst_left = sp [-1];
4873 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
4875 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4876 store->cil_code = ip;
4877 MONO_ADD_INS (bblock, store);
4879 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
4886 MONO_INST_NEW (cfg, ins, *ip);
4889 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4890 mono_class_init (klass);
4891 ins->type = STACK_MP;
4892 ins->inst_left = *sp;
4894 ins->inst_newa_class = klass;
4899 case CEE_MKREFANY: {
4900 MonoInst *loc, *klassconst;
4903 MONO_INST_NEW (cfg, ins, *ip);
4906 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4907 mono_class_init (klass);
4910 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
4911 NEW_TEMPLOADA (cfg, ins->inst_right, loc->inst_c0);
4913 NEW_PCONST (cfg, klassconst, klass);
4914 NEW_GROUP (cfg, ins->inst_left, *sp, klassconst);
4916 MONO_ADD_INS (bblock, ins);
4918 NEW_TEMPLOAD (cfg, *sp, loc->inst_c0);
4925 MonoClass *handle_class;
4927 CHECK_STACK_OVF (1);
4930 n = read32 (ip + 1);
4932 handle = mono_ldtoken (image, n, &handle_class, generic_context);
4933 mono_class_init (handle_class);
4935 if (cfg->opt & MONO_OPT_SHARED) {
4937 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
4939 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
4941 NEW_IMAGECONST (cfg, iargs [0], image);
4942 NEW_ICONST (cfg, iargs [1], n);
4943 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
4944 NEW_TEMPLOAD (cfg, res, temp);
4945 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4946 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
4947 MONO_ADD_INS (bblock, store);
4948 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4950 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method_full (image, read32 (ip + 6), NULL, generic_context)) &&
4951 (cmethod->klass == mono_defaults.monotype_class->parent) &&
4952 (strcmp (cmethod->name, "GetTypeFromHandle") == 0) &&
4953 ((g_hash_table_lookup (bbhash, ip + 5) == NULL) ||
4954 (g_hash_table_lookup (bbhash, ip + 5) == bblock))) {
4955 MonoClass *tclass = mono_class_from_mono_type (handle);
4956 mono_class_init (tclass);
4957 if (mono_compile_aot)
4958 NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n);
4960 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
4961 ins->type = STACK_OBJ;
4962 ins->klass = cmethod->klass;
4965 MonoInst *store, *addr, *vtvar;
4967 if (mono_compile_aot)
4968 NEW_LDTOKENCONST (cfg, ins, image, n);
4970 NEW_PCONST (cfg, ins, handle);
4971 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
4972 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4973 NEW_INDSTORE (cfg, store, addr, ins, &mono_defaults.int_class->byval_arg);
4974 MONO_ADD_INS (bblock, store);
4975 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4991 case CEE_ADD_OVF_UN:
4993 case CEE_MUL_OVF_UN:
4995 case CEE_SUB_OVF_UN:
4998 if (mono_find_jit_opcode_emulation (ins->opcode)) {
4999 MonoInst *store, *temp, *load;
5001 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
5002 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
5003 store->cil_code = ins->cil_code;
5004 MONO_ADD_INS (bblock, store);
5005 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
5006 load->cil_code = ins->cil_code;
5008 /*g_print ("found emulation for %d\n", ins->opcode);*/
5012 case CEE_ENDFINALLY:
5013 MONO_INST_NEW (cfg, ins, *ip);
5014 MONO_ADD_INS (bblock, ins);
5015 ins->cil_code = ip++;
5016 start_new_bblock = 1;
5019 * Control will leave the method so empty the stack, otherwise
5020 * the next basic block will start with a nonempty stack.
5022 while (sp != stack_start) {
5023 MONO_INST_NEW (cfg, ins, CEE_POP);
5027 MONO_ADD_INS (bblock, ins);
5033 if (*ip == CEE_LEAVE) {
5035 target = ip + 5 + (gint32)read32(ip + 1);
5038 target = ip + 2 + (signed char)(ip [1]);
5041 /* empty the stack */
5042 while (sp != stack_start) {
5043 MONO_INST_NEW (cfg, ins, CEE_POP);
5047 MONO_ADD_INS (bblock, ins);
5051 * If this leave statement is in a catch block, check for a
5052 * pending exception, and rethrow it if necessary.
5054 for (i = 0; i < header->num_clauses; ++i) {
5055 MonoExceptionClause *clause = &header->clauses [i];
5056 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code)) {
5059 temp = mono_emit_jit_icall (cfg, bblock, mono_thread_get_pending_exception, NULL, ip);
5060 NEW_TEMPLOAD (cfg, *sp, temp);
5062 MONO_INST_NEW (cfg, ins, OP_THROW_OR_NULL);
5063 ins->inst_left = *sp;
5065 MONO_ADD_INS (bblock, ins);
5069 /* fixme: call fault handler ? */
5071 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
5073 for (tmp = handlers; tmp; tmp = tmp->next) {
5075 link_bblock (cfg, bblock, tblock);
5076 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
5078 ins->inst_target_bb = tblock;
5079 MONO_ADD_INS (bblock, ins);
5081 g_list_free (handlers);
5084 MONO_INST_NEW (cfg, ins, CEE_BR);
5086 MONO_ADD_INS (bblock, ins);
5087 GET_BBLOCK (cfg, bbhash, tblock, target);
5088 link_bblock (cfg, bblock, tblock);
5089 CHECK_BBLOCK (target, ip, tblock);
5090 ins->inst_target_bb = tblock;
5091 start_new_bblock = 1;
5093 if (*ip == CEE_LEAVE)
5102 MONO_INST_NEW (cfg, ins, *ip);
5104 handle_loaded_temps (cfg, bblock, stack_start, sp);
5105 MONO_ADD_INS (bblock, ins);
5106 ins->cil_code = ip++;
5107 ins->inst_i0 = sp [0];
5108 ins->inst_i1 = sp [1];
5116 /* trampoline mono specific opcodes */
5117 case MONO_CUSTOM_PREFIX: {
5119 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
5124 case CEE_MONO_FUNC1: {
5126 gpointer func = NULL;
5132 case MONO_MARSHAL_CONV_STR_LPWSTR:
5133 func = mono_string_to_utf16;
5135 case MONO_MARSHAL_CONV_LPWSTR_STR:
5136 func = mono_string_from_utf16;
5138 case MONO_MARSHAL_CONV_LPSTR_STR:
5139 func = mono_string_new_wrapper;
5141 case MONO_MARSHAL_CONV_STR_LPTSTR:
5142 case MONO_MARSHAL_CONV_STR_LPSTR:
5143 func = mono_string_to_utf8;
5145 case MONO_MARSHAL_CONV_STR_BSTR:
5146 func = mono_string_to_bstr;
5148 case MONO_MARSHAL_CONV_STR_TBSTR:
5149 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
5150 func = mono_string_to_ansibstr;
5152 case MONO_MARSHAL_CONV_SB_LPSTR:
5153 case MONO_MARSHAL_CONV_SB_LPTSTR:
5154 func = mono_string_builder_to_utf8;
5156 case MONO_MARSHAL_CONV_SB_LPWSTR:
5157 func = mono_string_builder_to_utf16;
5159 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
5160 func = mono_array_to_savearray;
5162 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
5163 func = mono_array_to_lparray;
5165 case MONO_MARSHAL_CONV_DEL_FTN:
5166 func = mono_delegate_to_ftnptr;
5168 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
5169 func = mono_marshal_string_array;
5171 case MONO_MARSHAL_CONV_STRARRAY_STRWLPARRAY:
5172 func = mono_marshal_string_array_to_unicode;
5175 g_warning ("unknown conversion %d\n", ip [2]);
5176 g_assert_not_reached ();
5179 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5180 NEW_TEMPLOAD (cfg, *sp, temp);
5184 inline_costs += 10 * num_calls++;
5188 case CEE_MONO_FUNC2: {
5190 gpointer func = NULL;
5196 case MONO_MARSHAL_CONV_ASANY:
5197 func = mono_marshal_asany;
5199 case MONO_MARSHAL_CONV_FTN_DEL:
5200 func = mono_ftnptr_to_delegate;
5203 g_warning ("unknown conversion %d\n", ip [2]);
5204 g_assert_not_reached ();
5206 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5207 NEW_TEMPLOAD (cfg, *sp, temp);
5211 inline_costs += 10 * num_calls++;
5215 case CEE_MONO_PROC2: {
5216 gpointer func = NULL;
5222 case MONO_MARSHAL_CONV_LPSTR_SB:
5223 case MONO_MARSHAL_CONV_LPTSTR_SB:
5224 func = mono_string_utf8_to_builder;
5226 case MONO_MARSHAL_CONV_LPWSTR_SB:
5227 func = mono_string_utf16_to_builder;
5229 case MONO_MARSHAL_FREE_ARRAY:
5230 func = mono_marshal_free_array;
5232 case MONO_MARSHAL_CONV_ASANY:
5233 func = mono_marshal_asany;
5236 g_assert_not_reached ();
5239 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5241 inline_costs += 10 * num_calls++;
5244 case CEE_MONO_PROC3: {
5245 gpointer func = NULL;
5251 case MONO_MARSHAL_CONV_STR_BYVALSTR:
5252 func = mono_string_to_byvalstr;
5254 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
5255 func = mono_string_to_byvalwstr;
5257 case MONO_MARSHAL_FREE_ASANY:
5258 func = mono_marshal_free_asany;
5261 g_assert_not_reached ();
5264 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5266 inline_costs += 10 * num_calls++;
5272 mono_emit_jit_icall (cfg, bblock, g_free, sp, ip);
5274 inline_costs += 10 * num_calls++;
5276 case CEE_MONO_LDPTR:
5277 CHECK_STACK_OVF (1);
5279 token = read32 (ip + 2);
5280 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
5284 inline_costs += 10 * num_calls++;
5286 case CEE_MONO_VTADDR:
5289 MONO_INST_NEW (cfg, ins, OP_VTADDR);
5291 ins->type = STACK_MP;
5292 ins->inst_left = *sp;
5296 case CEE_MONO_NEWOBJ: {
5297 MonoInst *iargs [2];
5299 CHECK_STACK_OVF (1);
5301 token = read32 (ip + 2);
5302 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5303 mono_class_init (klass);
5304 NEW_DOMAINCONST (cfg, iargs [0]);
5305 NEW_CLASSCONST (cfg, iargs [1], klass);
5306 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
5307 NEW_TEMPLOAD (cfg, *sp, temp);
5310 inline_costs += 10 * num_calls++;
5313 case CEE_MONO_OBJADDR:
5316 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
5318 ins->type = STACK_MP;
5319 ins->inst_left = *sp;
5323 case CEE_MONO_LDNATIVEOBJ:
5326 token = read32 (ip + 2);
5327 klass = mono_method_get_wrapper_data (method, token);
5328 g_assert (klass->valuetype);
5329 mono_class_init (klass);
5330 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
5334 case CEE_MONO_RETOBJ:
5335 g_assert (cfg->ret);
5336 g_assert (method->signature->pinvoke);
5341 token = read32 (ip + 2);
5342 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5344 NEW_RETLOADA (cfg, ins);
5345 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
5347 if (sp != stack_start)
5350 MONO_INST_NEW (cfg, ins, CEE_BR);
5352 ins->inst_target_bb = end_bblock;
5353 MONO_ADD_INS (bblock, ins);
5354 link_bblock (cfg, bblock, end_bblock);
5355 start_new_bblock = 1;
5358 case CEE_MONO_CISINST:
5359 case CEE_MONO_CCASTCLASS: {
5364 token = read32 (ip + 2);
5365 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5366 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_CISINST) ? OP_CISINST : OP_CCASTCLASS);
5367 ins->type = STACK_I4;
5368 ins->inst_left = *sp;
5369 ins->inst_newa_class = klass;
5375 case CEE_MONO_SAVE_LMF:
5376 case CEE_MONO_RESTORE_LMF:
5377 #ifdef MONO_ARCH_HAVE_LMF_OPS
5378 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_SAVE_LMF) ? OP_SAVE_LMF : OP_RESTORE_LMF);
5379 MONO_ADD_INS (bblock, ins);
5380 cfg->need_lmf_area = TRUE;
5385 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
5394 /* somewhat similar to LDTOKEN */
5395 MonoInst *addr, *vtvar;
5396 CHECK_STACK_OVF (1);
5397 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
5399 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5400 addr->cil_code = ip;
5401 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
5403 ins->inst_left = addr;
5404 MONO_ADD_INS (bblock, ins);
5405 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5419 * The following transforms:
5420 * CEE_CEQ into OP_CEQ
5421 * CEE_CGT into OP_CGT
5422 * CEE_CGT_UN into OP_CGT_UN
5423 * CEE_CLT into OP_CLT
5424 * CEE_CLT_UN into OP_CLT_UN
5426 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
5428 MONO_INST_NEW (cfg, ins, cmp->opcode);
5430 cmp->inst_i0 = sp [0];
5431 cmp->inst_i1 = sp [1];
5435 cmp->opcode = OP_COMPARE;
5437 ins->type = STACK_I4;
5440 /* spill it to reduce the expression complexity
5441 * and workaround bug 54209
5443 if (cmp->inst_left->type == STACK_I8) {
5444 MonoInst *store, *temp, *load;
5446 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
5447 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
5448 store->cil_code = ins->cil_code;
5449 MONO_ADD_INS (bblock, store);
5450 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
5451 load->cil_code = ins->cil_code;
5461 CHECK_STACK_OVF (1);
5463 n = read32 (ip + 2);
5464 if (method->wrapper_type != MONO_WRAPPER_NONE)
5465 cmethod = mono_method_get_wrapper_data (method, n);
5467 cmethod = mono_get_method_full (image, n, NULL, generic_context);
5470 mono_class_init (cmethod->klass);
5471 handle_loaded_temps (cfg, bblock, stack_start, sp);
5473 NEW_METHODCONST (cfg, argconst, cmethod);
5474 if (method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED)
5475 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
5477 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn_nosync, &argconst, ip);
5478 NEW_TEMPLOAD (cfg, *sp, temp);
5482 inline_costs += 10 * num_calls++;
5485 case CEE_LDVIRTFTN: {
5491 n = read32 (ip + 2);
5492 if (method->wrapper_type != MONO_WRAPPER_NONE)
5493 cmethod = mono_method_get_wrapper_data (method, n);
5495 cmethod = mono_get_method_full (image, n, NULL, generic_context);
5497 mono_class_init (cmethod->klass);
5498 handle_loaded_temps (cfg, bblock, stack_start, sp);
5502 NEW_METHODCONST (cfg, args [1], cmethod);
5503 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
5504 NEW_TEMPLOAD (cfg, *sp, temp);
5508 inline_costs += 10 * num_calls++;
5512 CHECK_STACK_OVF (1);
5514 n = read16 (ip + 2);
5516 NEW_ARGLOAD (cfg, ins, n);
5522 CHECK_STACK_OVF (1);
5524 n = read16 (ip + 2);
5526 NEW_ARGLOADA (cfg, ins, n);
5534 handle_loaded_temps (cfg, bblock, stack_start, sp);
5536 n = read16 (ip + 2);
5538 NEW_ARGSTORE (cfg, ins, n, *sp);
5540 if (ins->opcode == CEE_STOBJ) {
5541 NEW_ARGLOADA (cfg, ins, n);
5542 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5544 MONO_ADD_INS (bblock, ins);
5548 CHECK_STACK_OVF (1);
5550 n = read16 (ip + 2);
5552 NEW_LOCLOAD (cfg, ins, n);
5558 CHECK_STACK_OVF (1);
5560 n = read16 (ip + 2);
5562 NEW_LOCLOADA (cfg, ins, n);
5571 n = read16 (ip + 2);
5573 handle_loaded_temps (cfg, bblock, stack_start, sp);
5574 NEW_LOCSTORE (cfg, ins, n, *sp);
5576 if (ins->opcode == CEE_STOBJ) {
5577 NEW_LOCLOADA (cfg, ins, n);
5578 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5580 MONO_ADD_INS (bblock, ins);
5587 if (sp != stack_start)
5589 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
5590 ins->inst_left = *sp;
5593 cfg->flags |= MONO_CFG_HAS_ALLOCA;
5594 if (header->init_locals)
5595 ins->flags |= MONO_INST_INIT;
5599 /* FIXME: set init flag if locals init is set in this method */
5601 case CEE_ENDFILTER: {
5602 MonoExceptionClause *clause, *nearest;
5603 int cc, nearest_num;
5607 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
5609 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
5610 ins->inst_left = *sp;
5612 MONO_ADD_INS (bblock, ins);
5613 start_new_bblock = 1;
5618 for (cc = 0; cc < header->num_clauses; ++cc) {
5619 clause = &header->clauses [cc];
5620 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
5621 (!nearest || (clause->token_or_filter > nearest->token_or_filter))) {
5627 filter_lengths [nearest_num] = (ip - header->code) - nearest->token_or_filter;
5631 case CEE_UNALIGNED_:
5632 ins_flag |= MONO_INST_UNALIGNED;
5633 /* FIXME: record alignment? we can assume 1 for now */
5638 ins_flag |= MONO_INST_VOLATILE;
5642 ins_flag |= MONO_INST_TAILCALL;
5643 /* Can't inline tail calls at this time */
5644 inline_costs += 100000;
5651 token = read32 (ip + 2);
5652 if (method->wrapper_type != MONO_WRAPPER_NONE)
5653 klass = mono_method_get_wrapper_data (method, token);
5655 klass = mono_class_get_full (image, token, generic_context);
5656 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5657 MonoInst *store, *load;
5658 NEW_PCONST (cfg, load, NULL);
5659 load->cil_code = ip;
5660 load->type = STACK_OBJ;
5661 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
5662 store->cil_code = ip;
5663 handle_loaded_temps (cfg, bblock, stack_start, sp);
5664 MONO_ADD_INS (bblock, store);
5665 store->inst_i0 = sp [0];
5666 store->inst_i1 = load;
5668 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
5673 case CEE_CONSTRAINED_:
5674 /* FIXME: implement */
5676 token = read32 (ip + 2);
5677 constrained_call = mono_class_get_full (image, token, generic_context);
5682 MonoInst *iargs [3];
5685 if ((cfg->opt & MONO_OPT_INTRINS) && (ip [1] == CEE_CPBLK) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5)) {
5687 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
5688 copy->inst_left = sp [0];
5689 copy->inst_right = sp [1];
5690 copy->cil_code = ip;
5692 MONO_ADD_INS (bblock, copy);
5699 handle_loaded_temps (cfg, bblock, stack_start, sp);
5700 if (ip [1] == CEE_CPBLK) {
5701 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
5703 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
5712 ins_flag |= MONO_INST_NOTYPECHECK;
5714 ins_flag |= MONO_INST_NORANGECHECK;
5715 /* we ignore the no-nullcheck for now since we
5716 * really do it explicitly only when doing callvirt->call
5722 /* FIXME: check we are in a catch handler */
5723 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
5724 load->cil_code = ip;
5725 MONO_INST_NEW (cfg, ins, CEE_THROW);
5726 ins->inst_left = load;
5728 MONO_ADD_INS (bblock, ins);
5730 start_new_bblock = 1;
5735 CHECK_STACK_OVF (1);
5737 token = read32 (ip + 2);
5738 /* FIXXME: handle generics. */
5739 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
5740 MonoType *type = mono_type_create_from_typespec (image, token);
5741 token = mono_type_size (type, &align);
5743 MonoClass *szclass = mono_class_get_full (image, token, generic_context);
5744 mono_class_init (szclass);
5745 token = mono_class_value_size (szclass, &align);
5747 NEW_ICONST (cfg, ins, token);
5752 case CEE_REFANYTYPE:
5754 MONO_INST_NEW (cfg, ins, OP_REFANYTYPE);
5756 ins->type = STACK_MP;
5757 ins->inst_left = *sp;
5758 ins->type = STACK_VTYPE;
5759 ins->klass = mono_defaults.typehandle_class;
5768 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
5773 g_error ("opcode 0x%02x not handled", *ip);
5776 if (start_new_bblock != 1)
5779 bblock->cil_length = ip - bblock->cil_code;
5780 bblock->next_bb = end_bblock;
5781 link_bblock (cfg, bblock, end_bblock);
5783 if (cfg->method == method && cfg->domainvar) {
5787 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
5788 call->signature = helper_sig_domain_get;
5789 call->inst.type = STACK_PTR;
5790 call->fptr = mono_domain_get;
5791 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, (MonoInst*)call);
5793 MONO_ADD_INS (init_localsbb, store);
5796 if (header->init_locals) {
5798 for (i = 0; i < header->num_locals; ++i) {
5799 int t = header->locals [i]->type;
5800 if (t == MONO_TYPE_VALUETYPE && header->locals [i]->data.klass->enumtype)
5801 t = header->locals [i]->data.klass->enum_basetype->type;
5802 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
5803 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
5804 NEW_ICONST (cfg, ins, 0);
5805 NEW_LOCSTORE (cfg, store, i, ins);
5806 MONO_ADD_INS (init_localsbb, store);
5807 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
5808 MONO_INST_NEW (cfg, ins, OP_I8CONST);
5809 ins->type = STACK_I8;
5811 NEW_LOCSTORE (cfg, store, i, ins);
5812 MONO_ADD_INS (init_localsbb, store);
5813 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
5814 MONO_INST_NEW (cfg, ins, OP_R8CONST);
5815 ins->type = STACK_R8;
5816 ins->inst_p0 = (void*)&r8_0;
5817 NEW_LOCSTORE (cfg, store, i, ins);
5818 MONO_ADD_INS (init_localsbb, store);
5819 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF)) {
5820 NEW_LOCLOADA (cfg, ins, i);
5821 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (header->locals [i]), NULL, NULL);
5824 NEW_PCONST (cfg, ins, NULL);
5825 NEW_LOCSTORE (cfg, store, i, ins);
5826 MONO_ADD_INS (init_localsbb, store);
5832 /* resolve backward branches in the middle of an existing basic block */
5833 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
5835 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
5836 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
5837 if (tblock != start_bblock) {
5839 split_bblock (cfg, tblock, bblock);
5840 l = bblock->cil_code - header->code;
5841 bblock->cil_length = tblock->cil_length - l;
5842 tblock->cil_length = l;
5844 g_print ("recheck failed.\n");
5849 * we compute regions here, because the length of filter clauses is not known in advance.
5850 * It is computed in the CEE_ENDFILTER case in the above switch statement
5852 if (cfg->method == method) {
5854 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5855 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
5857 mono_create_spvar_for_region (cfg, bb->region);
5858 if (cfg->verbose_level > 2)
5859 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
5862 g_hash_table_destroy (bbhash);
5865 dont_inline = g_list_remove (dont_inline, method);
5866 return inline_costs;
5869 if (cfg->method != method)
5870 g_hash_table_destroy (bbhash);
5871 dont_inline = g_list_remove (dont_inline, method);
5875 if (cfg->method != method)
5876 g_hash_table_destroy (bbhash);
5877 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
5878 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
5879 dont_inline = g_list_remove (dont_inline, method);
5884 mono_print_tree (MonoInst *tree) {
5890 arity = mono_burg_arity [tree->opcode];
5892 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
5894 switch (tree->opcode) {
5896 printf ("[%d]", tree->inst_c0);
5899 printf ("[%lld]", tree->inst_l);
5902 printf ("[%f]", *(double*)tree->inst_p0);
5905 printf ("[%f]", *(float*)tree->inst_p0);
5909 printf ("[%d]", tree->inst_c0);
5912 if (tree->inst_offset < 0)
5913 printf ("[-0x%x(%s)]", -tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5915 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5918 printf ("[%s]", mono_arch_regname (tree->dreg));
5921 printf ("[%s]", tree->inst_newa_class->name);
5922 mono_print_tree (tree->inst_newa_len);
5933 case OP_VOIDCALLVIRT: {
5934 MonoCallInst *call = (MonoCallInst*)tree;
5936 printf ("[%s]", call->method->name);
5941 printf ("[%d (", tree->inst_c0);
5942 for (i = 0; i < tree->inst_phi_args [0]; i++) {
5945 printf ("%d", tree->inst_phi_args [i + 1]);
5956 case OP_LOAD_MEMBASE:
5957 case OP_LOADI4_MEMBASE:
5958 case OP_LOADU4_MEMBASE:
5959 case OP_LOADU1_MEMBASE:
5960 case OP_LOADI1_MEMBASE:
5961 case OP_LOADU2_MEMBASE:
5962 case OP_LOADI2_MEMBASE:
5963 printf ("[%s] <- [%s + 0x%x]", mono_arch_regname (tree->dreg), mono_arch_regname (tree->inst_basereg), tree->inst_offset);
5966 printf ("[B%d]", tree->inst_target_bb->block_num);
5976 case OP_VOIDCALL_REG:
5977 mono_print_tree (tree->inst_left);
5989 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
5990 mono_print_tree (tree->inst_left);
5993 if (!mono_arch_print_tree(tree, arity)) {
5995 mono_print_tree (tree->inst_left);
5997 mono_print_tree (tree->inst_right);
6008 mono_print_tree_nl (MonoInst *tree)
6010 mono_print_tree (tree);
6015 type_from_typename (char *typename)
6019 if (!strcmp (typename, "int"))
6020 klass = mono_defaults.int_class;
6021 else if (!strcmp (typename, "ptr"))
6022 klass = mono_defaults.int_class;
6023 else if (!strcmp (typename, "void"))
6024 klass = mono_defaults.void_class;
6025 else if (!strcmp (typename, "int32"))
6026 klass = mono_defaults.int32_class;
6027 else if (!strcmp (typename, "uint32"))
6028 klass = mono_defaults.uint32_class;
6029 else if (!strcmp (typename, "long"))
6030 klass = mono_defaults.int64_class;
6031 else if (!strcmp (typename, "ulong"))
6032 klass = mono_defaults.uint64_class;
6033 else if (!strcmp (typename, "float"))
6034 klass = mono_defaults.single_class;
6035 else if (!strcmp (typename, "double"))
6036 klass = mono_defaults.double_class;
6037 else if (!strcmp (typename, "object"))
6038 klass = mono_defaults.object_class;
6041 g_assert_not_reached ();
6043 return &klass->byval_arg;
6046 static MonoMethodSignature*
6047 make_icall_sig (const char *sigstr)
6052 MonoMethodSignature *res;
6054 parts = g_strsplit (sigstr, " ", 256);
6063 res = mono_metadata_signature_alloc (mono_defaults.corlib, len - 1);
6066 #ifdef PLATFORM_WIN32
6068 * Under windows, the default pinvoke calling convention is STDCALL but
6071 res->call_convention = MONO_CALL_C;
6074 res->ret = type_from_typename (parts [0]);
6075 for (i = 1; i < len; ++i) {
6076 res->params [i - 1] = type_from_typename (parts [i]);
6085 create_helper_signature (void)
6088 /* FIXME: set call conv */
6089 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
6090 helper_sig_newarr = make_icall_sig ("object ptr ptr int32");
6092 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
6093 helper_sig_newarr_specific = make_icall_sig ("object ptr int32");
6095 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
6096 helper_sig_object_new = make_icall_sig ("object ptr ptr");
6098 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
6099 helper_sig_object_new_specific = make_icall_sig ("object ptr");
6101 /* void* mono_method_compile (MonoMethod*) */
6102 helper_sig_compile = make_icall_sig ("ptr ptr");
6104 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
6105 helper_sig_compile_virt = make_icall_sig ("ptr object ptr");
6107 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
6108 helper_sig_ldstr = make_icall_sig ("object ptr ptr int32");
6110 /* MonoDomain *mono_domain_get (void) */
6111 helper_sig_domain_get = make_icall_sig ("ptr");
6113 /* void stelem_ref (MonoArray *, int index, MonoObject *) */
6114 helper_sig_stelem_ref = make_icall_sig ("void ptr int32 object");
6116 /* void stelem_ref_check (MonoArray *, MonoObject *) */
6117 helper_sig_stelem_ref_check = make_icall_sig ("void object object");
6119 /* long amethod (long, long) */
6120 helper_sig_long_long_long = make_icall_sig ("long long long");
6122 /* object amethod (intptr) */
6123 helper_sig_obj_ptr = make_icall_sig ("object ptr");
6125 helper_sig_obj_ptr_ptr = make_icall_sig ("object ptr ptr");
6127 /* void amethod (intptr) */
6128 helper_sig_void_ptr = make_icall_sig ("void ptr");
6130 /* void amethod (MonoObject *obj) */
6131 helper_sig_void_obj = make_icall_sig ("void object");
6133 /* void amethod (MonoObject *obj, void *ptr, int i) */
6134 helper_sig_void_obj_ptr_int = make_icall_sig ("void object ptr int");
6136 /* intptr amethod (void) */
6137 helper_sig_ptr_void = make_icall_sig ("ptr");
6139 /* object amethod (void) */
6140 helper_sig_obj_void = make_icall_sig ("object");
6142 /* void amethod (intptr, intptr) */
6143 helper_sig_void_ptr_ptr = make_icall_sig ("void ptr ptr");
6145 /* void amethod (intptr, intptr, intptr) */
6146 helper_sig_void_ptr_ptr_ptr = make_icall_sig ("void ptr ptr ptr");
6148 /* intptr amethod (intptr, intptr) */
6149 helper_sig_ptr_ptr_ptr = make_icall_sig ("ptr ptr ptr");
6151 /* IntPtr amethod (object) */
6152 helper_sig_ptr_obj = make_icall_sig ("ptr object");
6154 /* IntPtr amethod (object, int) */
6155 helper_sig_ptr_obj_int = make_icall_sig ("ptr object int");
6157 /* IntPtr amethod (int) */
6158 helper_sig_ptr_int = make_icall_sig ("ptr int32");
6160 /* long amethod (long, guint32) */
6161 helper_sig_long_long_int = make_icall_sig ("long long int32");
6163 /* ulong amethod (double) */
6164 helper_sig_ulong_double = make_icall_sig ("ulong double");
6166 /* long amethod (double) */
6167 helper_sig_long_double = make_icall_sig ("long double");
6169 /* double amethod (long) */
6170 helper_sig_double_long = make_icall_sig ("double long");
6172 /* double amethod (int) */
6173 helper_sig_double_int = make_icall_sig ("double int32");
6175 /* float amethod (long) */
6176 helper_sig_float_long = make_icall_sig ("float long");
6178 /* double amethod (double, double) */
6179 helper_sig_double_double_double = make_icall_sig ("double double double");
6181 /* uint amethod (double) */
6182 helper_sig_uint_double = make_icall_sig ("uint32 double");
6184 /* int amethod (double) */
6185 helper_sig_int_double = make_icall_sig ("int32 double");
6187 /* void initobj (intptr, int size) */
6188 helper_sig_initobj = make_icall_sig ("void ptr int32");
6190 /* void memcpy (intptr, intptr, int size) */
6191 helper_sig_memcpy = make_icall_sig ("void ptr ptr int32");
6193 /* void memset (intptr, int val, int size) */
6194 helper_sig_memset = make_icall_sig ("void ptr int32 int32");
6196 helper_sig_class_init_trampoline = make_icall_sig ("void");
6199 static GHashTable *jit_icall_hash_name = NULL;
6200 static GHashTable *jit_icall_hash_addr = NULL;
6203 mono_find_jit_icall_by_name (const char *name)
6205 MonoJitICallInfo *info;
6206 g_assert (jit_icall_hash_name);
6208 //printf ("lookup addr %s %p\n", name, g_hash_table_lookup (jit_icall_hash_name, name));
6209 EnterCriticalSection (&trampoline_hash_mutex);
6210 info = g_hash_table_lookup (jit_icall_hash_name, name);
6211 LeaveCriticalSection (&trampoline_hash_mutex);
6216 mono_find_jit_icall_by_addr (gconstpointer addr)
6218 MonoJitICallInfo *info;
6219 g_assert (jit_icall_hash_addr);
6221 EnterCriticalSection (&trampoline_hash_mutex);
6222 info = g_hash_table_lookup (jit_icall_hash_addr, (gpointer)addr);
6223 LeaveCriticalSection (&trampoline_hash_mutex);
6229 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
6232 MonoMethod *wrapper;
6235 if (callinfo->wrapper)
6236 return callinfo->wrapper;
6238 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
6239 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
6240 /* Must be domain neutral since there is only one copy */
6241 code = mono_jit_compile_method_with_opt (wrapper, default_opt | MONO_OPT_SHARED);
6243 EnterCriticalSection (&trampoline_hash_mutex);
6244 if (!callinfo->wrapper) {
6245 callinfo->wrapper = code;
6246 g_hash_table_insert (jit_icall_hash_addr, (gpointer)callinfo->wrapper, callinfo);
6248 LeaveCriticalSection (&trampoline_hash_mutex);
6251 return callinfo->wrapper;
6255 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save)
6257 MonoJitICallInfo *info;
6262 EnterCriticalSection (&trampoline_hash_mutex);
6264 if (!jit_icall_hash_name) {
6265 jit_icall_hash_name = g_hash_table_new (g_str_hash, g_str_equal);
6266 jit_icall_hash_addr = g_hash_table_new (NULL, NULL);
6269 if (g_hash_table_lookup (jit_icall_hash_name, name)) {
6270 g_warning ("jit icall already defined \"%s\"\n", name);
6271 g_assert_not_reached ();
6274 info = g_new (MonoJitICallInfo, 1);
6281 #ifdef MONO_USE_EXC_TABLES
6282 || mono_arch_has_unwind_info (func)
6285 info->wrapper = func;
6287 info->wrapper = NULL;
6290 g_hash_table_insert (jit_icall_hash_name, (gpointer)info->name, info);
6291 g_hash_table_insert (jit_icall_hash_addr, (gpointer)func, info);
6293 LeaveCriticalSection (&trampoline_hash_mutex);
6298 mono_create_class_init_trampoline (MonoVTable *vtable)
6302 /* previously created trampoline code */
6303 mono_domain_lock (vtable->domain);
6305 mono_g_hash_table_lookup (vtable->domain->class_init_trampoline_hash,
6307 mono_domain_unlock (vtable->domain);
6311 code = mono_arch_create_class_init_trampoline (vtable);
6313 /* store trampoline address */
6314 mono_domain_lock (vtable->domain);
6315 mono_g_hash_table_insert (vtable->domain->class_init_trampoline_hash,
6317 mono_domain_unlock (vtable->domain);
6319 EnterCriticalSection (&trampoline_hash_mutex);
6320 if (!class_init_hash_addr)
6321 class_init_hash_addr = g_hash_table_new (NULL, NULL);
6322 g_hash_table_insert (class_init_hash_addr, code, vtable);
6323 LeaveCriticalSection (&trampoline_hash_mutex);
6329 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method,
6330 gboolean add_sync_wrapper)
6335 if (add_sync_wrapper && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
6336 return mono_create_jump_trampoline (domain, mono_marshal_get_synchronized_wrapper (method), FALSE);
6338 code = mono_jit_find_compiled_method (domain, method);
6342 EnterCriticalSection (&trampoline_hash_mutex);
6344 if (jump_trampoline_hash) {
6345 code = g_hash_table_lookup (jump_trampoline_hash, method);
6347 LeaveCriticalSection (&trampoline_hash_mutex);
6352 ji = mono_arch_create_jump_trampoline (method);
6355 * mono_delegate_ctor needs to find the method metadata from the
6356 * trampoline address, so we save it here.
6359 mono_jit_info_table_add (mono_root_domain, ji);
6361 if (!jump_trampoline_hash)
6362 jump_trampoline_hash = g_hash_table_new (NULL, NULL);
6363 g_hash_table_insert (jump_trampoline_hash, method, ji->code_start);
6365 LeaveCriticalSection (&trampoline_hash_mutex);
6367 return ji->code_start;
6371 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
6375 EnterCriticalSection (&trampoline_hash_mutex);
6376 if (class_init_hash_addr)
6377 res = g_hash_table_lookup (class_init_hash_addr, addr);
6380 LeaveCriticalSection (&trampoline_hash_mutex);
6385 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func, gboolean no_throw)
6387 MonoJitICallInfo *info;
6389 if (!emul_opcode_map)
6390 emul_opcode_map = g_new0 (MonoJitICallInfo*, OP_LAST + 1);
6392 g_assert (!sig->hasthis);
6393 g_assert (sig->param_count < 3);
6395 info = mono_register_jit_icall (func, name, sig, no_throw);
6397 emul_opcode_map [opcode] = info;
6401 decompose_foreach (MonoInst *tree, gpointer data)
6403 static MonoJitICallInfo *newarr_info = NULL;
6404 static MonoJitICallInfo *newarr_specific_info = NULL;
6405 MonoJitICallInfo *info;
6408 switch (tree->opcode) {
6410 MonoCompile *cfg = data;
6411 MonoInst *iargs [3];
6414 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
6415 g_assert (newarr_info);
6416 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
6417 g_assert (newarr_specific_info);
6420 if (cfg->opt & MONO_OPT_SHARED) {
6421 NEW_DOMAINCONST (cfg, iargs [0]);
6422 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
6423 iargs [2] = tree->inst_newa_len;
6428 MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
6430 NEW_VTABLECONST (cfg, iargs [0], vtable);
6431 iargs [1] = tree->inst_newa_len;
6433 info = newarr_specific_info;
6436 mono_emulate_opcode (cfg, tree, iargs, info);
6438 /* Need to decompose arguments after the the opcode is decomposed */
6439 for (i = 0; i < info->sig->param_count; ++i)
6440 dec_foreach (iargs [i], cfg);
6450 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
6452 switch (mono_burg_arity [tree->opcode]) {
6455 mono_inst_foreach (tree->inst_left, func, data);
6458 mono_inst_foreach (tree->inst_left, func, data);
6459 mono_inst_foreach (tree->inst_right, func, data);
6462 g_assert_not_reached ();
6469 mono_print_bb_code (MonoBasicBlock *bb) {
6471 MonoInst *c = bb->code;
6473 mono_print_tree (c);
6481 print_dfn (MonoCompile *cfg) {
6486 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
6488 for (i = 0; i < cfg->num_bblocks; ++i) {
6489 bb = cfg->bblocks [i];
6491 char* code1, *code2;
6492 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
6493 if (bb->last_ins->cil_code)
6494 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
6496 code2 = g_strdup ("");
6498 code1 [strlen (code1) - 1] = 0;
6499 code = g_strdup_printf ("%s -> %s", code1, code2);
6503 code = g_strdup ("\n");
6504 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
6506 MonoInst *c = bb->code;
6508 mono_print_tree (c);
6516 g_print ("\tprev:");
6517 for (j = 0; j < bb->in_count; ++j) {
6518 g_print (" BB%d", bb->in_bb [j]->block_num);
6520 g_print ("\t\tsucc:");
6521 for (j = 0; j < bb->out_count; ++j) {
6522 g_print (" BB%d", bb->out_bb [j]->block_num);
6524 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
6527 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
6530 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
6532 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
6540 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
6544 g_assert (bb->code);
6545 bb->last_ins->next = inst;
6546 bb->last_ins = inst;
6548 bb->last_ins = bb->code = inst;
6553 mono_destroy_compile (MonoCompile *cfg)
6555 //mono_mempool_stats (cfg->mempool);
6556 g_hash_table_destroy (cfg->bb_hash);
6557 mono_free_loop_info (cfg);
6559 mono_regstate_free (cfg->rs);
6561 g_hash_table_destroy (cfg->spvars);
6562 mono_mempool_destroy (cfg->mempool);
6563 g_list_free (cfg->ldstr_list);
6565 g_free (cfg->varinfo);
6571 mono_get_lmf_addr (void)
6573 MonoJitTlsData *jit_tls;
6575 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
6576 return &jit_tls->lmf;
6578 g_assert_not_reached ();
6583 * mono_thread_abort:
6584 * @obj: exception object
6586 * abort the thread, print exception information and stack trace
6589 mono_thread_abort (MonoObject *obj)
6591 /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
6593 /* handle_remove should be eventually called for this thread, too
6600 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
6602 MonoJitTlsData *jit_tls;
6605 jit_tls = TlsGetValue (mono_jit_tls_id);
6609 jit_tls = g_new0 (MonoJitTlsData, 1);
6611 TlsSetValue (mono_jit_tls_id, jit_tls);
6613 jit_tls->abort_func = abort_func;
6614 jit_tls->end_of_stack = stack_start;
6616 lmf = g_new0 (MonoLMF, 1);
6619 jit_tls->lmf = jit_tls->first_lmf = lmf;
6621 mono_arch_setup_jit_tls_data (jit_tls);
6627 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
6630 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
6631 thread = mono_thread_current ();
6633 thread->jit_data = jit_tls;
6636 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
6639 mono_thread_abort_dummy (MonoObject *obj)
6641 if (mono_thread_attach_aborted_cb)
6642 mono_thread_attach_aborted_cb (obj);
6644 mono_thread_abort (obj);
6648 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
6651 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
6652 thread = mono_thread_current ();
6654 thread->jit_data = jit_tls;
6658 mini_thread_cleanup (MonoThread *thread)
6660 MonoJitTlsData *jit_tls = thread->jit_data;
6663 mono_arch_free_jit_tls_data (jit_tls);
6664 g_free (jit_tls->first_lmf);
6666 thread->jit_data = NULL;
6671 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
6673 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
6677 ji->data.target = target;
6678 ji->next = cfg->patch_info;
6680 cfg->patch_info = ji;
6684 mono_remove_patch_info (MonoCompile *cfg, int ip)
6686 MonoJumpInfo **ji = &cfg->patch_info;
6689 if ((*ji)->ip.i == ip)
6692 ji = &((*ji)->next);
6697 mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors)
6699 unsigned char *ip = patch_info->ip.i + code;
6700 gconstpointer target = NULL;
6702 switch (patch_info->type) {
6703 case MONO_PATCH_INFO_BB:
6704 target = patch_info->data.bb->native_offset + code;
6706 case MONO_PATCH_INFO_ABS:
6707 target = patch_info->data.target;
6709 case MONO_PATCH_INFO_LABEL:
6710 target = patch_info->data.inst->inst_c0 + code;
6712 case MONO_PATCH_INFO_IP:
6715 case MONO_PATCH_INFO_METHOD_REL:
6716 target = code + patch_info->data.offset;
6718 case MONO_PATCH_INFO_INTERNAL_METHOD: {
6719 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
6721 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
6722 g_assert_not_reached ();
6724 target = mono_icall_get_wrapper (mi);
6727 case MONO_PATCH_INFO_METHOD_JUMP: {
6730 /* get the trampoline to the method from the domain */
6731 target = mono_create_jump_trampoline (domain, patch_info->data.method, TRUE);
6732 if (!domain->jump_target_hash)
6733 domain->jump_target_hash = g_hash_table_new (NULL, NULL);
6734 list = g_hash_table_lookup (domain->jump_target_hash, patch_info->data.method);
6735 list = g_slist_prepend (list, ip);
6736 g_hash_table_insert (domain->jump_target_hash, patch_info->data.method, list);
6739 case MONO_PATCH_INFO_METHOD:
6740 if (patch_info->data.method == method) {
6743 /* get the trampoline to the method from the domain */
6744 target = mono_arch_create_jit_trampoline (patch_info->data.method);
6746 case MONO_PATCH_INFO_SWITCH: {
6747 gpointer *jump_table;
6750 mono_domain_lock (domain);
6751 jump_table = mono_code_manager_reserve (domain->code_mp, sizeof (gpointer) * patch_info->table_size);
6752 mono_domain_unlock (domain);
6754 for (i = 0; i < patch_info->table_size; i++) {
6755 jump_table [i] = code + (int)patch_info->data.table [i];
6757 target = jump_table;
6760 case MONO_PATCH_INFO_METHODCONST:
6761 case MONO_PATCH_INFO_CLASS:
6762 case MONO_PATCH_INFO_IMAGE:
6763 case MONO_PATCH_INFO_FIELD:
6764 target = patch_info->data.target;
6766 case MONO_PATCH_INFO_IID:
6767 mono_class_init (patch_info->data.klass);
6768 target = (gpointer)patch_info->data.klass->interface_id;
6770 case MONO_PATCH_INFO_VTABLE:
6771 target = mono_class_vtable (domain, patch_info->data.klass);
6773 case MONO_PATCH_INFO_CLASS_INIT:
6774 target = mono_create_class_init_trampoline (mono_class_vtable (domain, patch_info->data.klass));
6776 case MONO_PATCH_INFO_SFLDA: {
6777 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
6778 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (vtable->klass, method))
6779 /* Done by the generated code */
6783 mono_runtime_class_init (vtable);
6785 target = (char*)vtable->data + patch_info->data.field->offset;
6788 case MONO_PATCH_INFO_R4:
6789 case MONO_PATCH_INFO_R8:
6790 target = patch_info->data.target;
6792 case MONO_PATCH_INFO_EXC_NAME:
6793 target = patch_info->data.name;
6795 case MONO_PATCH_INFO_LDSTR:
6797 mono_ldstr (domain, patch_info->data.token->image,
6798 mono_metadata_token_index (patch_info->data.token->token));
6800 case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
6802 MonoClass *handle_class;
6804 handle = mono_ldtoken (patch_info->data.token->image,
6805 patch_info->data.token->token, &handle_class, NULL);
6806 mono_class_init (handle_class);
6807 mono_class_init (mono_class_from_mono_type (handle));
6810 mono_type_get_object (domain, handle);
6813 case MONO_PATCH_INFO_LDTOKEN: {
6815 MonoClass *handle_class;
6817 handle = mono_ldtoken (patch_info->data.token->image,
6818 patch_info->data.token->token, &handle_class, NULL);
6819 mono_class_init (handle_class);
6824 case MONO_PATCH_INFO_BB_OVF:
6825 case MONO_PATCH_INFO_EXC_OVF:
6828 g_assert_not_reached ();
6831 return (gpointer)target;
6835 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
6836 MonoJitICallInfo *info;
6838 decompose_foreach (tree, cfg);
6840 switch (mono_burg_arity [tree->opcode]) {
6843 dec_foreach (tree->inst_left, cfg);
6845 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6846 MonoInst *iargs [2];
6848 iargs [0] = tree->inst_left;
6850 mono_emulate_opcode (cfg, tree, iargs, info);
6856 #ifdef MONO_ARCH_BIGMUL_INTRINS
6857 if (tree->opcode == OP_LMUL
6858 && (cfg->opt & MONO_OPT_INTRINS)
6859 && (tree->inst_left->opcode == CEE_CONV_I8
6860 || tree->inst_left->opcode == CEE_CONV_U8)
6861 && tree->inst_left->inst_left->type == STACK_I4
6862 && (tree->inst_right->opcode == CEE_CONV_I8
6863 || tree->inst_right->opcode == CEE_CONV_U8)
6864 && tree->inst_right->inst_left->type == STACK_I4) {
6865 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
6866 tree->inst_left = tree->inst_left->inst_left;
6867 tree->inst_right = tree->inst_right->inst_left;
6868 dec_foreach (tree, cfg);
6871 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6872 MonoInst *iargs [2];
6874 iargs [0] = tree->inst_i0;
6875 iargs [1] = tree->inst_i1;
6877 mono_emulate_opcode (cfg, tree, iargs, info);
6879 dec_foreach (iargs [0], cfg);
6880 dec_foreach (iargs [1], cfg);
6883 dec_foreach (tree->inst_left, cfg);
6884 dec_foreach (tree->inst_right, cfg);
6888 g_assert_not_reached ();
6893 decompose_pass (MonoCompile *cfg) {
6896 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6899 cfg->prev_ins = NULL;
6900 for (tree = cfg->cbb->code; tree; tree = tree->next) {
6901 dec_foreach (tree, cfg);
6902 cfg->prev_ins = tree;
6908 nullify_basic_block (MonoBasicBlock *bb)
6915 bb->code = bb->last_ins = NULL;
6916 bb->cil_code = NULL;
6920 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6924 for (i = 0; i < bb->out_count; i++) {
6925 MonoBasicBlock *ob = bb->out_bb [i];
6928 if (bb->out_count > 1) {
6929 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
6933 bb->out_bb [i] = repl;
6940 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6944 for (i = 0; i < bb->in_count; i++) {
6945 MonoBasicBlock *ib = bb->in_bb [i];
6948 if (bb->in_count > 1) {
6949 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
6953 bb->in_bb [i] = repl;
6960 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6964 for (i = 0; i < bb->out_count; i++) {
6965 MonoBasicBlock *ob = bb->out_bb [i];
6966 for (j = 0; j < ob->in_count; j++) {
6967 if (ob->in_bb [j] == orig) {
6968 ob->in_bb [j] = repl;
6977 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
6979 bb->out_count = bbn->out_count;
6980 bb->out_bb = bbn->out_bb;
6982 replace_basic_block (bb, bbn, bb);
6986 bb->last_ins->next = bbn->code;
6987 bb->last_ins = bbn->last_ins;
6990 bb->code = bbn->code;
6991 bb->last_ins = bbn->last_ins;
6993 bb->next_bb = bbn->next_bb;
6994 nullify_basic_block (bbn);
6998 * Optimizes the branches on the Control Flow Graph
7002 optimize_branches (MonoCompile *cfg)
7004 int i, changed = FALSE;
7005 MonoBasicBlock *bb, *bbn;
7006 guint32 niterations;
7009 * Some crazy loops could cause the code below to go into an infinite
7010 * loop, see bug #53003 for an example. To prevent this, we put an upper
7011 * bound on the number of iterations.
7018 /* we skip the entry block (exit is handled specially instead ) */
7019 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
7021 /* dont touch code inside exception clauses */
7022 if (bb->region != -1)
7025 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
7026 if (cfg->verbose_level > 2)
7027 g_print ("nullify block triggered %d\n", bbn->block_num);
7029 bb->next_bb = bbn->next_bb;
7031 for (i = 0; i < bbn->out_count; i++)
7032 replace_in_block (bbn->out_bb [i], bbn, NULL);
7034 nullify_basic_block (bbn);
7038 if (bb->out_count == 1) {
7039 bbn = bb->out_bb [0];
7041 /* conditional branches where true and false targets are the same can be also replaced with CEE_BR */
7042 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
7043 bb->last_ins->opcode = CEE_BR;
7044 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
7046 if (cfg->verbose_level > 2)
7047 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
7050 if (bb->region == bbn->region && bb->next_bb == bbn) {
7051 /* the block are in sequence anyway ... */
7053 /* branches to the following block can be removed */
7054 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
7055 bb->last_ins->opcode = CEE_NOP;
7057 if (cfg->verbose_level > 2)
7058 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
7061 if (bbn->in_count == 1) {
7063 if (bbn != cfg->bb_exit) {
7064 if (cfg->verbose_level > 2)
7065 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
7066 merge_basic_blocks (bb, bbn);
7070 //mono_print_bb_code (bb);
7075 } while (changed && (niterations > 0));
7082 /* we skip the entry block (exit is handled specially instead ) */
7083 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
7085 /* dont touch code inside exception clauses */
7086 if (bb->region != -1)
7089 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
7090 if (cfg->verbose_level > 2) {
7091 g_print ("nullify block triggered %d\n", bbn->block_num);
7093 bb->next_bb = bbn->next_bb;
7095 for (i = 0; i < bbn->out_count; i++)
7096 replace_in_block (bbn->out_bb [i], bbn, NULL);
7098 nullify_basic_block (bbn);
7104 if (bb->out_count == 1) {
7105 bbn = bb->out_bb [0];
7107 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
7108 bbn = bb->last_ins->inst_target_bb;
7109 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7110 bbn->code->inst_target_bb->region == bb->region) {
7112 if (cfg->verbose_level > 2)
7113 g_print ("in %s branch to branch triggered %d -> %d\n", cfg->method->name,
7114 bb->block_num, bbn->block_num);
7116 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
7117 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
7122 } else if (bb->out_count == 2) {
7123 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
7124 bbn = bb->last_ins->inst_true_bb;
7125 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7126 bbn->code->inst_target_bb->region == bb->region) {
7127 if (cfg->verbose_level > 2)
7128 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
7129 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
7132 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
7134 replace_in_block (bbn, bb, NULL);
7136 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
7137 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7139 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7145 bbn = bb->last_ins->inst_false_bb;
7146 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7147 bbn->code->inst_target_bb->region == bb->region) {
7148 if (cfg->verbose_level > 2)
7149 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
7150 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
7153 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
7155 replace_in_block (bbn, bb, NULL);
7157 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
7158 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7160 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7168 } while (changed && (niterations > 0));
7173 mono_compile_create_vars (MonoCompile *cfg)
7175 MonoMethodSignature *sig;
7176 MonoMethodHeader *header;
7179 header = ((MonoMethodNormal *)cfg->method)->header;
7181 sig = cfg->method->signature;
7183 if (!MONO_TYPE_IS_VOID (sig->ret)) {
7184 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
7185 cfg->ret->opcode = OP_RETARG;
7186 cfg->ret->inst_vtype = sig->ret;
7187 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
7189 if (cfg->verbose_level > 2)
7190 g_print ("creating vars\n");
7193 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
7195 for (i = 0; i < sig->param_count; ++i)
7196 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
7198 cfg->locals_start = cfg->num_varinfo;
7200 if (cfg->verbose_level > 2)
7201 g_print ("creating locals\n");
7202 for (i = 0; i < header->num_locals; ++i)
7203 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
7204 if (cfg->verbose_level > 2)
7205 g_print ("locals done\n");
7209 mono_print_code (MonoCompile *cfg)
7213 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7214 MonoInst *tree = bb->code;
7219 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
7221 for (; tree; tree = tree->next) {
7222 mono_print_tree (tree);
7227 bb->last_ins->next = NULL;
7231 extern const char * const mono_burg_rule_string [];
7234 emit_state (MonoCompile *cfg, MBState *state, int goal)
7237 int ern = mono_burg_rule (state, goal);
7238 const guint16 *nts = mono_burg_nts [ern];
7241 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
7245 // state->reg1 = state->reg2; /* chain rule */
7247 state->reg1 = mono_regstate_next_int (cfg->rs);
7248 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
7251 state->reg1 = mono_regstate_next_int (cfg->rs);
7252 state->reg2 = mono_regstate_next_int (cfg->rs);
7255 state->reg1 = mono_regstate_next_float (cfg->rs);
7262 mono_burg_kids (state, ern, kids);
7264 emit_state (cfg, kids [0], nts [0]);
7266 emit_state (cfg, kids [1], nts [1]);
7268 g_assert (!nts [3]);
7269 emit_state (cfg, kids [2], nts [2]);
7274 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
7275 if ((emit = mono_burg_func [ern]))
7276 emit (state, state->tree, cfg);
7279 #define DEBUG_SELECTION
7282 mini_select_instructions (MonoCompile *cfg)
7284 static const int reverse_map [] = {
7285 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
7286 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
7288 static const int reverse_fmap [] = {
7289 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
7290 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
7292 static const int reverse_lmap [] = {
7293 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
7294 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
7299 cfg->state_pool = mono_mempool_new ();
7300 cfg->rs = mono_regstate_new ();
7302 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7303 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode) &&
7304 bb->next_bb != bb->last_ins->inst_false_bb) {
7306 if (bb->next_bb == bb->last_ins->inst_true_bb) {
7307 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
7308 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
7309 bb->last_ins->inst_false_bb = tmp;
7311 if (bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
7312 bb->last_ins->opcode = reverse_map [bb->last_ins->opcode - CEE_BEQ];
7313 } else if (bb->last_ins->opcode >= OP_FBEQ && bb->last_ins->opcode <= OP_FBLT_UN) {
7314 bb->last_ins->opcode = reverse_fmap [bb->last_ins->opcode - OP_FBEQ];
7315 } else if (bb->last_ins->opcode >= OP_LBEQ && bb->last_ins->opcode <= OP_LBLT_UN) {
7316 bb->last_ins->opcode = reverse_lmap [bb->last_ins->opcode - OP_LBEQ];
7319 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
7320 inst->opcode = CEE_BR;
7321 inst->inst_target_bb = bb->last_ins->inst_false_bb;
7322 mono_bblock_add_inst (bb, inst);
7327 #ifdef DEBUG_SELECTION
7328 if (cfg->verbose_level >= 4) {
7329 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7330 MonoInst *tree = bb->code;
7331 g_print ("DUMP BLOCK %d:\n", bb->block_num);
7334 for (; tree; tree = tree->next) {
7335 mono_print_tree (tree);
7342 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7343 MonoInst *tree = bb->code, *next;
7349 bb->last_ins = NULL;
7352 mono_regstate_reset (cfg->rs);
7354 #ifdef DEBUG_SELECTION
7355 if (cfg->verbose_level >= 3)
7356 g_print ("LABEL BLOCK %d:\n", bb->block_num);
7358 for (; tree; tree = next) {
7360 #ifdef DEBUG_SELECTION
7361 if (cfg->verbose_level >= 3) {
7362 mono_print_tree (tree);
7367 if (!(mbstate = mono_burg_label (tree, cfg))) {
7368 g_warning ("unable to label tree %p", tree);
7369 mono_print_tree (tree);
7371 g_assert_not_reached ();
7373 emit_state (cfg, mbstate, MB_NTERM_stmt);
7375 bb->max_ireg = cfg->rs->next_vireg;
7376 bb->max_freg = cfg->rs->next_vfreg;
7379 bb->last_ins->next = NULL;
7381 mono_mempool_empty (cfg->state_pool);
7383 mono_mempool_destroy (cfg->state_pool);
7387 mono_codegen (MonoCompile *cfg)
7389 MonoJumpInfo *patch_info;
7391 int i, max_epilog_size;
7394 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7395 cfg->spill_count = 0;
7396 /* we reuse dfn here */
7397 /* bb->dfn = bb_count++; */
7398 mono_arch_local_regalloc (cfg, bb);
7401 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
7402 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
7404 code = mono_arch_emit_prolog (cfg);
7406 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7407 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
7409 cfg->code_len = code - cfg->native_code;
7410 cfg->prolog_end = cfg->code_len;
7412 mono_debug_open_method (cfg);
7414 /* emit code all basic blocks */
7415 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7416 bb->native_offset = cfg->code_len;
7417 mono_arch_output_basic_block (cfg, bb);
7419 cfg->bb_exit->native_offset = cfg->code_len;
7421 code = cfg->native_code + cfg->code_len;
7423 max_epilog_size = mono_arch_max_epilog_size (cfg);
7425 /* we always allocate code in cfg->domain->code_mp to increase locality */
7426 cfg->code_size = cfg->code_len + max_epilog_size;
7427 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
7429 mono_domain_lock (cfg->domain);
7430 code = mono_code_manager_reserve (cfg->domain->code_mp, cfg->code_size);
7431 mono_domain_unlock (cfg->domain);
7433 memcpy (code, cfg->native_code, cfg->code_len);
7434 g_free (cfg->native_code);
7435 cfg->native_code = code;
7436 code = cfg->native_code + cfg->code_len;
7438 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
7440 cfg->epilog_begin = cfg->code_len;
7442 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7443 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
7445 cfg->code_len = code - cfg->native_code;
7447 mono_arch_emit_epilog (cfg);
7449 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7450 switch (patch_info->type) {
7451 case MONO_PATCH_INFO_ABS: {
7452 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
7454 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
7455 if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) &&
7456 strstr (cfg->method->name, info->name))
7458 * This is an icall wrapper, and this is a call to the
7463 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
7464 patch_info->data.name = info->name;
7468 MonoVTable *vtable = mono_find_class_init_trampoline_by_addr (patch_info->data.target);
7470 patch_info->type = MONO_PATCH_INFO_CLASS_INIT;
7471 patch_info->data.klass = vtable->klass;
7476 case MONO_PATCH_INFO_SWITCH: {
7478 mono_domain_lock (cfg->domain);
7479 table = mono_code_manager_reserve (cfg->domain->code_mp, sizeof (gpointer) * patch_info->table_size);
7480 mono_domain_unlock (cfg->domain);
7482 patch_info->ip.i = patch_info->ip.label->inst_c0;
7483 for (i = 0; i < patch_info->table_size; i++) {
7484 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
7486 patch_info->data.target = table;
7495 if (cfg->verbose_level > 0)
7496 g_print ("Method %s emitted at %p to %p [%s]\n",
7497 mono_method_full_name (cfg->method, TRUE),
7498 cfg->native_code, cfg->native_code + cfg->code_len, cfg->domain->friendly_name);
7500 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info, cfg->run_cctors);
7502 mono_domain_lock (cfg->domain);
7503 mono_code_manager_commit (cfg->domain->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
7504 mono_domain_unlock (cfg->domain);
7506 mono_arch_flush_icache (cfg->native_code, cfg->code_len);
7508 mono_debug_close_method (cfg);
7512 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
7517 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
7518 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
7520 if (cp->opcode == OP_ICONST) {
7521 if (cfg->opt & MONO_OPT_CONSPROP) {
7522 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
7526 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
7527 if (cfg->opt & MONO_OPT_COPYPROP) {
7528 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
7534 arity = mono_burg_arity [tree->opcode];
7537 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
7538 if (cfg->opt & MONO_OPT_CFOLD)
7539 mono_constant_fold_inst (tree, NULL);
7540 /* The opcode may have changed */
7541 if (mono_burg_arity [tree->opcode] > 1) {
7542 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
7543 if (cfg->opt & MONO_OPT_CFOLD)
7544 mono_constant_fold_inst (tree, NULL);
7546 mono_constant_fold_inst (tree, NULL);
7552 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
7556 switch (tree->opcode) {
7566 if (tree->ssa_op == MONO_SSA_NOP) {
7567 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7584 case OP_VOIDCALL_REG:
7585 case OP_VOIDCALLVIRT:
7587 MonoCallInst *call = (MonoCallInst *)tree;
7588 MonoMethodSignature *sig = call->signature;
7589 int i, byref = FALSE;
7591 for (i = 0; i < sig->param_count; i++) {
7592 if (sig->params [i]->byref) {
7599 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7607 arity = mono_burg_arity [tree->opcode];
7613 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7616 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7617 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
7620 g_assert_not_reached ();
7625 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
7627 MonoInst *tree = bb->code;
7633 for (; tree; tree = tree->next) {
7635 mono_cprop_copy_values (cfg, tree, acp);
7637 mono_cprop_invalidate_values (tree, acp, acp_size);
7639 if (tree->ssa_op == MONO_SSA_STORE &&
7640 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
7641 MonoInst *i1 = tree->inst_i1;
7643 acp [tree->inst_i0->inst_c0] = NULL;
7645 for (i = 0; i < acp_size; i++) {
7646 if (acp [i] && acp [i]->opcode != OP_ICONST &&
7647 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
7652 if (i1->opcode == OP_ICONST) {
7653 acp [tree->inst_i0->inst_c0] = i1;
7654 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
7656 if (i1->ssa_op == MONO_SSA_LOAD &&
7657 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
7658 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
7659 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
7660 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
7665 if (tree->opcode == CEE_BEQ) {
7666 g_assert (tree->inst_i0->opcode == OP_COMPARE);
7667 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
7668 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
7670 tree->opcode = CEE_BR;
7671 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
7672 tree->inst_target_bb = tree->inst_true_bb;
7674 tree->inst_target_bb = tree->inst_false_bb;
7683 mono_local_cprop (MonoCompile *cfg)
7688 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
7690 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7691 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
7692 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
7697 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, int parts)
7699 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
7700 guint8 *ip = (guint8 *)header->code;
7703 int dfn = 0, i, code_size_ratio;
7705 mono_jit_stats.methods_compiled++;
7706 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
7707 mono_profiler_method_jit (method);
7709 cfg = g_new0 (MonoCompile, 1);
7710 cfg->method = method;
7711 cfg->mempool = mono_mempool_new ();
7713 cfg->prof_options = mono_profiler_get_events ();
7714 cfg->run_cctors = run_cctors;
7715 cfg->bb_hash = g_hash_table_new (NULL, NULL);
7716 cfg->domain = domain;
7717 cfg->verbose_level = mini_verbose;
7718 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
7719 ((MonoMethodNormal *)method)->header->max_stack);
7721 if (cfg->verbose_level > 2)
7722 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
7725 * create MonoInst* which represents arguments and local variables
7727 mono_compile_create_vars (cfg);
7729 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
7730 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7731 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
7732 mono_destroy_compile (cfg);
7736 mono_jit_stats.basic_blocks += cfg->num_bblocks;
7737 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
7739 if (cfg->num_varinfo > 2000) {
7741 * we disable some optimizations if there are too many variables
7742 * because JIT time may become too expensive. The actual number needs
7743 * to be tweaked and eventually the non-linear algorithms should be fixed.
7745 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
7746 cfg->disable_ssa = TRUE;
7748 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
7750 /* Depth-first ordering on basic blocks */
7751 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
7753 if (cfg->opt & MONO_OPT_BRANCH)
7754 optimize_branches (cfg);
7756 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
7757 if (cfg->num_bblocks != dfn + 1) {
7760 cfg->num_bblocks = dfn + 1;
7762 if (!header->clauses) {
7763 /* remove unreachable code, because the code in them may be
7764 * inconsistent (access to dead variables for example) */
7765 for (bb = cfg->bb_entry; bb;) {
7766 MonoBasicBlock *bbn = bb->next_bb;
7768 if (bbn && bbn->region == -1 && !bbn->dfn) {
7769 if (cfg->verbose_level > 1)
7770 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
7771 bb->next_bb = bbn->next_bb;
7772 nullify_basic_block (bbn);
7780 if (cfg->opt & MONO_OPT_LOOP) {
7781 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
7782 mono_compute_natural_loops (cfg);
7785 /* after method_to_ir */
7789 //#define DEBUGSSA "logic_run"
7790 #define DEBUGSSA_CLASS "Tests"
7793 if (!header->num_clauses && !cfg->disable_ssa) {
7794 mono_local_cprop (cfg);
7795 mono_ssa_compute (cfg);
7799 /* fixme: add all optimizations which requires SSA */
7800 if (cfg->opt & (MONO_OPT_DEADCE | MONO_OPT_ABCREM)) {
7801 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
7802 mono_local_cprop (cfg);
7803 mono_ssa_compute (cfg);
7805 if (cfg->verbose_level >= 2) {
7812 /* after SSA translation */
7816 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
7817 if (cfg->comp_done & MONO_COMP_SSA) {
7818 mono_ssa_cprop (cfg);
7820 mono_local_cprop (cfg);
7824 if (cfg->comp_done & MONO_COMP_SSA) {
7825 mono_ssa_deadce (cfg);
7827 //mono_ssa_strength_reduction (cfg);
7829 if ((cfg->flags & MONO_CFG_HAS_LDELEMA) && (cfg->opt & MONO_OPT_ABCREM))
7830 mono_perform_abc_removal (cfg);
7832 mono_ssa_remove (cfg);
7834 if (cfg->opt & MONO_OPT_BRANCH)
7835 optimize_branches (cfg);
7838 /* after SSA removal */
7842 decompose_pass (cfg);
7844 if (cfg->opt & MONO_OPT_LINEARS) {
7847 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
7848 cfg->comp_done &= ~MONO_COMP_LIVENESS;
7849 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
7850 mono_analyze_liveness (cfg);
7852 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
7853 regs = mono_arch_get_global_int_regs (cfg);
7854 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
7858 //mono_print_code (cfg);
7862 /* variables are allocated after decompose, since decompose could create temps */
7863 mono_arch_allocate_vars (cfg);
7865 if (cfg->opt & MONO_OPT_CFOLD)
7866 mono_constant_fold (cfg);
7868 mini_select_instructions (cfg);
7871 if (cfg->verbose_level >= 2) {
7872 char *id = mono_method_full_name (cfg->method, FALSE);
7873 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
7877 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
7879 jinfo = g_new0 (MonoJitInfo, 1);
7880 jinfo->method = method;
7881 jinfo->code_start = cfg->native_code;
7882 jinfo->code_size = cfg->code_len;
7883 jinfo->used_regs = cfg->used_int_regs;
7884 jinfo->domain_neutral = (cfg->opt & MONO_OPT_SHARED) != 0;
7886 if (header->num_clauses) {
7889 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
7890 jinfo->num_clauses = header->num_clauses;
7891 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
7892 sizeof (MonoJitExceptionInfo) * header->num_clauses);
7894 for (i = 0; i < header->num_clauses; i++) {
7895 MonoExceptionClause *ec = &header->clauses [i];
7896 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
7897 MonoBasicBlock *tblock;
7899 ei->flags = ec->flags;
7901 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7902 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->token_or_filter);
7904 ei->data.filter = cfg->native_code + tblock->native_offset;
7906 ei->data.token = ec->token_or_filter;
7909 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
7911 ei->try_start = cfg->native_code + tblock->native_offset;
7912 g_assert (tblock->native_offset);
7913 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
7915 ei->try_end = cfg->native_code + tblock->native_offset;
7916 g_assert (tblock->native_offset);
7917 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
7919 ei->handler_start = cfg->native_code + tblock->native_offset;
7923 cfg->jit_info = jinfo;
7925 mono_jit_info_table_add (cfg->domain, jinfo);
7927 /* collect statistics */
7928 mono_jit_stats.allocated_code_size += cfg->code_len;
7929 code_size_ratio = cfg->code_len;
7930 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
7931 mono_jit_stats.biggest_method_size = code_size_ratio;
7932 mono_jit_stats.biggest_method = method;
7934 code_size_ratio = (code_size_ratio * 100) / ((MonoMethodNormal *)method)->header->code_size;
7935 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
7936 mono_jit_stats.max_code_size_ratio = code_size_ratio;
7937 mono_jit_stats.max_ratio_method = method;
7939 mono_jit_stats.native_code_size += cfg->code_len;
7941 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7942 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
7948 mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain)
7951 GHashTable *jit_code_hash;
7952 gpointer code = NULL;
7958 jit_code_hash = target_domain->jit_code_hash;
7960 #ifdef MONO_USE_AOT_COMPILER
7961 if (!mono_compile_aot && (opt & MONO_OPT_AOT)) {
7963 MonoDomain *domain = mono_domain_get ();
7965 mono_domain_lock (domain);
7967 mono_class_init (method->klass);
7968 if ((info = mono_aot_get_method (domain, method))) {
7969 g_hash_table_insert (domain->jit_code_hash, method, info);
7970 mono_domain_unlock (domain);
7971 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
7972 return info->code_start;
7975 mono_domain_unlock (domain);
7979 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
7980 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
7983 if (!method->addr) {
7984 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
7985 method->addr = mono_lookup_internal_call (method);
7987 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
7988 mono_lookup_pinvoke_call (method, NULL, NULL);
7990 #ifdef MONO_USE_EXC_TABLES
7991 if (mono_method_blittable (method)) {
7992 return method->addr;
7995 nm = mono_marshal_get_native_wrapper (method);
7996 return mono_compile_method (nm);
7998 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
7999 //mono_debug_add_wrapper (method, nm);
8000 #ifdef MONO_USE_EXC_TABLES
8003 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
8004 const char *name = method->name;
8007 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
8008 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
8009 /* FIXME: uhm, we need a wrapper to handle exceptions? */
8010 return (gpointer)mono_delegate_ctor;
8011 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
8012 nm = mono_marshal_get_delegate_invoke (method);
8013 return mono_jit_compile_method (nm);
8014 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
8015 nm = mono_marshal_get_delegate_begin_invoke (method);
8016 return mono_jit_compile_method (nm);
8017 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
8018 nm = mono_marshal_get_delegate_end_invoke (method);
8019 return mono_jit_compile_method (nm);
8025 cfg = mini_method_compile (method, opt, target_domain, TRUE, 0);
8027 mono_domain_lock (target_domain);
8029 /* Check if some other thread already did the job. In this case, we can
8030 discard the code this thread generated. */
8032 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
8033 /* We can't use a domain specific method in another domain */
8034 if ((target_domain == mono_domain_get ()) || info->domain_neutral) {
8035 code = info->code_start;
8036 // printf("Discarding code for method %s\n", method->name);
8041 g_hash_table_insert (jit_code_hash, method, cfg->jit_info);
8042 code = cfg->native_code;
8045 mono_destroy_compile (cfg);
8047 if (target_domain->jump_target_hash) {
8048 MonoJumpInfo patch_info;
8050 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
8052 patch_info.next = NULL;
8053 patch_info.ip.i = 0;
8054 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
8055 patch_info.data.method = method;
8056 g_hash_table_remove (target_domain->jump_target_hash, method);
8058 for (tmp = list; tmp; tmp = tmp->next)
8059 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info, TRUE);
8060 g_slist_free (list);
8063 mono_domain_unlock (target_domain);
8065 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
8070 mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt)
8072 /* FIXME: later copy the code from mono */
8073 MonoDomain *target_domain, *domain = mono_domain_get ();
8077 if (default_opt & MONO_OPT_SHARED)
8078 target_domain = mono_root_domain;
8080 target_domain = domain;
8082 mono_domain_lock (target_domain);
8084 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
8085 /* We can't use a domain specific method in another domain */
8086 if (! ((domain != target_domain) && !info->domain_neutral)) {
8087 mono_domain_unlock (target_domain);
8088 mono_jit_stats.methods_lookups++;
8089 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
8090 return info->code_start;
8094 mono_domain_unlock (target_domain);
8095 p = mono_jit_compile_method_inner (method, target_domain);
8100 mono_jit_compile_method (MonoMethod *method)
8102 return mono_jit_compile_method_with_opt (method, default_opt);
8106 mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method)
8108 MonoDomain *target_domain;
8111 if (default_opt & MONO_OPT_SHARED)
8112 target_domain = mono_root_domain;
8114 target_domain = domain;
8116 mono_domain_lock (target_domain);
8118 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
8119 /* We can't use a domain specific method in another domain */
8120 if (! ((domain != target_domain) && !info->domain_neutral)) {
8121 mono_domain_unlock (target_domain);
8122 mono_jit_stats.methods_lookups++;
8123 return info->code_start;
8127 mono_domain_unlock (target_domain);
8133 * mono_jit_runtime_invoke:
8134 * @method: the method to invoke
8135 * @obj: this pointer
8136 * @params: array of parameter values.
8137 * @exc: used to catch exceptions objects
8140 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
8143 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc, void* compiled_method);
8144 void* compiled_method;
8146 if (obj == NULL && !(method->flags & METHOD_ATTRIBUTE_STATIC) && !method->string_ctor && (method->wrapper_type == 0)) {
8147 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
8151 invoke = mono_marshal_get_runtime_invoke (method);
8152 runtime_invoke = mono_jit_compile_method (invoke);
8153 compiled_method = mono_jit_compile_method (method);
8154 return runtime_invoke (obj, params, exc, compiled_method);
8157 #ifdef PLATFORM_WIN32
8158 #define GET_CONTEXT \
8159 struct sigcontext *ctx = (struct sigcontext*)_dummy;
8162 #define GET_CONTEXT \
8163 void *ctx = context;
8164 #elif defined(__ppc__) || defined (__powerpc__) || defined (__s390__)
8165 #define GET_CONTEXT \
8166 void *ctx = context;
8168 #define GET_CONTEXT \
8169 void **_p = (void **)&_dummy; \
8170 struct sigcontext *ctx = (struct sigcontext *)++_p;
8174 #ifdef MONO_ARCH_USE_SIGACTION
8175 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, siginfo_t *info, void *context)
8177 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy)
8181 SIG_HANDLER_SIGNATURE (sigfpe_signal_handler)
8186 exc = mono_get_exception_divide_by_zero ();
8188 mono_arch_handle_exception (ctx, exc, FALSE);
8192 SIG_HANDLER_SIGNATURE (sigill_signal_handler)
8196 exc = mono_get_exception_execution_engine ("SIGILL");
8198 mono_arch_handle_exception (ctx, exc, FALSE);
8201 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8204 sigsegv_signal_handler (int _dummy, siginfo_t *info, void *context)
8207 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
8208 struct sigcontext *ctx = (struct sigcontext *)&(((ucontext_t*)context)->uc_mcontext);
8210 /* Can't allocate memory using Boehm GC on altstack */
8211 if (jit_tls->stack_size &&
8212 ((guint8*)info->si_addr >= (guint8*)jit_tls->end_of_stack - jit_tls->stack_size) &&
8213 ((guint8*)info->si_addr < (guint8*)jit_tls->end_of_stack))
8214 exc = mono_domain_get ()->stack_overflow_ex;
8216 exc = mono_domain_get ()->null_reference_ex;
8218 mono_arch_handle_exception (ctx, exc, FALSE);
8224 SIG_HANDLER_SIGNATURE (sigsegv_signal_handler)
8228 mono_arch_handle_exception (ctx, NULL, FALSE);
8234 SIG_HANDLER_SIGNATURE (sigusr1_signal_handler)
8236 gboolean running_managed;
8241 running_managed = (mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx)) != NULL);
8243 exc = mono_thread_request_interruption (running_managed);
8246 mono_arch_handle_exception (ctx, exc, FALSE);
8250 SIG_HANDLER_SIGNATURE (sigquit_signal_handler)
8255 exc = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
8257 mono_arch_handle_exception (ctx, exc, FALSE);
8261 SIG_HANDLER_SIGNATURE (sigint_signal_handler)
8266 exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
8268 mono_arch_handle_exception (ctx, exc, FALSE);
8271 #ifndef PLATFORM_WIN32
8273 add_signal_handler (int signo, gpointer handler)
8275 struct sigaction sa;
8277 #ifdef MONO_ARCH_USE_SIGACTION
8278 sa.sa_sigaction = handler;
8279 sigemptyset (&sa.sa_mask);
8280 sa.sa_flags = SA_SIGINFO;
8282 sa.sa_handler = handler;
8283 sigemptyset (&sa.sa_mask);
8286 g_assert (sigaction (signo, &sa, NULL) != -1);
8291 mono_runtime_install_handlers (void)
8293 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8294 struct sigaction sa;
8297 #ifdef PLATFORM_WIN32
8299 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
8300 win32_seh_set_handler(SIGILL, sigill_signal_handler);
8301 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
8302 if (getenv ("MONO_DEBUG"))
8303 win32_seh_set_handler(SIGINT, sigint_signal_handler);
8304 #else /* !PLATFORM_WIN32 */
8306 /* libpthreads has its own implementation of sigaction(),
8307 * but it seems to work well with our current exception
8308 * handlers. If not we must call syscall directly instead
8311 if (getenv ("MONO_DEBUG")) {
8312 add_signal_handler (SIGINT, sigint_signal_handler);
8315 add_signal_handler (SIGFPE, sigfpe_signal_handler);
8316 add_signal_handler (SIGQUIT, sigquit_signal_handler);
8317 add_signal_handler (SIGILL, sigill_signal_handler);
8318 add_signal_handler (SIGBUS, sigsegv_signal_handler);
8319 add_signal_handler (mono_thread_get_abort_signal (), sigusr1_signal_handler);
8322 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8323 sa.sa_sigaction = sigsegv_signal_handler;
8324 sigemptyset (&sa.sa_mask);
8325 sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
8326 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
8328 add_signal_handler (SIGSEGV, sigsegv_signal_handler);
8331 #endif /* PLATFORM_WIN32 */
8334 /* mono_jit_create_remoting_trampoline:
8335 * @method: pointer to the method info
8337 * Creates a trampoline which calls the remoting functions. This
8338 * is used in the vtable of transparent proxies.
8340 * Returns: a pointer to the newly created code
8343 mono_jit_create_remoting_trampoline (MonoMethod *method)
8346 guint8 *addr = NULL;
8348 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
8349 (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
8350 nm = mono_marshal_get_remoting_invoke (method);
8351 addr = mono_compile_method (nm);
8353 addr = mono_compile_method (method);
8359 mini_init (const char *filename)
8363 mono_arch_cpu_init ();
8365 if (!g_thread_supported ())
8366 g_thread_init (NULL);
8368 mono_jit_tls_id = TlsAlloc ();
8369 setup_jit_tls_data ((gpointer)-1, mono_thread_abort);
8371 InitializeCriticalSection (&trampoline_hash_mutex);
8375 if (default_opt & MONO_OPT_AOT)
8378 mono_runtime_install_handlers ();
8379 mono_threads_install_cleanup (mini_thread_cleanup);
8381 #define JIT_TRAMPOLINES_WORK
8382 #ifdef JIT_TRAMPOLINES_WORK
8383 mono_install_compile_method (mono_jit_compile_method);
8384 mono_install_trampoline (mono_arch_create_jit_trampoline);
8385 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
8387 #define JIT_INVOKE_WORKS
8388 #ifdef JIT_INVOKE_WORKS
8389 mono_install_runtime_invoke (mono_jit_runtime_invoke);
8390 mono_install_handler (mono_arch_get_throw_exception ());
8392 mono_install_stack_walk (mono_jit_walk_stack);
8394 domain = mono_init (filename);
8397 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
8398 ves_icall_get_frame_info);
8399 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
8400 ves_icall_get_trace);
8401 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
8402 mono_runtime_install_handlers);
8405 create_helper_signature ();
8407 #define JIT_CALLS_WORK
8408 #ifdef JIT_CALLS_WORK
8409 /* Needs to be called here since register_jit_icall depends on it */
8410 mono_marshal_init ();
8412 mono_arch_register_lowlevel_calls ();
8413 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
8414 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
8415 mono_register_jit_icall (mono_trace_enter_method, "mono_trace_enter_method", NULL, TRUE);
8416 mono_register_jit_icall (mono_trace_leave_method, "mono_trace_leave_method", NULL, TRUE);
8418 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
8419 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
8421 /* fixme: we cant hanlde vararg methods this way, because the signature is not constant */
8422 //mono_register_jit_icall (ves_array_element_address, "ves_array_element_address", NULL);
8423 //mono_register_jit_icall (mono_array_new_va, "mono_array_new_va", NULL);
8425 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
8426 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
8427 helper_sig_void_ptr, TRUE);
8428 mono_register_jit_icall (mono_thread_get_pending_exception, "mono_thread_get_pending_exception", helper_sig_obj_void, FALSE);
8431 * NOTE, NOTE, NOTE, NOTE:
8432 * when adding emulation for some opcodes, remember to also add a dummy
8433 * rule to the burg files, because we need the arity information to be correct.
8435 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult, TRUE);
8436 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un, FALSE);
8437 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf, FALSE);
8438 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv, FALSE);
8439 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un, FALSE);
8440 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem, FALSE);
8441 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un, FALSE);
8443 #ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
8444 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl, TRUE);
8445 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr, TRUE);
8446 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un, TRUE);
8449 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8, FALSE);
8450 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4, FALSE);
8451 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8, FALSE);
8452 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8, FALSE);
8454 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
8455 mono_register_opcode_emulation (OP_FCONV_TO_I8, "__emul_fconv_to_i8", helper_sig_long_double, mono_fconv_i8, FALSE);
8457 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
8458 mono_register_opcode_emulation (CEE_CONV_R_UN, "__emul_conv_r_un", helper_sig_double_int, mono_conv_to_r8_un, FALSE);
8460 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
8461 mono_register_opcode_emulation (OP_LCONV_TO_R8, "__emul_lconv_to_r8", helper_sig_double_long, mono_lconv_to_r8, FALSE);
8463 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
8464 mono_register_opcode_emulation (OP_LCONV_TO_R4, "__emul_lconv_to_r4", helper_sig_float_long, mono_lconv_to_r4, FALSE);
8466 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
8467 mono_register_opcode_emulation (OP_LCONV_TO_R_UN, "__emul_lconv_to_r8_un", helper_sig_double_long, mono_lconv_to_r8_un, FALSE);
8469 #ifdef MONO_ARCH_EMULATE_FREM
8470 mono_register_opcode_emulation (OP_FREM, "__emul_frem", helper_sig_double_double_double, fmod, FALSE);
8473 #if SIZEOF_VOID_P == 4
8474 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4, TRUE);
8477 #warning "fixme: add opcode emulation"
8481 /* other jit icalls */
8482 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
8483 helper_sig_ptr_ptr_ptr, FALSE);
8484 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
8485 mono_register_jit_icall (mono_get_special_static_data, "mono_get_special_static_data", helper_sig_ptr_int, FALSE);
8486 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
8487 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
8488 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
8489 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
8490 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
8491 mono_register_jit_icall (helper_stelem_ref_check, "helper_stelem_ref_check", helper_sig_stelem_ref_check, FALSE);
8492 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
8493 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
8494 mono_register_jit_icall (mono_object_new_fast, "mono_object_new_fast", helper_sig_object_new_specific, FALSE);
8495 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
8496 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
8497 mono_register_jit_icall (mono_string_to_utf16, "mono_string_to_utf16", helper_sig_ptr_obj, FALSE);
8498 mono_register_jit_icall (mono_string_from_utf16, "mono_string_from_utf16", helper_sig_obj_ptr, FALSE);
8499 mono_register_jit_icall (mono_string_new_wrapper, "mono_string_new_wrapper", helper_sig_obj_ptr, FALSE);
8500 mono_register_jit_icall (mono_string_to_utf8, "mono_string_to_utf8", helper_sig_ptr_obj, FALSE);
8501 mono_register_jit_icall (mono_string_to_bstr, "mono_string_to_bstr", helper_sig_ptr_obj, FALSE);
8502 mono_register_jit_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", helper_sig_ptr_obj, FALSE);
8503 mono_register_jit_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", helper_sig_ptr_obj, FALSE);
8504 mono_register_jit_icall (mono_string_builder_to_utf16, "mono_string_builder_to_utf16", helper_sig_ptr_obj, FALSE);
8505 mono_register_jit_icall (mono_array_to_savearray, "mono_array_to_savearray", helper_sig_ptr_obj, FALSE);
8506 mono_register_jit_icall (mono_array_to_lparray, "mono_array_to_lparray", helper_sig_ptr_obj, FALSE);
8507 mono_register_jit_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", helper_sig_ptr_obj, FALSE);
8508 mono_register_jit_icall (mono_ftnptr_to_delegate, "mono_ftnptr_to_delegate", helper_sig_obj_ptr_ptr, FALSE);
8509 mono_register_jit_icall (mono_marshal_string_array, "mono_marshal_string_array", helper_sig_ptr_obj, FALSE);
8510 mono_register_jit_icall (mono_marshal_string_array_to_unicode, "mono_marshal_string_array_to_unicode", helper_sig_ptr_obj, FALSE);
8511 mono_register_jit_icall (mono_marshal_asany, "mono_marshal_asany", helper_sig_ptr_obj_int, FALSE);
8512 mono_register_jit_icall (mono_marshal_free_asany, "mono_marshal_free_asany", helper_sig_void_obj_ptr_int, FALSE);
8513 mono_register_jit_icall (mono_string_utf8_to_builder, "mono_string_utf8_to_builder", helper_sig_void_ptr_ptr, FALSE);
8514 mono_register_jit_icall (mono_string_utf16_to_builder, "mono_string_utf16_to_builder", helper_sig_void_ptr_ptr, FALSE);
8515 mono_register_jit_icall (mono_marshal_free_array, "mono_marshal_free_array", helper_sig_void_ptr_ptr, FALSE);
8516 mono_register_jit_icall (mono_string_to_byvalstr, "mono_string_to_byvalstr", helper_sig_void_ptr_ptr_ptr, FALSE);
8517 mono_register_jit_icall (mono_string_to_byvalwstr, "mono_string_to_byvalwstr", helper_sig_void_ptr_ptr_ptr, FALSE);
8518 mono_register_jit_icall (g_free, "g_free", helper_sig_void_ptr, FALSE);
8519 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
8520 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
8521 mono_register_jit_icall (mono_ldftn_nosync, "mono_ldftn_nosync", helper_sig_compile, FALSE);
8522 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
8525 #define JIT_RUNTIME_WORKS
8526 #ifdef JIT_RUNTIME_WORKS
8527 mono_runtime_install_cleanup ((MonoDomainFunc)mini_cleanup);
8528 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
8531 mono_thread_attach (domain);
8535 MonoJitStats mono_jit_stats = {0};
8538 print_jit_stats (void)
8540 if (mono_jit_stats.enabled) {
8541 g_print ("Mono Jit statistics\n");
8542 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
8543 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
8544 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
8545 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
8546 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
8547 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
8548 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
8549 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
8550 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
8551 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
8552 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
8553 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
8554 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
8555 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
8556 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
8557 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
8558 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
8559 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
8561 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
8562 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
8563 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
8564 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
8565 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
8567 g_print ("\nGeneric instances: %ld\n", mono_stats.generic_instance_count);
8568 g_print ("Inflated methods: %ld\n", mono_stats.inflated_method_count);
8569 g_print ("Inflated types: %ld\n", mono_stats.inflated_type_count);
8570 g_print ("Generics metadata size: %ld\n", mono_stats.generics_metadata_size);
8575 mini_cleanup (MonoDomain *domain)
8578 * mono_runtime_cleanup() and mono_domain_finalize () need to
8579 * be called early since they need the execution engine still
8580 * fully working (mono_domain_finalize may invoke managed finalizers
8581 * and mono_runtime_cleanup will wait for other threads to finish).
8583 mono_domain_finalize (domain, 2000);
8585 mono_runtime_cleanup (domain);
8587 mono_profiler_shutdown ();
8589 mono_debug_cleanup ();
8591 #ifdef PLATFORM_WIN32
8592 win32_seh_cleanup();
8595 mono_domain_free (domain, TRUE);
8601 mono_set_defaults (int verbose_level, guint32 opts)
8603 mini_verbose = verbose_level;
8608 mono_precompile_assembly (MonoAssembly *ass, void *user_data)
8610 MonoImage *image = ass->image;
8614 if (mini_verbose > 0)
8615 printf ("PRECOMPILE: %s.\n", ass->image->name);
8617 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
8618 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
8619 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
8621 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
8625 if (mini_verbose > 1) {
8626 char * desc = mono_method_full_name (method, TRUE);
8627 g_print ("Compiling %d %s\n", count, desc);
8630 mono_compile_method (method);
8634 void mono_precompile_assemblies ()
8636 mono_assembly_foreach ((GFunc)mono_precompile_assembly, NULL);