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;
5419 CHECK_STACK_OVF (1);
5421 n = read32 (ip + 2);
5422 if (method->wrapper_type != MONO_WRAPPER_NONE)
5423 cmethod = mono_method_get_wrapper_data (method, n);
5425 cmethod = mono_get_method_full (image, n, NULL, generic_context);
5428 mono_class_init (cmethod->klass);
5429 handle_loaded_temps (cfg, bblock, stack_start, sp);
5431 NEW_METHODCONST (cfg, argconst, cmethod);
5432 if (method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED)
5433 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
5435 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn_nosync, &argconst, ip);
5436 NEW_TEMPLOAD (cfg, *sp, temp);
5440 inline_costs += 10 * num_calls++;
5443 case CEE_LDVIRTFTN: {
5449 n = read32 (ip + 2);
5450 if (method->wrapper_type != MONO_WRAPPER_NONE)
5451 cmethod = mono_method_get_wrapper_data (method, n);
5453 cmethod = mono_get_method_full (image, n, NULL, generic_context);
5455 mono_class_init (cmethod->klass);
5456 handle_loaded_temps (cfg, bblock, stack_start, sp);
5460 NEW_METHODCONST (cfg, args [1], cmethod);
5461 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
5462 NEW_TEMPLOAD (cfg, *sp, temp);
5466 inline_costs += 10 * num_calls++;
5470 CHECK_STACK_OVF (1);
5472 n = read16 (ip + 2);
5474 NEW_ARGLOAD (cfg, ins, n);
5480 CHECK_STACK_OVF (1);
5482 n = read16 (ip + 2);
5484 NEW_ARGLOADA (cfg, ins, n);
5492 handle_loaded_temps (cfg, bblock, stack_start, sp);
5494 n = read16 (ip + 2);
5496 NEW_ARGSTORE (cfg, ins, n, *sp);
5498 if (ins->opcode == CEE_STOBJ) {
5499 NEW_ARGLOADA (cfg, ins, n);
5500 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5502 MONO_ADD_INS (bblock, ins);
5506 CHECK_STACK_OVF (1);
5508 n = read16 (ip + 2);
5510 NEW_LOCLOAD (cfg, ins, n);
5516 CHECK_STACK_OVF (1);
5518 n = read16 (ip + 2);
5520 NEW_LOCLOADA (cfg, ins, n);
5529 n = read16 (ip + 2);
5531 handle_loaded_temps (cfg, bblock, stack_start, sp);
5532 NEW_LOCSTORE (cfg, ins, n, *sp);
5534 if (ins->opcode == CEE_STOBJ) {
5535 NEW_LOCLOADA (cfg, ins, n);
5536 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5538 MONO_ADD_INS (bblock, ins);
5545 if (sp != stack_start)
5547 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
5548 ins->inst_left = *sp;
5551 cfg->flags |= MONO_CFG_HAS_ALLOCA;
5552 if (header->init_locals)
5553 ins->flags |= MONO_INST_INIT;
5557 /* FIXME: set init flag if locals init is set in this method */
5559 case CEE_ENDFILTER: {
5560 MonoExceptionClause *clause, *nearest;
5561 int cc, nearest_num;
5565 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
5567 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
5568 ins->inst_left = *sp;
5570 MONO_ADD_INS (bblock, ins);
5571 start_new_bblock = 1;
5576 for (cc = 0; cc < header->num_clauses; ++cc) {
5577 clause = &header->clauses [cc];
5578 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
5579 (!nearest || (clause->token_or_filter > nearest->token_or_filter))) {
5585 filter_lengths [nearest_num] = (ip - header->code) - nearest->token_or_filter;
5589 case CEE_UNALIGNED_:
5590 ins_flag |= MONO_INST_UNALIGNED;
5591 /* FIXME: record alignment? we can assume 1 for now */
5596 ins_flag |= MONO_INST_VOLATILE;
5600 ins_flag |= MONO_INST_TAILCALL;
5601 /* Can't inline tail calls at this time */
5602 inline_costs += 100000;
5609 token = read32 (ip + 2);
5610 if (method->wrapper_type != MONO_WRAPPER_NONE)
5611 klass = mono_method_get_wrapper_data (method, token);
5613 klass = mono_class_get_full (image, token, generic_context);
5614 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5615 MonoInst *store, *load;
5616 NEW_PCONST (cfg, load, NULL);
5617 load->cil_code = ip;
5618 load->type = STACK_OBJ;
5619 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
5620 store->cil_code = ip;
5621 handle_loaded_temps (cfg, bblock, stack_start, sp);
5622 MONO_ADD_INS (bblock, store);
5623 store->inst_i0 = sp [0];
5624 store->inst_i1 = load;
5626 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
5631 case CEE_CONSTRAINED_:
5632 /* FIXME: implement */
5634 token = read32 (ip + 2);
5635 constrained_call = mono_class_get_full (image, token, generic_context);
5640 MonoInst *iargs [3];
5643 if ((cfg->opt & MONO_OPT_INTRINS) && (ip [1] == CEE_CPBLK) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5)) {
5645 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
5646 copy->inst_left = sp [0];
5647 copy->inst_right = sp [1];
5648 copy->cil_code = ip;
5650 MONO_ADD_INS (bblock, copy);
5657 handle_loaded_temps (cfg, bblock, stack_start, sp);
5658 if (ip [1] == CEE_CPBLK) {
5659 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
5661 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
5670 ins_flag |= MONO_INST_NOTYPECHECK;
5672 ins_flag |= MONO_INST_NORANGECHECK;
5673 /* we ignore the no-nullcheck for now since we
5674 * really do it explicitly only when doing callvirt->call
5680 /* FIXME: check we are in a catch handler */
5681 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
5682 load->cil_code = ip;
5683 MONO_INST_NEW (cfg, ins, CEE_THROW);
5684 ins->inst_left = load;
5686 MONO_ADD_INS (bblock, ins);
5688 start_new_bblock = 1;
5693 CHECK_STACK_OVF (1);
5695 token = read32 (ip + 2);
5696 /* FIXXME: handle generics. */
5697 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
5698 MonoType *type = mono_type_create_from_typespec (image, token);
5699 token = mono_type_size (type, &align);
5701 MonoClass *szclass = mono_class_get_full (image, token, generic_context);
5702 mono_class_init (szclass);
5703 token = mono_class_value_size (szclass, &align);
5705 NEW_ICONST (cfg, ins, token);
5710 case CEE_REFANYTYPE:
5712 MONO_INST_NEW (cfg, ins, OP_REFANYTYPE);
5714 ins->type = STACK_MP;
5715 ins->inst_left = *sp;
5716 ins->type = STACK_VTYPE;
5717 ins->klass = mono_defaults.typehandle_class;
5726 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
5731 g_error ("opcode 0x%02x not handled", *ip);
5734 if (start_new_bblock != 1)
5737 bblock->cil_length = ip - bblock->cil_code;
5738 bblock->next_bb = end_bblock;
5739 link_bblock (cfg, bblock, end_bblock);
5741 if (cfg->method == method && cfg->domainvar) {
5745 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
5746 call->signature = helper_sig_domain_get;
5747 call->inst.type = STACK_PTR;
5748 call->fptr = mono_domain_get;
5749 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, (MonoInst*)call);
5751 MONO_ADD_INS (init_localsbb, store);
5754 if (header->init_locals) {
5756 for (i = 0; i < header->num_locals; ++i) {
5757 int t = header->locals [i]->type;
5758 if (t == MONO_TYPE_VALUETYPE && header->locals [i]->data.klass->enumtype)
5759 t = header->locals [i]->data.klass->enum_basetype->type;
5760 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
5761 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
5762 NEW_ICONST (cfg, ins, 0);
5763 NEW_LOCSTORE (cfg, store, i, ins);
5764 MONO_ADD_INS (init_localsbb, store);
5765 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
5766 MONO_INST_NEW (cfg, ins, OP_I8CONST);
5767 ins->type = STACK_I8;
5769 NEW_LOCSTORE (cfg, store, i, ins);
5770 MONO_ADD_INS (init_localsbb, store);
5771 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
5772 MONO_INST_NEW (cfg, ins, OP_R8CONST);
5773 ins->type = STACK_R8;
5774 ins->inst_p0 = (void*)&r8_0;
5775 NEW_LOCSTORE (cfg, store, i, ins);
5776 MONO_ADD_INS (init_localsbb, store);
5777 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF)) {
5778 NEW_LOCLOADA (cfg, ins, i);
5779 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (header->locals [i]), NULL, NULL);
5782 NEW_PCONST (cfg, ins, NULL);
5783 NEW_LOCSTORE (cfg, store, i, ins);
5784 MONO_ADD_INS (init_localsbb, store);
5790 /* resolve backward branches in the middle of an existing basic block */
5791 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
5793 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
5794 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
5795 if (tblock != start_bblock) {
5797 split_bblock (cfg, tblock, bblock);
5798 l = bblock->cil_code - header->code;
5799 bblock->cil_length = tblock->cil_length - l;
5800 tblock->cil_length = l;
5802 g_print ("recheck failed.\n");
5807 * we compute regions here, because the length of filter clauses is not known in advance.
5808 * It is computed in the CEE_ENDFILTER case in the above switch statement
5810 if (cfg->method == method) {
5812 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5813 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
5815 mono_create_spvar_for_region (cfg, bb->region);
5816 if (cfg->verbose_level > 2)
5817 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
5820 g_hash_table_destroy (bbhash);
5823 dont_inline = g_list_remove (dont_inline, method);
5824 return inline_costs;
5827 if (cfg->method != method)
5828 g_hash_table_destroy (bbhash);
5829 dont_inline = g_list_remove (dont_inline, method);
5833 if (cfg->method != method)
5834 g_hash_table_destroy (bbhash);
5835 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
5836 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
5837 dont_inline = g_list_remove (dont_inline, method);
5842 mono_print_tree (MonoInst *tree) {
5848 arity = mono_burg_arity [tree->opcode];
5850 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
5852 switch (tree->opcode) {
5854 printf ("[%d]", tree->inst_c0);
5857 printf ("[%lld]", tree->inst_l);
5860 printf ("[%f]", *(double*)tree->inst_p0);
5863 printf ("[%f]", *(float*)tree->inst_p0);
5867 printf ("[%d]", tree->inst_c0);
5870 if (tree->inst_offset < 0)
5871 printf ("[-0x%x(%s)]", -tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5873 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5876 printf ("[%s]", mono_arch_regname (tree->dreg));
5879 printf ("[%s]", tree->inst_newa_class->name);
5880 mono_print_tree (tree->inst_newa_len);
5891 case OP_VOIDCALLVIRT: {
5892 MonoCallInst *call = (MonoCallInst*)tree;
5894 printf ("[%s]", call->method->name);
5899 printf ("[%d (", tree->inst_c0);
5900 for (i = 0; i < tree->inst_phi_args [0]; i++) {
5903 printf ("%d", tree->inst_phi_args [i + 1]);
5914 case OP_LOAD_MEMBASE:
5915 case OP_LOADI4_MEMBASE:
5916 case OP_LOADU4_MEMBASE:
5917 case OP_LOADU1_MEMBASE:
5918 case OP_LOADI1_MEMBASE:
5919 case OP_LOADU2_MEMBASE:
5920 case OP_LOADI2_MEMBASE:
5921 printf ("[%s] <- [%s + 0x%x]", mono_arch_regname (tree->dreg), mono_arch_regname (tree->inst_basereg), tree->inst_offset);
5924 printf ("[B%d]", tree->inst_target_bb->block_num);
5934 case OP_VOIDCALL_REG:
5935 mono_print_tree (tree->inst_left);
5947 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
5948 mono_print_tree (tree->inst_left);
5951 if (!mono_arch_print_tree(tree, arity)) {
5953 mono_print_tree (tree->inst_left);
5955 mono_print_tree (tree->inst_right);
5966 mono_print_tree_nl (MonoInst *tree)
5968 mono_print_tree (tree);
5973 create_helper_signature (void)
5975 /* FIXME: set call conv */
5976 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
5977 helper_sig_newarr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5978 helper_sig_newarr->params [0] = helper_sig_newarr->params [1] = &mono_defaults.int_class->byval_arg;
5979 helper_sig_newarr->ret = &mono_defaults.object_class->byval_arg;
5980 helper_sig_newarr->params [2] = &mono_defaults.int32_class->byval_arg;
5981 helper_sig_newarr->pinvoke = 1;
5983 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
5984 helper_sig_newarr_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5985 helper_sig_newarr_specific->params [0] = &mono_defaults.int_class->byval_arg;
5986 helper_sig_newarr_specific->params [1] = &mono_defaults.int32_class->byval_arg;
5987 helper_sig_newarr_specific->ret = &mono_defaults.object_class->byval_arg;
5988 helper_sig_newarr_specific->pinvoke = 1;
5990 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
5991 helper_sig_object_new = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5992 helper_sig_object_new->params [0] = helper_sig_object_new->params [1] = &mono_defaults.int_class->byval_arg;
5993 helper_sig_object_new->ret = &mono_defaults.object_class->byval_arg;
5994 helper_sig_object_new->pinvoke = 1;
5996 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
5997 helper_sig_object_new_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5998 helper_sig_object_new_specific->params [0] = &mono_defaults.int_class->byval_arg;
5999 helper_sig_object_new_specific->ret = &mono_defaults.object_class->byval_arg;
6000 helper_sig_object_new_specific->pinvoke = 1;
6002 /* void* mono_method_compile (MonoMethod*) */
6003 helper_sig_compile = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6004 helper_sig_compile->params [0] = helper_sig_compile->ret = &mono_defaults.int_class->byval_arg;
6005 helper_sig_compile->pinvoke = 1;
6007 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
6008 helper_sig_compile_virt = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6009 helper_sig_compile_virt->params [0] = &mono_defaults.object_class->byval_arg;
6010 helper_sig_compile_virt->params [1] = helper_sig_compile_virt->ret = &mono_defaults.int_class->byval_arg;
6011 helper_sig_compile_virt->pinvoke = 1;
6013 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
6014 helper_sig_ldstr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
6015 helper_sig_ldstr->params [0] = helper_sig_ldstr->params [1] = &mono_defaults.int_class->byval_arg;
6016 helper_sig_ldstr->params [2] = &mono_defaults.int32_class->byval_arg;
6017 helper_sig_ldstr->ret = &mono_defaults.object_class->byval_arg;
6018 helper_sig_ldstr->pinvoke = 1;
6020 /* MonoDomain *mono_domain_get (void) */
6021 helper_sig_domain_get = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
6022 helper_sig_domain_get->ret = &mono_defaults.int_class->byval_arg;
6023 helper_sig_domain_get->pinvoke = 1;
6025 /* void* stelem_ref (MonoArray *, int index, MonoObject *) */
6026 helper_sig_stelem_ref = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
6027 helper_sig_stelem_ref->params [0] = &mono_defaults.array_class->byval_arg;
6028 helper_sig_stelem_ref->params [1] = &mono_defaults.int32_class->byval_arg;
6029 helper_sig_stelem_ref->params [2] = &mono_defaults.object_class->byval_arg;
6030 helper_sig_stelem_ref->ret = &mono_defaults.void_class->byval_arg;
6031 helper_sig_stelem_ref->pinvoke = 1;
6033 /* void* stelem_ref_check (MonoArray *, MonoObject *) */
6034 helper_sig_stelem_ref_check = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6035 helper_sig_stelem_ref_check->params [0] = &mono_defaults.array_class->byval_arg;
6036 helper_sig_stelem_ref_check->params [1] = &mono_defaults.object_class->byval_arg;
6037 helper_sig_stelem_ref_check->ret = &mono_defaults.void_class->byval_arg;
6038 helper_sig_stelem_ref_check->pinvoke = 1;
6040 /* long amethod (long, long) */
6041 helper_sig_long_long_long = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6042 helper_sig_long_long_long->params [0] = helper_sig_long_long_long->params [1] =
6043 &mono_defaults.int64_class->byval_arg;
6044 helper_sig_long_long_long->ret = &mono_defaults.int64_class->byval_arg;
6045 helper_sig_long_long_long->pinvoke = 1;
6047 /* object amethod (intptr) */
6048 helper_sig_obj_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6049 helper_sig_obj_ptr->params [0] = &mono_defaults.int_class->byval_arg;
6050 helper_sig_obj_ptr->ret = &mono_defaults.object_class->byval_arg;
6051 helper_sig_obj_ptr->pinvoke = 1;
6053 /* void amethod (intptr) */
6054 helper_sig_void_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6055 helper_sig_void_ptr->params [0] = &mono_defaults.int_class->byval_arg;
6056 helper_sig_void_ptr->ret = &mono_defaults.void_class->byval_arg;
6057 helper_sig_void_ptr->pinvoke = 1;
6059 /* void amethod (MonoObject *obj) */
6060 helper_sig_void_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6061 helper_sig_void_obj->params [0] = &mono_defaults.object_class->byval_arg;
6062 helper_sig_void_obj->ret = &mono_defaults.void_class->byval_arg;
6063 helper_sig_void_obj->pinvoke = 1;
6065 /* void amethod (MonoObject *obj, void *ptr, int i) */
6066 helper_sig_void_obj_ptr_int = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
6067 helper_sig_void_obj_ptr_int->params [0] = &mono_defaults.object_class->byval_arg;
6068 helper_sig_void_obj_ptr_int->params [1] = &mono_defaults.int_class->byval_arg;
6069 helper_sig_void_obj_ptr_int->params [2] = &mono_defaults.int_class->byval_arg;
6070 helper_sig_void_obj_ptr_int->ret = &mono_defaults.void_class->byval_arg;
6071 helper_sig_void_obj_ptr_int->pinvoke = 1;
6073 /* intptr amethod (void) */
6074 helper_sig_ptr_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
6075 helper_sig_ptr_void->ret = &mono_defaults.int_class->byval_arg;
6076 helper_sig_ptr_void->pinvoke = 1;
6078 /* object amethod (void) */
6079 helper_sig_obj_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
6080 helper_sig_obj_void->ret = &mono_defaults.object_class->byval_arg;
6081 helper_sig_obj_void->pinvoke = 1;
6083 /* void amethod (intptr, intptr) */
6084 helper_sig_void_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6085 helper_sig_void_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
6086 helper_sig_void_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
6087 helper_sig_void_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
6088 helper_sig_void_ptr_ptr->pinvoke = 1;
6090 /* void amethod (intptr, intptr, intptr) */
6091 helper_sig_void_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
6092 helper_sig_void_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
6093 helper_sig_void_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
6094 helper_sig_void_ptr_ptr_ptr->params [2] = &mono_defaults.int_class->byval_arg;
6095 helper_sig_void_ptr_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
6096 helper_sig_void_ptr_ptr_ptr->pinvoke = 1;
6098 /* intptr amethod (intptr, intptr) */
6099 helper_sig_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6100 helper_sig_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
6101 helper_sig_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
6102 helper_sig_ptr_ptr_ptr->ret = &mono_defaults.int_class->byval_arg;
6103 helper_sig_ptr_ptr_ptr->pinvoke = 1;
6105 /* IntPtr amethod (object) */
6106 helper_sig_ptr_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6107 helper_sig_ptr_obj->params [0] = &mono_defaults.object_class->byval_arg;
6108 helper_sig_ptr_obj->ret = &mono_defaults.int_class->byval_arg;
6109 helper_sig_ptr_obj->pinvoke = 1;
6111 /* IntPtr amethod (object, int) */
6112 helper_sig_ptr_obj_int = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6113 helper_sig_ptr_obj_int->params [0] = &mono_defaults.object_class->byval_arg;
6114 helper_sig_ptr_obj_int->params [1] = &mono_defaults.int_class->byval_arg;
6115 helper_sig_ptr_obj_int->ret = &mono_defaults.int_class->byval_arg;
6116 helper_sig_ptr_obj_int->pinvoke = 1;
6118 /* IntPtr amethod (int) */
6119 helper_sig_ptr_int = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6120 helper_sig_ptr_int->params [0] = &mono_defaults.int32_class->byval_arg;
6121 helper_sig_ptr_int->ret = &mono_defaults.int_class->byval_arg;
6122 helper_sig_ptr_int->pinvoke = 1;
6124 /* long amethod (long, guint32) */
6125 helper_sig_long_long_int = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6126 helper_sig_long_long_int->params [0] = &mono_defaults.int64_class->byval_arg;
6127 helper_sig_long_long_int->params [1] = &mono_defaults.int32_class->byval_arg;
6128 helper_sig_long_long_int->ret = &mono_defaults.int64_class->byval_arg;
6129 helper_sig_long_long_int->pinvoke = 1;
6131 /* ulong amethod (double) */
6132 helper_sig_ulong_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6133 helper_sig_ulong_double->params [0] = &mono_defaults.double_class->byval_arg;
6134 helper_sig_ulong_double->ret = &mono_defaults.uint64_class->byval_arg;
6135 helper_sig_ulong_double->pinvoke = 1;
6137 /* long amethod (double) */
6138 helper_sig_long_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6139 helper_sig_long_double->params [0] = &mono_defaults.double_class->byval_arg;
6140 helper_sig_long_double->ret = &mono_defaults.int64_class->byval_arg;
6141 helper_sig_long_double->pinvoke = 1;
6143 /* double amethod (long) */
6144 helper_sig_double_long = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6145 helper_sig_double_long->params [0] = &mono_defaults.int64_class->byval_arg;
6146 helper_sig_double_long->ret = &mono_defaults.double_class->byval_arg;
6147 helper_sig_double_long->pinvoke = 1;
6149 /* double amethod (int) */
6150 helper_sig_double_int = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6151 helper_sig_double_int->params [0] = &mono_defaults.int32_class->byval_arg;
6152 helper_sig_double_int->ret = &mono_defaults.double_class->byval_arg;
6153 helper_sig_double_int->pinvoke = 1;
6155 /* float amethod (long) */
6156 helper_sig_float_long = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6157 helper_sig_float_long->params [0] = &mono_defaults.int64_class->byval_arg;
6158 helper_sig_float_long->ret = &mono_defaults.single_class->byval_arg;
6159 helper_sig_float_long->pinvoke = 1;
6161 /* double amethod (double, double) */
6162 helper_sig_double_double_double = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6163 helper_sig_double_double_double->params [0] = &mono_defaults.double_class->byval_arg;
6164 helper_sig_double_double_double->params [1] = &mono_defaults.double_class->byval_arg;
6165 helper_sig_double_double_double->ret = &mono_defaults.double_class->byval_arg;
6166 helper_sig_double_double_double->pinvoke = 1;
6168 /* uint amethod (double) */
6169 helper_sig_uint_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6170 helper_sig_uint_double->params [0] = &mono_defaults.double_class->byval_arg;
6171 helper_sig_uint_double->ret = &mono_defaults.uint32_class->byval_arg;
6172 helper_sig_uint_double->pinvoke = 1;
6174 /* int amethod (double) */
6175 helper_sig_int_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6176 helper_sig_int_double->params [0] = &mono_defaults.double_class->byval_arg;
6177 helper_sig_int_double->ret = &mono_defaults.int32_class->byval_arg;
6178 helper_sig_int_double->pinvoke = 1;
6180 /* void initobj (intptr, int size) */
6181 helper_sig_initobj = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6182 helper_sig_initobj->params [0] = &mono_defaults.int_class->byval_arg;
6183 helper_sig_initobj->params [1] = &mono_defaults.int32_class->byval_arg;
6184 helper_sig_initobj->ret = &mono_defaults.void_class->byval_arg;
6185 helper_sig_initobj->pinvoke = 1;
6187 /* void memcpy (intptr, intptr, int size) */
6188 helper_sig_memcpy = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
6189 helper_sig_memcpy->params [0] = &mono_defaults.int_class->byval_arg;
6190 helper_sig_memcpy->params [1] = &mono_defaults.int_class->byval_arg;
6191 helper_sig_memcpy->params [2] = &mono_defaults.int32_class->byval_arg;
6192 helper_sig_memcpy->ret = &mono_defaults.void_class->byval_arg;
6193 helper_sig_memcpy->pinvoke = 1;
6195 /* void memset (intptr, int val, int size) */
6196 helper_sig_memset = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
6197 helper_sig_memset->params [0] = &mono_defaults.int_class->byval_arg;
6198 helper_sig_memset->params [1] = &mono_defaults.int32_class->byval_arg;
6199 helper_sig_memset->params [2] = &mono_defaults.int32_class->byval_arg;
6200 helper_sig_memset->ret = &mono_defaults.void_class->byval_arg;
6201 helper_sig_memset->pinvoke = 1;
6203 helper_sig_class_init_trampoline = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
6204 helper_sig_class_init_trampoline->ret = &mono_defaults.void_class->byval_arg;
6205 helper_sig_class_init_trampoline->pinvoke = 1;
6208 static GHashTable *jit_icall_hash_name = NULL;
6209 static GHashTable *jit_icall_hash_addr = NULL;
6212 mono_find_jit_icall_by_name (const char *name)
6214 MonoJitICallInfo *info;
6215 g_assert (jit_icall_hash_name);
6217 //printf ("lookup addr %s %p\n", name, g_hash_table_lookup (jit_icall_hash_name, name));
6218 EnterCriticalSection (&trampoline_hash_mutex);
6219 info = g_hash_table_lookup (jit_icall_hash_name, name);
6220 LeaveCriticalSection (&trampoline_hash_mutex);
6225 mono_find_jit_icall_by_addr (gconstpointer addr)
6227 MonoJitICallInfo *info;
6228 g_assert (jit_icall_hash_addr);
6230 EnterCriticalSection (&trampoline_hash_mutex);
6231 info = g_hash_table_lookup (jit_icall_hash_addr, (gpointer)addr);
6232 LeaveCriticalSection (&trampoline_hash_mutex);
6238 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
6241 MonoMethod *wrapper;
6244 if (callinfo->wrapper)
6245 return callinfo->wrapper;
6247 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
6248 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
6249 /* Must be domain neutral since there is only one copy */
6250 code = mono_jit_compile_method_with_opt (wrapper, default_opt | MONO_OPT_SHARED);
6252 EnterCriticalSection (&trampoline_hash_mutex);
6253 if (!callinfo->wrapper) {
6254 callinfo->wrapper = code;
6255 g_hash_table_insert (jit_icall_hash_addr, (gpointer)callinfo->wrapper, callinfo);
6257 LeaveCriticalSection (&trampoline_hash_mutex);
6260 return callinfo->wrapper;
6264 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save)
6266 MonoJitICallInfo *info;
6271 EnterCriticalSection (&trampoline_hash_mutex);
6273 if (!jit_icall_hash_name) {
6274 jit_icall_hash_name = g_hash_table_new (g_str_hash, g_str_equal);
6275 jit_icall_hash_addr = g_hash_table_new (NULL, NULL);
6278 if (g_hash_table_lookup (jit_icall_hash_name, name)) {
6279 g_warning ("jit icall already defined \"%s\"\n", name);
6280 g_assert_not_reached ();
6283 info = g_new (MonoJitICallInfo, 1);
6290 #ifdef MONO_USE_EXC_TABLES
6291 || mono_arch_has_unwind_info (func)
6294 info->wrapper = func;
6296 info->wrapper = NULL;
6299 g_hash_table_insert (jit_icall_hash_name, (gpointer)info->name, info);
6300 g_hash_table_insert (jit_icall_hash_addr, (gpointer)func, info);
6302 LeaveCriticalSection (&trampoline_hash_mutex);
6307 mono_create_class_init_trampoline (MonoVTable *vtable)
6311 /* previously created trampoline code */
6312 mono_domain_lock (vtable->domain);
6314 mono_g_hash_table_lookup (vtable->domain->class_init_trampoline_hash,
6316 mono_domain_unlock (vtable->domain);
6320 code = mono_arch_create_class_init_trampoline (vtable);
6322 /* store trampoline address */
6323 mono_domain_lock (vtable->domain);
6324 mono_g_hash_table_insert (vtable->domain->class_init_trampoline_hash,
6326 mono_domain_unlock (vtable->domain);
6328 EnterCriticalSection (&trampoline_hash_mutex);
6329 if (!class_init_hash_addr)
6330 class_init_hash_addr = g_hash_table_new (NULL, NULL);
6331 g_hash_table_insert (class_init_hash_addr, code, vtable);
6332 LeaveCriticalSection (&trampoline_hash_mutex);
6338 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method,
6339 gboolean add_sync_wrapper)
6344 if (add_sync_wrapper && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
6345 return mono_create_jump_trampoline (domain, mono_marshal_get_synchronized_wrapper (method), FALSE);
6347 code = mono_jit_find_compiled_method (domain, method);
6351 EnterCriticalSection (&trampoline_hash_mutex);
6353 if (jump_trampoline_hash) {
6354 code = g_hash_table_lookup (jump_trampoline_hash, method);
6356 LeaveCriticalSection (&trampoline_hash_mutex);
6361 ji = mono_arch_create_jump_trampoline (method);
6364 * mono_delegate_ctor needs to find the method metadata from the
6365 * trampoline address, so we save it here.
6368 mono_jit_info_table_add (mono_root_domain, ji);
6370 if (!jump_trampoline_hash)
6371 jump_trampoline_hash = g_hash_table_new (NULL, NULL);
6372 g_hash_table_insert (jump_trampoline_hash, method, ji->code_start);
6374 LeaveCriticalSection (&trampoline_hash_mutex);
6376 return ji->code_start;
6380 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
6384 EnterCriticalSection (&trampoline_hash_mutex);
6385 if (class_init_hash_addr)
6386 res = g_hash_table_lookup (class_init_hash_addr, addr);
6389 LeaveCriticalSection (&trampoline_hash_mutex);
6394 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func, gboolean no_throw)
6396 MonoJitICallInfo *info;
6398 if (!emul_opcode_map)
6399 emul_opcode_map = g_new0 (MonoJitICallInfo*, OP_LAST + 1);
6401 g_assert (!sig->hasthis);
6402 g_assert (sig->param_count < 3);
6404 info = mono_register_jit_icall (func, name, sig, no_throw);
6406 emul_opcode_map [opcode] = info;
6410 decompose_foreach (MonoInst *tree, gpointer data)
6412 static MonoJitICallInfo *newarr_info = NULL;
6413 static MonoJitICallInfo *newarr_specific_info = NULL;
6414 MonoJitICallInfo *info;
6417 switch (tree->opcode) {
6419 MonoCompile *cfg = data;
6420 MonoInst *iargs [3];
6423 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
6424 g_assert (newarr_info);
6425 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
6426 g_assert (newarr_specific_info);
6429 if (cfg->opt & MONO_OPT_SHARED) {
6430 NEW_DOMAINCONST (cfg, iargs [0]);
6431 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
6432 iargs [2] = tree->inst_newa_len;
6437 MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
6439 NEW_VTABLECONST (cfg, iargs [0], vtable);
6440 iargs [1] = tree->inst_newa_len;
6442 info = newarr_specific_info;
6445 mono_emulate_opcode (cfg, tree, iargs, info);
6447 /* Need to decompose arguments after the the opcode is decomposed */
6448 for (i = 0; i < info->sig->param_count; ++i)
6449 dec_foreach (iargs [i], cfg);
6459 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
6461 switch (mono_burg_arity [tree->opcode]) {
6464 mono_inst_foreach (tree->inst_left, func, data);
6467 mono_inst_foreach (tree->inst_left, func, data);
6468 mono_inst_foreach (tree->inst_right, func, data);
6471 g_assert_not_reached ();
6478 mono_print_bb_code (MonoBasicBlock *bb) {
6480 MonoInst *c = bb->code;
6482 mono_print_tree (c);
6490 print_dfn (MonoCompile *cfg) {
6495 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
6497 for (i = 0; i < cfg->num_bblocks; ++i) {
6498 bb = cfg->bblocks [i];
6500 char* code1, *code2;
6501 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
6502 if (bb->last_ins->cil_code)
6503 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
6505 code2 = g_strdup ("");
6507 code1 [strlen (code1) - 1] = 0;
6508 code = g_strdup_printf ("%s -> %s", code1, code2);
6512 code = g_strdup ("\n");
6513 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
6515 MonoInst *c = bb->code;
6517 mono_print_tree (c);
6525 g_print ("\tprev:");
6526 for (j = 0; j < bb->in_count; ++j) {
6527 g_print (" BB%d", bb->in_bb [j]->block_num);
6529 g_print ("\t\tsucc:");
6530 for (j = 0; j < bb->out_count; ++j) {
6531 g_print (" BB%d", bb->out_bb [j]->block_num);
6533 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
6536 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
6539 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
6541 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
6549 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
6553 g_assert (bb->code);
6554 bb->last_ins->next = inst;
6555 bb->last_ins = inst;
6557 bb->last_ins = bb->code = inst;
6562 mono_destroy_compile (MonoCompile *cfg)
6564 //mono_mempool_stats (cfg->mempool);
6565 g_hash_table_destroy (cfg->bb_hash);
6566 mono_free_loop_info (cfg);
6568 mono_regstate_free (cfg->rs);
6570 g_hash_table_destroy (cfg->spvars);
6571 mono_mempool_destroy (cfg->mempool);
6572 g_list_free (cfg->ldstr_list);
6574 g_free (cfg->varinfo);
6580 mono_get_lmf_addr (void)
6582 MonoJitTlsData *jit_tls;
6584 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
6585 return &jit_tls->lmf;
6587 g_assert_not_reached ();
6592 * mono_thread_abort:
6593 * @obj: exception object
6595 * abort the thread, print exception information and stack trace
6598 mono_thread_abort (MonoObject *obj)
6600 /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
6602 /* handle_remove should be eventually called for this thread, too
6609 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
6611 MonoJitTlsData *jit_tls;
6614 jit_tls = TlsGetValue (mono_jit_tls_id);
6618 jit_tls = g_new0 (MonoJitTlsData, 1);
6620 TlsSetValue (mono_jit_tls_id, jit_tls);
6622 jit_tls->abort_func = abort_func;
6623 jit_tls->end_of_stack = stack_start;
6625 lmf = g_new0 (MonoLMF, 1);
6628 jit_tls->lmf = jit_tls->first_lmf = lmf;
6630 mono_arch_setup_jit_tls_data (jit_tls);
6636 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
6639 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
6640 thread = mono_thread_current ();
6642 thread->jit_data = jit_tls;
6645 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
6648 mono_thread_abort_dummy (MonoObject *obj)
6650 if (mono_thread_attach_aborted_cb)
6651 mono_thread_attach_aborted_cb (obj);
6653 mono_thread_abort (obj);
6657 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
6660 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
6661 thread = mono_thread_current ();
6663 thread->jit_data = jit_tls;
6667 mini_thread_cleanup (MonoThread *thread)
6669 MonoJitTlsData *jit_tls = thread->jit_data;
6672 mono_arch_free_jit_tls_data (jit_tls);
6673 g_free (jit_tls->first_lmf);
6675 thread->jit_data = NULL;
6680 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
6682 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
6686 ji->data.target = target;
6687 ji->next = cfg->patch_info;
6689 cfg->patch_info = ji;
6693 mono_remove_patch_info (MonoCompile *cfg, int ip)
6695 MonoJumpInfo **ji = &cfg->patch_info;
6698 if ((*ji)->ip.i == ip)
6701 ji = &((*ji)->next);
6706 mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors)
6708 unsigned char *ip = patch_info->ip.i + code;
6709 gconstpointer target = NULL;
6711 switch (patch_info->type) {
6712 case MONO_PATCH_INFO_BB:
6713 target = patch_info->data.bb->native_offset + code;
6715 case MONO_PATCH_INFO_ABS:
6716 target = patch_info->data.target;
6718 case MONO_PATCH_INFO_LABEL:
6719 target = patch_info->data.inst->inst_c0 + code;
6721 case MONO_PATCH_INFO_IP:
6724 case MONO_PATCH_INFO_METHOD_REL:
6725 target = code + patch_info->data.offset;
6727 case MONO_PATCH_INFO_INTERNAL_METHOD: {
6728 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
6730 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
6731 g_assert_not_reached ();
6733 target = mono_icall_get_wrapper (mi);
6736 case MONO_PATCH_INFO_METHOD_JUMP: {
6739 /* get the trampoline to the method from the domain */
6740 target = mono_create_jump_trampoline (domain, patch_info->data.method, TRUE);
6741 if (!domain->jump_target_hash)
6742 domain->jump_target_hash = g_hash_table_new (NULL, NULL);
6743 list = g_hash_table_lookup (domain->jump_target_hash, patch_info->data.method);
6744 list = g_slist_prepend (list, ip);
6745 g_hash_table_insert (domain->jump_target_hash, patch_info->data.method, list);
6748 case MONO_PATCH_INFO_METHOD:
6749 if (patch_info->data.method == method) {
6752 /* get the trampoline to the method from the domain */
6753 target = mono_arch_create_jit_trampoline (patch_info->data.method);
6755 case MONO_PATCH_INFO_SWITCH: {
6756 gpointer *jump_table;
6759 mono_domain_lock (domain);
6760 jump_table = mono_code_manager_reserve (domain->code_mp, sizeof (gpointer) * patch_info->table_size);
6761 mono_domain_unlock (domain);
6763 for (i = 0; i < patch_info->table_size; i++) {
6764 jump_table [i] = code + (int)patch_info->data.table [i];
6766 target = jump_table;
6769 case MONO_PATCH_INFO_METHODCONST:
6770 case MONO_PATCH_INFO_CLASS:
6771 case MONO_PATCH_INFO_IMAGE:
6772 case MONO_PATCH_INFO_FIELD:
6773 target = patch_info->data.target;
6775 case MONO_PATCH_INFO_IID:
6776 mono_class_init (patch_info->data.klass);
6777 target = (gpointer)patch_info->data.klass->interface_id;
6779 case MONO_PATCH_INFO_VTABLE:
6780 target = mono_class_vtable (domain, patch_info->data.klass);
6782 case MONO_PATCH_INFO_CLASS_INIT:
6783 target = mono_create_class_init_trampoline (mono_class_vtable (domain, patch_info->data.klass));
6785 case MONO_PATCH_INFO_SFLDA: {
6786 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
6787 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (vtable->klass, method))
6788 /* Done by the generated code */
6792 mono_runtime_class_init (vtable);
6794 target = (char*)vtable->data + patch_info->data.field->offset;
6797 case MONO_PATCH_INFO_R4:
6798 case MONO_PATCH_INFO_R8:
6799 target = patch_info->data.target;
6801 case MONO_PATCH_INFO_EXC_NAME:
6802 target = patch_info->data.name;
6804 case MONO_PATCH_INFO_LDSTR:
6806 mono_ldstr (domain, patch_info->data.token->image,
6807 mono_metadata_token_index (patch_info->data.token->token));
6809 case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
6811 MonoClass *handle_class;
6813 handle = mono_ldtoken (patch_info->data.token->image,
6814 patch_info->data.token->token, &handle_class, NULL);
6815 mono_class_init (handle_class);
6816 mono_class_init (mono_class_from_mono_type (handle));
6819 mono_type_get_object (domain, handle);
6822 case MONO_PATCH_INFO_LDTOKEN: {
6824 MonoClass *handle_class;
6826 handle = mono_ldtoken (patch_info->data.token->image,
6827 patch_info->data.token->token, &handle_class, NULL);
6828 mono_class_init (handle_class);
6833 case MONO_PATCH_INFO_BB_OVF:
6834 case MONO_PATCH_INFO_EXC_OVF:
6837 g_assert_not_reached ();
6840 return (gpointer)target;
6844 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
6845 MonoJitICallInfo *info;
6847 decompose_foreach (tree, cfg);
6849 switch (mono_burg_arity [tree->opcode]) {
6852 dec_foreach (tree->inst_left, cfg);
6854 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6855 MonoInst *iargs [2];
6857 iargs [0] = tree->inst_left;
6859 mono_emulate_opcode (cfg, tree, iargs, info);
6865 #ifdef MONO_ARCH_BIGMUL_INTRINS
6866 if (tree->opcode == OP_LMUL
6867 && (cfg->opt & MONO_OPT_INTRINS)
6868 && (tree->inst_left->opcode == CEE_CONV_I8
6869 || tree->inst_left->opcode == CEE_CONV_U8)
6870 && tree->inst_left->inst_left->type == STACK_I4
6871 && (tree->inst_right->opcode == CEE_CONV_I8
6872 || tree->inst_right->opcode == CEE_CONV_U8)
6873 && tree->inst_right->inst_left->type == STACK_I4) {
6874 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
6875 tree->inst_left = tree->inst_left->inst_left;
6876 tree->inst_right = tree->inst_right->inst_left;
6877 dec_foreach (tree, cfg);
6880 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6881 MonoInst *iargs [2];
6883 iargs [0] = tree->inst_i0;
6884 iargs [1] = tree->inst_i1;
6886 mono_emulate_opcode (cfg, tree, iargs, info);
6888 dec_foreach (iargs [0], cfg);
6889 dec_foreach (iargs [1], cfg);
6892 dec_foreach (tree->inst_left, cfg);
6893 dec_foreach (tree->inst_right, cfg);
6897 g_assert_not_reached ();
6902 decompose_pass (MonoCompile *cfg) {
6905 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6908 cfg->prev_ins = NULL;
6909 for (tree = cfg->cbb->code; tree; tree = tree->next) {
6910 dec_foreach (tree, cfg);
6911 cfg->prev_ins = tree;
6917 nullify_basic_block (MonoBasicBlock *bb)
6924 bb->code = bb->last_ins = NULL;
6925 bb->cil_code = NULL;
6929 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6933 for (i = 0; i < bb->out_count; i++) {
6934 MonoBasicBlock *ob = bb->out_bb [i];
6937 if (bb->out_count > 1) {
6938 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
6942 bb->out_bb [i] = repl;
6949 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6953 for (i = 0; i < bb->in_count; i++) {
6954 MonoBasicBlock *ib = bb->in_bb [i];
6957 if (bb->in_count > 1) {
6958 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
6962 bb->in_bb [i] = repl;
6969 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6973 for (i = 0; i < bb->out_count; i++) {
6974 MonoBasicBlock *ob = bb->out_bb [i];
6975 for (j = 0; j < ob->in_count; j++) {
6976 if (ob->in_bb [j] == orig) {
6977 ob->in_bb [j] = repl;
6986 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
6988 bb->out_count = bbn->out_count;
6989 bb->out_bb = bbn->out_bb;
6991 replace_basic_block (bb, bbn, bb);
6995 bb->last_ins->next = bbn->code;
6996 bb->last_ins = bbn->last_ins;
6999 bb->code = bbn->code;
7000 bb->last_ins = bbn->last_ins;
7002 bb->next_bb = bbn->next_bb;
7003 nullify_basic_block (bbn);
7007 * Optimizes the branches on the Control Flow Graph
7011 optimize_branches (MonoCompile *cfg)
7013 int i, changed = FALSE;
7014 MonoBasicBlock *bb, *bbn;
7015 guint32 niterations;
7018 * Some crazy loops could cause the code below to go into an infinite
7019 * loop, see bug #53003 for an example. To prevent this, we put an upper
7020 * bound on the number of iterations.
7027 /* we skip the entry block (exit is handled specially instead ) */
7028 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
7030 /* dont touch code inside exception clauses */
7031 if (bb->region != -1)
7034 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
7035 if (cfg->verbose_level > 2)
7036 g_print ("nullify block triggered %d\n", bbn->block_num);
7038 bb->next_bb = bbn->next_bb;
7040 for (i = 0; i < bbn->out_count; i++)
7041 replace_in_block (bbn->out_bb [i], bbn, NULL);
7043 nullify_basic_block (bbn);
7047 if (bb->out_count == 1) {
7048 bbn = bb->out_bb [0];
7050 /* conditional branches where true and false targets are the same can be also replaced with CEE_BR */
7051 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
7052 bb->last_ins->opcode = CEE_BR;
7053 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
7055 if (cfg->verbose_level > 2)
7056 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
7059 if (bb->region == bbn->region && bb->next_bb == bbn) {
7060 /* the block are in sequence anyway ... */
7062 /* branches to the following block can be removed */
7063 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
7064 bb->last_ins->opcode = CEE_NOP;
7066 if (cfg->verbose_level > 2)
7067 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
7070 if (bbn->in_count == 1) {
7072 if (bbn != cfg->bb_exit) {
7073 if (cfg->verbose_level > 2)
7074 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
7075 merge_basic_blocks (bb, bbn);
7079 //mono_print_bb_code (bb);
7084 } while (changed && (niterations > 0));
7091 /* we skip the entry block (exit is handled specially instead ) */
7092 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
7094 /* dont touch code inside exception clauses */
7095 if (bb->region != -1)
7098 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
7099 if (cfg->verbose_level > 2) {
7100 g_print ("nullify block triggered %d\n", bbn->block_num);
7102 bb->next_bb = bbn->next_bb;
7104 for (i = 0; i < bbn->out_count; i++)
7105 replace_in_block (bbn->out_bb [i], bbn, NULL);
7107 nullify_basic_block (bbn);
7113 if (bb->out_count == 1) {
7114 bbn = bb->out_bb [0];
7116 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
7117 bbn = bb->last_ins->inst_target_bb;
7118 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7119 bbn->code->inst_target_bb->region == bb->region) {
7121 if (cfg->verbose_level > 2)
7122 g_print ("in %s branch to branch triggered %d -> %d\n", cfg->method->name,
7123 bb->block_num, bbn->block_num);
7125 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
7126 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
7131 } else if (bb->out_count == 2) {
7132 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
7133 bbn = bb->last_ins->inst_true_bb;
7134 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7135 bbn->code->inst_target_bb->region == bb->region) {
7136 if (cfg->verbose_level > 2)
7137 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
7138 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
7141 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
7143 replace_in_block (bbn, bb, NULL);
7145 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
7146 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7148 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7154 bbn = bb->last_ins->inst_false_bb;
7155 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7156 bbn->code->inst_target_bb->region == bb->region) {
7157 if (cfg->verbose_level > 2)
7158 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
7159 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
7162 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
7164 replace_in_block (bbn, bb, NULL);
7166 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
7167 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7169 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7177 } while (changed && (niterations > 0));
7182 mono_compile_create_vars (MonoCompile *cfg)
7184 MonoMethodSignature *sig;
7185 MonoMethodHeader *header;
7188 header = ((MonoMethodNormal *)cfg->method)->header;
7190 sig = cfg->method->signature;
7192 if (!MONO_TYPE_IS_VOID (sig->ret)) {
7193 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
7194 cfg->ret->opcode = OP_RETARG;
7195 cfg->ret->inst_vtype = sig->ret;
7196 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
7198 if (cfg->verbose_level > 2)
7199 g_print ("creating vars\n");
7202 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
7204 for (i = 0; i < sig->param_count; ++i)
7205 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
7207 cfg->locals_start = cfg->num_varinfo;
7209 if (cfg->verbose_level > 2)
7210 g_print ("creating locals\n");
7211 for (i = 0; i < header->num_locals; ++i)
7212 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
7213 if (cfg->verbose_level > 2)
7214 g_print ("locals done\n");
7218 mono_print_code (MonoCompile *cfg)
7222 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7223 MonoInst *tree = bb->code;
7228 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
7230 for (; tree; tree = tree->next) {
7231 mono_print_tree (tree);
7236 bb->last_ins->next = NULL;
7240 extern const char * const mono_burg_rule_string [];
7243 emit_state (MonoCompile *cfg, MBState *state, int goal)
7246 int ern = mono_burg_rule (state, goal);
7247 const guint16 *nts = mono_burg_nts [ern];
7250 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
7254 // state->reg1 = state->reg2; /* chain rule */
7256 state->reg1 = mono_regstate_next_int (cfg->rs);
7257 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
7260 state->reg1 = mono_regstate_next_int (cfg->rs);
7261 state->reg2 = mono_regstate_next_int (cfg->rs);
7264 state->reg1 = mono_regstate_next_float (cfg->rs);
7271 mono_burg_kids (state, ern, kids);
7273 emit_state (cfg, kids [0], nts [0]);
7275 emit_state (cfg, kids [1], nts [1]);
7277 g_assert (!nts [3]);
7278 emit_state (cfg, kids [2], nts [2]);
7283 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
7284 if ((emit = mono_burg_func [ern]))
7285 emit (state, state->tree, cfg);
7288 #define DEBUG_SELECTION
7291 mini_select_instructions (MonoCompile *cfg)
7293 static const int reverse_map [] = {
7294 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
7295 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
7297 static const int reverse_fmap [] = {
7298 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
7299 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
7301 static const int reverse_lmap [] = {
7302 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
7303 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
7308 cfg->state_pool = mono_mempool_new ();
7309 cfg->rs = mono_regstate_new ();
7311 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7312 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode) &&
7313 bb->next_bb != bb->last_ins->inst_false_bb) {
7315 if (bb->next_bb == bb->last_ins->inst_true_bb) {
7316 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
7317 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
7318 bb->last_ins->inst_false_bb = tmp;
7320 if (bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
7321 bb->last_ins->opcode = reverse_map [bb->last_ins->opcode - CEE_BEQ];
7322 } else if (bb->last_ins->opcode >= OP_FBEQ && bb->last_ins->opcode <= OP_FBLT_UN) {
7323 bb->last_ins->opcode = reverse_fmap [bb->last_ins->opcode - OP_FBEQ];
7324 } else if (bb->last_ins->opcode >= OP_LBEQ && bb->last_ins->opcode <= OP_LBLT_UN) {
7325 bb->last_ins->opcode = reverse_lmap [bb->last_ins->opcode - OP_LBEQ];
7328 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
7329 inst->opcode = CEE_BR;
7330 inst->inst_target_bb = bb->last_ins->inst_false_bb;
7331 mono_bblock_add_inst (bb, inst);
7336 #ifdef DEBUG_SELECTION
7337 if (cfg->verbose_level >= 4) {
7338 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7339 MonoInst *tree = bb->code;
7340 g_print ("DUMP BLOCK %d:\n", bb->block_num);
7343 for (; tree; tree = tree->next) {
7344 mono_print_tree (tree);
7351 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7352 MonoInst *tree = bb->code, *next;
7358 bb->last_ins = NULL;
7361 mono_regstate_reset (cfg->rs);
7363 #ifdef DEBUG_SELECTION
7364 if (cfg->verbose_level >= 3)
7365 g_print ("LABEL BLOCK %d:\n", bb->block_num);
7367 for (; tree; tree = next) {
7369 #ifdef DEBUG_SELECTION
7370 if (cfg->verbose_level >= 3) {
7371 mono_print_tree (tree);
7376 if (!(mbstate = mono_burg_label (tree, cfg))) {
7377 g_warning ("unable to label tree %p", tree);
7378 mono_print_tree (tree);
7380 g_assert_not_reached ();
7382 emit_state (cfg, mbstate, MB_NTERM_stmt);
7384 bb->max_ireg = cfg->rs->next_vireg;
7385 bb->max_freg = cfg->rs->next_vfreg;
7388 bb->last_ins->next = NULL;
7390 mono_mempool_empty (cfg->state_pool);
7392 mono_mempool_destroy (cfg->state_pool);
7396 mono_codegen (MonoCompile *cfg)
7398 MonoJumpInfo *patch_info;
7400 int i, max_epilog_size;
7403 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7404 cfg->spill_count = 0;
7405 /* we reuse dfn here */
7406 /* bb->dfn = bb_count++; */
7407 mono_arch_local_regalloc (cfg, bb);
7410 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
7411 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
7413 code = mono_arch_emit_prolog (cfg);
7415 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7416 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
7418 cfg->code_len = code - cfg->native_code;
7419 cfg->prolog_end = cfg->code_len;
7421 mono_debug_open_method (cfg);
7423 /* emit code all basic blocks */
7424 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7425 bb->native_offset = cfg->code_len;
7426 mono_arch_output_basic_block (cfg, bb);
7428 cfg->bb_exit->native_offset = cfg->code_len;
7430 code = cfg->native_code + cfg->code_len;
7432 max_epilog_size = mono_arch_max_epilog_size (cfg);
7434 /* we always allocate code in cfg->domain->code_mp to increase locality */
7435 cfg->code_size = cfg->code_len + max_epilog_size;
7436 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
7438 mono_domain_lock (cfg->domain);
7439 code = mono_code_manager_reserve (cfg->domain->code_mp, cfg->code_size);
7440 mono_domain_unlock (cfg->domain);
7442 memcpy (code, cfg->native_code, cfg->code_len);
7443 g_free (cfg->native_code);
7444 cfg->native_code = code;
7445 code = cfg->native_code + cfg->code_len;
7447 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
7449 cfg->epilog_begin = cfg->code_len;
7451 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7452 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
7454 cfg->code_len = code - cfg->native_code;
7456 mono_arch_emit_epilog (cfg);
7458 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7459 switch (patch_info->type) {
7460 case MONO_PATCH_INFO_ABS: {
7461 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
7463 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
7464 if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) &&
7465 strstr (cfg->method->name, info->name))
7467 * This is an icall wrapper, and this is a call to the
7472 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
7473 patch_info->data.name = info->name;
7477 MonoVTable *vtable = mono_find_class_init_trampoline_by_addr (patch_info->data.target);
7479 patch_info->type = MONO_PATCH_INFO_CLASS_INIT;
7480 patch_info->data.klass = vtable->klass;
7485 case MONO_PATCH_INFO_SWITCH: {
7487 mono_domain_lock (cfg->domain);
7488 table = mono_code_manager_reserve (cfg->domain->code_mp, sizeof (gpointer) * patch_info->table_size);
7489 mono_domain_unlock (cfg->domain);
7491 patch_info->ip.i = patch_info->ip.label->inst_c0;
7492 for (i = 0; i < patch_info->table_size; i++) {
7493 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
7495 patch_info->data.target = table;
7504 if (cfg->verbose_level > 0)
7505 g_print ("Method %s emitted at %p to %p [%s]\n",
7506 mono_method_full_name (cfg->method, TRUE),
7507 cfg->native_code, cfg->native_code + cfg->code_len, cfg->domain->friendly_name);
7509 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info, cfg->run_cctors);
7511 mono_domain_lock (cfg->domain);
7512 mono_code_manager_commit (cfg->domain->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
7513 mono_domain_unlock (cfg->domain);
7515 mono_arch_flush_icache (cfg->native_code, cfg->code_len);
7517 mono_debug_close_method (cfg);
7521 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
7526 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
7527 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
7529 if (cp->opcode == OP_ICONST) {
7530 if (cfg->opt & MONO_OPT_CONSPROP) {
7531 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
7535 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
7536 if (cfg->opt & MONO_OPT_COPYPROP) {
7537 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
7543 arity = mono_burg_arity [tree->opcode];
7546 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
7547 if (cfg->opt & MONO_OPT_CFOLD)
7548 mono_constant_fold_inst (tree, NULL);
7549 /* The opcode may have changed */
7550 if (mono_burg_arity [tree->opcode] > 1) {
7551 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
7552 if (cfg->opt & MONO_OPT_CFOLD)
7553 mono_constant_fold_inst (tree, NULL);
7555 mono_constant_fold_inst (tree, NULL);
7561 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
7565 switch (tree->opcode) {
7575 if (tree->ssa_op == MONO_SSA_NOP) {
7576 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7593 case OP_VOIDCALL_REG:
7594 case OP_VOIDCALLVIRT:
7596 MonoCallInst *call = (MonoCallInst *)tree;
7597 MonoMethodSignature *sig = call->signature;
7598 int i, byref = FALSE;
7600 for (i = 0; i < sig->param_count; i++) {
7601 if (sig->params [i]->byref) {
7608 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7616 arity = mono_burg_arity [tree->opcode];
7622 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7625 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7626 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
7629 g_assert_not_reached ();
7634 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
7636 MonoInst *tree = bb->code;
7642 for (; tree; tree = tree->next) {
7644 mono_cprop_copy_values (cfg, tree, acp);
7646 mono_cprop_invalidate_values (tree, acp, acp_size);
7648 if (tree->ssa_op == MONO_SSA_STORE &&
7649 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
7650 MonoInst *i1 = tree->inst_i1;
7652 acp [tree->inst_i0->inst_c0] = NULL;
7654 for (i = 0; i < acp_size; i++) {
7655 if (acp [i] && acp [i]->opcode != OP_ICONST &&
7656 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
7661 if (i1->opcode == OP_ICONST) {
7662 acp [tree->inst_i0->inst_c0] = i1;
7663 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
7665 if (i1->ssa_op == MONO_SSA_LOAD &&
7666 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
7667 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
7668 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
7669 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
7674 if (tree->opcode == CEE_BEQ) {
7675 g_assert (tree->inst_i0->opcode == OP_COMPARE);
7676 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
7677 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
7679 tree->opcode = CEE_BR;
7680 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
7681 tree->inst_target_bb = tree->inst_true_bb;
7683 tree->inst_target_bb = tree->inst_false_bb;
7692 mono_local_cprop (MonoCompile *cfg)
7697 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
7699 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7700 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
7701 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
7706 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, int parts)
7708 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
7709 guint8 *ip = (guint8 *)header->code;
7712 int dfn = 0, i, code_size_ratio;
7714 mono_jit_stats.methods_compiled++;
7715 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
7716 mono_profiler_method_jit (method);
7718 cfg = g_new0 (MonoCompile, 1);
7719 cfg->method = method;
7720 cfg->mempool = mono_mempool_new ();
7722 cfg->prof_options = mono_profiler_get_events ();
7723 cfg->run_cctors = run_cctors;
7724 cfg->bb_hash = g_hash_table_new (NULL, NULL);
7725 cfg->domain = domain;
7726 cfg->verbose_level = mini_verbose;
7727 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
7728 ((MonoMethodNormal *)method)->header->max_stack);
7730 if (cfg->verbose_level > 2)
7731 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
7734 * create MonoInst* which represents arguments and local variables
7736 mono_compile_create_vars (cfg);
7738 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
7739 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7740 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
7741 mono_destroy_compile (cfg);
7745 mono_jit_stats.basic_blocks += cfg->num_bblocks;
7746 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
7748 if (cfg->num_varinfo > 2000) {
7750 * we disable some optimizations if there are too many variables
7751 * because JIT time may become too expensive. The actual number needs
7752 * to be tweaked and eventually the non-linear algorithms should be fixed.
7754 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
7755 cfg->disable_ssa = TRUE;
7757 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
7759 /* Depth-first ordering on basic blocks */
7760 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
7762 if (cfg->opt & MONO_OPT_BRANCH)
7763 optimize_branches (cfg);
7765 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
7766 if (cfg->num_bblocks != dfn + 1) {
7769 cfg->num_bblocks = dfn + 1;
7771 if (!header->clauses) {
7772 /* remove unreachable code, because the code in them may be
7773 * inconsistent (access to dead variables for example) */
7774 for (bb = cfg->bb_entry; bb;) {
7775 MonoBasicBlock *bbn = bb->next_bb;
7777 if (bbn && bbn->region == -1 && !bbn->dfn) {
7778 if (cfg->verbose_level > 1)
7779 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
7780 bb->next_bb = bbn->next_bb;
7781 nullify_basic_block (bbn);
7789 if (cfg->opt & MONO_OPT_LOOP) {
7790 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
7791 mono_compute_natural_loops (cfg);
7794 /* after method_to_ir */
7798 //#define DEBUGSSA "logic_run"
7799 #define DEBUGSSA_CLASS "Tests"
7802 if (!header->num_clauses && !cfg->disable_ssa) {
7803 mono_local_cprop (cfg);
7804 mono_ssa_compute (cfg);
7808 /* fixme: add all optimizations which requires SSA */
7809 if ((cfg->opt & (MONO_OPT_DEADCE)) || (cfg->opt & (MONO_OPT_ABCREM))) {
7810 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
7811 mono_local_cprop (cfg);
7812 mono_ssa_compute (cfg);
7814 if (cfg->verbose_level >= 2) {
7821 /* after SSA translation */
7825 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
7826 if (cfg->comp_done & MONO_COMP_SSA) {
7827 mono_ssa_cprop (cfg);
7829 mono_local_cprop (cfg);
7833 if (cfg->comp_done & MONO_COMP_SSA) {
7834 mono_ssa_deadce (cfg);
7836 //mono_ssa_strength_reduction (cfg);
7838 if (cfg->opt & MONO_OPT_ABCREM)
7840 mono_perform_abc_removal (cfg);
7843 mono_ssa_remove (cfg);
7845 if (cfg->opt & MONO_OPT_BRANCH)
7846 optimize_branches (cfg);
7849 /* after SSA removal */
7853 decompose_pass (cfg);
7855 if (cfg->opt & MONO_OPT_LINEARS) {
7858 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
7859 cfg->comp_done &= ~MONO_COMP_LIVENESS;
7860 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
7861 mono_analyze_liveness (cfg);
7863 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
7864 regs = mono_arch_get_global_int_regs (cfg);
7865 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
7869 //mono_print_code (cfg);
7873 /* variables are allocated after decompose, since decompose could create temps */
7874 mono_arch_allocate_vars (cfg);
7876 if (cfg->opt & MONO_OPT_CFOLD)
7877 mono_constant_fold (cfg);
7879 mini_select_instructions (cfg);
7882 if (cfg->verbose_level >= 2) {
7883 char *id = mono_method_full_name (cfg->method, FALSE);
7884 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
7888 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
7890 jinfo = g_new0 (MonoJitInfo, 1);
7891 jinfo->method = method;
7892 jinfo->code_start = cfg->native_code;
7893 jinfo->code_size = cfg->code_len;
7894 jinfo->used_regs = cfg->used_int_regs;
7895 jinfo->domain_neutral = (cfg->opt & MONO_OPT_SHARED) != 0;
7897 if (header->num_clauses) {
7900 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
7901 jinfo->num_clauses = header->num_clauses;
7902 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
7903 sizeof (MonoJitExceptionInfo) * header->num_clauses);
7905 for (i = 0; i < header->num_clauses; i++) {
7906 MonoExceptionClause *ec = &header->clauses [i];
7907 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
7908 MonoBasicBlock *tblock;
7910 ei->flags = ec->flags;
7912 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7913 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->token_or_filter);
7915 ei->data.filter = cfg->native_code + tblock->native_offset;
7917 ei->data.token = ec->token_or_filter;
7920 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
7922 ei->try_start = cfg->native_code + tblock->native_offset;
7923 g_assert (tblock->native_offset);
7924 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
7926 ei->try_end = cfg->native_code + tblock->native_offset;
7927 g_assert (tblock->native_offset);
7928 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
7930 ei->handler_start = cfg->native_code + tblock->native_offset;
7934 cfg->jit_info = jinfo;
7936 mono_jit_info_table_add (cfg->domain, jinfo);
7938 /* collect statistics */
7939 mono_jit_stats.allocated_code_size += cfg->code_len;
7940 code_size_ratio = cfg->code_len;
7941 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
7942 mono_jit_stats.biggest_method_size = code_size_ratio;
7943 mono_jit_stats.biggest_method = method;
7945 code_size_ratio = (code_size_ratio * 100) / ((MonoMethodNormal *)method)->header->code_size;
7946 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
7947 mono_jit_stats.max_code_size_ratio = code_size_ratio;
7948 mono_jit_stats.max_ratio_method = method;
7950 mono_jit_stats.native_code_size += cfg->code_len;
7952 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7953 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
7959 mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain)
7962 GHashTable *jit_code_hash;
7963 gpointer code = NULL;
7969 jit_code_hash = target_domain->jit_code_hash;
7971 #ifdef MONO_USE_AOT_COMPILER
7972 if (!mono_compile_aot && (opt & MONO_OPT_AOT)) {
7974 MonoDomain *domain = mono_domain_get ();
7976 mono_domain_lock (domain);
7978 mono_class_init (method->klass);
7979 if ((info = mono_aot_get_method (domain, method))) {
7980 g_hash_table_insert (domain->jit_code_hash, method, info);
7981 mono_domain_unlock (domain);
7982 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
7983 return info->code_start;
7986 mono_domain_unlock (domain);
7990 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
7991 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
7994 if (!method->addr) {
7995 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
7996 method->addr = mono_lookup_internal_call (method);
7998 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
7999 mono_lookup_pinvoke_call (method, NULL, NULL);
8001 #ifdef MONO_USE_EXC_TABLES
8002 if (mono_method_blittable (method)) {
8003 return method->addr;
8006 nm = mono_marshal_get_native_wrapper (method);
8007 return mono_compile_method (nm);
8009 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
8010 //mono_debug_add_wrapper (method, nm);
8011 #ifdef MONO_USE_EXC_TABLES
8014 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
8015 const char *name = method->name;
8018 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
8019 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
8020 /* FIXME: uhm, we need a wrapper to handle exceptions? */
8021 return (gpointer)mono_delegate_ctor;
8022 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
8023 nm = mono_marshal_get_delegate_invoke (method);
8024 return mono_jit_compile_method (nm);
8025 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
8026 nm = mono_marshal_get_delegate_begin_invoke (method);
8027 return mono_jit_compile_method (nm);
8028 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
8029 nm = mono_marshal_get_delegate_end_invoke (method);
8030 return mono_jit_compile_method (nm);
8036 cfg = mini_method_compile (method, opt, target_domain, TRUE, 0);
8038 mono_domain_lock (target_domain);
8040 /* Check if some other thread already did the job. In this case, we can
8041 discard the code this thread generated. */
8043 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
8044 /* We can't use a domain specific method in another domain */
8045 if ((target_domain == mono_domain_get ()) || info->domain_neutral) {
8046 code = info->code_start;
8047 // printf("Discarding code for method %s\n", method->name);
8052 g_hash_table_insert (jit_code_hash, method, cfg->jit_info);
8053 code = cfg->native_code;
8056 mono_destroy_compile (cfg);
8058 if (target_domain->jump_target_hash) {
8059 MonoJumpInfo patch_info;
8061 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
8063 patch_info.next = NULL;
8064 patch_info.ip.i = 0;
8065 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
8066 patch_info.data.method = method;
8067 g_hash_table_remove (target_domain->jump_target_hash, method);
8069 for (tmp = list; tmp; tmp = tmp->next)
8070 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info, TRUE);
8071 g_slist_free (list);
8074 mono_domain_unlock (target_domain);
8076 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
8081 mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt)
8083 /* FIXME: later copy the code from mono */
8084 MonoDomain *target_domain, *domain = mono_domain_get ();
8088 if (default_opt & MONO_OPT_SHARED)
8089 target_domain = mono_root_domain;
8091 target_domain = domain;
8093 mono_domain_lock (target_domain);
8095 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
8096 /* We can't use a domain specific method in another domain */
8097 if (! ((domain != target_domain) && !info->domain_neutral)) {
8098 mono_domain_unlock (target_domain);
8099 mono_jit_stats.methods_lookups++;
8100 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
8101 return info->code_start;
8105 mono_domain_unlock (target_domain);
8106 p = mono_jit_compile_method_inner (method, target_domain);
8111 mono_jit_compile_method (MonoMethod *method)
8113 return mono_jit_compile_method_with_opt (method, default_opt);
8117 mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method)
8119 MonoDomain *target_domain;
8122 if (default_opt & MONO_OPT_SHARED)
8123 target_domain = mono_root_domain;
8125 target_domain = domain;
8127 mono_domain_lock (target_domain);
8129 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
8130 /* We can't use a domain specific method in another domain */
8131 if (! ((domain != target_domain) && !info->domain_neutral)) {
8132 mono_domain_unlock (target_domain);
8133 mono_jit_stats.methods_lookups++;
8134 return info->code_start;
8138 mono_domain_unlock (target_domain);
8144 * mono_jit_runtime_invoke:
8145 * @method: the method to invoke
8146 * @obj: this pointer
8147 * @params: array of parameter values.
8148 * @exc: used to catch exceptions objects
8151 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
8154 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc);
8156 if (obj == NULL && !(method->flags & METHOD_ATTRIBUTE_STATIC) && !method->string_ctor && (method->wrapper_type == 0)) {
8157 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
8161 invoke = mono_marshal_get_runtime_invoke (method);
8162 runtime_invoke = mono_jit_compile_method (invoke);
8163 return runtime_invoke (obj, params, exc);
8166 #ifdef PLATFORM_WIN32
8167 #define GET_CONTEXT \
8168 struct sigcontext *ctx = (struct sigcontext*)_dummy;
8171 #define GET_CONTEXT \
8172 void *ctx = context;
8173 #elif defined(__ppc__) || defined (__powerpc__) || defined (__s390__)
8174 #define GET_CONTEXT \
8175 void *ctx = context;
8177 #define GET_CONTEXT \
8178 void **_p = (void **)&_dummy; \
8179 struct sigcontext *ctx = (struct sigcontext *)++_p;
8183 #ifdef MONO_ARCH_USE_SIGACTION
8184 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, siginfo_t *info, void *context)
8186 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy)
8190 SIG_HANDLER_SIGNATURE (sigfpe_signal_handler)
8195 exc = mono_get_exception_divide_by_zero ();
8197 mono_arch_handle_exception (ctx, exc, FALSE);
8201 SIG_HANDLER_SIGNATURE (sigill_signal_handler)
8205 exc = mono_get_exception_execution_engine ("SIGILL");
8207 mono_arch_handle_exception (ctx, exc, FALSE);
8210 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8213 sigsegv_signal_handler (int _dummy, siginfo_t *info, void *context)
8216 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
8217 struct sigcontext *ctx = (struct sigcontext *)&(((ucontext_t*)context)->uc_mcontext);
8219 /* Can't allocate memory using Boehm GC on altstack */
8220 if (jit_tls->stack_size &&
8221 ((guint8*)info->si_addr >= (guint8*)jit_tls->end_of_stack - jit_tls->stack_size) &&
8222 ((guint8*)info->si_addr < (guint8*)jit_tls->end_of_stack))
8223 exc = mono_domain_get ()->stack_overflow_ex;
8225 exc = mono_domain_get ()->null_reference_ex;
8227 mono_arch_handle_exception (ctx, exc, FALSE);
8233 SIG_HANDLER_SIGNATURE (sigsegv_signal_handler)
8237 mono_arch_handle_exception (ctx, NULL, FALSE);
8243 SIG_HANDLER_SIGNATURE (sigusr1_signal_handler)
8246 gboolean stopRequested = FALSE;
8250 thread = mono_thread_current ();
8252 mono_monitor_try_enter ((MonoObject *)thread, INFINITE);
8253 stopRequested = (thread->state & ThreadState_StopRequested) != 0;
8254 mono_monitor_exit ((MonoObject *)thread);
8256 thread->abort_exc = mono_get_exception_thread_abort ();
8258 if (stopRequested) {
8259 /* Don't throw the exception, just abort the thread */
8260 mono_thread_abort ((MonoObject *) thread->abort_exc);
8263 mono_arch_handle_exception (ctx, thread->abort_exc, FALSE);
8268 SIG_HANDLER_SIGNATURE (sigquit_signal_handler)
8273 exc = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
8275 mono_arch_handle_exception (ctx, exc, FALSE);
8279 SIG_HANDLER_SIGNATURE (sigint_signal_handler)
8284 exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
8286 mono_arch_handle_exception (ctx, exc, FALSE);
8289 #ifndef PLATFORM_WIN32
8291 add_signal_handler (int signo, gpointer handler)
8293 struct sigaction sa;
8295 #ifdef MONO_ARCH_USE_SIGACTION
8296 sa.sa_sigaction = handler;
8297 sigemptyset (&sa.sa_mask);
8298 sa.sa_flags = SA_SIGINFO;
8300 sa.sa_handler = handler;
8301 sigemptyset (&sa.sa_mask);
8304 g_assert (sigaction (signo, &sa, NULL) != -1);
8309 mono_runtime_install_handlers (void)
8311 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8312 struct sigaction sa;
8315 #ifdef PLATFORM_WIN32
8317 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
8318 win32_seh_set_handler(SIGILL, sigill_signal_handler);
8319 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
8320 if (getenv ("MONO_DEBUG"))
8321 win32_seh_set_handler(SIGINT, sigint_signal_handler);
8322 #else /* !PLATFORM_WIN32 */
8324 /* libpthreads has its own implementation of sigaction(),
8325 * but it seems to work well with our current exception
8326 * handlers. If not we must call syscall directly instead
8329 if (getenv ("MONO_DEBUG")) {
8330 add_signal_handler (SIGINT, sigint_signal_handler);
8333 add_signal_handler (SIGFPE, sigfpe_signal_handler);
8334 add_signal_handler (SIGQUIT, sigquit_signal_handler);
8335 add_signal_handler (SIGILL, sigill_signal_handler);
8336 add_signal_handler (SIGBUS, sigsegv_signal_handler);
8337 add_signal_handler (mono_thread_get_abort_signal (), sigusr1_signal_handler);
8340 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8341 sa.sa_sigaction = sigsegv_signal_handler;
8342 sigemptyset (&sa.sa_mask);
8343 sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
8344 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
8346 add_signal_handler (SIGSEGV, sigsegv_signal_handler);
8349 #endif /* PLATFORM_WIN32 */
8352 /* mono_jit_create_remoting_trampoline:
8353 * @method: pointer to the method info
8355 * Creates a trampoline which calls the remoting functions. This
8356 * is used in the vtable of transparent proxies.
8358 * Returns: a pointer to the newly created code
8361 mono_jit_create_remoting_trampoline (MonoMethod *method)
8364 guint8 *addr = NULL;
8366 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
8367 (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
8368 nm = mono_marshal_get_remoting_invoke (method);
8369 addr = mono_compile_method (nm);
8371 addr = mono_compile_method (method);
8377 mini_init (const char *filename)
8381 mono_arch_cpu_init ();
8383 if (!g_thread_supported ())
8384 g_thread_init (NULL);
8386 mono_jit_tls_id = TlsAlloc ();
8387 setup_jit_tls_data ((gpointer)-1, mono_thread_abort);
8389 InitializeCriticalSection (&trampoline_hash_mutex);
8393 if (default_opt & MONO_OPT_AOT)
8396 mono_runtime_install_handlers ();
8397 mono_threads_install_cleanup (mini_thread_cleanup);
8399 #define JIT_TRAMPOLINES_WORK
8400 #ifdef JIT_TRAMPOLINES_WORK
8401 mono_install_compile_method (mono_jit_compile_method);
8402 mono_install_trampoline (mono_arch_create_jit_trampoline);
8403 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
8405 #define JIT_INVOKE_WORKS
8406 #ifdef JIT_INVOKE_WORKS
8407 mono_install_runtime_invoke (mono_jit_runtime_invoke);
8408 mono_install_handler (mono_arch_get_throw_exception ());
8410 mono_install_stack_walk (mono_jit_walk_stack);
8412 domain = mono_init (filename);
8415 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
8416 ves_icall_get_frame_info);
8417 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
8418 ves_icall_get_trace);
8419 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
8420 mono_runtime_install_handlers);
8423 create_helper_signature ();
8425 #define JIT_CALLS_WORK
8426 #ifdef JIT_CALLS_WORK
8427 /* Needs to be called here since register_jit_icall depends on it */
8428 mono_marshal_init ();
8430 mono_arch_register_lowlevel_calls ();
8431 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
8432 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
8433 mono_register_jit_icall (mono_trace_enter_method, "mono_trace_enter_method", NULL, TRUE);
8434 mono_register_jit_icall (mono_trace_leave_method, "mono_trace_leave_method", NULL, TRUE);
8436 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
8437 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
8439 /* fixme: we cant hanlde vararg methods this way, because the signature is not constant */
8440 //mono_register_jit_icall (ves_array_element_address, "ves_array_element_address", NULL);
8441 //mono_register_jit_icall (mono_array_new_va, "mono_array_new_va", NULL);
8443 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
8444 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
8445 helper_sig_void_ptr, TRUE);
8446 mono_register_jit_icall (mono_thread_get_pending_exception, "mono_thread_get_pending_exception", helper_sig_obj_void, FALSE);
8449 * NOTE, NOTE, NOTE, NOTE:
8450 * when adding emulation for some opcodes, remember to also add a dummy
8451 * rule to the burg files, because we need the arity information to be correct.
8453 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult, TRUE);
8454 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un, FALSE);
8455 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf, FALSE);
8456 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv, FALSE);
8457 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un, FALSE);
8458 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem, FALSE);
8459 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un, FALSE);
8461 #ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
8462 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl, TRUE);
8463 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr, TRUE);
8464 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un, TRUE);
8467 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8, FALSE);
8468 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4, FALSE);
8469 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8, FALSE);
8470 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8, FALSE);
8472 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
8473 mono_register_opcode_emulation (OP_FCONV_TO_I8, "__emul_fconv_to_i8", helper_sig_long_double, mono_fconv_i8, FALSE);
8475 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
8476 mono_register_opcode_emulation (CEE_CONV_R_UN, "__emul_conv_r_un", helper_sig_double_int, mono_conv_to_r8_un, FALSE);
8478 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
8479 mono_register_opcode_emulation (OP_LCONV_TO_R8, "__emul_lconv_to_r8", helper_sig_double_long, mono_lconv_to_r8, FALSE);
8481 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
8482 mono_register_opcode_emulation (OP_LCONV_TO_R4, "__emul_lconv_to_r4", helper_sig_float_long, mono_lconv_to_r4, FALSE);
8484 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
8485 mono_register_opcode_emulation (OP_LCONV_TO_R_UN, "__emul_lconv_to_r8_un", helper_sig_double_long, mono_lconv_to_r8_un, FALSE);
8487 #ifdef MONO_ARCH_EMULATE_FREM
8488 mono_register_opcode_emulation (OP_FREM, "__emul_frem", helper_sig_double_double_double, fmod, FALSE);
8491 #if SIZEOF_VOID_P == 4
8492 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4, TRUE);
8495 #warning "fixme: add opcode emulation"
8499 /* other jit icalls */
8500 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
8501 helper_sig_ptr_ptr_ptr, FALSE);
8502 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
8503 mono_register_jit_icall (mono_get_special_static_data, "mono_get_special_static_data", helper_sig_ptr_int, FALSE);
8504 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
8505 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
8506 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
8507 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
8508 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
8509 mono_register_jit_icall (helper_stelem_ref_check, "helper_stelem_ref_check", helper_sig_stelem_ref_check, FALSE);
8510 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
8511 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
8512 mono_register_jit_icall (mono_object_new_fast, "mono_object_new_fast", helper_sig_object_new_specific, FALSE);
8513 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
8514 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
8515 mono_register_jit_icall (mono_string_to_utf16, "mono_string_to_utf16", helper_sig_ptr_obj, FALSE);
8516 mono_register_jit_icall (mono_string_from_utf16, "mono_string_from_utf16", helper_sig_obj_ptr, FALSE);
8517 mono_register_jit_icall (mono_string_new_wrapper, "mono_string_new_wrapper", helper_sig_obj_ptr, FALSE);
8518 mono_register_jit_icall (mono_string_to_utf8, "mono_string_to_utf8", helper_sig_ptr_obj, FALSE);
8519 mono_register_jit_icall (mono_string_to_bstr, "mono_string_to_bstr", helper_sig_ptr_obj, FALSE);
8520 mono_register_jit_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", helper_sig_ptr_obj, FALSE);
8521 mono_register_jit_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", helper_sig_ptr_obj, FALSE);
8522 mono_register_jit_icall (mono_string_builder_to_utf16, "mono_string_builder_to_utf16", helper_sig_ptr_obj, FALSE);
8523 mono_register_jit_icall (mono_array_to_savearray, "mono_array_to_savearray", helper_sig_ptr_obj, FALSE);
8524 mono_register_jit_icall (mono_array_to_lparray, "mono_array_to_lparray", helper_sig_ptr_obj, FALSE);
8525 mono_register_jit_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", helper_sig_ptr_obj, FALSE);
8526 mono_register_jit_icall (mono_marshal_string_array, "mono_marshal_string_array", helper_sig_ptr_obj, FALSE);
8527 mono_register_jit_icall (mono_marshal_string_array_to_unicode, "mono_marshal_string_array_to_unicode", helper_sig_ptr_obj, FALSE);
8528 mono_register_jit_icall (mono_marshal_asany, "mono_marshal_asany", helper_sig_ptr_obj_int, FALSE);
8529 mono_register_jit_icall (mono_marshal_free_asany, "mono_marshal_free_asany", helper_sig_void_obj_ptr_int, FALSE);
8530 mono_register_jit_icall (mono_string_utf8_to_builder, "mono_string_utf8_to_builder", helper_sig_void_ptr_ptr, FALSE);
8531 mono_register_jit_icall (mono_string_utf16_to_builder, "mono_string_utf16_to_builder", helper_sig_void_ptr_ptr, FALSE);
8532 mono_register_jit_icall (mono_marshal_free_array, "mono_marshal_free_array", helper_sig_void_ptr_ptr, FALSE);
8533 mono_register_jit_icall (mono_string_to_byvalstr, "mono_string_to_byvalstr", helper_sig_void_ptr_ptr_ptr, FALSE);
8534 mono_register_jit_icall (mono_string_to_byvalwstr, "mono_string_to_byvalwstr", helper_sig_void_ptr_ptr_ptr, FALSE);
8535 mono_register_jit_icall (g_free, "g_free", helper_sig_void_ptr, FALSE);
8536 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
8537 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
8538 mono_register_jit_icall (mono_ldftn_nosync, "mono_ldftn_nosync", helper_sig_compile, FALSE);
8539 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
8542 #define JIT_RUNTIME_WORKS
8543 #ifdef JIT_RUNTIME_WORKS
8544 mono_runtime_install_cleanup ((MonoDomainFunc)mini_cleanup);
8545 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
8548 mono_thread_attach (domain);
8552 MonoJitStats mono_jit_stats = {0};
8555 print_jit_stats (void)
8557 if (mono_jit_stats.enabled) {
8558 g_print ("Mono Jit statistics\n");
8559 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
8560 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
8561 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
8562 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
8563 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
8564 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
8565 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
8566 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
8567 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
8568 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
8569 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
8570 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
8571 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
8572 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
8573 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
8574 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
8575 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
8576 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
8578 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
8579 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
8580 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
8581 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
8582 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
8584 g_print ("\nGeneric instances: %ld\n", mono_stats.generic_instance_count);
8585 g_print ("Inflated methods: %ld\n", mono_stats.inflated_method_count);
8586 g_print ("Inflated types: %ld\n", mono_stats.inflated_type_count);
8587 g_print ("Generics metadata size: %ld\n", mono_stats.generics_metadata_size);
8592 mini_cleanup (MonoDomain *domain)
8595 * mono_runtime_cleanup() and mono_domain_finalize () need to
8596 * be called early since they need the execution engine still
8597 * fully working (mono_domain_finalize may invoke managed finalizers
8598 * and mono_runtime_cleanup will wait for other threads to finish).
8600 mono_domain_finalize (domain, 2000);
8602 mono_runtime_cleanup (domain);
8604 mono_profiler_shutdown ();
8606 mono_debug_cleanup ();
8608 #ifdef PLATFORM_WIN32
8609 win32_seh_cleanup();
8612 mono_domain_free (domain, TRUE);
8618 mono_set_defaults (int verbose_level, guint32 opts)
8620 mini_verbose = verbose_level;
8625 mono_precompile_assembly (MonoAssembly *ass, void *user_data)
8627 MonoImage *image = ass->image;
8631 if (mini_verbose > 0)
8632 printf ("PRECOMPILE: %s.\n", ass->image->name);
8634 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
8635 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
8636 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
8638 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
8642 if (mini_verbose > 1) {
8643 char * desc = mono_method_full_name (method, TRUE);
8644 g_print ("Compiling %d %s\n", count, desc);
8647 mono_compile_method (method);
8651 void mono_precompile_assemblies ()
8653 mono_assembly_foreach ((GFunc)mono_precompile_assembly, NULL);