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>
50 #include "jit-icalls.c"
52 #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))
54 #define MONO_CHECK_THIS(ins) (cfg->method->signature->hasthis && (ins)->ssa_op == MONO_SSA_LOAD && (ins)->inst_left->inst_c0 == 0)
56 gboolean mono_arch_handle_exception (struct sigcontext *ctx, gpointer obj, gboolean test_only);
57 gboolean mono_arch_print_tree(MonoInst *tree, int arity);
58 static gpointer mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt);
59 static gpointer mono_jit_compile_method (MonoMethod *method);
60 static gpointer mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method);
62 static void handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src,
63 const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native);
65 static void dec_foreach (MonoInst *tree, MonoCompile *cfg);
67 static int mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
68 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
69 guint inline_offset, gboolean is_virtual_call);
71 extern guint8 mono_burg_arity [];
72 /* helper methods signature */
73 static MonoMethodSignature *helper_sig_long_long_long = NULL;
74 static MonoMethodSignature *helper_sig_long_long_int = NULL;
75 static MonoMethodSignature *helper_sig_newarr = NULL;
76 static MonoMethodSignature *helper_sig_newarr_specific = NULL;
77 static MonoMethodSignature *helper_sig_ldstr = NULL;
78 static MonoMethodSignature *helper_sig_domain_get = NULL;
79 static MonoMethodSignature *helper_sig_object_new = NULL;
80 static MonoMethodSignature *helper_sig_object_new_specific = NULL;
81 static MonoMethodSignature *helper_sig_compile = NULL;
82 static MonoMethodSignature *helper_sig_compile_virt = NULL;
83 static MonoMethodSignature *helper_sig_obj_ptr = NULL;
84 static MonoMethodSignature *helper_sig_obj_void = NULL;
85 static MonoMethodSignature *helper_sig_ptr_void = NULL;
86 static MonoMethodSignature *helper_sig_void_ptr = NULL;
87 static MonoMethodSignature *helper_sig_void_obj = NULL;
88 static MonoMethodSignature *helper_sig_void_ptr_ptr = NULL;
89 static MonoMethodSignature *helper_sig_void_ptr_ptr_ptr = NULL;
90 static MonoMethodSignature *helper_sig_ptr_ptr_ptr = NULL;
91 static MonoMethodSignature *helper_sig_ptr_obj = NULL;
92 static MonoMethodSignature *helper_sig_ptr_int = NULL;
93 static MonoMethodSignature *helper_sig_initobj = NULL;
94 static MonoMethodSignature *helper_sig_memcpy = NULL;
95 static MonoMethodSignature *helper_sig_memset = NULL;
96 static MonoMethodSignature *helper_sig_ulong_double = NULL;
97 static MonoMethodSignature *helper_sig_long_double = NULL;
98 static MonoMethodSignature *helper_sig_double_long = NULL;
99 static MonoMethodSignature *helper_sig_double_int = NULL;
100 static MonoMethodSignature *helper_sig_float_long = NULL;
101 static MonoMethodSignature *helper_sig_double_double_double = NULL;
102 static MonoMethodSignature *helper_sig_uint_double = NULL;
103 static MonoMethodSignature *helper_sig_int_double = NULL;
104 static MonoMethodSignature *helper_sig_stelem_ref = NULL;
105 static MonoMethodSignature *helper_sig_stelem_ref_check = NULL;
106 static MonoMethodSignature *helper_sig_class_init_trampoline = NULL;
108 static guint32 default_opt = MONO_OPT_PEEPHOLE;
110 guint32 mono_jit_tls_id = -1;
111 MonoTraceSpec *mono_jit_trace_calls = NULL;
112 gboolean mono_break_on_exc = FALSE;
113 gboolean mono_compile_aot = FALSE;
115 static int mini_verbose = 0;
117 static CRITICAL_SECTION trampoline_hash_mutex;
119 static GHashTable *class_init_hash_addr = NULL;
121 static GHashTable *jump_trampoline_hash = NULL;
124 mono_running_on_valgrind (void)
126 #ifdef HAVE_VALGRIND_MEMCHECK_H
127 if (RUNNING_ON_VALGRIND)
136 #ifdef MONO_USE_EXC_TABLES
138 mono_type_blittable (MonoType *type)
158 case MONO_TYPE_VALUETYPE:
159 case MONO_TYPE_CLASS:
160 return type->data.klass->blittable;
170 mono_method_blittable (MonoMethod *method)
172 MonoMethodSignature *sig;
178 if (!mono_arch_has_unwind_info (method->addr)) {
182 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
185 sig = method->signature;
187 if (!mono_type_blittable (sig->ret))
190 for (i = 0; i < sig->param_count; i++)
191 if (!mono_type_blittable (sig->params [i]))
194 if (mono_method_has_marshal_info (method))
197 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && ((MonoMethodPInvoke*)method)->piflags & PINVOKE_ATTRIBUTE_SUPPORTS_LAST_ERROR)
205 G_GNUC_UNUSED static void
206 print_method_from_ip (void *ip)
211 MonoDomain *domain = mono_domain_get ();
213 ji = mono_jit_info_table_find (domain, ip);
215 g_print ("No method at %p\n", ip);
218 method = mono_method_full_name (ji->method, TRUE);
219 source = mono_debug_source_location_from_address (ji->method, (int) ip, NULL, domain);
221 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);
224 g_print ("%s\n", source);
232 * mono_method_same_domain:
234 * Determine whenever two compiled methods are in the same domain, thus
235 * the address of the callee can be embedded in the caller.
237 gboolean mono_method_same_domain (MonoJitInfo *caller, MonoJitInfo *callee)
239 if (!caller || !callee)
243 * If the call was made from domain-neutral to domain-specific
244 * code, we can't patch the call site.
246 if (caller->domain_neutral && !callee->domain_neutral)
249 if ((caller->method->klass == mono_defaults.appdomain_class) &&
250 (strstr (caller->method->name, "InvokeInDomain"))) {
251 /* The InvokeInDomain methods change the current appdomain */
259 mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token)
261 MonoJumpInfoToken *res = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoToken));
268 #define MONO_INIT_VARINFO(vi,id) do { \
269 (vi)->range.first_use.pos.bid = 0xffff; \
275 * Basic blocks have two numeric identifiers:
276 * dfn: Depth First Number
277 * block_num: unique ID assigned at bblock creation
279 #define NEW_BBLOCK(cfg) (mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)))
280 #define ADD_BBLOCK(cfg,bbhash,b) do { \
281 g_hash_table_insert (bbhash, (b)->cil_code, (b)); \
282 (b)->block_num = cfg->num_bblocks++; \
283 (b)->real_offset = real_offset; \
286 #define GET_BBLOCK(cfg,bbhash,tblock,ip) do { \
287 (tblock) = g_hash_table_lookup (bbhash, (ip)); \
289 if ((ip) >= end || (ip) < header->code) goto unverified; \
290 (tblock) = NEW_BBLOCK (cfg); \
291 (tblock)->cil_code = (ip); \
292 ADD_BBLOCK (cfg, (bbhash), (tblock)); \
296 #define CHECK_BBLOCK(target,ip,tblock) do { \
297 if ((target) < (ip) && !(tblock)->code) { \
298 bb_recheck = g_list_prepend (bb_recheck, (tblock)); \
299 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); \
303 #define NEW_ICONST(cfg,dest,val) do { \
304 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
305 (dest)->opcode = OP_ICONST; \
306 (dest)->inst_c0 = (val); \
307 (dest)->type = STACK_I4; \
310 /* FIXME: have a different definition of NEW_PCONST for 64 bit systems */
311 #define NEW_PCONST(cfg,dest,val) do { \
312 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
313 (dest)->opcode = OP_ICONST; \
314 (dest)->inst_p0 = (val); \
315 (dest)->type = STACK_PTR; \
318 #define NEW_CLASSCONST(cfg,dest,val) do { \
319 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
320 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
321 (dest)->inst_p0 = (val); \
322 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_CLASS; \
323 (dest)->type = STACK_PTR; \
326 #define NEW_IMAGECONST(cfg,dest,val) do { \
327 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
328 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
329 (dest)->inst_p0 = (val); \
330 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_IMAGE; \
331 (dest)->type = STACK_PTR; \
334 #define NEW_FIELDCONST(cfg,dest,field) do { \
335 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
336 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
337 (dest)->inst_p0 = (field); \
338 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_FIELD; \
339 (dest)->type = STACK_PTR; \
342 #define NEW_METHODCONST(cfg,dest,val) do { \
343 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
344 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
345 (dest)->inst_p0 = (val); \
346 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_METHODCONST; \
347 (dest)->type = STACK_PTR; \
350 #define NEW_VTABLECONST(cfg,dest,vtable) do { \
351 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
352 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
353 (dest)->inst_p0 = mono_compile_aot ? (gpointer)((vtable)->klass) : (vtable); \
354 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_VTABLE; \
355 (dest)->type = STACK_PTR; \
358 #define NEW_SFLDACONST(cfg,dest,field) do { \
359 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
360 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
361 (dest)->inst_p0 = (field); \
362 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_SFLDA; \
363 (dest)->type = STACK_PTR; \
366 #define NEW_LDSTRCONST(cfg,dest,image,token) do { \
367 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
368 (dest)->opcode = OP_AOTCONST; \
369 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
370 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_LDSTR; \
371 (dest)->type = STACK_OBJ; \
374 #define NEW_TYPE_FROM_HANDLE_CONST(cfg,dest,image,token) do { \
375 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
376 (dest)->opcode = OP_AOTCONST; \
377 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
378 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_TYPE_FROM_HANDLE; \
379 (dest)->type = STACK_OBJ; \
382 #define NEW_LDTOKENCONST(cfg,dest,image,token) do { \
383 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
384 (dest)->opcode = OP_AOTCONST; \
385 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
386 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_LDTOKEN; \
387 (dest)->type = STACK_PTR; \
390 #define NEW_DOMAINCONST(cfg,dest) do { \
391 if (cfg->opt & MONO_OPT_SHARED) { \
392 NEW_TEMPLOAD (cfg, dest, mono_get_domainvar (cfg)->inst_c0); \
394 NEW_PCONST (cfg, dest, (cfg)->domain); \
398 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
400 #define NEW_ARGLOAD(cfg,dest,num) do { \
401 if (arg_array [(num)]->opcode == OP_ICONST) (dest) = arg_array [(num)]; else { \
402 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
403 (dest)->ssa_op = MONO_SSA_LOAD; \
404 (dest)->inst_i0 = arg_array [(num)]; \
405 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
406 type_to_eval_stack_type (param_types [(num)], (dest)); \
407 (dest)->klass = (dest)->inst_i0->klass; \
410 #define NEW_LOCLOAD(cfg,dest,num) do { \
411 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
412 (dest)->ssa_op = MONO_SSA_LOAD; \
413 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
414 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
415 type_to_eval_stack_type (header->locals [(num)], (dest)); \
416 (dest)->klass = (dest)->inst_i0->klass; \
419 #define NEW_LOCLOADA(cfg,dest,num) do { \
420 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
421 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
422 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
423 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
424 (dest)->opcode = OP_LDADDR; \
425 (dest)->type = STACK_MP; \
426 (dest)->klass = (dest)->inst_i0->klass; \
427 if (!MONO_TYPE_ISSTRUCT (header->locals [(num)])) \
428 (cfg)->disable_ssa = TRUE; \
431 #define NEW_RETLOADA(cfg,dest) do { \
432 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
433 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
434 (dest)->inst_i0 = (cfg)->ret; \
435 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
436 (dest)->opcode = CEE_LDIND_I; \
437 (dest)->type = STACK_MP; \
438 (dest)->klass = (dest)->inst_i0->klass; \
439 (cfg)->disable_ssa = TRUE; \
442 #define NEW_ARGLOADA(cfg,dest,num) do { \
443 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
444 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
445 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
446 (dest)->inst_i0 = arg_array [(num)]; \
447 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
448 (dest)->opcode = OP_LDADDR; \
449 (dest)->type = STACK_MP; \
450 (dest)->klass = (dest)->inst_i0->klass; \
451 (cfg)->disable_ssa = TRUE; \
454 #define NEW_TEMPLOAD(cfg,dest,num) do { \
455 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
456 (dest)->ssa_op = MONO_SSA_LOAD; \
457 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
458 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
459 type_to_eval_stack_type ((dest)->inst_i0->inst_vtype, (dest)); \
460 (dest)->klass = (dest)->inst_i0->klass; \
463 #define NEW_TEMPLOADA(cfg,dest,num) do { \
464 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
465 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
466 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
467 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
468 (dest)->opcode = OP_LDADDR; \
469 (dest)->type = STACK_MP; \
470 (dest)->klass = (dest)->inst_i0->klass; \
471 if (!MONO_TYPE_ISSTRUCT (cfg->varinfo [(num)]->inst_vtype)) \
472 (cfg)->disable_ssa = TRUE; \
476 #define NEW_INDLOAD(cfg,dest,addr,vtype) do { \
477 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
478 (dest)->inst_left = addr; \
479 (dest)->opcode = mono_type_to_ldind (vtype); \
480 type_to_eval_stack_type (vtype, (dest)); \
481 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
484 #define NEW_INDSTORE(cfg,dest,addr,value,vtype) do { \
485 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
486 (dest)->inst_i0 = addr; \
487 (dest)->opcode = mono_type_to_stind (vtype); \
488 (dest)->inst_i1 = (value); \
489 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
492 #define NEW_TEMPSTORE(cfg,dest,num,inst) do { \
493 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
494 (dest)->ssa_op = MONO_SSA_STORE; \
495 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
496 (dest)->opcode = mono_type_to_stind ((dest)->inst_i0->inst_vtype); \
497 (dest)->inst_i1 = (inst); \
498 (dest)->klass = (dest)->inst_i0->klass; \
501 #define NEW_LOCSTORE(cfg,dest,num,inst) do { \
502 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
503 (dest)->opcode = mono_type_to_stind (header->locals [(num)]); \
504 (dest)->ssa_op = MONO_SSA_STORE; \
505 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
506 (dest)->inst_i1 = (inst); \
507 (dest)->klass = (dest)->inst_i0->klass; \
510 #define NEW_ARGSTORE(cfg,dest,num,inst) do { \
511 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
512 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
513 (dest)->opcode = mono_type_to_stind (param_types [(num)]); \
514 (dest)->ssa_op = MONO_SSA_STORE; \
515 (dest)->inst_i0 = arg_array [(num)]; \
516 (dest)->inst_i1 = (inst); \
517 (dest)->klass = (dest)->inst_i0->klass; \
520 #define ADD_BINOP(op) do { \
521 MONO_INST_NEW (cfg, ins, (op)); \
522 ins->cil_code = ip; \
524 ins->inst_i0 = sp [0]; \
525 ins->inst_i1 = sp [1]; \
527 type_from_op (ins); \
531 #define ADD_UNOP(op) do { \
532 MONO_INST_NEW (cfg, ins, (op)); \
533 ins->cil_code = ip; \
535 ins->inst_i0 = sp [0]; \
537 type_from_op (ins); \
541 #define ADD_BINCOND(next_block) do { \
543 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
545 cmp->inst_i0 = sp [0]; \
546 cmp->inst_i1 = sp [1]; \
547 cmp->cil_code = ins->cil_code; \
548 type_from_op (cmp); \
550 ins->inst_i0 = cmp; \
551 MONO_ADD_INS (bblock, ins); \
552 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
553 GET_BBLOCK (cfg, bbhash, tblock, target); \
554 link_bblock (cfg, bblock, tblock); \
555 ins->inst_true_bb = tblock; \
556 CHECK_BBLOCK (target, ip, tblock); \
557 if ((next_block)) { \
558 link_bblock (cfg, bblock, (next_block)); \
559 ins->inst_false_bb = (next_block); \
560 start_new_bblock = 1; \
562 GET_BBLOCK (cfg, bbhash, tblock, ip); \
563 link_bblock (cfg, bblock, tblock); \
564 ins->inst_false_bb = tblock; \
565 start_new_bblock = 2; \
569 /* FIXME: handle float, long ... */
570 #define ADD_UNCOND(istrue) do { \
572 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
574 cmp->inst_i0 = sp [0]; \
575 switch (cmp->inst_i0->type) { \
577 cmp->inst_i1 = zero_int64; break; \
579 cmp->inst_i1 = zero_r8; break; \
582 cmp->inst_i1 = zero_ptr; break; \
584 cmp->inst_i1 = zero_obj; break; \
586 cmp->inst_i1 = zero_int32; \
588 cmp->cil_code = ins->cil_code; \
589 type_from_op (cmp); \
591 ins->inst_i0 = cmp; \
592 ins->opcode = (istrue)? CEE_BNE_UN: CEE_BEQ; \
593 MONO_ADD_INS (bblock, ins); \
594 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
595 GET_BBLOCK (cfg, bbhash, tblock, target); \
596 link_bblock (cfg, bblock, tblock); \
597 ins->inst_true_bb = tblock; \
598 CHECK_BBLOCK (target, ip, tblock); \
599 GET_BBLOCK (cfg, bbhash, tblock, ip); \
600 link_bblock (cfg, bblock, tblock); \
601 ins->inst_false_bb = tblock; \
602 start_new_bblock = 2; \
605 #define NEW_LDELEMA(cfg,dest,sp,k) do { \
606 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
607 (dest)->opcode = CEE_LDELEMA; \
608 (dest)->inst_left = (sp) [0]; \
609 (dest)->inst_right = (sp) [1]; \
610 (dest)->type = STACK_MP; \
611 (dest)->klass = (k); \
614 #define NEW_GROUP(cfg,dest,el1,el2) do { \
615 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
616 (dest)->opcode = OP_GROUP; \
617 (dest)->inst_left = (el1); \
618 (dest)->inst_right = (el2); \
623 compare_bblock (gconstpointer a, gconstpointer b)
625 const MonoBasicBlock *b1 = a;
626 const MonoBasicBlock *b2 = b;
628 return b2->cil_code - b1->cil_code;
633 * link_bblock: Links two basic blocks
635 * links two basic blocks in the control flow graph, the 'from'
636 * argument is the starting block and the 'to' argument is the block
637 * the control flow ends to after 'from'.
640 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
642 MonoBasicBlock **newa;
646 if (from->cil_code) {
648 g_print ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
650 g_print ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
653 g_print ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
655 g_print ("edge from entry to exit\n");
659 for (i = 0; i < from->out_count; ++i) {
660 if (to == from->out_bb [i]) {
666 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
667 for (i = 0; i < from->out_count; ++i) {
668 newa [i] = from->out_bb [i];
676 for (i = 0; i < to->in_count; ++i) {
677 if (from == to->in_bb [i]) {
683 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
684 for (i = 0; i < to->in_count; ++i) {
685 newa [i] = to->in_bb [i];
694 * mono_find_block_region:
696 * We mark each basic block with a region ID. We use that to avoid BB
697 * optimizations when blocks are in different regions.
700 * A region token that encodes where this region is, and information
701 * about the clause owner for this block.
703 * The region encodes the try/catch/filter clause that owns this block
704 * as well as the type. -1 is a special value that represents a block
705 * that is in none of try/catch/filter.
708 mono_find_block_region (MonoCompile *cfg, int offset, int *filter_lengths)
710 MonoMethod *method = cfg->method;
711 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
712 MonoExceptionClause *clause;
715 /* first search for handlers and filters */
716 for (i = 0; i < header->num_clauses; ++i) {
717 clause = &header->clauses [i];
718 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->token_or_filter) &&
719 (offset < (clause->token_or_filter + filter_lengths [i])))
720 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
722 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
723 if (clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)
724 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
726 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
730 /* search the try blocks */
731 for (i = 0; i < header->num_clauses; ++i) {
732 clause = &header->clauses [i];
733 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
734 return ((i + 1) << 8) | clause->flags;
741 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
743 MonoMethod *method = cfg->method;
744 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
745 MonoExceptionClause *clause;
746 MonoBasicBlock *handler;
750 for (i = 0; i < header->num_clauses; ++i) {
751 clause = &header->clauses [i];
752 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
753 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
754 if (clause->flags == type) {
755 handler = g_hash_table_lookup (cfg->bb_hash, header->code + clause->handler_offset);
757 res = g_list_append (res, handler);
765 mono_find_spvar_for_region (MonoCompile *cfg, int region)
767 return g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
771 mono_create_spvar_for_region (MonoCompile *cfg, int region)
775 var = g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
779 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
780 /* prevent it from being register allocated */
781 var->flags |= MONO_INST_INDIRECT;
783 g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
787 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
791 array [*dfn] = start;
792 /*g_print ("visit %d at %p\n", *dfn, start->cil_code);*/
793 for (i = 0; i < start->out_count; ++i) {
794 if (start->out_bb [i]->dfn)
797 start->out_bb [i]->dfn = *dfn;
798 start->out_bb [i]->df_parent = start;
799 array [*dfn] = start->out_bb [i];
800 df_visit (start->out_bb [i], dfn, array);
806 MonoBasicBlock *best;
810 previous_foreach (gconstpointer key, gpointer val, gpointer data)
812 PrevStruct *p = data;
813 MonoBasicBlock *bb = val;
814 //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,
815 //bb->method == p->best->method, bb->cil_code < p->code, bb->cil_code > p->best->cil_code);
817 if (bb->cil_code && bb->cil_code < p->code && bb->cil_code > p->best->cil_code)
821 static MonoBasicBlock*
822 find_previous (GHashTable *bb_hash, MonoBasicBlock *start, const guchar *code) {
828 g_hash_table_foreach (bb_hash, (GHFunc)previous_foreach, &p);
833 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
842 * FIXME: take into account all the details:
843 * second may have been the target of more than one bblock
845 second->out_count = first->out_count;
846 second->out_bb = first->out_bb;
848 for (i = 0; i < first->out_count; ++i) {
849 bb = first->out_bb [i];
850 for (j = 0; j < bb->in_count; ++j) {
851 if (bb->in_bb [j] == first)
852 bb->in_bb [j] = second;
856 first->out_count = 0;
857 first->out_bb = NULL;
858 link_bblock (cfg, first, second);
860 second->last_ins = first->last_ins;
862 /*g_print ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
863 for (inst = first->code; inst && inst->next; inst = inst->next) {
864 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
865 g_print ("found %p: %s", inst->next->cil_code, code);
867 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
868 second->code = inst->next;
870 first->last_ins = inst;
871 second->next_bb = first->next_bb;
872 first->next_bb = second;
877 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
883 mono_type_to_ldind (MonoType *type)
895 case MONO_TYPE_BOOLEAN:
909 case MONO_TYPE_FNPTR:
911 case MONO_TYPE_CLASS:
912 case MONO_TYPE_STRING:
913 case MONO_TYPE_OBJECT:
914 case MONO_TYPE_SZARRAY:
915 case MONO_TYPE_ARRAY:
916 return CEE_LDIND_REF;
924 case MONO_TYPE_VALUETYPE:
925 if (type->data.klass->enumtype) {
926 t = type->data.klass->enum_basetype->type;
930 case MONO_TYPE_TYPEDBYREF:
932 case MONO_TYPE_GENERICINST:
933 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE)
935 return CEE_LDIND_REF;
937 g_error ("unknown type 0x%02x in type_to_ldind", type->type);
943 mono_type_to_stind (MonoType *type)
954 case MONO_TYPE_BOOLEAN:
966 case MONO_TYPE_FNPTR:
968 case MONO_TYPE_CLASS:
969 case MONO_TYPE_STRING:
970 case MONO_TYPE_OBJECT:
971 case MONO_TYPE_SZARRAY:
972 case MONO_TYPE_ARRAY:
973 return CEE_STIND_REF;
981 case MONO_TYPE_VALUETYPE:
982 if (type->data.klass->enumtype) {
983 t = type->data.klass->enum_basetype->type;
987 case MONO_TYPE_TYPEDBYREF:
989 case MONO_TYPE_GENERICINST:
990 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE)
992 return CEE_STIND_REF;
994 g_error ("unknown type 0x%02x in type_to_stind", type->type);
1000 * Returns the type used in the eval stack when @type is loaded.
1001 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
1004 type_to_eval_stack_type (MonoType *type, MonoInst *inst) {
1008 inst->type = STACK_MP;
1016 case MONO_TYPE_BOOLEAN:
1019 case MONO_TYPE_CHAR:
1022 inst->type = STACK_I4;
1027 case MONO_TYPE_FNPTR:
1028 inst->type = STACK_PTR;
1030 case MONO_TYPE_CLASS:
1031 case MONO_TYPE_STRING:
1032 case MONO_TYPE_OBJECT:
1033 case MONO_TYPE_SZARRAY:
1034 case MONO_TYPE_ARRAY:
1035 inst->type = STACK_OBJ;
1039 inst->type = STACK_I8;
1043 inst->type = STACK_R8;
1045 case MONO_TYPE_VALUETYPE:
1046 if (type->data.klass->enumtype) {
1047 t = type->data.klass->enum_basetype->type;
1050 inst->klass = type->data.klass;
1051 inst->type = STACK_VTYPE;
1054 case MONO_TYPE_TYPEDBYREF:
1055 inst->klass = mono_defaults.typed_reference_class;
1056 inst->type = STACK_VTYPE;
1058 case MONO_TYPE_GENERICINST:
1059 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE) {
1060 inst->klass = mono_class_from_mono_type (type);
1061 inst->type = STACK_VTYPE;
1063 inst->type = STACK_OBJ;
1067 g_error ("unknown type 0x%02x in eval stack type", type->type);
1072 * The following tables are used to quickly validate the IL code in type_from_op ().
1075 bin_num_table [STACK_MAX] [STACK_MAX] = {
1076 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1077 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1078 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1079 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1080 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
1081 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
1082 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1083 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1088 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
1091 /* reduce the size of this table */
1093 bin_int_table [STACK_MAX] [STACK_MAX] = {
1094 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1095 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1096 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1097 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1098 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1099 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1100 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, 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}
1105 bin_comp_table [STACK_MAX] [STACK_MAX] = {
1107 {0, 1, 0, 1, 0, 0, 4, 0},
1108 {0, 0, 1, 0, 0, 0, 0, 0},
1109 {0, 1, 0, 1, 0, 2, 4, 0},
1110 {0, 0, 0, 0, 1, 0, 0, 0},
1111 {0, 0, 0, 2, 0, 1, 0, 0},
1112 {0, 4, 0, 4, 0, 0, 3, 0},
1113 {0, 0, 0, 0, 0, 0, 0, 0},
1116 /* reduce the size of this table */
1118 shift_table [STACK_MAX] [STACK_MAX] = {
1119 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1120 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1121 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1122 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1123 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1124 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1125 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, 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}
1130 * Tables to map from the non-specific opcode to the matching
1131 * type-specific opcode.
1133 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
1134 static const guint16
1135 binops_op_map [STACK_MAX] = {
1136 0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, 0
1139 /* handles from CEE_NEG to CEE_CONV_U8 */
1140 static const guint16
1141 unops_op_map [STACK_MAX] = {
1142 0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, 0
1145 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
1146 static const guint16
1147 ovfops_op_map [STACK_MAX] = {
1148 0, 0, OP_LCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_FCONV_TO_U2-CEE_CONV_U2, 0
1151 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
1152 static const guint16
1153 ovf2ops_op_map [STACK_MAX] = {
1154 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
1157 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
1158 static const guint16
1159 ovf3ops_op_map [STACK_MAX] = {
1160 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
1163 /* handles from CEE_CEQ to CEE_CLT_UN */
1164 static const guint16
1165 ceqops_op_map [STACK_MAX] = {
1166 0, 0, OP_LCEQ-CEE_CEQ, OP_PCEQ-CEE_CEQ, OP_FCEQ-CEE_CEQ, 0
1170 * Sets ins->type (the type on the eval stack) according to the
1171 * type of the opcode and the arguments to it.
1172 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1174 * FIXME: this function sets ins->type unconditionally in some cases, but
1175 * it should set it to invalid for some types (a conv.x on an object)
1178 type_from_op (MonoInst *ins) {
1179 switch (ins->opcode) {
1186 /* FIXME: check unverifiable args for STACK_MP */
1187 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1188 ins->opcode += binops_op_map [ins->type];
1195 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1196 ins->opcode += binops_op_map [ins->type];
1201 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1202 ins->opcode += binops_op_map [ins->type];
1205 /* FIXME: handle some specifics with ins->next->type */
1206 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1213 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1214 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1218 ins->type = neg_table [ins->inst_i0->type];
1219 ins->opcode += unops_op_map [ins->type];
1222 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1223 ins->type = ins->inst_i0->type;
1225 ins->type = STACK_INV;
1226 ins->opcode += unops_op_map [ins->type];
1232 ins->type = STACK_I4;
1233 ins->opcode += unops_op_map [ins->inst_i0->type];
1236 ins->type = STACK_R8;
1237 switch (ins->inst_i0->type) {
1242 ins->opcode = OP_LCONV_TO_R_UN;
1246 case CEE_CONV_OVF_I1:
1247 case CEE_CONV_OVF_U1:
1248 case CEE_CONV_OVF_I2:
1249 case CEE_CONV_OVF_U2:
1250 case CEE_CONV_OVF_I4:
1251 case CEE_CONV_OVF_U4:
1252 ins->type = STACK_I4;
1253 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1255 case CEE_CONV_OVF_I_UN:
1256 case CEE_CONV_OVF_U_UN:
1257 ins->type = STACK_PTR;
1258 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1260 case CEE_CONV_OVF_I1_UN:
1261 case CEE_CONV_OVF_I2_UN:
1262 case CEE_CONV_OVF_I4_UN:
1263 case CEE_CONV_OVF_U1_UN:
1264 case CEE_CONV_OVF_U2_UN:
1265 case CEE_CONV_OVF_U4_UN:
1266 ins->type = STACK_I4;
1267 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1270 ins->type = STACK_PTR;
1271 switch (ins->inst_i0->type) {
1277 ins->opcode = OP_LCONV_TO_U;
1280 ins->opcode = OP_FCONV_TO_U;
1286 ins->type = STACK_I8;
1287 ins->opcode += unops_op_map [ins->inst_i0->type];
1289 case CEE_CONV_OVF_I8:
1290 case CEE_CONV_OVF_U8:
1291 ins->type = STACK_I8;
1292 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1294 case CEE_CONV_OVF_U8_UN:
1295 case CEE_CONV_OVF_I8_UN:
1296 ins->type = STACK_I8;
1297 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1301 ins->type = STACK_R8;
1302 ins->opcode += unops_op_map [ins->inst_i0->type];
1305 ins->type = STACK_R8;
1309 ins->type = STACK_I4;
1310 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1313 case CEE_CONV_OVF_I:
1314 case CEE_CONV_OVF_U:
1315 ins->type = STACK_PTR;
1316 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1319 case CEE_ADD_OVF_UN:
1321 case CEE_MUL_OVF_UN:
1323 case CEE_SUB_OVF_UN:
1324 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1325 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1328 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1335 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_MP, STACK_R8, STACK_R8, STACK_OBJ
1338 /* map ldelem.x to the matching ldind.x opcode */
1340 ldelem_to_ldind [] = {
1354 /* map stelem.x to the matching stind.x opcode */
1356 stelem_to_stind [] = {
1370 param_table [STACK_MAX] [STACK_MAX] = {
1375 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1379 switch (args->type) {
1389 for (i = 0; i < sig->param_count; ++i) {
1390 switch (args [i].type) {
1394 if (!sig->params [i]->byref)
1398 if (sig->params [i]->byref)
1400 switch (sig->params [i]->type) {
1401 case MONO_TYPE_CLASS:
1402 case MONO_TYPE_STRING:
1403 case MONO_TYPE_OBJECT:
1404 case MONO_TYPE_SZARRAY:
1405 case MONO_TYPE_ARRAY:
1412 if (sig->params [i]->byref)
1414 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1423 /*if (!param_table [args [i].type] [sig->params [i]->type])
1431 * When we need a pointer to the current domain many times in a method, we
1432 * call mono_domain_get() once and we store the result in a local variable.
1433 * This function returns the variable that represents the MonoDomain*.
1435 inline static MonoInst *
1436 mono_get_domainvar (MonoCompile *cfg)
1438 if (!cfg->domainvar)
1439 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1440 return cfg->domainvar;
1444 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
1447 int num = cfg->num_varinfo;
1449 if ((num + 1) >= cfg->varinfo_count) {
1450 cfg->varinfo_count = (cfg->varinfo_count + 2) * 2;
1451 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
1452 cfg->vars = (MonoMethodVar **)g_realloc (cfg->vars, sizeof (MonoMethodVar*) * cfg->varinfo_count);
1455 /*g_print ("created temp %d of type 0x%x\n", num, type->type);*/
1456 mono_jit_stats.allocate_var++;
1458 MONO_INST_NEW (cfg, inst, opcode);
1459 inst->inst_c0 = num;
1460 inst->inst_vtype = type;
1461 inst->klass = mono_class_from_mono_type (type);
1462 /* if set to 1 the variable is native */
1465 cfg->varinfo [num] = inst;
1467 cfg->vars [num] = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMethodVar));
1468 MONO_INIT_VARINFO (cfg->vars [num], num);
1471 //g_print ("created temp %d of type %s\n", num, mono_type_get_name (type));
1476 type_from_stack_type (MonoInst *ins) {
1477 switch (ins->type) {
1478 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1479 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1480 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1481 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1482 case STACK_MP: return &mono_defaults.int_class->byval_arg;
1483 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1484 case STACK_VTYPE: return &ins->klass->byval_arg;
1486 g_error ("stack type %d to montype not handled\n", ins->type);
1492 array_access_to_klass (int opcode)
1496 return mono_defaults.byte_class;
1498 return mono_defaults.uint16_class;
1501 return mono_defaults.int_class;
1504 return mono_defaults.sbyte_class;
1507 return mono_defaults.int16_class;
1510 return mono_defaults.int32_class;
1512 return mono_defaults.uint32_class;
1515 return mono_defaults.int64_class;
1518 return mono_defaults.single_class;
1521 return mono_defaults.double_class;
1522 case CEE_LDELEM_REF:
1523 case CEE_STELEM_REF:
1524 return mono_defaults.object_class;
1526 g_assert_not_reached ();
1532 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
1536 MONO_ADD_INS (bb, inst);
1539 switch (bb->last_ins->opcode) {
1553 while (prev->next && prev->next != bb->last_ins)
1555 if (prev == bb->code) {
1556 if (bb->last_ins == bb->code) {
1557 inst->next = bb->code;
1560 inst->next = prev->next;
1564 inst->next = bb->last_ins;
1568 // g_warning ("handle conditional jump in add_ins_to_end ()\n");
1570 MONO_ADD_INS (bb, inst);
1576 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
1578 MonoInst *inst, *load;
1580 NEW_TEMPLOAD (cfg, load, src);
1582 NEW_TEMPSTORE (cfg, inst, dest, load);
1583 if (inst->opcode == CEE_STOBJ) {
1584 NEW_TEMPLOADA (cfg, inst, dest);
1585 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE);
1587 inst->cil_code = NULL;
1588 mono_add_ins_to_end (bb, inst);
1593 * We try to share variables when possible
1596 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1601 /* inlining can result in deeper stacks */
1602 if (slot >= ((MonoMethodNormal *)cfg->method)->header->max_stack)
1603 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1605 pos = ins->type - 1 + slot * STACK_MAX;
1607 switch (ins->type) {
1614 if ((vnum = cfg->intvars [pos]))
1615 return cfg->varinfo [vnum];
1616 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1617 cfg->intvars [pos] = res->inst_c0;
1620 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1626 * This function is called to handle items that are left on the evaluation stack
1627 * at basic block boundaries. What happens is that we save the values to local variables
1628 * and we reload them later when first entering the target basic block (with the
1629 * handle_loaded_temps () function).
1630 * It is also used to handle items on the stack in store opcodes, since it is
1631 * possible that the variable to be stored into is already on the stack, in
1632 * which case its old value should be used.
1633 * A single joint point will use the same variables (stored in the array bb->out_stack or
1634 * bb->in_stack, if the basic block is before or after the joint point).
1637 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count) {
1639 MonoBasicBlock *outb;
1640 MonoInst *inst, **locals;
1644 if (cfg->verbose_level > 3)
1645 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
1646 if (!bb->out_scount) {
1648 bb->out_scount = count;
1649 //g_print ("bblock %d has out:", bb->block_num);
1650 for (i = 0; i < bb->out_count; ++i) {
1651 outb = bb->out_bb [i];
1652 //g_print (" %d", outb->block_num);
1653 if (outb->in_stack) {
1655 bb->out_stack = outb->in_stack;
1661 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1662 for (i = 0; i < count; ++i) {
1664 /* try to reuse temps already allocated for this purpouse, if they occupy the same
1665 * stack slot and if they are of the same type. */
1666 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1668 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1673 locals = bb->out_stack;
1674 for (i = 0; i < count; ++i) {
1675 /* add store ops at the end of the bb, before the branch */
1676 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1677 if (inst->opcode == CEE_STOBJ) {
1678 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
1679 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE);
1681 inst->cil_code = sp [i]->cil_code;
1682 mono_add_ins_to_end (bb, inst);
1684 if (cfg->verbose_level > 3)
1685 g_print ("storing %d to temp %d\n", i, locals [i]->inst_c0);
1688 for (i = 0; i < bb->out_count; ++i) {
1689 outb = bb->out_bb [i];
1690 if (outb->in_scount)
1691 continue; /* check they are the same locals */
1692 outb->in_scount = count;
1693 outb->in_stack = locals;
1699 ret_type_to_call_opcode (MonoType *type, int calli, int virt)
1704 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1708 case MONO_TYPE_VOID:
1709 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
1712 case MONO_TYPE_BOOLEAN:
1715 case MONO_TYPE_CHAR:
1718 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1722 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1723 case MONO_TYPE_CLASS:
1724 case MONO_TYPE_STRING:
1725 case MONO_TYPE_OBJECT:
1726 case MONO_TYPE_SZARRAY:
1727 case MONO_TYPE_ARRAY:
1728 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1731 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
1734 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
1735 case MONO_TYPE_VALUETYPE:
1736 if (type->data.klass->enumtype) {
1737 t = type->data.klass->enum_basetype->type;
1740 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1741 case MONO_TYPE_TYPEDBYREF:
1742 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1743 case MONO_TYPE_GENERICINST:
1744 t = type->data.generic_inst->generic_type->type;
1747 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
1753 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
1755 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
1757 ji->ip.label = label;
1758 ji->type = MONO_PATCH_INFO_SWITCH;
1759 ji->data.table = bbs;
1760 ji->next = cfg->patch_info;
1761 ji->table_size = num_blocks;
1762 cfg->patch_info = ji;
1766 * When we add a tree of instructions, we need to ensure the instructions currently
1767 * on the stack are executed before (like, if we load a value from a local).
1768 * We ensure this by saving the currently loaded values to temps and rewriting the
1769 * instructions to load the values.
1770 * This is not done for opcodes that terminate a basic block (because it's handled already
1771 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
1774 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
1776 MonoInst *load, *store, *temp, *ins;
1778 while (stack < sp) {
1780 /* handle also other constants */
1781 if (ins->opcode != OP_ICONST) {
1782 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1783 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1784 store->cil_code = ins->cil_code;
1785 if (store->opcode == CEE_STOBJ) {
1786 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
1787 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE);
1789 MONO_ADD_INS (bblock, store);
1790 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1791 load->cil_code = ins->cil_code;
1799 * Prepare arguments for passing to a function call.
1800 * Return a non-zero value if the arguments can't be passed to the given
1802 * The type checks are not yet complete and some conversions may need
1803 * casts on 32 or 64 bit architectures.
1806 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
1811 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
1815 for (i = 0; i < sig->param_count; ++i) {
1816 if (sig->params [i]->byref) {
1818 * check the result of ldelema is only passed as an argument if the byref
1819 * type matches exactly the array element type.
1820 * FIXME: if the argument as been saved on the stack as part of the
1821 * interface variable code (the value was on the stack at a basic block boundary)
1822 * we need to add the check in that case, too.
1824 if (args [i]->opcode == CEE_LDELEMA) {
1826 MonoClass *exact_class = mono_class_from_mono_type (sig->params [i]);
1827 if (!exact_class->valuetype) {
1828 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
1829 check->cil_code = args [i]->cil_code;
1830 check->klass = exact_class;
1831 check->inst_left = args [i]->inst_left;
1832 check->type = STACK_OBJ;
1833 args [i]->inst_left = check;
1836 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
1840 simple_type = sig->params [i]->type;
1842 switch (simple_type) {
1843 case MONO_TYPE_VOID:
1848 case MONO_TYPE_BOOLEAN:
1851 case MONO_TYPE_CHAR:
1854 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
1860 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
1863 case MONO_TYPE_CLASS:
1864 case MONO_TYPE_STRING:
1865 case MONO_TYPE_OBJECT:
1866 case MONO_TYPE_SZARRAY:
1867 case MONO_TYPE_ARRAY:
1868 if (args [i]->type != STACK_OBJ)
1873 if (args [i]->type != STACK_I8)
1878 if (args [i]->type != STACK_R8)
1881 case MONO_TYPE_VALUETYPE:
1882 if (sig->params [i]->data.klass->enumtype) {
1883 simple_type = sig->params [i]->data.klass->enum_basetype->type;
1886 if (args [i]->type != STACK_VTYPE)
1889 case MONO_TYPE_TYPEDBYREF:
1890 if (args [i]->type != STACK_VTYPE)
1893 case MONO_TYPE_GENERICINST:
1894 simple_type = sig->params [i]->data.generic_inst->generic_type->type;
1898 g_error ("unknown type 0x%02x in check_call_signature", simple_type);
1905 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
1906 const guint8 *ip, gboolean to_end)
1908 MonoInst *temp, *store, *ins = (MonoInst*)call;
1909 MonoType *ret = sig->ret;
1911 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
1913 call->inst.type = STACK_OBJ;
1914 call->inst.opcode = CEE_CALL;
1915 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
1917 type_to_eval_stack_type (ret, ins);
1918 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
1921 if (MONO_TYPE_ISSTRUCT (ret)) {
1924 /* we use this to allocate native sized structs */
1925 temp->unused = sig->pinvoke;
1927 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
1928 if (call->inst.opcode == OP_VCALL)
1929 ins->inst_left = loada;
1931 ins->inst_right = loada; /* a virtual or indirect call */
1934 mono_add_ins_to_end (bblock, ins);
1936 MONO_ADD_INS (bblock, ins);
1938 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1939 store->cil_code = ip;
1941 mono_add_ins_to_end (bblock, store);
1943 MONO_ADD_INS (bblock, store);
1945 return temp->inst_c0;
1948 mono_add_ins_to_end (bblock, ins);
1950 MONO_ADD_INS (bblock, ins);
1955 inline static MonoCallInst *
1956 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1957 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
1962 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual));
1964 call->inst.cil_code = ip;
1966 call->signature = sig;
1967 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
1969 for (arg = call->out_args; arg;) {
1970 MonoInst *narg = arg->next;
1975 mono_add_ins_to_end (bblock, arg);
1977 MONO_ADD_INS (bblock, arg);
1984 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1985 MonoInst **args, MonoInst *addr, const guint8 *ip)
1987 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
1989 call->inst.inst_i0 = addr;
1991 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
1994 static MonoCallInst*
1995 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
1996 MonoInst **args, const guint8 *ip, MonoInst *this)
1998 gboolean virtual = this != NULL;
2001 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, FALSE);
2003 if (this && sig->hasthis &&
2004 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
2005 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
2006 call->method = mono_marshal_get_remoting_invoke_with_check (method);
2008 call->method = method;
2010 call->inst.flags |= MONO_INST_HAS_METHOD;
2011 call->inst.inst_left = this;
2017 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
2018 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
2020 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, signature, args, ip, this);
2022 return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
2026 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
2027 MonoInst **args, const guint8 *ip, gboolean to_end)
2033 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
2035 return mono_spill_call (cfg, bblock, call, sig, func == mono_array_new_va, ip, to_end);
2039 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
2041 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
2044 g_warning ("unregistered JIT ICall");
2045 g_assert_not_reached ();
2048 return mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, args, ip, FALSE);
2052 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
2054 MonoInst *ins, *temp = NULL, *store, *load, *begin;
2055 MonoInst *last_arg = NULL;
2059 //g_print ("emulating: ");
2060 //mono_print_tree_nl (tree);
2061 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE));
2062 ins = (MonoInst*)call;
2064 call->inst.cil_code = tree->cil_code;
2066 call->signature = info->sig;
2068 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
2070 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2071 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
2072 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2073 store->cil_code = tree->cil_code;
2078 nargs = info->sig->param_count + info->sig->hasthis;
2080 for (last_arg = call->out_args; last_arg && last_arg->next; last_arg = last_arg->next) ;
2083 last_arg->next = store;
2086 begin = call->out_args;
2090 if (cfg->prev_ins) {
2092 * This assumes that that in a tree, emulate_opcode is called for a
2093 * node before it is called for its children. dec_foreach needs to
2094 * take this into account.
2096 store->next = cfg->prev_ins->next;
2097 cfg->prev_ins->next = begin;
2099 store->next = cfg->cbb->code;
2100 cfg->cbb->code = begin;
2103 call->fptr = mono_icall_get_wrapper (info);
2105 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2106 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2111 static MonoMethodSignature *
2112 mono_get_element_address_signature (int arity)
2114 static GHashTable *sighash = NULL;
2115 MonoMethodSignature *res;
2118 EnterCriticalSection (&trampoline_hash_mutex);
2120 sighash = g_hash_table_new (NULL, NULL);
2122 else if ((res = g_hash_table_lookup (sighash, (gpointer)arity))) {
2123 LeaveCriticalSection (&trampoline_hash_mutex);
2127 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
2130 res->params [0] = &mono_defaults.array_class->byval_arg;
2132 for (i = 1; i <= arity; i++)
2133 res->params [i] = &mono_defaults.int_class->byval_arg;
2135 res->ret = &mono_defaults.int_class->byval_arg;
2137 g_hash_table_insert (sighash, (gpointer)arity, res);
2138 LeaveCriticalSection (&trampoline_hash_mutex);
2144 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native) {
2145 MonoInst *iargs [3];
2151 * This check breaks with spilled vars... need to handle it during verification anyway.
2152 * g_assert (klass && klass == src->klass && klass == dest->klass);
2156 n = mono_class_native_size (klass, &align);
2158 n = mono_class_value_size (klass, &align);
2160 if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
2162 MONO_INST_NEW (cfg, inst, OP_MEMCPY);
2163 inst->inst_left = dest;
2164 inst->inst_right = src;
2165 inst->cil_code = ip;
2167 MONO_ADD_INS (bblock, inst);
2172 NEW_ICONST (cfg, iargs [2], n);
2174 mono_emit_native_call (cfg, bblock, helper_memcpy, helper_sig_memcpy, iargs, ip, to_end);
2178 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
2180 MonoInst *iargs [2];
2181 MonoInst *ins, *zero_int32;
2184 NEW_ICONST (cfg, zero_int32, 0);
2186 mono_class_init (klass);
2187 n = mono_class_value_size (klass, NULL);
2188 MONO_INST_NEW (cfg, ins, 0);
2190 ins->inst_left = dest;
2191 ins->inst_right = zero_int32;
2194 ins->opcode = CEE_STIND_I1;
2195 MONO_ADD_INS (bblock, ins);
2198 ins->opcode = CEE_STIND_I2;
2199 MONO_ADD_INS (bblock, ins);
2202 ins->opcode = CEE_STIND_I4;
2203 MONO_ADD_INS (bblock, ins);
2206 if (n <= sizeof (gpointer) * 5) {
2207 ins->opcode = OP_MEMSET;
2210 MONO_ADD_INS (bblock, ins);
2213 handle_loaded_temps (cfg, bblock, stack_start, sp);
2214 NEW_ICONST (cfg, ins, n);
2217 mono_emit_jit_icall (cfg, bblock, helper_initobj, iargs, ip);
2223 handle_box (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *val, const guchar *ip, MonoClass *klass)
2225 MonoInst *iargs [2];
2226 MonoInst *dest, *vtoffset, *add, *vstore;
2229 /* much like NEWOBJ */
2230 if (cfg->opt & MONO_OPT_SHARED) {
2231 NEW_DOMAINCONST (cfg, iargs [0]);
2232 NEW_CLASSCONST (cfg, iargs [1], klass);
2234 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
2236 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
2237 NEW_VTABLECONST (cfg, iargs [0], vtable);
2238 if (klass->has_finalize || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
2239 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
2241 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
2243 NEW_TEMPLOAD (cfg, dest, temp);
2244 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
2245 MONO_INST_NEW (cfg, add, CEE_ADD);
2246 add->inst_left = dest;
2247 add->inst_right = vtoffset;
2250 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
2251 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
2252 vstore->cil_code = ip;
2253 vstore->inst_left = add;
2254 vstore->inst_right = val;
2256 if (vstore->opcode == CEE_STOBJ) {
2257 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
2259 MONO_ADD_INS (bblock, vstore);
2261 NEW_TEMPLOAD (cfg, dest, temp);
2265 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
2268 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
2270 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
2271 MonoMethodSignature *signature = method->signature;
2275 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2276 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2277 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
2278 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
2279 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2280 (method->klass->marshalbyref) ||
2281 !header || header->num_clauses ||
2282 /* fixme: why cant we inline valuetype returns? */
2283 MONO_TYPE_ISSTRUCT (signature->ret))
2286 /* its not worth to inline methods with valuetype arguments?? */
2287 for (i = 0; i < signature->param_count; i++) {
2288 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
2294 * if we can initialize the class of the method right away, we do,
2295 * otherwise we don't allow inlining if the class needs initialization,
2296 * since it would mean inserting a call to mono_runtime_class_init()
2297 * inside the inlined code
2299 if (!(cfg->opt & MONO_OPT_SHARED)) {
2300 vtable = mono_class_vtable (cfg->domain, method->klass);
2301 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
2302 if (cfg->run_cctors)
2303 mono_runtime_class_init (vtable);
2305 else if (!vtable->initialized && mono_class_needs_cctor_run (method->klass, NULL))
2309 * If we're compiling for shared code
2310 * the cctor will need to be run at aot method load time, for example,
2311 * or at the end of the compilation of the inlining method.
2313 if (mono_class_needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
2316 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
2318 /* also consider num_locals? */
2319 if (getenv ("MONO_INLINELIMIT"))
2320 if (header->code_size < atoi (getenv ("MONO_INLINELIMIT"))) {
2324 if (header->code_size < 20)
2331 mini_get_ldelema_ins (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
2335 MonoMethodSignature *esig;
2336 char icall_name [256];
2337 MonoJitICallInfo *info;
2339 rank = cmethod->signature->param_count - (is_set? 1: 0);
2341 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
2343 NEW_GROUP (cfg, indexes, sp [1], sp [2]);
2344 MONO_INST_NEW (cfg, addr, OP_LDELEMA2D);
2345 addr->inst_left = sp [0];
2346 addr->inst_right = indexes;
2347 addr->cil_code = ip;
2348 addr->type = STACK_MP;
2349 addr->klass = cmethod->klass;
2353 /* Need to register the icall so it gets an icall wrapper */
2354 sprintf (icall_name, "ves_array_element_address_%d", rank);
2356 info = mono_find_jit_icall_by_name (icall_name);
2358 esig = mono_get_element_address_signature (rank);
2359 info = mono_register_jit_icall (ves_array_element_address, g_strdup (icall_name), esig, FALSE);
2362 temp = mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, sp, ip, FALSE);
2364 NEW_TEMPLOAD (cfg, addr, temp);
2368 static MonoJitICallInfo **emul_opcode_map = NULL;
2370 static inline MonoJitICallInfo *
2371 mono_find_jit_opcode_emulation (int opcode)
2373 if (emul_opcode_map)
2374 return emul_opcode_map [opcode];
2380 mini_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
2385 if (cmethod->klass == mono_defaults.string_class) {
2386 if (cmethod->name [0] != 'g')
2389 if (strcmp (cmethod->name, "get_Chars") == 0)
2391 else if (strcmp (cmethod->name, "get_Length") == 0)
2394 } else if (cmethod->klass == mono_defaults.object_class) {
2395 if (strcmp (cmethod->name, "GetType") == 0)
2399 } else if (cmethod->klass == mono_defaults.array_class) {
2400 if (strcmp (cmethod->name, "get_Rank") == 0)
2402 else if (strcmp (cmethod->name, "get_Length") == 0)
2407 op = mono_arch_get_opcode_for_method (cfg, cmethod, fsig, args);
2411 pc = fsig->param_count + fsig->hasthis;
2412 MONO_INST_NEW (cfg, ins, op);
2415 ins->inst_i0 = args [0];
2417 ins->inst_i1 = args [1];
2424 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
2426 MonoInst *store, *temp;
2429 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
2431 if (!sig->hasthis && sig->param_count == 0)
2435 if (sp [0]->opcode == OP_ICONST) {
2438 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
2440 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2441 store->cil_code = sp [0]->cil_code;
2442 MONO_ADD_INS (bblock, store);
2447 for (i = 0; i < sig->param_count; ++i) {
2448 if (sp [0]->opcode == OP_ICONST) {
2451 temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
2453 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2454 store->cil_code = sp [0]->cil_code;
2455 if (store->opcode == CEE_STOBJ) {
2456 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2457 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE);
2459 MONO_ADD_INS (bblock, store);
2467 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
2468 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b)
2470 MonoInst *ins, *rvar = NULL;
2471 MonoMethodHeader *cheader;
2472 MonoBasicBlock *ebblock, *sbblock;
2473 int i, costs, new_locals_offset;
2475 if (cfg->verbose_level > 2)
2476 g_print ("INLINE START %p %s -> %s\n", cmethod, mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2478 cheader = ((MonoMethodNormal *)cmethod)->header;
2480 if (!cmethod->inline_info) {
2481 mono_jit_stats.inlineable_methods++;
2482 cmethod->inline_info = 1;
2484 /* allocate space to store the return value */
2485 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2486 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2489 /* allocate local variables */
2490 new_locals_offset = cfg->num_varinfo;
2491 for (i = 0; i < cheader->num_locals; ++i)
2492 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
2494 /* allocate starte and end blocks */
2495 sbblock = NEW_BBLOCK (cfg);
2496 sbblock->block_num = cfg->num_bblocks++;
2497 sbblock->real_offset = real_offset;
2499 ebblock = NEW_BBLOCK (cfg);
2500 ebblock->block_num = cfg->num_bblocks++;
2501 ebblock->real_offset = real_offset;
2503 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
2505 if (costs >= 0 && costs < 60) {
2506 if (cfg->verbose_level > 2)
2507 g_print ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2509 mono_jit_stats.inlined_methods++;
2511 /* always add some code to avoid block split failures */
2512 MONO_INST_NEW (cfg, ins, CEE_NOP);
2513 MONO_ADD_INS (bblock, ins);
2516 bblock->next_bb = sbblock;
2517 link_bblock (cfg, bblock, sbblock);
2520 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
2526 if (cfg->verbose_level > 2)
2527 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
2533 * Some of these comments may well be out-of-date.
2534 * Design decisions: we do a single pass over the IL code (and we do bblock
2535 * splitting/merging in the few cases when it's required: a back jump to an IL
2536 * address that was not already seen as bblock starting point).
2537 * Code is validated as we go (full verification is still better left to metadata/verify.c).
2538 * Complex operations are decomposed in simpler ones right away. We need to let the
2539 * arch-specific code peek and poke inside this process somehow (except when the
2540 * optimizations can take advantage of the full semantic info of coarse opcodes).
2541 * All the opcodes of the form opcode.s are 'normalized' to opcode.
2542 * MonoInst->opcode initially is the IL opcode or some simplification of that
2543 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
2544 * opcode with value bigger than OP_LAST.
2545 * At this point the IR can be handed over to an interpreter, a dumb code generator
2546 * or to the optimizing code generator that will translate it to SSA form.
2548 * Profiling directed optimizations.
2549 * We may compile by default with few or no optimizations and instrument the code
2550 * or the user may indicate what methods to optimize the most either in a config file
2551 * or through repeated runs where the compiler applies offline the optimizations to
2552 * each method and then decides if it was worth it.
2555 * * consider using an array instead of an hash table (bb_hash)
2558 #define CHECK_TYPE(ins) if (!(ins)->type) goto unverified
2559 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) goto unverified
2560 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) goto unverified
2561 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) goto unverified
2562 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) goto unverified
2563 #define CHECK_OPSIZE(size) if (ip + size > end) goto unverified
2566 /* offset from br.s -> br like opcodes */
2567 #define BIG_BRANCH_OFFSET 13
2570 get_basic_blocks (MonoCompile *cfg, GHashTable *bbhash, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
2572 unsigned char *ip = start;
2573 unsigned char *target;
2576 MonoBasicBlock *bblock;
2577 const MonoOpcode *opcode;
2580 cli_addr = ip - start;
2581 i = mono_opcode_value ((const guint8 **)&ip);
2582 opcode = &mono_opcodes [i];
2583 switch (opcode->argument) {
2584 case MonoInlineNone:
2587 case MonoInlineString:
2588 case MonoInlineType:
2589 case MonoInlineField:
2590 case MonoInlineMethod:
2593 case MonoShortInlineR:
2600 case MonoShortInlineVar:
2601 case MonoShortInlineI:
2604 case MonoShortInlineBrTarget:
2605 target = start + cli_addr + 2 + (signed char)ip [1];
2606 GET_BBLOCK (cfg, bbhash, bblock, target);
2609 case MonoInlineBrTarget:
2610 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
2611 GET_BBLOCK (cfg, bbhash, bblock, target);
2614 case MonoInlineSwitch: {
2615 guint32 n = read32 (ip + 1);
2618 cli_addr += 5 + 4 * n;
2619 target = start + cli_addr;
2620 GET_BBLOCK (cfg, bbhash, bblock, target);
2622 for (j = 0; j < n; ++j) {
2623 target = start + cli_addr + (gint32)read32 (ip);
2624 GET_BBLOCK (cfg, bbhash, bblock, target);
2634 g_assert_not_reached ();
2644 * mono_method_to_ir: translates IL into basic blocks containing trees
2647 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
2648 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
2649 guint inline_offset, gboolean is_virtual_call)
2651 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
2652 MonoInst *ins, **sp, **stack_start;
2653 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
2655 MonoMethod *cmethod;
2656 MonoInst **arg_array;
2657 MonoMethodHeader *header;
2659 guint32 token, ins_flag;
2661 MonoClass *constrained_call = NULL;
2662 unsigned char *ip, *end, *target, *err_pos;
2663 static double r8_0 = 0.0;
2664 MonoMethodSignature *sig;
2665 MonoGenericContext *generic_context = NULL;
2666 MonoType **param_types;
2667 GList *bb_recheck = NULL, *tmp;
2668 int i, n, start_new_bblock, align;
2669 int num_calls = 0, inline_costs = 0;
2670 int *filter_lengths = NULL;
2671 int breakpoint_id = 0;
2672 guint real_offset, num_args;
2674 image = method->klass->image;
2675 header = ((MonoMethodNormal *)method)->header;
2676 sig = method->signature;
2677 num_args = sig->hasthis + sig->param_count;
2678 ip = (unsigned char*)header->code;
2679 end = ip + header->code_size;
2680 mono_jit_stats.cil_code_size += header->code_size;
2682 if (method->signature->is_inflated)
2683 generic_context = ((MonoMethodInflated *) method)->context;
2685 if (cfg->method == method) {
2687 bbhash = cfg->bb_hash;
2689 real_offset = inline_offset;
2690 bbhash = g_hash_table_new (g_direct_hash, NULL);
2693 if (cfg->verbose_level > 2)
2694 g_print ("method to IR %s\n", mono_method_full_name (method, TRUE));
2696 dont_inline = g_list_prepend (dont_inline, method);
2697 if (cfg->method == method) {
2699 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
2700 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
2703 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
2704 start_bblock->cil_code = NULL;
2705 start_bblock->cil_length = 0;
2706 start_bblock->block_num = cfg->num_bblocks++;
2709 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
2710 end_bblock->cil_code = NULL;
2711 end_bblock->cil_length = 0;
2712 end_bblock->block_num = cfg->num_bblocks++;
2713 g_assert (cfg->num_bblocks == 2);
2715 arg_array = alloca (sizeof (MonoInst *) * num_args);
2716 for (i = num_args - 1; i >= 0; i--)
2717 arg_array [i] = cfg->varinfo [i];
2719 if (header->num_clauses) {
2720 int size = sizeof (int) * header->num_clauses;
2721 filter_lengths = alloca (size);
2722 memset (filter_lengths, 0, size);
2724 cfg->spvars = g_hash_table_new (NULL, NULL);
2726 /* handle exception clauses */
2727 for (i = 0; i < header->num_clauses; ++i) {
2728 //unsigned char *p = ip;
2729 MonoExceptionClause *clause = &header->clauses [i];
2730 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->try_offset);
2731 tblock->real_offset = clause->try_offset;
2732 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
2733 tblock->real_offset = clause->handler_offset;
2735 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
2736 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2737 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2738 MONO_ADD_INS (tblock, ins);
2741 /*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);
2743 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
2745 /* catch and filter blocks get the exception object on the stack */
2746 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
2747 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2748 /* mostly like handle_stack_args (), but just sets the input args */
2749 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
2751 cfg->exvar = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
2752 /* prevent it from being register allocated */
2753 cfg->exvar->flags |= MONO_INST_INDIRECT;
2755 tblock->in_scount = 1;
2756 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2757 tblock->in_stack [0] = cfg->exvar;
2759 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2760 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->token_or_filter);
2761 tblock->real_offset = clause->token_or_filter;
2762 tblock->in_scount = 1;
2763 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2764 tblock->in_stack [0] = cfg->exvar;
2765 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2766 MONO_ADD_INS (tblock, ins);
2772 arg_array = alloca (sizeof (MonoInst *) * num_args);
2773 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
2776 /* FIRST CODE BLOCK */
2777 bblock = NEW_BBLOCK (cfg);
2778 bblock->cil_code = ip;
2780 ADD_BBLOCK (cfg, bbhash, bblock);
2782 if (cfg->method == method) {
2783 breakpoint_id = mono_debugger_method_has_breakpoint (method);
2784 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
2785 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2786 MONO_ADD_INS (bblock, ins);
2790 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED))) || mono_compile_aot) {
2791 /* we use a separate basic block for the initialization code */
2792 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
2793 init_localsbb->real_offset = real_offset;
2794 start_bblock->next_bb = init_localsbb;
2795 init_localsbb->next_bb = bblock;
2796 link_bblock (cfg, start_bblock, init_localsbb);
2797 link_bblock (cfg, init_localsbb, bblock);
2798 init_localsbb->block_num = cfg->num_bblocks++;
2800 start_bblock->next_bb = bblock;
2801 link_bblock (cfg, start_bblock, bblock);
2804 if (get_basic_blocks (cfg, bbhash, header, real_offset, ip, end, &err_pos)) {
2809 mono_debug_init_method (cfg, bblock, breakpoint_id);
2811 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
2813 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
2814 for (n = 0; n < sig->param_count; ++n)
2815 param_types [n + sig->hasthis] = sig->params [n];
2817 /* do this somewhere outside - not here */
2818 NEW_ICONST (cfg, zero_int32, 0);
2819 NEW_ICONST (cfg, zero_int64, 0);
2820 zero_int64->type = STACK_I8;
2821 NEW_PCONST (cfg, zero_ptr, 0);
2822 NEW_PCONST (cfg, zero_obj, 0);
2823 zero_obj->type = STACK_OBJ;
2825 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
2826 zero_r8->type = STACK_R8;
2827 zero_r8->inst_p0 = &r8_0;
2829 /* add a check for this != NULL to inlined methods */
2830 if (is_virtual_call) {
2831 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
2832 NEW_ARGLOAD (cfg, ins->inst_left, 0);
2834 MONO_ADD_INS (bblock, ins);
2837 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
2838 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
2841 start_new_bblock = 0;
2844 if (cfg->method == method)
2845 real_offset = ip - header->code;
2847 real_offset = inline_offset;
2849 if (start_new_bblock) {
2850 bblock->cil_length = ip - bblock->cil_code;
2851 if (start_new_bblock == 2) {
2852 g_assert (ip == tblock->cil_code);
2854 GET_BBLOCK (cfg, bbhash, tblock, ip);
2856 bblock->next_bb = tblock;
2858 start_new_bblock = 0;
2859 for (i = 0; i < bblock->in_scount; ++i) {
2860 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2864 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
2865 link_bblock (cfg, bblock, tblock);
2866 if (sp != stack_start) {
2867 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2870 bblock->next_bb = tblock;
2872 for (i = 0; i < bblock->in_scount; ++i) {
2873 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2879 bblock->real_offset = real_offset;
2881 if ((cfg->method == method) && cfg->coverage_info) {
2882 MonoInst *store, *one;
2883 guint32 cil_offset = ip - header->code;
2884 cfg->coverage_info->data [cil_offset].cil_code = ip;
2886 /* TODO: Use an increment here */
2887 NEW_ICONST (cfg, one, 1);
2890 NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
2893 MONO_INST_NEW (cfg, store, CEE_STIND_I);
2894 store->cil_code = ip;
2895 store->inst_left = ins;
2896 store->inst_right = one;
2898 MONO_ADD_INS (bblock, store);
2901 if (cfg->verbose_level > 3)
2902 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, sp-stack_start, mono_disasm_code_one (NULL, method, ip, NULL));
2909 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2910 ins->cil_code = ip++;
2911 MONO_ADD_INS (bblock, ins);
2917 CHECK_STACK_OVF (1);
2918 n = (*ip)-CEE_LDARG_0;
2920 NEW_ARGLOAD (cfg, ins, n);
2921 ins->cil_code = ip++;
2928 CHECK_STACK_OVF (1);
2929 n = (*ip)-CEE_LDLOC_0;
2931 NEW_LOCLOAD (cfg, ins, n);
2932 ins->cil_code = ip++;
2940 n = (*ip)-CEE_STLOC_0;
2943 handle_loaded_temps (cfg, bblock, stack_start, sp);
2944 NEW_LOCSTORE (cfg, ins, n, *sp);
2946 if (ins->opcode == CEE_STOBJ) {
2947 NEW_LOCLOADA (cfg, ins, n);
2948 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2950 MONO_ADD_INS (bblock, ins);
2956 CHECK_STACK_OVF (1);
2958 NEW_ARGLOAD (cfg, ins, ip [1]);
2965 CHECK_STACK_OVF (1);
2967 NEW_ARGLOADA (cfg, ins, ip [1]);
2977 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
2978 handle_loaded_temps (cfg, bblock, stack_start, sp);
2980 if (ins->opcode == CEE_STOBJ) {
2981 NEW_ARGLOADA (cfg, ins, ip [1]);
2982 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2984 MONO_ADD_INS (bblock, ins);
2989 CHECK_STACK_OVF (1);
2990 CHECK_LOCAL (ip [1]);
2991 NEW_LOCLOAD (cfg, ins, ip [1]);
2998 CHECK_STACK_OVF (1);
2999 CHECK_LOCAL (ip [1]);
3000 NEW_LOCLOADA (cfg, ins, ip [1]);
3009 handle_loaded_temps (cfg, bblock, stack_start, sp);
3010 CHECK_LOCAL (ip [1]);
3011 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
3013 if (ins->opcode == CEE_STOBJ) {
3014 NEW_LOCLOADA (cfg, ins, ip [1]);
3015 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3017 MONO_ADD_INS (bblock, ins);
3022 CHECK_STACK_OVF (1);
3023 NEW_PCONST (cfg, ins, NULL);
3025 ins->type = STACK_OBJ;
3030 CHECK_STACK_OVF (1);
3031 NEW_ICONST (cfg, ins, -1);
3045 CHECK_STACK_OVF (1);
3046 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
3053 CHECK_STACK_OVF (1);
3055 NEW_ICONST (cfg, ins, *((signed char*)ip));
3062 CHECK_STACK_OVF (1);
3063 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
3070 CHECK_STACK_OVF (1);
3071 MONO_INST_NEW (cfg, ins, OP_I8CONST);
3073 ins->type = STACK_I8;
3075 ins->inst_l = (gint64)read64 (ip);
3080 float *f = g_malloc (sizeof (float));
3082 CHECK_STACK_OVF (1);
3083 MONO_INST_NEW (cfg, ins, OP_R4CONST);
3084 ins->type = STACK_R8;
3093 double *d = g_malloc (sizeof (double));
3095 CHECK_STACK_OVF (1);
3096 MONO_INST_NEW (cfg, ins, OP_R8CONST);
3097 ins->type = STACK_R8;
3106 MonoInst *temp, *store;
3108 CHECK_STACK_OVF (1);
3113 * small optimization: if the loaded value was from a local already,
3114 * just load it twice.
3116 if (ins->ssa_op == MONO_SSA_LOAD &&
3117 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
3119 MONO_INST_NEW (cfg, temp, 0);
3121 temp->cil_code = ip;
3124 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3125 temp->cil_code = ip;
3126 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3127 store->cil_code = ip;
3128 MONO_ADD_INS (bblock, store);
3129 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3132 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3142 MONO_INST_NEW (cfg, ins, CEE_POP);
3143 MONO_ADD_INS (bblock, ins);
3144 ins->cil_code = ip++;
3150 if (stack_start != sp)
3152 MONO_INST_NEW (cfg, ins, CEE_JMP);
3153 token = read32 (ip + 1);
3154 /* FIXME: check the signature matches */
3155 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3156 ins->inst_p0 = cmethod;
3157 MONO_ADD_INS (bblock, ins);
3159 start_new_bblock = 1;
3163 case CEE_CALLVIRT: {
3164 MonoInst *addr = NULL;
3165 MonoMethodSignature *fsig = NULL;
3166 int temp, array_rank = 0;
3167 int virtual = *ip == CEE_CALLVIRT;
3170 token = read32 (ip + 1);
3172 if (*ip == CEE_CALLI) {
3177 if (method->wrapper_type != MONO_WRAPPER_NONE)
3178 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
3180 fsig = mono_metadata_parse_signature (image, token);
3182 n = fsig->param_count + fsig->hasthis;
3184 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3185 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
3186 } else if (constrained_call) {
3187 cmethod = mono_get_method_constrained (image, token, constrained_call, generic_context);
3189 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3194 if (!cmethod->klass->inited)
3195 mono_class_init (cmethod->klass);
3197 if (cmethod->signature->pinvoke) {
3198 #ifdef MONO_USE_EXC_TABLES
3199 if (mono_method_blittable (cmethod)) {
3200 fsig = cmethod->signature;
3203 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
3204 fsig = wrapper->signature;
3205 #ifdef MONO_USE_EXC_TABLES
3209 fsig = mono_method_get_signature (cmethod, image, token);
3212 n = fsig->param_count + fsig->hasthis;
3214 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
3215 cmethod->klass->parent == mono_defaults.array_class) {
3216 array_rank = cmethod->klass->rank;
3219 if (cmethod->string_ctor)
3220 g_assert_not_reached ();
3226 //g_assert (!virtual || fsig->hasthis);
3230 if (constrained_call) {
3232 * We have the `constrained.' prefix opcode.
3234 if (constrained_call->valuetype && !cmethod->klass->valuetype) {
3236 * The type parameter is instantiated as a valuetype,
3237 * but that type doesn't override the method we're
3238 * calling, so we need to box `this'.
3240 sp [0] = handle_box (cfg, bblock, sp [0], ip, constrained_call);
3241 } else if (!constrained_call->valuetype) {
3245 * The type parameter is instantiated as a reference
3246 * type. We have a managed pointer on the stack, so
3247 * we need to dereference it here.
3250 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3252 ins->inst_i0 = sp [0];
3253 ins->type = STACK_OBJ;
3255 } else if (cmethod->klass->valuetype)
3257 constrained_call = NULL;
3260 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
3263 if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL) && (mono_metadata_signature_equal (method->signature, cmethod->signature))) {
3265 /* FIXME: This assumes the two methods has the same number and type of arguments */
3266 for (i = 0; i < n; ++i) {
3267 /* Check if argument is the same */
3268 NEW_ARGLOAD (cfg, ins, i);
3269 if ((ins->opcode == sp [i]->opcode) && (ins->inst_i0 == sp [i]->inst_i0))
3272 /* Prevent argument from being register allocated */
3273 arg_array [i]->flags |= MONO_INST_VOLATILE;
3274 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3276 if (ins->opcode == CEE_STOBJ) {
3277 NEW_ARGLOADA (cfg, ins, i);
3278 handle_stobj (cfg, bblock, ins, sp [i], sp [i]->cil_code, ins->klass, FALSE, FALSE);
3281 MONO_ADD_INS (bblock, ins);
3283 MONO_INST_NEW (cfg, ins, CEE_JMP);
3285 ins->inst_p0 = cmethod;
3286 ins->inst_p1 = arg_array [0];
3287 MONO_ADD_INS (bblock, ins);
3288 start_new_bblock = 1;
3289 /* skip CEE_RET as well */
3294 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_opcode_for_method (cfg, cmethod, fsig, sp))) {
3297 if (MONO_TYPE_IS_VOID (fsig->ret)) {
3298 MONO_ADD_INS (bblock, ins);
3300 type_to_eval_stack_type (fsig->ret, ins);
3309 handle_loaded_temps (cfg, bblock, stack_start, sp);
3311 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3312 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
3313 mono_method_check_inlining (cfg, cmethod) &&
3314 !g_list_find (dont_inline, cmethod)) {
3316 MonoBasicBlock *ebblock;
3318 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3322 GET_BBLOCK (cfg, bbhash, bblock, ip);
3323 ebblock->next_bb = bblock;
3324 link_bblock (cfg, ebblock, bblock);
3326 if (!MONO_TYPE_IS_VOID (fsig->ret))
3329 /* indicates start of a new block, and triggers a load of all
3330 stack arguments at bb boundarie */
3333 inline_costs += costs;
3338 inline_costs += 10 * num_calls++;
3340 /* tail recursion elimination */
3341 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == method && ip [5] == CEE_RET) {
3342 gboolean has_vtargs = FALSE;
3345 /* keep it simple */
3346 for (i = fsig->param_count - 1; i >= 0; i--) {
3347 if (MONO_TYPE_ISSTRUCT (cmethod->signature->params [i]))
3352 for (i = 0; i < n; ++i) {
3353 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3355 MONO_ADD_INS (bblock, ins);
3357 MONO_INST_NEW (cfg, ins, CEE_BR);
3359 MONO_ADD_INS (bblock, ins);
3360 tblock = start_bblock->out_bb [0];
3361 link_bblock (cfg, bblock, tblock);
3362 ins->inst_target_bb = tblock;
3363 start_new_bblock = 1;
3366 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3367 /* just create a dummy - the value is never used */
3368 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
3369 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3377 if (*ip == CEE_CALLI) {
3379 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
3380 NEW_TEMPLOAD (cfg, *sp, temp);
3384 } else if (array_rank) {
3387 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
3388 if (sp [fsig->param_count]->type == STACK_OBJ) {
3389 MonoInst *iargs [2];
3390 MonoInst *array, *to_store, *store;
3392 handle_loaded_temps (cfg, bblock, stack_start, sp);
3394 array = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
3395 NEW_TEMPSTORE (cfg, store, array->inst_c0, sp [0]);
3396 store->cil_code = ip;
3397 MONO_ADD_INS (bblock, store);
3398 NEW_TEMPLOAD (cfg, iargs [0], array->inst_c0);
3400 to_store = mono_compile_create_var (cfg, type_from_stack_type (sp [fsig->param_count]), OP_LOCAL);
3401 NEW_TEMPSTORE (cfg, store, to_store->inst_c0, sp [fsig->param_count]);
3402 store->cil_code = ip;
3403 MONO_ADD_INS (bblock, store);
3404 NEW_TEMPLOAD (cfg, iargs [1], to_store->inst_c0);
3407 * We first save the args for the call so that the args are copied to the stack
3408 * and a new instruction tree for them is created. If we don't do this,
3409 * the same MonoInst is added to two different trees and this is not
3412 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref_check, iargs, ip);
3414 NEW_TEMPLOAD (cfg, sp [0], array->inst_c0);
3415 NEW_TEMPLOAD (cfg, sp [fsig->param_count], to_store->inst_c0);
3418 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
3419 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
3421 if (ins->opcode == CEE_STOBJ) {
3422 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
3424 MONO_ADD_INS (bblock, ins);
3427 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
3428 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3429 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
3433 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
3434 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3437 g_assert_not_reached ();
3441 if (0 && CODE_IS_STLOC (ip + 5) && (!MONO_TYPE_ISSTRUCT (fsig->ret)) && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)) {
3442 /* no need to spill */
3443 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
3446 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) {
3447 NEW_TEMPLOAD (cfg, *sp, temp);
3457 if (cfg->method != method) {
3458 /* return from inlined methode */
3463 //g_assert (returnvar != -1);
3464 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
3465 store->cil_code = sp [0]->cil_code;
3466 if (store->opcode == CEE_STOBJ) {
3467 g_assert_not_reached ();
3468 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
3469 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
3471 MONO_ADD_INS (bblock, store);
3475 g_assert (!return_var);
3478 MONO_INST_NEW (cfg, ins, CEE_NOP);
3479 ins->opcode = mono_type_to_stind (method->signature->ret);
3480 if (ins->opcode == CEE_STOBJ) {
3481 NEW_RETLOADA (cfg, ins);
3482 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3484 ins->opcode = OP_SETRET;
3486 ins->inst_i0 = *sp;;
3487 ins->inst_i1 = NULL;
3488 MONO_ADD_INS (bblock, ins);
3492 if (sp != stack_start)
3494 MONO_INST_NEW (cfg, ins, CEE_BR);
3495 ins->cil_code = ip++;
3496 ins->inst_target_bb = end_bblock;
3497 MONO_ADD_INS (bblock, ins);
3498 link_bblock (cfg, bblock, end_bblock);
3499 start_new_bblock = 1;
3503 MONO_INST_NEW (cfg, ins, CEE_BR);
3504 ins->cil_code = ip++;
3505 MONO_ADD_INS (bblock, ins);
3506 target = ip + 1 + (signed char)(*ip);
3508 GET_BBLOCK (cfg, bbhash, tblock, target);
3509 link_bblock (cfg, bblock, tblock);
3510 CHECK_BBLOCK (target, ip, tblock);
3511 ins->inst_target_bb = tblock;
3512 if (sp != stack_start) {
3513 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3516 start_new_bblock = 1;
3523 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3524 ins->cil_code = ip++;
3525 target = ip + 1 + *(signed char*)ip;
3527 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
3528 if (sp != stack_start) {
3529 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3546 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3547 ins->cil_code = ip++;
3548 target = ip + 1 + *(signed char*)ip;
3551 if (sp != stack_start) {
3552 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3559 MONO_INST_NEW (cfg, ins, CEE_BR);
3560 ins->cil_code = ip++;
3561 MONO_ADD_INS (bblock, ins);
3562 target = ip + 4 + (gint32)read32(ip);
3564 GET_BBLOCK (cfg, bbhash, tblock, target);
3565 link_bblock (cfg, bblock, tblock);
3566 CHECK_BBLOCK (target, ip, tblock);
3567 ins->inst_target_bb = tblock;
3568 if (sp != stack_start) {
3569 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3572 start_new_bblock = 1;
3579 MONO_INST_NEW (cfg, ins, *ip);
3580 ins->cil_code = ip++;
3581 target = ip + 4 + (gint32)read32(ip);
3583 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
3584 if (sp != stack_start) {
3585 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3602 MONO_INST_NEW (cfg, ins, *ip);
3603 ins->cil_code = ip++;
3604 target = ip + 4 + (gint32)read32(ip);
3607 if (sp != stack_start) {
3608 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3616 n = read32 (ip + 1);
3617 MONO_INST_NEW (cfg, ins, *ip);
3619 ins->inst_left = *sp;
3620 if (ins->inst_left->type != STACK_I4) goto unverified;
3623 CHECK_OPSIZE (n * sizeof (guint32));
3624 target = ip + n * sizeof (guint32);
3625 MONO_ADD_INS (bblock, ins);
3626 GET_BBLOCK (cfg, bbhash, tblock, target);
3627 link_bblock (cfg, bblock, tblock);
3628 ins->klass = GUINT_TO_POINTER (n);
3629 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
3630 ins->inst_many_bb [n] = tblock;
3632 for (i = 0; i < n; ++i) {
3633 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
3634 link_bblock (cfg, bblock, tblock);
3635 ins->inst_many_bb [i] = tblock;
3638 if (sp != stack_start) {
3639 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3656 MONO_INST_NEW (cfg, ins, *ip);
3661 ins->type = ldind_type [*ip - CEE_LDIND_I1];
3662 ins->flags |= ins_flag;
3674 MONO_INST_NEW (cfg, ins, *ip);
3675 ins->cil_code = ip++;
3677 handle_loaded_temps (cfg, bblock, stack_start, sp);
3678 MONO_ADD_INS (bblock, ins);
3679 ins->inst_i0 = sp [0];
3680 ins->inst_i1 = sp [1];
3681 ins->flags |= ins_flag;
3700 if (mono_find_jit_opcode_emulation (ins->opcode)) {
3701 MonoInst *store, *temp, *load;
3703 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3704 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3705 store->cil_code = ins->cil_code;
3706 MONO_ADD_INS (bblock, store);
3707 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
3708 load->cil_code = ins->cil_code;
3710 /*g_print ("found emulation for %d\n", ins->opcode);*/
3724 case CEE_CONV_OVF_I8:
3725 case CEE_CONV_OVF_U8:
3729 if (mono_find_jit_opcode_emulation (ins->opcode)) {
3730 MonoInst *store, *temp, *load;
3732 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3733 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3734 store->cil_code = ins->cil_code;
3735 MONO_ADD_INS (bblock, store);
3736 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
3737 load->cil_code = ins->cil_code;
3739 /*g_print ("found emulation for %d\n", ins->opcode);*/
3743 case CEE_CONV_OVF_I4:
3744 case CEE_CONV_OVF_I1:
3745 case CEE_CONV_OVF_I2:
3746 case CEE_CONV_OVF_I:
3747 case CEE_CONV_OVF_U:
3750 if (sp [-1]->type == STACK_R8) {
3751 ADD_UNOP (CEE_CONV_OVF_I8);
3759 case CEE_CONV_OVF_U1:
3760 case CEE_CONV_OVF_U2:
3761 case CEE_CONV_OVF_U4:
3764 if (sp [-1]->type == STACK_R8) {
3765 ADD_UNOP (CEE_CONV_OVF_U8);
3773 case CEE_CONV_OVF_I1_UN:
3774 case CEE_CONV_OVF_I2_UN:
3775 case CEE_CONV_OVF_I4_UN:
3776 case CEE_CONV_OVF_I8_UN:
3777 case CEE_CONV_OVF_U1_UN:
3778 case CEE_CONV_OVF_U2_UN:
3779 case CEE_CONV_OVF_U4_UN:
3780 case CEE_CONV_OVF_U8_UN:
3781 case CEE_CONV_OVF_I_UN:
3782 case CEE_CONV_OVF_U_UN:
3790 token = read32 (ip + 1);
3791 if (method->wrapper_type != MONO_WRAPPER_NONE)
3792 klass = mono_method_get_wrapper_data (method, token);
3794 klass = mono_class_get_full (image, token, generic_context);
3796 mono_class_init (klass);
3798 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3799 MonoInst *store, *load;
3800 MONO_INST_NEW (cfg, load, CEE_LDIND_REF);
3801 load->cil_code = ip;
3802 load->inst_i0 = sp [1];
3803 load->type = ldind_type [CEE_LDIND_REF];
3804 load->flags |= ins_flag;
3805 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
3806 store->cil_code = ip;
3807 handle_loaded_temps (cfg, bblock, stack_start, sp);
3808 MONO_ADD_INS (bblock, store);
3809 store->inst_i0 = sp [0];
3810 store->inst_i1 = load;
3811 store->flags |= ins_flag;
3813 n = mono_class_value_size (klass, NULL);
3814 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3816 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3817 copy->inst_left = sp [0];
3818 copy->inst_right = sp [1];
3819 copy->cil_code = ip;
3821 MONO_ADD_INS (bblock, copy);
3823 MonoInst *iargs [3];
3826 NEW_ICONST (cfg, iargs [2], n);
3827 iargs [2]->cil_code = ip;
3829 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3836 MonoInst *iargs [3];
3840 token = read32 (ip + 1);
3841 if (method->wrapper_type != MONO_WRAPPER_NONE)
3842 klass = mono_method_get_wrapper_data (method, token);
3844 klass = mono_class_get_full (image, token, generic_context);
3846 mono_class_init (klass);
3847 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3848 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3850 ins->inst_i0 = sp [0];
3851 ins->type = ldind_type [CEE_LDIND_REF];
3852 ins->flags |= ins_flag;
3858 n = mono_class_value_size (klass, NULL);
3859 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3860 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3861 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3863 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3864 copy->inst_left = iargs [0];
3865 copy->inst_right = *sp;
3866 copy->cil_code = ip;
3868 MONO_ADD_INS (bblock, copy);
3871 NEW_ICONST (cfg, iargs [2], n);
3872 iargs [2]->cil_code = ip;
3874 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3876 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3884 CHECK_STACK_OVF (1);
3886 n = read32 (ip + 1);
3888 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3890 MonoInst *iargs [1];
3892 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
3893 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
3894 NEW_TEMPLOAD (cfg, *sp, temp);
3898 if (cfg->opt & MONO_OPT_SHARED) {
3900 MonoInst *iargs [3];
3902 if (mono_compile_aot) {
3903 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n);
3906 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3907 NEW_IMAGECONST (cfg, iargs [1], image);
3908 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
3909 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
3910 NEW_TEMPLOAD (cfg, *sp, temp);
3911 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3913 if (mono_compile_aot)
3914 NEW_LDSTRCONST (cfg, ins, image, n);
3916 NEW_PCONST (cfg, ins, NULL);
3918 ins->type = STACK_OBJ;
3919 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3929 MonoInst *iargs [2];
3930 MonoMethodSignature *fsig;
3934 token = read32 (ip + 1);
3935 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3936 cmethod = mono_method_get_wrapper_data (method, token);
3938 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3939 fsig = mono_method_get_signature (cmethod, image, token);
3941 mono_class_init (cmethod->klass);
3943 n = fsig->param_count;
3946 /* move the args to allow room for 'this' in the first position */
3952 handle_loaded_temps (cfg, bblock, stack_start, sp);
3955 if (cmethod->klass->parent == mono_defaults.array_class) {
3956 NEW_METHODCONST (cfg, *sp, cmethod);
3957 temp = mono_emit_native_call (cfg, bblock, mono_array_new_va, fsig, sp, ip, FALSE);
3959 } else if (cmethod->string_ctor) {
3960 /* we simply pass a null pointer */
3961 NEW_PCONST (cfg, *sp, NULL);
3962 /* now call the string ctor */
3963 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
3965 if (cmethod->klass->valuetype) {
3966 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
3967 temp = iargs [0]->inst_c0;
3968 NEW_TEMPLOADA (cfg, *sp, temp);
3970 if (cfg->opt & MONO_OPT_SHARED) {
3971 NEW_DOMAINCONST (cfg, iargs [0]);
3972 NEW_CLASSCONST (cfg, iargs [1], cmethod->klass);
3974 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3976 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
3977 NEW_VTABLECONST (cfg, iargs [0], vtable);
3978 if (cmethod->klass->has_finalize || cmethod->klass->marshalbyref || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
3979 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
3981 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
3983 NEW_TEMPLOAD (cfg, *sp, temp);
3986 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3987 mono_method_check_inlining (cfg, cmethod) &&
3988 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
3989 !g_list_find (dont_inline, cmethod)) {
3991 MonoBasicBlock *ebblock;
3992 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3997 GET_BBLOCK (cfg, bbhash, bblock, ip);
3998 ebblock->next_bb = bblock;
3999 link_bblock (cfg, ebblock, bblock);
4001 NEW_TEMPLOAD (cfg, *sp, temp);
4004 /* indicates start of a new block, and triggers a load
4005 of all stack arguments at bb boundarie */
4008 inline_costs += costs;
4012 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
4015 /* now call the actual ctor */
4016 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
4020 NEW_TEMPLOAD (cfg, *sp, temp);
4031 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4032 mono_class_init (klass);
4034 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4036 MonoMethod *mono_isinst;
4037 MonoInst *iargs [1];
4038 MonoBasicBlock *ebblock;
4042 mono_isinst = mono_marshal_get_isinst (klass);
4045 costs = inline_method (cfg, mono_isinst, mono_isinst->signature, bblock,
4046 iargs, ip, real_offset, dont_inline, &ebblock);
4048 g_assert (costs > 0);
4053 GET_BBLOCK (cfg, bbhash, bblock, ip);
4054 ebblock->next_bb = bblock;
4055 link_bblock (cfg, ebblock, bblock);
4057 temp = iargs [0]->inst_i0->inst_c0;
4058 NEW_TEMPLOAD (cfg, *sp, temp);
4062 inline_costs += costs;
4066 MONO_INST_NEW (cfg, ins, *ip);
4067 ins->type = STACK_OBJ;
4068 ins->inst_left = *sp;
4069 ins->inst_newa_class = klass;
4075 case CEE_UNBOX_ANY: {
4076 MonoInst *add, *vtoffset;
4077 MonoInst *iargs [3];
4082 token = read32 (ip + 1);
4083 if (method->wrapper_type != MONO_WRAPPER_NONE)
4084 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4086 klass = mono_class_get_full (image, token, generic_context);
4087 mono_class_init (klass);
4089 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4091 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4092 MonoMethod *mono_castclass;
4093 MonoInst *iargs [1];
4094 MonoBasicBlock *ebblock;
4098 mono_castclass = mono_marshal_get_castclass (klass);
4101 costs = inline_method (cfg, mono_castclass, mono_castclass->signature, bblock,
4102 iargs, ip, real_offset, dont_inline, &ebblock);
4104 g_assert (costs > 0);
4109 GET_BBLOCK (cfg, bbhash, bblock, ip);
4110 ebblock->next_bb = bblock;
4111 link_bblock (cfg, ebblock, bblock);
4113 temp = iargs [0]->inst_i0->inst_c0;
4114 NEW_TEMPLOAD (cfg, *sp, temp);
4118 inline_costs += costs;
4121 MONO_INST_NEW (cfg, ins, CEE_CASTCLASS);
4122 ins->type = STACK_OBJ;
4123 ins->inst_left = *sp;
4125 ins->inst_newa_class = klass;
4133 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
4134 ins->type = STACK_OBJ;
4135 ins->inst_left = *sp;
4137 ins->inst_newa_class = klass;
4140 MONO_INST_NEW (cfg, add, CEE_ADD);
4141 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4142 add->inst_left = ins;
4143 add->inst_right = vtoffset;
4144 add->type = STACK_MP;
4148 n = mono_class_value_size (klass, NULL);
4149 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
4150 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
4151 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
4153 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
4154 copy->inst_left = iargs [0];
4155 copy->inst_right = *sp;
4156 copy->cil_code = ip;
4158 MONO_ADD_INS (bblock, copy);
4161 NEW_ICONST (cfg, iargs [2], n);
4162 iargs [2]->cil_code = ip;
4164 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
4166 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
4172 MonoInst *add, *vtoffset;
4177 token = read32 (ip + 1);
4178 if (method->wrapper_type != MONO_WRAPPER_NONE)
4179 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4181 klass = mono_class_get_full (image, token, generic_context);
4182 mono_class_init (klass);
4184 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
4185 ins->type = STACK_OBJ;
4186 ins->inst_left = *sp;
4188 ins->inst_newa_class = klass;
4191 MONO_INST_NEW (cfg, add, CEE_ADD);
4192 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4193 add->inst_left = ins;
4194 add->inst_right = vtoffset;
4195 add->type = STACK_MP;
4205 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4206 mono_class_init (klass);
4208 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4210 MonoMethod *mono_castclass;
4211 MonoInst *iargs [1];
4212 MonoBasicBlock *ebblock;
4216 mono_castclass = mono_marshal_get_castclass (klass);
4219 costs = inline_method (cfg, mono_castclass, mono_castclass->signature, bblock,
4220 iargs, ip, real_offset, dont_inline, &ebblock);
4222 g_assert (costs > 0);
4227 GET_BBLOCK (cfg, bbhash, bblock, ip);
4228 ebblock->next_bb = bblock;
4229 link_bblock (cfg, ebblock, bblock);
4231 temp = iargs [0]->inst_i0->inst_c0;
4232 NEW_TEMPLOAD (cfg, *sp, temp);
4236 inline_costs += costs;
4239 MONO_INST_NEW (cfg, ins, *ip);
4240 ins->type = STACK_OBJ;
4241 ins->inst_left = *sp;
4243 ins->inst_newa_class = klass;
4251 MONO_INST_NEW (cfg, ins, *ip);
4253 ins->inst_left = *sp;
4254 ins->cil_code = ip++;
4255 MONO_ADD_INS (bblock, ins);
4257 start_new_bblock = 1;
4262 MonoInst *offset_ins;
4263 MonoClassField *field;
4264 MonoBasicBlock *ebblock;
4268 if (*ip == CEE_STFLD) {
4275 // FIXME: enable this test later.
4276 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
4279 token = read32 (ip + 1);
4280 field = mono_field_from_token (image, token, &klass, generic_context);
4281 mono_class_init (klass);
4283 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
4284 /* FIXME: mark instructions for use in SSA */
4285 if (*ip == CEE_STFLD) {
4286 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound) {
4287 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
4288 MonoInst *iargs [5];
4291 NEW_CLASSCONST (cfg, iargs [1], klass);
4292 NEW_FIELDCONST (cfg, iargs [2], field);
4293 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
4297 if (cfg->opt & MONO_OPT_INLINE) {
4298 costs = inline_method (cfg, stfld_wrapper, stfld_wrapper->signature, bblock,
4299 iargs, ip, real_offset, dont_inline, &ebblock);
4300 g_assert (costs > 0);
4305 GET_BBLOCK (cfg, bbhash, bblock, ip);
4306 ebblock->next_bb = bblock;
4307 link_bblock (cfg, ebblock, bblock);
4309 /* indicates start of a new block, and triggers a load
4310 of all stack arguments at bb boundarie */
4313 inline_costs += costs;
4316 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, stfld_wrapper->signature, iargs, ip, NULL);
4320 NEW_ICONST (cfg, offset_ins, foffset);
4321 MONO_INST_NEW (cfg, ins, CEE_ADD);
4323 ins->inst_left = *sp;
4324 ins->inst_right = offset_ins;
4325 ins->type = STACK_MP;
4327 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4328 store->cil_code = ip;
4329 store->inst_left = ins;
4330 store->inst_right = sp [1];
4331 handle_loaded_temps (cfg, bblock, stack_start, sp);
4332 store->flags |= ins_flag;
4334 if (store->opcode == CEE_STOBJ) {
4335 handle_stobj (cfg, bblock, ins, sp [1], ip,
4336 mono_class_from_mono_type (field->type), FALSE, FALSE);
4338 MONO_ADD_INS (bblock, store);
4341 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound) {
4342 /* fixme: we need to inline that call somehow */
4343 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
4344 MonoInst *iargs [4];
4348 NEW_CLASSCONST (cfg, iargs [1], klass);
4349 NEW_FIELDCONST (cfg, iargs [2], field);
4350 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
4351 if (cfg->opt & MONO_OPT_INLINE) {
4352 costs = inline_method (cfg, ldfld_wrapper, ldfld_wrapper->signature, bblock,
4353 iargs, ip, real_offset, dont_inline, &ebblock);
4354 g_assert (costs > 0);
4359 GET_BBLOCK (cfg, bbhash, bblock, ip);
4360 ebblock->next_bb = bblock;
4361 link_bblock (cfg, ebblock, bblock);
4363 temp = iargs [0]->inst_i0->inst_c0;
4365 if (*ip == CEE_LDFLDA) {
4366 /* not sure howto handle this */
4367 NEW_TEMPLOADA (cfg, *sp, temp);
4369 NEW_TEMPLOAD (cfg, *sp, temp);
4373 /* indicates start of a new block, and triggers a load of
4374 all stack arguments at bb boundarie */
4377 inline_costs += costs;
4380 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, ldfld_wrapper->signature, iargs, ip, NULL);
4381 if (*ip == CEE_LDFLDA) {
4382 /* not sure howto handle this */
4383 NEW_TEMPLOADA (cfg, *sp, temp);
4385 NEW_TEMPLOAD (cfg, *sp, temp);
4390 NEW_ICONST (cfg, offset_ins, foffset);
4391 MONO_INST_NEW (cfg, ins, CEE_ADD);
4393 ins->inst_left = *sp;
4394 ins->inst_right = offset_ins;
4395 ins->type = STACK_MP;
4397 if (*ip == CEE_LDFLDA) {
4401 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4402 type_to_eval_stack_type (field->type, load);
4403 load->cil_code = ip;
4404 load->inst_left = ins;
4405 load->flags |= ins_flag;
4417 MonoClassField *field;
4418 gpointer addr = NULL;
4421 token = read32 (ip + 1);
4423 field = mono_field_from_token (image, token, &klass, generic_context);
4424 mono_class_init (klass);
4426 if ((*ip) == CEE_STSFLD)
4427 handle_loaded_temps (cfg, bblock, stack_start, sp);
4429 if (cfg->domain->special_static_fields)
4430 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
4432 if ((cfg->opt & MONO_OPT_SHARED) || (mono_compile_aot && addr)) {
4434 MonoInst *iargs [2];
4435 g_assert (field->parent);
4436 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
4437 NEW_FIELDCONST (cfg, iargs [1], field);
4438 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
4439 NEW_TEMPLOAD (cfg, ins, temp);
4442 vtable = mono_class_vtable (cfg->domain, klass);
4444 if ((!vtable->initialized || mono_compile_aot) && !(klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (klass, method)) {
4445 guint8 *tramp = mono_create_class_init_trampoline (vtable);
4446 mono_emit_native_call (cfg, bblock, tramp,
4447 helper_sig_class_init_trampoline,
4449 if (cfg->verbose_level > 2)
4450 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
4452 if (cfg->run_cctors)
4453 mono_runtime_class_init (vtable);
4455 addr = (char*)vtable->data + field->offset;
4457 if (mono_compile_aot)
4458 NEW_SFLDACONST (cfg, ins, field);
4460 NEW_PCONST (cfg, ins, addr);
4464 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
4465 * This could be later optimized to do just a couple of
4466 * memory dereferences with constant offsets.
4469 MonoInst *iargs [1];
4470 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
4471 temp = mono_emit_jit_icall (cfg, bblock, mono_get_special_static_data, iargs, ip);
4472 NEW_TEMPLOAD (cfg, ins, temp);
4476 /* FIXME: mark instructions for use in SSA */
4477 if (*ip == CEE_LDSFLDA) {
4479 } else if (*ip == CEE_STSFLD) {
4483 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4484 store->cil_code = ip;
4485 store->inst_left = ins;
4486 store->inst_right = sp [0];
4487 store->flags |= ins_flag;
4490 if (store->opcode == CEE_STOBJ) {
4491 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
4493 MONO_ADD_INS (bblock, store);
4495 gboolean is_const = FALSE;
4496 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4497 if (!((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) &&
4498 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
4499 gpointer addr = (char*)vtable->data + field->offset;
4500 /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
4502 switch (field->type->type) {
4503 case MONO_TYPE_BOOLEAN:
4505 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
4509 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
4512 case MONO_TYPE_CHAR:
4514 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
4518 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
4523 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
4527 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
4532 case MONO_TYPE_STRING:
4533 case MONO_TYPE_OBJECT:
4534 case MONO_TYPE_CLASS:
4535 case MONO_TYPE_SZARRAY:
4537 case MONO_TYPE_FNPTR:
4538 case MONO_TYPE_ARRAY:
4539 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
4540 type_to_eval_stack_type (field->type, *sp);
4545 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
4546 sp [0]->type = STACK_I8;
4547 sp [0]->inst_l = *((gint64 *)addr);
4552 case MONO_TYPE_VALUETYPE:
4561 CHECK_STACK_OVF (1);
4562 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4563 type_to_eval_stack_type (field->type, load);
4564 load->cil_code = ip;
4565 load->inst_left = ins;
4567 load->flags |= ins_flag;
4569 /* fixme: dont see the problem why this does not work */
4570 //cfg->disable_aot = TRUE;
4580 token = read32 (ip + 1);
4581 if (method->wrapper_type != MONO_WRAPPER_NONE)
4582 klass = mono_method_get_wrapper_data (method, token);
4584 klass = mono_class_get_full (image, token, generic_context);
4585 mono_class_init (klass);
4586 n = mono_type_to_stind (&klass->byval_arg);
4587 if (n == CEE_STOBJ) {
4588 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
4590 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
4592 MONO_INST_NEW (cfg, store, n);
4593 store->cil_code = ip;
4594 store->inst_left = sp [0];
4595 store->inst_right = sp [1];
4596 store->flags |= ins_flag;
4597 MONO_ADD_INS (bblock, store);
4609 token = read32 (ip + 1);
4610 if (method->wrapper_type != MONO_WRAPPER_NONE)
4611 klass = mono_method_get_wrapper_data (method, token);
4613 klass = mono_class_get_full (image, token, generic_context);
4614 mono_class_init (klass);
4616 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4621 *sp++ = handle_box (cfg, bblock, val, ip, klass);
4628 MONO_INST_NEW (cfg, ins, *ip);
4633 token = read32 (ip + 1);
4635 /* allocate the domainvar - becaus this is used in decompose_foreach */
4636 if (cfg->opt & MONO_OPT_SHARED)
4637 mono_get_domainvar (cfg);
4639 if (method->wrapper_type != MONO_WRAPPER_NONE)
4640 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4642 klass = mono_class_get_full (image, token, generic_context);
4644 mono_class_init (klass);
4645 ins->inst_newa_class = klass;
4646 ins->inst_newa_len = *sp;
4647 ins->type = STACK_OBJ;
4651 * we store the object so calls to create the array are not interleaved
4652 * with the arguments of other calls.
4655 MonoInst *store, *temp, *load;
4657 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
4658 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4659 store->cil_code = ins->cil_code;
4660 MONO_ADD_INS (bblock, store);
4661 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
4662 load->cil_code = ins->cil_code;
4669 MONO_INST_NEW (cfg, ins, *ip);
4670 ins->cil_code = ip++;
4672 ins->inst_left = *sp;
4673 ins->type = STACK_PTR;
4681 if (method->wrapper_type != MONO_WRAPPER_NONE)
4682 klass = (MonoClass*)mono_method_get_wrapper_data (method, read32 (ip + 1));
4684 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4685 mono_class_init (klass);
4686 NEW_LDELEMA (cfg, ins, sp, klass);
4696 token = read32 (ip + 1);
4697 klass = mono_class_get_full (image, token, generic_context);
4698 mono_class_init (klass);
4699 NEW_LDELEMA (cfg, load, sp, klass);
4700 load->cil_code = ip;
4701 MONO_INST_NEW (cfg, ins, mono_type_to_ldind (&klass->byval_arg));
4703 ins->inst_left = load;
4705 type_to_eval_stack_type (&klass->byval_arg, ins);
4719 case CEE_LDELEM_REF: {
4723 * ldind.x (ldelema (array, index))
4724 * ldelema does the bounds check
4728 klass = array_access_to_klass (*ip);
4729 NEW_LDELEMA (cfg, load, sp, klass);
4730 load->cil_code = ip;
4731 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
4733 ins->inst_left = load;
4735 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
4745 case CEE_STELEM_R8: {
4749 * stind.x (ldelema (array, index), val)
4750 * ldelema does the bounds check
4754 klass = array_access_to_klass (*ip);
4755 NEW_LDELEMA (cfg, load, sp, klass);
4756 load->cil_code = ip;
4757 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
4759 ins->inst_left = load;
4760 ins->inst_right = sp [2];
4762 handle_loaded_temps (cfg, bblock, stack_start, sp);
4763 MONO_ADD_INS (bblock, ins);
4771 * stind.x (ldelema (array, index), val)
4772 * ldelema does the bounds check
4777 token = read32 (ip + 1);
4778 klass = mono_class_get_full (image, token, generic_context);
4779 mono_class_init (klass);
4780 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4781 MonoInst *iargs [3];
4782 handle_loaded_temps (cfg, bblock, stack_start, sp);
4788 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4790 NEW_LDELEMA (cfg, load, sp, klass);
4791 load->cil_code = ip;
4793 n = mono_type_to_stind (&klass->byval_arg);
4795 handle_stobj (cfg, bblock, load, sp [2], ip, klass, FALSE, FALSE);
4797 MONO_INST_NEW (cfg, ins, n);
4799 ins->inst_left = load;
4800 ins->inst_right = sp [2];
4801 handle_loaded_temps (cfg, bblock, stack_start, sp);
4802 MONO_ADD_INS (bblock, ins);
4809 case CEE_STELEM_REF: {
4810 MonoInst *iargs [3];
4815 handle_loaded_temps (cfg, bblock, stack_start, sp);
4821 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4825 NEW_GROUP (cfg, group, sp [0], sp [1]);
4826 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
4828 ins->inst_left = group;
4829 ins->inst_right = sp [2];
4830 MONO_ADD_INS (bblock, ins);
4837 case CEE_CKFINITE: {
4838 MonoInst *store, *temp;
4841 /* this instr. can throw exceptions as side effect,
4842 * so we cant eliminate dead code which contains CKFINITE opdodes.
4843 * Spilling to memory makes sure that we always perform
4847 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
4849 ins->inst_left = sp [-1];
4850 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
4852 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4853 store->cil_code = ip;
4854 MONO_ADD_INS (bblock, store);
4856 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
4863 MONO_INST_NEW (cfg, ins, *ip);
4866 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4867 mono_class_init (klass);
4868 ins->type = STACK_MP;
4869 ins->inst_left = *sp;
4871 ins->inst_newa_class = klass;
4876 case CEE_MKREFANY: {
4877 MonoInst *loc, *klassconst;
4880 MONO_INST_NEW (cfg, ins, *ip);
4883 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4884 mono_class_init (klass);
4887 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
4888 NEW_TEMPLOADA (cfg, ins->inst_right, loc->inst_c0);
4890 NEW_PCONST (cfg, klassconst, klass);
4891 NEW_GROUP (cfg, ins->inst_left, *sp, klassconst);
4893 MONO_ADD_INS (bblock, ins);
4895 NEW_TEMPLOAD (cfg, *sp, loc->inst_c0);
4902 MonoClass *handle_class;
4904 CHECK_STACK_OVF (1);
4907 n = read32 (ip + 1);
4909 handle = mono_ldtoken (image, n, &handle_class, generic_context);
4910 mono_class_init (handle_class);
4912 if (cfg->opt & MONO_OPT_SHARED) {
4914 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
4916 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
4918 NEW_IMAGECONST (cfg, iargs [0], image);
4919 NEW_ICONST (cfg, iargs [1], n);
4920 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
4921 NEW_TEMPLOAD (cfg, res, temp);
4922 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4923 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
4924 MONO_ADD_INS (bblock, store);
4925 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4927 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method_full (image, read32 (ip + 6), NULL, generic_context)) &&
4928 (cmethod->klass == mono_defaults.monotype_class->parent) &&
4929 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
4930 MonoClass *tclass = mono_class_from_mono_type (handle);
4931 mono_class_init (tclass);
4932 if (mono_compile_aot)
4933 NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n);
4935 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
4936 ins->type = STACK_OBJ;
4937 ins->klass = cmethod->klass;
4940 if (mono_compile_aot)
4941 NEW_LDTOKENCONST (cfg, ins, image, n);
4943 NEW_PCONST (cfg, ins, handle);
4944 ins->type = STACK_VTYPE;
4945 ins->klass = handle_class;
4961 case CEE_ADD_OVF_UN:
4963 case CEE_MUL_OVF_UN:
4965 case CEE_SUB_OVF_UN:
4970 case CEE_ENDFINALLY:
4971 MONO_INST_NEW (cfg, ins, *ip);
4972 MONO_ADD_INS (bblock, ins);
4973 ins->cil_code = ip++;
4974 start_new_bblock = 1;
4977 * Control will leave the method so empty the stack, otherwise
4978 * the next basic block will start with a nonempty stack.
4980 while (sp != stack_start) {
4981 MONO_INST_NEW (cfg, ins, CEE_POP);
4985 MONO_ADD_INS (bblock, ins);
4991 if (*ip == CEE_LEAVE) {
4993 target = ip + 5 + (gint32)read32(ip + 1);
4996 target = ip + 2 + (signed char)(ip [1]);
4999 /* empty the stack */
5000 while (sp != stack_start) {
5001 MONO_INST_NEW (cfg, ins, CEE_POP);
5005 MONO_ADD_INS (bblock, ins);
5009 * If this leave statement is in a catch block, check for a
5010 * pending exception, and rethrow it if necessary.
5012 for (i = 0; i < header->num_clauses; ++i) {
5013 MonoExceptionClause *clause = &header->clauses [i];
5014 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code)) {
5017 temp = mono_emit_jit_icall (cfg, bblock, mono_thread_get_pending_exception, NULL, ip);
5018 NEW_TEMPLOAD (cfg, *sp, temp);
5020 MONO_INST_NEW (cfg, ins, OP_THROW_OR_NULL);
5021 ins->inst_left = *sp;
5023 MONO_ADD_INS (bblock, ins);
5027 /* fixme: call fault handler ? */
5029 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
5031 for (tmp = handlers; tmp; tmp = tmp->next) {
5033 link_bblock (cfg, bblock, tblock);
5034 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
5036 ins->inst_target_bb = tblock;
5037 MONO_ADD_INS (bblock, ins);
5039 g_list_free (handlers);
5042 MONO_INST_NEW (cfg, ins, CEE_BR);
5044 MONO_ADD_INS (bblock, ins);
5045 GET_BBLOCK (cfg, bbhash, tblock, target);
5046 link_bblock (cfg, bblock, tblock);
5047 CHECK_BBLOCK (target, ip, tblock);
5048 ins->inst_target_bb = tblock;
5049 start_new_bblock = 1;
5051 if (*ip == CEE_LEAVE)
5060 MONO_INST_NEW (cfg, ins, *ip);
5062 handle_loaded_temps (cfg, bblock, stack_start, sp);
5063 MONO_ADD_INS (bblock, ins);
5064 ins->cil_code = ip++;
5065 ins->inst_i0 = sp [0];
5066 ins->inst_i1 = sp [1];
5074 /* trampoline mono specific opcodes */
5075 case MONO_CUSTOM_PREFIX: {
5077 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
5082 case CEE_MONO_FUNC1: {
5084 gpointer func = NULL;
5090 case MONO_MARSHAL_CONV_STR_LPWSTR:
5091 func = mono_string_to_utf16;
5093 case MONO_MARSHAL_CONV_LPWSTR_STR:
5094 func = mono_string_from_utf16;
5096 case MONO_MARSHAL_CONV_LPSTR_STR:
5097 func = mono_string_new_wrapper;
5099 case MONO_MARSHAL_CONV_STR_LPTSTR:
5100 case MONO_MARSHAL_CONV_STR_LPSTR:
5101 func = mono_string_to_utf8;
5103 case MONO_MARSHAL_CONV_STR_BSTR:
5104 func = mono_string_to_bstr;
5106 case MONO_MARSHAL_CONV_STR_TBSTR:
5107 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
5108 func = mono_string_to_ansibstr;
5110 case MONO_MARSHAL_CONV_SB_LPSTR:
5111 case MONO_MARSHAL_CONV_SB_LPTSTR:
5112 func = mono_string_builder_to_utf8;
5114 case MONO_MARSHAL_CONV_SB_LPWSTR:
5115 func = mono_string_builder_to_utf16;
5117 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
5118 func = mono_array_to_savearray;
5120 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
5121 func = mono_array_to_lparray;
5123 case MONO_MARSHAL_CONV_DEL_FTN:
5124 func = mono_delegate_to_ftnptr;
5126 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
5127 func = mono_marshal_string_array;
5129 case MONO_MARSHAL_CONV_STRARRAY_STRWLPARRAY:
5130 func = mono_marshal_string_array_to_unicode;
5133 g_warning ("unknown conversion %d\n", ip [2]);
5134 g_assert_not_reached ();
5137 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5138 NEW_TEMPLOAD (cfg, *sp, temp);
5142 inline_costs += 10 * num_calls++;
5145 case CEE_MONO_PROC2: {
5146 gpointer func = NULL;
5152 case MONO_MARSHAL_CONV_LPSTR_SB:
5153 case MONO_MARSHAL_CONV_LPTSTR_SB:
5154 func = mono_string_utf8_to_builder;
5156 case MONO_MARSHAL_CONV_LPWSTR_SB:
5157 func = mono_string_utf16_to_builder;
5159 case MONO_MARSHAL_FREE_ARRAY:
5160 func = mono_marshal_free_array;
5163 g_assert_not_reached ();
5166 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5168 inline_costs += 10 * num_calls++;
5171 case CEE_MONO_PROC3: {
5172 gpointer func = NULL;
5178 case MONO_MARSHAL_CONV_STR_BYVALSTR:
5179 func = mono_string_to_byvalstr;
5181 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
5182 func = mono_string_to_byvalwstr;
5185 g_assert_not_reached ();
5188 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5190 inline_costs += 10 * num_calls++;
5196 mono_emit_jit_icall (cfg, bblock, g_free, sp, ip);
5198 inline_costs += 10 * num_calls++;
5200 case CEE_MONO_LDPTR:
5201 CHECK_STACK_OVF (1);
5203 token = read32 (ip + 2);
5204 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
5208 inline_costs += 10 * num_calls++;
5210 case CEE_MONO_VTADDR:
5213 MONO_INST_NEW (cfg, ins, OP_VTADDR);
5215 ins->type = STACK_MP;
5216 ins->inst_left = *sp;
5220 case CEE_MONO_NEWOBJ: {
5221 MonoInst *iargs [2];
5223 CHECK_STACK_OVF (1);
5225 token = read32 (ip + 2);
5226 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5227 mono_class_init (klass);
5228 NEW_DOMAINCONST (cfg, iargs [0]);
5229 NEW_CLASSCONST (cfg, iargs [1], klass);
5230 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
5231 NEW_TEMPLOAD (cfg, *sp, temp);
5234 inline_costs += 10 * num_calls++;
5237 case CEE_MONO_OBJADDR:
5240 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
5242 ins->type = STACK_MP;
5243 ins->inst_left = *sp;
5247 case CEE_MONO_LDNATIVEOBJ:
5250 token = read32 (ip + 2);
5251 klass = mono_method_get_wrapper_data (method, token);
5252 g_assert (klass->valuetype);
5253 mono_class_init (klass);
5254 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
5258 case CEE_MONO_RETOBJ:
5259 g_assert (cfg->ret);
5260 g_assert (method->signature->pinvoke);
5265 token = read32 (ip + 2);
5266 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5268 NEW_RETLOADA (cfg, ins);
5269 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
5271 if (sp != stack_start)
5274 MONO_INST_NEW (cfg, ins, CEE_BR);
5276 ins->inst_target_bb = end_bblock;
5277 MONO_ADD_INS (bblock, ins);
5278 link_bblock (cfg, bblock, end_bblock);
5279 start_new_bblock = 1;
5282 case CEE_MONO_CISINST:
5283 case CEE_MONO_CCASTCLASS: {
5288 token = read32 (ip + 2);
5289 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5290 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_CISINST) ? OP_CISINST : OP_CCASTCLASS);
5291 ins->type = STACK_I4;
5292 ins->inst_left = *sp;
5293 ins->inst_newa_class = klass;
5300 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
5309 /* somewhat similar to LDTOKEN */
5310 MonoInst *addr, *vtvar;
5311 CHECK_STACK_OVF (1);
5312 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
5314 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5315 addr->cil_code = ip;
5316 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
5318 ins->inst_left = addr;
5319 MONO_ADD_INS (bblock, ins);
5320 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5334 * The following transforms:
5335 * CEE_CEQ into OP_CEQ
5336 * CEE_CGT into OP_CGT
5337 * CEE_CGT_UN into OP_CGT_UN
5338 * CEE_CLT into OP_CLT
5339 * CEE_CLT_UN into OP_CLT_UN
5341 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
5343 MONO_INST_NEW (cfg, ins, cmp->opcode);
5345 cmp->inst_i0 = sp [0];
5346 cmp->inst_i1 = sp [1];
5350 cmp->opcode = OP_COMPARE;
5352 ins->type = STACK_I4;
5362 CHECK_STACK_OVF (1);
5364 n = read32 (ip + 2);
5365 if (method->wrapper_type != MONO_WRAPPER_NONE)
5366 cmethod = mono_method_get_wrapper_data (method, n);
5368 cmethod = mono_get_method_full (image, n, NULL, generic_context);
5371 mono_class_init (cmethod->klass);
5372 handle_loaded_temps (cfg, bblock, stack_start, sp);
5374 NEW_METHODCONST (cfg, argconst, cmethod);
5375 if (method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED)
5376 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
5378 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn_nosync, &argconst, ip);
5379 NEW_TEMPLOAD (cfg, *sp, temp);
5383 inline_costs += 10 * num_calls++;
5386 case CEE_LDVIRTFTN: {
5392 n = read32 (ip + 2);
5393 if (method->wrapper_type != MONO_WRAPPER_NONE)
5394 cmethod = mono_method_get_wrapper_data (method, n);
5396 cmethod = mono_get_method_full (image, n, NULL, generic_context);
5398 mono_class_init (cmethod->klass);
5399 handle_loaded_temps (cfg, bblock, stack_start, sp);
5403 NEW_METHODCONST (cfg, args [1], cmethod);
5404 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
5405 NEW_TEMPLOAD (cfg, *sp, temp);
5409 inline_costs += 10 * num_calls++;
5413 CHECK_STACK_OVF (1);
5415 n = read16 (ip + 2);
5417 NEW_ARGLOAD (cfg, ins, n);
5423 CHECK_STACK_OVF (1);
5425 n = read16 (ip + 2);
5427 NEW_ARGLOADA (cfg, ins, n);
5435 handle_loaded_temps (cfg, bblock, stack_start, sp);
5437 n = read16 (ip + 2);
5439 NEW_ARGSTORE (cfg, ins, n, *sp);
5441 if (ins->opcode == CEE_STOBJ) {
5442 NEW_ARGLOADA (cfg, ins, n);
5443 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5445 MONO_ADD_INS (bblock, ins);
5449 CHECK_STACK_OVF (1);
5451 n = read16 (ip + 2);
5453 NEW_LOCLOAD (cfg, ins, n);
5459 CHECK_STACK_OVF (1);
5461 n = read16 (ip + 2);
5463 NEW_LOCLOADA (cfg, ins, n);
5472 n = read16 (ip + 2);
5474 handle_loaded_temps (cfg, bblock, stack_start, sp);
5475 NEW_LOCSTORE (cfg, ins, n, *sp);
5477 if (ins->opcode == CEE_STOBJ) {
5478 NEW_LOCLOADA (cfg, ins, n);
5479 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5481 MONO_ADD_INS (bblock, ins);
5488 if (sp != stack_start)
5490 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
5491 ins->inst_left = *sp;
5494 cfg->flags |= MONO_CFG_HAS_ALLOCA;
5495 if (header->init_locals)
5496 ins->flags |= MONO_INST_INIT;
5500 /* FIXME: set init flag if locals init is set in this method */
5502 case CEE_ENDFILTER: {
5503 MonoExceptionClause *clause, *nearest;
5504 int cc, nearest_num;
5508 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
5510 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
5511 ins->inst_left = *sp;
5513 MONO_ADD_INS (bblock, ins);
5514 start_new_bblock = 1;
5519 for (cc = 0; cc < header->num_clauses; ++cc) {
5520 clause = &header->clauses [cc];
5521 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
5522 (!nearest || (clause->token_or_filter > nearest->token_or_filter))) {
5528 filter_lengths [nearest_num] = (ip - header->code) - nearest->token_or_filter;
5532 case CEE_UNALIGNED_:
5533 ins_flag |= MONO_INST_UNALIGNED;
5534 /* FIXME: record alignment? we can assume 1 for now */
5539 ins_flag |= MONO_INST_VOLATILE;
5543 ins_flag |= MONO_INST_TAILCALL;
5544 /* Can't inline tail calls at this time */
5545 inline_costs += 100000;
5552 token = read32 (ip + 2);
5553 if (method->wrapper_type != MONO_WRAPPER_NONE)
5554 klass = mono_method_get_wrapper_data (method, token);
5556 klass = mono_class_get_full (image, token, generic_context);
5557 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5558 MonoInst *store, *load;
5559 NEW_PCONST (cfg, load, NULL);
5560 load->cil_code = ip;
5561 load->type = STACK_OBJ;
5562 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
5563 store->cil_code = ip;
5564 handle_loaded_temps (cfg, bblock, stack_start, sp);
5565 MONO_ADD_INS (bblock, store);
5566 store->inst_i0 = sp [0];
5567 store->inst_i1 = load;
5569 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
5574 case CEE_CONSTRAINED_:
5575 /* FIXME: implement */
5577 token = read32 (ip + 2);
5578 constrained_call = mono_class_get_full (image, token, generic_context);
5583 MonoInst *iargs [3];
5586 if ((cfg->opt & MONO_OPT_INTRINS) && (ip [1] == CEE_CPBLK) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5)) {
5588 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
5589 copy->inst_left = sp [0];
5590 copy->inst_right = sp [1];
5591 copy->cil_code = ip;
5593 MONO_ADD_INS (bblock, copy);
5600 handle_loaded_temps (cfg, bblock, stack_start, sp);
5601 if (ip [1] == CEE_CPBLK) {
5602 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
5604 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
5613 ins_flag |= MONO_INST_NOTYPECHECK;
5615 ins_flag |= MONO_INST_NORANGECHECK;
5616 /* we ignore the no-nullcheck for now since we
5617 * really do it explicitly only when doing callvirt->call
5623 /* FIXME: check we are in a catch handler */
5624 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
5625 load->cil_code = ip;
5626 MONO_INST_NEW (cfg, ins, CEE_THROW);
5627 ins->inst_left = load;
5629 MONO_ADD_INS (bblock, ins);
5631 start_new_bblock = 1;
5636 CHECK_STACK_OVF (1);
5638 token = read32 (ip + 2);
5639 /* FIXXME: handle generics. */
5640 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
5641 MonoType *type = mono_type_create_from_typespec (image, token);
5642 token = mono_type_size (type, &align);
5644 MonoClass *szclass = mono_class_get_full (image, token, generic_context);
5645 mono_class_init (szclass);
5646 token = mono_class_value_size (szclass, &align);
5648 NEW_ICONST (cfg, ins, token);
5653 case CEE_REFANYTYPE:
5655 MONO_INST_NEW (cfg, ins, OP_REFANYTYPE);
5657 ins->type = STACK_MP;
5658 ins->inst_left = *sp;
5659 ins->type = STACK_VTYPE;
5660 ins->klass = mono_defaults.typehandle_class;
5669 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
5674 g_error ("opcode 0x%02x not handled", *ip);
5677 if (start_new_bblock != 1)
5680 bblock->cil_length = ip - bblock->cil_code;
5681 bblock->next_bb = end_bblock;
5682 link_bblock (cfg, bblock, end_bblock);
5684 if (cfg->method == method && cfg->domainvar) {
5688 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
5689 call->signature = helper_sig_domain_get;
5690 call->inst.type = STACK_PTR;
5691 call->fptr = mono_domain_get;
5692 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, (MonoInst*)call);
5694 MONO_ADD_INS (init_localsbb, store);
5697 if (header->init_locals) {
5699 for (i = 0; i < header->num_locals; ++i) {
5700 int t = header->locals [i]->type;
5701 if (t == MONO_TYPE_VALUETYPE && header->locals [i]->data.klass->enumtype)
5702 t = header->locals [i]->data.klass->enum_basetype->type;
5703 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
5704 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
5705 NEW_ICONST (cfg, ins, 0);
5706 NEW_LOCSTORE (cfg, store, i, ins);
5707 MONO_ADD_INS (init_localsbb, store);
5708 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
5709 MONO_INST_NEW (cfg, ins, OP_I8CONST);
5710 ins->type = STACK_I8;
5712 NEW_LOCSTORE (cfg, store, i, ins);
5713 MONO_ADD_INS (init_localsbb, store);
5714 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
5715 MONO_INST_NEW (cfg, ins, OP_R8CONST);
5716 ins->type = STACK_R8;
5717 ins->inst_p0 = (void*)&r8_0;
5718 NEW_LOCSTORE (cfg, store, i, ins);
5719 MONO_ADD_INS (init_localsbb, store);
5720 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF)) {
5721 NEW_LOCLOADA (cfg, ins, i);
5722 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (header->locals [i]), NULL, NULL);
5725 NEW_PCONST (cfg, ins, NULL);
5726 NEW_LOCSTORE (cfg, store, i, ins);
5727 MONO_ADD_INS (init_localsbb, store);
5733 /* resolve backward branches in the middle of an existing basic block */
5734 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
5736 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
5737 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
5738 if (tblock != start_bblock) {
5740 split_bblock (cfg, tblock, bblock);
5741 l = bblock->cil_code - header->code;
5742 bblock->cil_length = tblock->cil_length - l;
5743 tblock->cil_length = l;
5745 g_print ("recheck failed.\n");
5750 * we compute regions here, because the length of filter clauses is not known in advance.
5751 * It is computed in the CEE_ENDFILTER case in the above switch statement
5753 if (cfg->method == method) {
5755 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5756 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
5758 mono_create_spvar_for_region (cfg, bb->region);
5759 if (cfg->verbose_level > 2)
5760 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
5763 g_hash_table_destroy (bbhash);
5766 dont_inline = g_list_remove (dont_inline, method);
5767 return inline_costs;
5770 if (cfg->method != method)
5771 g_hash_table_destroy (bbhash);
5772 dont_inline = g_list_remove (dont_inline, method);
5776 if (cfg->method != method)
5777 g_hash_table_destroy (bbhash);
5778 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
5779 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
5780 dont_inline = g_list_remove (dont_inline, method);
5785 mono_print_tree (MonoInst *tree) {
5791 arity = mono_burg_arity [tree->opcode];
5793 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
5795 switch (tree->opcode) {
5797 printf ("[%d]", tree->inst_c0);
5800 printf ("[%lld]", tree->inst_l);
5803 printf ("[%f]", *(double*)tree->inst_p0);
5806 printf ("[%f]", *(float*)tree->inst_p0);
5810 printf ("[%d]", tree->inst_c0);
5813 if (tree->inst_offset < 0)
5814 printf ("[-0x%x(%s)]", -tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5816 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5819 printf ("[%s]", mono_arch_regname (tree->dreg));
5822 printf ("[%s]", tree->inst_newa_class->name);
5823 mono_print_tree (tree->inst_newa_len);
5834 case OP_VOIDCALLVIRT: {
5835 MonoCallInst *call = (MonoCallInst*)tree;
5837 printf ("[%s]", call->method->name);
5842 printf ("[%d (", tree->inst_c0);
5843 for (i = 0; i < tree->inst_phi_args [0]; i++) {
5846 printf ("%d", tree->inst_phi_args [i + 1]);
5857 case OP_LOAD_MEMBASE:
5858 case OP_LOADI4_MEMBASE:
5859 case OP_LOADU4_MEMBASE:
5860 case OP_LOADU1_MEMBASE:
5861 case OP_LOADI1_MEMBASE:
5862 case OP_LOADU2_MEMBASE:
5863 case OP_LOADI2_MEMBASE:
5864 printf ("[%s] <- [%s + 0x%x]", mono_arch_regname (tree->dreg), mono_arch_regname (tree->inst_basereg), tree->inst_offset);
5867 printf ("[B%d]", tree->inst_target_bb->block_num);
5877 case OP_VOIDCALL_REG:
5878 mono_print_tree (tree->inst_left);
5890 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
5891 mono_print_tree (tree->inst_left);
5894 if (!mono_arch_print_tree(tree, arity)) {
5896 mono_print_tree (tree->inst_left);
5898 mono_print_tree (tree->inst_right);
5909 mono_print_tree_nl (MonoInst *tree)
5911 mono_print_tree (tree);
5916 create_helper_signature (void)
5918 /* FIXME: set call conv */
5919 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
5920 helper_sig_newarr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5921 helper_sig_newarr->params [0] = helper_sig_newarr->params [1] = &mono_defaults.int_class->byval_arg;
5922 helper_sig_newarr->ret = &mono_defaults.object_class->byval_arg;
5923 helper_sig_newarr->params [2] = &mono_defaults.int32_class->byval_arg;
5924 helper_sig_newarr->pinvoke = 1;
5926 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
5927 helper_sig_newarr_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5928 helper_sig_newarr_specific->params [0] = &mono_defaults.int_class->byval_arg;
5929 helper_sig_newarr_specific->params [1] = &mono_defaults.int32_class->byval_arg;
5930 helper_sig_newarr_specific->ret = &mono_defaults.object_class->byval_arg;
5931 helper_sig_newarr_specific->pinvoke = 1;
5933 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
5934 helper_sig_object_new = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5935 helper_sig_object_new->params [0] = helper_sig_object_new->params [1] = &mono_defaults.int_class->byval_arg;
5936 helper_sig_object_new->ret = &mono_defaults.object_class->byval_arg;
5937 helper_sig_object_new->pinvoke = 1;
5939 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
5940 helper_sig_object_new_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5941 helper_sig_object_new_specific->params [0] = &mono_defaults.int_class->byval_arg;
5942 helper_sig_object_new_specific->ret = &mono_defaults.object_class->byval_arg;
5943 helper_sig_object_new_specific->pinvoke = 1;
5945 /* void* mono_method_compile (MonoMethod*) */
5946 helper_sig_compile = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5947 helper_sig_compile->params [0] = helper_sig_compile->ret = &mono_defaults.int_class->byval_arg;
5948 helper_sig_compile->pinvoke = 1;
5950 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
5951 helper_sig_compile_virt = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5952 helper_sig_compile_virt->params [0] = &mono_defaults.object_class->byval_arg;
5953 helper_sig_compile_virt->params [1] = helper_sig_compile_virt->ret = &mono_defaults.int_class->byval_arg;
5954 helper_sig_compile_virt->pinvoke = 1;
5956 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
5957 helper_sig_ldstr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5958 helper_sig_ldstr->params [0] = helper_sig_ldstr->params [1] = &mono_defaults.int_class->byval_arg;
5959 helper_sig_ldstr->params [2] = &mono_defaults.int32_class->byval_arg;
5960 helper_sig_ldstr->ret = &mono_defaults.object_class->byval_arg;
5961 helper_sig_ldstr->pinvoke = 1;
5963 /* MonoDomain *mono_domain_get (void) */
5964 helper_sig_domain_get = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5965 helper_sig_domain_get->ret = &mono_defaults.int_class->byval_arg;
5966 helper_sig_domain_get->pinvoke = 1;
5968 /* void* stelem_ref (MonoArray *, int index, MonoObject *) */
5969 helper_sig_stelem_ref = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5970 helper_sig_stelem_ref->params [0] = &mono_defaults.array_class->byval_arg;
5971 helper_sig_stelem_ref->params [1] = &mono_defaults.int32_class->byval_arg;
5972 helper_sig_stelem_ref->params [2] = &mono_defaults.object_class->byval_arg;
5973 helper_sig_stelem_ref->ret = &mono_defaults.void_class->byval_arg;
5974 helper_sig_stelem_ref->pinvoke = 1;
5976 /* void* stelem_ref_check (MonoArray *, MonoObject *) */
5977 helper_sig_stelem_ref_check = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5978 helper_sig_stelem_ref_check->params [0] = &mono_defaults.array_class->byval_arg;
5979 helper_sig_stelem_ref_check->params [1] = &mono_defaults.object_class->byval_arg;
5980 helper_sig_stelem_ref_check->ret = &mono_defaults.void_class->byval_arg;
5981 helper_sig_stelem_ref_check->pinvoke = 1;
5983 /* long amethod (long, long) */
5984 helper_sig_long_long_long = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5985 helper_sig_long_long_long->params [0] = helper_sig_long_long_long->params [1] =
5986 &mono_defaults.int64_class->byval_arg;
5987 helper_sig_long_long_long->ret = &mono_defaults.int64_class->byval_arg;
5988 helper_sig_long_long_long->pinvoke = 1;
5990 /* object amethod (intptr) */
5991 helper_sig_obj_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5992 helper_sig_obj_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5993 helper_sig_obj_ptr->ret = &mono_defaults.object_class->byval_arg;
5994 helper_sig_obj_ptr->pinvoke = 1;
5996 /* void amethod (intptr) */
5997 helper_sig_void_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5998 helper_sig_void_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5999 helper_sig_void_ptr->ret = &mono_defaults.void_class->byval_arg;
6000 helper_sig_void_ptr->pinvoke = 1;
6002 /* void amethod (MonoObject *obj) */
6003 helper_sig_void_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6004 helper_sig_void_obj->params [0] = &mono_defaults.object_class->byval_arg;
6005 helper_sig_void_obj->ret = &mono_defaults.void_class->byval_arg;
6006 helper_sig_void_obj->pinvoke = 1;
6008 /* intptr amethod (void) */
6009 helper_sig_ptr_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
6010 helper_sig_ptr_void->ret = &mono_defaults.int_class->byval_arg;
6011 helper_sig_ptr_void->pinvoke = 1;
6013 /* object amethod (void) */
6014 helper_sig_obj_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
6015 helper_sig_obj_void->ret = &mono_defaults.object_class->byval_arg;
6016 helper_sig_obj_void->pinvoke = 1;
6018 /* void amethod (intptr, intptr) */
6019 helper_sig_void_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6020 helper_sig_void_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
6021 helper_sig_void_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
6022 helper_sig_void_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
6023 helper_sig_void_ptr_ptr->pinvoke = 1;
6025 /* void amethod (intptr, intptr, intptr) */
6026 helper_sig_void_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
6027 helper_sig_void_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
6028 helper_sig_void_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
6029 helper_sig_void_ptr_ptr_ptr->params [2] = &mono_defaults.int_class->byval_arg;
6030 helper_sig_void_ptr_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
6031 helper_sig_void_ptr_ptr_ptr->pinvoke = 1;
6033 /* intptr amethod (intptr, intptr) */
6034 helper_sig_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6035 helper_sig_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
6036 helper_sig_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
6037 helper_sig_ptr_ptr_ptr->ret = &mono_defaults.int_class->byval_arg;
6038 helper_sig_ptr_ptr_ptr->pinvoke = 1;
6040 /* IntPtr amethod (object) */
6041 helper_sig_ptr_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6042 helper_sig_ptr_obj->params [0] = &mono_defaults.object_class->byval_arg;
6043 helper_sig_ptr_obj->ret = &mono_defaults.int_class->byval_arg;
6044 helper_sig_ptr_obj->pinvoke = 1;
6046 /* IntPtr amethod (int) */
6047 helper_sig_ptr_int = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6048 helper_sig_ptr_int->params [0] = &mono_defaults.int32_class->byval_arg;
6049 helper_sig_ptr_int->ret = &mono_defaults.int_class->byval_arg;
6050 helper_sig_ptr_int->pinvoke = 1;
6052 /* long amethod (long, guint32) */
6053 helper_sig_long_long_int = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6054 helper_sig_long_long_int->params [0] = &mono_defaults.int64_class->byval_arg;
6055 helper_sig_long_long_int->params [1] = &mono_defaults.int32_class->byval_arg;
6056 helper_sig_long_long_int->ret = &mono_defaults.int64_class->byval_arg;
6057 helper_sig_long_long_int->pinvoke = 1;
6059 /* ulong amethod (double) */
6060 helper_sig_ulong_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6061 helper_sig_ulong_double->params [0] = &mono_defaults.double_class->byval_arg;
6062 helper_sig_ulong_double->ret = &mono_defaults.uint64_class->byval_arg;
6063 helper_sig_ulong_double->pinvoke = 1;
6065 /* long amethod (double) */
6066 helper_sig_long_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6067 helper_sig_long_double->params [0] = &mono_defaults.double_class->byval_arg;
6068 helper_sig_long_double->ret = &mono_defaults.int64_class->byval_arg;
6069 helper_sig_long_double->pinvoke = 1;
6071 /* double amethod (long) */
6072 helper_sig_double_long = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6073 helper_sig_double_long->params [0] = &mono_defaults.int64_class->byval_arg;
6074 helper_sig_double_long->ret = &mono_defaults.double_class->byval_arg;
6075 helper_sig_double_long->pinvoke = 1;
6077 /* double amethod (int) */
6078 helper_sig_double_int = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6079 helper_sig_double_int->params [0] = &mono_defaults.int32_class->byval_arg;
6080 helper_sig_double_int->ret = &mono_defaults.double_class->byval_arg;
6081 helper_sig_double_int->pinvoke = 1;
6083 /* float amethod (long) */
6084 helper_sig_float_long = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6085 helper_sig_float_long->params [0] = &mono_defaults.int64_class->byval_arg;
6086 helper_sig_float_long->ret = &mono_defaults.single_class->byval_arg;
6087 helper_sig_float_long->pinvoke = 1;
6089 /* double amethod (double, double) */
6090 helper_sig_double_double_double = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6091 helper_sig_double_double_double->params [0] = &mono_defaults.double_class->byval_arg;
6092 helper_sig_double_double_double->params [1] = &mono_defaults.double_class->byval_arg;
6093 helper_sig_double_double_double->ret = &mono_defaults.double_class->byval_arg;
6094 helper_sig_double_double_double->pinvoke = 1;
6096 /* uint amethod (double) */
6097 helper_sig_uint_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6098 helper_sig_uint_double->params [0] = &mono_defaults.double_class->byval_arg;
6099 helper_sig_uint_double->ret = &mono_defaults.uint32_class->byval_arg;
6100 helper_sig_uint_double->pinvoke = 1;
6102 /* int amethod (double) */
6103 helper_sig_int_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6104 helper_sig_int_double->params [0] = &mono_defaults.double_class->byval_arg;
6105 helper_sig_int_double->ret = &mono_defaults.int32_class->byval_arg;
6106 helper_sig_int_double->pinvoke = 1;
6108 /* void initobj (intptr, int size) */
6109 helper_sig_initobj = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6110 helper_sig_initobj->params [0] = &mono_defaults.int_class->byval_arg;
6111 helper_sig_initobj->params [1] = &mono_defaults.int32_class->byval_arg;
6112 helper_sig_initobj->ret = &mono_defaults.void_class->byval_arg;
6113 helper_sig_initobj->pinvoke = 1;
6115 /* void memcpy (intptr, intptr, int size) */
6116 helper_sig_memcpy = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
6117 helper_sig_memcpy->params [0] = &mono_defaults.int_class->byval_arg;
6118 helper_sig_memcpy->params [1] = &mono_defaults.int_class->byval_arg;
6119 helper_sig_memcpy->params [2] = &mono_defaults.int32_class->byval_arg;
6120 helper_sig_memcpy->ret = &mono_defaults.void_class->byval_arg;
6121 helper_sig_memcpy->pinvoke = 1;
6123 /* void memset (intptr, int val, int size) */
6124 helper_sig_memset = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
6125 helper_sig_memset->params [0] = &mono_defaults.int_class->byval_arg;
6126 helper_sig_memset->params [1] = &mono_defaults.int32_class->byval_arg;
6127 helper_sig_memset->params [2] = &mono_defaults.int32_class->byval_arg;
6128 helper_sig_memset->ret = &mono_defaults.void_class->byval_arg;
6129 helper_sig_memset->pinvoke = 1;
6131 helper_sig_class_init_trampoline = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
6132 helper_sig_class_init_trampoline->ret = &mono_defaults.void_class->byval_arg;
6133 helper_sig_class_init_trampoline->pinvoke = 1;
6136 static GHashTable *jit_icall_hash_name = NULL;
6137 static GHashTable *jit_icall_hash_addr = NULL;
6140 mono_find_jit_icall_by_name (const char *name)
6142 MonoJitICallInfo *info;
6143 g_assert (jit_icall_hash_name);
6145 //printf ("lookup addr %s %p\n", name, g_hash_table_lookup (jit_icall_hash_name, name));
6146 EnterCriticalSection (&trampoline_hash_mutex);
6147 info = g_hash_table_lookup (jit_icall_hash_name, name);
6148 LeaveCriticalSection (&trampoline_hash_mutex);
6153 mono_find_jit_icall_by_addr (gconstpointer addr)
6155 MonoJitICallInfo *info;
6156 g_assert (jit_icall_hash_addr);
6158 EnterCriticalSection (&trampoline_hash_mutex);
6159 info = g_hash_table_lookup (jit_icall_hash_addr, (gpointer)addr);
6160 LeaveCriticalSection (&trampoline_hash_mutex);
6166 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
6169 MonoMethod *wrapper;
6172 if (callinfo->wrapper)
6173 return callinfo->wrapper;
6175 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
6176 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
6177 /* Must be domain neutral since there is only one copy */
6178 code = mono_jit_compile_method_with_opt (wrapper, default_opt | MONO_OPT_SHARED);
6180 EnterCriticalSection (&trampoline_hash_mutex);
6181 if (!callinfo->wrapper) {
6182 callinfo->wrapper = code;
6183 g_hash_table_insert (jit_icall_hash_addr, (gpointer)callinfo->wrapper, callinfo);
6185 LeaveCriticalSection (&trampoline_hash_mutex);
6188 return callinfo->wrapper;
6192 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save)
6194 MonoJitICallInfo *info;
6199 EnterCriticalSection (&trampoline_hash_mutex);
6201 if (!jit_icall_hash_name) {
6202 jit_icall_hash_name = g_hash_table_new (g_str_hash, g_str_equal);
6203 jit_icall_hash_addr = g_hash_table_new (NULL, NULL);
6206 if (g_hash_table_lookup (jit_icall_hash_name, name)) {
6207 g_warning ("jit icall already defined \"%s\"\n", name);
6208 g_assert_not_reached ();
6211 info = g_new (MonoJitICallInfo, 1);
6218 #ifdef MONO_USE_EXC_TABLES
6219 || mono_arch_has_unwind_info (func)
6222 info->wrapper = func;
6224 info->wrapper = NULL;
6227 g_hash_table_insert (jit_icall_hash_name, (gpointer)info->name, info);
6228 g_hash_table_insert (jit_icall_hash_addr, (gpointer)func, info);
6230 LeaveCriticalSection (&trampoline_hash_mutex);
6235 mono_create_class_init_trampoline (MonoVTable *vtable)
6239 /* previously created trampoline code */
6240 mono_domain_lock (vtable->domain);
6242 mono_g_hash_table_lookup (vtable->domain->class_init_trampoline_hash,
6244 mono_domain_unlock (vtable->domain);
6248 code = mono_arch_create_class_init_trampoline (vtable);
6250 /* store trampoline address */
6251 mono_domain_lock (vtable->domain);
6252 mono_g_hash_table_insert (vtable->domain->class_init_trampoline_hash,
6254 mono_domain_unlock (vtable->domain);
6256 EnterCriticalSection (&trampoline_hash_mutex);
6257 if (!class_init_hash_addr)
6258 class_init_hash_addr = g_hash_table_new (NULL, NULL);
6259 g_hash_table_insert (class_init_hash_addr, code, vtable);
6260 LeaveCriticalSection (&trampoline_hash_mutex);
6266 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method,
6267 gboolean add_sync_wrapper)
6272 if (add_sync_wrapper && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
6273 return mono_create_jump_trampoline (domain, mono_marshal_get_synchronized_wrapper (method), FALSE);
6275 code = mono_jit_find_compiled_method (domain, method);
6279 EnterCriticalSection (&trampoline_hash_mutex);
6281 if (jump_trampoline_hash) {
6282 code = g_hash_table_lookup (jump_trampoline_hash, method);
6284 LeaveCriticalSection (&trampoline_hash_mutex);
6289 ji = mono_arch_create_jump_trampoline (method);
6292 * mono_delegate_ctor needs to find the method metadata from the
6293 * trampoline address, so we save it here.
6296 mono_jit_info_table_add (mono_root_domain, ji);
6298 if (!jump_trampoline_hash)
6299 jump_trampoline_hash = g_hash_table_new (NULL, NULL);
6300 g_hash_table_insert (jump_trampoline_hash, method, ji->code_start);
6302 LeaveCriticalSection (&trampoline_hash_mutex);
6304 return ji->code_start;
6308 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
6312 EnterCriticalSection (&trampoline_hash_mutex);
6313 if (class_init_hash_addr)
6314 res = g_hash_table_lookup (class_init_hash_addr, addr);
6317 LeaveCriticalSection (&trampoline_hash_mutex);
6322 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func, gboolean no_throw)
6324 MonoJitICallInfo *info;
6326 if (!emul_opcode_map)
6327 emul_opcode_map = g_new0 (MonoJitICallInfo*, OP_LAST + 1);
6329 g_assert (!sig->hasthis);
6330 g_assert (sig->param_count < 3);
6332 info = mono_register_jit_icall (func, name, sig, no_throw);
6334 emul_opcode_map [opcode] = info;
6338 decompose_foreach (MonoInst *tree, gpointer data)
6340 static MonoJitICallInfo *newarr_info = NULL;
6341 static MonoJitICallInfo *newarr_specific_info = NULL;
6342 MonoJitICallInfo *info;
6345 switch (tree->opcode) {
6347 MonoCompile *cfg = data;
6348 MonoInst *iargs [3];
6351 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
6352 g_assert (newarr_info);
6353 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
6354 g_assert (newarr_specific_info);
6357 if (cfg->opt & MONO_OPT_SHARED) {
6358 NEW_DOMAINCONST (cfg, iargs [0]);
6359 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
6360 iargs [2] = tree->inst_newa_len;
6365 MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
6367 NEW_VTABLECONST (cfg, iargs [0], vtable);
6368 iargs [1] = tree->inst_newa_len;
6370 info = newarr_specific_info;
6373 mono_emulate_opcode (cfg, tree, iargs, info);
6375 /* Need to decompose arguments after the the opcode is decomposed */
6376 for (i = 0; i < info->sig->param_count; ++i)
6377 dec_foreach (iargs [i], cfg);
6387 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
6389 switch (mono_burg_arity [tree->opcode]) {
6392 mono_inst_foreach (tree->inst_left, func, data);
6395 mono_inst_foreach (tree->inst_left, func, data);
6396 mono_inst_foreach (tree->inst_right, func, data);
6399 g_assert_not_reached ();
6406 mono_print_bb_code (MonoBasicBlock *bb) {
6408 MonoInst *c = bb->code;
6410 mono_print_tree (c);
6418 print_dfn (MonoCompile *cfg) {
6423 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
6425 for (i = 0; i < cfg->num_bblocks; ++i) {
6426 bb = cfg->bblocks [i];
6428 char* code1, *code2;
6429 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
6430 if (bb->last_ins->cil_code)
6431 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
6433 code2 = g_strdup ("");
6435 code1 [strlen (code1) - 1] = 0;
6436 code = g_strdup_printf ("%s -> %s", code1, code2);
6440 code = g_strdup ("\n");
6441 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
6443 MonoInst *c = bb->code;
6445 mono_print_tree (c);
6453 g_print ("\tprev:");
6454 for (j = 0; j < bb->in_count; ++j) {
6455 g_print (" BB%d", bb->in_bb [j]->block_num);
6457 g_print ("\t\tsucc:");
6458 for (j = 0; j < bb->out_count; ++j) {
6459 g_print (" BB%d", bb->out_bb [j]->block_num);
6461 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
6464 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
6467 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
6469 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
6477 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
6481 g_assert (bb->code);
6482 bb->last_ins->next = inst;
6483 bb->last_ins = inst;
6485 bb->last_ins = bb->code = inst;
6490 mono_destroy_compile (MonoCompile *cfg)
6492 //mono_mempool_stats (cfg->mempool);
6493 g_hash_table_destroy (cfg->bb_hash);
6495 mono_regstate_free (cfg->rs);
6497 g_hash_table_destroy (cfg->spvars);
6498 mono_mempool_destroy (cfg->mempool);
6499 g_list_free (cfg->ldstr_list);
6501 g_free (cfg->varinfo);
6507 mono_get_lmf_addr (void)
6509 MonoJitTlsData *jit_tls;
6511 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
6512 return &jit_tls->lmf;
6514 g_assert_not_reached ();
6519 * mono_thread_abort:
6520 * @obj: exception object
6522 * abort the thread, print exception information and stack trace
6525 mono_thread_abort (MonoObject *obj)
6527 /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
6529 /* handle_remove should be eventually called for this thread, too
6536 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
6538 MonoJitTlsData *jit_tls;
6541 jit_tls = TlsGetValue (mono_jit_tls_id);
6545 jit_tls = g_new0 (MonoJitTlsData, 1);
6547 TlsSetValue (mono_jit_tls_id, jit_tls);
6549 jit_tls->abort_func = abort_func;
6550 jit_tls->end_of_stack = stack_start;
6552 lmf = g_new0 (MonoLMF, 1);
6555 jit_tls->lmf = jit_tls->first_lmf = lmf;
6557 mono_arch_setup_jit_tls_data (jit_tls);
6563 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
6566 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
6567 thread = mono_thread_current ();
6569 thread->jit_data = jit_tls;
6572 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
6575 mono_thread_abort_dummy (MonoObject *obj)
6577 if (mono_thread_attach_aborted_cb)
6578 mono_thread_attach_aborted_cb (obj);
6580 mono_thread_abort (obj);
6584 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
6587 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
6588 thread = mono_thread_current ();
6590 thread->jit_data = jit_tls;
6594 mini_thread_cleanup (MonoThread *thread)
6596 MonoJitTlsData *jit_tls = thread->jit_data;
6599 mono_arch_free_jit_tls_data (jit_tls);
6600 g_free (jit_tls->first_lmf);
6602 thread->jit_data = NULL;
6607 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
6609 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
6613 ji->data.target = target;
6614 ji->next = cfg->patch_info;
6616 cfg->patch_info = ji;
6620 mono_remove_patch_info (MonoCompile *cfg, int ip)
6622 MonoJumpInfo **ji = &cfg->patch_info;
6625 if ((*ji)->ip.i == ip)
6628 ji = &((*ji)->next);
6633 mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors)
6635 unsigned char *ip = patch_info->ip.i + code;
6636 gconstpointer target = NULL;
6638 switch (patch_info->type) {
6639 case MONO_PATCH_INFO_BB:
6640 target = patch_info->data.bb->native_offset + code;
6642 case MONO_PATCH_INFO_ABS:
6643 target = patch_info->data.target;
6645 case MONO_PATCH_INFO_LABEL:
6646 target = patch_info->data.inst->inst_c0 + code;
6648 case MONO_PATCH_INFO_IP:
6651 case MONO_PATCH_INFO_METHOD_REL:
6652 target = code + patch_info->data.offset;
6654 case MONO_PATCH_INFO_INTERNAL_METHOD: {
6655 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
6657 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
6658 g_assert_not_reached ();
6660 target = mono_icall_get_wrapper (mi);
6663 case MONO_PATCH_INFO_METHOD_JUMP: {
6666 /* get the trampoline to the method from the domain */
6667 target = mono_create_jump_trampoline (domain, patch_info->data.method, TRUE);
6668 if (!domain->jump_target_hash)
6669 domain->jump_target_hash = g_hash_table_new (NULL, NULL);
6670 list = g_hash_table_lookup (domain->jump_target_hash, patch_info->data.method);
6671 list = g_slist_prepend (list, ip);
6672 g_hash_table_insert (domain->jump_target_hash, patch_info->data.method, list);
6675 case MONO_PATCH_INFO_METHOD:
6676 if (patch_info->data.method == method) {
6679 /* get the trampoline to the method from the domain */
6680 target = mono_arch_create_jit_trampoline (patch_info->data.method);
6682 case MONO_PATCH_INFO_SWITCH: {
6683 gpointer *jump_table;
6686 mono_domain_lock (domain);
6687 jump_table = mono_code_manager_reserve (domain->code_mp, sizeof (gpointer) * patch_info->table_size);
6688 mono_domain_unlock (domain);
6690 for (i = 0; i < patch_info->table_size; i++) {
6691 jump_table [i] = code + (int)patch_info->data.table [i];
6693 target = jump_table;
6696 case MONO_PATCH_INFO_METHODCONST:
6697 case MONO_PATCH_INFO_CLASS:
6698 case MONO_PATCH_INFO_IMAGE:
6699 case MONO_PATCH_INFO_FIELD:
6700 target = patch_info->data.target;
6702 case MONO_PATCH_INFO_IID:
6703 mono_class_init (patch_info->data.klass);
6704 target = (gpointer)patch_info->data.klass->interface_id;
6706 case MONO_PATCH_INFO_VTABLE:
6707 target = mono_class_vtable (domain, patch_info->data.klass);
6709 case MONO_PATCH_INFO_CLASS_INIT:
6710 target = mono_create_class_init_trampoline (mono_class_vtable (domain, patch_info->data.klass));
6712 case MONO_PATCH_INFO_SFLDA: {
6713 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
6714 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (vtable->klass, method))
6715 /* Done by the generated code */
6719 mono_runtime_class_init (vtable);
6721 target = (char*)vtable->data + patch_info->data.field->offset;
6724 case MONO_PATCH_INFO_R4:
6725 case MONO_PATCH_INFO_R8:
6726 target = patch_info->data.target;
6728 case MONO_PATCH_INFO_EXC_NAME:
6729 target = patch_info->data.name;
6731 case MONO_PATCH_INFO_LDSTR:
6733 mono_ldstr (domain, patch_info->data.token->image,
6734 mono_metadata_token_index (patch_info->data.token->token));
6736 case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
6738 MonoClass *handle_class;
6740 handle = mono_ldtoken (patch_info->data.token->image,
6741 patch_info->data.token->token, &handle_class, NULL);
6742 mono_class_init (handle_class);
6743 mono_class_init (mono_class_from_mono_type (handle));
6746 mono_type_get_object (domain, handle);
6749 case MONO_PATCH_INFO_LDTOKEN: {
6751 MonoClass *handle_class;
6753 handle = mono_ldtoken (patch_info->data.token->image,
6754 patch_info->data.token->token, &handle_class, NULL);
6755 mono_class_init (handle_class);
6761 g_assert_not_reached ();
6764 return (gpointer)target;
6768 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
6769 MonoJitICallInfo *info;
6771 decompose_foreach (tree, cfg);
6773 switch (mono_burg_arity [tree->opcode]) {
6776 dec_foreach (tree->inst_left, cfg);
6778 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6779 MonoInst *iargs [2];
6781 iargs [0] = tree->inst_left;
6783 mono_emulate_opcode (cfg, tree, iargs, info);
6789 #ifdef MONO_ARCH_BIGMUL_INTRINS
6790 if (tree->opcode == OP_LMUL
6791 && (cfg->opt & MONO_OPT_INTRINS)
6792 && (tree->inst_left->opcode == CEE_CONV_I8
6793 || tree->inst_left->opcode == CEE_CONV_U8)
6794 && tree->inst_left->inst_left->type == STACK_I4
6795 && (tree->inst_right->opcode == CEE_CONV_I8
6796 || tree->inst_right->opcode == CEE_CONV_U8)
6797 && tree->inst_right->inst_left->type == STACK_I4) {
6798 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
6799 tree->inst_left = tree->inst_left->inst_left;
6800 tree->inst_right = tree->inst_right->inst_left;
6801 dec_foreach (tree, cfg);
6804 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6805 MonoInst *iargs [2];
6807 iargs [0] = tree->inst_i0;
6808 iargs [1] = tree->inst_i1;
6810 mono_emulate_opcode (cfg, tree, iargs, info);
6812 dec_foreach (iargs [0], cfg);
6813 dec_foreach (iargs [1], cfg);
6816 dec_foreach (tree->inst_left, cfg);
6817 dec_foreach (tree->inst_right, cfg);
6821 g_assert_not_reached ();
6826 decompose_pass (MonoCompile *cfg) {
6829 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6832 cfg->prev_ins = NULL;
6833 for (tree = cfg->cbb->code; tree; tree = tree->next) {
6834 dec_foreach (tree, cfg);
6835 cfg->prev_ins = tree;
6841 nullify_basic_block (MonoBasicBlock *bb)
6848 bb->code = bb->last_ins = NULL;
6849 bb->cil_code = NULL;
6853 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6857 for (i = 0; i < bb->out_count; i++) {
6858 MonoBasicBlock *ob = bb->out_bb [i];
6861 if (bb->out_count > 1) {
6862 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
6866 bb->out_bb [i] = repl;
6873 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6877 for (i = 0; i < bb->in_count; i++) {
6878 MonoBasicBlock *ib = bb->in_bb [i];
6881 if (bb->in_count > 1) {
6882 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
6886 bb->in_bb [i] = repl;
6893 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6897 for (i = 0; i < bb->out_count; i++) {
6898 MonoBasicBlock *ob = bb->out_bb [i];
6899 for (j = 0; j < ob->in_count; j++) {
6900 if (ob->in_bb [j] == orig) {
6901 ob->in_bb [j] = repl;
6910 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
6912 bb->out_count = bbn->out_count;
6913 bb->out_bb = bbn->out_bb;
6915 replace_basic_block (bb, bbn, bb);
6919 bb->last_ins->next = bbn->code;
6920 bb->last_ins = bbn->last_ins;
6923 bb->code = bbn->code;
6924 bb->last_ins = bbn->last_ins;
6926 bb->next_bb = bbn->next_bb;
6927 nullify_basic_block (bbn);
6931 * Optimizes the branches on the Control Flow Graph
6935 optimize_branches (MonoCompile *cfg)
6937 int i, changed = FALSE;
6938 MonoBasicBlock *bb, *bbn;
6939 guint32 niterations;
6942 * Some crazy loops could cause the code below to go into an infinite
6943 * loop, see bug #53003 for an example. To prevent this, we put an upper
6944 * bound on the number of iterations.
6951 /* we skip the entry block (exit is handled specially instead ) */
6952 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
6954 /* dont touch code inside exception clauses */
6955 if (bb->region != -1)
6958 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
6959 if (cfg->verbose_level > 2)
6960 g_print ("nullify block triggered %d\n", bbn->block_num);
6962 bb->next_bb = bbn->next_bb;
6964 for (i = 0; i < bbn->out_count; i++)
6965 replace_in_block (bbn->out_bb [i], bbn, NULL);
6967 nullify_basic_block (bbn);
6971 if (bb->out_count == 1) {
6972 bbn = bb->out_bb [0];
6974 /* conditional branches where true and false targets are the same can be also replaced with CEE_BR */
6975 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
6976 bb->last_ins->opcode = CEE_BR;
6977 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
6979 if (cfg->verbose_level > 2)
6980 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
6983 if (bb->region == bbn->region && bb->next_bb == bbn) {
6984 /* the block are in sequence anyway ... */
6986 /* branches to the following block can be removed */
6987 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
6988 bb->last_ins->opcode = CEE_NOP;
6990 if (cfg->verbose_level > 2)
6991 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
6994 if (bbn->in_count == 1) {
6996 if (bbn != cfg->bb_exit) {
6997 if (cfg->verbose_level > 2)
6998 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
6999 merge_basic_blocks (bb, bbn);
7003 //mono_print_bb_code (bb);
7008 } while (changed && (niterations > 0));
7015 /* we skip the entry block (exit is handled specially instead ) */
7016 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
7018 /* dont touch code inside exception clauses */
7019 if (bb->region != -1)
7022 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
7023 if (cfg->verbose_level > 2) {
7024 g_print ("nullify block triggered %d\n", bbn->block_num);
7026 bb->next_bb = bbn->next_bb;
7028 for (i = 0; i < bbn->out_count; i++)
7029 replace_in_block (bbn->out_bb [i], bbn, NULL);
7031 nullify_basic_block (bbn);
7037 if (bb->out_count == 1) {
7038 bbn = bb->out_bb [0];
7040 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
7041 bbn = bb->last_ins->inst_target_bb;
7042 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7043 bbn->code->inst_target_bb->region == bb->region) {
7045 if (cfg->verbose_level > 2)
7046 g_print ("in %s branch to branch triggered %d -> %d\n", cfg->method->name,
7047 bb->block_num, bbn->block_num);
7049 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
7050 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
7055 } else if (bb->out_count == 2) {
7056 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
7057 bbn = bb->last_ins->inst_true_bb;
7058 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7059 bbn->code->inst_target_bb->region == bb->region) {
7060 if (cfg->verbose_level > 2)
7061 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
7062 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
7065 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
7067 replace_in_block (bbn, bb, NULL);
7069 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
7070 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7072 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7078 bbn = bb->last_ins->inst_false_bb;
7079 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7080 bbn->code->inst_target_bb->region == bb->region) {
7081 if (cfg->verbose_level > 2)
7082 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
7083 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
7086 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
7088 replace_in_block (bbn, bb, NULL);
7090 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
7091 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7093 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7101 } while (changed && (niterations > 0));
7106 mono_compile_create_vars (MonoCompile *cfg)
7108 MonoMethodSignature *sig;
7109 MonoMethodHeader *header;
7112 header = ((MonoMethodNormal *)cfg->method)->header;
7114 sig = cfg->method->signature;
7116 if (!MONO_TYPE_IS_VOID (sig->ret)) {
7117 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
7118 cfg->ret->opcode = OP_RETARG;
7119 cfg->ret->inst_vtype = sig->ret;
7120 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
7122 if (cfg->verbose_level > 2)
7123 g_print ("creating vars\n");
7126 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
7128 for (i = 0; i < sig->param_count; ++i)
7129 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
7131 cfg->locals_start = cfg->num_varinfo;
7133 if (cfg->verbose_level > 2)
7134 g_print ("creating locals\n");
7135 for (i = 0; i < header->num_locals; ++i)
7136 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
7137 if (cfg->verbose_level > 2)
7138 g_print ("locals done\n");
7142 mono_print_code (MonoCompile *cfg)
7146 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7147 MonoInst *tree = bb->code;
7152 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
7154 for (; tree; tree = tree->next) {
7155 mono_print_tree (tree);
7160 bb->last_ins->next = NULL;
7164 extern const char * const mono_burg_rule_string [];
7167 emit_state (MonoCompile *cfg, MBState *state, int goal)
7170 int ern = mono_burg_rule (state, goal);
7171 const guint16 *nts = mono_burg_nts [ern];
7174 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
7178 // state->reg1 = state->reg2; /* chain rule */
7180 state->reg1 = mono_regstate_next_int (cfg->rs);
7181 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
7184 state->reg1 = mono_regstate_next_int (cfg->rs);
7185 state->reg2 = mono_regstate_next_int (cfg->rs);
7188 state->reg1 = mono_regstate_next_float (cfg->rs);
7195 mono_burg_kids (state, ern, kids);
7197 emit_state (cfg, kids [0], nts [0]);
7199 emit_state (cfg, kids [1], nts [1]);
7201 g_assert (!nts [3]);
7202 emit_state (cfg, kids [2], nts [2]);
7207 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
7208 if ((emit = mono_burg_func [ern]))
7209 emit (state, state->tree, cfg);
7212 #define DEBUG_SELECTION
7215 mini_select_instructions (MonoCompile *cfg)
7217 static int reverse_map [] = {
7218 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
7219 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
7221 static int reverse_fmap [] = {
7222 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
7223 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
7225 static int reverse_lmap [] = {
7226 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
7227 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
7232 cfg->state_pool = mono_mempool_new ();
7233 cfg->rs = mono_regstate_new ();
7235 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7236 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode) &&
7237 bb->next_bb != bb->last_ins->inst_false_bb) {
7239 if (bb->next_bb == bb->last_ins->inst_true_bb) {
7240 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
7241 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
7242 bb->last_ins->inst_false_bb = tmp;
7244 if (bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
7245 bb->last_ins->opcode = reverse_map [bb->last_ins->opcode - CEE_BEQ];
7246 } else if (bb->last_ins->opcode >= OP_FBEQ && bb->last_ins->opcode <= OP_FBLT_UN) {
7247 bb->last_ins->opcode = reverse_fmap [bb->last_ins->opcode - OP_FBEQ];
7248 } else if (bb->last_ins->opcode >= OP_LBEQ && bb->last_ins->opcode <= OP_LBLT_UN) {
7249 bb->last_ins->opcode = reverse_lmap [bb->last_ins->opcode - OP_LBEQ];
7252 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
7253 inst->opcode = CEE_BR;
7254 inst->inst_target_bb = bb->last_ins->inst_false_bb;
7255 mono_bblock_add_inst (bb, inst);
7260 #ifdef DEBUG_SELECTION
7261 if (cfg->verbose_level >= 4) {
7262 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7263 MonoInst *tree = bb->code;
7264 g_print ("DUMP BLOCK %d:\n", bb->block_num);
7267 for (; tree; tree = tree->next) {
7268 mono_print_tree (tree);
7275 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7276 MonoInst *tree = bb->code, *next;
7282 bb->last_ins = NULL;
7285 mono_regstate_reset (cfg->rs);
7287 #ifdef DEBUG_SELECTION
7288 if (cfg->verbose_level >= 3)
7289 g_print ("LABEL BLOCK %d:\n", bb->block_num);
7291 for (; tree; tree = next) {
7293 #ifdef DEBUG_SELECTION
7294 if (cfg->verbose_level >= 3) {
7295 mono_print_tree (tree);
7300 if (!(mbstate = mono_burg_label (tree, cfg))) {
7301 g_warning ("unable to label tree %p", tree);
7302 mono_print_tree (tree);
7304 g_assert_not_reached ();
7306 emit_state (cfg, mbstate, MB_NTERM_stmt);
7308 bb->max_ireg = cfg->rs->next_vireg;
7309 bb->max_freg = cfg->rs->next_vfreg;
7312 bb->last_ins->next = NULL;
7314 mono_mempool_empty (cfg->state_pool);
7316 mono_mempool_destroy (cfg->state_pool);
7320 mono_codegen (MonoCompile *cfg)
7322 MonoJumpInfo *patch_info;
7324 int i, max_epilog_size;
7327 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7328 cfg->spill_count = 0;
7329 /* we reuse dfn here */
7330 /* bb->dfn = bb_count++; */
7331 mono_arch_local_regalloc (cfg, bb);
7334 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
7335 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
7337 code = mono_arch_emit_prolog (cfg);
7339 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7340 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
7342 cfg->code_len = code - cfg->native_code;
7343 cfg->prolog_end = cfg->code_len;
7345 mono_debug_open_method (cfg);
7347 /* emit code all basic blocks */
7348 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7349 bb->native_offset = cfg->code_len;
7350 mono_arch_output_basic_block (cfg, bb);
7352 cfg->bb_exit->native_offset = cfg->code_len;
7354 code = cfg->native_code + cfg->code_len;
7356 max_epilog_size = mono_arch_max_epilog_size (cfg);
7358 /* we always allocate code in cfg->domain->code_mp to increase locality */
7359 cfg->code_size = cfg->code_len + max_epilog_size;
7360 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
7362 mono_domain_lock (cfg->domain);
7363 code = mono_code_manager_reserve (cfg->domain->code_mp, cfg->code_size);
7364 mono_domain_unlock (cfg->domain);
7366 memcpy (code, cfg->native_code, cfg->code_len);
7367 g_free (cfg->native_code);
7368 cfg->native_code = code;
7369 code = cfg->native_code + cfg->code_len;
7371 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
7373 cfg->epilog_begin = cfg->code_len;
7375 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7376 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
7378 cfg->code_len = code - cfg->native_code;
7380 mono_arch_emit_epilog (cfg);
7382 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7383 switch (patch_info->type) {
7384 case MONO_PATCH_INFO_ABS: {
7385 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
7387 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
7388 if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) &&
7389 strstr (cfg->method->name, info->name))
7391 * This is an icall wrapper, and this is a call to the
7396 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
7397 patch_info->data.name = info->name;
7401 MonoVTable *vtable = mono_find_class_init_trampoline_by_addr (patch_info->data.target);
7403 patch_info->type = MONO_PATCH_INFO_CLASS_INIT;
7404 patch_info->data.klass = vtable->klass;
7409 case MONO_PATCH_INFO_SWITCH: {
7411 mono_domain_lock (cfg->domain);
7412 table = mono_code_manager_reserve (cfg->domain->code_mp, sizeof (gpointer) * patch_info->table_size);
7413 mono_domain_unlock (cfg->domain);
7415 patch_info->ip.i = patch_info->ip.label->inst_c0;
7416 for (i = 0; i < patch_info->table_size; i++) {
7417 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
7419 patch_info->data.target = table;
7428 if (cfg->verbose_level > 0)
7429 g_print ("Method %s emitted at %p to %p [%s]\n",
7430 mono_method_full_name (cfg->method, TRUE),
7431 cfg->native_code, cfg->native_code + cfg->code_len, cfg->domain->friendly_name);
7433 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info, cfg->run_cctors);
7435 mono_domain_lock (cfg->domain);
7436 mono_code_manager_commit (cfg->domain->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
7437 mono_domain_unlock (cfg->domain);
7439 mono_arch_flush_icache (cfg->native_code, cfg->code_len);
7441 mono_debug_close_method (cfg);
7445 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
7450 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
7451 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
7453 if (cp->opcode == OP_ICONST) {
7454 if (cfg->opt & MONO_OPT_CONSPROP) {
7455 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
7459 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
7460 if (cfg->opt & MONO_OPT_COPYPROP) {
7461 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
7467 arity = mono_burg_arity [tree->opcode];
7470 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
7471 if (cfg->opt & MONO_OPT_CFOLD)
7472 mono_constant_fold_inst (tree, NULL);
7473 /* The opcode may have changed */
7474 if (mono_burg_arity [tree->opcode] > 1) {
7475 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
7476 if (cfg->opt & MONO_OPT_CFOLD)
7477 mono_constant_fold_inst (tree, NULL);
7479 mono_constant_fold_inst (tree, NULL);
7485 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
7489 switch (tree->opcode) {
7499 if (tree->ssa_op == MONO_SSA_NOP) {
7500 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7517 case OP_VOIDCALL_REG:
7518 case OP_VOIDCALLVIRT:
7520 MonoCallInst *call = (MonoCallInst *)tree;
7521 MonoMethodSignature *sig = call->signature;
7522 int i, byref = FALSE;
7524 for (i = 0; i < sig->param_count; i++) {
7525 if (sig->params [i]->byref) {
7532 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7540 arity = mono_burg_arity [tree->opcode];
7546 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7549 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7550 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
7553 g_assert_not_reached ();
7558 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
7560 MonoInst *tree = bb->code;
7566 for (; tree; tree = tree->next) {
7568 mono_cprop_copy_values (cfg, tree, acp);
7570 mono_cprop_invalidate_values (tree, acp, acp_size);
7572 if (tree->ssa_op == MONO_SSA_STORE &&
7573 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
7574 MonoInst *i1 = tree->inst_i1;
7576 acp [tree->inst_i0->inst_c0] = NULL;
7578 for (i = 0; i < acp_size; i++) {
7579 if (acp [i] && acp [i]->opcode != OP_ICONST &&
7580 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
7585 if (i1->opcode == OP_ICONST) {
7586 acp [tree->inst_i0->inst_c0] = i1;
7587 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
7589 if (i1->ssa_op == MONO_SSA_LOAD &&
7590 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
7591 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
7592 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
7593 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
7598 if (tree->opcode == CEE_BEQ) {
7599 g_assert (tree->inst_i0->opcode == OP_COMPARE);
7600 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
7601 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
7603 tree->opcode = CEE_BR;
7604 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
7605 tree->inst_target_bb = tree->inst_true_bb;
7607 tree->inst_target_bb = tree->inst_false_bb;
7616 mono_local_cprop (MonoCompile *cfg)
7621 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
7623 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7624 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
7625 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
7630 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, int parts)
7632 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
7633 guint8 *ip = (guint8 *)header->code;
7636 int dfn = 0, i, code_size_ratio;
7638 mono_jit_stats.methods_compiled++;
7639 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
7640 mono_profiler_method_jit (method);
7642 cfg = g_new0 (MonoCompile, 1);
7643 cfg->method = method;
7644 cfg->mempool = mono_mempool_new ();
7646 cfg->prof_options = mono_profiler_get_events ();
7647 cfg->run_cctors = run_cctors;
7648 cfg->bb_hash = g_hash_table_new (NULL, NULL);
7649 cfg->domain = domain;
7650 cfg->verbose_level = mini_verbose;
7651 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
7652 ((MonoMethodNormal *)method)->header->max_stack);
7654 if (cfg->verbose_level > 2)
7655 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
7658 * create MonoInst* which represents arguments and local variables
7660 mono_compile_create_vars (cfg);
7662 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
7663 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7664 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
7665 mono_destroy_compile (cfg);
7669 mono_jit_stats.basic_blocks += cfg->num_bblocks;
7670 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
7672 if (cfg->num_varinfo > 2000) {
7674 * we disable some optimizations if there are too many variables
7675 * because JIT time may become too expensive. The actual number needs
7676 * to be tweaked and eventually the non-linear algorithms should be fixed.
7678 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
7679 cfg->disable_ssa = TRUE;
7681 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
7683 /* Depth-first ordering on basic blocks */
7684 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
7686 if (cfg->opt & MONO_OPT_BRANCH)
7687 optimize_branches (cfg);
7689 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
7690 if (cfg->num_bblocks != dfn + 1) {
7693 cfg->num_bblocks = dfn + 1;
7695 if (!header->clauses) {
7696 /* remove unreachable code, because the code in them may be
7697 * inconsistent (access to dead variables for example) */
7698 for (bb = cfg->bb_entry; bb;) {
7699 MonoBasicBlock *bbn = bb->next_bb;
7701 if (bbn && bbn->region == -1 && !bbn->dfn) {
7702 if (cfg->verbose_level > 1)
7703 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
7704 bb->next_bb = bbn->next_bb;
7705 nullify_basic_block (bbn);
7713 if (cfg->opt & MONO_OPT_LOOP) {
7714 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
7715 mono_compute_natural_loops (cfg);
7718 /* after method_to_ir */
7722 //#define DEBUGSSA "logic_run"
7723 #define DEBUGSSA_CLASS "Tests"
7726 if (!header->num_clauses && !cfg->disable_ssa) {
7727 mono_local_cprop (cfg);
7728 mono_ssa_compute (cfg);
7732 /* fixme: add all optimizations which requires SSA */
7733 if (cfg->opt & (MONO_OPT_DEADCE)) {
7734 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
7735 mono_local_cprop (cfg);
7736 mono_ssa_compute (cfg);
7738 if (cfg->verbose_level >= 2) {
7745 /* after SSA translation */
7749 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
7750 if (cfg->comp_done & MONO_COMP_SSA) {
7751 mono_ssa_cprop (cfg);
7753 mono_local_cprop (cfg);
7757 if (cfg->comp_done & MONO_COMP_SSA) {
7758 mono_ssa_deadce (cfg);
7760 //mono_ssa_strength_reduction (cfg);
7762 mono_ssa_remove (cfg);
7764 if (cfg->opt & MONO_OPT_BRANCH)
7765 optimize_branches (cfg);
7768 /* after SSA removal */
7772 decompose_pass (cfg);
7774 if (cfg->opt & MONO_OPT_LINEARS) {
7777 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
7778 cfg->comp_done &= ~MONO_COMP_LIVENESS;
7779 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
7780 mono_analyze_liveness (cfg);
7782 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
7783 regs = mono_arch_get_global_int_regs (cfg);
7784 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
7788 //mono_print_code (cfg);
7792 /* variables are allocated after decompose, since decompose could create temps */
7793 mono_arch_allocate_vars (cfg);
7795 if (cfg->opt & MONO_OPT_CFOLD)
7796 mono_constant_fold (cfg);
7798 mini_select_instructions (cfg);
7801 if (cfg->verbose_level >= 2) {
7802 char *id = mono_method_full_name (cfg->method, FALSE);
7803 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
7807 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
7809 jinfo = g_new0 (MonoJitInfo, 1);
7810 jinfo->method = method;
7811 jinfo->code_start = cfg->native_code;
7812 jinfo->code_size = cfg->code_len;
7813 jinfo->used_regs = cfg->used_int_regs;
7814 jinfo->domain_neutral = (cfg->opt & MONO_OPT_SHARED) != 0;
7816 if (header->num_clauses) {
7819 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
7820 jinfo->num_clauses = header->num_clauses;
7821 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
7822 sizeof (MonoJitExceptionInfo) * header->num_clauses);
7824 for (i = 0; i < header->num_clauses; i++) {
7825 MonoExceptionClause *ec = &header->clauses [i];
7826 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
7827 MonoBasicBlock *tblock;
7829 ei->flags = ec->flags;
7831 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7832 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->token_or_filter);
7834 ei->data.filter = cfg->native_code + tblock->native_offset;
7836 ei->data.token = ec->token_or_filter;
7839 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
7841 ei->try_start = cfg->native_code + tblock->native_offset;
7842 g_assert (tblock->native_offset);
7843 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
7845 ei->try_end = cfg->native_code + tblock->native_offset;
7846 g_assert (tblock->native_offset);
7847 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
7849 ei->handler_start = cfg->native_code + tblock->native_offset;
7853 cfg->jit_info = jinfo;
7855 mono_jit_info_table_add (cfg->domain, jinfo);
7857 /* collect statistics */
7858 mono_jit_stats.allocated_code_size += cfg->code_len;
7859 code_size_ratio = cfg->code_len;
7860 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
7861 mono_jit_stats.biggest_method_size = code_size_ratio;
7862 mono_jit_stats.biggest_method = method;
7864 code_size_ratio = (code_size_ratio * 100) / ((MonoMethodNormal *)method)->header->code_size;
7865 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
7866 mono_jit_stats.max_code_size_ratio = code_size_ratio;
7867 mono_jit_stats.max_ratio_method = method;
7869 mono_jit_stats.native_code_size += cfg->code_len;
7871 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7872 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
7878 mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain)
7881 GHashTable *jit_code_hash;
7882 gpointer code = NULL;
7888 jit_code_hash = target_domain->jit_code_hash;
7890 #ifdef MONO_USE_AOT_COMPILER
7891 if (!mono_compile_aot && (opt & MONO_OPT_AOT)) {
7893 MonoDomain *domain = mono_domain_get ();
7895 mono_domain_lock (domain);
7897 mono_class_init (method->klass);
7898 if ((info = mono_aot_get_method (domain, method))) {
7899 g_hash_table_insert (domain->jit_code_hash, method, info);
7900 mono_domain_unlock (domain);
7901 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
7902 return info->code_start;
7905 mono_domain_unlock (domain);
7909 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
7910 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
7913 if (!method->addr) {
7914 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
7915 method->addr = mono_lookup_internal_call (method);
7917 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
7918 mono_lookup_pinvoke_call (method, NULL, NULL);
7920 #ifdef MONO_USE_EXC_TABLES
7921 if (mono_method_blittable (method)) {
7922 return method->addr;
7925 nm = mono_marshal_get_native_wrapper (method);
7926 return mono_compile_method (nm);
7928 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
7929 //mono_debug_add_wrapper (method, nm);
7930 #ifdef MONO_USE_EXC_TABLES
7933 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
7934 const char *name = method->name;
7937 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
7938 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
7939 /* FIXME: uhm, we need a wrapper to handle exceptions? */
7940 return (gpointer)mono_delegate_ctor;
7941 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
7942 nm = mono_marshal_get_delegate_invoke (method);
7943 return mono_jit_compile_method (nm);
7944 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
7945 nm = mono_marshal_get_delegate_begin_invoke (method);
7946 return mono_jit_compile_method (nm);
7947 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
7948 nm = mono_marshal_get_delegate_end_invoke (method);
7949 return mono_jit_compile_method (nm);
7955 cfg = mini_method_compile (method, opt, target_domain, TRUE, 0);
7957 mono_domain_lock (target_domain);
7959 /* Check if some other thread already did the job. In this case, we can
7960 discard the code this thread generated. */
7962 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
7963 /* We can't use a domain specific method in another domain */
7964 if ((target_domain == mono_domain_get ()) || info->domain_neutral) {
7965 code = info->code_start;
7966 // printf("Discarding code for method %s\n", method->name);
7971 g_hash_table_insert (jit_code_hash, method, cfg->jit_info);
7972 code = cfg->native_code;
7975 mono_destroy_compile (cfg);
7977 if (target_domain->jump_target_hash) {
7978 MonoJumpInfo patch_info;
7980 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
7982 patch_info.next = NULL;
7983 patch_info.ip.i = 0;
7984 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
7985 patch_info.data.method = method;
7986 g_hash_table_remove (target_domain->jump_target_hash, method);
7988 for (tmp = list; tmp; tmp = tmp->next)
7989 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info, TRUE);
7990 g_slist_free (list);
7993 mono_domain_unlock (target_domain);
7995 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
8000 mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt)
8002 /* FIXME: later copy the code from mono */
8003 MonoDomain *target_domain, *domain = mono_domain_get ();
8007 if (default_opt & MONO_OPT_SHARED)
8008 target_domain = mono_root_domain;
8010 target_domain = domain;
8012 mono_domain_lock (target_domain);
8014 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
8015 /* We can't use a domain specific method in another domain */
8016 if (! ((domain != target_domain) && !info->domain_neutral)) {
8017 mono_domain_unlock (target_domain);
8018 mono_jit_stats.methods_lookups++;
8019 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
8020 return info->code_start;
8024 mono_domain_unlock (target_domain);
8025 p = mono_jit_compile_method_inner (method, target_domain);
8030 mono_jit_compile_method (MonoMethod *method)
8032 return mono_jit_compile_method_with_opt (method, default_opt);
8036 mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method)
8038 MonoDomain *target_domain;
8041 if (default_opt & MONO_OPT_SHARED)
8042 target_domain = mono_root_domain;
8044 target_domain = domain;
8046 mono_domain_lock (target_domain);
8048 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
8049 /* We can't use a domain specific method in another domain */
8050 if (! ((domain != target_domain) && !info->domain_neutral)) {
8051 mono_domain_unlock (target_domain);
8052 mono_jit_stats.methods_lookups++;
8053 return info->code_start;
8057 mono_domain_unlock (target_domain);
8063 * mono_jit_runtime_invoke:
8064 * @method: the method to invoke
8065 * @obj: this pointer
8066 * @params: array of parameter values.
8067 * @exc: used to catch exceptions objects
8070 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
8073 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc);
8075 if (obj == NULL && !(method->flags & METHOD_ATTRIBUTE_STATIC) && !method->string_ctor && (method->wrapper_type == 0)) {
8076 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
8080 invoke = mono_marshal_get_runtime_invoke (method);
8081 runtime_invoke = mono_jit_compile_method (invoke);
8082 return runtime_invoke (obj, params, exc);
8085 #ifdef PLATFORM_WIN32
8086 #define GET_CONTEXT \
8087 struct sigcontext *ctx = (struct sigcontext*)_dummy;
8090 #define GET_CONTEXT \
8091 void *ctx = context;
8093 #define GET_CONTEXT \
8094 void **_p = (void **)&_dummy; \
8095 struct sigcontext *ctx = (struct sigcontext *)++_p;
8099 #ifdef MONO_ARCH_USE_SIGACTION
8100 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, siginfo_t *info, void *context)
8102 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy)
8106 SIG_HANDLER_SIGNATURE (sigfpe_signal_handler)
8111 exc = mono_get_exception_divide_by_zero ();
8113 mono_arch_handle_exception (ctx, exc, FALSE);
8117 SIG_HANDLER_SIGNATURE (sigill_signal_handler)
8121 exc = mono_get_exception_execution_engine ("SIGILL");
8123 mono_arch_handle_exception (ctx, exc, FALSE);
8126 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8129 sigsegv_signal_handler (int _dummy, siginfo_t *info, void *context)
8132 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
8133 struct sigcontext *ctx = (struct sigcontext *)&(((ucontext_t*)context)->uc_mcontext);
8135 /* Can't allocate memory using Boehm GC on altstack */
8136 if (jit_tls->stack_size &&
8137 ((guint8*)info->si_addr >= (guint8*)jit_tls->end_of_stack - jit_tls->stack_size) &&
8138 ((guint8*)info->si_addr < (guint8*)jit_tls->end_of_stack))
8139 exc = mono_domain_get ()->stack_overflow_ex;
8141 exc = mono_domain_get ()->null_reference_ex;
8143 mono_arch_handle_exception (ctx, exc, FALSE);
8149 SIG_HANDLER_SIGNATURE (sigsegv_signal_handler)
8153 mono_arch_handle_exception (ctx, NULL, FALSE);
8159 SIG_HANDLER_SIGNATURE (sigusr1_signal_handler)
8164 thread = mono_thread_current ();
8166 thread->abort_exc = mono_get_exception_thread_abort ();
8168 mono_arch_handle_exception (ctx, thread->abort_exc, FALSE);
8172 SIG_HANDLER_SIGNATURE (sigquit_signal_handler)
8177 exc = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
8179 mono_arch_handle_exception (ctx, exc, FALSE);
8183 SIG_HANDLER_SIGNATURE (sigint_signal_handler)
8188 exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
8190 mono_arch_handle_exception (ctx, exc, FALSE);
8193 #ifndef PLATFORM_WIN32
8195 add_signal_handler (int signo, gpointer handler)
8197 struct sigaction sa;
8199 #ifdef MONO_ARCH_USE_SIGACTION
8200 sa.sa_sigaction = handler;
8201 sigemptyset (&sa.sa_mask);
8202 sa.sa_flags = SA_SIGINFO;
8204 sa.sa_handler = handler;
8205 sigemptyset (&sa.sa_mask);
8208 g_assert (sigaction (signo, &sa, NULL) != -1);
8213 mono_runtime_install_handlers (void)
8215 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8216 struct sigaction sa;
8219 #ifdef PLATFORM_WIN32
8221 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
8222 win32_seh_set_handler(SIGILL, sigill_signal_handler);
8223 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
8224 if (getenv ("MONO_DEBUG"))
8225 win32_seh_set_handler(SIGINT, sigint_signal_handler);
8226 #else /* !PLATFORM_WIN32 */
8228 /* libpthreads has its own implementation of sigaction(),
8229 * but it seems to work well with our current exception
8230 * handlers. If not we must call syscall directly instead
8233 if (getenv ("MONO_DEBUG")) {
8234 add_signal_handler (SIGINT, sigint_signal_handler);
8237 add_signal_handler (SIGFPE, sigfpe_signal_handler);
8238 add_signal_handler (SIGQUIT, sigquit_signal_handler);
8239 add_signal_handler (SIGILL, sigill_signal_handler);
8240 add_signal_handler (mono_thread_get_abort_signal (), sigusr1_signal_handler);
8243 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8244 sa.sa_sigaction = sigsegv_signal_handler;
8245 sigemptyset (&sa.sa_mask);
8246 sa.sa_flags = SA_SIGINFO | SA_STACK;
8247 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
8249 add_signal_handler (SIGSEGV, sigsegv_signal_handler);
8252 #endif /* PLATFORM_WIN32 */
8255 /* mono_jit_create_remoting_trampoline:
8256 * @method: pointer to the method info
8258 * Creates a trampoline which calls the remoting functions. This
8259 * is used in the vtable of transparent proxies.
8261 * Returns: a pointer to the newly created code
8264 mono_jit_create_remoting_trampoline (MonoMethod *method)
8267 guint8 *addr = NULL;
8269 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
8270 (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
8271 nm = mono_marshal_get_remoting_invoke (method);
8272 addr = mono_compile_method (nm);
8274 addr = mono_compile_method (method);
8280 mini_init (const char *filename)
8284 mono_arch_cpu_init ();
8286 if (!g_thread_supported ())
8287 g_thread_init (NULL);
8289 mono_jit_tls_id = TlsAlloc ();
8290 setup_jit_tls_data ((gpointer)-1, mono_thread_abort);
8292 InitializeCriticalSection (&trampoline_hash_mutex);
8296 if (default_opt & MONO_OPT_AOT)
8299 mono_runtime_install_handlers ();
8300 mono_threads_install_cleanup (mini_thread_cleanup);
8302 #define JIT_TRAMPOLINES_WORK
8303 #ifdef JIT_TRAMPOLINES_WORK
8304 mono_install_compile_method (mono_jit_compile_method);
8305 mono_install_trampoline (mono_arch_create_jit_trampoline);
8306 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
8308 #define JIT_INVOKE_WORKS
8309 #ifdef JIT_INVOKE_WORKS
8310 mono_install_runtime_invoke (mono_jit_runtime_invoke);
8311 mono_install_handler (mono_arch_get_throw_exception ());
8313 mono_install_stack_walk (mono_jit_walk_stack);
8315 domain = mono_init (filename);
8318 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
8319 ves_icall_get_frame_info);
8320 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
8321 ves_icall_get_trace);
8322 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
8323 mono_runtime_install_handlers);
8326 create_helper_signature ();
8328 #define JIT_CALLS_WORK
8329 #ifdef JIT_CALLS_WORK
8330 /* Needs to be called here since register_jit_icall depends on it */
8331 mono_marshal_init ();
8333 mono_arch_register_lowlevel_calls ();
8334 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
8335 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
8336 mono_register_jit_icall (mono_trace_enter_method, "mono_trace_enter_method", NULL, TRUE);
8337 mono_register_jit_icall (mono_trace_leave_method, "mono_trace_leave_method", NULL, TRUE);
8339 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
8340 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
8342 /* fixme: we cant hanlde vararg methods this way, because the signature is not constant */
8343 //mono_register_jit_icall (ves_array_element_address, "ves_array_element_address", NULL);
8344 //mono_register_jit_icall (mono_array_new_va, "mono_array_new_va", NULL);
8346 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
8347 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
8348 helper_sig_void_ptr, TRUE);
8349 mono_register_jit_icall (mono_thread_get_pending_exception, "mono_thread_get_pending_exception", helper_sig_obj_void, FALSE);
8352 * NOTE, NOTE, NOTE, NOTE:
8353 * when adding emulation for some opcodes, remember to also add a dummy
8354 * rule to the burg files, because we need the arity information to be correct.
8356 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult, TRUE);
8357 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un, FALSE);
8358 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf, FALSE);
8359 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv, FALSE);
8360 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un, FALSE);
8361 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem, FALSE);
8362 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un, FALSE);
8364 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl, TRUE);
8365 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr, TRUE);
8366 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un, TRUE);
8368 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8, FALSE);
8369 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4, FALSE);
8370 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8, FALSE);
8371 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8, FALSE);
8373 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
8374 mono_register_opcode_emulation (OP_FCONV_TO_I8, "__emul_fconv_to_i8", helper_sig_long_double, mono_fconv_i8, FALSE);
8376 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
8377 mono_register_opcode_emulation (CEE_CONV_R_UN, "__emul_conv_r_un", helper_sig_double_int, mono_conv_to_r8_un, FALSE);
8379 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
8380 mono_register_opcode_emulation (OP_LCONV_TO_R8, "__emul_lconv_to_r8", helper_sig_double_long, mono_lconv_to_r8, FALSE);
8382 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
8383 mono_register_opcode_emulation (OP_LCONV_TO_R4, "__emul_lconv_to_r4", helper_sig_float_long, mono_lconv_to_r4, FALSE);
8385 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
8386 mono_register_opcode_emulation (OP_LCONV_TO_R_UN, "__emul_lconv_to_r8_un", helper_sig_double_long, mono_lconv_to_r8_un, FALSE);
8388 #ifdef MONO_ARCH_EMULATE_FREM
8389 mono_register_opcode_emulation (OP_FREM, "__emul_frem", helper_sig_double_double_double, fmod, FALSE);
8392 #if SIZEOF_VOID_P == 4
8393 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4, TRUE);
8396 #warning "fixme: add opcode emulation"
8400 /* other jit icalls */
8401 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
8402 helper_sig_ptr_ptr_ptr, FALSE);
8403 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
8404 mono_register_jit_icall (mono_get_special_static_data, "mono_get_special_static_data", helper_sig_ptr_int, FALSE);
8405 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
8406 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
8407 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
8408 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
8409 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
8410 mono_register_jit_icall (helper_stelem_ref_check, "helper_stelem_ref_check", helper_sig_stelem_ref_check, FALSE);
8411 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
8412 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
8413 mono_register_jit_icall (mono_object_new_fast, "mono_object_new_fast", helper_sig_object_new_specific, FALSE);
8414 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
8415 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
8416 mono_register_jit_icall (mono_string_to_utf16, "mono_string_to_utf16", helper_sig_ptr_obj, FALSE);
8417 mono_register_jit_icall (mono_string_from_utf16, "mono_string_from_utf16", helper_sig_obj_ptr, FALSE);
8418 mono_register_jit_icall (mono_string_new_wrapper, "mono_string_new_wrapper", helper_sig_obj_ptr, FALSE);
8419 mono_register_jit_icall (mono_string_to_utf8, "mono_string_to_utf8", helper_sig_ptr_obj, FALSE);
8420 mono_register_jit_icall (mono_string_to_bstr, "mono_string_to_bstr", helper_sig_ptr_obj, FALSE);
8421 mono_register_jit_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", helper_sig_ptr_obj, FALSE);
8422 mono_register_jit_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", helper_sig_ptr_obj, FALSE);
8423 mono_register_jit_icall (mono_string_builder_to_utf16, "mono_string_builder_to_utf16", helper_sig_ptr_obj, FALSE);
8424 mono_register_jit_icall (mono_array_to_savearray, "mono_array_to_savearray", helper_sig_ptr_obj, FALSE);
8425 mono_register_jit_icall (mono_array_to_lparray, "mono_array_to_lparray", helper_sig_ptr_obj, FALSE);
8426 mono_register_jit_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", helper_sig_ptr_obj, FALSE);
8427 mono_register_jit_icall (mono_marshal_string_array, "mono_marshal_string_array", helper_sig_ptr_obj, FALSE);
8428 mono_register_jit_icall (mono_marshal_string_array_to_unicode, "mono_marshal_string_array_to_unicode", helper_sig_ptr_obj, FALSE);
8429 mono_register_jit_icall (mono_string_utf8_to_builder, "mono_string_utf8_to_builder", helper_sig_void_ptr_ptr, FALSE);
8430 mono_register_jit_icall (mono_string_utf16_to_builder, "mono_string_utf16_to_builder", helper_sig_void_ptr_ptr, FALSE);
8431 mono_register_jit_icall (mono_marshal_free_array, "mono_marshal_free_array", helper_sig_void_ptr_ptr, FALSE);
8432 mono_register_jit_icall (mono_string_to_byvalstr, "mono_string_to_byvalstr", helper_sig_void_ptr_ptr_ptr, FALSE);
8433 mono_register_jit_icall (mono_string_to_byvalwstr, "mono_string_to_byvalwstr", helper_sig_void_ptr_ptr_ptr, FALSE);
8434 mono_register_jit_icall (g_free, "g_free", helper_sig_void_ptr, FALSE);
8435 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
8436 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
8437 mono_register_jit_icall (mono_ldftn_nosync, "mono_ldftn_nosync", helper_sig_compile, FALSE);
8438 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
8441 #define JIT_RUNTIME_WORKS
8442 #ifdef JIT_RUNTIME_WORKS
8443 mono_runtime_install_cleanup ((MonoDomainFunc)mini_cleanup);
8444 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
8447 mono_thread_attach (domain);
8451 MonoJitStats mono_jit_stats = {0};
8454 print_jit_stats (void)
8456 if (mono_jit_stats.enabled) {
8457 g_print ("Mono Jit statistics\n");
8458 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
8459 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
8460 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
8461 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
8462 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
8463 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
8464 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
8465 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
8466 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
8467 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
8468 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
8469 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
8470 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
8471 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
8472 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
8473 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
8474 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
8475 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
8477 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
8478 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
8479 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
8480 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
8481 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
8483 g_print ("\nGeneric instances: %ld\n", mono_stats.generic_instance_count);
8484 g_print ("Inflated methods: %ld\n", mono_stats.inflated_method_count);
8485 g_print ("Inflated types: %ld\n", mono_stats.inflated_type_count);
8486 g_print ("Generics metadata size: %ld\n", mono_stats.generics_metadata_size);
8491 mini_cleanup (MonoDomain *domain)
8494 * mono_runtime_cleanup() and mono_domain_finalize () need to
8495 * be called early since they need the execution engine still
8496 * fully working (mono_domain_finalize may invoke managed finalizers
8497 * and mono_runtime_cleanup will wait for other threads to finish).
8499 mono_domain_finalize (domain, 2000);
8501 mono_runtime_cleanup (domain);
8503 mono_profiler_shutdown ();
8505 mono_debug_cleanup ();
8507 #ifdef PLATFORM_WIN32
8508 win32_seh_cleanup();
8511 mono_domain_free (domain, TRUE);
8517 mono_set_defaults (int verbose_level, guint32 opts)
8519 mini_verbose = verbose_level;
8524 mono_precompile_assembly (MonoAssembly *ass, void *user_data)
8526 MonoImage *image = ass->image;
8530 if (mini_verbose > 0)
8531 printf ("PRECOMPILE: %s.\n", ass->image->name);
8533 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
8534 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
8535 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
8537 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
8541 if (mini_verbose > 1) {
8542 char * desc = mono_method_full_name (method, TRUE);
8543 g_print ("Compiling %d %s\n", count, desc);
8546 mono_compile_method (method);
8550 void mono_precompile_assemblies ()
8552 mono_assembly_foreach ((GFunc)mono_precompile_assembly, NULL);