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
2462 #define TYPE_PARAM_TO_TYPE(num) (method->klass->generic_inst->data.generic_inst->type_argv [(num)])
2463 #define TYPE_PARAM_TO_CLASS(num) (mono_class_from_mono_type (TYPE_PARAM_TO_TYPE ((num))))
2465 #define MTYPE_PARAM_TO_TYPE(num) (((MonoMethodNormal *) method)->header->geninst->type_argv [(num)])
2466 #define MTYPE_PARAM_TO_CLASS(num) (mono_class_from_mono_type (MTYPE_PARAM_TO_TYPE ((num))))
2469 /* offset from br.s -> br like opcodes */
2470 #define BIG_BRANCH_OFFSET 13
2473 get_basic_blocks (MonoCompile *cfg, GHashTable *bbhash, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
2475 unsigned char *ip = start;
2476 unsigned char *target;
2479 MonoBasicBlock *bblock;
2480 const MonoOpcode *opcode;
2483 cli_addr = ip - start;
2484 i = mono_opcode_value ((const guint8 **)&ip);
2485 opcode = &mono_opcodes [i];
2486 switch (opcode->argument) {
2487 case MonoInlineNone:
2490 case MonoInlineString:
2491 case MonoInlineType:
2492 case MonoInlineField:
2493 case MonoInlineMethod:
2496 case MonoShortInlineR:
2503 case MonoShortInlineVar:
2504 case MonoShortInlineI:
2507 case MonoShortInlineBrTarget:
2508 target = start + cli_addr + 2 + (signed char)ip [1];
2509 GET_BBLOCK (cfg, bbhash, bblock, target);
2512 case MonoInlineBrTarget:
2513 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
2514 GET_BBLOCK (cfg, bbhash, bblock, target);
2517 case MonoInlineSwitch: {
2518 guint32 n = read32 (ip + 1);
2521 cli_addr += 5 + 4 * n;
2522 target = start + cli_addr;
2523 GET_BBLOCK (cfg, bbhash, bblock, target);
2525 for (j = 0; j < n; ++j) {
2526 target = start + cli_addr + (gint32)read32 (ip);
2527 GET_BBLOCK (cfg, bbhash, bblock, target);
2537 g_assert_not_reached ();
2546 static MonoClassField *
2547 get_generic_field_inst (MonoClassField *field, MonoClass *klass, MonoClass **retclass)
2550 for (i = 0; i < field->parent->field.count; ++i) {
2551 if (field == &field->parent->fields [i]) {
2553 return &klass->fields [i];
2559 static MonoClassField *
2560 inflate_generic_field (MonoClassField *field, MonoClass *klass, MonoClass **retclass)
2562 MonoGenericInst *ginst;
2563 MonoClassField *res;
2565 res = g_new0 (MonoClassField, 1);
2567 ginst = klass->generic_inst->data.generic_inst;
2568 res->type = mono_class_inflate_generic_type (field->type, ginst);
2573 * mono_method_to_ir: translates IL into basic blocks containing trees
2576 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
2577 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
2578 guint inline_offset, gboolean is_virtual_call)
2580 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
2581 MonoInst *ins, **sp, **stack_start;
2582 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
2584 MonoMethod *cmethod;
2585 MonoInst **arg_array;
2586 MonoMethodHeader *header;
2588 guint32 token, ins_flag;
2590 unsigned char *ip, *end, *target, *err_pos;
2591 static double r8_0 = 0.0;
2592 MonoMethodSignature *sig;
2593 MonoType **param_types;
2594 GList *bb_recheck = NULL, *tmp;
2595 int i, n, start_new_bblock, align;
2596 int num_calls = 0, inline_costs = 0;
2597 int *filter_lengths = NULL;
2598 int breakpoint_id = 0;
2601 image = method->klass->image;
2602 header = ((MonoMethodNormal *)method)->header;
2603 sig = method->signature;
2604 ip = (unsigned char*)header->code;
2605 end = ip + header->code_size;
2606 mono_jit_stats.cil_code_size += header->code_size;
2608 if (cfg->method == method) {
2610 bbhash = cfg->bb_hash;
2612 real_offset = inline_offset;
2613 bbhash = g_hash_table_new (g_direct_hash, NULL);
2616 if (cfg->verbose_level > 2)
2617 g_print ("method to IR %s\n", mono_method_full_name (method, TRUE));
2619 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
2620 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
2622 dont_inline = g_list_prepend (dont_inline, method);
2623 if (cfg->method == method) {
2626 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
2627 start_bblock->cil_code = NULL;
2628 start_bblock->cil_length = 0;
2629 start_bblock->block_num = cfg->num_bblocks++;
2632 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
2633 end_bblock->cil_code = NULL;
2634 end_bblock->cil_length = 0;
2635 end_bblock->block_num = cfg->num_bblocks++;
2636 g_assert (cfg->num_bblocks == 2);
2638 arg_array = alloca (sizeof (MonoInst *) * (sig->hasthis + sig->param_count));
2639 for (i = sig->hasthis + sig->param_count - 1; i >= 0; i--)
2640 arg_array [i] = cfg->varinfo [i];
2642 if (header->num_clauses) {
2643 int size = sizeof (int) * header->num_clauses;
2644 filter_lengths = alloca (size);
2645 memset (filter_lengths, 0, size);
2647 cfg->spvars = g_hash_table_new (NULL, NULL);
2649 /* handle exception clauses */
2650 for (i = 0; i < header->num_clauses; ++i) {
2651 //unsigned char *p = ip;
2652 MonoExceptionClause *clause = &header->clauses [i];
2653 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->try_offset);
2654 tblock->real_offset = clause->try_offset;
2655 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
2656 tblock->real_offset = clause->handler_offset;
2658 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
2659 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2660 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2661 MONO_ADD_INS (tblock, ins);
2664 /*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);
2666 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
2668 /* catch and filter blocks get the exception object on the stack */
2669 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
2670 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2671 /* mostly like handle_stack_args (), but just sets the input args */
2672 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
2674 cfg->exvar = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
2675 /* prevent it from being register allocated */
2676 cfg->exvar->flags |= MONO_INST_INDIRECT;
2678 tblock->in_scount = 1;
2679 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2680 tblock->in_stack [0] = cfg->exvar;
2682 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2683 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->token_or_filter);
2684 tblock->real_offset = clause->token_or_filter;
2685 tblock->in_scount = 1;
2686 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2687 tblock->in_stack [0] = cfg->exvar;
2688 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2689 MONO_ADD_INS (tblock, ins);
2695 arg_array = alloca (sizeof (MonoInst *) * (sig->hasthis + sig->param_count));
2696 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
2699 /* FIRST CODE BLOCK */
2700 bblock = NEW_BBLOCK (cfg);
2701 bblock->cil_code = ip;
2703 ADD_BBLOCK (cfg, bbhash, bblock);
2705 if (cfg->method == method) {
2706 breakpoint_id = mono_debugger_method_has_breakpoint (method);
2707 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
2708 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2709 MONO_ADD_INS (bblock, ins);
2713 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED))) || mono_compile_aot) {
2714 /* we use a separate basic block for the initialization code */
2715 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
2716 init_localsbb->real_offset = real_offset;
2717 start_bblock->next_bb = init_localsbb;
2718 init_localsbb->next_bb = bblock;
2719 link_bblock (cfg, start_bblock, init_localsbb);
2720 link_bblock (cfg, init_localsbb, bblock);
2721 init_localsbb->block_num = cfg->num_bblocks++;
2723 start_bblock->next_bb = bblock;
2724 link_bblock (cfg, start_bblock, bblock);
2727 if (get_basic_blocks (cfg, bbhash, header, real_offset, ip, end, &err_pos)) {
2732 mono_debug_init_method (cfg, bblock, breakpoint_id);
2734 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * (sig->hasthis + sig->param_count));
2736 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
2737 for (n = 0; n < sig->param_count; ++n)
2738 param_types [n + sig->hasthis] = sig->params [n];
2740 /* do this somewhere outside - not here */
2741 NEW_ICONST (cfg, zero_int32, 0);
2742 NEW_ICONST (cfg, zero_int64, 0);
2743 zero_int64->type = STACK_I8;
2744 NEW_PCONST (cfg, zero_ptr, 0);
2745 NEW_PCONST (cfg, zero_obj, 0);
2746 zero_obj->type = STACK_OBJ;
2748 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
2749 zero_r8->type = STACK_R8;
2750 zero_r8->inst_p0 = &r8_0;
2752 /* add a check for this != NULL to inlined methods */
2753 if (is_virtual_call) {
2754 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
2755 NEW_ARGLOAD (cfg, ins->inst_left, 0);
2757 MONO_ADD_INS (bblock, ins);
2760 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
2761 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
2764 start_new_bblock = 0;
2767 if (cfg->method == method)
2768 real_offset = ip - header->code;
2770 real_offset = inline_offset;
2772 if (start_new_bblock) {
2773 bblock->cil_length = ip - bblock->cil_code;
2774 if (start_new_bblock == 2) {
2775 g_assert (ip == tblock->cil_code);
2777 GET_BBLOCK (cfg, bbhash, tblock, ip);
2779 bblock->next_bb = tblock;
2781 start_new_bblock = 0;
2782 for (i = 0; i < bblock->in_scount; ++i) {
2783 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2787 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
2788 link_bblock (cfg, bblock, tblock);
2789 if (sp != stack_start) {
2790 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2793 bblock->next_bb = tblock;
2795 for (i = 0; i < bblock->in_scount; ++i) {
2796 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2802 if (cfg->coverage_info) {
2803 MonoInst *store, *one;
2804 guint32 cil_offset = ip - header->code;
2805 cfg->coverage_info->data [cil_offset].cil_code = ip;
2807 /* TODO: Use an increment here */
2808 NEW_ICONST (cfg, one, 1);
2811 NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
2814 MONO_INST_NEW (cfg, store, CEE_STIND_I);
2815 store->cil_code = ip;
2816 store->inst_left = ins;
2817 store->inst_right = one;
2819 MONO_ADD_INS (bblock, store);
2822 if (cfg->verbose_level > 3)
2823 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, sp-stack_start, mono_disasm_code_one (NULL, method, ip, NULL));
2830 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2831 ins->cil_code = ip++;
2832 MONO_ADD_INS (bblock, ins);
2838 CHECK_STACK_OVF (1);
2839 n = (*ip)-CEE_LDARG_0;
2840 NEW_ARGLOAD (cfg, ins, n);
2841 ins->cil_code = ip++;
2848 CHECK_STACK_OVF (1);
2849 n = (*ip)-CEE_LDLOC_0;
2850 NEW_LOCLOAD (cfg, ins, n);
2851 ins->cil_code = ip++;
2859 n = (*ip)-CEE_STLOC_0;
2861 handle_loaded_temps (cfg, bblock, stack_start, sp);
2862 NEW_LOCSTORE (cfg, ins, n, *sp);
2864 if (ins->opcode == CEE_STOBJ) {
2865 NEW_LOCLOADA (cfg, ins, n);
2866 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2868 MONO_ADD_INS (bblock, ins);
2873 CHECK_STACK_OVF (1);
2874 NEW_ARGLOAD (cfg, ins, ip [1]);
2880 CHECK_STACK_OVF (1);
2881 NEW_ARGLOADA (cfg, ins, ip [1]);
2889 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
2890 handle_loaded_temps (cfg, bblock, stack_start, sp);
2892 if (ins->opcode == CEE_STOBJ) {
2893 NEW_ARGLOADA (cfg, ins, ip [1]);
2894 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2896 MONO_ADD_INS (bblock, ins);
2900 CHECK_STACK_OVF (1);
2901 NEW_LOCLOAD (cfg, ins, ip [1]);
2907 CHECK_STACK_OVF (1);
2908 NEW_LOCLOADA (cfg, ins, ip [1]);
2916 handle_loaded_temps (cfg, bblock, stack_start, sp);
2917 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
2919 if (ins->opcode == CEE_STOBJ) {
2920 NEW_LOCLOADA (cfg, ins, ip [1]);
2921 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2923 MONO_ADD_INS (bblock, ins);
2928 CHECK_STACK_OVF (1);
2929 NEW_PCONST (cfg, ins, NULL);
2931 ins->type = STACK_OBJ;
2936 CHECK_STACK_OVF (1);
2937 NEW_ICONST (cfg, ins, -1);
2951 CHECK_STACK_OVF (1);
2952 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
2958 CHECK_STACK_OVF (1);
2960 NEW_ICONST (cfg, ins, *((signed char*)ip));
2966 CHECK_STACK_OVF (1);
2967 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
2973 CHECK_STACK_OVF (1);
2974 MONO_INST_NEW (cfg, ins, OP_I8CONST);
2976 ins->type = STACK_I8;
2978 ins->inst_l = (gint64)read64 (ip);
2983 float *f = g_malloc (sizeof (float));
2984 CHECK_STACK_OVF (1);
2985 MONO_INST_NEW (cfg, ins, OP_R4CONST);
2986 ins->type = STACK_R8;
2995 double *d = g_malloc (sizeof (double));
2996 CHECK_STACK_OVF (1);
2997 MONO_INST_NEW (cfg, ins, OP_R8CONST);
2998 ins->type = STACK_R8;
3007 MonoInst *temp, *store;
3009 CHECK_STACK_OVF (1);
3014 * small optimization: if the loaded value was from a local already,
3015 * just load it twice.
3017 if (ins->ssa_op == MONO_SSA_LOAD &&
3018 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
3020 MONO_INST_NEW (cfg, temp, 0);
3022 temp->cil_code = ip;
3025 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3026 temp->cil_code = ip;
3027 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3028 store->cil_code = ip;
3029 MONO_ADD_INS (bblock, store);
3030 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3033 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3043 MONO_INST_NEW (cfg, ins, CEE_POP);
3044 MONO_ADD_INS (bblock, ins);
3045 ins->cil_code = ip++;
3050 if (stack_start != sp)
3052 MONO_INST_NEW (cfg, ins, CEE_JMP);
3053 token = read32 (ip + 1);
3054 /* FIXME: check the signature matches */
3055 cmethod = mono_get_method (image, token, NULL);
3056 ins->inst_p0 = cmethod;
3057 MONO_ADD_INS (bblock, ins);
3059 start_new_bblock = 1;
3063 case CEE_CALLVIRT: {
3064 MonoInst *addr = NULL;
3065 MonoMethodSignature *fsig = NULL;
3066 int temp, array_rank = 0;
3067 int virtual = *ip == CEE_CALLVIRT;
3069 token = read32 (ip + 1);
3071 if (*ip == CEE_CALLI) {
3076 if (method->wrapper_type != MONO_WRAPPER_NONE)
3077 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
3079 fsig = mono_metadata_parse_signature (image, token);
3081 n = fsig->param_count + fsig->hasthis;
3083 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3084 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
3086 cmethod = mono_get_method (image, token, NULL);
3089 if (!cmethod->klass->inited)
3090 mono_class_init (cmethod->klass);
3092 if (cmethod->signature->pinvoke) {
3093 #ifdef MONO_USE_EXC_TABLES
3094 if (mono_method_blittable (cmethod)) {
3095 fsig = cmethod->signature;
3098 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
3099 fsig = wrapper->signature;
3100 #ifdef MONO_USE_EXC_TABLES
3104 fsig = mono_method_get_signature (cmethod, image, token);
3107 n = fsig->param_count + fsig->hasthis;
3109 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
3110 cmethod->klass->parent == mono_defaults.array_class) {
3111 array_rank = cmethod->klass->rank;
3114 if (cmethod->string_ctor)
3115 g_assert_not_reached ();
3121 //g_assert (!virtual || fsig->hasthis);
3125 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
3128 if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL) && (mono_metadata_signature_equal (method->signature, cmethod->signature))) {
3130 /* FIXME: This assumes the two methods has the same number and type of arguments */
3131 for (i = 0; i < n; ++i) {
3132 /* Check if argument is the same */
3133 NEW_ARGLOAD (cfg, ins, i);
3134 if ((ins->opcode == sp [i]->opcode) && (ins->inst_i0 == sp [i]->inst_i0))
3137 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3139 if (ins->opcode == CEE_STOBJ) {
3140 NEW_ARGLOADA (cfg, ins, i);
3141 handle_stobj (cfg, bblock, ins, sp [i], sp [i]->cil_code, ins->klass, FALSE, FALSE);
3144 MONO_ADD_INS (bblock, ins);
3146 MONO_INST_NEW (cfg, ins, CEE_JMP);
3148 ins->inst_p0 = cmethod;
3149 ins->inst_p1 = arg_array [0];
3150 MONO_ADD_INS (bblock, ins);
3151 start_new_bblock = 1;
3152 /* skip CEE_RET as well */
3157 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_opcode_for_method (cfg, cmethod, fsig, sp))) {
3160 if (MONO_TYPE_IS_VOID (fsig->ret)) {
3161 MONO_ADD_INS (bblock, ins);
3163 type_to_eval_stack_type (fsig->ret, ins);
3172 handle_loaded_temps (cfg, bblock, stack_start, sp);
3174 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3175 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
3176 mono_method_check_inlining (cfg, cmethod) &&
3177 !g_list_find (dont_inline, cmethod)) {
3179 MonoBasicBlock *ebblock;
3181 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3185 GET_BBLOCK (cfg, bbhash, bblock, ip);
3186 ebblock->next_bb = bblock;
3187 link_bblock (cfg, ebblock, bblock);
3189 if (!MONO_TYPE_IS_VOID (fsig->ret))
3192 /* indicates start of a new block, and triggers a load of all
3193 stack arguments at bb boundarie */
3196 inline_costs += costs;
3201 inline_costs += 10 * num_calls++;
3203 /* tail recursion elimination */
3204 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == method && ip [5] == CEE_RET) {
3205 gboolean has_vtargs = FALSE;
3208 /* keep it simple */
3209 for (i = fsig->param_count - 1; i >= 0; i--) {
3210 if (MONO_TYPE_ISSTRUCT (cmethod->signature->params [i]))
3215 for (i = 0; i < n; ++i) {
3216 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3218 MONO_ADD_INS (bblock, ins);
3220 MONO_INST_NEW (cfg, ins, CEE_BR);
3222 MONO_ADD_INS (bblock, ins);
3223 tblock = start_bblock->out_bb [0];
3224 link_bblock (cfg, bblock, tblock);
3225 ins->inst_target_bb = tblock;
3226 start_new_bblock = 1;
3229 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3230 /* just create a dummy - the value is never used */
3231 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
3232 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3240 if (*ip == CEE_CALLI) {
3242 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
3243 NEW_TEMPLOAD (cfg, *sp, temp);
3247 } else if (array_rank) {
3250 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
3251 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
3252 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
3254 if (ins->opcode == CEE_STOBJ) {
3255 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
3257 MONO_ADD_INS (bblock, ins);
3260 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
3261 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3262 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
3266 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
3267 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3270 g_assert_not_reached ();
3274 if (0 && CODE_IS_STLOC (ip + 5) && (!MONO_TYPE_ISSTRUCT (fsig->ret)) && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)) {
3275 /* no need to spill */
3276 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
3279 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) {
3280 NEW_TEMPLOAD (cfg, *sp, temp);
3290 if (cfg->method != method) {
3291 /* return from inlined methode */
3296 //g_assert (returnvar != -1);
3297 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
3298 store->cil_code = sp [0]->cil_code;
3299 if (store->opcode == CEE_STOBJ) {
3300 g_assert_not_reached ();
3301 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
3302 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
3304 MONO_ADD_INS (bblock, store);
3308 g_assert (!return_var);
3311 MONO_INST_NEW (cfg, ins, CEE_NOP);
3312 ins->opcode = mono_type_to_stind (method->signature->ret);
3313 if (ins->opcode == CEE_STOBJ) {
3314 NEW_RETLOADA (cfg, ins);
3315 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3317 ins->opcode = OP_SETRET;
3319 ins->inst_i0 = *sp;;
3320 ins->inst_i1 = NULL;
3321 MONO_ADD_INS (bblock, ins);
3325 if (sp != stack_start)
3327 MONO_INST_NEW (cfg, ins, CEE_BR);
3328 ins->cil_code = ip++;
3329 ins->inst_target_bb = end_bblock;
3330 MONO_ADD_INS (bblock, ins);
3331 link_bblock (cfg, bblock, end_bblock);
3332 start_new_bblock = 1;
3335 MONO_INST_NEW (cfg, ins, CEE_BR);
3336 ins->cil_code = ip++;
3337 MONO_ADD_INS (bblock, ins);
3338 target = ip + 1 + (signed char)(*ip);
3340 GET_BBLOCK (cfg, bbhash, tblock, target);
3341 link_bblock (cfg, bblock, tblock);
3342 CHECK_BBLOCK (target, ip, tblock);
3343 ins->inst_target_bb = tblock;
3344 if (sp != stack_start) {
3345 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3348 start_new_bblock = 1;
3354 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3355 ins->cil_code = ip++;
3356 target = ip + 1 + *(signed char*)ip;
3358 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
3359 if (sp != stack_start) {
3360 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3376 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3377 ins->cil_code = ip++;
3378 target = ip + 1 + *(signed char*)ip;
3381 if (sp != stack_start) {
3382 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3388 MONO_INST_NEW (cfg, ins, CEE_BR);
3389 ins->cil_code = ip++;
3390 MONO_ADD_INS (bblock, ins);
3391 target = ip + 4 + (gint32)read32(ip);
3393 GET_BBLOCK (cfg, bbhash, tblock, target);
3394 link_bblock (cfg, bblock, tblock);
3395 CHECK_BBLOCK (target, ip, tblock);
3396 ins->inst_target_bb = tblock;
3397 if (sp != stack_start) {
3398 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3401 start_new_bblock = 1;
3407 MONO_INST_NEW (cfg, ins, *ip);
3408 ins->cil_code = ip++;
3409 target = ip + 4 + (gint32)read32(ip);
3411 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
3412 if (sp != stack_start) {
3413 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3429 MONO_INST_NEW (cfg, ins, *ip);
3430 ins->cil_code = ip++;
3431 target = ip + 4 + (gint32)read32(ip);
3434 if (sp != stack_start) {
3435 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3442 n = read32 (ip + 1);
3443 MONO_INST_NEW (cfg, ins, *ip);
3445 ins->inst_left = *sp;
3446 if (ins->inst_left->type != STACK_I4) goto unverified;
3449 target = ip + n * sizeof (guint32);
3450 MONO_ADD_INS (bblock, ins);
3451 GET_BBLOCK (cfg, bbhash, tblock, target);
3452 link_bblock (cfg, bblock, tblock);
3453 ins->klass = GUINT_TO_POINTER (n);
3454 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
3455 ins->inst_many_bb [n] = tblock;
3457 for (i = 0; i < n; ++i) {
3458 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
3459 link_bblock (cfg, bblock, tblock);
3460 ins->inst_many_bb [i] = tblock;
3463 /* FIXME: handle stack args */
3478 MONO_INST_NEW (cfg, ins, *ip);
3483 ins->type = ldind_type [*ip - CEE_LDIND_I1];
3484 ins->flags |= ins_flag;
3496 MONO_INST_NEW (cfg, ins, *ip);
3497 ins->cil_code = ip++;
3499 handle_loaded_temps (cfg, bblock, stack_start, sp);
3500 MONO_ADD_INS (bblock, ins);
3501 ins->inst_i0 = sp [0];
3502 ins->inst_i1 = sp [1];
3503 ins->flags |= ins_flag;
3534 case CEE_CONV_OVF_I8:
3535 case CEE_CONV_OVF_U8:
3541 case CEE_CONV_OVF_I4:
3542 case CEE_CONV_OVF_I1:
3543 case CEE_CONV_OVF_I2:
3544 case CEE_CONV_OVF_I:
3545 case CEE_CONV_OVF_U:
3548 if (sp [-1]->type == STACK_R8) {
3549 ADD_UNOP (CEE_CONV_OVF_I8);
3557 case CEE_CONV_OVF_U1:
3558 case CEE_CONV_OVF_U2:
3559 case CEE_CONV_OVF_U4:
3562 if (sp [-1]->type == STACK_R8) {
3563 ADD_UNOP (CEE_CONV_OVF_U8);
3571 case CEE_CONV_OVF_I1_UN:
3572 case CEE_CONV_OVF_I2_UN:
3573 case CEE_CONV_OVF_I4_UN:
3574 case CEE_CONV_OVF_I8_UN:
3575 case CEE_CONV_OVF_U1_UN:
3576 case CEE_CONV_OVF_U2_UN:
3577 case CEE_CONV_OVF_U4_UN:
3578 case CEE_CONV_OVF_U8_UN:
3579 case CEE_CONV_OVF_I_UN:
3580 case CEE_CONV_OVF_U_UN:
3587 token = read32 (ip + 1);
3588 if (method->wrapper_type != MONO_WRAPPER_NONE)
3589 klass = mono_method_get_wrapper_data (method, token);
3591 klass = mono_class_get (image, token);
3593 mono_class_init (klass);
3594 if (klass->byval_arg.type == MONO_TYPE_VAR)
3595 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3596 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
3597 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3599 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3600 MonoInst *store, *load;
3601 MONO_INST_NEW (cfg, load, CEE_LDIND_REF);
3602 load->cil_code = ip;
3603 load->inst_i0 = sp [1];
3604 load->type = ldind_type [CEE_LDIND_REF];
3605 load->flags |= ins_flag;
3606 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
3607 store->cil_code = ip;
3608 handle_loaded_temps (cfg, bblock, stack_start, sp);
3609 MONO_ADD_INS (bblock, store);
3610 store->inst_i0 = sp [0];
3611 store->inst_i1 = load;
3612 store->flags |= ins_flag;
3614 n = mono_class_value_size (klass, NULL);
3615 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3617 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3618 copy->inst_left = sp [0];
3619 copy->inst_right = sp [1];
3620 copy->cil_code = ip;
3622 MONO_ADD_INS (bblock, copy);
3624 MonoInst *iargs [3];
3627 NEW_ICONST (cfg, iargs [2], n);
3628 iargs [2]->cil_code = ip;
3630 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3637 MonoInst *iargs [3];
3640 token = read32 (ip + 1);
3641 if (method->wrapper_type != MONO_WRAPPER_NONE)
3642 klass = mono_method_get_wrapper_data (method, token);
3644 klass = mono_class_get (image, token);
3646 mono_class_init (klass);
3647 if (klass->byval_arg.type == MONO_TYPE_VAR)
3648 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3649 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
3650 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3651 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3652 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3654 ins->inst_i0 = sp [0];
3655 ins->type = ldind_type [CEE_LDIND_REF];
3656 ins->flags |= ins_flag;
3662 n = mono_class_value_size (klass, NULL);
3663 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3664 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3665 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3667 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3668 copy->inst_left = iargs [0];
3669 copy->inst_right = *sp;
3670 copy->cil_code = ip;
3672 MONO_ADD_INS (bblock, copy);
3675 NEW_ICONST (cfg, iargs [2], n);
3676 iargs [2]->cil_code = ip;
3678 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3680 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3688 CHECK_STACK_OVF (1);
3689 n = read32 (ip + 1);
3691 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3693 MonoInst *iargs [1];
3695 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
3696 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
3697 NEW_TEMPLOAD (cfg, *sp, temp);
3701 if (cfg->opt & MONO_OPT_SHARED) {
3703 MonoInst *iargs [3];
3705 if (mono_compile_aot) {
3706 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n);
3709 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3710 NEW_IMAGECONST (cfg, iargs [1], image);
3711 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
3712 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
3713 NEW_TEMPLOAD (cfg, *sp, temp);
3714 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3716 if (mono_compile_aot)
3717 NEW_LDSTRCONST (cfg, ins, image, n);
3719 NEW_PCONST (cfg, ins, NULL);
3721 ins->type = STACK_OBJ;
3722 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3732 MonoInst *iargs [2];
3733 MonoMethodSignature *fsig;
3736 token = read32 (ip + 1);
3737 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3738 cmethod = mono_method_get_wrapper_data (method, token);
3740 cmethod = mono_get_method (image, token, NULL);
3741 fsig = mono_method_get_signature (cmethod, image, token);
3743 mono_class_init (cmethod->klass);
3745 n = fsig->param_count;
3748 /* move the args to allow room for 'this' in the first position */
3754 handle_loaded_temps (cfg, bblock, stack_start, sp);
3757 if (cmethod->klass->parent == mono_defaults.array_class) {
3758 NEW_METHODCONST (cfg, *sp, cmethod);
3759 temp = mono_emit_native_call (cfg, bblock, mono_array_new_va, fsig, sp, ip, FALSE);
3761 } else if (cmethod->string_ctor) {
3762 /* we simply pass a null pointer */
3763 NEW_PCONST (cfg, *sp, NULL);
3764 /* now call the string ctor */
3765 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
3767 if (cmethod->klass->valuetype) {
3768 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
3769 temp = iargs [0]->inst_c0;
3770 NEW_TEMPLOADA (cfg, *sp, temp);
3772 if (cfg->opt & MONO_OPT_SHARED) {
3773 NEW_DOMAINCONST (cfg, iargs [0]);
3774 NEW_CLASSCONST (cfg, iargs [1], cmethod->klass);
3776 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3778 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
3779 NEW_VTABLECONST (cfg, iargs [0], vtable);
3780 if (cmethod->klass->has_finalize || cmethod->klass->marshalbyref || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
3781 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
3783 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
3785 NEW_TEMPLOAD (cfg, *sp, temp);
3788 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3789 mono_method_check_inlining (cfg, cmethod) &&
3790 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
3791 !g_list_find (dont_inline, cmethod)) {
3793 MonoBasicBlock *ebblock;
3794 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3799 GET_BBLOCK (cfg, bbhash, bblock, ip);
3800 ebblock->next_bb = bblock;
3801 link_bblock (cfg, ebblock, bblock);
3803 NEW_TEMPLOAD (cfg, *sp, temp);
3806 /* indicates start of a new block, and triggers a load
3807 of all stack arguments at bb boundarie */
3810 inline_costs += costs;
3814 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3817 /* now call the actual ctor */
3818 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3822 NEW_TEMPLOAD (cfg, *sp, temp);
3831 MONO_INST_NEW (cfg, ins, *ip);
3833 klass = mono_class_get (image, read32 (ip + 1));
3834 mono_class_init (klass);
3835 ins->type = STACK_OBJ;
3836 ins->inst_left = *sp;
3837 ins->inst_newa_class = klass;
3842 case CEE_UNBOX_ANY: {
3843 MonoInst *add, *vtoffset;
3844 MonoInst *iargs [3];
3848 token = read32 (ip + 1);
3849 if (method->wrapper_type != MONO_WRAPPER_NONE)
3850 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3852 klass = mono_class_get (image, token);
3853 mono_class_init (klass);
3855 if (klass->byval_arg.type == MONO_TYPE_VAR)
3856 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3857 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
3858 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3860 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3862 MONO_INST_NEW (cfg, ins, CEE_CASTCLASS);
3863 ins->type = STACK_OBJ;
3864 ins->inst_left = *sp;
3866 ins->inst_newa_class = klass;
3873 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
3874 ins->type = STACK_OBJ;
3875 ins->inst_left = *sp;
3877 ins->inst_newa_class = klass;
3880 MONO_INST_NEW (cfg, add, CEE_ADD);
3881 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3882 add->inst_left = ins;
3883 add->inst_right = vtoffset;
3884 add->type = STACK_MP;
3888 n = mono_class_value_size (klass, NULL);
3889 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3890 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3891 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3893 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3894 copy->inst_left = iargs [0];
3895 copy->inst_right = *sp;
3896 copy->cil_code = ip;
3898 MONO_ADD_INS (bblock, copy);
3901 NEW_ICONST (cfg, iargs [2], n);
3902 iargs [2]->cil_code = ip;
3904 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3906 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3912 MonoInst *add, *vtoffset;
3916 token = read32 (ip + 1);
3917 if (method->wrapper_type != MONO_WRAPPER_NONE)
3918 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3920 klass = mono_class_get (image, token);
3921 mono_class_init (klass);
3923 if (klass->byval_arg.type == MONO_TYPE_VAR)
3924 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3925 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
3926 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3928 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
3929 ins->type = STACK_OBJ;
3930 ins->inst_left = *sp;
3932 ins->inst_newa_class = klass;
3935 MONO_INST_NEW (cfg, add, CEE_ADD);
3936 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3937 add->inst_left = ins;
3938 add->inst_right = vtoffset;
3939 add->type = STACK_MP;
3947 MONO_INST_NEW (cfg, ins, *ip);
3949 klass = mono_class_get (image, read32 (ip + 1));
3950 mono_class_init (klass);
3951 if (klass->byval_arg.type == MONO_TYPE_VAR)
3952 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3953 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
3954 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3955 ins->type = STACK_OBJ;
3956 ins->inst_left = *sp;
3958 ins->inst_newa_class = klass;
3965 MONO_INST_NEW (cfg, ins, *ip);
3967 ins->inst_left = *sp;
3968 ins->cil_code = ip++;
3969 MONO_ADD_INS (bblock, ins);
3971 start_new_bblock = 1;
3976 MonoInst *offset_ins;
3977 MonoClassField *field;
3978 MonoBasicBlock *ebblock;
3982 if (*ip == CEE_STFLD) {
3989 // FIXME: enable this test later.
3990 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
3992 token = read32 (ip + 1);
3993 field = mono_field_from_token (image, token, &klass);
3994 if (field->parent->gen_params)
3995 field = get_generic_field_inst (field, method->klass, &klass);
3996 else if (field->parent->generic_inst && method->klass->generic_inst)
3997 field = inflate_generic_field (field, method->klass, &klass);
3998 mono_class_init (klass);
4000 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
4001 /* FIXME: mark instructions for use in SSA */
4002 if (*ip == CEE_STFLD) {
4003 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
4004 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
4005 MonoInst *iargs [5];
4008 NEW_CLASSCONST (cfg, iargs [1], klass);
4009 NEW_FIELDCONST (cfg, iargs [2], field);
4010 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
4014 if (cfg->opt & MONO_OPT_INLINE) {
4015 costs = inline_method (cfg, stfld_wrapper, stfld_wrapper->signature, bblock,
4016 iargs, ip, real_offset, dont_inline, &ebblock);
4017 g_assert (costs > 0);
4022 GET_BBLOCK (cfg, bbhash, bblock, ip);
4023 ebblock->next_bb = bblock;
4024 link_bblock (cfg, ebblock, bblock);
4026 /* indicates start of a new block, and triggers a load
4027 of all stack arguments at bb boundarie */
4030 inline_costs += costs;
4033 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, stfld_wrapper->signature, iargs, ip, NULL);
4037 NEW_ICONST (cfg, offset_ins, foffset);
4038 MONO_INST_NEW (cfg, ins, CEE_ADD);
4040 ins->inst_left = *sp;
4041 ins->inst_right = offset_ins;
4042 ins->type = STACK_MP;
4044 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4045 store->cil_code = ip;
4046 store->inst_left = ins;
4047 store->inst_right = sp [1];
4048 handle_loaded_temps (cfg, bblock, stack_start, sp);
4049 store->flags |= ins_flag;
4051 if (store->opcode == CEE_STOBJ) {
4052 handle_stobj (cfg, bblock, ins, sp [1], ip,
4053 mono_class_from_mono_type (field->type), FALSE, FALSE);
4055 MONO_ADD_INS (bblock, store);
4058 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
4059 /* fixme: we need to inline that call somehow */
4060 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
4061 MonoInst *iargs [4];
4065 NEW_CLASSCONST (cfg, iargs [1], klass);
4066 NEW_FIELDCONST (cfg, iargs [2], field);
4067 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
4068 if (cfg->opt & MONO_OPT_INLINE) {
4069 costs = inline_method (cfg, ldfld_wrapper, ldfld_wrapper->signature, bblock,
4070 iargs, ip, real_offset, dont_inline, &ebblock);
4071 g_assert (costs > 0);
4076 GET_BBLOCK (cfg, bbhash, bblock, ip);
4077 ebblock->next_bb = bblock;
4078 link_bblock (cfg, ebblock, bblock);
4080 temp = iargs [0]->inst_i0->inst_c0;
4082 if (*ip == CEE_LDFLDA) {
4083 /* not sure howto handle this */
4084 NEW_TEMPLOADA (cfg, *sp, temp);
4086 NEW_TEMPLOAD (cfg, *sp, temp);
4090 /* indicates start of a new block, and triggers a load of
4091 all stack arguments at bb boundarie */
4094 inline_costs += costs;
4097 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, ldfld_wrapper->signature, iargs, ip, NULL);
4098 if (*ip == CEE_LDFLDA) {
4099 /* not sure howto handle this */
4100 NEW_TEMPLOADA (cfg, *sp, temp);
4102 NEW_TEMPLOAD (cfg, *sp, temp);
4107 NEW_ICONST (cfg, offset_ins, foffset);
4108 MONO_INST_NEW (cfg, ins, CEE_ADD);
4110 ins->inst_left = *sp;
4111 ins->inst_right = offset_ins;
4112 ins->type = STACK_MP;
4114 if (*ip == CEE_LDFLDA) {
4118 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4119 type_to_eval_stack_type (field->type, load);
4120 load->cil_code = ip;
4121 load->inst_left = ins;
4122 load->flags |= ins_flag;
4134 MonoClassField *field;
4135 gpointer addr = NULL;
4137 token = read32 (ip + 1);
4139 field = mono_field_from_token (image, token, &klass);
4140 if (field->parent->gen_params)
4141 field = get_generic_field_inst (field, method->klass, &klass);
4142 else if (field->parent->generic_inst && method->klass->generic_inst)
4143 field = inflate_generic_field (field, method->klass, &klass);
4144 mono_class_init (klass);
4146 handle_loaded_temps (cfg, bblock, stack_start, sp);
4148 if (cfg->domain->special_static_fields)
4149 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
4151 if ((cfg->opt & MONO_OPT_SHARED) || (mono_compile_aot && addr)) {
4153 MonoInst *iargs [2];
4154 g_assert (field->parent);
4155 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
4156 NEW_FIELDCONST (cfg, iargs [1], field);
4157 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
4158 NEW_TEMPLOAD (cfg, ins, temp);
4161 vtable = mono_class_vtable (cfg->domain, klass);
4163 if ((!vtable->initialized || mono_compile_aot) && !(klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (klass, method)) {
4164 guint8 *tramp = mono_create_class_init_trampoline (vtable);
4165 mono_emit_native_call (cfg, bblock, tramp,
4166 helper_sig_class_init_trampoline,
4168 if (cfg->verbose_level > 2)
4169 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
4171 mono_runtime_class_init (vtable);
4173 addr = (char*)vtable->data + field->offset;
4175 if (mono_compile_aot)
4176 NEW_SFLDACONST (cfg, ins, field);
4178 NEW_PCONST (cfg, ins, addr);
4182 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
4183 * This could be later optimized to do just a couple of
4184 * memory dereferences with constant offsets.
4187 MonoInst *iargs [1];
4188 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
4189 temp = mono_emit_jit_icall (cfg, bblock, mono_get_special_static_data, iargs, ip);
4190 NEW_TEMPLOAD (cfg, ins, temp);
4194 /* FIXME: mark instructions for use in SSA */
4195 if (*ip == CEE_LDSFLDA) {
4197 } else if (*ip == CEE_STSFLD) {
4201 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4202 store->cil_code = ip;
4203 store->inst_left = ins;
4204 store->inst_right = sp [0];
4205 store->flags |= ins_flag;
4208 if (store->opcode == CEE_STOBJ) {
4209 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
4211 MONO_ADD_INS (bblock, store);
4213 gboolean is_const = FALSE;
4214 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4215 if (!((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) &&
4216 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
4217 gpointer addr = (char*)vtable->data + field->offset;
4218 /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
4220 switch (field->type->type) {
4221 case MONO_TYPE_BOOLEAN:
4223 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
4227 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
4230 case MONO_TYPE_CHAR:
4232 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
4236 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
4241 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
4245 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
4250 case MONO_TYPE_STRING:
4251 case MONO_TYPE_OBJECT:
4252 case MONO_TYPE_CLASS:
4253 case MONO_TYPE_SZARRAY:
4255 case MONO_TYPE_FNPTR:
4256 case MONO_TYPE_ARRAY:
4257 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
4258 type_to_eval_stack_type (field->type, *sp);
4263 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
4264 sp [0]->type = STACK_I8;
4265 sp [0]->inst_l = *((gint64 *)addr);
4270 case MONO_TYPE_VALUETYPE:
4279 CHECK_STACK_OVF (1);
4280 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4281 type_to_eval_stack_type (field->type, load);
4282 load->cil_code = ip;
4283 load->inst_left = ins;
4285 load->flags |= ins_flag;
4287 /* fixme: dont see the problem why this does not work */
4288 //cfg->disable_aot = TRUE;
4297 token = read32 (ip + 1);
4298 if (method->wrapper_type != MONO_WRAPPER_NONE)
4299 klass = mono_method_get_wrapper_data (method, token);
4301 klass = mono_class_get (image, token);
4302 mono_class_init (klass);
4303 if (klass->byval_arg.type == MONO_TYPE_VAR)
4304 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4305 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4306 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4307 n = mono_type_to_stind (&klass->byval_arg);
4308 if (n == CEE_STOBJ) {
4309 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
4311 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
4313 MONO_INST_NEW (cfg, store, n);
4314 store->cil_code = ip;
4315 store->inst_left = sp [0];
4316 store->inst_right = sp [1];
4317 store->flags |= ins_flag;
4318 MONO_ADD_INS (bblock, store);
4325 MonoInst *iargs [2];
4326 MonoInst *load, *vtoffset, *add, *val, *vstore;
4331 token = read32 (ip + 1);
4332 if (method->wrapper_type != MONO_WRAPPER_NONE)
4333 klass = mono_method_get_wrapper_data (method, token);
4335 klass = mono_class_get (image, token);
4336 mono_class_init (klass);
4337 if (klass->byval_arg.type == MONO_TYPE_VAR)
4338 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4339 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4340 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4342 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4347 /* much like NEWOBJ */
4348 if (cfg->opt & MONO_OPT_SHARED) {
4349 NEW_DOMAINCONST (cfg, iargs [0]);
4350 NEW_CLASSCONST (cfg, iargs [1], klass);
4352 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
4354 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4355 NEW_VTABLECONST (cfg, iargs [0], vtable);
4356 if (1 || klass->has_finalize || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
4357 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
4359 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
4361 NEW_TEMPLOAD (cfg, load, temp);
4362 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4363 MONO_INST_NEW (cfg, add, CEE_ADD);
4364 add->inst_left = load;
4365 add->inst_right = vtoffset;
4368 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
4369 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
4370 vstore->cil_code = ip;
4371 vstore->inst_left = add;
4372 vstore->inst_right = val;
4374 if (vstore->opcode == CEE_STOBJ) {
4375 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
4377 MONO_ADD_INS (bblock, vstore);
4379 NEW_TEMPLOAD (cfg, load, temp);
4387 MONO_INST_NEW (cfg, ins, *ip);
4391 token = read32 (ip + 1);
4393 /* allocate the domainvar - becaus this is used in decompose_foreach */
4394 if (cfg->opt & MONO_OPT_SHARED)
4395 mono_get_domainvar (cfg);
4397 if (method->wrapper_type != MONO_WRAPPER_NONE)
4398 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4400 klass = mono_class_get (image, token);
4402 mono_class_init (klass);
4403 if (klass->byval_arg.type == MONO_TYPE_VAR)
4404 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4405 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4406 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4407 ins->inst_newa_class = klass;
4408 ins->inst_newa_len = *sp;
4409 ins->type = STACK_OBJ;
4416 MONO_INST_NEW (cfg, ins, *ip);
4417 ins->cil_code = ip++;
4419 ins->inst_left = *sp;
4420 ins->type = STACK_PTR;
4427 if (method->wrapper_type != MONO_WRAPPER_NONE)
4428 klass = (MonoClass*)mono_method_get_wrapper_data (method, read32 (ip + 1));
4430 klass = mono_class_get (image, read32 (ip + 1));
4431 mono_class_init (klass);
4432 if (klass->byval_arg.type == MONO_TYPE_VAR)
4433 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4434 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4435 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4436 NEW_LDELEMA (cfg, ins, sp, klass);
4445 token = read32 (ip + 1);
4446 klass = mono_class_get (image, token);
4447 mono_class_init (klass);
4448 if (klass->byval_arg.type == MONO_TYPE_VAR)
4449 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4450 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4451 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4452 NEW_LDELEMA (cfg, load, sp, klass);
4453 load->cil_code = ip;
4454 MONO_INST_NEW (cfg, ins, mono_type_to_ldind (&klass->byval_arg));
4456 ins->inst_left = load;
4458 type_to_eval_stack_type (&klass->byval_arg, ins);
4472 case CEE_LDELEM_REF: {
4476 * ldind.x (ldelema (array, index))
4477 * ldelema does the bounds check
4481 klass = array_access_to_klass (*ip);
4482 NEW_LDELEMA (cfg, load, sp, klass);
4483 load->cil_code = ip;
4484 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
4486 ins->inst_left = load;
4488 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
4498 case CEE_STELEM_R8: {
4502 * stind.x (ldelema (array, index), val)
4503 * ldelema does the bounds check
4507 klass = array_access_to_klass (*ip);
4508 NEW_LDELEMA (cfg, load, sp, klass);
4509 load->cil_code = ip;
4510 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
4512 ins->inst_left = load;
4513 ins->inst_right = sp [2];
4515 handle_loaded_temps (cfg, bblock, stack_start, sp);
4516 MONO_ADD_INS (bblock, ins);
4518 cfg->disable_ssa = TRUE;
4525 * stind.x (ldelema (array, index), val)
4526 * ldelema does the bounds check
4530 token = read32 (ip + 1);
4531 klass = mono_class_get (image, token);
4532 mono_class_init (klass);
4533 if (klass->byval_arg.type == MONO_TYPE_VAR)
4534 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4535 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4536 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4537 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4538 MonoInst *iargs [3];
4539 handle_loaded_temps (cfg, bblock, stack_start, sp);
4545 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4547 NEW_LDELEMA (cfg, load, sp, klass);
4548 load->cil_code = ip;
4549 MONO_INST_NEW (cfg, ins, mono_type_to_stind (&klass->byval_arg));
4551 ins->inst_left = load;
4552 ins->inst_right = sp [2];
4553 handle_loaded_temps (cfg, bblock, stack_start, sp);
4554 MONO_ADD_INS (bblock, ins);
4558 cfg->disable_ssa = TRUE;
4561 case CEE_STELEM_REF: {
4562 MonoInst *iargs [3];
4567 handle_loaded_temps (cfg, bblock, stack_start, sp);
4573 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4577 NEW_GROUP (cfg, group, sp [0], sp [1]);
4578 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
4580 ins->inst_left = group;
4581 ins->inst_right = sp [2];
4582 MONO_ADD_INS (bblock, ins);
4587 cfg->disable_ssa = TRUE;
4590 case CEE_CKFINITE: {
4591 MonoInst *store, *temp;
4594 /* this instr. can throw exceptions as side effect,
4595 * so we cant eliminate dead code which contains CKFINITE opdodes.
4596 * Spilling to memory makes sure that we always perform
4600 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
4602 ins->inst_left = sp [-1];
4603 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
4605 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4606 store->cil_code = ip;
4607 MONO_ADD_INS (bblock, store);
4609 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
4616 MONO_INST_NEW (cfg, ins, *ip);
4618 klass = mono_class_get (image, read32 (ip + 1));
4619 mono_class_init (klass);
4620 if (klass->byval_arg.type == MONO_TYPE_VAR)
4621 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4622 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4623 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4624 ins->type = STACK_MP;
4625 ins->inst_left = *sp;
4627 ins->inst_newa_class = klass;
4632 case CEE_MKREFANY: {
4633 MonoInst *loc, *klassconst;
4636 MONO_INST_NEW (cfg, ins, *ip);
4638 klass = mono_class_get (image, read32 (ip + 1));
4639 mono_class_init (klass);
4640 if (klass->byval_arg.type == MONO_TYPE_VAR)
4641 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4642 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4643 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4646 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
4647 NEW_TEMPLOADA (cfg, ins->inst_right, loc->inst_c0);
4649 NEW_PCONST (cfg, klassconst, klass);
4650 NEW_GROUP (cfg, ins->inst_left, *sp, klassconst);
4652 MONO_ADD_INS (bblock, ins);
4654 NEW_TEMPLOAD (cfg, *sp, loc->inst_c0);
4661 MonoClass *handle_class;
4663 CHECK_STACK_OVF (1);
4665 n = read32 (ip + 1);
4667 handle = mono_ldtoken (image, n, &handle_class);
4668 mono_class_init (handle_class);
4670 if (cfg->opt & MONO_OPT_SHARED) {
4672 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
4674 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
4676 NEW_IMAGECONST (cfg, iargs [0], image);
4677 NEW_ICONST (cfg, iargs [1], n);
4678 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
4679 NEW_TEMPLOAD (cfg, res, temp);
4680 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4681 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
4682 MONO_ADD_INS (bblock, store);
4683 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4685 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method (image, read32 (ip + 6), NULL)) &&
4686 (cmethod->klass == mono_defaults.monotype_class->parent) &&
4687 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
4688 MonoClass *tclass = mono_class_from_mono_type (handle);
4689 mono_class_init (tclass);
4690 if (mono_compile_aot)
4691 NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n);
4693 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
4694 ins->type = STACK_OBJ;
4695 ins->klass = cmethod->klass;
4698 if (mono_compile_aot)
4699 NEW_LDTOKENCONST (cfg, ins, image, n);
4701 NEW_PCONST (cfg, ins, handle);
4702 ins->type = STACK_VTYPE;
4703 ins->klass = handle_class;
4719 case CEE_ADD_OVF_UN:
4721 case CEE_MUL_OVF_UN:
4723 case CEE_SUB_OVF_UN:
4728 case CEE_ENDFINALLY:
4729 /* FIXME: check stack state */
4730 MONO_INST_NEW (cfg, ins, *ip);
4731 MONO_ADD_INS (bblock, ins);
4732 ins->cil_code = ip++;
4733 start_new_bblock = 1;
4738 if (*ip == CEE_LEAVE) {
4739 target = ip + 5 + (gint32)read32(ip + 1);
4741 target = ip + 2 + (signed char)(ip [1]);
4744 /* empty the stack */
4745 while (sp != stack_start) {
4746 MONO_INST_NEW (cfg, ins, CEE_POP);
4750 MONO_ADD_INS (bblock, ins);
4754 * If this leave statement is in a catch block, check for a
4755 * pending exception, and rethrow it if necessary.
4757 for (i = 0; i < header->num_clauses; ++i) {
4758 MonoExceptionClause *clause = &header->clauses [i];
4759 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code)) {
4762 temp = mono_emit_jit_icall (cfg, bblock, mono_thread_get_pending_exception, NULL, ip);
4763 NEW_TEMPLOAD (cfg, *sp, temp);
4765 MONO_INST_NEW (cfg, ins, OP_THROW_OR_NULL);
4766 ins->inst_left = *sp;
4768 MONO_ADD_INS (bblock, ins);
4772 /* fixme: call fault handler ? */
4774 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
4776 for (tmp = handlers; tmp; tmp = tmp->next) {
4778 link_bblock (cfg, bblock, tblock);
4779 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
4781 ins->inst_target_bb = tblock;
4782 MONO_ADD_INS (bblock, ins);
4784 g_list_free (handlers);
4787 MONO_INST_NEW (cfg, ins, CEE_BR);
4789 MONO_ADD_INS (bblock, ins);
4790 GET_BBLOCK (cfg, bbhash, tblock, target);
4791 link_bblock (cfg, bblock, tblock);
4792 CHECK_BBLOCK (target, ip, tblock);
4793 ins->inst_target_bb = tblock;
4794 start_new_bblock = 1;
4796 if (*ip == CEE_LEAVE)
4805 MONO_INST_NEW (cfg, ins, *ip);
4807 handle_loaded_temps (cfg, bblock, stack_start, sp);
4808 MONO_ADD_INS (bblock, ins);
4809 ins->cil_code = ip++;
4810 ins->inst_i0 = sp [0];
4811 ins->inst_i1 = sp [1];
4819 /* trampoline mono specific opcodes */
4820 case MONO_CUSTOM_PREFIX: {
4822 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
4826 case CEE_MONO_FUNC1: {
4828 gpointer func = NULL;
4833 case MONO_MARSHAL_CONV_STR_LPWSTR:
4834 func = mono_string_to_utf16;
4836 case MONO_MARSHAL_CONV_LPWSTR_STR:
4837 func = mono_string_from_utf16;
4839 case MONO_MARSHAL_CONV_LPSTR_STR:
4840 func = mono_string_new_wrapper;
4842 case MONO_MARSHAL_CONV_STR_LPTSTR:
4843 case MONO_MARSHAL_CONV_STR_LPSTR:
4844 func = mono_string_to_utf8;
4846 case MONO_MARSHAL_CONV_STR_BSTR:
4847 func = mono_string_to_bstr;
4849 case MONO_MARSHAL_CONV_STR_TBSTR:
4850 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
4851 func = mono_string_to_ansibstr;
4853 case MONO_MARSHAL_CONV_SB_LPSTR:
4854 func = mono_string_builder_to_utf8;
4856 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
4857 func = mono_array_to_savearray;
4859 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
4860 func = mono_array_to_lparray;
4862 case MONO_MARSHAL_CONV_DEL_FTN:
4863 func = mono_delegate_to_ftnptr;
4865 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
4866 func = mono_marshal_string_array;
4869 g_warning ("unknown conversion %d\n", ip [2]);
4870 g_assert_not_reached ();
4873 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4874 NEW_TEMPLOAD (cfg, *sp, temp);
4878 inline_costs += 10 * num_calls++;
4881 case CEE_MONO_PROC2: {
4882 gpointer func = NULL;
4887 case MONO_MARSHAL_CONV_LPSTR_SB:
4888 func = mono_string_utf8_to_builder;
4890 case MONO_MARSHAL_FREE_ARRAY:
4891 func = mono_marshal_free_array;
4894 g_assert_not_reached ();
4897 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4899 inline_costs += 10 * num_calls++;
4902 case CEE_MONO_PROC3: {
4903 gpointer func = NULL;
4908 case MONO_MARSHAL_CONV_STR_BYVALSTR:
4909 func = mono_string_to_byvalstr;
4911 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
4912 func = mono_string_to_byvalwstr;
4915 g_assert_not_reached ();
4918 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4920 inline_costs += 10 * num_calls++;
4926 mono_emit_jit_icall (cfg, bblock, g_free, sp, ip);
4928 inline_costs += 10 * num_calls++;
4930 case CEE_MONO_LDPTR:
4931 CHECK_STACK_OVF (1);
4932 token = read32 (ip + 2);
4933 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
4937 inline_costs += 10 * num_calls++;
4939 case CEE_MONO_VTADDR:
4942 MONO_INST_NEW (cfg, ins, OP_VTADDR);
4944 ins->type = STACK_MP;
4945 ins->inst_left = *sp;
4949 case CEE_MONO_NEWOBJ: {
4950 MonoInst *iargs [2];
4952 CHECK_STACK_OVF (1);
4953 token = read32 (ip + 2);
4954 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4955 mono_class_init (klass);
4956 NEW_DOMAINCONST (cfg, iargs [0]);
4957 NEW_CLASSCONST (cfg, iargs [1], klass);
4958 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
4959 NEW_TEMPLOAD (cfg, *sp, temp);
4962 inline_costs += 10 * num_calls++;
4965 case CEE_MONO_OBJADDR:
4968 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
4970 ins->type = STACK_MP;
4971 ins->inst_left = *sp;
4975 case CEE_MONO_LDNATIVEOBJ:
4977 token = read32 (ip + 2);
4978 klass = mono_method_get_wrapper_data (method, token);
4979 g_assert (klass->valuetype);
4980 mono_class_init (klass);
4981 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
4985 case CEE_MONO_RETOBJ:
4986 g_assert (cfg->ret);
4987 g_assert (method->signature->pinvoke);
4991 token = read32 (ip + 2);
4992 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4994 NEW_RETLOADA (cfg, ins);
4995 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
4997 if (sp != stack_start)
5000 MONO_INST_NEW (cfg, ins, CEE_BR);
5002 ins->inst_target_bb = end_bblock;
5003 MONO_ADD_INS (bblock, ins);
5004 link_bblock (cfg, bblock, end_bblock);
5005 start_new_bblock = 1;
5009 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
5017 /* somewhat similar to LDTOKEN */
5018 MonoInst *addr, *vtvar;
5019 CHECK_STACK_OVF (1);
5020 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
5022 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5023 addr->cil_code = ip;
5024 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
5026 ins->inst_left = addr;
5027 MONO_ADD_INS (bblock, ins);
5028 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5041 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
5042 MONO_INST_NEW (cfg, ins, cmp->opcode);
5044 cmp->inst_i0 = sp [0];
5045 cmp->inst_i1 = sp [1];
5049 cmp->opcode = OP_COMPARE;
5051 ins->type = STACK_I4;
5061 CHECK_STACK_OVF (1);
5062 n = read32 (ip + 2);
5063 if (method->wrapper_type != MONO_WRAPPER_NONE)
5064 cmethod = mono_method_get_wrapper_data (method, n);
5066 cmethod = mono_get_method (image, n, NULL);
5069 * We can't do this in mono_ldftn, since it is used in
5070 * the synchronized wrapper, leading to an infinite loop.
5072 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
5073 cmethod = mono_marshal_get_synchronized_wrapper (cmethod);
5076 mono_class_init (cmethod->klass);
5077 handle_loaded_temps (cfg, bblock, stack_start, sp);
5079 NEW_METHODCONST (cfg, argconst, cmethod);
5080 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
5081 NEW_TEMPLOAD (cfg, *sp, temp);
5085 inline_costs += 10 * num_calls++;
5088 case CEE_LDVIRTFTN: {
5093 n = read32 (ip + 2);
5094 if (method->wrapper_type != MONO_WRAPPER_NONE)
5095 cmethod = mono_method_get_wrapper_data (method, n);
5097 cmethod = mono_get_method (image, n, NULL);
5099 mono_class_init (cmethod->klass);
5100 handle_loaded_temps (cfg, bblock, stack_start, sp);
5104 NEW_METHODCONST (cfg, args [1], cmethod);
5105 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
5106 NEW_TEMPLOAD (cfg, *sp, temp);
5110 inline_costs += 10 * num_calls++;
5114 CHECK_STACK_OVF (1);
5115 NEW_ARGLOAD (cfg, ins, read16 (ip + 2));
5121 CHECK_STACK_OVF (1);
5122 NEW_ARGLOADA (cfg, ins, read16 (ip + 2));
5130 handle_loaded_temps (cfg, bblock, stack_start, sp);
5131 n = read16 (ip + 2);
5132 NEW_ARGSTORE (cfg, ins, n, *sp);
5134 if (ins->opcode == CEE_STOBJ) {
5135 NEW_ARGLOADA (cfg, ins, n);
5136 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5138 MONO_ADD_INS (bblock, ins);
5142 CHECK_STACK_OVF (1);
5143 NEW_LOCLOAD (cfg, ins, read16 (ip + 2));
5149 CHECK_STACK_OVF (1);
5150 NEW_LOCLOADA (cfg, ins, read16 (ip + 2));
5158 n = read16 (ip + 2);
5159 handle_loaded_temps (cfg, bblock, stack_start, sp);
5160 NEW_LOCSTORE (cfg, ins, n, *sp);
5162 if (ins->opcode == CEE_STOBJ) {
5163 NEW_LOCLOADA (cfg, ins, n);
5164 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5166 MONO_ADD_INS (bblock, ins);
5173 if (sp != stack_start)
5175 MONO_INST_NEW (cfg, ins, 256 + ip [1]);
5176 ins->inst_left = *sp;
5179 cfg->flags |= MONO_CFG_HAS_ALLOCA;
5180 if (header->init_locals)
5181 ins->flags |= MONO_INST_INIT;
5185 /* FIXME: set init flag if locals init is set in this method */
5187 case CEE_ENDFILTER: {
5188 MonoExceptionClause *clause, *nearest;
5189 int cc, nearest_num;
5193 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
5195 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
5196 ins->inst_left = *sp;
5198 MONO_ADD_INS (bblock, ins);
5199 start_new_bblock = 1;
5204 for (cc = 0; cc < header->num_clauses; ++cc) {
5205 clause = &header->clauses [cc];
5206 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
5207 (!nearest || (clause->token_or_filter > nearest->token_or_filter))) {
5213 filter_lengths [nearest_num] = (ip - header->code) - nearest->token_or_filter;
5217 case CEE_UNALIGNED_:
5218 ins_flag |= MONO_INST_UNALIGNED;
5222 ins_flag |= MONO_INST_VOLATILE;
5226 ins_flag |= MONO_INST_TAILCALL;
5227 /* Can't inline tail calls at this time */
5228 inline_costs += 100000;
5234 token = read32 (ip + 2);
5235 if (method->wrapper_type != MONO_WRAPPER_NONE)
5236 klass = mono_method_get_wrapper_data (method, token);
5238 klass = mono_class_get (image, token);
5239 if (klass->byval_arg.type == MONO_TYPE_VAR)
5240 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
5241 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
5242 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
5244 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5245 MonoInst *store, *load;
5246 NEW_PCONST (cfg, load, NULL);
5247 load->cil_code = ip;
5248 load->type = STACK_OBJ;
5249 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
5250 store->cil_code = ip;
5251 handle_loaded_temps (cfg, bblock, stack_start, sp);
5252 MONO_ADD_INS (bblock, store);
5253 store->inst_i0 = sp [0];
5254 store->inst_i1 = load;
5257 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
5262 case CEE_CONSTRAINED_:
5263 /* FIXME: implement */
5264 token = read32 (ip + 2);
5269 MonoInst *iargs [3];
5275 handle_loaded_temps (cfg, bblock, stack_start, sp);
5276 if (ip [1] == CEE_CPBLK) {
5277 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
5279 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
5287 ins_flag |= MONO_INST_NOTYPECHECK;
5289 ins_flag |= MONO_INST_NORANGECHECK;
5290 /* we ignore the no-nullcheck for now since we
5291 * really do it explicitly only when doing callvirt->call
5297 /* FIXME: check we are in a catch handler */
5298 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
5299 load->cil_code = ip;
5300 MONO_INST_NEW (cfg, ins, CEE_THROW);
5301 ins->inst_left = load;
5303 MONO_ADD_INS (bblock, ins);
5305 start_new_bblock = 1;
5310 CHECK_STACK_OVF (1);
5311 token = read32 (ip + 2);
5312 /* FIXXME: handle generics. */
5313 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
5314 MonoType *type = mono_type_create_from_typespec (image, token);
5315 token = mono_type_size (type, &align);
5317 MonoClass *szclass = mono_class_get (image, token);
5318 mono_class_init (szclass);
5319 token = mono_class_value_size (szclass, &align);
5321 NEW_ICONST (cfg, ins, token);
5326 case CEE_REFANYTYPE:
5328 MONO_INST_NEW (cfg, ins, ip [1]);
5330 ins->type = STACK_MP;
5331 ins->inst_left = *sp;
5332 ins->type = STACK_VTYPE;
5333 ins->klass = mono_defaults.typehandle_class;
5342 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
5347 g_error ("opcode 0x%02x not handled", *ip);
5350 if (start_new_bblock != 1)
5353 bblock->cil_length = ip - bblock->cil_code;
5354 bblock->next_bb = end_bblock;
5355 link_bblock (cfg, bblock, end_bblock);
5357 if (cfg->method == method && cfg->domainvar) {
5361 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
5362 call->signature = helper_sig_domain_get;
5363 call->inst.type = STACK_PTR;
5364 call->fptr = mono_domain_get;
5365 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, (MonoInst*)call);
5367 MONO_ADD_INS (init_localsbb, store);
5370 if (header->init_locals) {
5372 for (i = 0; i < header->num_locals; ++i) {
5373 int t = header->locals [i]->type;
5374 if (t == MONO_TYPE_VALUETYPE && header->locals [i]->data.klass->enumtype)
5375 t = header->locals [i]->data.klass->enum_basetype->type;
5376 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
5377 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
5378 NEW_ICONST (cfg, ins, 0);
5379 NEW_LOCSTORE (cfg, store, i, ins);
5380 MONO_ADD_INS (init_localsbb, store);
5381 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
5382 MONO_INST_NEW (cfg, ins, OP_I8CONST);
5383 ins->type = STACK_I8;
5385 NEW_LOCSTORE (cfg, store, i, ins);
5386 MONO_ADD_INS (init_localsbb, store);
5387 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
5388 MONO_INST_NEW (cfg, ins, OP_R8CONST);
5389 ins->type = STACK_R8;
5390 ins->inst_p0 = (void*)&r8_0;
5391 NEW_LOCSTORE (cfg, store, i, ins);
5392 MONO_ADD_INS (init_localsbb, store);
5393 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF)) {
5394 NEW_LOCLOADA (cfg, ins, i);
5395 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (header->locals [i]), NULL, NULL);
5398 NEW_PCONST (cfg, ins, NULL);
5399 NEW_LOCSTORE (cfg, store, i, ins);
5400 MONO_ADD_INS (init_localsbb, store);
5406 /* resolve backward branches in the middle of an existing basic block */
5407 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
5409 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
5410 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
5411 if (tblock != start_bblock) {
5413 split_bblock (cfg, tblock, bblock);
5414 l = bblock->cil_code - header->code;
5415 bblock->cil_length = tblock->cil_length - l;
5416 tblock->cil_length = l;
5418 g_print ("recheck failed.\n");
5422 /* we compute regions here, because the length of filter clauses is not known in advance.
5423 * It is computed in the CEE_ENDFILTER case in the above switch statement*/
5424 if (cfg->method == method) {
5426 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5427 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
5429 mono_create_spvar_for_region (cfg, bb->region);
5430 if (cfg->verbose_level > 2)
5431 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
5434 g_hash_table_destroy (bbhash);
5437 dont_inline = g_list_remove (dont_inline, method);
5438 return inline_costs;
5441 if (cfg->method != method)
5442 g_hash_table_destroy (bbhash);
5443 dont_inline = g_list_remove (dont_inline, method);
5447 if (cfg->method != method)
5448 g_hash_table_destroy (bbhash);
5449 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
5450 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
5451 dont_inline = g_list_remove (dont_inline, method);
5456 mono_print_tree (MonoInst *tree) {
5462 arity = mono_burg_arity [tree->opcode];
5464 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
5466 switch (tree->opcode) {
5468 printf ("[%d]", tree->inst_c0);
5471 printf ("[%lld]", tree->inst_l);
5474 printf ("[%f]", *(double*)tree->inst_p0);
5477 printf ("[%f]", *(float*)tree->inst_p0);
5481 printf ("[%d]", tree->inst_c0);
5484 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5487 printf ("[%s]", mono_arch_regname (tree->dreg));
5490 printf ("[%s]", tree->inst_newa_class->name);
5491 mono_print_tree (tree->inst_newa_len);
5502 case OP_VOIDCALLVIRT: {
5503 MonoCallInst *call = (MonoCallInst*)tree;
5505 printf ("[%s]", call->method->name);
5510 printf ("[%d (", tree->inst_c0);
5511 for (i = 0; i < tree->inst_phi_args [0]; i++) {
5514 printf ("%d", tree->inst_phi_args [i + 1]);
5526 printf ("[B%d]", tree->inst_target_bb->block_num);
5536 case OP_VOIDCALL_REG:
5537 mono_print_tree (tree->inst_left);
5549 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
5550 mono_print_tree (tree->inst_left);
5554 mono_print_tree (tree->inst_left);
5556 mono_print_tree (tree->inst_right);
5566 mono_print_tree_nl (MonoInst *tree)
5568 mono_print_tree (tree);
5573 create_helper_signature (void)
5575 /* FIXME: set call conv */
5576 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
5577 helper_sig_newarr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5578 helper_sig_newarr->params [0] = helper_sig_newarr->params [1] = &mono_defaults.int_class->byval_arg;
5579 helper_sig_newarr->ret = &mono_defaults.object_class->byval_arg;
5580 helper_sig_newarr->params [2] = &mono_defaults.int32_class->byval_arg;
5581 helper_sig_newarr->pinvoke = 1;
5583 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
5584 helper_sig_newarr_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5585 helper_sig_newarr_specific->params [0] = &mono_defaults.int_class->byval_arg;
5586 helper_sig_newarr_specific->params [1] = &mono_defaults.int32_class->byval_arg;
5587 helper_sig_newarr_specific->ret = &mono_defaults.object_class->byval_arg;
5588 helper_sig_newarr_specific->pinvoke = 1;
5590 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
5591 helper_sig_object_new = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5592 helper_sig_object_new->params [0] = helper_sig_object_new->params [1] = &mono_defaults.int_class->byval_arg;
5593 helper_sig_object_new->ret = &mono_defaults.object_class->byval_arg;
5594 helper_sig_object_new->pinvoke = 1;
5596 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
5597 helper_sig_object_new_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5598 helper_sig_object_new_specific->params [0] = &mono_defaults.int_class->byval_arg;
5599 helper_sig_object_new_specific->ret = &mono_defaults.object_class->byval_arg;
5600 helper_sig_object_new_specific->pinvoke = 1;
5602 /* void* mono_method_compile (MonoMethod*) */
5603 helper_sig_compile = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5604 helper_sig_compile->params [0] = helper_sig_compile->ret = &mono_defaults.int_class->byval_arg;
5605 helper_sig_compile->pinvoke = 1;
5607 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
5608 helper_sig_compile_virt = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5609 helper_sig_compile_virt->params [0] = &mono_defaults.object_class->byval_arg;
5610 helper_sig_compile_virt->params [1] = helper_sig_compile_virt->ret = &mono_defaults.int_class->byval_arg;
5611 helper_sig_compile_virt->pinvoke = 1;
5613 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
5614 helper_sig_ldstr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5615 helper_sig_ldstr->params [0] = helper_sig_ldstr->params [1] = &mono_defaults.int_class->byval_arg;
5616 helper_sig_ldstr->params [2] = &mono_defaults.int32_class->byval_arg;
5617 helper_sig_ldstr->ret = &mono_defaults.object_class->byval_arg;
5618 helper_sig_ldstr->pinvoke = 1;
5620 /* MonoDomain *mono_domain_get (void) */
5621 helper_sig_domain_get = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5622 helper_sig_domain_get->ret = &mono_defaults.int_class->byval_arg;
5623 helper_sig_domain_get->pinvoke = 1;
5625 /* void* stelem_ref (MonoArray *, int index, MonoObject *) */
5626 helper_sig_stelem_ref = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5627 helper_sig_stelem_ref->params [0] = &mono_defaults.array_class->byval_arg;
5628 helper_sig_stelem_ref->params [1] = &mono_defaults.int32_class->byval_arg;
5629 helper_sig_stelem_ref->params [2] = &mono_defaults.object_class->byval_arg;
5630 helper_sig_stelem_ref->ret = &mono_defaults.void_class->byval_arg;
5631 helper_sig_stelem_ref->pinvoke = 1;
5633 /* long amethod (long, long) */
5634 helper_sig_long_long_long = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5635 helper_sig_long_long_long->params [0] = helper_sig_long_long_long->params [1] =
5636 &mono_defaults.int64_class->byval_arg;
5637 helper_sig_long_long_long->ret = &mono_defaults.int64_class->byval_arg;
5638 helper_sig_long_long_long->pinvoke = 1;
5640 /* object amethod (intptr) */
5641 helper_sig_obj_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5642 helper_sig_obj_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5643 helper_sig_obj_ptr->ret = &mono_defaults.object_class->byval_arg;
5644 helper_sig_obj_ptr->pinvoke = 1;
5646 /* void amethod (intptr) */
5647 helper_sig_void_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5648 helper_sig_void_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5649 helper_sig_void_ptr->ret = &mono_defaults.void_class->byval_arg;
5650 helper_sig_void_ptr->pinvoke = 1;
5652 /* void amethod (MonoObject *obj) */
5653 helper_sig_void_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5654 helper_sig_void_obj->params [0] = &mono_defaults.object_class->byval_arg;
5655 helper_sig_void_obj->ret = &mono_defaults.void_class->byval_arg;
5656 helper_sig_void_obj->pinvoke = 1;
5658 /* intptr amethod (void) */
5659 helper_sig_ptr_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5660 helper_sig_ptr_void->ret = &mono_defaults.int_class->byval_arg;
5661 helper_sig_ptr_void->pinvoke = 1;
5663 /* object amethod (void) */
5664 helper_sig_obj_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5665 helper_sig_obj_void->ret = &mono_defaults.object_class->byval_arg;
5666 helper_sig_obj_void->pinvoke = 1;
5668 /* void amethod (intptr, intptr) */
5669 helper_sig_void_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5670 helper_sig_void_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5671 helper_sig_void_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5672 helper_sig_void_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
5673 helper_sig_void_ptr_ptr->pinvoke = 1;
5675 /* void amethod (intptr, intptr, intptr) */
5676 helper_sig_void_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5677 helper_sig_void_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5678 helper_sig_void_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5679 helper_sig_void_ptr_ptr_ptr->params [2] = &mono_defaults.int_class->byval_arg;
5680 helper_sig_void_ptr_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
5681 helper_sig_void_ptr_ptr_ptr->pinvoke = 1;
5683 /* intptr amethod (intptr, intptr) */
5684 helper_sig_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5685 helper_sig_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5686 helper_sig_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5687 helper_sig_ptr_ptr_ptr->ret = &mono_defaults.int_class->byval_arg;
5688 helper_sig_ptr_ptr_ptr->pinvoke = 1;
5690 /* IntPtr amethod (object) */
5691 helper_sig_ptr_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5692 helper_sig_ptr_obj->params [0] = &mono_defaults.object_class->byval_arg;
5693 helper_sig_ptr_obj->ret = &mono_defaults.int_class->byval_arg;
5694 helper_sig_ptr_obj->pinvoke = 1;
5696 /* IntPtr amethod (int) */
5697 helper_sig_ptr_int = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5698 helper_sig_ptr_int->params [0] = &mono_defaults.int32_class->byval_arg;
5699 helper_sig_ptr_int->ret = &mono_defaults.int_class->byval_arg;
5700 helper_sig_ptr_int->pinvoke = 1;
5702 /* long amethod (long, guint32) */
5703 helper_sig_long_long_int = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5704 helper_sig_long_long_int->params [0] = &mono_defaults.int64_class->byval_arg;
5705 helper_sig_long_long_int->params [1] = &mono_defaults.int32_class->byval_arg;
5706 helper_sig_long_long_int->ret = &mono_defaults.int64_class->byval_arg;
5707 helper_sig_long_long_int->pinvoke = 1;
5709 /* ulong amethod (double) */
5710 helper_sig_ulong_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5711 helper_sig_ulong_double->params [0] = &mono_defaults.double_class->byval_arg;
5712 helper_sig_ulong_double->ret = &mono_defaults.uint64_class->byval_arg;
5713 helper_sig_ulong_double->pinvoke = 1;
5715 /* long amethod (double) */
5716 helper_sig_long_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5717 helper_sig_long_double->params [0] = &mono_defaults.double_class->byval_arg;
5718 helper_sig_long_double->ret = &mono_defaults.int64_class->byval_arg;
5719 helper_sig_long_double->pinvoke = 1;
5721 /* double amethod (long) */
5722 helper_sig_double_long = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5723 helper_sig_double_long->params [0] = &mono_defaults.int64_class->byval_arg;
5724 helper_sig_double_long->ret = &mono_defaults.double_class->byval_arg;
5725 helper_sig_double_long->pinvoke = 1;
5727 /* float amethod (long) */
5728 helper_sig_float_long = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5729 helper_sig_float_long->params [0] = &mono_defaults.int64_class->byval_arg;
5730 helper_sig_float_long->ret = &mono_defaults.single_class->byval_arg;
5731 helper_sig_float_long->pinvoke = 1;
5733 /* double amethod (double, double) */
5734 helper_sig_double_double_double = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5735 helper_sig_double_double_double->params [0] = &mono_defaults.double_class->byval_arg;
5736 helper_sig_double_double_double->params [1] = &mono_defaults.double_class->byval_arg;
5737 helper_sig_double_double_double->ret = &mono_defaults.double_class->byval_arg;
5738 helper_sig_double_double_double->pinvoke = 1;
5740 /* uint amethod (double) */
5741 helper_sig_uint_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5742 helper_sig_uint_double->params [0] = &mono_defaults.double_class->byval_arg;
5743 helper_sig_uint_double->ret = &mono_defaults.uint32_class->byval_arg;
5744 helper_sig_uint_double->pinvoke = 1;
5746 /* int amethod (double) */
5747 helper_sig_int_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5748 helper_sig_int_double->params [0] = &mono_defaults.double_class->byval_arg;
5749 helper_sig_int_double->ret = &mono_defaults.int32_class->byval_arg;
5750 helper_sig_int_double->pinvoke = 1;
5752 /* void initobj (intptr, int size) */
5753 helper_sig_initobj = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5754 helper_sig_initobj->params [0] = &mono_defaults.int_class->byval_arg;
5755 helper_sig_initobj->params [1] = &mono_defaults.int32_class->byval_arg;
5756 helper_sig_initobj->ret = &mono_defaults.void_class->byval_arg;
5757 helper_sig_initobj->pinvoke = 1;
5759 /* void memcpy (intptr, intptr, int size) */
5760 helper_sig_memcpy = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5761 helper_sig_memcpy->params [0] = &mono_defaults.int_class->byval_arg;
5762 helper_sig_memcpy->params [1] = &mono_defaults.int_class->byval_arg;
5763 helper_sig_memcpy->params [2] = &mono_defaults.int32_class->byval_arg;
5764 helper_sig_memcpy->ret = &mono_defaults.void_class->byval_arg;
5765 helper_sig_memcpy->pinvoke = 1;
5767 /* void memset (intptr, int val, int size) */
5768 helper_sig_memset = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5769 helper_sig_memset->params [0] = &mono_defaults.int_class->byval_arg;
5770 helper_sig_memset->params [1] = &mono_defaults.int32_class->byval_arg;
5771 helper_sig_memset->params [2] = &mono_defaults.int32_class->byval_arg;
5772 helper_sig_memset->ret = &mono_defaults.void_class->byval_arg;
5773 helper_sig_memset->pinvoke = 1;
5775 helper_sig_class_init_trampoline = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5776 helper_sig_class_init_trampoline->ret = &mono_defaults.void_class->byval_arg;
5777 helper_sig_class_init_trampoline->pinvoke = 1;
5780 static GHashTable *jit_icall_hash_name = NULL;
5781 static GHashTable *jit_icall_hash_addr = NULL;
5784 mono_find_jit_icall_by_name (const char *name)
5786 g_assert (jit_icall_hash_name);
5788 //printf ("lookup addr %s %p\n", name, g_hash_table_lookup (jit_icall_hash_name, name));
5789 return g_hash_table_lookup (jit_icall_hash_name, name);
5793 mono_find_jit_icall_by_addr (gconstpointer addr)
5795 g_assert (jit_icall_hash_addr);
5797 return g_hash_table_lookup (jit_icall_hash_addr, (gpointer)addr);
5801 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
5804 MonoMethod *wrapper;
5806 if (callinfo->wrapper)
5807 return callinfo->wrapper;
5808 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
5809 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
5810 callinfo->wrapper = mono_jit_compile_method (wrapper);
5812 return callinfo->wrapper;
5816 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save)
5818 MonoJitICallInfo *info;
5823 if (!jit_icall_hash_name) {
5824 jit_icall_hash_name = g_hash_table_new (g_str_hash, g_str_equal);
5825 jit_icall_hash_addr = g_hash_table_new (NULL, NULL);
5828 if (g_hash_table_lookup (jit_icall_hash_name, name)) {
5829 g_warning ("jit icall already defined \"%s\"\n", name);
5830 g_assert_not_reached ();
5833 info = g_new (MonoJitICallInfo, 1);
5840 #ifdef MONO_USE_EXC_TABLES
5841 || mono_arch_has_unwind_info (func)
5844 info->wrapper = func;
5846 info->wrapper = NULL;
5847 mono_icall_get_wrapper (info);
5850 g_hash_table_insert (jit_icall_hash_name, (gpointer)info->name, info);
5851 g_hash_table_insert (jit_icall_hash_addr, (gpointer)func, info);
5852 if (func != info->wrapper)
5853 g_hash_table_insert (jit_icall_hash_addr, (gpointer)info->wrapper, info);
5859 mono_create_class_init_trampoline (MonoVTable *vtable)
5863 /* previously created trampoline code */
5864 mono_domain_lock (vtable->domain);
5866 mono_g_hash_table_lookup (vtable->domain->class_init_trampoline_hash,
5868 mono_domain_unlock (vtable->domain);
5872 code = mono_arch_create_class_init_trampoline (vtable);
5874 /* store trampoline address */
5875 mono_domain_lock (vtable->domain);
5876 mono_g_hash_table_insert (vtable->domain->class_init_trampoline_hash,
5878 mono_domain_unlock (vtable->domain);
5880 EnterCriticalSection (&class_init_hash_mutex);
5881 if (!class_init_hash_addr)
5882 class_init_hash_addr = g_hash_table_new (NULL, NULL);
5883 g_hash_table_insert (class_init_hash_addr, code, vtable);
5884 LeaveCriticalSection (&class_init_hash_mutex);
5890 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
5894 EnterCriticalSection (&class_init_hash_mutex);
5895 if (class_init_hash_addr)
5896 res = g_hash_table_lookup (class_init_hash_addr, addr);
5899 LeaveCriticalSection (&class_init_hash_mutex);
5903 static GHashTable *emul_opcode_hash = NULL;
5905 static MonoJitICallInfo *
5906 mono_find_jit_opcode_emulation (int opcode)
5908 if (emul_opcode_hash)
5909 return g_hash_table_lookup (emul_opcode_hash, (gpointer)opcode);
5915 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func, gboolean no_throw)
5917 MonoJitICallInfo *info;
5919 if (!emul_opcode_hash)
5920 emul_opcode_hash = g_hash_table_new (NULL, NULL);
5922 g_assert (!sig->hasthis);
5923 g_assert (sig->param_count < 3);
5925 info = mono_register_jit_icall (func, name, sig, no_throw);
5927 g_hash_table_insert (emul_opcode_hash, (gpointer)opcode, info);
5931 decompose_foreach (MonoInst *tree, gpointer data)
5933 static MonoJitICallInfo *newarr_info = NULL;
5934 static MonoJitICallInfo *newarr_specific_info = NULL;
5935 MonoJitICallInfo *info;
5938 switch (tree->opcode) {
5940 MonoCompile *cfg = data;
5941 MonoInst *iargs [3];
5944 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
5945 g_assert (newarr_info);
5946 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
5947 g_assert (newarr_specific_info);
5950 if (cfg->opt & MONO_OPT_SHARED) {
5951 NEW_DOMAINCONST (cfg, iargs [0]);
5952 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
5953 iargs [2] = tree->inst_newa_len;
5958 MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
5960 NEW_VTABLECONST (cfg, iargs [0], vtable);
5961 iargs [1] = tree->inst_newa_len;
5963 info = newarr_specific_info;
5966 mono_emulate_opcode (cfg, tree, iargs, info);
5968 /* Need to decompose arguments after the the opcode is decomposed */
5969 for (i = 0; i < info->sig->param_count; ++i)
5970 dec_foreach (iargs [i], cfg);
5980 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
5982 switch (mono_burg_arity [tree->opcode]) {
5985 mono_inst_foreach (tree->inst_left, func, data);
5988 mono_inst_foreach (tree->inst_left, func, data);
5989 mono_inst_foreach (tree->inst_right, func, data);
5992 g_assert_not_reached ();
5999 mono_print_bb_code (MonoBasicBlock *bb) {
6001 MonoInst *c = bb->code;
6003 mono_print_tree (c);
6011 print_dfn (MonoCompile *cfg) {
6016 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
6018 for (i = 0; i < cfg->num_bblocks; ++i) {
6019 bb = cfg->bblocks [i];
6021 char* code1, *code2;
6022 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
6023 if (bb->last_ins->cil_code)
6024 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
6026 code2 = g_strdup ("");
6028 code1 [strlen (code1) - 1] = 0;
6029 code = g_strdup_printf ("%s -> %s", code1, code2);
6033 code = g_strdup ("\n");
6034 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
6036 MonoInst *c = bb->code;
6038 mono_print_tree (c);
6046 g_print ("\tprev:");
6047 for (j = 0; j < bb->in_count; ++j) {
6048 g_print (" BB%d", bb->in_bb [j]->block_num);
6050 g_print ("\t\tsucc:");
6051 for (j = 0; j < bb->out_count; ++j) {
6052 g_print (" BB%d", bb->out_bb [j]->block_num);
6054 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
6057 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
6060 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
6062 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
6070 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
6074 g_assert (bb->code);
6075 bb->last_ins->next = inst;
6076 bb->last_ins = inst;
6078 bb->last_ins = bb->code = inst;
6083 mono_destroy_compile (MonoCompile *cfg)
6085 //mono_mempool_stats (cfg->mempool);
6086 g_hash_table_destroy (cfg->bb_hash);
6088 mono_regstate_free (cfg->rs);
6090 g_hash_table_destroy (cfg->spvars);
6091 mono_mempool_destroy (cfg->mempool);
6092 g_list_free (cfg->ldstr_list);
6094 g_free (cfg->varinfo);
6100 mono_get_lmf_addr (void)
6102 MonoJitTlsData *jit_tls;
6104 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
6105 return &jit_tls->lmf;
6107 g_assert_not_reached ();
6112 * mono_thread_abort:
6113 * @obj: exception object
6115 * abort the thread, print exception information and stack trace
6118 mono_thread_abort (MonoObject *obj)
6120 /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
6122 /* handle_remove should be eventually called for this thread, too
6129 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
6131 MonoJitTlsData *jit_tls;
6134 jit_tls = g_new0 (MonoJitTlsData, 1);
6136 TlsSetValue (mono_jit_tls_id, jit_tls);
6138 jit_tls->abort_func = abort_func;
6139 jit_tls->end_of_stack = stack_start;
6141 lmf = g_new0 (MonoLMF, 1);
6144 jit_tls->lmf = jit_tls->first_lmf = lmf;
6146 mono_arch_setup_jit_tls_data (jit_tls);
6152 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
6155 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
6156 thread = mono_thread_current ();
6158 thread->jit_data = jit_tls;
6161 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
6164 mono_thread_abort_dummy (MonoObject *obj)
6166 if (mono_thread_attach_aborted_cb)
6167 mono_thread_attach_aborted_cb (obj);
6169 mono_thread_abort (obj);
6173 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
6176 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
6177 thread = mono_thread_current ();
6179 thread->jit_data = jit_tls;
6183 mini_thread_cleanup (MonoThread *thread)
6185 MonoJitTlsData *jit_tls = thread->jit_data;
6188 g_free (jit_tls->first_lmf);
6190 thread->jit_data = NULL;
6195 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
6197 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
6201 ji->data.target = target;
6202 ji->next = cfg->patch_info;
6204 cfg->patch_info = ji;
6208 mono_remove_patch_info (MonoCompile *cfg, int ip)
6210 MonoJumpInfo **ji = &cfg->patch_info;
6213 if ((*ji)->ip.i == ip)
6216 ji = &((*ji)->next);
6221 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
6222 MonoJitICallInfo *info;
6224 decompose_foreach (tree, cfg);
6226 switch (mono_burg_arity [tree->opcode]) {
6229 dec_foreach (tree->inst_left, cfg);
6231 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6232 MonoInst *iargs [2];
6234 iargs [0] = tree->inst_left;
6236 mono_emulate_opcode (cfg, tree, iargs, info);
6242 #ifdef MONO_ARCH_BIGMUL_INTRINS
6243 if (tree->opcode == OP_LMUL
6244 && (cfg->opt & MONO_OPT_INTRINS)
6245 && (tree->inst_left->opcode == CEE_CONV_I8
6246 || tree->inst_left->opcode == CEE_CONV_U8)
6247 && tree->inst_left->inst_left->type == STACK_I4
6248 && (tree->inst_right->opcode == CEE_CONV_I8
6249 || tree->inst_right->opcode == CEE_CONV_U8)
6250 && tree->inst_right->inst_left->type == STACK_I4) {
6251 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
6252 tree->inst_left = tree->inst_left->inst_left;
6253 tree->inst_right = tree->inst_right->inst_left;
6254 dec_foreach (tree, cfg);
6257 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6258 MonoInst *iargs [2];
6260 iargs [0] = tree->inst_i0;
6261 iargs [1] = tree->inst_i1;
6263 mono_emulate_opcode (cfg, tree, iargs, info);
6265 dec_foreach (iargs [0], cfg);
6266 dec_foreach (iargs [1], cfg);
6269 dec_foreach (tree->inst_left, cfg);
6270 dec_foreach (tree->inst_right, cfg);
6274 g_assert_not_reached ();
6279 decompose_pass (MonoCompile *cfg) {
6282 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6285 cfg->prev_ins = NULL;
6286 for (tree = cfg->cbb->code; tree; tree = tree->next) {
6287 dec_foreach (tree, cfg);
6288 cfg->prev_ins = tree;
6294 nullify_basic_block (MonoBasicBlock *bb)
6301 bb->code = bb->last_ins = NULL;
6302 bb->cil_code = NULL;
6306 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6310 for (i = 0; i < bb->out_count; i++) {
6311 MonoBasicBlock *ob = bb->out_bb [i];
6314 if (bb->out_count > 1) {
6315 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
6319 bb->out_bb [i] = repl;
6326 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6330 for (i = 0; i < bb->in_count; i++) {
6331 MonoBasicBlock *ib = bb->in_bb [i];
6334 if (bb->in_count > 1) {
6335 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
6339 bb->in_bb [i] = repl;
6346 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6350 for (i = 0; i < bb->out_count; i++) {
6351 MonoBasicBlock *ob = bb->out_bb [i];
6352 for (j = 0; j < ob->in_count; j++) {
6353 if (ob->in_bb [j] == orig) {
6354 ob->in_bb [j] = repl;
6363 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
6365 bb->out_count = bbn->out_count;
6366 bb->out_bb = bbn->out_bb;
6368 replace_basic_block (bb, bbn, bb);
6372 bb->last_ins->next = bbn->code;
6373 bb->last_ins = bbn->last_ins;
6376 bb->code = bbn->code;
6377 bb->last_ins = bbn->last_ins;
6379 bb->next_bb = bbn->next_bb;
6380 nullify_basic_block (bbn);
6384 optimize_branches (MonoCompile *cfg) {
6385 int i, changed = FALSE;
6386 MonoBasicBlock *bb, *bbn;
6391 /* we skip the entry block (exit is handled specially instead ) */
6392 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
6394 /* dont touch code inside exception clauses */
6395 if (bb->region != -1)
6398 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
6399 if (cfg->verbose_level > 2)
6400 g_print ("nullify block triggered %d\n", bbn->block_num);
6402 bb->next_bb = bbn->next_bb;
6404 for (i = 0; i < bbn->out_count; i++)
6405 replace_in_block (bbn->out_bb [i], bbn, NULL);
6407 nullify_basic_block (bbn);
6411 if (bb->out_count == 1) {
6412 bbn = bb->out_bb [0];
6414 /* conditional branches where true and false targets are the same can be also replaced with CEE_BR */
6415 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
6416 bb->last_ins->opcode = CEE_BR;
6417 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
6419 if (cfg->verbose_level > 2)
6420 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
6423 if (bb->region == bbn->region && bb->next_bb == bbn) {
6424 /* the block are in sequence anyway ... */
6426 /* branches to the following block can be removed */
6427 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
6428 bb->last_ins->opcode = CEE_NOP;
6430 if (cfg->verbose_level > 2)
6431 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
6434 if (bbn->in_count == 1) {
6436 if (bbn != cfg->bb_exit) {
6437 if (cfg->verbose_level > 2)
6438 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
6439 merge_basic_blocks (bb, bbn);
6443 //mono_print_bb_code (bb);
6453 /* we skip the entry block (exit is handled specially instead ) */
6454 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
6456 /* dont touch code inside exception clauses */
6457 if (bb->region != -1)
6460 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
6461 if (cfg->verbose_level > 2) {
6462 g_print ("nullify block triggered %d\n", bbn->block_num);
6464 bb->next_bb = bbn->next_bb;
6466 for (i = 0; i < bbn->out_count; i++)
6467 replace_in_block (bbn->out_bb [i], bbn, NULL);
6469 nullify_basic_block (bbn);
6475 if (bb->out_count == 1) {
6476 bbn = bb->out_bb [0];
6478 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
6479 bbn = bb->last_ins->inst_target_bb;
6480 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
6481 bbn->code->inst_target_bb->region == bb->region) {
6483 if (cfg->verbose_level > 2)
6484 g_print ("in %s branch to branch triggered %d -> %d\n", cfg->method->name,
6485 bb->block_num, bbn->block_num);
6487 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
6488 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
6493 } else if (bb->out_count == 2) {
6494 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
6495 bbn = bb->last_ins->inst_true_bb;
6496 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
6497 bbn->code->inst_target_bb->region == bb->region) {
6498 if (cfg->verbose_level > 2)
6499 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
6500 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
6503 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
6505 replace_in_block (bbn, bb, NULL);
6507 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
6508 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
6510 link_bblock (cfg, bb, bbn->code->inst_target_bb);
6516 bbn = bb->last_ins->inst_false_bb;
6517 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
6518 bbn->code->inst_target_bb->region == bb->region) {
6519 if (cfg->verbose_level > 2)
6520 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
6521 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
6524 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
6526 replace_in_block (bbn, bb, NULL);
6528 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
6529 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
6531 link_bblock (cfg, bb, bbn->code->inst_target_bb);
6544 mono_compile_create_vars (MonoCompile *cfg)
6546 MonoMethodSignature *sig;
6547 MonoMethodHeader *header;
6550 header = ((MonoMethodNormal *)cfg->method)->header;
6552 sig = cfg->method->signature;
6554 if (!MONO_TYPE_IS_VOID (sig->ret)) {
6555 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
6556 cfg->ret->opcode = OP_RETARG;
6557 cfg->ret->inst_vtype = sig->ret;
6558 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
6560 if (cfg->verbose_level > 2)
6561 g_print ("creating vars\n");
6564 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
6566 for (i = 0; i < sig->param_count; ++i)
6567 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
6569 cfg->locals_start = cfg->num_varinfo;
6571 if (cfg->verbose_level > 2)
6572 g_print ("creating locals\n");
6573 for (i = 0; i < header->num_locals; ++i)
6574 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
6575 if (cfg->verbose_level > 2)
6576 g_print ("locals done\n");
6580 mono_print_code (MonoCompile *cfg)
6584 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6585 MonoInst *tree = bb->code;
6590 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
6592 for (; tree; tree = tree->next) {
6593 mono_print_tree (tree);
6598 bb->last_ins->next = NULL;
6602 extern const char * const mono_burg_rule_string [];
6605 emit_state (MonoCompile *cfg, MBState *state, int goal)
6608 int ern = mono_burg_rule (state, goal);
6609 const guint16 *nts = mono_burg_nts [ern];
6612 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
6616 // state->reg1 = state->reg2; /* chain rule */
6618 state->reg1 = mono_regstate_next_int (cfg->rs);
6619 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
6622 state->reg1 = mono_regstate_next_int (cfg->rs);
6623 state->reg2 = mono_regstate_next_int (cfg->rs);
6626 state->reg1 = mono_regstate_next_float (cfg->rs);
6633 mono_burg_kids (state, ern, kids);
6635 emit_state (cfg, kids [0], nts [0]);
6637 emit_state (cfg, kids [1], nts [1]);
6639 g_assert (!nts [3]);
6640 emit_state (cfg, kids [2], nts [2]);
6645 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
6646 if ((emit = mono_burg_func [ern]))
6647 emit (state, state->tree, cfg);
6650 #define DEBUG_SELECTION
6653 mini_select_instructions (MonoCompile *cfg)
6655 static int reverse_map [] = {
6656 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
6657 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
6659 static int reverse_fmap [] = {
6660 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
6661 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
6663 static int reverse_lmap [] = {
6664 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
6665 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
6670 cfg->state_pool = mono_mempool_new ();
6671 cfg->rs = mono_regstate_new ();
6673 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6674 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode) &&
6675 bb->next_bb != bb->last_ins->inst_false_bb) {
6677 if (bb->next_bb == bb->last_ins->inst_true_bb) {
6678 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
6679 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
6680 bb->last_ins->inst_false_bb = tmp;
6682 if (bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
6683 bb->last_ins->opcode = reverse_map [bb->last_ins->opcode - CEE_BEQ];
6684 } else if (bb->last_ins->opcode >= OP_FBEQ && bb->last_ins->opcode <= OP_FBLT_UN) {
6685 bb->last_ins->opcode = reverse_fmap [bb->last_ins->opcode - OP_FBEQ];
6686 } else if (bb->last_ins->opcode >= OP_LBEQ && bb->last_ins->opcode <= OP_LBLT_UN) {
6687 bb->last_ins->opcode = reverse_lmap [bb->last_ins->opcode - OP_LBEQ];
6690 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
6691 inst->opcode = CEE_BR;
6692 inst->inst_target_bb = bb->last_ins->inst_false_bb;
6693 mono_bblock_add_inst (bb, inst);
6698 #ifdef DEBUG_SELECTION
6699 if (cfg->verbose_level >= 4) {
6700 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6701 MonoInst *tree = bb->code;
6702 g_print ("DUMP BLOCK %d:\n", bb->block_num);
6705 for (; tree; tree = tree->next) {
6706 mono_print_tree (tree);
6713 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6714 MonoInst *tree = bb->code, *next;
6720 bb->last_ins = NULL;
6723 mono_regstate_reset (cfg->rs);
6725 #ifdef DEBUG_SELECTION
6726 if (cfg->verbose_level >= 3)
6727 g_print ("LABEL BLOCK %d:\n", bb->block_num);
6729 for (; tree; tree = next) {
6731 #ifdef DEBUG_SELECTION
6732 if (cfg->verbose_level >= 3) {
6733 mono_print_tree (tree);
6738 if (!(mbstate = mono_burg_label (tree, cfg))) {
6739 g_warning ("unable to label tree %p", tree);
6740 mono_print_tree (tree);
6742 g_assert_not_reached ();
6744 emit_state (cfg, mbstate, MB_NTERM_stmt);
6746 bb->max_ireg = cfg->rs->next_vireg;
6747 bb->max_freg = cfg->rs->next_vfreg;
6750 bb->last_ins->next = NULL;
6752 mono_mempool_empty (cfg->state_pool);
6754 mono_mempool_destroy (cfg->state_pool);
6758 mono_codegen (MonoCompile *cfg)
6760 MonoJumpInfo *patch_info;
6762 int i, max_epilog_size;
6765 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6766 cfg->spill_count = 0;
6767 /* we reuse dfn here */
6768 /* bb->dfn = bb_count++; */
6769 mono_arch_local_regalloc (cfg, bb);
6772 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
6773 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
6775 code = mono_arch_emit_prolog (cfg);
6777 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6778 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
6780 cfg->code_len = code - cfg->native_code;
6781 cfg->prolog_end = cfg->code_len;
6783 mono_debug_open_method (cfg);
6785 /* emit code all basic blocks */
6786 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6787 bb->native_offset = cfg->code_len;
6788 mono_arch_output_basic_block (cfg, bb);
6790 cfg->bb_exit->native_offset = cfg->code_len;
6792 code = cfg->native_code + cfg->code_len;
6794 max_epilog_size = mono_arch_max_epilog_size (cfg);
6796 /* we always allocate code in cfg->domain->code_mp to increase locality */
6797 cfg->code_size = cfg->code_len + max_epilog_size;
6798 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
6799 code = mono_mempool_alloc (cfg->domain->code_mp, cfg->code_size);
6800 memcpy (code, cfg->native_code, cfg->code_len);
6801 g_free (cfg->native_code);
6802 cfg->native_code = code;
6803 code = cfg->native_code + cfg->code_len;
6805 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
6807 cfg->epilog_begin = cfg->code_len;
6809 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6810 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
6812 cfg->code_len = code - cfg->native_code;
6814 mono_arch_emit_epilog (cfg);
6816 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
6817 switch (patch_info->type) {
6818 case MONO_PATCH_INFO_ABS: {
6819 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
6821 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
6822 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
6823 patch_info->data.name = info->name;
6826 MonoVTable *vtable = mono_find_class_init_trampoline_by_addr (patch_info->data.target);
6828 patch_info->type = MONO_PATCH_INFO_CLASS_INIT;
6829 patch_info->data.klass = vtable->klass;
6834 case MONO_PATCH_INFO_SWITCH: {
6835 gpointer *table = mono_mempool_alloc (cfg->domain->code_mp, sizeof (gpointer) * patch_info->table_size);
6836 patch_info->ip.i = patch_info->ip.label->inst_c0;
6837 for (i = 0; i < patch_info->table_size; i++) {
6838 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
6840 patch_info->data.target = table;
6849 if (cfg->verbose_level > 0)
6850 g_print ("Method %s emitted at %p to %p [%s]\n",
6851 mono_method_full_name (cfg->method, TRUE),
6852 cfg->native_code, cfg->native_code + cfg->code_len, cfg->domain->friendly_name);
6854 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info);
6856 mono_arch_flush_icache (cfg->native_code, cfg->code_len);
6858 mono_debug_close_method (cfg);
6862 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
6867 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
6868 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
6870 if (cp->opcode == OP_ICONST) {
6871 if (cfg->opt & MONO_OPT_CONSPROP) {
6872 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
6876 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
6877 if (cfg->opt & MONO_OPT_COPYPROP) {
6878 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
6884 arity = mono_burg_arity [tree->opcode];
6887 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
6888 if (cfg->opt & MONO_OPT_CFOLD)
6889 mono_constant_fold_inst (tree, NULL);
6891 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
6892 if (cfg->opt & MONO_OPT_CFOLD)
6893 mono_constant_fold_inst (tree, NULL);
6895 mono_constant_fold_inst (tree, NULL);
6901 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
6905 switch (tree->opcode) {
6915 if (tree->ssa_op == MONO_SSA_NOP) {
6916 memset (acp, 0, sizeof (MonoInst *) * acp_size);
6933 case OP_VOIDCALL_REG:
6934 case OP_VOIDCALLVIRT:
6936 MonoCallInst *call = (MonoCallInst *)tree;
6937 MonoMethodSignature *sig = call->signature;
6938 int i, byref = FALSE;
6940 for (i = 0; i < sig->param_count; i++) {
6941 if (sig->params [i]->byref) {
6948 memset (acp, 0, sizeof (MonoInst *) * acp_size);
6956 arity = mono_burg_arity [tree->opcode];
6962 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
6965 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
6966 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
6969 g_assert_not_reached ();
6974 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
6976 MonoInst *tree = bb->code;
6982 for (; tree; tree = tree->next) {
6984 mono_cprop_copy_values (cfg, tree, acp);
6986 mono_cprop_invalidate_values (tree, acp, acp_size);
6988 if (tree->ssa_op == MONO_SSA_STORE &&
6989 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
6990 MonoInst *i1 = tree->inst_i1;
6992 acp [tree->inst_i0->inst_c0] = NULL;
6994 for (i = 0; i < acp_size; i++) {
6995 if (acp [i] && acp [i]->opcode != OP_ICONST &&
6996 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
7001 if (i1->opcode == OP_ICONST) {
7002 acp [tree->inst_i0->inst_c0] = i1;
7003 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
7005 if (i1->ssa_op == MONO_SSA_LOAD &&
7006 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
7007 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
7008 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
7009 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
7014 if (tree->opcode == CEE_BEQ) {
7015 g_assert (tree->inst_i0->opcode == OP_COMPARE);
7016 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
7017 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
7019 tree->opcode = CEE_BR;
7020 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
7021 tree->inst_target_bb = tree->inst_true_bb;
7023 tree->inst_target_bb = tree->inst_false_bb;
7032 mono_local_cprop (MonoCompile *cfg)
7037 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
7039 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7040 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
7041 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
7046 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, int parts)
7048 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
7049 guint8 *ip = (guint8 *)header->code;
7052 int dfn = 0, i, code_size_ratio;
7054 mono_jit_stats.methods_compiled++;
7055 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
7056 mono_profiler_method_jit (method);
7058 cfg = g_new0 (MonoCompile, 1);
7059 cfg->method = method;
7060 cfg->mempool = mono_mempool_new ();
7062 cfg->prof_options = mono_profiler_get_events ();
7063 cfg->bb_hash = g_hash_table_new (NULL, NULL);
7064 cfg->domain = domain;
7065 cfg->verbose_level = mini_verbose;
7066 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
7067 ((MonoMethodNormal *)method)->header->max_stack);
7069 if (cfg->verbose_level > 2)
7070 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
7073 * create MonoInst* which represents arguments and local variables
7075 mono_compile_create_vars (cfg);
7077 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
7078 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7079 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
7080 mono_destroy_compile (cfg);
7084 mono_jit_stats.basic_blocks += cfg->num_bblocks;
7085 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
7087 if (cfg->num_varinfo > 2000) {
7089 * we disable some optimizations if there are too many variables
7090 * because JIT time may become too expensive. The actual number needs
7091 * to be tweaked and eventually the non-linear algorithms should be fixed.
7093 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
7094 cfg->disable_ssa = TRUE;
7096 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
7098 /* Depth-first ordering on basic blocks */
7099 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
7101 if (cfg->opt & MONO_OPT_BRANCH)
7102 optimize_branches (cfg);
7104 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
7105 if (cfg->num_bblocks != dfn + 1) {
7108 cfg->num_bblocks = dfn + 1;
7110 if (!header->clauses) {
7111 /* remove unreachable code, because the code in them may be
7112 * inconsistent (access to dead variables for example) */
7113 for (bb = cfg->bb_entry; bb;) {
7114 MonoBasicBlock *bbn = bb->next_bb;
7116 if (bbn && bbn->region == -1 && !bbn->dfn) {
7117 if (cfg->verbose_level > 1)
7118 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
7119 bb->next_bb = bbn->next_bb;
7120 nullify_basic_block (bbn);
7128 if (cfg->opt & MONO_OPT_LOOP) {
7129 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
7130 mono_compute_natural_loops (cfg);
7133 /* after method_to_ir */
7137 //#define DEBUGSSA "logic_run"
7138 #define DEBUGSSA_CLASS "Tests"
7141 if (!header->num_clauses && !cfg->disable_ssa) {
7142 mono_local_cprop (cfg);
7143 mono_ssa_compute (cfg);
7147 /* fixme: add all optimizations which requires SSA */
7148 if (cfg->opt & (MONO_OPT_DEADCE)) {
7149 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
7150 mono_local_cprop (cfg);
7151 mono_ssa_compute (cfg);
7153 if (cfg->verbose_level >= 2) {
7160 /* after SSA translation */
7164 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
7165 if (cfg->comp_done & MONO_COMP_SSA) {
7166 mono_ssa_cprop (cfg);
7168 mono_local_cprop (cfg);
7172 if (cfg->comp_done & MONO_COMP_SSA) {
7173 mono_ssa_deadce (cfg);
7175 //mono_ssa_strength_reduction (cfg);
7177 mono_ssa_remove (cfg);
7179 if (cfg->opt & MONO_OPT_BRANCH)
7180 optimize_branches (cfg);
7183 /* after SSA removal */
7187 decompose_pass (cfg);
7189 /* FIXME: disabled with exception clauses: bug #42136 */
7190 if ((!header->num_clauses) && (cfg->opt & MONO_OPT_LINEARS)) {
7193 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
7194 cfg->comp_done &= ~MONO_COMP_LIVENESS;
7195 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
7196 mono_analyze_liveness (cfg);
7198 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
7199 regs = mono_arch_get_global_int_regs (cfg);
7200 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
7204 //mono_print_code (cfg);
7208 /* variables are allocated after decompose, since decompose could create temps */
7209 mono_arch_allocate_vars (cfg);
7211 if (cfg->opt & MONO_OPT_CFOLD)
7212 mono_constant_fold (cfg);
7214 mini_select_instructions (cfg);
7217 if (cfg->verbose_level >= 2) {
7218 char *id = mono_method_full_name (cfg->method, FALSE);
7219 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
7223 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
7225 jinfo = g_new0 (MonoJitInfo, 1);
7226 jinfo->method = method;
7227 jinfo->code_start = cfg->native_code;
7228 jinfo->code_size = cfg->code_len;
7229 jinfo->used_regs = cfg->used_int_regs;
7230 jinfo->domain_neutral = (cfg->opt & MONO_OPT_SHARED) != 0;
7232 if (header->num_clauses) {
7235 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
7236 jinfo->num_clauses = header->num_clauses;
7237 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
7238 sizeof (MonoJitExceptionInfo) * header->num_clauses);
7240 for (i = 0; i < header->num_clauses; i++) {
7241 MonoExceptionClause *ec = &header->clauses [i];
7242 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
7243 MonoBasicBlock *tblock;
7245 ei->flags = ec->flags;
7247 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7248 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->token_or_filter);
7250 ei->data.filter = cfg->native_code + tblock->native_offset;
7252 ei->data.token = ec->token_or_filter;
7255 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
7257 ei->try_start = cfg->native_code + tblock->native_offset;
7258 g_assert (tblock->native_offset);
7259 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
7261 ei->try_end = cfg->native_code + tblock->native_offset;
7262 g_assert (tblock->native_offset);
7263 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
7265 ei->handler_start = cfg->native_code + tblock->native_offset;
7269 cfg->jit_info = jinfo;
7271 mono_jit_info_table_add (cfg->domain, jinfo);
7273 /* collect statistics */
7274 mono_jit_stats.allocated_code_size += cfg->code_len;
7275 code_size_ratio = cfg->code_len;
7276 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
7277 mono_jit_stats.biggest_method_size = code_size_ratio;
7278 mono_jit_stats.biggest_method = method;
7280 code_size_ratio = (code_size_ratio * 100) / ((MonoMethodNormal *)method)->header->code_size;
7281 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
7282 mono_jit_stats.max_code_size_ratio = code_size_ratio;
7283 mono_jit_stats.max_ratio_method = method;
7285 mono_jit_stats.native_code_size += cfg->code_len;
7287 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7288 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
7294 mono_jit_compile_method_inner (MonoMethod *method)
7296 /* FIXME: later copy the code from mono */
7297 MonoDomain *target_domain, *domain = mono_domain_get ();
7299 GHashTable *jit_code_hash;
7305 if (opt & MONO_OPT_SHARED)
7306 target_domain = mono_root_domain;
7308 target_domain = domain;
7310 jit_code_hash = target_domain->jit_code_hash;
7312 #ifdef MONO_USE_AOT_COMPILER
7313 if (!mono_compile_aot && (opt & MONO_OPT_AOT)) {
7316 mono_domain_lock (domain);
7318 mono_class_init (method->klass);
7319 if ((info = mono_aot_get_method (domain, method))) {
7321 g_hash_table_insert (domain->jit_code_hash, method, info);
7323 mono_domain_unlock (domain);
7325 /* make sure runtime_init is called */
7326 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
7328 return info->code_start;
7331 mono_domain_unlock (domain);
7335 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
7336 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
7339 if (!method->addr) {
7340 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
7341 method->addr = mono_lookup_internal_call (method);
7343 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
7344 mono_lookup_pinvoke_call (method);
7346 #ifdef MONO_USE_EXC_TABLES
7347 if (mono_method_blittable (method)) {
7348 return method->addr;
7351 nm = mono_marshal_get_native_wrapper (method);
7352 return mono_compile_method (nm);
7354 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
7355 //mono_debug_add_wrapper (method, nm);
7356 #ifdef MONO_USE_EXC_TABLES
7359 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
7360 const char *name = method->name;
7363 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
7364 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
7365 /* FIXME: uhm, we need a wrapper to handle exceptions? */
7366 return (gpointer)mono_delegate_ctor;
7367 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
7368 nm = mono_marshal_get_delegate_invoke (method);
7369 return mono_jit_compile_method (nm);
7370 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
7371 nm = mono_marshal_get_delegate_begin_invoke (method);
7372 return mono_jit_compile_method (nm);
7373 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
7374 nm = mono_marshal_get_delegate_end_invoke (method);
7375 return mono_jit_compile_method (nm);
7381 cfg = mini_method_compile (method, opt, target_domain, 0);
7382 code = cfg->native_code;
7384 g_hash_table_insert (jit_code_hash, method, cfg->jit_info);
7386 mono_destroy_compile (cfg);
7388 if (target_domain->jump_target_hash) {
7389 MonoJumpInfo patch_info;
7391 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
7393 patch_info.next = NULL;
7394 patch_info.ip.i = 0;
7395 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
7396 patch_info.data.method = method;
7397 g_hash_table_remove (target_domain->jump_target_hash, method);
7399 for (tmp = list; tmp; tmp = tmp->next)
7400 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info);
7401 g_slist_free (list);
7403 /* make sure runtime_init is called */
7404 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
7410 mono_jit_compile_method (MonoMethod *method)
7412 /* FIXME: later copy the code from mono */
7413 MonoDomain *target_domain, *domain = mono_domain_get ();
7417 if (default_opt & MONO_OPT_SHARED)
7418 target_domain = mono_root_domain;
7420 target_domain = domain;
7422 mono_domain_lock (target_domain);
7424 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
7425 /* We can't use a domain specific method in another domain */
7426 if (! ((domain != target_domain) && !info->domain_neutral)) {
7427 mono_domain_unlock (target_domain);
7428 mono_jit_stats.methods_lookups++;
7429 return info->code_start;
7433 code = mono_jit_compile_method_inner (method);
7435 mono_domain_unlock (target_domain);
7441 * mono_jit_runtime_invoke:
7442 * @method: the method to invoke
7443 * @obj: this pointer
7444 * @params: array of parameter values.
7445 * @exc: used to catch exceptions objects
7448 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
7451 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc);
7453 if (obj == NULL && !(method->flags & METHOD_ATTRIBUTE_STATIC) && !method->string_ctor && (method->wrapper_type == 0)) {
7454 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
7458 invoke = mono_marshal_get_runtime_invoke (method);
7459 runtime_invoke = mono_jit_compile_method (invoke);
7460 return runtime_invoke (obj, params, exc);
7463 #ifdef PLATFORM_WIN32
7464 #define GET_CONTEXT \
7465 struct sigcontext *ctx = (struct sigcontext*)_dummy;
7467 #define GET_CONTEXT \
7468 void **_p = (void **)&_dummy; \
7469 struct sigcontext *ctx = (struct sigcontext *)++_p;
7473 sigfpe_signal_handler (int _dummy)
7478 exc = mono_get_exception_divide_by_zero ();
7480 mono_arch_handle_exception (ctx, exc, FALSE);
7484 sigill_signal_handler (int _dummy)
7488 exc = mono_get_exception_execution_engine ("SIGILL");
7490 mono_arch_handle_exception (ctx, exc, FALSE);
7494 sigsegv_signal_handler (int _dummy)
7499 exc = mono_get_exception_null_reference ();
7501 mono_arch_handle_exception (ctx, exc, FALSE);
7505 sigusr1_signal_handler (int _dummy)
7510 thread = mono_thread_current ();
7512 thread->abort_exc = mono_get_exception_thread_abort ();
7514 mono_arch_handle_exception (ctx, thread->abort_exc, FALSE);
7518 sigquit_signal_handler (int _dummy)
7523 exc = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
7525 mono_arch_handle_exception (ctx, exc, FALSE);
7529 sigint_signal_handler (int _dummy)
7534 exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
7536 mono_arch_handle_exception (ctx, exc, FALSE);
7540 mono_runtime_install_handlers (void)
7542 gboolean skip_sigabort = FALSE;
7543 #ifndef PLATFORM_WIN32
7544 struct sigaction sa;
7547 #ifdef PLATFORM_WIN32
7549 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
7550 win32_seh_set_handler(SIGILL, sigill_signal_handler);
7551 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
7552 if (getenv ("MONO_DEBUG"))
7553 win32_seh_set_handler(SIGINT, sigint_signal_handler);
7554 #else /* !PLATFORM_WIN32 */
7556 /* libpthreads has its own implementation of sigaction(),
7557 * but it seems to work well with our current exception
7558 * handlers. If not we must call syscall directly instead
7561 if (getenv ("MONO_DEBUG")) {
7563 sa.sa_handler = sigint_signal_handler;
7564 sigemptyset (&sa.sa_mask);
7566 //g_assert (syscall (SYS_sigaction, SIGINT, &sa, NULL) != -1);
7567 g_assert (sigaction (SIGINT, &sa, NULL) != -1);
7571 sa.sa_handler = sigfpe_signal_handler;
7572 sigemptyset (&sa.sa_mask);
7574 //g_assert (syscall (SYS_sigaction, SIGFPE, &sa, NULL) != -1);
7575 g_assert (sigaction (SIGFPE, &sa, NULL) != -1);
7578 sa.sa_handler = sigquit_signal_handler;
7579 sigemptyset (&sa.sa_mask);
7581 g_assert (sigaction (SIGQUIT, &sa, NULL) != -1);
7584 sa.sa_handler = sigill_signal_handler;
7585 sigemptyset (&sa.sa_mask);
7587 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
7588 g_assert (sigaction (SIGILL, &sa, NULL) != -1);
7590 /* catch thread abort signal */
7591 sa.sa_handler = sigusr1_signal_handler;
7592 sigemptyset (&sa.sa_mask);
7594 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
7596 #ifdef HAVE_VALGRIND_MEMCHECK_H
7597 if (RUNNING_ON_VALGRIND)
7598 /* valgrind 20030725 and earlier aborts on this call so we skip it */
7599 skip_sigabort = TRUE;
7603 g_assert (sigaction (mono_thread_get_abort_signal (), &sa, NULL) != -1);
7607 sa.sa_handler = sigsegv_signal_handler;
7608 sigemptyset (&sa.sa_mask);
7610 //g_assert (syscall (SYS_sigaction, SIGSEGV, &sa, NULL) != -1);
7611 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
7613 #endif /* PLATFORM_WIN32 */
7616 /* mono_jit_create_remoting_trampoline:
7617 * @method: pointer to the method info
7619 * Creates a trampoline which calls the remoting functions. This
7620 * is used in the vtable of transparent proxies.
7622 * Returns: a pointer to the newly created code
7625 mono_jit_create_remoting_trampoline (MonoMethod *method)
7628 guint8 *addr = NULL;
7630 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
7631 (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
7632 nm = mono_marshal_get_remoting_invoke (method);
7633 addr = mono_compile_method (nm);
7635 addr = mono_compile_method (method);
7641 mini_init (const char *filename)
7645 mono_arch_cpu_init ();
7647 g_thread_init (NULL);
7649 mono_jit_tls_id = TlsAlloc ();
7650 setup_jit_tls_data ((gpointer)-1, mono_thread_abort);
7652 InitializeCriticalSection (&class_init_hash_mutex);
7656 if (default_opt & MONO_OPT_AOT)
7659 mono_runtime_install_handlers ();
7660 mono_threads_install_cleanup (mini_thread_cleanup);
7662 #define JIT_TRAMPOLINES_WORK
7663 #ifdef JIT_TRAMPOLINES_WORK
7664 mono_install_compile_method (mono_jit_compile_method);
7665 mono_install_trampoline (mono_arch_create_jit_trampoline);
7666 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
7668 #define JIT_INVOKE_WORKS
7669 #ifdef JIT_INVOKE_WORKS
7670 mono_install_runtime_invoke (mono_jit_runtime_invoke);
7671 mono_install_handler (mono_arch_get_throw_exception ());
7673 mono_install_stack_walk (mono_jit_walk_stack);
7674 mono_install_get_config_dir ();
7676 domain = mono_init (filename);
7679 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
7680 ves_icall_get_frame_info);
7681 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
7682 ves_icall_get_trace);
7683 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
7684 mono_runtime_install_handlers);
7687 create_helper_signature ();
7689 #define JIT_CALLS_WORK
7690 #ifdef JIT_CALLS_WORK
7691 /* Needs to be called here since register_jit_icall depends on it */
7692 mono_marshal_init ();
7694 mono_arch_register_lowlevel_calls ();
7695 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
7696 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
7698 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
7699 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
7701 /* fixme: we cant hanlde vararg methods this way, because the signature is not constant */
7702 //mono_register_jit_icall (ves_array_element_address, "ves_array_element_address", NULL);
7703 //mono_register_jit_icall (mono_array_new_va, "mono_array_new_va", NULL);
7705 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
7706 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
7707 helper_sig_void_ptr, TRUE);
7708 mono_register_jit_icall (mono_thread_get_pending_exception, "mono_thread_get_pending_exception", helper_sig_obj_void, FALSE);
7711 * NOTE, NOTE, NOTE, NOTE:
7712 * when adding emulation for some opcodes, remember to also add a dummy
7713 * rule to the burg files, because we need the arity information to be correct.
7715 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult, TRUE);
7716 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un, FALSE);
7717 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf, FALSE);
7718 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv, FALSE);
7719 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un, FALSE);
7720 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem, FALSE);
7721 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un, FALSE);
7723 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl, TRUE);
7724 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr, TRUE);
7725 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un, TRUE);
7727 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8, FALSE);
7728 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4, FALSE);
7729 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8, TRUE);
7730 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8, TRUE);
7732 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
7733 mono_register_opcode_emulation (OP_FCONV_TO_I8, "__emul_fconv_to_i8", helper_sig_long_double, mono_fconv_i8, FALSE);
7735 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
7736 mono_register_opcode_emulation (OP_LCONV_TO_R8, "__emul_lconv_to_r8", helper_sig_double_long, mono_lconv_to_r8, FALSE);
7738 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
7739 mono_register_opcode_emulation (OP_LCONV_TO_R4, "__emul_lconv_to_r4", helper_sig_float_long, mono_lconv_to_r4, FALSE);
7741 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
7742 mono_register_opcode_emulation (OP_LCONV_TO_R_UN, "__emul_lconv_to_r8_un", helper_sig_double_long, mono_lconv_to_r8_un, FALSE);
7744 #ifdef MONO_ARCH_EMULATE_FREM
7745 mono_register_opcode_emulation (OP_FREM, "__emul_frem", helper_sig_double_double_double, fmod, FALSE);
7748 #if SIZEOF_VOID_P == 4
7749 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4, TRUE);
7751 #warning "fixme: add opcode emulation"
7754 /* other jit icalls */
7755 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
7756 helper_sig_ptr_ptr_ptr, FALSE);
7757 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
7758 mono_register_jit_icall (mono_get_special_static_data, "mono_get_special_static_data", helper_sig_ptr_int, FALSE);
7759 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
7760 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
7761 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
7762 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
7763 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
7764 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
7765 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
7766 mono_register_jit_icall (mono_object_new_fast, "mono_object_new_fast", helper_sig_object_new_specific, FALSE);
7767 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
7768 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
7769 mono_register_jit_icall (mono_string_to_utf16, "mono_string_to_utf16", helper_sig_ptr_obj, FALSE);
7770 mono_register_jit_icall (mono_string_from_utf16, "mono_string_from_utf16", helper_sig_obj_ptr, FALSE);
7771 mono_register_jit_icall (mono_string_new_wrapper, "mono_string_new_wrapper", helper_sig_obj_ptr, FALSE);
7772 mono_register_jit_icall (mono_string_to_utf8, "mono_string_to_utf8", helper_sig_ptr_obj, FALSE);
7773 mono_register_jit_icall (mono_string_to_bstr, "mono_string_to_bstr", helper_sig_ptr_obj, FALSE);
7774 mono_register_jit_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", helper_sig_ptr_obj, FALSE);
7775 mono_register_jit_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", helper_sig_ptr_obj, FALSE);
7776 mono_register_jit_icall (mono_array_to_savearray, "mono_array_to_savearray", helper_sig_ptr_obj, FALSE);
7777 mono_register_jit_icall (mono_array_to_lparray, "mono_array_to_lparray", helper_sig_ptr_obj, FALSE);
7778 mono_register_jit_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", helper_sig_ptr_obj, FALSE);
7779 mono_register_jit_icall (mono_marshal_string_array, "mono_marshal_string_array", helper_sig_ptr_obj, FALSE);
7780 mono_register_jit_icall (mono_string_utf8_to_builder, "mono_string_utf8_to_builder", helper_sig_void_ptr_ptr, FALSE);
7781 mono_register_jit_icall (mono_marshal_free_array, "mono_marshal_free_array", helper_sig_void_ptr_ptr, FALSE);
7782 mono_register_jit_icall (mono_string_to_byvalstr, "mono_string_to_byvalstr", helper_sig_void_ptr_ptr_ptr, FALSE);
7783 mono_register_jit_icall (mono_string_to_byvalwstr, "mono_string_to_byvalwstr", helper_sig_void_ptr_ptr_ptr, FALSE);
7784 mono_register_jit_icall (g_free, "g_free", helper_sig_void_ptr, FALSE);
7785 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
7786 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
7787 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
7790 #define JIT_RUNTIME_WORKS
7791 #ifdef JIT_RUNTIME_WORKS
7792 mono_runtime_install_cleanup ((MonoDomainFunc)mini_cleanup);
7793 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
7796 //mono_thread_attach (domain);
7800 MonoJitStats mono_jit_stats = {0};
7803 print_jit_stats (void)
7805 if (mono_jit_stats.enabled) {
7806 g_print ("Mono Jit statistics\n");
7807 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
7808 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
7809 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
7810 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
7811 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
7812 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
7813 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
7814 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
7815 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
7816 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
7817 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
7818 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
7819 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
7820 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
7821 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
7822 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
7823 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
7824 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
7826 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
7827 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
7828 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
7829 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
7830 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
7835 mini_cleanup (MonoDomain *domain)
7838 * mono_runtime_cleanup() and mono_domain_finalize () need to
7839 * be called early since they need the execution engine still
7840 * fully working (mono_domain_finalize may invoke managed finalizers
7841 * and mono_runtime_cleanup will wait for other threads to finish).
7843 mono_domain_finalize (domain, -1);
7845 mono_runtime_cleanup (domain);
7847 mono_profiler_shutdown ();
7849 mono_debug_cleanup ();
7851 #ifdef PLATFORM_WIN32
7852 win32_seh_cleanup();
7855 mono_domain_free (domain, TRUE);
7861 mono_set_defaults (int verbose_level, guint32 opts)
7863 mini_verbose = verbose_level;
7868 mono_precompile_assembly (MonoAssembly *ass, void *user_data)
7870 MonoImage *image = ass->image;
7874 if (mini_verbose > 0)
7875 printf ("PRECOMPILE: %s.\n", ass->image->name);
7877 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
7878 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
7879 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
7881 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
7885 if (mini_verbose > 1) {
7886 char * desc = mono_method_full_name (method, TRUE);
7887 g_print ("Compiling %d %s\n", count, desc);
7890 mono_compile_method (method);
7894 void mono_precompile_assemblies ()
7896 mono_assembly_foreach ((GFunc)mono_precompile_assembly, NULL);