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_print_tree(MonoInst *tree, int arity);
57 static gpointer mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt);
58 static gpointer mono_jit_compile_method (MonoMethod *method);
59 static gpointer mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method);
61 static void handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src,
62 const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native);
64 static void dec_foreach (MonoInst *tree, MonoCompile *cfg);
66 static int mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
67 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
68 guint inline_offset, gboolean is_virtual_call);
70 extern guint8 mono_burg_arity [];
71 /* helper methods signature */
72 static MonoMethodSignature *helper_sig_long_long_long = NULL;
73 static MonoMethodSignature *helper_sig_long_long_int = NULL;
74 static MonoMethodSignature *helper_sig_newarr = NULL;
75 static MonoMethodSignature *helper_sig_newarr_specific = NULL;
76 static MonoMethodSignature *helper_sig_ldstr = NULL;
77 static MonoMethodSignature *helper_sig_domain_get = NULL;
78 static MonoMethodSignature *helper_sig_object_new = NULL;
79 static MonoMethodSignature *helper_sig_object_new_specific = NULL;
80 static MonoMethodSignature *helper_sig_compile = NULL;
81 static MonoMethodSignature *helper_sig_compile_virt = NULL;
82 static MonoMethodSignature *helper_sig_obj_ptr = NULL;
83 static MonoMethodSignature *helper_sig_obj_void = NULL;
84 static MonoMethodSignature *helper_sig_ptr_void = NULL;
85 static MonoMethodSignature *helper_sig_void_ptr = NULL;
86 static MonoMethodSignature *helper_sig_void_obj = NULL;
87 static MonoMethodSignature *helper_sig_void_ptr_ptr = NULL;
88 static MonoMethodSignature *helper_sig_void_ptr_ptr_ptr = NULL;
89 static MonoMethodSignature *helper_sig_ptr_ptr_ptr = NULL;
90 static MonoMethodSignature *helper_sig_ptr_obj = NULL;
91 static MonoMethodSignature *helper_sig_ptr_int = NULL;
92 static MonoMethodSignature *helper_sig_initobj = NULL;
93 static MonoMethodSignature *helper_sig_memcpy = NULL;
94 static MonoMethodSignature *helper_sig_memset = NULL;
95 static MonoMethodSignature *helper_sig_ulong_double = NULL;
96 static MonoMethodSignature *helper_sig_long_double = NULL;
97 static MonoMethodSignature *helper_sig_double_long = NULL;
98 static MonoMethodSignature *helper_sig_double_int = NULL;
99 static MonoMethodSignature *helper_sig_float_long = NULL;
100 static MonoMethodSignature *helper_sig_double_double_double = NULL;
101 static MonoMethodSignature *helper_sig_uint_double = NULL;
102 static MonoMethodSignature *helper_sig_int_double = NULL;
103 static MonoMethodSignature *helper_sig_stelem_ref = NULL;
104 static MonoMethodSignature *helper_sig_stelem_ref_check = NULL;
105 static MonoMethodSignature *helper_sig_class_init_trampoline = NULL;
107 static guint32 default_opt = MONO_OPT_PEEPHOLE;
109 guint32 mono_jit_tls_id = -1;
110 MonoTraceSpec *mono_jit_trace_calls = NULL;
111 gboolean mono_break_on_exc = FALSE;
112 gboolean mono_compile_aot = FALSE;
114 static int mini_verbose = 0;
116 static CRITICAL_SECTION trampoline_hash_mutex;
118 static GHashTable *class_init_hash_addr = NULL;
120 static GHashTable *jump_trampoline_hash = NULL;
123 mono_running_on_valgrind (void)
125 #ifdef HAVE_VALGRIND_MEMCHECK_H
126 if (RUNNING_ON_VALGRIND)
135 #ifdef MONO_USE_EXC_TABLES
137 mono_type_blittable (MonoType *type)
157 case MONO_TYPE_VALUETYPE:
158 case MONO_TYPE_CLASS:
159 return type->data.klass->blittable;
169 mono_method_blittable (MonoMethod *method)
171 MonoMethodSignature *sig;
177 if (!mono_arch_has_unwind_info (method->addr)) {
181 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
184 sig = method->signature;
186 if (!mono_type_blittable (sig->ret))
189 for (i = 0; i < sig->param_count; i++)
190 if (!mono_type_blittable (sig->params [i]))
193 if (mono_method_has_marshal_info (method))
196 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && ((MonoMethodPInvoke*)method)->piflags & PINVOKE_ATTRIBUTE_SUPPORTS_LAST_ERROR)
204 G_GNUC_UNUSED static void
205 print_method_from_ip (void *ip)
210 MonoDomain *domain = mono_domain_get ();
212 ji = mono_jit_info_table_find (domain, ip);
214 g_print ("No method at %p\n", ip);
217 method = mono_method_full_name (ji->method, TRUE);
218 source = mono_debug_source_location_from_address (ji->method, (int) ip, NULL, domain);
220 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);
223 g_print ("%s\n", source);
231 * mono_method_same_domain:
233 * Determine whenever two compiled methods are in the same domain, thus
234 * the address of the callee can be embedded in the caller.
236 gboolean mono_method_same_domain (MonoJitInfo *caller, MonoJitInfo *callee)
238 if (!caller || !callee)
242 * If the call was made from domain-neutral to domain-specific
243 * code, we can't patch the call site.
245 if (caller->domain_neutral && !callee->domain_neutral)
248 if ((caller->method->klass == mono_defaults.appdomain_class) &&
249 (strstr (caller->method->name, "InvokeInDomain"))) {
250 /* The InvokeInDomain methods change the current appdomain */
258 mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token)
260 MonoJumpInfoToken *res = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoToken));
267 #define MONO_INIT_VARINFO(vi,id) do { \
268 (vi)->range.first_use.pos.bid = 0xffff; \
274 * Basic blocks have two numeric identifiers:
275 * dfn: Depth First Number
276 * block_num: unique ID assigned at bblock creation
278 #define NEW_BBLOCK(cfg) (mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)))
279 #define ADD_BBLOCK(cfg,bbhash,b) do { \
280 g_hash_table_insert (bbhash, (b)->cil_code, (b)); \
281 (b)->block_num = cfg->num_bblocks++; \
282 (b)->real_offset = real_offset; \
285 #define GET_BBLOCK(cfg,bbhash,tblock,ip) do { \
286 (tblock) = g_hash_table_lookup (bbhash, (ip)); \
288 if ((ip) >= end || (ip) < header->code) goto unverified; \
289 (tblock) = NEW_BBLOCK (cfg); \
290 (tblock)->cil_code = (ip); \
291 ADD_BBLOCK (cfg, (bbhash), (tblock)); \
295 #define CHECK_BBLOCK(target,ip,tblock) do { \
296 if ((target) < (ip) && !(tblock)->code) { \
297 bb_recheck = g_list_prepend (bb_recheck, (tblock)); \
298 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); \
302 #define NEW_ICONST(cfg,dest,val) do { \
303 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
304 (dest)->opcode = OP_ICONST; \
305 (dest)->inst_c0 = (val); \
306 (dest)->type = STACK_I4; \
309 /* FIXME: have a different definition of NEW_PCONST for 64 bit systems */
310 #define NEW_PCONST(cfg,dest,val) do { \
311 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
312 (dest)->opcode = OP_ICONST; \
313 (dest)->inst_p0 = (val); \
314 (dest)->type = STACK_PTR; \
317 #define NEW_CLASSCONST(cfg,dest,val) do { \
318 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
319 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
320 (dest)->inst_p0 = (val); \
321 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_CLASS; \
322 (dest)->type = STACK_PTR; \
325 #define NEW_IMAGECONST(cfg,dest,val) do { \
326 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
327 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
328 (dest)->inst_p0 = (val); \
329 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_IMAGE; \
330 (dest)->type = STACK_PTR; \
333 #define NEW_FIELDCONST(cfg,dest,field) do { \
334 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
335 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
336 (dest)->inst_p0 = (field); \
337 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_FIELD; \
338 (dest)->type = STACK_PTR; \
341 #define NEW_METHODCONST(cfg,dest,val) do { \
342 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
343 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
344 (dest)->inst_p0 = (val); \
345 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_METHODCONST; \
346 (dest)->type = STACK_PTR; \
349 #define NEW_VTABLECONST(cfg,dest,vtable) do { \
350 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
351 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
352 (dest)->inst_p0 = mono_compile_aot ? (gpointer)((vtable)->klass) : (vtable); \
353 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_VTABLE; \
354 (dest)->type = STACK_PTR; \
357 #define NEW_SFLDACONST(cfg,dest,field) do { \
358 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
359 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
360 (dest)->inst_p0 = (field); \
361 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_SFLDA; \
362 (dest)->type = STACK_PTR; \
365 #define NEW_LDSTRCONST(cfg,dest,image,token) do { \
366 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
367 (dest)->opcode = OP_AOTCONST; \
368 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
369 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_LDSTR; \
370 (dest)->type = STACK_OBJ; \
373 #define NEW_TYPE_FROM_HANDLE_CONST(cfg,dest,image,token) do { \
374 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
375 (dest)->opcode = OP_AOTCONST; \
376 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
377 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_TYPE_FROM_HANDLE; \
378 (dest)->type = STACK_OBJ; \
381 #define NEW_LDTOKENCONST(cfg,dest,image,token) do { \
382 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
383 (dest)->opcode = OP_AOTCONST; \
384 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
385 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_LDTOKEN; \
386 (dest)->type = STACK_PTR; \
389 #define NEW_DOMAINCONST(cfg,dest) do { \
390 if (cfg->opt & MONO_OPT_SHARED) { \
391 NEW_TEMPLOAD (cfg, dest, mono_get_domainvar (cfg)->inst_c0); \
393 NEW_PCONST (cfg, dest, (cfg)->domain); \
397 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
399 #define NEW_ARGLOAD(cfg,dest,num) do { \
400 if (arg_array [(num)]->opcode == OP_ICONST) (dest) = arg_array [(num)]; else { \
401 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
402 (dest)->ssa_op = MONO_SSA_LOAD; \
403 (dest)->inst_i0 = arg_array [(num)]; \
404 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
405 type_to_eval_stack_type (param_types [(num)], (dest)); \
406 (dest)->klass = (dest)->inst_i0->klass; \
409 #define NEW_LOCLOAD(cfg,dest,num) do { \
410 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
411 (dest)->ssa_op = MONO_SSA_LOAD; \
412 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
413 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
414 type_to_eval_stack_type (header->locals [(num)], (dest)); \
415 (dest)->klass = (dest)->inst_i0->klass; \
418 #define NEW_LOCLOADA(cfg,dest,num) do { \
419 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
420 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
421 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
422 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
423 (dest)->opcode = OP_LDADDR; \
424 (dest)->type = STACK_MP; \
425 (dest)->klass = (dest)->inst_i0->klass; \
426 if (!MONO_TYPE_ISSTRUCT (header->locals [(num)])) \
427 (cfg)->disable_ssa = TRUE; \
430 #define NEW_RETLOADA(cfg,dest) do { \
431 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
432 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
433 (dest)->inst_i0 = (cfg)->ret; \
434 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
435 (dest)->opcode = CEE_LDIND_I; \
436 (dest)->type = STACK_MP; \
437 (dest)->klass = (dest)->inst_i0->klass; \
438 (cfg)->disable_ssa = TRUE; \
441 #define NEW_ARGLOADA(cfg,dest,num) do { \
442 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
443 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
444 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
445 (dest)->inst_i0 = arg_array [(num)]; \
446 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
447 (dest)->opcode = OP_LDADDR; \
448 (dest)->type = STACK_MP; \
449 (dest)->klass = (dest)->inst_i0->klass; \
450 (cfg)->disable_ssa = TRUE; \
453 #define NEW_TEMPLOAD(cfg,dest,num) do { \
454 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
455 (dest)->ssa_op = MONO_SSA_LOAD; \
456 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
457 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
458 type_to_eval_stack_type ((dest)->inst_i0->inst_vtype, (dest)); \
459 (dest)->klass = (dest)->inst_i0->klass; \
462 #define NEW_TEMPLOADA(cfg,dest,num) do { \
463 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
464 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
465 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
466 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
467 (dest)->opcode = OP_LDADDR; \
468 (dest)->type = STACK_MP; \
469 (dest)->klass = (dest)->inst_i0->klass; \
470 if (!MONO_TYPE_ISSTRUCT (cfg->varinfo [(num)]->inst_vtype)) \
471 (cfg)->disable_ssa = TRUE; \
475 #define NEW_INDLOAD(cfg,dest,addr,vtype) do { \
476 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
477 (dest)->inst_left = addr; \
478 (dest)->opcode = mono_type_to_ldind (vtype); \
479 type_to_eval_stack_type (vtype, (dest)); \
480 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
483 #define NEW_INDSTORE(cfg,dest,addr,value,vtype) do { \
484 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
485 (dest)->inst_i0 = addr; \
486 (dest)->opcode = mono_type_to_stind (vtype); \
487 (dest)->inst_i1 = (value); \
488 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
491 #define NEW_TEMPSTORE(cfg,dest,num,inst) do { \
492 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
493 (dest)->ssa_op = MONO_SSA_STORE; \
494 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
495 (dest)->opcode = mono_type_to_stind ((dest)->inst_i0->inst_vtype); \
496 (dest)->inst_i1 = (inst); \
497 (dest)->klass = (dest)->inst_i0->klass; \
500 #define NEW_LOCSTORE(cfg,dest,num,inst) do { \
501 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
502 (dest)->opcode = mono_type_to_stind (header->locals [(num)]); \
503 (dest)->ssa_op = MONO_SSA_STORE; \
504 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
505 (dest)->inst_i1 = (inst); \
506 (dest)->klass = (dest)->inst_i0->klass; \
509 #define NEW_ARGSTORE(cfg,dest,num,inst) do { \
510 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
511 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
512 (dest)->opcode = mono_type_to_stind (param_types [(num)]); \
513 (dest)->ssa_op = MONO_SSA_STORE; \
514 (dest)->inst_i0 = arg_array [(num)]; \
515 (dest)->inst_i1 = (inst); \
516 (dest)->klass = (dest)->inst_i0->klass; \
519 #define ADD_BINOP(op) do { \
520 MONO_INST_NEW (cfg, ins, (op)); \
521 ins->cil_code = ip; \
523 ins->inst_i0 = sp [0]; \
524 ins->inst_i1 = sp [1]; \
526 type_from_op (ins); \
530 #define ADD_UNOP(op) do { \
531 MONO_INST_NEW (cfg, ins, (op)); \
532 ins->cil_code = ip; \
534 ins->inst_i0 = sp [0]; \
536 type_from_op (ins); \
540 #define ADD_BINCOND(next_block) do { \
542 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
544 cmp->inst_i0 = sp [0]; \
545 cmp->inst_i1 = sp [1]; \
546 cmp->cil_code = ins->cil_code; \
547 type_from_op (cmp); \
549 ins->inst_i0 = cmp; \
550 MONO_ADD_INS (bblock, ins); \
551 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
552 GET_BBLOCK (cfg, bbhash, tblock, target); \
553 link_bblock (cfg, bblock, tblock); \
554 ins->inst_true_bb = tblock; \
555 CHECK_BBLOCK (target, ip, tblock); \
556 if ((next_block)) { \
557 link_bblock (cfg, bblock, (next_block)); \
558 ins->inst_false_bb = (next_block); \
559 start_new_bblock = 1; \
561 GET_BBLOCK (cfg, bbhash, tblock, ip); \
562 link_bblock (cfg, bblock, tblock); \
563 ins->inst_false_bb = tblock; \
564 start_new_bblock = 2; \
568 /* FIXME: handle float, long ... */
569 #define ADD_UNCOND(istrue) do { \
571 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
573 cmp->inst_i0 = sp [0]; \
574 switch (cmp->inst_i0->type) { \
576 cmp->inst_i1 = zero_int64; break; \
578 cmp->inst_i1 = zero_r8; break; \
581 cmp->inst_i1 = zero_ptr; break; \
583 cmp->inst_i1 = zero_obj; break; \
585 cmp->inst_i1 = zero_int32; \
587 cmp->cil_code = ins->cil_code; \
588 type_from_op (cmp); \
590 ins->inst_i0 = cmp; \
591 ins->opcode = (istrue)? CEE_BNE_UN: CEE_BEQ; \
592 MONO_ADD_INS (bblock, ins); \
593 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
594 GET_BBLOCK (cfg, bbhash, tblock, target); \
595 link_bblock (cfg, bblock, tblock); \
596 ins->inst_true_bb = tblock; \
597 CHECK_BBLOCK (target, ip, tblock); \
598 GET_BBLOCK (cfg, bbhash, tblock, ip); \
599 link_bblock (cfg, bblock, tblock); \
600 ins->inst_false_bb = tblock; \
601 start_new_bblock = 2; \
604 #define NEW_LDELEMA(cfg,dest,sp,k) do { \
605 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
606 (dest)->opcode = CEE_LDELEMA; \
607 (dest)->inst_left = (sp) [0]; \
608 (dest)->inst_right = (sp) [1]; \
609 (dest)->type = STACK_MP; \
610 (dest)->klass = (k); \
613 #define NEW_GROUP(cfg,dest,el1,el2) do { \
614 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
615 (dest)->opcode = OP_GROUP; \
616 (dest)->inst_left = (el1); \
617 (dest)->inst_right = (el2); \
622 compare_bblock (gconstpointer a, gconstpointer b)
624 const MonoBasicBlock *b1 = a;
625 const MonoBasicBlock *b2 = b;
627 return b2->cil_code - b1->cil_code;
632 * link_bblock: Links two basic blocks
634 * links two basic blocks in the control flow graph, the 'from'
635 * argument is the starting block and the 'to' argument is the block
636 * the control flow ends to after 'from'.
639 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
641 MonoBasicBlock **newa;
645 if (from->cil_code) {
647 g_print ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
649 g_print ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
652 g_print ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
654 g_print ("edge from entry to exit\n");
658 for (i = 0; i < from->out_count; ++i) {
659 if (to == from->out_bb [i]) {
665 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
666 for (i = 0; i < from->out_count; ++i) {
667 newa [i] = from->out_bb [i];
675 for (i = 0; i < to->in_count; ++i) {
676 if (from == to->in_bb [i]) {
682 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
683 for (i = 0; i < to->in_count; ++i) {
684 newa [i] = to->in_bb [i];
693 * mono_find_block_region:
695 * We mark each basic block with a region ID. We use that to avoid BB
696 * optimizations when blocks are in different regions.
699 * A region token that encodes where this region is, and information
700 * about the clause owner for this block.
702 * The region encodes the try/catch/filter clause that owns this block
703 * as well as the type. -1 is a special value that represents a block
704 * that is in none of try/catch/filter.
707 mono_find_block_region (MonoCompile *cfg, int offset, int *filter_lengths)
709 MonoMethod *method = cfg->method;
710 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
711 MonoExceptionClause *clause;
714 /* first search for handlers and filters */
715 for (i = 0; i < header->num_clauses; ++i) {
716 clause = &header->clauses [i];
717 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->token_or_filter) &&
718 (offset < (clause->token_or_filter + filter_lengths [i])))
719 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
721 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
722 if (clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)
723 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
725 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
729 /* search the try blocks */
730 for (i = 0; i < header->num_clauses; ++i) {
731 clause = &header->clauses [i];
732 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
733 return ((i + 1) << 8) | clause->flags;
740 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
742 MonoMethod *method = cfg->method;
743 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
744 MonoExceptionClause *clause;
745 MonoBasicBlock *handler;
749 for (i = 0; i < header->num_clauses; ++i) {
750 clause = &header->clauses [i];
751 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
752 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
753 if (clause->flags == type) {
754 handler = g_hash_table_lookup (cfg->bb_hash, header->code + clause->handler_offset);
756 res = g_list_append (res, handler);
764 mono_find_spvar_for_region (MonoCompile *cfg, int region)
766 return g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
770 mono_create_spvar_for_region (MonoCompile *cfg, int region)
774 var = g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
778 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
779 /* prevent it from being register allocated */
780 var->flags |= MONO_INST_INDIRECT;
782 g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
786 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
790 array [*dfn] = start;
791 /*g_print ("visit %d at %p\n", *dfn, start->cil_code);*/
792 for (i = 0; i < start->out_count; ++i) {
793 if (start->out_bb [i]->dfn)
796 start->out_bb [i]->dfn = *dfn;
797 start->out_bb [i]->df_parent = start;
798 array [*dfn] = start->out_bb [i];
799 df_visit (start->out_bb [i], dfn, array);
805 MonoBasicBlock *best;
809 previous_foreach (gconstpointer key, gpointer val, gpointer data)
811 PrevStruct *p = data;
812 MonoBasicBlock *bb = val;
813 //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,
814 //bb->method == p->best->method, bb->cil_code < p->code, bb->cil_code > p->best->cil_code);
816 if (bb->cil_code && bb->cil_code < p->code && bb->cil_code > p->best->cil_code)
820 static MonoBasicBlock*
821 find_previous (GHashTable *bb_hash, MonoBasicBlock *start, const guchar *code) {
827 g_hash_table_foreach (bb_hash, (GHFunc)previous_foreach, &p);
832 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
841 * FIXME: take into account all the details:
842 * second may have been the target of more than one bblock
844 second->out_count = first->out_count;
845 second->out_bb = first->out_bb;
847 for (i = 0; i < first->out_count; ++i) {
848 bb = first->out_bb [i];
849 for (j = 0; j < bb->in_count; ++j) {
850 if (bb->in_bb [j] == first)
851 bb->in_bb [j] = second;
855 first->out_count = 0;
856 first->out_bb = NULL;
857 link_bblock (cfg, first, second);
859 second->last_ins = first->last_ins;
861 /*g_print ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
862 for (inst = first->code; inst && inst->next; inst = inst->next) {
863 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
864 g_print ("found %p: %s", inst->next->cil_code, code);
866 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
867 second->code = inst->next;
869 first->last_ins = inst;
870 second->next_bb = first->next_bb;
871 first->next_bb = second;
876 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
882 mono_type_to_ldind (MonoType *type)
894 case MONO_TYPE_BOOLEAN:
908 case MONO_TYPE_FNPTR:
910 case MONO_TYPE_CLASS:
911 case MONO_TYPE_STRING:
912 case MONO_TYPE_OBJECT:
913 case MONO_TYPE_SZARRAY:
914 case MONO_TYPE_ARRAY:
915 return CEE_LDIND_REF;
923 case MONO_TYPE_VALUETYPE:
924 if (type->data.klass->enumtype) {
925 t = type->data.klass->enum_basetype->type;
929 case MONO_TYPE_TYPEDBYREF:
931 case MONO_TYPE_GENERICINST:
932 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE)
934 return CEE_LDIND_REF;
936 g_error ("unknown type 0x%02x in type_to_ldind", type->type);
942 mono_type_to_stind (MonoType *type)
953 case MONO_TYPE_BOOLEAN:
965 case MONO_TYPE_FNPTR:
967 case MONO_TYPE_CLASS:
968 case MONO_TYPE_STRING:
969 case MONO_TYPE_OBJECT:
970 case MONO_TYPE_SZARRAY:
971 case MONO_TYPE_ARRAY:
972 return CEE_STIND_REF;
980 case MONO_TYPE_VALUETYPE:
981 if (type->data.klass->enumtype) {
982 t = type->data.klass->enum_basetype->type;
986 case MONO_TYPE_TYPEDBYREF:
988 case MONO_TYPE_GENERICINST:
989 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE)
991 return CEE_STIND_REF;
993 g_error ("unknown type 0x%02x in type_to_stind", type->type);
999 * Returns the type used in the eval stack when @type is loaded.
1000 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
1003 type_to_eval_stack_type (MonoType *type, MonoInst *inst) {
1007 inst->type = STACK_MP;
1015 case MONO_TYPE_BOOLEAN:
1018 case MONO_TYPE_CHAR:
1021 inst->type = STACK_I4;
1026 case MONO_TYPE_FNPTR:
1027 inst->type = STACK_PTR;
1029 case MONO_TYPE_CLASS:
1030 case MONO_TYPE_STRING:
1031 case MONO_TYPE_OBJECT:
1032 case MONO_TYPE_SZARRAY:
1033 case MONO_TYPE_ARRAY:
1034 inst->type = STACK_OBJ;
1038 inst->type = STACK_I8;
1042 inst->type = STACK_R8;
1044 case MONO_TYPE_VALUETYPE:
1045 if (type->data.klass->enumtype) {
1046 t = type->data.klass->enum_basetype->type;
1049 inst->klass = type->data.klass;
1050 inst->type = STACK_VTYPE;
1053 case MONO_TYPE_TYPEDBYREF:
1054 inst->klass = mono_defaults.typed_reference_class;
1055 inst->type = STACK_VTYPE;
1057 case MONO_TYPE_GENERICINST:
1058 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE) {
1059 inst->klass = mono_class_from_mono_type (type);
1060 inst->type = STACK_VTYPE;
1062 inst->type = STACK_OBJ;
1066 g_error ("unknown type 0x%02x in eval stack type", type->type);
1071 * The following tables are used to quickly validate the IL code in type_from_op ().
1074 bin_num_table [STACK_MAX] [STACK_MAX] = {
1075 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1076 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1077 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1078 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1079 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
1080 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
1081 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1082 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1087 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
1090 /* reduce the size of this table */
1092 bin_int_table [STACK_MAX] [STACK_MAX] = {
1093 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1094 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1095 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1096 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1097 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, 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}
1104 bin_comp_table [STACK_MAX] [STACK_MAX] = {
1106 {0, 1, 0, 1, 0, 0, 4, 0},
1107 {0, 0, 1, 0, 0, 0, 0, 0},
1108 {0, 1, 0, 1, 0, 2, 4, 0},
1109 {0, 0, 0, 0, 1, 0, 0, 0},
1110 {0, 0, 0, 2, 0, 1, 0, 0},
1111 {0, 4, 0, 4, 0, 0, 3, 0},
1112 {0, 0, 0, 0, 0, 0, 0, 0},
1115 /* reduce the size of this table */
1117 shift_table [STACK_MAX] [STACK_MAX] = {
1118 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1119 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1120 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1121 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1122 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, 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}
1129 * Tables to map from the non-specific opcode to the matching
1130 * type-specific opcode.
1132 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
1133 static const guint16
1134 binops_op_map [STACK_MAX] = {
1135 0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, 0
1138 /* handles from CEE_NEG to CEE_CONV_U8 */
1139 static const guint16
1140 unops_op_map [STACK_MAX] = {
1141 0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, 0
1144 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
1145 static const guint16
1146 ovfops_op_map [STACK_MAX] = {
1147 0, 0, OP_LCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_FCONV_TO_U2-CEE_CONV_U2, 0
1150 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
1151 static const guint16
1152 ovf2ops_op_map [STACK_MAX] = {
1153 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
1156 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
1157 static const guint16
1158 ovf3ops_op_map [STACK_MAX] = {
1159 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
1162 /* handles from CEE_CEQ to CEE_CLT_UN */
1163 static const guint16
1164 ceqops_op_map [STACK_MAX] = {
1165 0, 0, OP_LCEQ-CEE_CEQ, OP_PCEQ-CEE_CEQ, OP_FCEQ-CEE_CEQ, 0
1169 * Sets ins->type (the type on the eval stack) according to the
1170 * type of the opcode and the arguments to it.
1171 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1173 * FIXME: this function sets ins->type unconditionally in some cases, but
1174 * it should set it to invalid for some types (a conv.x on an object)
1177 type_from_op (MonoInst *ins) {
1178 switch (ins->opcode) {
1185 /* FIXME: check unverifiable args for STACK_MP */
1186 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1187 ins->opcode += binops_op_map [ins->type];
1194 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1195 ins->opcode += binops_op_map [ins->type];
1200 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1201 ins->opcode += binops_op_map [ins->type];
1204 /* FIXME: handle some specifics with ins->next->type */
1205 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1212 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1213 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1217 ins->type = neg_table [ins->inst_i0->type];
1218 ins->opcode += unops_op_map [ins->type];
1221 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1222 ins->type = ins->inst_i0->type;
1224 ins->type = STACK_INV;
1225 ins->opcode += unops_op_map [ins->type];
1231 ins->type = STACK_I4;
1232 ins->opcode += unops_op_map [ins->inst_i0->type];
1235 ins->type = STACK_R8;
1236 switch (ins->inst_i0->type) {
1241 ins->opcode = OP_LCONV_TO_R_UN;
1245 case CEE_CONV_OVF_I1:
1246 case CEE_CONV_OVF_U1:
1247 case CEE_CONV_OVF_I2:
1248 case CEE_CONV_OVF_U2:
1249 case CEE_CONV_OVF_I4:
1250 case CEE_CONV_OVF_U4:
1251 ins->type = STACK_I4;
1252 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1254 case CEE_CONV_OVF_I_UN:
1255 case CEE_CONV_OVF_U_UN:
1256 ins->type = STACK_PTR;
1257 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1259 case CEE_CONV_OVF_I1_UN:
1260 case CEE_CONV_OVF_I2_UN:
1261 case CEE_CONV_OVF_I4_UN:
1262 case CEE_CONV_OVF_U1_UN:
1263 case CEE_CONV_OVF_U2_UN:
1264 case CEE_CONV_OVF_U4_UN:
1265 ins->type = STACK_I4;
1266 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1269 ins->type = STACK_PTR;
1270 switch (ins->inst_i0->type) {
1276 ins->opcode = OP_LCONV_TO_U;
1279 ins->opcode = OP_FCONV_TO_U;
1285 ins->type = STACK_I8;
1286 ins->opcode += unops_op_map [ins->inst_i0->type];
1288 case CEE_CONV_OVF_I8:
1289 case CEE_CONV_OVF_U8:
1290 ins->type = STACK_I8;
1291 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1293 case CEE_CONV_OVF_U8_UN:
1294 case CEE_CONV_OVF_I8_UN:
1295 ins->type = STACK_I8;
1296 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1300 ins->type = STACK_R8;
1301 ins->opcode += unops_op_map [ins->inst_i0->type];
1304 ins->type = STACK_R8;
1308 ins->type = STACK_I4;
1309 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1312 case CEE_CONV_OVF_I:
1313 case CEE_CONV_OVF_U:
1314 ins->type = STACK_PTR;
1315 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1318 case CEE_ADD_OVF_UN:
1320 case CEE_MUL_OVF_UN:
1322 case CEE_SUB_OVF_UN:
1323 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1324 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1327 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1334 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_MP, STACK_R8, STACK_R8, STACK_OBJ
1337 /* map ldelem.x to the matching ldind.x opcode */
1339 ldelem_to_ldind [] = {
1353 /* map stelem.x to the matching stind.x opcode */
1355 stelem_to_stind [] = {
1369 param_table [STACK_MAX] [STACK_MAX] = {
1374 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1378 switch (args->type) {
1388 for (i = 0; i < sig->param_count; ++i) {
1389 switch (args [i].type) {
1393 if (!sig->params [i]->byref)
1397 if (sig->params [i]->byref)
1399 switch (sig->params [i]->type) {
1400 case MONO_TYPE_CLASS:
1401 case MONO_TYPE_STRING:
1402 case MONO_TYPE_OBJECT:
1403 case MONO_TYPE_SZARRAY:
1404 case MONO_TYPE_ARRAY:
1411 if (sig->params [i]->byref)
1413 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1422 /*if (!param_table [args [i].type] [sig->params [i]->type])
1430 * When we need a pointer to the current domain many times in a method, we
1431 * call mono_domain_get() once and we store the result in a local variable.
1432 * This function returns the variable that represents the MonoDomain*.
1434 inline static MonoInst *
1435 mono_get_domainvar (MonoCompile *cfg)
1437 if (!cfg->domainvar)
1438 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1439 return cfg->domainvar;
1443 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
1446 int num = cfg->num_varinfo;
1448 if ((num + 1) >= cfg->varinfo_count) {
1449 cfg->varinfo_count = (cfg->varinfo_count + 2) * 2;
1450 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
1451 cfg->vars = (MonoMethodVar **)g_realloc (cfg->vars, sizeof (MonoMethodVar*) * cfg->varinfo_count);
1454 /*g_print ("created temp %d of type 0x%x\n", num, type->type);*/
1455 mono_jit_stats.allocate_var++;
1457 MONO_INST_NEW (cfg, inst, opcode);
1458 inst->inst_c0 = num;
1459 inst->inst_vtype = type;
1460 inst->klass = mono_class_from_mono_type (type);
1461 /* if set to 1 the variable is native */
1464 cfg->varinfo [num] = inst;
1466 cfg->vars [num] = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMethodVar));
1467 MONO_INIT_VARINFO (cfg->vars [num], num);
1470 //g_print ("created temp %d of type %s\n", num, mono_type_get_name (type));
1475 type_from_stack_type (MonoInst *ins) {
1476 switch (ins->type) {
1477 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1478 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1479 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1480 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1481 case STACK_MP: return &mono_defaults.int_class->byval_arg;
1482 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1483 case STACK_VTYPE: return &ins->klass->byval_arg;
1485 g_error ("stack type %d to montype not handled\n", ins->type);
1491 array_access_to_klass (int opcode)
1495 return mono_defaults.byte_class;
1497 return mono_defaults.uint16_class;
1500 return mono_defaults.int_class;
1503 return mono_defaults.sbyte_class;
1506 return mono_defaults.int16_class;
1509 return mono_defaults.int32_class;
1511 return mono_defaults.uint32_class;
1514 return mono_defaults.int64_class;
1517 return mono_defaults.single_class;
1520 return mono_defaults.double_class;
1521 case CEE_LDELEM_REF:
1522 case CEE_STELEM_REF:
1523 return mono_defaults.object_class;
1525 g_assert_not_reached ();
1531 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
1535 MONO_ADD_INS (bb, inst);
1538 switch (bb->last_ins->opcode) {
1552 while (prev->next && prev->next != bb->last_ins)
1554 if (prev == bb->code) {
1555 if (bb->last_ins == bb->code) {
1556 inst->next = bb->code;
1559 inst->next = prev->next;
1563 inst->next = bb->last_ins;
1567 // g_warning ("handle conditional jump in add_ins_to_end ()\n");
1569 MONO_ADD_INS (bb, inst);
1575 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
1577 MonoInst *inst, *load;
1579 NEW_TEMPLOAD (cfg, load, src);
1581 NEW_TEMPSTORE (cfg, inst, dest, load);
1582 if (inst->opcode == CEE_STOBJ) {
1583 NEW_TEMPLOADA (cfg, inst, dest);
1584 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE);
1586 inst->cil_code = NULL;
1587 mono_add_ins_to_end (bb, inst);
1592 * We try to share variables when possible
1595 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1600 /* inlining can result in deeper stacks */
1601 if (slot >= ((MonoMethodNormal *)cfg->method)->header->max_stack)
1602 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1604 pos = ins->type - 1 + slot * STACK_MAX;
1606 switch (ins->type) {
1613 if ((vnum = cfg->intvars [pos]))
1614 return cfg->varinfo [vnum];
1615 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1616 cfg->intvars [pos] = res->inst_c0;
1619 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1625 * This function is called to handle items that are left on the evaluation stack
1626 * at basic block boundaries. What happens is that we save the values to local variables
1627 * and we reload them later when first entering the target basic block (with the
1628 * handle_loaded_temps () function).
1629 * It is also used to handle items on the stack in store opcodes, since it is
1630 * possible that the variable to be stored into is already on the stack, in
1631 * which case its old value should be used.
1632 * A single joint point will use the same variables (stored in the array bb->out_stack or
1633 * bb->in_stack, if the basic block is before or after the joint point).
1636 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count) {
1638 MonoBasicBlock *outb;
1639 MonoInst *inst, **locals;
1643 if (cfg->verbose_level > 3)
1644 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
1645 if (!bb->out_scount) {
1647 bb->out_scount = count;
1648 //g_print ("bblock %d has out:", bb->block_num);
1649 for (i = 0; i < bb->out_count; ++i) {
1650 outb = bb->out_bb [i];
1651 //g_print (" %d", outb->block_num);
1652 if (outb->in_stack) {
1654 bb->out_stack = outb->in_stack;
1660 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1661 for (i = 0; i < count; ++i) {
1663 /* try to reuse temps already allocated for this purpouse, if they occupy the same
1664 * stack slot and if they are of the same type. */
1665 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1667 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1672 locals = bb->out_stack;
1673 for (i = 0; i < count; ++i) {
1674 /* add store ops at the end of the bb, before the branch */
1675 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1676 if (inst->opcode == CEE_STOBJ) {
1677 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
1678 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE);
1680 inst->cil_code = sp [i]->cil_code;
1681 mono_add_ins_to_end (bb, inst);
1683 if (cfg->verbose_level > 3)
1684 g_print ("storing %d to temp %d\n", i, locals [i]->inst_c0);
1687 for (i = 0; i < bb->out_count; ++i) {
1688 outb = bb->out_bb [i];
1689 if (outb->in_scount)
1690 continue; /* check they are the same locals */
1691 outb->in_scount = count;
1692 outb->in_stack = locals;
1698 ret_type_to_call_opcode (MonoType *type, int calli, int virt)
1703 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1707 case MONO_TYPE_VOID:
1708 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
1711 case MONO_TYPE_BOOLEAN:
1714 case MONO_TYPE_CHAR:
1717 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1721 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1722 case MONO_TYPE_CLASS:
1723 case MONO_TYPE_STRING:
1724 case MONO_TYPE_OBJECT:
1725 case MONO_TYPE_SZARRAY:
1726 case MONO_TYPE_ARRAY:
1727 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1730 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
1733 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
1734 case MONO_TYPE_VALUETYPE:
1735 if (type->data.klass->enumtype) {
1736 t = type->data.klass->enum_basetype->type;
1739 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1740 case MONO_TYPE_TYPEDBYREF:
1741 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1742 case MONO_TYPE_GENERICINST:
1743 t = type->data.generic_inst->generic_type->type;
1746 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
1752 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
1754 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
1756 ji->ip.label = label;
1757 ji->type = MONO_PATCH_INFO_SWITCH;
1758 ji->data.table = bbs;
1759 ji->next = cfg->patch_info;
1760 ji->table_size = num_blocks;
1761 cfg->patch_info = ji;
1765 * When we add a tree of instructions, we need to ensure the instructions currently
1766 * on the stack are executed before (like, if we load a value from a local).
1767 * We ensure this by saving the currently loaded values to temps and rewriting the
1768 * instructions to load the values.
1769 * This is not done for opcodes that terminate a basic block (because it's handled already
1770 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
1773 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
1775 MonoInst *load, *store, *temp, *ins;
1777 while (stack < sp) {
1779 /* handle also other constants */
1780 if (ins->opcode != OP_ICONST) {
1781 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1782 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1783 store->cil_code = ins->cil_code;
1784 if (store->opcode == CEE_STOBJ) {
1785 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
1786 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE);
1788 MONO_ADD_INS (bblock, store);
1789 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1790 load->cil_code = ins->cil_code;
1798 * Prepare arguments for passing to a function call.
1799 * Return a non-zero value if the arguments can't be passed to the given
1801 * The type checks are not yet complete and some conversions may need
1802 * casts on 32 or 64 bit architectures.
1805 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
1810 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
1814 for (i = 0; i < sig->param_count; ++i) {
1815 if (sig->params [i]->byref) {
1817 * check the result of ldelema is only passed as an argument if the byref
1818 * type matches exactly the array element type.
1819 * FIXME: if the argument as been saved on the stack as part of the
1820 * interface variable code (the value was on the stack at a basic block boundary)
1821 * we need to add the check in that case, too.
1823 if (args [i]->opcode == CEE_LDELEMA) {
1825 MonoClass *exact_class = mono_class_from_mono_type (sig->params [i]);
1826 if (!exact_class->valuetype) {
1827 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
1828 check->cil_code = args [i]->cil_code;
1829 check->klass = exact_class;
1830 check->inst_left = args [i]->inst_left;
1831 check->type = STACK_OBJ;
1832 args [i]->inst_left = check;
1835 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
1839 simple_type = sig->params [i]->type;
1841 switch (simple_type) {
1842 case MONO_TYPE_VOID:
1847 case MONO_TYPE_BOOLEAN:
1850 case MONO_TYPE_CHAR:
1853 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
1859 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
1862 case MONO_TYPE_CLASS:
1863 case MONO_TYPE_STRING:
1864 case MONO_TYPE_OBJECT:
1865 case MONO_TYPE_SZARRAY:
1866 case MONO_TYPE_ARRAY:
1867 if (args [i]->type != STACK_OBJ)
1872 if (args [i]->type != STACK_I8)
1877 if (args [i]->type != STACK_R8)
1880 case MONO_TYPE_VALUETYPE:
1881 if (sig->params [i]->data.klass->enumtype) {
1882 simple_type = sig->params [i]->data.klass->enum_basetype->type;
1885 if (args [i]->type != STACK_VTYPE)
1888 case MONO_TYPE_TYPEDBYREF:
1889 if (args [i]->type != STACK_VTYPE)
1892 case MONO_TYPE_GENERICINST:
1893 simple_type = sig->params [i]->data.generic_inst->generic_type->type;
1897 g_error ("unknown type 0x%02x in check_call_signature", simple_type);
1904 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
1905 const guint8 *ip, gboolean to_end)
1907 MonoInst *temp, *store, *ins = (MonoInst*)call;
1908 MonoType *ret = sig->ret;
1910 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
1912 call->inst.type = STACK_OBJ;
1913 call->inst.opcode = CEE_CALL;
1914 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
1916 type_to_eval_stack_type (ret, ins);
1917 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
1920 if (MONO_TYPE_ISSTRUCT (ret)) {
1923 /* we use this to allocate native sized structs */
1924 temp->unused = sig->pinvoke;
1926 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
1927 if (call->inst.opcode == OP_VCALL)
1928 ins->inst_left = loada;
1930 ins->inst_right = loada; /* a virtual or indirect call */
1933 mono_add_ins_to_end (bblock, ins);
1935 MONO_ADD_INS (bblock, ins);
1937 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1938 store->cil_code = ip;
1940 mono_add_ins_to_end (bblock, store);
1942 MONO_ADD_INS (bblock, store);
1944 return temp->inst_c0;
1947 mono_add_ins_to_end (bblock, ins);
1949 MONO_ADD_INS (bblock, ins);
1954 inline static MonoCallInst *
1955 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1956 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
1961 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual));
1963 call->inst.cil_code = ip;
1965 call->signature = sig;
1966 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
1968 for (arg = call->out_args; arg;) {
1969 MonoInst *narg = arg->next;
1974 mono_add_ins_to_end (bblock, arg);
1976 MONO_ADD_INS (bblock, arg);
1983 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1984 MonoInst **args, MonoInst *addr, const guint8 *ip)
1986 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
1988 call->inst.inst_i0 = addr;
1990 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
1993 static MonoCallInst*
1994 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
1995 MonoInst **args, const guint8 *ip, MonoInst *this)
1997 gboolean virtual = this != NULL;
2000 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, FALSE);
2002 if (this && sig->hasthis &&
2003 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
2004 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
2005 call->method = mono_marshal_get_remoting_invoke_with_check (method);
2007 call->method = method;
2009 call->inst.flags |= MONO_INST_HAS_METHOD;
2010 call->inst.inst_left = this;
2016 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
2017 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
2019 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, signature, args, ip, this);
2021 return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
2025 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
2026 MonoInst **args, const guint8 *ip, gboolean to_end)
2032 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
2034 return mono_spill_call (cfg, bblock, call, sig, func == mono_array_new_va, ip, to_end);
2038 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
2040 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
2043 g_warning ("unregistered JIT ICall");
2044 g_assert_not_reached ();
2047 return mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, args, ip, FALSE);
2051 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
2053 MonoInst *ins, *temp = NULL, *store, *load, *begin;
2054 MonoInst *last_arg = NULL;
2058 //g_print ("emulating: ");
2059 //mono_print_tree_nl (tree);
2060 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE));
2061 ins = (MonoInst*)call;
2063 call->inst.cil_code = tree->cil_code;
2065 call->signature = info->sig;
2067 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
2069 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2070 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
2071 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2072 store->cil_code = tree->cil_code;
2077 nargs = info->sig->param_count + info->sig->hasthis;
2079 for (last_arg = call->out_args; last_arg && last_arg->next; last_arg = last_arg->next) ;
2082 last_arg->next = store;
2085 begin = call->out_args;
2089 if (cfg->prev_ins) {
2091 * This assumes that that in a tree, emulate_opcode is called for a
2092 * node before it is called for its children. dec_foreach needs to
2093 * take this into account.
2095 store->next = cfg->prev_ins->next;
2096 cfg->prev_ins->next = begin;
2098 store->next = cfg->cbb->code;
2099 cfg->cbb->code = begin;
2102 call->fptr = mono_icall_get_wrapper (info);
2104 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2105 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2110 static MonoMethodSignature *
2111 mono_get_element_address_signature (int arity)
2113 static GHashTable *sighash = NULL;
2114 MonoMethodSignature *res;
2117 EnterCriticalSection (&trampoline_hash_mutex);
2119 sighash = g_hash_table_new (NULL, NULL);
2121 else if ((res = g_hash_table_lookup (sighash, (gpointer)arity))) {
2122 LeaveCriticalSection (&trampoline_hash_mutex);
2126 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
2129 res->params [0] = &mono_defaults.array_class->byval_arg;
2131 for (i = 1; i <= arity; i++)
2132 res->params [i] = &mono_defaults.int_class->byval_arg;
2134 res->ret = &mono_defaults.int_class->byval_arg;
2136 g_hash_table_insert (sighash, (gpointer)arity, res);
2137 LeaveCriticalSection (&trampoline_hash_mutex);
2143 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native) {
2144 MonoInst *iargs [3];
2150 * This check breaks with spilled vars... need to handle it during verification anyway.
2151 * g_assert (klass && klass == src->klass && klass == dest->klass);
2155 n = mono_class_native_size (klass, &align);
2157 n = mono_class_value_size (klass, &align);
2159 if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
2161 MONO_INST_NEW (cfg, inst, OP_MEMCPY);
2162 inst->inst_left = dest;
2163 inst->inst_right = src;
2164 inst->cil_code = ip;
2166 MONO_ADD_INS (bblock, inst);
2171 NEW_ICONST (cfg, iargs [2], n);
2173 mono_emit_native_call (cfg, bblock, helper_memcpy, helper_sig_memcpy, iargs, ip, to_end);
2177 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
2179 MonoInst *iargs [2];
2180 MonoInst *ins, *zero_int32;
2183 NEW_ICONST (cfg, zero_int32, 0);
2185 mono_class_init (klass);
2186 n = mono_class_value_size (klass, NULL);
2187 MONO_INST_NEW (cfg, ins, 0);
2189 ins->inst_left = dest;
2190 ins->inst_right = zero_int32;
2193 ins->opcode = CEE_STIND_I1;
2194 MONO_ADD_INS (bblock, ins);
2197 ins->opcode = CEE_STIND_I2;
2198 MONO_ADD_INS (bblock, ins);
2201 ins->opcode = CEE_STIND_I4;
2202 MONO_ADD_INS (bblock, ins);
2205 if (n <= sizeof (gpointer) * 5) {
2206 ins->opcode = OP_MEMSET;
2209 MONO_ADD_INS (bblock, ins);
2212 handle_loaded_temps (cfg, bblock, stack_start, sp);
2213 NEW_ICONST (cfg, ins, n);
2216 mono_emit_jit_icall (cfg, bblock, helper_initobj, iargs, ip);
2222 handle_box (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *val, const guchar *ip, MonoClass *klass)
2224 MonoInst *iargs [2];
2225 MonoInst *dest, *vtoffset, *add, *vstore;
2228 /* much like NEWOBJ */
2229 if (cfg->opt & MONO_OPT_SHARED) {
2230 NEW_DOMAINCONST (cfg, iargs [0]);
2231 NEW_CLASSCONST (cfg, iargs [1], klass);
2233 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
2235 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
2236 NEW_VTABLECONST (cfg, iargs [0], vtable);
2237 if (klass->has_finalize || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
2238 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
2240 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
2242 NEW_TEMPLOAD (cfg, dest, temp);
2243 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
2244 MONO_INST_NEW (cfg, add, CEE_ADD);
2245 add->inst_left = dest;
2246 add->inst_right = vtoffset;
2249 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
2250 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
2251 vstore->cil_code = ip;
2252 vstore->inst_left = add;
2253 vstore->inst_right = val;
2255 if (vstore->opcode == CEE_STOBJ) {
2256 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
2258 MONO_ADD_INS (bblock, vstore);
2260 NEW_TEMPLOAD (cfg, dest, temp);
2264 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
2267 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
2269 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
2270 MonoMethodSignature *signature = method->signature;
2274 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2275 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2276 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
2277 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
2278 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2279 (method->klass->marshalbyref) ||
2280 !header || header->num_clauses ||
2281 /* fixme: why cant we inline valuetype returns? */
2282 MONO_TYPE_ISSTRUCT (signature->ret))
2285 /* its not worth to inline methods with valuetype arguments?? */
2286 for (i = 0; i < signature->param_count; i++) {
2287 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
2293 * if we can initialize the class of the method right away, we do,
2294 * otherwise we don't allow inlining if the class needs initialization,
2295 * since it would mean inserting a call to mono_runtime_class_init()
2296 * inside the inlined code
2298 if (!(cfg->opt & MONO_OPT_SHARED)) {
2299 vtable = mono_class_vtable (cfg->domain, method->klass);
2300 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
2301 if (cfg->run_cctors)
2302 mono_runtime_class_init (vtable);
2304 else if (!vtable->initialized && mono_class_needs_cctor_run (method->klass, NULL))
2308 * If we're compiling for shared code
2309 * the cctor will need to be run at aot method load time, for example,
2310 * or at the end of the compilation of the inlining method.
2312 if (mono_class_needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
2315 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
2317 /* also consider num_locals? */
2318 if (getenv ("MONO_INLINELIMIT"))
2319 if (header->code_size < atoi (getenv ("MONO_INLINELIMIT"))) {
2323 if (header->code_size < 20)
2330 mini_get_ldelema_ins (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
2334 MonoMethodSignature *esig;
2335 char icall_name [256];
2336 MonoJitICallInfo *info;
2338 rank = cmethod->signature->param_count - (is_set? 1: 0);
2340 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
2342 NEW_GROUP (cfg, indexes, sp [1], sp [2]);
2343 MONO_INST_NEW (cfg, addr, OP_LDELEMA2D);
2344 addr->inst_left = sp [0];
2345 addr->inst_right = indexes;
2346 addr->cil_code = ip;
2347 addr->type = STACK_MP;
2348 addr->klass = cmethod->klass;
2352 /* Need to register the icall so it gets an icall wrapper */
2353 sprintf (icall_name, "ves_array_element_address_%d", rank);
2355 info = mono_find_jit_icall_by_name (icall_name);
2357 esig = mono_get_element_address_signature (rank);
2358 info = mono_register_jit_icall (ves_array_element_address, g_strdup (icall_name), esig, FALSE);
2361 temp = mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, sp, ip, FALSE);
2363 NEW_TEMPLOAD (cfg, addr, temp);
2367 static MonoJitICallInfo **emul_opcode_map = NULL;
2369 static inline MonoJitICallInfo *
2370 mono_find_jit_opcode_emulation (int opcode)
2372 if (emul_opcode_map)
2373 return emul_opcode_map [opcode];
2379 mini_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
2384 if (cmethod->klass == mono_defaults.string_class) {
2385 if (cmethod->name [0] != 'g')
2388 if (strcmp (cmethod->name, "get_Chars") == 0)
2390 else if (strcmp (cmethod->name, "get_Length") == 0)
2393 } else if (cmethod->klass == mono_defaults.object_class) {
2394 if (strcmp (cmethod->name, "GetType") == 0)
2398 } else if (cmethod->klass == mono_defaults.array_class) {
2399 if (strcmp (cmethod->name, "get_Rank") == 0)
2401 else if (strcmp (cmethod->name, "get_Length") == 0)
2406 op = mono_arch_get_opcode_for_method (cfg, cmethod, fsig, args);
2410 pc = fsig->param_count + fsig->hasthis;
2411 MONO_INST_NEW (cfg, ins, op);
2414 ins->inst_i0 = args [0];
2416 ins->inst_i1 = args [1];
2423 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
2425 MonoInst *store, *temp;
2428 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
2430 if (!sig->hasthis && sig->param_count == 0)
2434 if (sp [0]->opcode == OP_ICONST) {
2437 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
2439 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2440 store->cil_code = sp [0]->cil_code;
2441 MONO_ADD_INS (bblock, store);
2446 for (i = 0; i < sig->param_count; ++i) {
2447 if (sp [0]->opcode == OP_ICONST) {
2450 temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
2452 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2453 store->cil_code = sp [0]->cil_code;
2454 if (store->opcode == CEE_STOBJ) {
2455 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2456 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE);
2458 MONO_ADD_INS (bblock, store);
2466 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
2467 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b)
2469 MonoInst *ins, *rvar = NULL;
2470 MonoMethodHeader *cheader;
2471 MonoBasicBlock *ebblock, *sbblock;
2472 int i, costs, new_locals_offset;
2474 if (cfg->verbose_level > 2)
2475 g_print ("INLINE START %p %s -> %s\n", cmethod, mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2477 cheader = ((MonoMethodNormal *)cmethod)->header;
2479 if (!cmethod->inline_info) {
2480 mono_jit_stats.inlineable_methods++;
2481 cmethod->inline_info = 1;
2483 /* allocate space to store the return value */
2484 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2485 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2488 /* allocate local variables */
2489 new_locals_offset = cfg->num_varinfo;
2490 for (i = 0; i < cheader->num_locals; ++i)
2491 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
2493 /* allocate starte and end blocks */
2494 sbblock = NEW_BBLOCK (cfg);
2495 sbblock->block_num = cfg->num_bblocks++;
2496 sbblock->real_offset = real_offset;
2498 ebblock = NEW_BBLOCK (cfg);
2499 ebblock->block_num = cfg->num_bblocks++;
2500 ebblock->real_offset = real_offset;
2502 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
2504 if (costs >= 0 && costs < 60) {
2505 if (cfg->verbose_level > 2)
2506 g_print ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2508 mono_jit_stats.inlined_methods++;
2510 /* always add some code to avoid block split failures */
2511 MONO_INST_NEW (cfg, ins, CEE_NOP);
2512 MONO_ADD_INS (bblock, ins);
2515 bblock->next_bb = sbblock;
2516 link_bblock (cfg, bblock, sbblock);
2519 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
2525 if (cfg->verbose_level > 2)
2526 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
2532 * Some of these comments may well be out-of-date.
2533 * Design decisions: we do a single pass over the IL code (and we do bblock
2534 * splitting/merging in the few cases when it's required: a back jump to an IL
2535 * address that was not already seen as bblock starting point).
2536 * Code is validated as we go (full verification is still better left to metadata/verify.c).
2537 * Complex operations are decomposed in simpler ones right away. We need to let the
2538 * arch-specific code peek and poke inside this process somehow (except when the
2539 * optimizations can take advantage of the full semantic info of coarse opcodes).
2540 * All the opcodes of the form opcode.s are 'normalized' to opcode.
2541 * MonoInst->opcode initially is the IL opcode or some simplification of that
2542 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
2543 * opcode with value bigger than OP_LAST.
2544 * At this point the IR can be handed over to an interpreter, a dumb code generator
2545 * or to the optimizing code generator that will translate it to SSA form.
2547 * Profiling directed optimizations.
2548 * We may compile by default with few or no optimizations and instrument the code
2549 * or the user may indicate what methods to optimize the most either in a config file
2550 * or through repeated runs where the compiler applies offline the optimizations to
2551 * each method and then decides if it was worth it.
2554 * * consider using an array instead of an hash table (bb_hash)
2557 #define CHECK_TYPE(ins) if (!(ins)->type) goto unverified
2558 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) goto unverified
2559 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) goto unverified
2560 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) goto unverified
2561 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) goto unverified
2562 #define CHECK_OPSIZE(size) if (ip + size > end) goto unverified
2565 /* offset from br.s -> br like opcodes */
2566 #define BIG_BRANCH_OFFSET 13
2569 get_basic_blocks (MonoCompile *cfg, GHashTable *bbhash, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
2571 unsigned char *ip = start;
2572 unsigned char *target;
2575 MonoBasicBlock *bblock;
2576 const MonoOpcode *opcode;
2579 cli_addr = ip - start;
2580 i = mono_opcode_value ((const guint8 **)&ip);
2581 opcode = &mono_opcodes [i];
2582 switch (opcode->argument) {
2583 case MonoInlineNone:
2586 case MonoInlineString:
2587 case MonoInlineType:
2588 case MonoInlineField:
2589 case MonoInlineMethod:
2592 case MonoShortInlineR:
2599 case MonoShortInlineVar:
2600 case MonoShortInlineI:
2603 case MonoShortInlineBrTarget:
2604 target = start + cli_addr + 2 + (signed char)ip [1];
2605 GET_BBLOCK (cfg, bbhash, bblock, target);
2608 case MonoInlineBrTarget:
2609 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
2610 GET_BBLOCK (cfg, bbhash, bblock, target);
2613 case MonoInlineSwitch: {
2614 guint32 n = read32 (ip + 1);
2617 cli_addr += 5 + 4 * n;
2618 target = start + cli_addr;
2619 GET_BBLOCK (cfg, bbhash, bblock, target);
2621 for (j = 0; j < n; ++j) {
2622 target = start + cli_addr + (gint32)read32 (ip);
2623 GET_BBLOCK (cfg, bbhash, bblock, target);
2633 g_assert_not_reached ();
2643 * mono_method_to_ir: translates IL into basic blocks containing trees
2646 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
2647 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
2648 guint inline_offset, gboolean is_virtual_call)
2650 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
2651 MonoInst *ins, **sp, **stack_start;
2652 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
2654 MonoMethod *cmethod;
2655 MonoInst **arg_array;
2656 MonoMethodHeader *header;
2658 guint32 token, ins_flag;
2660 MonoClass *constrained_call = NULL;
2661 unsigned char *ip, *end, *target, *err_pos;
2662 static double r8_0 = 0.0;
2663 MonoMethodSignature *sig;
2664 MonoGenericContext *generic_context = NULL;
2665 MonoType **param_types;
2666 GList *bb_recheck = NULL, *tmp;
2667 int i, n, start_new_bblock, align;
2668 int num_calls = 0, inline_costs = 0;
2669 int *filter_lengths = NULL;
2670 int breakpoint_id = 0;
2671 guint real_offset, num_args;
2673 image = method->klass->image;
2674 header = ((MonoMethodNormal *)method)->header;
2675 sig = method->signature;
2676 num_args = sig->hasthis + sig->param_count;
2677 ip = (unsigned char*)header->code;
2678 end = ip + header->code_size;
2679 mono_jit_stats.cil_code_size += header->code_size;
2681 if (method->signature->is_inflated)
2682 generic_context = ((MonoMethodInflated *) method)->context;
2684 if (cfg->method == method) {
2686 bbhash = cfg->bb_hash;
2688 real_offset = inline_offset;
2689 bbhash = g_hash_table_new (g_direct_hash, NULL);
2692 if (cfg->verbose_level > 2)
2693 g_print ("method to IR %s\n", mono_method_full_name (method, TRUE));
2695 dont_inline = g_list_prepend (dont_inline, method);
2696 if (cfg->method == method) {
2698 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
2699 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
2702 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
2703 start_bblock->cil_code = NULL;
2704 start_bblock->cil_length = 0;
2705 start_bblock->block_num = cfg->num_bblocks++;
2708 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
2709 end_bblock->cil_code = NULL;
2710 end_bblock->cil_length = 0;
2711 end_bblock->block_num = cfg->num_bblocks++;
2712 g_assert (cfg->num_bblocks == 2);
2714 arg_array = alloca (sizeof (MonoInst *) * num_args);
2715 for (i = num_args - 1; i >= 0; i--)
2716 arg_array [i] = cfg->varinfo [i];
2718 if (header->num_clauses) {
2719 int size = sizeof (int) * header->num_clauses;
2720 filter_lengths = alloca (size);
2721 memset (filter_lengths, 0, size);
2723 cfg->spvars = g_hash_table_new (NULL, NULL);
2725 /* handle exception clauses */
2726 for (i = 0; i < header->num_clauses; ++i) {
2727 //unsigned char *p = ip;
2728 MonoExceptionClause *clause = &header->clauses [i];
2729 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->try_offset);
2730 tblock->real_offset = clause->try_offset;
2731 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
2732 tblock->real_offset = clause->handler_offset;
2734 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
2735 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2736 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2737 MONO_ADD_INS (tblock, ins);
2740 /*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);
2742 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
2744 /* catch and filter blocks get the exception object on the stack */
2745 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
2746 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2747 /* mostly like handle_stack_args (), but just sets the input args */
2748 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
2750 cfg->exvar = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
2751 /* prevent it from being register allocated */
2752 cfg->exvar->flags |= MONO_INST_INDIRECT;
2754 tblock->in_scount = 1;
2755 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2756 tblock->in_stack [0] = cfg->exvar;
2758 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2759 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->token_or_filter);
2760 tblock->real_offset = clause->token_or_filter;
2761 tblock->in_scount = 1;
2762 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2763 tblock->in_stack [0] = cfg->exvar;
2764 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2765 MONO_ADD_INS (tblock, ins);
2771 arg_array = alloca (sizeof (MonoInst *) * num_args);
2772 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
2775 /* FIRST CODE BLOCK */
2776 bblock = NEW_BBLOCK (cfg);
2777 bblock->cil_code = ip;
2779 ADD_BBLOCK (cfg, bbhash, bblock);
2781 if (cfg->method == method) {
2782 breakpoint_id = mono_debugger_method_has_breakpoint (method);
2783 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
2784 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2785 MONO_ADD_INS (bblock, ins);
2789 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED))) || mono_compile_aot) {
2790 /* we use a separate basic block for the initialization code */
2791 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
2792 init_localsbb->real_offset = real_offset;
2793 start_bblock->next_bb = init_localsbb;
2794 init_localsbb->next_bb = bblock;
2795 link_bblock (cfg, start_bblock, init_localsbb);
2796 link_bblock (cfg, init_localsbb, bblock);
2797 init_localsbb->block_num = cfg->num_bblocks++;
2799 start_bblock->next_bb = bblock;
2800 link_bblock (cfg, start_bblock, bblock);
2803 if (get_basic_blocks (cfg, bbhash, header, real_offset, ip, end, &err_pos)) {
2808 mono_debug_init_method (cfg, bblock, breakpoint_id);
2810 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
2812 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
2813 for (n = 0; n < sig->param_count; ++n)
2814 param_types [n + sig->hasthis] = sig->params [n];
2816 /* do this somewhere outside - not here */
2817 NEW_ICONST (cfg, zero_int32, 0);
2818 NEW_ICONST (cfg, zero_int64, 0);
2819 zero_int64->type = STACK_I8;
2820 NEW_PCONST (cfg, zero_ptr, 0);
2821 NEW_PCONST (cfg, zero_obj, 0);
2822 zero_obj->type = STACK_OBJ;
2824 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
2825 zero_r8->type = STACK_R8;
2826 zero_r8->inst_p0 = &r8_0;
2828 /* add a check for this != NULL to inlined methods */
2829 if (is_virtual_call) {
2830 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
2831 NEW_ARGLOAD (cfg, ins->inst_left, 0);
2833 MONO_ADD_INS (bblock, ins);
2836 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
2837 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
2840 start_new_bblock = 0;
2843 if (cfg->method == method)
2844 real_offset = ip - header->code;
2846 real_offset = inline_offset;
2848 if (start_new_bblock) {
2849 bblock->cil_length = ip - bblock->cil_code;
2850 if (start_new_bblock == 2) {
2851 g_assert (ip == tblock->cil_code);
2853 GET_BBLOCK (cfg, bbhash, tblock, ip);
2855 bblock->next_bb = tblock;
2857 start_new_bblock = 0;
2858 for (i = 0; i < bblock->in_scount; ++i) {
2859 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2863 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
2864 link_bblock (cfg, bblock, tblock);
2865 if (sp != stack_start) {
2866 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2869 bblock->next_bb = tblock;
2871 for (i = 0; i < bblock->in_scount; ++i) {
2872 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2878 bblock->real_offset = real_offset;
2880 if ((cfg->method == method) && cfg->coverage_info) {
2881 MonoInst *store, *one;
2882 guint32 cil_offset = ip - header->code;
2883 cfg->coverage_info->data [cil_offset].cil_code = ip;
2885 /* TODO: Use an increment here */
2886 NEW_ICONST (cfg, one, 1);
2889 NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
2892 MONO_INST_NEW (cfg, store, CEE_STIND_I);
2893 store->cil_code = ip;
2894 store->inst_left = ins;
2895 store->inst_right = one;
2897 MONO_ADD_INS (bblock, store);
2900 if (cfg->verbose_level > 3)
2901 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, sp-stack_start, mono_disasm_code_one (NULL, method, ip, NULL));
2908 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2909 ins->cil_code = ip++;
2910 MONO_ADD_INS (bblock, ins);
2916 CHECK_STACK_OVF (1);
2917 n = (*ip)-CEE_LDARG_0;
2919 NEW_ARGLOAD (cfg, ins, n);
2920 ins->cil_code = ip++;
2927 CHECK_STACK_OVF (1);
2928 n = (*ip)-CEE_LDLOC_0;
2930 NEW_LOCLOAD (cfg, ins, n);
2931 ins->cil_code = ip++;
2939 n = (*ip)-CEE_STLOC_0;
2942 handle_loaded_temps (cfg, bblock, stack_start, sp);
2943 NEW_LOCSTORE (cfg, ins, n, *sp);
2945 if (ins->opcode == CEE_STOBJ) {
2946 NEW_LOCLOADA (cfg, ins, n);
2947 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2949 MONO_ADD_INS (bblock, ins);
2955 CHECK_STACK_OVF (1);
2957 NEW_ARGLOAD (cfg, ins, ip [1]);
2964 CHECK_STACK_OVF (1);
2966 NEW_ARGLOADA (cfg, ins, ip [1]);
2976 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
2977 handle_loaded_temps (cfg, bblock, stack_start, sp);
2979 if (ins->opcode == CEE_STOBJ) {
2980 NEW_ARGLOADA (cfg, ins, ip [1]);
2981 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2983 MONO_ADD_INS (bblock, ins);
2988 CHECK_STACK_OVF (1);
2989 CHECK_LOCAL (ip [1]);
2990 NEW_LOCLOAD (cfg, ins, ip [1]);
2997 CHECK_STACK_OVF (1);
2998 CHECK_LOCAL (ip [1]);
2999 NEW_LOCLOADA (cfg, ins, ip [1]);
3008 handle_loaded_temps (cfg, bblock, stack_start, sp);
3009 CHECK_LOCAL (ip [1]);
3010 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
3012 if (ins->opcode == CEE_STOBJ) {
3013 NEW_LOCLOADA (cfg, ins, ip [1]);
3014 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3016 MONO_ADD_INS (bblock, ins);
3021 CHECK_STACK_OVF (1);
3022 NEW_PCONST (cfg, ins, NULL);
3024 ins->type = STACK_OBJ;
3029 CHECK_STACK_OVF (1);
3030 NEW_ICONST (cfg, ins, -1);
3044 CHECK_STACK_OVF (1);
3045 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
3052 CHECK_STACK_OVF (1);
3054 NEW_ICONST (cfg, ins, *((signed char*)ip));
3061 CHECK_STACK_OVF (1);
3062 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
3069 CHECK_STACK_OVF (1);
3070 MONO_INST_NEW (cfg, ins, OP_I8CONST);
3072 ins->type = STACK_I8;
3074 ins->inst_l = (gint64)read64 (ip);
3079 float *f = g_malloc (sizeof (float));
3081 CHECK_STACK_OVF (1);
3082 MONO_INST_NEW (cfg, ins, OP_R4CONST);
3083 ins->type = STACK_R8;
3092 double *d = g_malloc (sizeof (double));
3094 CHECK_STACK_OVF (1);
3095 MONO_INST_NEW (cfg, ins, OP_R8CONST);
3096 ins->type = STACK_R8;
3105 MonoInst *temp, *store;
3107 CHECK_STACK_OVF (1);
3112 * small optimization: if the loaded value was from a local already,
3113 * just load it twice.
3115 if (ins->ssa_op == MONO_SSA_LOAD &&
3116 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
3118 MONO_INST_NEW (cfg, temp, 0);
3120 temp->cil_code = ip;
3123 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3124 temp->cil_code = ip;
3125 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3126 store->cil_code = ip;
3127 MONO_ADD_INS (bblock, store);
3128 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3131 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3141 MONO_INST_NEW (cfg, ins, CEE_POP);
3142 MONO_ADD_INS (bblock, ins);
3143 ins->cil_code = ip++;
3149 if (stack_start != sp)
3151 MONO_INST_NEW (cfg, ins, CEE_JMP);
3152 token = read32 (ip + 1);
3153 /* FIXME: check the signature matches */
3154 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3155 ins->inst_p0 = cmethod;
3156 MONO_ADD_INS (bblock, ins);
3158 start_new_bblock = 1;
3162 case CEE_CALLVIRT: {
3163 MonoInst *addr = NULL;
3164 MonoMethodSignature *fsig = NULL;
3165 int temp, array_rank = 0;
3166 int virtual = *ip == CEE_CALLVIRT;
3169 token = read32 (ip + 1);
3171 if (*ip == CEE_CALLI) {
3176 if (method->wrapper_type != MONO_WRAPPER_NONE)
3177 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
3179 fsig = mono_metadata_parse_signature (image, token);
3181 n = fsig->param_count + fsig->hasthis;
3183 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3184 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
3185 } else if (constrained_call) {
3186 cmethod = mono_get_method_constrained (image, token, constrained_call, generic_context);
3188 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3193 if (!cmethod->klass->inited)
3194 mono_class_init (cmethod->klass);
3196 if (cmethod->signature->pinvoke) {
3197 #ifdef MONO_USE_EXC_TABLES
3198 if (mono_method_blittable (cmethod)) {
3199 fsig = cmethod->signature;
3202 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
3203 fsig = wrapper->signature;
3204 #ifdef MONO_USE_EXC_TABLES
3208 fsig = mono_method_get_signature (cmethod, image, token);
3211 n = fsig->param_count + fsig->hasthis;
3213 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
3214 cmethod->klass->parent == mono_defaults.array_class) {
3215 array_rank = cmethod->klass->rank;
3218 if (cmethod->string_ctor)
3219 g_assert_not_reached ();
3225 //g_assert (!virtual || fsig->hasthis);
3229 if (constrained_call) {
3231 * We have the `constrained.' prefix opcode.
3233 if (constrained_call->valuetype && !cmethod->klass->valuetype) {
3235 * The type parameter is instantiated as a valuetype,
3236 * but that type doesn't override the method we're
3237 * calling, so we need to box `this'.
3239 sp [0] = handle_box (cfg, bblock, sp [0], ip, constrained_call);
3240 } else if (!constrained_call->valuetype) {
3244 * The type parameter is instantiated as a reference
3245 * type. We have a managed pointer on the stack, so
3246 * we need to dereference it here.
3249 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3251 ins->inst_i0 = sp [0];
3252 ins->type = STACK_OBJ;
3254 } else if (cmethod->klass->valuetype)
3256 constrained_call = NULL;
3259 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
3262 if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL) && (mono_metadata_signature_equal (method->signature, cmethod->signature))) {
3264 /* FIXME: This assumes the two methods has the same number and type of arguments */
3265 for (i = 0; i < n; ++i) {
3266 /* Check if argument is the same */
3267 NEW_ARGLOAD (cfg, ins, i);
3268 if ((ins->opcode == sp [i]->opcode) && (ins->inst_i0 == sp [i]->inst_i0))
3271 /* Prevent argument from being register allocated */
3272 arg_array [i]->flags |= MONO_INST_VOLATILE;
3273 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3275 if (ins->opcode == CEE_STOBJ) {
3276 NEW_ARGLOADA (cfg, ins, i);
3277 handle_stobj (cfg, bblock, ins, sp [i], sp [i]->cil_code, ins->klass, FALSE, FALSE);
3280 MONO_ADD_INS (bblock, ins);
3282 MONO_INST_NEW (cfg, ins, CEE_JMP);
3284 ins->inst_p0 = cmethod;
3285 ins->inst_p1 = arg_array [0];
3286 MONO_ADD_INS (bblock, ins);
3287 start_new_bblock = 1;
3288 /* skip CEE_RET as well */
3293 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_opcode_for_method (cfg, cmethod, fsig, sp))) {
3296 if (MONO_TYPE_IS_VOID (fsig->ret)) {
3297 MONO_ADD_INS (bblock, ins);
3299 type_to_eval_stack_type (fsig->ret, ins);
3308 handle_loaded_temps (cfg, bblock, stack_start, sp);
3310 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3311 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
3312 mono_method_check_inlining (cfg, cmethod) &&
3313 !g_list_find (dont_inline, cmethod)) {
3315 MonoBasicBlock *ebblock;
3317 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3321 GET_BBLOCK (cfg, bbhash, bblock, ip);
3322 ebblock->next_bb = bblock;
3323 link_bblock (cfg, ebblock, bblock);
3325 if (!MONO_TYPE_IS_VOID (fsig->ret))
3328 /* indicates start of a new block, and triggers a load of all
3329 stack arguments at bb boundarie */
3332 inline_costs += costs;
3337 inline_costs += 10 * num_calls++;
3339 /* tail recursion elimination */
3340 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == method && ip [5] == CEE_RET) {
3341 gboolean has_vtargs = FALSE;
3344 /* keep it simple */
3345 for (i = fsig->param_count - 1; i >= 0; i--) {
3346 if (MONO_TYPE_ISSTRUCT (cmethod->signature->params [i]))
3351 for (i = 0; i < n; ++i) {
3352 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3354 MONO_ADD_INS (bblock, ins);
3356 MONO_INST_NEW (cfg, ins, CEE_BR);
3358 MONO_ADD_INS (bblock, ins);
3359 tblock = start_bblock->out_bb [0];
3360 link_bblock (cfg, bblock, tblock);
3361 ins->inst_target_bb = tblock;
3362 start_new_bblock = 1;
3365 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3366 /* just create a dummy - the value is never used */
3367 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
3368 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3376 if (*ip == CEE_CALLI) {
3378 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
3379 NEW_TEMPLOAD (cfg, *sp, temp);
3383 } else if (array_rank) {
3386 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
3387 if (sp [fsig->param_count]->type == STACK_OBJ) {
3388 MonoInst *iargs [2];
3389 MonoInst *array, *to_store, *store;
3391 handle_loaded_temps (cfg, bblock, stack_start, sp);
3393 array = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
3394 NEW_TEMPSTORE (cfg, store, array->inst_c0, sp [0]);
3395 store->cil_code = ip;
3396 MONO_ADD_INS (bblock, store);
3397 NEW_TEMPLOAD (cfg, iargs [0], array->inst_c0);
3399 to_store = mono_compile_create_var (cfg, type_from_stack_type (sp [fsig->param_count]), OP_LOCAL);
3400 NEW_TEMPSTORE (cfg, store, to_store->inst_c0, sp [fsig->param_count]);
3401 store->cil_code = ip;
3402 MONO_ADD_INS (bblock, store);
3403 NEW_TEMPLOAD (cfg, iargs [1], to_store->inst_c0);
3406 * We first save the args for the call so that the args are copied to the stack
3407 * and a new instruction tree for them is created. If we don't do this,
3408 * the same MonoInst is added to two different trees and this is not
3411 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref_check, iargs, ip);
3413 NEW_TEMPLOAD (cfg, sp [0], array->inst_c0);
3414 NEW_TEMPLOAD (cfg, sp [fsig->param_count], to_store->inst_c0);
3417 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
3418 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
3420 if (ins->opcode == CEE_STOBJ) {
3421 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
3423 MONO_ADD_INS (bblock, ins);
3426 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
3427 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3428 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
3432 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
3433 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3436 g_assert_not_reached ();
3440 if (0 && CODE_IS_STLOC (ip + 5) && (!MONO_TYPE_ISSTRUCT (fsig->ret)) && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)) {
3441 /* no need to spill */
3442 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
3445 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) {
3446 NEW_TEMPLOAD (cfg, *sp, temp);
3456 if (cfg->method != method) {
3457 /* return from inlined methode */
3462 //g_assert (returnvar != -1);
3463 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
3464 store->cil_code = sp [0]->cil_code;
3465 if (store->opcode == CEE_STOBJ) {
3466 g_assert_not_reached ();
3467 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
3468 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
3470 MONO_ADD_INS (bblock, store);
3474 g_assert (!return_var);
3477 MONO_INST_NEW (cfg, ins, CEE_NOP);
3478 ins->opcode = mono_type_to_stind (method->signature->ret);
3479 if (ins->opcode == CEE_STOBJ) {
3480 NEW_RETLOADA (cfg, ins);
3481 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3483 ins->opcode = OP_SETRET;
3485 ins->inst_i0 = *sp;;
3486 ins->inst_i1 = NULL;
3487 MONO_ADD_INS (bblock, ins);
3491 if (sp != stack_start)
3493 MONO_INST_NEW (cfg, ins, CEE_BR);
3494 ins->cil_code = ip++;
3495 ins->inst_target_bb = end_bblock;
3496 MONO_ADD_INS (bblock, ins);
3497 link_bblock (cfg, bblock, end_bblock);
3498 start_new_bblock = 1;
3502 MONO_INST_NEW (cfg, ins, CEE_BR);
3503 ins->cil_code = ip++;
3504 MONO_ADD_INS (bblock, ins);
3505 target = ip + 1 + (signed char)(*ip);
3507 GET_BBLOCK (cfg, bbhash, tblock, target);
3508 link_bblock (cfg, bblock, tblock);
3509 CHECK_BBLOCK (target, ip, tblock);
3510 ins->inst_target_bb = tblock;
3511 if (sp != stack_start) {
3512 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3515 start_new_bblock = 1;
3522 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3523 ins->cil_code = ip++;
3524 target = ip + 1 + *(signed char*)ip;
3526 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
3527 if (sp != stack_start) {
3528 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3545 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3546 ins->cil_code = ip++;
3547 target = ip + 1 + *(signed char*)ip;
3550 if (sp != stack_start) {
3551 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3558 MONO_INST_NEW (cfg, ins, CEE_BR);
3559 ins->cil_code = ip++;
3560 MONO_ADD_INS (bblock, ins);
3561 target = ip + 4 + (gint32)read32(ip);
3563 GET_BBLOCK (cfg, bbhash, tblock, target);
3564 link_bblock (cfg, bblock, tblock);
3565 CHECK_BBLOCK (target, ip, tblock);
3566 ins->inst_target_bb = tblock;
3567 if (sp != stack_start) {
3568 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3571 start_new_bblock = 1;
3578 MONO_INST_NEW (cfg, ins, *ip);
3579 ins->cil_code = ip++;
3580 target = ip + 4 + (gint32)read32(ip);
3582 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
3583 if (sp != stack_start) {
3584 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3601 MONO_INST_NEW (cfg, ins, *ip);
3602 ins->cil_code = ip++;
3603 target = ip + 4 + (gint32)read32(ip);
3606 if (sp != stack_start) {
3607 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3615 n = read32 (ip + 1);
3616 MONO_INST_NEW (cfg, ins, *ip);
3618 ins->inst_left = *sp;
3619 if (ins->inst_left->type != STACK_I4) goto unverified;
3622 CHECK_OPSIZE (n * sizeof (guint32));
3623 target = ip + n * sizeof (guint32);
3624 MONO_ADD_INS (bblock, ins);
3625 GET_BBLOCK (cfg, bbhash, tblock, target);
3626 link_bblock (cfg, bblock, tblock);
3627 ins->klass = GUINT_TO_POINTER (n);
3628 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
3629 ins->inst_many_bb [n] = tblock;
3631 for (i = 0; i < n; ++i) {
3632 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
3633 link_bblock (cfg, bblock, tblock);
3634 ins->inst_many_bb [i] = tblock;
3637 if (sp != stack_start) {
3638 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3655 MONO_INST_NEW (cfg, ins, *ip);
3660 ins->type = ldind_type [*ip - CEE_LDIND_I1];
3661 ins->flags |= ins_flag;
3673 MONO_INST_NEW (cfg, ins, *ip);
3674 ins->cil_code = ip++;
3676 handle_loaded_temps (cfg, bblock, stack_start, sp);
3677 MONO_ADD_INS (bblock, ins);
3678 ins->inst_i0 = sp [0];
3679 ins->inst_i1 = sp [1];
3680 ins->flags |= ins_flag;
3699 /* special case that gives a nice speedup and happens to workaorund a ppc jit but (for the release)
3700 * later apply the speedup to the left shift as well
3703 if ((ins->opcode == OP_LSHR_UN) && (ins->type == STACK_I8)
3704 && (ins->inst_right->opcode == OP_ICONST) && (ins->inst_right->inst_c0 == 32)) {
3705 ins->opcode = OP_LONG_SHRUN_32;
3706 /*g_print ("applied long shr speedup to %s\n", cfg->method->name);*/
3710 if (mono_find_jit_opcode_emulation (ins->opcode)) {
3711 MonoInst *store, *temp, *load;
3713 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3714 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3715 store->cil_code = ins->cil_code;
3716 MONO_ADD_INS (bblock, store);
3717 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
3718 load->cil_code = ins->cil_code;
3720 /*g_print ("found emulation for %d\n", ins->opcode);*/
3734 case CEE_CONV_OVF_I8:
3735 case CEE_CONV_OVF_U8:
3739 if (mono_find_jit_opcode_emulation (ins->opcode)) {
3740 MonoInst *store, *temp, *load;
3742 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3743 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3744 store->cil_code = ins->cil_code;
3745 MONO_ADD_INS (bblock, store);
3746 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
3747 load->cil_code = ins->cil_code;
3749 /*g_print ("found emulation for %d\n", ins->opcode);*/
3753 case CEE_CONV_OVF_I4:
3754 case CEE_CONV_OVF_I1:
3755 case CEE_CONV_OVF_I2:
3756 case CEE_CONV_OVF_I:
3757 case CEE_CONV_OVF_U:
3760 if (sp [-1]->type == STACK_R8) {
3761 ADD_UNOP (CEE_CONV_OVF_I8);
3769 case CEE_CONV_OVF_U1:
3770 case CEE_CONV_OVF_U2:
3771 case CEE_CONV_OVF_U4:
3774 if (sp [-1]->type == STACK_R8) {
3775 ADD_UNOP (CEE_CONV_OVF_U8);
3783 case CEE_CONV_OVF_I1_UN:
3784 case CEE_CONV_OVF_I2_UN:
3785 case CEE_CONV_OVF_I4_UN:
3786 case CEE_CONV_OVF_I8_UN:
3787 case CEE_CONV_OVF_U1_UN:
3788 case CEE_CONV_OVF_U2_UN:
3789 case CEE_CONV_OVF_U4_UN:
3790 case CEE_CONV_OVF_U8_UN:
3791 case CEE_CONV_OVF_I_UN:
3792 case CEE_CONV_OVF_U_UN:
3800 token = read32 (ip + 1);
3801 if (method->wrapper_type != MONO_WRAPPER_NONE)
3802 klass = mono_method_get_wrapper_data (method, token);
3804 klass = mono_class_get_full (image, token, generic_context);
3806 mono_class_init (klass);
3808 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3809 MonoInst *store, *load;
3810 MONO_INST_NEW (cfg, load, CEE_LDIND_REF);
3811 load->cil_code = ip;
3812 load->inst_i0 = sp [1];
3813 load->type = ldind_type [CEE_LDIND_REF];
3814 load->flags |= ins_flag;
3815 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
3816 store->cil_code = ip;
3817 handle_loaded_temps (cfg, bblock, stack_start, sp);
3818 MONO_ADD_INS (bblock, store);
3819 store->inst_i0 = sp [0];
3820 store->inst_i1 = load;
3821 store->flags |= ins_flag;
3823 n = mono_class_value_size (klass, NULL);
3824 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3826 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3827 copy->inst_left = sp [0];
3828 copy->inst_right = sp [1];
3829 copy->cil_code = ip;
3831 MONO_ADD_INS (bblock, copy);
3833 MonoInst *iargs [3];
3836 NEW_ICONST (cfg, iargs [2], n);
3837 iargs [2]->cil_code = ip;
3839 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3846 MonoInst *iargs [3];
3850 token = read32 (ip + 1);
3851 if (method->wrapper_type != MONO_WRAPPER_NONE)
3852 klass = mono_method_get_wrapper_data (method, token);
3854 klass = mono_class_get_full (image, token, generic_context);
3856 mono_class_init (klass);
3857 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3858 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3860 ins->inst_i0 = sp [0];
3861 ins->type = ldind_type [CEE_LDIND_REF];
3862 ins->flags |= ins_flag;
3868 n = mono_class_value_size (klass, NULL);
3869 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3870 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3871 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3873 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3874 copy->inst_left = iargs [0];
3875 copy->inst_right = *sp;
3876 copy->cil_code = ip;
3878 MONO_ADD_INS (bblock, copy);
3881 NEW_ICONST (cfg, iargs [2], n);
3882 iargs [2]->cil_code = ip;
3884 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3886 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3894 CHECK_STACK_OVF (1);
3896 n = read32 (ip + 1);
3898 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3900 MonoInst *iargs [1];
3902 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
3903 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
3904 NEW_TEMPLOAD (cfg, *sp, temp);
3908 if (cfg->opt & MONO_OPT_SHARED) {
3910 MonoInst *iargs [3];
3912 if (mono_compile_aot) {
3913 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n);
3916 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3917 NEW_IMAGECONST (cfg, iargs [1], image);
3918 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
3919 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
3920 NEW_TEMPLOAD (cfg, *sp, temp);
3921 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3923 if (mono_compile_aot)
3924 NEW_LDSTRCONST (cfg, ins, image, n);
3926 NEW_PCONST (cfg, ins, NULL);
3928 ins->type = STACK_OBJ;
3929 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3939 MonoInst *iargs [2];
3940 MonoMethodSignature *fsig;
3944 token = read32 (ip + 1);
3945 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3946 cmethod = mono_method_get_wrapper_data (method, token);
3948 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3949 fsig = mono_method_get_signature (cmethod, image, token);
3951 mono_class_init (cmethod->klass);
3953 n = fsig->param_count;
3956 /* move the args to allow room for 'this' in the first position */
3962 handle_loaded_temps (cfg, bblock, stack_start, sp);
3965 if (cmethod->klass->parent == mono_defaults.array_class) {
3966 NEW_METHODCONST (cfg, *sp, cmethod);
3967 temp = mono_emit_native_call (cfg, bblock, mono_array_new_va, fsig, sp, ip, FALSE);
3969 } else if (cmethod->string_ctor) {
3970 /* we simply pass a null pointer */
3971 NEW_PCONST (cfg, *sp, NULL);
3972 /* now call the string ctor */
3973 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
3975 if (cmethod->klass->valuetype) {
3976 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
3977 temp = iargs [0]->inst_c0;
3978 NEW_TEMPLOADA (cfg, *sp, temp);
3980 if (cfg->opt & MONO_OPT_SHARED) {
3981 NEW_DOMAINCONST (cfg, iargs [0]);
3982 NEW_CLASSCONST (cfg, iargs [1], cmethod->klass);
3984 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3986 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
3987 NEW_VTABLECONST (cfg, iargs [0], vtable);
3988 if (cmethod->klass->has_finalize || cmethod->klass->marshalbyref || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
3989 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
3991 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
3993 NEW_TEMPLOAD (cfg, *sp, temp);
3996 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3997 mono_method_check_inlining (cfg, cmethod) &&
3998 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
3999 !g_list_find (dont_inline, cmethod)) {
4001 MonoBasicBlock *ebblock;
4002 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
4007 GET_BBLOCK (cfg, bbhash, bblock, ip);
4008 ebblock->next_bb = bblock;
4009 link_bblock (cfg, ebblock, bblock);
4011 NEW_TEMPLOAD (cfg, *sp, temp);
4014 /* indicates start of a new block, and triggers a load
4015 of all stack arguments at bb boundarie */
4018 inline_costs += costs;
4022 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
4025 /* now call the actual ctor */
4026 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
4030 NEW_TEMPLOAD (cfg, *sp, temp);
4041 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4042 mono_class_init (klass);
4044 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4046 MonoMethod *mono_isinst;
4047 MonoInst *iargs [1];
4048 MonoBasicBlock *ebblock;
4052 mono_isinst = mono_marshal_get_isinst (klass);
4055 costs = inline_method (cfg, mono_isinst, mono_isinst->signature, bblock,
4056 iargs, ip, real_offset, dont_inline, &ebblock);
4058 g_assert (costs > 0);
4063 GET_BBLOCK (cfg, bbhash, bblock, ip);
4064 ebblock->next_bb = bblock;
4065 link_bblock (cfg, ebblock, bblock);
4067 temp = iargs [0]->inst_i0->inst_c0;
4068 NEW_TEMPLOAD (cfg, *sp, temp);
4072 inline_costs += costs;
4076 MONO_INST_NEW (cfg, ins, *ip);
4077 ins->type = STACK_OBJ;
4078 ins->inst_left = *sp;
4079 ins->inst_newa_class = klass;
4085 case CEE_UNBOX_ANY: {
4086 MonoInst *add, *vtoffset;
4087 MonoInst *iargs [3];
4092 token = read32 (ip + 1);
4093 if (method->wrapper_type != MONO_WRAPPER_NONE)
4094 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4096 klass = mono_class_get_full (image, token, generic_context);
4097 mono_class_init (klass);
4099 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4101 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4102 MonoMethod *mono_castclass;
4103 MonoInst *iargs [1];
4104 MonoBasicBlock *ebblock;
4108 mono_castclass = mono_marshal_get_castclass (klass);
4111 costs = inline_method (cfg, mono_castclass, mono_castclass->signature, bblock,
4112 iargs, ip, real_offset, dont_inline, &ebblock);
4114 g_assert (costs > 0);
4119 GET_BBLOCK (cfg, bbhash, bblock, ip);
4120 ebblock->next_bb = bblock;
4121 link_bblock (cfg, ebblock, bblock);
4123 temp = iargs [0]->inst_i0->inst_c0;
4124 NEW_TEMPLOAD (cfg, *sp, temp);
4128 inline_costs += costs;
4131 MONO_INST_NEW (cfg, ins, CEE_CASTCLASS);
4132 ins->type = STACK_OBJ;
4133 ins->inst_left = *sp;
4135 ins->inst_newa_class = klass;
4143 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
4144 ins->type = STACK_OBJ;
4145 ins->inst_left = *sp;
4147 ins->inst_newa_class = klass;
4150 MONO_INST_NEW (cfg, add, CEE_ADD);
4151 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4152 add->inst_left = ins;
4153 add->inst_right = vtoffset;
4154 add->type = STACK_MP;
4158 n = mono_class_value_size (klass, NULL);
4159 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
4160 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
4161 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
4163 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
4164 copy->inst_left = iargs [0];
4165 copy->inst_right = *sp;
4166 copy->cil_code = ip;
4168 MONO_ADD_INS (bblock, copy);
4171 NEW_ICONST (cfg, iargs [2], n);
4172 iargs [2]->cil_code = ip;
4174 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
4176 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
4182 MonoInst *add, *vtoffset;
4187 token = read32 (ip + 1);
4188 if (method->wrapper_type != MONO_WRAPPER_NONE)
4189 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4191 klass = mono_class_get_full (image, token, generic_context);
4192 mono_class_init (klass);
4194 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
4195 ins->type = STACK_OBJ;
4196 ins->inst_left = *sp;
4198 ins->inst_newa_class = klass;
4201 MONO_INST_NEW (cfg, add, CEE_ADD);
4202 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4203 add->inst_left = ins;
4204 add->inst_right = vtoffset;
4205 add->type = STACK_MP;
4215 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4216 mono_class_init (klass);
4218 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4220 MonoMethod *mono_castclass;
4221 MonoInst *iargs [1];
4222 MonoBasicBlock *ebblock;
4226 mono_castclass = mono_marshal_get_castclass (klass);
4229 costs = inline_method (cfg, mono_castclass, mono_castclass->signature, bblock,
4230 iargs, ip, real_offset, dont_inline, &ebblock);
4232 g_assert (costs > 0);
4237 GET_BBLOCK (cfg, bbhash, bblock, ip);
4238 ebblock->next_bb = bblock;
4239 link_bblock (cfg, ebblock, bblock);
4241 temp = iargs [0]->inst_i0->inst_c0;
4242 NEW_TEMPLOAD (cfg, *sp, temp);
4246 inline_costs += costs;
4249 MONO_INST_NEW (cfg, ins, *ip);
4250 ins->type = STACK_OBJ;
4251 ins->inst_left = *sp;
4253 ins->inst_newa_class = klass;
4261 MONO_INST_NEW (cfg, ins, *ip);
4263 ins->inst_left = *sp;
4264 ins->cil_code = ip++;
4265 MONO_ADD_INS (bblock, ins);
4267 start_new_bblock = 1;
4272 MonoInst *offset_ins;
4273 MonoClassField *field;
4274 MonoBasicBlock *ebblock;
4278 if (*ip == CEE_STFLD) {
4285 // FIXME: enable this test later.
4286 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
4289 token = read32 (ip + 1);
4290 field = mono_field_from_token (image, token, &klass, generic_context);
4291 mono_class_init (klass);
4293 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
4294 /* FIXME: mark instructions for use in SSA */
4295 if (*ip == CEE_STFLD) {
4296 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound) {
4297 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
4298 MonoInst *iargs [5];
4301 NEW_CLASSCONST (cfg, iargs [1], klass);
4302 NEW_FIELDCONST (cfg, iargs [2], field);
4303 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
4307 if (cfg->opt & MONO_OPT_INLINE) {
4308 costs = inline_method (cfg, stfld_wrapper, stfld_wrapper->signature, bblock,
4309 iargs, ip, real_offset, dont_inline, &ebblock);
4310 g_assert (costs > 0);
4315 GET_BBLOCK (cfg, bbhash, bblock, ip);
4316 ebblock->next_bb = bblock;
4317 link_bblock (cfg, ebblock, bblock);
4319 /* indicates start of a new block, and triggers a load
4320 of all stack arguments at bb boundarie */
4323 inline_costs += costs;
4326 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, stfld_wrapper->signature, iargs, ip, NULL);
4330 NEW_ICONST (cfg, offset_ins, foffset);
4331 MONO_INST_NEW (cfg, ins, CEE_ADD);
4333 ins->inst_left = *sp;
4334 ins->inst_right = offset_ins;
4335 ins->type = STACK_MP;
4337 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4338 store->cil_code = ip;
4339 store->inst_left = ins;
4340 store->inst_right = sp [1];
4341 handle_loaded_temps (cfg, bblock, stack_start, sp);
4342 store->flags |= ins_flag;
4344 if (store->opcode == CEE_STOBJ) {
4345 handle_stobj (cfg, bblock, ins, sp [1], ip,
4346 mono_class_from_mono_type (field->type), FALSE, FALSE);
4348 MONO_ADD_INS (bblock, store);
4351 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound) {
4352 /* fixme: we need to inline that call somehow */
4353 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
4354 MonoInst *iargs [4];
4358 NEW_CLASSCONST (cfg, iargs [1], klass);
4359 NEW_FIELDCONST (cfg, iargs [2], field);
4360 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
4361 if (cfg->opt & MONO_OPT_INLINE) {
4362 costs = inline_method (cfg, ldfld_wrapper, ldfld_wrapper->signature, bblock,
4363 iargs, ip, real_offset, dont_inline, &ebblock);
4364 g_assert (costs > 0);
4369 GET_BBLOCK (cfg, bbhash, bblock, ip);
4370 ebblock->next_bb = bblock;
4371 link_bblock (cfg, ebblock, bblock);
4373 temp = iargs [0]->inst_i0->inst_c0;
4375 if (*ip == CEE_LDFLDA) {
4376 /* not sure howto handle this */
4377 NEW_TEMPLOADA (cfg, *sp, temp);
4379 NEW_TEMPLOAD (cfg, *sp, temp);
4383 /* indicates start of a new block, and triggers a load of
4384 all stack arguments at bb boundarie */
4387 inline_costs += costs;
4390 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, ldfld_wrapper->signature, iargs, ip, NULL);
4391 if (*ip == CEE_LDFLDA) {
4392 /* not sure howto handle this */
4393 NEW_TEMPLOADA (cfg, *sp, temp);
4395 NEW_TEMPLOAD (cfg, *sp, temp);
4400 NEW_ICONST (cfg, offset_ins, foffset);
4401 MONO_INST_NEW (cfg, ins, CEE_ADD);
4403 ins->inst_left = *sp;
4404 ins->inst_right = offset_ins;
4405 ins->type = STACK_MP;
4407 if (*ip == CEE_LDFLDA) {
4411 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4412 type_to_eval_stack_type (field->type, load);
4413 load->cil_code = ip;
4414 load->inst_left = ins;
4415 load->flags |= ins_flag;
4427 MonoClassField *field;
4428 gpointer addr = NULL;
4431 token = read32 (ip + 1);
4433 field = mono_field_from_token (image, token, &klass, generic_context);
4434 mono_class_init (klass);
4436 if ((*ip) == CEE_STSFLD)
4437 handle_loaded_temps (cfg, bblock, stack_start, sp);
4439 if (cfg->domain->special_static_fields)
4440 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
4442 if ((cfg->opt & MONO_OPT_SHARED) || (mono_compile_aot && addr)) {
4444 MonoInst *iargs [2];
4445 g_assert (field->parent);
4446 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
4447 NEW_FIELDCONST (cfg, iargs [1], field);
4448 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
4449 NEW_TEMPLOAD (cfg, ins, temp);
4452 vtable = mono_class_vtable (cfg->domain, klass);
4454 if ((!vtable->initialized || mono_compile_aot) && !(klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (klass, method)) {
4455 guint8 *tramp = mono_create_class_init_trampoline (vtable);
4456 mono_emit_native_call (cfg, bblock, tramp,
4457 helper_sig_class_init_trampoline,
4459 if (cfg->verbose_level > 2)
4460 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
4462 if (cfg->run_cctors)
4463 mono_runtime_class_init (vtable);
4465 addr = (char*)vtable->data + field->offset;
4467 if (mono_compile_aot)
4468 NEW_SFLDACONST (cfg, ins, field);
4470 NEW_PCONST (cfg, ins, addr);
4474 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
4475 * This could be later optimized to do just a couple of
4476 * memory dereferences with constant offsets.
4479 MonoInst *iargs [1];
4480 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
4481 temp = mono_emit_jit_icall (cfg, bblock, mono_get_special_static_data, iargs, ip);
4482 NEW_TEMPLOAD (cfg, ins, temp);
4486 /* FIXME: mark instructions for use in SSA */
4487 if (*ip == CEE_LDSFLDA) {
4489 } else if (*ip == CEE_STSFLD) {
4493 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4494 store->cil_code = ip;
4495 store->inst_left = ins;
4496 store->inst_right = sp [0];
4497 store->flags |= ins_flag;
4500 if (store->opcode == CEE_STOBJ) {
4501 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
4503 MONO_ADD_INS (bblock, store);
4505 gboolean is_const = FALSE;
4506 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4507 if (!((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) &&
4508 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
4509 gpointer addr = (char*)vtable->data + field->offset;
4510 /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
4512 switch (field->type->type) {
4513 case MONO_TYPE_BOOLEAN:
4515 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
4519 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
4522 case MONO_TYPE_CHAR:
4524 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
4528 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
4533 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
4537 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
4542 case MONO_TYPE_STRING:
4543 case MONO_TYPE_OBJECT:
4544 case MONO_TYPE_CLASS:
4545 case MONO_TYPE_SZARRAY:
4547 case MONO_TYPE_FNPTR:
4548 case MONO_TYPE_ARRAY:
4549 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
4550 type_to_eval_stack_type (field->type, *sp);
4555 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
4556 sp [0]->type = STACK_I8;
4557 sp [0]->inst_l = *((gint64 *)addr);
4562 case MONO_TYPE_VALUETYPE:
4571 CHECK_STACK_OVF (1);
4572 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4573 type_to_eval_stack_type (field->type, load);
4574 load->cil_code = ip;
4575 load->inst_left = ins;
4577 load->flags |= ins_flag;
4579 /* fixme: dont see the problem why this does not work */
4580 //cfg->disable_aot = TRUE;
4590 token = read32 (ip + 1);
4591 if (method->wrapper_type != MONO_WRAPPER_NONE)
4592 klass = mono_method_get_wrapper_data (method, token);
4594 klass = mono_class_get_full (image, token, generic_context);
4595 mono_class_init (klass);
4596 n = mono_type_to_stind (&klass->byval_arg);
4597 if (n == CEE_STOBJ) {
4598 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
4600 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
4602 MONO_INST_NEW (cfg, store, n);
4603 store->cil_code = ip;
4604 store->inst_left = sp [0];
4605 store->inst_right = sp [1];
4606 store->flags |= ins_flag;
4607 MONO_ADD_INS (bblock, store);
4619 token = read32 (ip + 1);
4620 if (method->wrapper_type != MONO_WRAPPER_NONE)
4621 klass = mono_method_get_wrapper_data (method, token);
4623 klass = mono_class_get_full (image, token, generic_context);
4624 mono_class_init (klass);
4626 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4631 *sp++ = handle_box (cfg, bblock, val, ip, klass);
4638 MONO_INST_NEW (cfg, ins, *ip);
4643 token = read32 (ip + 1);
4645 /* allocate the domainvar - becaus this is used in decompose_foreach */
4646 if (cfg->opt & MONO_OPT_SHARED)
4647 mono_get_domainvar (cfg);
4649 if (method->wrapper_type != MONO_WRAPPER_NONE)
4650 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4652 klass = mono_class_get_full (image, token, generic_context);
4654 mono_class_init (klass);
4655 ins->inst_newa_class = klass;
4656 ins->inst_newa_len = *sp;
4657 ins->type = STACK_OBJ;
4661 * we store the object so calls to create the array are not interleaved
4662 * with the arguments of other calls.
4665 MonoInst *store, *temp, *load;
4667 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
4668 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4669 store->cil_code = ins->cil_code;
4670 MONO_ADD_INS (bblock, store);
4671 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
4672 load->cil_code = ins->cil_code;
4679 MONO_INST_NEW (cfg, ins, *ip);
4680 ins->cil_code = ip++;
4682 ins->inst_left = *sp;
4683 ins->type = STACK_PTR;
4691 if (method->wrapper_type != MONO_WRAPPER_NONE)
4692 klass = (MonoClass*)mono_method_get_wrapper_data (method, read32 (ip + 1));
4694 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4695 mono_class_init (klass);
4696 NEW_LDELEMA (cfg, ins, sp, klass);
4706 token = read32 (ip + 1);
4707 klass = mono_class_get_full (image, token, generic_context);
4708 mono_class_init (klass);
4709 NEW_LDELEMA (cfg, load, sp, klass);
4710 load->cil_code = ip;
4711 MONO_INST_NEW (cfg, ins, mono_type_to_ldind (&klass->byval_arg));
4713 ins->inst_left = load;
4715 type_to_eval_stack_type (&klass->byval_arg, ins);
4729 case CEE_LDELEM_REF: {
4733 * ldind.x (ldelema (array, index))
4734 * ldelema does the bounds check
4738 klass = array_access_to_klass (*ip);
4739 NEW_LDELEMA (cfg, load, sp, klass);
4740 load->cil_code = ip;
4741 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
4743 ins->inst_left = load;
4745 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
4755 case CEE_STELEM_R8: {
4759 * stind.x (ldelema (array, index), val)
4760 * ldelema does the bounds check
4764 klass = array_access_to_klass (*ip);
4765 NEW_LDELEMA (cfg, load, sp, klass);
4766 load->cil_code = ip;
4767 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
4769 ins->inst_left = load;
4770 ins->inst_right = sp [2];
4772 handle_loaded_temps (cfg, bblock, stack_start, sp);
4773 MONO_ADD_INS (bblock, ins);
4781 * stind.x (ldelema (array, index), val)
4782 * ldelema does the bounds check
4787 token = read32 (ip + 1);
4788 klass = mono_class_get_full (image, token, generic_context);
4789 mono_class_init (klass);
4790 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4791 MonoInst *iargs [3];
4792 handle_loaded_temps (cfg, bblock, stack_start, sp);
4798 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4800 NEW_LDELEMA (cfg, load, sp, klass);
4801 load->cil_code = ip;
4803 n = mono_type_to_stind (&klass->byval_arg);
4805 handle_stobj (cfg, bblock, load, sp [2], ip, klass, FALSE, FALSE);
4807 MONO_INST_NEW (cfg, ins, n);
4809 ins->inst_left = load;
4810 ins->inst_right = sp [2];
4811 handle_loaded_temps (cfg, bblock, stack_start, sp);
4812 MONO_ADD_INS (bblock, ins);
4819 case CEE_STELEM_REF: {
4820 MonoInst *iargs [3];
4825 handle_loaded_temps (cfg, bblock, stack_start, sp);
4831 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4835 NEW_GROUP (cfg, group, sp [0], sp [1]);
4836 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
4838 ins->inst_left = group;
4839 ins->inst_right = sp [2];
4840 MONO_ADD_INS (bblock, ins);
4847 case CEE_CKFINITE: {
4848 MonoInst *store, *temp;
4851 /* this instr. can throw exceptions as side effect,
4852 * so we cant eliminate dead code which contains CKFINITE opdodes.
4853 * Spilling to memory makes sure that we always perform
4857 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
4859 ins->inst_left = sp [-1];
4860 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
4862 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4863 store->cil_code = ip;
4864 MONO_ADD_INS (bblock, store);
4866 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
4873 MONO_INST_NEW (cfg, ins, *ip);
4876 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4877 mono_class_init (klass);
4878 ins->type = STACK_MP;
4879 ins->inst_left = *sp;
4881 ins->inst_newa_class = klass;
4886 case CEE_MKREFANY: {
4887 MonoInst *loc, *klassconst;
4890 MONO_INST_NEW (cfg, ins, *ip);
4893 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4894 mono_class_init (klass);
4897 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
4898 NEW_TEMPLOADA (cfg, ins->inst_right, loc->inst_c0);
4900 NEW_PCONST (cfg, klassconst, klass);
4901 NEW_GROUP (cfg, ins->inst_left, *sp, klassconst);
4903 MONO_ADD_INS (bblock, ins);
4905 NEW_TEMPLOAD (cfg, *sp, loc->inst_c0);
4912 MonoClass *handle_class;
4914 CHECK_STACK_OVF (1);
4917 n = read32 (ip + 1);
4919 handle = mono_ldtoken (image, n, &handle_class, generic_context);
4920 mono_class_init (handle_class);
4922 if (cfg->opt & MONO_OPT_SHARED) {
4924 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
4926 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
4928 NEW_IMAGECONST (cfg, iargs [0], image);
4929 NEW_ICONST (cfg, iargs [1], n);
4930 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
4931 NEW_TEMPLOAD (cfg, res, temp);
4932 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4933 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
4934 MONO_ADD_INS (bblock, store);
4935 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4937 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method_full (image, read32 (ip + 6), NULL, generic_context)) &&
4938 (cmethod->klass == mono_defaults.monotype_class->parent) &&
4939 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
4940 MonoClass *tclass = mono_class_from_mono_type (handle);
4941 mono_class_init (tclass);
4942 if (mono_compile_aot)
4943 NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n);
4945 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
4946 ins->type = STACK_OBJ;
4947 ins->klass = cmethod->klass;
4950 if (mono_compile_aot)
4951 NEW_LDTOKENCONST (cfg, ins, image, n);
4953 NEW_PCONST (cfg, ins, handle);
4954 ins->type = STACK_VTYPE;
4955 ins->klass = handle_class;
4971 case CEE_ADD_OVF_UN:
4973 case CEE_MUL_OVF_UN:
4975 case CEE_SUB_OVF_UN:
4980 case CEE_ENDFINALLY:
4981 MONO_INST_NEW (cfg, ins, *ip);
4982 MONO_ADD_INS (bblock, ins);
4983 ins->cil_code = ip++;
4984 start_new_bblock = 1;
4987 * Control will leave the method so empty the stack, otherwise
4988 * the next basic block will start with a nonempty stack.
4990 while (sp != stack_start) {
4991 MONO_INST_NEW (cfg, ins, CEE_POP);
4995 MONO_ADD_INS (bblock, ins);
5001 if (*ip == CEE_LEAVE) {
5003 target = ip + 5 + (gint32)read32(ip + 1);
5006 target = ip + 2 + (signed char)(ip [1]);
5009 /* empty the stack */
5010 while (sp != stack_start) {
5011 MONO_INST_NEW (cfg, ins, CEE_POP);
5015 MONO_ADD_INS (bblock, ins);
5019 * If this leave statement is in a catch block, check for a
5020 * pending exception, and rethrow it if necessary.
5022 for (i = 0; i < header->num_clauses; ++i) {
5023 MonoExceptionClause *clause = &header->clauses [i];
5024 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code)) {
5027 temp = mono_emit_jit_icall (cfg, bblock, mono_thread_get_pending_exception, NULL, ip);
5028 NEW_TEMPLOAD (cfg, *sp, temp);
5030 MONO_INST_NEW (cfg, ins, OP_THROW_OR_NULL);
5031 ins->inst_left = *sp;
5033 MONO_ADD_INS (bblock, ins);
5037 /* fixme: call fault handler ? */
5039 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
5041 for (tmp = handlers; tmp; tmp = tmp->next) {
5043 link_bblock (cfg, bblock, tblock);
5044 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
5046 ins->inst_target_bb = tblock;
5047 MONO_ADD_INS (bblock, ins);
5049 g_list_free (handlers);
5052 MONO_INST_NEW (cfg, ins, CEE_BR);
5054 MONO_ADD_INS (bblock, ins);
5055 GET_BBLOCK (cfg, bbhash, tblock, target);
5056 link_bblock (cfg, bblock, tblock);
5057 CHECK_BBLOCK (target, ip, tblock);
5058 ins->inst_target_bb = tblock;
5059 start_new_bblock = 1;
5061 if (*ip == CEE_LEAVE)
5070 MONO_INST_NEW (cfg, ins, *ip);
5072 handle_loaded_temps (cfg, bblock, stack_start, sp);
5073 MONO_ADD_INS (bblock, ins);
5074 ins->cil_code = ip++;
5075 ins->inst_i0 = sp [0];
5076 ins->inst_i1 = sp [1];
5084 /* trampoline mono specific opcodes */
5085 case MONO_CUSTOM_PREFIX: {
5087 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
5092 case CEE_MONO_FUNC1: {
5094 gpointer func = NULL;
5100 case MONO_MARSHAL_CONV_STR_LPWSTR:
5101 func = mono_string_to_utf16;
5103 case MONO_MARSHAL_CONV_LPWSTR_STR:
5104 func = mono_string_from_utf16;
5106 case MONO_MARSHAL_CONV_LPSTR_STR:
5107 func = mono_string_new_wrapper;
5109 case MONO_MARSHAL_CONV_STR_LPTSTR:
5110 case MONO_MARSHAL_CONV_STR_LPSTR:
5111 func = mono_string_to_utf8;
5113 case MONO_MARSHAL_CONV_STR_BSTR:
5114 func = mono_string_to_bstr;
5116 case MONO_MARSHAL_CONV_STR_TBSTR:
5117 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
5118 func = mono_string_to_ansibstr;
5120 case MONO_MARSHAL_CONV_SB_LPSTR:
5121 case MONO_MARSHAL_CONV_SB_LPTSTR:
5122 func = mono_string_builder_to_utf8;
5124 case MONO_MARSHAL_CONV_SB_LPWSTR:
5125 func = mono_string_builder_to_utf16;
5127 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
5128 func = mono_array_to_savearray;
5130 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
5131 func = mono_array_to_lparray;
5133 case MONO_MARSHAL_CONV_DEL_FTN:
5134 func = mono_delegate_to_ftnptr;
5136 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
5137 func = mono_marshal_string_array;
5139 case MONO_MARSHAL_CONV_STRARRAY_STRWLPARRAY:
5140 func = mono_marshal_string_array_to_unicode;
5143 g_warning ("unknown conversion %d\n", ip [2]);
5144 g_assert_not_reached ();
5147 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5148 NEW_TEMPLOAD (cfg, *sp, temp);
5152 inline_costs += 10 * num_calls++;
5155 case CEE_MONO_PROC2: {
5156 gpointer func = NULL;
5162 case MONO_MARSHAL_CONV_LPSTR_SB:
5163 case MONO_MARSHAL_CONV_LPTSTR_SB:
5164 func = mono_string_utf8_to_builder;
5166 case MONO_MARSHAL_CONV_LPWSTR_SB:
5167 func = mono_string_utf16_to_builder;
5169 case MONO_MARSHAL_FREE_ARRAY:
5170 func = mono_marshal_free_array;
5173 g_assert_not_reached ();
5176 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5178 inline_costs += 10 * num_calls++;
5181 case CEE_MONO_PROC3: {
5182 gpointer func = NULL;
5188 case MONO_MARSHAL_CONV_STR_BYVALSTR:
5189 func = mono_string_to_byvalstr;
5191 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
5192 func = mono_string_to_byvalwstr;
5195 g_assert_not_reached ();
5198 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5200 inline_costs += 10 * num_calls++;
5206 mono_emit_jit_icall (cfg, bblock, g_free, sp, ip);
5208 inline_costs += 10 * num_calls++;
5210 case CEE_MONO_LDPTR:
5211 CHECK_STACK_OVF (1);
5213 token = read32 (ip + 2);
5214 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
5218 inline_costs += 10 * num_calls++;
5220 case CEE_MONO_VTADDR:
5223 MONO_INST_NEW (cfg, ins, OP_VTADDR);
5225 ins->type = STACK_MP;
5226 ins->inst_left = *sp;
5230 case CEE_MONO_NEWOBJ: {
5231 MonoInst *iargs [2];
5233 CHECK_STACK_OVF (1);
5235 token = read32 (ip + 2);
5236 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5237 mono_class_init (klass);
5238 NEW_DOMAINCONST (cfg, iargs [0]);
5239 NEW_CLASSCONST (cfg, iargs [1], klass);
5240 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
5241 NEW_TEMPLOAD (cfg, *sp, temp);
5244 inline_costs += 10 * num_calls++;
5247 case CEE_MONO_OBJADDR:
5250 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
5252 ins->type = STACK_MP;
5253 ins->inst_left = *sp;
5257 case CEE_MONO_LDNATIVEOBJ:
5260 token = read32 (ip + 2);
5261 klass = mono_method_get_wrapper_data (method, token);
5262 g_assert (klass->valuetype);
5263 mono_class_init (klass);
5264 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
5268 case CEE_MONO_RETOBJ:
5269 g_assert (cfg->ret);
5270 g_assert (method->signature->pinvoke);
5275 token = read32 (ip + 2);
5276 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5278 NEW_RETLOADA (cfg, ins);
5279 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
5281 if (sp != stack_start)
5284 MONO_INST_NEW (cfg, ins, CEE_BR);
5286 ins->inst_target_bb = end_bblock;
5287 MONO_ADD_INS (bblock, ins);
5288 link_bblock (cfg, bblock, end_bblock);
5289 start_new_bblock = 1;
5292 case CEE_MONO_CISINST:
5293 case CEE_MONO_CCASTCLASS: {
5298 token = read32 (ip + 2);
5299 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5300 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_CISINST) ? OP_CISINST : OP_CCASTCLASS);
5301 ins->type = STACK_I4;
5302 ins->inst_left = *sp;
5303 ins->inst_newa_class = klass;
5310 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
5319 /* somewhat similar to LDTOKEN */
5320 MonoInst *addr, *vtvar;
5321 CHECK_STACK_OVF (1);
5322 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
5324 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5325 addr->cil_code = ip;
5326 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
5328 ins->inst_left = addr;
5329 MONO_ADD_INS (bblock, ins);
5330 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5344 * The following transforms:
5345 * CEE_CEQ into OP_CEQ
5346 * CEE_CGT into OP_CGT
5347 * CEE_CGT_UN into OP_CGT_UN
5348 * CEE_CLT into OP_CLT
5349 * CEE_CLT_UN into OP_CLT_UN
5351 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
5353 MONO_INST_NEW (cfg, ins, cmp->opcode);
5355 cmp->inst_i0 = sp [0];
5356 cmp->inst_i1 = sp [1];
5360 cmp->opcode = OP_COMPARE;
5362 ins->type = STACK_I4;
5372 CHECK_STACK_OVF (1);
5374 n = read32 (ip + 2);
5375 if (method->wrapper_type != MONO_WRAPPER_NONE)
5376 cmethod = mono_method_get_wrapper_data (method, n);
5378 cmethod = mono_get_method_full (image, n, NULL, generic_context);
5381 mono_class_init (cmethod->klass);
5382 handle_loaded_temps (cfg, bblock, stack_start, sp);
5384 NEW_METHODCONST (cfg, argconst, cmethod);
5385 if (method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED)
5386 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
5388 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn_nosync, &argconst, ip);
5389 NEW_TEMPLOAD (cfg, *sp, temp);
5393 inline_costs += 10 * num_calls++;
5396 case CEE_LDVIRTFTN: {
5402 n = read32 (ip + 2);
5403 if (method->wrapper_type != MONO_WRAPPER_NONE)
5404 cmethod = mono_method_get_wrapper_data (method, n);
5406 cmethod = mono_get_method_full (image, n, NULL, generic_context);
5408 mono_class_init (cmethod->klass);
5409 handle_loaded_temps (cfg, bblock, stack_start, sp);
5413 NEW_METHODCONST (cfg, args [1], cmethod);
5414 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
5415 NEW_TEMPLOAD (cfg, *sp, temp);
5419 inline_costs += 10 * num_calls++;
5423 CHECK_STACK_OVF (1);
5425 n = read16 (ip + 2);
5427 NEW_ARGLOAD (cfg, ins, n);
5433 CHECK_STACK_OVF (1);
5435 n = read16 (ip + 2);
5437 NEW_ARGLOADA (cfg, ins, n);
5445 handle_loaded_temps (cfg, bblock, stack_start, sp);
5447 n = read16 (ip + 2);
5449 NEW_ARGSTORE (cfg, ins, n, *sp);
5451 if (ins->opcode == CEE_STOBJ) {
5452 NEW_ARGLOADA (cfg, ins, n);
5453 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5455 MONO_ADD_INS (bblock, ins);
5459 CHECK_STACK_OVF (1);
5461 n = read16 (ip + 2);
5463 NEW_LOCLOAD (cfg, ins, n);
5469 CHECK_STACK_OVF (1);
5471 n = read16 (ip + 2);
5473 NEW_LOCLOADA (cfg, ins, n);
5482 n = read16 (ip + 2);
5484 handle_loaded_temps (cfg, bblock, stack_start, sp);
5485 NEW_LOCSTORE (cfg, ins, n, *sp);
5487 if (ins->opcode == CEE_STOBJ) {
5488 NEW_LOCLOADA (cfg, ins, n);
5489 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5491 MONO_ADD_INS (bblock, ins);
5498 if (sp != stack_start)
5500 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
5501 ins->inst_left = *sp;
5504 cfg->flags |= MONO_CFG_HAS_ALLOCA;
5505 if (header->init_locals)
5506 ins->flags |= MONO_INST_INIT;
5510 /* FIXME: set init flag if locals init is set in this method */
5512 case CEE_ENDFILTER: {
5513 MonoExceptionClause *clause, *nearest;
5514 int cc, nearest_num;
5518 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
5520 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
5521 ins->inst_left = *sp;
5523 MONO_ADD_INS (bblock, ins);
5524 start_new_bblock = 1;
5529 for (cc = 0; cc < header->num_clauses; ++cc) {
5530 clause = &header->clauses [cc];
5531 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
5532 (!nearest || (clause->token_or_filter > nearest->token_or_filter))) {
5538 filter_lengths [nearest_num] = (ip - header->code) - nearest->token_or_filter;
5542 case CEE_UNALIGNED_:
5543 ins_flag |= MONO_INST_UNALIGNED;
5544 /* FIXME: record alignment? we can assume 1 for now */
5549 ins_flag |= MONO_INST_VOLATILE;
5553 ins_flag |= MONO_INST_TAILCALL;
5554 /* Can't inline tail calls at this time */
5555 inline_costs += 100000;
5562 token = read32 (ip + 2);
5563 if (method->wrapper_type != MONO_WRAPPER_NONE)
5564 klass = mono_method_get_wrapper_data (method, token);
5566 klass = mono_class_get_full (image, token, generic_context);
5567 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5568 MonoInst *store, *load;
5569 NEW_PCONST (cfg, load, NULL);
5570 load->cil_code = ip;
5571 load->type = STACK_OBJ;
5572 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
5573 store->cil_code = ip;
5574 handle_loaded_temps (cfg, bblock, stack_start, sp);
5575 MONO_ADD_INS (bblock, store);
5576 store->inst_i0 = sp [0];
5577 store->inst_i1 = load;
5579 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
5584 case CEE_CONSTRAINED_:
5585 /* FIXME: implement */
5587 token = read32 (ip + 2);
5588 constrained_call = mono_class_get_full (image, token, generic_context);
5593 MonoInst *iargs [3];
5596 if ((cfg->opt & MONO_OPT_INTRINS) && (ip [1] == CEE_CPBLK) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5)) {
5598 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
5599 copy->inst_left = sp [0];
5600 copy->inst_right = sp [1];
5601 copy->cil_code = ip;
5603 MONO_ADD_INS (bblock, copy);
5610 handle_loaded_temps (cfg, bblock, stack_start, sp);
5611 if (ip [1] == CEE_CPBLK) {
5612 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
5614 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
5623 ins_flag |= MONO_INST_NOTYPECHECK;
5625 ins_flag |= MONO_INST_NORANGECHECK;
5626 /* we ignore the no-nullcheck for now since we
5627 * really do it explicitly only when doing callvirt->call
5633 /* FIXME: check we are in a catch handler */
5634 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
5635 load->cil_code = ip;
5636 MONO_INST_NEW (cfg, ins, CEE_THROW);
5637 ins->inst_left = load;
5639 MONO_ADD_INS (bblock, ins);
5641 start_new_bblock = 1;
5646 CHECK_STACK_OVF (1);
5648 token = read32 (ip + 2);
5649 /* FIXXME: handle generics. */
5650 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
5651 MonoType *type = mono_type_create_from_typespec (image, token);
5652 token = mono_type_size (type, &align);
5654 MonoClass *szclass = mono_class_get_full (image, token, generic_context);
5655 mono_class_init (szclass);
5656 token = mono_class_value_size (szclass, &align);
5658 NEW_ICONST (cfg, ins, token);
5663 case CEE_REFANYTYPE:
5665 MONO_INST_NEW (cfg, ins, OP_REFANYTYPE);
5667 ins->type = STACK_MP;
5668 ins->inst_left = *sp;
5669 ins->type = STACK_VTYPE;
5670 ins->klass = mono_defaults.typehandle_class;
5679 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
5684 g_error ("opcode 0x%02x not handled", *ip);
5687 if (start_new_bblock != 1)
5690 bblock->cil_length = ip - bblock->cil_code;
5691 bblock->next_bb = end_bblock;
5692 link_bblock (cfg, bblock, end_bblock);
5694 if (cfg->method == method && cfg->domainvar) {
5698 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
5699 call->signature = helper_sig_domain_get;
5700 call->inst.type = STACK_PTR;
5701 call->fptr = mono_domain_get;
5702 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, (MonoInst*)call);
5704 MONO_ADD_INS (init_localsbb, store);
5707 if (header->init_locals) {
5709 for (i = 0; i < header->num_locals; ++i) {
5710 int t = header->locals [i]->type;
5711 if (t == MONO_TYPE_VALUETYPE && header->locals [i]->data.klass->enumtype)
5712 t = header->locals [i]->data.klass->enum_basetype->type;
5713 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
5714 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
5715 NEW_ICONST (cfg, ins, 0);
5716 NEW_LOCSTORE (cfg, store, i, ins);
5717 MONO_ADD_INS (init_localsbb, store);
5718 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
5719 MONO_INST_NEW (cfg, ins, OP_I8CONST);
5720 ins->type = STACK_I8;
5722 NEW_LOCSTORE (cfg, store, i, ins);
5723 MONO_ADD_INS (init_localsbb, store);
5724 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
5725 MONO_INST_NEW (cfg, ins, OP_R8CONST);
5726 ins->type = STACK_R8;
5727 ins->inst_p0 = (void*)&r8_0;
5728 NEW_LOCSTORE (cfg, store, i, ins);
5729 MONO_ADD_INS (init_localsbb, store);
5730 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF)) {
5731 NEW_LOCLOADA (cfg, ins, i);
5732 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (header->locals [i]), NULL, NULL);
5735 NEW_PCONST (cfg, ins, NULL);
5736 NEW_LOCSTORE (cfg, store, i, ins);
5737 MONO_ADD_INS (init_localsbb, store);
5743 /* resolve backward branches in the middle of an existing basic block */
5744 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
5746 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
5747 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
5748 if (tblock != start_bblock) {
5750 split_bblock (cfg, tblock, bblock);
5751 l = bblock->cil_code - header->code;
5752 bblock->cil_length = tblock->cil_length - l;
5753 tblock->cil_length = l;
5755 g_print ("recheck failed.\n");
5760 * we compute regions here, because the length of filter clauses is not known in advance.
5761 * It is computed in the CEE_ENDFILTER case in the above switch statement
5763 if (cfg->method == method) {
5765 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5766 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
5768 mono_create_spvar_for_region (cfg, bb->region);
5769 if (cfg->verbose_level > 2)
5770 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
5773 g_hash_table_destroy (bbhash);
5776 dont_inline = g_list_remove (dont_inline, method);
5777 return inline_costs;
5780 if (cfg->method != method)
5781 g_hash_table_destroy (bbhash);
5782 dont_inline = g_list_remove (dont_inline, method);
5786 if (cfg->method != method)
5787 g_hash_table_destroy (bbhash);
5788 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
5789 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
5790 dont_inline = g_list_remove (dont_inline, method);
5795 mono_print_tree (MonoInst *tree) {
5801 arity = mono_burg_arity [tree->opcode];
5803 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
5805 switch (tree->opcode) {
5807 printf ("[%d]", tree->inst_c0);
5810 printf ("[%lld]", tree->inst_l);
5813 printf ("[%f]", *(double*)tree->inst_p0);
5816 printf ("[%f]", *(float*)tree->inst_p0);
5820 printf ("[%d]", tree->inst_c0);
5823 if (tree->inst_offset < 0)
5824 printf ("[-0x%x(%s)]", -tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5826 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5829 printf ("[%s]", mono_arch_regname (tree->dreg));
5832 printf ("[%s]", tree->inst_newa_class->name);
5833 mono_print_tree (tree->inst_newa_len);
5844 case OP_VOIDCALLVIRT: {
5845 MonoCallInst *call = (MonoCallInst*)tree;
5847 printf ("[%s]", call->method->name);
5852 printf ("[%d (", tree->inst_c0);
5853 for (i = 0; i < tree->inst_phi_args [0]; i++) {
5856 printf ("%d", tree->inst_phi_args [i + 1]);
5867 case OP_LOAD_MEMBASE:
5868 case OP_LOADI4_MEMBASE:
5869 case OP_LOADU4_MEMBASE:
5870 case OP_LOADU1_MEMBASE:
5871 case OP_LOADI1_MEMBASE:
5872 case OP_LOADU2_MEMBASE:
5873 case OP_LOADI2_MEMBASE:
5874 printf ("[%s] <- [%s + 0x%x]", mono_arch_regname (tree->dreg), mono_arch_regname (tree->inst_basereg), tree->inst_offset);
5877 printf ("[B%d]", tree->inst_target_bb->block_num);
5887 case OP_VOIDCALL_REG:
5888 mono_print_tree (tree->inst_left);
5900 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
5901 mono_print_tree (tree->inst_left);
5904 if (!mono_arch_print_tree(tree, arity)) {
5906 mono_print_tree (tree->inst_left);
5908 mono_print_tree (tree->inst_right);
5919 mono_print_tree_nl (MonoInst *tree)
5921 mono_print_tree (tree);
5926 create_helper_signature (void)
5928 /* FIXME: set call conv */
5929 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
5930 helper_sig_newarr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5931 helper_sig_newarr->params [0] = helper_sig_newarr->params [1] = &mono_defaults.int_class->byval_arg;
5932 helper_sig_newarr->ret = &mono_defaults.object_class->byval_arg;
5933 helper_sig_newarr->params [2] = &mono_defaults.int32_class->byval_arg;
5934 helper_sig_newarr->pinvoke = 1;
5936 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
5937 helper_sig_newarr_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5938 helper_sig_newarr_specific->params [0] = &mono_defaults.int_class->byval_arg;
5939 helper_sig_newarr_specific->params [1] = &mono_defaults.int32_class->byval_arg;
5940 helper_sig_newarr_specific->ret = &mono_defaults.object_class->byval_arg;
5941 helper_sig_newarr_specific->pinvoke = 1;
5943 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
5944 helper_sig_object_new = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5945 helper_sig_object_new->params [0] = helper_sig_object_new->params [1] = &mono_defaults.int_class->byval_arg;
5946 helper_sig_object_new->ret = &mono_defaults.object_class->byval_arg;
5947 helper_sig_object_new->pinvoke = 1;
5949 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
5950 helper_sig_object_new_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5951 helper_sig_object_new_specific->params [0] = &mono_defaults.int_class->byval_arg;
5952 helper_sig_object_new_specific->ret = &mono_defaults.object_class->byval_arg;
5953 helper_sig_object_new_specific->pinvoke = 1;
5955 /* void* mono_method_compile (MonoMethod*) */
5956 helper_sig_compile = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5957 helper_sig_compile->params [0] = helper_sig_compile->ret = &mono_defaults.int_class->byval_arg;
5958 helper_sig_compile->pinvoke = 1;
5960 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
5961 helper_sig_compile_virt = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5962 helper_sig_compile_virt->params [0] = &mono_defaults.object_class->byval_arg;
5963 helper_sig_compile_virt->params [1] = helper_sig_compile_virt->ret = &mono_defaults.int_class->byval_arg;
5964 helper_sig_compile_virt->pinvoke = 1;
5966 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
5967 helper_sig_ldstr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5968 helper_sig_ldstr->params [0] = helper_sig_ldstr->params [1] = &mono_defaults.int_class->byval_arg;
5969 helper_sig_ldstr->params [2] = &mono_defaults.int32_class->byval_arg;
5970 helper_sig_ldstr->ret = &mono_defaults.object_class->byval_arg;
5971 helper_sig_ldstr->pinvoke = 1;
5973 /* MonoDomain *mono_domain_get (void) */
5974 helper_sig_domain_get = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5975 helper_sig_domain_get->ret = &mono_defaults.int_class->byval_arg;
5976 helper_sig_domain_get->pinvoke = 1;
5978 /* void* stelem_ref (MonoArray *, int index, MonoObject *) */
5979 helper_sig_stelem_ref = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5980 helper_sig_stelem_ref->params [0] = &mono_defaults.array_class->byval_arg;
5981 helper_sig_stelem_ref->params [1] = &mono_defaults.int32_class->byval_arg;
5982 helper_sig_stelem_ref->params [2] = &mono_defaults.object_class->byval_arg;
5983 helper_sig_stelem_ref->ret = &mono_defaults.void_class->byval_arg;
5984 helper_sig_stelem_ref->pinvoke = 1;
5986 /* void* stelem_ref_check (MonoArray *, MonoObject *) */
5987 helper_sig_stelem_ref_check = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5988 helper_sig_stelem_ref_check->params [0] = &mono_defaults.array_class->byval_arg;
5989 helper_sig_stelem_ref_check->params [1] = &mono_defaults.object_class->byval_arg;
5990 helper_sig_stelem_ref_check->ret = &mono_defaults.void_class->byval_arg;
5991 helper_sig_stelem_ref_check->pinvoke = 1;
5993 /* long amethod (long, long) */
5994 helper_sig_long_long_long = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5995 helper_sig_long_long_long->params [0] = helper_sig_long_long_long->params [1] =
5996 &mono_defaults.int64_class->byval_arg;
5997 helper_sig_long_long_long->ret = &mono_defaults.int64_class->byval_arg;
5998 helper_sig_long_long_long->pinvoke = 1;
6000 /* object amethod (intptr) */
6001 helper_sig_obj_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6002 helper_sig_obj_ptr->params [0] = &mono_defaults.int_class->byval_arg;
6003 helper_sig_obj_ptr->ret = &mono_defaults.object_class->byval_arg;
6004 helper_sig_obj_ptr->pinvoke = 1;
6006 /* void amethod (intptr) */
6007 helper_sig_void_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6008 helper_sig_void_ptr->params [0] = &mono_defaults.int_class->byval_arg;
6009 helper_sig_void_ptr->ret = &mono_defaults.void_class->byval_arg;
6010 helper_sig_void_ptr->pinvoke = 1;
6012 /* void amethod (MonoObject *obj) */
6013 helper_sig_void_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6014 helper_sig_void_obj->params [0] = &mono_defaults.object_class->byval_arg;
6015 helper_sig_void_obj->ret = &mono_defaults.void_class->byval_arg;
6016 helper_sig_void_obj->pinvoke = 1;
6018 /* intptr amethod (void) */
6019 helper_sig_ptr_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
6020 helper_sig_ptr_void->ret = &mono_defaults.int_class->byval_arg;
6021 helper_sig_ptr_void->pinvoke = 1;
6023 /* object amethod (void) */
6024 helper_sig_obj_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
6025 helper_sig_obj_void->ret = &mono_defaults.object_class->byval_arg;
6026 helper_sig_obj_void->pinvoke = 1;
6028 /* void amethod (intptr, intptr) */
6029 helper_sig_void_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6030 helper_sig_void_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
6031 helper_sig_void_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
6032 helper_sig_void_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
6033 helper_sig_void_ptr_ptr->pinvoke = 1;
6035 /* void amethod (intptr, intptr, intptr) */
6036 helper_sig_void_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
6037 helper_sig_void_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
6038 helper_sig_void_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
6039 helper_sig_void_ptr_ptr_ptr->params [2] = &mono_defaults.int_class->byval_arg;
6040 helper_sig_void_ptr_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
6041 helper_sig_void_ptr_ptr_ptr->pinvoke = 1;
6043 /* intptr amethod (intptr, intptr) */
6044 helper_sig_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6045 helper_sig_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
6046 helper_sig_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
6047 helper_sig_ptr_ptr_ptr->ret = &mono_defaults.int_class->byval_arg;
6048 helper_sig_ptr_ptr_ptr->pinvoke = 1;
6050 /* IntPtr amethod (object) */
6051 helper_sig_ptr_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6052 helper_sig_ptr_obj->params [0] = &mono_defaults.object_class->byval_arg;
6053 helper_sig_ptr_obj->ret = &mono_defaults.int_class->byval_arg;
6054 helper_sig_ptr_obj->pinvoke = 1;
6056 /* IntPtr amethod (int) */
6057 helper_sig_ptr_int = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6058 helper_sig_ptr_int->params [0] = &mono_defaults.int32_class->byval_arg;
6059 helper_sig_ptr_int->ret = &mono_defaults.int_class->byval_arg;
6060 helper_sig_ptr_int->pinvoke = 1;
6062 /* long amethod (long, guint32) */
6063 helper_sig_long_long_int = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6064 helper_sig_long_long_int->params [0] = &mono_defaults.int64_class->byval_arg;
6065 helper_sig_long_long_int->params [1] = &mono_defaults.int32_class->byval_arg;
6066 helper_sig_long_long_int->ret = &mono_defaults.int64_class->byval_arg;
6067 helper_sig_long_long_int->pinvoke = 1;
6069 /* ulong amethod (double) */
6070 helper_sig_ulong_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6071 helper_sig_ulong_double->params [0] = &mono_defaults.double_class->byval_arg;
6072 helper_sig_ulong_double->ret = &mono_defaults.uint64_class->byval_arg;
6073 helper_sig_ulong_double->pinvoke = 1;
6075 /* long amethod (double) */
6076 helper_sig_long_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6077 helper_sig_long_double->params [0] = &mono_defaults.double_class->byval_arg;
6078 helper_sig_long_double->ret = &mono_defaults.int64_class->byval_arg;
6079 helper_sig_long_double->pinvoke = 1;
6081 /* double amethod (long) */
6082 helper_sig_double_long = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6083 helper_sig_double_long->params [0] = &mono_defaults.int64_class->byval_arg;
6084 helper_sig_double_long->ret = &mono_defaults.double_class->byval_arg;
6085 helper_sig_double_long->pinvoke = 1;
6087 /* double amethod (int) */
6088 helper_sig_double_int = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6089 helper_sig_double_int->params [0] = &mono_defaults.int32_class->byval_arg;
6090 helper_sig_double_int->ret = &mono_defaults.double_class->byval_arg;
6091 helper_sig_double_int->pinvoke = 1;
6093 /* float amethod (long) */
6094 helper_sig_float_long = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6095 helper_sig_float_long->params [0] = &mono_defaults.int64_class->byval_arg;
6096 helper_sig_float_long->ret = &mono_defaults.single_class->byval_arg;
6097 helper_sig_float_long->pinvoke = 1;
6099 /* double amethod (double, double) */
6100 helper_sig_double_double_double = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6101 helper_sig_double_double_double->params [0] = &mono_defaults.double_class->byval_arg;
6102 helper_sig_double_double_double->params [1] = &mono_defaults.double_class->byval_arg;
6103 helper_sig_double_double_double->ret = &mono_defaults.double_class->byval_arg;
6104 helper_sig_double_double_double->pinvoke = 1;
6106 /* uint amethod (double) */
6107 helper_sig_uint_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6108 helper_sig_uint_double->params [0] = &mono_defaults.double_class->byval_arg;
6109 helper_sig_uint_double->ret = &mono_defaults.uint32_class->byval_arg;
6110 helper_sig_uint_double->pinvoke = 1;
6112 /* int amethod (double) */
6113 helper_sig_int_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6114 helper_sig_int_double->params [0] = &mono_defaults.double_class->byval_arg;
6115 helper_sig_int_double->ret = &mono_defaults.int32_class->byval_arg;
6116 helper_sig_int_double->pinvoke = 1;
6118 /* void initobj (intptr, int size) */
6119 helper_sig_initobj = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6120 helper_sig_initobj->params [0] = &mono_defaults.int_class->byval_arg;
6121 helper_sig_initobj->params [1] = &mono_defaults.int32_class->byval_arg;
6122 helper_sig_initobj->ret = &mono_defaults.void_class->byval_arg;
6123 helper_sig_initobj->pinvoke = 1;
6125 /* void memcpy (intptr, intptr, int size) */
6126 helper_sig_memcpy = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
6127 helper_sig_memcpy->params [0] = &mono_defaults.int_class->byval_arg;
6128 helper_sig_memcpy->params [1] = &mono_defaults.int_class->byval_arg;
6129 helper_sig_memcpy->params [2] = &mono_defaults.int32_class->byval_arg;
6130 helper_sig_memcpy->ret = &mono_defaults.void_class->byval_arg;
6131 helper_sig_memcpy->pinvoke = 1;
6133 /* void memset (intptr, int val, int size) */
6134 helper_sig_memset = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
6135 helper_sig_memset->params [0] = &mono_defaults.int_class->byval_arg;
6136 helper_sig_memset->params [1] = &mono_defaults.int32_class->byval_arg;
6137 helper_sig_memset->params [2] = &mono_defaults.int32_class->byval_arg;
6138 helper_sig_memset->ret = &mono_defaults.void_class->byval_arg;
6139 helper_sig_memset->pinvoke = 1;
6141 helper_sig_class_init_trampoline = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
6142 helper_sig_class_init_trampoline->ret = &mono_defaults.void_class->byval_arg;
6143 helper_sig_class_init_trampoline->pinvoke = 1;
6146 static GHashTable *jit_icall_hash_name = NULL;
6147 static GHashTable *jit_icall_hash_addr = NULL;
6150 mono_find_jit_icall_by_name (const char *name)
6152 MonoJitICallInfo *info;
6153 g_assert (jit_icall_hash_name);
6155 //printf ("lookup addr %s %p\n", name, g_hash_table_lookup (jit_icall_hash_name, name));
6156 EnterCriticalSection (&trampoline_hash_mutex);
6157 info = g_hash_table_lookup (jit_icall_hash_name, name);
6158 LeaveCriticalSection (&trampoline_hash_mutex);
6163 mono_find_jit_icall_by_addr (gconstpointer addr)
6165 MonoJitICallInfo *info;
6166 g_assert (jit_icall_hash_addr);
6168 EnterCriticalSection (&trampoline_hash_mutex);
6169 info = g_hash_table_lookup (jit_icall_hash_addr, (gpointer)addr);
6170 LeaveCriticalSection (&trampoline_hash_mutex);
6176 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
6179 MonoMethod *wrapper;
6182 if (callinfo->wrapper)
6183 return callinfo->wrapper;
6185 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
6186 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
6187 /* Must be domain neutral since there is only one copy */
6188 code = mono_jit_compile_method_with_opt (wrapper, default_opt | MONO_OPT_SHARED);
6190 EnterCriticalSection (&trampoline_hash_mutex);
6191 if (!callinfo->wrapper) {
6192 callinfo->wrapper = code;
6193 g_hash_table_insert (jit_icall_hash_addr, (gpointer)callinfo->wrapper, callinfo);
6195 LeaveCriticalSection (&trampoline_hash_mutex);
6198 return callinfo->wrapper;
6202 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save)
6204 MonoJitICallInfo *info;
6209 EnterCriticalSection (&trampoline_hash_mutex);
6211 if (!jit_icall_hash_name) {
6212 jit_icall_hash_name = g_hash_table_new (g_str_hash, g_str_equal);
6213 jit_icall_hash_addr = g_hash_table_new (NULL, NULL);
6216 if (g_hash_table_lookup (jit_icall_hash_name, name)) {
6217 g_warning ("jit icall already defined \"%s\"\n", name);
6218 g_assert_not_reached ();
6221 info = g_new (MonoJitICallInfo, 1);
6228 #ifdef MONO_USE_EXC_TABLES
6229 || mono_arch_has_unwind_info (func)
6232 info->wrapper = func;
6234 info->wrapper = NULL;
6237 g_hash_table_insert (jit_icall_hash_name, (gpointer)info->name, info);
6238 g_hash_table_insert (jit_icall_hash_addr, (gpointer)func, info);
6240 LeaveCriticalSection (&trampoline_hash_mutex);
6245 mono_create_class_init_trampoline (MonoVTable *vtable)
6249 /* previously created trampoline code */
6250 mono_domain_lock (vtable->domain);
6252 mono_g_hash_table_lookup (vtable->domain->class_init_trampoline_hash,
6254 mono_domain_unlock (vtable->domain);
6258 code = mono_arch_create_class_init_trampoline (vtable);
6260 /* store trampoline address */
6261 mono_domain_lock (vtable->domain);
6262 mono_g_hash_table_insert (vtable->domain->class_init_trampoline_hash,
6264 mono_domain_unlock (vtable->domain);
6266 EnterCriticalSection (&trampoline_hash_mutex);
6267 if (!class_init_hash_addr)
6268 class_init_hash_addr = g_hash_table_new (NULL, NULL);
6269 g_hash_table_insert (class_init_hash_addr, code, vtable);
6270 LeaveCriticalSection (&trampoline_hash_mutex);
6276 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method,
6277 gboolean add_sync_wrapper)
6282 if (add_sync_wrapper && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
6283 return mono_create_jump_trampoline (domain, mono_marshal_get_synchronized_wrapper (method), FALSE);
6285 code = mono_jit_find_compiled_method (domain, method);
6289 EnterCriticalSection (&trampoline_hash_mutex);
6291 if (jump_trampoline_hash) {
6292 code = g_hash_table_lookup (jump_trampoline_hash, method);
6294 LeaveCriticalSection (&trampoline_hash_mutex);
6299 ji = mono_arch_create_jump_trampoline (method);
6302 * mono_delegate_ctor needs to find the method metadata from the
6303 * trampoline address, so we save it here.
6306 mono_jit_info_table_add (mono_root_domain, ji);
6308 if (!jump_trampoline_hash)
6309 jump_trampoline_hash = g_hash_table_new (NULL, NULL);
6310 g_hash_table_insert (jump_trampoline_hash, method, ji->code_start);
6312 LeaveCriticalSection (&trampoline_hash_mutex);
6314 return ji->code_start;
6318 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
6322 EnterCriticalSection (&trampoline_hash_mutex);
6323 if (class_init_hash_addr)
6324 res = g_hash_table_lookup (class_init_hash_addr, addr);
6327 LeaveCriticalSection (&trampoline_hash_mutex);
6332 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func, gboolean no_throw)
6334 MonoJitICallInfo *info;
6336 if (!emul_opcode_map)
6337 emul_opcode_map = g_new0 (MonoJitICallInfo*, OP_LAST + 1);
6339 g_assert (!sig->hasthis);
6340 g_assert (sig->param_count < 3);
6342 info = mono_register_jit_icall (func, name, sig, no_throw);
6344 emul_opcode_map [opcode] = info;
6348 decompose_foreach (MonoInst *tree, gpointer data)
6350 static MonoJitICallInfo *newarr_info = NULL;
6351 static MonoJitICallInfo *newarr_specific_info = NULL;
6352 MonoJitICallInfo *info;
6355 switch (tree->opcode) {
6357 MonoCompile *cfg = data;
6358 MonoInst *iargs [3];
6361 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
6362 g_assert (newarr_info);
6363 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
6364 g_assert (newarr_specific_info);
6367 if (cfg->opt & MONO_OPT_SHARED) {
6368 NEW_DOMAINCONST (cfg, iargs [0]);
6369 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
6370 iargs [2] = tree->inst_newa_len;
6375 MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
6377 NEW_VTABLECONST (cfg, iargs [0], vtable);
6378 iargs [1] = tree->inst_newa_len;
6380 info = newarr_specific_info;
6383 mono_emulate_opcode (cfg, tree, iargs, info);
6385 /* Need to decompose arguments after the the opcode is decomposed */
6386 for (i = 0; i < info->sig->param_count; ++i)
6387 dec_foreach (iargs [i], cfg);
6397 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
6399 switch (mono_burg_arity [tree->opcode]) {
6402 mono_inst_foreach (tree->inst_left, func, data);
6405 mono_inst_foreach (tree->inst_left, func, data);
6406 mono_inst_foreach (tree->inst_right, func, data);
6409 g_assert_not_reached ();
6416 mono_print_bb_code (MonoBasicBlock *bb) {
6418 MonoInst *c = bb->code;
6420 mono_print_tree (c);
6428 print_dfn (MonoCompile *cfg) {
6433 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
6435 for (i = 0; i < cfg->num_bblocks; ++i) {
6436 bb = cfg->bblocks [i];
6438 char* code1, *code2;
6439 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
6440 if (bb->last_ins->cil_code)
6441 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
6443 code2 = g_strdup ("");
6445 code1 [strlen (code1) - 1] = 0;
6446 code = g_strdup_printf ("%s -> %s", code1, code2);
6450 code = g_strdup ("\n");
6451 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
6453 MonoInst *c = bb->code;
6455 mono_print_tree (c);
6463 g_print ("\tprev:");
6464 for (j = 0; j < bb->in_count; ++j) {
6465 g_print (" BB%d", bb->in_bb [j]->block_num);
6467 g_print ("\t\tsucc:");
6468 for (j = 0; j < bb->out_count; ++j) {
6469 g_print (" BB%d", bb->out_bb [j]->block_num);
6471 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
6474 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
6477 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
6479 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
6487 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
6491 g_assert (bb->code);
6492 bb->last_ins->next = inst;
6493 bb->last_ins = inst;
6495 bb->last_ins = bb->code = inst;
6500 mono_destroy_compile (MonoCompile *cfg)
6502 //mono_mempool_stats (cfg->mempool);
6503 g_hash_table_destroy (cfg->bb_hash);
6505 mono_regstate_free (cfg->rs);
6507 g_hash_table_destroy (cfg->spvars);
6508 mono_mempool_destroy (cfg->mempool);
6509 g_list_free (cfg->ldstr_list);
6511 g_free (cfg->varinfo);
6517 mono_get_lmf_addr (void)
6519 MonoJitTlsData *jit_tls;
6521 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
6522 return &jit_tls->lmf;
6524 g_assert_not_reached ();
6529 * mono_thread_abort:
6530 * @obj: exception object
6532 * abort the thread, print exception information and stack trace
6535 mono_thread_abort (MonoObject *obj)
6537 /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
6539 /* handle_remove should be eventually called for this thread, too
6546 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
6548 MonoJitTlsData *jit_tls;
6551 jit_tls = TlsGetValue (mono_jit_tls_id);
6555 jit_tls = g_new0 (MonoJitTlsData, 1);
6557 TlsSetValue (mono_jit_tls_id, jit_tls);
6559 jit_tls->abort_func = abort_func;
6560 jit_tls->end_of_stack = stack_start;
6562 lmf = g_new0 (MonoLMF, 1);
6565 jit_tls->lmf = jit_tls->first_lmf = lmf;
6567 mono_arch_setup_jit_tls_data (jit_tls);
6573 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
6576 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
6577 thread = mono_thread_current ();
6579 thread->jit_data = jit_tls;
6582 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
6585 mono_thread_abort_dummy (MonoObject *obj)
6587 if (mono_thread_attach_aborted_cb)
6588 mono_thread_attach_aborted_cb (obj);
6590 mono_thread_abort (obj);
6594 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
6597 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
6598 thread = mono_thread_current ();
6600 thread->jit_data = jit_tls;
6604 mini_thread_cleanup (MonoThread *thread)
6606 MonoJitTlsData *jit_tls = thread->jit_data;
6609 mono_arch_free_jit_tls_data (jit_tls);
6610 g_free (jit_tls->first_lmf);
6612 thread->jit_data = NULL;
6617 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
6619 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
6623 ji->data.target = target;
6624 ji->next = cfg->patch_info;
6626 cfg->patch_info = ji;
6630 mono_remove_patch_info (MonoCompile *cfg, int ip)
6632 MonoJumpInfo **ji = &cfg->patch_info;
6635 if ((*ji)->ip.i == ip)
6638 ji = &((*ji)->next);
6643 mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors)
6645 unsigned char *ip = patch_info->ip.i + code;
6646 gconstpointer target = NULL;
6648 switch (patch_info->type) {
6649 case MONO_PATCH_INFO_BB:
6650 target = patch_info->data.bb->native_offset + code;
6652 case MONO_PATCH_INFO_ABS:
6653 target = patch_info->data.target;
6655 case MONO_PATCH_INFO_LABEL:
6656 target = patch_info->data.inst->inst_c0 + code;
6658 case MONO_PATCH_INFO_IP:
6661 case MONO_PATCH_INFO_METHOD_REL:
6662 target = code + patch_info->data.offset;
6664 case MONO_PATCH_INFO_INTERNAL_METHOD: {
6665 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
6667 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
6668 g_assert_not_reached ();
6670 target = mono_icall_get_wrapper (mi);
6673 case MONO_PATCH_INFO_METHOD_JUMP: {
6676 /* get the trampoline to the method from the domain */
6677 target = mono_create_jump_trampoline (domain, patch_info->data.method, TRUE);
6678 if (!domain->jump_target_hash)
6679 domain->jump_target_hash = g_hash_table_new (NULL, NULL);
6680 list = g_hash_table_lookup (domain->jump_target_hash, patch_info->data.method);
6681 list = g_slist_prepend (list, ip);
6682 g_hash_table_insert (domain->jump_target_hash, patch_info->data.method, list);
6685 case MONO_PATCH_INFO_METHOD:
6686 if (patch_info->data.method == method) {
6689 /* get the trampoline to the method from the domain */
6690 target = mono_arch_create_jit_trampoline (patch_info->data.method);
6692 case MONO_PATCH_INFO_SWITCH: {
6693 gpointer *jump_table;
6696 mono_domain_lock (domain);
6697 jump_table = mono_code_manager_reserve (domain->code_mp, sizeof (gpointer) * patch_info->table_size);
6698 mono_domain_unlock (domain);
6700 for (i = 0; i < patch_info->table_size; i++) {
6701 jump_table [i] = code + (int)patch_info->data.table [i];
6703 target = jump_table;
6706 case MONO_PATCH_INFO_METHODCONST:
6707 case MONO_PATCH_INFO_CLASS:
6708 case MONO_PATCH_INFO_IMAGE:
6709 case MONO_PATCH_INFO_FIELD:
6710 target = patch_info->data.target;
6712 case MONO_PATCH_INFO_IID:
6713 mono_class_init (patch_info->data.klass);
6714 target = (gpointer)patch_info->data.klass->interface_id;
6716 case MONO_PATCH_INFO_VTABLE:
6717 target = mono_class_vtable (domain, patch_info->data.klass);
6719 case MONO_PATCH_INFO_CLASS_INIT:
6720 target = mono_create_class_init_trampoline (mono_class_vtable (domain, patch_info->data.klass));
6722 case MONO_PATCH_INFO_SFLDA: {
6723 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
6724 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (vtable->klass, method))
6725 /* Done by the generated code */
6729 mono_runtime_class_init (vtable);
6731 target = (char*)vtable->data + patch_info->data.field->offset;
6734 case MONO_PATCH_INFO_R4:
6735 case MONO_PATCH_INFO_R8:
6736 target = patch_info->data.target;
6738 case MONO_PATCH_INFO_EXC_NAME:
6739 target = patch_info->data.name;
6741 case MONO_PATCH_INFO_LDSTR:
6743 mono_ldstr (domain, patch_info->data.token->image,
6744 mono_metadata_token_index (patch_info->data.token->token));
6746 case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
6748 MonoClass *handle_class;
6750 handle = mono_ldtoken (patch_info->data.token->image,
6751 patch_info->data.token->token, &handle_class, NULL);
6752 mono_class_init (handle_class);
6753 mono_class_init (mono_class_from_mono_type (handle));
6756 mono_type_get_object (domain, handle);
6759 case MONO_PATCH_INFO_LDTOKEN: {
6761 MonoClass *handle_class;
6763 handle = mono_ldtoken (patch_info->data.token->image,
6764 patch_info->data.token->token, &handle_class, NULL);
6765 mono_class_init (handle_class);
6771 g_assert_not_reached ();
6774 return (gpointer)target;
6778 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
6779 MonoJitICallInfo *info;
6781 decompose_foreach (tree, cfg);
6783 switch (mono_burg_arity [tree->opcode]) {
6786 dec_foreach (tree->inst_left, cfg);
6788 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6789 MonoInst *iargs [2];
6791 iargs [0] = tree->inst_left;
6793 mono_emulate_opcode (cfg, tree, iargs, info);
6799 #ifdef MONO_ARCH_BIGMUL_INTRINS
6800 if (tree->opcode == OP_LMUL
6801 && (cfg->opt & MONO_OPT_INTRINS)
6802 && (tree->inst_left->opcode == CEE_CONV_I8
6803 || tree->inst_left->opcode == CEE_CONV_U8)
6804 && tree->inst_left->inst_left->type == STACK_I4
6805 && (tree->inst_right->opcode == CEE_CONV_I8
6806 || tree->inst_right->opcode == CEE_CONV_U8)
6807 && tree->inst_right->inst_left->type == STACK_I4) {
6808 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
6809 tree->inst_left = tree->inst_left->inst_left;
6810 tree->inst_right = tree->inst_right->inst_left;
6811 dec_foreach (tree, cfg);
6814 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6815 MonoInst *iargs [2];
6817 iargs [0] = tree->inst_i0;
6818 iargs [1] = tree->inst_i1;
6820 mono_emulate_opcode (cfg, tree, iargs, info);
6822 dec_foreach (iargs [0], cfg);
6823 dec_foreach (iargs [1], cfg);
6826 dec_foreach (tree->inst_left, cfg);
6827 dec_foreach (tree->inst_right, cfg);
6831 g_assert_not_reached ();
6836 decompose_pass (MonoCompile *cfg) {
6839 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6842 cfg->prev_ins = NULL;
6843 for (tree = cfg->cbb->code; tree; tree = tree->next) {
6844 dec_foreach (tree, cfg);
6845 cfg->prev_ins = tree;
6851 nullify_basic_block (MonoBasicBlock *bb)
6858 bb->code = bb->last_ins = NULL;
6859 bb->cil_code = NULL;
6863 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6867 for (i = 0; i < bb->out_count; i++) {
6868 MonoBasicBlock *ob = bb->out_bb [i];
6871 if (bb->out_count > 1) {
6872 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
6876 bb->out_bb [i] = repl;
6883 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6887 for (i = 0; i < bb->in_count; i++) {
6888 MonoBasicBlock *ib = bb->in_bb [i];
6891 if (bb->in_count > 1) {
6892 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
6896 bb->in_bb [i] = repl;
6903 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6907 for (i = 0; i < bb->out_count; i++) {
6908 MonoBasicBlock *ob = bb->out_bb [i];
6909 for (j = 0; j < ob->in_count; j++) {
6910 if (ob->in_bb [j] == orig) {
6911 ob->in_bb [j] = repl;
6920 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
6922 bb->out_count = bbn->out_count;
6923 bb->out_bb = bbn->out_bb;
6925 replace_basic_block (bb, bbn, bb);
6929 bb->last_ins->next = bbn->code;
6930 bb->last_ins = bbn->last_ins;
6933 bb->code = bbn->code;
6934 bb->last_ins = bbn->last_ins;
6936 bb->next_bb = bbn->next_bb;
6937 nullify_basic_block (bbn);
6941 * Optimizes the branches on the Control Flow Graph
6945 optimize_branches (MonoCompile *cfg)
6947 int i, changed = FALSE;
6948 MonoBasicBlock *bb, *bbn;
6949 guint32 niterations;
6952 * Some crazy loops could cause the code below to go into an infinite
6953 * loop, see bug #53003 for an example. To prevent this, we put an upper
6954 * bound on the number of iterations.
6961 /* we skip the entry block (exit is handled specially instead ) */
6962 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
6964 /* dont touch code inside exception clauses */
6965 if (bb->region != -1)
6968 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
6969 if (cfg->verbose_level > 2)
6970 g_print ("nullify block triggered %d\n", bbn->block_num);
6972 bb->next_bb = bbn->next_bb;
6974 for (i = 0; i < bbn->out_count; i++)
6975 replace_in_block (bbn->out_bb [i], bbn, NULL);
6977 nullify_basic_block (bbn);
6981 if (bb->out_count == 1) {
6982 bbn = bb->out_bb [0];
6984 /* conditional branches where true and false targets are the same can be also replaced with CEE_BR */
6985 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
6986 bb->last_ins->opcode = CEE_BR;
6987 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
6989 if (cfg->verbose_level > 2)
6990 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
6993 if (bb->region == bbn->region && bb->next_bb == bbn) {
6994 /* the block are in sequence anyway ... */
6996 /* branches to the following block can be removed */
6997 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
6998 bb->last_ins->opcode = CEE_NOP;
7000 if (cfg->verbose_level > 2)
7001 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
7004 if (bbn->in_count == 1) {
7006 if (bbn != cfg->bb_exit) {
7007 if (cfg->verbose_level > 2)
7008 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
7009 merge_basic_blocks (bb, bbn);
7013 //mono_print_bb_code (bb);
7018 } while (changed && (niterations > 0));
7025 /* we skip the entry block (exit is handled specially instead ) */
7026 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
7028 /* dont touch code inside exception clauses */
7029 if (bb->region != -1)
7032 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
7033 if (cfg->verbose_level > 2) {
7034 g_print ("nullify block triggered %d\n", bbn->block_num);
7036 bb->next_bb = bbn->next_bb;
7038 for (i = 0; i < bbn->out_count; i++)
7039 replace_in_block (bbn->out_bb [i], bbn, NULL);
7041 nullify_basic_block (bbn);
7047 if (bb->out_count == 1) {
7048 bbn = bb->out_bb [0];
7050 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
7051 bbn = bb->last_ins->inst_target_bb;
7052 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7053 bbn->code->inst_target_bb->region == bb->region) {
7055 if (cfg->verbose_level > 2)
7056 g_print ("in %s branch to branch triggered %d -> %d\n", cfg->method->name,
7057 bb->block_num, bbn->block_num);
7059 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
7060 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
7065 } else if (bb->out_count == 2) {
7066 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
7067 bbn = bb->last_ins->inst_true_bb;
7068 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7069 bbn->code->inst_target_bb->region == bb->region) {
7070 if (cfg->verbose_level > 2)
7071 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
7072 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
7075 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
7077 replace_in_block (bbn, bb, NULL);
7079 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
7080 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7082 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7088 bbn = bb->last_ins->inst_false_bb;
7089 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7090 bbn->code->inst_target_bb->region == bb->region) {
7091 if (cfg->verbose_level > 2)
7092 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
7093 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
7096 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
7098 replace_in_block (bbn, bb, NULL);
7100 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
7101 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7103 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7111 } while (changed && (niterations > 0));
7116 mono_compile_create_vars (MonoCompile *cfg)
7118 MonoMethodSignature *sig;
7119 MonoMethodHeader *header;
7122 header = ((MonoMethodNormal *)cfg->method)->header;
7124 sig = cfg->method->signature;
7126 if (!MONO_TYPE_IS_VOID (sig->ret)) {
7127 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
7128 cfg->ret->opcode = OP_RETARG;
7129 cfg->ret->inst_vtype = sig->ret;
7130 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
7132 if (cfg->verbose_level > 2)
7133 g_print ("creating vars\n");
7136 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
7138 for (i = 0; i < sig->param_count; ++i)
7139 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
7141 cfg->locals_start = cfg->num_varinfo;
7143 if (cfg->verbose_level > 2)
7144 g_print ("creating locals\n");
7145 for (i = 0; i < header->num_locals; ++i)
7146 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
7147 if (cfg->verbose_level > 2)
7148 g_print ("locals done\n");
7152 mono_print_code (MonoCompile *cfg)
7156 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7157 MonoInst *tree = bb->code;
7162 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
7164 for (; tree; tree = tree->next) {
7165 mono_print_tree (tree);
7170 bb->last_ins->next = NULL;
7174 extern const char * const mono_burg_rule_string [];
7177 emit_state (MonoCompile *cfg, MBState *state, int goal)
7180 int ern = mono_burg_rule (state, goal);
7181 const guint16 *nts = mono_burg_nts [ern];
7184 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
7188 // state->reg1 = state->reg2; /* chain rule */
7190 state->reg1 = mono_regstate_next_int (cfg->rs);
7191 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
7194 state->reg1 = mono_regstate_next_int (cfg->rs);
7195 state->reg2 = mono_regstate_next_int (cfg->rs);
7198 state->reg1 = mono_regstate_next_float (cfg->rs);
7205 mono_burg_kids (state, ern, kids);
7207 emit_state (cfg, kids [0], nts [0]);
7209 emit_state (cfg, kids [1], nts [1]);
7211 g_assert (!nts [3]);
7212 emit_state (cfg, kids [2], nts [2]);
7217 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
7218 if ((emit = mono_burg_func [ern]))
7219 emit (state, state->tree, cfg);
7222 #define DEBUG_SELECTION
7225 mini_select_instructions (MonoCompile *cfg)
7227 static const int reverse_map [] = {
7228 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
7229 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
7231 static const int reverse_fmap [] = {
7232 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
7233 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
7235 static const int reverse_lmap [] = {
7236 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
7237 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
7242 cfg->state_pool = mono_mempool_new ();
7243 cfg->rs = mono_regstate_new ();
7245 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7246 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode) &&
7247 bb->next_bb != bb->last_ins->inst_false_bb) {
7249 if (bb->next_bb == bb->last_ins->inst_true_bb) {
7250 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
7251 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
7252 bb->last_ins->inst_false_bb = tmp;
7254 if (bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
7255 bb->last_ins->opcode = reverse_map [bb->last_ins->opcode - CEE_BEQ];
7256 } else if (bb->last_ins->opcode >= OP_FBEQ && bb->last_ins->opcode <= OP_FBLT_UN) {
7257 bb->last_ins->opcode = reverse_fmap [bb->last_ins->opcode - OP_FBEQ];
7258 } else if (bb->last_ins->opcode >= OP_LBEQ && bb->last_ins->opcode <= OP_LBLT_UN) {
7259 bb->last_ins->opcode = reverse_lmap [bb->last_ins->opcode - OP_LBEQ];
7262 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
7263 inst->opcode = CEE_BR;
7264 inst->inst_target_bb = bb->last_ins->inst_false_bb;
7265 mono_bblock_add_inst (bb, inst);
7270 #ifdef DEBUG_SELECTION
7271 if (cfg->verbose_level >= 4) {
7272 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7273 MonoInst *tree = bb->code;
7274 g_print ("DUMP BLOCK %d:\n", bb->block_num);
7277 for (; tree; tree = tree->next) {
7278 mono_print_tree (tree);
7285 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7286 MonoInst *tree = bb->code, *next;
7292 bb->last_ins = NULL;
7295 mono_regstate_reset (cfg->rs);
7297 #ifdef DEBUG_SELECTION
7298 if (cfg->verbose_level >= 3)
7299 g_print ("LABEL BLOCK %d:\n", bb->block_num);
7301 for (; tree; tree = next) {
7303 #ifdef DEBUG_SELECTION
7304 if (cfg->verbose_level >= 3) {
7305 mono_print_tree (tree);
7310 if (!(mbstate = mono_burg_label (tree, cfg))) {
7311 g_warning ("unable to label tree %p", tree);
7312 mono_print_tree (tree);
7314 g_assert_not_reached ();
7316 emit_state (cfg, mbstate, MB_NTERM_stmt);
7318 bb->max_ireg = cfg->rs->next_vireg;
7319 bb->max_freg = cfg->rs->next_vfreg;
7322 bb->last_ins->next = NULL;
7324 mono_mempool_empty (cfg->state_pool);
7326 mono_mempool_destroy (cfg->state_pool);
7330 mono_codegen (MonoCompile *cfg)
7332 MonoJumpInfo *patch_info;
7334 int i, max_epilog_size;
7337 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7338 cfg->spill_count = 0;
7339 /* we reuse dfn here */
7340 /* bb->dfn = bb_count++; */
7341 mono_arch_local_regalloc (cfg, bb);
7344 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
7345 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
7347 code = mono_arch_emit_prolog (cfg);
7349 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7350 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
7352 cfg->code_len = code - cfg->native_code;
7353 cfg->prolog_end = cfg->code_len;
7355 mono_debug_open_method (cfg);
7357 /* emit code all basic blocks */
7358 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7359 bb->native_offset = cfg->code_len;
7360 mono_arch_output_basic_block (cfg, bb);
7362 cfg->bb_exit->native_offset = cfg->code_len;
7364 code = cfg->native_code + cfg->code_len;
7366 max_epilog_size = mono_arch_max_epilog_size (cfg);
7368 /* we always allocate code in cfg->domain->code_mp to increase locality */
7369 cfg->code_size = cfg->code_len + max_epilog_size;
7370 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
7372 mono_domain_lock (cfg->domain);
7373 code = mono_code_manager_reserve (cfg->domain->code_mp, cfg->code_size);
7374 mono_domain_unlock (cfg->domain);
7376 memcpy (code, cfg->native_code, cfg->code_len);
7377 g_free (cfg->native_code);
7378 cfg->native_code = code;
7379 code = cfg->native_code + cfg->code_len;
7381 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
7383 cfg->epilog_begin = cfg->code_len;
7385 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7386 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
7388 cfg->code_len = code - cfg->native_code;
7390 mono_arch_emit_epilog (cfg);
7392 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7393 switch (patch_info->type) {
7394 case MONO_PATCH_INFO_ABS: {
7395 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
7397 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
7398 if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) &&
7399 strstr (cfg->method->name, info->name))
7401 * This is an icall wrapper, and this is a call to the
7406 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
7407 patch_info->data.name = info->name;
7411 MonoVTable *vtable = mono_find_class_init_trampoline_by_addr (patch_info->data.target);
7413 patch_info->type = MONO_PATCH_INFO_CLASS_INIT;
7414 patch_info->data.klass = vtable->klass;
7419 case MONO_PATCH_INFO_SWITCH: {
7421 mono_domain_lock (cfg->domain);
7422 table = mono_code_manager_reserve (cfg->domain->code_mp, sizeof (gpointer) * patch_info->table_size);
7423 mono_domain_unlock (cfg->domain);
7425 patch_info->ip.i = patch_info->ip.label->inst_c0;
7426 for (i = 0; i < patch_info->table_size; i++) {
7427 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
7429 patch_info->data.target = table;
7438 if (cfg->verbose_level > 0)
7439 g_print ("Method %s emitted at %p to %p [%s]\n",
7440 mono_method_full_name (cfg->method, TRUE),
7441 cfg->native_code, cfg->native_code + cfg->code_len, cfg->domain->friendly_name);
7443 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info, cfg->run_cctors);
7445 mono_domain_lock (cfg->domain);
7446 mono_code_manager_commit (cfg->domain->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
7447 mono_domain_unlock (cfg->domain);
7449 mono_arch_flush_icache (cfg->native_code, cfg->code_len);
7451 mono_debug_close_method (cfg);
7455 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
7460 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
7461 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
7463 if (cp->opcode == OP_ICONST) {
7464 if (cfg->opt & MONO_OPT_CONSPROP) {
7465 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
7469 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
7470 if (cfg->opt & MONO_OPT_COPYPROP) {
7471 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
7477 arity = mono_burg_arity [tree->opcode];
7480 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
7481 if (cfg->opt & MONO_OPT_CFOLD)
7482 mono_constant_fold_inst (tree, NULL);
7483 /* The opcode may have changed */
7484 if (mono_burg_arity [tree->opcode] > 1) {
7485 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
7486 if (cfg->opt & MONO_OPT_CFOLD)
7487 mono_constant_fold_inst (tree, NULL);
7489 mono_constant_fold_inst (tree, NULL);
7495 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
7499 switch (tree->opcode) {
7509 if (tree->ssa_op == MONO_SSA_NOP) {
7510 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7527 case OP_VOIDCALL_REG:
7528 case OP_VOIDCALLVIRT:
7530 MonoCallInst *call = (MonoCallInst *)tree;
7531 MonoMethodSignature *sig = call->signature;
7532 int i, byref = FALSE;
7534 for (i = 0; i < sig->param_count; i++) {
7535 if (sig->params [i]->byref) {
7542 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7550 arity = mono_burg_arity [tree->opcode];
7556 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7559 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7560 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
7563 g_assert_not_reached ();
7568 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
7570 MonoInst *tree = bb->code;
7576 for (; tree; tree = tree->next) {
7578 mono_cprop_copy_values (cfg, tree, acp);
7580 mono_cprop_invalidate_values (tree, acp, acp_size);
7582 if (tree->ssa_op == MONO_SSA_STORE &&
7583 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
7584 MonoInst *i1 = tree->inst_i1;
7586 acp [tree->inst_i0->inst_c0] = NULL;
7588 for (i = 0; i < acp_size; i++) {
7589 if (acp [i] && acp [i]->opcode != OP_ICONST &&
7590 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
7595 if (i1->opcode == OP_ICONST) {
7596 acp [tree->inst_i0->inst_c0] = i1;
7597 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
7599 if (i1->ssa_op == MONO_SSA_LOAD &&
7600 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
7601 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
7602 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
7603 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
7608 if (tree->opcode == CEE_BEQ) {
7609 g_assert (tree->inst_i0->opcode == OP_COMPARE);
7610 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
7611 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
7613 tree->opcode = CEE_BR;
7614 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
7615 tree->inst_target_bb = tree->inst_true_bb;
7617 tree->inst_target_bb = tree->inst_false_bb;
7626 mono_local_cprop (MonoCompile *cfg)
7631 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
7633 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7634 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
7635 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
7640 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, int parts)
7642 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
7643 guint8 *ip = (guint8 *)header->code;
7646 int dfn = 0, i, code_size_ratio;
7648 mono_jit_stats.methods_compiled++;
7649 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
7650 mono_profiler_method_jit (method);
7652 cfg = g_new0 (MonoCompile, 1);
7653 cfg->method = method;
7654 cfg->mempool = mono_mempool_new ();
7656 cfg->prof_options = mono_profiler_get_events ();
7657 cfg->run_cctors = run_cctors;
7658 cfg->bb_hash = g_hash_table_new (NULL, NULL);
7659 cfg->domain = domain;
7660 cfg->verbose_level = mini_verbose;
7661 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
7662 ((MonoMethodNormal *)method)->header->max_stack);
7664 if (cfg->verbose_level > 2)
7665 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
7668 * create MonoInst* which represents arguments and local variables
7670 mono_compile_create_vars (cfg);
7672 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
7673 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7674 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
7675 mono_destroy_compile (cfg);
7679 mono_jit_stats.basic_blocks += cfg->num_bblocks;
7680 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
7682 if (cfg->num_varinfo > 2000) {
7684 * we disable some optimizations if there are too many variables
7685 * because JIT time may become too expensive. The actual number needs
7686 * to be tweaked and eventually the non-linear algorithms should be fixed.
7688 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
7689 cfg->disable_ssa = TRUE;
7691 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
7693 /* Depth-first ordering on basic blocks */
7694 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
7696 if (cfg->opt & MONO_OPT_BRANCH)
7697 optimize_branches (cfg);
7699 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
7700 if (cfg->num_bblocks != dfn + 1) {
7703 cfg->num_bblocks = dfn + 1;
7705 if (!header->clauses) {
7706 /* remove unreachable code, because the code in them may be
7707 * inconsistent (access to dead variables for example) */
7708 for (bb = cfg->bb_entry; bb;) {
7709 MonoBasicBlock *bbn = bb->next_bb;
7711 if (bbn && bbn->region == -1 && !bbn->dfn) {
7712 if (cfg->verbose_level > 1)
7713 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
7714 bb->next_bb = bbn->next_bb;
7715 nullify_basic_block (bbn);
7723 if (cfg->opt & MONO_OPT_LOOP) {
7724 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
7725 mono_compute_natural_loops (cfg);
7728 /* after method_to_ir */
7732 //#define DEBUGSSA "logic_run"
7733 #define DEBUGSSA_CLASS "Tests"
7736 if (!header->num_clauses && !cfg->disable_ssa) {
7737 mono_local_cprop (cfg);
7738 mono_ssa_compute (cfg);
7742 /* fixme: add all optimizations which requires SSA */
7743 if (cfg->opt & (MONO_OPT_DEADCE)) {
7744 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
7745 mono_local_cprop (cfg);
7746 mono_ssa_compute (cfg);
7748 if (cfg->verbose_level >= 2) {
7755 /* after SSA translation */
7759 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
7760 if (cfg->comp_done & MONO_COMP_SSA) {
7761 mono_ssa_cprop (cfg);
7763 mono_local_cprop (cfg);
7767 if (cfg->comp_done & MONO_COMP_SSA) {
7768 mono_ssa_deadce (cfg);
7770 //mono_ssa_strength_reduction (cfg);
7772 mono_ssa_remove (cfg);
7774 if (cfg->opt & MONO_OPT_BRANCH)
7775 optimize_branches (cfg);
7778 /* after SSA removal */
7782 decompose_pass (cfg);
7784 if (cfg->opt & MONO_OPT_LINEARS) {
7787 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
7788 cfg->comp_done &= ~MONO_COMP_LIVENESS;
7789 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
7790 mono_analyze_liveness (cfg);
7792 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
7793 regs = mono_arch_get_global_int_regs (cfg);
7794 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
7798 //mono_print_code (cfg);
7802 /* variables are allocated after decompose, since decompose could create temps */
7803 mono_arch_allocate_vars (cfg);
7805 if (cfg->opt & MONO_OPT_CFOLD)
7806 mono_constant_fold (cfg);
7808 mini_select_instructions (cfg);
7811 if (cfg->verbose_level >= 2) {
7812 char *id = mono_method_full_name (cfg->method, FALSE);
7813 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
7817 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
7819 jinfo = g_new0 (MonoJitInfo, 1);
7820 jinfo->method = method;
7821 jinfo->code_start = cfg->native_code;
7822 jinfo->code_size = cfg->code_len;
7823 jinfo->used_regs = cfg->used_int_regs;
7824 jinfo->domain_neutral = (cfg->opt & MONO_OPT_SHARED) != 0;
7826 if (header->num_clauses) {
7829 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
7830 jinfo->num_clauses = header->num_clauses;
7831 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
7832 sizeof (MonoJitExceptionInfo) * header->num_clauses);
7834 for (i = 0; i < header->num_clauses; i++) {
7835 MonoExceptionClause *ec = &header->clauses [i];
7836 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
7837 MonoBasicBlock *tblock;
7839 ei->flags = ec->flags;
7841 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7842 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->token_or_filter);
7844 ei->data.filter = cfg->native_code + tblock->native_offset;
7846 ei->data.token = ec->token_or_filter;
7849 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
7851 ei->try_start = cfg->native_code + tblock->native_offset;
7852 g_assert (tblock->native_offset);
7853 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
7855 ei->try_end = cfg->native_code + tblock->native_offset;
7856 g_assert (tblock->native_offset);
7857 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
7859 ei->handler_start = cfg->native_code + tblock->native_offset;
7863 cfg->jit_info = jinfo;
7865 mono_jit_info_table_add (cfg->domain, jinfo);
7867 /* collect statistics */
7868 mono_jit_stats.allocated_code_size += cfg->code_len;
7869 code_size_ratio = cfg->code_len;
7870 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
7871 mono_jit_stats.biggest_method_size = code_size_ratio;
7872 mono_jit_stats.biggest_method = method;
7874 code_size_ratio = (code_size_ratio * 100) / ((MonoMethodNormal *)method)->header->code_size;
7875 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
7876 mono_jit_stats.max_code_size_ratio = code_size_ratio;
7877 mono_jit_stats.max_ratio_method = method;
7879 mono_jit_stats.native_code_size += cfg->code_len;
7881 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7882 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
7888 mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain)
7891 GHashTable *jit_code_hash;
7892 gpointer code = NULL;
7898 jit_code_hash = target_domain->jit_code_hash;
7900 #ifdef MONO_USE_AOT_COMPILER
7901 if (!mono_compile_aot && (opt & MONO_OPT_AOT)) {
7903 MonoDomain *domain = mono_domain_get ();
7905 mono_domain_lock (domain);
7907 mono_class_init (method->klass);
7908 if ((info = mono_aot_get_method (domain, method))) {
7909 g_hash_table_insert (domain->jit_code_hash, method, info);
7910 mono_domain_unlock (domain);
7911 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
7912 return info->code_start;
7915 mono_domain_unlock (domain);
7919 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
7920 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
7923 if (!method->addr) {
7924 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
7925 method->addr = mono_lookup_internal_call (method);
7927 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
7928 mono_lookup_pinvoke_call (method, NULL, NULL);
7930 #ifdef MONO_USE_EXC_TABLES
7931 if (mono_method_blittable (method)) {
7932 return method->addr;
7935 nm = mono_marshal_get_native_wrapper (method);
7936 return mono_compile_method (nm);
7938 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
7939 //mono_debug_add_wrapper (method, nm);
7940 #ifdef MONO_USE_EXC_TABLES
7943 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
7944 const char *name = method->name;
7947 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
7948 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
7949 /* FIXME: uhm, we need a wrapper to handle exceptions? */
7950 return (gpointer)mono_delegate_ctor;
7951 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
7952 nm = mono_marshal_get_delegate_invoke (method);
7953 return mono_jit_compile_method (nm);
7954 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
7955 nm = mono_marshal_get_delegate_begin_invoke (method);
7956 return mono_jit_compile_method (nm);
7957 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
7958 nm = mono_marshal_get_delegate_end_invoke (method);
7959 return mono_jit_compile_method (nm);
7965 cfg = mini_method_compile (method, opt, target_domain, TRUE, 0);
7967 mono_domain_lock (target_domain);
7969 /* Check if some other thread already did the job. In this case, we can
7970 discard the code this thread generated. */
7972 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
7973 /* We can't use a domain specific method in another domain */
7974 if ((target_domain == mono_domain_get ()) || info->domain_neutral) {
7975 code = info->code_start;
7976 // printf("Discarding code for method %s\n", method->name);
7981 g_hash_table_insert (jit_code_hash, method, cfg->jit_info);
7982 code = cfg->native_code;
7985 mono_destroy_compile (cfg);
7987 if (target_domain->jump_target_hash) {
7988 MonoJumpInfo patch_info;
7990 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
7992 patch_info.next = NULL;
7993 patch_info.ip.i = 0;
7994 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
7995 patch_info.data.method = method;
7996 g_hash_table_remove (target_domain->jump_target_hash, method);
7998 for (tmp = list; tmp; tmp = tmp->next)
7999 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info, TRUE);
8000 g_slist_free (list);
8003 mono_domain_unlock (target_domain);
8005 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
8010 mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt)
8012 /* FIXME: later copy the code from mono */
8013 MonoDomain *target_domain, *domain = mono_domain_get ();
8017 if (default_opt & MONO_OPT_SHARED)
8018 target_domain = mono_root_domain;
8020 target_domain = domain;
8022 mono_domain_lock (target_domain);
8024 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
8025 /* We can't use a domain specific method in another domain */
8026 if (! ((domain != target_domain) && !info->domain_neutral)) {
8027 mono_domain_unlock (target_domain);
8028 mono_jit_stats.methods_lookups++;
8029 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
8030 return info->code_start;
8034 mono_domain_unlock (target_domain);
8035 p = mono_jit_compile_method_inner (method, target_domain);
8040 mono_jit_compile_method (MonoMethod *method)
8042 return mono_jit_compile_method_with_opt (method, default_opt);
8046 mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method)
8048 MonoDomain *target_domain;
8051 if (default_opt & MONO_OPT_SHARED)
8052 target_domain = mono_root_domain;
8054 target_domain = domain;
8056 mono_domain_lock (target_domain);
8058 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
8059 /* We can't use a domain specific method in another domain */
8060 if (! ((domain != target_domain) && !info->domain_neutral)) {
8061 mono_domain_unlock (target_domain);
8062 mono_jit_stats.methods_lookups++;
8063 return info->code_start;
8067 mono_domain_unlock (target_domain);
8073 * mono_jit_runtime_invoke:
8074 * @method: the method to invoke
8075 * @obj: this pointer
8076 * @params: array of parameter values.
8077 * @exc: used to catch exceptions objects
8080 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
8083 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc);
8085 if (obj == NULL && !(method->flags & METHOD_ATTRIBUTE_STATIC) && !method->string_ctor && (method->wrapper_type == 0)) {
8086 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
8090 invoke = mono_marshal_get_runtime_invoke (method);
8091 runtime_invoke = mono_jit_compile_method (invoke);
8092 return runtime_invoke (obj, params, exc);
8095 #ifdef PLATFORM_WIN32
8096 #define GET_CONTEXT \
8097 struct sigcontext *ctx = (struct sigcontext*)_dummy;
8100 #define GET_CONTEXT \
8101 void *ctx = context;
8102 #elif defined(__ppc__) || defined (__powerpc__)
8103 #define GET_CONTEXT \
8104 void *ctx = context;
8106 #define GET_CONTEXT \
8107 void **_p = (void **)&_dummy; \
8108 struct sigcontext *ctx = (struct sigcontext *)++_p;
8112 #ifdef MONO_ARCH_USE_SIGACTION
8113 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, siginfo_t *info, void *context)
8115 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy)
8119 SIG_HANDLER_SIGNATURE (sigfpe_signal_handler)
8124 exc = mono_get_exception_divide_by_zero ();
8126 mono_arch_handle_exception (ctx, exc, FALSE);
8130 SIG_HANDLER_SIGNATURE (sigill_signal_handler)
8134 exc = mono_get_exception_execution_engine ("SIGILL");
8136 mono_arch_handle_exception (ctx, exc, FALSE);
8139 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8142 sigsegv_signal_handler (int _dummy, siginfo_t *info, void *context)
8145 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
8146 struct sigcontext *ctx = (struct sigcontext *)&(((ucontext_t*)context)->uc_mcontext);
8148 /* Can't allocate memory using Boehm GC on altstack */
8149 if (jit_tls->stack_size &&
8150 ((guint8*)info->si_addr >= (guint8*)jit_tls->end_of_stack - jit_tls->stack_size) &&
8151 ((guint8*)info->si_addr < (guint8*)jit_tls->end_of_stack))
8152 exc = mono_domain_get ()->stack_overflow_ex;
8154 exc = mono_domain_get ()->null_reference_ex;
8156 mono_arch_handle_exception (ctx, exc, FALSE);
8162 SIG_HANDLER_SIGNATURE (sigsegv_signal_handler)
8166 mono_arch_handle_exception (ctx, NULL, FALSE);
8172 SIG_HANDLER_SIGNATURE (sigusr1_signal_handler)
8177 thread = mono_thread_current ();
8179 thread->abort_exc = mono_get_exception_thread_abort ();
8181 mono_arch_handle_exception (ctx, thread->abort_exc, FALSE);
8185 SIG_HANDLER_SIGNATURE (sigquit_signal_handler)
8190 exc = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
8192 mono_arch_handle_exception (ctx, exc, FALSE);
8196 SIG_HANDLER_SIGNATURE (sigint_signal_handler)
8201 exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
8203 mono_arch_handle_exception (ctx, exc, FALSE);
8206 #ifndef PLATFORM_WIN32
8208 add_signal_handler (int signo, gpointer handler)
8210 struct sigaction sa;
8212 #ifdef MONO_ARCH_USE_SIGACTION
8213 sa.sa_sigaction = handler;
8214 sigemptyset (&sa.sa_mask);
8215 sa.sa_flags = SA_SIGINFO;
8217 sa.sa_handler = handler;
8218 sigemptyset (&sa.sa_mask);
8221 g_assert (sigaction (signo, &sa, NULL) != -1);
8226 mono_runtime_install_handlers (void)
8228 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8229 struct sigaction sa;
8232 #ifdef PLATFORM_WIN32
8234 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
8235 win32_seh_set_handler(SIGILL, sigill_signal_handler);
8236 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
8237 if (getenv ("MONO_DEBUG"))
8238 win32_seh_set_handler(SIGINT, sigint_signal_handler);
8239 #else /* !PLATFORM_WIN32 */
8241 /* libpthreads has its own implementation of sigaction(),
8242 * but it seems to work well with our current exception
8243 * handlers. If not we must call syscall directly instead
8246 if (getenv ("MONO_DEBUG")) {
8247 add_signal_handler (SIGINT, sigint_signal_handler);
8250 add_signal_handler (SIGFPE, sigfpe_signal_handler);
8251 add_signal_handler (SIGQUIT, sigquit_signal_handler);
8252 add_signal_handler (SIGILL, sigill_signal_handler);
8253 add_signal_handler (SIGBUS, sigsegv_signal_handler);
8254 add_signal_handler (mono_thread_get_abort_signal (), sigusr1_signal_handler);
8257 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8258 sa.sa_sigaction = sigsegv_signal_handler;
8259 sigemptyset (&sa.sa_mask);
8260 sa.sa_flags = SA_SIGINFO | SA_STACK;
8261 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
8263 add_signal_handler (SIGSEGV, sigsegv_signal_handler);
8266 #endif /* PLATFORM_WIN32 */
8269 /* mono_jit_create_remoting_trampoline:
8270 * @method: pointer to the method info
8272 * Creates a trampoline which calls the remoting functions. This
8273 * is used in the vtable of transparent proxies.
8275 * Returns: a pointer to the newly created code
8278 mono_jit_create_remoting_trampoline (MonoMethod *method)
8281 guint8 *addr = NULL;
8283 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
8284 (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
8285 nm = mono_marshal_get_remoting_invoke (method);
8286 addr = mono_compile_method (nm);
8288 addr = mono_compile_method (method);
8294 mini_init (const char *filename)
8298 mono_arch_cpu_init ();
8300 if (!g_thread_supported ())
8301 g_thread_init (NULL);
8303 mono_jit_tls_id = TlsAlloc ();
8304 setup_jit_tls_data ((gpointer)-1, mono_thread_abort);
8306 InitializeCriticalSection (&trampoline_hash_mutex);
8310 if (default_opt & MONO_OPT_AOT)
8313 mono_runtime_install_handlers ();
8314 mono_threads_install_cleanup (mini_thread_cleanup);
8316 #define JIT_TRAMPOLINES_WORK
8317 #ifdef JIT_TRAMPOLINES_WORK
8318 mono_install_compile_method (mono_jit_compile_method);
8319 mono_install_trampoline (mono_arch_create_jit_trampoline);
8320 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
8322 #define JIT_INVOKE_WORKS
8323 #ifdef JIT_INVOKE_WORKS
8324 mono_install_runtime_invoke (mono_jit_runtime_invoke);
8325 mono_install_handler (mono_arch_get_throw_exception ());
8327 mono_install_stack_walk (mono_jit_walk_stack);
8329 domain = mono_init (filename);
8332 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
8333 ves_icall_get_frame_info);
8334 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
8335 ves_icall_get_trace);
8336 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
8337 mono_runtime_install_handlers);
8340 create_helper_signature ();
8342 #define JIT_CALLS_WORK
8343 #ifdef JIT_CALLS_WORK
8344 /* Needs to be called here since register_jit_icall depends on it */
8345 mono_marshal_init ();
8347 mono_arch_register_lowlevel_calls ();
8348 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
8349 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
8350 mono_register_jit_icall (mono_trace_enter_method, "mono_trace_enter_method", NULL, TRUE);
8351 mono_register_jit_icall (mono_trace_leave_method, "mono_trace_leave_method", NULL, TRUE);
8353 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
8354 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
8356 /* fixme: we cant hanlde vararg methods this way, because the signature is not constant */
8357 //mono_register_jit_icall (ves_array_element_address, "ves_array_element_address", NULL);
8358 //mono_register_jit_icall (mono_array_new_va, "mono_array_new_va", NULL);
8360 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
8361 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
8362 helper_sig_void_ptr, TRUE);
8363 mono_register_jit_icall (mono_thread_get_pending_exception, "mono_thread_get_pending_exception", helper_sig_obj_void, FALSE);
8366 * NOTE, NOTE, NOTE, NOTE:
8367 * when adding emulation for some opcodes, remember to also add a dummy
8368 * rule to the burg files, because we need the arity information to be correct.
8370 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult, TRUE);
8371 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un, FALSE);
8372 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf, FALSE);
8373 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv, FALSE);
8374 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un, FALSE);
8375 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem, FALSE);
8376 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un, FALSE);
8378 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl, TRUE);
8379 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr, TRUE);
8380 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un, TRUE);
8382 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8, FALSE);
8383 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4, FALSE);
8384 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8, FALSE);
8385 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8, FALSE);
8387 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
8388 mono_register_opcode_emulation (OP_FCONV_TO_I8, "__emul_fconv_to_i8", helper_sig_long_double, mono_fconv_i8, FALSE);
8390 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
8391 mono_register_opcode_emulation (CEE_CONV_R_UN, "__emul_conv_r_un", helper_sig_double_int, mono_conv_to_r8_un, FALSE);
8393 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
8394 mono_register_opcode_emulation (OP_LCONV_TO_R8, "__emul_lconv_to_r8", helper_sig_double_long, mono_lconv_to_r8, FALSE);
8396 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
8397 mono_register_opcode_emulation (OP_LCONV_TO_R4, "__emul_lconv_to_r4", helper_sig_float_long, mono_lconv_to_r4, FALSE);
8399 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
8400 mono_register_opcode_emulation (OP_LCONV_TO_R_UN, "__emul_lconv_to_r8_un", helper_sig_double_long, mono_lconv_to_r8_un, FALSE);
8402 #ifdef MONO_ARCH_EMULATE_FREM
8403 mono_register_opcode_emulation (OP_FREM, "__emul_frem", helper_sig_double_double_double, fmod, FALSE);
8406 #if SIZEOF_VOID_P == 4
8407 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4, TRUE);
8410 #warning "fixme: add opcode emulation"
8414 /* other jit icalls */
8415 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
8416 helper_sig_ptr_ptr_ptr, FALSE);
8417 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
8418 mono_register_jit_icall (mono_get_special_static_data, "mono_get_special_static_data", helper_sig_ptr_int, FALSE);
8419 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
8420 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
8421 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
8422 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
8423 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
8424 mono_register_jit_icall (helper_stelem_ref_check, "helper_stelem_ref_check", helper_sig_stelem_ref_check, FALSE);
8425 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
8426 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
8427 mono_register_jit_icall (mono_object_new_fast, "mono_object_new_fast", helper_sig_object_new_specific, FALSE);
8428 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
8429 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
8430 mono_register_jit_icall (mono_string_to_utf16, "mono_string_to_utf16", helper_sig_ptr_obj, FALSE);
8431 mono_register_jit_icall (mono_string_from_utf16, "mono_string_from_utf16", helper_sig_obj_ptr, FALSE);
8432 mono_register_jit_icall (mono_string_new_wrapper, "mono_string_new_wrapper", helper_sig_obj_ptr, FALSE);
8433 mono_register_jit_icall (mono_string_to_utf8, "mono_string_to_utf8", helper_sig_ptr_obj, FALSE);
8434 mono_register_jit_icall (mono_string_to_bstr, "mono_string_to_bstr", helper_sig_ptr_obj, FALSE);
8435 mono_register_jit_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", helper_sig_ptr_obj, FALSE);
8436 mono_register_jit_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", helper_sig_ptr_obj, FALSE);
8437 mono_register_jit_icall (mono_string_builder_to_utf16, "mono_string_builder_to_utf16", helper_sig_ptr_obj, FALSE);
8438 mono_register_jit_icall (mono_array_to_savearray, "mono_array_to_savearray", helper_sig_ptr_obj, FALSE);
8439 mono_register_jit_icall (mono_array_to_lparray, "mono_array_to_lparray", helper_sig_ptr_obj, FALSE);
8440 mono_register_jit_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", helper_sig_ptr_obj, FALSE);
8441 mono_register_jit_icall (mono_marshal_string_array, "mono_marshal_string_array", helper_sig_ptr_obj, FALSE);
8442 mono_register_jit_icall (mono_marshal_string_array_to_unicode, "mono_marshal_string_array_to_unicode", helper_sig_ptr_obj, FALSE);
8443 mono_register_jit_icall (mono_string_utf8_to_builder, "mono_string_utf8_to_builder", helper_sig_void_ptr_ptr, FALSE);
8444 mono_register_jit_icall (mono_string_utf16_to_builder, "mono_string_utf16_to_builder", helper_sig_void_ptr_ptr, FALSE);
8445 mono_register_jit_icall (mono_marshal_free_array, "mono_marshal_free_array", helper_sig_void_ptr_ptr, FALSE);
8446 mono_register_jit_icall (mono_string_to_byvalstr, "mono_string_to_byvalstr", helper_sig_void_ptr_ptr_ptr, FALSE);
8447 mono_register_jit_icall (mono_string_to_byvalwstr, "mono_string_to_byvalwstr", helper_sig_void_ptr_ptr_ptr, FALSE);
8448 mono_register_jit_icall (g_free, "g_free", helper_sig_void_ptr, FALSE);
8449 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
8450 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
8451 mono_register_jit_icall (mono_ldftn_nosync, "mono_ldftn_nosync", helper_sig_compile, FALSE);
8452 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
8455 #define JIT_RUNTIME_WORKS
8456 #ifdef JIT_RUNTIME_WORKS
8457 mono_runtime_install_cleanup ((MonoDomainFunc)mini_cleanup);
8458 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
8461 mono_thread_attach (domain);
8465 MonoJitStats mono_jit_stats = {0};
8468 print_jit_stats (void)
8470 if (mono_jit_stats.enabled) {
8471 g_print ("Mono Jit statistics\n");
8472 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
8473 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
8474 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
8475 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
8476 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
8477 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
8478 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
8479 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
8480 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
8481 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
8482 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
8483 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
8484 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
8485 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
8486 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
8487 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
8488 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
8489 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
8491 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
8492 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
8493 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
8494 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
8495 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
8497 g_print ("\nGeneric instances: %ld\n", mono_stats.generic_instance_count);
8498 g_print ("Inflated methods: %ld\n", mono_stats.inflated_method_count);
8499 g_print ("Inflated types: %ld\n", mono_stats.inflated_type_count);
8500 g_print ("Generics metadata size: %ld\n", mono_stats.generics_metadata_size);
8505 mini_cleanup (MonoDomain *domain)
8508 * mono_runtime_cleanup() and mono_domain_finalize () need to
8509 * be called early since they need the execution engine still
8510 * fully working (mono_domain_finalize may invoke managed finalizers
8511 * and mono_runtime_cleanup will wait for other threads to finish).
8513 mono_domain_finalize (domain, 2000);
8515 mono_runtime_cleanup (domain);
8517 mono_profiler_shutdown ();
8519 mono_debug_cleanup ();
8521 #ifdef PLATFORM_WIN32
8522 win32_seh_cleanup();
8525 mono_domain_free (domain, TRUE);
8531 mono_set_defaults (int verbose_level, guint32 opts)
8533 mini_verbose = verbose_level;
8538 mono_precompile_assembly (MonoAssembly *ass, void *user_data)
8540 MonoImage *image = ass->image;
8544 if (mini_verbose > 0)
8545 printf ("PRECOMPILE: %s.\n", ass->image->name);
8547 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
8548 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
8549 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
8551 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
8555 if (mini_verbose > 1) {
8556 char * desc = mono_method_full_name (method, TRUE);
8557 g_print ("Compiling %d %s\n", count, desc);
8560 mono_compile_method (method);
8564 void mono_precompile_assemblies ()
8566 mono_assembly_foreach ((GFunc)mono_precompile_assembly, NULL);