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:
4978 if (mono_find_jit_opcode_emulation (ins->opcode)) {
4979 MonoInst *store, *temp, *load;
4981 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
4982 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4983 store->cil_code = ins->cil_code;
4984 MONO_ADD_INS (bblock, store);
4985 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
4986 load->cil_code = ins->cil_code;
4988 /*g_print ("found emulation for %d\n", ins->opcode);*/
4992 case CEE_ENDFINALLY:
4993 MONO_INST_NEW (cfg, ins, *ip);
4994 MONO_ADD_INS (bblock, ins);
4995 ins->cil_code = ip++;
4996 start_new_bblock = 1;
4999 * Control will leave the method so empty the stack, otherwise
5000 * the next basic block will start with a nonempty stack.
5002 while (sp != stack_start) {
5003 MONO_INST_NEW (cfg, ins, CEE_POP);
5007 MONO_ADD_INS (bblock, ins);
5013 if (*ip == CEE_LEAVE) {
5015 target = ip + 5 + (gint32)read32(ip + 1);
5018 target = ip + 2 + (signed char)(ip [1]);
5021 /* empty the stack */
5022 while (sp != stack_start) {
5023 MONO_INST_NEW (cfg, ins, CEE_POP);
5027 MONO_ADD_INS (bblock, ins);
5031 * If this leave statement is in a catch block, check for a
5032 * pending exception, and rethrow it if necessary.
5034 for (i = 0; i < header->num_clauses; ++i) {
5035 MonoExceptionClause *clause = &header->clauses [i];
5036 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code)) {
5039 temp = mono_emit_jit_icall (cfg, bblock, mono_thread_get_pending_exception, NULL, ip);
5040 NEW_TEMPLOAD (cfg, *sp, temp);
5042 MONO_INST_NEW (cfg, ins, OP_THROW_OR_NULL);
5043 ins->inst_left = *sp;
5045 MONO_ADD_INS (bblock, ins);
5049 /* fixme: call fault handler ? */
5051 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
5053 for (tmp = handlers; tmp; tmp = tmp->next) {
5055 link_bblock (cfg, bblock, tblock);
5056 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
5058 ins->inst_target_bb = tblock;
5059 MONO_ADD_INS (bblock, ins);
5061 g_list_free (handlers);
5064 MONO_INST_NEW (cfg, ins, CEE_BR);
5066 MONO_ADD_INS (bblock, ins);
5067 GET_BBLOCK (cfg, bbhash, tblock, target);
5068 link_bblock (cfg, bblock, tblock);
5069 CHECK_BBLOCK (target, ip, tblock);
5070 ins->inst_target_bb = tblock;
5071 start_new_bblock = 1;
5073 if (*ip == CEE_LEAVE)
5082 MONO_INST_NEW (cfg, ins, *ip);
5084 handle_loaded_temps (cfg, bblock, stack_start, sp);
5085 MONO_ADD_INS (bblock, ins);
5086 ins->cil_code = ip++;
5087 ins->inst_i0 = sp [0];
5088 ins->inst_i1 = sp [1];
5096 /* trampoline mono specific opcodes */
5097 case MONO_CUSTOM_PREFIX: {
5099 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
5104 case CEE_MONO_FUNC1: {
5106 gpointer func = NULL;
5112 case MONO_MARSHAL_CONV_STR_LPWSTR:
5113 func = mono_string_to_utf16;
5115 case MONO_MARSHAL_CONV_LPWSTR_STR:
5116 func = mono_string_from_utf16;
5118 case MONO_MARSHAL_CONV_LPSTR_STR:
5119 func = mono_string_new_wrapper;
5121 case MONO_MARSHAL_CONV_STR_LPTSTR:
5122 case MONO_MARSHAL_CONV_STR_LPSTR:
5123 func = mono_string_to_utf8;
5125 case MONO_MARSHAL_CONV_STR_BSTR:
5126 func = mono_string_to_bstr;
5128 case MONO_MARSHAL_CONV_STR_TBSTR:
5129 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
5130 func = mono_string_to_ansibstr;
5132 case MONO_MARSHAL_CONV_SB_LPSTR:
5133 case MONO_MARSHAL_CONV_SB_LPTSTR:
5134 func = mono_string_builder_to_utf8;
5136 case MONO_MARSHAL_CONV_SB_LPWSTR:
5137 func = mono_string_builder_to_utf16;
5139 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
5140 func = mono_array_to_savearray;
5142 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
5143 func = mono_array_to_lparray;
5145 case MONO_MARSHAL_CONV_DEL_FTN:
5146 func = mono_delegate_to_ftnptr;
5148 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
5149 func = mono_marshal_string_array;
5151 case MONO_MARSHAL_CONV_STRARRAY_STRWLPARRAY:
5152 func = mono_marshal_string_array_to_unicode;
5155 g_warning ("unknown conversion %d\n", ip [2]);
5156 g_assert_not_reached ();
5159 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5160 NEW_TEMPLOAD (cfg, *sp, temp);
5164 inline_costs += 10 * num_calls++;
5167 case CEE_MONO_PROC2: {
5168 gpointer func = NULL;
5174 case MONO_MARSHAL_CONV_LPSTR_SB:
5175 case MONO_MARSHAL_CONV_LPTSTR_SB:
5176 func = mono_string_utf8_to_builder;
5178 case MONO_MARSHAL_CONV_LPWSTR_SB:
5179 func = mono_string_utf16_to_builder;
5181 case MONO_MARSHAL_FREE_ARRAY:
5182 func = mono_marshal_free_array;
5185 g_assert_not_reached ();
5188 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5190 inline_costs += 10 * num_calls++;
5193 case CEE_MONO_PROC3: {
5194 gpointer func = NULL;
5200 case MONO_MARSHAL_CONV_STR_BYVALSTR:
5201 func = mono_string_to_byvalstr;
5203 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
5204 func = mono_string_to_byvalwstr;
5207 g_assert_not_reached ();
5210 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5212 inline_costs += 10 * num_calls++;
5218 mono_emit_jit_icall (cfg, bblock, g_free, sp, ip);
5220 inline_costs += 10 * num_calls++;
5222 case CEE_MONO_LDPTR:
5223 CHECK_STACK_OVF (1);
5225 token = read32 (ip + 2);
5226 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
5230 inline_costs += 10 * num_calls++;
5232 case CEE_MONO_VTADDR:
5235 MONO_INST_NEW (cfg, ins, OP_VTADDR);
5237 ins->type = STACK_MP;
5238 ins->inst_left = *sp;
5242 case CEE_MONO_NEWOBJ: {
5243 MonoInst *iargs [2];
5245 CHECK_STACK_OVF (1);
5247 token = read32 (ip + 2);
5248 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5249 mono_class_init (klass);
5250 NEW_DOMAINCONST (cfg, iargs [0]);
5251 NEW_CLASSCONST (cfg, iargs [1], klass);
5252 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
5253 NEW_TEMPLOAD (cfg, *sp, temp);
5256 inline_costs += 10 * num_calls++;
5259 case CEE_MONO_OBJADDR:
5262 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
5264 ins->type = STACK_MP;
5265 ins->inst_left = *sp;
5269 case CEE_MONO_LDNATIVEOBJ:
5272 token = read32 (ip + 2);
5273 klass = mono_method_get_wrapper_data (method, token);
5274 g_assert (klass->valuetype);
5275 mono_class_init (klass);
5276 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
5280 case CEE_MONO_RETOBJ:
5281 g_assert (cfg->ret);
5282 g_assert (method->signature->pinvoke);
5287 token = read32 (ip + 2);
5288 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5290 NEW_RETLOADA (cfg, ins);
5291 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
5293 if (sp != stack_start)
5296 MONO_INST_NEW (cfg, ins, CEE_BR);
5298 ins->inst_target_bb = end_bblock;
5299 MONO_ADD_INS (bblock, ins);
5300 link_bblock (cfg, bblock, end_bblock);
5301 start_new_bblock = 1;
5304 case CEE_MONO_CISINST:
5305 case CEE_MONO_CCASTCLASS: {
5310 token = read32 (ip + 2);
5311 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5312 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_CISINST) ? OP_CISINST : OP_CCASTCLASS);
5313 ins->type = STACK_I4;
5314 ins->inst_left = *sp;
5315 ins->inst_newa_class = klass;
5322 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
5331 /* somewhat similar to LDTOKEN */
5332 MonoInst *addr, *vtvar;
5333 CHECK_STACK_OVF (1);
5334 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
5336 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5337 addr->cil_code = ip;
5338 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
5340 ins->inst_left = addr;
5341 MONO_ADD_INS (bblock, ins);
5342 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5356 * The following transforms:
5357 * CEE_CEQ into OP_CEQ
5358 * CEE_CGT into OP_CGT
5359 * CEE_CGT_UN into OP_CGT_UN
5360 * CEE_CLT into OP_CLT
5361 * CEE_CLT_UN into OP_CLT_UN
5363 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
5365 MONO_INST_NEW (cfg, ins, cmp->opcode);
5367 cmp->inst_i0 = sp [0];
5368 cmp->inst_i1 = sp [1];
5372 cmp->opcode = OP_COMPARE;
5374 ins->type = STACK_I4;
5384 CHECK_STACK_OVF (1);
5386 n = read32 (ip + 2);
5387 if (method->wrapper_type != MONO_WRAPPER_NONE)
5388 cmethod = mono_method_get_wrapper_data (method, n);
5390 cmethod = mono_get_method_full (image, n, NULL, generic_context);
5393 mono_class_init (cmethod->klass);
5394 handle_loaded_temps (cfg, bblock, stack_start, sp);
5396 NEW_METHODCONST (cfg, argconst, cmethod);
5397 if (method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED)
5398 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
5400 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn_nosync, &argconst, ip);
5401 NEW_TEMPLOAD (cfg, *sp, temp);
5405 inline_costs += 10 * num_calls++;
5408 case CEE_LDVIRTFTN: {
5414 n = read32 (ip + 2);
5415 if (method->wrapper_type != MONO_WRAPPER_NONE)
5416 cmethod = mono_method_get_wrapper_data (method, n);
5418 cmethod = mono_get_method_full (image, n, NULL, generic_context);
5420 mono_class_init (cmethod->klass);
5421 handle_loaded_temps (cfg, bblock, stack_start, sp);
5425 NEW_METHODCONST (cfg, args [1], cmethod);
5426 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
5427 NEW_TEMPLOAD (cfg, *sp, temp);
5431 inline_costs += 10 * num_calls++;
5435 CHECK_STACK_OVF (1);
5437 n = read16 (ip + 2);
5439 NEW_ARGLOAD (cfg, ins, n);
5445 CHECK_STACK_OVF (1);
5447 n = read16 (ip + 2);
5449 NEW_ARGLOADA (cfg, ins, n);
5457 handle_loaded_temps (cfg, bblock, stack_start, sp);
5459 n = read16 (ip + 2);
5461 NEW_ARGSTORE (cfg, ins, n, *sp);
5463 if (ins->opcode == CEE_STOBJ) {
5464 NEW_ARGLOADA (cfg, ins, n);
5465 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5467 MONO_ADD_INS (bblock, ins);
5471 CHECK_STACK_OVF (1);
5473 n = read16 (ip + 2);
5475 NEW_LOCLOAD (cfg, ins, n);
5481 CHECK_STACK_OVF (1);
5483 n = read16 (ip + 2);
5485 NEW_LOCLOADA (cfg, ins, n);
5494 n = read16 (ip + 2);
5496 handle_loaded_temps (cfg, bblock, stack_start, sp);
5497 NEW_LOCSTORE (cfg, ins, n, *sp);
5499 if (ins->opcode == CEE_STOBJ) {
5500 NEW_LOCLOADA (cfg, ins, n);
5501 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5503 MONO_ADD_INS (bblock, ins);
5510 if (sp != stack_start)
5512 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
5513 ins->inst_left = *sp;
5516 cfg->flags |= MONO_CFG_HAS_ALLOCA;
5517 if (header->init_locals)
5518 ins->flags |= MONO_INST_INIT;
5522 /* FIXME: set init flag if locals init is set in this method */
5524 case CEE_ENDFILTER: {
5525 MonoExceptionClause *clause, *nearest;
5526 int cc, nearest_num;
5530 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
5532 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
5533 ins->inst_left = *sp;
5535 MONO_ADD_INS (bblock, ins);
5536 start_new_bblock = 1;
5541 for (cc = 0; cc < header->num_clauses; ++cc) {
5542 clause = &header->clauses [cc];
5543 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
5544 (!nearest || (clause->token_or_filter > nearest->token_or_filter))) {
5550 filter_lengths [nearest_num] = (ip - header->code) - nearest->token_or_filter;
5554 case CEE_UNALIGNED_:
5555 ins_flag |= MONO_INST_UNALIGNED;
5556 /* FIXME: record alignment? we can assume 1 for now */
5561 ins_flag |= MONO_INST_VOLATILE;
5565 ins_flag |= MONO_INST_TAILCALL;
5566 /* Can't inline tail calls at this time */
5567 inline_costs += 100000;
5574 token = read32 (ip + 2);
5575 if (method->wrapper_type != MONO_WRAPPER_NONE)
5576 klass = mono_method_get_wrapper_data (method, token);
5578 klass = mono_class_get_full (image, token, generic_context);
5579 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5580 MonoInst *store, *load;
5581 NEW_PCONST (cfg, load, NULL);
5582 load->cil_code = ip;
5583 load->type = STACK_OBJ;
5584 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
5585 store->cil_code = ip;
5586 handle_loaded_temps (cfg, bblock, stack_start, sp);
5587 MONO_ADD_INS (bblock, store);
5588 store->inst_i0 = sp [0];
5589 store->inst_i1 = load;
5591 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
5596 case CEE_CONSTRAINED_:
5597 /* FIXME: implement */
5599 token = read32 (ip + 2);
5600 constrained_call = mono_class_get_full (image, token, generic_context);
5605 MonoInst *iargs [3];
5608 if ((cfg->opt & MONO_OPT_INTRINS) && (ip [1] == CEE_CPBLK) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5)) {
5610 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
5611 copy->inst_left = sp [0];
5612 copy->inst_right = sp [1];
5613 copy->cil_code = ip;
5615 MONO_ADD_INS (bblock, copy);
5622 handle_loaded_temps (cfg, bblock, stack_start, sp);
5623 if (ip [1] == CEE_CPBLK) {
5624 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
5626 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
5635 ins_flag |= MONO_INST_NOTYPECHECK;
5637 ins_flag |= MONO_INST_NORANGECHECK;
5638 /* we ignore the no-nullcheck for now since we
5639 * really do it explicitly only when doing callvirt->call
5645 /* FIXME: check we are in a catch handler */
5646 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
5647 load->cil_code = ip;
5648 MONO_INST_NEW (cfg, ins, CEE_THROW);
5649 ins->inst_left = load;
5651 MONO_ADD_INS (bblock, ins);
5653 start_new_bblock = 1;
5658 CHECK_STACK_OVF (1);
5660 token = read32 (ip + 2);
5661 /* FIXXME: handle generics. */
5662 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
5663 MonoType *type = mono_type_create_from_typespec (image, token);
5664 token = mono_type_size (type, &align);
5666 MonoClass *szclass = mono_class_get_full (image, token, generic_context);
5667 mono_class_init (szclass);
5668 token = mono_class_value_size (szclass, &align);
5670 NEW_ICONST (cfg, ins, token);
5675 case CEE_REFANYTYPE:
5677 MONO_INST_NEW (cfg, ins, OP_REFANYTYPE);
5679 ins->type = STACK_MP;
5680 ins->inst_left = *sp;
5681 ins->type = STACK_VTYPE;
5682 ins->klass = mono_defaults.typehandle_class;
5691 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
5696 g_error ("opcode 0x%02x not handled", *ip);
5699 if (start_new_bblock != 1)
5702 bblock->cil_length = ip - bblock->cil_code;
5703 bblock->next_bb = end_bblock;
5704 link_bblock (cfg, bblock, end_bblock);
5706 if (cfg->method == method && cfg->domainvar) {
5710 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
5711 call->signature = helper_sig_domain_get;
5712 call->inst.type = STACK_PTR;
5713 call->fptr = mono_domain_get;
5714 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, (MonoInst*)call);
5716 MONO_ADD_INS (init_localsbb, store);
5719 if (header->init_locals) {
5721 for (i = 0; i < header->num_locals; ++i) {
5722 int t = header->locals [i]->type;
5723 if (t == MONO_TYPE_VALUETYPE && header->locals [i]->data.klass->enumtype)
5724 t = header->locals [i]->data.klass->enum_basetype->type;
5725 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
5726 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
5727 NEW_ICONST (cfg, ins, 0);
5728 NEW_LOCSTORE (cfg, store, i, ins);
5729 MONO_ADD_INS (init_localsbb, store);
5730 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
5731 MONO_INST_NEW (cfg, ins, OP_I8CONST);
5732 ins->type = STACK_I8;
5734 NEW_LOCSTORE (cfg, store, i, ins);
5735 MONO_ADD_INS (init_localsbb, store);
5736 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
5737 MONO_INST_NEW (cfg, ins, OP_R8CONST);
5738 ins->type = STACK_R8;
5739 ins->inst_p0 = (void*)&r8_0;
5740 NEW_LOCSTORE (cfg, store, i, ins);
5741 MONO_ADD_INS (init_localsbb, store);
5742 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF)) {
5743 NEW_LOCLOADA (cfg, ins, i);
5744 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (header->locals [i]), NULL, NULL);
5747 NEW_PCONST (cfg, ins, NULL);
5748 NEW_LOCSTORE (cfg, store, i, ins);
5749 MONO_ADD_INS (init_localsbb, store);
5755 /* resolve backward branches in the middle of an existing basic block */
5756 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
5758 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
5759 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
5760 if (tblock != start_bblock) {
5762 split_bblock (cfg, tblock, bblock);
5763 l = bblock->cil_code - header->code;
5764 bblock->cil_length = tblock->cil_length - l;
5765 tblock->cil_length = l;
5767 g_print ("recheck failed.\n");
5772 * we compute regions here, because the length of filter clauses is not known in advance.
5773 * It is computed in the CEE_ENDFILTER case in the above switch statement
5775 if (cfg->method == method) {
5777 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5778 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
5780 mono_create_spvar_for_region (cfg, bb->region);
5781 if (cfg->verbose_level > 2)
5782 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
5785 g_hash_table_destroy (bbhash);
5788 dont_inline = g_list_remove (dont_inline, method);
5789 return inline_costs;
5792 if (cfg->method != method)
5793 g_hash_table_destroy (bbhash);
5794 dont_inline = g_list_remove (dont_inline, method);
5798 if (cfg->method != method)
5799 g_hash_table_destroy (bbhash);
5800 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
5801 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
5802 dont_inline = g_list_remove (dont_inline, method);
5807 mono_print_tree (MonoInst *tree) {
5813 arity = mono_burg_arity [tree->opcode];
5815 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
5817 switch (tree->opcode) {
5819 printf ("[%d]", tree->inst_c0);
5822 printf ("[%lld]", tree->inst_l);
5825 printf ("[%f]", *(double*)tree->inst_p0);
5828 printf ("[%f]", *(float*)tree->inst_p0);
5832 printf ("[%d]", tree->inst_c0);
5835 if (tree->inst_offset < 0)
5836 printf ("[-0x%x(%s)]", -tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5838 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5841 printf ("[%s]", mono_arch_regname (tree->dreg));
5844 printf ("[%s]", tree->inst_newa_class->name);
5845 mono_print_tree (tree->inst_newa_len);
5856 case OP_VOIDCALLVIRT: {
5857 MonoCallInst *call = (MonoCallInst*)tree;
5859 printf ("[%s]", call->method->name);
5864 printf ("[%d (", tree->inst_c0);
5865 for (i = 0; i < tree->inst_phi_args [0]; i++) {
5868 printf ("%d", tree->inst_phi_args [i + 1]);
5879 case OP_LOAD_MEMBASE:
5880 case OP_LOADI4_MEMBASE:
5881 case OP_LOADU4_MEMBASE:
5882 case OP_LOADU1_MEMBASE:
5883 case OP_LOADI1_MEMBASE:
5884 case OP_LOADU2_MEMBASE:
5885 case OP_LOADI2_MEMBASE:
5886 printf ("[%s] <- [%s + 0x%x]", mono_arch_regname (tree->dreg), mono_arch_regname (tree->inst_basereg), tree->inst_offset);
5889 printf ("[B%d]", tree->inst_target_bb->block_num);
5899 case OP_VOIDCALL_REG:
5900 mono_print_tree (tree->inst_left);
5912 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
5913 mono_print_tree (tree->inst_left);
5916 if (!mono_arch_print_tree(tree, arity)) {
5918 mono_print_tree (tree->inst_left);
5920 mono_print_tree (tree->inst_right);
5931 mono_print_tree_nl (MonoInst *tree)
5933 mono_print_tree (tree);
5938 create_helper_signature (void)
5940 /* FIXME: set call conv */
5941 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
5942 helper_sig_newarr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5943 helper_sig_newarr->params [0] = helper_sig_newarr->params [1] = &mono_defaults.int_class->byval_arg;
5944 helper_sig_newarr->ret = &mono_defaults.object_class->byval_arg;
5945 helper_sig_newarr->params [2] = &mono_defaults.int32_class->byval_arg;
5946 helper_sig_newarr->pinvoke = 1;
5948 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
5949 helper_sig_newarr_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5950 helper_sig_newarr_specific->params [0] = &mono_defaults.int_class->byval_arg;
5951 helper_sig_newarr_specific->params [1] = &mono_defaults.int32_class->byval_arg;
5952 helper_sig_newarr_specific->ret = &mono_defaults.object_class->byval_arg;
5953 helper_sig_newarr_specific->pinvoke = 1;
5955 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
5956 helper_sig_object_new = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5957 helper_sig_object_new->params [0] = helper_sig_object_new->params [1] = &mono_defaults.int_class->byval_arg;
5958 helper_sig_object_new->ret = &mono_defaults.object_class->byval_arg;
5959 helper_sig_object_new->pinvoke = 1;
5961 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
5962 helper_sig_object_new_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5963 helper_sig_object_new_specific->params [0] = &mono_defaults.int_class->byval_arg;
5964 helper_sig_object_new_specific->ret = &mono_defaults.object_class->byval_arg;
5965 helper_sig_object_new_specific->pinvoke = 1;
5967 /* void* mono_method_compile (MonoMethod*) */
5968 helper_sig_compile = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5969 helper_sig_compile->params [0] = helper_sig_compile->ret = &mono_defaults.int_class->byval_arg;
5970 helper_sig_compile->pinvoke = 1;
5972 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
5973 helper_sig_compile_virt = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5974 helper_sig_compile_virt->params [0] = &mono_defaults.object_class->byval_arg;
5975 helper_sig_compile_virt->params [1] = helper_sig_compile_virt->ret = &mono_defaults.int_class->byval_arg;
5976 helper_sig_compile_virt->pinvoke = 1;
5978 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
5979 helper_sig_ldstr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5980 helper_sig_ldstr->params [0] = helper_sig_ldstr->params [1] = &mono_defaults.int_class->byval_arg;
5981 helper_sig_ldstr->params [2] = &mono_defaults.int32_class->byval_arg;
5982 helper_sig_ldstr->ret = &mono_defaults.object_class->byval_arg;
5983 helper_sig_ldstr->pinvoke = 1;
5985 /* MonoDomain *mono_domain_get (void) */
5986 helper_sig_domain_get = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5987 helper_sig_domain_get->ret = &mono_defaults.int_class->byval_arg;
5988 helper_sig_domain_get->pinvoke = 1;
5990 /* void* stelem_ref (MonoArray *, int index, MonoObject *) */
5991 helper_sig_stelem_ref = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5992 helper_sig_stelem_ref->params [0] = &mono_defaults.array_class->byval_arg;
5993 helper_sig_stelem_ref->params [1] = &mono_defaults.int32_class->byval_arg;
5994 helper_sig_stelem_ref->params [2] = &mono_defaults.object_class->byval_arg;
5995 helper_sig_stelem_ref->ret = &mono_defaults.void_class->byval_arg;
5996 helper_sig_stelem_ref->pinvoke = 1;
5998 /* void* stelem_ref_check (MonoArray *, MonoObject *) */
5999 helper_sig_stelem_ref_check = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6000 helper_sig_stelem_ref_check->params [0] = &mono_defaults.array_class->byval_arg;
6001 helper_sig_stelem_ref_check->params [1] = &mono_defaults.object_class->byval_arg;
6002 helper_sig_stelem_ref_check->ret = &mono_defaults.void_class->byval_arg;
6003 helper_sig_stelem_ref_check->pinvoke = 1;
6005 /* long amethod (long, long) */
6006 helper_sig_long_long_long = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6007 helper_sig_long_long_long->params [0] = helper_sig_long_long_long->params [1] =
6008 &mono_defaults.int64_class->byval_arg;
6009 helper_sig_long_long_long->ret = &mono_defaults.int64_class->byval_arg;
6010 helper_sig_long_long_long->pinvoke = 1;
6012 /* object amethod (intptr) */
6013 helper_sig_obj_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6014 helper_sig_obj_ptr->params [0] = &mono_defaults.int_class->byval_arg;
6015 helper_sig_obj_ptr->ret = &mono_defaults.object_class->byval_arg;
6016 helper_sig_obj_ptr->pinvoke = 1;
6018 /* void amethod (intptr) */
6019 helper_sig_void_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6020 helper_sig_void_ptr->params [0] = &mono_defaults.int_class->byval_arg;
6021 helper_sig_void_ptr->ret = &mono_defaults.void_class->byval_arg;
6022 helper_sig_void_ptr->pinvoke = 1;
6024 /* void amethod (MonoObject *obj) */
6025 helper_sig_void_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6026 helper_sig_void_obj->params [0] = &mono_defaults.object_class->byval_arg;
6027 helper_sig_void_obj->ret = &mono_defaults.void_class->byval_arg;
6028 helper_sig_void_obj->pinvoke = 1;
6030 /* intptr amethod (void) */
6031 helper_sig_ptr_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
6032 helper_sig_ptr_void->ret = &mono_defaults.int_class->byval_arg;
6033 helper_sig_ptr_void->pinvoke = 1;
6035 /* object amethod (void) */
6036 helper_sig_obj_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
6037 helper_sig_obj_void->ret = &mono_defaults.object_class->byval_arg;
6038 helper_sig_obj_void->pinvoke = 1;
6040 /* void amethod (intptr, intptr) */
6041 helper_sig_void_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6042 helper_sig_void_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
6043 helper_sig_void_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
6044 helper_sig_void_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
6045 helper_sig_void_ptr_ptr->pinvoke = 1;
6047 /* void amethod (intptr, intptr, intptr) */
6048 helper_sig_void_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
6049 helper_sig_void_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
6050 helper_sig_void_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
6051 helper_sig_void_ptr_ptr_ptr->params [2] = &mono_defaults.int_class->byval_arg;
6052 helper_sig_void_ptr_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
6053 helper_sig_void_ptr_ptr_ptr->pinvoke = 1;
6055 /* intptr amethod (intptr, intptr) */
6056 helper_sig_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6057 helper_sig_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
6058 helper_sig_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
6059 helper_sig_ptr_ptr_ptr->ret = &mono_defaults.int_class->byval_arg;
6060 helper_sig_ptr_ptr_ptr->pinvoke = 1;
6062 /* IntPtr amethod (object) */
6063 helper_sig_ptr_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6064 helper_sig_ptr_obj->params [0] = &mono_defaults.object_class->byval_arg;
6065 helper_sig_ptr_obj->ret = &mono_defaults.int_class->byval_arg;
6066 helper_sig_ptr_obj->pinvoke = 1;
6068 /* IntPtr amethod (int) */
6069 helper_sig_ptr_int = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6070 helper_sig_ptr_int->params [0] = &mono_defaults.int32_class->byval_arg;
6071 helper_sig_ptr_int->ret = &mono_defaults.int_class->byval_arg;
6072 helper_sig_ptr_int->pinvoke = 1;
6074 /* long amethod (long, guint32) */
6075 helper_sig_long_long_int = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6076 helper_sig_long_long_int->params [0] = &mono_defaults.int64_class->byval_arg;
6077 helper_sig_long_long_int->params [1] = &mono_defaults.int32_class->byval_arg;
6078 helper_sig_long_long_int->ret = &mono_defaults.int64_class->byval_arg;
6079 helper_sig_long_long_int->pinvoke = 1;
6081 /* ulong amethod (double) */
6082 helper_sig_ulong_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6083 helper_sig_ulong_double->params [0] = &mono_defaults.double_class->byval_arg;
6084 helper_sig_ulong_double->ret = &mono_defaults.uint64_class->byval_arg;
6085 helper_sig_ulong_double->pinvoke = 1;
6087 /* long amethod (double) */
6088 helper_sig_long_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6089 helper_sig_long_double->params [0] = &mono_defaults.double_class->byval_arg;
6090 helper_sig_long_double->ret = &mono_defaults.int64_class->byval_arg;
6091 helper_sig_long_double->pinvoke = 1;
6093 /* double amethod (long) */
6094 helper_sig_double_long = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6095 helper_sig_double_long->params [0] = &mono_defaults.int64_class->byval_arg;
6096 helper_sig_double_long->ret = &mono_defaults.double_class->byval_arg;
6097 helper_sig_double_long->pinvoke = 1;
6099 /* double amethod (int) */
6100 helper_sig_double_int = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6101 helper_sig_double_int->params [0] = &mono_defaults.int32_class->byval_arg;
6102 helper_sig_double_int->ret = &mono_defaults.double_class->byval_arg;
6103 helper_sig_double_int->pinvoke = 1;
6105 /* float amethod (long) */
6106 helper_sig_float_long = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6107 helper_sig_float_long->params [0] = &mono_defaults.int64_class->byval_arg;
6108 helper_sig_float_long->ret = &mono_defaults.single_class->byval_arg;
6109 helper_sig_float_long->pinvoke = 1;
6111 /* double amethod (double, double) */
6112 helper_sig_double_double_double = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6113 helper_sig_double_double_double->params [0] = &mono_defaults.double_class->byval_arg;
6114 helper_sig_double_double_double->params [1] = &mono_defaults.double_class->byval_arg;
6115 helper_sig_double_double_double->ret = &mono_defaults.double_class->byval_arg;
6116 helper_sig_double_double_double->pinvoke = 1;
6118 /* uint amethod (double) */
6119 helper_sig_uint_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6120 helper_sig_uint_double->params [0] = &mono_defaults.double_class->byval_arg;
6121 helper_sig_uint_double->ret = &mono_defaults.uint32_class->byval_arg;
6122 helper_sig_uint_double->pinvoke = 1;
6124 /* int amethod (double) */
6125 helper_sig_int_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6126 helper_sig_int_double->params [0] = &mono_defaults.double_class->byval_arg;
6127 helper_sig_int_double->ret = &mono_defaults.int32_class->byval_arg;
6128 helper_sig_int_double->pinvoke = 1;
6130 /* void initobj (intptr, int size) */
6131 helper_sig_initobj = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6132 helper_sig_initobj->params [0] = &mono_defaults.int_class->byval_arg;
6133 helper_sig_initobj->params [1] = &mono_defaults.int32_class->byval_arg;
6134 helper_sig_initobj->ret = &mono_defaults.void_class->byval_arg;
6135 helper_sig_initobj->pinvoke = 1;
6137 /* void memcpy (intptr, intptr, int size) */
6138 helper_sig_memcpy = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
6139 helper_sig_memcpy->params [0] = &mono_defaults.int_class->byval_arg;
6140 helper_sig_memcpy->params [1] = &mono_defaults.int_class->byval_arg;
6141 helper_sig_memcpy->params [2] = &mono_defaults.int32_class->byval_arg;
6142 helper_sig_memcpy->ret = &mono_defaults.void_class->byval_arg;
6143 helper_sig_memcpy->pinvoke = 1;
6145 /* void memset (intptr, int val, int size) */
6146 helper_sig_memset = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
6147 helper_sig_memset->params [0] = &mono_defaults.int_class->byval_arg;
6148 helper_sig_memset->params [1] = &mono_defaults.int32_class->byval_arg;
6149 helper_sig_memset->params [2] = &mono_defaults.int32_class->byval_arg;
6150 helper_sig_memset->ret = &mono_defaults.void_class->byval_arg;
6151 helper_sig_memset->pinvoke = 1;
6153 helper_sig_class_init_trampoline = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
6154 helper_sig_class_init_trampoline->ret = &mono_defaults.void_class->byval_arg;
6155 helper_sig_class_init_trampoline->pinvoke = 1;
6158 static GHashTable *jit_icall_hash_name = NULL;
6159 static GHashTable *jit_icall_hash_addr = NULL;
6162 mono_find_jit_icall_by_name (const char *name)
6164 MonoJitICallInfo *info;
6165 g_assert (jit_icall_hash_name);
6167 //printf ("lookup addr %s %p\n", name, g_hash_table_lookup (jit_icall_hash_name, name));
6168 EnterCriticalSection (&trampoline_hash_mutex);
6169 info = g_hash_table_lookup (jit_icall_hash_name, name);
6170 LeaveCriticalSection (&trampoline_hash_mutex);
6175 mono_find_jit_icall_by_addr (gconstpointer addr)
6177 MonoJitICallInfo *info;
6178 g_assert (jit_icall_hash_addr);
6180 EnterCriticalSection (&trampoline_hash_mutex);
6181 info = g_hash_table_lookup (jit_icall_hash_addr, (gpointer)addr);
6182 LeaveCriticalSection (&trampoline_hash_mutex);
6188 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
6191 MonoMethod *wrapper;
6194 if (callinfo->wrapper)
6195 return callinfo->wrapper;
6197 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
6198 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
6199 /* Must be domain neutral since there is only one copy */
6200 code = mono_jit_compile_method_with_opt (wrapper, default_opt | MONO_OPT_SHARED);
6202 EnterCriticalSection (&trampoline_hash_mutex);
6203 if (!callinfo->wrapper) {
6204 callinfo->wrapper = code;
6205 g_hash_table_insert (jit_icall_hash_addr, (gpointer)callinfo->wrapper, callinfo);
6207 LeaveCriticalSection (&trampoline_hash_mutex);
6210 return callinfo->wrapper;
6214 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save)
6216 MonoJitICallInfo *info;
6221 EnterCriticalSection (&trampoline_hash_mutex);
6223 if (!jit_icall_hash_name) {
6224 jit_icall_hash_name = g_hash_table_new (g_str_hash, g_str_equal);
6225 jit_icall_hash_addr = g_hash_table_new (NULL, NULL);
6228 if (g_hash_table_lookup (jit_icall_hash_name, name)) {
6229 g_warning ("jit icall already defined \"%s\"\n", name);
6230 g_assert_not_reached ();
6233 info = g_new (MonoJitICallInfo, 1);
6240 #ifdef MONO_USE_EXC_TABLES
6241 || mono_arch_has_unwind_info (func)
6244 info->wrapper = func;
6246 info->wrapper = NULL;
6249 g_hash_table_insert (jit_icall_hash_name, (gpointer)info->name, info);
6250 g_hash_table_insert (jit_icall_hash_addr, (gpointer)func, info);
6252 LeaveCriticalSection (&trampoline_hash_mutex);
6257 mono_create_class_init_trampoline (MonoVTable *vtable)
6261 /* previously created trampoline code */
6262 mono_domain_lock (vtable->domain);
6264 mono_g_hash_table_lookup (vtable->domain->class_init_trampoline_hash,
6266 mono_domain_unlock (vtable->domain);
6270 code = mono_arch_create_class_init_trampoline (vtable);
6272 /* store trampoline address */
6273 mono_domain_lock (vtable->domain);
6274 mono_g_hash_table_insert (vtable->domain->class_init_trampoline_hash,
6276 mono_domain_unlock (vtable->domain);
6278 EnterCriticalSection (&trampoline_hash_mutex);
6279 if (!class_init_hash_addr)
6280 class_init_hash_addr = g_hash_table_new (NULL, NULL);
6281 g_hash_table_insert (class_init_hash_addr, code, vtable);
6282 LeaveCriticalSection (&trampoline_hash_mutex);
6288 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method,
6289 gboolean add_sync_wrapper)
6294 if (add_sync_wrapper && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
6295 return mono_create_jump_trampoline (domain, mono_marshal_get_synchronized_wrapper (method), FALSE);
6297 code = mono_jit_find_compiled_method (domain, method);
6301 EnterCriticalSection (&trampoline_hash_mutex);
6303 if (jump_trampoline_hash) {
6304 code = g_hash_table_lookup (jump_trampoline_hash, method);
6306 LeaveCriticalSection (&trampoline_hash_mutex);
6311 ji = mono_arch_create_jump_trampoline (method);
6314 * mono_delegate_ctor needs to find the method metadata from the
6315 * trampoline address, so we save it here.
6318 mono_jit_info_table_add (mono_root_domain, ji);
6320 if (!jump_trampoline_hash)
6321 jump_trampoline_hash = g_hash_table_new (NULL, NULL);
6322 g_hash_table_insert (jump_trampoline_hash, method, ji->code_start);
6324 LeaveCriticalSection (&trampoline_hash_mutex);
6326 return ji->code_start;
6330 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
6334 EnterCriticalSection (&trampoline_hash_mutex);
6335 if (class_init_hash_addr)
6336 res = g_hash_table_lookup (class_init_hash_addr, addr);
6339 LeaveCriticalSection (&trampoline_hash_mutex);
6344 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func, gboolean no_throw)
6346 MonoJitICallInfo *info;
6348 if (!emul_opcode_map)
6349 emul_opcode_map = g_new0 (MonoJitICallInfo*, OP_LAST + 1);
6351 g_assert (!sig->hasthis);
6352 g_assert (sig->param_count < 3);
6354 info = mono_register_jit_icall (func, name, sig, no_throw);
6356 emul_opcode_map [opcode] = info;
6360 decompose_foreach (MonoInst *tree, gpointer data)
6362 static MonoJitICallInfo *newarr_info = NULL;
6363 static MonoJitICallInfo *newarr_specific_info = NULL;
6364 MonoJitICallInfo *info;
6367 switch (tree->opcode) {
6369 MonoCompile *cfg = data;
6370 MonoInst *iargs [3];
6373 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
6374 g_assert (newarr_info);
6375 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
6376 g_assert (newarr_specific_info);
6379 if (cfg->opt & MONO_OPT_SHARED) {
6380 NEW_DOMAINCONST (cfg, iargs [0]);
6381 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
6382 iargs [2] = tree->inst_newa_len;
6387 MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
6389 NEW_VTABLECONST (cfg, iargs [0], vtable);
6390 iargs [1] = tree->inst_newa_len;
6392 info = newarr_specific_info;
6395 mono_emulate_opcode (cfg, tree, iargs, info);
6397 /* Need to decompose arguments after the the opcode is decomposed */
6398 for (i = 0; i < info->sig->param_count; ++i)
6399 dec_foreach (iargs [i], cfg);
6409 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
6411 switch (mono_burg_arity [tree->opcode]) {
6414 mono_inst_foreach (tree->inst_left, func, data);
6417 mono_inst_foreach (tree->inst_left, func, data);
6418 mono_inst_foreach (tree->inst_right, func, data);
6421 g_assert_not_reached ();
6428 mono_print_bb_code (MonoBasicBlock *bb) {
6430 MonoInst *c = bb->code;
6432 mono_print_tree (c);
6440 print_dfn (MonoCompile *cfg) {
6445 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
6447 for (i = 0; i < cfg->num_bblocks; ++i) {
6448 bb = cfg->bblocks [i];
6450 char* code1, *code2;
6451 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
6452 if (bb->last_ins->cil_code)
6453 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
6455 code2 = g_strdup ("");
6457 code1 [strlen (code1) - 1] = 0;
6458 code = g_strdup_printf ("%s -> %s", code1, code2);
6462 code = g_strdup ("\n");
6463 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
6465 MonoInst *c = bb->code;
6467 mono_print_tree (c);
6475 g_print ("\tprev:");
6476 for (j = 0; j < bb->in_count; ++j) {
6477 g_print (" BB%d", bb->in_bb [j]->block_num);
6479 g_print ("\t\tsucc:");
6480 for (j = 0; j < bb->out_count; ++j) {
6481 g_print (" BB%d", bb->out_bb [j]->block_num);
6483 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
6486 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
6489 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
6491 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
6499 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
6503 g_assert (bb->code);
6504 bb->last_ins->next = inst;
6505 bb->last_ins = inst;
6507 bb->last_ins = bb->code = inst;
6512 mono_destroy_compile (MonoCompile *cfg)
6514 //mono_mempool_stats (cfg->mempool);
6515 g_hash_table_destroy (cfg->bb_hash);
6517 mono_regstate_free (cfg->rs);
6519 g_hash_table_destroy (cfg->spvars);
6520 mono_mempool_destroy (cfg->mempool);
6521 g_list_free (cfg->ldstr_list);
6523 g_free (cfg->varinfo);
6529 mono_get_lmf_addr (void)
6531 MonoJitTlsData *jit_tls;
6533 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
6534 return &jit_tls->lmf;
6536 g_assert_not_reached ();
6541 * mono_thread_abort:
6542 * @obj: exception object
6544 * abort the thread, print exception information and stack trace
6547 mono_thread_abort (MonoObject *obj)
6549 /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
6551 /* handle_remove should be eventually called for this thread, too
6558 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
6560 MonoJitTlsData *jit_tls;
6563 jit_tls = TlsGetValue (mono_jit_tls_id);
6567 jit_tls = g_new0 (MonoJitTlsData, 1);
6569 TlsSetValue (mono_jit_tls_id, jit_tls);
6571 jit_tls->abort_func = abort_func;
6572 jit_tls->end_of_stack = stack_start;
6574 lmf = g_new0 (MonoLMF, 1);
6577 jit_tls->lmf = jit_tls->first_lmf = lmf;
6579 mono_arch_setup_jit_tls_data (jit_tls);
6585 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
6588 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
6589 thread = mono_thread_current ();
6591 thread->jit_data = jit_tls;
6594 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
6597 mono_thread_abort_dummy (MonoObject *obj)
6599 if (mono_thread_attach_aborted_cb)
6600 mono_thread_attach_aborted_cb (obj);
6602 mono_thread_abort (obj);
6606 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
6609 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
6610 thread = mono_thread_current ();
6612 thread->jit_data = jit_tls;
6616 mini_thread_cleanup (MonoThread *thread)
6618 MonoJitTlsData *jit_tls = thread->jit_data;
6621 mono_arch_free_jit_tls_data (jit_tls);
6622 g_free (jit_tls->first_lmf);
6624 thread->jit_data = NULL;
6629 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
6631 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
6635 ji->data.target = target;
6636 ji->next = cfg->patch_info;
6638 cfg->patch_info = ji;
6642 mono_remove_patch_info (MonoCompile *cfg, int ip)
6644 MonoJumpInfo **ji = &cfg->patch_info;
6647 if ((*ji)->ip.i == ip)
6650 ji = &((*ji)->next);
6655 mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors)
6657 unsigned char *ip = patch_info->ip.i + code;
6658 gconstpointer target = NULL;
6660 switch (patch_info->type) {
6661 case MONO_PATCH_INFO_BB:
6662 target = patch_info->data.bb->native_offset + code;
6664 case MONO_PATCH_INFO_ABS:
6665 target = patch_info->data.target;
6667 case MONO_PATCH_INFO_LABEL:
6668 target = patch_info->data.inst->inst_c0 + code;
6670 case MONO_PATCH_INFO_IP:
6673 case MONO_PATCH_INFO_METHOD_REL:
6674 target = code + patch_info->data.offset;
6676 case MONO_PATCH_INFO_INTERNAL_METHOD: {
6677 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
6679 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
6680 g_assert_not_reached ();
6682 target = mono_icall_get_wrapper (mi);
6685 case MONO_PATCH_INFO_METHOD_JUMP: {
6688 /* get the trampoline to the method from the domain */
6689 target = mono_create_jump_trampoline (domain, patch_info->data.method, TRUE);
6690 if (!domain->jump_target_hash)
6691 domain->jump_target_hash = g_hash_table_new (NULL, NULL);
6692 list = g_hash_table_lookup (domain->jump_target_hash, patch_info->data.method);
6693 list = g_slist_prepend (list, ip);
6694 g_hash_table_insert (domain->jump_target_hash, patch_info->data.method, list);
6697 case MONO_PATCH_INFO_METHOD:
6698 if (patch_info->data.method == method) {
6701 /* get the trampoline to the method from the domain */
6702 target = mono_arch_create_jit_trampoline (patch_info->data.method);
6704 case MONO_PATCH_INFO_SWITCH: {
6705 gpointer *jump_table;
6708 mono_domain_lock (domain);
6709 jump_table = mono_code_manager_reserve (domain->code_mp, sizeof (gpointer) * patch_info->table_size);
6710 mono_domain_unlock (domain);
6712 for (i = 0; i < patch_info->table_size; i++) {
6713 jump_table [i] = code + (int)patch_info->data.table [i];
6715 target = jump_table;
6718 case MONO_PATCH_INFO_METHODCONST:
6719 case MONO_PATCH_INFO_CLASS:
6720 case MONO_PATCH_INFO_IMAGE:
6721 case MONO_PATCH_INFO_FIELD:
6722 target = patch_info->data.target;
6724 case MONO_PATCH_INFO_IID:
6725 mono_class_init (patch_info->data.klass);
6726 target = (gpointer)patch_info->data.klass->interface_id;
6728 case MONO_PATCH_INFO_VTABLE:
6729 target = mono_class_vtable (domain, patch_info->data.klass);
6731 case MONO_PATCH_INFO_CLASS_INIT:
6732 target = mono_create_class_init_trampoline (mono_class_vtable (domain, patch_info->data.klass));
6734 case MONO_PATCH_INFO_SFLDA: {
6735 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
6736 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (vtable->klass, method))
6737 /* Done by the generated code */
6741 mono_runtime_class_init (vtable);
6743 target = (char*)vtable->data + patch_info->data.field->offset;
6746 case MONO_PATCH_INFO_R4:
6747 case MONO_PATCH_INFO_R8:
6748 target = patch_info->data.target;
6750 case MONO_PATCH_INFO_EXC_NAME:
6751 target = patch_info->data.name;
6753 case MONO_PATCH_INFO_LDSTR:
6755 mono_ldstr (domain, patch_info->data.token->image,
6756 mono_metadata_token_index (patch_info->data.token->token));
6758 case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
6760 MonoClass *handle_class;
6762 handle = mono_ldtoken (patch_info->data.token->image,
6763 patch_info->data.token->token, &handle_class, NULL);
6764 mono_class_init (handle_class);
6765 mono_class_init (mono_class_from_mono_type (handle));
6768 mono_type_get_object (domain, handle);
6771 case MONO_PATCH_INFO_LDTOKEN: {
6773 MonoClass *handle_class;
6775 handle = mono_ldtoken (patch_info->data.token->image,
6776 patch_info->data.token->token, &handle_class, NULL);
6777 mono_class_init (handle_class);
6783 g_assert_not_reached ();
6786 return (gpointer)target;
6790 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
6791 MonoJitICallInfo *info;
6793 decompose_foreach (tree, cfg);
6795 switch (mono_burg_arity [tree->opcode]) {
6798 dec_foreach (tree->inst_left, cfg);
6800 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6801 MonoInst *iargs [2];
6803 iargs [0] = tree->inst_left;
6805 mono_emulate_opcode (cfg, tree, iargs, info);
6811 #ifdef MONO_ARCH_BIGMUL_INTRINS
6812 if (tree->opcode == OP_LMUL
6813 && (cfg->opt & MONO_OPT_INTRINS)
6814 && (tree->inst_left->opcode == CEE_CONV_I8
6815 || tree->inst_left->opcode == CEE_CONV_U8)
6816 && tree->inst_left->inst_left->type == STACK_I4
6817 && (tree->inst_right->opcode == CEE_CONV_I8
6818 || tree->inst_right->opcode == CEE_CONV_U8)
6819 && tree->inst_right->inst_left->type == STACK_I4) {
6820 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
6821 tree->inst_left = tree->inst_left->inst_left;
6822 tree->inst_right = tree->inst_right->inst_left;
6823 dec_foreach (tree, cfg);
6826 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6827 MonoInst *iargs [2];
6829 iargs [0] = tree->inst_i0;
6830 iargs [1] = tree->inst_i1;
6832 mono_emulate_opcode (cfg, tree, iargs, info);
6834 dec_foreach (iargs [0], cfg);
6835 dec_foreach (iargs [1], cfg);
6838 dec_foreach (tree->inst_left, cfg);
6839 dec_foreach (tree->inst_right, cfg);
6843 g_assert_not_reached ();
6848 decompose_pass (MonoCompile *cfg) {
6851 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6854 cfg->prev_ins = NULL;
6855 for (tree = cfg->cbb->code; tree; tree = tree->next) {
6856 dec_foreach (tree, cfg);
6857 cfg->prev_ins = tree;
6863 nullify_basic_block (MonoBasicBlock *bb)
6870 bb->code = bb->last_ins = NULL;
6871 bb->cil_code = NULL;
6875 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6879 for (i = 0; i < bb->out_count; i++) {
6880 MonoBasicBlock *ob = bb->out_bb [i];
6883 if (bb->out_count > 1) {
6884 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
6888 bb->out_bb [i] = repl;
6895 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6899 for (i = 0; i < bb->in_count; i++) {
6900 MonoBasicBlock *ib = bb->in_bb [i];
6903 if (bb->in_count > 1) {
6904 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
6908 bb->in_bb [i] = repl;
6915 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6919 for (i = 0; i < bb->out_count; i++) {
6920 MonoBasicBlock *ob = bb->out_bb [i];
6921 for (j = 0; j < ob->in_count; j++) {
6922 if (ob->in_bb [j] == orig) {
6923 ob->in_bb [j] = repl;
6932 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
6934 bb->out_count = bbn->out_count;
6935 bb->out_bb = bbn->out_bb;
6937 replace_basic_block (bb, bbn, bb);
6941 bb->last_ins->next = bbn->code;
6942 bb->last_ins = bbn->last_ins;
6945 bb->code = bbn->code;
6946 bb->last_ins = bbn->last_ins;
6948 bb->next_bb = bbn->next_bb;
6949 nullify_basic_block (bbn);
6953 * Optimizes the branches on the Control Flow Graph
6957 optimize_branches (MonoCompile *cfg)
6959 int i, changed = FALSE;
6960 MonoBasicBlock *bb, *bbn;
6961 guint32 niterations;
6964 * Some crazy loops could cause the code below to go into an infinite
6965 * loop, see bug #53003 for an example. To prevent this, we put an upper
6966 * bound on the number of iterations.
6973 /* we skip the entry block (exit is handled specially instead ) */
6974 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
6976 /* dont touch code inside exception clauses */
6977 if (bb->region != -1)
6980 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
6981 if (cfg->verbose_level > 2)
6982 g_print ("nullify block triggered %d\n", bbn->block_num);
6984 bb->next_bb = bbn->next_bb;
6986 for (i = 0; i < bbn->out_count; i++)
6987 replace_in_block (bbn->out_bb [i], bbn, NULL);
6989 nullify_basic_block (bbn);
6993 if (bb->out_count == 1) {
6994 bbn = bb->out_bb [0];
6996 /* conditional branches where true and false targets are the same can be also replaced with CEE_BR */
6997 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
6998 bb->last_ins->opcode = CEE_BR;
6999 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
7001 if (cfg->verbose_level > 2)
7002 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
7005 if (bb->region == bbn->region && bb->next_bb == bbn) {
7006 /* the block are in sequence anyway ... */
7008 /* branches to the following block can be removed */
7009 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
7010 bb->last_ins->opcode = CEE_NOP;
7012 if (cfg->verbose_level > 2)
7013 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
7016 if (bbn->in_count == 1) {
7018 if (bbn != cfg->bb_exit) {
7019 if (cfg->verbose_level > 2)
7020 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
7021 merge_basic_blocks (bb, bbn);
7025 //mono_print_bb_code (bb);
7030 } while (changed && (niterations > 0));
7037 /* we skip the entry block (exit is handled specially instead ) */
7038 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
7040 /* dont touch code inside exception clauses */
7041 if (bb->region != -1)
7044 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
7045 if (cfg->verbose_level > 2) {
7046 g_print ("nullify block triggered %d\n", bbn->block_num);
7048 bb->next_bb = bbn->next_bb;
7050 for (i = 0; i < bbn->out_count; i++)
7051 replace_in_block (bbn->out_bb [i], bbn, NULL);
7053 nullify_basic_block (bbn);
7059 if (bb->out_count == 1) {
7060 bbn = bb->out_bb [0];
7062 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
7063 bbn = bb->last_ins->inst_target_bb;
7064 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7065 bbn->code->inst_target_bb->region == bb->region) {
7067 if (cfg->verbose_level > 2)
7068 g_print ("in %s branch to branch triggered %d -> %d\n", cfg->method->name,
7069 bb->block_num, bbn->block_num);
7071 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
7072 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
7077 } else if (bb->out_count == 2) {
7078 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
7079 bbn = bb->last_ins->inst_true_bb;
7080 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7081 bbn->code->inst_target_bb->region == bb->region) {
7082 if (cfg->verbose_level > 2)
7083 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
7084 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
7087 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
7089 replace_in_block (bbn, bb, NULL);
7091 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
7092 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7094 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7100 bbn = bb->last_ins->inst_false_bb;
7101 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7102 bbn->code->inst_target_bb->region == bb->region) {
7103 if (cfg->verbose_level > 2)
7104 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
7105 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
7108 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
7110 replace_in_block (bbn, bb, NULL);
7112 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
7113 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7115 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7123 } while (changed && (niterations > 0));
7128 mono_compile_create_vars (MonoCompile *cfg)
7130 MonoMethodSignature *sig;
7131 MonoMethodHeader *header;
7134 header = ((MonoMethodNormal *)cfg->method)->header;
7136 sig = cfg->method->signature;
7138 if (!MONO_TYPE_IS_VOID (sig->ret)) {
7139 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
7140 cfg->ret->opcode = OP_RETARG;
7141 cfg->ret->inst_vtype = sig->ret;
7142 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
7144 if (cfg->verbose_level > 2)
7145 g_print ("creating vars\n");
7148 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
7150 for (i = 0; i < sig->param_count; ++i)
7151 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
7153 cfg->locals_start = cfg->num_varinfo;
7155 if (cfg->verbose_level > 2)
7156 g_print ("creating locals\n");
7157 for (i = 0; i < header->num_locals; ++i)
7158 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
7159 if (cfg->verbose_level > 2)
7160 g_print ("locals done\n");
7164 mono_print_code (MonoCompile *cfg)
7168 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7169 MonoInst *tree = bb->code;
7174 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
7176 for (; tree; tree = tree->next) {
7177 mono_print_tree (tree);
7182 bb->last_ins->next = NULL;
7186 extern const char * const mono_burg_rule_string [];
7189 emit_state (MonoCompile *cfg, MBState *state, int goal)
7192 int ern = mono_burg_rule (state, goal);
7193 const guint16 *nts = mono_burg_nts [ern];
7196 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
7200 // state->reg1 = state->reg2; /* chain rule */
7202 state->reg1 = mono_regstate_next_int (cfg->rs);
7203 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
7206 state->reg1 = mono_regstate_next_int (cfg->rs);
7207 state->reg2 = mono_regstate_next_int (cfg->rs);
7210 state->reg1 = mono_regstate_next_float (cfg->rs);
7217 mono_burg_kids (state, ern, kids);
7219 emit_state (cfg, kids [0], nts [0]);
7221 emit_state (cfg, kids [1], nts [1]);
7223 g_assert (!nts [3]);
7224 emit_state (cfg, kids [2], nts [2]);
7229 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
7230 if ((emit = mono_burg_func [ern]))
7231 emit (state, state->tree, cfg);
7234 #define DEBUG_SELECTION
7237 mini_select_instructions (MonoCompile *cfg)
7239 static const int reverse_map [] = {
7240 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
7241 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
7243 static const int reverse_fmap [] = {
7244 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
7245 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
7247 static const int reverse_lmap [] = {
7248 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
7249 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
7254 cfg->state_pool = mono_mempool_new ();
7255 cfg->rs = mono_regstate_new ();
7257 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7258 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode) &&
7259 bb->next_bb != bb->last_ins->inst_false_bb) {
7261 if (bb->next_bb == bb->last_ins->inst_true_bb) {
7262 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
7263 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
7264 bb->last_ins->inst_false_bb = tmp;
7266 if (bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
7267 bb->last_ins->opcode = reverse_map [bb->last_ins->opcode - CEE_BEQ];
7268 } else if (bb->last_ins->opcode >= OP_FBEQ && bb->last_ins->opcode <= OP_FBLT_UN) {
7269 bb->last_ins->opcode = reverse_fmap [bb->last_ins->opcode - OP_FBEQ];
7270 } else if (bb->last_ins->opcode >= OP_LBEQ && bb->last_ins->opcode <= OP_LBLT_UN) {
7271 bb->last_ins->opcode = reverse_lmap [bb->last_ins->opcode - OP_LBEQ];
7274 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
7275 inst->opcode = CEE_BR;
7276 inst->inst_target_bb = bb->last_ins->inst_false_bb;
7277 mono_bblock_add_inst (bb, inst);
7282 #ifdef DEBUG_SELECTION
7283 if (cfg->verbose_level >= 4) {
7284 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7285 MonoInst *tree = bb->code;
7286 g_print ("DUMP BLOCK %d:\n", bb->block_num);
7289 for (; tree; tree = tree->next) {
7290 mono_print_tree (tree);
7297 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7298 MonoInst *tree = bb->code, *next;
7304 bb->last_ins = NULL;
7307 mono_regstate_reset (cfg->rs);
7309 #ifdef DEBUG_SELECTION
7310 if (cfg->verbose_level >= 3)
7311 g_print ("LABEL BLOCK %d:\n", bb->block_num);
7313 for (; tree; tree = next) {
7315 #ifdef DEBUG_SELECTION
7316 if (cfg->verbose_level >= 3) {
7317 mono_print_tree (tree);
7322 if (!(mbstate = mono_burg_label (tree, cfg))) {
7323 g_warning ("unable to label tree %p", tree);
7324 mono_print_tree (tree);
7326 g_assert_not_reached ();
7328 emit_state (cfg, mbstate, MB_NTERM_stmt);
7330 bb->max_ireg = cfg->rs->next_vireg;
7331 bb->max_freg = cfg->rs->next_vfreg;
7334 bb->last_ins->next = NULL;
7336 mono_mempool_empty (cfg->state_pool);
7338 mono_mempool_destroy (cfg->state_pool);
7342 mono_codegen (MonoCompile *cfg)
7344 MonoJumpInfo *patch_info;
7346 int i, max_epilog_size;
7349 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7350 cfg->spill_count = 0;
7351 /* we reuse dfn here */
7352 /* bb->dfn = bb_count++; */
7353 mono_arch_local_regalloc (cfg, bb);
7356 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
7357 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
7359 code = mono_arch_emit_prolog (cfg);
7361 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7362 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
7364 cfg->code_len = code - cfg->native_code;
7365 cfg->prolog_end = cfg->code_len;
7367 mono_debug_open_method (cfg);
7369 /* emit code all basic blocks */
7370 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7371 bb->native_offset = cfg->code_len;
7372 mono_arch_output_basic_block (cfg, bb);
7374 cfg->bb_exit->native_offset = cfg->code_len;
7376 code = cfg->native_code + cfg->code_len;
7378 max_epilog_size = mono_arch_max_epilog_size (cfg);
7380 /* we always allocate code in cfg->domain->code_mp to increase locality */
7381 cfg->code_size = cfg->code_len + max_epilog_size;
7382 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
7384 mono_domain_lock (cfg->domain);
7385 code = mono_code_manager_reserve (cfg->domain->code_mp, cfg->code_size);
7386 mono_domain_unlock (cfg->domain);
7388 memcpy (code, cfg->native_code, cfg->code_len);
7389 g_free (cfg->native_code);
7390 cfg->native_code = code;
7391 code = cfg->native_code + cfg->code_len;
7393 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
7395 cfg->epilog_begin = cfg->code_len;
7397 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7398 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
7400 cfg->code_len = code - cfg->native_code;
7402 mono_arch_emit_epilog (cfg);
7404 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7405 switch (patch_info->type) {
7406 case MONO_PATCH_INFO_ABS: {
7407 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
7409 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
7410 if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) &&
7411 strstr (cfg->method->name, info->name))
7413 * This is an icall wrapper, and this is a call to the
7418 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
7419 patch_info->data.name = info->name;
7423 MonoVTable *vtable = mono_find_class_init_trampoline_by_addr (patch_info->data.target);
7425 patch_info->type = MONO_PATCH_INFO_CLASS_INIT;
7426 patch_info->data.klass = vtable->klass;
7431 case MONO_PATCH_INFO_SWITCH: {
7433 mono_domain_lock (cfg->domain);
7434 table = mono_code_manager_reserve (cfg->domain->code_mp, sizeof (gpointer) * patch_info->table_size);
7435 mono_domain_unlock (cfg->domain);
7437 patch_info->ip.i = patch_info->ip.label->inst_c0;
7438 for (i = 0; i < patch_info->table_size; i++) {
7439 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
7441 patch_info->data.target = table;
7450 if (cfg->verbose_level > 0)
7451 g_print ("Method %s emitted at %p to %p [%s]\n",
7452 mono_method_full_name (cfg->method, TRUE),
7453 cfg->native_code, cfg->native_code + cfg->code_len, cfg->domain->friendly_name);
7455 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info, cfg->run_cctors);
7457 mono_domain_lock (cfg->domain);
7458 mono_code_manager_commit (cfg->domain->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
7459 mono_domain_unlock (cfg->domain);
7461 mono_arch_flush_icache (cfg->native_code, cfg->code_len);
7463 mono_debug_close_method (cfg);
7467 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
7472 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
7473 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
7475 if (cp->opcode == OP_ICONST) {
7476 if (cfg->opt & MONO_OPT_CONSPROP) {
7477 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
7481 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
7482 if (cfg->opt & MONO_OPT_COPYPROP) {
7483 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
7489 arity = mono_burg_arity [tree->opcode];
7492 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
7493 if (cfg->opt & MONO_OPT_CFOLD)
7494 mono_constant_fold_inst (tree, NULL);
7495 /* The opcode may have changed */
7496 if (mono_burg_arity [tree->opcode] > 1) {
7497 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
7498 if (cfg->opt & MONO_OPT_CFOLD)
7499 mono_constant_fold_inst (tree, NULL);
7501 mono_constant_fold_inst (tree, NULL);
7507 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
7511 switch (tree->opcode) {
7521 if (tree->ssa_op == MONO_SSA_NOP) {
7522 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7539 case OP_VOIDCALL_REG:
7540 case OP_VOIDCALLVIRT:
7542 MonoCallInst *call = (MonoCallInst *)tree;
7543 MonoMethodSignature *sig = call->signature;
7544 int i, byref = FALSE;
7546 for (i = 0; i < sig->param_count; i++) {
7547 if (sig->params [i]->byref) {
7554 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7562 arity = mono_burg_arity [tree->opcode];
7568 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7571 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7572 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
7575 g_assert_not_reached ();
7580 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
7582 MonoInst *tree = bb->code;
7588 for (; tree; tree = tree->next) {
7590 mono_cprop_copy_values (cfg, tree, acp);
7592 mono_cprop_invalidate_values (tree, acp, acp_size);
7594 if (tree->ssa_op == MONO_SSA_STORE &&
7595 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
7596 MonoInst *i1 = tree->inst_i1;
7598 acp [tree->inst_i0->inst_c0] = NULL;
7600 for (i = 0; i < acp_size; i++) {
7601 if (acp [i] && acp [i]->opcode != OP_ICONST &&
7602 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
7607 if (i1->opcode == OP_ICONST) {
7608 acp [tree->inst_i0->inst_c0] = i1;
7609 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
7611 if (i1->ssa_op == MONO_SSA_LOAD &&
7612 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
7613 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
7614 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
7615 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
7620 if (tree->opcode == CEE_BEQ) {
7621 g_assert (tree->inst_i0->opcode == OP_COMPARE);
7622 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
7623 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
7625 tree->opcode = CEE_BR;
7626 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
7627 tree->inst_target_bb = tree->inst_true_bb;
7629 tree->inst_target_bb = tree->inst_false_bb;
7638 mono_local_cprop (MonoCompile *cfg)
7643 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
7645 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7646 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
7647 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
7652 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, int parts)
7654 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
7655 guint8 *ip = (guint8 *)header->code;
7658 int dfn = 0, i, code_size_ratio;
7660 mono_jit_stats.methods_compiled++;
7661 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
7662 mono_profiler_method_jit (method);
7664 cfg = g_new0 (MonoCompile, 1);
7665 cfg->method = method;
7666 cfg->mempool = mono_mempool_new ();
7668 cfg->prof_options = mono_profiler_get_events ();
7669 cfg->run_cctors = run_cctors;
7670 cfg->bb_hash = g_hash_table_new (NULL, NULL);
7671 cfg->domain = domain;
7672 cfg->verbose_level = mini_verbose;
7673 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
7674 ((MonoMethodNormal *)method)->header->max_stack);
7676 if (cfg->verbose_level > 2)
7677 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
7680 * create MonoInst* which represents arguments and local variables
7682 mono_compile_create_vars (cfg);
7684 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
7685 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7686 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
7687 mono_destroy_compile (cfg);
7691 mono_jit_stats.basic_blocks += cfg->num_bblocks;
7692 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
7694 if (cfg->num_varinfo > 2000) {
7696 * we disable some optimizations if there are too many variables
7697 * because JIT time may become too expensive. The actual number needs
7698 * to be tweaked and eventually the non-linear algorithms should be fixed.
7700 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
7701 cfg->disable_ssa = TRUE;
7703 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
7705 /* Depth-first ordering on basic blocks */
7706 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
7708 if (cfg->opt & MONO_OPT_BRANCH)
7709 optimize_branches (cfg);
7711 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
7712 if (cfg->num_bblocks != dfn + 1) {
7715 cfg->num_bblocks = dfn + 1;
7717 if (!header->clauses) {
7718 /* remove unreachable code, because the code in them may be
7719 * inconsistent (access to dead variables for example) */
7720 for (bb = cfg->bb_entry; bb;) {
7721 MonoBasicBlock *bbn = bb->next_bb;
7723 if (bbn && bbn->region == -1 && !bbn->dfn) {
7724 if (cfg->verbose_level > 1)
7725 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
7726 bb->next_bb = bbn->next_bb;
7727 nullify_basic_block (bbn);
7735 if (cfg->opt & MONO_OPT_LOOP) {
7736 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
7737 mono_compute_natural_loops (cfg);
7740 /* after method_to_ir */
7744 //#define DEBUGSSA "logic_run"
7745 #define DEBUGSSA_CLASS "Tests"
7748 if (!header->num_clauses && !cfg->disable_ssa) {
7749 mono_local_cprop (cfg);
7750 mono_ssa_compute (cfg);
7754 /* fixme: add all optimizations which requires SSA */
7755 if (cfg->opt & (MONO_OPT_DEADCE)) {
7756 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
7757 mono_local_cprop (cfg);
7758 mono_ssa_compute (cfg);
7760 if (cfg->verbose_level >= 2) {
7767 /* after SSA translation */
7771 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
7772 if (cfg->comp_done & MONO_COMP_SSA) {
7773 mono_ssa_cprop (cfg);
7775 mono_local_cprop (cfg);
7779 if (cfg->comp_done & MONO_COMP_SSA) {
7780 mono_ssa_deadce (cfg);
7782 //mono_ssa_strength_reduction (cfg);
7784 mono_ssa_remove (cfg);
7786 if (cfg->opt & MONO_OPT_BRANCH)
7787 optimize_branches (cfg);
7790 /* after SSA removal */
7794 decompose_pass (cfg);
7796 if (cfg->opt & MONO_OPT_LINEARS) {
7799 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
7800 cfg->comp_done &= ~MONO_COMP_LIVENESS;
7801 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
7802 mono_analyze_liveness (cfg);
7804 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
7805 regs = mono_arch_get_global_int_regs (cfg);
7806 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
7810 //mono_print_code (cfg);
7814 /* variables are allocated after decompose, since decompose could create temps */
7815 mono_arch_allocate_vars (cfg);
7817 if (cfg->opt & MONO_OPT_CFOLD)
7818 mono_constant_fold (cfg);
7820 mini_select_instructions (cfg);
7823 if (cfg->verbose_level >= 2) {
7824 char *id = mono_method_full_name (cfg->method, FALSE);
7825 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
7829 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
7831 jinfo = g_new0 (MonoJitInfo, 1);
7832 jinfo->method = method;
7833 jinfo->code_start = cfg->native_code;
7834 jinfo->code_size = cfg->code_len;
7835 jinfo->used_regs = cfg->used_int_regs;
7836 jinfo->domain_neutral = (cfg->opt & MONO_OPT_SHARED) != 0;
7838 if (header->num_clauses) {
7841 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
7842 jinfo->num_clauses = header->num_clauses;
7843 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
7844 sizeof (MonoJitExceptionInfo) * header->num_clauses);
7846 for (i = 0; i < header->num_clauses; i++) {
7847 MonoExceptionClause *ec = &header->clauses [i];
7848 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
7849 MonoBasicBlock *tblock;
7851 ei->flags = ec->flags;
7853 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7854 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->token_or_filter);
7856 ei->data.filter = cfg->native_code + tblock->native_offset;
7858 ei->data.token = ec->token_or_filter;
7861 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
7863 ei->try_start = cfg->native_code + tblock->native_offset;
7864 g_assert (tblock->native_offset);
7865 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
7867 ei->try_end = cfg->native_code + tblock->native_offset;
7868 g_assert (tblock->native_offset);
7869 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
7871 ei->handler_start = cfg->native_code + tblock->native_offset;
7875 cfg->jit_info = jinfo;
7877 mono_jit_info_table_add (cfg->domain, jinfo);
7879 /* collect statistics */
7880 mono_jit_stats.allocated_code_size += cfg->code_len;
7881 code_size_ratio = cfg->code_len;
7882 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
7883 mono_jit_stats.biggest_method_size = code_size_ratio;
7884 mono_jit_stats.biggest_method = method;
7886 code_size_ratio = (code_size_ratio * 100) / ((MonoMethodNormal *)method)->header->code_size;
7887 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
7888 mono_jit_stats.max_code_size_ratio = code_size_ratio;
7889 mono_jit_stats.max_ratio_method = method;
7891 mono_jit_stats.native_code_size += cfg->code_len;
7893 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7894 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
7900 mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain)
7903 GHashTable *jit_code_hash;
7904 gpointer code = NULL;
7910 jit_code_hash = target_domain->jit_code_hash;
7912 #ifdef MONO_USE_AOT_COMPILER
7913 if (!mono_compile_aot && (opt & MONO_OPT_AOT)) {
7915 MonoDomain *domain = mono_domain_get ();
7917 mono_domain_lock (domain);
7919 mono_class_init (method->klass);
7920 if ((info = mono_aot_get_method (domain, method))) {
7921 g_hash_table_insert (domain->jit_code_hash, method, info);
7922 mono_domain_unlock (domain);
7923 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
7924 return info->code_start;
7927 mono_domain_unlock (domain);
7931 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
7932 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
7935 if (!method->addr) {
7936 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
7937 method->addr = mono_lookup_internal_call (method);
7939 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
7940 mono_lookup_pinvoke_call (method, NULL, NULL);
7942 #ifdef MONO_USE_EXC_TABLES
7943 if (mono_method_blittable (method)) {
7944 return method->addr;
7947 nm = mono_marshal_get_native_wrapper (method);
7948 return mono_compile_method (nm);
7950 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
7951 //mono_debug_add_wrapper (method, nm);
7952 #ifdef MONO_USE_EXC_TABLES
7955 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
7956 const char *name = method->name;
7959 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
7960 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
7961 /* FIXME: uhm, we need a wrapper to handle exceptions? */
7962 return (gpointer)mono_delegate_ctor;
7963 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
7964 nm = mono_marshal_get_delegate_invoke (method);
7965 return mono_jit_compile_method (nm);
7966 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
7967 nm = mono_marshal_get_delegate_begin_invoke (method);
7968 return mono_jit_compile_method (nm);
7969 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
7970 nm = mono_marshal_get_delegate_end_invoke (method);
7971 return mono_jit_compile_method (nm);
7977 cfg = mini_method_compile (method, opt, target_domain, TRUE, 0);
7979 mono_domain_lock (target_domain);
7981 /* Check if some other thread already did the job. In this case, we can
7982 discard the code this thread generated. */
7984 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
7985 /* We can't use a domain specific method in another domain */
7986 if ((target_domain == mono_domain_get ()) || info->domain_neutral) {
7987 code = info->code_start;
7988 // printf("Discarding code for method %s\n", method->name);
7993 g_hash_table_insert (jit_code_hash, method, cfg->jit_info);
7994 code = cfg->native_code;
7997 mono_destroy_compile (cfg);
7999 if (target_domain->jump_target_hash) {
8000 MonoJumpInfo patch_info;
8002 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
8004 patch_info.next = NULL;
8005 patch_info.ip.i = 0;
8006 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
8007 patch_info.data.method = method;
8008 g_hash_table_remove (target_domain->jump_target_hash, method);
8010 for (tmp = list; tmp; tmp = tmp->next)
8011 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info, TRUE);
8012 g_slist_free (list);
8015 mono_domain_unlock (target_domain);
8017 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
8022 mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt)
8024 /* FIXME: later copy the code from mono */
8025 MonoDomain *target_domain, *domain = mono_domain_get ();
8029 if (default_opt & MONO_OPT_SHARED)
8030 target_domain = mono_root_domain;
8032 target_domain = domain;
8034 mono_domain_lock (target_domain);
8036 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
8037 /* We can't use a domain specific method in another domain */
8038 if (! ((domain != target_domain) && !info->domain_neutral)) {
8039 mono_domain_unlock (target_domain);
8040 mono_jit_stats.methods_lookups++;
8041 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
8042 return info->code_start;
8046 mono_domain_unlock (target_domain);
8047 p = mono_jit_compile_method_inner (method, target_domain);
8052 mono_jit_compile_method (MonoMethod *method)
8054 return mono_jit_compile_method_with_opt (method, default_opt);
8058 mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method)
8060 MonoDomain *target_domain;
8063 if (default_opt & MONO_OPT_SHARED)
8064 target_domain = mono_root_domain;
8066 target_domain = domain;
8068 mono_domain_lock (target_domain);
8070 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
8071 /* We can't use a domain specific method in another domain */
8072 if (! ((domain != target_domain) && !info->domain_neutral)) {
8073 mono_domain_unlock (target_domain);
8074 mono_jit_stats.methods_lookups++;
8075 return info->code_start;
8079 mono_domain_unlock (target_domain);
8085 * mono_jit_runtime_invoke:
8086 * @method: the method to invoke
8087 * @obj: this pointer
8088 * @params: array of parameter values.
8089 * @exc: used to catch exceptions objects
8092 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
8095 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc);
8097 if (obj == NULL && !(method->flags & METHOD_ATTRIBUTE_STATIC) && !method->string_ctor && (method->wrapper_type == 0)) {
8098 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
8102 invoke = mono_marshal_get_runtime_invoke (method);
8103 runtime_invoke = mono_jit_compile_method (invoke);
8104 return runtime_invoke (obj, params, exc);
8107 #ifdef PLATFORM_WIN32
8108 #define GET_CONTEXT \
8109 struct sigcontext *ctx = (struct sigcontext*)_dummy;
8112 #define GET_CONTEXT \
8113 void *ctx = context;
8114 #elif defined(__ppc__) || defined (__powerpc__) || defined (__s390__)
8115 #define GET_CONTEXT \
8116 void *ctx = context;
8118 #define GET_CONTEXT \
8119 void **_p = (void **)&_dummy; \
8120 struct sigcontext *ctx = (struct sigcontext *)++_p;
8124 #ifdef MONO_ARCH_USE_SIGACTION
8125 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, siginfo_t *info, void *context)
8127 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy)
8131 SIG_HANDLER_SIGNATURE (sigfpe_signal_handler)
8136 exc = mono_get_exception_divide_by_zero ();
8138 mono_arch_handle_exception (ctx, exc, FALSE);
8142 SIG_HANDLER_SIGNATURE (sigill_signal_handler)
8146 exc = mono_get_exception_execution_engine ("SIGILL");
8148 mono_arch_handle_exception (ctx, exc, FALSE);
8151 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8154 sigsegv_signal_handler (int _dummy, siginfo_t *info, void *context)
8157 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
8158 struct sigcontext *ctx = (struct sigcontext *)&(((ucontext_t*)context)->uc_mcontext);
8160 /* Can't allocate memory using Boehm GC on altstack */
8161 if (jit_tls->stack_size &&
8162 ((guint8*)info->si_addr >= (guint8*)jit_tls->end_of_stack - jit_tls->stack_size) &&
8163 ((guint8*)info->si_addr < (guint8*)jit_tls->end_of_stack))
8164 exc = mono_domain_get ()->stack_overflow_ex;
8166 exc = mono_domain_get ()->null_reference_ex;
8168 mono_arch_handle_exception (ctx, exc, FALSE);
8174 SIG_HANDLER_SIGNATURE (sigsegv_signal_handler)
8178 mono_arch_handle_exception (ctx, NULL, FALSE);
8184 SIG_HANDLER_SIGNATURE (sigusr1_signal_handler)
8189 thread = mono_thread_current ();
8191 thread->abort_exc = mono_get_exception_thread_abort ();
8193 mono_arch_handle_exception (ctx, thread->abort_exc, FALSE);
8197 SIG_HANDLER_SIGNATURE (sigquit_signal_handler)
8202 exc = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
8204 mono_arch_handle_exception (ctx, exc, FALSE);
8208 SIG_HANDLER_SIGNATURE (sigint_signal_handler)
8213 exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
8215 mono_arch_handle_exception (ctx, exc, FALSE);
8218 #ifndef PLATFORM_WIN32
8220 add_signal_handler (int signo, gpointer handler)
8222 struct sigaction sa;
8224 #ifdef MONO_ARCH_USE_SIGACTION
8225 sa.sa_sigaction = handler;
8226 sigemptyset (&sa.sa_mask);
8227 sa.sa_flags = SA_SIGINFO;
8229 sa.sa_handler = handler;
8230 sigemptyset (&sa.sa_mask);
8233 g_assert (sigaction (signo, &sa, NULL) != -1);
8238 mono_runtime_install_handlers (void)
8240 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8241 struct sigaction sa;
8244 #ifdef PLATFORM_WIN32
8246 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
8247 win32_seh_set_handler(SIGILL, sigill_signal_handler);
8248 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
8249 if (getenv ("MONO_DEBUG"))
8250 win32_seh_set_handler(SIGINT, sigint_signal_handler);
8251 #else /* !PLATFORM_WIN32 */
8253 /* libpthreads has its own implementation of sigaction(),
8254 * but it seems to work well with our current exception
8255 * handlers. If not we must call syscall directly instead
8258 if (getenv ("MONO_DEBUG")) {
8259 add_signal_handler (SIGINT, sigint_signal_handler);
8262 add_signal_handler (SIGFPE, sigfpe_signal_handler);
8263 add_signal_handler (SIGQUIT, sigquit_signal_handler);
8264 add_signal_handler (SIGILL, sigill_signal_handler);
8265 add_signal_handler (SIGBUS, sigsegv_signal_handler);
8266 add_signal_handler (mono_thread_get_abort_signal (), sigusr1_signal_handler);
8269 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8270 sa.sa_sigaction = sigsegv_signal_handler;
8271 sigemptyset (&sa.sa_mask);
8272 sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
8273 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
8275 add_signal_handler (SIGSEGV, sigsegv_signal_handler);
8278 #endif /* PLATFORM_WIN32 */
8281 /* mono_jit_create_remoting_trampoline:
8282 * @method: pointer to the method info
8284 * Creates a trampoline which calls the remoting functions. This
8285 * is used in the vtable of transparent proxies.
8287 * Returns: a pointer to the newly created code
8290 mono_jit_create_remoting_trampoline (MonoMethod *method)
8293 guint8 *addr = NULL;
8295 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
8296 (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
8297 nm = mono_marshal_get_remoting_invoke (method);
8298 addr = mono_compile_method (nm);
8300 addr = mono_compile_method (method);
8306 mini_init (const char *filename)
8310 mono_arch_cpu_init ();
8312 if (!g_thread_supported ())
8313 g_thread_init (NULL);
8315 mono_jit_tls_id = TlsAlloc ();
8316 setup_jit_tls_data ((gpointer)-1, mono_thread_abort);
8318 InitializeCriticalSection (&trampoline_hash_mutex);
8322 if (default_opt & MONO_OPT_AOT)
8325 mono_runtime_install_handlers ();
8326 mono_threads_install_cleanup (mini_thread_cleanup);
8328 #define JIT_TRAMPOLINES_WORK
8329 #ifdef JIT_TRAMPOLINES_WORK
8330 mono_install_compile_method (mono_jit_compile_method);
8331 mono_install_trampoline (mono_arch_create_jit_trampoline);
8332 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
8334 #define JIT_INVOKE_WORKS
8335 #ifdef JIT_INVOKE_WORKS
8336 mono_install_runtime_invoke (mono_jit_runtime_invoke);
8337 mono_install_handler (mono_arch_get_throw_exception ());
8339 mono_install_stack_walk (mono_jit_walk_stack);
8341 domain = mono_init (filename);
8344 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
8345 ves_icall_get_frame_info);
8346 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
8347 ves_icall_get_trace);
8348 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
8349 mono_runtime_install_handlers);
8352 create_helper_signature ();
8354 #define JIT_CALLS_WORK
8355 #ifdef JIT_CALLS_WORK
8356 /* Needs to be called here since register_jit_icall depends on it */
8357 mono_marshal_init ();
8359 mono_arch_register_lowlevel_calls ();
8360 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
8361 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
8362 mono_register_jit_icall (mono_trace_enter_method, "mono_trace_enter_method", NULL, TRUE);
8363 mono_register_jit_icall (mono_trace_leave_method, "mono_trace_leave_method", NULL, TRUE);
8365 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
8366 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
8368 /* fixme: we cant hanlde vararg methods this way, because the signature is not constant */
8369 //mono_register_jit_icall (ves_array_element_address, "ves_array_element_address", NULL);
8370 //mono_register_jit_icall (mono_array_new_va, "mono_array_new_va", NULL);
8372 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
8373 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
8374 helper_sig_void_ptr, TRUE);
8375 mono_register_jit_icall (mono_thread_get_pending_exception, "mono_thread_get_pending_exception", helper_sig_obj_void, FALSE);
8378 * NOTE, NOTE, NOTE, NOTE:
8379 * when adding emulation for some opcodes, remember to also add a dummy
8380 * rule to the burg files, because we need the arity information to be correct.
8382 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult, TRUE);
8383 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un, FALSE);
8384 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf, FALSE);
8385 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv, FALSE);
8386 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un, FALSE);
8387 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem, FALSE);
8388 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un, FALSE);
8390 #ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
8391 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl, TRUE);
8392 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr, TRUE);
8393 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un, TRUE);
8396 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8, FALSE);
8397 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4, FALSE);
8398 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8, FALSE);
8399 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8, FALSE);
8401 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
8402 mono_register_opcode_emulation (OP_FCONV_TO_I8, "__emul_fconv_to_i8", helper_sig_long_double, mono_fconv_i8, FALSE);
8404 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
8405 mono_register_opcode_emulation (CEE_CONV_R_UN, "__emul_conv_r_un", helper_sig_double_int, mono_conv_to_r8_un, FALSE);
8407 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
8408 mono_register_opcode_emulation (OP_LCONV_TO_R8, "__emul_lconv_to_r8", helper_sig_double_long, mono_lconv_to_r8, FALSE);
8410 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
8411 mono_register_opcode_emulation (OP_LCONV_TO_R4, "__emul_lconv_to_r4", helper_sig_float_long, mono_lconv_to_r4, FALSE);
8413 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
8414 mono_register_opcode_emulation (OP_LCONV_TO_R_UN, "__emul_lconv_to_r8_un", helper_sig_double_long, mono_lconv_to_r8_un, FALSE);
8416 #ifdef MONO_ARCH_EMULATE_FREM
8417 mono_register_opcode_emulation (OP_FREM, "__emul_frem", helper_sig_double_double_double, fmod, FALSE);
8420 #if SIZEOF_VOID_P == 4
8421 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4, TRUE);
8424 #warning "fixme: add opcode emulation"
8428 /* other jit icalls */
8429 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
8430 helper_sig_ptr_ptr_ptr, FALSE);
8431 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
8432 mono_register_jit_icall (mono_get_special_static_data, "mono_get_special_static_data", helper_sig_ptr_int, FALSE);
8433 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
8434 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
8435 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
8436 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
8437 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
8438 mono_register_jit_icall (helper_stelem_ref_check, "helper_stelem_ref_check", helper_sig_stelem_ref_check, FALSE);
8439 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
8440 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
8441 mono_register_jit_icall (mono_object_new_fast, "mono_object_new_fast", helper_sig_object_new_specific, FALSE);
8442 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
8443 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
8444 mono_register_jit_icall (mono_string_to_utf16, "mono_string_to_utf16", helper_sig_ptr_obj, FALSE);
8445 mono_register_jit_icall (mono_string_from_utf16, "mono_string_from_utf16", helper_sig_obj_ptr, FALSE);
8446 mono_register_jit_icall (mono_string_new_wrapper, "mono_string_new_wrapper", helper_sig_obj_ptr, FALSE);
8447 mono_register_jit_icall (mono_string_to_utf8, "mono_string_to_utf8", helper_sig_ptr_obj, FALSE);
8448 mono_register_jit_icall (mono_string_to_bstr, "mono_string_to_bstr", helper_sig_ptr_obj, FALSE);
8449 mono_register_jit_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", helper_sig_ptr_obj, FALSE);
8450 mono_register_jit_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", helper_sig_ptr_obj, FALSE);
8451 mono_register_jit_icall (mono_string_builder_to_utf16, "mono_string_builder_to_utf16", helper_sig_ptr_obj, FALSE);
8452 mono_register_jit_icall (mono_array_to_savearray, "mono_array_to_savearray", helper_sig_ptr_obj, FALSE);
8453 mono_register_jit_icall (mono_array_to_lparray, "mono_array_to_lparray", helper_sig_ptr_obj, FALSE);
8454 mono_register_jit_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", helper_sig_ptr_obj, FALSE);
8455 mono_register_jit_icall (mono_marshal_string_array, "mono_marshal_string_array", helper_sig_ptr_obj, FALSE);
8456 mono_register_jit_icall (mono_marshal_string_array_to_unicode, "mono_marshal_string_array_to_unicode", helper_sig_ptr_obj, FALSE);
8457 mono_register_jit_icall (mono_string_utf8_to_builder, "mono_string_utf8_to_builder", helper_sig_void_ptr_ptr, FALSE);
8458 mono_register_jit_icall (mono_string_utf16_to_builder, "mono_string_utf16_to_builder", helper_sig_void_ptr_ptr, FALSE);
8459 mono_register_jit_icall (mono_marshal_free_array, "mono_marshal_free_array", helper_sig_void_ptr_ptr, FALSE);
8460 mono_register_jit_icall (mono_string_to_byvalstr, "mono_string_to_byvalstr", helper_sig_void_ptr_ptr_ptr, FALSE);
8461 mono_register_jit_icall (mono_string_to_byvalwstr, "mono_string_to_byvalwstr", helper_sig_void_ptr_ptr_ptr, FALSE);
8462 mono_register_jit_icall (g_free, "g_free", helper_sig_void_ptr, FALSE);
8463 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
8464 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
8465 mono_register_jit_icall (mono_ldftn_nosync, "mono_ldftn_nosync", helper_sig_compile, FALSE);
8466 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
8469 #define JIT_RUNTIME_WORKS
8470 #ifdef JIT_RUNTIME_WORKS
8471 mono_runtime_install_cleanup ((MonoDomainFunc)mini_cleanup);
8472 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
8475 mono_thread_attach (domain);
8479 MonoJitStats mono_jit_stats = {0};
8482 print_jit_stats (void)
8484 if (mono_jit_stats.enabled) {
8485 g_print ("Mono Jit statistics\n");
8486 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
8487 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
8488 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
8489 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
8490 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
8491 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
8492 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
8493 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
8494 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
8495 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
8496 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
8497 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
8498 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
8499 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
8500 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
8501 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
8502 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
8503 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
8505 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
8506 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
8507 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
8508 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
8509 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
8511 g_print ("\nGeneric instances: %ld\n", mono_stats.generic_instance_count);
8512 g_print ("Inflated methods: %ld\n", mono_stats.inflated_method_count);
8513 g_print ("Inflated types: %ld\n", mono_stats.inflated_type_count);
8514 g_print ("Generics metadata size: %ld\n", mono_stats.generics_metadata_size);
8519 mini_cleanup (MonoDomain *domain)
8522 * mono_runtime_cleanup() and mono_domain_finalize () need to
8523 * be called early since they need the execution engine still
8524 * fully working (mono_domain_finalize may invoke managed finalizers
8525 * and mono_runtime_cleanup will wait for other threads to finish).
8527 mono_domain_finalize (domain, 2000);
8529 mono_runtime_cleanup (domain);
8531 mono_profiler_shutdown ();
8533 mono_debug_cleanup ();
8535 #ifdef PLATFORM_WIN32
8536 win32_seh_cleanup();
8539 mono_domain_free (domain, TRUE);
8545 mono_set_defaults (int verbose_level, guint32 opts)
8547 mini_verbose = verbose_level;
8552 mono_precompile_assembly (MonoAssembly *ass, void *user_data)
8554 MonoImage *image = ass->image;
8558 if (mini_verbose > 0)
8559 printf ("PRECOMPILE: %s.\n", ass->image->name);
8561 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
8562 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
8563 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
8565 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
8569 if (mini_verbose > 1) {
8570 char * desc = mono_method_full_name (method, TRUE);
8571 g_print ("Compiling %d %s\n", count, desc);
8574 mono_compile_method (method);
8578 void mono_precompile_assemblies ()
8580 mono_assembly_foreach ((GFunc)mono_precompile_assembly, NULL);