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;
102 static guint32 default_opt = MONO_OPT_PEEPHOLE;
104 guint32 mono_jit_tls_id = -1;
105 MonoTraceSpec *mono_jit_trace_calls = NULL;
106 gboolean mono_break_on_exc = FALSE;
107 gboolean mono_compile_aot = FALSE;
109 static int mini_verbose = 0;
111 static CRITICAL_SECTION class_init_hash_mutex;
113 static GHashTable *class_init_hash_addr = NULL;
115 #ifdef MONO_USE_EXC_TABLES
117 mono_type_blittable (MonoType *type)
137 case MONO_TYPE_VALUETYPE:
138 case MONO_TYPE_CLASS:
139 return type->data.klass->blittable;
149 mono_method_blittable (MonoMethod *method)
151 MonoMethodSignature *sig;
157 if (!mono_arch_has_unwind_info (method->addr)) {
161 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
164 sig = method->signature;
166 if (!mono_type_blittable (sig->ret))
169 for (i = 0; i < sig->param_count; i++)
170 if (!mono_type_blittable (sig->params [i]))
173 if (mono_method_has_marshal_info (method))
176 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && ((MonoMethodPInvoke*)method)->piflags & PINVOKE_ATTRIBUTE_SUPPORTS_LAST_ERROR)
184 G_GNUC_UNUSED static void
185 print_method_from_ip (void *ip)
190 MonoDomain *domain = mono_domain_get ();
192 ji = mono_jit_info_table_find (domain, ip);
194 g_print ("No method at %p\n", ip);
197 method = mono_method_full_name (ji->method, TRUE);
198 source = mono_debug_source_location_from_address (ji->method, (int) ip, NULL, domain);
200 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);
203 g_print ("%s\n", source);
211 * mono_method_same_domain:
213 * Determine whenever two compiled methods are in the same domain, thus
214 * the address of the callee can be embedded in the caller.
216 gboolean mono_method_same_domain (MonoJitInfo *caller, MonoJitInfo *callee)
218 if (!caller || !callee)
222 * If the call was made from domain-neutral to domain-specific
223 * code, we can't patch the call site.
225 if (caller->domain_neutral && !callee->domain_neutral)
228 if ((caller->method->klass == mono_defaults.appdomain_class) &&
229 (strstr (caller->method->name, "InvokeInDomain"))) {
230 /* The InvokeInDomain methods change the current appdomain */
238 mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token)
240 MonoJumpInfoToken *res = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoToken));
247 #define MONO_INIT_VARINFO(vi,id) do { \
248 (vi)->range.first_use.pos.bid = 0xffff; \
254 * Basic blocks have two numeric identifiers:
255 * dfn: Depth First Number
256 * block_num: unique ID assigned at bblock creation
258 #define NEW_BBLOCK(cfg) (mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)))
259 #define ADD_BBLOCK(cfg,bbhash,b) do { \
260 g_hash_table_insert (bbhash, (b)->cil_code, (b)); \
261 (b)->block_num = cfg->num_bblocks++; \
262 (b)->real_offset = real_offset; \
265 #define GET_BBLOCK(cfg,bbhash,tblock,ip) do { \
266 (tblock) = g_hash_table_lookup (bbhash, (ip)); \
268 if ((ip) >= end || (ip) < header->code) goto unverified; \
269 (tblock) = NEW_BBLOCK (cfg); \
270 (tblock)->cil_code = (ip); \
271 ADD_BBLOCK (cfg, (bbhash), (tblock)); \
273 (tblock)->real_offset = real_offset; \
276 #define CHECK_BBLOCK(target,ip,tblock) do { \
277 if ((target) < (ip) && !(tblock)->code) { \
278 bb_recheck = g_list_prepend (bb_recheck, (tblock)); \
279 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); \
283 #define NEW_ICONST(cfg,dest,val) do { \
284 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
285 (dest)->opcode = OP_ICONST; \
286 (dest)->inst_c0 = (val); \
287 (dest)->type = STACK_I4; \
290 /* FIXME: have a different definition of NEW_PCONST for 64 bit systems */
291 #define NEW_PCONST(cfg,dest,val) do { \
292 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
293 (dest)->opcode = OP_ICONST; \
294 (dest)->inst_p0 = (val); \
295 (dest)->type = STACK_PTR; \
298 #define NEW_CLASSCONST(cfg,dest,val) do { \
299 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
300 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
301 (dest)->inst_p0 = (val); \
302 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_CLASS; \
303 (dest)->type = STACK_PTR; \
306 #define NEW_IMAGECONST(cfg,dest,val) do { \
307 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
308 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
309 (dest)->inst_p0 = (val); \
310 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_IMAGE; \
311 (dest)->type = STACK_PTR; \
314 #define NEW_FIELDCONST(cfg,dest,field) do { \
315 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
316 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
317 (dest)->inst_p0 = (field); \
318 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_FIELD; \
319 (dest)->type = STACK_PTR; \
322 #define NEW_METHODCONST(cfg,dest,val) do { \
323 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
324 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
325 (dest)->inst_p0 = (val); \
326 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_METHODCONST; \
327 (dest)->type = STACK_PTR; \
330 #define NEW_VTABLECONST(cfg,dest,vtable) do { \
331 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
332 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
333 (dest)->inst_p0 = mono_compile_aot ? (gpointer)((vtable)->klass) : (vtable); \
334 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_VTABLE; \
335 (dest)->type = STACK_PTR; \
338 #define NEW_SFLDACONST(cfg,dest,field) do { \
339 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
340 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
341 (dest)->inst_p0 = (field); \
342 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_SFLDA; \
343 (dest)->type = STACK_PTR; \
346 #define NEW_LDSTRCONST(cfg,dest,image,token) do { \
347 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
348 (dest)->opcode = OP_AOTCONST; \
349 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
350 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_LDSTR; \
351 (dest)->type = STACK_OBJ; \
354 #define NEW_TYPE_FROM_HANDLE_CONST(cfg,dest,image,token) do { \
355 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
356 (dest)->opcode = OP_AOTCONST; \
357 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
358 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_TYPE_FROM_HANDLE; \
359 (dest)->type = STACK_OBJ; \
362 #define NEW_LDTOKENCONST(cfg,dest,image,token) do { \
363 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
364 (dest)->opcode = OP_AOTCONST; \
365 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
366 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_LDTOKEN; \
367 (dest)->type = STACK_PTR; \
370 #define NEW_DOMAINCONST(cfg,dest) do { \
371 if (cfg->opt & MONO_OPT_SHARED) { \
372 NEW_TEMPLOAD (cfg, dest, mono_get_domainvar (cfg)->inst_c0); \
374 NEW_PCONST (cfg, dest, (cfg)->domain); \
378 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
380 #define NEW_ARGLOAD(cfg,dest,num) do { \
381 if (arg_array [(num)]->opcode == OP_ICONST) (dest) = arg_array [(num)]; else { \
382 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
383 (dest)->ssa_op = MONO_SSA_LOAD; \
384 (dest)->inst_i0 = arg_array [(num)]; \
385 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
386 type_to_eval_stack_type (param_types [(num)], (dest)); \
387 (dest)->klass = (dest)->inst_i0->klass; \
390 #define NEW_LOCLOAD(cfg,dest,num) do { \
391 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
392 (dest)->ssa_op = MONO_SSA_LOAD; \
393 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
394 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
395 type_to_eval_stack_type (header->locals [(num)], (dest)); \
396 (dest)->klass = (dest)->inst_i0->klass; \
399 #define NEW_LOCLOADA(cfg,dest,num) do { \
400 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
401 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
402 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
403 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
404 (dest)->opcode = OP_LDADDR; \
405 (dest)->type = STACK_MP; \
406 (dest)->klass = (dest)->inst_i0->klass; \
407 (cfg)->disable_ssa = TRUE; \
410 #define NEW_RETLOADA(cfg,dest) do { \
411 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
412 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
413 (dest)->inst_i0 = (cfg)->ret; \
414 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
415 (dest)->opcode = CEE_LDIND_I; \
416 (dest)->type = STACK_MP; \
417 (dest)->klass = (dest)->inst_i0->klass; \
418 (cfg)->disable_ssa = TRUE; \
421 #define NEW_ARGLOADA(cfg,dest,num) do { \
422 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
423 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
424 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
425 (dest)->inst_i0 = arg_array [(num)]; \
426 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
427 (dest)->opcode = OP_LDADDR; \
428 (dest)->type = STACK_MP; \
429 (dest)->klass = (dest)->inst_i0->klass; \
430 (cfg)->disable_ssa = TRUE; \
433 #define NEW_TEMPLOAD(cfg,dest,num) do { \
434 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
435 (dest)->ssa_op = MONO_SSA_LOAD; \
436 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
437 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
438 type_to_eval_stack_type ((dest)->inst_i0->inst_vtype, (dest)); \
439 (dest)->klass = (dest)->inst_i0->klass; \
442 #define NEW_TEMPLOADA(cfg,dest,num) do { \
443 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
444 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
445 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
446 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
447 (dest)->opcode = OP_LDADDR; \
448 (dest)->type = STACK_MP; \
449 (dest)->klass = (dest)->inst_i0->klass; \
450 (cfg)->disable_ssa = TRUE; \
454 #define NEW_INDLOAD(cfg,dest,addr,vtype) do { \
455 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
456 (dest)->inst_left = addr; \
457 (dest)->opcode = mono_type_to_ldind (vtype); \
458 type_to_eval_stack_type (vtype, (dest)); \
459 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
462 #define NEW_INDSTORE(cfg,dest,addr,value,vtype) do { \
463 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
464 (dest)->inst_i0 = addr; \
465 (dest)->opcode = mono_type_to_stind (vtype); \
466 (dest)->inst_i1 = (value); \
467 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
470 #define NEW_TEMPSTORE(cfg,dest,num,inst) do { \
471 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
472 (dest)->ssa_op = MONO_SSA_STORE; \
473 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
474 (dest)->opcode = mono_type_to_stind ((dest)->inst_i0->inst_vtype); \
475 (dest)->inst_i1 = (inst); \
476 (dest)->klass = (dest)->inst_i0->klass; \
479 #define NEW_LOCSTORE(cfg,dest,num,inst) do { \
480 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
481 (dest)->opcode = mono_type_to_stind (header->locals [(num)]); \
482 (dest)->ssa_op = MONO_SSA_STORE; \
483 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
484 (dest)->inst_i1 = (inst); \
485 (dest)->klass = (dest)->inst_i0->klass; \
488 #define NEW_ARGSTORE(cfg,dest,num,inst) do { \
489 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
490 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
491 (dest)->opcode = mono_type_to_stind (param_types [(num)]); \
492 (dest)->ssa_op = MONO_SSA_STORE; \
493 (dest)->inst_i0 = arg_array [(num)]; \
494 (dest)->inst_i1 = (inst); \
495 (dest)->klass = (dest)->inst_i0->klass; \
498 #define ADD_BINOP(op) do { \
499 MONO_INST_NEW (cfg, ins, (op)); \
500 ins->cil_code = ip; \
502 ins->inst_i0 = sp [0]; \
503 ins->inst_i1 = sp [1]; \
505 type_from_op (ins); \
509 #define ADD_UNOP(op) do { \
510 MONO_INST_NEW (cfg, ins, (op)); \
511 ins->cil_code = ip; \
513 ins->inst_i0 = sp [0]; \
515 type_from_op (ins); \
519 #define ADD_BINCOND(next_block) do { \
521 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
523 cmp->inst_i0 = sp [0]; \
524 cmp->inst_i1 = sp [1]; \
525 cmp->cil_code = ins->cil_code; \
526 type_from_op (cmp); \
528 ins->inst_i0 = cmp; \
529 MONO_ADD_INS (bblock, ins); \
530 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
531 GET_BBLOCK (cfg, bbhash, tblock, target); \
532 link_bblock (cfg, bblock, tblock); \
533 ins->inst_true_bb = tblock; \
534 CHECK_BBLOCK (target, ip, tblock); \
535 if ((next_block)) { \
536 link_bblock (cfg, bblock, (next_block)); \
537 ins->inst_false_bb = (next_block); \
538 start_new_bblock = 1; \
540 GET_BBLOCK (cfg, bbhash, tblock, ip); \
541 link_bblock (cfg, bblock, tblock); \
542 ins->inst_false_bb = tblock; \
543 start_new_bblock = 2; \
547 /* FIXME: handle float, long ... */
548 #define ADD_UNCOND(istrue) do { \
550 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
552 cmp->inst_i0 = sp [0]; \
553 switch (cmp->inst_i0->type) { \
555 cmp->inst_i1 = zero_int64; break; \
557 cmp->inst_i1 = zero_r8; break; \
560 cmp->inst_i1 = zero_ptr; break; \
562 cmp->inst_i1 = zero_obj; break; \
564 cmp->inst_i1 = zero_int32; \
566 cmp->cil_code = ins->cil_code; \
567 type_from_op (cmp); \
569 ins->inst_i0 = cmp; \
570 ins->opcode = (istrue)? CEE_BNE_UN: CEE_BEQ; \
571 MONO_ADD_INS (bblock, ins); \
572 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
573 GET_BBLOCK (cfg, bbhash, tblock, target); \
574 link_bblock (cfg, bblock, tblock); \
575 ins->inst_true_bb = tblock; \
576 CHECK_BBLOCK (target, ip, tblock); \
577 GET_BBLOCK (cfg, bbhash, tblock, ip); \
578 link_bblock (cfg, bblock, tblock); \
579 ins->inst_false_bb = tblock; \
580 start_new_bblock = 2; \
583 #define NEW_LDELEMA(cfg,dest,sp,k) do { \
584 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
585 (dest)->opcode = CEE_LDELEMA; \
586 (dest)->inst_left = (sp) [0]; \
587 (dest)->inst_right = (sp) [1]; \
588 (dest)->type = STACK_MP; \
589 (dest)->klass = (k); \
592 #define NEW_GROUP(cfg,dest,el1,el2) do { \
593 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
594 (dest)->opcode = OP_GROUP; \
595 (dest)->inst_left = (el1); \
596 (dest)->inst_right = (el2); \
601 compare_bblock (gconstpointer a, gconstpointer b)
603 const MonoBasicBlock *b1 = a;
604 const MonoBasicBlock *b2 = b;
606 return b2->cil_code - b1->cil_code;
611 * link_bblock: Links two basic blocks
613 * links two basic blocks in the control flow graph, the 'from'
614 * argument is the starting block and the 'to' argument is the block
615 * the control flow ends to after 'from'.
618 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
620 MonoBasicBlock **newa;
624 if (from->cil_code) {
626 g_print ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
628 g_print ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
631 g_print ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
633 g_print ("edge from entry to exit\n");
637 for (i = 0; i < from->out_count; ++i) {
638 if (to == from->out_bb [i]) {
644 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
645 for (i = 0; i < from->out_count; ++i) {
646 newa [i] = from->out_bb [i];
654 for (i = 0; i < to->in_count; ++i) {
655 if (from == to->in_bb [i]) {
661 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
662 for (i = 0; i < to->in_count; ++i) {
663 newa [i] = to->in_bb [i];
672 * We mark each basic block with a region ID. We use that to avoid BB
673 * optimizations when blocks are in different regions.
676 mono_find_block_region (MonoCompile *cfg, int offset, int *filter_lengths)
678 MonoMethod *method = cfg->method;
679 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
680 MonoExceptionClause *clause;
683 /* first search for handlers and filters */
684 for (i = 0; i < header->num_clauses; ++i) {
685 clause = &header->clauses [i];
686 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->token_or_filter) &&
687 (offset < (clause->token_or_filter + filter_lengths [i])))
688 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
690 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
691 if (clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)
692 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
694 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
698 /* search the try blocks */
699 for (i = 0; i < header->num_clauses; ++i) {
700 clause = &header->clauses [i];
701 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
702 return ((i + 1) << 8) | clause->flags;
709 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
711 MonoMethod *method = cfg->method;
712 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
713 MonoExceptionClause *clause;
714 MonoBasicBlock *handler;
718 for (i = 0; i < header->num_clauses; ++i) {
719 clause = &header->clauses [i];
720 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
721 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
722 if (clause->flags == type) {
723 handler = g_hash_table_lookup (cfg->bb_hash, header->code + clause->handler_offset);
725 res = g_list_append (res, handler);
733 mono_find_spvar_for_region (MonoCompile *cfg, int region)
735 return g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
739 mono_create_spvar_for_region (MonoCompile *cfg, int region)
743 var = g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
747 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
748 /* prevent it from being register allocated */
749 var->flags |= MONO_INST_INDIRECT;
751 g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
755 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
759 array [*dfn] = start;
760 /*g_print ("visit %d at %p\n", *dfn, start->cil_code);*/
761 for (i = 0; i < start->out_count; ++i) {
762 if (start->out_bb [i]->dfn)
765 start->out_bb [i]->dfn = *dfn;
766 start->out_bb [i]->df_parent = start;
767 array [*dfn] = start->out_bb [i];
768 df_visit (start->out_bb [i], dfn, array);
774 MonoBasicBlock *best;
778 previous_foreach (gconstpointer key, gpointer val, gpointer data)
780 PrevStruct *p = data;
781 MonoBasicBlock *bb = val;
782 //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,
783 //bb->method == p->best->method, bb->cil_code < p->code, bb->cil_code > p->best->cil_code);
785 if (bb->cil_code && bb->cil_code < p->code && bb->cil_code > p->best->cil_code)
789 static MonoBasicBlock*
790 find_previous (GHashTable *bb_hash, MonoBasicBlock *start, const guchar *code) {
796 g_hash_table_foreach (bb_hash, (GHFunc)previous_foreach, &p);
801 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
810 * FIXME: take into account all the details:
811 * second may have been the target of more than one bblock
813 second->out_count = first->out_count;
814 second->out_bb = first->out_bb;
816 for (i = 0; i < first->out_count; ++i) {
817 bb = first->out_bb [i];
818 for (j = 0; j < bb->in_count; ++j) {
819 if (bb->in_bb [j] == first)
820 bb->in_bb [j] = second;
824 first->out_count = 0;
825 first->out_bb = NULL;
826 link_bblock (cfg, first, second);
828 second->last_ins = first->last_ins;
830 /*g_print ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
831 for (inst = first->code; inst && inst->next; inst = inst->next) {
832 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
833 g_print ("found %p: %s", inst->next->cil_code, code);
835 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
836 second->code = inst->next;
838 first->last_ins = inst;
839 second->next_bb = first->next_bb;
840 first->next_bb = second;
845 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
851 mono_type_to_ldind (MonoType *type)
863 case MONO_TYPE_BOOLEAN:
877 case MONO_TYPE_FNPTR:
879 case MONO_TYPE_CLASS:
880 case MONO_TYPE_STRING:
881 case MONO_TYPE_OBJECT:
882 case MONO_TYPE_SZARRAY:
883 case MONO_TYPE_ARRAY:
884 return CEE_LDIND_REF;
892 case MONO_TYPE_VALUETYPE:
893 if (type->data.klass->enumtype) {
894 t = type->data.klass->enum_basetype->type;
898 case MONO_TYPE_TYPEDBYREF:
900 case MONO_TYPE_GENERICINST:
901 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE)
903 return CEE_LDIND_REF;
905 g_error ("unknown type 0x%02x in type_to_ldind", type->type);
911 mono_type_to_stind (MonoType *type)
922 case MONO_TYPE_BOOLEAN:
934 case MONO_TYPE_FNPTR:
936 case MONO_TYPE_CLASS:
937 case MONO_TYPE_STRING:
938 case MONO_TYPE_OBJECT:
939 case MONO_TYPE_SZARRAY:
940 case MONO_TYPE_ARRAY:
941 return CEE_STIND_REF;
949 case MONO_TYPE_VALUETYPE:
950 if (type->data.klass->enumtype) {
951 t = type->data.klass->enum_basetype->type;
955 case MONO_TYPE_TYPEDBYREF:
957 case MONO_TYPE_GENERICINST:
958 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE)
960 return CEE_STIND_REF;
962 g_error ("unknown type 0x%02x in type_to_stind", type->type);
968 * Returns the type used in the eval stack when @type is loaded.
969 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
972 type_to_eval_stack_type (MonoType *type, MonoInst *inst) {
976 inst->type = STACK_MP;
984 case MONO_TYPE_BOOLEAN:
990 inst->type = STACK_I4;
995 case MONO_TYPE_FNPTR:
996 inst->type = STACK_PTR;
998 case MONO_TYPE_CLASS:
999 case MONO_TYPE_STRING:
1000 case MONO_TYPE_OBJECT:
1001 case MONO_TYPE_SZARRAY:
1002 case MONO_TYPE_ARRAY:
1003 inst->type = STACK_OBJ;
1007 inst->type = STACK_I8;
1011 inst->type = STACK_R8;
1013 case MONO_TYPE_VALUETYPE:
1014 if (type->data.klass->enumtype) {
1015 t = type->data.klass->enum_basetype->type;
1018 inst->klass = type->data.klass;
1019 inst->type = STACK_VTYPE;
1022 case MONO_TYPE_TYPEDBYREF:
1023 inst->klass = mono_defaults.typed_reference_class;
1024 inst->type = STACK_VTYPE;
1026 case MONO_TYPE_GENERICINST:
1027 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE) {
1028 inst->klass = mono_class_from_mono_type (type);
1029 inst->type = STACK_VTYPE;
1031 inst->type = STACK_OBJ;
1035 g_error ("unknown type 0x%02x in eval stack type", type->type);
1040 * The following tables are used to quickly validate the IL code in type_from_op ().
1043 bin_num_table [STACK_MAX] [STACK_MAX] = {
1044 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1045 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1046 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1047 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1048 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
1049 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
1050 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1051 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1056 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
1059 /* reduce the size of this table */
1061 bin_int_table [STACK_MAX] [STACK_MAX] = {
1062 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1063 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1064 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1065 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1066 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, 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}
1073 bin_comp_table [STACK_MAX] [STACK_MAX] = {
1075 {0, 1, 0, 1, 0, 0, 4, 0},
1076 {0, 0, 1, 0, 0, 0, 0, 0},
1077 {0, 1, 0, 1, 0, 2, 4, 0},
1078 {0, 0, 0, 0, 1, 0, 0, 0},
1079 {0, 0, 0, 2, 0, 1, 0, 0},
1080 {0, 4, 0, 4, 0, 0, 3, 0},
1081 {0, 0, 0, 0, 0, 0, 0, 0},
1084 /* reduce the size of this table */
1086 shift_table [STACK_MAX] [STACK_MAX] = {
1087 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1088 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1089 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1090 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1091 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, 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}
1098 * Tables to map from the non-specific opcode to the matching
1099 * type-specific opcode.
1101 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
1102 static const guint16
1103 binops_op_map [STACK_MAX] = {
1104 0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, 0
1107 /* handles from CEE_NEG to CEE_CONV_U8 */
1108 static const guint16
1109 unops_op_map [STACK_MAX] = {
1110 0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, 0
1113 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
1114 static const guint16
1115 ovfops_op_map [STACK_MAX] = {
1116 0, 0, OP_LCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_FCONV_TO_U2-CEE_CONV_U2, 0
1119 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
1120 static const guint16
1121 ovf2ops_op_map [STACK_MAX] = {
1122 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
1125 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
1126 static const guint16
1127 ovf3ops_op_map [STACK_MAX] = {
1128 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
1131 /* handles from CEE_CEQ to CEE_CLT_UN */
1132 static const guint16
1133 ceqops_op_map [STACK_MAX] = {
1134 0, 0, OP_LCEQ-CEE_CEQ, OP_PCEQ-CEE_CEQ, OP_FCEQ-CEE_CEQ, 0
1138 * Sets ins->type (the type on the eval stack) according to the
1139 * type of the opcode and the arguments to it.
1140 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1142 * FIXME: this function sets ins->type unconditionally in some cases, but
1143 * it should set it to invalid for some types (a conv.x on an object)
1146 type_from_op (MonoInst *ins) {
1147 switch (ins->opcode) {
1154 /* FIXME: check unverifiable args for STACK_MP */
1155 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1156 ins->opcode += binops_op_map [ins->type];
1163 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1164 ins->opcode += binops_op_map [ins->type];
1169 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1170 ins->opcode += binops_op_map [ins->type];
1173 /* FIXME: handle some specifics with ins->next->type */
1174 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1178 case 256+CEE_CGT_UN:
1180 case 256+CEE_CLT_UN:
1181 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1182 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1186 ins->type = neg_table [ins->inst_i0->type];
1187 ins->opcode += unops_op_map [ins->type];
1190 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1191 ins->type = ins->inst_i0->type;
1193 ins->type = STACK_INV;
1194 ins->opcode += unops_op_map [ins->type];
1200 ins->type = STACK_I4;
1201 ins->opcode += unops_op_map [ins->inst_i0->type];
1204 ins->type = STACK_R8;
1205 switch (ins->inst_i0->type) {
1210 ins->opcode = OP_LCONV_TO_R_UN;
1214 case CEE_CONV_OVF_I1:
1215 case CEE_CONV_OVF_U1:
1216 case CEE_CONV_OVF_I2:
1217 case CEE_CONV_OVF_U2:
1218 case CEE_CONV_OVF_I4:
1219 case CEE_CONV_OVF_U4:
1220 ins->type = STACK_I4;
1221 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1223 case CEE_CONV_OVF_I_UN:
1224 case CEE_CONV_OVF_U_UN:
1225 ins->type = STACK_PTR;
1226 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1228 case CEE_CONV_OVF_I1_UN:
1229 case CEE_CONV_OVF_I2_UN:
1230 case CEE_CONV_OVF_I4_UN:
1231 case CEE_CONV_OVF_U1_UN:
1232 case CEE_CONV_OVF_U2_UN:
1233 case CEE_CONV_OVF_U4_UN:
1234 ins->type = STACK_I4;
1235 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1238 ins->type = STACK_PTR;
1239 switch (ins->inst_i0->type) {
1245 ins->opcode = OP_LCONV_TO_U;
1248 ins->opcode = OP_FCONV_TO_U;
1254 ins->type = STACK_I8;
1255 ins->opcode += unops_op_map [ins->inst_i0->type];
1257 case CEE_CONV_OVF_I8:
1258 case CEE_CONV_OVF_U8:
1259 ins->type = STACK_I8;
1260 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1262 case CEE_CONV_OVF_U8_UN:
1263 case CEE_CONV_OVF_I8_UN:
1264 ins->type = STACK_I8;
1265 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1269 ins->type = STACK_R8;
1270 ins->opcode += unops_op_map [ins->inst_i0->type];
1273 ins->type = STACK_R8;
1277 ins->type = STACK_I4;
1278 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1281 case CEE_CONV_OVF_I:
1282 case CEE_CONV_OVF_U:
1283 ins->type = STACK_PTR;
1284 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1287 case CEE_ADD_OVF_UN:
1289 case CEE_MUL_OVF_UN:
1291 case CEE_SUB_OVF_UN:
1292 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1293 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1296 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1303 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_MP, STACK_R8, STACK_R8, STACK_OBJ
1306 /* map ldelem.x to the matching ldind.x opcode */
1308 ldelem_to_ldind [] = {
1322 /* map stelem.x to the matching stind.x opcode */
1324 stelem_to_stind [] = {
1338 param_table [STACK_MAX] [STACK_MAX] = {
1343 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1347 switch (args->type) {
1357 for (i = 0; i < sig->param_count; ++i) {
1358 switch (args [i].type) {
1362 if (!sig->params [i]->byref)
1366 if (sig->params [i]->byref)
1368 switch (sig->params [i]->type) {
1369 case MONO_TYPE_CLASS:
1370 case MONO_TYPE_STRING:
1371 case MONO_TYPE_OBJECT:
1372 case MONO_TYPE_SZARRAY:
1373 case MONO_TYPE_ARRAY:
1380 if (sig->params [i]->byref)
1382 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1391 /*if (!param_table [args [i].type] [sig->params [i]->type])
1399 * When we need a pointer to the current domain many times in a method, we
1400 * call mono_domain_get() once and we store the result in a local variable.
1401 * This function returns the variable that represents the MonoDomain*.
1403 inline static MonoInst *
1404 mono_get_domainvar (MonoCompile *cfg)
1406 if (!cfg->domainvar)
1407 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1408 return cfg->domainvar;
1412 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
1415 int num = cfg->num_varinfo;
1417 if ((num + 1) >= cfg->varinfo_count) {
1418 cfg->varinfo_count = (cfg->varinfo_count + 2) * 2;
1419 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
1420 cfg->vars = (MonoMethodVar **)g_realloc (cfg->vars, sizeof (MonoMethodVar*) * cfg->varinfo_count);
1423 /*g_print ("created temp %d of type 0x%x\n", num, type->type);*/
1424 mono_jit_stats.allocate_var++;
1426 MONO_INST_NEW (cfg, inst, opcode);
1427 inst->inst_c0 = num;
1428 inst->inst_vtype = type;
1429 inst->klass = mono_class_from_mono_type (type);
1430 /* if set to 1 the variable is native */
1433 cfg->varinfo [num] = inst;
1435 cfg->vars [num] = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMethodVar));
1436 MONO_INIT_VARINFO (cfg->vars [num], num);
1439 //g_print ("created temp %d of type %s\n", num, mono_type_get_name (type));
1444 type_from_stack_type (MonoInst *ins) {
1445 switch (ins->type) {
1446 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1447 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1448 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1449 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1450 case STACK_MP: return &mono_defaults.int_class->byval_arg;
1451 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1452 case STACK_VTYPE: return &ins->klass->byval_arg;
1454 g_error ("stack type %d to montype not handled\n", ins->type);
1460 array_access_to_klass (int opcode)
1464 return mono_defaults.byte_class;
1466 return mono_defaults.uint16_class;
1469 return mono_defaults.int_class;
1472 return mono_defaults.sbyte_class;
1475 return mono_defaults.int16_class;
1478 return mono_defaults.int32_class;
1480 return mono_defaults.uint32_class;
1483 return mono_defaults.int64_class;
1486 return mono_defaults.single_class;
1489 return mono_defaults.double_class;
1490 case CEE_LDELEM_REF:
1491 case CEE_STELEM_REF:
1492 return mono_defaults.object_class;
1494 g_assert_not_reached ();
1500 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
1504 MONO_ADD_INS (bb, inst);
1507 switch (bb->last_ins->opcode) {
1520 while (prev->next && prev->next != bb->last_ins)
1522 if (prev == bb->code) {
1523 if (bb->last_ins == bb->code) {
1524 inst->next = bb->code;
1527 inst->next = prev->next;
1531 inst->next = bb->last_ins;
1535 // g_warning ("handle conditional jump in add_ins_to_end ()\n");
1537 MONO_ADD_INS (bb, inst);
1543 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
1545 MonoInst *inst, *load;
1547 NEW_TEMPLOAD (cfg, load, src);
1549 NEW_TEMPSTORE (cfg, inst, dest, load);
1550 if (inst->opcode == CEE_STOBJ) {
1551 NEW_TEMPLOADA (cfg, inst, dest);
1552 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE);
1554 inst->cil_code = NULL;
1555 mono_add_ins_to_end (bb, inst);
1560 * We try to share variables when possible
1563 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1568 /* inlining can result in deeper stacks */
1569 if (slot >= ((MonoMethodNormal *)cfg->method)->header->max_stack)
1570 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1572 pos = ins->type - 1 + slot * STACK_MAX;
1574 switch (ins->type) {
1581 if ((vnum = cfg->intvars [pos]))
1582 return cfg->varinfo [vnum];
1583 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1584 cfg->intvars [pos] = res->inst_c0;
1587 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1593 * This function is called to handle items that are left on the evaluation stack
1594 * at basic block boundaries. What happens is that we save the values to local variables
1595 * and we reload them later when first entering the target basic block (with the
1596 * handle_loaded_temps () function).
1597 * A single joint point will use the same variables (stored in the array bb->out_stack or
1598 * bb->in_stack, if the basic block is before or after the joint point).
1601 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count) {
1603 MonoBasicBlock *outb;
1604 MonoInst *inst, **locals;
1608 if (cfg->verbose_level > 3)
1609 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
1610 if (!bb->out_scount) {
1612 bb->out_scount = count;
1613 //g_print ("bblock %d has out:", bb->block_num);
1614 for (i = 0; i < bb->out_count; ++i) {
1615 outb = bb->out_bb [i];
1616 //g_print (" %d", outb->block_num);
1617 if (outb->in_stack) {
1619 bb->out_stack = outb->in_stack;
1625 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1626 for (i = 0; i < count; ++i) {
1628 /* try to reuse temps already allocated for this purpouse, if they occupy the same
1629 * stack slot and if they are of the same type. */
1630 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1632 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1637 locals = bb->out_stack;
1638 for (i = 0; i < count; ++i) {
1639 /* add store ops at the end of the bb, before the branch */
1640 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1641 if (inst->opcode == CEE_STOBJ) {
1642 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
1643 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE);
1645 inst->cil_code = sp [i]->cil_code;
1646 mono_add_ins_to_end (bb, inst);
1648 if (cfg->verbose_level > 3)
1649 g_print ("storing %d to temp %d\n", i, locals [i]->inst_c0);
1652 for (i = 0; i < bb->out_count; ++i) {
1653 outb = bb->out_bb [i];
1654 if (outb->in_scount)
1655 continue; /* check they are the same locals */
1656 outb->in_scount = count;
1657 outb->in_stack = locals;
1663 ret_type_to_call_opcode (MonoType *type, int calli, int virt)
1668 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1672 case MONO_TYPE_VOID:
1673 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
1676 case MONO_TYPE_BOOLEAN:
1679 case MONO_TYPE_CHAR:
1682 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1686 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1687 case MONO_TYPE_CLASS:
1688 case MONO_TYPE_STRING:
1689 case MONO_TYPE_OBJECT:
1690 case MONO_TYPE_SZARRAY:
1691 case MONO_TYPE_ARRAY:
1692 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1695 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
1698 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
1699 case MONO_TYPE_VALUETYPE:
1700 if (type->data.klass->enumtype) {
1701 t = type->data.klass->enum_basetype->type;
1704 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1705 case MONO_TYPE_TYPEDBYREF:
1706 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1708 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
1714 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
1716 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
1718 ji->ip.label = label;
1719 ji->type = MONO_PATCH_INFO_SWITCH;
1720 ji->data.table = bbs;
1721 ji->next = cfg->patch_info;
1722 ji->table_size = num_blocks;
1723 cfg->patch_info = ji;
1727 * When we add a tree of instructions, we need to ensure the instructions currently
1728 * on the stack are executed before (like, if we load a value from a local).
1729 * We ensure this by saving the currently loaded values to temps and rewriting the
1730 * instructions to load the values.
1731 * This is not done for opcodes that terminate a basic block (because it's handled already
1732 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
1735 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
1737 MonoInst *load, *store, *temp, *ins;
1739 while (stack < sp) {
1741 /* handle also other constants */
1742 if (ins->opcode != OP_ICONST) {
1743 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1744 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1745 store->cil_code = ins->cil_code;
1746 if (store->opcode == CEE_STOBJ) {
1747 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
1748 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE);
1750 MONO_ADD_INS (bblock, store);
1751 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1752 load->cil_code = ins->cil_code;
1760 * Prepare arguments for passing to a function call.
1761 * Return a non-zero value if the arguments can't be passed to the given
1763 * The type checks are not yet complete and some conversions may need
1764 * casts on 32 or 64 bit architectures.
1767 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
1772 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
1776 for (i = 0; i < sig->param_count; ++i) {
1777 if (sig->params [i]->byref) {
1779 * check the result of ldelema is only passed as an argument if the byref
1780 * type matches exactly the array element type.
1781 * FIXME: if the argument as been saved on the stack as part of the
1782 * interface variable code (the value was on the stack at a basic block boundary)
1783 * we need to add the check in that case, too.
1785 if (args [i]->opcode == CEE_LDELEMA) {
1787 MonoClass *exact_class = mono_class_from_mono_type (sig->params [i]);
1788 if (!exact_class->valuetype) {
1789 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
1790 check->cil_code = args [i]->cil_code;
1791 check->klass = exact_class;
1792 check->inst_left = args [i]->inst_left;
1793 check->type = STACK_OBJ;
1794 args [i]->inst_left = check;
1797 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
1801 simple_type = sig->params [i]->type;
1803 switch (simple_type) {
1804 case MONO_TYPE_VOID:
1809 case MONO_TYPE_BOOLEAN:
1812 case MONO_TYPE_CHAR:
1815 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
1821 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
1824 case MONO_TYPE_CLASS:
1825 case MONO_TYPE_STRING:
1826 case MONO_TYPE_OBJECT:
1827 case MONO_TYPE_SZARRAY:
1828 case MONO_TYPE_ARRAY:
1829 if (args [i]->type != STACK_OBJ)
1834 if (args [i]->type != STACK_I8)
1839 if (args [i]->type != STACK_R8)
1842 case MONO_TYPE_VALUETYPE:
1843 if (sig->params [i]->data.klass->enumtype) {
1844 simple_type = sig->params [i]->data.klass->enum_basetype->type;
1847 if (args [i]->type != STACK_VTYPE)
1850 case MONO_TYPE_TYPEDBYREF:
1851 if (args [i]->type != STACK_VTYPE)
1854 case MONO_TYPE_GENERICINST:
1855 simple_type = sig->params [i]->data.generic_inst->generic_type->type;
1859 g_error ("unknown type 0x%02x in check_call_signature", simple_type);
1866 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
1867 const guint8 *ip, gboolean to_end)
1869 MonoInst *temp, *store, *ins = (MonoInst*)call;
1870 MonoType *ret = sig->ret;
1872 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
1874 call->inst.type = STACK_OBJ;
1875 call->inst.opcode = CEE_CALL;
1876 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
1878 type_to_eval_stack_type (ret, ins);
1879 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
1882 if (MONO_TYPE_ISSTRUCT (ret)) {
1885 /* we use this to allocate native sized structs */
1886 temp->unused = sig->pinvoke;
1888 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
1889 if (call->inst.opcode == OP_VCALL)
1890 ins->inst_left = loada;
1892 ins->inst_right = loada; /* a virtual or indirect call */
1895 mono_add_ins_to_end (bblock, ins);
1897 MONO_ADD_INS (bblock, ins);
1899 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1900 store->cil_code = ip;
1902 mono_add_ins_to_end (bblock, store);
1904 MONO_ADD_INS (bblock, store);
1906 return temp->inst_c0;
1909 mono_add_ins_to_end (bblock, ins);
1911 MONO_ADD_INS (bblock, ins);
1916 inline static MonoCallInst *
1917 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1918 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
1923 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual));
1925 call->inst.cil_code = ip;
1927 call->signature = sig;
1928 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
1930 for (arg = call->out_args; arg;) {
1931 MonoInst *narg = arg->next;
1936 mono_add_ins_to_end (bblock, arg);
1938 MONO_ADD_INS (bblock, arg);
1945 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1946 MonoInst **args, MonoInst *addr, const guint8 *ip)
1948 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
1950 call->inst.inst_i0 = addr;
1952 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
1955 static MonoCallInst*
1956 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
1957 MonoInst **args, const guint8 *ip, MonoInst *this)
1959 gboolean virtual = this != NULL;
1962 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, FALSE);
1964 if (this && sig->hasthis &&
1965 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
1966 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
1967 call->method = mono_marshal_get_remoting_invoke_with_check (method);
1969 call->method = method;
1971 call->inst.flags |= MONO_INST_HAS_METHOD;
1972 call->inst.inst_left = this;
1978 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
1979 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
1981 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, signature, args, ip, this);
1983 return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
1987 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
1988 MonoInst **args, const guint8 *ip, gboolean to_end)
1994 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
1996 return mono_spill_call (cfg, bblock, call, sig, func == mono_array_new_va, ip, to_end);
2000 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
2002 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
2005 g_warning ("unregistered JIT ICall");
2006 g_assert_not_reached ();
2009 return mono_emit_native_call (cfg, bblock, info->wrapper, info->sig, args, ip, FALSE);
2013 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
2015 MonoInst *ins, *temp = NULL, *store, *load, *begin;
2016 MonoInst *last_arg = NULL;
2020 //g_print ("emulating: ");
2021 //mono_print_tree_nl (tree);
2022 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE));
2023 ins = (MonoInst*)call;
2025 call->inst.cil_code = tree->cil_code;
2027 call->signature = info->sig;
2029 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
2031 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2032 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
2033 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2034 store->cil_code = tree->cil_code;
2039 nargs = info->sig->param_count + info->sig->hasthis;
2041 for (last_arg = call->out_args; last_arg && last_arg->next; last_arg = last_arg->next) ;
2044 last_arg->next = store;
2047 begin = call->out_args;
2051 if (cfg->prev_ins) {
2053 * This assumes that that in a tree, emulate_opcode is called for a
2054 * node before it is called for its children. dec_foreach needs to
2055 * take this into account.
2057 store->next = cfg->prev_ins->next;
2058 cfg->prev_ins->next = begin;
2060 store->next = cfg->cbb->code;
2061 cfg->cbb->code = begin;
2064 call->fptr = info->wrapper;
2066 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2067 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2072 static MonoMethodSignature *
2073 mono_get_element_address_signature (int arity)
2075 static GHashTable *sighash = NULL;
2076 MonoMethodSignature *res;
2080 sighash = g_hash_table_new (NULL, NULL);
2083 if ((res = g_hash_table_lookup (sighash, (gpointer)arity)))
2086 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
2088 res->params [0] = &mono_defaults.array_class->byval_arg;
2090 for (i = 1; i <= arity; i++)
2091 res->params [i] = &mono_defaults.int_class->byval_arg;
2093 res->ret = &mono_defaults.int_class->byval_arg;
2095 g_hash_table_insert (sighash, (gpointer)arity, res);
2101 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native) {
2102 MonoInst *iargs [3];
2108 * This check breaks with spilled vars... need to handle it during verification anyway.
2109 * g_assert (klass && klass == src->klass && klass == dest->klass);
2113 n = mono_class_native_size (klass, &align);
2115 n = mono_class_value_size (klass, &align);
2117 if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
2119 MONO_INST_NEW (cfg, inst, OP_MEMCPY);
2120 inst->inst_left = dest;
2121 inst->inst_right = src;
2122 inst->cil_code = ip;
2124 MONO_ADD_INS (bblock, inst);
2129 NEW_ICONST (cfg, iargs [2], n);
2131 mono_emit_native_call (cfg, bblock, helper_memcpy, helper_sig_memcpy, iargs, ip, to_end);
2135 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
2137 MonoInst *iargs [2];
2138 MonoInst *ins, *zero_int32;
2141 NEW_ICONST (cfg, zero_int32, 0);
2143 mono_class_init (klass);
2144 n = mono_class_value_size (klass, NULL);
2145 MONO_INST_NEW (cfg, ins, 0);
2147 ins->inst_left = dest;
2148 ins->inst_right = zero_int32;
2151 ins->opcode = CEE_STIND_I1;
2152 MONO_ADD_INS (bblock, ins);
2155 ins->opcode = CEE_STIND_I2;
2156 MONO_ADD_INS (bblock, ins);
2159 ins->opcode = CEE_STIND_I4;
2160 MONO_ADD_INS (bblock, ins);
2163 if (n <= sizeof (gpointer) * 5) {
2164 ins->opcode = OP_MEMSET;
2167 MONO_ADD_INS (bblock, ins);
2170 handle_loaded_temps (cfg, bblock, stack_start, sp);
2171 NEW_ICONST (cfg, ins, n);
2174 mono_emit_jit_icall (cfg, bblock, helper_initobj, iargs, ip);
2179 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
2182 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
2184 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
2185 MonoMethodSignature *signature = method->signature;
2189 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2190 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2191 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
2192 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
2193 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2194 (method->klass->marshalbyref) ||
2195 !header || header->num_clauses ||
2196 /* fixme: why cant we inline valuetype returns? */
2197 MONO_TYPE_ISSTRUCT (signature->ret))
2200 /* its not worth to inline methods with valuetype arguments?? */
2201 for (i = 0; i < signature->param_count; i++) {
2202 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
2208 * if we can initialize the class of the method right away, we do,
2209 * otherwise we don't allow inlining if the class needs initialization,
2210 * since it would mean inserting a call to mono_runtime_class_init()
2211 * inside the inlined code
2213 if (!(cfg->opt & MONO_OPT_SHARED)) {
2214 vtable = mono_class_vtable (cfg->domain, method->klass);
2215 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)
2216 mono_runtime_class_init (vtable);
2217 else if (!vtable->initialized && mono_class_needs_cctor_run (method->klass, NULL))
2221 * If we're compiling for shared code
2222 * the cctor will need to be run at aot method load time, for example,
2223 * or at the end of the compilation of the inlining method.
2225 if (mono_class_needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
2228 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
2230 /* also consider num_locals? */
2231 if (getenv ("MONO_INLINELIMIT"))
2232 if (header->code_size < atoi (getenv ("MONO_INLINELIMIT"))) {
2236 if (header->code_size < 20)
2243 mini_get_ldelema_ins (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
2247 MonoMethodSignature *esig;
2249 rank = cmethod->signature->param_count - (is_set? 1: 0);
2251 * FIXME: handle TypeMismatch for set or use the slow path
2254 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
2256 NEW_GROUP (cfg, indexes, sp [1], sp [2]);
2257 MONO_INST_NEW (cfg, addr, OP_LDELEMA2D);
2258 addr->inst_left = sp [0];
2259 addr->inst_right = indexes;
2260 addr->cil_code = ip;
2261 addr->type = STACK_MP;
2262 addr->klass = cmethod->klass;
2265 esig = mono_get_element_address_signature (rank);
2266 temp = mono_emit_native_call (cfg, bblock, ves_array_element_address, esig, sp, ip, FALSE);
2267 NEW_TEMPLOAD (cfg, addr, temp);
2272 mini_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
2277 if (cmethod->klass == mono_defaults.string_class) {
2278 if (cmethod->name [0] != 'g' || strcmp (cmethod->name, "get_Chars"))
2281 } else if (cmethod->klass == mono_defaults.math_class) {
2282 if (strcmp (cmethod->name, "Sin") == 0)
2284 else if (strcmp (cmethod->name, "Cos") == 0)
2286 else if (strcmp (cmethod->name, "Tan") == 0)
2288 else if (strcmp (cmethod->name, "Atan") == 0)
2290 else if (strcmp (cmethod->name, "Sqrt") == 0)
2292 else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8)
2295 /* OP_FREM is not IEEE compatible */
2296 else if (strcmp (cmethod->name, "IEEERemainder") == 0)
2301 } else if (cmethod->klass == mono_defaults.array_class) {
2302 if (strcmp (cmethod->name, "get_Rank") == 0)
2304 else if (strcmp (cmethod->name, "get_Length") == 0)
2311 pc = fsig->param_count + fsig->hasthis;
2312 MONO_INST_NEW (cfg, ins, op);
2315 ins->inst_i0 = args [0];
2317 ins->inst_i1 = args [1];
2324 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
2326 MonoInst *store, *temp;
2329 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
2331 if (!sig->hasthis && sig->param_count == 0)
2335 if (sp [0]->opcode == OP_ICONST) {
2338 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
2340 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2341 store->cil_code = sp [0]->cil_code;
2342 MONO_ADD_INS (bblock, store);
2347 for (i = 0; i < sig->param_count; ++i) {
2348 if (sp [0]->opcode == OP_ICONST) {
2351 temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
2353 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2354 store->cil_code = sp [0]->cil_code;
2355 if (store->opcode == CEE_STOBJ) {
2356 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2357 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE);
2359 MONO_ADD_INS (bblock, store);
2367 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
2368 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b)
2370 MonoInst *ins, *rvar = NULL;
2371 MonoMethodHeader *cheader;
2372 MonoBasicBlock *ebblock, *sbblock;
2373 int i, costs, new_locals_offset;
2375 if (cfg->verbose_level > 2)
2376 g_print ("INLINE START %p %s -> %s\n", cmethod, mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2378 cheader = ((MonoMethodNormal *)cmethod)->header;
2380 if (!cmethod->inline_info) {
2381 mono_jit_stats.inlineable_methods++;
2382 cmethod->inline_info = 1;
2384 /* allocate space to store the return value */
2385 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2386 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2389 /* allocate local variables */
2390 new_locals_offset = cfg->num_varinfo;
2391 for (i = 0; i < cheader->num_locals; ++i)
2392 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
2394 /* allocate starte and end blocks */
2395 sbblock = NEW_BBLOCK (cfg);
2396 sbblock->block_num = cfg->num_bblocks++;
2397 sbblock->real_offset = real_offset;
2399 ebblock = NEW_BBLOCK (cfg);
2400 ebblock->block_num = cfg->num_bblocks++;
2401 ebblock->real_offset = real_offset;
2403 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
2405 if (costs >= 0 && costs < 60) {
2406 if (cfg->verbose_level > 2)
2407 g_print ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2409 mono_jit_stats.inlined_methods++;
2411 /* always add some code to avoid block split failures */
2412 MONO_INST_NEW (cfg, ins, CEE_NOP);
2413 MONO_ADD_INS (bblock, ins);
2416 bblock->next_bb = sbblock;
2417 link_bblock (cfg, bblock, sbblock);
2420 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
2426 if (cfg->verbose_level > 2)
2427 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
2433 * Some of these comments may well be out-of-date.
2434 * Design decisions: we do a single pass over the IL code (and we do bblock
2435 * splitting/merging in the few cases when it's required: a back jump to an IL
2436 * address that was not already seen as bblock starting point).
2437 * Code is validated as we go (full verification is still better left to metadata/verify.c).
2438 * Complex operations are decomposed in simpler ones right away. We need to let the
2439 * arch-specific code peek and poke inside this process somehow (except when the
2440 * optimizations can take advantage of the full semantic info of coarse opcodes).
2441 * All the opcodes of the form opcode.s are 'normalized' to opcode.
2442 * MonoInst->opcode initially is the IL opcode or some simplification of that
2443 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
2444 * opcode with value bigger than OP_LAST.
2445 * At this point the IR can be handed over to an interpreter, a dumb code generator
2446 * or to the optimizing code generator that will translate it to SSA form.
2448 * Profiling directed optimizations.
2449 * We may compile by default with few or no optimizations and instrument the code
2450 * or the user may indicate what methods to optimize the most either in a config file
2451 * or through repeated runs where the compiler applies offline the optimizations to
2452 * each method and then decides if it was worth it.
2455 * * consider using an array instead of an hash table (bb_hash)
2458 #define CHECK_TYPE(ins) if (!(ins)->type) goto unverified
2459 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) goto unverified
2460 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) goto unverified
2461 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) goto unverified
2462 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) goto unverified
2464 #define TYPE_PARAM_TO_TYPE(num) (method->klass->generic_inst->data.generic_inst->type_argv [(num)])
2465 #define TYPE_PARAM_TO_CLASS(num) (mono_class_from_mono_type (TYPE_PARAM_TO_TYPE ((num))))
2467 #define MTYPE_PARAM_TO_TYPE(num) (((MonoMethodNormal *) method)->header->geninst->type_argv [(num)])
2468 #define MTYPE_PARAM_TO_CLASS(num) (mono_class_from_mono_type (MTYPE_PARAM_TO_TYPE ((num))))
2471 /* offset from br.s -> br like opcodes */
2472 #define BIG_BRANCH_OFFSET 13
2475 get_basic_blocks (MonoCompile *cfg, GHashTable *bbhash, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
2477 unsigned char *ip = start;
2478 unsigned char *target;
2481 MonoBasicBlock *bblock;
2482 const MonoOpcode *opcode;
2485 cli_addr = ip - start;
2486 i = mono_opcode_value ((const guint8 **)&ip);
2487 opcode = &mono_opcodes [i];
2488 switch (opcode->argument) {
2489 case MonoInlineNone:
2492 case MonoInlineString:
2493 case MonoInlineType:
2494 case MonoInlineField:
2495 case MonoInlineMethod:
2498 case MonoShortInlineR:
2505 case MonoShortInlineVar:
2506 case MonoShortInlineI:
2509 case MonoShortInlineBrTarget:
2510 target = start + cli_addr + 2 + (signed char)ip [1];
2511 GET_BBLOCK (cfg, bbhash, bblock, target);
2514 case MonoInlineBrTarget:
2515 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
2516 GET_BBLOCK (cfg, bbhash, bblock, target);
2519 case MonoInlineSwitch: {
2520 guint32 n = read32 (ip + 1);
2523 cli_addr += 5 + 4 * n;
2524 target = start + cli_addr;
2525 GET_BBLOCK (cfg, bbhash, bblock, target);
2527 for (j = 0; j < n; ++j) {
2528 target = start + cli_addr + (gint32)read32 (ip);
2529 GET_BBLOCK (cfg, bbhash, bblock, target);
2539 g_assert_not_reached ();
2548 static MonoClassField *
2549 get_generic_field_inst (MonoClassField *field, MonoClass *klass, MonoClass **retclass)
2552 for (i = 0; i < field->parent->field.count; ++i) {
2553 if (field == &field->parent->fields [i]) {
2555 return &klass->fields [i];
2561 static MonoClassField *
2562 inflate_generic_field (MonoClassField *field, MonoClass *klass, MonoClass **retclass)
2564 MonoGenericInst *ginst;
2565 MonoClassField *res;
2567 res = g_new0 (MonoClassField, 1);
2569 ginst = klass->generic_inst->data.generic_inst;
2570 res->type = mono_class_inflate_generic_type (field->type, ginst);
2575 * mono_method_to_ir: translates IL into basic blocks containing trees
2578 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
2579 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
2580 guint inline_offset, gboolean is_virtual_call)
2582 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
2583 MonoInst *ins, **sp, **stack_start;
2584 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
2586 MonoMethod *cmethod;
2587 MonoInst **arg_array;
2588 MonoMethodHeader *header;
2590 guint32 token, ins_flag;
2592 unsigned char *ip, *end, *target, *err_pos;
2593 static double r8_0 = 0.0;
2594 MonoMethodSignature *sig;
2595 MonoType **param_types;
2596 GList *bb_recheck = NULL, *tmp;
2597 int i, n, start_new_bblock, align;
2598 int num_calls = 0, inline_costs = 0;
2599 int *filter_lengths = NULL;
2600 int breakpoint_id = 0;
2601 guint real_offset, num_args;
2603 image = method->klass->image;
2604 header = ((MonoMethodNormal *)method)->header;
2605 sig = method->signature;
2606 num_args = sig->hasthis + sig->param_count;
2607 ip = (unsigned char*)header->code;
2608 end = ip + header->code_size;
2609 mono_jit_stats.cil_code_size += header->code_size;
2611 if (cfg->method == method) {
2613 bbhash = cfg->bb_hash;
2615 real_offset = inline_offset;
2616 bbhash = g_hash_table_new (g_direct_hash, NULL);
2619 if (cfg->verbose_level > 2)
2620 g_print ("method to IR %s\n", mono_method_full_name (method, TRUE));
2622 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
2623 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
2625 dont_inline = g_list_prepend (dont_inline, method);
2626 if (cfg->method == method) {
2629 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
2630 start_bblock->cil_code = NULL;
2631 start_bblock->cil_length = 0;
2632 start_bblock->block_num = cfg->num_bblocks++;
2635 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
2636 end_bblock->cil_code = NULL;
2637 end_bblock->cil_length = 0;
2638 end_bblock->block_num = cfg->num_bblocks++;
2639 g_assert (cfg->num_bblocks == 2);
2641 arg_array = alloca (sizeof (MonoInst *) * num_args);
2642 for (i = num_args - 1; i >= 0; i--)
2643 arg_array [i] = cfg->varinfo [i];
2645 if (header->num_clauses) {
2646 int size = sizeof (int) * header->num_clauses;
2647 filter_lengths = alloca (size);
2648 memset (filter_lengths, 0, size);
2650 cfg->spvars = g_hash_table_new (NULL, NULL);
2652 /* handle exception clauses */
2653 for (i = 0; i < header->num_clauses; ++i) {
2654 //unsigned char *p = ip;
2655 MonoExceptionClause *clause = &header->clauses [i];
2656 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->try_offset);
2657 tblock->real_offset = clause->try_offset;
2658 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
2659 tblock->real_offset = clause->handler_offset;
2661 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
2662 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2663 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2664 MONO_ADD_INS (tblock, ins);
2667 /*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);
2669 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
2671 /* catch and filter blocks get the exception object on the stack */
2672 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
2673 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2674 /* mostly like handle_stack_args (), but just sets the input args */
2675 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
2677 cfg->exvar = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
2678 /* prevent it from being register allocated */
2679 cfg->exvar->flags |= MONO_INST_INDIRECT;
2681 tblock->in_scount = 1;
2682 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2683 tblock->in_stack [0] = cfg->exvar;
2685 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2686 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->token_or_filter);
2687 tblock->real_offset = clause->token_or_filter;
2688 tblock->in_scount = 1;
2689 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2690 tblock->in_stack [0] = cfg->exvar;
2691 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2692 MONO_ADD_INS (tblock, ins);
2698 arg_array = alloca (sizeof (MonoInst *) * num_args);
2699 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
2702 /* FIRST CODE BLOCK */
2703 bblock = NEW_BBLOCK (cfg);
2704 bblock->cil_code = ip;
2706 ADD_BBLOCK (cfg, bbhash, bblock);
2708 if (cfg->method == method) {
2709 breakpoint_id = mono_debugger_method_has_breakpoint (method);
2710 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
2711 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2712 MONO_ADD_INS (bblock, ins);
2716 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED))) || mono_compile_aot) {
2717 /* we use a separate basic block for the initialization code */
2718 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
2719 init_localsbb->real_offset = real_offset;
2720 start_bblock->next_bb = init_localsbb;
2721 init_localsbb->next_bb = bblock;
2722 link_bblock (cfg, start_bblock, init_localsbb);
2723 link_bblock (cfg, init_localsbb, bblock);
2724 init_localsbb->block_num = cfg->num_bblocks++;
2726 start_bblock->next_bb = bblock;
2727 link_bblock (cfg, start_bblock, bblock);
2730 if (get_basic_blocks (cfg, bbhash, header, real_offset, ip, end, &err_pos)) {
2735 mono_debug_init_method (cfg, bblock, breakpoint_id);
2737 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
2739 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
2740 for (n = 0; n < sig->param_count; ++n)
2741 param_types [n + sig->hasthis] = sig->params [n];
2743 /* do this somewhere outside - not here */
2744 NEW_ICONST (cfg, zero_int32, 0);
2745 NEW_ICONST (cfg, zero_int64, 0);
2746 zero_int64->type = STACK_I8;
2747 NEW_PCONST (cfg, zero_ptr, 0);
2748 NEW_PCONST (cfg, zero_obj, 0);
2749 zero_obj->type = STACK_OBJ;
2751 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
2752 zero_r8->type = STACK_R8;
2753 zero_r8->inst_p0 = &r8_0;
2755 /* add a check for this != NULL to inlined methods */
2756 if (is_virtual_call) {
2757 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
2758 NEW_ARGLOAD (cfg, ins->inst_left, 0);
2760 MONO_ADD_INS (bblock, ins);
2763 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
2764 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
2767 start_new_bblock = 0;
2770 if (cfg->method == method)
2771 real_offset = ip - header->code;
2773 real_offset = inline_offset;
2775 if (start_new_bblock) {
2776 bblock->cil_length = ip - bblock->cil_code;
2777 if (start_new_bblock == 2) {
2778 g_assert (ip == tblock->cil_code);
2780 GET_BBLOCK (cfg, bbhash, tblock, ip);
2782 bblock->next_bb = tblock;
2784 start_new_bblock = 0;
2785 for (i = 0; i < bblock->in_scount; ++i) {
2786 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2790 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
2791 link_bblock (cfg, bblock, tblock);
2792 if (sp != stack_start) {
2793 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2796 bblock->next_bb = tblock;
2798 for (i = 0; i < bblock->in_scount; ++i) {
2799 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2805 if (cfg->coverage_info) {
2806 MonoInst *store, *one;
2807 guint32 cil_offset = ip - header->code;
2808 cfg->coverage_info->data [cil_offset].cil_code = ip;
2810 /* TODO: Use an increment here */
2811 NEW_ICONST (cfg, one, 1);
2814 NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
2817 MONO_INST_NEW (cfg, store, CEE_STIND_I);
2818 store->cil_code = ip;
2819 store->inst_left = ins;
2820 store->inst_right = one;
2822 MONO_ADD_INS (bblock, store);
2825 if (cfg->verbose_level > 3)
2826 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, sp-stack_start, mono_disasm_code_one (NULL, method, ip, NULL));
2833 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2834 ins->cil_code = ip++;
2835 MONO_ADD_INS (bblock, ins);
2841 CHECK_STACK_OVF (1);
2842 n = (*ip)-CEE_LDARG_0;
2844 NEW_ARGLOAD (cfg, ins, n);
2845 ins->cil_code = ip++;
2852 CHECK_STACK_OVF (1);
2853 n = (*ip)-CEE_LDLOC_0;
2855 NEW_LOCLOAD (cfg, ins, n);
2856 ins->cil_code = ip++;
2864 n = (*ip)-CEE_STLOC_0;
2867 handle_loaded_temps (cfg, bblock, stack_start, sp);
2868 NEW_LOCSTORE (cfg, ins, n, *sp);
2870 if (ins->opcode == CEE_STOBJ) {
2871 NEW_LOCLOADA (cfg, ins, n);
2872 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2874 MONO_ADD_INS (bblock, ins);
2879 CHECK_STACK_OVF (1);
2881 NEW_ARGLOAD (cfg, ins, ip [1]);
2887 CHECK_STACK_OVF (1);
2889 NEW_ARGLOADA (cfg, ins, ip [1]);
2898 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
2899 handle_loaded_temps (cfg, bblock, stack_start, sp);
2901 if (ins->opcode == CEE_STOBJ) {
2902 NEW_ARGLOADA (cfg, ins, ip [1]);
2903 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2905 MONO_ADD_INS (bblock, ins);
2909 CHECK_STACK_OVF (1);
2910 CHECK_LOCAL (ip [1]);
2911 NEW_LOCLOAD (cfg, ins, ip [1]);
2917 CHECK_STACK_OVF (1);
2918 CHECK_LOCAL (ip [1]);
2919 NEW_LOCLOADA (cfg, ins, ip [1]);
2927 handle_loaded_temps (cfg, bblock, stack_start, sp);
2928 CHECK_LOCAL (ip [1]);
2929 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
2931 if (ins->opcode == CEE_STOBJ) {
2932 NEW_LOCLOADA (cfg, ins, ip [1]);
2933 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2935 MONO_ADD_INS (bblock, ins);
2940 CHECK_STACK_OVF (1);
2941 NEW_PCONST (cfg, ins, NULL);
2943 ins->type = STACK_OBJ;
2948 CHECK_STACK_OVF (1);
2949 NEW_ICONST (cfg, ins, -1);
2963 CHECK_STACK_OVF (1);
2964 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
2970 CHECK_STACK_OVF (1);
2972 NEW_ICONST (cfg, ins, *((signed char*)ip));
2978 CHECK_STACK_OVF (1);
2979 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
2985 CHECK_STACK_OVF (1);
2986 MONO_INST_NEW (cfg, ins, OP_I8CONST);
2988 ins->type = STACK_I8;
2990 ins->inst_l = (gint64)read64 (ip);
2995 float *f = g_malloc (sizeof (float));
2996 CHECK_STACK_OVF (1);
2997 MONO_INST_NEW (cfg, ins, OP_R4CONST);
2998 ins->type = STACK_R8;
3007 double *d = g_malloc (sizeof (double));
3008 CHECK_STACK_OVF (1);
3009 MONO_INST_NEW (cfg, ins, OP_R8CONST);
3010 ins->type = STACK_R8;
3019 MonoInst *temp, *store;
3021 CHECK_STACK_OVF (1);
3026 * small optimization: if the loaded value was from a local already,
3027 * just load it twice.
3029 if (ins->ssa_op == MONO_SSA_LOAD &&
3030 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
3032 MONO_INST_NEW (cfg, temp, 0);
3034 temp->cil_code = ip;
3037 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3038 temp->cil_code = ip;
3039 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3040 store->cil_code = ip;
3041 MONO_ADD_INS (bblock, store);
3042 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3045 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3055 MONO_INST_NEW (cfg, ins, CEE_POP);
3056 MONO_ADD_INS (bblock, ins);
3057 ins->cil_code = ip++;
3062 if (stack_start != sp)
3064 MONO_INST_NEW (cfg, ins, CEE_JMP);
3065 token = read32 (ip + 1);
3066 /* FIXME: check the signature matches */
3067 cmethod = mono_get_method (image, token, NULL);
3068 ins->inst_p0 = cmethod;
3069 MONO_ADD_INS (bblock, ins);
3071 start_new_bblock = 1;
3075 case CEE_CALLVIRT: {
3076 MonoInst *addr = NULL;
3077 MonoMethodSignature *fsig = NULL;
3078 int temp, array_rank = 0;
3079 int virtual = *ip == CEE_CALLVIRT;
3081 token = read32 (ip + 1);
3083 if (*ip == CEE_CALLI) {
3088 if (method->wrapper_type != MONO_WRAPPER_NONE)
3089 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
3091 fsig = mono_metadata_parse_signature (image, token);
3093 n = fsig->param_count + fsig->hasthis;
3095 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3096 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
3098 cmethod = mono_get_method (image, token, NULL);
3101 if (!cmethod->klass->inited)
3102 mono_class_init (cmethod->klass);
3104 if (cmethod->signature->pinvoke) {
3105 #ifdef MONO_USE_EXC_TABLES
3106 if (mono_method_blittable (cmethod)) {
3107 fsig = cmethod->signature;
3110 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
3111 fsig = wrapper->signature;
3112 #ifdef MONO_USE_EXC_TABLES
3116 fsig = mono_method_get_signature (cmethod, image, token);
3119 n = fsig->param_count + fsig->hasthis;
3121 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
3122 cmethod->klass->parent == mono_defaults.array_class) {
3123 array_rank = cmethod->klass->rank;
3126 if (cmethod->string_ctor)
3127 g_assert_not_reached ();
3133 //g_assert (!virtual || fsig->hasthis);
3137 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
3140 if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL) && (mono_metadata_signature_equal (method->signature, cmethod->signature))) {
3142 /* FIXME: This assumes the two methods has the same number and type of arguments */
3143 for (i = 0; i < n; ++i) {
3144 /* Check if argument is the same */
3145 NEW_ARGLOAD (cfg, ins, i);
3146 if ((ins->opcode == sp [i]->opcode) && (ins->inst_i0 == sp [i]->inst_i0))
3149 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3151 if (ins->opcode == CEE_STOBJ) {
3152 NEW_ARGLOADA (cfg, ins, i);
3153 handle_stobj (cfg, bblock, ins, sp [i], sp [i]->cil_code, ins->klass, FALSE, FALSE);
3156 MONO_ADD_INS (bblock, ins);
3158 MONO_INST_NEW (cfg, ins, CEE_JMP);
3160 ins->inst_p0 = cmethod;
3161 ins->inst_p1 = arg_array [0];
3162 MONO_ADD_INS (bblock, ins);
3163 start_new_bblock = 1;
3164 /* skip CEE_RET as well */
3169 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_opcode_for_method (cfg, cmethod, fsig, sp))) {
3172 if (MONO_TYPE_IS_VOID (fsig->ret)) {
3173 MONO_ADD_INS (bblock, ins);
3175 type_to_eval_stack_type (fsig->ret, ins);
3184 handle_loaded_temps (cfg, bblock, stack_start, sp);
3186 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3187 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
3188 mono_method_check_inlining (cfg, cmethod) &&
3189 !g_list_find (dont_inline, cmethod)) {
3191 MonoBasicBlock *ebblock;
3193 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3197 GET_BBLOCK (cfg, bbhash, bblock, ip);
3198 ebblock->next_bb = bblock;
3199 link_bblock (cfg, ebblock, bblock);
3201 if (!MONO_TYPE_IS_VOID (fsig->ret))
3204 /* indicates start of a new block, and triggers a load of all
3205 stack arguments at bb boundarie */
3208 inline_costs += costs;
3213 inline_costs += 10 * num_calls++;
3215 /* tail recursion elimination */
3216 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == method && ip [5] == CEE_RET) {
3217 gboolean has_vtargs = FALSE;
3220 /* keep it simple */
3221 for (i = fsig->param_count - 1; i >= 0; i--) {
3222 if (MONO_TYPE_ISSTRUCT (cmethod->signature->params [i]))
3227 for (i = 0; i < n; ++i) {
3228 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3230 MONO_ADD_INS (bblock, ins);
3232 MONO_INST_NEW (cfg, ins, CEE_BR);
3234 MONO_ADD_INS (bblock, ins);
3235 tblock = start_bblock->out_bb [0];
3236 link_bblock (cfg, bblock, tblock);
3237 ins->inst_target_bb = tblock;
3238 start_new_bblock = 1;
3241 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3242 /* just create a dummy - the value is never used */
3243 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
3244 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3252 if (*ip == CEE_CALLI) {
3254 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
3255 NEW_TEMPLOAD (cfg, *sp, temp);
3259 } else if (array_rank) {
3262 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
3263 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
3264 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
3266 if (ins->opcode == CEE_STOBJ) {
3267 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
3269 MONO_ADD_INS (bblock, ins);
3272 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
3273 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3274 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
3278 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
3279 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3282 g_assert_not_reached ();
3286 if (0 && CODE_IS_STLOC (ip + 5) && (!MONO_TYPE_ISSTRUCT (fsig->ret)) && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)) {
3287 /* no need to spill */
3288 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
3291 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) {
3292 NEW_TEMPLOAD (cfg, *sp, temp);
3302 if (cfg->method != method) {
3303 /* return from inlined methode */
3308 //g_assert (returnvar != -1);
3309 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
3310 store->cil_code = sp [0]->cil_code;
3311 if (store->opcode == CEE_STOBJ) {
3312 g_assert_not_reached ();
3313 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
3314 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
3316 MONO_ADD_INS (bblock, store);
3320 g_assert (!return_var);
3323 MONO_INST_NEW (cfg, ins, CEE_NOP);
3324 ins->opcode = mono_type_to_stind (method->signature->ret);
3325 if (ins->opcode == CEE_STOBJ) {
3326 NEW_RETLOADA (cfg, ins);
3327 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3329 ins->opcode = OP_SETRET;
3331 ins->inst_i0 = *sp;;
3332 ins->inst_i1 = NULL;
3333 MONO_ADD_INS (bblock, ins);
3337 if (sp != stack_start)
3339 MONO_INST_NEW (cfg, ins, CEE_BR);
3340 ins->cil_code = ip++;
3341 ins->inst_target_bb = end_bblock;
3342 MONO_ADD_INS (bblock, ins);
3343 link_bblock (cfg, bblock, end_bblock);
3344 start_new_bblock = 1;
3347 MONO_INST_NEW (cfg, ins, CEE_BR);
3348 ins->cil_code = ip++;
3349 MONO_ADD_INS (bblock, ins);
3350 target = ip + 1 + (signed char)(*ip);
3352 GET_BBLOCK (cfg, bbhash, tblock, target);
3353 link_bblock (cfg, bblock, tblock);
3354 CHECK_BBLOCK (target, ip, tblock);
3355 ins->inst_target_bb = tblock;
3356 if (sp != stack_start) {
3357 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3360 start_new_bblock = 1;
3366 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3367 ins->cil_code = ip++;
3368 target = ip + 1 + *(signed char*)ip;
3370 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
3371 if (sp != stack_start) {
3372 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3388 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3389 ins->cil_code = ip++;
3390 target = ip + 1 + *(signed char*)ip;
3393 if (sp != stack_start) {
3394 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3400 MONO_INST_NEW (cfg, ins, CEE_BR);
3401 ins->cil_code = ip++;
3402 MONO_ADD_INS (bblock, ins);
3403 target = ip + 4 + (gint32)read32(ip);
3405 GET_BBLOCK (cfg, bbhash, tblock, target);
3406 link_bblock (cfg, bblock, tblock);
3407 CHECK_BBLOCK (target, ip, tblock);
3408 ins->inst_target_bb = tblock;
3409 if (sp != stack_start) {
3410 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3413 start_new_bblock = 1;
3419 MONO_INST_NEW (cfg, ins, *ip);
3420 ins->cil_code = ip++;
3421 target = ip + 4 + (gint32)read32(ip);
3423 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
3424 if (sp != stack_start) {
3425 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3441 MONO_INST_NEW (cfg, ins, *ip);
3442 ins->cil_code = ip++;
3443 target = ip + 4 + (gint32)read32(ip);
3446 if (sp != stack_start) {
3447 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3454 n = read32 (ip + 1);
3455 MONO_INST_NEW (cfg, ins, *ip);
3457 ins->inst_left = *sp;
3458 if (ins->inst_left->type != STACK_I4) goto unverified;
3461 target = ip + n * sizeof (guint32);
3462 MONO_ADD_INS (bblock, ins);
3463 GET_BBLOCK (cfg, bbhash, tblock, target);
3464 link_bblock (cfg, bblock, tblock);
3465 ins->klass = GUINT_TO_POINTER (n);
3466 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
3467 ins->inst_many_bb [n] = tblock;
3469 for (i = 0; i < n; ++i) {
3470 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
3471 link_bblock (cfg, bblock, tblock);
3472 ins->inst_many_bb [i] = tblock;
3475 /* FIXME: handle stack args */
3490 MONO_INST_NEW (cfg, ins, *ip);
3495 ins->type = ldind_type [*ip - CEE_LDIND_I1];
3496 ins->flags |= ins_flag;
3508 MONO_INST_NEW (cfg, ins, *ip);
3509 ins->cil_code = ip++;
3511 handle_loaded_temps (cfg, bblock, stack_start, sp);
3512 MONO_ADD_INS (bblock, ins);
3513 ins->inst_i0 = sp [0];
3514 ins->inst_i1 = sp [1];
3515 ins->flags |= ins_flag;
3546 case CEE_CONV_OVF_I8:
3547 case CEE_CONV_OVF_U8:
3553 case CEE_CONV_OVF_I4:
3554 case CEE_CONV_OVF_I1:
3555 case CEE_CONV_OVF_I2:
3556 case CEE_CONV_OVF_I:
3557 case CEE_CONV_OVF_U:
3560 if (sp [-1]->type == STACK_R8) {
3561 ADD_UNOP (CEE_CONV_OVF_I8);
3569 case CEE_CONV_OVF_U1:
3570 case CEE_CONV_OVF_U2:
3571 case CEE_CONV_OVF_U4:
3574 if (sp [-1]->type == STACK_R8) {
3575 ADD_UNOP (CEE_CONV_OVF_U8);
3583 case CEE_CONV_OVF_I1_UN:
3584 case CEE_CONV_OVF_I2_UN:
3585 case CEE_CONV_OVF_I4_UN:
3586 case CEE_CONV_OVF_I8_UN:
3587 case CEE_CONV_OVF_U1_UN:
3588 case CEE_CONV_OVF_U2_UN:
3589 case CEE_CONV_OVF_U4_UN:
3590 case CEE_CONV_OVF_U8_UN:
3591 case CEE_CONV_OVF_I_UN:
3592 case CEE_CONV_OVF_U_UN:
3599 token = read32 (ip + 1);
3600 if (method->wrapper_type != MONO_WRAPPER_NONE)
3601 klass = mono_method_get_wrapper_data (method, token);
3603 klass = mono_class_get (image, token);
3605 mono_class_init (klass);
3606 if (klass->byval_arg.type == MONO_TYPE_VAR)
3607 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3608 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
3609 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3611 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3612 MonoInst *store, *load;
3613 MONO_INST_NEW (cfg, load, CEE_LDIND_REF);
3614 load->cil_code = ip;
3615 load->inst_i0 = sp [1];
3616 load->type = ldind_type [CEE_LDIND_REF];
3617 load->flags |= ins_flag;
3618 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
3619 store->cil_code = ip;
3620 handle_loaded_temps (cfg, bblock, stack_start, sp);
3621 MONO_ADD_INS (bblock, store);
3622 store->inst_i0 = sp [0];
3623 store->inst_i1 = load;
3624 store->flags |= ins_flag;
3626 n = mono_class_value_size (klass, NULL);
3627 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3629 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3630 copy->inst_left = sp [0];
3631 copy->inst_right = sp [1];
3632 copy->cil_code = ip;
3634 MONO_ADD_INS (bblock, copy);
3636 MonoInst *iargs [3];
3639 NEW_ICONST (cfg, iargs [2], n);
3640 iargs [2]->cil_code = ip;
3642 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3649 MonoInst *iargs [3];
3652 token = read32 (ip + 1);
3653 if (method->wrapper_type != MONO_WRAPPER_NONE)
3654 klass = mono_method_get_wrapper_data (method, token);
3656 klass = mono_class_get (image, token);
3658 mono_class_init (klass);
3659 if (klass->byval_arg.type == MONO_TYPE_VAR)
3660 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3661 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
3662 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3663 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3664 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3666 ins->inst_i0 = sp [0];
3667 ins->type = ldind_type [CEE_LDIND_REF];
3668 ins->flags |= ins_flag;
3674 n = mono_class_value_size (klass, NULL);
3675 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3676 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3677 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3679 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3680 copy->inst_left = iargs [0];
3681 copy->inst_right = *sp;
3682 copy->cil_code = ip;
3684 MONO_ADD_INS (bblock, copy);
3687 NEW_ICONST (cfg, iargs [2], n);
3688 iargs [2]->cil_code = ip;
3690 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3692 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3700 CHECK_STACK_OVF (1);
3701 n = read32 (ip + 1);
3703 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3705 MonoInst *iargs [1];
3707 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
3708 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
3709 NEW_TEMPLOAD (cfg, *sp, temp);
3713 if (cfg->opt & MONO_OPT_SHARED) {
3715 MonoInst *iargs [3];
3717 if (mono_compile_aot) {
3718 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n);
3721 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3722 NEW_IMAGECONST (cfg, iargs [1], image);
3723 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
3724 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
3725 NEW_TEMPLOAD (cfg, *sp, temp);
3726 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3728 if (mono_compile_aot)
3729 NEW_LDSTRCONST (cfg, ins, image, n);
3731 NEW_PCONST (cfg, ins, NULL);
3733 ins->type = STACK_OBJ;
3734 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3744 MonoInst *iargs [2];
3745 MonoMethodSignature *fsig;
3748 token = read32 (ip + 1);
3749 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3750 cmethod = mono_method_get_wrapper_data (method, token);
3752 cmethod = mono_get_method (image, token, NULL);
3753 fsig = mono_method_get_signature (cmethod, image, token);
3755 mono_class_init (cmethod->klass);
3757 n = fsig->param_count;
3760 /* move the args to allow room for 'this' in the first position */
3766 handle_loaded_temps (cfg, bblock, stack_start, sp);
3769 if (cmethod->klass->parent == mono_defaults.array_class) {
3770 NEW_METHODCONST (cfg, *sp, cmethod);
3771 temp = mono_emit_native_call (cfg, bblock, mono_array_new_va, fsig, sp, ip, FALSE);
3773 } else if (cmethod->string_ctor) {
3774 /* we simply pass a null pointer */
3775 NEW_PCONST (cfg, *sp, NULL);
3776 /* now call the string ctor */
3777 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
3779 if (cmethod->klass->valuetype) {
3780 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
3781 temp = iargs [0]->inst_c0;
3782 NEW_TEMPLOADA (cfg, *sp, temp);
3784 if (cfg->opt & MONO_OPT_SHARED) {
3785 NEW_DOMAINCONST (cfg, iargs [0]);
3786 NEW_CLASSCONST (cfg, iargs [1], cmethod->klass);
3788 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3790 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
3791 NEW_VTABLECONST (cfg, iargs [0], vtable);
3792 if (cmethod->klass->has_finalize || cmethod->klass->marshalbyref || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
3793 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
3795 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
3797 NEW_TEMPLOAD (cfg, *sp, temp);
3800 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3801 mono_method_check_inlining (cfg, cmethod) &&
3802 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
3803 !g_list_find (dont_inline, cmethod)) {
3805 MonoBasicBlock *ebblock;
3806 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3811 GET_BBLOCK (cfg, bbhash, bblock, ip);
3812 ebblock->next_bb = bblock;
3813 link_bblock (cfg, ebblock, bblock);
3815 NEW_TEMPLOAD (cfg, *sp, temp);
3818 /* indicates start of a new block, and triggers a load
3819 of all stack arguments at bb boundarie */
3822 inline_costs += costs;
3826 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3829 /* now call the actual ctor */
3830 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3834 NEW_TEMPLOAD (cfg, *sp, temp);
3843 MONO_INST_NEW (cfg, ins, *ip);
3845 klass = mono_class_get (image, read32 (ip + 1));
3846 mono_class_init (klass);
3847 ins->type = STACK_OBJ;
3848 ins->inst_left = *sp;
3849 ins->inst_newa_class = klass;
3854 case CEE_UNBOX_ANY: {
3855 MonoInst *add, *vtoffset;
3856 MonoInst *iargs [3];
3860 token = read32 (ip + 1);
3861 if (method->wrapper_type != MONO_WRAPPER_NONE)
3862 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3864 klass = mono_class_get (image, token);
3865 mono_class_init (klass);
3867 if (klass->byval_arg.type == MONO_TYPE_VAR)
3868 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3869 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
3870 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3872 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3874 MONO_INST_NEW (cfg, ins, CEE_CASTCLASS);
3875 ins->type = STACK_OBJ;
3876 ins->inst_left = *sp;
3878 ins->inst_newa_class = klass;
3885 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
3886 ins->type = STACK_OBJ;
3887 ins->inst_left = *sp;
3889 ins->inst_newa_class = klass;
3892 MONO_INST_NEW (cfg, add, CEE_ADD);
3893 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3894 add->inst_left = ins;
3895 add->inst_right = vtoffset;
3896 add->type = STACK_MP;
3900 n = mono_class_value_size (klass, NULL);
3901 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3902 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3903 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3905 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3906 copy->inst_left = iargs [0];
3907 copy->inst_right = *sp;
3908 copy->cil_code = ip;
3910 MONO_ADD_INS (bblock, copy);
3913 NEW_ICONST (cfg, iargs [2], n);
3914 iargs [2]->cil_code = ip;
3916 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3918 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3924 MonoInst *add, *vtoffset;
3928 token = read32 (ip + 1);
3929 if (method->wrapper_type != MONO_WRAPPER_NONE)
3930 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3932 klass = mono_class_get (image, token);
3933 mono_class_init (klass);
3935 if (klass->byval_arg.type == MONO_TYPE_VAR)
3936 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3937 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
3938 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3940 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
3941 ins->type = STACK_OBJ;
3942 ins->inst_left = *sp;
3944 ins->inst_newa_class = klass;
3947 MONO_INST_NEW (cfg, add, CEE_ADD);
3948 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3949 add->inst_left = ins;
3950 add->inst_right = vtoffset;
3951 add->type = STACK_MP;
3959 MONO_INST_NEW (cfg, ins, *ip);
3961 klass = mono_class_get (image, read32 (ip + 1));
3962 mono_class_init (klass);
3963 if (klass->byval_arg.type == MONO_TYPE_VAR)
3964 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3965 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
3966 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3967 ins->type = STACK_OBJ;
3968 ins->inst_left = *sp;
3970 ins->inst_newa_class = klass;
3977 MONO_INST_NEW (cfg, ins, *ip);
3979 ins->inst_left = *sp;
3980 ins->cil_code = ip++;
3981 MONO_ADD_INS (bblock, ins);
3983 start_new_bblock = 1;
3988 MonoInst *offset_ins;
3989 MonoClassField *field;
3990 MonoBasicBlock *ebblock;
3994 if (*ip == CEE_STFLD) {
4001 // FIXME: enable this test later.
4002 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
4004 token = read32 (ip + 1);
4005 field = mono_field_from_token (image, token, &klass);
4006 if (field->parent->gen_params)
4007 field = get_generic_field_inst (field, method->klass, &klass);
4008 else if (field->parent->generic_inst && method->klass->generic_inst)
4009 field = inflate_generic_field (field, method->klass, &klass);
4010 mono_class_init (klass);
4012 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
4013 /* FIXME: mark instructions for use in SSA */
4014 if (*ip == CEE_STFLD) {
4015 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
4016 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
4017 MonoInst *iargs [5];
4020 NEW_CLASSCONST (cfg, iargs [1], klass);
4021 NEW_FIELDCONST (cfg, iargs [2], field);
4022 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
4026 if (cfg->opt & MONO_OPT_INLINE) {
4027 costs = inline_method (cfg, stfld_wrapper, stfld_wrapper->signature, bblock,
4028 iargs, ip, real_offset, dont_inline, &ebblock);
4029 g_assert (costs > 0);
4034 GET_BBLOCK (cfg, bbhash, bblock, ip);
4035 ebblock->next_bb = bblock;
4036 link_bblock (cfg, ebblock, bblock);
4038 /* indicates start of a new block, and triggers a load
4039 of all stack arguments at bb boundarie */
4042 inline_costs += costs;
4045 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, stfld_wrapper->signature, iargs, ip, NULL);
4049 NEW_ICONST (cfg, offset_ins, foffset);
4050 MONO_INST_NEW (cfg, ins, CEE_ADD);
4052 ins->inst_left = *sp;
4053 ins->inst_right = offset_ins;
4054 ins->type = STACK_MP;
4056 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4057 store->cil_code = ip;
4058 store->inst_left = ins;
4059 store->inst_right = sp [1];
4060 handle_loaded_temps (cfg, bblock, stack_start, sp);
4061 store->flags |= ins_flag;
4063 if (store->opcode == CEE_STOBJ) {
4064 handle_stobj (cfg, bblock, ins, sp [1], ip,
4065 mono_class_from_mono_type (field->type), FALSE, FALSE);
4067 MONO_ADD_INS (bblock, store);
4070 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
4071 /* fixme: we need to inline that call somehow */
4072 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
4073 MonoInst *iargs [4];
4077 NEW_CLASSCONST (cfg, iargs [1], klass);
4078 NEW_FIELDCONST (cfg, iargs [2], field);
4079 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
4080 if (cfg->opt & MONO_OPT_INLINE) {
4081 costs = inline_method (cfg, ldfld_wrapper, ldfld_wrapper->signature, bblock,
4082 iargs, ip, real_offset, dont_inline, &ebblock);
4083 g_assert (costs > 0);
4088 GET_BBLOCK (cfg, bbhash, bblock, ip);
4089 ebblock->next_bb = bblock;
4090 link_bblock (cfg, ebblock, bblock);
4092 temp = iargs [0]->inst_i0->inst_c0;
4094 if (*ip == CEE_LDFLDA) {
4095 /* not sure howto handle this */
4096 NEW_TEMPLOADA (cfg, *sp, temp);
4098 NEW_TEMPLOAD (cfg, *sp, temp);
4102 /* indicates start of a new block, and triggers a load of
4103 all stack arguments at bb boundarie */
4106 inline_costs += costs;
4109 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, ldfld_wrapper->signature, iargs, ip, NULL);
4110 if (*ip == CEE_LDFLDA) {
4111 /* not sure howto handle this */
4112 NEW_TEMPLOADA (cfg, *sp, temp);
4114 NEW_TEMPLOAD (cfg, *sp, temp);
4119 NEW_ICONST (cfg, offset_ins, foffset);
4120 MONO_INST_NEW (cfg, ins, CEE_ADD);
4122 ins->inst_left = *sp;
4123 ins->inst_right = offset_ins;
4124 ins->type = STACK_MP;
4126 if (*ip == CEE_LDFLDA) {
4130 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4131 type_to_eval_stack_type (field->type, load);
4132 load->cil_code = ip;
4133 load->inst_left = ins;
4134 load->flags |= ins_flag;
4146 MonoClassField *field;
4147 gpointer addr = NULL;
4149 token = read32 (ip + 1);
4151 field = mono_field_from_token (image, token, &klass);
4152 if (field->parent->gen_params)
4153 field = get_generic_field_inst (field, method->klass, &klass);
4154 else if (field->parent->generic_inst && method->klass->generic_inst)
4155 field = inflate_generic_field (field, method->klass, &klass);
4156 mono_class_init (klass);
4158 handle_loaded_temps (cfg, bblock, stack_start, sp);
4160 if (cfg->domain->special_static_fields)
4161 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
4163 if ((cfg->opt & MONO_OPT_SHARED) || (mono_compile_aot && addr)) {
4165 MonoInst *iargs [2];
4166 g_assert (field->parent);
4167 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
4168 NEW_FIELDCONST (cfg, iargs [1], field);
4169 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
4170 NEW_TEMPLOAD (cfg, ins, temp);
4173 vtable = mono_class_vtable (cfg->domain, klass);
4175 if ((!vtable->initialized || mono_compile_aot) && !(klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (klass, method)) {
4176 guint8 *tramp = mono_create_class_init_trampoline (vtable);
4177 mono_emit_native_call (cfg, bblock, tramp,
4178 helper_sig_class_init_trampoline,
4180 if (cfg->verbose_level > 2)
4181 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
4183 mono_runtime_class_init (vtable);
4185 addr = (char*)vtable->data + field->offset;
4187 if (mono_compile_aot)
4188 NEW_SFLDACONST (cfg, ins, field);
4190 NEW_PCONST (cfg, ins, addr);
4194 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
4195 * This could be later optimized to do just a couple of
4196 * memory dereferences with constant offsets.
4199 MonoInst *iargs [1];
4200 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
4201 temp = mono_emit_jit_icall (cfg, bblock, mono_get_special_static_data, iargs, ip);
4202 NEW_TEMPLOAD (cfg, ins, temp);
4206 /* FIXME: mark instructions for use in SSA */
4207 if (*ip == CEE_LDSFLDA) {
4209 } else if (*ip == CEE_STSFLD) {
4213 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4214 store->cil_code = ip;
4215 store->inst_left = ins;
4216 store->inst_right = sp [0];
4217 store->flags |= ins_flag;
4220 if (store->opcode == CEE_STOBJ) {
4221 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
4223 MONO_ADD_INS (bblock, store);
4225 gboolean is_const = FALSE;
4226 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4227 if (!((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) &&
4228 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
4229 gpointer addr = (char*)vtable->data + field->offset;
4230 /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
4232 switch (field->type->type) {
4233 case MONO_TYPE_BOOLEAN:
4235 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
4239 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
4242 case MONO_TYPE_CHAR:
4244 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
4248 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
4253 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
4257 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
4262 case MONO_TYPE_STRING:
4263 case MONO_TYPE_OBJECT:
4264 case MONO_TYPE_CLASS:
4265 case MONO_TYPE_SZARRAY:
4267 case MONO_TYPE_FNPTR:
4268 case MONO_TYPE_ARRAY:
4269 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
4270 type_to_eval_stack_type (field->type, *sp);
4275 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
4276 sp [0]->type = STACK_I8;
4277 sp [0]->inst_l = *((gint64 *)addr);
4282 case MONO_TYPE_VALUETYPE:
4291 CHECK_STACK_OVF (1);
4292 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4293 type_to_eval_stack_type (field->type, load);
4294 load->cil_code = ip;
4295 load->inst_left = ins;
4297 load->flags |= ins_flag;
4299 /* fixme: dont see the problem why this does not work */
4300 //cfg->disable_aot = TRUE;
4309 token = read32 (ip + 1);
4310 if (method->wrapper_type != MONO_WRAPPER_NONE)
4311 klass = mono_method_get_wrapper_data (method, token);
4313 klass = mono_class_get (image, token);
4314 mono_class_init (klass);
4315 if (klass->byval_arg.type == MONO_TYPE_VAR)
4316 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4317 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4318 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4319 n = mono_type_to_stind (&klass->byval_arg);
4320 if (n == CEE_STOBJ) {
4321 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
4323 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
4325 MONO_INST_NEW (cfg, store, n);
4326 store->cil_code = ip;
4327 store->inst_left = sp [0];
4328 store->inst_right = sp [1];
4329 store->flags |= ins_flag;
4330 MONO_ADD_INS (bblock, store);
4337 MonoInst *iargs [2];
4338 MonoInst *load, *vtoffset, *add, *val, *vstore;
4343 token = read32 (ip + 1);
4344 if (method->wrapper_type != MONO_WRAPPER_NONE)
4345 klass = mono_method_get_wrapper_data (method, token);
4347 klass = mono_class_get (image, token);
4348 mono_class_init (klass);
4349 if (klass->byval_arg.type == MONO_TYPE_VAR)
4350 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4351 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4352 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4354 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4359 /* much like NEWOBJ */
4360 if (cfg->opt & MONO_OPT_SHARED) {
4361 NEW_DOMAINCONST (cfg, iargs [0]);
4362 NEW_CLASSCONST (cfg, iargs [1], klass);
4364 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
4366 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4367 NEW_VTABLECONST (cfg, iargs [0], vtable);
4368 if (1 || klass->has_finalize || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
4369 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
4371 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
4373 NEW_TEMPLOAD (cfg, load, temp);
4374 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4375 MONO_INST_NEW (cfg, add, CEE_ADD);
4376 add->inst_left = load;
4377 add->inst_right = vtoffset;
4380 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
4381 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
4382 vstore->cil_code = ip;
4383 vstore->inst_left = add;
4384 vstore->inst_right = val;
4386 if (vstore->opcode == CEE_STOBJ) {
4387 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
4389 MONO_ADD_INS (bblock, vstore);
4391 NEW_TEMPLOAD (cfg, load, temp);
4399 MONO_INST_NEW (cfg, ins, *ip);
4403 token = read32 (ip + 1);
4405 /* allocate the domainvar - becaus this is used in decompose_foreach */
4406 if (cfg->opt & MONO_OPT_SHARED)
4407 mono_get_domainvar (cfg);
4409 if (method->wrapper_type != MONO_WRAPPER_NONE)
4410 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4412 klass = mono_class_get (image, token);
4414 mono_class_init (klass);
4415 if (klass->byval_arg.type == MONO_TYPE_VAR)
4416 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4417 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4418 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4419 ins->inst_newa_class = klass;
4420 ins->inst_newa_len = *sp;
4421 ins->type = STACK_OBJ;
4428 MONO_INST_NEW (cfg, ins, *ip);
4429 ins->cil_code = ip++;
4431 ins->inst_left = *sp;
4432 ins->type = STACK_PTR;
4439 if (method->wrapper_type != MONO_WRAPPER_NONE)
4440 klass = (MonoClass*)mono_method_get_wrapper_data (method, read32 (ip + 1));
4442 klass = mono_class_get (image, read32 (ip + 1));
4443 mono_class_init (klass);
4444 if (klass->byval_arg.type == MONO_TYPE_VAR)
4445 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4446 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4447 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4448 NEW_LDELEMA (cfg, ins, sp, klass);
4457 token = read32 (ip + 1);
4458 klass = mono_class_get (image, token);
4459 mono_class_init (klass);
4460 if (klass->byval_arg.type == MONO_TYPE_VAR)
4461 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4462 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4463 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4464 NEW_LDELEMA (cfg, load, sp, klass);
4465 load->cil_code = ip;
4466 MONO_INST_NEW (cfg, ins, mono_type_to_ldind (&klass->byval_arg));
4468 ins->inst_left = load;
4470 type_to_eval_stack_type (&klass->byval_arg, ins);
4484 case CEE_LDELEM_REF: {
4488 * ldind.x (ldelema (array, index))
4489 * ldelema does the bounds check
4493 klass = array_access_to_klass (*ip);
4494 NEW_LDELEMA (cfg, load, sp, klass);
4495 load->cil_code = ip;
4496 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
4498 ins->inst_left = load;
4500 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
4510 case CEE_STELEM_R8: {
4514 * stind.x (ldelema (array, index), val)
4515 * ldelema does the bounds check
4519 klass = array_access_to_klass (*ip);
4520 NEW_LDELEMA (cfg, load, sp, klass);
4521 load->cil_code = ip;
4522 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
4524 ins->inst_left = load;
4525 ins->inst_right = sp [2];
4527 handle_loaded_temps (cfg, bblock, stack_start, sp);
4528 MONO_ADD_INS (bblock, ins);
4530 cfg->disable_ssa = TRUE;
4537 * stind.x (ldelema (array, index), val)
4538 * ldelema does the bounds check
4542 token = read32 (ip + 1);
4543 klass = mono_class_get (image, token);
4544 mono_class_init (klass);
4545 if (klass->byval_arg.type == MONO_TYPE_VAR)
4546 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4547 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4548 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4549 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4550 MonoInst *iargs [3];
4551 handle_loaded_temps (cfg, bblock, stack_start, sp);
4557 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4559 NEW_LDELEMA (cfg, load, sp, klass);
4560 load->cil_code = ip;
4561 MONO_INST_NEW (cfg, ins, mono_type_to_stind (&klass->byval_arg));
4563 ins->inst_left = load;
4564 ins->inst_right = sp [2];
4565 handle_loaded_temps (cfg, bblock, stack_start, sp);
4566 MONO_ADD_INS (bblock, ins);
4570 cfg->disable_ssa = TRUE;
4573 case CEE_STELEM_REF: {
4574 MonoInst *iargs [3];
4579 handle_loaded_temps (cfg, bblock, stack_start, sp);
4585 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4589 NEW_GROUP (cfg, group, sp [0], sp [1]);
4590 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
4592 ins->inst_left = group;
4593 ins->inst_right = sp [2];
4594 MONO_ADD_INS (bblock, ins);
4599 cfg->disable_ssa = TRUE;
4602 case CEE_CKFINITE: {
4603 MonoInst *store, *temp;
4606 /* this instr. can throw exceptions as side effect,
4607 * so we cant eliminate dead code which contains CKFINITE opdodes.
4608 * Spilling to memory makes sure that we always perform
4612 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
4614 ins->inst_left = sp [-1];
4615 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
4617 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4618 store->cil_code = ip;
4619 MONO_ADD_INS (bblock, store);
4621 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
4628 MONO_INST_NEW (cfg, ins, *ip);
4630 klass = mono_class_get (image, read32 (ip + 1));
4631 mono_class_init (klass);
4632 if (klass->byval_arg.type == MONO_TYPE_VAR)
4633 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4634 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4635 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4636 ins->type = STACK_MP;
4637 ins->inst_left = *sp;
4639 ins->inst_newa_class = klass;
4644 case CEE_MKREFANY: {
4645 MonoInst *loc, *klassconst;
4648 MONO_INST_NEW (cfg, ins, *ip);
4650 klass = mono_class_get (image, read32 (ip + 1));
4651 mono_class_init (klass);
4652 if (klass->byval_arg.type == MONO_TYPE_VAR)
4653 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4654 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4655 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4658 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
4659 NEW_TEMPLOADA (cfg, ins->inst_right, loc->inst_c0);
4661 NEW_PCONST (cfg, klassconst, klass);
4662 NEW_GROUP (cfg, ins->inst_left, *sp, klassconst);
4664 MONO_ADD_INS (bblock, ins);
4666 NEW_TEMPLOAD (cfg, *sp, loc->inst_c0);
4673 MonoClass *handle_class;
4675 CHECK_STACK_OVF (1);
4677 n = read32 (ip + 1);
4679 handle = mono_ldtoken (image, n, &handle_class);
4680 mono_class_init (handle_class);
4682 if (cfg->opt & MONO_OPT_SHARED) {
4684 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
4686 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
4688 NEW_IMAGECONST (cfg, iargs [0], image);
4689 NEW_ICONST (cfg, iargs [1], n);
4690 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
4691 NEW_TEMPLOAD (cfg, res, temp);
4692 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4693 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
4694 MONO_ADD_INS (bblock, store);
4695 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4697 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method (image, read32 (ip + 6), NULL)) &&
4698 (cmethod->klass == mono_defaults.monotype_class->parent) &&
4699 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
4700 MonoClass *tclass = mono_class_from_mono_type (handle);
4701 mono_class_init (tclass);
4702 if (mono_compile_aot)
4703 NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n);
4705 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
4706 ins->type = STACK_OBJ;
4707 ins->klass = cmethod->klass;
4710 if (mono_compile_aot)
4711 NEW_LDTOKENCONST (cfg, ins, image, n);
4713 NEW_PCONST (cfg, ins, handle);
4714 ins->type = STACK_VTYPE;
4715 ins->klass = handle_class;
4731 case CEE_ADD_OVF_UN:
4733 case CEE_MUL_OVF_UN:
4735 case CEE_SUB_OVF_UN:
4740 case CEE_ENDFINALLY:
4741 /* FIXME: check stack state */
4742 MONO_INST_NEW (cfg, ins, *ip);
4743 MONO_ADD_INS (bblock, ins);
4744 ins->cil_code = ip++;
4745 start_new_bblock = 1;
4750 if (*ip == CEE_LEAVE) {
4751 target = ip + 5 + (gint32)read32(ip + 1);
4753 target = ip + 2 + (signed char)(ip [1]);
4756 /* empty the stack */
4757 while (sp != stack_start) {
4758 MONO_INST_NEW (cfg, ins, CEE_POP);
4762 MONO_ADD_INS (bblock, ins);
4766 * If this leave statement is in a catch block, check for a
4767 * pending exception, and rethrow it if necessary.
4769 for (i = 0; i < header->num_clauses; ++i) {
4770 MonoExceptionClause *clause = &header->clauses [i];
4771 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code)) {
4774 temp = mono_emit_jit_icall (cfg, bblock, mono_thread_get_pending_exception, NULL, ip);
4775 NEW_TEMPLOAD (cfg, *sp, temp);
4777 MONO_INST_NEW (cfg, ins, OP_THROW_OR_NULL);
4778 ins->inst_left = *sp;
4780 MONO_ADD_INS (bblock, ins);
4784 /* fixme: call fault handler ? */
4786 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
4788 for (tmp = handlers; tmp; tmp = tmp->next) {
4790 link_bblock (cfg, bblock, tblock);
4791 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
4793 ins->inst_target_bb = tblock;
4794 MONO_ADD_INS (bblock, ins);
4796 g_list_free (handlers);
4799 MONO_INST_NEW (cfg, ins, CEE_BR);
4801 MONO_ADD_INS (bblock, ins);
4802 GET_BBLOCK (cfg, bbhash, tblock, target);
4803 link_bblock (cfg, bblock, tblock);
4804 CHECK_BBLOCK (target, ip, tblock);
4805 ins->inst_target_bb = tblock;
4806 start_new_bblock = 1;
4808 if (*ip == CEE_LEAVE)
4817 MONO_INST_NEW (cfg, ins, *ip);
4819 handle_loaded_temps (cfg, bblock, stack_start, sp);
4820 MONO_ADD_INS (bblock, ins);
4821 ins->cil_code = ip++;
4822 ins->inst_i0 = sp [0];
4823 ins->inst_i1 = sp [1];
4831 /* trampoline mono specific opcodes */
4832 case MONO_CUSTOM_PREFIX: {
4834 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
4838 case CEE_MONO_FUNC1: {
4840 gpointer func = NULL;
4845 case MONO_MARSHAL_CONV_STR_LPWSTR:
4846 func = mono_string_to_utf16;
4848 case MONO_MARSHAL_CONV_LPWSTR_STR:
4849 func = mono_string_from_utf16;
4851 case MONO_MARSHAL_CONV_LPSTR_STR:
4852 func = mono_string_new_wrapper;
4854 case MONO_MARSHAL_CONV_STR_LPTSTR:
4855 case MONO_MARSHAL_CONV_STR_LPSTR:
4856 func = mono_string_to_utf8;
4858 case MONO_MARSHAL_CONV_STR_BSTR:
4859 func = mono_string_to_bstr;
4861 case MONO_MARSHAL_CONV_STR_TBSTR:
4862 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
4863 func = mono_string_to_ansibstr;
4865 case MONO_MARSHAL_CONV_SB_LPSTR:
4866 func = mono_string_builder_to_utf8;
4868 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
4869 func = mono_array_to_savearray;
4871 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
4872 func = mono_array_to_lparray;
4874 case MONO_MARSHAL_CONV_DEL_FTN:
4875 func = mono_delegate_to_ftnptr;
4877 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
4878 func = mono_marshal_string_array;
4881 g_warning ("unknown conversion %d\n", ip [2]);
4882 g_assert_not_reached ();
4885 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4886 NEW_TEMPLOAD (cfg, *sp, temp);
4890 inline_costs += 10 * num_calls++;
4893 case CEE_MONO_PROC2: {
4894 gpointer func = NULL;
4899 case MONO_MARSHAL_CONV_LPSTR_SB:
4900 func = mono_string_utf8_to_builder;
4902 case MONO_MARSHAL_FREE_ARRAY:
4903 func = mono_marshal_free_array;
4906 g_assert_not_reached ();
4909 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4911 inline_costs += 10 * num_calls++;
4914 case CEE_MONO_PROC3: {
4915 gpointer func = NULL;
4920 case MONO_MARSHAL_CONV_STR_BYVALSTR:
4921 func = mono_string_to_byvalstr;
4923 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
4924 func = mono_string_to_byvalwstr;
4927 g_assert_not_reached ();
4930 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4932 inline_costs += 10 * num_calls++;
4938 mono_emit_jit_icall (cfg, bblock, g_free, sp, ip);
4940 inline_costs += 10 * num_calls++;
4942 case CEE_MONO_LDPTR:
4943 CHECK_STACK_OVF (1);
4944 token = read32 (ip + 2);
4945 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
4949 inline_costs += 10 * num_calls++;
4951 case CEE_MONO_VTADDR:
4954 MONO_INST_NEW (cfg, ins, OP_VTADDR);
4956 ins->type = STACK_MP;
4957 ins->inst_left = *sp;
4961 case CEE_MONO_NEWOBJ: {
4962 MonoInst *iargs [2];
4964 CHECK_STACK_OVF (1);
4965 token = read32 (ip + 2);
4966 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4967 mono_class_init (klass);
4968 NEW_DOMAINCONST (cfg, iargs [0]);
4969 NEW_CLASSCONST (cfg, iargs [1], klass);
4970 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
4971 NEW_TEMPLOAD (cfg, *sp, temp);
4974 inline_costs += 10 * num_calls++;
4977 case CEE_MONO_OBJADDR:
4980 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
4982 ins->type = STACK_MP;
4983 ins->inst_left = *sp;
4987 case CEE_MONO_LDNATIVEOBJ:
4989 token = read32 (ip + 2);
4990 klass = mono_method_get_wrapper_data (method, token);
4991 g_assert (klass->valuetype);
4992 mono_class_init (klass);
4993 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
4997 case CEE_MONO_RETOBJ:
4998 g_assert (cfg->ret);
4999 g_assert (method->signature->pinvoke);
5003 token = read32 (ip + 2);
5004 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5006 NEW_RETLOADA (cfg, ins);
5007 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
5009 if (sp != stack_start)
5012 MONO_INST_NEW (cfg, ins, CEE_BR);
5014 ins->inst_target_bb = end_bblock;
5015 MONO_ADD_INS (bblock, ins);
5016 link_bblock (cfg, bblock, end_bblock);
5017 start_new_bblock = 1;
5021 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
5029 /* somewhat similar to LDTOKEN */
5030 MonoInst *addr, *vtvar;
5031 CHECK_STACK_OVF (1);
5032 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
5034 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5035 addr->cil_code = ip;
5036 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
5038 ins->inst_left = addr;
5039 MONO_ADD_INS (bblock, ins);
5040 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5053 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
5054 MONO_INST_NEW (cfg, ins, cmp->opcode);
5056 cmp->inst_i0 = sp [0];
5057 cmp->inst_i1 = sp [1];
5061 cmp->opcode = OP_COMPARE;
5063 ins->type = STACK_I4;
5073 CHECK_STACK_OVF (1);
5074 n = read32 (ip + 2);
5075 if (method->wrapper_type != MONO_WRAPPER_NONE)
5076 cmethod = mono_method_get_wrapper_data (method, n);
5078 cmethod = mono_get_method (image, n, NULL);
5081 * We can't do this in mono_ldftn, since it is used in
5082 * the synchronized wrapper, leading to an infinite loop.
5084 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
5085 cmethod = mono_marshal_get_synchronized_wrapper (cmethod);
5088 mono_class_init (cmethod->klass);
5089 handle_loaded_temps (cfg, bblock, stack_start, sp);
5091 NEW_METHODCONST (cfg, argconst, cmethod);
5092 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
5093 NEW_TEMPLOAD (cfg, *sp, temp);
5097 inline_costs += 10 * num_calls++;
5100 case CEE_LDVIRTFTN: {
5105 n = read32 (ip + 2);
5106 if (method->wrapper_type != MONO_WRAPPER_NONE)
5107 cmethod = mono_method_get_wrapper_data (method, n);
5109 cmethod = mono_get_method (image, n, NULL);
5111 mono_class_init (cmethod->klass);
5112 handle_loaded_temps (cfg, bblock, stack_start, sp);
5116 NEW_METHODCONST (cfg, args [1], cmethod);
5117 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
5118 NEW_TEMPLOAD (cfg, *sp, temp);
5122 inline_costs += 10 * num_calls++;
5126 CHECK_STACK_OVF (1);
5127 n = read16 (ip + 2);
5129 NEW_ARGLOAD (cfg, ins, n);
5135 CHECK_STACK_OVF (1);
5136 n = read16 (ip + 2);
5138 NEW_ARGLOADA (cfg, ins, n);
5146 handle_loaded_temps (cfg, bblock, stack_start, sp);
5147 n = read16 (ip + 2);
5149 NEW_ARGSTORE (cfg, ins, n, *sp);
5151 if (ins->opcode == CEE_STOBJ) {
5152 NEW_ARGLOADA (cfg, ins, n);
5153 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5155 MONO_ADD_INS (bblock, ins);
5159 CHECK_STACK_OVF (1);
5160 n = read16 (ip + 2);
5162 NEW_LOCLOAD (cfg, ins, n);
5168 CHECK_STACK_OVF (1);
5169 n = read16 (ip + 2);
5171 NEW_LOCLOADA (cfg, ins, n);
5179 n = read16 (ip + 2);
5181 handle_loaded_temps (cfg, bblock, stack_start, sp);
5182 NEW_LOCSTORE (cfg, ins, n, *sp);
5184 if (ins->opcode == CEE_STOBJ) {
5185 NEW_LOCLOADA (cfg, ins, n);
5186 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5188 MONO_ADD_INS (bblock, ins);
5195 if (sp != stack_start)
5197 MONO_INST_NEW (cfg, ins, 256 + ip [1]);
5198 ins->inst_left = *sp;
5201 cfg->flags |= MONO_CFG_HAS_ALLOCA;
5202 if (header->init_locals)
5203 ins->flags |= MONO_INST_INIT;
5207 /* FIXME: set init flag if locals init is set in this method */
5209 case CEE_ENDFILTER: {
5210 MonoExceptionClause *clause, *nearest;
5211 int cc, nearest_num;
5215 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
5217 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
5218 ins->inst_left = *sp;
5220 MONO_ADD_INS (bblock, ins);
5221 start_new_bblock = 1;
5226 for (cc = 0; cc < header->num_clauses; ++cc) {
5227 clause = &header->clauses [cc];
5228 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
5229 (!nearest || (clause->token_or_filter > nearest->token_or_filter))) {
5235 filter_lengths [nearest_num] = (ip - header->code) - nearest->token_or_filter;
5239 case CEE_UNALIGNED_:
5240 ins_flag |= MONO_INST_UNALIGNED;
5244 ins_flag |= MONO_INST_VOLATILE;
5248 ins_flag |= MONO_INST_TAILCALL;
5249 /* Can't inline tail calls at this time */
5250 inline_costs += 100000;
5256 token = read32 (ip + 2);
5257 if (method->wrapper_type != MONO_WRAPPER_NONE)
5258 klass = mono_method_get_wrapper_data (method, token);
5260 klass = mono_class_get (image, token);
5261 if (klass->byval_arg.type == MONO_TYPE_VAR)
5262 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
5263 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
5264 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
5266 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5267 MonoInst *store, *load;
5268 NEW_PCONST (cfg, load, NULL);
5269 load->cil_code = ip;
5270 load->type = STACK_OBJ;
5271 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
5272 store->cil_code = ip;
5273 handle_loaded_temps (cfg, bblock, stack_start, sp);
5274 MONO_ADD_INS (bblock, store);
5275 store->inst_i0 = sp [0];
5276 store->inst_i1 = load;
5279 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
5284 case CEE_CONSTRAINED_:
5285 /* FIXME: implement */
5286 token = read32 (ip + 2);
5291 MonoInst *iargs [3];
5297 handle_loaded_temps (cfg, bblock, stack_start, sp);
5298 if (ip [1] == CEE_CPBLK) {
5299 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
5301 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
5309 ins_flag |= MONO_INST_NOTYPECHECK;
5311 ins_flag |= MONO_INST_NORANGECHECK;
5312 /* we ignore the no-nullcheck for now since we
5313 * really do it explicitly only when doing callvirt->call
5319 /* FIXME: check we are in a catch handler */
5320 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
5321 load->cil_code = ip;
5322 MONO_INST_NEW (cfg, ins, CEE_THROW);
5323 ins->inst_left = load;
5325 MONO_ADD_INS (bblock, ins);
5327 start_new_bblock = 1;
5332 CHECK_STACK_OVF (1);
5333 token = read32 (ip + 2);
5334 /* FIXXME: handle generics. */
5335 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
5336 MonoType *type = mono_type_create_from_typespec (image, token);
5337 token = mono_type_size (type, &align);
5339 MonoClass *szclass = mono_class_get (image, token);
5340 mono_class_init (szclass);
5341 token = mono_class_value_size (szclass, &align);
5343 NEW_ICONST (cfg, ins, token);
5348 case CEE_REFANYTYPE:
5350 MONO_INST_NEW (cfg, ins, ip [1]);
5352 ins->type = STACK_MP;
5353 ins->inst_left = *sp;
5354 ins->type = STACK_VTYPE;
5355 ins->klass = mono_defaults.typehandle_class;
5364 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
5369 g_error ("opcode 0x%02x not handled", *ip);
5372 if (start_new_bblock != 1)
5375 bblock->cil_length = ip - bblock->cil_code;
5376 bblock->next_bb = end_bblock;
5377 link_bblock (cfg, bblock, end_bblock);
5379 if (cfg->method == method && cfg->domainvar) {
5383 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
5384 call->signature = helper_sig_domain_get;
5385 call->inst.type = STACK_PTR;
5386 call->fptr = mono_domain_get;
5387 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, (MonoInst*)call);
5389 MONO_ADD_INS (init_localsbb, store);
5392 if (header->init_locals) {
5394 for (i = 0; i < header->num_locals; ++i) {
5395 int t = header->locals [i]->type;
5396 if (t == MONO_TYPE_VALUETYPE && header->locals [i]->data.klass->enumtype)
5397 t = header->locals [i]->data.klass->enum_basetype->type;
5398 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
5399 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
5400 NEW_ICONST (cfg, ins, 0);
5401 NEW_LOCSTORE (cfg, store, i, ins);
5402 MONO_ADD_INS (init_localsbb, store);
5403 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
5404 MONO_INST_NEW (cfg, ins, OP_I8CONST);
5405 ins->type = STACK_I8;
5407 NEW_LOCSTORE (cfg, store, i, ins);
5408 MONO_ADD_INS (init_localsbb, store);
5409 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
5410 MONO_INST_NEW (cfg, ins, OP_R8CONST);
5411 ins->type = STACK_R8;
5412 ins->inst_p0 = (void*)&r8_0;
5413 NEW_LOCSTORE (cfg, store, i, ins);
5414 MONO_ADD_INS (init_localsbb, store);
5415 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF)) {
5416 NEW_LOCLOADA (cfg, ins, i);
5417 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (header->locals [i]), NULL, NULL);
5420 NEW_PCONST (cfg, ins, NULL);
5421 NEW_LOCSTORE (cfg, store, i, ins);
5422 MONO_ADD_INS (init_localsbb, store);
5428 /* resolve backward branches in the middle of an existing basic block */
5429 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
5431 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
5432 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
5433 if (tblock != start_bblock) {
5435 split_bblock (cfg, tblock, bblock);
5436 l = bblock->cil_code - header->code;
5437 bblock->cil_length = tblock->cil_length - l;
5438 tblock->cil_length = l;
5440 g_print ("recheck failed.\n");
5444 /* we compute regions here, because the length of filter clauses is not known in advance.
5445 * It is computed in the CEE_ENDFILTER case in the above switch statement*/
5446 if (cfg->method == method) {
5448 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5449 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
5451 mono_create_spvar_for_region (cfg, bb->region);
5452 if (cfg->verbose_level > 2)
5453 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
5456 g_hash_table_destroy (bbhash);
5459 dont_inline = g_list_remove (dont_inline, method);
5460 return inline_costs;
5463 if (cfg->method != method)
5464 g_hash_table_destroy (bbhash);
5465 dont_inline = g_list_remove (dont_inline, method);
5469 if (cfg->method != method)
5470 g_hash_table_destroy (bbhash);
5471 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
5472 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
5473 dont_inline = g_list_remove (dont_inline, method);
5478 mono_print_tree (MonoInst *tree) {
5484 arity = mono_burg_arity [tree->opcode];
5486 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
5488 switch (tree->opcode) {
5490 printf ("[%d]", tree->inst_c0);
5493 printf ("[%lld]", tree->inst_l);
5496 printf ("[%f]", *(double*)tree->inst_p0);
5499 printf ("[%f]", *(float*)tree->inst_p0);
5503 printf ("[%d]", tree->inst_c0);
5506 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5509 printf ("[%s]", mono_arch_regname (tree->dreg));
5512 printf ("[%s]", tree->inst_newa_class->name);
5513 mono_print_tree (tree->inst_newa_len);
5524 case OP_VOIDCALLVIRT: {
5525 MonoCallInst *call = (MonoCallInst*)tree;
5527 printf ("[%s]", call->method->name);
5532 printf ("[%d (", tree->inst_c0);
5533 for (i = 0; i < tree->inst_phi_args [0]; i++) {
5536 printf ("%d", tree->inst_phi_args [i + 1]);
5548 printf ("[B%d]", tree->inst_target_bb->block_num);
5558 case OP_VOIDCALL_REG:
5559 mono_print_tree (tree->inst_left);
5571 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
5572 mono_print_tree (tree->inst_left);
5576 mono_print_tree (tree->inst_left);
5578 mono_print_tree (tree->inst_right);
5588 mono_print_tree_nl (MonoInst *tree)
5590 mono_print_tree (tree);
5595 create_helper_signature (void)
5597 /* FIXME: set call conv */
5598 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
5599 helper_sig_newarr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5600 helper_sig_newarr->params [0] = helper_sig_newarr->params [1] = &mono_defaults.int_class->byval_arg;
5601 helper_sig_newarr->ret = &mono_defaults.object_class->byval_arg;
5602 helper_sig_newarr->params [2] = &mono_defaults.int32_class->byval_arg;
5603 helper_sig_newarr->pinvoke = 1;
5605 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
5606 helper_sig_newarr_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5607 helper_sig_newarr_specific->params [0] = &mono_defaults.int_class->byval_arg;
5608 helper_sig_newarr_specific->params [1] = &mono_defaults.int32_class->byval_arg;
5609 helper_sig_newarr_specific->ret = &mono_defaults.object_class->byval_arg;
5610 helper_sig_newarr_specific->pinvoke = 1;
5612 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
5613 helper_sig_object_new = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5614 helper_sig_object_new->params [0] = helper_sig_object_new->params [1] = &mono_defaults.int_class->byval_arg;
5615 helper_sig_object_new->ret = &mono_defaults.object_class->byval_arg;
5616 helper_sig_object_new->pinvoke = 1;
5618 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
5619 helper_sig_object_new_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5620 helper_sig_object_new_specific->params [0] = &mono_defaults.int_class->byval_arg;
5621 helper_sig_object_new_specific->ret = &mono_defaults.object_class->byval_arg;
5622 helper_sig_object_new_specific->pinvoke = 1;
5624 /* void* mono_method_compile (MonoMethod*) */
5625 helper_sig_compile = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5626 helper_sig_compile->params [0] = helper_sig_compile->ret = &mono_defaults.int_class->byval_arg;
5627 helper_sig_compile->pinvoke = 1;
5629 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
5630 helper_sig_compile_virt = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5631 helper_sig_compile_virt->params [0] = &mono_defaults.object_class->byval_arg;
5632 helper_sig_compile_virt->params [1] = helper_sig_compile_virt->ret = &mono_defaults.int_class->byval_arg;
5633 helper_sig_compile_virt->pinvoke = 1;
5635 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
5636 helper_sig_ldstr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5637 helper_sig_ldstr->params [0] = helper_sig_ldstr->params [1] = &mono_defaults.int_class->byval_arg;
5638 helper_sig_ldstr->params [2] = &mono_defaults.int32_class->byval_arg;
5639 helper_sig_ldstr->ret = &mono_defaults.object_class->byval_arg;
5640 helper_sig_ldstr->pinvoke = 1;
5642 /* MonoDomain *mono_domain_get (void) */
5643 helper_sig_domain_get = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5644 helper_sig_domain_get->ret = &mono_defaults.int_class->byval_arg;
5645 helper_sig_domain_get->pinvoke = 1;
5647 /* void* stelem_ref (MonoArray *, int index, MonoObject *) */
5648 helper_sig_stelem_ref = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5649 helper_sig_stelem_ref->params [0] = &mono_defaults.array_class->byval_arg;
5650 helper_sig_stelem_ref->params [1] = &mono_defaults.int32_class->byval_arg;
5651 helper_sig_stelem_ref->params [2] = &mono_defaults.object_class->byval_arg;
5652 helper_sig_stelem_ref->ret = &mono_defaults.void_class->byval_arg;
5653 helper_sig_stelem_ref->pinvoke = 1;
5655 /* long amethod (long, long) */
5656 helper_sig_long_long_long = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5657 helper_sig_long_long_long->params [0] = helper_sig_long_long_long->params [1] =
5658 &mono_defaults.int64_class->byval_arg;
5659 helper_sig_long_long_long->ret = &mono_defaults.int64_class->byval_arg;
5660 helper_sig_long_long_long->pinvoke = 1;
5662 /* object amethod (intptr) */
5663 helper_sig_obj_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5664 helper_sig_obj_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5665 helper_sig_obj_ptr->ret = &mono_defaults.object_class->byval_arg;
5666 helper_sig_obj_ptr->pinvoke = 1;
5668 /* void amethod (intptr) */
5669 helper_sig_void_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5670 helper_sig_void_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5671 helper_sig_void_ptr->ret = &mono_defaults.void_class->byval_arg;
5672 helper_sig_void_ptr->pinvoke = 1;
5674 /* void amethod (MonoObject *obj) */
5675 helper_sig_void_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5676 helper_sig_void_obj->params [0] = &mono_defaults.object_class->byval_arg;
5677 helper_sig_void_obj->ret = &mono_defaults.void_class->byval_arg;
5678 helper_sig_void_obj->pinvoke = 1;
5680 /* intptr amethod (void) */
5681 helper_sig_ptr_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5682 helper_sig_ptr_void->ret = &mono_defaults.int_class->byval_arg;
5683 helper_sig_ptr_void->pinvoke = 1;
5685 /* object amethod (void) */
5686 helper_sig_obj_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5687 helper_sig_obj_void->ret = &mono_defaults.object_class->byval_arg;
5688 helper_sig_obj_void->pinvoke = 1;
5690 /* void amethod (intptr, intptr) */
5691 helper_sig_void_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5692 helper_sig_void_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5693 helper_sig_void_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5694 helper_sig_void_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
5695 helper_sig_void_ptr_ptr->pinvoke = 1;
5697 /* void amethod (intptr, intptr, intptr) */
5698 helper_sig_void_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5699 helper_sig_void_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5700 helper_sig_void_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5701 helper_sig_void_ptr_ptr_ptr->params [2] = &mono_defaults.int_class->byval_arg;
5702 helper_sig_void_ptr_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
5703 helper_sig_void_ptr_ptr_ptr->pinvoke = 1;
5705 /* intptr amethod (intptr, intptr) */
5706 helper_sig_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5707 helper_sig_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5708 helper_sig_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5709 helper_sig_ptr_ptr_ptr->ret = &mono_defaults.int_class->byval_arg;
5710 helper_sig_ptr_ptr_ptr->pinvoke = 1;
5712 /* IntPtr amethod (object) */
5713 helper_sig_ptr_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5714 helper_sig_ptr_obj->params [0] = &mono_defaults.object_class->byval_arg;
5715 helper_sig_ptr_obj->ret = &mono_defaults.int_class->byval_arg;
5716 helper_sig_ptr_obj->pinvoke = 1;
5718 /* IntPtr amethod (int) */
5719 helper_sig_ptr_int = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5720 helper_sig_ptr_int->params [0] = &mono_defaults.int32_class->byval_arg;
5721 helper_sig_ptr_int->ret = &mono_defaults.int_class->byval_arg;
5722 helper_sig_ptr_int->pinvoke = 1;
5724 /* long amethod (long, guint32) */
5725 helper_sig_long_long_int = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5726 helper_sig_long_long_int->params [0] = &mono_defaults.int64_class->byval_arg;
5727 helper_sig_long_long_int->params [1] = &mono_defaults.int32_class->byval_arg;
5728 helper_sig_long_long_int->ret = &mono_defaults.int64_class->byval_arg;
5729 helper_sig_long_long_int->pinvoke = 1;
5731 /* ulong amethod (double) */
5732 helper_sig_ulong_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5733 helper_sig_ulong_double->params [0] = &mono_defaults.double_class->byval_arg;
5734 helper_sig_ulong_double->ret = &mono_defaults.uint64_class->byval_arg;
5735 helper_sig_ulong_double->pinvoke = 1;
5737 /* long amethod (double) */
5738 helper_sig_long_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5739 helper_sig_long_double->params [0] = &mono_defaults.double_class->byval_arg;
5740 helper_sig_long_double->ret = &mono_defaults.int64_class->byval_arg;
5741 helper_sig_long_double->pinvoke = 1;
5743 /* double amethod (long) */
5744 helper_sig_double_long = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5745 helper_sig_double_long->params [0] = &mono_defaults.int64_class->byval_arg;
5746 helper_sig_double_long->ret = &mono_defaults.double_class->byval_arg;
5747 helper_sig_double_long->pinvoke = 1;
5749 /* float amethod (long) */
5750 helper_sig_float_long = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5751 helper_sig_float_long->params [0] = &mono_defaults.int64_class->byval_arg;
5752 helper_sig_float_long->ret = &mono_defaults.single_class->byval_arg;
5753 helper_sig_float_long->pinvoke = 1;
5755 /* double amethod (double, double) */
5756 helper_sig_double_double_double = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5757 helper_sig_double_double_double->params [0] = &mono_defaults.double_class->byval_arg;
5758 helper_sig_double_double_double->params [1] = &mono_defaults.double_class->byval_arg;
5759 helper_sig_double_double_double->ret = &mono_defaults.double_class->byval_arg;
5760 helper_sig_double_double_double->pinvoke = 1;
5762 /* uint amethod (double) */
5763 helper_sig_uint_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5764 helper_sig_uint_double->params [0] = &mono_defaults.double_class->byval_arg;
5765 helper_sig_uint_double->ret = &mono_defaults.uint32_class->byval_arg;
5766 helper_sig_uint_double->pinvoke = 1;
5768 /* int amethod (double) */
5769 helper_sig_int_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5770 helper_sig_int_double->params [0] = &mono_defaults.double_class->byval_arg;
5771 helper_sig_int_double->ret = &mono_defaults.int32_class->byval_arg;
5772 helper_sig_int_double->pinvoke = 1;
5774 /* void initobj (intptr, int size) */
5775 helper_sig_initobj = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5776 helper_sig_initobj->params [0] = &mono_defaults.int_class->byval_arg;
5777 helper_sig_initobj->params [1] = &mono_defaults.int32_class->byval_arg;
5778 helper_sig_initobj->ret = &mono_defaults.void_class->byval_arg;
5779 helper_sig_initobj->pinvoke = 1;
5781 /* void memcpy (intptr, intptr, int size) */
5782 helper_sig_memcpy = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5783 helper_sig_memcpy->params [0] = &mono_defaults.int_class->byval_arg;
5784 helper_sig_memcpy->params [1] = &mono_defaults.int_class->byval_arg;
5785 helper_sig_memcpy->params [2] = &mono_defaults.int32_class->byval_arg;
5786 helper_sig_memcpy->ret = &mono_defaults.void_class->byval_arg;
5787 helper_sig_memcpy->pinvoke = 1;
5789 /* void memset (intptr, int val, int size) */
5790 helper_sig_memset = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5791 helper_sig_memset->params [0] = &mono_defaults.int_class->byval_arg;
5792 helper_sig_memset->params [1] = &mono_defaults.int32_class->byval_arg;
5793 helper_sig_memset->params [2] = &mono_defaults.int32_class->byval_arg;
5794 helper_sig_memset->ret = &mono_defaults.void_class->byval_arg;
5795 helper_sig_memset->pinvoke = 1;
5797 helper_sig_class_init_trampoline = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5798 helper_sig_class_init_trampoline->ret = &mono_defaults.void_class->byval_arg;
5799 helper_sig_class_init_trampoline->pinvoke = 1;
5802 static GHashTable *jit_icall_hash_name = NULL;
5803 static GHashTable *jit_icall_hash_addr = NULL;
5806 mono_find_jit_icall_by_name (const char *name)
5808 g_assert (jit_icall_hash_name);
5810 //printf ("lookup addr %s %p\n", name, g_hash_table_lookup (jit_icall_hash_name, name));
5811 return g_hash_table_lookup (jit_icall_hash_name, name);
5815 mono_find_jit_icall_by_addr (gconstpointer addr)
5817 g_assert (jit_icall_hash_addr);
5819 return g_hash_table_lookup (jit_icall_hash_addr, (gpointer)addr);
5823 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
5826 MonoMethod *wrapper;
5828 if (callinfo->wrapper)
5829 return callinfo->wrapper;
5830 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
5831 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
5832 callinfo->wrapper = mono_jit_compile_method (wrapper);
5834 return callinfo->wrapper;
5838 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save)
5840 MonoJitICallInfo *info;
5845 if (!jit_icall_hash_name) {
5846 jit_icall_hash_name = g_hash_table_new (g_str_hash, g_str_equal);
5847 jit_icall_hash_addr = g_hash_table_new (NULL, NULL);
5850 if (g_hash_table_lookup (jit_icall_hash_name, name)) {
5851 g_warning ("jit icall already defined \"%s\"\n", name);
5852 g_assert_not_reached ();
5855 info = g_new (MonoJitICallInfo, 1);
5862 #ifdef MONO_USE_EXC_TABLES
5863 || mono_arch_has_unwind_info (func)
5866 info->wrapper = func;
5868 info->wrapper = NULL;
5869 mono_icall_get_wrapper (info);
5872 g_hash_table_insert (jit_icall_hash_name, (gpointer)info->name, info);
5873 g_hash_table_insert (jit_icall_hash_addr, (gpointer)func, info);
5874 if (func != info->wrapper)
5875 g_hash_table_insert (jit_icall_hash_addr, (gpointer)info->wrapper, info);
5881 mono_create_class_init_trampoline (MonoVTable *vtable)
5885 /* previously created trampoline code */
5886 mono_domain_lock (vtable->domain);
5888 mono_g_hash_table_lookup (vtable->domain->class_init_trampoline_hash,
5890 mono_domain_unlock (vtable->domain);
5894 code = mono_arch_create_class_init_trampoline (vtable);
5896 /* store trampoline address */
5897 mono_domain_lock (vtable->domain);
5898 mono_g_hash_table_insert (vtable->domain->class_init_trampoline_hash,
5900 mono_domain_unlock (vtable->domain);
5902 EnterCriticalSection (&class_init_hash_mutex);
5903 if (!class_init_hash_addr)
5904 class_init_hash_addr = g_hash_table_new (NULL, NULL);
5905 g_hash_table_insert (class_init_hash_addr, code, vtable);
5906 LeaveCriticalSection (&class_init_hash_mutex);
5912 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
5916 EnterCriticalSection (&class_init_hash_mutex);
5917 if (class_init_hash_addr)
5918 res = g_hash_table_lookup (class_init_hash_addr, addr);
5921 LeaveCriticalSection (&class_init_hash_mutex);
5925 static MonoJitICallInfo **emul_opcode_map = NULL;
5927 static inline MonoJitICallInfo *
5928 mono_find_jit_opcode_emulation (int opcode)
5930 if (emul_opcode_map)
5931 return emul_opcode_map [opcode];
5937 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func, gboolean no_throw)
5939 MonoJitICallInfo *info;
5941 if (!emul_opcode_map)
5942 emul_opcode_map = g_new0 (MonoJitICallInfo*, OP_LAST + 1);
5944 g_assert (!sig->hasthis);
5945 g_assert (sig->param_count < 3);
5947 info = mono_register_jit_icall (func, name, sig, no_throw);
5949 emul_opcode_map [opcode] = info;
5953 decompose_foreach (MonoInst *tree, gpointer data)
5955 static MonoJitICallInfo *newarr_info = NULL;
5956 static MonoJitICallInfo *newarr_specific_info = NULL;
5957 MonoJitICallInfo *info;
5960 switch (tree->opcode) {
5962 MonoCompile *cfg = data;
5963 MonoInst *iargs [3];
5966 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
5967 g_assert (newarr_info);
5968 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
5969 g_assert (newarr_specific_info);
5972 if (cfg->opt & MONO_OPT_SHARED) {
5973 NEW_DOMAINCONST (cfg, iargs [0]);
5974 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
5975 iargs [2] = tree->inst_newa_len;
5980 MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
5982 NEW_VTABLECONST (cfg, iargs [0], vtable);
5983 iargs [1] = tree->inst_newa_len;
5985 info = newarr_specific_info;
5988 mono_emulate_opcode (cfg, tree, iargs, info);
5990 /* Need to decompose arguments after the the opcode is decomposed */
5991 for (i = 0; i < info->sig->param_count; ++i)
5992 dec_foreach (iargs [i], cfg);
6002 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
6004 switch (mono_burg_arity [tree->opcode]) {
6007 mono_inst_foreach (tree->inst_left, func, data);
6010 mono_inst_foreach (tree->inst_left, func, data);
6011 mono_inst_foreach (tree->inst_right, func, data);
6014 g_assert_not_reached ();
6021 mono_print_bb_code (MonoBasicBlock *bb) {
6023 MonoInst *c = bb->code;
6025 mono_print_tree (c);
6033 print_dfn (MonoCompile *cfg) {
6038 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
6040 for (i = 0; i < cfg->num_bblocks; ++i) {
6041 bb = cfg->bblocks [i];
6043 char* code1, *code2;
6044 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
6045 if (bb->last_ins->cil_code)
6046 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
6048 code2 = g_strdup ("");
6050 code1 [strlen (code1) - 1] = 0;
6051 code = g_strdup_printf ("%s -> %s", code1, code2);
6055 code = g_strdup ("\n");
6056 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
6058 MonoInst *c = bb->code;
6060 mono_print_tree (c);
6068 g_print ("\tprev:");
6069 for (j = 0; j < bb->in_count; ++j) {
6070 g_print (" BB%d", bb->in_bb [j]->block_num);
6072 g_print ("\t\tsucc:");
6073 for (j = 0; j < bb->out_count; ++j) {
6074 g_print (" BB%d", bb->out_bb [j]->block_num);
6076 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
6079 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
6082 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
6084 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
6092 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
6096 g_assert (bb->code);
6097 bb->last_ins->next = inst;
6098 bb->last_ins = inst;
6100 bb->last_ins = bb->code = inst;
6105 mono_destroy_compile (MonoCompile *cfg)
6107 //mono_mempool_stats (cfg->mempool);
6108 g_hash_table_destroy (cfg->bb_hash);
6110 mono_regstate_free (cfg->rs);
6112 g_hash_table_destroy (cfg->spvars);
6113 mono_mempool_destroy (cfg->mempool);
6114 g_list_free (cfg->ldstr_list);
6116 g_free (cfg->varinfo);
6122 mono_get_lmf_addr (void)
6124 MonoJitTlsData *jit_tls;
6126 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
6127 return &jit_tls->lmf;
6129 g_assert_not_reached ();
6134 * mono_thread_abort:
6135 * @obj: exception object
6137 * abort the thread, print exception information and stack trace
6140 mono_thread_abort (MonoObject *obj)
6142 /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
6144 /* handle_remove should be eventually called for this thread, too
6151 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
6153 MonoJitTlsData *jit_tls;
6156 jit_tls = g_new0 (MonoJitTlsData, 1);
6158 TlsSetValue (mono_jit_tls_id, jit_tls);
6160 jit_tls->abort_func = abort_func;
6161 jit_tls->end_of_stack = stack_start;
6163 lmf = g_new0 (MonoLMF, 1);
6166 jit_tls->lmf = jit_tls->first_lmf = lmf;
6168 mono_arch_setup_jit_tls_data (jit_tls);
6174 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
6177 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
6178 thread = mono_thread_current ();
6180 thread->jit_data = jit_tls;
6183 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
6186 mono_thread_abort_dummy (MonoObject *obj)
6188 if (mono_thread_attach_aborted_cb)
6189 mono_thread_attach_aborted_cb (obj);
6191 mono_thread_abort (obj);
6195 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
6198 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
6199 thread = mono_thread_current ();
6201 thread->jit_data = jit_tls;
6205 mini_thread_cleanup (MonoThread *thread)
6207 MonoJitTlsData *jit_tls = thread->jit_data;
6210 g_free (jit_tls->first_lmf);
6212 thread->jit_data = NULL;
6217 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
6219 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
6223 ji->data.target = target;
6224 ji->next = cfg->patch_info;
6226 cfg->patch_info = ji;
6230 mono_remove_patch_info (MonoCompile *cfg, int ip)
6232 MonoJumpInfo **ji = &cfg->patch_info;
6235 if ((*ji)->ip.i == ip)
6238 ji = &((*ji)->next);
6243 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
6244 MonoJitICallInfo *info;
6246 decompose_foreach (tree, cfg);
6248 switch (mono_burg_arity [tree->opcode]) {
6251 dec_foreach (tree->inst_left, cfg);
6253 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6254 MonoInst *iargs [2];
6256 iargs [0] = tree->inst_left;
6258 mono_emulate_opcode (cfg, tree, iargs, info);
6264 #ifdef MONO_ARCH_BIGMUL_INTRINS
6265 if (tree->opcode == OP_LMUL
6266 && (cfg->opt & MONO_OPT_INTRINS)
6267 && (tree->inst_left->opcode == CEE_CONV_I8
6268 || tree->inst_left->opcode == CEE_CONV_U8)
6269 && tree->inst_left->inst_left->type == STACK_I4
6270 && (tree->inst_right->opcode == CEE_CONV_I8
6271 || tree->inst_right->opcode == CEE_CONV_U8)
6272 && tree->inst_right->inst_left->type == STACK_I4) {
6273 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
6274 tree->inst_left = tree->inst_left->inst_left;
6275 tree->inst_right = tree->inst_right->inst_left;
6276 dec_foreach (tree, cfg);
6279 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6280 MonoInst *iargs [2];
6282 iargs [0] = tree->inst_i0;
6283 iargs [1] = tree->inst_i1;
6285 mono_emulate_opcode (cfg, tree, iargs, info);
6287 dec_foreach (iargs [0], cfg);
6288 dec_foreach (iargs [1], cfg);
6291 dec_foreach (tree->inst_left, cfg);
6292 dec_foreach (tree->inst_right, cfg);
6296 g_assert_not_reached ();
6301 decompose_pass (MonoCompile *cfg) {
6304 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6307 cfg->prev_ins = NULL;
6308 for (tree = cfg->cbb->code; tree; tree = tree->next) {
6309 dec_foreach (tree, cfg);
6310 cfg->prev_ins = tree;
6316 nullify_basic_block (MonoBasicBlock *bb)
6323 bb->code = bb->last_ins = NULL;
6324 bb->cil_code = NULL;
6328 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6332 for (i = 0; i < bb->out_count; i++) {
6333 MonoBasicBlock *ob = bb->out_bb [i];
6336 if (bb->out_count > 1) {
6337 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
6341 bb->out_bb [i] = repl;
6348 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6352 for (i = 0; i < bb->in_count; i++) {
6353 MonoBasicBlock *ib = bb->in_bb [i];
6356 if (bb->in_count > 1) {
6357 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
6361 bb->in_bb [i] = repl;
6368 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6372 for (i = 0; i < bb->out_count; i++) {
6373 MonoBasicBlock *ob = bb->out_bb [i];
6374 for (j = 0; j < ob->in_count; j++) {
6375 if (ob->in_bb [j] == orig) {
6376 ob->in_bb [j] = repl;
6385 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
6387 bb->out_count = bbn->out_count;
6388 bb->out_bb = bbn->out_bb;
6390 replace_basic_block (bb, bbn, bb);
6394 bb->last_ins->next = bbn->code;
6395 bb->last_ins = bbn->last_ins;
6398 bb->code = bbn->code;
6399 bb->last_ins = bbn->last_ins;
6401 bb->next_bb = bbn->next_bb;
6402 nullify_basic_block (bbn);
6406 optimize_branches (MonoCompile *cfg) {
6407 int i, changed = FALSE;
6408 MonoBasicBlock *bb, *bbn;
6413 /* we skip the entry block (exit is handled specially instead ) */
6414 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
6416 /* dont touch code inside exception clauses */
6417 if (bb->region != -1)
6420 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
6421 if (cfg->verbose_level > 2)
6422 g_print ("nullify block triggered %d\n", bbn->block_num);
6424 bb->next_bb = bbn->next_bb;
6426 for (i = 0; i < bbn->out_count; i++)
6427 replace_in_block (bbn->out_bb [i], bbn, NULL);
6429 nullify_basic_block (bbn);
6433 if (bb->out_count == 1) {
6434 bbn = bb->out_bb [0];
6436 /* conditional branches where true and false targets are the same can be also replaced with CEE_BR */
6437 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
6438 bb->last_ins->opcode = CEE_BR;
6439 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
6441 if (cfg->verbose_level > 2)
6442 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
6445 if (bb->region == bbn->region && bb->next_bb == bbn) {
6446 /* the block are in sequence anyway ... */
6448 /* branches to the following block can be removed */
6449 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
6450 bb->last_ins->opcode = CEE_NOP;
6452 if (cfg->verbose_level > 2)
6453 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
6456 if (bbn->in_count == 1) {
6458 if (bbn != cfg->bb_exit) {
6459 if (cfg->verbose_level > 2)
6460 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
6461 merge_basic_blocks (bb, bbn);
6465 //mono_print_bb_code (bb);
6475 /* we skip the entry block (exit is handled specially instead ) */
6476 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
6478 /* dont touch code inside exception clauses */
6479 if (bb->region != -1)
6482 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
6483 if (cfg->verbose_level > 2) {
6484 g_print ("nullify block triggered %d\n", bbn->block_num);
6486 bb->next_bb = bbn->next_bb;
6488 for (i = 0; i < bbn->out_count; i++)
6489 replace_in_block (bbn->out_bb [i], bbn, NULL);
6491 nullify_basic_block (bbn);
6497 if (bb->out_count == 1) {
6498 bbn = bb->out_bb [0];
6500 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
6501 bbn = bb->last_ins->inst_target_bb;
6502 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
6503 bbn->code->inst_target_bb->region == bb->region) {
6505 if (cfg->verbose_level > 2)
6506 g_print ("in %s branch to branch triggered %d -> %d\n", cfg->method->name,
6507 bb->block_num, bbn->block_num);
6509 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
6510 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
6515 } else if (bb->out_count == 2) {
6516 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
6517 bbn = bb->last_ins->inst_true_bb;
6518 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
6519 bbn->code->inst_target_bb->region == bb->region) {
6520 if (cfg->verbose_level > 2)
6521 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
6522 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
6525 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
6527 replace_in_block (bbn, bb, NULL);
6529 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
6530 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
6532 link_bblock (cfg, bb, bbn->code->inst_target_bb);
6538 bbn = bb->last_ins->inst_false_bb;
6539 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
6540 bbn->code->inst_target_bb->region == bb->region) {
6541 if (cfg->verbose_level > 2)
6542 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
6543 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
6546 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
6548 replace_in_block (bbn, bb, NULL);
6550 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
6551 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
6553 link_bblock (cfg, bb, bbn->code->inst_target_bb);
6566 mono_compile_create_vars (MonoCompile *cfg)
6568 MonoMethodSignature *sig;
6569 MonoMethodHeader *header;
6572 header = ((MonoMethodNormal *)cfg->method)->header;
6574 sig = cfg->method->signature;
6576 if (!MONO_TYPE_IS_VOID (sig->ret)) {
6577 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
6578 cfg->ret->opcode = OP_RETARG;
6579 cfg->ret->inst_vtype = sig->ret;
6580 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
6582 if (cfg->verbose_level > 2)
6583 g_print ("creating vars\n");
6586 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
6588 for (i = 0; i < sig->param_count; ++i)
6589 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
6591 cfg->locals_start = cfg->num_varinfo;
6593 if (cfg->verbose_level > 2)
6594 g_print ("creating locals\n");
6595 for (i = 0; i < header->num_locals; ++i)
6596 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
6597 if (cfg->verbose_level > 2)
6598 g_print ("locals done\n");
6602 mono_print_code (MonoCompile *cfg)
6606 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6607 MonoInst *tree = bb->code;
6612 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
6614 for (; tree; tree = tree->next) {
6615 mono_print_tree (tree);
6620 bb->last_ins->next = NULL;
6624 extern const char * const mono_burg_rule_string [];
6627 emit_state (MonoCompile *cfg, MBState *state, int goal)
6630 int ern = mono_burg_rule (state, goal);
6631 const guint16 *nts = mono_burg_nts [ern];
6634 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
6638 // state->reg1 = state->reg2; /* chain rule */
6640 state->reg1 = mono_regstate_next_int (cfg->rs);
6641 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
6644 state->reg1 = mono_regstate_next_int (cfg->rs);
6645 state->reg2 = mono_regstate_next_int (cfg->rs);
6648 state->reg1 = mono_regstate_next_float (cfg->rs);
6655 mono_burg_kids (state, ern, kids);
6657 emit_state (cfg, kids [0], nts [0]);
6659 emit_state (cfg, kids [1], nts [1]);
6661 g_assert (!nts [3]);
6662 emit_state (cfg, kids [2], nts [2]);
6667 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
6668 if ((emit = mono_burg_func [ern]))
6669 emit (state, state->tree, cfg);
6672 #define DEBUG_SELECTION
6675 mini_select_instructions (MonoCompile *cfg)
6677 static int reverse_map [] = {
6678 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
6679 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
6681 static int reverse_fmap [] = {
6682 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
6683 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
6685 static int reverse_lmap [] = {
6686 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
6687 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
6692 cfg->state_pool = mono_mempool_new ();
6693 cfg->rs = mono_regstate_new ();
6695 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6696 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode) &&
6697 bb->next_bb != bb->last_ins->inst_false_bb) {
6699 if (bb->next_bb == bb->last_ins->inst_true_bb) {
6700 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
6701 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
6702 bb->last_ins->inst_false_bb = tmp;
6704 if (bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
6705 bb->last_ins->opcode = reverse_map [bb->last_ins->opcode - CEE_BEQ];
6706 } else if (bb->last_ins->opcode >= OP_FBEQ && bb->last_ins->opcode <= OP_FBLT_UN) {
6707 bb->last_ins->opcode = reverse_fmap [bb->last_ins->opcode - OP_FBEQ];
6708 } else if (bb->last_ins->opcode >= OP_LBEQ && bb->last_ins->opcode <= OP_LBLT_UN) {
6709 bb->last_ins->opcode = reverse_lmap [bb->last_ins->opcode - OP_LBEQ];
6712 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
6713 inst->opcode = CEE_BR;
6714 inst->inst_target_bb = bb->last_ins->inst_false_bb;
6715 mono_bblock_add_inst (bb, inst);
6720 #ifdef DEBUG_SELECTION
6721 if (cfg->verbose_level >= 4) {
6722 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6723 MonoInst *tree = bb->code;
6724 g_print ("DUMP BLOCK %d:\n", bb->block_num);
6727 for (; tree; tree = tree->next) {
6728 mono_print_tree (tree);
6735 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6736 MonoInst *tree = bb->code, *next;
6742 bb->last_ins = NULL;
6745 mono_regstate_reset (cfg->rs);
6747 #ifdef DEBUG_SELECTION
6748 if (cfg->verbose_level >= 3)
6749 g_print ("LABEL BLOCK %d:\n", bb->block_num);
6751 for (; tree; tree = next) {
6753 #ifdef DEBUG_SELECTION
6754 if (cfg->verbose_level >= 3) {
6755 mono_print_tree (tree);
6760 if (!(mbstate = mono_burg_label (tree, cfg))) {
6761 g_warning ("unable to label tree %p", tree);
6762 mono_print_tree (tree);
6764 g_assert_not_reached ();
6766 emit_state (cfg, mbstate, MB_NTERM_stmt);
6768 bb->max_ireg = cfg->rs->next_vireg;
6769 bb->max_freg = cfg->rs->next_vfreg;
6772 bb->last_ins->next = NULL;
6774 mono_mempool_empty (cfg->state_pool);
6776 mono_mempool_destroy (cfg->state_pool);
6780 mono_codegen (MonoCompile *cfg)
6782 MonoJumpInfo *patch_info;
6784 int i, max_epilog_size;
6787 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6788 cfg->spill_count = 0;
6789 /* we reuse dfn here */
6790 /* bb->dfn = bb_count++; */
6791 mono_arch_local_regalloc (cfg, bb);
6794 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
6795 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
6797 code = mono_arch_emit_prolog (cfg);
6799 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6800 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
6802 cfg->code_len = code - cfg->native_code;
6803 cfg->prolog_end = cfg->code_len;
6805 mono_debug_open_method (cfg);
6807 /* emit code all basic blocks */
6808 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6809 bb->native_offset = cfg->code_len;
6810 mono_arch_output_basic_block (cfg, bb);
6812 cfg->bb_exit->native_offset = cfg->code_len;
6814 code = cfg->native_code + cfg->code_len;
6816 max_epilog_size = mono_arch_max_epilog_size (cfg);
6818 /* we always allocate code in cfg->domain->code_mp to increase locality */
6819 cfg->code_size = cfg->code_len + max_epilog_size;
6820 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
6821 code = mono_mempool_alloc (cfg->domain->code_mp, cfg->code_size);
6822 memcpy (code, cfg->native_code, cfg->code_len);
6823 g_free (cfg->native_code);
6824 cfg->native_code = code;
6825 code = cfg->native_code + cfg->code_len;
6827 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
6829 cfg->epilog_begin = cfg->code_len;
6831 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6832 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
6834 cfg->code_len = code - cfg->native_code;
6836 mono_arch_emit_epilog (cfg);
6838 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
6839 switch (patch_info->type) {
6840 case MONO_PATCH_INFO_ABS: {
6841 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
6843 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
6844 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
6845 patch_info->data.name = info->name;
6848 MonoVTable *vtable = mono_find_class_init_trampoline_by_addr (patch_info->data.target);
6850 patch_info->type = MONO_PATCH_INFO_CLASS_INIT;
6851 patch_info->data.klass = vtable->klass;
6856 case MONO_PATCH_INFO_SWITCH: {
6857 gpointer *table = mono_mempool_alloc (cfg->domain->code_mp, sizeof (gpointer) * patch_info->table_size);
6858 patch_info->ip.i = patch_info->ip.label->inst_c0;
6859 for (i = 0; i < patch_info->table_size; i++) {
6860 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
6862 patch_info->data.target = table;
6871 if (cfg->verbose_level > 0)
6872 g_print ("Method %s emitted at %p to %p [%s]\n",
6873 mono_method_full_name (cfg->method, TRUE),
6874 cfg->native_code, cfg->native_code + cfg->code_len, cfg->domain->friendly_name);
6876 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info);
6878 mono_arch_flush_icache (cfg->native_code, cfg->code_len);
6880 mono_debug_close_method (cfg);
6884 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
6889 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
6890 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
6892 if (cp->opcode == OP_ICONST) {
6893 if (cfg->opt & MONO_OPT_CONSPROP) {
6894 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
6898 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
6899 if (cfg->opt & MONO_OPT_COPYPROP) {
6900 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
6906 arity = mono_burg_arity [tree->opcode];
6909 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
6910 if (cfg->opt & MONO_OPT_CFOLD)
6911 mono_constant_fold_inst (tree, NULL);
6912 /* The opcode may have changed */
6913 if (mono_burg_arity [tree->opcode] > 1) {
6914 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
6915 if (cfg->opt & MONO_OPT_CFOLD)
6916 mono_constant_fold_inst (tree, NULL);
6918 mono_constant_fold_inst (tree, NULL);
6924 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
6928 switch (tree->opcode) {
6938 if (tree->ssa_op == MONO_SSA_NOP) {
6939 memset (acp, 0, sizeof (MonoInst *) * acp_size);
6956 case OP_VOIDCALL_REG:
6957 case OP_VOIDCALLVIRT:
6959 MonoCallInst *call = (MonoCallInst *)tree;
6960 MonoMethodSignature *sig = call->signature;
6961 int i, byref = FALSE;
6963 for (i = 0; i < sig->param_count; i++) {
6964 if (sig->params [i]->byref) {
6971 memset (acp, 0, sizeof (MonoInst *) * acp_size);
6979 arity = mono_burg_arity [tree->opcode];
6985 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
6988 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
6989 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
6992 g_assert_not_reached ();
6997 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
6999 MonoInst *tree = bb->code;
7005 for (; tree; tree = tree->next) {
7007 mono_cprop_copy_values (cfg, tree, acp);
7009 mono_cprop_invalidate_values (tree, acp, acp_size);
7011 if (tree->ssa_op == MONO_SSA_STORE &&
7012 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
7013 MonoInst *i1 = tree->inst_i1;
7015 acp [tree->inst_i0->inst_c0] = NULL;
7017 for (i = 0; i < acp_size; i++) {
7018 if (acp [i] && acp [i]->opcode != OP_ICONST &&
7019 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
7024 if (i1->opcode == OP_ICONST) {
7025 acp [tree->inst_i0->inst_c0] = i1;
7026 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
7028 if (i1->ssa_op == MONO_SSA_LOAD &&
7029 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
7030 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
7031 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
7032 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
7037 if (tree->opcode == CEE_BEQ) {
7038 g_assert (tree->inst_i0->opcode == OP_COMPARE);
7039 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
7040 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
7042 tree->opcode = CEE_BR;
7043 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
7044 tree->inst_target_bb = tree->inst_true_bb;
7046 tree->inst_target_bb = tree->inst_false_bb;
7055 mono_local_cprop (MonoCompile *cfg)
7060 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
7062 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7063 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
7064 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
7069 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, int parts)
7071 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
7072 guint8 *ip = (guint8 *)header->code;
7075 int dfn = 0, i, code_size_ratio;
7077 mono_jit_stats.methods_compiled++;
7078 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
7079 mono_profiler_method_jit (method);
7081 cfg = g_new0 (MonoCompile, 1);
7082 cfg->method = method;
7083 cfg->mempool = mono_mempool_new ();
7085 cfg->prof_options = mono_profiler_get_events ();
7086 cfg->bb_hash = g_hash_table_new (NULL, NULL);
7087 cfg->domain = domain;
7088 cfg->verbose_level = mini_verbose;
7089 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
7090 ((MonoMethodNormal *)method)->header->max_stack);
7092 if (cfg->verbose_level > 2)
7093 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
7096 * create MonoInst* which represents arguments and local variables
7098 mono_compile_create_vars (cfg);
7100 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
7101 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7102 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
7103 mono_destroy_compile (cfg);
7107 mono_jit_stats.basic_blocks += cfg->num_bblocks;
7108 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
7110 if (cfg->num_varinfo > 2000) {
7112 * we disable some optimizations if there are too many variables
7113 * because JIT time may become too expensive. The actual number needs
7114 * to be tweaked and eventually the non-linear algorithms should be fixed.
7116 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
7117 cfg->disable_ssa = TRUE;
7119 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
7121 /* Depth-first ordering on basic blocks */
7122 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
7124 if (cfg->opt & MONO_OPT_BRANCH)
7125 optimize_branches (cfg);
7127 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
7128 if (cfg->num_bblocks != dfn + 1) {
7131 cfg->num_bblocks = dfn + 1;
7133 if (!header->clauses) {
7134 /* remove unreachable code, because the code in them may be
7135 * inconsistent (access to dead variables for example) */
7136 for (bb = cfg->bb_entry; bb;) {
7137 MonoBasicBlock *bbn = bb->next_bb;
7139 if (bbn && bbn->region == -1 && !bbn->dfn) {
7140 if (cfg->verbose_level > 1)
7141 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
7142 bb->next_bb = bbn->next_bb;
7143 nullify_basic_block (bbn);
7151 if (cfg->opt & MONO_OPT_LOOP) {
7152 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
7153 mono_compute_natural_loops (cfg);
7156 /* after method_to_ir */
7160 //#define DEBUGSSA "logic_run"
7161 #define DEBUGSSA_CLASS "Tests"
7164 if (!header->num_clauses && !cfg->disable_ssa) {
7165 mono_local_cprop (cfg);
7166 mono_ssa_compute (cfg);
7170 /* fixme: add all optimizations which requires SSA */
7171 if (cfg->opt & (MONO_OPT_DEADCE)) {
7172 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
7173 mono_local_cprop (cfg);
7174 mono_ssa_compute (cfg);
7176 if (cfg->verbose_level >= 2) {
7183 /* after SSA translation */
7187 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
7188 if (cfg->comp_done & MONO_COMP_SSA) {
7189 mono_ssa_cprop (cfg);
7191 mono_local_cprop (cfg);
7195 if (cfg->comp_done & MONO_COMP_SSA) {
7196 mono_ssa_deadce (cfg);
7198 //mono_ssa_strength_reduction (cfg);
7200 mono_ssa_remove (cfg);
7202 if (cfg->opt & MONO_OPT_BRANCH)
7203 optimize_branches (cfg);
7206 /* after SSA removal */
7210 decompose_pass (cfg);
7212 /* FIXME: disabled with exception clauses: bug #42136 */
7213 if ((!header->num_clauses) && (cfg->opt & MONO_OPT_LINEARS)) {
7216 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
7217 cfg->comp_done &= ~MONO_COMP_LIVENESS;
7218 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
7219 mono_analyze_liveness (cfg);
7221 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
7222 regs = mono_arch_get_global_int_regs (cfg);
7223 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
7227 //mono_print_code (cfg);
7231 /* variables are allocated after decompose, since decompose could create temps */
7232 mono_arch_allocate_vars (cfg);
7234 if (cfg->opt & MONO_OPT_CFOLD)
7235 mono_constant_fold (cfg);
7237 mini_select_instructions (cfg);
7240 if (cfg->verbose_level >= 2) {
7241 char *id = mono_method_full_name (cfg->method, FALSE);
7242 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
7246 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
7248 jinfo = g_new0 (MonoJitInfo, 1);
7249 jinfo->method = method;
7250 jinfo->code_start = cfg->native_code;
7251 jinfo->code_size = cfg->code_len;
7252 jinfo->used_regs = cfg->used_int_regs;
7253 jinfo->domain_neutral = (cfg->opt & MONO_OPT_SHARED) != 0;
7255 if (header->num_clauses) {
7258 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
7259 jinfo->num_clauses = header->num_clauses;
7260 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
7261 sizeof (MonoJitExceptionInfo) * header->num_clauses);
7263 for (i = 0; i < header->num_clauses; i++) {
7264 MonoExceptionClause *ec = &header->clauses [i];
7265 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
7266 MonoBasicBlock *tblock;
7268 ei->flags = ec->flags;
7270 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7271 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->token_or_filter);
7273 ei->data.filter = cfg->native_code + tblock->native_offset;
7275 ei->data.token = ec->token_or_filter;
7278 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
7280 ei->try_start = cfg->native_code + tblock->native_offset;
7281 g_assert (tblock->native_offset);
7282 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
7284 ei->try_end = cfg->native_code + tblock->native_offset;
7285 g_assert (tblock->native_offset);
7286 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
7288 ei->handler_start = cfg->native_code + tblock->native_offset;
7292 cfg->jit_info = jinfo;
7294 mono_jit_info_table_add (cfg->domain, jinfo);
7296 /* collect statistics */
7297 mono_jit_stats.allocated_code_size += cfg->code_len;
7298 code_size_ratio = cfg->code_len;
7299 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
7300 mono_jit_stats.biggest_method_size = code_size_ratio;
7301 mono_jit_stats.biggest_method = method;
7303 code_size_ratio = (code_size_ratio * 100) / ((MonoMethodNormal *)method)->header->code_size;
7304 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
7305 mono_jit_stats.max_code_size_ratio = code_size_ratio;
7306 mono_jit_stats.max_ratio_method = method;
7308 mono_jit_stats.native_code_size += cfg->code_len;
7310 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7311 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
7317 mono_jit_compile_method_inner (MonoMethod *method)
7319 /* FIXME: later copy the code from mono */
7320 MonoDomain *target_domain, *domain = mono_domain_get ();
7322 GHashTable *jit_code_hash;
7328 if (opt & MONO_OPT_SHARED)
7329 target_domain = mono_root_domain;
7331 target_domain = domain;
7333 jit_code_hash = target_domain->jit_code_hash;
7335 #ifdef MONO_USE_AOT_COMPILER
7336 if (!mono_compile_aot && (opt & MONO_OPT_AOT)) {
7339 mono_domain_lock (domain);
7341 mono_class_init (method->klass);
7342 if ((info = mono_aot_get_method (domain, method))) {
7344 g_hash_table_insert (domain->jit_code_hash, method, info);
7346 mono_domain_unlock (domain);
7348 /* make sure runtime_init is called */
7349 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
7351 return info->code_start;
7354 mono_domain_unlock (domain);
7358 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
7359 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
7362 if (!method->addr) {
7363 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
7364 method->addr = mono_lookup_internal_call (method);
7366 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
7367 mono_lookup_pinvoke_call (method);
7369 #ifdef MONO_USE_EXC_TABLES
7370 if (mono_method_blittable (method)) {
7371 return method->addr;
7374 nm = mono_marshal_get_native_wrapper (method);
7375 return mono_compile_method (nm);
7377 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
7378 //mono_debug_add_wrapper (method, nm);
7379 #ifdef MONO_USE_EXC_TABLES
7382 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
7383 const char *name = method->name;
7386 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
7387 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
7388 /* FIXME: uhm, we need a wrapper to handle exceptions? */
7389 return (gpointer)mono_delegate_ctor;
7390 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
7391 nm = mono_marshal_get_delegate_invoke (method);
7392 return mono_jit_compile_method (nm);
7393 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
7394 nm = mono_marshal_get_delegate_begin_invoke (method);
7395 return mono_jit_compile_method (nm);
7396 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
7397 nm = mono_marshal_get_delegate_end_invoke (method);
7398 return mono_jit_compile_method (nm);
7404 cfg = mini_method_compile (method, opt, target_domain, 0);
7405 code = cfg->native_code;
7407 g_hash_table_insert (jit_code_hash, method, cfg->jit_info);
7409 mono_destroy_compile (cfg);
7411 if (target_domain->jump_target_hash) {
7412 MonoJumpInfo patch_info;
7414 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
7416 patch_info.next = NULL;
7417 patch_info.ip.i = 0;
7418 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
7419 patch_info.data.method = method;
7420 g_hash_table_remove (target_domain->jump_target_hash, method);
7422 for (tmp = list; tmp; tmp = tmp->next)
7423 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info);
7424 g_slist_free (list);
7426 /* make sure runtime_init is called */
7427 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
7433 mono_jit_compile_method (MonoMethod *method)
7435 /* FIXME: later copy the code from mono */
7436 MonoDomain *target_domain, *domain = mono_domain_get ();
7440 if (default_opt & MONO_OPT_SHARED)
7441 target_domain = mono_root_domain;
7443 target_domain = domain;
7445 mono_domain_lock (target_domain);
7447 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
7448 /* We can't use a domain specific method in another domain */
7449 if (! ((domain != target_domain) && !info->domain_neutral)) {
7450 mono_domain_unlock (target_domain);
7451 mono_jit_stats.methods_lookups++;
7452 return info->code_start;
7456 code = mono_jit_compile_method_inner (method);
7458 mono_domain_unlock (target_domain);
7464 * mono_jit_runtime_invoke:
7465 * @method: the method to invoke
7466 * @obj: this pointer
7467 * @params: array of parameter values.
7468 * @exc: used to catch exceptions objects
7471 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
7474 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc);
7476 if (obj == NULL && !(method->flags & METHOD_ATTRIBUTE_STATIC) && !method->string_ctor && (method->wrapper_type == 0)) {
7477 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
7481 invoke = mono_marshal_get_runtime_invoke (method);
7482 runtime_invoke = mono_jit_compile_method (invoke);
7483 return runtime_invoke (obj, params, exc);
7486 #ifdef PLATFORM_WIN32
7487 #define GET_CONTEXT \
7488 struct sigcontext *ctx = (struct sigcontext*)_dummy;
7490 #define GET_CONTEXT \
7491 void **_p = (void **)&_dummy; \
7492 struct sigcontext *ctx = (struct sigcontext *)++_p;
7496 sigfpe_signal_handler (int _dummy)
7501 exc = mono_get_exception_divide_by_zero ();
7503 mono_arch_handle_exception (ctx, exc, FALSE);
7507 sigill_signal_handler (int _dummy)
7511 exc = mono_get_exception_execution_engine ("SIGILL");
7513 mono_arch_handle_exception (ctx, exc, FALSE);
7517 sigsegv_signal_handler (int _dummy)
7522 exc = mono_get_exception_null_reference ();
7524 mono_arch_handle_exception (ctx, exc, FALSE);
7528 sigusr1_signal_handler (int _dummy)
7533 thread = mono_thread_current ();
7535 thread->abort_exc = mono_get_exception_thread_abort ();
7537 mono_arch_handle_exception (ctx, thread->abort_exc, FALSE);
7541 sigquit_signal_handler (int _dummy)
7546 exc = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
7548 mono_arch_handle_exception (ctx, exc, FALSE);
7552 sigint_signal_handler (int _dummy)
7557 exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
7559 mono_arch_handle_exception (ctx, exc, FALSE);
7563 mono_runtime_install_handlers (void)
7565 gboolean skip_sigabort = FALSE;
7566 #ifndef PLATFORM_WIN32
7567 struct sigaction sa;
7570 #ifdef PLATFORM_WIN32
7572 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
7573 win32_seh_set_handler(SIGILL, sigill_signal_handler);
7574 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
7575 if (getenv ("MONO_DEBUG"))
7576 win32_seh_set_handler(SIGINT, sigint_signal_handler);
7577 #else /* !PLATFORM_WIN32 */
7579 /* libpthreads has its own implementation of sigaction(),
7580 * but it seems to work well with our current exception
7581 * handlers. If not we must call syscall directly instead
7584 if (getenv ("MONO_DEBUG")) {
7586 sa.sa_handler = sigint_signal_handler;
7587 sigemptyset (&sa.sa_mask);
7589 //g_assert (syscall (SYS_sigaction, SIGINT, &sa, NULL) != -1);
7590 g_assert (sigaction (SIGINT, &sa, NULL) != -1);
7594 sa.sa_handler = sigfpe_signal_handler;
7595 sigemptyset (&sa.sa_mask);
7597 //g_assert (syscall (SYS_sigaction, SIGFPE, &sa, NULL) != -1);
7598 g_assert (sigaction (SIGFPE, &sa, NULL) != -1);
7601 sa.sa_handler = sigquit_signal_handler;
7602 sigemptyset (&sa.sa_mask);
7604 g_assert (sigaction (SIGQUIT, &sa, NULL) != -1);
7607 sa.sa_handler = sigill_signal_handler;
7608 sigemptyset (&sa.sa_mask);
7610 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
7611 g_assert (sigaction (SIGILL, &sa, NULL) != -1);
7613 /* catch thread abort signal */
7614 sa.sa_handler = sigusr1_signal_handler;
7615 sigemptyset (&sa.sa_mask);
7617 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
7619 #ifdef HAVE_VALGRIND_MEMCHECK_H
7620 if (RUNNING_ON_VALGRIND)
7621 /* valgrind 20030725 and earlier aborts on this call so we skip it */
7622 skip_sigabort = TRUE;
7626 g_assert (sigaction (mono_thread_get_abort_signal (), &sa, NULL) != -1);
7630 sa.sa_handler = sigsegv_signal_handler;
7631 sigemptyset (&sa.sa_mask);
7633 //g_assert (syscall (SYS_sigaction, SIGSEGV, &sa, NULL) != -1);
7634 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
7636 #endif /* PLATFORM_WIN32 */
7639 /* mono_jit_create_remoting_trampoline:
7640 * @method: pointer to the method info
7642 * Creates a trampoline which calls the remoting functions. This
7643 * is used in the vtable of transparent proxies.
7645 * Returns: a pointer to the newly created code
7648 mono_jit_create_remoting_trampoline (MonoMethod *method)
7651 guint8 *addr = NULL;
7653 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
7654 (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
7655 nm = mono_marshal_get_remoting_invoke (method);
7656 addr = mono_compile_method (nm);
7658 addr = mono_compile_method (method);
7664 mini_init (const char *filename)
7668 mono_arch_cpu_init ();
7670 g_thread_init (NULL);
7672 mono_jit_tls_id = TlsAlloc ();
7673 setup_jit_tls_data ((gpointer)-1, mono_thread_abort);
7675 InitializeCriticalSection (&class_init_hash_mutex);
7679 if (default_opt & MONO_OPT_AOT)
7682 mono_runtime_install_handlers ();
7683 mono_threads_install_cleanup (mini_thread_cleanup);
7685 #define JIT_TRAMPOLINES_WORK
7686 #ifdef JIT_TRAMPOLINES_WORK
7687 mono_install_compile_method (mono_jit_compile_method);
7688 mono_install_trampoline (mono_arch_create_jit_trampoline);
7689 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
7691 #define JIT_INVOKE_WORKS
7692 #ifdef JIT_INVOKE_WORKS
7693 mono_install_runtime_invoke (mono_jit_runtime_invoke);
7694 mono_install_handler (mono_arch_get_throw_exception ());
7696 mono_install_stack_walk (mono_jit_walk_stack);
7697 mono_install_get_config_dir ();
7699 domain = mono_init (filename);
7702 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
7703 ves_icall_get_frame_info);
7704 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
7705 ves_icall_get_trace);
7706 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
7707 mono_runtime_install_handlers);
7710 create_helper_signature ();
7712 #define JIT_CALLS_WORK
7713 #ifdef JIT_CALLS_WORK
7714 /* Needs to be called here since register_jit_icall depends on it */
7715 mono_marshal_init ();
7717 mono_arch_register_lowlevel_calls ();
7718 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
7719 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
7721 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
7722 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
7724 /* fixme: we cant hanlde vararg methods this way, because the signature is not constant */
7725 //mono_register_jit_icall (ves_array_element_address, "ves_array_element_address", NULL);
7726 //mono_register_jit_icall (mono_array_new_va, "mono_array_new_va", NULL);
7728 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
7729 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
7730 helper_sig_void_ptr, TRUE);
7731 mono_register_jit_icall (mono_thread_get_pending_exception, "mono_thread_get_pending_exception", helper_sig_obj_void, FALSE);
7734 * NOTE, NOTE, NOTE, NOTE:
7735 * when adding emulation for some opcodes, remember to also add a dummy
7736 * rule to the burg files, because we need the arity information to be correct.
7738 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult, TRUE);
7739 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un, FALSE);
7740 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf, FALSE);
7741 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv, FALSE);
7742 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un, FALSE);
7743 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem, FALSE);
7744 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un, FALSE);
7746 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl, TRUE);
7747 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr, TRUE);
7748 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un, TRUE);
7750 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8, FALSE);
7751 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4, FALSE);
7752 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8, TRUE);
7753 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8, TRUE);
7755 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
7756 mono_register_opcode_emulation (OP_FCONV_TO_I8, "__emul_fconv_to_i8", helper_sig_long_double, mono_fconv_i8, FALSE);
7758 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
7759 mono_register_opcode_emulation (OP_LCONV_TO_R8, "__emul_lconv_to_r8", helper_sig_double_long, mono_lconv_to_r8, FALSE);
7761 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
7762 mono_register_opcode_emulation (OP_LCONV_TO_R4, "__emul_lconv_to_r4", helper_sig_float_long, mono_lconv_to_r4, FALSE);
7764 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
7765 mono_register_opcode_emulation (OP_LCONV_TO_R_UN, "__emul_lconv_to_r8_un", helper_sig_double_long, mono_lconv_to_r8_un, FALSE);
7767 #ifdef MONO_ARCH_EMULATE_FREM
7768 mono_register_opcode_emulation (OP_FREM, "__emul_frem", helper_sig_double_double_double, fmod, FALSE);
7771 #if SIZEOF_VOID_P == 4
7772 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4, TRUE);
7774 #warning "fixme: add opcode emulation"
7777 /* other jit icalls */
7778 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
7779 helper_sig_ptr_ptr_ptr, FALSE);
7780 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
7781 mono_register_jit_icall (mono_get_special_static_data, "mono_get_special_static_data", helper_sig_ptr_int, FALSE);
7782 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
7783 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
7784 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
7785 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
7786 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
7787 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
7788 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
7789 mono_register_jit_icall (mono_object_new_fast, "mono_object_new_fast", helper_sig_object_new_specific, FALSE);
7790 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
7791 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
7792 mono_register_jit_icall (mono_string_to_utf16, "mono_string_to_utf16", helper_sig_ptr_obj, FALSE);
7793 mono_register_jit_icall (mono_string_from_utf16, "mono_string_from_utf16", helper_sig_obj_ptr, FALSE);
7794 mono_register_jit_icall (mono_string_new_wrapper, "mono_string_new_wrapper", helper_sig_obj_ptr, FALSE);
7795 mono_register_jit_icall (mono_string_to_utf8, "mono_string_to_utf8", helper_sig_ptr_obj, FALSE);
7796 mono_register_jit_icall (mono_string_to_bstr, "mono_string_to_bstr", helper_sig_ptr_obj, FALSE);
7797 mono_register_jit_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", helper_sig_ptr_obj, FALSE);
7798 mono_register_jit_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", helper_sig_ptr_obj, FALSE);
7799 mono_register_jit_icall (mono_array_to_savearray, "mono_array_to_savearray", helper_sig_ptr_obj, FALSE);
7800 mono_register_jit_icall (mono_array_to_lparray, "mono_array_to_lparray", helper_sig_ptr_obj, FALSE);
7801 mono_register_jit_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", helper_sig_ptr_obj, FALSE);
7802 mono_register_jit_icall (mono_marshal_string_array, "mono_marshal_string_array", helper_sig_ptr_obj, FALSE);
7803 mono_register_jit_icall (mono_string_utf8_to_builder, "mono_string_utf8_to_builder", helper_sig_void_ptr_ptr, FALSE);
7804 mono_register_jit_icall (mono_marshal_free_array, "mono_marshal_free_array", helper_sig_void_ptr_ptr, FALSE);
7805 mono_register_jit_icall (mono_string_to_byvalstr, "mono_string_to_byvalstr", helper_sig_void_ptr_ptr_ptr, FALSE);
7806 mono_register_jit_icall (mono_string_to_byvalwstr, "mono_string_to_byvalwstr", helper_sig_void_ptr_ptr_ptr, FALSE);
7807 mono_register_jit_icall (g_free, "g_free", helper_sig_void_ptr, FALSE);
7808 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
7809 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
7810 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
7813 #define JIT_RUNTIME_WORKS
7814 #ifdef JIT_RUNTIME_WORKS
7815 mono_runtime_install_cleanup ((MonoDomainFunc)mini_cleanup);
7816 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
7819 //mono_thread_attach (domain);
7823 MonoJitStats mono_jit_stats = {0};
7826 print_jit_stats (void)
7828 if (mono_jit_stats.enabled) {
7829 g_print ("Mono Jit statistics\n");
7830 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
7831 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
7832 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
7833 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
7834 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
7835 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
7836 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
7837 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
7838 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
7839 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
7840 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
7841 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
7842 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
7843 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
7844 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
7845 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
7846 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
7847 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
7849 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
7850 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
7851 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
7852 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
7853 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
7858 mini_cleanup (MonoDomain *domain)
7861 * mono_runtime_cleanup() and mono_domain_finalize () need to
7862 * be called early since they need the execution engine still
7863 * fully working (mono_domain_finalize may invoke managed finalizers
7864 * and mono_runtime_cleanup will wait for other threads to finish).
7866 mono_domain_finalize (domain, -1);
7868 mono_runtime_cleanup (domain);
7870 mono_profiler_shutdown ();
7872 mono_debug_cleanup ();
7874 #ifdef PLATFORM_WIN32
7875 win32_seh_cleanup();
7878 mono_domain_free (domain, TRUE);
7884 mono_set_defaults (int verbose_level, guint32 opts)
7886 mini_verbose = verbose_level;
7891 mono_precompile_assembly (MonoAssembly *ass, void *user_data)
7893 MonoImage *image = ass->image;
7897 if (mini_verbose > 0)
7898 printf ("PRECOMPILE: %s.\n", ass->image->name);
7900 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
7901 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
7902 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
7904 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
7908 if (mini_verbose > 1) {
7909 char * desc = mono_method_full_name (method, TRUE);
7910 g_print ("Compiling %d %s\n", count, desc);
7913 mono_compile_method (method);
7917 void mono_precompile_assemblies ()
7919 mono_assembly_foreach ((GFunc)mono_precompile_assembly, NULL);