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>
49 #include "jit-icalls.c"
51 #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))
53 #define MONO_CHECK_THIS(ins) (cfg->method->signature->hasthis && (ins)->ssa_op == MONO_SSA_LOAD && (ins)->inst_left->inst_c0 == 0)
55 gboolean mono_arch_handle_exception (struct sigcontext *ctx, gpointer obj, gboolean test_only);
56 static gpointer mono_jit_compile_method (MonoMethod *method);
58 static void handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src,
59 const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native);
61 static void dec_foreach (MonoInst *tree, MonoCompile *cfg);
63 static int mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
64 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
65 guint inline_offset, gboolean is_virtual_call);
67 extern guint8 mono_burg_arity [];
68 /* helper methods signature */
69 static MonoMethodSignature *helper_sig_long_long_long = NULL;
70 static MonoMethodSignature *helper_sig_long_long_int = NULL;
71 static MonoMethodSignature *helper_sig_newarr = NULL;
72 static MonoMethodSignature *helper_sig_newarr_specific = NULL;
73 static MonoMethodSignature *helper_sig_ldstr = NULL;
74 static MonoMethodSignature *helper_sig_domain_get = NULL;
75 static MonoMethodSignature *helper_sig_object_new = NULL;
76 static MonoMethodSignature *helper_sig_object_new_specific = NULL;
77 static MonoMethodSignature *helper_sig_compile = NULL;
78 static MonoMethodSignature *helper_sig_compile_virt = NULL;
79 static MonoMethodSignature *helper_sig_obj_ptr = NULL;
80 static MonoMethodSignature *helper_sig_obj_void = NULL;
81 static MonoMethodSignature *helper_sig_ptr_void = NULL;
82 static MonoMethodSignature *helper_sig_void_ptr = NULL;
83 static MonoMethodSignature *helper_sig_void_obj = NULL;
84 static MonoMethodSignature *helper_sig_void_ptr_ptr = NULL;
85 static MonoMethodSignature *helper_sig_void_ptr_ptr_ptr = NULL;
86 static MonoMethodSignature *helper_sig_ptr_ptr_ptr = NULL;
87 static MonoMethodSignature *helper_sig_ptr_obj = NULL;
88 static MonoMethodSignature *helper_sig_ptr_int = NULL;
89 static MonoMethodSignature *helper_sig_initobj = NULL;
90 static MonoMethodSignature *helper_sig_memcpy = NULL;
91 static MonoMethodSignature *helper_sig_memset = NULL;
92 static MonoMethodSignature *helper_sig_ulong_double = NULL;
93 static MonoMethodSignature *helper_sig_long_double = NULL;
94 static MonoMethodSignature *helper_sig_double_long = NULL;
95 static MonoMethodSignature *helper_sig_float_long = NULL;
96 static MonoMethodSignature *helper_sig_double_double_double = NULL;
97 static MonoMethodSignature *helper_sig_uint_double = NULL;
98 static MonoMethodSignature *helper_sig_int_double = NULL;
99 static MonoMethodSignature *helper_sig_stelem_ref = NULL;
100 static MonoMethodSignature *helper_sig_class_init_trampoline = NULL;
101 static MonoMethodSignature *helper_sig_obj_obj_cls = NULL;
103 static guint32 default_opt = MONO_OPT_PEEPHOLE;
105 guint32 mono_jit_tls_id = -1;
106 MonoTraceSpec *mono_jit_trace_calls = NULL;
107 gboolean mono_break_on_exc = FALSE;
108 gboolean mono_compile_aot = FALSE;
110 static int mini_verbose = 0;
112 static CRITICAL_SECTION class_init_hash_mutex;
114 static GHashTable *class_init_hash_addr = NULL;
116 #ifdef MONO_USE_EXC_TABLES
118 mono_type_blittable (MonoType *type)
138 case MONO_TYPE_VALUETYPE:
139 case MONO_TYPE_CLASS:
140 return type->data.klass->blittable;
150 mono_method_blittable (MonoMethod *method)
152 MonoMethodSignature *sig;
158 if (!mono_arch_has_unwind_info (method->addr)) {
162 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
165 sig = method->signature;
167 if (!mono_type_blittable (sig->ret))
170 for (i = 0; i < sig->param_count; i++)
171 if (!mono_type_blittable (sig->params [i]))
174 if (mono_method_has_marshal_info (method))
177 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && ((MonoMethodPInvoke*)method)->piflags & PINVOKE_ATTRIBUTE_SUPPORTS_LAST_ERROR)
185 G_GNUC_UNUSED static void
186 print_method_from_ip (void *ip)
191 MonoDomain *domain = mono_domain_get ();
193 ji = mono_jit_info_table_find (domain, ip);
195 g_print ("No method at %p\n", ip);
198 method = mono_method_full_name (ji->method, TRUE);
199 source = mono_debug_source_location_from_address (ji->method, (int) ip, NULL, domain);
201 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);
204 g_print ("%s\n", source);
212 * mono_method_same_domain:
214 * Determine whenever two compiled methods are in the same domain, thus
215 * the address of the callee can be embedded in the caller.
217 gboolean mono_method_same_domain (MonoJitInfo *caller, MonoJitInfo *callee)
219 if (!caller || !callee)
223 * If the call was made from domain-neutral to domain-specific
224 * code, we can't patch the call site.
226 if (caller->domain_neutral && !callee->domain_neutral)
229 if ((caller->method->klass == mono_defaults.appdomain_class) &&
230 (strstr (caller->method->name, "InvokeInDomain"))) {
231 /* The InvokeInDomain methods change the current appdomain */
239 mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token)
241 MonoJumpInfoToken *res = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoToken));
248 #define MONO_INIT_VARINFO(vi,id) do { \
249 (vi)->range.first_use.pos.bid = 0xffff; \
255 * Basic blocks have two numeric identifiers:
256 * dfn: Depth First Number
257 * block_num: unique ID assigned at bblock creation
259 #define NEW_BBLOCK(cfg) (mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)))
260 #define ADD_BBLOCK(cfg,bbhash,b) do { \
261 g_hash_table_insert (bbhash, (b)->cil_code, (b)); \
262 (b)->block_num = cfg->num_bblocks++; \
263 (b)->real_offset = real_offset; \
266 #define GET_BBLOCK(cfg,bbhash,tblock,ip) do { \
267 (tblock) = g_hash_table_lookup (bbhash, (ip)); \
269 if ((ip) >= end || (ip) < header->code) goto unverified; \
270 (tblock) = NEW_BBLOCK (cfg); \
271 (tblock)->cil_code = (ip); \
272 ADD_BBLOCK (cfg, (bbhash), (tblock)); \
274 (tblock)->real_offset = real_offset; \
277 #define CHECK_BBLOCK(target,ip,tblock) do { \
278 if ((target) < (ip) && !(tblock)->code) { \
279 bb_recheck = g_list_prepend (bb_recheck, (tblock)); \
280 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); \
284 #define NEW_ICONST(cfg,dest,val) do { \
285 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
286 (dest)->opcode = OP_ICONST; \
287 (dest)->inst_c0 = (val); \
288 (dest)->type = STACK_I4; \
291 /* FIXME: have a different definition of NEW_PCONST for 64 bit systems */
292 #define NEW_PCONST(cfg,dest,val) do { \
293 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
294 (dest)->opcode = OP_ICONST; \
295 (dest)->inst_p0 = (val); \
296 (dest)->type = STACK_PTR; \
299 #define NEW_CLASSCONST(cfg,dest,val) do { \
300 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
301 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
302 (dest)->inst_p0 = (val); \
303 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_CLASS; \
304 (dest)->type = STACK_PTR; \
307 #define NEW_IMAGECONST(cfg,dest,val) do { \
308 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
309 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
310 (dest)->inst_p0 = (val); \
311 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_IMAGE; \
312 (dest)->type = STACK_PTR; \
315 #define NEW_FIELDCONST(cfg,dest,field) do { \
316 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
317 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
318 (dest)->inst_p0 = (field); \
319 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_FIELD; \
320 (dest)->type = STACK_PTR; \
323 #define NEW_METHODCONST(cfg,dest,val) do { \
324 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
325 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
326 (dest)->inst_p0 = (val); \
327 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_METHODCONST; \
328 (dest)->type = STACK_PTR; \
331 #define NEW_VTABLECONST(cfg,dest,vtable) do { \
332 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
333 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
334 (dest)->inst_p0 = mono_compile_aot ? (gpointer)((vtable)->klass) : (vtable); \
335 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_VTABLE; \
336 (dest)->type = STACK_PTR; \
339 #define NEW_SFLDACONST(cfg,dest,field) do { \
340 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
341 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
342 (dest)->inst_p0 = (field); \
343 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_SFLDA; \
344 (dest)->type = STACK_PTR; \
347 #define NEW_LDSTRCONST(cfg,dest,image,token) do { \
348 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
349 (dest)->opcode = OP_AOTCONST; \
350 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
351 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_LDSTR; \
352 (dest)->type = STACK_OBJ; \
355 #define NEW_TYPE_FROM_HANDLE_CONST(cfg,dest,image,token) do { \
356 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
357 (dest)->opcode = OP_AOTCONST; \
358 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
359 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_TYPE_FROM_HANDLE; \
360 (dest)->type = STACK_OBJ; \
363 #define NEW_LDTOKENCONST(cfg,dest,image,token) do { \
364 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
365 (dest)->opcode = OP_AOTCONST; \
366 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
367 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_LDTOKEN; \
368 (dest)->type = STACK_PTR; \
371 #define NEW_DOMAINCONST(cfg,dest) do { \
372 if (cfg->opt & MONO_OPT_SHARED) { \
373 NEW_TEMPLOAD (cfg, dest, mono_get_domainvar (cfg)->inst_c0); \
375 NEW_PCONST (cfg, dest, (cfg)->domain); \
379 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
381 #define NEW_ARGLOAD(cfg,dest,num) do { \
382 if (arg_array [(num)]->opcode == OP_ICONST) (dest) = arg_array [(num)]; else { \
383 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
384 (dest)->ssa_op = MONO_SSA_LOAD; \
385 (dest)->inst_i0 = arg_array [(num)]; \
386 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
387 type_to_eval_stack_type (param_types [(num)], (dest)); \
388 (dest)->klass = (dest)->inst_i0->klass; \
391 #define NEW_LOCLOAD(cfg,dest,num) do { \
392 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
393 (dest)->ssa_op = MONO_SSA_LOAD; \
394 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
395 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
396 type_to_eval_stack_type (header->locals [(num)], (dest)); \
397 (dest)->klass = (dest)->inst_i0->klass; \
400 #define NEW_LOCLOADA(cfg,dest,num) do { \
401 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
402 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
403 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
404 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
405 (dest)->opcode = OP_LDADDR; \
406 (dest)->type = STACK_MP; \
407 (dest)->klass = (dest)->inst_i0->klass; \
408 (cfg)->disable_ssa = TRUE; \
411 #define NEW_RETLOADA(cfg,dest) do { \
412 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
413 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
414 (dest)->inst_i0 = (cfg)->ret; \
415 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
416 (dest)->opcode = CEE_LDIND_I; \
417 (dest)->type = STACK_MP; \
418 (dest)->klass = (dest)->inst_i0->klass; \
419 (cfg)->disable_ssa = TRUE; \
422 #define NEW_ARGLOADA(cfg,dest,num) do { \
423 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
424 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
425 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
426 (dest)->inst_i0 = arg_array [(num)]; \
427 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
428 (dest)->opcode = OP_LDADDR; \
429 (dest)->type = STACK_MP; \
430 (dest)->klass = (dest)->inst_i0->klass; \
431 (cfg)->disable_ssa = TRUE; \
434 #define NEW_TEMPLOAD(cfg,dest,num) do { \
435 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
436 (dest)->ssa_op = MONO_SSA_LOAD; \
437 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
438 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
439 type_to_eval_stack_type ((dest)->inst_i0->inst_vtype, (dest)); \
440 (dest)->klass = (dest)->inst_i0->klass; \
443 #define NEW_TEMPLOADA(cfg,dest,num) do { \
444 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
445 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
446 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
447 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
448 (dest)->opcode = OP_LDADDR; \
449 (dest)->type = STACK_MP; \
450 (dest)->klass = (dest)->inst_i0->klass; \
451 (cfg)->disable_ssa = TRUE; \
455 #define NEW_INDLOAD(cfg,dest,addr,vtype) do { \
456 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
457 (dest)->inst_left = addr; \
458 (dest)->opcode = mono_type_to_ldind (vtype); \
459 type_to_eval_stack_type (vtype, (dest)); \
460 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
463 #define NEW_INDSTORE(cfg,dest,addr,value,vtype) do { \
464 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
465 (dest)->inst_i0 = addr; \
466 (dest)->opcode = mono_type_to_stind (vtype); \
467 (dest)->inst_i1 = (value); \
468 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
471 #define NEW_TEMPSTORE(cfg,dest,num,inst) do { \
472 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
473 (dest)->ssa_op = MONO_SSA_STORE; \
474 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
475 (dest)->opcode = mono_type_to_stind ((dest)->inst_i0->inst_vtype); \
476 (dest)->inst_i1 = (inst); \
477 (dest)->klass = (dest)->inst_i0->klass; \
480 #define NEW_LOCSTORE(cfg,dest,num,inst) do { \
481 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
482 (dest)->opcode = mono_type_to_stind (header->locals [(num)]); \
483 (dest)->ssa_op = MONO_SSA_STORE; \
484 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
485 (dest)->inst_i1 = (inst); \
486 (dest)->klass = (dest)->inst_i0->klass; \
489 #define NEW_ARGSTORE(cfg,dest,num,inst) do { \
490 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
491 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
492 (dest)->opcode = mono_type_to_stind (param_types [(num)]); \
493 (dest)->ssa_op = MONO_SSA_STORE; \
494 (dest)->inst_i0 = arg_array [(num)]; \
495 (dest)->inst_i1 = (inst); \
496 (dest)->klass = (dest)->inst_i0->klass; \
499 #define ADD_BINOP(op) do { \
500 MONO_INST_NEW (cfg, ins, (op)); \
501 ins->cil_code = ip; \
503 ins->inst_i0 = sp [0]; \
504 ins->inst_i1 = sp [1]; \
506 type_from_op (ins); \
510 #define ADD_UNOP(op) do { \
511 MONO_INST_NEW (cfg, ins, (op)); \
512 ins->cil_code = ip; \
514 ins->inst_i0 = sp [0]; \
516 type_from_op (ins); \
520 #define ADD_BINCOND(next_block) do { \
522 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
524 cmp->inst_i0 = sp [0]; \
525 cmp->inst_i1 = sp [1]; \
526 cmp->cil_code = ins->cil_code; \
527 type_from_op (cmp); \
529 ins->inst_i0 = cmp; \
530 MONO_ADD_INS (bblock, ins); \
531 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
532 GET_BBLOCK (cfg, bbhash, tblock, target); \
533 link_bblock (cfg, bblock, tblock); \
534 ins->inst_true_bb = tblock; \
535 CHECK_BBLOCK (target, ip, tblock); \
536 if ((next_block)) { \
537 link_bblock (cfg, bblock, (next_block)); \
538 ins->inst_false_bb = (next_block); \
539 start_new_bblock = 1; \
541 GET_BBLOCK (cfg, bbhash, tblock, ip); \
542 link_bblock (cfg, bblock, tblock); \
543 ins->inst_false_bb = tblock; \
544 start_new_bblock = 2; \
548 /* FIXME: handle float, long ... */
549 #define ADD_UNCOND(istrue) do { \
551 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
553 cmp->inst_i0 = sp [0]; \
554 switch (cmp->inst_i0->type) { \
556 cmp->inst_i1 = zero_int64; break; \
558 cmp->inst_i1 = zero_r8; break; \
561 cmp->inst_i1 = zero_ptr; break; \
563 cmp->inst_i1 = zero_obj; break; \
565 cmp->inst_i1 = zero_int32; \
567 cmp->cil_code = ins->cil_code; \
568 type_from_op (cmp); \
570 ins->inst_i0 = cmp; \
571 ins->opcode = (istrue)? CEE_BNE_UN: CEE_BEQ; \
572 MONO_ADD_INS (bblock, ins); \
573 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
574 GET_BBLOCK (cfg, bbhash, tblock, target); \
575 link_bblock (cfg, bblock, tblock); \
576 ins->inst_true_bb = tblock; \
577 CHECK_BBLOCK (target, ip, tblock); \
578 GET_BBLOCK (cfg, bbhash, tblock, ip); \
579 link_bblock (cfg, bblock, tblock); \
580 ins->inst_false_bb = tblock; \
581 start_new_bblock = 2; \
584 #define NEW_LDELEMA(cfg,dest,sp,k) do { \
585 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
586 (dest)->opcode = CEE_LDELEMA; \
587 (dest)->inst_left = (sp) [0]; \
588 (dest)->inst_right = (sp) [1]; \
589 (dest)->type = STACK_MP; \
590 (dest)->klass = (k); \
593 #define NEW_GROUP(cfg,dest,el1,el2) do { \
594 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
595 (dest)->opcode = OP_GROUP; \
596 (dest)->inst_left = (el1); \
597 (dest)->inst_right = (el2); \
602 compare_bblock (gconstpointer a, gconstpointer b)
604 const MonoBasicBlock *b1 = a;
605 const MonoBasicBlock *b2 = b;
607 return b2->cil_code - b1->cil_code;
612 * link_bblock: Links two basic blocks
614 * links two basic blocks in the control flow graph, the 'from'
615 * argument is the starting block and the 'to' argument is the block
616 * the control flow ends to after 'from'.
619 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
621 MonoBasicBlock **newa;
625 if (from->cil_code) {
627 g_print ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
629 g_print ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
632 g_print ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
634 g_print ("edge from entry to exit\n");
638 for (i = 0; i < from->out_count; ++i) {
639 if (to == from->out_bb [i]) {
645 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
646 for (i = 0; i < from->out_count; ++i) {
647 newa [i] = from->out_bb [i];
655 for (i = 0; i < to->in_count; ++i) {
656 if (from == to->in_bb [i]) {
662 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
663 for (i = 0; i < to->in_count; ++i) {
664 newa [i] = to->in_bb [i];
673 * We mark each basic block with a region ID. We use that to avoid BB
674 * optimizations when blocks are in different regions.
677 mono_find_block_region (MonoCompile *cfg, int offset, int *filter_lengths)
679 MonoMethod *method = cfg->method;
680 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
681 MonoExceptionClause *clause;
684 /* first search for handlers and filters */
685 for (i = 0; i < header->num_clauses; ++i) {
686 clause = &header->clauses [i];
687 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->token_or_filter) &&
688 (offset < (clause->token_or_filter + filter_lengths [i])))
689 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
691 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
692 if (clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)
693 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
695 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
699 /* search the try blocks */
700 for (i = 0; i < header->num_clauses; ++i) {
701 clause = &header->clauses [i];
702 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
703 return ((i + 1) << 8) | clause->flags;
710 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
712 MonoMethod *method = cfg->method;
713 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
714 MonoExceptionClause *clause;
715 MonoBasicBlock *handler;
719 for (i = 0; i < header->num_clauses; ++i) {
720 clause = &header->clauses [i];
721 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
722 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
723 if (clause->flags == type) {
724 handler = g_hash_table_lookup (cfg->bb_hash, header->code + clause->handler_offset);
726 res = g_list_append (res, handler);
734 mono_find_spvar_for_region (MonoCompile *cfg, int region)
736 return g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
740 mono_create_spvar_for_region (MonoCompile *cfg, int region)
744 var = g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
748 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
749 /* prevent it from being register allocated */
750 var->flags |= MONO_INST_INDIRECT;
752 g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
756 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
760 array [*dfn] = start;
761 /*g_print ("visit %d at %p\n", *dfn, start->cil_code);*/
762 for (i = 0; i < start->out_count; ++i) {
763 if (start->out_bb [i]->dfn)
766 start->out_bb [i]->dfn = *dfn;
767 start->out_bb [i]->df_parent = start;
768 array [*dfn] = start->out_bb [i];
769 df_visit (start->out_bb [i], dfn, array);
775 MonoBasicBlock *best;
779 previous_foreach (gconstpointer key, gpointer val, gpointer data)
781 PrevStruct *p = data;
782 MonoBasicBlock *bb = val;
783 //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,
784 //bb->method == p->best->method, bb->cil_code < p->code, bb->cil_code > p->best->cil_code);
786 if (bb->cil_code && bb->cil_code < p->code && bb->cil_code > p->best->cil_code)
790 static MonoBasicBlock*
791 find_previous (GHashTable *bb_hash, MonoBasicBlock *start, const guchar *code) {
797 g_hash_table_foreach (bb_hash, (GHFunc)previous_foreach, &p);
802 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
811 * FIXME: take into account all the details:
812 * second may have been the target of more than one bblock
814 second->out_count = first->out_count;
815 second->out_bb = first->out_bb;
817 for (i = 0; i < first->out_count; ++i) {
818 bb = first->out_bb [i];
819 for (j = 0; j < bb->in_count; ++j) {
820 if (bb->in_bb [j] == first)
821 bb->in_bb [j] = second;
825 first->out_count = 0;
826 first->out_bb = NULL;
827 link_bblock (cfg, first, second);
829 second->last_ins = first->last_ins;
831 /*g_print ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
832 for (inst = first->code; inst && inst->next; inst = inst->next) {
833 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
834 g_print ("found %p: %s", inst->next->cil_code, code);
836 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
837 second->code = inst->next;
839 first->last_ins = inst;
840 second->next_bb = first->next_bb;
841 first->next_bb = second;
846 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
852 mono_type_to_ldind (MonoType *type)
864 case MONO_TYPE_BOOLEAN:
878 case MONO_TYPE_FNPTR:
880 case MONO_TYPE_CLASS:
881 case MONO_TYPE_STRING:
882 case MONO_TYPE_OBJECT:
883 case MONO_TYPE_SZARRAY:
884 case MONO_TYPE_ARRAY:
885 return CEE_LDIND_REF;
893 case MONO_TYPE_VALUETYPE:
894 if (type->data.klass->enumtype) {
895 t = type->data.klass->enum_basetype->type;
899 case MONO_TYPE_TYPEDBYREF:
901 case MONO_TYPE_GENERICINST:
902 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE)
904 return CEE_LDIND_REF;
906 g_error ("unknown type 0x%02x in type_to_ldind", type->type);
912 mono_type_to_stind (MonoType *type)
923 case MONO_TYPE_BOOLEAN:
935 case MONO_TYPE_FNPTR:
937 case MONO_TYPE_CLASS:
938 case MONO_TYPE_STRING:
939 case MONO_TYPE_OBJECT:
940 case MONO_TYPE_SZARRAY:
941 case MONO_TYPE_ARRAY:
942 return CEE_STIND_REF;
950 case MONO_TYPE_VALUETYPE:
951 if (type->data.klass->enumtype) {
952 t = type->data.klass->enum_basetype->type;
956 case MONO_TYPE_TYPEDBYREF:
958 case MONO_TYPE_GENERICINST:
959 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE)
961 return CEE_STIND_REF;
963 g_error ("unknown type 0x%02x in type_to_stind", type->type);
969 * Returns the type used in the eval stack when @type is loaded.
970 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
973 type_to_eval_stack_type (MonoType *type, MonoInst *inst) {
977 inst->type = STACK_MP;
985 case MONO_TYPE_BOOLEAN:
991 inst->type = STACK_I4;
996 case MONO_TYPE_FNPTR:
997 inst->type = STACK_PTR;
999 case MONO_TYPE_CLASS:
1000 case MONO_TYPE_STRING:
1001 case MONO_TYPE_OBJECT:
1002 case MONO_TYPE_SZARRAY:
1003 case MONO_TYPE_ARRAY:
1004 inst->type = STACK_OBJ;
1008 inst->type = STACK_I8;
1012 inst->type = STACK_R8;
1014 case MONO_TYPE_VALUETYPE:
1015 if (type->data.klass->enumtype) {
1016 t = type->data.klass->enum_basetype->type;
1019 inst->klass = type->data.klass;
1020 inst->type = STACK_VTYPE;
1023 case MONO_TYPE_TYPEDBYREF:
1024 inst->klass = mono_defaults.typed_reference_class;
1025 inst->type = STACK_VTYPE;
1027 case MONO_TYPE_GENERICINST:
1028 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE) {
1029 inst->klass = mono_class_from_mono_type (type);
1030 inst->type = STACK_VTYPE;
1032 inst->type = STACK_OBJ;
1036 g_error ("unknown type 0x%02x in eval stack type", type->type);
1041 * The following tables are used to quickly validate the IL code in type_from_op ().
1044 bin_num_table [STACK_MAX] [STACK_MAX] = {
1045 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1046 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1047 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1048 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1049 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
1050 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
1051 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1052 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1057 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
1060 /* reduce the size of this table */
1062 bin_int_table [STACK_MAX] [STACK_MAX] = {
1063 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1064 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1065 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1066 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1067 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1068 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1069 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1070 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1074 bin_comp_table [STACK_MAX] [STACK_MAX] = {
1076 {0, 1, 0, 1, 0, 0, 4, 0},
1077 {0, 0, 1, 0, 0, 0, 0, 0},
1078 {0, 1, 0, 1, 0, 2, 4, 0},
1079 {0, 0, 0, 0, 1, 0, 0, 0},
1080 {0, 0, 0, 2, 0, 1, 0, 0},
1081 {0, 4, 0, 4, 0, 0, 3, 0},
1082 {0, 0, 0, 0, 0, 0, 0, 0},
1085 /* reduce the size of this table */
1087 shift_table [STACK_MAX] [STACK_MAX] = {
1088 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1089 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1090 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1091 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1092 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1093 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1094 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1095 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1099 * Tables to map from the non-specific opcode to the matching
1100 * type-specific opcode.
1102 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
1103 static const guint16
1104 binops_op_map [STACK_MAX] = {
1105 0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, 0
1108 /* handles from CEE_NEG to CEE_CONV_U8 */
1109 static const guint16
1110 unops_op_map [STACK_MAX] = {
1111 0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, 0
1114 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
1115 static const guint16
1116 ovfops_op_map [STACK_MAX] = {
1117 0, 0, OP_LCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_FCONV_TO_U2-CEE_CONV_U2, 0
1120 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
1121 static const guint16
1122 ovf2ops_op_map [STACK_MAX] = {
1123 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
1126 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
1127 static const guint16
1128 ovf3ops_op_map [STACK_MAX] = {
1129 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
1132 /* handles from CEE_CEQ to CEE_CLT_UN */
1133 static const guint16
1134 ceqops_op_map [STACK_MAX] = {
1135 0, 0, OP_LCEQ-CEE_CEQ, OP_PCEQ-CEE_CEQ, OP_FCEQ-CEE_CEQ, 0
1139 * Sets ins->type (the type on the eval stack) according to the
1140 * type of the opcode and the arguments to it.
1141 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1143 * FIXME: this function sets ins->type unconditionally in some cases, but
1144 * it should set it to invalid for some types (a conv.x on an object)
1147 type_from_op (MonoInst *ins) {
1148 switch (ins->opcode) {
1155 /* FIXME: check unverifiable args for STACK_MP */
1156 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1157 ins->opcode += binops_op_map [ins->type];
1164 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1165 ins->opcode += binops_op_map [ins->type];
1170 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1171 ins->opcode += binops_op_map [ins->type];
1174 /* FIXME: handle some specifics with ins->next->type */
1175 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1179 case 256+CEE_CGT_UN:
1181 case 256+CEE_CLT_UN:
1182 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1183 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1187 ins->type = neg_table [ins->inst_i0->type];
1188 ins->opcode += unops_op_map [ins->type];
1191 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1192 ins->type = ins->inst_i0->type;
1194 ins->type = STACK_INV;
1195 ins->opcode += unops_op_map [ins->type];
1201 ins->type = STACK_I4;
1202 ins->opcode += unops_op_map [ins->inst_i0->type];
1205 ins->type = STACK_R8;
1206 switch (ins->inst_i0->type) {
1211 ins->opcode = OP_LCONV_TO_R_UN;
1215 case CEE_CONV_OVF_I1:
1216 case CEE_CONV_OVF_U1:
1217 case CEE_CONV_OVF_I2:
1218 case CEE_CONV_OVF_U2:
1219 case CEE_CONV_OVF_I4:
1220 case CEE_CONV_OVF_U4:
1221 ins->type = STACK_I4;
1222 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1224 case CEE_CONV_OVF_I_UN:
1225 case CEE_CONV_OVF_U_UN:
1226 ins->type = STACK_PTR;
1227 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1229 case CEE_CONV_OVF_I1_UN:
1230 case CEE_CONV_OVF_I2_UN:
1231 case CEE_CONV_OVF_I4_UN:
1232 case CEE_CONV_OVF_U1_UN:
1233 case CEE_CONV_OVF_U2_UN:
1234 case CEE_CONV_OVF_U4_UN:
1235 ins->type = STACK_I4;
1236 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1239 ins->type = STACK_PTR;
1240 switch (ins->inst_i0->type) {
1246 ins->opcode = OP_LCONV_TO_U;
1249 ins->opcode = OP_FCONV_TO_U;
1255 ins->type = STACK_I8;
1256 ins->opcode += unops_op_map [ins->inst_i0->type];
1258 case CEE_CONV_OVF_I8:
1259 case CEE_CONV_OVF_U8:
1260 ins->type = STACK_I8;
1261 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1263 case CEE_CONV_OVF_U8_UN:
1264 case CEE_CONV_OVF_I8_UN:
1265 ins->type = STACK_I8;
1266 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1270 ins->type = STACK_R8;
1271 ins->opcode += unops_op_map [ins->inst_i0->type];
1274 ins->type = STACK_R8;
1278 ins->type = STACK_I4;
1279 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1282 case CEE_CONV_OVF_I:
1283 case CEE_CONV_OVF_U:
1284 ins->type = STACK_PTR;
1285 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1288 case CEE_ADD_OVF_UN:
1290 case CEE_MUL_OVF_UN:
1292 case CEE_SUB_OVF_UN:
1293 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1294 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1297 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1304 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_MP, STACK_R8, STACK_R8, STACK_OBJ
1307 /* map ldelem.x to the matching ldind.x opcode */
1309 ldelem_to_ldind [] = {
1323 /* map stelem.x to the matching stind.x opcode */
1325 stelem_to_stind [] = {
1339 param_table [STACK_MAX] [STACK_MAX] = {
1344 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1348 switch (args->type) {
1358 for (i = 0; i < sig->param_count; ++i) {
1359 switch (args [i].type) {
1363 if (!sig->params [i]->byref)
1367 if (sig->params [i]->byref)
1369 switch (sig->params [i]->type) {
1370 case MONO_TYPE_CLASS:
1371 case MONO_TYPE_STRING:
1372 case MONO_TYPE_OBJECT:
1373 case MONO_TYPE_SZARRAY:
1374 case MONO_TYPE_ARRAY:
1381 if (sig->params [i]->byref)
1383 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1392 /*if (!param_table [args [i].type] [sig->params [i]->type])
1400 * When we need a pointer to the current domain many times in a method, we
1401 * call mono_domain_get() once and we store the result in a local variable.
1402 * This function returns the variable that represents the MonoDomain*.
1404 inline static MonoInst *
1405 mono_get_domainvar (MonoCompile *cfg)
1407 if (!cfg->domainvar)
1408 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1409 return cfg->domainvar;
1413 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
1416 int num = cfg->num_varinfo;
1418 if ((num + 1) >= cfg->varinfo_count) {
1419 cfg->varinfo_count = (cfg->varinfo_count + 2) * 2;
1420 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
1421 cfg->vars = (MonoMethodVar **)g_realloc (cfg->vars, sizeof (MonoMethodVar*) * cfg->varinfo_count);
1424 /*g_print ("created temp %d of type 0x%x\n", num, type->type);*/
1425 mono_jit_stats.allocate_var++;
1427 MONO_INST_NEW (cfg, inst, opcode);
1428 inst->inst_c0 = num;
1429 inst->inst_vtype = type;
1430 inst->klass = mono_class_from_mono_type (type);
1431 /* if set to 1 the variable is native */
1434 cfg->varinfo [num] = inst;
1436 cfg->vars [num] = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMethodVar));
1437 MONO_INIT_VARINFO (cfg->vars [num], num);
1440 //g_print ("created temp %d of type %s\n", num, mono_type_get_name (type));
1445 type_from_stack_type (MonoInst *ins) {
1446 switch (ins->type) {
1447 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1448 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1449 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1450 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1451 case STACK_MP: return &mono_defaults.int_class->byval_arg;
1452 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1453 case STACK_VTYPE: return &ins->klass->byval_arg;
1455 g_error ("stack type %d to montype not handled\n", ins->type);
1461 array_access_to_klass (int opcode)
1465 return mono_defaults.byte_class;
1467 return mono_defaults.uint16_class;
1470 return mono_defaults.int_class;
1473 return mono_defaults.sbyte_class;
1476 return mono_defaults.int16_class;
1479 return mono_defaults.int32_class;
1481 return mono_defaults.uint32_class;
1484 return mono_defaults.int64_class;
1487 return mono_defaults.single_class;
1490 return mono_defaults.double_class;
1491 case CEE_LDELEM_REF:
1492 case CEE_STELEM_REF:
1493 return mono_defaults.object_class;
1495 g_assert_not_reached ();
1501 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
1505 MONO_ADD_INS (bb, inst);
1508 switch (bb->last_ins->opcode) {
1521 while (prev->next && prev->next != bb->last_ins)
1523 if (prev == bb->code) {
1524 if (bb->last_ins == bb->code) {
1525 inst->next = bb->code;
1528 inst->next = prev->next;
1532 inst->next = bb->last_ins;
1536 // g_warning ("handle conditional jump in add_ins_to_end ()\n");
1538 MONO_ADD_INS (bb, inst);
1544 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
1546 MonoInst *inst, *load;
1548 NEW_TEMPLOAD (cfg, load, src);
1550 NEW_TEMPSTORE (cfg, inst, dest, load);
1551 if (inst->opcode == CEE_STOBJ) {
1552 NEW_TEMPLOADA (cfg, inst, dest);
1553 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE);
1555 inst->cil_code = NULL;
1556 mono_add_ins_to_end (bb, inst);
1561 * We try to share variables when possible
1564 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1569 /* inlining can result in deeper stacks */
1570 if (slot >= ((MonoMethodNormal *)cfg->method)->header->max_stack)
1571 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1573 pos = ins->type - 1 + slot * STACK_MAX;
1575 switch (ins->type) {
1582 if ((vnum = cfg->intvars [pos]))
1583 return cfg->varinfo [vnum];
1584 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1585 cfg->intvars [pos] = res->inst_c0;
1588 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1594 * This function is called to handle items that are left on the evaluation stack
1595 * at basic block boundaries. What happens is that we save the values to local variables
1596 * and we reload them later when first entering the target basic block (with the
1597 * handle_loaded_temps () function).
1598 * A single joint point will use the same variables (stored in the array bb->out_stack or
1599 * bb->in_stack, if the basic block is before or after the joint point).
1602 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count) {
1604 MonoBasicBlock *outb;
1605 MonoInst *inst, **locals;
1609 if (cfg->verbose_level > 3)
1610 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
1611 if (!bb->out_scount) {
1613 bb->out_scount = count;
1614 //g_print ("bblock %d has out:", bb->block_num);
1615 for (i = 0; i < bb->out_count; ++i) {
1616 outb = bb->out_bb [i];
1617 //g_print (" %d", outb->block_num);
1618 if (outb->in_stack) {
1620 bb->out_stack = outb->in_stack;
1626 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1627 for (i = 0; i < count; ++i) {
1629 /* try to reuse temps already allocated for this purpouse, if they occupy the same
1630 * stack slot and if they are of the same type. */
1631 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1633 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1638 locals = bb->out_stack;
1639 for (i = 0; i < count; ++i) {
1640 /* add store ops at the end of the bb, before the branch */
1641 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1642 if (inst->opcode == CEE_STOBJ) {
1643 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
1644 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE);
1646 inst->cil_code = sp [i]->cil_code;
1647 mono_add_ins_to_end (bb, inst);
1649 if (cfg->verbose_level > 3)
1650 g_print ("storing %d to temp %d\n", i, locals [i]->inst_c0);
1653 for (i = 0; i < bb->out_count; ++i) {
1654 outb = bb->out_bb [i];
1655 if (outb->in_scount)
1656 continue; /* check they are the same locals */
1657 outb->in_scount = count;
1658 outb->in_stack = locals;
1664 ret_type_to_call_opcode (MonoType *type, int calli, int virt)
1669 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1673 case MONO_TYPE_VOID:
1674 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
1677 case MONO_TYPE_BOOLEAN:
1680 case MONO_TYPE_CHAR:
1683 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1687 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1688 case MONO_TYPE_CLASS:
1689 case MONO_TYPE_STRING:
1690 case MONO_TYPE_OBJECT:
1691 case MONO_TYPE_SZARRAY:
1692 case MONO_TYPE_ARRAY:
1693 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1696 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
1699 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
1700 case MONO_TYPE_VALUETYPE:
1701 if (type->data.klass->enumtype) {
1702 t = type->data.klass->enum_basetype->type;
1705 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1706 case MONO_TYPE_TYPEDBYREF:
1707 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1709 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
1715 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
1717 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
1719 ji->ip.label = label;
1720 ji->type = MONO_PATCH_INFO_SWITCH;
1721 ji->data.table = bbs;
1722 ji->next = cfg->patch_info;
1723 ji->table_size = num_blocks;
1724 cfg->patch_info = ji;
1728 * When we add a tree of instructions, we need to ensure the instructions currently
1729 * on the stack are executed before (like, if we load a value from a local).
1730 * We ensure this by saving the currently loaded values to temps and rewriting the
1731 * instructions to load the values.
1732 * This is not done for opcodes that terminate a basic block (because it's handled already
1733 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
1736 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
1738 MonoInst *load, *store, *temp, *ins;
1740 while (stack < sp) {
1742 /* handle also other constants */
1743 if (ins->opcode != OP_ICONST) {
1744 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1745 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1746 store->cil_code = ins->cil_code;
1747 if (store->opcode == CEE_STOBJ) {
1748 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
1749 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE);
1751 MONO_ADD_INS (bblock, store);
1752 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1753 load->cil_code = ins->cil_code;
1761 * Prepare arguments for passing to a function call.
1762 * Return a non-zero value if the arguments can't be passed to the given
1764 * The type checks are not yet complete and some conversions may need
1765 * casts on 32 or 64 bit architectures.
1768 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
1773 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
1777 for (i = 0; i < sig->param_count; ++i) {
1778 if (sig->params [i]->byref) {
1780 * check the result of ldelema is only passed as an argument if the byref
1781 * type matches exactly the array element type.
1782 * FIXME: if the argument as been saved on the stack as part of the
1783 * interface variable code (the value was on the stack at a basic block boundary)
1784 * we need to add the check in that case, too.
1786 if (args [i]->opcode == CEE_LDELEMA) {
1788 MonoClass *exact_class = mono_class_from_mono_type (sig->params [i]);
1789 if (!exact_class->valuetype) {
1790 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
1791 check->cil_code = args [i]->cil_code;
1792 check->klass = exact_class;
1793 check->inst_left = args [i]->inst_left;
1794 check->type = STACK_OBJ;
1795 args [i]->inst_left = check;
1798 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
1802 simple_type = sig->params [i]->type;
1804 switch (simple_type) {
1805 case MONO_TYPE_VOID:
1810 case MONO_TYPE_BOOLEAN:
1813 case MONO_TYPE_CHAR:
1816 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
1822 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
1825 case MONO_TYPE_CLASS:
1826 case MONO_TYPE_STRING:
1827 case MONO_TYPE_OBJECT:
1828 case MONO_TYPE_SZARRAY:
1829 case MONO_TYPE_ARRAY:
1830 if (args [i]->type != STACK_OBJ)
1835 if (args [i]->type != STACK_I8)
1840 if (args [i]->type != STACK_R8)
1843 case MONO_TYPE_VALUETYPE:
1844 if (sig->params [i]->data.klass->enumtype) {
1845 simple_type = sig->params [i]->data.klass->enum_basetype->type;
1848 if (args [i]->type != STACK_VTYPE)
1851 case MONO_TYPE_TYPEDBYREF:
1852 if (args [i]->type != STACK_VTYPE)
1855 case MONO_TYPE_GENERICINST:
1856 simple_type = sig->params [i]->data.generic_inst->generic_type->type;
1860 g_error ("unknown type 0x%02x in check_call_signature", simple_type);
1867 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
1868 const guint8 *ip, gboolean to_end)
1870 MonoInst *temp, *store, *ins = (MonoInst*)call;
1871 MonoType *ret = sig->ret;
1873 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
1875 call->inst.type = STACK_OBJ;
1876 call->inst.opcode = CEE_CALL;
1877 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
1879 type_to_eval_stack_type (ret, ins);
1880 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
1883 if (MONO_TYPE_ISSTRUCT (ret)) {
1886 /* we use this to allocate native sized structs */
1887 temp->unused = sig->pinvoke;
1889 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
1890 if (call->inst.opcode == OP_VCALL)
1891 ins->inst_left = loada;
1893 ins->inst_right = loada; /* a virtual or indirect call */
1896 mono_add_ins_to_end (bblock, ins);
1898 MONO_ADD_INS (bblock, ins);
1900 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1901 store->cil_code = ip;
1903 mono_add_ins_to_end (bblock, store);
1905 MONO_ADD_INS (bblock, store);
1907 return temp->inst_c0;
1910 mono_add_ins_to_end (bblock, ins);
1912 MONO_ADD_INS (bblock, ins);
1917 inline static MonoCallInst *
1918 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1919 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
1924 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual));
1926 call->inst.cil_code = ip;
1928 call->signature = sig;
1929 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
1931 for (arg = call->out_args; arg;) {
1932 MonoInst *narg = arg->next;
1937 mono_add_ins_to_end (bblock, arg);
1939 MONO_ADD_INS (bblock, arg);
1946 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1947 MonoInst **args, MonoInst *addr, const guint8 *ip)
1949 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
1951 call->inst.inst_i0 = addr;
1953 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
1956 static MonoCallInst*
1957 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
1958 MonoInst **args, const guint8 *ip, MonoInst *this)
1960 gboolean virtual = this != NULL;
1963 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, FALSE);
1965 if (this && sig->hasthis &&
1966 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
1967 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
1968 call->method = mono_marshal_get_remoting_invoke_with_check (method);
1970 call->method = method;
1972 call->inst.flags |= MONO_INST_HAS_METHOD;
1973 call->inst.inst_left = this;
1979 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
1980 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
1982 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, signature, args, ip, this);
1984 return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
1988 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
1989 MonoInst **args, const guint8 *ip, gboolean to_end)
1995 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
1997 return mono_spill_call (cfg, bblock, call, sig, func == mono_array_new_va, ip, to_end);
2001 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
2003 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
2006 g_warning ("unregistered JIT ICall");
2007 g_assert_not_reached ();
2010 return mono_emit_native_call (cfg, bblock, info->wrapper, info->sig, args, ip, FALSE);
2014 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
2016 MonoInst *ins, *temp = NULL, *store, *load, *begin;
2017 MonoInst *last_arg = NULL;
2021 //g_print ("emulating: ");
2022 //mono_print_tree_nl (tree);
2023 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE));
2024 ins = (MonoInst*)call;
2026 call->inst.cil_code = tree->cil_code;
2028 call->signature = info->sig;
2030 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
2032 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2033 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
2034 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2035 store->cil_code = tree->cil_code;
2040 nargs = info->sig->param_count + info->sig->hasthis;
2042 for (last_arg = call->out_args; last_arg && last_arg->next; last_arg = last_arg->next) ;
2045 last_arg->next = store;
2048 begin = call->out_args;
2052 if (cfg->prev_ins) {
2054 * This assumes that that in a tree, emulate_opcode is called for a
2055 * node before it is called for its children. dec_foreach needs to
2056 * take this into account.
2058 store->next = cfg->prev_ins->next;
2059 cfg->prev_ins->next = begin;
2061 store->next = cfg->cbb->code;
2062 cfg->cbb->code = begin;
2065 call->fptr = info->wrapper;
2067 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2068 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2073 static MonoMethodSignature *
2074 mono_get_element_address_signature (int arity)
2076 static GHashTable *sighash = NULL;
2077 MonoMethodSignature *res;
2081 sighash = g_hash_table_new (NULL, NULL);
2084 if ((res = g_hash_table_lookup (sighash, (gpointer)arity)))
2087 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
2089 res->params [0] = &mono_defaults.array_class->byval_arg;
2091 for (i = 1; i <= arity; i++)
2092 res->params [i] = &mono_defaults.int_class->byval_arg;
2094 res->ret = &mono_defaults.int_class->byval_arg;
2096 g_hash_table_insert (sighash, (gpointer)arity, res);
2102 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native) {
2103 MonoInst *iargs [3];
2109 * This check breaks with spilled vars... need to handle it during verification anyway.
2110 * g_assert (klass && klass == src->klass && klass == dest->klass);
2114 n = mono_class_native_size (klass, &align);
2116 n = mono_class_value_size (klass, &align);
2118 if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
2120 MONO_INST_NEW (cfg, inst, OP_MEMCPY);
2121 inst->inst_left = dest;
2122 inst->inst_right = src;
2123 inst->cil_code = ip;
2125 MONO_ADD_INS (bblock, inst);
2130 NEW_ICONST (cfg, iargs [2], n);
2132 mono_emit_native_call (cfg, bblock, helper_memcpy, helper_sig_memcpy, iargs, ip, to_end);
2136 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
2138 MonoInst *iargs [2];
2139 MonoInst *ins, *zero_int32;
2142 NEW_ICONST (cfg, zero_int32, 0);
2144 mono_class_init (klass);
2145 n = mono_class_value_size (klass, NULL);
2146 MONO_INST_NEW (cfg, ins, 0);
2148 ins->inst_left = dest;
2149 ins->inst_right = zero_int32;
2152 ins->opcode = CEE_STIND_I1;
2153 MONO_ADD_INS (bblock, ins);
2156 ins->opcode = CEE_STIND_I2;
2157 MONO_ADD_INS (bblock, ins);
2160 ins->opcode = CEE_STIND_I4;
2161 MONO_ADD_INS (bblock, ins);
2164 if (n <= sizeof (gpointer) * 5) {
2165 ins->opcode = OP_MEMSET;
2168 MONO_ADD_INS (bblock, ins);
2171 handle_loaded_temps (cfg, bblock, stack_start, sp);
2172 NEW_ICONST (cfg, ins, n);
2175 mono_emit_jit_icall (cfg, bblock, helper_initobj, iargs, ip);
2180 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
2183 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
2185 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
2186 MonoMethodSignature *signature = method->signature;
2190 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2191 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2192 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
2193 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
2194 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2195 (method->klass->marshalbyref) ||
2196 !header || header->num_clauses ||
2197 /* fixme: why cant we inline valuetype returns? */
2198 MONO_TYPE_ISSTRUCT (signature->ret))
2201 /* its not worth to inline methods with valuetype arguments?? */
2202 for (i = 0; i < signature->param_count; i++) {
2203 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
2209 * if we can initialize the class of the method right away, we do,
2210 * otherwise we don't allow inlining if the class needs initialization,
2211 * since it would mean inserting a call to mono_runtime_class_init()
2212 * inside the inlined code
2214 if (!(cfg->opt & MONO_OPT_SHARED)) {
2215 vtable = mono_class_vtable (cfg->domain, method->klass);
2216 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
2217 if (cfg->run_cctors)
2218 mono_runtime_class_init (vtable);
2220 else if (!vtable->initialized && mono_class_needs_cctor_run (method->klass, NULL))
2224 * If we're compiling for shared code
2225 * the cctor will need to be run at aot method load time, for example,
2226 * or at the end of the compilation of the inlining method.
2228 if (mono_class_needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
2231 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
2233 /* also consider num_locals? */
2234 if (getenv ("MONO_INLINELIMIT"))
2235 if (header->code_size < atoi (getenv ("MONO_INLINELIMIT"))) {
2239 if (header->code_size < 20)
2246 mini_get_ldelema_ins (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
2250 MonoMethodSignature *esig;
2252 rank = cmethod->signature->param_count - (is_set? 1: 0);
2254 * FIXME: handle TypeMismatch for set or use the slow path
2257 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
2259 NEW_GROUP (cfg, indexes, sp [1], sp [2]);
2260 MONO_INST_NEW (cfg, addr, OP_LDELEMA2D);
2261 addr->inst_left = sp [0];
2262 addr->inst_right = indexes;
2263 addr->cil_code = ip;
2264 addr->type = STACK_MP;
2265 addr->klass = cmethod->klass;
2268 esig = mono_get_element_address_signature (rank);
2269 temp = mono_emit_native_call (cfg, bblock, ves_array_element_address, esig, sp, ip, FALSE);
2270 NEW_TEMPLOAD (cfg, addr, temp);
2275 mini_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
2280 if (cmethod->klass == mono_defaults.string_class) {
2281 if (cmethod->name [0] != 'g' || strcmp (cmethod->name, "get_Chars"))
2284 } else if (cmethod->klass == mono_defaults.math_class) {
2285 if (strcmp (cmethod->name, "Sin") == 0)
2287 else if (strcmp (cmethod->name, "Cos") == 0)
2289 else if (strcmp (cmethod->name, "Tan") == 0)
2291 else if (strcmp (cmethod->name, "Atan") == 0)
2293 else if (strcmp (cmethod->name, "Sqrt") == 0)
2295 else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8)
2298 /* OP_FREM is not IEEE compatible */
2299 else if (strcmp (cmethod->name, "IEEERemainder") == 0)
2304 } else if (cmethod->klass == mono_defaults.array_class) {
2305 if (strcmp (cmethod->name, "get_Rank") == 0)
2307 else if (strcmp (cmethod->name, "get_Length") == 0)
2314 pc = fsig->param_count + fsig->hasthis;
2315 MONO_INST_NEW (cfg, ins, op);
2318 ins->inst_i0 = args [0];
2320 ins->inst_i1 = args [1];
2327 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
2329 MonoInst *store, *temp;
2332 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
2334 if (!sig->hasthis && sig->param_count == 0)
2338 if (sp [0]->opcode == OP_ICONST) {
2341 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
2343 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2344 store->cil_code = sp [0]->cil_code;
2345 MONO_ADD_INS (bblock, store);
2350 for (i = 0; i < sig->param_count; ++i) {
2351 if (sp [0]->opcode == OP_ICONST) {
2354 temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
2356 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2357 store->cil_code = sp [0]->cil_code;
2358 if (store->opcode == CEE_STOBJ) {
2359 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2360 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE);
2362 MONO_ADD_INS (bblock, store);
2370 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
2371 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b)
2373 MonoInst *ins, *rvar = NULL;
2374 MonoMethodHeader *cheader;
2375 MonoBasicBlock *ebblock, *sbblock;
2376 int i, costs, new_locals_offset;
2378 if (cfg->verbose_level > 2)
2379 g_print ("INLINE START %p %s -> %s\n", cmethod, mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2381 cheader = ((MonoMethodNormal *)cmethod)->header;
2383 if (!cmethod->inline_info) {
2384 mono_jit_stats.inlineable_methods++;
2385 cmethod->inline_info = 1;
2387 /* allocate space to store the return value */
2388 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2389 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2392 /* allocate local variables */
2393 new_locals_offset = cfg->num_varinfo;
2394 for (i = 0; i < cheader->num_locals; ++i)
2395 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
2397 /* allocate starte and end blocks */
2398 sbblock = NEW_BBLOCK (cfg);
2399 sbblock->block_num = cfg->num_bblocks++;
2400 sbblock->real_offset = real_offset;
2402 ebblock = NEW_BBLOCK (cfg);
2403 ebblock->block_num = cfg->num_bblocks++;
2404 ebblock->real_offset = real_offset;
2406 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
2408 if (costs >= 0 && costs < 60) {
2409 if (cfg->verbose_level > 2)
2410 g_print ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2412 mono_jit_stats.inlined_methods++;
2414 /* always add some code to avoid block split failures */
2415 MONO_INST_NEW (cfg, ins, CEE_NOP);
2416 MONO_ADD_INS (bblock, ins);
2419 bblock->next_bb = sbblock;
2420 link_bblock (cfg, bblock, sbblock);
2423 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
2429 if (cfg->verbose_level > 2)
2430 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
2436 * Some of these comments may well be out-of-date.
2437 * Design decisions: we do a single pass over the IL code (and we do bblock
2438 * splitting/merging in the few cases when it's required: a back jump to an IL
2439 * address that was not already seen as bblock starting point).
2440 * Code is validated as we go (full verification is still better left to metadata/verify.c).
2441 * Complex operations are decomposed in simpler ones right away. We need to let the
2442 * arch-specific code peek and poke inside this process somehow (except when the
2443 * optimizations can take advantage of the full semantic info of coarse opcodes).
2444 * All the opcodes of the form opcode.s are 'normalized' to opcode.
2445 * MonoInst->opcode initially is the IL opcode or some simplification of that
2446 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
2447 * opcode with value bigger than OP_LAST.
2448 * At this point the IR can be handed over to an interpreter, a dumb code generator
2449 * or to the optimizing code generator that will translate it to SSA form.
2451 * Profiling directed optimizations.
2452 * We may compile by default with few or no optimizations and instrument the code
2453 * or the user may indicate what methods to optimize the most either in a config file
2454 * or through repeated runs where the compiler applies offline the optimizations to
2455 * each method and then decides if it was worth it.
2458 * * consider using an array instead of an hash table (bb_hash)
2461 #define CHECK_TYPE(ins) if (!(ins)->type) goto unverified
2462 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) goto unverified
2463 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) goto unverified
2464 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) goto unverified
2465 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) goto unverified
2466 #define CHECK_OPSIZE(size) if (ip + size > end) goto unverified
2468 #define TYPE_PARAM_TO_TYPE(num) (method->klass->generic_inst->data.generic_inst->type_argv [(num)])
2469 #define TYPE_PARAM_TO_CLASS(num) (mono_class_from_mono_type (TYPE_PARAM_TO_TYPE ((num))))
2471 #define MTYPE_PARAM_TO_TYPE(num) (((MonoMethodNormal *) method)->header->geninst->type_argv [(num)])
2472 #define MTYPE_PARAM_TO_CLASS(num) (mono_class_from_mono_type (MTYPE_PARAM_TO_TYPE ((num))))
2475 /* offset from br.s -> br like opcodes */
2476 #define BIG_BRANCH_OFFSET 13
2479 get_basic_blocks (MonoCompile *cfg, GHashTable *bbhash, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
2481 unsigned char *ip = start;
2482 unsigned char *target;
2485 MonoBasicBlock *bblock;
2486 const MonoOpcode *opcode;
2489 cli_addr = ip - start;
2490 i = mono_opcode_value ((const guint8 **)&ip);
2491 opcode = &mono_opcodes [i];
2492 switch (opcode->argument) {
2493 case MonoInlineNone:
2496 case MonoInlineString:
2497 case MonoInlineType:
2498 case MonoInlineField:
2499 case MonoInlineMethod:
2502 case MonoShortInlineR:
2509 case MonoShortInlineVar:
2510 case MonoShortInlineI:
2513 case MonoShortInlineBrTarget:
2514 target = start + cli_addr + 2 + (signed char)ip [1];
2515 GET_BBLOCK (cfg, bbhash, bblock, target);
2518 case MonoInlineBrTarget:
2519 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
2520 GET_BBLOCK (cfg, bbhash, bblock, target);
2523 case MonoInlineSwitch: {
2524 guint32 n = read32 (ip + 1);
2527 cli_addr += 5 + 4 * n;
2528 target = start + cli_addr;
2529 GET_BBLOCK (cfg, bbhash, bblock, target);
2531 for (j = 0; j < n; ++j) {
2532 target = start + cli_addr + (gint32)read32 (ip);
2533 GET_BBLOCK (cfg, bbhash, bblock, target);
2543 g_assert_not_reached ();
2552 static MonoClassField *
2553 get_generic_field_inst (MonoClassField *field, MonoClass *klass, MonoClass **retclass)
2556 for (i = 0; i < field->parent->field.count; ++i) {
2557 if (field == &field->parent->fields [i]) {
2559 return &klass->fields [i];
2565 static MonoClassField *
2566 inflate_generic_field (MonoClassField *field, MonoClass *klass, MonoClass **retclass)
2568 MonoGenericInst *ginst;
2569 MonoClassField *res;
2571 res = g_new0 (MonoClassField, 1);
2573 ginst = klass->generic_inst->data.generic_inst;
2574 res->type = mono_class_inflate_generic_type (field->type, ginst);
2579 * mono_method_to_ir: translates IL into basic blocks containing trees
2582 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
2583 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
2584 guint inline_offset, gboolean is_virtual_call)
2586 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
2587 MonoInst *ins, **sp, **stack_start;
2588 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
2590 MonoMethod *cmethod;
2591 MonoInst **arg_array;
2592 MonoMethodHeader *header;
2594 guint32 token, ins_flag;
2596 unsigned char *ip, *end, *target, *err_pos;
2597 static double r8_0 = 0.0;
2598 MonoMethodSignature *sig;
2599 MonoType **param_types;
2600 GList *bb_recheck = NULL, *tmp;
2601 int i, n, start_new_bblock, align;
2602 int num_calls = 0, inline_costs = 0;
2603 int *filter_lengths = NULL;
2604 int breakpoint_id = 0;
2605 guint real_offset, num_args;
2607 image = method->klass->image;
2608 header = ((MonoMethodNormal *)method)->header;
2609 sig = method->signature;
2610 num_args = sig->hasthis + sig->param_count;
2611 ip = (unsigned char*)header->code;
2612 end = ip + header->code_size;
2613 mono_jit_stats.cil_code_size += header->code_size;
2615 if (cfg->method == method) {
2617 bbhash = cfg->bb_hash;
2619 real_offset = inline_offset;
2620 bbhash = g_hash_table_new (g_direct_hash, NULL);
2623 if (cfg->verbose_level > 2)
2624 g_print ("method to IR %s\n", mono_method_full_name (method, TRUE));
2626 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
2627 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
2629 dont_inline = g_list_prepend (dont_inline, method);
2630 if (cfg->method == method) {
2633 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
2634 start_bblock->cil_code = NULL;
2635 start_bblock->cil_length = 0;
2636 start_bblock->block_num = cfg->num_bblocks++;
2639 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
2640 end_bblock->cil_code = NULL;
2641 end_bblock->cil_length = 0;
2642 end_bblock->block_num = cfg->num_bblocks++;
2643 g_assert (cfg->num_bblocks == 2);
2645 arg_array = alloca (sizeof (MonoInst *) * num_args);
2646 for (i = num_args - 1; i >= 0; i--)
2647 arg_array [i] = cfg->varinfo [i];
2649 if (header->num_clauses) {
2650 int size = sizeof (int) * header->num_clauses;
2651 filter_lengths = alloca (size);
2652 memset (filter_lengths, 0, size);
2654 cfg->spvars = g_hash_table_new (NULL, NULL);
2656 /* handle exception clauses */
2657 for (i = 0; i < header->num_clauses; ++i) {
2658 //unsigned char *p = ip;
2659 MonoExceptionClause *clause = &header->clauses [i];
2660 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->try_offset);
2661 tblock->real_offset = clause->try_offset;
2662 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
2663 tblock->real_offset = clause->handler_offset;
2665 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
2666 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2667 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2668 MONO_ADD_INS (tblock, ins);
2671 /*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);
2673 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
2675 /* catch and filter blocks get the exception object on the stack */
2676 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
2677 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2678 /* mostly like handle_stack_args (), but just sets the input args */
2679 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
2681 cfg->exvar = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
2682 /* prevent it from being register allocated */
2683 cfg->exvar->flags |= MONO_INST_INDIRECT;
2685 tblock->in_scount = 1;
2686 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2687 tblock->in_stack [0] = cfg->exvar;
2689 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2690 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->token_or_filter);
2691 tblock->real_offset = clause->token_or_filter;
2692 tblock->in_scount = 1;
2693 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2694 tblock->in_stack [0] = cfg->exvar;
2695 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2696 MONO_ADD_INS (tblock, ins);
2702 arg_array = alloca (sizeof (MonoInst *) * num_args);
2703 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
2706 /* FIRST CODE BLOCK */
2707 bblock = NEW_BBLOCK (cfg);
2708 bblock->cil_code = ip;
2710 ADD_BBLOCK (cfg, bbhash, bblock);
2712 if (cfg->method == method) {
2713 breakpoint_id = mono_debugger_method_has_breakpoint (method);
2714 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
2715 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2716 MONO_ADD_INS (bblock, ins);
2720 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED))) || mono_compile_aot) {
2721 /* we use a separate basic block for the initialization code */
2722 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
2723 init_localsbb->real_offset = real_offset;
2724 start_bblock->next_bb = init_localsbb;
2725 init_localsbb->next_bb = bblock;
2726 link_bblock (cfg, start_bblock, init_localsbb);
2727 link_bblock (cfg, init_localsbb, bblock);
2728 init_localsbb->block_num = cfg->num_bblocks++;
2730 start_bblock->next_bb = bblock;
2731 link_bblock (cfg, start_bblock, bblock);
2734 if (get_basic_blocks (cfg, bbhash, header, real_offset, ip, end, &err_pos)) {
2739 mono_debug_init_method (cfg, bblock, breakpoint_id);
2741 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
2743 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
2744 for (n = 0; n < sig->param_count; ++n)
2745 param_types [n + sig->hasthis] = sig->params [n];
2747 /* do this somewhere outside - not here */
2748 NEW_ICONST (cfg, zero_int32, 0);
2749 NEW_ICONST (cfg, zero_int64, 0);
2750 zero_int64->type = STACK_I8;
2751 NEW_PCONST (cfg, zero_ptr, 0);
2752 NEW_PCONST (cfg, zero_obj, 0);
2753 zero_obj->type = STACK_OBJ;
2755 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
2756 zero_r8->type = STACK_R8;
2757 zero_r8->inst_p0 = &r8_0;
2759 /* add a check for this != NULL to inlined methods */
2760 if (is_virtual_call) {
2761 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
2762 NEW_ARGLOAD (cfg, ins->inst_left, 0);
2764 MONO_ADD_INS (bblock, ins);
2767 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
2768 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
2771 start_new_bblock = 0;
2774 if (cfg->method == method)
2775 real_offset = ip - header->code;
2777 real_offset = inline_offset;
2779 if (start_new_bblock) {
2780 bblock->cil_length = ip - bblock->cil_code;
2781 if (start_new_bblock == 2) {
2782 g_assert (ip == tblock->cil_code);
2784 GET_BBLOCK (cfg, bbhash, tblock, ip);
2786 bblock->next_bb = tblock;
2788 start_new_bblock = 0;
2789 for (i = 0; i < bblock->in_scount; ++i) {
2790 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2794 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
2795 link_bblock (cfg, bblock, tblock);
2796 if (sp != stack_start) {
2797 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2800 bblock->next_bb = tblock;
2802 for (i = 0; i < bblock->in_scount; ++i) {
2803 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2809 if (cfg->coverage_info) {
2810 MonoInst *store, *one;
2811 guint32 cil_offset = ip - header->code;
2812 cfg->coverage_info->data [cil_offset].cil_code = ip;
2814 /* TODO: Use an increment here */
2815 NEW_ICONST (cfg, one, 1);
2818 NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
2821 MONO_INST_NEW (cfg, store, CEE_STIND_I);
2822 store->cil_code = ip;
2823 store->inst_left = ins;
2824 store->inst_right = one;
2826 MONO_ADD_INS (bblock, store);
2829 if (cfg->verbose_level > 3)
2830 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, sp-stack_start, mono_disasm_code_one (NULL, method, ip, NULL));
2837 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2838 ins->cil_code = ip++;
2839 MONO_ADD_INS (bblock, ins);
2845 CHECK_STACK_OVF (1);
2846 n = (*ip)-CEE_LDARG_0;
2848 NEW_ARGLOAD (cfg, ins, n);
2849 ins->cil_code = ip++;
2856 CHECK_STACK_OVF (1);
2857 n = (*ip)-CEE_LDLOC_0;
2859 NEW_LOCLOAD (cfg, ins, n);
2860 ins->cil_code = ip++;
2868 n = (*ip)-CEE_STLOC_0;
2871 handle_loaded_temps (cfg, bblock, stack_start, sp);
2872 NEW_LOCSTORE (cfg, ins, n, *sp);
2874 if (ins->opcode == CEE_STOBJ) {
2875 NEW_LOCLOADA (cfg, ins, n);
2876 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2878 MONO_ADD_INS (bblock, ins);
2884 CHECK_STACK_OVF (1);
2886 NEW_ARGLOAD (cfg, ins, ip [1]);
2893 CHECK_STACK_OVF (1);
2895 NEW_ARGLOADA (cfg, ins, ip [1]);
2905 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
2906 handle_loaded_temps (cfg, bblock, stack_start, sp);
2908 if (ins->opcode == CEE_STOBJ) {
2909 NEW_ARGLOADA (cfg, ins, ip [1]);
2910 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2912 MONO_ADD_INS (bblock, ins);
2917 CHECK_STACK_OVF (1);
2918 CHECK_LOCAL (ip [1]);
2919 NEW_LOCLOAD (cfg, ins, ip [1]);
2926 CHECK_STACK_OVF (1);
2927 CHECK_LOCAL (ip [1]);
2928 NEW_LOCLOADA (cfg, ins, ip [1]);
2937 handle_loaded_temps (cfg, bblock, stack_start, sp);
2938 CHECK_LOCAL (ip [1]);
2939 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
2941 if (ins->opcode == CEE_STOBJ) {
2942 NEW_LOCLOADA (cfg, ins, ip [1]);
2943 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2945 MONO_ADD_INS (bblock, ins);
2950 CHECK_STACK_OVF (1);
2951 NEW_PCONST (cfg, ins, NULL);
2953 ins->type = STACK_OBJ;
2958 CHECK_STACK_OVF (1);
2959 NEW_ICONST (cfg, ins, -1);
2973 CHECK_STACK_OVF (1);
2974 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
2981 CHECK_STACK_OVF (1);
2983 NEW_ICONST (cfg, ins, *((signed char*)ip));
2990 CHECK_STACK_OVF (1);
2991 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
2998 CHECK_STACK_OVF (1);
2999 MONO_INST_NEW (cfg, ins, OP_I8CONST);
3001 ins->type = STACK_I8;
3003 ins->inst_l = (gint64)read64 (ip);
3008 float *f = g_malloc (sizeof (float));
3010 CHECK_STACK_OVF (1);
3011 MONO_INST_NEW (cfg, ins, OP_R4CONST);
3012 ins->type = STACK_R8;
3021 double *d = g_malloc (sizeof (double));
3023 CHECK_STACK_OVF (1);
3024 MONO_INST_NEW (cfg, ins, OP_R8CONST);
3025 ins->type = STACK_R8;
3034 MonoInst *temp, *store;
3036 CHECK_STACK_OVF (1);
3041 * small optimization: if the loaded value was from a local already,
3042 * just load it twice.
3044 if (ins->ssa_op == MONO_SSA_LOAD &&
3045 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
3047 MONO_INST_NEW (cfg, temp, 0);
3049 temp->cil_code = ip;
3052 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3053 temp->cil_code = ip;
3054 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3055 store->cil_code = ip;
3056 MONO_ADD_INS (bblock, store);
3057 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3060 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3070 MONO_INST_NEW (cfg, ins, CEE_POP);
3071 MONO_ADD_INS (bblock, ins);
3072 ins->cil_code = ip++;
3078 if (stack_start != sp)
3080 MONO_INST_NEW (cfg, ins, CEE_JMP);
3081 token = read32 (ip + 1);
3082 /* FIXME: check the signature matches */
3083 cmethod = mono_get_method (image, token, NULL);
3084 ins->inst_p0 = cmethod;
3085 MONO_ADD_INS (bblock, ins);
3087 start_new_bblock = 1;
3091 case CEE_CALLVIRT: {
3092 MonoInst *addr = NULL;
3093 MonoMethodSignature *fsig = NULL;
3094 int temp, array_rank = 0;
3095 int virtual = *ip == CEE_CALLVIRT;
3098 token = read32 (ip + 1);
3100 if (*ip == CEE_CALLI) {
3105 if (method->wrapper_type != MONO_WRAPPER_NONE)
3106 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
3108 fsig = mono_metadata_parse_signature (image, token);
3110 n = fsig->param_count + fsig->hasthis;
3112 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3113 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
3115 cmethod = mono_get_method (image, token, NULL);
3118 if (!cmethod->klass->inited)
3119 mono_class_init (cmethod->klass);
3121 if (cmethod->signature->pinvoke) {
3122 #ifdef MONO_USE_EXC_TABLES
3123 if (mono_method_blittable (cmethod)) {
3124 fsig = cmethod->signature;
3127 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
3128 fsig = wrapper->signature;
3129 #ifdef MONO_USE_EXC_TABLES
3133 fsig = mono_method_get_signature (cmethod, image, token);
3136 n = fsig->param_count + fsig->hasthis;
3138 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
3139 cmethod->klass->parent == mono_defaults.array_class) {
3140 array_rank = cmethod->klass->rank;
3143 if (cmethod->string_ctor)
3144 g_assert_not_reached ();
3150 //g_assert (!virtual || fsig->hasthis);
3154 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
3157 if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL) && (mono_metadata_signature_equal (method->signature, cmethod->signature))) {
3159 /* FIXME: This assumes the two methods has the same number and type of arguments */
3160 for (i = 0; i < n; ++i) {
3161 /* Check if argument is the same */
3162 NEW_ARGLOAD (cfg, ins, i);
3163 if ((ins->opcode == sp [i]->opcode) && (ins->inst_i0 == sp [i]->inst_i0))
3166 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3168 if (ins->opcode == CEE_STOBJ) {
3169 NEW_ARGLOADA (cfg, ins, i);
3170 handle_stobj (cfg, bblock, ins, sp [i], sp [i]->cil_code, ins->klass, FALSE, FALSE);
3173 MONO_ADD_INS (bblock, ins);
3175 MONO_INST_NEW (cfg, ins, CEE_JMP);
3177 ins->inst_p0 = cmethod;
3178 ins->inst_p1 = arg_array [0];
3179 MONO_ADD_INS (bblock, ins);
3180 start_new_bblock = 1;
3181 /* skip CEE_RET as well */
3186 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_opcode_for_method (cfg, cmethod, fsig, sp))) {
3189 if (MONO_TYPE_IS_VOID (fsig->ret)) {
3190 MONO_ADD_INS (bblock, ins);
3192 type_to_eval_stack_type (fsig->ret, ins);
3201 handle_loaded_temps (cfg, bblock, stack_start, sp);
3203 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3204 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
3205 mono_method_check_inlining (cfg, cmethod) &&
3206 !g_list_find (dont_inline, cmethod)) {
3208 MonoBasicBlock *ebblock;
3210 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3214 GET_BBLOCK (cfg, bbhash, bblock, ip);
3215 ebblock->next_bb = bblock;
3216 link_bblock (cfg, ebblock, bblock);
3218 if (!MONO_TYPE_IS_VOID (fsig->ret))
3221 /* indicates start of a new block, and triggers a load of all
3222 stack arguments at bb boundarie */
3225 inline_costs += costs;
3230 inline_costs += 10 * num_calls++;
3232 /* tail recursion elimination */
3233 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == method && ip [5] == CEE_RET) {
3234 gboolean has_vtargs = FALSE;
3237 /* keep it simple */
3238 for (i = fsig->param_count - 1; i >= 0; i--) {
3239 if (MONO_TYPE_ISSTRUCT (cmethod->signature->params [i]))
3244 for (i = 0; i < n; ++i) {
3245 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3247 MONO_ADD_INS (bblock, ins);
3249 MONO_INST_NEW (cfg, ins, CEE_BR);
3251 MONO_ADD_INS (bblock, ins);
3252 tblock = start_bblock->out_bb [0];
3253 link_bblock (cfg, bblock, tblock);
3254 ins->inst_target_bb = tblock;
3255 start_new_bblock = 1;
3258 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3259 /* just create a dummy - the value is never used */
3260 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
3261 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3269 if (*ip == CEE_CALLI) {
3271 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
3272 NEW_TEMPLOAD (cfg, *sp, temp);
3276 } else if (array_rank) {
3279 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
3280 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
3281 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
3283 if (ins->opcode == CEE_STOBJ) {
3284 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
3286 MONO_ADD_INS (bblock, ins);
3289 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
3290 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3291 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
3295 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
3296 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3299 g_assert_not_reached ();
3303 if (0 && CODE_IS_STLOC (ip + 5) && (!MONO_TYPE_ISSTRUCT (fsig->ret)) && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)) {
3304 /* no need to spill */
3305 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
3308 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) {
3309 NEW_TEMPLOAD (cfg, *sp, temp);
3319 if (cfg->method != method) {
3320 /* return from inlined methode */
3325 //g_assert (returnvar != -1);
3326 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
3327 store->cil_code = sp [0]->cil_code;
3328 if (store->opcode == CEE_STOBJ) {
3329 g_assert_not_reached ();
3330 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
3331 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
3333 MONO_ADD_INS (bblock, store);
3337 g_assert (!return_var);
3340 MONO_INST_NEW (cfg, ins, CEE_NOP);
3341 ins->opcode = mono_type_to_stind (method->signature->ret);
3342 if (ins->opcode == CEE_STOBJ) {
3343 NEW_RETLOADA (cfg, ins);
3344 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3346 ins->opcode = OP_SETRET;
3348 ins->inst_i0 = *sp;;
3349 ins->inst_i1 = NULL;
3350 MONO_ADD_INS (bblock, ins);
3354 if (sp != stack_start)
3356 MONO_INST_NEW (cfg, ins, CEE_BR);
3357 ins->cil_code = ip++;
3358 ins->inst_target_bb = end_bblock;
3359 MONO_ADD_INS (bblock, ins);
3360 link_bblock (cfg, bblock, end_bblock);
3361 start_new_bblock = 1;
3365 MONO_INST_NEW (cfg, ins, CEE_BR);
3366 ins->cil_code = ip++;
3367 MONO_ADD_INS (bblock, ins);
3368 target = ip + 1 + (signed char)(*ip);
3370 GET_BBLOCK (cfg, bbhash, tblock, target);
3371 link_bblock (cfg, bblock, tblock);
3372 CHECK_BBLOCK (target, ip, tblock);
3373 ins->inst_target_bb = tblock;
3374 if (sp != stack_start) {
3375 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3378 start_new_bblock = 1;
3385 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3386 ins->cil_code = ip++;
3387 target = ip + 1 + *(signed char*)ip;
3389 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
3390 if (sp != stack_start) {
3391 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3408 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3409 ins->cil_code = ip++;
3410 target = ip + 1 + *(signed char*)ip;
3413 if (sp != stack_start) {
3414 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3421 MONO_INST_NEW (cfg, ins, CEE_BR);
3422 ins->cil_code = ip++;
3423 MONO_ADD_INS (bblock, ins);
3424 target = ip + 4 + (gint32)read32(ip);
3426 GET_BBLOCK (cfg, bbhash, tblock, target);
3427 link_bblock (cfg, bblock, tblock);
3428 CHECK_BBLOCK (target, ip, tblock);
3429 ins->inst_target_bb = tblock;
3430 if (sp != stack_start) {
3431 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3434 start_new_bblock = 1;
3441 MONO_INST_NEW (cfg, ins, *ip);
3442 ins->cil_code = ip++;
3443 target = ip + 4 + (gint32)read32(ip);
3445 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
3446 if (sp != stack_start) {
3447 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3464 MONO_INST_NEW (cfg, ins, *ip);
3465 ins->cil_code = ip++;
3466 target = ip + 4 + (gint32)read32(ip);
3469 if (sp != stack_start) {
3470 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3478 n = read32 (ip + 1);
3479 MONO_INST_NEW (cfg, ins, *ip);
3481 ins->inst_left = *sp;
3482 if (ins->inst_left->type != STACK_I4) goto unverified;
3485 CHECK_OPSIZE (n * sizeof (guint32));
3486 target = ip + n * sizeof (guint32);
3487 MONO_ADD_INS (bblock, ins);
3488 GET_BBLOCK (cfg, bbhash, tblock, target);
3489 link_bblock (cfg, bblock, tblock);
3490 ins->klass = GUINT_TO_POINTER (n);
3491 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
3492 ins->inst_many_bb [n] = tblock;
3494 for (i = 0; i < n; ++i) {
3495 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
3496 link_bblock (cfg, bblock, tblock);
3497 ins->inst_many_bb [i] = tblock;
3500 /* FIXME: handle stack args */
3515 MONO_INST_NEW (cfg, ins, *ip);
3520 ins->type = ldind_type [*ip - CEE_LDIND_I1];
3521 ins->flags |= ins_flag;
3533 MONO_INST_NEW (cfg, ins, *ip);
3534 ins->cil_code = ip++;
3536 handle_loaded_temps (cfg, bblock, stack_start, sp);
3537 MONO_ADD_INS (bblock, ins);
3538 ins->inst_i0 = sp [0];
3539 ins->inst_i1 = sp [1];
3540 ins->flags |= ins_flag;
3571 case CEE_CONV_OVF_I8:
3572 case CEE_CONV_OVF_U8:
3578 case CEE_CONV_OVF_I4:
3579 case CEE_CONV_OVF_I1:
3580 case CEE_CONV_OVF_I2:
3581 case CEE_CONV_OVF_I:
3582 case CEE_CONV_OVF_U:
3585 if (sp [-1]->type == STACK_R8) {
3586 ADD_UNOP (CEE_CONV_OVF_I8);
3594 case CEE_CONV_OVF_U1:
3595 case CEE_CONV_OVF_U2:
3596 case CEE_CONV_OVF_U4:
3599 if (sp [-1]->type == STACK_R8) {
3600 ADD_UNOP (CEE_CONV_OVF_U8);
3608 case CEE_CONV_OVF_I1_UN:
3609 case CEE_CONV_OVF_I2_UN:
3610 case CEE_CONV_OVF_I4_UN:
3611 case CEE_CONV_OVF_I8_UN:
3612 case CEE_CONV_OVF_U1_UN:
3613 case CEE_CONV_OVF_U2_UN:
3614 case CEE_CONV_OVF_U4_UN:
3615 case CEE_CONV_OVF_U8_UN:
3616 case CEE_CONV_OVF_I_UN:
3617 case CEE_CONV_OVF_U_UN:
3625 token = read32 (ip + 1);
3626 if (method->wrapper_type != MONO_WRAPPER_NONE)
3627 klass = mono_method_get_wrapper_data (method, token);
3629 klass = mono_class_get (image, token);
3631 mono_class_init (klass);
3632 if (klass->byval_arg.type == MONO_TYPE_VAR)
3633 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3634 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
3635 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3637 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3638 MonoInst *store, *load;
3639 MONO_INST_NEW (cfg, load, CEE_LDIND_REF);
3640 load->cil_code = ip;
3641 load->inst_i0 = sp [1];
3642 load->type = ldind_type [CEE_LDIND_REF];
3643 load->flags |= ins_flag;
3644 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
3645 store->cil_code = ip;
3646 handle_loaded_temps (cfg, bblock, stack_start, sp);
3647 MONO_ADD_INS (bblock, store);
3648 store->inst_i0 = sp [0];
3649 store->inst_i1 = load;
3650 store->flags |= ins_flag;
3652 n = mono_class_value_size (klass, NULL);
3653 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3655 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3656 copy->inst_left = sp [0];
3657 copy->inst_right = sp [1];
3658 copy->cil_code = ip;
3660 MONO_ADD_INS (bblock, copy);
3662 MonoInst *iargs [3];
3665 NEW_ICONST (cfg, iargs [2], n);
3666 iargs [2]->cil_code = ip;
3668 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3675 MonoInst *iargs [3];
3679 token = read32 (ip + 1);
3680 if (method->wrapper_type != MONO_WRAPPER_NONE)
3681 klass = mono_method_get_wrapper_data (method, token);
3683 klass = mono_class_get (image, token);
3685 mono_class_init (klass);
3686 if (klass->byval_arg.type == MONO_TYPE_VAR)
3687 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3688 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
3689 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3690 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3691 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3693 ins->inst_i0 = sp [0];
3694 ins->type = ldind_type [CEE_LDIND_REF];
3695 ins->flags |= ins_flag;
3701 n = mono_class_value_size (klass, NULL);
3702 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3703 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3704 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3706 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3707 copy->inst_left = iargs [0];
3708 copy->inst_right = *sp;
3709 copy->cil_code = ip;
3711 MONO_ADD_INS (bblock, copy);
3714 NEW_ICONST (cfg, iargs [2], n);
3715 iargs [2]->cil_code = ip;
3717 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3719 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3727 CHECK_STACK_OVF (1);
3729 n = read32 (ip + 1);
3731 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3733 MonoInst *iargs [1];
3735 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
3736 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
3737 NEW_TEMPLOAD (cfg, *sp, temp);
3741 if (cfg->opt & MONO_OPT_SHARED) {
3743 MonoInst *iargs [3];
3745 if (mono_compile_aot) {
3746 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n);
3749 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3750 NEW_IMAGECONST (cfg, iargs [1], image);
3751 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
3752 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
3753 NEW_TEMPLOAD (cfg, *sp, temp);
3754 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3756 if (mono_compile_aot)
3757 NEW_LDSTRCONST (cfg, ins, image, n);
3759 NEW_PCONST (cfg, ins, NULL);
3761 ins->type = STACK_OBJ;
3762 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3772 MonoInst *iargs [2];
3773 MonoMethodSignature *fsig;
3777 token = read32 (ip + 1);
3778 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3779 cmethod = mono_method_get_wrapper_data (method, token);
3781 cmethod = mono_get_method (image, token, NULL);
3782 fsig = mono_method_get_signature (cmethod, image, token);
3784 mono_class_init (cmethod->klass);
3786 n = fsig->param_count;
3789 /* move the args to allow room for 'this' in the first position */
3795 handle_loaded_temps (cfg, bblock, stack_start, sp);
3798 if (cmethod->klass->parent == mono_defaults.array_class) {
3799 NEW_METHODCONST (cfg, *sp, cmethod);
3800 temp = mono_emit_native_call (cfg, bblock, mono_array_new_va, fsig, sp, ip, FALSE);
3802 } else if (cmethod->string_ctor) {
3803 /* we simply pass a null pointer */
3804 NEW_PCONST (cfg, *sp, NULL);
3805 /* now call the string ctor */
3806 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
3808 if (cmethod->klass->valuetype) {
3809 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
3810 temp = iargs [0]->inst_c0;
3811 NEW_TEMPLOADA (cfg, *sp, temp);
3813 if (cfg->opt & MONO_OPT_SHARED) {
3814 NEW_DOMAINCONST (cfg, iargs [0]);
3815 NEW_CLASSCONST (cfg, iargs [1], cmethod->klass);
3817 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3819 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
3820 NEW_VTABLECONST (cfg, iargs [0], vtable);
3821 if (cmethod->klass->has_finalize || cmethod->klass->marshalbyref || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
3822 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
3824 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
3826 NEW_TEMPLOAD (cfg, *sp, temp);
3829 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3830 mono_method_check_inlining (cfg, cmethod) &&
3831 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
3832 !g_list_find (dont_inline, cmethod)) {
3834 MonoBasicBlock *ebblock;
3835 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3840 GET_BBLOCK (cfg, bbhash, bblock, ip);
3841 ebblock->next_bb = bblock;
3842 link_bblock (cfg, ebblock, bblock);
3844 NEW_TEMPLOAD (cfg, *sp, temp);
3847 /* indicates start of a new block, and triggers a load
3848 of all stack arguments at bb boundarie */
3851 inline_costs += costs;
3855 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3858 /* now call the actual ctor */
3859 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3863 NEW_TEMPLOAD (cfg, *sp, temp);
3874 klass = mono_class_get (image, read32 (ip + 1));
3875 mono_class_init (klass);
3877 if (klass->marshalbyref) {
3878 MonoInst *iargs [2];
3881 NEW_CLASSCONST (cfg, iargs [1], klass);
3882 temp = mono_emit_native_call (cfg, bblock, mono_object_isinst_mbyref, helper_sig_obj_obj_cls, iargs, ip, FALSE);
3883 NEW_TEMPLOAD (cfg, *sp, temp);
3887 MONO_INST_NEW (cfg, ins, *ip);
3888 ins->type = STACK_OBJ;
3889 ins->inst_left = *sp;
3890 ins->inst_newa_class = klass;
3896 case CEE_UNBOX_ANY: {
3897 MonoInst *add, *vtoffset;
3898 MonoInst *iargs [3];
3903 token = read32 (ip + 1);
3904 if (method->wrapper_type != MONO_WRAPPER_NONE)
3905 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3907 klass = mono_class_get (image, token);
3908 mono_class_init (klass);
3910 if (klass->byval_arg.type == MONO_TYPE_VAR)
3911 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3912 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
3913 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3915 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3917 if (klass->marshalbyref) {
3918 MonoInst *iargs [2];
3921 NEW_CLASSCONST (cfg, iargs [1], klass);
3922 temp = mono_emit_jit_icall (cfg, bblock, mono_object_castclass_mbyref, iargs, ip);
3923 NEW_TEMPLOAD (cfg, *sp, temp);
3927 MONO_INST_NEW (cfg, ins, CEE_CASTCLASS);
3928 ins->type = STACK_OBJ;
3929 ins->inst_left = *sp;
3931 ins->inst_newa_class = klass;
3939 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
3940 ins->type = STACK_OBJ;
3941 ins->inst_left = *sp;
3943 ins->inst_newa_class = klass;
3946 MONO_INST_NEW (cfg, add, CEE_ADD);
3947 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3948 add->inst_left = ins;
3949 add->inst_right = vtoffset;
3950 add->type = STACK_MP;
3954 n = mono_class_value_size (klass, NULL);
3955 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3956 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3957 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3959 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3960 copy->inst_left = iargs [0];
3961 copy->inst_right = *sp;
3962 copy->cil_code = ip;
3964 MONO_ADD_INS (bblock, copy);
3967 NEW_ICONST (cfg, iargs [2], n);
3968 iargs [2]->cil_code = ip;
3970 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3972 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3978 MonoInst *add, *vtoffset;
3983 token = read32 (ip + 1);
3984 if (method->wrapper_type != MONO_WRAPPER_NONE)
3985 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3987 klass = mono_class_get (image, token);
3988 mono_class_init (klass);
3990 if (klass->byval_arg.type == MONO_TYPE_VAR)
3991 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3992 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
3993 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3995 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
3996 ins->type = STACK_OBJ;
3997 ins->inst_left = *sp;
3999 ins->inst_newa_class = klass;
4002 MONO_INST_NEW (cfg, add, CEE_ADD);
4003 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4004 add->inst_left = ins;
4005 add->inst_right = vtoffset;
4006 add->type = STACK_MP;
4016 klass = mono_class_get (image, read32 (ip + 1));
4017 mono_class_init (klass);
4018 if (klass->byval_arg.type == MONO_TYPE_VAR)
4019 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4020 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4021 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4023 if (klass->marshalbyref) {
4024 MonoInst *iargs [2];
4027 NEW_CLASSCONST (cfg, iargs [1], klass);
4028 temp = mono_emit_jit_icall (cfg, bblock, mono_object_castclass_mbyref, iargs, ip);
4029 NEW_TEMPLOAD (cfg, *sp, temp);
4033 MONO_INST_NEW (cfg, ins, *ip);
4034 ins->type = STACK_OBJ;
4035 ins->inst_left = *sp;
4037 ins->inst_newa_class = klass;
4045 MONO_INST_NEW (cfg, ins, *ip);
4047 ins->inst_left = *sp;
4048 ins->cil_code = ip++;
4049 MONO_ADD_INS (bblock, ins);
4051 start_new_bblock = 1;
4056 MonoInst *offset_ins;
4057 MonoClassField *field;
4058 MonoBasicBlock *ebblock;
4062 if (*ip == CEE_STFLD) {
4069 // FIXME: enable this test later.
4070 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
4073 token = read32 (ip + 1);
4074 field = mono_field_from_token (image, token, &klass);
4075 if (field->parent->gen_params)
4076 field = get_generic_field_inst (field, method->klass, &klass);
4077 else if (field->parent->generic_inst && method->klass->generic_inst)
4078 field = inflate_generic_field (field, method->klass, &klass);
4079 mono_class_init (klass);
4081 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
4082 /* FIXME: mark instructions for use in SSA */
4083 if (*ip == CEE_STFLD) {
4084 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
4085 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
4086 MonoInst *iargs [5];
4089 NEW_CLASSCONST (cfg, iargs [1], klass);
4090 NEW_FIELDCONST (cfg, iargs [2], field);
4091 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
4095 if (cfg->opt & MONO_OPT_INLINE) {
4096 costs = inline_method (cfg, stfld_wrapper, stfld_wrapper->signature, bblock,
4097 iargs, ip, real_offset, dont_inline, &ebblock);
4098 g_assert (costs > 0);
4103 GET_BBLOCK (cfg, bbhash, bblock, ip);
4104 ebblock->next_bb = bblock;
4105 link_bblock (cfg, ebblock, bblock);
4107 /* indicates start of a new block, and triggers a load
4108 of all stack arguments at bb boundarie */
4111 inline_costs += costs;
4114 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, stfld_wrapper->signature, iargs, ip, NULL);
4118 NEW_ICONST (cfg, offset_ins, foffset);
4119 MONO_INST_NEW (cfg, ins, CEE_ADD);
4121 ins->inst_left = *sp;
4122 ins->inst_right = offset_ins;
4123 ins->type = STACK_MP;
4125 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4126 store->cil_code = ip;
4127 store->inst_left = ins;
4128 store->inst_right = sp [1];
4129 handle_loaded_temps (cfg, bblock, stack_start, sp);
4130 store->flags |= ins_flag;
4132 if (store->opcode == CEE_STOBJ) {
4133 handle_stobj (cfg, bblock, ins, sp [1], ip,
4134 mono_class_from_mono_type (field->type), FALSE, FALSE);
4136 MONO_ADD_INS (bblock, store);
4139 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
4140 /* fixme: we need to inline that call somehow */
4141 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
4142 MonoInst *iargs [4];
4146 NEW_CLASSCONST (cfg, iargs [1], klass);
4147 NEW_FIELDCONST (cfg, iargs [2], field);
4148 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
4149 if (cfg->opt & MONO_OPT_INLINE) {
4150 costs = inline_method (cfg, ldfld_wrapper, ldfld_wrapper->signature, bblock,
4151 iargs, ip, real_offset, dont_inline, &ebblock);
4152 g_assert (costs > 0);
4157 GET_BBLOCK (cfg, bbhash, bblock, ip);
4158 ebblock->next_bb = bblock;
4159 link_bblock (cfg, ebblock, bblock);
4161 temp = iargs [0]->inst_i0->inst_c0;
4163 if (*ip == CEE_LDFLDA) {
4164 /* not sure howto handle this */
4165 NEW_TEMPLOADA (cfg, *sp, temp);
4167 NEW_TEMPLOAD (cfg, *sp, temp);
4171 /* indicates start of a new block, and triggers a load of
4172 all stack arguments at bb boundarie */
4175 inline_costs += costs;
4178 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, ldfld_wrapper->signature, iargs, ip, NULL);
4179 if (*ip == CEE_LDFLDA) {
4180 /* not sure howto handle this */
4181 NEW_TEMPLOADA (cfg, *sp, temp);
4183 NEW_TEMPLOAD (cfg, *sp, temp);
4188 NEW_ICONST (cfg, offset_ins, foffset);
4189 MONO_INST_NEW (cfg, ins, CEE_ADD);
4191 ins->inst_left = *sp;
4192 ins->inst_right = offset_ins;
4193 ins->type = STACK_MP;
4195 if (*ip == CEE_LDFLDA) {
4199 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4200 type_to_eval_stack_type (field->type, load);
4201 load->cil_code = ip;
4202 load->inst_left = ins;
4203 load->flags |= ins_flag;
4215 MonoClassField *field;
4216 gpointer addr = NULL;
4219 token = read32 (ip + 1);
4221 field = mono_field_from_token (image, token, &klass);
4222 if (field->parent->gen_params)
4223 field = get_generic_field_inst (field, method->klass, &klass);
4224 else if (field->parent->generic_inst && method->klass->generic_inst)
4225 field = inflate_generic_field (field, method->klass, &klass);
4226 mono_class_init (klass);
4228 handle_loaded_temps (cfg, bblock, stack_start, sp);
4230 if (cfg->domain->special_static_fields)
4231 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
4233 if ((cfg->opt & MONO_OPT_SHARED) || (mono_compile_aot && addr)) {
4235 MonoInst *iargs [2];
4236 g_assert (field->parent);
4237 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
4238 NEW_FIELDCONST (cfg, iargs [1], field);
4239 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
4240 NEW_TEMPLOAD (cfg, ins, temp);
4243 vtable = mono_class_vtable (cfg->domain, klass);
4245 if ((!vtable->initialized || mono_compile_aot) && !(klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (klass, method)) {
4246 guint8 *tramp = mono_create_class_init_trampoline (vtable);
4247 mono_emit_native_call (cfg, bblock, tramp,
4248 helper_sig_class_init_trampoline,
4250 if (cfg->verbose_level > 2)
4251 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
4253 if (cfg->run_cctors)
4254 mono_runtime_class_init (vtable);
4256 addr = (char*)vtable->data + field->offset;
4258 if (mono_compile_aot)
4259 NEW_SFLDACONST (cfg, ins, field);
4261 NEW_PCONST (cfg, ins, addr);
4265 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
4266 * This could be later optimized to do just a couple of
4267 * memory dereferences with constant offsets.
4270 MonoInst *iargs [1];
4271 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
4272 temp = mono_emit_jit_icall (cfg, bblock, mono_get_special_static_data, iargs, ip);
4273 NEW_TEMPLOAD (cfg, ins, temp);
4277 /* FIXME: mark instructions for use in SSA */
4278 if (*ip == CEE_LDSFLDA) {
4280 } else if (*ip == CEE_STSFLD) {
4284 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4285 store->cil_code = ip;
4286 store->inst_left = ins;
4287 store->inst_right = sp [0];
4288 store->flags |= ins_flag;
4291 if (store->opcode == CEE_STOBJ) {
4292 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
4294 MONO_ADD_INS (bblock, store);
4296 gboolean is_const = FALSE;
4297 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4298 if (!((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) &&
4299 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
4300 gpointer addr = (char*)vtable->data + field->offset;
4301 /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
4303 switch (field->type->type) {
4304 case MONO_TYPE_BOOLEAN:
4306 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
4310 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
4313 case MONO_TYPE_CHAR:
4315 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
4319 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
4324 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
4328 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
4333 case MONO_TYPE_STRING:
4334 case MONO_TYPE_OBJECT:
4335 case MONO_TYPE_CLASS:
4336 case MONO_TYPE_SZARRAY:
4338 case MONO_TYPE_FNPTR:
4339 case MONO_TYPE_ARRAY:
4340 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
4341 type_to_eval_stack_type (field->type, *sp);
4346 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
4347 sp [0]->type = STACK_I8;
4348 sp [0]->inst_l = *((gint64 *)addr);
4353 case MONO_TYPE_VALUETYPE:
4362 CHECK_STACK_OVF (1);
4363 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4364 type_to_eval_stack_type (field->type, load);
4365 load->cil_code = ip;
4366 load->inst_left = ins;
4368 load->flags |= ins_flag;
4370 /* fixme: dont see the problem why this does not work */
4371 //cfg->disable_aot = TRUE;
4381 token = read32 (ip + 1);
4382 if (method->wrapper_type != MONO_WRAPPER_NONE)
4383 klass = mono_method_get_wrapper_data (method, token);
4385 klass = mono_class_get (image, token);
4386 mono_class_init (klass);
4387 if (klass->byval_arg.type == MONO_TYPE_VAR)
4388 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4389 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4390 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4391 n = mono_type_to_stind (&klass->byval_arg);
4392 if (n == CEE_STOBJ) {
4393 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
4395 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
4397 MONO_INST_NEW (cfg, store, n);
4398 store->cil_code = ip;
4399 store->inst_left = sp [0];
4400 store->inst_right = sp [1];
4401 store->flags |= ins_flag;
4402 MONO_ADD_INS (bblock, store);
4409 MonoInst *iargs [2];
4410 MonoInst *load, *vtoffset, *add, *val, *vstore;
4416 token = read32 (ip + 1);
4417 if (method->wrapper_type != MONO_WRAPPER_NONE)
4418 klass = mono_method_get_wrapper_data (method, token);
4420 klass = mono_class_get (image, token);
4421 mono_class_init (klass);
4422 if (klass->byval_arg.type == MONO_TYPE_VAR)
4423 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4424 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4425 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4427 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4432 /* much like NEWOBJ */
4433 if (cfg->opt & MONO_OPT_SHARED) {
4434 NEW_DOMAINCONST (cfg, iargs [0]);
4435 NEW_CLASSCONST (cfg, iargs [1], klass);
4437 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
4439 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4440 NEW_VTABLECONST (cfg, iargs [0], vtable);
4441 if (1 || klass->has_finalize || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
4442 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
4444 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
4446 NEW_TEMPLOAD (cfg, load, temp);
4447 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4448 MONO_INST_NEW (cfg, add, CEE_ADD);
4449 add->inst_left = load;
4450 add->inst_right = vtoffset;
4453 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
4454 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
4455 vstore->cil_code = ip;
4456 vstore->inst_left = add;
4457 vstore->inst_right = val;
4459 if (vstore->opcode == CEE_STOBJ) {
4460 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
4462 MONO_ADD_INS (bblock, vstore);
4464 NEW_TEMPLOAD (cfg, load, temp);
4472 MONO_INST_NEW (cfg, ins, *ip);
4477 token = read32 (ip + 1);
4479 /* allocate the domainvar - becaus this is used in decompose_foreach */
4480 if (cfg->opt & MONO_OPT_SHARED)
4481 mono_get_domainvar (cfg);
4483 if (method->wrapper_type != MONO_WRAPPER_NONE)
4484 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4486 klass = mono_class_get (image, token);
4488 mono_class_init (klass);
4489 if (klass->byval_arg.type == MONO_TYPE_VAR)
4490 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4491 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4492 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4493 ins->inst_newa_class = klass;
4494 ins->inst_newa_len = *sp;
4495 ins->type = STACK_OBJ;
4502 MONO_INST_NEW (cfg, ins, *ip);
4503 ins->cil_code = ip++;
4505 ins->inst_left = *sp;
4506 ins->type = STACK_PTR;
4514 if (method->wrapper_type != MONO_WRAPPER_NONE)
4515 klass = (MonoClass*)mono_method_get_wrapper_data (method, read32 (ip + 1));
4517 klass = mono_class_get (image, read32 (ip + 1));
4518 mono_class_init (klass);
4519 if (klass->byval_arg.type == MONO_TYPE_VAR)
4520 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4521 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4522 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4523 NEW_LDELEMA (cfg, ins, sp, klass);
4533 token = read32 (ip + 1);
4534 klass = mono_class_get (image, token);
4535 mono_class_init (klass);
4536 if (klass->byval_arg.type == MONO_TYPE_VAR)
4537 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4538 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4539 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4540 NEW_LDELEMA (cfg, load, sp, klass);
4541 load->cil_code = ip;
4542 MONO_INST_NEW (cfg, ins, mono_type_to_ldind (&klass->byval_arg));
4544 ins->inst_left = load;
4546 type_to_eval_stack_type (&klass->byval_arg, ins);
4560 case CEE_LDELEM_REF: {
4564 * ldind.x (ldelema (array, index))
4565 * ldelema does the bounds check
4569 klass = array_access_to_klass (*ip);
4570 NEW_LDELEMA (cfg, load, sp, klass);
4571 load->cil_code = ip;
4572 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
4574 ins->inst_left = load;
4576 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
4586 case CEE_STELEM_R8: {
4590 * stind.x (ldelema (array, index), val)
4591 * ldelema does the bounds check
4595 klass = array_access_to_klass (*ip);
4596 NEW_LDELEMA (cfg, load, sp, klass);
4597 load->cil_code = ip;
4598 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
4600 ins->inst_left = load;
4601 ins->inst_right = sp [2];
4603 handle_loaded_temps (cfg, bblock, stack_start, sp);
4604 MONO_ADD_INS (bblock, ins);
4606 cfg->disable_ssa = TRUE;
4613 * stind.x (ldelema (array, index), val)
4614 * ldelema does the bounds check
4619 token = read32 (ip + 1);
4620 klass = mono_class_get (image, token);
4621 mono_class_init (klass);
4622 if (klass->byval_arg.type == MONO_TYPE_VAR)
4623 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4624 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4625 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4626 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4627 MonoInst *iargs [3];
4628 handle_loaded_temps (cfg, bblock, stack_start, sp);
4634 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4636 NEW_LDELEMA (cfg, load, sp, klass);
4637 load->cil_code = ip;
4638 MONO_INST_NEW (cfg, ins, mono_type_to_stind (&klass->byval_arg));
4640 ins->inst_left = load;
4641 ins->inst_right = sp [2];
4642 handle_loaded_temps (cfg, bblock, stack_start, sp);
4643 MONO_ADD_INS (bblock, ins);
4647 cfg->disable_ssa = TRUE;
4650 case CEE_STELEM_REF: {
4651 MonoInst *iargs [3];
4656 handle_loaded_temps (cfg, bblock, stack_start, sp);
4662 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4666 NEW_GROUP (cfg, group, sp [0], sp [1]);
4667 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
4669 ins->inst_left = group;
4670 ins->inst_right = sp [2];
4671 MONO_ADD_INS (bblock, ins);
4676 cfg->disable_ssa = TRUE;
4679 case CEE_CKFINITE: {
4680 MonoInst *store, *temp;
4683 /* this instr. can throw exceptions as side effect,
4684 * so we cant eliminate dead code which contains CKFINITE opdodes.
4685 * Spilling to memory makes sure that we always perform
4689 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
4691 ins->inst_left = sp [-1];
4692 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
4694 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4695 store->cil_code = ip;
4696 MONO_ADD_INS (bblock, store);
4698 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
4705 MONO_INST_NEW (cfg, ins, *ip);
4708 klass = mono_class_get (image, read32 (ip + 1));
4709 mono_class_init (klass);
4710 if (klass->byval_arg.type == MONO_TYPE_VAR)
4711 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4712 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4713 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4714 ins->type = STACK_MP;
4715 ins->inst_left = *sp;
4717 ins->inst_newa_class = klass;
4722 case CEE_MKREFANY: {
4723 MonoInst *loc, *klassconst;
4726 MONO_INST_NEW (cfg, ins, *ip);
4729 klass = mono_class_get (image, read32 (ip + 1));
4730 mono_class_init (klass);
4731 if (klass->byval_arg.type == MONO_TYPE_VAR)
4732 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4733 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4734 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4737 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
4738 NEW_TEMPLOADA (cfg, ins->inst_right, loc->inst_c0);
4740 NEW_PCONST (cfg, klassconst, klass);
4741 NEW_GROUP (cfg, ins->inst_left, *sp, klassconst);
4743 MONO_ADD_INS (bblock, ins);
4745 NEW_TEMPLOAD (cfg, *sp, loc->inst_c0);
4752 MonoClass *handle_class;
4754 CHECK_STACK_OVF (1);
4757 n = read32 (ip + 1);
4759 handle = mono_ldtoken (image, n, &handle_class);
4760 mono_class_init (handle_class);
4762 if (cfg->opt & MONO_OPT_SHARED) {
4764 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
4766 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
4768 NEW_IMAGECONST (cfg, iargs [0], image);
4769 NEW_ICONST (cfg, iargs [1], n);
4770 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
4771 NEW_TEMPLOAD (cfg, res, temp);
4772 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4773 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
4774 MONO_ADD_INS (bblock, store);
4775 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4777 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method (image, read32 (ip + 6), NULL)) &&
4778 (cmethod->klass == mono_defaults.monotype_class->parent) &&
4779 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
4780 MonoClass *tclass = mono_class_from_mono_type (handle);
4781 mono_class_init (tclass);
4782 if (mono_compile_aot)
4783 NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n);
4785 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
4786 ins->type = STACK_OBJ;
4787 ins->klass = cmethod->klass;
4790 if (mono_compile_aot)
4791 NEW_LDTOKENCONST (cfg, ins, image, n);
4793 NEW_PCONST (cfg, ins, handle);
4794 ins->type = STACK_VTYPE;
4795 ins->klass = handle_class;
4811 case CEE_ADD_OVF_UN:
4813 case CEE_MUL_OVF_UN:
4815 case CEE_SUB_OVF_UN:
4820 case CEE_ENDFINALLY:
4821 /* FIXME: check stack state */
4822 MONO_INST_NEW (cfg, ins, *ip);
4823 MONO_ADD_INS (bblock, ins);
4824 ins->cil_code = ip++;
4825 start_new_bblock = 1;
4830 if (*ip == CEE_LEAVE) {
4832 target = ip + 5 + (gint32)read32(ip + 1);
4835 target = ip + 2 + (signed char)(ip [1]);
4838 /* empty the stack */
4839 while (sp != stack_start) {
4840 MONO_INST_NEW (cfg, ins, CEE_POP);
4844 MONO_ADD_INS (bblock, ins);
4848 * If this leave statement is in a catch block, check for a
4849 * pending exception, and rethrow it if necessary.
4851 for (i = 0; i < header->num_clauses; ++i) {
4852 MonoExceptionClause *clause = &header->clauses [i];
4853 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code)) {
4856 temp = mono_emit_jit_icall (cfg, bblock, mono_thread_get_pending_exception, NULL, ip);
4857 NEW_TEMPLOAD (cfg, *sp, temp);
4859 MONO_INST_NEW (cfg, ins, OP_THROW_OR_NULL);
4860 ins->inst_left = *sp;
4862 MONO_ADD_INS (bblock, ins);
4866 /* fixme: call fault handler ? */
4868 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
4870 for (tmp = handlers; tmp; tmp = tmp->next) {
4872 link_bblock (cfg, bblock, tblock);
4873 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
4875 ins->inst_target_bb = tblock;
4876 MONO_ADD_INS (bblock, ins);
4878 g_list_free (handlers);
4881 MONO_INST_NEW (cfg, ins, CEE_BR);
4883 MONO_ADD_INS (bblock, ins);
4884 GET_BBLOCK (cfg, bbhash, tblock, target);
4885 link_bblock (cfg, bblock, tblock);
4886 CHECK_BBLOCK (target, ip, tblock);
4887 ins->inst_target_bb = tblock;
4888 start_new_bblock = 1;
4890 if (*ip == CEE_LEAVE)
4899 MONO_INST_NEW (cfg, ins, *ip);
4901 handle_loaded_temps (cfg, bblock, stack_start, sp);
4902 MONO_ADD_INS (bblock, ins);
4903 ins->cil_code = ip++;
4904 ins->inst_i0 = sp [0];
4905 ins->inst_i1 = sp [1];
4913 /* trampoline mono specific opcodes */
4914 case MONO_CUSTOM_PREFIX: {
4916 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
4921 case CEE_MONO_FUNC1: {
4923 gpointer func = NULL;
4929 case MONO_MARSHAL_CONV_STR_LPWSTR:
4930 func = mono_string_to_utf16;
4932 case MONO_MARSHAL_CONV_LPWSTR_STR:
4933 func = mono_string_from_utf16;
4935 case MONO_MARSHAL_CONV_LPSTR_STR:
4936 func = mono_string_new_wrapper;
4938 case MONO_MARSHAL_CONV_STR_LPTSTR:
4939 case MONO_MARSHAL_CONV_STR_LPSTR:
4940 func = mono_string_to_utf8;
4942 case MONO_MARSHAL_CONV_STR_BSTR:
4943 func = mono_string_to_bstr;
4945 case MONO_MARSHAL_CONV_STR_TBSTR:
4946 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
4947 func = mono_string_to_ansibstr;
4949 case MONO_MARSHAL_CONV_SB_LPSTR:
4950 func = mono_string_builder_to_utf8;
4952 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
4953 func = mono_array_to_savearray;
4955 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
4956 func = mono_array_to_lparray;
4958 case MONO_MARSHAL_CONV_DEL_FTN:
4959 func = mono_delegate_to_ftnptr;
4961 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
4962 func = mono_marshal_string_array;
4965 g_warning ("unknown conversion %d\n", ip [2]);
4966 g_assert_not_reached ();
4969 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4970 NEW_TEMPLOAD (cfg, *sp, temp);
4974 inline_costs += 10 * num_calls++;
4977 case CEE_MONO_PROC2: {
4978 gpointer func = NULL;
4984 case MONO_MARSHAL_CONV_LPSTR_SB:
4985 func = mono_string_utf8_to_builder;
4987 case MONO_MARSHAL_FREE_ARRAY:
4988 func = mono_marshal_free_array;
4991 g_assert_not_reached ();
4994 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4996 inline_costs += 10 * num_calls++;
4999 case CEE_MONO_PROC3: {
5000 gpointer func = NULL;
5006 case MONO_MARSHAL_CONV_STR_BYVALSTR:
5007 func = mono_string_to_byvalstr;
5009 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
5010 func = mono_string_to_byvalwstr;
5013 g_assert_not_reached ();
5016 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5018 inline_costs += 10 * num_calls++;
5024 mono_emit_jit_icall (cfg, bblock, g_free, sp, ip);
5026 inline_costs += 10 * num_calls++;
5028 case CEE_MONO_LDPTR:
5029 CHECK_STACK_OVF (1);
5031 token = read32 (ip + 2);
5032 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
5036 inline_costs += 10 * num_calls++;
5038 case CEE_MONO_VTADDR:
5041 MONO_INST_NEW (cfg, ins, OP_VTADDR);
5043 ins->type = STACK_MP;
5044 ins->inst_left = *sp;
5048 case CEE_MONO_NEWOBJ: {
5049 MonoInst *iargs [2];
5051 CHECK_STACK_OVF (1);
5053 token = read32 (ip + 2);
5054 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5055 mono_class_init (klass);
5056 NEW_DOMAINCONST (cfg, iargs [0]);
5057 NEW_CLASSCONST (cfg, iargs [1], klass);
5058 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
5059 NEW_TEMPLOAD (cfg, *sp, temp);
5062 inline_costs += 10 * num_calls++;
5065 case CEE_MONO_OBJADDR:
5068 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
5070 ins->type = STACK_MP;
5071 ins->inst_left = *sp;
5075 case CEE_MONO_LDNATIVEOBJ:
5078 token = read32 (ip + 2);
5079 klass = mono_method_get_wrapper_data (method, token);
5080 g_assert (klass->valuetype);
5081 mono_class_init (klass);
5082 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
5086 case CEE_MONO_RETOBJ:
5087 g_assert (cfg->ret);
5088 g_assert (method->signature->pinvoke);
5093 token = read32 (ip + 2);
5094 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5096 NEW_RETLOADA (cfg, ins);
5097 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
5099 if (sp != stack_start)
5102 MONO_INST_NEW (cfg, ins, CEE_BR);
5104 ins->inst_target_bb = end_bblock;
5105 MONO_ADD_INS (bblock, ins);
5106 link_bblock (cfg, bblock, end_bblock);
5107 start_new_bblock = 1;
5111 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
5120 /* somewhat similar to LDTOKEN */
5121 MonoInst *addr, *vtvar;
5122 CHECK_STACK_OVF (1);
5123 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
5125 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5126 addr->cil_code = ip;
5127 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
5129 ins->inst_left = addr;
5130 MONO_ADD_INS (bblock, ins);
5131 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5144 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
5145 MONO_INST_NEW (cfg, ins, cmp->opcode);
5147 cmp->inst_i0 = sp [0];
5148 cmp->inst_i1 = sp [1];
5152 cmp->opcode = OP_COMPARE;
5154 ins->type = STACK_I4;
5164 CHECK_STACK_OVF (1);
5166 n = read32 (ip + 2);
5167 if (method->wrapper_type != MONO_WRAPPER_NONE)
5168 cmethod = mono_method_get_wrapper_data (method, n);
5170 cmethod = mono_get_method (image, n, NULL);
5173 * We can't do this in mono_ldftn, since it is used in
5174 * the synchronized wrapper, leading to an infinite loop.
5176 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
5177 cmethod = mono_marshal_get_synchronized_wrapper (cmethod);
5180 mono_class_init (cmethod->klass);
5181 handle_loaded_temps (cfg, bblock, stack_start, sp);
5183 NEW_METHODCONST (cfg, argconst, cmethod);
5184 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
5185 NEW_TEMPLOAD (cfg, *sp, temp);
5189 inline_costs += 10 * num_calls++;
5192 case CEE_LDVIRTFTN: {
5198 n = read32 (ip + 2);
5199 if (method->wrapper_type != MONO_WRAPPER_NONE)
5200 cmethod = mono_method_get_wrapper_data (method, n);
5202 cmethod = mono_get_method (image, n, NULL);
5204 mono_class_init (cmethod->klass);
5205 handle_loaded_temps (cfg, bblock, stack_start, sp);
5209 NEW_METHODCONST (cfg, args [1], cmethod);
5210 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
5211 NEW_TEMPLOAD (cfg, *sp, temp);
5215 inline_costs += 10 * num_calls++;
5219 CHECK_STACK_OVF (1);
5221 n = read16 (ip + 2);
5223 NEW_ARGLOAD (cfg, ins, n);
5229 CHECK_STACK_OVF (1);
5231 n = read16 (ip + 2);
5233 NEW_ARGLOADA (cfg, ins, n);
5241 handle_loaded_temps (cfg, bblock, stack_start, sp);
5243 n = read16 (ip + 2);
5245 NEW_ARGSTORE (cfg, ins, n, *sp);
5247 if (ins->opcode == CEE_STOBJ) {
5248 NEW_ARGLOADA (cfg, ins, n);
5249 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5251 MONO_ADD_INS (bblock, ins);
5255 CHECK_STACK_OVF (1);
5257 n = read16 (ip + 2);
5259 NEW_LOCLOAD (cfg, ins, n);
5265 CHECK_STACK_OVF (1);
5267 n = read16 (ip + 2);
5269 NEW_LOCLOADA (cfg, ins, n);
5278 n = read16 (ip + 2);
5280 handle_loaded_temps (cfg, bblock, stack_start, sp);
5281 NEW_LOCSTORE (cfg, ins, n, *sp);
5283 if (ins->opcode == CEE_STOBJ) {
5284 NEW_LOCLOADA (cfg, ins, n);
5285 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5287 MONO_ADD_INS (bblock, ins);
5294 if (sp != stack_start)
5296 MONO_INST_NEW (cfg, ins, 256 + ip [1]);
5297 ins->inst_left = *sp;
5300 cfg->flags |= MONO_CFG_HAS_ALLOCA;
5301 if (header->init_locals)
5302 ins->flags |= MONO_INST_INIT;
5306 /* FIXME: set init flag if locals init is set in this method */
5308 case CEE_ENDFILTER: {
5309 MonoExceptionClause *clause, *nearest;
5310 int cc, nearest_num;
5314 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
5316 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
5317 ins->inst_left = *sp;
5319 MONO_ADD_INS (bblock, ins);
5320 start_new_bblock = 1;
5325 for (cc = 0; cc < header->num_clauses; ++cc) {
5326 clause = &header->clauses [cc];
5327 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
5328 (!nearest || (clause->token_or_filter > nearest->token_or_filter))) {
5334 filter_lengths [nearest_num] = (ip - header->code) - nearest->token_or_filter;
5338 case CEE_UNALIGNED_:
5339 ins_flag |= MONO_INST_UNALIGNED;
5340 /* FIXME: record alignment? we can assume 1 for now */
5345 ins_flag |= MONO_INST_VOLATILE;
5349 ins_flag |= MONO_INST_TAILCALL;
5350 /* Can't inline tail calls at this time */
5351 inline_costs += 100000;
5358 token = read32 (ip + 2);
5359 if (method->wrapper_type != MONO_WRAPPER_NONE)
5360 klass = mono_method_get_wrapper_data (method, token);
5362 klass = mono_class_get (image, token);
5363 if (klass->byval_arg.type == MONO_TYPE_VAR)
5364 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
5365 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
5366 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
5368 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5369 MonoInst *store, *load;
5370 NEW_PCONST (cfg, load, NULL);
5371 load->cil_code = ip;
5372 load->type = STACK_OBJ;
5373 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
5374 store->cil_code = ip;
5375 handle_loaded_temps (cfg, bblock, stack_start, sp);
5376 MONO_ADD_INS (bblock, store);
5377 store->inst_i0 = sp [0];
5378 store->inst_i1 = load;
5381 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
5386 case CEE_CONSTRAINED_:
5387 /* FIXME: implement */
5389 token = read32 (ip + 2);
5394 MonoInst *iargs [3];
5400 handle_loaded_temps (cfg, bblock, stack_start, sp);
5401 if (ip [1] == CEE_CPBLK) {
5402 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
5404 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
5413 ins_flag |= MONO_INST_NOTYPECHECK;
5415 ins_flag |= MONO_INST_NORANGECHECK;
5416 /* we ignore the no-nullcheck for now since we
5417 * really do it explicitly only when doing callvirt->call
5423 /* FIXME: check we are in a catch handler */
5424 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
5425 load->cil_code = ip;
5426 MONO_INST_NEW (cfg, ins, CEE_THROW);
5427 ins->inst_left = load;
5429 MONO_ADD_INS (bblock, ins);
5431 start_new_bblock = 1;
5436 CHECK_STACK_OVF (1);
5438 token = read32 (ip + 2);
5439 /* FIXXME: handle generics. */
5440 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
5441 MonoType *type = mono_type_create_from_typespec (image, token);
5442 token = mono_type_size (type, &align);
5444 MonoClass *szclass = mono_class_get (image, token);
5445 mono_class_init (szclass);
5446 token = mono_class_value_size (szclass, &align);
5448 NEW_ICONST (cfg, ins, token);
5453 case CEE_REFANYTYPE:
5455 MONO_INST_NEW (cfg, ins, ip [1]);
5457 ins->type = STACK_MP;
5458 ins->inst_left = *sp;
5459 ins->type = STACK_VTYPE;
5460 ins->klass = mono_defaults.typehandle_class;
5469 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
5474 g_error ("opcode 0x%02x not handled", *ip);
5477 if (start_new_bblock != 1)
5480 bblock->cil_length = ip - bblock->cil_code;
5481 bblock->next_bb = end_bblock;
5482 link_bblock (cfg, bblock, end_bblock);
5484 if (cfg->method == method && cfg->domainvar) {
5488 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
5489 call->signature = helper_sig_domain_get;
5490 call->inst.type = STACK_PTR;
5491 call->fptr = mono_domain_get;
5492 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, (MonoInst*)call);
5494 MONO_ADD_INS (init_localsbb, store);
5497 if (header->init_locals) {
5499 for (i = 0; i < header->num_locals; ++i) {
5500 int t = header->locals [i]->type;
5501 if (t == MONO_TYPE_VALUETYPE && header->locals [i]->data.klass->enumtype)
5502 t = header->locals [i]->data.klass->enum_basetype->type;
5503 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
5504 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
5505 NEW_ICONST (cfg, ins, 0);
5506 NEW_LOCSTORE (cfg, store, i, ins);
5507 MONO_ADD_INS (init_localsbb, store);
5508 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
5509 MONO_INST_NEW (cfg, ins, OP_I8CONST);
5510 ins->type = STACK_I8;
5512 NEW_LOCSTORE (cfg, store, i, ins);
5513 MONO_ADD_INS (init_localsbb, store);
5514 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
5515 MONO_INST_NEW (cfg, ins, OP_R8CONST);
5516 ins->type = STACK_R8;
5517 ins->inst_p0 = (void*)&r8_0;
5518 NEW_LOCSTORE (cfg, store, i, ins);
5519 MONO_ADD_INS (init_localsbb, store);
5520 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF)) {
5521 NEW_LOCLOADA (cfg, ins, i);
5522 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (header->locals [i]), NULL, NULL);
5525 NEW_PCONST (cfg, ins, NULL);
5526 NEW_LOCSTORE (cfg, store, i, ins);
5527 MONO_ADD_INS (init_localsbb, store);
5533 /* resolve backward branches in the middle of an existing basic block */
5534 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
5536 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
5537 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
5538 if (tblock != start_bblock) {
5540 split_bblock (cfg, tblock, bblock);
5541 l = bblock->cil_code - header->code;
5542 bblock->cil_length = tblock->cil_length - l;
5543 tblock->cil_length = l;
5545 g_print ("recheck failed.\n");
5549 /* we compute regions here, because the length of filter clauses is not known in advance.
5550 * It is computed in the CEE_ENDFILTER case in the above switch statement*/
5551 if (cfg->method == method) {
5553 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5554 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
5556 mono_create_spvar_for_region (cfg, bb->region);
5557 if (cfg->verbose_level > 2)
5558 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
5561 g_hash_table_destroy (bbhash);
5564 dont_inline = g_list_remove (dont_inline, method);
5565 return inline_costs;
5568 if (cfg->method != method)
5569 g_hash_table_destroy (bbhash);
5570 dont_inline = g_list_remove (dont_inline, method);
5574 if (cfg->method != method)
5575 g_hash_table_destroy (bbhash);
5576 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
5577 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
5578 dont_inline = g_list_remove (dont_inline, method);
5583 mono_print_tree (MonoInst *tree) {
5589 arity = mono_burg_arity [tree->opcode];
5591 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
5593 switch (tree->opcode) {
5595 printf ("[%d]", tree->inst_c0);
5598 printf ("[%lld]", tree->inst_l);
5601 printf ("[%f]", *(double*)tree->inst_p0);
5604 printf ("[%f]", *(float*)tree->inst_p0);
5608 printf ("[%d]", tree->inst_c0);
5611 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5614 printf ("[%s]", mono_arch_regname (tree->dreg));
5617 printf ("[%s]", tree->inst_newa_class->name);
5618 mono_print_tree (tree->inst_newa_len);
5629 case OP_VOIDCALLVIRT: {
5630 MonoCallInst *call = (MonoCallInst*)tree;
5632 printf ("[%s]", call->method->name);
5637 printf ("[%d (", tree->inst_c0);
5638 for (i = 0; i < tree->inst_phi_args [0]; i++) {
5641 printf ("%d", tree->inst_phi_args [i + 1]);
5653 printf ("[B%d]", tree->inst_target_bb->block_num);
5663 case OP_VOIDCALL_REG:
5664 mono_print_tree (tree->inst_left);
5676 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
5677 mono_print_tree (tree->inst_left);
5681 mono_print_tree (tree->inst_left);
5683 mono_print_tree (tree->inst_right);
5693 mono_print_tree_nl (MonoInst *tree)
5695 mono_print_tree (tree);
5700 create_helper_signature (void)
5702 /* FIXME: set call conv */
5703 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
5704 helper_sig_newarr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5705 helper_sig_newarr->params [0] = helper_sig_newarr->params [1] = &mono_defaults.int_class->byval_arg;
5706 helper_sig_newarr->ret = &mono_defaults.object_class->byval_arg;
5707 helper_sig_newarr->params [2] = &mono_defaults.int32_class->byval_arg;
5708 helper_sig_newarr->pinvoke = 1;
5710 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
5711 helper_sig_newarr_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5712 helper_sig_newarr_specific->params [0] = &mono_defaults.int_class->byval_arg;
5713 helper_sig_newarr_specific->params [1] = &mono_defaults.int32_class->byval_arg;
5714 helper_sig_newarr_specific->ret = &mono_defaults.object_class->byval_arg;
5715 helper_sig_newarr_specific->pinvoke = 1;
5717 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
5718 helper_sig_object_new = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5719 helper_sig_object_new->params [0] = helper_sig_object_new->params [1] = &mono_defaults.int_class->byval_arg;
5720 helper_sig_object_new->ret = &mono_defaults.object_class->byval_arg;
5721 helper_sig_object_new->pinvoke = 1;
5723 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
5724 helper_sig_object_new_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5725 helper_sig_object_new_specific->params [0] = &mono_defaults.int_class->byval_arg;
5726 helper_sig_object_new_specific->ret = &mono_defaults.object_class->byval_arg;
5727 helper_sig_object_new_specific->pinvoke = 1;
5729 /* void* mono_method_compile (MonoMethod*) */
5730 helper_sig_compile = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5731 helper_sig_compile->params [0] = helper_sig_compile->ret = &mono_defaults.int_class->byval_arg;
5732 helper_sig_compile->pinvoke = 1;
5734 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
5735 helper_sig_compile_virt = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5736 helper_sig_compile_virt->params [0] = &mono_defaults.object_class->byval_arg;
5737 helper_sig_compile_virt->params [1] = helper_sig_compile_virt->ret = &mono_defaults.int_class->byval_arg;
5738 helper_sig_compile_virt->pinvoke = 1;
5740 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
5741 helper_sig_ldstr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5742 helper_sig_ldstr->params [0] = helper_sig_ldstr->params [1] = &mono_defaults.int_class->byval_arg;
5743 helper_sig_ldstr->params [2] = &mono_defaults.int32_class->byval_arg;
5744 helper_sig_ldstr->ret = &mono_defaults.object_class->byval_arg;
5745 helper_sig_ldstr->pinvoke = 1;
5747 /* MonoDomain *mono_domain_get (void) */
5748 helper_sig_domain_get = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5749 helper_sig_domain_get->ret = &mono_defaults.int_class->byval_arg;
5750 helper_sig_domain_get->pinvoke = 1;
5752 /* void* stelem_ref (MonoArray *, int index, MonoObject *) */
5753 helper_sig_stelem_ref = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5754 helper_sig_stelem_ref->params [0] = &mono_defaults.array_class->byval_arg;
5755 helper_sig_stelem_ref->params [1] = &mono_defaults.int32_class->byval_arg;
5756 helper_sig_stelem_ref->params [2] = &mono_defaults.object_class->byval_arg;
5757 helper_sig_stelem_ref->ret = &mono_defaults.void_class->byval_arg;
5758 helper_sig_stelem_ref->pinvoke = 1;
5760 /* long amethod (long, long) */
5761 helper_sig_long_long_long = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5762 helper_sig_long_long_long->params [0] = helper_sig_long_long_long->params [1] =
5763 &mono_defaults.int64_class->byval_arg;
5764 helper_sig_long_long_long->ret = &mono_defaults.int64_class->byval_arg;
5765 helper_sig_long_long_long->pinvoke = 1;
5767 /* object amethod (intptr) */
5768 helper_sig_obj_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5769 helper_sig_obj_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5770 helper_sig_obj_ptr->ret = &mono_defaults.object_class->byval_arg;
5771 helper_sig_obj_ptr->pinvoke = 1;
5773 /* void amethod (intptr) */
5774 helper_sig_void_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5775 helper_sig_void_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5776 helper_sig_void_ptr->ret = &mono_defaults.void_class->byval_arg;
5777 helper_sig_void_ptr->pinvoke = 1;
5779 /* void amethod (MonoObject *obj) */
5780 helper_sig_void_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5781 helper_sig_void_obj->params [0] = &mono_defaults.object_class->byval_arg;
5782 helper_sig_void_obj->ret = &mono_defaults.void_class->byval_arg;
5783 helper_sig_void_obj->pinvoke = 1;
5785 /* intptr amethod (void) */
5786 helper_sig_ptr_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5787 helper_sig_ptr_void->ret = &mono_defaults.int_class->byval_arg;
5788 helper_sig_ptr_void->pinvoke = 1;
5790 /* object amethod (void) */
5791 helper_sig_obj_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5792 helper_sig_obj_void->ret = &mono_defaults.object_class->byval_arg;
5793 helper_sig_obj_void->pinvoke = 1;
5795 /* void amethod (intptr, intptr) */
5796 helper_sig_void_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5797 helper_sig_void_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5798 helper_sig_void_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5799 helper_sig_void_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
5800 helper_sig_void_ptr_ptr->pinvoke = 1;
5802 /* void amethod (intptr, intptr, intptr) */
5803 helper_sig_void_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5804 helper_sig_void_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5805 helper_sig_void_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5806 helper_sig_void_ptr_ptr_ptr->params [2] = &mono_defaults.int_class->byval_arg;
5807 helper_sig_void_ptr_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
5808 helper_sig_void_ptr_ptr_ptr->pinvoke = 1;
5810 /* intptr amethod (intptr, intptr) */
5811 helper_sig_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5812 helper_sig_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5813 helper_sig_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5814 helper_sig_ptr_ptr_ptr->ret = &mono_defaults.int_class->byval_arg;
5815 helper_sig_ptr_ptr_ptr->pinvoke = 1;
5817 /* IntPtr amethod (object) */
5818 helper_sig_ptr_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5819 helper_sig_ptr_obj->params [0] = &mono_defaults.object_class->byval_arg;
5820 helper_sig_ptr_obj->ret = &mono_defaults.int_class->byval_arg;
5821 helper_sig_ptr_obj->pinvoke = 1;
5823 /* IntPtr amethod (int) */
5824 helper_sig_ptr_int = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5825 helper_sig_ptr_int->params [0] = &mono_defaults.int32_class->byval_arg;
5826 helper_sig_ptr_int->ret = &mono_defaults.int_class->byval_arg;
5827 helper_sig_ptr_int->pinvoke = 1;
5829 /* long amethod (long, guint32) */
5830 helper_sig_long_long_int = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5831 helper_sig_long_long_int->params [0] = &mono_defaults.int64_class->byval_arg;
5832 helper_sig_long_long_int->params [1] = &mono_defaults.int32_class->byval_arg;
5833 helper_sig_long_long_int->ret = &mono_defaults.int64_class->byval_arg;
5834 helper_sig_long_long_int->pinvoke = 1;
5836 /* ulong amethod (double) */
5837 helper_sig_ulong_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5838 helper_sig_ulong_double->params [0] = &mono_defaults.double_class->byval_arg;
5839 helper_sig_ulong_double->ret = &mono_defaults.uint64_class->byval_arg;
5840 helper_sig_ulong_double->pinvoke = 1;
5842 /* long amethod (double) */
5843 helper_sig_long_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5844 helper_sig_long_double->params [0] = &mono_defaults.double_class->byval_arg;
5845 helper_sig_long_double->ret = &mono_defaults.int64_class->byval_arg;
5846 helper_sig_long_double->pinvoke = 1;
5848 /* double amethod (long) */
5849 helper_sig_double_long = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5850 helper_sig_double_long->params [0] = &mono_defaults.int64_class->byval_arg;
5851 helper_sig_double_long->ret = &mono_defaults.double_class->byval_arg;
5852 helper_sig_double_long->pinvoke = 1;
5854 /* float amethod (long) */
5855 helper_sig_float_long = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5856 helper_sig_float_long->params [0] = &mono_defaults.int64_class->byval_arg;
5857 helper_sig_float_long->ret = &mono_defaults.single_class->byval_arg;
5858 helper_sig_float_long->pinvoke = 1;
5860 /* double amethod (double, double) */
5861 helper_sig_double_double_double = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5862 helper_sig_double_double_double->params [0] = &mono_defaults.double_class->byval_arg;
5863 helper_sig_double_double_double->params [1] = &mono_defaults.double_class->byval_arg;
5864 helper_sig_double_double_double->ret = &mono_defaults.double_class->byval_arg;
5865 helper_sig_double_double_double->pinvoke = 1;
5867 /* uint amethod (double) */
5868 helper_sig_uint_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5869 helper_sig_uint_double->params [0] = &mono_defaults.double_class->byval_arg;
5870 helper_sig_uint_double->ret = &mono_defaults.uint32_class->byval_arg;
5871 helper_sig_uint_double->pinvoke = 1;
5873 /* int amethod (double) */
5874 helper_sig_int_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5875 helper_sig_int_double->params [0] = &mono_defaults.double_class->byval_arg;
5876 helper_sig_int_double->ret = &mono_defaults.int32_class->byval_arg;
5877 helper_sig_int_double->pinvoke = 1;
5879 /* void initobj (intptr, int size) */
5880 helper_sig_initobj = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5881 helper_sig_initobj->params [0] = &mono_defaults.int_class->byval_arg;
5882 helper_sig_initobj->params [1] = &mono_defaults.int32_class->byval_arg;
5883 helper_sig_initobj->ret = &mono_defaults.void_class->byval_arg;
5884 helper_sig_initobj->pinvoke = 1;
5886 /* void memcpy (intptr, intptr, int size) */
5887 helper_sig_memcpy = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5888 helper_sig_memcpy->params [0] = &mono_defaults.int_class->byval_arg;
5889 helper_sig_memcpy->params [1] = &mono_defaults.int_class->byval_arg;
5890 helper_sig_memcpy->params [2] = &mono_defaults.int32_class->byval_arg;
5891 helper_sig_memcpy->ret = &mono_defaults.void_class->byval_arg;
5892 helper_sig_memcpy->pinvoke = 1;
5894 /* void memset (intptr, int val, int size) */
5895 helper_sig_memset = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5896 helper_sig_memset->params [0] = &mono_defaults.int_class->byval_arg;
5897 helper_sig_memset->params [1] = &mono_defaults.int32_class->byval_arg;
5898 helper_sig_memset->params [2] = &mono_defaults.int32_class->byval_arg;
5899 helper_sig_memset->ret = &mono_defaults.void_class->byval_arg;
5900 helper_sig_memset->pinvoke = 1;
5902 helper_sig_class_init_trampoline = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5903 helper_sig_class_init_trampoline->ret = &mono_defaults.void_class->byval_arg;
5904 helper_sig_class_init_trampoline->pinvoke = 1;
5906 /* MonoObject * amethod (MonoObject *obj, MonoClass *klass) */
5907 helper_sig_obj_obj_cls = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5908 helper_sig_obj_obj_cls->params [0] = &mono_defaults.object_class->byval_arg;
5909 helper_sig_obj_obj_cls->params [1] = &mono_defaults.int_class->byval_arg;
5910 helper_sig_obj_obj_cls->ret = &mono_defaults.object_class->byval_arg;
5911 helper_sig_obj_obj_cls->pinvoke = 1;
5914 static GHashTable *jit_icall_hash_name = NULL;
5915 static GHashTable *jit_icall_hash_addr = NULL;
5918 mono_find_jit_icall_by_name (const char *name)
5920 g_assert (jit_icall_hash_name);
5922 //printf ("lookup addr %s %p\n", name, g_hash_table_lookup (jit_icall_hash_name, name));
5923 return g_hash_table_lookup (jit_icall_hash_name, name);
5927 mono_find_jit_icall_by_addr (gconstpointer addr)
5929 g_assert (jit_icall_hash_addr);
5931 return g_hash_table_lookup (jit_icall_hash_addr, (gpointer)addr);
5935 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
5938 MonoMethod *wrapper;
5940 if (callinfo->wrapper)
5941 return callinfo->wrapper;
5942 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
5943 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
5944 callinfo->wrapper = mono_jit_compile_method (wrapper);
5946 return callinfo->wrapper;
5950 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save)
5952 MonoJitICallInfo *info;
5957 if (!jit_icall_hash_name) {
5958 jit_icall_hash_name = g_hash_table_new (g_str_hash, g_str_equal);
5959 jit_icall_hash_addr = g_hash_table_new (NULL, NULL);
5962 if (g_hash_table_lookup (jit_icall_hash_name, name)) {
5963 g_warning ("jit icall already defined \"%s\"\n", name);
5964 g_assert_not_reached ();
5967 info = g_new (MonoJitICallInfo, 1);
5974 #ifdef MONO_USE_EXC_TABLES
5975 || mono_arch_has_unwind_info (func)
5978 info->wrapper = func;
5980 info->wrapper = NULL;
5981 mono_icall_get_wrapper (info);
5984 g_hash_table_insert (jit_icall_hash_name, (gpointer)info->name, info);
5985 g_hash_table_insert (jit_icall_hash_addr, (gpointer)func, info);
5986 if (func != info->wrapper)
5987 g_hash_table_insert (jit_icall_hash_addr, (gpointer)info->wrapper, info);
5993 mono_create_class_init_trampoline (MonoVTable *vtable)
5997 /* previously created trampoline code */
5998 mono_domain_lock (vtable->domain);
6000 mono_g_hash_table_lookup (vtable->domain->class_init_trampoline_hash,
6002 mono_domain_unlock (vtable->domain);
6006 code = mono_arch_create_class_init_trampoline (vtable);
6008 /* store trampoline address */
6009 mono_domain_lock (vtable->domain);
6010 mono_g_hash_table_insert (vtable->domain->class_init_trampoline_hash,
6012 mono_domain_unlock (vtable->domain);
6014 EnterCriticalSection (&class_init_hash_mutex);
6015 if (!class_init_hash_addr)
6016 class_init_hash_addr = g_hash_table_new (NULL, NULL);
6017 g_hash_table_insert (class_init_hash_addr, code, vtable);
6018 LeaveCriticalSection (&class_init_hash_mutex);
6024 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
6028 EnterCriticalSection (&class_init_hash_mutex);
6029 if (class_init_hash_addr)
6030 res = g_hash_table_lookup (class_init_hash_addr, addr);
6033 LeaveCriticalSection (&class_init_hash_mutex);
6037 static MonoJitICallInfo **emul_opcode_map = NULL;
6039 static inline MonoJitICallInfo *
6040 mono_find_jit_opcode_emulation (int opcode)
6042 if (emul_opcode_map)
6043 return emul_opcode_map [opcode];
6049 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func, gboolean no_throw)
6051 MonoJitICallInfo *info;
6053 if (!emul_opcode_map)
6054 emul_opcode_map = g_new0 (MonoJitICallInfo*, OP_LAST + 1);
6056 g_assert (!sig->hasthis);
6057 g_assert (sig->param_count < 3);
6059 info = mono_register_jit_icall (func, name, sig, no_throw);
6061 emul_opcode_map [opcode] = info;
6065 decompose_foreach (MonoInst *tree, gpointer data)
6067 static MonoJitICallInfo *newarr_info = NULL;
6068 static MonoJitICallInfo *newarr_specific_info = NULL;
6069 MonoJitICallInfo *info;
6072 switch (tree->opcode) {
6074 MonoCompile *cfg = data;
6075 MonoInst *iargs [3];
6078 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
6079 g_assert (newarr_info);
6080 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
6081 g_assert (newarr_specific_info);
6084 if (cfg->opt & MONO_OPT_SHARED) {
6085 NEW_DOMAINCONST (cfg, iargs [0]);
6086 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
6087 iargs [2] = tree->inst_newa_len;
6092 MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
6094 NEW_VTABLECONST (cfg, iargs [0], vtable);
6095 iargs [1] = tree->inst_newa_len;
6097 info = newarr_specific_info;
6100 mono_emulate_opcode (cfg, tree, iargs, info);
6102 /* Need to decompose arguments after the the opcode is decomposed */
6103 for (i = 0; i < info->sig->param_count; ++i)
6104 dec_foreach (iargs [i], cfg);
6114 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
6116 switch (mono_burg_arity [tree->opcode]) {
6119 mono_inst_foreach (tree->inst_left, func, data);
6122 mono_inst_foreach (tree->inst_left, func, data);
6123 mono_inst_foreach (tree->inst_right, func, data);
6126 g_assert_not_reached ();
6133 mono_print_bb_code (MonoBasicBlock *bb) {
6135 MonoInst *c = bb->code;
6137 mono_print_tree (c);
6145 print_dfn (MonoCompile *cfg) {
6150 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
6152 for (i = 0; i < cfg->num_bblocks; ++i) {
6153 bb = cfg->bblocks [i];
6155 char* code1, *code2;
6156 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
6157 if (bb->last_ins->cil_code)
6158 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
6160 code2 = g_strdup ("");
6162 code1 [strlen (code1) - 1] = 0;
6163 code = g_strdup_printf ("%s -> %s", code1, code2);
6167 code = g_strdup ("\n");
6168 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
6170 MonoInst *c = bb->code;
6172 mono_print_tree (c);
6180 g_print ("\tprev:");
6181 for (j = 0; j < bb->in_count; ++j) {
6182 g_print (" BB%d", bb->in_bb [j]->block_num);
6184 g_print ("\t\tsucc:");
6185 for (j = 0; j < bb->out_count; ++j) {
6186 g_print (" BB%d", bb->out_bb [j]->block_num);
6188 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
6191 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
6194 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
6196 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
6204 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
6208 g_assert (bb->code);
6209 bb->last_ins->next = inst;
6210 bb->last_ins = inst;
6212 bb->last_ins = bb->code = inst;
6217 mono_destroy_compile (MonoCompile *cfg)
6219 //mono_mempool_stats (cfg->mempool);
6220 g_hash_table_destroy (cfg->bb_hash);
6222 mono_regstate_free (cfg->rs);
6224 g_hash_table_destroy (cfg->spvars);
6225 mono_mempool_destroy (cfg->mempool);
6226 g_list_free (cfg->ldstr_list);
6228 g_free (cfg->varinfo);
6234 mono_get_lmf_addr (void)
6236 MonoJitTlsData *jit_tls;
6238 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
6239 return &jit_tls->lmf;
6241 g_assert_not_reached ();
6246 * mono_thread_abort:
6247 * @obj: exception object
6249 * abort the thread, print exception information and stack trace
6252 mono_thread_abort (MonoObject *obj)
6254 /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
6256 /* handle_remove should be eventually called for this thread, too
6263 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
6265 MonoJitTlsData *jit_tls;
6268 jit_tls = g_new0 (MonoJitTlsData, 1);
6270 TlsSetValue (mono_jit_tls_id, jit_tls);
6272 jit_tls->abort_func = abort_func;
6273 jit_tls->end_of_stack = stack_start;
6275 lmf = g_new0 (MonoLMF, 1);
6278 jit_tls->lmf = jit_tls->first_lmf = lmf;
6280 mono_arch_setup_jit_tls_data (jit_tls);
6286 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
6289 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
6290 thread = mono_thread_current ();
6292 thread->jit_data = jit_tls;
6295 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
6298 mono_thread_abort_dummy (MonoObject *obj)
6300 if (mono_thread_attach_aborted_cb)
6301 mono_thread_attach_aborted_cb (obj);
6303 mono_thread_abort (obj);
6307 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
6310 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
6311 thread = mono_thread_current ();
6313 thread->jit_data = jit_tls;
6317 mini_thread_cleanup (MonoThread *thread)
6319 MonoJitTlsData *jit_tls = thread->jit_data;
6322 g_free (jit_tls->first_lmf);
6324 thread->jit_data = NULL;
6329 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
6331 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
6335 ji->data.target = target;
6336 ji->next = cfg->patch_info;
6338 cfg->patch_info = ji;
6342 mono_remove_patch_info (MonoCompile *cfg, int ip)
6344 MonoJumpInfo **ji = &cfg->patch_info;
6347 if ((*ji)->ip.i == ip)
6350 ji = &((*ji)->next);
6355 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
6356 MonoJitICallInfo *info;
6358 decompose_foreach (tree, cfg);
6360 switch (mono_burg_arity [tree->opcode]) {
6363 dec_foreach (tree->inst_left, cfg);
6365 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6366 MonoInst *iargs [2];
6368 iargs [0] = tree->inst_left;
6370 mono_emulate_opcode (cfg, tree, iargs, info);
6376 #ifdef MONO_ARCH_BIGMUL_INTRINS
6377 if (tree->opcode == OP_LMUL
6378 && (cfg->opt & MONO_OPT_INTRINS)
6379 && (tree->inst_left->opcode == CEE_CONV_I8
6380 || tree->inst_left->opcode == CEE_CONV_U8)
6381 && tree->inst_left->inst_left->type == STACK_I4
6382 && (tree->inst_right->opcode == CEE_CONV_I8
6383 || tree->inst_right->opcode == CEE_CONV_U8)
6384 && tree->inst_right->inst_left->type == STACK_I4) {
6385 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
6386 tree->inst_left = tree->inst_left->inst_left;
6387 tree->inst_right = tree->inst_right->inst_left;
6388 dec_foreach (tree, cfg);
6391 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6392 MonoInst *iargs [2];
6394 iargs [0] = tree->inst_i0;
6395 iargs [1] = tree->inst_i1;
6397 mono_emulate_opcode (cfg, tree, iargs, info);
6399 dec_foreach (iargs [0], cfg);
6400 dec_foreach (iargs [1], cfg);
6403 dec_foreach (tree->inst_left, cfg);
6404 dec_foreach (tree->inst_right, cfg);
6408 g_assert_not_reached ();
6413 decompose_pass (MonoCompile *cfg) {
6416 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6419 cfg->prev_ins = NULL;
6420 for (tree = cfg->cbb->code; tree; tree = tree->next) {
6421 dec_foreach (tree, cfg);
6422 cfg->prev_ins = tree;
6428 nullify_basic_block (MonoBasicBlock *bb)
6435 bb->code = bb->last_ins = NULL;
6436 bb->cil_code = NULL;
6440 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6444 for (i = 0; i < bb->out_count; i++) {
6445 MonoBasicBlock *ob = bb->out_bb [i];
6448 if (bb->out_count > 1) {
6449 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
6453 bb->out_bb [i] = repl;
6460 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6464 for (i = 0; i < bb->in_count; i++) {
6465 MonoBasicBlock *ib = bb->in_bb [i];
6468 if (bb->in_count > 1) {
6469 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
6473 bb->in_bb [i] = repl;
6480 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6484 for (i = 0; i < bb->out_count; i++) {
6485 MonoBasicBlock *ob = bb->out_bb [i];
6486 for (j = 0; j < ob->in_count; j++) {
6487 if (ob->in_bb [j] == orig) {
6488 ob->in_bb [j] = repl;
6497 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
6499 bb->out_count = bbn->out_count;
6500 bb->out_bb = bbn->out_bb;
6502 replace_basic_block (bb, bbn, bb);
6506 bb->last_ins->next = bbn->code;
6507 bb->last_ins = bbn->last_ins;
6510 bb->code = bbn->code;
6511 bb->last_ins = bbn->last_ins;
6513 bb->next_bb = bbn->next_bb;
6514 nullify_basic_block (bbn);
6518 optimize_branches (MonoCompile *cfg) {
6519 int i, changed = FALSE;
6520 MonoBasicBlock *bb, *bbn;
6525 /* we skip the entry block (exit is handled specially instead ) */
6526 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
6528 /* dont touch code inside exception clauses */
6529 if (bb->region != -1)
6532 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
6533 if (cfg->verbose_level > 2)
6534 g_print ("nullify block triggered %d\n", bbn->block_num);
6536 bb->next_bb = bbn->next_bb;
6538 for (i = 0; i < bbn->out_count; i++)
6539 replace_in_block (bbn->out_bb [i], bbn, NULL);
6541 nullify_basic_block (bbn);
6545 if (bb->out_count == 1) {
6546 bbn = bb->out_bb [0];
6548 /* conditional branches where true and false targets are the same can be also replaced with CEE_BR */
6549 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
6550 bb->last_ins->opcode = CEE_BR;
6551 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
6553 if (cfg->verbose_level > 2)
6554 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
6557 if (bb->region == bbn->region && bb->next_bb == bbn) {
6558 /* the block are in sequence anyway ... */
6560 /* branches to the following block can be removed */
6561 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
6562 bb->last_ins->opcode = CEE_NOP;
6564 if (cfg->verbose_level > 2)
6565 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
6568 if (bbn->in_count == 1) {
6570 if (bbn != cfg->bb_exit) {
6571 if (cfg->verbose_level > 2)
6572 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
6573 merge_basic_blocks (bb, bbn);
6577 //mono_print_bb_code (bb);
6587 /* we skip the entry block (exit is handled specially instead ) */
6588 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
6590 /* dont touch code inside exception clauses */
6591 if (bb->region != -1)
6594 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
6595 if (cfg->verbose_level > 2) {
6596 g_print ("nullify block triggered %d\n", bbn->block_num);
6598 bb->next_bb = bbn->next_bb;
6600 for (i = 0; i < bbn->out_count; i++)
6601 replace_in_block (bbn->out_bb [i], bbn, NULL);
6603 nullify_basic_block (bbn);
6609 if (bb->out_count == 1) {
6610 bbn = bb->out_bb [0];
6612 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
6613 bbn = bb->last_ins->inst_target_bb;
6614 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
6615 bbn->code->inst_target_bb->region == bb->region) {
6617 if (cfg->verbose_level > 2)
6618 g_print ("in %s branch to branch triggered %d -> %d\n", cfg->method->name,
6619 bb->block_num, bbn->block_num);
6621 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
6622 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
6627 } else if (bb->out_count == 2) {
6628 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
6629 bbn = bb->last_ins->inst_true_bb;
6630 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
6631 bbn->code->inst_target_bb->region == bb->region) {
6632 if (cfg->verbose_level > 2)
6633 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
6634 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
6637 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
6639 replace_in_block (bbn, bb, NULL);
6641 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
6642 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
6644 link_bblock (cfg, bb, bbn->code->inst_target_bb);
6650 bbn = bb->last_ins->inst_false_bb;
6651 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
6652 bbn->code->inst_target_bb->region == bb->region) {
6653 if (cfg->verbose_level > 2)
6654 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
6655 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
6658 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
6660 replace_in_block (bbn, bb, NULL);
6662 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
6663 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
6665 link_bblock (cfg, bb, bbn->code->inst_target_bb);
6678 mono_compile_create_vars (MonoCompile *cfg)
6680 MonoMethodSignature *sig;
6681 MonoMethodHeader *header;
6684 header = ((MonoMethodNormal *)cfg->method)->header;
6686 sig = cfg->method->signature;
6688 if (!MONO_TYPE_IS_VOID (sig->ret)) {
6689 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
6690 cfg->ret->opcode = OP_RETARG;
6691 cfg->ret->inst_vtype = sig->ret;
6692 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
6694 if (cfg->verbose_level > 2)
6695 g_print ("creating vars\n");
6698 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
6700 for (i = 0; i < sig->param_count; ++i)
6701 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
6703 cfg->locals_start = cfg->num_varinfo;
6705 if (cfg->verbose_level > 2)
6706 g_print ("creating locals\n");
6707 for (i = 0; i < header->num_locals; ++i)
6708 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
6709 if (cfg->verbose_level > 2)
6710 g_print ("locals done\n");
6714 mono_print_code (MonoCompile *cfg)
6718 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6719 MonoInst *tree = bb->code;
6724 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
6726 for (; tree; tree = tree->next) {
6727 mono_print_tree (tree);
6732 bb->last_ins->next = NULL;
6736 extern const char * const mono_burg_rule_string [];
6739 emit_state (MonoCompile *cfg, MBState *state, int goal)
6742 int ern = mono_burg_rule (state, goal);
6743 const guint16 *nts = mono_burg_nts [ern];
6746 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
6750 // state->reg1 = state->reg2; /* chain rule */
6752 state->reg1 = mono_regstate_next_int (cfg->rs);
6753 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
6756 state->reg1 = mono_regstate_next_int (cfg->rs);
6757 state->reg2 = mono_regstate_next_int (cfg->rs);
6760 state->reg1 = mono_regstate_next_float (cfg->rs);
6767 mono_burg_kids (state, ern, kids);
6769 emit_state (cfg, kids [0], nts [0]);
6771 emit_state (cfg, kids [1], nts [1]);
6773 g_assert (!nts [3]);
6774 emit_state (cfg, kids [2], nts [2]);
6779 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
6780 if ((emit = mono_burg_func [ern]))
6781 emit (state, state->tree, cfg);
6784 #define DEBUG_SELECTION
6787 mini_select_instructions (MonoCompile *cfg)
6789 static int reverse_map [] = {
6790 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
6791 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
6793 static int reverse_fmap [] = {
6794 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
6795 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
6797 static int reverse_lmap [] = {
6798 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
6799 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
6804 cfg->state_pool = mono_mempool_new ();
6805 cfg->rs = mono_regstate_new ();
6807 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6808 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode) &&
6809 bb->next_bb != bb->last_ins->inst_false_bb) {
6811 if (bb->next_bb == bb->last_ins->inst_true_bb) {
6812 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
6813 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
6814 bb->last_ins->inst_false_bb = tmp;
6816 if (bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
6817 bb->last_ins->opcode = reverse_map [bb->last_ins->opcode - CEE_BEQ];
6818 } else if (bb->last_ins->opcode >= OP_FBEQ && bb->last_ins->opcode <= OP_FBLT_UN) {
6819 bb->last_ins->opcode = reverse_fmap [bb->last_ins->opcode - OP_FBEQ];
6820 } else if (bb->last_ins->opcode >= OP_LBEQ && bb->last_ins->opcode <= OP_LBLT_UN) {
6821 bb->last_ins->opcode = reverse_lmap [bb->last_ins->opcode - OP_LBEQ];
6824 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
6825 inst->opcode = CEE_BR;
6826 inst->inst_target_bb = bb->last_ins->inst_false_bb;
6827 mono_bblock_add_inst (bb, inst);
6832 #ifdef DEBUG_SELECTION
6833 if (cfg->verbose_level >= 4) {
6834 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6835 MonoInst *tree = bb->code;
6836 g_print ("DUMP BLOCK %d:\n", bb->block_num);
6839 for (; tree; tree = tree->next) {
6840 mono_print_tree (tree);
6847 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6848 MonoInst *tree = bb->code, *next;
6854 bb->last_ins = NULL;
6857 mono_regstate_reset (cfg->rs);
6859 #ifdef DEBUG_SELECTION
6860 if (cfg->verbose_level >= 3)
6861 g_print ("LABEL BLOCK %d:\n", bb->block_num);
6863 for (; tree; tree = next) {
6865 #ifdef DEBUG_SELECTION
6866 if (cfg->verbose_level >= 3) {
6867 mono_print_tree (tree);
6872 if (!(mbstate = mono_burg_label (tree, cfg))) {
6873 g_warning ("unable to label tree %p", tree);
6874 mono_print_tree (tree);
6876 g_assert_not_reached ();
6878 emit_state (cfg, mbstate, MB_NTERM_stmt);
6880 bb->max_ireg = cfg->rs->next_vireg;
6881 bb->max_freg = cfg->rs->next_vfreg;
6884 bb->last_ins->next = NULL;
6886 mono_mempool_empty (cfg->state_pool);
6888 mono_mempool_destroy (cfg->state_pool);
6892 mono_codegen (MonoCompile *cfg)
6894 MonoJumpInfo *patch_info;
6896 int i, max_epilog_size;
6899 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6900 cfg->spill_count = 0;
6901 /* we reuse dfn here */
6902 /* bb->dfn = bb_count++; */
6903 mono_arch_local_regalloc (cfg, bb);
6906 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
6907 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
6909 code = mono_arch_emit_prolog (cfg);
6911 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6912 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
6914 cfg->code_len = code - cfg->native_code;
6915 cfg->prolog_end = cfg->code_len;
6917 mono_debug_open_method (cfg);
6919 /* emit code all basic blocks */
6920 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6921 bb->native_offset = cfg->code_len;
6922 mono_arch_output_basic_block (cfg, bb);
6924 cfg->bb_exit->native_offset = cfg->code_len;
6926 code = cfg->native_code + cfg->code_len;
6928 max_epilog_size = mono_arch_max_epilog_size (cfg);
6930 /* we always allocate code in cfg->domain->code_mp to increase locality */
6931 cfg->code_size = cfg->code_len + max_epilog_size;
6932 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
6933 code = mono_mempool_alloc (cfg->domain->code_mp, cfg->code_size);
6934 memcpy (code, cfg->native_code, cfg->code_len);
6935 g_free (cfg->native_code);
6936 cfg->native_code = code;
6937 code = cfg->native_code + cfg->code_len;
6939 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
6941 cfg->epilog_begin = cfg->code_len;
6943 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6944 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
6946 cfg->code_len = code - cfg->native_code;
6948 mono_arch_emit_epilog (cfg);
6950 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
6951 switch (patch_info->type) {
6952 case MONO_PATCH_INFO_ABS: {
6953 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
6955 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
6956 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
6957 patch_info->data.name = info->name;
6960 MonoVTable *vtable = mono_find_class_init_trampoline_by_addr (patch_info->data.target);
6962 patch_info->type = MONO_PATCH_INFO_CLASS_INIT;
6963 patch_info->data.klass = vtable->klass;
6968 case MONO_PATCH_INFO_SWITCH: {
6969 gpointer *table = mono_mempool_alloc (cfg->domain->code_mp, sizeof (gpointer) * patch_info->table_size);
6970 patch_info->ip.i = patch_info->ip.label->inst_c0;
6971 for (i = 0; i < patch_info->table_size; i++) {
6972 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
6974 patch_info->data.target = table;
6983 if (cfg->verbose_level > 0)
6984 g_print ("Method %s emitted at %p to %p [%s]\n",
6985 mono_method_full_name (cfg->method, TRUE),
6986 cfg->native_code, cfg->native_code + cfg->code_len, cfg->domain->friendly_name);
6988 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info, cfg->run_cctors);
6990 mono_arch_flush_icache (cfg->native_code, cfg->code_len);
6992 mono_debug_close_method (cfg);
6996 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
7001 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
7002 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
7004 if (cp->opcode == OP_ICONST) {
7005 if (cfg->opt & MONO_OPT_CONSPROP) {
7006 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
7010 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
7011 if (cfg->opt & MONO_OPT_COPYPROP) {
7012 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
7018 arity = mono_burg_arity [tree->opcode];
7021 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
7022 if (cfg->opt & MONO_OPT_CFOLD)
7023 mono_constant_fold_inst (tree, NULL);
7024 /* The opcode may have changed */
7025 if (mono_burg_arity [tree->opcode] > 1) {
7026 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
7027 if (cfg->opt & MONO_OPT_CFOLD)
7028 mono_constant_fold_inst (tree, NULL);
7030 mono_constant_fold_inst (tree, NULL);
7036 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
7040 switch (tree->opcode) {
7050 if (tree->ssa_op == MONO_SSA_NOP) {
7051 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7068 case OP_VOIDCALL_REG:
7069 case OP_VOIDCALLVIRT:
7071 MonoCallInst *call = (MonoCallInst *)tree;
7072 MonoMethodSignature *sig = call->signature;
7073 int i, byref = FALSE;
7075 for (i = 0; i < sig->param_count; i++) {
7076 if (sig->params [i]->byref) {
7083 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7091 arity = mono_burg_arity [tree->opcode];
7097 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7100 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7101 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
7104 g_assert_not_reached ();
7109 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
7111 MonoInst *tree = bb->code;
7117 for (; tree; tree = tree->next) {
7119 mono_cprop_copy_values (cfg, tree, acp);
7121 mono_cprop_invalidate_values (tree, acp, acp_size);
7123 if (tree->ssa_op == MONO_SSA_STORE &&
7124 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
7125 MonoInst *i1 = tree->inst_i1;
7127 acp [tree->inst_i0->inst_c0] = NULL;
7129 for (i = 0; i < acp_size; i++) {
7130 if (acp [i] && acp [i]->opcode != OP_ICONST &&
7131 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
7136 if (i1->opcode == OP_ICONST) {
7137 acp [tree->inst_i0->inst_c0] = i1;
7138 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
7140 if (i1->ssa_op == MONO_SSA_LOAD &&
7141 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
7142 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
7143 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
7144 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
7149 if (tree->opcode == CEE_BEQ) {
7150 g_assert (tree->inst_i0->opcode == OP_COMPARE);
7151 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
7152 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
7154 tree->opcode = CEE_BR;
7155 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
7156 tree->inst_target_bb = tree->inst_true_bb;
7158 tree->inst_target_bb = tree->inst_false_bb;
7167 mono_local_cprop (MonoCompile *cfg)
7172 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
7174 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7175 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
7176 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
7181 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, int parts)
7183 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
7184 guint8 *ip = (guint8 *)header->code;
7187 int dfn = 0, i, code_size_ratio;
7189 mono_jit_stats.methods_compiled++;
7190 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
7191 mono_profiler_method_jit (method);
7193 cfg = g_new0 (MonoCompile, 1);
7194 cfg->method = method;
7195 cfg->mempool = mono_mempool_new ();
7197 cfg->prof_options = mono_profiler_get_events ();
7198 cfg->run_cctors = run_cctors;
7199 cfg->bb_hash = g_hash_table_new (NULL, NULL);
7200 cfg->domain = domain;
7201 cfg->verbose_level = mini_verbose;
7202 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
7203 ((MonoMethodNormal *)method)->header->max_stack);
7205 if (cfg->verbose_level > 2)
7206 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
7209 * create MonoInst* which represents arguments and local variables
7211 mono_compile_create_vars (cfg);
7213 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
7214 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7215 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
7216 mono_destroy_compile (cfg);
7220 mono_jit_stats.basic_blocks += cfg->num_bblocks;
7221 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
7223 if (cfg->num_varinfo > 2000) {
7225 * we disable some optimizations if there are too many variables
7226 * because JIT time may become too expensive. The actual number needs
7227 * to be tweaked and eventually the non-linear algorithms should be fixed.
7229 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
7230 cfg->disable_ssa = TRUE;
7232 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
7234 /* Depth-first ordering on basic blocks */
7235 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
7237 if (cfg->opt & MONO_OPT_BRANCH)
7238 optimize_branches (cfg);
7240 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
7241 if (cfg->num_bblocks != dfn + 1) {
7244 cfg->num_bblocks = dfn + 1;
7246 if (!header->clauses) {
7247 /* remove unreachable code, because the code in them may be
7248 * inconsistent (access to dead variables for example) */
7249 for (bb = cfg->bb_entry; bb;) {
7250 MonoBasicBlock *bbn = bb->next_bb;
7252 if (bbn && bbn->region == -1 && !bbn->dfn) {
7253 if (cfg->verbose_level > 1)
7254 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
7255 bb->next_bb = bbn->next_bb;
7256 nullify_basic_block (bbn);
7264 if (cfg->opt & MONO_OPT_LOOP) {
7265 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
7266 mono_compute_natural_loops (cfg);
7269 /* after method_to_ir */
7273 //#define DEBUGSSA "logic_run"
7274 #define DEBUGSSA_CLASS "Tests"
7277 if (!header->num_clauses && !cfg->disable_ssa) {
7278 mono_local_cprop (cfg);
7279 mono_ssa_compute (cfg);
7283 /* fixme: add all optimizations which requires SSA */
7284 if (cfg->opt & (MONO_OPT_DEADCE)) {
7285 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
7286 mono_local_cprop (cfg);
7287 mono_ssa_compute (cfg);
7289 if (cfg->verbose_level >= 2) {
7296 /* after SSA translation */
7300 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
7301 if (cfg->comp_done & MONO_COMP_SSA) {
7302 mono_ssa_cprop (cfg);
7304 mono_local_cprop (cfg);
7308 if (cfg->comp_done & MONO_COMP_SSA) {
7309 mono_ssa_deadce (cfg);
7311 //mono_ssa_strength_reduction (cfg);
7313 mono_ssa_remove (cfg);
7315 if (cfg->opt & MONO_OPT_BRANCH)
7316 optimize_branches (cfg);
7319 /* after SSA removal */
7323 decompose_pass (cfg);
7325 /* FIXME: disabled with exception clauses: bug #42136 */
7326 if ((!header->num_clauses) && (cfg->opt & MONO_OPT_LINEARS)) {
7329 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
7330 cfg->comp_done &= ~MONO_COMP_LIVENESS;
7331 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
7332 mono_analyze_liveness (cfg);
7334 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
7335 regs = mono_arch_get_global_int_regs (cfg);
7336 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
7340 //mono_print_code (cfg);
7344 /* variables are allocated after decompose, since decompose could create temps */
7345 mono_arch_allocate_vars (cfg);
7347 if (cfg->opt & MONO_OPT_CFOLD)
7348 mono_constant_fold (cfg);
7350 mini_select_instructions (cfg);
7353 if (cfg->verbose_level >= 2) {
7354 char *id = mono_method_full_name (cfg->method, FALSE);
7355 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
7359 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
7361 jinfo = g_new0 (MonoJitInfo, 1);
7362 jinfo->method = method;
7363 jinfo->code_start = cfg->native_code;
7364 jinfo->code_size = cfg->code_len;
7365 jinfo->used_regs = cfg->used_int_regs;
7366 jinfo->domain_neutral = (cfg->opt & MONO_OPT_SHARED) != 0;
7368 if (header->num_clauses) {
7371 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
7372 jinfo->num_clauses = header->num_clauses;
7373 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
7374 sizeof (MonoJitExceptionInfo) * header->num_clauses);
7376 for (i = 0; i < header->num_clauses; i++) {
7377 MonoExceptionClause *ec = &header->clauses [i];
7378 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
7379 MonoBasicBlock *tblock;
7381 ei->flags = ec->flags;
7383 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7384 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->token_or_filter);
7386 ei->data.filter = cfg->native_code + tblock->native_offset;
7388 ei->data.token = ec->token_or_filter;
7391 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
7393 ei->try_start = cfg->native_code + tblock->native_offset;
7394 g_assert (tblock->native_offset);
7395 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
7397 ei->try_end = cfg->native_code + tblock->native_offset;
7398 g_assert (tblock->native_offset);
7399 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
7401 ei->handler_start = cfg->native_code + tblock->native_offset;
7405 cfg->jit_info = jinfo;
7407 mono_jit_info_table_add (cfg->domain, jinfo);
7409 /* collect statistics */
7410 mono_jit_stats.allocated_code_size += cfg->code_len;
7411 code_size_ratio = cfg->code_len;
7412 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
7413 mono_jit_stats.biggest_method_size = code_size_ratio;
7414 mono_jit_stats.biggest_method = method;
7416 code_size_ratio = (code_size_ratio * 100) / ((MonoMethodNormal *)method)->header->code_size;
7417 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
7418 mono_jit_stats.max_code_size_ratio = code_size_ratio;
7419 mono_jit_stats.max_ratio_method = method;
7421 mono_jit_stats.native_code_size += cfg->code_len;
7423 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7424 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
7430 mono_jit_compile_method_inner (MonoMethod *method)
7432 /* FIXME: later copy the code from mono */
7433 MonoDomain *target_domain, *domain = mono_domain_get ();
7435 GHashTable *jit_code_hash;
7441 if (opt & MONO_OPT_SHARED)
7442 target_domain = mono_root_domain;
7444 target_domain = domain;
7446 jit_code_hash = target_domain->jit_code_hash;
7448 #ifdef MONO_USE_AOT_COMPILER
7449 if (!mono_compile_aot && (opt & MONO_OPT_AOT)) {
7452 mono_domain_lock (domain);
7454 mono_class_init (method->klass);
7455 if ((info = mono_aot_get_method (domain, method))) {
7457 g_hash_table_insert (domain->jit_code_hash, method, info);
7459 mono_domain_unlock (domain);
7461 /* make sure runtime_init is called */
7462 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
7464 return info->code_start;
7467 mono_domain_unlock (domain);
7471 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
7472 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
7475 if (!method->addr) {
7476 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
7477 method->addr = mono_lookup_internal_call (method);
7479 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
7480 mono_lookup_pinvoke_call (method);
7482 #ifdef MONO_USE_EXC_TABLES
7483 if (mono_method_blittable (method)) {
7484 return method->addr;
7487 nm = mono_marshal_get_native_wrapper (method);
7488 return mono_compile_method (nm);
7490 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
7491 //mono_debug_add_wrapper (method, nm);
7492 #ifdef MONO_USE_EXC_TABLES
7495 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
7496 const char *name = method->name;
7499 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
7500 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
7501 /* FIXME: uhm, we need a wrapper to handle exceptions? */
7502 return (gpointer)mono_delegate_ctor;
7503 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
7504 nm = mono_marshal_get_delegate_invoke (method);
7505 return mono_jit_compile_method (nm);
7506 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
7507 nm = mono_marshal_get_delegate_begin_invoke (method);
7508 return mono_jit_compile_method (nm);
7509 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
7510 nm = mono_marshal_get_delegate_end_invoke (method);
7511 return mono_jit_compile_method (nm);
7517 cfg = mini_method_compile (method, opt, target_domain, TRUE, 0);
7518 code = cfg->native_code;
7520 g_hash_table_insert (jit_code_hash, method, cfg->jit_info);
7522 mono_destroy_compile (cfg);
7524 if (target_domain->jump_target_hash) {
7525 MonoJumpInfo patch_info;
7527 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
7529 patch_info.next = NULL;
7530 patch_info.ip.i = 0;
7531 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
7532 patch_info.data.method = method;
7533 g_hash_table_remove (target_domain->jump_target_hash, method);
7535 for (tmp = list; tmp; tmp = tmp->next)
7536 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info, TRUE);
7537 g_slist_free (list);
7539 /* make sure runtime_init is called */
7540 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
7546 mono_jit_compile_method (MonoMethod *method)
7548 /* FIXME: later copy the code from mono */
7549 MonoDomain *target_domain, *domain = mono_domain_get ();
7553 if (default_opt & MONO_OPT_SHARED)
7554 target_domain = mono_root_domain;
7556 target_domain = domain;
7558 mono_domain_lock (target_domain);
7560 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
7561 /* We can't use a domain specific method in another domain */
7562 if (! ((domain != target_domain) && !info->domain_neutral)) {
7563 mono_domain_unlock (target_domain);
7564 mono_jit_stats.methods_lookups++;
7565 return info->code_start;
7569 code = mono_jit_compile_method_inner (method);
7571 mono_domain_unlock (target_domain);
7577 * mono_jit_runtime_invoke:
7578 * @method: the method to invoke
7579 * @obj: this pointer
7580 * @params: array of parameter values.
7581 * @exc: used to catch exceptions objects
7584 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
7587 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc);
7589 if (obj == NULL && !(method->flags & METHOD_ATTRIBUTE_STATIC) && !method->string_ctor && (method->wrapper_type == 0)) {
7590 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
7594 invoke = mono_marshal_get_runtime_invoke (method);
7595 runtime_invoke = mono_jit_compile_method (invoke);
7596 return runtime_invoke (obj, params, exc);
7599 #ifdef PLATFORM_WIN32
7600 #define GET_CONTEXT \
7601 struct sigcontext *ctx = (struct sigcontext*)_dummy;
7603 #define GET_CONTEXT \
7604 void **_p = (void **)&_dummy; \
7605 struct sigcontext *ctx = (struct sigcontext *)++_p;
7609 sigfpe_signal_handler (int _dummy)
7614 exc = mono_get_exception_divide_by_zero ();
7616 mono_arch_handle_exception (ctx, exc, FALSE);
7620 sigill_signal_handler (int _dummy)
7624 exc = mono_get_exception_execution_engine ("SIGILL");
7626 mono_arch_handle_exception (ctx, exc, FALSE);
7630 sigsegv_signal_handler (int _dummy)
7635 exc = mono_get_exception_null_reference ();
7637 mono_arch_handle_exception (ctx, exc, FALSE);
7641 sigusr1_signal_handler (int _dummy)
7646 thread = mono_thread_current ();
7648 thread->abort_exc = mono_get_exception_thread_abort ();
7650 mono_arch_handle_exception (ctx, thread->abort_exc, FALSE);
7654 sigquit_signal_handler (int _dummy)
7659 exc = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
7661 mono_arch_handle_exception (ctx, exc, FALSE);
7665 sigint_signal_handler (int _dummy)
7670 exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
7672 mono_arch_handle_exception (ctx, exc, FALSE);
7676 mono_runtime_install_handlers (void)
7678 gboolean skip_sigabort = FALSE;
7679 #ifndef PLATFORM_WIN32
7680 struct sigaction sa;
7683 #ifdef PLATFORM_WIN32
7685 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
7686 win32_seh_set_handler(SIGILL, sigill_signal_handler);
7687 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
7688 if (getenv ("MONO_DEBUG"))
7689 win32_seh_set_handler(SIGINT, sigint_signal_handler);
7690 #else /* !PLATFORM_WIN32 */
7692 /* libpthreads has its own implementation of sigaction(),
7693 * but it seems to work well with our current exception
7694 * handlers. If not we must call syscall directly instead
7697 if (getenv ("MONO_DEBUG")) {
7699 sa.sa_handler = sigint_signal_handler;
7700 sigemptyset (&sa.sa_mask);
7702 //g_assert (syscall (SYS_sigaction, SIGINT, &sa, NULL) != -1);
7703 g_assert (sigaction (SIGINT, &sa, NULL) != -1);
7707 sa.sa_handler = sigfpe_signal_handler;
7708 sigemptyset (&sa.sa_mask);
7710 //g_assert (syscall (SYS_sigaction, SIGFPE, &sa, NULL) != -1);
7711 g_assert (sigaction (SIGFPE, &sa, NULL) != -1);
7714 sa.sa_handler = sigquit_signal_handler;
7715 sigemptyset (&sa.sa_mask);
7717 g_assert (sigaction (SIGQUIT, &sa, NULL) != -1);
7720 sa.sa_handler = sigill_signal_handler;
7721 sigemptyset (&sa.sa_mask);
7723 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
7724 g_assert (sigaction (SIGILL, &sa, NULL) != -1);
7726 /* catch thread abort signal */
7727 sa.sa_handler = sigusr1_signal_handler;
7728 sigemptyset (&sa.sa_mask);
7730 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
7732 #ifdef HAVE_VALGRIND_MEMCHECK_H
7733 if (RUNNING_ON_VALGRIND)
7734 /* valgrind 20030725 and earlier aborts on this call so we skip it */
7735 skip_sigabort = TRUE;
7739 g_assert (sigaction (mono_thread_get_abort_signal (), &sa, NULL) != -1);
7743 sa.sa_handler = sigsegv_signal_handler;
7744 sigemptyset (&sa.sa_mask);
7746 //g_assert (syscall (SYS_sigaction, SIGSEGV, &sa, NULL) != -1);
7747 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
7749 #endif /* PLATFORM_WIN32 */
7752 /* mono_jit_create_remoting_trampoline:
7753 * @method: pointer to the method info
7755 * Creates a trampoline which calls the remoting functions. This
7756 * is used in the vtable of transparent proxies.
7758 * Returns: a pointer to the newly created code
7761 mono_jit_create_remoting_trampoline (MonoMethod *method)
7764 guint8 *addr = NULL;
7766 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
7767 (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
7768 nm = mono_marshal_get_remoting_invoke (method);
7769 addr = mono_compile_method (nm);
7771 addr = mono_compile_method (method);
7777 mini_init (const char *filename)
7781 mono_arch_cpu_init ();
7783 if (!g_thread_supported ())
7784 g_thread_init (NULL);
7786 mono_jit_tls_id = TlsAlloc ();
7787 setup_jit_tls_data ((gpointer)-1, mono_thread_abort);
7789 InitializeCriticalSection (&class_init_hash_mutex);
7793 if (default_opt & MONO_OPT_AOT)
7796 mono_runtime_install_handlers ();
7797 mono_threads_install_cleanup (mini_thread_cleanup);
7799 #define JIT_TRAMPOLINES_WORK
7800 #ifdef JIT_TRAMPOLINES_WORK
7801 mono_install_compile_method (mono_jit_compile_method);
7802 mono_install_trampoline (mono_arch_create_jit_trampoline);
7803 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
7805 #define JIT_INVOKE_WORKS
7806 #ifdef JIT_INVOKE_WORKS
7807 mono_install_runtime_invoke (mono_jit_runtime_invoke);
7808 mono_install_handler (mono_arch_get_throw_exception ());
7810 mono_install_stack_walk (mono_jit_walk_stack);
7811 mono_install_get_config_dir ();
7813 domain = mono_init (filename);
7816 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
7817 ves_icall_get_frame_info);
7818 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
7819 ves_icall_get_trace);
7820 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
7821 mono_runtime_install_handlers);
7824 create_helper_signature ();
7826 #define JIT_CALLS_WORK
7827 #ifdef JIT_CALLS_WORK
7828 /* Needs to be called here since register_jit_icall depends on it */
7829 mono_marshal_init ();
7831 mono_arch_register_lowlevel_calls ();
7832 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
7833 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
7835 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
7836 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
7838 /* fixme: we cant hanlde vararg methods this way, because the signature is not constant */
7839 //mono_register_jit_icall (ves_array_element_address, "ves_array_element_address", NULL);
7840 //mono_register_jit_icall (mono_array_new_va, "mono_array_new_va", NULL);
7842 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
7843 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
7844 helper_sig_void_ptr, TRUE);
7845 mono_register_jit_icall (mono_thread_get_pending_exception, "mono_thread_get_pending_exception", helper_sig_obj_void, FALSE);
7848 * NOTE, NOTE, NOTE, NOTE:
7849 * when adding emulation for some opcodes, remember to also add a dummy
7850 * rule to the burg files, because we need the arity information to be correct.
7852 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult, TRUE);
7853 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un, FALSE);
7854 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf, FALSE);
7855 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv, FALSE);
7856 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un, FALSE);
7857 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem, FALSE);
7858 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un, FALSE);
7860 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl, TRUE);
7861 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr, TRUE);
7862 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un, TRUE);
7864 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8, FALSE);
7865 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4, FALSE);
7866 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8, TRUE);
7867 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8, TRUE);
7869 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
7870 mono_register_opcode_emulation (OP_FCONV_TO_I8, "__emul_fconv_to_i8", helper_sig_long_double, mono_fconv_i8, FALSE);
7872 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
7873 mono_register_opcode_emulation (OP_LCONV_TO_R8, "__emul_lconv_to_r8", helper_sig_double_long, mono_lconv_to_r8, FALSE);
7875 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
7876 mono_register_opcode_emulation (OP_LCONV_TO_R4, "__emul_lconv_to_r4", helper_sig_float_long, mono_lconv_to_r4, FALSE);
7878 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
7879 mono_register_opcode_emulation (OP_LCONV_TO_R_UN, "__emul_lconv_to_r8_un", helper_sig_double_long, mono_lconv_to_r8_un, FALSE);
7881 #ifdef MONO_ARCH_EMULATE_FREM
7882 mono_register_opcode_emulation (OP_FREM, "__emul_frem", helper_sig_double_double_double, fmod, FALSE);
7885 #if SIZEOF_VOID_P == 4
7886 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4, TRUE);
7888 #warning "fixme: add opcode emulation"
7891 /* other jit icalls */
7892 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
7893 helper_sig_ptr_ptr_ptr, FALSE);
7894 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
7895 mono_register_jit_icall (mono_get_special_static_data, "mono_get_special_static_data", helper_sig_ptr_int, FALSE);
7896 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
7897 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
7898 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
7899 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
7900 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
7901 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
7902 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
7903 mono_register_jit_icall (mono_object_new_fast, "mono_object_new_fast", helper_sig_object_new_specific, FALSE);
7904 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
7905 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
7906 mono_register_jit_icall (mono_string_to_utf16, "mono_string_to_utf16", helper_sig_ptr_obj, FALSE);
7907 mono_register_jit_icall (mono_string_from_utf16, "mono_string_from_utf16", helper_sig_obj_ptr, FALSE);
7908 mono_register_jit_icall (mono_string_new_wrapper, "mono_string_new_wrapper", helper_sig_obj_ptr, FALSE);
7909 mono_register_jit_icall (mono_string_to_utf8, "mono_string_to_utf8", helper_sig_ptr_obj, FALSE);
7910 mono_register_jit_icall (mono_string_to_bstr, "mono_string_to_bstr", helper_sig_ptr_obj, FALSE);
7911 mono_register_jit_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", helper_sig_ptr_obj, FALSE);
7912 mono_register_jit_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", helper_sig_ptr_obj, FALSE);
7913 mono_register_jit_icall (mono_array_to_savearray, "mono_array_to_savearray", helper_sig_ptr_obj, FALSE);
7914 mono_register_jit_icall (mono_array_to_lparray, "mono_array_to_lparray", helper_sig_ptr_obj, FALSE);
7915 mono_register_jit_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", helper_sig_ptr_obj, FALSE);
7916 mono_register_jit_icall (mono_marshal_string_array, "mono_marshal_string_array", helper_sig_ptr_obj, FALSE);
7917 mono_register_jit_icall (mono_string_utf8_to_builder, "mono_string_utf8_to_builder", helper_sig_void_ptr_ptr, FALSE);
7918 mono_register_jit_icall (mono_marshal_free_array, "mono_marshal_free_array", helper_sig_void_ptr_ptr, FALSE);
7919 mono_register_jit_icall (mono_string_to_byvalstr, "mono_string_to_byvalstr", helper_sig_void_ptr_ptr_ptr, FALSE);
7920 mono_register_jit_icall (mono_string_to_byvalwstr, "mono_string_to_byvalwstr", helper_sig_void_ptr_ptr_ptr, FALSE);
7921 mono_register_jit_icall (g_free, "g_free", helper_sig_void_ptr, FALSE);
7922 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
7923 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
7924 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
7925 mono_register_jit_icall (mono_object_castclass_mbyref, "mono_object_castclass", helper_sig_obj_obj_cls, FALSE);
7928 #define JIT_RUNTIME_WORKS
7929 #ifdef JIT_RUNTIME_WORKS
7930 mono_runtime_install_cleanup ((MonoDomainFunc)mini_cleanup);
7931 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
7934 //mono_thread_attach (domain);
7938 MonoJitStats mono_jit_stats = {0};
7941 print_jit_stats (void)
7943 if (mono_jit_stats.enabled) {
7944 g_print ("Mono Jit statistics\n");
7945 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
7946 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
7947 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
7948 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
7949 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
7950 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
7951 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
7952 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
7953 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
7954 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
7955 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
7956 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
7957 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
7958 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
7959 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
7960 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
7961 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
7962 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
7964 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
7965 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
7966 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
7967 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
7968 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
7973 mini_cleanup (MonoDomain *domain)
7976 * mono_runtime_cleanup() and mono_domain_finalize () need to
7977 * be called early since they need the execution engine still
7978 * fully working (mono_domain_finalize may invoke managed finalizers
7979 * and mono_runtime_cleanup will wait for other threads to finish).
7981 mono_domain_finalize (domain, -1);
7983 mono_runtime_cleanup (domain);
7985 mono_profiler_shutdown ();
7987 mono_debug_cleanup ();
7989 #ifdef PLATFORM_WIN32
7990 win32_seh_cleanup();
7993 mono_domain_free (domain, TRUE);
7999 mono_set_defaults (int verbose_level, guint32 opts)
8001 mini_verbose = verbose_level;
8006 mono_precompile_assembly (MonoAssembly *ass, void *user_data)
8008 MonoImage *image = ass->image;
8012 if (mini_verbose > 0)
8013 printf ("PRECOMPILE: %s.\n", ass->image->name);
8015 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
8016 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
8017 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
8019 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
8023 if (mini_verbose > 1) {
8024 char * desc = mono_method_full_name (method, TRUE);
8025 g_print ("Compiling %d %s\n", count, desc);
8028 mono_compile_method (method);
8032 void mono_precompile_assemblies ()
8034 mono_assembly_foreach ((GFunc)mono_precompile_assembly, NULL);