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 "abcremoval.h"
52 #include "jit-icalls.c"
54 #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))
56 #define MONO_CHECK_THIS(ins) (cfg->method->signature->hasthis && (ins)->ssa_op == MONO_SSA_LOAD && (ins)->inst_left->inst_c0 == 0)
58 gboolean mono_arch_print_tree(MonoInst *tree, int arity);
59 static gpointer mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt);
60 static gpointer mono_jit_compile_method (MonoMethod *method);
61 static gpointer mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method);
63 static void handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src,
64 const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native);
66 static void dec_foreach (MonoInst *tree, MonoCompile *cfg);
68 static int mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
69 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
70 guint inline_offset, gboolean is_virtual_call);
72 extern guint8 mono_burg_arity [];
73 /* helper methods signature */
74 static MonoMethodSignature *helper_sig_long_long_long = NULL;
75 static MonoMethodSignature *helper_sig_long_long_int = NULL;
76 static MonoMethodSignature *helper_sig_newarr = NULL;
77 static MonoMethodSignature *helper_sig_newarr_specific = NULL;
78 static MonoMethodSignature *helper_sig_ldstr = NULL;
79 static MonoMethodSignature *helper_sig_domain_get = NULL;
80 static MonoMethodSignature *helper_sig_object_new = NULL;
81 static MonoMethodSignature *helper_sig_object_new_specific = NULL;
82 static MonoMethodSignature *helper_sig_compile = NULL;
83 static MonoMethodSignature *helper_sig_compile_virt = NULL;
84 static MonoMethodSignature *helper_sig_obj_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); \
617 #define NEW_GROUP(cfg,dest,el1,el2) do { \
618 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
619 (dest)->opcode = OP_GROUP; \
620 (dest)->inst_left = (el1); \
621 (dest)->inst_right = (el2); \
626 compare_bblock (gconstpointer a, gconstpointer b)
628 const MonoBasicBlock *b1 = a;
629 const MonoBasicBlock *b2 = b;
631 return b2->cil_code - b1->cil_code;
636 * link_bblock: Links two basic blocks
638 * links two basic blocks in the control flow graph, the 'from'
639 * argument is the starting block and the 'to' argument is the block
640 * the control flow ends to after 'from'.
643 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
645 MonoBasicBlock **newa;
649 if (from->cil_code) {
651 g_print ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
653 g_print ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
656 g_print ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
658 g_print ("edge from entry to exit\n");
662 for (i = 0; i < from->out_count; ++i) {
663 if (to == from->out_bb [i]) {
669 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
670 for (i = 0; i < from->out_count; ++i) {
671 newa [i] = from->out_bb [i];
679 for (i = 0; i < to->in_count; ++i) {
680 if (from == to->in_bb [i]) {
686 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
687 for (i = 0; i < to->in_count; ++i) {
688 newa [i] = to->in_bb [i];
697 * mono_find_block_region:
699 * We mark each basic block with a region ID. We use that to avoid BB
700 * optimizations when blocks are in different regions.
703 * A region token that encodes where this region is, and information
704 * about the clause owner for this block.
706 * The region encodes the try/catch/filter clause that owns this block
707 * as well as the type. -1 is a special value that represents a block
708 * that is in none of try/catch/filter.
711 mono_find_block_region (MonoCompile *cfg, int offset, int *filter_lengths)
713 MonoMethod *method = cfg->method;
714 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
715 MonoExceptionClause *clause;
718 /* first search for handlers and filters */
719 for (i = 0; i < header->num_clauses; ++i) {
720 clause = &header->clauses [i];
721 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->token_or_filter) &&
722 (offset < (clause->token_or_filter + filter_lengths [i])))
723 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
725 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
726 if (clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)
727 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
729 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
733 /* search the try blocks */
734 for (i = 0; i < header->num_clauses; ++i) {
735 clause = &header->clauses [i];
736 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
737 return ((i + 1) << 8) | clause->flags;
744 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
746 MonoMethod *method = cfg->method;
747 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
748 MonoExceptionClause *clause;
749 MonoBasicBlock *handler;
753 for (i = 0; i < header->num_clauses; ++i) {
754 clause = &header->clauses [i];
755 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
756 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
757 if (clause->flags == type) {
758 handler = g_hash_table_lookup (cfg->bb_hash, header->code + clause->handler_offset);
760 res = g_list_append (res, handler);
768 mono_find_spvar_for_region (MonoCompile *cfg, int region)
770 return g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
774 mono_create_spvar_for_region (MonoCompile *cfg, int region)
778 var = g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
782 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
783 /* prevent it from being register allocated */
784 var->flags |= MONO_INST_INDIRECT;
786 g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
790 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
794 array [*dfn] = start;
795 /*g_print ("visit %d at %p\n", *dfn, start->cil_code);*/
796 for (i = 0; i < start->out_count; ++i) {
797 if (start->out_bb [i]->dfn)
800 start->out_bb [i]->dfn = *dfn;
801 start->out_bb [i]->df_parent = start;
802 array [*dfn] = start->out_bb [i];
803 df_visit (start->out_bb [i], dfn, array);
809 MonoBasicBlock *best;
813 previous_foreach (gconstpointer key, gpointer val, gpointer data)
815 PrevStruct *p = data;
816 MonoBasicBlock *bb = val;
817 //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,
818 //bb->method == p->best->method, bb->cil_code < p->code, bb->cil_code > p->best->cil_code);
820 if (bb->cil_code && bb->cil_code < p->code && bb->cil_code > p->best->cil_code)
824 static MonoBasicBlock*
825 find_previous (GHashTable *bb_hash, MonoBasicBlock *start, const guchar *code) {
831 g_hash_table_foreach (bb_hash, (GHFunc)previous_foreach, &p);
836 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
845 * FIXME: take into account all the details:
846 * second may have been the target of more than one bblock
848 second->out_count = first->out_count;
849 second->out_bb = first->out_bb;
851 for (i = 0; i < first->out_count; ++i) {
852 bb = first->out_bb [i];
853 for (j = 0; j < bb->in_count; ++j) {
854 if (bb->in_bb [j] == first)
855 bb->in_bb [j] = second;
859 first->out_count = 0;
860 first->out_bb = NULL;
861 link_bblock (cfg, first, second);
863 second->last_ins = first->last_ins;
865 /*g_print ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
866 for (inst = first->code; inst && inst->next; inst = inst->next) {
867 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
868 g_print ("found %p: %s", inst->next->cil_code, code);
870 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
871 second->code = inst->next;
873 first->last_ins = inst;
874 second->next_bb = first->next_bb;
875 first->next_bb = second;
880 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
886 mono_type_to_ldind (MonoType *type)
898 case MONO_TYPE_BOOLEAN:
912 case MONO_TYPE_FNPTR:
914 case MONO_TYPE_CLASS:
915 case MONO_TYPE_STRING:
916 case MONO_TYPE_OBJECT:
917 case MONO_TYPE_SZARRAY:
918 case MONO_TYPE_ARRAY:
919 return CEE_LDIND_REF;
927 case MONO_TYPE_VALUETYPE:
928 if (type->data.klass->enumtype) {
929 t = type->data.klass->enum_basetype->type;
933 case MONO_TYPE_TYPEDBYREF:
935 case MONO_TYPE_GENERICINST:
936 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE)
938 return CEE_LDIND_REF;
940 g_error ("unknown type 0x%02x in type_to_ldind", type->type);
946 mono_type_to_stind (MonoType *type)
957 case MONO_TYPE_BOOLEAN:
969 case MONO_TYPE_FNPTR:
971 case MONO_TYPE_CLASS:
972 case MONO_TYPE_STRING:
973 case MONO_TYPE_OBJECT:
974 case MONO_TYPE_SZARRAY:
975 case MONO_TYPE_ARRAY:
976 return CEE_STIND_REF;
984 case MONO_TYPE_VALUETYPE:
985 if (type->data.klass->enumtype) {
986 t = type->data.klass->enum_basetype->type;
990 case MONO_TYPE_TYPEDBYREF:
992 case MONO_TYPE_GENERICINST:
993 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE)
995 return CEE_STIND_REF;
997 g_error ("unknown type 0x%02x in type_to_stind", type->type);
1003 * Returns the type used in the eval stack when @type is loaded.
1004 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
1007 type_to_eval_stack_type (MonoType *type, MonoInst *inst) {
1011 inst->type = STACK_MP;
1019 case MONO_TYPE_BOOLEAN:
1022 case MONO_TYPE_CHAR:
1025 inst->type = STACK_I4;
1030 case MONO_TYPE_FNPTR:
1031 inst->type = STACK_PTR;
1033 case MONO_TYPE_CLASS:
1034 case MONO_TYPE_STRING:
1035 case MONO_TYPE_OBJECT:
1036 case MONO_TYPE_SZARRAY:
1037 case MONO_TYPE_ARRAY:
1038 inst->type = STACK_OBJ;
1042 inst->type = STACK_I8;
1046 inst->type = STACK_R8;
1048 case MONO_TYPE_VALUETYPE:
1049 if (type->data.klass->enumtype) {
1050 t = type->data.klass->enum_basetype->type;
1053 inst->klass = type->data.klass;
1054 inst->type = STACK_VTYPE;
1057 case MONO_TYPE_TYPEDBYREF:
1058 inst->klass = mono_defaults.typed_reference_class;
1059 inst->type = STACK_VTYPE;
1061 case MONO_TYPE_GENERICINST:
1062 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE) {
1063 inst->klass = mono_class_from_mono_type (type);
1064 inst->type = STACK_VTYPE;
1066 inst->type = STACK_OBJ;
1070 g_error ("unknown type 0x%02x in eval stack type", type->type);
1075 * The following tables are used to quickly validate the IL code in type_from_op ().
1078 bin_num_table [STACK_MAX] [STACK_MAX] = {
1079 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1080 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1081 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1082 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1083 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
1084 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
1085 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1086 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1091 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
1094 /* reduce the size of this table */
1096 bin_int_table [STACK_MAX] [STACK_MAX] = {
1097 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1098 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1099 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1100 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1101 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, 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}
1108 bin_comp_table [STACK_MAX] [STACK_MAX] = {
1110 {0, 1, 0, 1, 0, 0, 4, 0},
1111 {0, 0, 1, 0, 0, 0, 0, 0},
1112 {0, 1, 0, 1, 0, 2, 4, 0},
1113 {0, 0, 0, 0, 1, 0, 0, 0},
1114 {0, 0, 0, 2, 0, 1, 0, 0},
1115 {0, 4, 0, 4, 0, 0, 3, 0},
1116 {0, 0, 0, 0, 0, 0, 0, 0},
1119 /* reduce the size of this table */
1121 shift_table [STACK_MAX] [STACK_MAX] = {
1122 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1123 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1124 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1125 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1126 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, 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}
1133 * Tables to map from the non-specific opcode to the matching
1134 * type-specific opcode.
1136 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
1137 static const guint16
1138 binops_op_map [STACK_MAX] = {
1139 0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, 0
1142 /* handles from CEE_NEG to CEE_CONV_U8 */
1143 static const guint16
1144 unops_op_map [STACK_MAX] = {
1145 0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, 0
1148 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
1149 static const guint16
1150 ovfops_op_map [STACK_MAX] = {
1151 0, 0, OP_LCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_FCONV_TO_U2-CEE_CONV_U2, 0
1154 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
1155 static const guint16
1156 ovf2ops_op_map [STACK_MAX] = {
1157 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
1160 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
1161 static const guint16
1162 ovf3ops_op_map [STACK_MAX] = {
1163 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
1166 /* handles from CEE_CEQ to CEE_CLT_UN */
1167 static const guint16
1168 ceqops_op_map [STACK_MAX] = {
1169 0, 0, OP_LCEQ-CEE_CEQ, OP_PCEQ-CEE_CEQ, OP_FCEQ-CEE_CEQ, 0
1173 * Sets ins->type (the type on the eval stack) according to the
1174 * type of the opcode and the arguments to it.
1175 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1177 * FIXME: this function sets ins->type unconditionally in some cases, but
1178 * it should set it to invalid for some types (a conv.x on an object)
1181 type_from_op (MonoInst *ins) {
1182 switch (ins->opcode) {
1189 /* FIXME: check unverifiable args for STACK_MP */
1190 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1191 ins->opcode += binops_op_map [ins->type];
1198 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1199 ins->opcode += binops_op_map [ins->type];
1204 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1205 ins->opcode += binops_op_map [ins->type];
1208 /* FIXME: handle some specifics with ins->next->type */
1209 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1216 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1217 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1221 ins->type = neg_table [ins->inst_i0->type];
1222 ins->opcode += unops_op_map [ins->type];
1225 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1226 ins->type = ins->inst_i0->type;
1228 ins->type = STACK_INV;
1229 ins->opcode += unops_op_map [ins->type];
1235 ins->type = STACK_I4;
1236 ins->opcode += unops_op_map [ins->inst_i0->type];
1239 ins->type = STACK_R8;
1240 switch (ins->inst_i0->type) {
1245 ins->opcode = OP_LCONV_TO_R_UN;
1249 case CEE_CONV_OVF_I1:
1250 case CEE_CONV_OVF_U1:
1251 case CEE_CONV_OVF_I2:
1252 case CEE_CONV_OVF_U2:
1253 case CEE_CONV_OVF_I4:
1254 case CEE_CONV_OVF_U4:
1255 ins->type = STACK_I4;
1256 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1258 case CEE_CONV_OVF_I_UN:
1259 case CEE_CONV_OVF_U_UN:
1260 ins->type = STACK_PTR;
1261 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1263 case CEE_CONV_OVF_I1_UN:
1264 case CEE_CONV_OVF_I2_UN:
1265 case CEE_CONV_OVF_I4_UN:
1266 case CEE_CONV_OVF_U1_UN:
1267 case CEE_CONV_OVF_U2_UN:
1268 case CEE_CONV_OVF_U4_UN:
1269 ins->type = STACK_I4;
1270 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1273 ins->type = STACK_PTR;
1274 switch (ins->inst_i0->type) {
1280 ins->opcode = OP_LCONV_TO_U;
1283 ins->opcode = OP_FCONV_TO_U;
1289 ins->type = STACK_I8;
1290 ins->opcode += unops_op_map [ins->inst_i0->type];
1292 case CEE_CONV_OVF_I8:
1293 case CEE_CONV_OVF_U8:
1294 ins->type = STACK_I8;
1295 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1297 case CEE_CONV_OVF_U8_UN:
1298 case CEE_CONV_OVF_I8_UN:
1299 ins->type = STACK_I8;
1300 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1304 ins->type = STACK_R8;
1305 ins->opcode += unops_op_map [ins->inst_i0->type];
1308 ins->type = STACK_R8;
1312 ins->type = STACK_I4;
1313 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1316 case CEE_CONV_OVF_I:
1317 case CEE_CONV_OVF_U:
1318 ins->type = STACK_PTR;
1319 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1322 case CEE_ADD_OVF_UN:
1324 case CEE_MUL_OVF_UN:
1326 case CEE_SUB_OVF_UN:
1327 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1328 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1331 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1338 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_MP, STACK_R8, STACK_R8, STACK_OBJ
1341 /* map ldelem.x to the matching ldind.x opcode */
1343 ldelem_to_ldind [] = {
1357 /* map stelem.x to the matching stind.x opcode */
1359 stelem_to_stind [] = {
1373 param_table [STACK_MAX] [STACK_MAX] = {
1378 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1382 switch (args->type) {
1392 for (i = 0; i < sig->param_count; ++i) {
1393 switch (args [i].type) {
1397 if (!sig->params [i]->byref)
1401 if (sig->params [i]->byref)
1403 switch (sig->params [i]->type) {
1404 case MONO_TYPE_CLASS:
1405 case MONO_TYPE_STRING:
1406 case MONO_TYPE_OBJECT:
1407 case MONO_TYPE_SZARRAY:
1408 case MONO_TYPE_ARRAY:
1415 if (sig->params [i]->byref)
1417 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1426 /*if (!param_table [args [i].type] [sig->params [i]->type])
1434 * When we need a pointer to the current domain many times in a method, we
1435 * call mono_domain_get() once and we store the result in a local variable.
1436 * This function returns the variable that represents the MonoDomain*.
1438 inline static MonoInst *
1439 mono_get_domainvar (MonoCompile *cfg)
1441 if (!cfg->domainvar)
1442 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1443 return cfg->domainvar;
1447 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
1450 int num = cfg->num_varinfo;
1452 if ((num + 1) >= cfg->varinfo_count) {
1453 cfg->varinfo_count = (cfg->varinfo_count + 2) * 2;
1454 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
1455 cfg->vars = (MonoMethodVar **)g_realloc (cfg->vars, sizeof (MonoMethodVar*) * cfg->varinfo_count);
1458 /*g_print ("created temp %d of type 0x%x\n", num, type->type);*/
1459 mono_jit_stats.allocate_var++;
1461 MONO_INST_NEW (cfg, inst, opcode);
1462 inst->inst_c0 = num;
1463 inst->inst_vtype = type;
1464 inst->klass = mono_class_from_mono_type (type);
1465 /* if set to 1 the variable is native */
1468 cfg->varinfo [num] = inst;
1470 cfg->vars [num] = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMethodVar));
1471 MONO_INIT_VARINFO (cfg->vars [num], num);
1474 //g_print ("created temp %d of type %s\n", num, mono_type_get_name (type));
1479 type_from_stack_type (MonoInst *ins) {
1480 switch (ins->type) {
1481 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1482 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1483 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1484 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1485 case STACK_MP: return &mono_defaults.int_class->byval_arg;
1486 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1487 case STACK_VTYPE: return &ins->klass->byval_arg;
1489 g_error ("stack type %d to montype not handled\n", ins->type);
1495 array_access_to_klass (int opcode)
1499 return mono_defaults.byte_class;
1501 return mono_defaults.uint16_class;
1504 return mono_defaults.int_class;
1507 return mono_defaults.sbyte_class;
1510 return mono_defaults.int16_class;
1513 return mono_defaults.int32_class;
1515 return mono_defaults.uint32_class;
1518 return mono_defaults.int64_class;
1521 return mono_defaults.single_class;
1524 return mono_defaults.double_class;
1525 case CEE_LDELEM_REF:
1526 case CEE_STELEM_REF:
1527 return mono_defaults.object_class;
1529 g_assert_not_reached ();
1535 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
1539 MONO_ADD_INS (bb, inst);
1542 switch (bb->last_ins->opcode) {
1556 while (prev->next && prev->next != bb->last_ins)
1558 if (prev == bb->code) {
1559 if (bb->last_ins == bb->code) {
1560 inst->next = bb->code;
1563 inst->next = prev->next;
1567 inst->next = bb->last_ins;
1571 // g_warning ("handle conditional jump in add_ins_to_end ()\n");
1573 MONO_ADD_INS (bb, inst);
1579 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
1581 MonoInst *inst, *load;
1583 NEW_TEMPLOAD (cfg, load, src);
1585 NEW_TEMPSTORE (cfg, inst, dest, load);
1586 if (inst->opcode == CEE_STOBJ) {
1587 NEW_TEMPLOADA (cfg, inst, dest);
1588 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE);
1590 inst->cil_code = NULL;
1591 mono_add_ins_to_end (bb, inst);
1596 * We try to share variables when possible
1599 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1604 /* inlining can result in deeper stacks */
1605 if (slot >= ((MonoMethodNormal *)cfg->method)->header->max_stack)
1606 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1608 pos = ins->type - 1 + slot * STACK_MAX;
1610 switch (ins->type) {
1617 if ((vnum = cfg->intvars [pos]))
1618 return cfg->varinfo [vnum];
1619 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1620 cfg->intvars [pos] = res->inst_c0;
1623 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1629 * This function is called to handle items that are left on the evaluation stack
1630 * at basic block boundaries. What happens is that we save the values to local variables
1631 * and we reload them later when first entering the target basic block (with the
1632 * handle_loaded_temps () function).
1633 * It is also used to handle items on the stack in store opcodes, since it is
1634 * possible that the variable to be stored into is already on the stack, in
1635 * which case its old value should be used.
1636 * A single joint point will use the same variables (stored in the array bb->out_stack or
1637 * bb->in_stack, if the basic block is before or after the joint point).
1640 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count) {
1642 MonoBasicBlock *outb;
1643 MonoInst *inst, **locals;
1647 if (cfg->verbose_level > 3)
1648 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
1649 if (!bb->out_scount) {
1651 bb->out_scount = count;
1652 //g_print ("bblock %d has out:", bb->block_num);
1653 for (i = 0; i < bb->out_count; ++i) {
1654 outb = bb->out_bb [i];
1655 //g_print (" %d", outb->block_num);
1656 if (outb->in_stack) {
1658 bb->out_stack = outb->in_stack;
1664 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1665 for (i = 0; i < count; ++i) {
1667 /* try to reuse temps already allocated for this purpouse, if they occupy the same
1668 * stack slot and if they are of the same type. */
1669 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1671 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1676 locals = bb->out_stack;
1677 for (i = 0; i < count; ++i) {
1678 /* add store ops at the end of the bb, before the branch */
1679 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1680 if (inst->opcode == CEE_STOBJ) {
1681 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
1682 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE);
1684 inst->cil_code = sp [i]->cil_code;
1685 mono_add_ins_to_end (bb, inst);
1687 if (cfg->verbose_level > 3)
1688 g_print ("storing %d to temp %d\n", i, locals [i]->inst_c0);
1691 for (i = 0; i < bb->out_count; ++i) {
1692 outb = bb->out_bb [i];
1693 if (outb->in_scount)
1694 continue; /* check they are the same locals */
1695 outb->in_scount = count;
1696 outb->in_stack = locals;
1702 ret_type_to_call_opcode (MonoType *type, int calli, int virt)
1707 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1711 case MONO_TYPE_VOID:
1712 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
1715 case MONO_TYPE_BOOLEAN:
1718 case MONO_TYPE_CHAR:
1721 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1725 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1726 case MONO_TYPE_CLASS:
1727 case MONO_TYPE_STRING:
1728 case MONO_TYPE_OBJECT:
1729 case MONO_TYPE_SZARRAY:
1730 case MONO_TYPE_ARRAY:
1731 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1734 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
1737 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
1738 case MONO_TYPE_VALUETYPE:
1739 if (type->data.klass->enumtype) {
1740 t = type->data.klass->enum_basetype->type;
1743 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1744 case MONO_TYPE_TYPEDBYREF:
1745 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1746 case MONO_TYPE_GENERICINST:
1747 t = type->data.generic_inst->generic_type->type;
1750 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
1756 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
1758 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
1760 ji->ip.label = label;
1761 ji->type = MONO_PATCH_INFO_SWITCH;
1762 ji->data.table = bbs;
1763 ji->next = cfg->patch_info;
1764 ji->table_size = num_blocks;
1765 cfg->patch_info = ji;
1769 * When we add a tree of instructions, we need to ensure the instructions currently
1770 * on the stack are executed before (like, if we load a value from a local).
1771 * We ensure this by saving the currently loaded values to temps and rewriting the
1772 * instructions to load the values.
1773 * This is not done for opcodes that terminate a basic block (because it's handled already
1774 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
1777 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
1779 MonoInst *load, *store, *temp, *ins;
1781 while (stack < sp) {
1783 /* handle also other constants */
1784 if (ins->opcode != OP_ICONST) {
1785 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1786 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1787 store->cil_code = ins->cil_code;
1788 if (store->opcode == CEE_STOBJ) {
1789 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
1790 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE);
1792 MONO_ADD_INS (bblock, store);
1793 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1794 load->cil_code = ins->cil_code;
1802 * Prepare arguments for passing to a function call.
1803 * Return a non-zero value if the arguments can't be passed to the given
1805 * The type checks are not yet complete and some conversions may need
1806 * casts on 32 or 64 bit architectures.
1809 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
1814 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
1818 for (i = 0; i < sig->param_count; ++i) {
1819 if (sig->params [i]->byref) {
1821 * check the result of ldelema is only passed as an argument if the byref
1822 * type matches exactly the array element type.
1823 * FIXME: if the argument as been saved on the stack as part of the
1824 * interface variable code (the value was on the stack at a basic block boundary)
1825 * we need to add the check in that case, too.
1827 if (args [i]->opcode == CEE_LDELEMA) {
1829 MonoClass *exact_class = mono_class_from_mono_type (sig->params [i]);
1830 if (!exact_class->valuetype) {
1831 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
1832 check->cil_code = args [i]->cil_code;
1833 check->klass = exact_class;
1834 check->inst_left = args [i]->inst_left;
1835 check->type = STACK_OBJ;
1836 args [i]->inst_left = check;
1839 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
1843 simple_type = sig->params [i]->type;
1845 switch (simple_type) {
1846 case MONO_TYPE_VOID:
1851 case MONO_TYPE_BOOLEAN:
1854 case MONO_TYPE_CHAR:
1857 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
1863 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
1866 case MONO_TYPE_CLASS:
1867 case MONO_TYPE_STRING:
1868 case MONO_TYPE_OBJECT:
1869 case MONO_TYPE_SZARRAY:
1870 case MONO_TYPE_ARRAY:
1871 if (args [i]->type != STACK_OBJ)
1876 if (args [i]->type != STACK_I8)
1881 if (args [i]->type != STACK_R8)
1884 case MONO_TYPE_VALUETYPE:
1885 if (sig->params [i]->data.klass->enumtype) {
1886 simple_type = sig->params [i]->data.klass->enum_basetype->type;
1889 if (args [i]->type != STACK_VTYPE)
1892 case MONO_TYPE_TYPEDBYREF:
1893 if (args [i]->type != STACK_VTYPE)
1896 case MONO_TYPE_GENERICINST:
1897 simple_type = sig->params [i]->data.generic_inst->generic_type->type;
1901 g_error ("unknown type 0x%02x in check_call_signature", simple_type);
1908 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
1909 const guint8 *ip, gboolean to_end)
1911 MonoInst *temp, *store, *ins = (MonoInst*)call;
1912 MonoType *ret = sig->ret;
1914 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
1916 call->inst.type = STACK_OBJ;
1917 call->inst.opcode = CEE_CALL;
1918 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
1920 type_to_eval_stack_type (ret, ins);
1921 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
1924 if (MONO_TYPE_ISSTRUCT (ret)) {
1927 /* we use this to allocate native sized structs */
1928 temp->unused = sig->pinvoke;
1930 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
1931 if (call->inst.opcode == OP_VCALL)
1932 ins->inst_left = loada;
1934 ins->inst_right = loada; /* a virtual or indirect call */
1937 mono_add_ins_to_end (bblock, ins);
1939 MONO_ADD_INS (bblock, ins);
1941 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1942 store->cil_code = ip;
1944 mono_add_ins_to_end (bblock, store);
1946 MONO_ADD_INS (bblock, store);
1948 return temp->inst_c0;
1951 mono_add_ins_to_end (bblock, ins);
1953 MONO_ADD_INS (bblock, ins);
1958 inline static MonoCallInst *
1959 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1960 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
1965 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual));
1967 call->inst.cil_code = ip;
1969 call->signature = sig;
1970 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
1972 for (arg = call->out_args; arg;) {
1973 MonoInst *narg = arg->next;
1978 mono_add_ins_to_end (bblock, arg);
1980 MONO_ADD_INS (bblock, arg);
1987 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1988 MonoInst **args, MonoInst *addr, const guint8 *ip)
1990 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
1992 call->inst.inst_i0 = addr;
1994 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
1997 static MonoCallInst*
1998 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
1999 MonoInst **args, const guint8 *ip, MonoInst *this)
2001 gboolean virtual = this != NULL;
2004 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, FALSE);
2006 if (this && sig->hasthis &&
2007 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
2008 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
2009 call->method = mono_marshal_get_remoting_invoke_with_check (method);
2011 call->method = method;
2013 call->inst.flags |= MONO_INST_HAS_METHOD;
2014 call->inst.inst_left = this;
2020 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
2021 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
2023 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, signature, args, ip, this);
2025 return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
2029 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
2030 MonoInst **args, const guint8 *ip, gboolean to_end)
2036 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
2038 return mono_spill_call (cfg, bblock, call, sig, func == mono_array_new_va, ip, to_end);
2042 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
2044 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
2047 g_warning ("unregistered JIT ICall");
2048 g_assert_not_reached ();
2051 return mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, args, ip, FALSE);
2055 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
2057 MonoInst *ins, *temp = NULL, *store, *load, *begin;
2058 MonoInst *last_arg = NULL;
2062 //g_print ("emulating: ");
2063 //mono_print_tree_nl (tree);
2064 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE));
2065 ins = (MonoInst*)call;
2067 call->inst.cil_code = tree->cil_code;
2069 call->signature = info->sig;
2071 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
2073 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2074 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
2075 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2076 store->cil_code = tree->cil_code;
2081 nargs = info->sig->param_count + info->sig->hasthis;
2083 for (last_arg = call->out_args; last_arg && last_arg->next; last_arg = last_arg->next) ;
2086 last_arg->next = store;
2089 begin = call->out_args;
2093 if (cfg->prev_ins) {
2095 * This assumes that that in a tree, emulate_opcode is called for a
2096 * node before it is called for its children. dec_foreach needs to
2097 * take this into account.
2099 store->next = cfg->prev_ins->next;
2100 cfg->prev_ins->next = begin;
2102 store->next = cfg->cbb->code;
2103 cfg->cbb->code = begin;
2106 call->fptr = mono_icall_get_wrapper (info);
2108 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2109 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2114 static MonoMethodSignature *
2115 mono_get_element_address_signature (int arity)
2117 static GHashTable *sighash = NULL;
2118 MonoMethodSignature *res;
2121 EnterCriticalSection (&trampoline_hash_mutex);
2123 sighash = g_hash_table_new (NULL, NULL);
2125 else if ((res = g_hash_table_lookup (sighash, (gpointer)arity))) {
2126 LeaveCriticalSection (&trampoline_hash_mutex);
2130 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
2133 res->params [0] = &mono_defaults.array_class->byval_arg;
2135 for (i = 1; i <= arity; i++)
2136 res->params [i] = &mono_defaults.int_class->byval_arg;
2138 res->ret = &mono_defaults.int_class->byval_arg;
2140 g_hash_table_insert (sighash, (gpointer)arity, res);
2141 LeaveCriticalSection (&trampoline_hash_mutex);
2147 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native) {
2148 MonoInst *iargs [3];
2154 * This check breaks with spilled vars... need to handle it during verification anyway.
2155 * g_assert (klass && klass == src->klass && klass == dest->klass);
2159 n = mono_class_native_size (klass, &align);
2161 n = mono_class_value_size (klass, &align);
2163 if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
2165 MONO_INST_NEW (cfg, inst, OP_MEMCPY);
2166 inst->inst_left = dest;
2167 inst->inst_right = src;
2168 inst->cil_code = ip;
2170 MONO_ADD_INS (bblock, inst);
2175 NEW_ICONST (cfg, iargs [2], n);
2177 mono_emit_native_call (cfg, bblock, helper_memcpy, helper_sig_memcpy, iargs, ip, to_end);
2181 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
2183 MonoInst *iargs [2];
2184 MonoInst *ins, *zero_int32;
2187 NEW_ICONST (cfg, zero_int32, 0);
2189 mono_class_init (klass);
2190 n = mono_class_value_size (klass, NULL);
2191 MONO_INST_NEW (cfg, ins, 0);
2193 ins->inst_left = dest;
2194 ins->inst_right = zero_int32;
2197 ins->opcode = CEE_STIND_I1;
2198 MONO_ADD_INS (bblock, ins);
2201 ins->opcode = CEE_STIND_I2;
2202 MONO_ADD_INS (bblock, ins);
2205 ins->opcode = CEE_STIND_I4;
2206 MONO_ADD_INS (bblock, ins);
2209 if (n <= sizeof (gpointer) * 5) {
2210 ins->opcode = OP_MEMSET;
2213 MONO_ADD_INS (bblock, ins);
2216 handle_loaded_temps (cfg, bblock, stack_start, sp);
2217 NEW_ICONST (cfg, ins, n);
2220 mono_emit_jit_icall (cfg, bblock, helper_initobj, iargs, ip);
2226 handle_box (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *val, const guchar *ip, MonoClass *klass)
2228 MonoInst *iargs [2];
2229 MonoInst *dest, *vtoffset, *add, *vstore;
2232 /* much like NEWOBJ */
2233 if (cfg->opt & MONO_OPT_SHARED) {
2234 NEW_DOMAINCONST (cfg, iargs [0]);
2235 NEW_CLASSCONST (cfg, iargs [1], klass);
2237 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
2239 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
2240 NEW_VTABLECONST (cfg, iargs [0], vtable);
2241 if (klass->has_finalize || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
2242 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
2244 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
2246 NEW_TEMPLOAD (cfg, dest, temp);
2247 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
2248 MONO_INST_NEW (cfg, add, CEE_ADD);
2249 add->inst_left = dest;
2250 add->inst_right = vtoffset;
2253 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
2254 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
2255 vstore->cil_code = ip;
2256 vstore->inst_left = add;
2257 vstore->inst_right = val;
2259 if (vstore->opcode == CEE_STOBJ) {
2260 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
2262 MONO_ADD_INS (bblock, vstore);
2264 NEW_TEMPLOAD (cfg, dest, temp);
2268 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
2271 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
2273 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
2274 MonoMethodSignature *signature = method->signature;
2278 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2279 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2280 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
2281 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
2282 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2283 (method->klass->marshalbyref) ||
2284 !header || header->num_clauses ||
2285 /* fixme: why cant we inline valuetype returns? */
2286 MONO_TYPE_ISSTRUCT (signature->ret))
2289 /* its not worth to inline methods with valuetype arguments?? */
2290 for (i = 0; i < signature->param_count; i++) {
2291 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
2297 * if we can initialize the class of the method right away, we do,
2298 * otherwise we don't allow inlining if the class needs initialization,
2299 * since it would mean inserting a call to mono_runtime_class_init()
2300 * inside the inlined code
2302 if (!(cfg->opt & MONO_OPT_SHARED)) {
2303 vtable = mono_class_vtable (cfg->domain, method->klass);
2304 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
2305 if (cfg->run_cctors)
2306 mono_runtime_class_init (vtable);
2308 else if (!vtable->initialized && mono_class_needs_cctor_run (method->klass, NULL))
2312 * If we're compiling for shared code
2313 * the cctor will need to be run at aot method load time, for example,
2314 * or at the end of the compilation of the inlining method.
2316 if (mono_class_needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
2319 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
2321 /* also consider num_locals? */
2322 if (getenv ("MONO_INLINELIMIT"))
2323 if (header->code_size < atoi (getenv ("MONO_INLINELIMIT"))) {
2327 if (header->code_size < 20)
2334 mini_get_ldelema_ins (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
2338 MonoMethodSignature *esig;
2339 char icall_name [256];
2340 MonoJitICallInfo *info;
2342 rank = cmethod->signature->param_count - (is_set? 1: 0);
2344 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
2346 NEW_GROUP (cfg, indexes, sp [1], sp [2]);
2347 MONO_INST_NEW (cfg, addr, OP_LDELEMA2D);
2348 addr->inst_left = sp [0];
2349 addr->inst_right = indexes;
2350 addr->cil_code = ip;
2351 addr->type = STACK_MP;
2352 addr->klass = cmethod->klass;
2356 /* Need to register the icall so it gets an icall wrapper */
2357 sprintf (icall_name, "ves_array_element_address_%d", rank);
2359 info = mono_find_jit_icall_by_name (icall_name);
2361 esig = mono_get_element_address_signature (rank);
2362 info = mono_register_jit_icall (ves_array_element_address, g_strdup (icall_name), esig, FALSE);
2365 temp = mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, sp, ip, FALSE);
2367 NEW_TEMPLOAD (cfg, addr, temp);
2371 static MonoJitICallInfo **emul_opcode_map = NULL;
2373 static inline MonoJitICallInfo *
2374 mono_find_jit_opcode_emulation (int opcode)
2376 if (emul_opcode_map)
2377 return emul_opcode_map [opcode];
2383 mini_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
2388 if (cmethod->klass == mono_defaults.string_class) {
2389 if (cmethod->name [0] != 'g')
2392 if (strcmp (cmethod->name, "get_Chars") == 0)
2394 else if (strcmp (cmethod->name, "get_Length") == 0)
2397 } else if (cmethod->klass == mono_defaults.object_class) {
2398 if (strcmp (cmethod->name, "GetType") == 0)
2402 } else if (cmethod->klass == mono_defaults.array_class) {
2403 if (strcmp (cmethod->name, "get_Rank") == 0)
2405 else if (strcmp (cmethod->name, "get_Length") == 0)
2410 op = mono_arch_get_opcode_for_method (cfg, cmethod, fsig, args);
2414 pc = fsig->param_count + fsig->hasthis;
2415 MONO_INST_NEW (cfg, ins, op);
2418 ins->inst_i0 = args [0];
2420 ins->inst_i1 = args [1];
2427 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
2429 MonoInst *store, *temp;
2432 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
2434 if (!sig->hasthis && sig->param_count == 0)
2438 if (sp [0]->opcode == OP_ICONST) {
2441 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
2443 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2444 store->cil_code = sp [0]->cil_code;
2445 MONO_ADD_INS (bblock, store);
2450 for (i = 0; i < sig->param_count; ++i) {
2451 if (sp [0]->opcode == OP_ICONST) {
2454 temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
2456 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2457 store->cil_code = sp [0]->cil_code;
2458 if (store->opcode == CEE_STOBJ) {
2459 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2460 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE);
2462 MONO_ADD_INS (bblock, store);
2470 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
2471 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b)
2473 MonoInst *ins, *rvar = NULL;
2474 MonoMethodHeader *cheader;
2475 MonoBasicBlock *ebblock, *sbblock;
2476 int i, costs, new_locals_offset;
2478 if (cfg->verbose_level > 2)
2479 g_print ("INLINE START %p %s -> %s\n", cmethod, mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2481 cheader = ((MonoMethodNormal *)cmethod)->header;
2483 if (!cmethod->inline_info) {
2484 mono_jit_stats.inlineable_methods++;
2485 cmethod->inline_info = 1;
2487 /* allocate space to store the return value */
2488 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2489 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2492 /* allocate local variables */
2493 new_locals_offset = cfg->num_varinfo;
2494 for (i = 0; i < cheader->num_locals; ++i)
2495 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
2497 /* allocate starte and end blocks */
2498 sbblock = NEW_BBLOCK (cfg);
2499 sbblock->block_num = cfg->num_bblocks++;
2500 sbblock->real_offset = real_offset;
2502 ebblock = NEW_BBLOCK (cfg);
2503 ebblock->block_num = cfg->num_bblocks++;
2504 ebblock->real_offset = real_offset;
2506 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
2508 if (costs >= 0 && costs < 60) {
2509 if (cfg->verbose_level > 2)
2510 g_print ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2512 mono_jit_stats.inlined_methods++;
2514 /* always add some code to avoid block split failures */
2515 MONO_INST_NEW (cfg, ins, CEE_NOP);
2516 MONO_ADD_INS (bblock, ins);
2519 bblock->next_bb = sbblock;
2520 link_bblock (cfg, bblock, sbblock);
2523 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
2529 if (cfg->verbose_level > 2)
2530 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
2536 * Some of these comments may well be out-of-date.
2537 * Design decisions: we do a single pass over the IL code (and we do bblock
2538 * splitting/merging in the few cases when it's required: a back jump to an IL
2539 * address that was not already seen as bblock starting point).
2540 * Code is validated as we go (full verification is still better left to metadata/verify.c).
2541 * Complex operations are decomposed in simpler ones right away. We need to let the
2542 * arch-specific code peek and poke inside this process somehow (except when the
2543 * optimizations can take advantage of the full semantic info of coarse opcodes).
2544 * All the opcodes of the form opcode.s are 'normalized' to opcode.
2545 * MonoInst->opcode initially is the IL opcode or some simplification of that
2546 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
2547 * opcode with value bigger than OP_LAST.
2548 * At this point the IR can be handed over to an interpreter, a dumb code generator
2549 * or to the optimizing code generator that will translate it to SSA form.
2551 * Profiling directed optimizations.
2552 * We may compile by default with few or no optimizations and instrument the code
2553 * or the user may indicate what methods to optimize the most either in a config file
2554 * or through repeated runs where the compiler applies offline the optimizations to
2555 * each method and then decides if it was worth it.
2558 * * consider using an array instead of an hash table (bb_hash)
2561 #define CHECK_TYPE(ins) if (!(ins)->type) goto unverified
2562 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) goto unverified
2563 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) goto unverified
2564 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) goto unverified
2565 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) goto unverified
2566 #define CHECK_OPSIZE(size) if (ip + size > end) goto unverified
2569 /* offset from br.s -> br like opcodes */
2570 #define BIG_BRANCH_OFFSET 13
2573 get_basic_blocks (MonoCompile *cfg, GHashTable *bbhash, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
2575 unsigned char *ip = start;
2576 unsigned char *target;
2579 MonoBasicBlock *bblock;
2580 const MonoOpcode *opcode;
2583 cli_addr = ip - start;
2584 i = mono_opcode_value ((const guint8 **)&ip);
2585 opcode = &mono_opcodes [i];
2586 switch (opcode->argument) {
2587 case MonoInlineNone:
2590 case MonoInlineString:
2591 case MonoInlineType:
2592 case MonoInlineField:
2593 case MonoInlineMethod:
2596 case MonoShortInlineR:
2603 case MonoShortInlineVar:
2604 case MonoShortInlineI:
2607 case MonoShortInlineBrTarget:
2608 target = start + cli_addr + 2 + (signed char)ip [1];
2609 GET_BBLOCK (cfg, bbhash, bblock, target);
2612 case MonoInlineBrTarget:
2613 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
2614 GET_BBLOCK (cfg, bbhash, bblock, target);
2617 case MonoInlineSwitch: {
2618 guint32 n = read32 (ip + 1);
2621 cli_addr += 5 + 4 * n;
2622 target = start + cli_addr;
2623 GET_BBLOCK (cfg, bbhash, bblock, target);
2625 for (j = 0; j < n; ++j) {
2626 target = start + cli_addr + (gint32)read32 (ip);
2627 GET_BBLOCK (cfg, bbhash, bblock, target);
2637 g_assert_not_reached ();
2647 * mono_method_to_ir: translates IL into basic blocks containing trees
2650 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
2651 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
2652 guint inline_offset, gboolean is_virtual_call)
2654 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
2655 MonoInst *ins, **sp, **stack_start;
2656 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
2658 MonoMethod *cmethod;
2659 MonoInst **arg_array;
2660 MonoMethodHeader *header;
2662 guint32 token, ins_flag;
2664 MonoClass *constrained_call = NULL;
2665 unsigned char *ip, *end, *target, *err_pos;
2666 static double r8_0 = 0.0;
2667 MonoMethodSignature *sig;
2668 MonoGenericContext *generic_context = NULL;
2669 MonoType **param_types;
2670 GList *bb_recheck = NULL, *tmp;
2671 int i, n, start_new_bblock, align;
2672 int num_calls = 0, inline_costs = 0;
2673 int *filter_lengths = NULL;
2674 int breakpoint_id = 0;
2675 guint real_offset, num_args;
2677 image = method->klass->image;
2678 header = ((MonoMethodNormal *)method)->header;
2679 sig = method->signature;
2680 num_args = sig->hasthis + sig->param_count;
2681 ip = (unsigned char*)header->code;
2682 end = ip + header->code_size;
2683 mono_jit_stats.cil_code_size += header->code_size;
2685 if (method->signature->is_inflated)
2686 generic_context = ((MonoMethodInflated *) method)->context;
2688 if (cfg->method == method) {
2690 bbhash = cfg->bb_hash;
2692 real_offset = inline_offset;
2693 bbhash = g_hash_table_new (g_direct_hash, NULL);
2696 if (cfg->verbose_level > 2)
2697 g_print ("method to IR %s\n", mono_method_full_name (method, TRUE));
2699 dont_inline = g_list_prepend (dont_inline, method);
2700 if (cfg->method == method) {
2702 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
2703 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
2706 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
2707 start_bblock->cil_code = NULL;
2708 start_bblock->cil_length = 0;
2709 start_bblock->block_num = cfg->num_bblocks++;
2712 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
2713 end_bblock->cil_code = NULL;
2714 end_bblock->cil_length = 0;
2715 end_bblock->block_num = cfg->num_bblocks++;
2716 g_assert (cfg->num_bblocks == 2);
2718 arg_array = alloca (sizeof (MonoInst *) * num_args);
2719 for (i = num_args - 1; i >= 0; i--)
2720 arg_array [i] = cfg->varinfo [i];
2722 if (header->num_clauses) {
2723 int size = sizeof (int) * header->num_clauses;
2724 filter_lengths = alloca (size);
2725 memset (filter_lengths, 0, size);
2727 cfg->spvars = g_hash_table_new (NULL, NULL);
2729 /* handle exception clauses */
2730 for (i = 0; i < header->num_clauses; ++i) {
2731 //unsigned char *p = ip;
2732 MonoExceptionClause *clause = &header->clauses [i];
2733 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->try_offset);
2734 tblock->real_offset = clause->try_offset;
2735 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
2736 tblock->real_offset = clause->handler_offset;
2738 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
2739 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2740 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2741 MONO_ADD_INS (tblock, ins);
2744 /*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);
2746 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
2748 /* catch and filter blocks get the exception object on the stack */
2749 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
2750 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2751 /* mostly like handle_stack_args (), but just sets the input args */
2752 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
2754 cfg->exvar = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
2755 /* prevent it from being register allocated */
2756 cfg->exvar->flags |= MONO_INST_INDIRECT;
2758 tblock->in_scount = 1;
2759 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2760 tblock->in_stack [0] = cfg->exvar;
2762 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2763 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->token_or_filter);
2764 tblock->real_offset = clause->token_or_filter;
2765 tblock->in_scount = 1;
2766 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2767 tblock->in_stack [0] = cfg->exvar;
2768 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2769 MONO_ADD_INS (tblock, ins);
2775 arg_array = alloca (sizeof (MonoInst *) * num_args);
2776 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
2779 /* FIRST CODE BLOCK */
2780 bblock = NEW_BBLOCK (cfg);
2781 bblock->cil_code = ip;
2783 ADD_BBLOCK (cfg, bbhash, bblock);
2785 if (cfg->method == method) {
2786 breakpoint_id = mono_debugger_method_has_breakpoint (method);
2787 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
2788 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2789 MONO_ADD_INS (bblock, ins);
2793 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED))) || mono_compile_aot) {
2794 /* we use a separate basic block for the initialization code */
2795 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
2796 init_localsbb->real_offset = real_offset;
2797 start_bblock->next_bb = init_localsbb;
2798 init_localsbb->next_bb = bblock;
2799 link_bblock (cfg, start_bblock, init_localsbb);
2800 link_bblock (cfg, init_localsbb, bblock);
2801 init_localsbb->block_num = cfg->num_bblocks++;
2803 start_bblock->next_bb = bblock;
2804 link_bblock (cfg, start_bblock, bblock);
2807 if (get_basic_blocks (cfg, bbhash, header, real_offset, ip, end, &err_pos)) {
2812 mono_debug_init_method (cfg, bblock, breakpoint_id);
2814 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
2816 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
2817 for (n = 0; n < sig->param_count; ++n)
2818 param_types [n + sig->hasthis] = sig->params [n];
2820 /* do this somewhere outside - not here */
2821 NEW_ICONST (cfg, zero_int32, 0);
2822 NEW_ICONST (cfg, zero_int64, 0);
2823 zero_int64->type = STACK_I8;
2824 NEW_PCONST (cfg, zero_ptr, 0);
2825 NEW_PCONST (cfg, zero_obj, 0);
2826 zero_obj->type = STACK_OBJ;
2828 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
2829 zero_r8->type = STACK_R8;
2830 zero_r8->inst_p0 = &r8_0;
2832 /* add a check for this != NULL to inlined methods */
2833 if (is_virtual_call) {
2834 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
2835 NEW_ARGLOAD (cfg, ins->inst_left, 0);
2837 MONO_ADD_INS (bblock, ins);
2840 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
2841 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
2844 start_new_bblock = 0;
2847 if (cfg->method == method)
2848 real_offset = ip - header->code;
2850 real_offset = inline_offset;
2852 if (start_new_bblock) {
2853 bblock->cil_length = ip - bblock->cil_code;
2854 if (start_new_bblock == 2) {
2855 g_assert (ip == tblock->cil_code);
2857 GET_BBLOCK (cfg, bbhash, tblock, ip);
2859 bblock->next_bb = tblock;
2861 start_new_bblock = 0;
2862 for (i = 0; i < bblock->in_scount; ++i) {
2863 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2867 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
2868 link_bblock (cfg, bblock, tblock);
2869 if (sp != stack_start) {
2870 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2873 bblock->next_bb = tblock;
2875 for (i = 0; i < bblock->in_scount; ++i) {
2876 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2882 bblock->real_offset = real_offset;
2884 if ((cfg->method == method) && cfg->coverage_info) {
2885 MonoInst *store, *one;
2886 guint32 cil_offset = ip - header->code;
2887 cfg->coverage_info->data [cil_offset].cil_code = ip;
2889 /* TODO: Use an increment here */
2890 NEW_ICONST (cfg, one, 1);
2893 NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
2896 MONO_INST_NEW (cfg, store, CEE_STIND_I);
2897 store->cil_code = ip;
2898 store->inst_left = ins;
2899 store->inst_right = one;
2901 MONO_ADD_INS (bblock, store);
2904 if (cfg->verbose_level > 3)
2905 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, sp-stack_start, mono_disasm_code_one (NULL, method, ip, NULL));
2912 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2913 ins->cil_code = ip++;
2914 MONO_ADD_INS (bblock, ins);
2920 CHECK_STACK_OVF (1);
2921 n = (*ip)-CEE_LDARG_0;
2923 NEW_ARGLOAD (cfg, ins, n);
2924 ins->cil_code = ip++;
2931 CHECK_STACK_OVF (1);
2932 n = (*ip)-CEE_LDLOC_0;
2934 NEW_LOCLOAD (cfg, ins, n);
2935 ins->cil_code = ip++;
2943 n = (*ip)-CEE_STLOC_0;
2946 handle_loaded_temps (cfg, bblock, stack_start, sp);
2947 NEW_LOCSTORE (cfg, ins, n, *sp);
2949 if (ins->opcode == CEE_STOBJ) {
2950 NEW_LOCLOADA (cfg, ins, n);
2951 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2953 MONO_ADD_INS (bblock, ins);
2959 CHECK_STACK_OVF (1);
2961 NEW_ARGLOAD (cfg, ins, ip [1]);
2968 CHECK_STACK_OVF (1);
2970 NEW_ARGLOADA (cfg, ins, ip [1]);
2980 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
2981 handle_loaded_temps (cfg, bblock, stack_start, sp);
2983 if (ins->opcode == CEE_STOBJ) {
2984 NEW_ARGLOADA (cfg, ins, ip [1]);
2985 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2987 MONO_ADD_INS (bblock, ins);
2992 CHECK_STACK_OVF (1);
2993 CHECK_LOCAL (ip [1]);
2994 NEW_LOCLOAD (cfg, ins, ip [1]);
3001 CHECK_STACK_OVF (1);
3002 CHECK_LOCAL (ip [1]);
3003 NEW_LOCLOADA (cfg, ins, ip [1]);
3012 handle_loaded_temps (cfg, bblock, stack_start, sp);
3013 CHECK_LOCAL (ip [1]);
3014 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
3016 if (ins->opcode == CEE_STOBJ) {
3017 NEW_LOCLOADA (cfg, ins, ip [1]);
3018 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3020 MONO_ADD_INS (bblock, ins);
3025 CHECK_STACK_OVF (1);
3026 NEW_PCONST (cfg, ins, NULL);
3028 ins->type = STACK_OBJ;
3033 CHECK_STACK_OVF (1);
3034 NEW_ICONST (cfg, ins, -1);
3048 CHECK_STACK_OVF (1);
3049 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
3056 CHECK_STACK_OVF (1);
3058 NEW_ICONST (cfg, ins, *((signed char*)ip));
3065 CHECK_STACK_OVF (1);
3066 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
3073 CHECK_STACK_OVF (1);
3074 MONO_INST_NEW (cfg, ins, OP_I8CONST);
3076 ins->type = STACK_I8;
3078 ins->inst_l = (gint64)read64 (ip);
3083 float *f = g_malloc (sizeof (float));
3085 CHECK_STACK_OVF (1);
3086 MONO_INST_NEW (cfg, ins, OP_R4CONST);
3087 ins->type = STACK_R8;
3096 double *d = g_malloc (sizeof (double));
3098 CHECK_STACK_OVF (1);
3099 MONO_INST_NEW (cfg, ins, OP_R8CONST);
3100 ins->type = STACK_R8;
3109 MonoInst *temp, *store;
3111 CHECK_STACK_OVF (1);
3116 * small optimization: if the loaded value was from a local already,
3117 * just load it twice.
3119 if (ins->ssa_op == MONO_SSA_LOAD &&
3120 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
3122 MONO_INST_NEW (cfg, temp, 0);
3124 temp->cil_code = ip;
3127 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3128 temp->cil_code = ip;
3129 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3130 store->cil_code = ip;
3131 MONO_ADD_INS (bblock, store);
3132 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3135 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3145 MONO_INST_NEW (cfg, ins, CEE_POP);
3146 MONO_ADD_INS (bblock, ins);
3147 ins->cil_code = ip++;
3153 if (stack_start != sp)
3155 MONO_INST_NEW (cfg, ins, CEE_JMP);
3156 token = read32 (ip + 1);
3157 /* FIXME: check the signature matches */
3158 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3159 ins->inst_p0 = cmethod;
3160 MONO_ADD_INS (bblock, ins);
3162 start_new_bblock = 1;
3166 case CEE_CALLVIRT: {
3167 MonoInst *addr = NULL;
3168 MonoMethodSignature *fsig = NULL;
3169 int temp, array_rank = 0;
3170 int virtual = *ip == CEE_CALLVIRT;
3173 token = read32 (ip + 1);
3175 if (*ip == CEE_CALLI) {
3180 if (method->wrapper_type != MONO_WRAPPER_NONE)
3181 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
3183 fsig = mono_metadata_parse_signature (image, token);
3185 n = fsig->param_count + fsig->hasthis;
3187 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3188 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
3189 } else if (constrained_call) {
3190 cmethod = mono_get_method_constrained (image, token, constrained_call, generic_context);
3192 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3197 if (!cmethod->klass->inited)
3198 mono_class_init (cmethod->klass);
3200 if (cmethod->signature->pinvoke) {
3201 #ifdef MONO_USE_EXC_TABLES
3202 if (mono_method_blittable (cmethod)) {
3203 fsig = cmethod->signature;
3206 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
3207 fsig = wrapper->signature;
3208 #ifdef MONO_USE_EXC_TABLES
3212 fsig = mono_method_get_signature (cmethod, image, token);
3215 n = fsig->param_count + fsig->hasthis;
3217 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
3218 cmethod->klass->parent == mono_defaults.array_class) {
3219 array_rank = cmethod->klass->rank;
3222 if (cmethod->string_ctor)
3223 g_assert_not_reached ();
3229 //g_assert (!virtual || fsig->hasthis);
3233 if (constrained_call) {
3235 * We have the `constrained.' prefix opcode.
3237 if (constrained_call->valuetype && !cmethod->klass->valuetype) {
3239 * The type parameter is instantiated as a valuetype,
3240 * but that type doesn't override the method we're
3241 * calling, so we need to box `this'.
3243 sp [0] = handle_box (cfg, bblock, sp [0], ip, constrained_call);
3244 } else if (!constrained_call->valuetype) {
3248 * The type parameter is instantiated as a reference
3249 * type. We have a managed pointer on the stack, so
3250 * we need to dereference it here.
3253 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3255 ins->inst_i0 = sp [0];
3256 ins->type = STACK_OBJ;
3258 } else if (cmethod->klass->valuetype)
3260 constrained_call = NULL;
3263 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
3266 if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL) && (mono_metadata_signature_equal (method->signature, cmethod->signature))) {
3268 /* FIXME: This assumes the two methods has the same number and type of arguments */
3269 for (i = 0; i < n; ++i) {
3270 /* Check if argument is the same */
3271 NEW_ARGLOAD (cfg, ins, i);
3272 if ((ins->opcode == sp [i]->opcode) && (ins->inst_i0 == sp [i]->inst_i0))
3275 /* Prevent argument from being register allocated */
3276 arg_array [i]->flags |= MONO_INST_VOLATILE;
3277 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3279 if (ins->opcode == CEE_STOBJ) {
3280 NEW_ARGLOADA (cfg, ins, i);
3281 handle_stobj (cfg, bblock, ins, sp [i], sp [i]->cil_code, ins->klass, FALSE, FALSE);
3284 MONO_ADD_INS (bblock, ins);
3286 MONO_INST_NEW (cfg, ins, CEE_JMP);
3288 ins->inst_p0 = cmethod;
3289 ins->inst_p1 = arg_array [0];
3290 MONO_ADD_INS (bblock, ins);
3291 start_new_bblock = 1;
3292 /* skip CEE_RET as well */
3297 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_opcode_for_method (cfg, cmethod, fsig, sp))) {
3300 if (MONO_TYPE_IS_VOID (fsig->ret)) {
3301 MONO_ADD_INS (bblock, ins);
3303 type_to_eval_stack_type (fsig->ret, ins);
3312 handle_loaded_temps (cfg, bblock, stack_start, sp);
3314 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3315 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
3316 mono_method_check_inlining (cfg, cmethod) &&
3317 !g_list_find (dont_inline, cmethod)) {
3319 MonoBasicBlock *ebblock;
3321 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3325 GET_BBLOCK (cfg, bbhash, bblock, ip);
3326 ebblock->next_bb = bblock;
3327 link_bblock (cfg, ebblock, bblock);
3329 if (!MONO_TYPE_IS_VOID (fsig->ret))
3332 /* indicates start of a new block, and triggers a load of all
3333 stack arguments at bb boundarie */
3336 inline_costs += costs;
3341 inline_costs += 10 * num_calls++;
3343 /* tail recursion elimination */
3344 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == method && ip [5] == CEE_RET) {
3345 gboolean has_vtargs = FALSE;
3348 /* keep it simple */
3349 for (i = fsig->param_count - 1; i >= 0; i--) {
3350 if (MONO_TYPE_ISSTRUCT (cmethod->signature->params [i]))
3355 for (i = 0; i < n; ++i) {
3356 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3358 MONO_ADD_INS (bblock, ins);
3360 MONO_INST_NEW (cfg, ins, CEE_BR);
3362 MONO_ADD_INS (bblock, ins);
3363 tblock = start_bblock->out_bb [0];
3364 link_bblock (cfg, bblock, tblock);
3365 ins->inst_target_bb = tblock;
3366 start_new_bblock = 1;
3369 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3370 /* just create a dummy - the value is never used */
3371 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
3372 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3380 if (*ip == CEE_CALLI) {
3382 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
3383 NEW_TEMPLOAD (cfg, *sp, temp);
3387 } else if (array_rank) {
3390 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
3391 if (sp [fsig->param_count]->type == STACK_OBJ) {
3392 MonoInst *iargs [2];
3393 MonoInst *array, *to_store, *store;
3395 handle_loaded_temps (cfg, bblock, stack_start, sp);
3397 array = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
3398 NEW_TEMPSTORE (cfg, store, array->inst_c0, sp [0]);
3399 store->cil_code = ip;
3400 MONO_ADD_INS (bblock, store);
3401 NEW_TEMPLOAD (cfg, iargs [0], array->inst_c0);
3403 to_store = mono_compile_create_var (cfg, type_from_stack_type (sp [fsig->param_count]), OP_LOCAL);
3404 NEW_TEMPSTORE (cfg, store, to_store->inst_c0, sp [fsig->param_count]);
3405 store->cil_code = ip;
3406 MONO_ADD_INS (bblock, store);
3407 NEW_TEMPLOAD (cfg, iargs [1], to_store->inst_c0);
3410 * We first save the args for the call so that the args are copied to the stack
3411 * and a new instruction tree for them is created. If we don't do this,
3412 * the same MonoInst is added to two different trees and this is not
3415 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref_check, iargs, ip);
3417 NEW_TEMPLOAD (cfg, sp [0], array->inst_c0);
3418 NEW_TEMPLOAD (cfg, sp [fsig->param_count], to_store->inst_c0);
3421 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
3422 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
3424 if (ins->opcode == CEE_STOBJ) {
3425 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
3427 MONO_ADD_INS (bblock, ins);
3430 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
3431 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3432 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
3436 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
3437 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3440 g_assert_not_reached ();
3444 if (0 && CODE_IS_STLOC (ip + 5) && (!MONO_TYPE_ISSTRUCT (fsig->ret)) && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)) {
3445 /* no need to spill */
3446 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
3449 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) {
3450 NEW_TEMPLOAD (cfg, *sp, temp);
3460 if (cfg->method != method) {
3461 /* return from inlined methode */
3466 //g_assert (returnvar != -1);
3467 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
3468 store->cil_code = sp [0]->cil_code;
3469 if (store->opcode == CEE_STOBJ) {
3470 g_assert_not_reached ();
3471 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
3472 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
3474 MONO_ADD_INS (bblock, store);
3478 g_assert (!return_var);
3481 MONO_INST_NEW (cfg, ins, CEE_NOP);
3482 ins->opcode = mono_type_to_stind (method->signature->ret);
3483 if (ins->opcode == CEE_STOBJ) {
3484 NEW_RETLOADA (cfg, ins);
3485 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3487 ins->opcode = OP_SETRET;
3489 ins->inst_i0 = *sp;;
3490 ins->inst_i1 = NULL;
3491 MONO_ADD_INS (bblock, ins);
3495 if (sp != stack_start)
3497 MONO_INST_NEW (cfg, ins, CEE_BR);
3498 ins->cil_code = ip++;
3499 ins->inst_target_bb = end_bblock;
3500 MONO_ADD_INS (bblock, ins);
3501 link_bblock (cfg, bblock, end_bblock);
3502 start_new_bblock = 1;
3506 MONO_INST_NEW (cfg, ins, CEE_BR);
3507 ins->cil_code = ip++;
3508 MONO_ADD_INS (bblock, ins);
3509 target = ip + 1 + (signed char)(*ip);
3511 GET_BBLOCK (cfg, bbhash, tblock, target);
3512 link_bblock (cfg, bblock, tblock);
3513 CHECK_BBLOCK (target, ip, tblock);
3514 ins->inst_target_bb = tblock;
3515 if (sp != stack_start) {
3516 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3519 start_new_bblock = 1;
3526 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3527 ins->cil_code = ip++;
3528 target = ip + 1 + *(signed char*)ip;
3530 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
3531 if (sp != stack_start) {
3532 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3549 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3550 ins->cil_code = ip++;
3551 target = ip + 1 + *(signed char*)ip;
3554 if (sp != stack_start) {
3555 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3562 MONO_INST_NEW (cfg, ins, CEE_BR);
3563 ins->cil_code = ip++;
3564 MONO_ADD_INS (bblock, ins);
3565 target = ip + 4 + (gint32)read32(ip);
3567 GET_BBLOCK (cfg, bbhash, tblock, target);
3568 link_bblock (cfg, bblock, tblock);
3569 CHECK_BBLOCK (target, ip, tblock);
3570 ins->inst_target_bb = tblock;
3571 if (sp != stack_start) {
3572 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3575 start_new_bblock = 1;
3582 MONO_INST_NEW (cfg, ins, *ip);
3583 ins->cil_code = ip++;
3584 target = ip + 4 + (gint32)read32(ip);
3586 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
3587 if (sp != stack_start) {
3588 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3605 MONO_INST_NEW (cfg, ins, *ip);
3606 ins->cil_code = ip++;
3607 target = ip + 4 + (gint32)read32(ip);
3610 if (sp != stack_start) {
3611 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3619 n = read32 (ip + 1);
3620 MONO_INST_NEW (cfg, ins, *ip);
3622 ins->inst_left = *sp;
3623 if (ins->inst_left->type != STACK_I4) goto unverified;
3626 CHECK_OPSIZE (n * sizeof (guint32));
3627 target = ip + n * sizeof (guint32);
3628 MONO_ADD_INS (bblock, ins);
3629 GET_BBLOCK (cfg, bbhash, tblock, target);
3630 link_bblock (cfg, bblock, tblock);
3631 ins->klass = GUINT_TO_POINTER (n);
3632 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
3633 ins->inst_many_bb [n] = tblock;
3635 for (i = 0; i < n; ++i) {
3636 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
3637 link_bblock (cfg, bblock, tblock);
3638 ins->inst_many_bb [i] = tblock;
3641 if (sp != stack_start) {
3642 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3659 MONO_INST_NEW (cfg, ins, *ip);
3664 ins->type = ldind_type [*ip - CEE_LDIND_I1];
3665 ins->flags |= ins_flag;
3677 MONO_INST_NEW (cfg, ins, *ip);
3678 ins->cil_code = ip++;
3680 handle_loaded_temps (cfg, bblock, stack_start, sp);
3681 MONO_ADD_INS (bblock, ins);
3682 ins->inst_i0 = sp [0];
3683 ins->inst_i1 = sp [1];
3684 ins->flags |= ins_flag;
3703 /* special case that gives a nice speedup and happens to workaorund a ppc jit but (for the release)
3704 * later apply the speedup to the left shift as well
3707 if ((ins->opcode == OP_LSHR_UN) && (ins->type == STACK_I8)
3708 && (ins->inst_right->opcode == OP_ICONST) && (ins->inst_right->inst_c0 == 32)) {
3709 ins->opcode = OP_LONG_SHRUN_32;
3710 /*g_print ("applied long shr speedup to %s\n", cfg->method->name);*/
3714 if (mono_find_jit_opcode_emulation (ins->opcode)) {
3715 MonoInst *store, *temp, *load;
3717 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3718 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3719 store->cil_code = ins->cil_code;
3720 MONO_ADD_INS (bblock, store);
3721 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
3722 load->cil_code = ins->cil_code;
3724 /*g_print ("found emulation for %d\n", ins->opcode);*/
3738 case CEE_CONV_OVF_I8:
3739 case CEE_CONV_OVF_U8:
3743 if (mono_find_jit_opcode_emulation (ins->opcode)) {
3744 MonoInst *store, *temp, *load;
3746 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3747 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3748 store->cil_code = ins->cil_code;
3749 MONO_ADD_INS (bblock, store);
3750 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
3751 load->cil_code = ins->cil_code;
3753 /*g_print ("found emulation for %d\n", ins->opcode);*/
3757 case CEE_CONV_OVF_I4:
3758 case CEE_CONV_OVF_I1:
3759 case CEE_CONV_OVF_I2:
3760 case CEE_CONV_OVF_I:
3761 case CEE_CONV_OVF_U:
3764 if (sp [-1]->type == STACK_R8) {
3765 ADD_UNOP (CEE_CONV_OVF_I8);
3773 case CEE_CONV_OVF_U1:
3774 case CEE_CONV_OVF_U2:
3775 case CEE_CONV_OVF_U4:
3778 if (sp [-1]->type == STACK_R8) {
3779 ADD_UNOP (CEE_CONV_OVF_U8);
3787 case CEE_CONV_OVF_I1_UN:
3788 case CEE_CONV_OVF_I2_UN:
3789 case CEE_CONV_OVF_I4_UN:
3790 case CEE_CONV_OVF_I8_UN:
3791 case CEE_CONV_OVF_U1_UN:
3792 case CEE_CONV_OVF_U2_UN:
3793 case CEE_CONV_OVF_U4_UN:
3794 case CEE_CONV_OVF_U8_UN:
3795 case CEE_CONV_OVF_I_UN:
3796 case CEE_CONV_OVF_U_UN:
3804 token = read32 (ip + 1);
3805 if (method->wrapper_type != MONO_WRAPPER_NONE)
3806 klass = mono_method_get_wrapper_data (method, token);
3808 klass = mono_class_get_full (image, token, generic_context);
3810 mono_class_init (klass);
3812 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3813 MonoInst *store, *load;
3814 MONO_INST_NEW (cfg, load, CEE_LDIND_REF);
3815 load->cil_code = ip;
3816 load->inst_i0 = sp [1];
3817 load->type = ldind_type [CEE_LDIND_REF];
3818 load->flags |= ins_flag;
3819 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
3820 store->cil_code = ip;
3821 handle_loaded_temps (cfg, bblock, stack_start, sp);
3822 MONO_ADD_INS (bblock, store);
3823 store->inst_i0 = sp [0];
3824 store->inst_i1 = load;
3825 store->flags |= ins_flag;
3827 n = mono_class_value_size (klass, NULL);
3828 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3830 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3831 copy->inst_left = sp [0];
3832 copy->inst_right = sp [1];
3833 copy->cil_code = ip;
3835 MONO_ADD_INS (bblock, copy);
3837 MonoInst *iargs [3];
3840 NEW_ICONST (cfg, iargs [2], n);
3841 iargs [2]->cil_code = ip;
3843 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3850 MonoInst *iargs [3];
3854 token = read32 (ip + 1);
3855 if (method->wrapper_type != MONO_WRAPPER_NONE)
3856 klass = mono_method_get_wrapper_data (method, token);
3858 klass = mono_class_get_full (image, token, generic_context);
3860 mono_class_init (klass);
3861 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3862 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3864 ins->inst_i0 = sp [0];
3865 ins->type = ldind_type [CEE_LDIND_REF];
3866 ins->flags |= ins_flag;
3872 n = mono_class_value_size (klass, NULL);
3873 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3874 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3875 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3877 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3878 copy->inst_left = iargs [0];
3879 copy->inst_right = *sp;
3880 copy->cil_code = ip;
3882 MONO_ADD_INS (bblock, copy);
3885 NEW_ICONST (cfg, iargs [2], n);
3886 iargs [2]->cil_code = ip;
3888 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3890 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3898 CHECK_STACK_OVF (1);
3900 n = read32 (ip + 1);
3902 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3904 MonoInst *iargs [1];
3906 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
3907 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
3908 NEW_TEMPLOAD (cfg, *sp, temp);
3912 if (cfg->opt & MONO_OPT_SHARED) {
3914 MonoInst *iargs [3];
3916 if (mono_compile_aot) {
3917 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n);
3920 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3921 NEW_IMAGECONST (cfg, iargs [1], image);
3922 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
3923 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
3924 NEW_TEMPLOAD (cfg, *sp, temp);
3925 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3927 if (mono_compile_aot)
3928 NEW_LDSTRCONST (cfg, ins, image, n);
3930 NEW_PCONST (cfg, ins, NULL);
3932 ins->type = STACK_OBJ;
3933 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3943 MonoInst *iargs [2];
3944 MonoMethodSignature *fsig;
3948 token = read32 (ip + 1);
3949 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3950 cmethod = mono_method_get_wrapper_data (method, token);
3952 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3953 fsig = mono_method_get_signature (cmethod, image, token);
3955 mono_class_init (cmethod->klass);
3957 n = fsig->param_count;
3960 /* move the args to allow room for 'this' in the first position */
3966 handle_loaded_temps (cfg, bblock, stack_start, sp);
3969 if (cmethod->klass->parent == mono_defaults.array_class) {
3970 NEW_METHODCONST (cfg, *sp, cmethod);
3971 temp = mono_emit_native_call (cfg, bblock, mono_array_new_va, fsig, sp, ip, FALSE);
3973 } else if (cmethod->string_ctor) {
3974 /* we simply pass a null pointer */
3975 NEW_PCONST (cfg, *sp, NULL);
3976 /* now call the string ctor */
3977 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
3979 if (cmethod->klass->valuetype) {
3980 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
3981 temp = iargs [0]->inst_c0;
3982 NEW_TEMPLOADA (cfg, *sp, temp);
3984 if (cfg->opt & MONO_OPT_SHARED) {
3985 NEW_DOMAINCONST (cfg, iargs [0]);
3986 NEW_CLASSCONST (cfg, iargs [1], cmethod->klass);
3988 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3990 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
3991 NEW_VTABLECONST (cfg, iargs [0], vtable);
3992 if (cmethod->klass->has_finalize || cmethod->klass->marshalbyref || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
3993 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
3995 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
3997 NEW_TEMPLOAD (cfg, *sp, temp);
4000 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
4001 mono_method_check_inlining (cfg, cmethod) &&
4002 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
4003 !g_list_find (dont_inline, cmethod)) {
4005 MonoBasicBlock *ebblock;
4006 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
4011 GET_BBLOCK (cfg, bbhash, bblock, ip);
4012 ebblock->next_bb = bblock;
4013 link_bblock (cfg, ebblock, bblock);
4015 NEW_TEMPLOAD (cfg, *sp, temp);
4018 /* indicates start of a new block, and triggers a load
4019 of all stack arguments at bb boundarie */
4022 inline_costs += costs;
4026 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
4029 /* now call the actual ctor */
4030 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
4034 NEW_TEMPLOAD (cfg, *sp, temp);
4045 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4046 mono_class_init (klass);
4048 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4050 MonoMethod *mono_isinst;
4051 MonoInst *iargs [1];
4052 MonoBasicBlock *ebblock;
4056 mono_isinst = mono_marshal_get_isinst (klass);
4059 costs = inline_method (cfg, mono_isinst, mono_isinst->signature, bblock,
4060 iargs, ip, real_offset, dont_inline, &ebblock);
4062 g_assert (costs > 0);
4067 GET_BBLOCK (cfg, bbhash, bblock, ip);
4068 ebblock->next_bb = bblock;
4069 link_bblock (cfg, ebblock, bblock);
4071 temp = iargs [0]->inst_i0->inst_c0;
4072 NEW_TEMPLOAD (cfg, *sp, temp);
4076 inline_costs += costs;
4080 MONO_INST_NEW (cfg, ins, *ip);
4081 ins->type = STACK_OBJ;
4082 ins->inst_left = *sp;
4083 ins->inst_newa_class = klass;
4089 case CEE_UNBOX_ANY: {
4090 MonoInst *add, *vtoffset;
4091 MonoInst *iargs [3];
4096 token = read32 (ip + 1);
4097 if (method->wrapper_type != MONO_WRAPPER_NONE)
4098 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4100 klass = mono_class_get_full (image, token, generic_context);
4101 mono_class_init (klass);
4103 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4105 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4106 MonoMethod *mono_castclass;
4107 MonoInst *iargs [1];
4108 MonoBasicBlock *ebblock;
4112 mono_castclass = mono_marshal_get_castclass (klass);
4115 costs = inline_method (cfg, mono_castclass, mono_castclass->signature, bblock,
4116 iargs, ip, real_offset, dont_inline, &ebblock);
4118 g_assert (costs > 0);
4123 GET_BBLOCK (cfg, bbhash, bblock, ip);
4124 ebblock->next_bb = bblock;
4125 link_bblock (cfg, ebblock, bblock);
4127 temp = iargs [0]->inst_i0->inst_c0;
4128 NEW_TEMPLOAD (cfg, *sp, temp);
4132 inline_costs += costs;
4135 MONO_INST_NEW (cfg, ins, CEE_CASTCLASS);
4136 ins->type = STACK_OBJ;
4137 ins->inst_left = *sp;
4139 ins->inst_newa_class = klass;
4147 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
4148 ins->type = STACK_OBJ;
4149 ins->inst_left = *sp;
4151 ins->inst_newa_class = klass;
4154 MONO_INST_NEW (cfg, add, CEE_ADD);
4155 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4156 add->inst_left = ins;
4157 add->inst_right = vtoffset;
4158 add->type = STACK_MP;
4162 n = mono_class_value_size (klass, NULL);
4163 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
4164 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
4165 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
4167 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
4168 copy->inst_left = iargs [0];
4169 copy->inst_right = *sp;
4170 copy->cil_code = ip;
4172 MONO_ADD_INS (bblock, copy);
4175 NEW_ICONST (cfg, iargs [2], n);
4176 iargs [2]->cil_code = ip;
4178 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
4180 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
4186 MonoInst *add, *vtoffset;
4191 token = read32 (ip + 1);
4192 if (method->wrapper_type != MONO_WRAPPER_NONE)
4193 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4195 klass = mono_class_get_full (image, token, generic_context);
4196 mono_class_init (klass);
4198 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
4199 ins->type = STACK_OBJ;
4200 ins->inst_left = *sp;
4202 ins->inst_newa_class = klass;
4205 MONO_INST_NEW (cfg, add, CEE_ADD);
4206 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4207 add->inst_left = ins;
4208 add->inst_right = vtoffset;
4209 add->type = STACK_MP;
4219 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4220 mono_class_init (klass);
4222 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4224 MonoMethod *mono_castclass;
4225 MonoInst *iargs [1];
4226 MonoBasicBlock *ebblock;
4230 mono_castclass = mono_marshal_get_castclass (klass);
4233 costs = inline_method (cfg, mono_castclass, mono_castclass->signature, bblock,
4234 iargs, ip, real_offset, dont_inline, &ebblock);
4236 g_assert (costs > 0);
4241 GET_BBLOCK (cfg, bbhash, bblock, ip);
4242 ebblock->next_bb = bblock;
4243 link_bblock (cfg, ebblock, bblock);
4245 temp = iargs [0]->inst_i0->inst_c0;
4246 NEW_TEMPLOAD (cfg, *sp, temp);
4250 inline_costs += costs;
4253 MONO_INST_NEW (cfg, ins, *ip);
4254 ins->type = STACK_OBJ;
4255 ins->inst_left = *sp;
4257 ins->inst_newa_class = klass;
4265 MONO_INST_NEW (cfg, ins, *ip);
4267 ins->inst_left = *sp;
4268 ins->cil_code = ip++;
4269 MONO_ADD_INS (bblock, ins);
4271 start_new_bblock = 1;
4276 MonoInst *offset_ins;
4277 MonoClassField *field;
4278 MonoBasicBlock *ebblock;
4282 if (*ip == CEE_STFLD) {
4289 // FIXME: enable this test later.
4290 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
4293 token = read32 (ip + 1);
4294 field = mono_field_from_token (image, token, &klass, generic_context);
4295 mono_class_init (klass);
4297 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
4298 /* FIXME: mark instructions for use in SSA */
4299 if (*ip == CEE_STFLD) {
4300 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound) {
4301 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
4302 MonoInst *iargs [5];
4305 NEW_CLASSCONST (cfg, iargs [1], klass);
4306 NEW_FIELDCONST (cfg, iargs [2], field);
4307 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
4311 if (cfg->opt & MONO_OPT_INLINE) {
4312 costs = inline_method (cfg, stfld_wrapper, stfld_wrapper->signature, bblock,
4313 iargs, ip, real_offset, dont_inline, &ebblock);
4314 g_assert (costs > 0);
4319 GET_BBLOCK (cfg, bbhash, bblock, ip);
4320 ebblock->next_bb = bblock;
4321 link_bblock (cfg, ebblock, bblock);
4323 /* indicates start of a new block, and triggers a load
4324 of all stack arguments at bb boundarie */
4327 inline_costs += costs;
4330 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, stfld_wrapper->signature, iargs, ip, NULL);
4334 NEW_ICONST (cfg, offset_ins, foffset);
4335 MONO_INST_NEW (cfg, ins, CEE_ADD);
4337 ins->inst_left = *sp;
4338 ins->inst_right = offset_ins;
4339 ins->type = STACK_MP;
4341 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4342 store->cil_code = ip;
4343 store->inst_left = ins;
4344 store->inst_right = sp [1];
4345 handle_loaded_temps (cfg, bblock, stack_start, sp);
4346 store->flags |= ins_flag;
4348 if (store->opcode == CEE_STOBJ) {
4349 handle_stobj (cfg, bblock, ins, sp [1], ip,
4350 mono_class_from_mono_type (field->type), FALSE, FALSE);
4352 MONO_ADD_INS (bblock, store);
4355 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound) {
4356 /* fixme: we need to inline that call somehow */
4357 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
4358 MonoInst *iargs [4];
4362 NEW_CLASSCONST (cfg, iargs [1], klass);
4363 NEW_FIELDCONST (cfg, iargs [2], field);
4364 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
4365 if (cfg->opt & MONO_OPT_INLINE) {
4366 costs = inline_method (cfg, ldfld_wrapper, ldfld_wrapper->signature, bblock,
4367 iargs, ip, real_offset, dont_inline, &ebblock);
4368 g_assert (costs > 0);
4373 GET_BBLOCK (cfg, bbhash, bblock, ip);
4374 ebblock->next_bb = bblock;
4375 link_bblock (cfg, ebblock, bblock);
4377 temp = iargs [0]->inst_i0->inst_c0;
4379 if (*ip == CEE_LDFLDA) {
4380 /* not sure howto handle this */
4381 NEW_TEMPLOADA (cfg, *sp, temp);
4383 NEW_TEMPLOAD (cfg, *sp, temp);
4387 /* indicates start of a new block, and triggers a load of
4388 all stack arguments at bb boundarie */
4391 inline_costs += costs;
4394 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, ldfld_wrapper->signature, iargs, ip, NULL);
4395 if (*ip == CEE_LDFLDA) {
4396 /* not sure howto handle this */
4397 NEW_TEMPLOADA (cfg, *sp, temp);
4399 NEW_TEMPLOAD (cfg, *sp, temp);
4404 NEW_ICONST (cfg, offset_ins, foffset);
4405 MONO_INST_NEW (cfg, ins, CEE_ADD);
4407 ins->inst_left = *sp;
4408 ins->inst_right = offset_ins;
4409 ins->type = STACK_MP;
4411 if (*ip == CEE_LDFLDA) {
4415 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4416 type_to_eval_stack_type (field->type, load);
4417 load->cil_code = ip;
4418 load->inst_left = ins;
4419 load->flags |= ins_flag;
4431 MonoClassField *field;
4432 gpointer addr = NULL;
4435 token = read32 (ip + 1);
4437 field = mono_field_from_token (image, token, &klass, generic_context);
4438 mono_class_init (klass);
4440 if ((*ip) == CEE_STSFLD)
4441 handle_loaded_temps (cfg, bblock, stack_start, sp);
4443 if (cfg->domain->special_static_fields)
4444 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
4446 if ((cfg->opt & MONO_OPT_SHARED) || (mono_compile_aot && addr)) {
4448 MonoInst *iargs [2];
4449 g_assert (field->parent);
4450 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
4451 NEW_FIELDCONST (cfg, iargs [1], field);
4452 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
4453 NEW_TEMPLOAD (cfg, ins, temp);
4456 vtable = mono_class_vtable (cfg->domain, klass);
4458 if ((!vtable->initialized || mono_compile_aot) && !(klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (klass, method)) {
4459 guint8 *tramp = mono_create_class_init_trampoline (vtable);
4460 mono_emit_native_call (cfg, bblock, tramp,
4461 helper_sig_class_init_trampoline,
4463 if (cfg->verbose_level > 2)
4464 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
4466 if (cfg->run_cctors)
4467 mono_runtime_class_init (vtable);
4469 addr = (char*)vtable->data + field->offset;
4471 if (mono_compile_aot)
4472 NEW_SFLDACONST (cfg, ins, field);
4474 NEW_PCONST (cfg, ins, addr);
4478 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
4479 * This could be later optimized to do just a couple of
4480 * memory dereferences with constant offsets.
4483 MonoInst *iargs [1];
4484 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
4485 temp = mono_emit_jit_icall (cfg, bblock, mono_get_special_static_data, iargs, ip);
4486 NEW_TEMPLOAD (cfg, ins, temp);
4490 /* FIXME: mark instructions for use in SSA */
4491 if (*ip == CEE_LDSFLDA) {
4493 } else if (*ip == CEE_STSFLD) {
4497 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4498 store->cil_code = ip;
4499 store->inst_left = ins;
4500 store->inst_right = sp [0];
4501 store->flags |= ins_flag;
4504 if (store->opcode == CEE_STOBJ) {
4505 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
4507 MONO_ADD_INS (bblock, store);
4509 gboolean is_const = FALSE;
4510 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4511 if (!((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) &&
4512 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
4513 gpointer addr = (char*)vtable->data + field->offset;
4514 /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
4516 switch (field->type->type) {
4517 case MONO_TYPE_BOOLEAN:
4519 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
4523 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
4526 case MONO_TYPE_CHAR:
4528 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
4532 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
4537 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
4541 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
4546 case MONO_TYPE_STRING:
4547 case MONO_TYPE_OBJECT:
4548 case MONO_TYPE_CLASS:
4549 case MONO_TYPE_SZARRAY:
4551 case MONO_TYPE_FNPTR:
4552 case MONO_TYPE_ARRAY:
4553 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
4554 type_to_eval_stack_type (field->type, *sp);
4559 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
4560 sp [0]->type = STACK_I8;
4561 sp [0]->inst_l = *((gint64 *)addr);
4566 case MONO_TYPE_VALUETYPE:
4575 CHECK_STACK_OVF (1);
4576 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4577 type_to_eval_stack_type (field->type, load);
4578 load->cil_code = ip;
4579 load->inst_left = ins;
4581 load->flags |= ins_flag;
4583 /* fixme: dont see the problem why this does not work */
4584 //cfg->disable_aot = TRUE;
4594 token = read32 (ip + 1);
4595 if (method->wrapper_type != MONO_WRAPPER_NONE)
4596 klass = mono_method_get_wrapper_data (method, token);
4598 klass = mono_class_get_full (image, token, generic_context);
4599 mono_class_init (klass);
4600 n = mono_type_to_stind (&klass->byval_arg);
4601 if (n == CEE_STOBJ) {
4602 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
4604 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
4606 MONO_INST_NEW (cfg, store, n);
4607 store->cil_code = ip;
4608 store->inst_left = sp [0];
4609 store->inst_right = sp [1];
4610 store->flags |= ins_flag;
4611 MONO_ADD_INS (bblock, store);
4623 token = read32 (ip + 1);
4624 if (method->wrapper_type != MONO_WRAPPER_NONE)
4625 klass = mono_method_get_wrapper_data (method, token);
4627 klass = mono_class_get_full (image, token, generic_context);
4628 mono_class_init (klass);
4630 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4635 *sp++ = handle_box (cfg, bblock, val, ip, klass);
4642 MONO_INST_NEW (cfg, ins, *ip);
4647 token = read32 (ip + 1);
4649 /* allocate the domainvar - becaus this is used in decompose_foreach */
4650 if (cfg->opt & MONO_OPT_SHARED)
4651 mono_get_domainvar (cfg);
4653 if (method->wrapper_type != MONO_WRAPPER_NONE)
4654 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4656 klass = mono_class_get_full (image, token, generic_context);
4658 mono_class_init (klass);
4659 ins->inst_newa_class = klass;
4660 ins->inst_newa_len = *sp;
4661 ins->type = STACK_OBJ;
4665 * we store the object so calls to create the array are not interleaved
4666 * with the arguments of other calls.
4669 MonoInst *store, *temp, *load;
4671 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
4672 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4673 store->cil_code = ins->cil_code;
4674 MONO_ADD_INS (bblock, store);
4675 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
4676 load->cil_code = ins->cil_code;
4683 MONO_INST_NEW (cfg, ins, *ip);
4684 ins->cil_code = ip++;
4686 ins->inst_left = *sp;
4687 ins->type = STACK_PTR;
4695 if (method->wrapper_type != MONO_WRAPPER_NONE)
4696 klass = (MonoClass*)mono_method_get_wrapper_data (method, read32 (ip + 1));
4698 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4699 mono_class_init (klass);
4700 NEW_LDELEMA (cfg, ins, sp, klass);
4710 token = read32 (ip + 1);
4711 klass = mono_class_get_full (image, token, generic_context);
4712 mono_class_init (klass);
4713 NEW_LDELEMA (cfg, load, sp, klass);
4714 load->cil_code = ip;
4715 MONO_INST_NEW (cfg, ins, mono_type_to_ldind (&klass->byval_arg));
4717 ins->inst_left = load;
4719 type_to_eval_stack_type (&klass->byval_arg, ins);
4733 case CEE_LDELEM_REF: {
4737 * ldind.x (ldelema (array, index))
4738 * ldelema does the bounds check
4742 klass = array_access_to_klass (*ip);
4743 NEW_LDELEMA (cfg, load, sp, klass);
4744 load->cil_code = ip;
4745 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
4747 ins->inst_left = load;
4749 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
4759 case CEE_STELEM_R8: {
4763 * stind.x (ldelema (array, index), val)
4764 * ldelema does the bounds check
4768 klass = array_access_to_klass (*ip);
4769 NEW_LDELEMA (cfg, load, sp, klass);
4770 load->cil_code = ip;
4771 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
4773 ins->inst_left = load;
4774 ins->inst_right = sp [2];
4776 handle_loaded_temps (cfg, bblock, stack_start, sp);
4777 MONO_ADD_INS (bblock, ins);
4785 * stind.x (ldelema (array, index), val)
4786 * ldelema does the bounds check
4791 token = read32 (ip + 1);
4792 klass = mono_class_get_full (image, token, generic_context);
4793 mono_class_init (klass);
4794 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4795 MonoInst *iargs [3];
4796 handle_loaded_temps (cfg, bblock, stack_start, sp);
4802 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4804 NEW_LDELEMA (cfg, load, sp, klass);
4805 load->cil_code = ip;
4807 n = mono_type_to_stind (&klass->byval_arg);
4809 handle_stobj (cfg, bblock, load, sp [2], ip, klass, FALSE, FALSE);
4811 MONO_INST_NEW (cfg, ins, n);
4813 ins->inst_left = load;
4814 ins->inst_right = sp [2];
4815 handle_loaded_temps (cfg, bblock, stack_start, sp);
4816 MONO_ADD_INS (bblock, ins);
4823 case CEE_STELEM_REF: {
4824 MonoInst *iargs [3];
4829 handle_loaded_temps (cfg, bblock, stack_start, sp);
4835 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4839 NEW_GROUP (cfg, group, sp [0], sp [1]);
4840 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
4842 ins->inst_left = group;
4843 ins->inst_right = sp [2];
4844 MONO_ADD_INS (bblock, ins);
4851 case CEE_CKFINITE: {
4852 MonoInst *store, *temp;
4855 /* this instr. can throw exceptions as side effect,
4856 * so we cant eliminate dead code which contains CKFINITE opdodes.
4857 * Spilling to memory makes sure that we always perform
4861 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
4863 ins->inst_left = sp [-1];
4864 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
4866 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4867 store->cil_code = ip;
4868 MONO_ADD_INS (bblock, store);
4870 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
4877 MONO_INST_NEW (cfg, ins, *ip);
4880 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4881 mono_class_init (klass);
4882 ins->type = STACK_MP;
4883 ins->inst_left = *sp;
4885 ins->inst_newa_class = klass;
4890 case CEE_MKREFANY: {
4891 MonoInst *loc, *klassconst;
4894 MONO_INST_NEW (cfg, ins, *ip);
4897 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4898 mono_class_init (klass);
4901 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
4902 NEW_TEMPLOADA (cfg, ins->inst_right, loc->inst_c0);
4904 NEW_PCONST (cfg, klassconst, klass);
4905 NEW_GROUP (cfg, ins->inst_left, *sp, klassconst);
4907 MONO_ADD_INS (bblock, ins);
4909 NEW_TEMPLOAD (cfg, *sp, loc->inst_c0);
4916 MonoClass *handle_class;
4918 CHECK_STACK_OVF (1);
4921 n = read32 (ip + 1);
4923 handle = mono_ldtoken (image, n, &handle_class, generic_context);
4924 mono_class_init (handle_class);
4926 if (cfg->opt & MONO_OPT_SHARED) {
4928 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
4930 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
4932 NEW_IMAGECONST (cfg, iargs [0], image);
4933 NEW_ICONST (cfg, iargs [1], n);
4934 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
4935 NEW_TEMPLOAD (cfg, res, temp);
4936 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4937 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
4938 MONO_ADD_INS (bblock, store);
4939 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4941 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method_full (image, read32 (ip + 6), NULL, generic_context)) &&
4942 (cmethod->klass == mono_defaults.monotype_class->parent) &&
4943 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
4944 MonoClass *tclass = mono_class_from_mono_type (handle);
4945 mono_class_init (tclass);
4946 if (mono_compile_aot)
4947 NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n);
4949 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
4950 ins->type = STACK_OBJ;
4951 ins->klass = cmethod->klass;
4954 if (mono_compile_aot)
4955 NEW_LDTOKENCONST (cfg, ins, image, n);
4957 NEW_PCONST (cfg, ins, handle);
4958 ins->type = STACK_VTYPE;
4959 ins->klass = handle_class;
4975 case CEE_ADD_OVF_UN:
4977 case CEE_MUL_OVF_UN:
4979 case CEE_SUB_OVF_UN:
4982 if (mono_find_jit_opcode_emulation (ins->opcode)) {
4983 MonoInst *store, *temp, *load;
4985 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
4986 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4987 store->cil_code = ins->cil_code;
4988 MONO_ADD_INS (bblock, store);
4989 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
4990 load->cil_code = ins->cil_code;
4992 /*g_print ("found emulation for %d\n", ins->opcode);*/
4996 case CEE_ENDFINALLY:
4997 MONO_INST_NEW (cfg, ins, *ip);
4998 MONO_ADD_INS (bblock, ins);
4999 ins->cil_code = ip++;
5000 start_new_bblock = 1;
5003 * Control will leave the method so empty the stack, otherwise
5004 * the next basic block will start with a nonempty stack.
5006 while (sp != stack_start) {
5007 MONO_INST_NEW (cfg, ins, CEE_POP);
5011 MONO_ADD_INS (bblock, ins);
5017 if (*ip == CEE_LEAVE) {
5019 target = ip + 5 + (gint32)read32(ip + 1);
5022 target = ip + 2 + (signed char)(ip [1]);
5025 /* empty the stack */
5026 while (sp != stack_start) {
5027 MONO_INST_NEW (cfg, ins, CEE_POP);
5031 MONO_ADD_INS (bblock, ins);
5035 * If this leave statement is in a catch block, check for a
5036 * pending exception, and rethrow it if necessary.
5038 for (i = 0; i < header->num_clauses; ++i) {
5039 MonoExceptionClause *clause = &header->clauses [i];
5040 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code)) {
5043 temp = mono_emit_jit_icall (cfg, bblock, mono_thread_get_pending_exception, NULL, ip);
5044 NEW_TEMPLOAD (cfg, *sp, temp);
5046 MONO_INST_NEW (cfg, ins, OP_THROW_OR_NULL);
5047 ins->inst_left = *sp;
5049 MONO_ADD_INS (bblock, ins);
5053 /* fixme: call fault handler ? */
5055 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
5057 for (tmp = handlers; tmp; tmp = tmp->next) {
5059 link_bblock (cfg, bblock, tblock);
5060 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
5062 ins->inst_target_bb = tblock;
5063 MONO_ADD_INS (bblock, ins);
5065 g_list_free (handlers);
5068 MONO_INST_NEW (cfg, ins, CEE_BR);
5070 MONO_ADD_INS (bblock, ins);
5071 GET_BBLOCK (cfg, bbhash, tblock, target);
5072 link_bblock (cfg, bblock, tblock);
5073 CHECK_BBLOCK (target, ip, tblock);
5074 ins->inst_target_bb = tblock;
5075 start_new_bblock = 1;
5077 if (*ip == CEE_LEAVE)
5086 MONO_INST_NEW (cfg, ins, *ip);
5088 handle_loaded_temps (cfg, bblock, stack_start, sp);
5089 MONO_ADD_INS (bblock, ins);
5090 ins->cil_code = ip++;
5091 ins->inst_i0 = sp [0];
5092 ins->inst_i1 = sp [1];
5100 /* trampoline mono specific opcodes */
5101 case MONO_CUSTOM_PREFIX: {
5103 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
5108 case CEE_MONO_FUNC1: {
5110 gpointer func = NULL;
5116 case MONO_MARSHAL_CONV_STR_LPWSTR:
5117 func = mono_string_to_utf16;
5119 case MONO_MARSHAL_CONV_LPWSTR_STR:
5120 func = mono_string_from_utf16;
5122 case MONO_MARSHAL_CONV_LPSTR_STR:
5123 func = mono_string_new_wrapper;
5125 case MONO_MARSHAL_CONV_STR_LPTSTR:
5126 case MONO_MARSHAL_CONV_STR_LPSTR:
5127 func = mono_string_to_utf8;
5129 case MONO_MARSHAL_CONV_STR_BSTR:
5130 func = mono_string_to_bstr;
5132 case MONO_MARSHAL_CONV_STR_TBSTR:
5133 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
5134 func = mono_string_to_ansibstr;
5136 case MONO_MARSHAL_CONV_SB_LPSTR:
5137 case MONO_MARSHAL_CONV_SB_LPTSTR:
5138 func = mono_string_builder_to_utf8;
5140 case MONO_MARSHAL_CONV_SB_LPWSTR:
5141 func = mono_string_builder_to_utf16;
5143 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
5144 func = mono_array_to_savearray;
5146 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
5147 func = mono_array_to_lparray;
5149 case MONO_MARSHAL_CONV_DEL_FTN:
5150 func = mono_delegate_to_ftnptr;
5152 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
5153 func = mono_marshal_string_array;
5155 case MONO_MARSHAL_CONV_STRARRAY_STRWLPARRAY:
5156 func = mono_marshal_string_array_to_unicode;
5159 g_warning ("unknown conversion %d\n", ip [2]);
5160 g_assert_not_reached ();
5163 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5164 NEW_TEMPLOAD (cfg, *sp, temp);
5168 inline_costs += 10 * num_calls++;
5172 case CEE_MONO_FUNC2: {
5174 gpointer func = NULL;
5180 case MONO_MARSHAL_CONV_ASANY:
5181 func = mono_marshal_asany;
5184 g_warning ("unknown conversion %d\n", ip [2]);
5185 g_assert_not_reached ();
5187 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5188 NEW_TEMPLOAD (cfg, *sp, temp);
5192 inline_costs += 10 * num_calls++;
5196 case CEE_MONO_PROC2: {
5197 gpointer func = NULL;
5203 case MONO_MARSHAL_CONV_LPSTR_SB:
5204 case MONO_MARSHAL_CONV_LPTSTR_SB:
5205 func = mono_string_utf8_to_builder;
5207 case MONO_MARSHAL_CONV_LPWSTR_SB:
5208 func = mono_string_utf16_to_builder;
5210 case MONO_MARSHAL_FREE_ARRAY:
5211 func = mono_marshal_free_array;
5213 case MONO_MARSHAL_CONV_ASANY:
5214 func = mono_marshal_asany;
5217 g_assert_not_reached ();
5220 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5222 inline_costs += 10 * num_calls++;
5225 case CEE_MONO_PROC3: {
5226 gpointer func = NULL;
5232 case MONO_MARSHAL_CONV_STR_BYVALSTR:
5233 func = mono_string_to_byvalstr;
5235 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
5236 func = mono_string_to_byvalwstr;
5238 case MONO_MARSHAL_FREE_ASANY:
5239 func = mono_marshal_free_asany;
5242 g_assert_not_reached ();
5245 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5247 inline_costs += 10 * num_calls++;
5253 mono_emit_jit_icall (cfg, bblock, g_free, sp, ip);
5255 inline_costs += 10 * num_calls++;
5257 case CEE_MONO_LDPTR:
5258 CHECK_STACK_OVF (1);
5260 token = read32 (ip + 2);
5261 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
5265 inline_costs += 10 * num_calls++;
5267 case CEE_MONO_VTADDR:
5270 MONO_INST_NEW (cfg, ins, OP_VTADDR);
5272 ins->type = STACK_MP;
5273 ins->inst_left = *sp;
5277 case CEE_MONO_NEWOBJ: {
5278 MonoInst *iargs [2];
5280 CHECK_STACK_OVF (1);
5282 token = read32 (ip + 2);
5283 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5284 mono_class_init (klass);
5285 NEW_DOMAINCONST (cfg, iargs [0]);
5286 NEW_CLASSCONST (cfg, iargs [1], klass);
5287 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
5288 NEW_TEMPLOAD (cfg, *sp, temp);
5291 inline_costs += 10 * num_calls++;
5294 case CEE_MONO_OBJADDR:
5297 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
5299 ins->type = STACK_MP;
5300 ins->inst_left = *sp;
5304 case CEE_MONO_LDNATIVEOBJ:
5307 token = read32 (ip + 2);
5308 klass = mono_method_get_wrapper_data (method, token);
5309 g_assert (klass->valuetype);
5310 mono_class_init (klass);
5311 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
5315 case CEE_MONO_RETOBJ:
5316 g_assert (cfg->ret);
5317 g_assert (method->signature->pinvoke);
5322 token = read32 (ip + 2);
5323 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5325 NEW_RETLOADA (cfg, ins);
5326 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
5328 if (sp != stack_start)
5331 MONO_INST_NEW (cfg, ins, CEE_BR);
5333 ins->inst_target_bb = end_bblock;
5334 MONO_ADD_INS (bblock, ins);
5335 link_bblock (cfg, bblock, end_bblock);
5336 start_new_bblock = 1;
5339 case CEE_MONO_CISINST:
5340 case CEE_MONO_CCASTCLASS: {
5345 token = read32 (ip + 2);
5346 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5347 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_CISINST) ? OP_CISINST : OP_CCASTCLASS);
5348 ins->type = STACK_I4;
5349 ins->inst_left = *sp;
5350 ins->inst_newa_class = klass;
5357 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
5366 /* somewhat similar to LDTOKEN */
5367 MonoInst *addr, *vtvar;
5368 CHECK_STACK_OVF (1);
5369 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
5371 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5372 addr->cil_code = ip;
5373 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
5375 ins->inst_left = addr;
5376 MONO_ADD_INS (bblock, ins);
5377 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5391 * The following transforms:
5392 * CEE_CEQ into OP_CEQ
5393 * CEE_CGT into OP_CGT
5394 * CEE_CGT_UN into OP_CGT_UN
5395 * CEE_CLT into OP_CLT
5396 * CEE_CLT_UN into OP_CLT_UN
5398 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
5400 MONO_INST_NEW (cfg, ins, cmp->opcode);
5402 cmp->inst_i0 = sp [0];
5403 cmp->inst_i1 = sp [1];
5407 cmp->opcode = OP_COMPARE;
5409 ins->type = STACK_I4;
5412 /* spill it to reduce the expression complexity
5413 * and workaround bug 54209
5415 if (cmp->inst_left->type == STACK_I8) {
5416 MonoInst *store, *temp, *load;
5418 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
5419 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
5420 store->cil_code = ins->cil_code;
5421 MONO_ADD_INS (bblock, store);
5422 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
5423 load->cil_code = ins->cil_code;
5433 CHECK_STACK_OVF (1);
5435 n = read32 (ip + 2);
5436 if (method->wrapper_type != MONO_WRAPPER_NONE)
5437 cmethod = mono_method_get_wrapper_data (method, n);
5439 cmethod = mono_get_method_full (image, n, NULL, generic_context);
5442 mono_class_init (cmethod->klass);
5443 handle_loaded_temps (cfg, bblock, stack_start, sp);
5445 NEW_METHODCONST (cfg, argconst, cmethod);
5446 if (method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED)
5447 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
5449 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn_nosync, &argconst, ip);
5450 NEW_TEMPLOAD (cfg, *sp, temp);
5454 inline_costs += 10 * num_calls++;
5457 case CEE_LDVIRTFTN: {
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);
5469 mono_class_init (cmethod->klass);
5470 handle_loaded_temps (cfg, bblock, stack_start, sp);
5474 NEW_METHODCONST (cfg, args [1], cmethod);
5475 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
5476 NEW_TEMPLOAD (cfg, *sp, temp);
5480 inline_costs += 10 * num_calls++;
5484 CHECK_STACK_OVF (1);
5486 n = read16 (ip + 2);
5488 NEW_ARGLOAD (cfg, ins, n);
5494 CHECK_STACK_OVF (1);
5496 n = read16 (ip + 2);
5498 NEW_ARGLOADA (cfg, ins, n);
5506 handle_loaded_temps (cfg, bblock, stack_start, sp);
5508 n = read16 (ip + 2);
5510 NEW_ARGSTORE (cfg, ins, n, *sp);
5512 if (ins->opcode == CEE_STOBJ) {
5513 NEW_ARGLOADA (cfg, ins, n);
5514 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5516 MONO_ADD_INS (bblock, ins);
5520 CHECK_STACK_OVF (1);
5522 n = read16 (ip + 2);
5524 NEW_LOCLOAD (cfg, ins, n);
5530 CHECK_STACK_OVF (1);
5532 n = read16 (ip + 2);
5534 NEW_LOCLOADA (cfg, ins, n);
5543 n = read16 (ip + 2);
5545 handle_loaded_temps (cfg, bblock, stack_start, sp);
5546 NEW_LOCSTORE (cfg, ins, n, *sp);
5548 if (ins->opcode == CEE_STOBJ) {
5549 NEW_LOCLOADA (cfg, ins, n);
5550 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5552 MONO_ADD_INS (bblock, ins);
5559 if (sp != stack_start)
5561 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
5562 ins->inst_left = *sp;
5565 cfg->flags |= MONO_CFG_HAS_ALLOCA;
5566 if (header->init_locals)
5567 ins->flags |= MONO_INST_INIT;
5571 /* FIXME: set init flag if locals init is set in this method */
5573 case CEE_ENDFILTER: {
5574 MonoExceptionClause *clause, *nearest;
5575 int cc, nearest_num;
5579 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
5581 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
5582 ins->inst_left = *sp;
5584 MONO_ADD_INS (bblock, ins);
5585 start_new_bblock = 1;
5590 for (cc = 0; cc < header->num_clauses; ++cc) {
5591 clause = &header->clauses [cc];
5592 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
5593 (!nearest || (clause->token_or_filter > nearest->token_or_filter))) {
5599 filter_lengths [nearest_num] = (ip - header->code) - nearest->token_or_filter;
5603 case CEE_UNALIGNED_:
5604 ins_flag |= MONO_INST_UNALIGNED;
5605 /* FIXME: record alignment? we can assume 1 for now */
5610 ins_flag |= MONO_INST_VOLATILE;
5614 ins_flag |= MONO_INST_TAILCALL;
5615 /* Can't inline tail calls at this time */
5616 inline_costs += 100000;
5623 token = read32 (ip + 2);
5624 if (method->wrapper_type != MONO_WRAPPER_NONE)
5625 klass = mono_method_get_wrapper_data (method, token);
5627 klass = mono_class_get_full (image, token, generic_context);
5628 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5629 MonoInst *store, *load;
5630 NEW_PCONST (cfg, load, NULL);
5631 load->cil_code = ip;
5632 load->type = STACK_OBJ;
5633 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
5634 store->cil_code = ip;
5635 handle_loaded_temps (cfg, bblock, stack_start, sp);
5636 MONO_ADD_INS (bblock, store);
5637 store->inst_i0 = sp [0];
5638 store->inst_i1 = load;
5640 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
5645 case CEE_CONSTRAINED_:
5646 /* FIXME: implement */
5648 token = read32 (ip + 2);
5649 constrained_call = mono_class_get_full (image, token, generic_context);
5654 MonoInst *iargs [3];
5657 if ((cfg->opt & MONO_OPT_INTRINS) && (ip [1] == CEE_CPBLK) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5)) {
5659 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
5660 copy->inst_left = sp [0];
5661 copy->inst_right = sp [1];
5662 copy->cil_code = ip;
5664 MONO_ADD_INS (bblock, copy);
5671 handle_loaded_temps (cfg, bblock, stack_start, sp);
5672 if (ip [1] == CEE_CPBLK) {
5673 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
5675 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
5684 ins_flag |= MONO_INST_NOTYPECHECK;
5686 ins_flag |= MONO_INST_NORANGECHECK;
5687 /* we ignore the no-nullcheck for now since we
5688 * really do it explicitly only when doing callvirt->call
5694 /* FIXME: check we are in a catch handler */
5695 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
5696 load->cil_code = ip;
5697 MONO_INST_NEW (cfg, ins, CEE_THROW);
5698 ins->inst_left = load;
5700 MONO_ADD_INS (bblock, ins);
5702 start_new_bblock = 1;
5707 CHECK_STACK_OVF (1);
5709 token = read32 (ip + 2);
5710 /* FIXXME: handle generics. */
5711 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
5712 MonoType *type = mono_type_create_from_typespec (image, token);
5713 token = mono_type_size (type, &align);
5715 MonoClass *szclass = mono_class_get_full (image, token, generic_context);
5716 mono_class_init (szclass);
5717 token = mono_class_value_size (szclass, &align);
5719 NEW_ICONST (cfg, ins, token);
5724 case CEE_REFANYTYPE:
5726 MONO_INST_NEW (cfg, ins, OP_REFANYTYPE);
5728 ins->type = STACK_MP;
5729 ins->inst_left = *sp;
5730 ins->type = STACK_VTYPE;
5731 ins->klass = mono_defaults.typehandle_class;
5740 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
5745 g_error ("opcode 0x%02x not handled", *ip);
5748 if (start_new_bblock != 1)
5751 bblock->cil_length = ip - bblock->cil_code;
5752 bblock->next_bb = end_bblock;
5753 link_bblock (cfg, bblock, end_bblock);
5755 if (cfg->method == method && cfg->domainvar) {
5759 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
5760 call->signature = helper_sig_domain_get;
5761 call->inst.type = STACK_PTR;
5762 call->fptr = mono_domain_get;
5763 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, (MonoInst*)call);
5765 MONO_ADD_INS (init_localsbb, store);
5768 if (header->init_locals) {
5770 for (i = 0; i < header->num_locals; ++i) {
5771 int t = header->locals [i]->type;
5772 if (t == MONO_TYPE_VALUETYPE && header->locals [i]->data.klass->enumtype)
5773 t = header->locals [i]->data.klass->enum_basetype->type;
5774 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
5775 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
5776 NEW_ICONST (cfg, ins, 0);
5777 NEW_LOCSTORE (cfg, store, i, ins);
5778 MONO_ADD_INS (init_localsbb, store);
5779 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
5780 MONO_INST_NEW (cfg, ins, OP_I8CONST);
5781 ins->type = STACK_I8;
5783 NEW_LOCSTORE (cfg, store, i, ins);
5784 MONO_ADD_INS (init_localsbb, store);
5785 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
5786 MONO_INST_NEW (cfg, ins, OP_R8CONST);
5787 ins->type = STACK_R8;
5788 ins->inst_p0 = (void*)&r8_0;
5789 NEW_LOCSTORE (cfg, store, i, ins);
5790 MONO_ADD_INS (init_localsbb, store);
5791 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF)) {
5792 NEW_LOCLOADA (cfg, ins, i);
5793 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (header->locals [i]), NULL, NULL);
5796 NEW_PCONST (cfg, ins, NULL);
5797 NEW_LOCSTORE (cfg, store, i, ins);
5798 MONO_ADD_INS (init_localsbb, store);
5804 /* resolve backward branches in the middle of an existing basic block */
5805 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
5807 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
5808 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
5809 if (tblock != start_bblock) {
5811 split_bblock (cfg, tblock, bblock);
5812 l = bblock->cil_code - header->code;
5813 bblock->cil_length = tblock->cil_length - l;
5814 tblock->cil_length = l;
5816 g_print ("recheck failed.\n");
5821 * we compute regions here, because the length of filter clauses is not known in advance.
5822 * It is computed in the CEE_ENDFILTER case in the above switch statement
5824 if (cfg->method == method) {
5826 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5827 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
5829 mono_create_spvar_for_region (cfg, bb->region);
5830 if (cfg->verbose_level > 2)
5831 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
5834 g_hash_table_destroy (bbhash);
5837 dont_inline = g_list_remove (dont_inline, method);
5838 return inline_costs;
5841 if (cfg->method != method)
5842 g_hash_table_destroy (bbhash);
5843 dont_inline = g_list_remove (dont_inline, method);
5847 if (cfg->method != method)
5848 g_hash_table_destroy (bbhash);
5849 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
5850 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
5851 dont_inline = g_list_remove (dont_inline, method);
5856 mono_print_tree (MonoInst *tree) {
5862 arity = mono_burg_arity [tree->opcode];
5864 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
5866 switch (tree->opcode) {
5868 printf ("[%d]", tree->inst_c0);
5871 printf ("[%lld]", tree->inst_l);
5874 printf ("[%f]", *(double*)tree->inst_p0);
5877 printf ("[%f]", *(float*)tree->inst_p0);
5881 printf ("[%d]", tree->inst_c0);
5884 if (tree->inst_offset < 0)
5885 printf ("[-0x%x(%s)]", -tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5887 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5890 printf ("[%s]", mono_arch_regname (tree->dreg));
5893 printf ("[%s]", tree->inst_newa_class->name);
5894 mono_print_tree (tree->inst_newa_len);
5905 case OP_VOIDCALLVIRT: {
5906 MonoCallInst *call = (MonoCallInst*)tree;
5908 printf ("[%s]", call->method->name);
5913 printf ("[%d (", tree->inst_c0);
5914 for (i = 0; i < tree->inst_phi_args [0]; i++) {
5917 printf ("%d", tree->inst_phi_args [i + 1]);
5928 case OP_LOAD_MEMBASE:
5929 case OP_LOADI4_MEMBASE:
5930 case OP_LOADU4_MEMBASE:
5931 case OP_LOADU1_MEMBASE:
5932 case OP_LOADI1_MEMBASE:
5933 case OP_LOADU2_MEMBASE:
5934 case OP_LOADI2_MEMBASE:
5935 printf ("[%s] <- [%s + 0x%x]", mono_arch_regname (tree->dreg), mono_arch_regname (tree->inst_basereg), tree->inst_offset);
5938 printf ("[B%d]", tree->inst_target_bb->block_num);
5948 case OP_VOIDCALL_REG:
5949 mono_print_tree (tree->inst_left);
5961 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
5962 mono_print_tree (tree->inst_left);
5965 if (!mono_arch_print_tree(tree, arity)) {
5967 mono_print_tree (tree->inst_left);
5969 mono_print_tree (tree->inst_right);
5980 mono_print_tree_nl (MonoInst *tree)
5982 mono_print_tree (tree);
5987 create_helper_signature (void)
5989 /* FIXME: set call conv */
5990 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
5991 helper_sig_newarr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5992 helper_sig_newarr->params [0] = helper_sig_newarr->params [1] = &mono_defaults.int_class->byval_arg;
5993 helper_sig_newarr->ret = &mono_defaults.object_class->byval_arg;
5994 helper_sig_newarr->params [2] = &mono_defaults.int32_class->byval_arg;
5995 helper_sig_newarr->pinvoke = 1;
5997 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
5998 helper_sig_newarr_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5999 helper_sig_newarr_specific->params [0] = &mono_defaults.int_class->byval_arg;
6000 helper_sig_newarr_specific->params [1] = &mono_defaults.int32_class->byval_arg;
6001 helper_sig_newarr_specific->ret = &mono_defaults.object_class->byval_arg;
6002 helper_sig_newarr_specific->pinvoke = 1;
6004 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
6005 helper_sig_object_new = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6006 helper_sig_object_new->params [0] = helper_sig_object_new->params [1] = &mono_defaults.int_class->byval_arg;
6007 helper_sig_object_new->ret = &mono_defaults.object_class->byval_arg;
6008 helper_sig_object_new->pinvoke = 1;
6010 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
6011 helper_sig_object_new_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6012 helper_sig_object_new_specific->params [0] = &mono_defaults.int_class->byval_arg;
6013 helper_sig_object_new_specific->ret = &mono_defaults.object_class->byval_arg;
6014 helper_sig_object_new_specific->pinvoke = 1;
6016 /* void* mono_method_compile (MonoMethod*) */
6017 helper_sig_compile = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6018 helper_sig_compile->params [0] = helper_sig_compile->ret = &mono_defaults.int_class->byval_arg;
6019 helper_sig_compile->pinvoke = 1;
6021 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
6022 helper_sig_compile_virt = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6023 helper_sig_compile_virt->params [0] = &mono_defaults.object_class->byval_arg;
6024 helper_sig_compile_virt->params [1] = helper_sig_compile_virt->ret = &mono_defaults.int_class->byval_arg;
6025 helper_sig_compile_virt->pinvoke = 1;
6027 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
6028 helper_sig_ldstr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
6029 helper_sig_ldstr->params [0] = helper_sig_ldstr->params [1] = &mono_defaults.int_class->byval_arg;
6030 helper_sig_ldstr->params [2] = &mono_defaults.int32_class->byval_arg;
6031 helper_sig_ldstr->ret = &mono_defaults.object_class->byval_arg;
6032 helper_sig_ldstr->pinvoke = 1;
6034 /* MonoDomain *mono_domain_get (void) */
6035 helper_sig_domain_get = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
6036 helper_sig_domain_get->ret = &mono_defaults.int_class->byval_arg;
6037 helper_sig_domain_get->pinvoke = 1;
6039 /* void* stelem_ref (MonoArray *, int index, MonoObject *) */
6040 helper_sig_stelem_ref = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
6041 helper_sig_stelem_ref->params [0] = &mono_defaults.array_class->byval_arg;
6042 helper_sig_stelem_ref->params [1] = &mono_defaults.int32_class->byval_arg;
6043 helper_sig_stelem_ref->params [2] = &mono_defaults.object_class->byval_arg;
6044 helper_sig_stelem_ref->ret = &mono_defaults.void_class->byval_arg;
6045 helper_sig_stelem_ref->pinvoke = 1;
6047 /* void* stelem_ref_check (MonoArray *, MonoObject *) */
6048 helper_sig_stelem_ref_check = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6049 helper_sig_stelem_ref_check->params [0] = &mono_defaults.array_class->byval_arg;
6050 helper_sig_stelem_ref_check->params [1] = &mono_defaults.object_class->byval_arg;
6051 helper_sig_stelem_ref_check->ret = &mono_defaults.void_class->byval_arg;
6052 helper_sig_stelem_ref_check->pinvoke = 1;
6054 /* long amethod (long, long) */
6055 helper_sig_long_long_long = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6056 helper_sig_long_long_long->params [0] = helper_sig_long_long_long->params [1] =
6057 &mono_defaults.int64_class->byval_arg;
6058 helper_sig_long_long_long->ret = &mono_defaults.int64_class->byval_arg;
6059 helper_sig_long_long_long->pinvoke = 1;
6061 /* object amethod (intptr) */
6062 helper_sig_obj_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6063 helper_sig_obj_ptr->params [0] = &mono_defaults.int_class->byval_arg;
6064 helper_sig_obj_ptr->ret = &mono_defaults.object_class->byval_arg;
6065 helper_sig_obj_ptr->pinvoke = 1;
6067 /* void amethod (intptr) */
6068 helper_sig_void_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6069 helper_sig_void_ptr->params [0] = &mono_defaults.int_class->byval_arg;
6070 helper_sig_void_ptr->ret = &mono_defaults.void_class->byval_arg;
6071 helper_sig_void_ptr->pinvoke = 1;
6073 /* void amethod (MonoObject *obj) */
6074 helper_sig_void_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6075 helper_sig_void_obj->params [0] = &mono_defaults.object_class->byval_arg;
6076 helper_sig_void_obj->ret = &mono_defaults.void_class->byval_arg;
6077 helper_sig_void_obj->pinvoke = 1;
6079 /* void amethod (MonoObject *obj, void *ptr, int i) */
6080 helper_sig_void_obj_ptr_int = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
6081 helper_sig_void_obj_ptr_int->params [0] = &mono_defaults.object_class->byval_arg;
6082 helper_sig_void_obj_ptr_int->params [1] = &mono_defaults.int_class->byval_arg;
6083 helper_sig_void_obj_ptr_int->params [2] = &mono_defaults.int_class->byval_arg;
6084 helper_sig_void_obj_ptr_int->ret = &mono_defaults.void_class->byval_arg;
6085 helper_sig_void_obj_ptr_int->pinvoke = 1;
6087 /* intptr amethod (void) */
6088 helper_sig_ptr_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
6089 helper_sig_ptr_void->ret = &mono_defaults.int_class->byval_arg;
6090 helper_sig_ptr_void->pinvoke = 1;
6092 /* object amethod (void) */
6093 helper_sig_obj_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
6094 helper_sig_obj_void->ret = &mono_defaults.object_class->byval_arg;
6095 helper_sig_obj_void->pinvoke = 1;
6097 /* void amethod (intptr, intptr) */
6098 helper_sig_void_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6099 helper_sig_void_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
6100 helper_sig_void_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
6101 helper_sig_void_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
6102 helper_sig_void_ptr_ptr->pinvoke = 1;
6104 /* void amethod (intptr, intptr, intptr) */
6105 helper_sig_void_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
6106 helper_sig_void_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
6107 helper_sig_void_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
6108 helper_sig_void_ptr_ptr_ptr->params [2] = &mono_defaults.int_class->byval_arg;
6109 helper_sig_void_ptr_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
6110 helper_sig_void_ptr_ptr_ptr->pinvoke = 1;
6112 /* intptr amethod (intptr, intptr) */
6113 helper_sig_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6114 helper_sig_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
6115 helper_sig_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
6116 helper_sig_ptr_ptr_ptr->ret = &mono_defaults.int_class->byval_arg;
6117 helper_sig_ptr_ptr_ptr->pinvoke = 1;
6119 /* IntPtr amethod (object) */
6120 helper_sig_ptr_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6121 helper_sig_ptr_obj->params [0] = &mono_defaults.object_class->byval_arg;
6122 helper_sig_ptr_obj->ret = &mono_defaults.int_class->byval_arg;
6123 helper_sig_ptr_obj->pinvoke = 1;
6125 /* IntPtr amethod (object, int) */
6126 helper_sig_ptr_obj_int = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6127 helper_sig_ptr_obj_int->params [0] = &mono_defaults.object_class->byval_arg;
6128 helper_sig_ptr_obj_int->params [1] = &mono_defaults.int_class->byval_arg;
6129 helper_sig_ptr_obj_int->ret = &mono_defaults.int_class->byval_arg;
6130 helper_sig_ptr_obj_int->pinvoke = 1;
6132 /* IntPtr amethod (int) */
6133 helper_sig_ptr_int = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6134 helper_sig_ptr_int->params [0] = &mono_defaults.int32_class->byval_arg;
6135 helper_sig_ptr_int->ret = &mono_defaults.int_class->byval_arg;
6136 helper_sig_ptr_int->pinvoke = 1;
6138 /* long amethod (long, guint32) */
6139 helper_sig_long_long_int = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6140 helper_sig_long_long_int->params [0] = &mono_defaults.int64_class->byval_arg;
6141 helper_sig_long_long_int->params [1] = &mono_defaults.int32_class->byval_arg;
6142 helper_sig_long_long_int->ret = &mono_defaults.int64_class->byval_arg;
6143 helper_sig_long_long_int->pinvoke = 1;
6145 /* ulong amethod (double) */
6146 helper_sig_ulong_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6147 helper_sig_ulong_double->params [0] = &mono_defaults.double_class->byval_arg;
6148 helper_sig_ulong_double->ret = &mono_defaults.uint64_class->byval_arg;
6149 helper_sig_ulong_double->pinvoke = 1;
6151 /* long amethod (double) */
6152 helper_sig_long_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6153 helper_sig_long_double->params [0] = &mono_defaults.double_class->byval_arg;
6154 helper_sig_long_double->ret = &mono_defaults.int64_class->byval_arg;
6155 helper_sig_long_double->pinvoke = 1;
6157 /* double amethod (long) */
6158 helper_sig_double_long = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6159 helper_sig_double_long->params [0] = &mono_defaults.int64_class->byval_arg;
6160 helper_sig_double_long->ret = &mono_defaults.double_class->byval_arg;
6161 helper_sig_double_long->pinvoke = 1;
6163 /* double amethod (int) */
6164 helper_sig_double_int = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6165 helper_sig_double_int->params [0] = &mono_defaults.int32_class->byval_arg;
6166 helper_sig_double_int->ret = &mono_defaults.double_class->byval_arg;
6167 helper_sig_double_int->pinvoke = 1;
6169 /* float amethod (long) */
6170 helper_sig_float_long = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6171 helper_sig_float_long->params [0] = &mono_defaults.int64_class->byval_arg;
6172 helper_sig_float_long->ret = &mono_defaults.single_class->byval_arg;
6173 helper_sig_float_long->pinvoke = 1;
6175 /* double amethod (double, double) */
6176 helper_sig_double_double_double = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6177 helper_sig_double_double_double->params [0] = &mono_defaults.double_class->byval_arg;
6178 helper_sig_double_double_double->params [1] = &mono_defaults.double_class->byval_arg;
6179 helper_sig_double_double_double->ret = &mono_defaults.double_class->byval_arg;
6180 helper_sig_double_double_double->pinvoke = 1;
6182 /* uint amethod (double) */
6183 helper_sig_uint_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6184 helper_sig_uint_double->params [0] = &mono_defaults.double_class->byval_arg;
6185 helper_sig_uint_double->ret = &mono_defaults.uint32_class->byval_arg;
6186 helper_sig_uint_double->pinvoke = 1;
6188 /* int amethod (double) */
6189 helper_sig_int_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6190 helper_sig_int_double->params [0] = &mono_defaults.double_class->byval_arg;
6191 helper_sig_int_double->ret = &mono_defaults.int32_class->byval_arg;
6192 helper_sig_int_double->pinvoke = 1;
6194 /* void initobj (intptr, int size) */
6195 helper_sig_initobj = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6196 helper_sig_initobj->params [0] = &mono_defaults.int_class->byval_arg;
6197 helper_sig_initobj->params [1] = &mono_defaults.int32_class->byval_arg;
6198 helper_sig_initobj->ret = &mono_defaults.void_class->byval_arg;
6199 helper_sig_initobj->pinvoke = 1;
6201 /* void memcpy (intptr, intptr, int size) */
6202 helper_sig_memcpy = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
6203 helper_sig_memcpy->params [0] = &mono_defaults.int_class->byval_arg;
6204 helper_sig_memcpy->params [1] = &mono_defaults.int_class->byval_arg;
6205 helper_sig_memcpy->params [2] = &mono_defaults.int32_class->byval_arg;
6206 helper_sig_memcpy->ret = &mono_defaults.void_class->byval_arg;
6207 helper_sig_memcpy->pinvoke = 1;
6209 /* void memset (intptr, int val, int size) */
6210 helper_sig_memset = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
6211 helper_sig_memset->params [0] = &mono_defaults.int_class->byval_arg;
6212 helper_sig_memset->params [1] = &mono_defaults.int32_class->byval_arg;
6213 helper_sig_memset->params [2] = &mono_defaults.int32_class->byval_arg;
6214 helper_sig_memset->ret = &mono_defaults.void_class->byval_arg;
6215 helper_sig_memset->pinvoke = 1;
6217 helper_sig_class_init_trampoline = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
6218 helper_sig_class_init_trampoline->ret = &mono_defaults.void_class->byval_arg;
6219 helper_sig_class_init_trampoline->pinvoke = 1;
6222 static GHashTable *jit_icall_hash_name = NULL;
6223 static GHashTable *jit_icall_hash_addr = NULL;
6226 mono_find_jit_icall_by_name (const char *name)
6228 MonoJitICallInfo *info;
6229 g_assert (jit_icall_hash_name);
6231 //printf ("lookup addr %s %p\n", name, g_hash_table_lookup (jit_icall_hash_name, name));
6232 EnterCriticalSection (&trampoline_hash_mutex);
6233 info = g_hash_table_lookup (jit_icall_hash_name, name);
6234 LeaveCriticalSection (&trampoline_hash_mutex);
6239 mono_find_jit_icall_by_addr (gconstpointer addr)
6241 MonoJitICallInfo *info;
6242 g_assert (jit_icall_hash_addr);
6244 EnterCriticalSection (&trampoline_hash_mutex);
6245 info = g_hash_table_lookup (jit_icall_hash_addr, (gpointer)addr);
6246 LeaveCriticalSection (&trampoline_hash_mutex);
6252 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
6255 MonoMethod *wrapper;
6258 if (callinfo->wrapper)
6259 return callinfo->wrapper;
6261 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
6262 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
6263 /* Must be domain neutral since there is only one copy */
6264 code = mono_jit_compile_method_with_opt (wrapper, default_opt | MONO_OPT_SHARED);
6266 EnterCriticalSection (&trampoline_hash_mutex);
6267 if (!callinfo->wrapper) {
6268 callinfo->wrapper = code;
6269 g_hash_table_insert (jit_icall_hash_addr, (gpointer)callinfo->wrapper, callinfo);
6271 LeaveCriticalSection (&trampoline_hash_mutex);
6274 return callinfo->wrapper;
6278 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save)
6280 MonoJitICallInfo *info;
6285 EnterCriticalSection (&trampoline_hash_mutex);
6287 if (!jit_icall_hash_name) {
6288 jit_icall_hash_name = g_hash_table_new (g_str_hash, g_str_equal);
6289 jit_icall_hash_addr = g_hash_table_new (NULL, NULL);
6292 if (g_hash_table_lookup (jit_icall_hash_name, name)) {
6293 g_warning ("jit icall already defined \"%s\"\n", name);
6294 g_assert_not_reached ();
6297 info = g_new (MonoJitICallInfo, 1);
6304 #ifdef MONO_USE_EXC_TABLES
6305 || mono_arch_has_unwind_info (func)
6308 info->wrapper = func;
6310 info->wrapper = NULL;
6313 g_hash_table_insert (jit_icall_hash_name, (gpointer)info->name, info);
6314 g_hash_table_insert (jit_icall_hash_addr, (gpointer)func, info);
6316 LeaveCriticalSection (&trampoline_hash_mutex);
6321 mono_create_class_init_trampoline (MonoVTable *vtable)
6325 /* previously created trampoline code */
6326 mono_domain_lock (vtable->domain);
6328 mono_g_hash_table_lookup (vtable->domain->class_init_trampoline_hash,
6330 mono_domain_unlock (vtable->domain);
6334 code = mono_arch_create_class_init_trampoline (vtable);
6336 /* store trampoline address */
6337 mono_domain_lock (vtable->domain);
6338 mono_g_hash_table_insert (vtable->domain->class_init_trampoline_hash,
6340 mono_domain_unlock (vtable->domain);
6342 EnterCriticalSection (&trampoline_hash_mutex);
6343 if (!class_init_hash_addr)
6344 class_init_hash_addr = g_hash_table_new (NULL, NULL);
6345 g_hash_table_insert (class_init_hash_addr, code, vtable);
6346 LeaveCriticalSection (&trampoline_hash_mutex);
6352 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method,
6353 gboolean add_sync_wrapper)
6358 if (add_sync_wrapper && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
6359 return mono_create_jump_trampoline (domain, mono_marshal_get_synchronized_wrapper (method), FALSE);
6361 code = mono_jit_find_compiled_method (domain, method);
6365 EnterCriticalSection (&trampoline_hash_mutex);
6367 if (jump_trampoline_hash) {
6368 code = g_hash_table_lookup (jump_trampoline_hash, method);
6370 LeaveCriticalSection (&trampoline_hash_mutex);
6375 ji = mono_arch_create_jump_trampoline (method);
6378 * mono_delegate_ctor needs to find the method metadata from the
6379 * trampoline address, so we save it here.
6382 mono_jit_info_table_add (mono_root_domain, ji);
6384 if (!jump_trampoline_hash)
6385 jump_trampoline_hash = g_hash_table_new (NULL, NULL);
6386 g_hash_table_insert (jump_trampoline_hash, method, ji->code_start);
6388 LeaveCriticalSection (&trampoline_hash_mutex);
6390 return ji->code_start;
6394 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
6398 EnterCriticalSection (&trampoline_hash_mutex);
6399 if (class_init_hash_addr)
6400 res = g_hash_table_lookup (class_init_hash_addr, addr);
6403 LeaveCriticalSection (&trampoline_hash_mutex);
6408 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func, gboolean no_throw)
6410 MonoJitICallInfo *info;
6412 if (!emul_opcode_map)
6413 emul_opcode_map = g_new0 (MonoJitICallInfo*, OP_LAST + 1);
6415 g_assert (!sig->hasthis);
6416 g_assert (sig->param_count < 3);
6418 info = mono_register_jit_icall (func, name, sig, no_throw);
6420 emul_opcode_map [opcode] = info;
6424 decompose_foreach (MonoInst *tree, gpointer data)
6426 static MonoJitICallInfo *newarr_info = NULL;
6427 static MonoJitICallInfo *newarr_specific_info = NULL;
6428 MonoJitICallInfo *info;
6431 switch (tree->opcode) {
6433 MonoCompile *cfg = data;
6434 MonoInst *iargs [3];
6437 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
6438 g_assert (newarr_info);
6439 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
6440 g_assert (newarr_specific_info);
6443 if (cfg->opt & MONO_OPT_SHARED) {
6444 NEW_DOMAINCONST (cfg, iargs [0]);
6445 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
6446 iargs [2] = tree->inst_newa_len;
6451 MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
6453 NEW_VTABLECONST (cfg, iargs [0], vtable);
6454 iargs [1] = tree->inst_newa_len;
6456 info = newarr_specific_info;
6459 mono_emulate_opcode (cfg, tree, iargs, info);
6461 /* Need to decompose arguments after the the opcode is decomposed */
6462 for (i = 0; i < info->sig->param_count; ++i)
6463 dec_foreach (iargs [i], cfg);
6473 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
6475 switch (mono_burg_arity [tree->opcode]) {
6478 mono_inst_foreach (tree->inst_left, func, data);
6481 mono_inst_foreach (tree->inst_left, func, data);
6482 mono_inst_foreach (tree->inst_right, func, data);
6485 g_assert_not_reached ();
6492 mono_print_bb_code (MonoBasicBlock *bb) {
6494 MonoInst *c = bb->code;
6496 mono_print_tree (c);
6504 print_dfn (MonoCompile *cfg) {
6509 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
6511 for (i = 0; i < cfg->num_bblocks; ++i) {
6512 bb = cfg->bblocks [i];
6514 char* code1, *code2;
6515 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
6516 if (bb->last_ins->cil_code)
6517 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
6519 code2 = g_strdup ("");
6521 code1 [strlen (code1) - 1] = 0;
6522 code = g_strdup_printf ("%s -> %s", code1, code2);
6526 code = g_strdup ("\n");
6527 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
6529 MonoInst *c = bb->code;
6531 mono_print_tree (c);
6539 g_print ("\tprev:");
6540 for (j = 0; j < bb->in_count; ++j) {
6541 g_print (" BB%d", bb->in_bb [j]->block_num);
6543 g_print ("\t\tsucc:");
6544 for (j = 0; j < bb->out_count; ++j) {
6545 g_print (" BB%d", bb->out_bb [j]->block_num);
6547 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
6550 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
6553 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
6555 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
6563 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
6567 g_assert (bb->code);
6568 bb->last_ins->next = inst;
6569 bb->last_ins = inst;
6571 bb->last_ins = bb->code = inst;
6576 mono_destroy_compile (MonoCompile *cfg)
6578 //mono_mempool_stats (cfg->mempool);
6579 g_hash_table_destroy (cfg->bb_hash);
6580 mono_free_loop_info (cfg);
6582 mono_regstate_free (cfg->rs);
6584 g_hash_table_destroy (cfg->spvars);
6585 mono_mempool_destroy (cfg->mempool);
6586 g_list_free (cfg->ldstr_list);
6588 g_free (cfg->varinfo);
6594 mono_get_lmf_addr (void)
6596 MonoJitTlsData *jit_tls;
6598 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
6599 return &jit_tls->lmf;
6601 g_assert_not_reached ();
6606 * mono_thread_abort:
6607 * @obj: exception object
6609 * abort the thread, print exception information and stack trace
6612 mono_thread_abort (MonoObject *obj)
6614 /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
6616 /* handle_remove should be eventually called for this thread, too
6623 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
6625 MonoJitTlsData *jit_tls;
6628 jit_tls = TlsGetValue (mono_jit_tls_id);
6632 jit_tls = g_new0 (MonoJitTlsData, 1);
6634 TlsSetValue (mono_jit_tls_id, jit_tls);
6636 jit_tls->abort_func = abort_func;
6637 jit_tls->end_of_stack = stack_start;
6639 lmf = g_new0 (MonoLMF, 1);
6642 jit_tls->lmf = jit_tls->first_lmf = lmf;
6644 mono_arch_setup_jit_tls_data (jit_tls);
6650 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
6653 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
6654 thread = mono_thread_current ();
6656 thread->jit_data = jit_tls;
6659 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
6662 mono_thread_abort_dummy (MonoObject *obj)
6664 if (mono_thread_attach_aborted_cb)
6665 mono_thread_attach_aborted_cb (obj);
6667 mono_thread_abort (obj);
6671 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
6674 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
6675 thread = mono_thread_current ();
6677 thread->jit_data = jit_tls;
6681 mini_thread_cleanup (MonoThread *thread)
6683 MonoJitTlsData *jit_tls = thread->jit_data;
6686 mono_arch_free_jit_tls_data (jit_tls);
6687 g_free (jit_tls->first_lmf);
6689 thread->jit_data = NULL;
6694 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
6696 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
6700 ji->data.target = target;
6701 ji->next = cfg->patch_info;
6703 cfg->patch_info = ji;
6707 mono_remove_patch_info (MonoCompile *cfg, int ip)
6709 MonoJumpInfo **ji = &cfg->patch_info;
6712 if ((*ji)->ip.i == ip)
6715 ji = &((*ji)->next);
6720 mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors)
6722 unsigned char *ip = patch_info->ip.i + code;
6723 gconstpointer target = NULL;
6725 switch (patch_info->type) {
6726 case MONO_PATCH_INFO_BB:
6727 target = patch_info->data.bb->native_offset + code;
6729 case MONO_PATCH_INFO_ABS:
6730 target = patch_info->data.target;
6732 case MONO_PATCH_INFO_LABEL:
6733 target = patch_info->data.inst->inst_c0 + code;
6735 case MONO_PATCH_INFO_IP:
6738 case MONO_PATCH_INFO_METHOD_REL:
6739 target = code + patch_info->data.offset;
6741 case MONO_PATCH_INFO_INTERNAL_METHOD: {
6742 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
6744 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
6745 g_assert_not_reached ();
6747 target = mono_icall_get_wrapper (mi);
6750 case MONO_PATCH_INFO_METHOD_JUMP: {
6753 /* get the trampoline to the method from the domain */
6754 target = mono_create_jump_trampoline (domain, patch_info->data.method, TRUE);
6755 if (!domain->jump_target_hash)
6756 domain->jump_target_hash = g_hash_table_new (NULL, NULL);
6757 list = g_hash_table_lookup (domain->jump_target_hash, patch_info->data.method);
6758 list = g_slist_prepend (list, ip);
6759 g_hash_table_insert (domain->jump_target_hash, patch_info->data.method, list);
6762 case MONO_PATCH_INFO_METHOD:
6763 if (patch_info->data.method == method) {
6766 /* get the trampoline to the method from the domain */
6767 target = mono_arch_create_jit_trampoline (patch_info->data.method);
6769 case MONO_PATCH_INFO_SWITCH: {
6770 gpointer *jump_table;
6773 mono_domain_lock (domain);
6774 jump_table = mono_code_manager_reserve (domain->code_mp, sizeof (gpointer) * patch_info->table_size);
6775 mono_domain_unlock (domain);
6777 for (i = 0; i < patch_info->table_size; i++) {
6778 jump_table [i] = code + (int)patch_info->data.table [i];
6780 target = jump_table;
6783 case MONO_PATCH_INFO_METHODCONST:
6784 case MONO_PATCH_INFO_CLASS:
6785 case MONO_PATCH_INFO_IMAGE:
6786 case MONO_PATCH_INFO_FIELD:
6787 target = patch_info->data.target;
6789 case MONO_PATCH_INFO_IID:
6790 mono_class_init (patch_info->data.klass);
6791 target = (gpointer)patch_info->data.klass->interface_id;
6793 case MONO_PATCH_INFO_VTABLE:
6794 target = mono_class_vtable (domain, patch_info->data.klass);
6796 case MONO_PATCH_INFO_CLASS_INIT:
6797 target = mono_create_class_init_trampoline (mono_class_vtable (domain, patch_info->data.klass));
6799 case MONO_PATCH_INFO_SFLDA: {
6800 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
6801 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (vtable->klass, method))
6802 /* Done by the generated code */
6806 mono_runtime_class_init (vtable);
6808 target = (char*)vtable->data + patch_info->data.field->offset;
6811 case MONO_PATCH_INFO_R4:
6812 case MONO_PATCH_INFO_R8:
6813 target = patch_info->data.target;
6815 case MONO_PATCH_INFO_EXC_NAME:
6816 target = patch_info->data.name;
6818 case MONO_PATCH_INFO_LDSTR:
6820 mono_ldstr (domain, patch_info->data.token->image,
6821 mono_metadata_token_index (patch_info->data.token->token));
6823 case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
6825 MonoClass *handle_class;
6827 handle = mono_ldtoken (patch_info->data.token->image,
6828 patch_info->data.token->token, &handle_class, NULL);
6829 mono_class_init (handle_class);
6830 mono_class_init (mono_class_from_mono_type (handle));
6833 mono_type_get_object (domain, handle);
6836 case MONO_PATCH_INFO_LDTOKEN: {
6838 MonoClass *handle_class;
6840 handle = mono_ldtoken (patch_info->data.token->image,
6841 patch_info->data.token->token, &handle_class, NULL);
6842 mono_class_init (handle_class);
6847 case MONO_PATCH_INFO_BB_OVF:
6848 case MONO_PATCH_INFO_EXC_OVF:
6851 g_assert_not_reached ();
6854 return (gpointer)target;
6858 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
6859 MonoJitICallInfo *info;
6861 decompose_foreach (tree, cfg);
6863 switch (mono_burg_arity [tree->opcode]) {
6866 dec_foreach (tree->inst_left, cfg);
6868 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6869 MonoInst *iargs [2];
6871 iargs [0] = tree->inst_left;
6873 mono_emulate_opcode (cfg, tree, iargs, info);
6879 #ifdef MONO_ARCH_BIGMUL_INTRINS
6880 if (tree->opcode == OP_LMUL
6881 && (cfg->opt & MONO_OPT_INTRINS)
6882 && (tree->inst_left->opcode == CEE_CONV_I8
6883 || tree->inst_left->opcode == CEE_CONV_U8)
6884 && tree->inst_left->inst_left->type == STACK_I4
6885 && (tree->inst_right->opcode == CEE_CONV_I8
6886 || tree->inst_right->opcode == CEE_CONV_U8)
6887 && tree->inst_right->inst_left->type == STACK_I4) {
6888 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
6889 tree->inst_left = tree->inst_left->inst_left;
6890 tree->inst_right = tree->inst_right->inst_left;
6891 dec_foreach (tree, cfg);
6894 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6895 MonoInst *iargs [2];
6897 iargs [0] = tree->inst_i0;
6898 iargs [1] = tree->inst_i1;
6900 mono_emulate_opcode (cfg, tree, iargs, info);
6902 dec_foreach (iargs [0], cfg);
6903 dec_foreach (iargs [1], cfg);
6906 dec_foreach (tree->inst_left, cfg);
6907 dec_foreach (tree->inst_right, cfg);
6911 g_assert_not_reached ();
6916 decompose_pass (MonoCompile *cfg) {
6919 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6922 cfg->prev_ins = NULL;
6923 for (tree = cfg->cbb->code; tree; tree = tree->next) {
6924 dec_foreach (tree, cfg);
6925 cfg->prev_ins = tree;
6931 nullify_basic_block (MonoBasicBlock *bb)
6938 bb->code = bb->last_ins = NULL;
6939 bb->cil_code = NULL;
6943 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6947 for (i = 0; i < bb->out_count; i++) {
6948 MonoBasicBlock *ob = bb->out_bb [i];
6951 if (bb->out_count > 1) {
6952 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
6956 bb->out_bb [i] = repl;
6963 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6967 for (i = 0; i < bb->in_count; i++) {
6968 MonoBasicBlock *ib = bb->in_bb [i];
6971 if (bb->in_count > 1) {
6972 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
6976 bb->in_bb [i] = repl;
6983 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6987 for (i = 0; i < bb->out_count; i++) {
6988 MonoBasicBlock *ob = bb->out_bb [i];
6989 for (j = 0; j < ob->in_count; j++) {
6990 if (ob->in_bb [j] == orig) {
6991 ob->in_bb [j] = repl;
7000 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
7002 bb->out_count = bbn->out_count;
7003 bb->out_bb = bbn->out_bb;
7005 replace_basic_block (bb, bbn, bb);
7009 bb->last_ins->next = bbn->code;
7010 bb->last_ins = bbn->last_ins;
7013 bb->code = bbn->code;
7014 bb->last_ins = bbn->last_ins;
7016 bb->next_bb = bbn->next_bb;
7017 nullify_basic_block (bbn);
7021 * Optimizes the branches on the Control Flow Graph
7025 optimize_branches (MonoCompile *cfg)
7027 int i, changed = FALSE;
7028 MonoBasicBlock *bb, *bbn;
7029 guint32 niterations;
7032 * Some crazy loops could cause the code below to go into an infinite
7033 * loop, see bug #53003 for an example. To prevent this, we put an upper
7034 * bound on the number of iterations.
7041 /* we skip the entry block (exit is handled specially instead ) */
7042 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
7044 /* dont touch code inside exception clauses */
7045 if (bb->region != -1)
7048 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
7049 if (cfg->verbose_level > 2)
7050 g_print ("nullify block triggered %d\n", bbn->block_num);
7052 bb->next_bb = bbn->next_bb;
7054 for (i = 0; i < bbn->out_count; i++)
7055 replace_in_block (bbn->out_bb [i], bbn, NULL);
7057 nullify_basic_block (bbn);
7061 if (bb->out_count == 1) {
7062 bbn = bb->out_bb [0];
7064 /* conditional branches where true and false targets are the same can be also replaced with CEE_BR */
7065 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
7066 bb->last_ins->opcode = CEE_BR;
7067 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
7069 if (cfg->verbose_level > 2)
7070 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
7073 if (bb->region == bbn->region && bb->next_bb == bbn) {
7074 /* the block are in sequence anyway ... */
7076 /* branches to the following block can be removed */
7077 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
7078 bb->last_ins->opcode = CEE_NOP;
7080 if (cfg->verbose_level > 2)
7081 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
7084 if (bbn->in_count == 1) {
7086 if (bbn != cfg->bb_exit) {
7087 if (cfg->verbose_level > 2)
7088 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
7089 merge_basic_blocks (bb, bbn);
7093 //mono_print_bb_code (bb);
7098 } while (changed && (niterations > 0));
7105 /* we skip the entry block (exit is handled specially instead ) */
7106 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
7108 /* dont touch code inside exception clauses */
7109 if (bb->region != -1)
7112 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
7113 if (cfg->verbose_level > 2) {
7114 g_print ("nullify block triggered %d\n", bbn->block_num);
7116 bb->next_bb = bbn->next_bb;
7118 for (i = 0; i < bbn->out_count; i++)
7119 replace_in_block (bbn->out_bb [i], bbn, NULL);
7121 nullify_basic_block (bbn);
7127 if (bb->out_count == 1) {
7128 bbn = bb->out_bb [0];
7130 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
7131 bbn = bb->last_ins->inst_target_bb;
7132 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7133 bbn->code->inst_target_bb->region == bb->region) {
7135 if (cfg->verbose_level > 2)
7136 g_print ("in %s branch to branch triggered %d -> %d\n", cfg->method->name,
7137 bb->block_num, bbn->block_num);
7139 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
7140 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
7145 } else if (bb->out_count == 2) {
7146 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
7147 bbn = bb->last_ins->inst_true_bb;
7148 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7149 bbn->code->inst_target_bb->region == bb->region) {
7150 if (cfg->verbose_level > 2)
7151 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
7152 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
7155 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
7157 replace_in_block (bbn, bb, NULL);
7159 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
7160 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7162 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7168 bbn = bb->last_ins->inst_false_bb;
7169 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7170 bbn->code->inst_target_bb->region == bb->region) {
7171 if (cfg->verbose_level > 2)
7172 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
7173 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
7176 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
7178 replace_in_block (bbn, bb, NULL);
7180 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
7181 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7183 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7191 } while (changed && (niterations > 0));
7196 mono_compile_create_vars (MonoCompile *cfg)
7198 MonoMethodSignature *sig;
7199 MonoMethodHeader *header;
7202 header = ((MonoMethodNormal *)cfg->method)->header;
7204 sig = cfg->method->signature;
7206 if (!MONO_TYPE_IS_VOID (sig->ret)) {
7207 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
7208 cfg->ret->opcode = OP_RETARG;
7209 cfg->ret->inst_vtype = sig->ret;
7210 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
7212 if (cfg->verbose_level > 2)
7213 g_print ("creating vars\n");
7216 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
7218 for (i = 0; i < sig->param_count; ++i)
7219 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
7221 cfg->locals_start = cfg->num_varinfo;
7223 if (cfg->verbose_level > 2)
7224 g_print ("creating locals\n");
7225 for (i = 0; i < header->num_locals; ++i)
7226 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
7227 if (cfg->verbose_level > 2)
7228 g_print ("locals done\n");
7232 mono_print_code (MonoCompile *cfg)
7236 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7237 MonoInst *tree = bb->code;
7242 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
7244 for (; tree; tree = tree->next) {
7245 mono_print_tree (tree);
7250 bb->last_ins->next = NULL;
7254 extern const char * const mono_burg_rule_string [];
7257 emit_state (MonoCompile *cfg, MBState *state, int goal)
7260 int ern = mono_burg_rule (state, goal);
7261 const guint16 *nts = mono_burg_nts [ern];
7264 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
7268 // state->reg1 = state->reg2; /* chain rule */
7270 state->reg1 = mono_regstate_next_int (cfg->rs);
7271 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
7274 state->reg1 = mono_regstate_next_int (cfg->rs);
7275 state->reg2 = mono_regstate_next_int (cfg->rs);
7278 state->reg1 = mono_regstate_next_float (cfg->rs);
7285 mono_burg_kids (state, ern, kids);
7287 emit_state (cfg, kids [0], nts [0]);
7289 emit_state (cfg, kids [1], nts [1]);
7291 g_assert (!nts [3]);
7292 emit_state (cfg, kids [2], nts [2]);
7297 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
7298 if ((emit = mono_burg_func [ern]))
7299 emit (state, state->tree, cfg);
7302 #define DEBUG_SELECTION
7305 mini_select_instructions (MonoCompile *cfg)
7307 static const int reverse_map [] = {
7308 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
7309 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
7311 static const int reverse_fmap [] = {
7312 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
7313 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
7315 static const int reverse_lmap [] = {
7316 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
7317 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
7322 cfg->state_pool = mono_mempool_new ();
7323 cfg->rs = mono_regstate_new ();
7325 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7326 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode) &&
7327 bb->next_bb != bb->last_ins->inst_false_bb) {
7329 if (bb->next_bb == bb->last_ins->inst_true_bb) {
7330 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
7331 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
7332 bb->last_ins->inst_false_bb = tmp;
7334 if (bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
7335 bb->last_ins->opcode = reverse_map [bb->last_ins->opcode - CEE_BEQ];
7336 } else if (bb->last_ins->opcode >= OP_FBEQ && bb->last_ins->opcode <= OP_FBLT_UN) {
7337 bb->last_ins->opcode = reverse_fmap [bb->last_ins->opcode - OP_FBEQ];
7338 } else if (bb->last_ins->opcode >= OP_LBEQ && bb->last_ins->opcode <= OP_LBLT_UN) {
7339 bb->last_ins->opcode = reverse_lmap [bb->last_ins->opcode - OP_LBEQ];
7342 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
7343 inst->opcode = CEE_BR;
7344 inst->inst_target_bb = bb->last_ins->inst_false_bb;
7345 mono_bblock_add_inst (bb, inst);
7350 #ifdef DEBUG_SELECTION
7351 if (cfg->verbose_level >= 4) {
7352 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7353 MonoInst *tree = bb->code;
7354 g_print ("DUMP BLOCK %d:\n", bb->block_num);
7357 for (; tree; tree = tree->next) {
7358 mono_print_tree (tree);
7365 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7366 MonoInst *tree = bb->code, *next;
7372 bb->last_ins = NULL;
7375 mono_regstate_reset (cfg->rs);
7377 #ifdef DEBUG_SELECTION
7378 if (cfg->verbose_level >= 3)
7379 g_print ("LABEL BLOCK %d:\n", bb->block_num);
7381 for (; tree; tree = next) {
7383 #ifdef DEBUG_SELECTION
7384 if (cfg->verbose_level >= 3) {
7385 mono_print_tree (tree);
7390 if (!(mbstate = mono_burg_label (tree, cfg))) {
7391 g_warning ("unable to label tree %p", tree);
7392 mono_print_tree (tree);
7394 g_assert_not_reached ();
7396 emit_state (cfg, mbstate, MB_NTERM_stmt);
7398 bb->max_ireg = cfg->rs->next_vireg;
7399 bb->max_freg = cfg->rs->next_vfreg;
7402 bb->last_ins->next = NULL;
7404 mono_mempool_empty (cfg->state_pool);
7406 mono_mempool_destroy (cfg->state_pool);
7410 mono_codegen (MonoCompile *cfg)
7412 MonoJumpInfo *patch_info;
7414 int i, max_epilog_size;
7417 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7418 cfg->spill_count = 0;
7419 /* we reuse dfn here */
7420 /* bb->dfn = bb_count++; */
7421 mono_arch_local_regalloc (cfg, bb);
7424 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
7425 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
7427 code = mono_arch_emit_prolog (cfg);
7429 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7430 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
7432 cfg->code_len = code - cfg->native_code;
7433 cfg->prolog_end = cfg->code_len;
7435 mono_debug_open_method (cfg);
7437 /* emit code all basic blocks */
7438 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7439 bb->native_offset = cfg->code_len;
7440 mono_arch_output_basic_block (cfg, bb);
7442 cfg->bb_exit->native_offset = cfg->code_len;
7444 code = cfg->native_code + cfg->code_len;
7446 max_epilog_size = mono_arch_max_epilog_size (cfg);
7448 /* we always allocate code in cfg->domain->code_mp to increase locality */
7449 cfg->code_size = cfg->code_len + max_epilog_size;
7450 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
7452 mono_domain_lock (cfg->domain);
7453 code = mono_code_manager_reserve (cfg->domain->code_mp, cfg->code_size);
7454 mono_domain_unlock (cfg->domain);
7456 memcpy (code, cfg->native_code, cfg->code_len);
7457 g_free (cfg->native_code);
7458 cfg->native_code = code;
7459 code = cfg->native_code + cfg->code_len;
7461 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
7463 cfg->epilog_begin = cfg->code_len;
7465 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7466 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
7468 cfg->code_len = code - cfg->native_code;
7470 mono_arch_emit_epilog (cfg);
7472 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7473 switch (patch_info->type) {
7474 case MONO_PATCH_INFO_ABS: {
7475 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
7477 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
7478 if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) &&
7479 strstr (cfg->method->name, info->name))
7481 * This is an icall wrapper, and this is a call to the
7486 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
7487 patch_info->data.name = info->name;
7491 MonoVTable *vtable = mono_find_class_init_trampoline_by_addr (patch_info->data.target);
7493 patch_info->type = MONO_PATCH_INFO_CLASS_INIT;
7494 patch_info->data.klass = vtable->klass;
7499 case MONO_PATCH_INFO_SWITCH: {
7501 mono_domain_lock (cfg->domain);
7502 table = mono_code_manager_reserve (cfg->domain->code_mp, sizeof (gpointer) * patch_info->table_size);
7503 mono_domain_unlock (cfg->domain);
7505 patch_info->ip.i = patch_info->ip.label->inst_c0;
7506 for (i = 0; i < patch_info->table_size; i++) {
7507 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
7509 patch_info->data.target = table;
7518 if (cfg->verbose_level > 0)
7519 g_print ("Method %s emitted at %p to %p [%s]\n",
7520 mono_method_full_name (cfg->method, TRUE),
7521 cfg->native_code, cfg->native_code + cfg->code_len, cfg->domain->friendly_name);
7523 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info, cfg->run_cctors);
7525 mono_domain_lock (cfg->domain);
7526 mono_code_manager_commit (cfg->domain->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
7527 mono_domain_unlock (cfg->domain);
7529 mono_arch_flush_icache (cfg->native_code, cfg->code_len);
7531 mono_debug_close_method (cfg);
7535 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
7540 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
7541 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
7543 if (cp->opcode == OP_ICONST) {
7544 if (cfg->opt & MONO_OPT_CONSPROP) {
7545 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
7549 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
7550 if (cfg->opt & MONO_OPT_COPYPROP) {
7551 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
7557 arity = mono_burg_arity [tree->opcode];
7560 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
7561 if (cfg->opt & MONO_OPT_CFOLD)
7562 mono_constant_fold_inst (tree, NULL);
7563 /* The opcode may have changed */
7564 if (mono_burg_arity [tree->opcode] > 1) {
7565 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
7566 if (cfg->opt & MONO_OPT_CFOLD)
7567 mono_constant_fold_inst (tree, NULL);
7569 mono_constant_fold_inst (tree, NULL);
7575 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
7579 switch (tree->opcode) {
7589 if (tree->ssa_op == MONO_SSA_NOP) {
7590 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7607 case OP_VOIDCALL_REG:
7608 case OP_VOIDCALLVIRT:
7610 MonoCallInst *call = (MonoCallInst *)tree;
7611 MonoMethodSignature *sig = call->signature;
7612 int i, byref = FALSE;
7614 for (i = 0; i < sig->param_count; i++) {
7615 if (sig->params [i]->byref) {
7622 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7630 arity = mono_burg_arity [tree->opcode];
7636 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7639 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7640 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
7643 g_assert_not_reached ();
7648 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
7650 MonoInst *tree = bb->code;
7656 for (; tree; tree = tree->next) {
7658 mono_cprop_copy_values (cfg, tree, acp);
7660 mono_cprop_invalidate_values (tree, acp, acp_size);
7662 if (tree->ssa_op == MONO_SSA_STORE &&
7663 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
7664 MonoInst *i1 = tree->inst_i1;
7666 acp [tree->inst_i0->inst_c0] = NULL;
7668 for (i = 0; i < acp_size; i++) {
7669 if (acp [i] && acp [i]->opcode != OP_ICONST &&
7670 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
7675 if (i1->opcode == OP_ICONST) {
7676 acp [tree->inst_i0->inst_c0] = i1;
7677 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
7679 if (i1->ssa_op == MONO_SSA_LOAD &&
7680 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
7681 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
7682 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
7683 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
7688 if (tree->opcode == CEE_BEQ) {
7689 g_assert (tree->inst_i0->opcode == OP_COMPARE);
7690 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
7691 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
7693 tree->opcode = CEE_BR;
7694 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
7695 tree->inst_target_bb = tree->inst_true_bb;
7697 tree->inst_target_bb = tree->inst_false_bb;
7706 mono_local_cprop (MonoCompile *cfg)
7711 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
7713 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7714 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
7715 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
7720 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, int parts)
7722 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
7723 guint8 *ip = (guint8 *)header->code;
7726 int dfn = 0, i, code_size_ratio;
7728 mono_jit_stats.methods_compiled++;
7729 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
7730 mono_profiler_method_jit (method);
7732 cfg = g_new0 (MonoCompile, 1);
7733 cfg->method = method;
7734 cfg->mempool = mono_mempool_new ();
7736 cfg->prof_options = mono_profiler_get_events ();
7737 cfg->run_cctors = run_cctors;
7738 cfg->bb_hash = g_hash_table_new (NULL, NULL);
7739 cfg->domain = domain;
7740 cfg->verbose_level = mini_verbose;
7741 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
7742 ((MonoMethodNormal *)method)->header->max_stack);
7744 if (cfg->verbose_level > 2)
7745 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
7748 * create MonoInst* which represents arguments and local variables
7750 mono_compile_create_vars (cfg);
7752 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
7753 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7754 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
7755 mono_destroy_compile (cfg);
7759 mono_jit_stats.basic_blocks += cfg->num_bblocks;
7760 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
7762 if (cfg->num_varinfo > 2000) {
7764 * we disable some optimizations if there are too many variables
7765 * because JIT time may become too expensive. The actual number needs
7766 * to be tweaked and eventually the non-linear algorithms should be fixed.
7768 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
7769 cfg->disable_ssa = TRUE;
7771 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
7773 /* Depth-first ordering on basic blocks */
7774 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
7776 if (cfg->opt & MONO_OPT_BRANCH)
7777 optimize_branches (cfg);
7779 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
7780 if (cfg->num_bblocks != dfn + 1) {
7783 cfg->num_bblocks = dfn + 1;
7785 if (!header->clauses) {
7786 /* remove unreachable code, because the code in them may be
7787 * inconsistent (access to dead variables for example) */
7788 for (bb = cfg->bb_entry; bb;) {
7789 MonoBasicBlock *bbn = bb->next_bb;
7791 if (bbn && bbn->region == -1 && !bbn->dfn) {
7792 if (cfg->verbose_level > 1)
7793 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
7794 bb->next_bb = bbn->next_bb;
7795 nullify_basic_block (bbn);
7803 if (cfg->opt & MONO_OPT_LOOP) {
7804 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
7805 mono_compute_natural_loops (cfg);
7808 /* after method_to_ir */
7812 //#define DEBUGSSA "logic_run"
7813 #define DEBUGSSA_CLASS "Tests"
7816 if (!header->num_clauses && !cfg->disable_ssa) {
7817 mono_local_cprop (cfg);
7818 mono_ssa_compute (cfg);
7822 /* fixme: add all optimizations which requires SSA */
7823 if ((cfg->opt & (MONO_OPT_DEADCE)) || (cfg->opt & (MONO_OPT_ABCREM))) {
7824 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
7825 mono_local_cprop (cfg);
7826 mono_ssa_compute (cfg);
7828 if (cfg->verbose_level >= 2) {
7835 /* after SSA translation */
7839 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
7840 if (cfg->comp_done & MONO_COMP_SSA) {
7841 mono_ssa_cprop (cfg);
7843 mono_local_cprop (cfg);
7847 if (cfg->comp_done & MONO_COMP_SSA) {
7848 mono_ssa_deadce (cfg);
7850 //mono_ssa_strength_reduction (cfg);
7852 if (cfg->opt & MONO_OPT_ABCREM)
7854 mono_perform_abc_removal (cfg);
7857 mono_ssa_remove (cfg);
7859 if (cfg->opt & MONO_OPT_BRANCH)
7860 optimize_branches (cfg);
7863 /* after SSA removal */
7867 decompose_pass (cfg);
7869 if (cfg->opt & MONO_OPT_LINEARS) {
7872 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
7873 cfg->comp_done &= ~MONO_COMP_LIVENESS;
7874 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
7875 mono_analyze_liveness (cfg);
7877 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
7878 regs = mono_arch_get_global_int_regs (cfg);
7879 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
7883 //mono_print_code (cfg);
7887 /* variables are allocated after decompose, since decompose could create temps */
7888 mono_arch_allocate_vars (cfg);
7890 if (cfg->opt & MONO_OPT_CFOLD)
7891 mono_constant_fold (cfg);
7893 mini_select_instructions (cfg);
7896 if (cfg->verbose_level >= 2) {
7897 char *id = mono_method_full_name (cfg->method, FALSE);
7898 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
7902 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
7904 jinfo = g_new0 (MonoJitInfo, 1);
7905 jinfo->method = method;
7906 jinfo->code_start = cfg->native_code;
7907 jinfo->code_size = cfg->code_len;
7908 jinfo->used_regs = cfg->used_int_regs;
7909 jinfo->domain_neutral = (cfg->opt & MONO_OPT_SHARED) != 0;
7911 if (header->num_clauses) {
7914 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
7915 jinfo->num_clauses = header->num_clauses;
7916 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
7917 sizeof (MonoJitExceptionInfo) * header->num_clauses);
7919 for (i = 0; i < header->num_clauses; i++) {
7920 MonoExceptionClause *ec = &header->clauses [i];
7921 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
7922 MonoBasicBlock *tblock;
7924 ei->flags = ec->flags;
7926 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7927 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->token_or_filter);
7929 ei->data.filter = cfg->native_code + tblock->native_offset;
7931 ei->data.token = ec->token_or_filter;
7934 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
7936 ei->try_start = cfg->native_code + tblock->native_offset;
7937 g_assert (tblock->native_offset);
7938 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
7940 ei->try_end = cfg->native_code + tblock->native_offset;
7941 g_assert (tblock->native_offset);
7942 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
7944 ei->handler_start = cfg->native_code + tblock->native_offset;
7948 cfg->jit_info = jinfo;
7950 mono_jit_info_table_add (cfg->domain, jinfo);
7952 /* collect statistics */
7953 mono_jit_stats.allocated_code_size += cfg->code_len;
7954 code_size_ratio = cfg->code_len;
7955 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
7956 mono_jit_stats.biggest_method_size = code_size_ratio;
7957 mono_jit_stats.biggest_method = method;
7959 code_size_ratio = (code_size_ratio * 100) / ((MonoMethodNormal *)method)->header->code_size;
7960 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
7961 mono_jit_stats.max_code_size_ratio = code_size_ratio;
7962 mono_jit_stats.max_ratio_method = method;
7964 mono_jit_stats.native_code_size += cfg->code_len;
7966 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7967 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
7973 mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain)
7976 GHashTable *jit_code_hash;
7977 gpointer code = NULL;
7983 jit_code_hash = target_domain->jit_code_hash;
7985 #ifdef MONO_USE_AOT_COMPILER
7986 if (!mono_compile_aot && (opt & MONO_OPT_AOT)) {
7988 MonoDomain *domain = mono_domain_get ();
7990 mono_domain_lock (domain);
7992 mono_class_init (method->klass);
7993 if ((info = mono_aot_get_method (domain, method))) {
7994 g_hash_table_insert (domain->jit_code_hash, method, info);
7995 mono_domain_unlock (domain);
7996 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
7997 return info->code_start;
8000 mono_domain_unlock (domain);
8004 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
8005 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
8008 if (!method->addr) {
8009 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
8010 method->addr = mono_lookup_internal_call (method);
8012 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
8013 mono_lookup_pinvoke_call (method, NULL, NULL);
8015 #ifdef MONO_USE_EXC_TABLES
8016 if (mono_method_blittable (method)) {
8017 return method->addr;
8020 nm = mono_marshal_get_native_wrapper (method);
8021 return mono_compile_method (nm);
8023 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
8024 //mono_debug_add_wrapper (method, nm);
8025 #ifdef MONO_USE_EXC_TABLES
8028 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
8029 const char *name = method->name;
8032 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
8033 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
8034 /* FIXME: uhm, we need a wrapper to handle exceptions? */
8035 return (gpointer)mono_delegate_ctor;
8036 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
8037 nm = mono_marshal_get_delegate_invoke (method);
8038 return mono_jit_compile_method (nm);
8039 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
8040 nm = mono_marshal_get_delegate_begin_invoke (method);
8041 return mono_jit_compile_method (nm);
8042 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
8043 nm = mono_marshal_get_delegate_end_invoke (method);
8044 return mono_jit_compile_method (nm);
8050 cfg = mini_method_compile (method, opt, target_domain, TRUE, 0);
8052 mono_domain_lock (target_domain);
8054 /* Check if some other thread already did the job. In this case, we can
8055 discard the code this thread generated. */
8057 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
8058 /* We can't use a domain specific method in another domain */
8059 if ((target_domain == mono_domain_get ()) || info->domain_neutral) {
8060 code = info->code_start;
8061 // printf("Discarding code for method %s\n", method->name);
8066 g_hash_table_insert (jit_code_hash, method, cfg->jit_info);
8067 code = cfg->native_code;
8070 mono_destroy_compile (cfg);
8072 if (target_domain->jump_target_hash) {
8073 MonoJumpInfo patch_info;
8075 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
8077 patch_info.next = NULL;
8078 patch_info.ip.i = 0;
8079 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
8080 patch_info.data.method = method;
8081 g_hash_table_remove (target_domain->jump_target_hash, method);
8083 for (tmp = list; tmp; tmp = tmp->next)
8084 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info, TRUE);
8085 g_slist_free (list);
8088 mono_domain_unlock (target_domain);
8090 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
8095 mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt)
8097 /* FIXME: later copy the code from mono */
8098 MonoDomain *target_domain, *domain = mono_domain_get ();
8102 if (default_opt & MONO_OPT_SHARED)
8103 target_domain = mono_root_domain;
8105 target_domain = domain;
8107 mono_domain_lock (target_domain);
8109 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
8110 /* We can't use a domain specific method in another domain */
8111 if (! ((domain != target_domain) && !info->domain_neutral)) {
8112 mono_domain_unlock (target_domain);
8113 mono_jit_stats.methods_lookups++;
8114 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
8115 return info->code_start;
8119 mono_domain_unlock (target_domain);
8120 p = mono_jit_compile_method_inner (method, target_domain);
8125 mono_jit_compile_method (MonoMethod *method)
8127 return mono_jit_compile_method_with_opt (method, default_opt);
8131 mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method)
8133 MonoDomain *target_domain;
8136 if (default_opt & MONO_OPT_SHARED)
8137 target_domain = mono_root_domain;
8139 target_domain = domain;
8141 mono_domain_lock (target_domain);
8143 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
8144 /* We can't use a domain specific method in another domain */
8145 if (! ((domain != target_domain) && !info->domain_neutral)) {
8146 mono_domain_unlock (target_domain);
8147 mono_jit_stats.methods_lookups++;
8148 return info->code_start;
8152 mono_domain_unlock (target_domain);
8158 * mono_jit_runtime_invoke:
8159 * @method: the method to invoke
8160 * @obj: this pointer
8161 * @params: array of parameter values.
8162 * @exc: used to catch exceptions objects
8165 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
8168 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc);
8170 if (obj == NULL && !(method->flags & METHOD_ATTRIBUTE_STATIC) && !method->string_ctor && (method->wrapper_type == 0)) {
8171 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
8175 invoke = mono_marshal_get_runtime_invoke (method);
8176 runtime_invoke = mono_jit_compile_method (invoke);
8177 return runtime_invoke (obj, params, exc);
8180 #ifdef PLATFORM_WIN32
8181 #define GET_CONTEXT \
8182 struct sigcontext *ctx = (struct sigcontext*)_dummy;
8185 #define GET_CONTEXT \
8186 void *ctx = context;
8187 #elif defined(__ppc__) || defined (__powerpc__) || defined (__s390__)
8188 #define GET_CONTEXT \
8189 void *ctx = context;
8191 #define GET_CONTEXT \
8192 void **_p = (void **)&_dummy; \
8193 struct sigcontext *ctx = (struct sigcontext *)++_p;
8197 #ifdef MONO_ARCH_USE_SIGACTION
8198 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, siginfo_t *info, void *context)
8200 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy)
8204 SIG_HANDLER_SIGNATURE (sigfpe_signal_handler)
8209 exc = mono_get_exception_divide_by_zero ();
8211 mono_arch_handle_exception (ctx, exc, FALSE);
8215 SIG_HANDLER_SIGNATURE (sigill_signal_handler)
8219 exc = mono_get_exception_execution_engine ("SIGILL");
8221 mono_arch_handle_exception (ctx, exc, FALSE);
8224 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8227 sigsegv_signal_handler (int _dummy, siginfo_t *info, void *context)
8230 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
8231 struct sigcontext *ctx = (struct sigcontext *)&(((ucontext_t*)context)->uc_mcontext);
8233 /* Can't allocate memory using Boehm GC on altstack */
8234 if (jit_tls->stack_size &&
8235 ((guint8*)info->si_addr >= (guint8*)jit_tls->end_of_stack - jit_tls->stack_size) &&
8236 ((guint8*)info->si_addr < (guint8*)jit_tls->end_of_stack))
8237 exc = mono_domain_get ()->stack_overflow_ex;
8239 exc = mono_domain_get ()->null_reference_ex;
8241 mono_arch_handle_exception (ctx, exc, FALSE);
8247 SIG_HANDLER_SIGNATURE (sigsegv_signal_handler)
8251 mono_arch_handle_exception (ctx, NULL, FALSE);
8257 SIG_HANDLER_SIGNATURE (sigusr1_signal_handler)
8260 gboolean stopRequested = FALSE;
8264 thread = mono_thread_current ();
8266 mono_monitor_try_enter ((MonoObject *)thread, INFINITE);
8267 stopRequested = (thread->state & ThreadState_StopRequested) != 0;
8268 mono_monitor_exit ((MonoObject *)thread);
8270 thread->abort_exc = mono_get_exception_thread_abort ();
8272 if (stopRequested) {
8273 /* Don't throw the exception, just abort the thread */
8274 mono_thread_abort ((MonoObject *) thread->abort_exc);
8277 mono_arch_handle_exception (ctx, thread->abort_exc, FALSE);
8282 SIG_HANDLER_SIGNATURE (sigquit_signal_handler)
8287 exc = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
8289 mono_arch_handle_exception (ctx, exc, FALSE);
8293 SIG_HANDLER_SIGNATURE (sigint_signal_handler)
8298 exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
8300 mono_arch_handle_exception (ctx, exc, FALSE);
8303 #ifndef PLATFORM_WIN32
8305 add_signal_handler (int signo, gpointer handler)
8307 struct sigaction sa;
8309 #ifdef MONO_ARCH_USE_SIGACTION
8310 sa.sa_sigaction = handler;
8311 sigemptyset (&sa.sa_mask);
8312 sa.sa_flags = SA_SIGINFO;
8314 sa.sa_handler = handler;
8315 sigemptyset (&sa.sa_mask);
8318 g_assert (sigaction (signo, &sa, NULL) != -1);
8323 mono_runtime_install_handlers (void)
8325 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8326 struct sigaction sa;
8329 #ifdef PLATFORM_WIN32
8331 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
8332 win32_seh_set_handler(SIGILL, sigill_signal_handler);
8333 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
8334 if (getenv ("MONO_DEBUG"))
8335 win32_seh_set_handler(SIGINT, sigint_signal_handler);
8336 #else /* !PLATFORM_WIN32 */
8338 /* libpthreads has its own implementation of sigaction(),
8339 * but it seems to work well with our current exception
8340 * handlers. If not we must call syscall directly instead
8343 if (getenv ("MONO_DEBUG")) {
8344 add_signal_handler (SIGINT, sigint_signal_handler);
8347 add_signal_handler (SIGFPE, sigfpe_signal_handler);
8348 add_signal_handler (SIGQUIT, sigquit_signal_handler);
8349 add_signal_handler (SIGILL, sigill_signal_handler);
8350 add_signal_handler (SIGBUS, sigsegv_signal_handler);
8351 add_signal_handler (mono_thread_get_abort_signal (), sigusr1_signal_handler);
8354 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8355 sa.sa_sigaction = sigsegv_signal_handler;
8356 sigemptyset (&sa.sa_mask);
8357 sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
8358 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
8360 add_signal_handler (SIGSEGV, sigsegv_signal_handler);
8363 #endif /* PLATFORM_WIN32 */
8366 /* mono_jit_create_remoting_trampoline:
8367 * @method: pointer to the method info
8369 * Creates a trampoline which calls the remoting functions. This
8370 * is used in the vtable of transparent proxies.
8372 * Returns: a pointer to the newly created code
8375 mono_jit_create_remoting_trampoline (MonoMethod *method)
8378 guint8 *addr = NULL;
8380 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
8381 (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
8382 nm = mono_marshal_get_remoting_invoke (method);
8383 addr = mono_compile_method (nm);
8385 addr = mono_compile_method (method);
8391 mini_init (const char *filename)
8395 mono_arch_cpu_init ();
8397 if (!g_thread_supported ())
8398 g_thread_init (NULL);
8400 mono_jit_tls_id = TlsAlloc ();
8401 setup_jit_tls_data ((gpointer)-1, mono_thread_abort);
8403 InitializeCriticalSection (&trampoline_hash_mutex);
8407 if (default_opt & MONO_OPT_AOT)
8410 mono_runtime_install_handlers ();
8411 mono_threads_install_cleanup (mini_thread_cleanup);
8413 #define JIT_TRAMPOLINES_WORK
8414 #ifdef JIT_TRAMPOLINES_WORK
8415 mono_install_compile_method (mono_jit_compile_method);
8416 mono_install_trampoline (mono_arch_create_jit_trampoline);
8417 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
8419 #define JIT_INVOKE_WORKS
8420 #ifdef JIT_INVOKE_WORKS
8421 mono_install_runtime_invoke (mono_jit_runtime_invoke);
8422 mono_install_handler (mono_arch_get_throw_exception ());
8424 mono_install_stack_walk (mono_jit_walk_stack);
8426 domain = mono_init (filename);
8429 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
8430 ves_icall_get_frame_info);
8431 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
8432 ves_icall_get_trace);
8433 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
8434 mono_runtime_install_handlers);
8437 create_helper_signature ();
8439 #define JIT_CALLS_WORK
8440 #ifdef JIT_CALLS_WORK
8441 /* Needs to be called here since register_jit_icall depends on it */
8442 mono_marshal_init ();
8444 mono_arch_register_lowlevel_calls ();
8445 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
8446 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
8447 mono_register_jit_icall (mono_trace_enter_method, "mono_trace_enter_method", NULL, TRUE);
8448 mono_register_jit_icall (mono_trace_leave_method, "mono_trace_leave_method", NULL, TRUE);
8450 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
8451 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
8453 /* fixme: we cant hanlde vararg methods this way, because the signature is not constant */
8454 //mono_register_jit_icall (ves_array_element_address, "ves_array_element_address", NULL);
8455 //mono_register_jit_icall (mono_array_new_va, "mono_array_new_va", NULL);
8457 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
8458 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
8459 helper_sig_void_ptr, TRUE);
8460 mono_register_jit_icall (mono_thread_get_pending_exception, "mono_thread_get_pending_exception", helper_sig_obj_void, FALSE);
8463 * NOTE, NOTE, NOTE, NOTE:
8464 * when adding emulation for some opcodes, remember to also add a dummy
8465 * rule to the burg files, because we need the arity information to be correct.
8467 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult, TRUE);
8468 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un, FALSE);
8469 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf, FALSE);
8470 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv, FALSE);
8471 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un, FALSE);
8472 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem, FALSE);
8473 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un, FALSE);
8475 #ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
8476 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl, TRUE);
8477 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr, TRUE);
8478 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un, TRUE);
8481 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8, FALSE);
8482 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4, FALSE);
8483 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8, FALSE);
8484 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8, FALSE);
8486 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
8487 mono_register_opcode_emulation (OP_FCONV_TO_I8, "__emul_fconv_to_i8", helper_sig_long_double, mono_fconv_i8, FALSE);
8489 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
8490 mono_register_opcode_emulation (CEE_CONV_R_UN, "__emul_conv_r_un", helper_sig_double_int, mono_conv_to_r8_un, FALSE);
8492 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
8493 mono_register_opcode_emulation (OP_LCONV_TO_R8, "__emul_lconv_to_r8", helper_sig_double_long, mono_lconv_to_r8, FALSE);
8495 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
8496 mono_register_opcode_emulation (OP_LCONV_TO_R4, "__emul_lconv_to_r4", helper_sig_float_long, mono_lconv_to_r4, FALSE);
8498 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
8499 mono_register_opcode_emulation (OP_LCONV_TO_R_UN, "__emul_lconv_to_r8_un", helper_sig_double_long, mono_lconv_to_r8_un, FALSE);
8501 #ifdef MONO_ARCH_EMULATE_FREM
8502 mono_register_opcode_emulation (OP_FREM, "__emul_frem", helper_sig_double_double_double, fmod, FALSE);
8505 #if SIZEOF_VOID_P == 4
8506 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4, TRUE);
8509 #warning "fixme: add opcode emulation"
8513 /* other jit icalls */
8514 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
8515 helper_sig_ptr_ptr_ptr, FALSE);
8516 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
8517 mono_register_jit_icall (mono_get_special_static_data, "mono_get_special_static_data", helper_sig_ptr_int, FALSE);
8518 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
8519 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
8520 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
8521 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
8522 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
8523 mono_register_jit_icall (helper_stelem_ref_check, "helper_stelem_ref_check", helper_sig_stelem_ref_check, FALSE);
8524 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
8525 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
8526 mono_register_jit_icall (mono_object_new_fast, "mono_object_new_fast", helper_sig_object_new_specific, FALSE);
8527 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
8528 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
8529 mono_register_jit_icall (mono_string_to_utf16, "mono_string_to_utf16", helper_sig_ptr_obj, FALSE);
8530 mono_register_jit_icall (mono_string_from_utf16, "mono_string_from_utf16", helper_sig_obj_ptr, FALSE);
8531 mono_register_jit_icall (mono_string_new_wrapper, "mono_string_new_wrapper", helper_sig_obj_ptr, FALSE);
8532 mono_register_jit_icall (mono_string_to_utf8, "mono_string_to_utf8", helper_sig_ptr_obj, FALSE);
8533 mono_register_jit_icall (mono_string_to_bstr, "mono_string_to_bstr", helper_sig_ptr_obj, FALSE);
8534 mono_register_jit_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", helper_sig_ptr_obj, FALSE);
8535 mono_register_jit_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", helper_sig_ptr_obj, FALSE);
8536 mono_register_jit_icall (mono_string_builder_to_utf16, "mono_string_builder_to_utf16", helper_sig_ptr_obj, FALSE);
8537 mono_register_jit_icall (mono_array_to_savearray, "mono_array_to_savearray", helper_sig_ptr_obj, FALSE);
8538 mono_register_jit_icall (mono_array_to_lparray, "mono_array_to_lparray", helper_sig_ptr_obj, FALSE);
8539 mono_register_jit_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", helper_sig_ptr_obj, FALSE);
8540 mono_register_jit_icall (mono_marshal_string_array, "mono_marshal_string_array", helper_sig_ptr_obj, FALSE);
8541 mono_register_jit_icall (mono_marshal_string_array_to_unicode, "mono_marshal_string_array_to_unicode", helper_sig_ptr_obj, FALSE);
8542 mono_register_jit_icall (mono_marshal_asany, "mono_marshal_asany", helper_sig_ptr_obj_int, FALSE);
8543 mono_register_jit_icall (mono_marshal_free_asany, "mono_marshal_free_asany", helper_sig_void_obj_ptr_int, FALSE);
8544 mono_register_jit_icall (mono_string_utf8_to_builder, "mono_string_utf8_to_builder", helper_sig_void_ptr_ptr, FALSE);
8545 mono_register_jit_icall (mono_string_utf16_to_builder, "mono_string_utf16_to_builder", helper_sig_void_ptr_ptr, FALSE);
8546 mono_register_jit_icall (mono_marshal_free_array, "mono_marshal_free_array", helper_sig_void_ptr_ptr, FALSE);
8547 mono_register_jit_icall (mono_string_to_byvalstr, "mono_string_to_byvalstr", helper_sig_void_ptr_ptr_ptr, FALSE);
8548 mono_register_jit_icall (mono_string_to_byvalwstr, "mono_string_to_byvalwstr", helper_sig_void_ptr_ptr_ptr, FALSE);
8549 mono_register_jit_icall (g_free, "g_free", helper_sig_void_ptr, FALSE);
8550 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
8551 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
8552 mono_register_jit_icall (mono_ldftn_nosync, "mono_ldftn_nosync", helper_sig_compile, FALSE);
8553 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
8556 #define JIT_RUNTIME_WORKS
8557 #ifdef JIT_RUNTIME_WORKS
8558 mono_runtime_install_cleanup ((MonoDomainFunc)mini_cleanup);
8559 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
8562 mono_thread_attach (domain);
8566 MonoJitStats mono_jit_stats = {0};
8569 print_jit_stats (void)
8571 if (mono_jit_stats.enabled) {
8572 g_print ("Mono Jit statistics\n");
8573 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
8574 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
8575 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
8576 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
8577 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
8578 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
8579 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
8580 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
8581 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
8582 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
8583 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
8584 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
8585 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
8586 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
8587 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
8588 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
8589 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
8590 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
8592 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
8593 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
8594 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
8595 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
8596 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
8598 g_print ("\nGeneric instances: %ld\n", mono_stats.generic_instance_count);
8599 g_print ("Inflated methods: %ld\n", mono_stats.inflated_method_count);
8600 g_print ("Inflated types: %ld\n", mono_stats.inflated_type_count);
8601 g_print ("Generics metadata size: %ld\n", mono_stats.generics_metadata_size);
8606 mini_cleanup (MonoDomain *domain)
8609 * mono_runtime_cleanup() and mono_domain_finalize () need to
8610 * be called early since they need the execution engine still
8611 * fully working (mono_domain_finalize may invoke managed finalizers
8612 * and mono_runtime_cleanup will wait for other threads to finish).
8614 mono_domain_finalize (domain, 2000);
8616 mono_runtime_cleanup (domain);
8618 mono_profiler_shutdown ();
8620 mono_debug_cleanup ();
8622 #ifdef PLATFORM_WIN32
8623 win32_seh_cleanup();
8626 mono_domain_free (domain, TRUE);
8632 mono_set_defaults (int verbose_level, guint32 opts)
8634 mini_verbose = verbose_level;
8639 mono_precompile_assembly (MonoAssembly *ass, void *user_data)
8641 MonoImage *image = ass->image;
8645 if (mini_verbose > 0)
8646 printf ("PRECOMPILE: %s.\n", ass->image->name);
8648 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
8649 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
8650 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
8652 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
8656 if (mini_verbose > 1) {
8657 char * desc = mono_method_full_name (method, TRUE);
8658 g_print ("Compiling %d %s\n", count, desc);
8661 mono_compile_method (method);
8665 void mono_precompile_assemblies ()
8667 mono_assembly_foreach ((GFunc)mono_precompile_assembly, NULL);