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.
15 #include <mono/metadata/assembly.h>
16 #include <mono/metadata/loader.h>
17 #include <mono/metadata/cil-coff.h>
18 #include <mono/metadata/tabledefs.h>
19 #include <mono/metadata/class.h>
20 #include <mono/metadata/object.h>
21 #include <mono/metadata/exception.h>
22 #include <mono/metadata/opcodes.h>
23 #include <mono/metadata/mono-endian.h>
24 #include <mono/metadata/tokentype.h>
25 #include <mono/metadata/tabledefs.h>
26 #include <mono/metadata/threads.h>
27 #include <mono/metadata/marshal.h>
28 #include <mono/metadata/socket-io.h>
29 #include <mono/metadata/appdomain.h>
30 #include <mono/metadata/debug-helpers.h>
31 #include <mono/io-layer/io-layer.h>
32 #include "mono/metadata/profiler.h"
33 #include <mono/metadata/profiler-private.h>
34 #include <mono/metadata/mono-config.h>
35 #include <mono/metadata/environment.h>
36 #include <mono/metadata/mono-debug.h>
37 #include <mono/metadata/mono-debug-debugger.h>
44 #include "jit-icalls.c"
46 #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))
48 #define MONO_CHECK_THIS(ins) (cfg->method->signature->hasthis && (ins)->ssa_op == MONO_SSA_LOAD && (ins)->inst_left->inst_c0 == 0)
50 gboolean mono_arch_handle_exception (struct sigcontext *ctx, gpointer obj, gboolean test_only);
51 static gpointer mono_jit_compile_method (MonoMethod *method);
53 static void handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src,
54 const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native);
56 static int mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
57 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
58 guint inline_offset, gboolean is_virtual_call);
60 extern guint8 mono_burg_arity [];
61 /* helper methods signature */
62 static MonoMethodSignature *helper_sig_long_long_long = NULL;
63 static MonoMethodSignature *helper_sig_long_long_int = NULL;
64 static MonoMethodSignature *helper_sig_newarr = NULL;
65 static MonoMethodSignature *helper_sig_newarr_specific = NULL;
66 static MonoMethodSignature *helper_sig_ldstr = NULL;
67 static MonoMethodSignature *helper_sig_domain_get = NULL;
68 static MonoMethodSignature *helper_sig_object_new = NULL;
69 static MonoMethodSignature *helper_sig_object_new_specific = NULL;
70 static MonoMethodSignature *helper_sig_compile = NULL;
71 static MonoMethodSignature *helper_sig_compile_virt = NULL;
72 static MonoMethodSignature *helper_sig_obj_ptr = NULL;
73 static MonoMethodSignature *helper_sig_ptr_void = NULL;
74 static MonoMethodSignature *helper_sig_void_ptr = NULL;
75 static MonoMethodSignature *helper_sig_void_obj = NULL;
76 static MonoMethodSignature *helper_sig_void_ptr_ptr = NULL;
77 static MonoMethodSignature *helper_sig_void_ptr_ptr_ptr = NULL;
78 static MonoMethodSignature *helper_sig_ptr_ptr_ptr = NULL;
79 static MonoMethodSignature *helper_sig_ptr_obj = NULL;
80 static MonoMethodSignature *helper_sig_ptr_int = NULL;
81 static MonoMethodSignature *helper_sig_initobj = NULL;
82 static MonoMethodSignature *helper_sig_memcpy = NULL;
83 static MonoMethodSignature *helper_sig_memset = NULL;
84 static MonoMethodSignature *helper_sig_ulong_double = NULL;
85 static MonoMethodSignature *helper_sig_long_double = NULL;
86 static MonoMethodSignature *helper_sig_uint_double = NULL;
87 static MonoMethodSignature *helper_sig_int_double = NULL;
88 static MonoMethodSignature *helper_sig_stelem_ref = NULL;
89 static MonoMethodSignature *helper_sig_class_init_trampoline = NULL;
91 static guint32 default_opt = MONO_OPT_PEEPHOLE;
93 guint32 mono_jit_tls_id = -1;
94 gboolean mono_jit_trace_calls = FALSE;
95 gboolean mono_break_on_exc = FALSE;
96 gboolean mono_compile_aot = FALSE;
97 gboolean mono_no_aot = FALSE;
99 static int mini_verbose = 0;
101 #ifdef MONO_USE_EXC_TABLES
103 mono_type_blittable (MonoType *type)
123 case MONO_TYPE_VALUETYPE:
124 case MONO_TYPE_CLASS:
125 case MONO_TYPE_OBJECT:
126 return type->data.klass->blittable;
136 mono_method_blittable (MonoMethod *method)
138 MonoMethodSignature *sig;
144 if (!mono_arch_has_unwind_info (method->addr)) {
148 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
151 sig = method->signature;
153 if (!mono_type_blittable (sig->ret))
156 for (i = 0; i < sig->param_count; i++)
157 if (!mono_type_blittable (sig->params [i]))
160 if (mono_method_has_marshal_info (method))
168 G_GNUC_UNUSED static void
169 print_method_from_ip (void *ip)
174 MonoDomain *domain = mono_domain_get ();
176 ji = mono_jit_info_table_find (domain, ip);
178 g_print ("No method at %p\n", ip);
181 method = mono_method_full_name (ji->method, TRUE);
182 source = mono_debug_source_location_from_address (ji->method, (int) ip, NULL, domain);
184 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);
187 g_print ("%s\n", source);
194 #define MONO_INIT_VARINFO(vi,id) do { \
195 (vi)->range.first_use.pos.bid = 0xffff; \
201 * Basic blocks have two numeric identifiers:
202 * dfn: Depth First Number
203 * block_num: unique ID assigned at bblock creation
205 #define NEW_BBLOCK(cfg) (mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)))
206 #define ADD_BBLOCK(cfg,bbhash,b) do { \
207 g_hash_table_insert (bbhash, (b)->cil_code, (b)); \
208 (b)->block_num = cfg->num_bblocks++; \
209 (b)->real_offset = real_offset; \
212 #define GET_BBLOCK(cfg,bbhash,tblock,ip) do { \
213 (tblock) = g_hash_table_lookup (bbhash, (ip)); \
215 if ((ip) >= end || (ip) < header->code) goto unverified; \
216 (tblock) = NEW_BBLOCK (cfg); \
217 (tblock)->cil_code = (ip); \
218 ADD_BBLOCK (cfg, (bbhash), (tblock)); \
220 (tblock)->real_offset = real_offset; \
223 #define CHECK_BBLOCK(target,ip,tblock) do { \
224 if ((target) < (ip) && !(tblock)->code) { \
225 bb_recheck = g_list_prepend (bb_recheck, (tblock)); \
226 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); \
230 #define NEW_ICONST(cfg,dest,val) do { \
231 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
232 (dest)->opcode = OP_ICONST; \
233 (dest)->inst_c0 = (val); \
234 (dest)->type = STACK_I4; \
237 /* FIXME: have a different definition of NEW_PCONST for 64 bit systems */
238 #define NEW_PCONST(cfg,dest,val) do { \
239 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
240 (dest)->opcode = OP_ICONST; \
241 (dest)->inst_p0 = (val); \
242 (dest)->type = STACK_PTR; \
245 #define NEW_CLASSCONST(cfg,dest,val) do { \
246 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
247 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
248 (dest)->inst_p0 = (val); \
249 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_CLASS; \
250 (dest)->type = STACK_PTR; \
253 #define NEW_IMAGECONST(cfg,dest,val) do { \
254 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
255 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
256 (dest)->inst_p0 = (val); \
257 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_IMAGE; \
258 (dest)->type = STACK_PTR; \
261 #define NEW_FIELDCONST(cfg,dest,field) do { \
262 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
263 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
264 (dest)->inst_p0 = (field); \
265 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_FIELD; \
266 (dest)->type = STACK_PTR; \
269 #define NEW_METHODCONST(cfg,dest,val) do { \
270 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
271 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
272 (dest)->inst_p0 = (val); \
273 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_METHODCONST; \
274 (dest)->type = STACK_PTR; \
277 #define NEW_VTABLECONST(cfg,dest,vtable) do { \
278 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
279 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
280 (dest)->inst_p0 = mono_compile_aot ? (gpointer)((vtable)->klass) : (vtable); \
281 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_VTABLE; \
282 (dest)->type = STACK_PTR; \
285 #define NEW_SFLDACONST(cfg,dest,field) do { \
286 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
287 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
288 (dest)->inst_p0 = (field); \
289 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_SFLDA; \
290 (dest)->type = STACK_PTR; \
293 #define NEW_LDSTRCONST(cfg,dest,token) do { \
294 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
295 (dest)->opcode = OP_AOTCONST; \
296 (dest)->inst_p0 = (gpointer)(token); \
297 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_LDSTR; \
298 (dest)->type = STACK_OBJ; \
301 #define NEW_TYPE_FROM_HANDLE_CONST(cfg,dest,token) do { \
302 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
303 (dest)->opcode = OP_AOTCONST; \
304 (dest)->inst_p0 = (gpointer)(token); \
305 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_TYPE_FROM_HANDLE; \
306 (dest)->type = STACK_OBJ; \
309 #define NEW_LDTOKENCONST(cfg,dest,token) do { \
310 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
311 (dest)->opcode = OP_AOTCONST; \
312 (dest)->inst_p0 = (gpointer)(token); \
313 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_LDTOKEN; \
314 (dest)->type = STACK_PTR; \
317 #define NEW_DOMAINCONST(cfg,dest) do { \
318 if (cfg->opt & MONO_OPT_SHARED) { \
319 NEW_TEMPLOAD (cfg, dest, mono_get_domainvar (cfg)->inst_c0); \
321 NEW_PCONST (cfg, dest, (cfg)->domain); \
325 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
327 #define NEW_ARGLOAD(cfg,dest,num) do { \
328 if (arg_array [(num)]->opcode == OP_ICONST) (dest) = arg_array [(num)]; else { \
329 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
330 (dest)->ssa_op = MONO_SSA_LOAD; \
331 (dest)->inst_i0 = arg_array [(num)]; \
332 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
333 type_to_eval_stack_type (param_types [(num)], (dest)); \
334 (dest)->klass = (dest)->inst_i0->klass; \
337 #define NEW_LOCLOAD(cfg,dest,num) do { \
338 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
339 (dest)->ssa_op = MONO_SSA_LOAD; \
340 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
341 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
342 type_to_eval_stack_type (header->locals [(num)], (dest)); \
343 (dest)->klass = (dest)->inst_i0->klass; \
346 #define NEW_LOCLOADA(cfg,dest,num) do { \
347 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
348 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
349 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
350 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
351 (dest)->opcode = OP_LDADDR; \
352 (dest)->type = STACK_MP; \
353 (dest)->klass = (dest)->inst_i0->klass; \
354 (cfg)->disable_ssa = TRUE; \
357 #define NEW_RETLOADA(cfg,dest) do { \
358 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
359 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
360 (dest)->inst_i0 = (cfg)->ret; \
361 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
362 (dest)->opcode = CEE_LDIND_I; \
363 (dest)->type = STACK_MP; \
364 (dest)->klass = (dest)->inst_i0->klass; \
365 (cfg)->disable_ssa = TRUE; \
368 #define NEW_ARGLOADA(cfg,dest,num) do { \
369 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
370 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
371 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
372 (dest)->inst_i0 = arg_array [(num)]; \
373 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
374 (dest)->opcode = OP_LDADDR; \
375 (dest)->type = STACK_MP; \
376 (dest)->klass = (dest)->inst_i0->klass; \
377 (cfg)->disable_ssa = TRUE; \
380 #define NEW_TEMPLOAD(cfg,dest,num) do { \
381 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
382 (dest)->ssa_op = MONO_SSA_LOAD; \
383 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
384 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
385 type_to_eval_stack_type ((dest)->inst_i0->inst_vtype, (dest)); \
386 (dest)->klass = (dest)->inst_i0->klass; \
389 #define NEW_TEMPLOADA(cfg,dest,num) do { \
390 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
391 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
392 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
393 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
394 (dest)->opcode = OP_LDADDR; \
395 (dest)->type = STACK_MP; \
396 (dest)->klass = (dest)->inst_i0->klass; \
397 (cfg)->disable_ssa = TRUE; \
401 #define NEW_INDLOAD(cfg,dest,addr,vtype) do { \
402 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
403 (dest)->inst_left = addr; \
404 (dest)->opcode = mono_type_to_ldind (vtype); \
405 type_to_eval_stack_type (vtype, (dest)); \
406 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
409 #define NEW_INDSTORE(cfg,dest,addr,value,vtype) do { \
410 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
411 (dest)->inst_i0 = addr; \
412 (dest)->opcode = mono_type_to_stind (vtype); \
413 (dest)->inst_i1 = (value); \
414 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
417 #define NEW_TEMPSTORE(cfg,dest,num,inst) do { \
418 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
419 (dest)->ssa_op = MONO_SSA_STORE; \
420 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
421 (dest)->opcode = mono_type_to_stind ((dest)->inst_i0->inst_vtype); \
422 (dest)->inst_i1 = (inst); \
423 (dest)->klass = (dest)->inst_i0->klass; \
426 #define NEW_LOCSTORE(cfg,dest,num,inst) do { \
427 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
428 (dest)->opcode = mono_type_to_stind (header->locals [(num)]); \
429 (dest)->ssa_op = MONO_SSA_STORE; \
430 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
431 (dest)->inst_i1 = (inst); \
432 (dest)->klass = (dest)->inst_i0->klass; \
435 #define NEW_ARGSTORE(cfg,dest,num,inst) do { \
436 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
437 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
438 (dest)->opcode = mono_type_to_stind (param_types [(num)]); \
439 (dest)->ssa_op = MONO_SSA_STORE; \
440 (dest)->inst_i0 = arg_array [(num)]; \
441 (dest)->inst_i1 = (inst); \
442 (dest)->klass = (dest)->inst_i0->klass; \
445 #define ADD_BINOP(op) do { \
446 MONO_INST_NEW (cfg, ins, (op)); \
447 ins->cil_code = ip; \
449 ins->inst_i0 = sp [0]; \
450 ins->inst_i1 = sp [1]; \
452 type_from_op (ins); \
456 #define ADD_UNOP(op) do { \
457 MONO_INST_NEW (cfg, ins, (op)); \
458 ins->cil_code = ip; \
460 ins->inst_i0 = sp [0]; \
462 type_from_op (ins); \
466 #define ADD_BINCOND(next_block) do { \
468 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
470 cmp->inst_i0 = sp [0]; \
471 cmp->inst_i1 = sp [1]; \
472 cmp->cil_code = ins->cil_code; \
473 type_from_op (cmp); \
475 ins->inst_i0 = cmp; \
476 MONO_ADD_INS (bblock, ins); \
477 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
478 GET_BBLOCK (cfg, bbhash, tblock, target); \
479 link_bblock (cfg, bblock, tblock); \
480 ins->inst_true_bb = tblock; \
481 CHECK_BBLOCK (target, ip, tblock); \
482 if ((next_block)) { \
483 link_bblock (cfg, bblock, (next_block)); \
484 ins->inst_false_bb = (next_block); \
485 start_new_bblock = 1; \
487 GET_BBLOCK (cfg, bbhash, tblock, ip); \
488 link_bblock (cfg, bblock, tblock); \
489 ins->inst_false_bb = tblock; \
490 start_new_bblock = 2; \
494 /* FIXME: handle float, long ... */
495 #define ADD_UNCOND(istrue) do { \
497 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
499 cmp->inst_i0 = sp [0]; \
500 switch (cmp->inst_i0->type) { \
502 cmp->inst_i1 = zero_int64; break; \
504 cmp->inst_i1 = zero_r8; break; \
507 cmp->inst_i1 = zero_ptr; break; \
509 cmp->inst_i1 = zero_obj; break; \
511 cmp->inst_i1 = zero_int32; \
513 cmp->cil_code = ins->cil_code; \
514 type_from_op (cmp); \
516 ins->inst_i0 = cmp; \
517 ins->opcode = (istrue)? CEE_BNE_UN: CEE_BEQ; \
518 MONO_ADD_INS (bblock, ins); \
519 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
520 GET_BBLOCK (cfg, bbhash, tblock, target); \
521 link_bblock (cfg, bblock, tblock); \
522 ins->inst_true_bb = tblock; \
523 CHECK_BBLOCK (target, ip, tblock); \
524 GET_BBLOCK (cfg, bbhash, tblock, ip); \
525 link_bblock (cfg, bblock, tblock); \
526 ins->inst_false_bb = tblock; \
527 start_new_bblock = 2; \
530 #define NEW_LDELEMA(cfg,dest,sp,k) do { \
531 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
532 (dest)->opcode = CEE_LDELEMA; \
533 (dest)->inst_left = (sp) [0]; \
534 (dest)->inst_right = (sp) [1]; \
535 (dest)->type = STACK_MP; \
536 (dest)->klass = (k); \
539 #define NEW_GROUP(cfg,dest,el1,el2) do { \
540 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
541 (dest)->opcode = OP_GROUP; \
542 (dest)->inst_left = (el1); \
543 (dest)->inst_right = (el2); \
548 compare_bblock (gconstpointer a, gconstpointer b)
550 const MonoBasicBlock *b1 = a;
551 const MonoBasicBlock *b2 = b;
553 return b2->cil_code - b1->cil_code;
558 * link_bblock: Links two basic blocks
560 * links two basic blocks in the control flow graph, the 'from'
561 * argument is the starting block and the 'to' argument is the block
562 * the control flow ends to after 'from'.
565 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
567 MonoBasicBlock **newa;
571 if (from->cil_code) {
573 g_print ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
575 g_print ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
578 g_print ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
580 g_print ("edge from entry to exit\n");
584 for (i = 0; i < from->out_count; ++i) {
585 if (to == from->out_bb [i]) {
591 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
592 for (i = 0; i < from->out_count; ++i) {
593 newa [i] = from->out_bb [i];
601 for (i = 0; i < to->in_count; ++i) {
602 if (from == to->in_bb [i]) {
608 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
609 for (i = 0; i < to->in_count; ++i) {
610 newa [i] = to->in_bb [i];
619 * We mark each basic block with a region ID. We use that to avoid BB
620 * optimizations when blocks are in different regions.
623 mono_find_block_region (MonoCompile *cfg, int offset, int *filter_lengths)
625 MonoMethod *method = cfg->method;
626 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
627 MonoExceptionClause *clause;
630 /* first search for handlers and filters */
631 for (i = 0; i < header->num_clauses; ++i) {
632 clause = &header->clauses [i];
633 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->token_or_filter) &&
634 (offset < (clause->token_or_filter + filter_lengths [i])))
635 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
637 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
638 if (clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)
639 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
641 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
645 /* search the try blocks */
646 for (i = 0; i < header->num_clauses; ++i) {
647 clause = &header->clauses [i];
648 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
649 return ((i + 1) << 8) | clause->flags;
656 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
658 MonoMethod *method = cfg->method;
659 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
660 MonoExceptionClause *clause;
661 MonoBasicBlock *handler;
665 for (i = 0; i < header->num_clauses; ++i) {
666 clause = &header->clauses [i];
667 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
668 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
669 if (clause->flags == type) {
670 handler = g_hash_table_lookup (cfg->bb_hash, header->code + clause->handler_offset);
672 res = g_list_append (res, handler);
681 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
685 array [*dfn] = start;
686 /*g_print ("visit %d at %p\n", *dfn, start->cil_code);*/
687 for (i = 0; i < start->out_count; ++i) {
688 if (start->out_bb [i]->dfn)
691 start->out_bb [i]->dfn = *dfn;
692 start->out_bb [i]->df_parent = start;
693 array [*dfn] = start->out_bb [i];
694 df_visit (start->out_bb [i], dfn, array);
700 MonoBasicBlock *best;
704 previous_foreach (gconstpointer key, gpointer val, gpointer data)
706 PrevStruct *p = data;
707 MonoBasicBlock *bb = val;
708 //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,
709 //bb->method == p->best->method, bb->cil_code < p->code, bb->cil_code > p->best->cil_code);
711 if (bb->cil_code && bb->cil_code < p->code && bb->cil_code > p->best->cil_code)
715 static MonoBasicBlock*
716 find_previous (GHashTable *bb_hash, MonoBasicBlock *start, const guchar *code) {
722 g_hash_table_foreach (bb_hash, (GHFunc)previous_foreach, &p);
727 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
736 * FIXME: take into account all the details:
737 * second may have been the target of more than one bblock
739 second->out_count = first->out_count;
740 second->out_bb = first->out_bb;
742 for (i = 0; i < first->out_count; ++i) {
743 bb = first->out_bb [i];
744 for (j = 0; j < bb->in_count; ++j) {
745 if (bb->in_bb [j] == first)
746 bb->in_bb [j] = second;
750 first->out_count = 0;
751 first->out_bb = NULL;
752 link_bblock (cfg, first, second);
754 second->last_ins = first->last_ins;
756 /*g_print ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
757 for (inst = first->code; inst && inst->next; inst = inst->next) {
758 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
759 g_print ("found %p: %s", inst->next->cil_code, code);
761 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
762 second->code = inst->next;
764 first->last_ins = inst;
765 second->next_bb = first->next_bb;
766 first->next_bb = second;
771 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
777 mono_type_to_ldind (MonoType *type)
789 case MONO_TYPE_BOOLEAN:
803 case MONO_TYPE_FNPTR:
805 case MONO_TYPE_CLASS:
806 case MONO_TYPE_STRING:
807 case MONO_TYPE_OBJECT:
808 case MONO_TYPE_SZARRAY:
809 case MONO_TYPE_ARRAY:
810 return CEE_LDIND_REF;
818 case MONO_TYPE_VALUETYPE:
819 if (type->data.klass->enumtype) {
820 t = type->data.klass->enum_basetype->type;
824 case MONO_TYPE_TYPEDBYREF:
826 case MONO_TYPE_GENERICINST:
827 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE)
829 return CEE_LDIND_REF;
831 g_error ("unknown type 0x%02x in type_to_ldind", type->type);
837 mono_type_to_stind (MonoType *type)
848 case MONO_TYPE_BOOLEAN:
860 case MONO_TYPE_FNPTR:
862 case MONO_TYPE_CLASS:
863 case MONO_TYPE_STRING:
864 case MONO_TYPE_OBJECT:
865 case MONO_TYPE_SZARRAY:
866 case MONO_TYPE_ARRAY:
867 return CEE_STIND_REF;
875 case MONO_TYPE_VALUETYPE:
876 if (type->data.klass->enumtype) {
877 t = type->data.klass->enum_basetype->type;
881 case MONO_TYPE_TYPEDBYREF:
883 case MONO_TYPE_GENERICINST:
884 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE)
886 return CEE_STIND_REF;
888 g_error ("unknown type 0x%02x in type_to_stind", type->type);
894 * Returns the type used in the eval stack when @type is loaded.
895 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
898 type_to_eval_stack_type (MonoType *type, MonoInst *inst) {
902 inst->type = STACK_MP;
910 case MONO_TYPE_BOOLEAN:
916 inst->type = STACK_I4;
921 case MONO_TYPE_FNPTR:
922 inst->type = STACK_PTR;
924 case MONO_TYPE_CLASS:
925 case MONO_TYPE_STRING:
926 case MONO_TYPE_OBJECT:
927 case MONO_TYPE_SZARRAY:
928 case MONO_TYPE_ARRAY:
929 inst->type = STACK_OBJ;
933 inst->type = STACK_I8;
937 inst->type = STACK_R8;
939 case MONO_TYPE_VALUETYPE:
940 if (type->data.klass->enumtype) {
941 t = type->data.klass->enum_basetype->type;
944 inst->klass = type->data.klass;
945 inst->type = STACK_VTYPE;
948 case MONO_TYPE_TYPEDBYREF:
949 inst->klass = mono_defaults.typed_reference_class;
950 inst->type = STACK_VTYPE;
952 case MONO_TYPE_GENERICINST:
953 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE) {
954 inst->klass = mono_class_from_mono_type (type);
955 inst->type = STACK_VTYPE;
957 inst->type = STACK_OBJ;
961 g_error ("unknown type 0x%02x in eval stack type", type->type);
966 * The following tables are used to quickly validate the IL code in type_from_op ().
969 bin_num_table [STACK_MAX] [STACK_MAX] = {
970 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
971 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
972 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
973 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
974 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
975 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
976 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
977 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
982 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
985 /* reduce the size of this table */
987 bin_int_table [STACK_MAX] [STACK_MAX] = {
988 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
989 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
990 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
991 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
992 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
993 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
994 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
995 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
999 bin_comp_table [STACK_MAX] [STACK_MAX] = {
1001 {0, 1, 0, 1, 0, 0, 4, 0},
1002 {0, 0, 1, 0, 0, 0, 0, 0},
1003 {0, 1, 0, 1, 0, 2, 4, 0},
1004 {0, 0, 0, 0, 1, 0, 0, 0},
1005 {0, 0, 0, 2, 0, 1, 0, 0},
1006 {0, 4, 0, 4, 0, 0, 3, 0},
1007 {0, 0, 0, 0, 0, 0, 0, 0},
1010 /* reduce the size of this table */
1012 shift_table [STACK_MAX] [STACK_MAX] = {
1013 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1014 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1015 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1016 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1017 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1018 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1019 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1020 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1024 * Tables to map from the non-specific opcode to the matching
1025 * type-specific opcode.
1027 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
1028 static const guint16
1029 binops_op_map [STACK_MAX] = {
1030 0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, 0
1033 /* handles from CEE_NEG to CEE_CONV_U8 */
1034 static const guint16
1035 unops_op_map [STACK_MAX] = {
1036 0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, 0
1039 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
1040 static const guint16
1041 ovfops_op_map [STACK_MAX] = {
1042 0, 0, OP_LCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_FCONV_TO_U2-CEE_CONV_U2, 0
1045 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
1046 static const guint16
1047 ovf2ops_op_map [STACK_MAX] = {
1048 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
1051 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
1052 static const guint16
1053 ovf3ops_op_map [STACK_MAX] = {
1054 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
1057 /* handles from CEE_CEQ to CEE_CLT_UN */
1058 static const guint16
1059 ceqops_op_map [STACK_MAX] = {
1060 0, 0, OP_LCEQ-CEE_CEQ, OP_PCEQ-CEE_CEQ, OP_FCEQ-CEE_CEQ, 0
1064 * Sets ins->type (the type on the eval stack) according to the
1065 * type of the opcode and the arguments to it.
1066 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1068 * FIXME: this function sets ins->type unconditionally in some cases, but
1069 * it should set it to invalid for some types (a conv.x on an object)
1072 type_from_op (MonoInst *ins) {
1073 switch (ins->opcode) {
1080 /* FIXME: check unverifiable args for STACK_MP */
1081 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1082 ins->opcode += binops_op_map [ins->type];
1089 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1090 ins->opcode += binops_op_map [ins->type];
1095 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1096 ins->opcode += binops_op_map [ins->type];
1099 /* FIXME: handle some specifics with ins->next->type */
1100 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1104 case 256+CEE_CGT_UN:
1106 case 256+CEE_CLT_UN:
1107 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1108 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1112 ins->type = neg_table [ins->inst_i0->type];
1113 ins->opcode += unops_op_map [ins->type];
1116 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1117 ins->type = ins->inst_i0->type;
1119 ins->type = STACK_INV;
1120 ins->opcode += unops_op_map [ins->type];
1126 ins->type = STACK_I4;
1127 ins->opcode += unops_op_map [ins->inst_i0->type];
1130 ins->type = STACK_R8;
1131 switch (ins->inst_i0->type) {
1136 ins->opcode = OP_LCONV_TO_R_UN;
1140 case CEE_CONV_OVF_I1:
1141 case CEE_CONV_OVF_U1:
1142 case CEE_CONV_OVF_I2:
1143 case CEE_CONV_OVF_U2:
1144 case CEE_CONV_OVF_I4:
1145 case CEE_CONV_OVF_U4:
1146 ins->type = STACK_I4;
1147 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1149 case CEE_CONV_OVF_I_UN:
1150 case CEE_CONV_OVF_U_UN:
1151 ins->type = STACK_PTR;
1152 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1154 case CEE_CONV_OVF_I1_UN:
1155 case CEE_CONV_OVF_I2_UN:
1156 case CEE_CONV_OVF_I4_UN:
1157 case CEE_CONV_OVF_U1_UN:
1158 case CEE_CONV_OVF_U2_UN:
1159 case CEE_CONV_OVF_U4_UN:
1160 ins->type = STACK_I4;
1161 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1164 ins->type = STACK_PTR;
1165 switch (ins->inst_i0->type) {
1171 ins->opcode = OP_LCONV_TO_U;
1174 ins->opcode = OP_FCONV_TO_U;
1180 ins->type = STACK_I8;
1181 ins->opcode += unops_op_map [ins->inst_i0->type];
1183 case CEE_CONV_OVF_I8:
1184 case CEE_CONV_OVF_U8:
1185 ins->type = STACK_I8;
1186 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1188 case CEE_CONV_OVF_U8_UN:
1189 case CEE_CONV_OVF_I8_UN:
1190 ins->type = STACK_I8;
1191 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1195 ins->type = STACK_R8;
1196 ins->opcode += unops_op_map [ins->inst_i0->type];
1199 ins->type = STACK_R8;
1203 ins->type = STACK_I4;
1204 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1207 case CEE_CONV_OVF_I:
1208 case CEE_CONV_OVF_U:
1209 ins->type = STACK_PTR;
1210 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1213 case CEE_ADD_OVF_UN:
1215 case CEE_MUL_OVF_UN:
1217 case CEE_SUB_OVF_UN:
1218 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1219 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1222 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1229 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_MP, STACK_R8, STACK_R8, STACK_OBJ
1232 /* map ldelem.x to the matching ldind.x opcode */
1234 ldelem_to_ldind [] = {
1248 /* map stelem.x to the matching stind.x opcode */
1250 stelem_to_stind [] = {
1264 param_table [STACK_MAX] [STACK_MAX] = {
1269 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1273 switch (args->type) {
1283 for (i = 0; i < sig->param_count; ++i) {
1284 switch (args [i].type) {
1288 if (!sig->params [i]->byref)
1292 if (sig->params [i]->byref)
1294 switch (sig->params [i]->type) {
1295 case MONO_TYPE_CLASS:
1296 case MONO_TYPE_STRING:
1297 case MONO_TYPE_OBJECT:
1298 case MONO_TYPE_SZARRAY:
1299 case MONO_TYPE_ARRAY:
1306 if (sig->params [i]->byref)
1308 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1317 /*if (!param_table [args [i].type] [sig->params [i]->type])
1325 * When we need a pointer to the current domain many times in a method, we
1326 * call mono_domain_get() once and we store the result in a local variable.
1327 * This function returns the variable that represents the MonoDomain*.
1329 inline static MonoInst *
1330 mono_get_domainvar (MonoCompile *cfg)
1332 if (!cfg->domainvar)
1333 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1334 return cfg->domainvar;
1338 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
1341 int num = cfg->num_varinfo;
1343 if ((num + 1) >= cfg->varinfo_count) {
1344 cfg->varinfo_count = (cfg->varinfo_count + 2) * 2;
1345 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
1346 cfg->vars = (MonoMethodVar **)g_realloc (cfg->vars, sizeof (MonoMethodVar*) * cfg->varinfo_count);
1349 /*g_print ("created temp %d of type 0x%x\n", num, type->type);*/
1350 mono_jit_stats.allocate_var++;
1352 MONO_INST_NEW (cfg, inst, opcode);
1353 inst->inst_c0 = num;
1354 inst->inst_vtype = type;
1355 inst->klass = mono_class_from_mono_type (type);
1356 /* if set to 1 the variable is native */
1359 cfg->varinfo [num] = inst;
1361 cfg->vars [num] = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMethodVar));
1362 MONO_INIT_VARINFO (cfg->vars [num], num);
1365 //g_print ("created temp %d of type %s\n", num, mono_type_get_name (type));
1370 type_from_stack_type (MonoInst *ins) {
1371 switch (ins->type) {
1372 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1373 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1374 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1375 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1376 case STACK_MP: return &mono_defaults.int_class->byval_arg;
1377 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1378 case STACK_VTYPE: return &ins->klass->byval_arg;
1380 g_error ("stack type %d to montype not handled\n", ins->type);
1386 array_access_to_klass (int opcode)
1390 return mono_defaults.byte_class;
1392 return mono_defaults.uint16_class;
1395 return mono_defaults.int_class;
1398 return mono_defaults.sbyte_class;
1401 return mono_defaults.int16_class;
1404 return mono_defaults.int32_class;
1406 return mono_defaults.uint32_class;
1409 return mono_defaults.int64_class;
1412 return mono_defaults.single_class;
1415 return mono_defaults.double_class;
1416 case CEE_LDELEM_REF:
1417 case CEE_STELEM_REF:
1418 return mono_defaults.object_class;
1420 g_assert_not_reached ();
1426 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
1430 MONO_ADD_INS (bb, inst);
1433 switch (bb->last_ins->opcode) {
1446 while (prev->next && prev->next != bb->last_ins)
1448 if (prev == bb->code) {
1449 if (bb->last_ins == bb->code) {
1450 inst->next = bb->code;
1453 inst->next = prev->next;
1457 inst->next = bb->last_ins;
1461 // g_warning ("handle conditional jump in add_ins_to_end ()\n");
1463 MONO_ADD_INS (bb, inst);
1469 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
1471 MonoInst *inst, *load;
1473 NEW_TEMPLOAD (cfg, load, src);
1475 NEW_TEMPSTORE (cfg, inst, dest, load);
1476 if (inst->opcode == CEE_STOBJ) {
1477 NEW_TEMPLOADA (cfg, inst, dest);
1478 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE);
1480 inst->cil_code = NULL;
1481 mono_add_ins_to_end (bb, inst);
1486 * We try to share variables when possible
1489 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1494 /* inlining can result in deeper stacks */
1495 if (slot >= ((MonoMethodNormal *)cfg->method)->header->max_stack)
1496 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1498 pos = ins->type - 1 + slot * STACK_MAX;
1500 switch (ins->type) {
1507 if ((vnum = cfg->intvars [pos]))
1508 return cfg->varinfo [vnum];
1509 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1510 cfg->intvars [pos] = res->inst_c0;
1513 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1519 * This function is called to handle items that are left on the evaluation stack
1520 * at basic block boundaries. What happens is that we save the values to local variables
1521 * and we reload them later when first entering the target basic block (with the
1522 * handle_loaded_temps () function).
1523 * A single joint point will use the same variables (stored in the array bb->out_stack or
1524 * bb->in_stack, if the basic block is before or after the joint point).
1527 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count) {
1529 MonoBasicBlock *outb;
1530 MonoInst *inst, **locals;
1534 if (cfg->verbose_level > 3)
1535 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
1536 if (!bb->out_scount) {
1538 bb->out_scount = count;
1539 //g_print ("bblock %d has out:", bb->block_num);
1540 for (i = 0; i < bb->out_count; ++i) {
1541 outb = bb->out_bb [i];
1542 //g_print (" %d", outb->block_num);
1543 if (outb->in_stack) {
1545 bb->out_stack = outb->in_stack;
1551 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1552 for (i = 0; i < count; ++i) {
1554 /* try to reuse temps already allocated for this purpouse, if they occupy the same
1555 * stack slot and if they are of the same type. */
1556 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1558 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1563 locals = bb->out_stack;
1564 for (i = 0; i < count; ++i) {
1565 /* add store ops at the end of the bb, before the branch */
1566 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1567 if (inst->opcode == CEE_STOBJ) {
1568 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
1569 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE);
1571 inst->cil_code = sp [i]->cil_code;
1572 mono_add_ins_to_end (bb, inst);
1574 if (cfg->verbose_level > 3)
1575 g_print ("storing %d to temp %d\n", i, locals [i]->inst_c0);
1578 for (i = 0; i < bb->out_count; ++i) {
1579 outb = bb->out_bb [i];
1580 if (outb->in_scount)
1581 continue; /* check they are the same locals */
1582 outb->in_scount = count;
1583 outb->in_stack = locals;
1589 ret_type_to_call_opcode (MonoType *type, int calli, int virt)
1594 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1598 case MONO_TYPE_VOID:
1599 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
1602 case MONO_TYPE_BOOLEAN:
1605 case MONO_TYPE_CHAR:
1608 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1612 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1613 case MONO_TYPE_CLASS:
1614 case MONO_TYPE_STRING:
1615 case MONO_TYPE_OBJECT:
1616 case MONO_TYPE_SZARRAY:
1617 case MONO_TYPE_ARRAY:
1618 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1621 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
1624 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
1625 case MONO_TYPE_VALUETYPE:
1626 if (type->data.klass->enumtype) {
1627 t = type->data.klass->enum_basetype->type;
1630 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1631 case MONO_TYPE_TYPEDBYREF:
1632 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1634 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
1640 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
1642 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
1644 ji->ip.label = label;
1645 ji->type = MONO_PATCH_INFO_SWITCH;
1646 ji->data.table = bbs;
1647 ji->next = cfg->patch_info;
1648 ji->table_size = num_blocks;
1649 cfg->patch_info = ji;
1653 * When we add a tree of instructions, we need to ensure the instructions currently
1654 * on the stack are executed before (like, if we load a value from a local).
1655 * We ensure this by saving the currently loaded values to temps and rewriting the
1656 * instructions to load the values.
1657 * This is not done for opcodes that terminate a basic block (because it's handled already
1658 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
1661 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
1663 MonoInst *load, *store, *temp, *ins;
1665 while (stack < sp) {
1667 /* handle also other constants */
1668 if (ins->opcode != OP_ICONST) {
1669 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1670 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1671 store->cil_code = ins->cil_code;
1672 if (store->opcode == CEE_STOBJ) {
1673 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
1674 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE);
1676 MONO_ADD_INS (bblock, store);
1677 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1678 load->cil_code = ins->cil_code;
1686 * Prepare arguments for passing to a function call.
1687 * Return a non-zero value if the arguments can't be passed to the given
1689 * The type checks are not yet complete and some conversions may need
1690 * casts on 32 or 64 bit architectures.
1693 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
1698 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
1702 for (i = 0; i < sig->param_count; ++i) {
1703 if (sig->params [i]->byref) {
1705 * check the result of ldelema is only passed as an argument if the byref
1706 * type matches exactly the array element type.
1707 * FIXME: if the argument as been saved on the stack as part of the
1708 * interface variable code (the value was on the stack at a basic block boundary)
1709 * we need to add the check in that case, too.
1711 if (args [i]->opcode == CEE_LDELEMA) {
1713 MonoClass *exact_class = mono_class_from_mono_type (sig->params [i]);
1714 if (!exact_class->valuetype) {
1715 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
1716 check->cil_code = args [i]->cil_code;
1717 check->klass = exact_class;
1718 check->inst_left = args [i]->inst_left;
1719 check->type = STACK_OBJ;
1720 args [i]->inst_left = check;
1723 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
1727 simple_type = sig->params [i]->type;
1729 switch (simple_type) {
1730 case MONO_TYPE_VOID:
1735 case MONO_TYPE_BOOLEAN:
1738 case MONO_TYPE_CHAR:
1741 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
1747 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
1750 case MONO_TYPE_CLASS:
1751 case MONO_TYPE_STRING:
1752 case MONO_TYPE_OBJECT:
1753 case MONO_TYPE_SZARRAY:
1754 case MONO_TYPE_ARRAY:
1755 if (args [i]->type != STACK_OBJ)
1760 if (args [i]->type != STACK_I8)
1765 if (args [i]->type != STACK_R8)
1768 case MONO_TYPE_VALUETYPE:
1769 if (sig->params [i]->data.klass->enumtype) {
1770 simple_type = sig->params [i]->data.klass->enum_basetype->type;
1773 if (args [i]->type != STACK_VTYPE)
1776 case MONO_TYPE_TYPEDBYREF:
1777 if (args [i]->type != STACK_VTYPE)
1780 case MONO_TYPE_GENERICINST:
1781 simple_type = sig->params [i]->data.generic_inst->generic_type->type;
1785 g_error ("unknown type 0x%02x in check_call_signature", simple_type);
1792 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
1793 const guint8 *ip, gboolean to_end)
1795 MonoInst *temp, *store, *ins = (MonoInst*)call;
1796 MonoType *ret = sig->ret;
1798 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
1800 call->inst.type = STACK_OBJ;
1801 call->inst.opcode = CEE_CALL;
1802 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
1804 type_to_eval_stack_type (ret, ins);
1805 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
1808 if (MONO_TYPE_ISSTRUCT (ret)) {
1811 /* we use this to allocate native sized structs */
1812 temp->unused = sig->pinvoke;
1814 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
1815 if (call->inst.opcode == OP_VCALL)
1816 ins->inst_left = loada;
1818 ins->inst_right = loada; /* a virtual or indirect call */
1821 mono_add_ins_to_end (bblock, ins);
1823 MONO_ADD_INS (bblock, ins);
1825 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1826 store->cil_code = ip;
1828 mono_add_ins_to_end (bblock, store);
1830 MONO_ADD_INS (bblock, store);
1832 return temp->inst_c0;
1835 mono_add_ins_to_end (bblock, ins);
1837 MONO_ADD_INS (bblock, ins);
1842 inline static MonoCallInst *
1843 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1844 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
1849 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual));
1851 call->inst.cil_code = ip;
1853 call->signature = sig;
1854 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
1856 for (arg = call->out_args; arg;) {
1857 MonoInst *narg = arg->next;
1862 mono_add_ins_to_end (bblock, arg);
1864 MONO_ADD_INS (bblock, arg);
1871 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1872 MonoInst **args, MonoInst *addr, const guint8 *ip)
1874 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
1876 call->inst.inst_i0 = addr;
1878 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
1881 static MonoCallInst*
1882 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
1883 MonoInst **args, const guint8 *ip, MonoInst *this)
1885 gboolean virtual = this != NULL;
1888 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, FALSE);
1890 if (this && sig->hasthis &&
1891 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
1892 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
1893 call->method = mono_marshal_get_remoting_invoke_with_check (method);
1895 call->method = method;
1897 call->inst.flags |= MONO_INST_HAS_METHOD;
1898 call->inst.inst_left = this;
1904 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
1905 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
1907 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, signature, args, ip, this);
1909 return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
1913 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
1914 MonoInst **args, const guint8 *ip, gboolean to_end)
1920 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
1922 return mono_spill_call (cfg, bblock, call, sig, func == mono_array_new_va, ip, to_end);
1926 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
1928 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
1931 g_warning ("unregistered JIT ICall");
1932 g_assert_not_reached ();
1935 return mono_emit_native_call (cfg, bblock, info->wrapper, info->sig, args, ip, FALSE);
1939 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
1941 MonoInst *ins, *temp = NULL, *store, *load, *begin;
1942 MonoInst *last_arg = NULL;
1946 /*g_print ("emulating: ");
1947 mono_print_tree (tree);
1949 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE));
1950 ins = (MonoInst*)call;
1952 call->inst.cil_code = tree->cil_code;
1954 call->signature = info->sig;
1956 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
1958 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
1959 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
1960 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1961 store->cil_code = tree->cil_code;
1966 nargs = info->sig->param_count + info->sig->hasthis;
1968 for (last_arg = call->out_args; last_arg && last_arg->next; last_arg = last_arg->next) ;
1971 last_arg->next = store;
1974 begin = call->out_args;
1978 if (cfg->prev_ins) {
1979 store->next = cfg->prev_ins->next;
1980 cfg->prev_ins->next = begin;
1982 store->next = cfg->cbb->code;
1983 cfg->cbb->code = begin;
1986 call->fptr = info->wrapper;
1988 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
1989 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1994 static MonoMethodSignature *
1995 mono_get_element_address_signature (int arity)
1997 static GHashTable *sighash = NULL;
1998 MonoMethodSignature *res;
2002 sighash = g_hash_table_new (NULL, NULL);
2005 if ((res = g_hash_table_lookup (sighash, (gpointer)arity)))
2008 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
2010 res->params [0] = &mono_defaults.array_class->byval_arg;
2012 for (i = 1; i <= arity; i++)
2013 res->params [i] = &mono_defaults.int_class->byval_arg;
2015 res->ret = &mono_defaults.int_class->byval_arg;
2017 g_hash_table_insert (sighash, (gpointer)arity, res);
2023 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native) {
2024 MonoInst *iargs [3];
2030 * This check breaks with spilled vars... need to handle it during verification anyway.
2031 * g_assert (klass && klass == src->klass && klass == dest->klass);
2035 n = mono_class_native_size (klass, &align);
2037 n = mono_class_value_size (klass, &align);
2039 if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
2041 MONO_INST_NEW (cfg, inst, OP_MEMCPY);
2042 inst->inst_left = dest;
2043 inst->inst_right = src;
2044 inst->cil_code = ip;
2046 MONO_ADD_INS (bblock, inst);
2051 NEW_ICONST (cfg, iargs [2], n);
2053 mono_emit_native_call (cfg, bblock, helper_memcpy, helper_sig_memcpy, iargs, ip, to_end);
2057 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
2059 MonoInst *iargs [2];
2060 MonoInst *ins, *zero_int32;
2063 NEW_ICONST (cfg, zero_int32, 0);
2065 mono_class_init (klass);
2066 n = mono_class_value_size (klass, NULL);
2067 MONO_INST_NEW (cfg, ins, 0);
2069 ins->inst_left = dest;
2070 ins->inst_right = zero_int32;
2073 ins->opcode = CEE_STIND_I1;
2074 MONO_ADD_INS (bblock, ins);
2077 ins->opcode = CEE_STIND_I2;
2078 MONO_ADD_INS (bblock, ins);
2081 ins->opcode = CEE_STIND_I4;
2082 MONO_ADD_INS (bblock, ins);
2085 if (n <= sizeof (gpointer) * 5) {
2086 ins->opcode = OP_MEMSET;
2089 MONO_ADD_INS (bblock, ins);
2092 handle_loaded_temps (cfg, bblock, stack_start, sp);
2093 NEW_ICONST (cfg, ins, n);
2096 mono_emit_jit_icall (cfg, bblock, helper_initobj, iargs, ip);
2101 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
2104 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
2106 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
2107 MonoMethodSignature *signature = method->signature;
2111 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2112 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2113 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
2114 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
2115 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2116 (method->klass->marshalbyref) ||
2117 !header || header->num_clauses ||
2118 /* fixme: why cant we inline valuetype returns? */
2119 MONO_TYPE_ISSTRUCT (signature->ret))
2122 /* its not worth to inline methods with valuetype arguments?? */
2123 for (i = 0; i < signature->param_count; i++) {
2124 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
2130 * if we can initialize the class of the method right away, we do,
2131 * otherwise we don't allow inlining if the class needs initialization,
2132 * since it would mean inserting a call to mono_runtime_class_init()
2133 * inside the inlined code
2135 if (!(cfg->opt & MONO_OPT_SHARED)) {
2136 vtable = mono_class_vtable (cfg->domain, method->klass);
2137 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)
2138 mono_runtime_class_init (vtable);
2139 else if (!vtable->initialized && mono_class_needs_cctor_run (method->klass, NULL))
2143 * If we're compiling for shared code
2144 * the cctor will need to be run at aot method load time, for example,
2145 * or at the end of the compilation of the inlining method.
2147 if (mono_class_needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
2150 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
2152 /* also consider num_locals? */
2153 if (header->code_size < 20)
2160 mini_get_ldelema_ins (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
2164 MonoMethodSignature *esig;
2166 rank = cmethod->signature->param_count - (is_set? 1: 0);
2168 * FIXME: handle TypeMismatch for set or use the slow path
2171 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
2173 NEW_GROUP (cfg, indexes, sp [1], sp [2]);
2174 MONO_INST_NEW (cfg, addr, OP_LDELEMA2D);
2175 addr->inst_left = sp [0];
2176 addr->inst_right = indexes;
2177 addr->cil_code = ip;
2178 addr->type = STACK_MP;
2179 addr->klass = cmethod->klass;
2182 esig = mono_get_element_address_signature (rank);
2183 temp = mono_emit_native_call (cfg, bblock, ves_array_element_address, esig, sp, ip, FALSE);
2184 NEW_TEMPLOAD (cfg, addr, temp);
2189 mini_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
2194 if (cmethod->klass == mono_defaults.string_class) {
2195 if (cmethod->name [0] != 'g' || strcmp (cmethod->name, "get_Chars"))
2198 } else if (cmethod->klass == mono_defaults.math_class) {
2199 if (strcmp (cmethod->name, "Sin") == 0)
2201 else if (strcmp (cmethod->name, "Cos") == 0)
2203 else if (strcmp (cmethod->name, "Tan") == 0)
2205 else if (strcmp (cmethod->name, "Atan") == 0)
2207 else if (strcmp (cmethod->name, "Sqrt") == 0)
2209 else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8)
2212 /* OP_FREM is not IEEE compatible */
2213 else if (strcmp (cmethod->name, "IEEERemainder") == 0)
2218 } else if (cmethod->klass == mono_defaults.array_class) {
2219 if (strcmp (cmethod->name, "get_Rank") == 0)
2221 else if (strcmp (cmethod->name, "get_Length") == 0)
2228 pc = fsig->param_count + fsig->hasthis;
2229 MONO_INST_NEW (cfg, ins, op);
2232 ins->inst_i0 = args [0];
2234 ins->inst_i1 = args [1];
2241 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
2243 MonoInst *store, *temp;
2246 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
2248 if (!sig->hasthis && sig->param_count == 0)
2252 if (sp [0]->opcode == OP_ICONST) {
2255 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
2257 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2258 store->cil_code = sp [0]->cil_code;
2259 MONO_ADD_INS (bblock, store);
2264 for (i = 0; i < sig->param_count; ++i) {
2265 if (sp [0]->opcode == OP_ICONST) {
2268 temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
2270 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2271 store->cil_code = sp [0]->cil_code;
2272 if (store->opcode == CEE_STOBJ) {
2273 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2274 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE);
2276 MONO_ADD_INS (bblock, store);
2284 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
2285 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b)
2287 MonoInst *ins, *rvar = NULL;
2288 MonoMethodHeader *cheader;
2289 MonoBasicBlock *ebblock, *sbblock;
2290 int i, costs, new_locals_offset;
2292 if (cfg->verbose_level > 2)
2293 g_print ("INLINE START %p %s\n", cmethod, mono_method_full_name (cmethod, TRUE));
2295 cheader = ((MonoMethodNormal *)cmethod)->header;
2297 if (!cmethod->inline_info) {
2298 mono_jit_stats.inlineable_methods++;
2299 cmethod->inline_info = 1;
2301 /* allocate space to store the return value */
2302 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2303 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2306 /* allocate local variables */
2307 new_locals_offset = cfg->num_varinfo;
2308 for (i = 0; i < cheader->num_locals; ++i)
2309 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
2311 /* allocate starte and end blocks */
2312 sbblock = NEW_BBLOCK (cfg);
2313 sbblock->block_num = cfg->num_bblocks++;
2314 sbblock->real_offset = real_offset;
2316 ebblock = NEW_BBLOCK (cfg);
2317 ebblock->block_num = cfg->num_bblocks++;
2318 ebblock->real_offset = real_offset;
2320 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
2322 if (costs >= 0 && costs < 60) {
2323 if (cfg->verbose_level > 2)
2324 g_print ("INLINE END %s\n", mono_method_full_name (cmethod, TRUE));
2326 mono_jit_stats.inlined_methods++;
2328 /* always add some code to avoid block split failures */
2329 MONO_INST_NEW (cfg, ins, CEE_NOP);
2330 MONO_ADD_INS (bblock, ins);
2333 bblock->next_bb = sbblock;
2334 link_bblock (cfg, bblock, sbblock);
2337 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
2343 if (cfg->verbose_level > 2)
2344 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
2350 * Some of these comments may well be out-of-date.
2351 * Design decisions: we do a single pass over the IL code (and we do bblock
2352 * splitting/merging in the few cases when it's required: a back jump to an IL
2353 * address that was not already seen as bblock starting point).
2354 * Code is validated as we go (full verification is still better left to metadata/verify.c).
2355 * Complex operations are decomposed in simpler ones right away. We need to let the
2356 * arch-specific code peek and poke inside this process somehow (except when the
2357 * optimizations can take advantage of the full semantic info of coarse opcodes).
2358 * All the opcodes of the form opcode.s are 'normalized' to opcode.
2359 * MonoInst->opcode initially is the IL opcode or some simplification of that
2360 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
2361 * opcode with value bigger than OP_LAST.
2362 * At this point the IR can be handed over to an interpreter, a dumb code generator
2363 * or to the optimizing code generator that will translate it to SSA form.
2365 * Profiling directed optimizations.
2366 * We may compile by default with few or no optimizations and instrument the code
2367 * or the user may indicate what methods to optimize the most either in a config file
2368 * or through repeated runs where the compiler applies offline the optimizations to
2369 * each method and then decides if it was worth it.
2372 * * consider using an array instead of an hash table (bb_hash)
2375 #define CHECK_TYPE(ins) if (!(ins)->type) goto unverified
2376 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) goto unverified
2377 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) goto unverified
2379 #define TYPE_PARAM_TO_TYPE(num) (method->klass->generic_inst->data.generic_inst->type_argv [(num)])
2380 #define TYPE_PARAM_TO_CLASS(num) (mono_class_from_mono_type (TYPE_PARAM_TO_TYPE ((num))))
2382 /* offset from br.s -> br like opcodes */
2383 #define BIG_BRANCH_OFFSET 13
2386 get_basic_blocks (MonoCompile *cfg, GHashTable *bbhash, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
2388 unsigned char *ip = start;
2389 unsigned char *target;
2392 MonoBasicBlock *bblock;
2393 const MonoOpcode *opcode;
2396 cli_addr = ip - start;
2397 i = mono_opcode_value ((const guint8 **)&ip);
2398 opcode = &mono_opcodes [i];
2399 switch (opcode->argument) {
2400 case MonoInlineNone:
2403 case MonoInlineString:
2404 case MonoInlineType:
2405 case MonoInlineField:
2406 case MonoInlineMethod:
2409 case MonoShortInlineR:
2416 case MonoShortInlineVar:
2417 case MonoShortInlineI:
2420 case MonoShortInlineBrTarget:
2421 target = start + cli_addr + 2 + (signed char)ip [1];
2422 GET_BBLOCK (cfg, bbhash, bblock, target);
2425 case MonoInlineBrTarget:
2426 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
2427 GET_BBLOCK (cfg, bbhash, bblock, target);
2430 case MonoInlineSwitch: {
2431 guint32 n = read32 (ip + 1);
2434 cli_addr += 5 + 4 * n;
2435 target = start + cli_addr;
2436 GET_BBLOCK (cfg, bbhash, bblock, target);
2438 for (j = 0; j < n; ++j) {
2439 target = start + cli_addr + (gint32)read32 (ip);
2440 GET_BBLOCK (cfg, bbhash, bblock, target);
2450 g_assert_not_reached ();
2459 static MonoClassField *
2460 get_generic_field_inst (MonoClassField *field, MonoClass *klass, MonoClass **retclass)
2463 for (i = 0; i < field->parent->field.count; ++i) {
2464 if (field == &field->parent->fields [i]) {
2466 return &klass->fields [i];
2473 * mono_method_to_ir: translates IL into basic blocks containing trees
2476 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
2477 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
2478 guint inline_offset, gboolean is_virtual_call)
2480 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
2481 MonoInst *ins, **sp, **stack_start;
2482 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
2484 MonoMethod *cmethod;
2485 MonoInst **arg_array;
2486 MonoMethodHeader *header;
2488 guint32 token, ins_flag;
2490 unsigned char *ip, *end, *target, *err_pos;
2491 static double r8_0 = 0.0;
2492 MonoMethodSignature *sig;
2493 MonoType **param_types;
2494 GList *bb_recheck = NULL, *tmp;
2495 int i, n, start_new_bblock, align;
2496 int num_calls = 0, inline_costs = 0;
2497 int *filter_lengths = NULL;
2498 int breakpoint_id = 0;
2501 image = method->klass->image;
2502 header = ((MonoMethodNormal *)method)->header;
2503 sig = method->signature;
2504 ip = (unsigned char*)header->code;
2505 end = ip + header->code_size;
2506 mono_jit_stats.cil_code_size += header->code_size;
2508 if (cfg->method == method) {
2510 bbhash = cfg->bb_hash;
2512 real_offset = inline_offset;
2513 bbhash = g_hash_table_new (g_direct_hash, NULL);
2516 if (cfg->verbose_level > 2)
2517 g_print ("method to IR %s\n", mono_method_full_name (method, TRUE));
2519 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
2520 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
2522 dont_inline = g_list_prepend (dont_inline, method);
2523 if (cfg->method == method) {
2526 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
2527 start_bblock->cil_code = NULL;
2528 start_bblock->cil_length = 0;
2529 start_bblock->block_num = cfg->num_bblocks++;
2532 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
2533 end_bblock->cil_code = NULL;
2534 end_bblock->cil_length = 0;
2535 end_bblock->block_num = cfg->num_bblocks++;
2536 g_assert (cfg->num_bblocks == 2);
2538 arg_array = alloca (sizeof (MonoInst *) * (sig->hasthis + sig->param_count));
2539 for (i = sig->hasthis + sig->param_count - 1; i >= 0; i--)
2540 arg_array [i] = cfg->varinfo [i];
2542 if (header->num_clauses) {
2543 int size = sizeof (int) * header->num_clauses;
2544 filter_lengths = alloca (size);
2545 memset (filter_lengths, 0, size);
2547 cfg->spvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2548 /* prevent it from being register allocated */
2549 cfg->spvar->flags |= MONO_INST_INDIRECT;
2551 /* handle exception clauses */
2552 for (i = 0; i < header->num_clauses; ++i) {
2553 //unsigned char *p = ip;
2554 MonoExceptionClause *clause = &header->clauses [i];
2555 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->try_offset);
2556 tblock->real_offset = clause->try_offset;
2557 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
2558 tblock->real_offset = clause->handler_offset;
2560 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
2561 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2562 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2563 MONO_ADD_INS (tblock, ins);
2566 /*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);
2568 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
2570 /* catch and filter blocks get the exception object on the stack */
2571 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
2572 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2573 /* mostly like handle_stack_args (), but just sets the input args */
2574 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
2576 cfg->exvar = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
2577 /* prevent it from being register allocated */
2578 cfg->exvar->flags |= MONO_INST_INDIRECT;
2580 tblock->in_scount = 1;
2581 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2582 tblock->in_stack [0] = cfg->exvar;
2584 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2585 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->token_or_filter);
2586 tblock->real_offset = clause->token_or_filter;
2587 tblock->in_scount = 1;
2588 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2589 tblock->in_stack [0] = cfg->exvar;
2590 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2591 MONO_ADD_INS (tblock, ins);
2597 arg_array = alloca (sizeof (MonoInst *) * (sig->hasthis + sig->param_count));
2598 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
2601 /* FIRST CODE BLOCK */
2602 bblock = NEW_BBLOCK (cfg);
2603 bblock->cil_code = ip;
2605 ADD_BBLOCK (cfg, bbhash, bblock);
2607 if (cfg->method == method) {
2608 breakpoint_id = mono_debugger_method_has_breakpoint (method);
2609 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
2610 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2611 MONO_ADD_INS (bblock, ins);
2615 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED))) || mono_compile_aot) {
2616 /* we use a separate basic block for the initialization code */
2617 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
2618 init_localsbb->real_offset = real_offset;
2619 start_bblock->next_bb = init_localsbb;
2620 init_localsbb->next_bb = bblock;
2621 link_bblock (cfg, start_bblock, init_localsbb);
2622 link_bblock (cfg, init_localsbb, bblock);
2623 init_localsbb->block_num = cfg->num_bblocks++;
2625 start_bblock->next_bb = bblock;
2626 link_bblock (cfg, start_bblock, bblock);
2629 if (get_basic_blocks (cfg, bbhash, header, real_offset, ip, end, &err_pos)) {
2634 mono_debug_init_method (cfg, bblock, breakpoint_id);
2636 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * (sig->hasthis + sig->param_count));
2638 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
2639 for (n = 0; n < sig->param_count; ++n)
2640 param_types [n + sig->hasthis] = sig->params [n];
2642 /* do this somewhere outside - not here */
2643 NEW_ICONST (cfg, zero_int32, 0);
2644 NEW_ICONST (cfg, zero_int64, 0);
2645 zero_int64->type = STACK_I8;
2646 NEW_PCONST (cfg, zero_ptr, 0);
2647 NEW_PCONST (cfg, zero_obj, 0);
2648 zero_obj->type = STACK_OBJ;
2650 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
2651 zero_r8->type = STACK_R8;
2652 zero_r8->inst_p0 = &r8_0;
2654 /* add a check for this != NULL to inlined methods */
2655 if (is_virtual_call) {
2656 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
2657 NEW_ARGLOAD (cfg, ins->inst_left, 0);
2659 MONO_ADD_INS (bblock, ins);
2662 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
2663 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
2666 start_new_bblock = 0;
2669 if (cfg->method == method)
2670 real_offset = ip - header->code;
2672 real_offset = inline_offset;
2674 if (start_new_bblock) {
2675 bblock->cil_length = ip - bblock->cil_code;
2676 if (start_new_bblock == 2) {
2677 g_assert (ip == tblock->cil_code);
2679 GET_BBLOCK (cfg, bbhash, tblock, ip);
2681 bblock->next_bb = tblock;
2683 start_new_bblock = 0;
2684 for (i = 0; i < bblock->in_scount; ++i) {
2685 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2689 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
2690 link_bblock (cfg, bblock, tblock);
2691 if (sp != stack_start) {
2692 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2695 bblock->next_bb = tblock;
2697 for (i = 0; i < bblock->in_scount; ++i) {
2698 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2704 if (cfg->coverage_info) {
2705 MonoInst *store, *one;
2706 guint32 cil_offset = ip - header->code;
2707 cfg->coverage_info->data [cil_offset].cil_code = ip;
2709 /* TODO: Use an increment here */
2710 NEW_ICONST (cfg, one, 1);
2713 NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
2716 MONO_INST_NEW (cfg, store, CEE_STIND_I);
2717 store->cil_code = ip;
2718 store->inst_left = ins;
2719 store->inst_right = one;
2721 MONO_ADD_INS (bblock, store);
2724 if (cfg->verbose_level > 3)
2725 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, sp-stack_start, mono_disasm_code_one (NULL, method, ip, NULL));
2732 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2733 ins->cil_code = ip++;
2734 MONO_ADD_INS (bblock, ins);
2740 CHECK_STACK_OVF (1);
2741 n = (*ip)-CEE_LDARG_0;
2742 NEW_ARGLOAD (cfg, ins, n);
2743 ins->cil_code = ip++;
2750 CHECK_STACK_OVF (1);
2751 n = (*ip)-CEE_LDLOC_0;
2752 NEW_LOCLOAD (cfg, ins, n);
2753 ins->cil_code = ip++;
2761 n = (*ip)-CEE_STLOC_0;
2763 handle_loaded_temps (cfg, bblock, stack_start, sp);
2764 NEW_LOCSTORE (cfg, ins, n, *sp);
2766 if (ins->opcode == CEE_STOBJ) {
2767 NEW_LOCLOADA (cfg, ins, n);
2768 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2770 MONO_ADD_INS (bblock, ins);
2775 CHECK_STACK_OVF (1);
2776 NEW_ARGLOAD (cfg, ins, ip [1]);
2782 CHECK_STACK_OVF (1);
2783 NEW_ARGLOADA (cfg, ins, ip [1]);
2791 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
2792 handle_loaded_temps (cfg, bblock, stack_start, sp);
2794 if (ins->opcode == CEE_STOBJ) {
2795 NEW_ARGLOADA (cfg, ins, ip [1]);
2796 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2798 MONO_ADD_INS (bblock, ins);
2802 CHECK_STACK_OVF (1);
2803 NEW_LOCLOAD (cfg, ins, ip [1]);
2809 CHECK_STACK_OVF (1);
2810 NEW_LOCLOADA (cfg, ins, ip [1]);
2818 handle_loaded_temps (cfg, bblock, stack_start, sp);
2819 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
2821 if (ins->opcode == CEE_STOBJ) {
2822 NEW_LOCLOADA (cfg, ins, ip [1]);
2823 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2825 MONO_ADD_INS (bblock, ins);
2830 CHECK_STACK_OVF (1);
2831 NEW_PCONST (cfg, ins, NULL);
2833 ins->type = STACK_OBJ;
2838 CHECK_STACK_OVF (1);
2839 NEW_ICONST (cfg, ins, -1);
2853 CHECK_STACK_OVF (1);
2854 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
2860 CHECK_STACK_OVF (1);
2862 NEW_ICONST (cfg, ins, *((signed char*)ip));
2868 CHECK_STACK_OVF (1);
2869 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
2875 CHECK_STACK_OVF (1);
2876 MONO_INST_NEW (cfg, ins, OP_I8CONST);
2878 ins->type = STACK_I8;
2880 ins->inst_l = (gint64)read64 (ip);
2885 float *f = g_malloc (sizeof (float));
2886 CHECK_STACK_OVF (1);
2887 MONO_INST_NEW (cfg, ins, OP_R4CONST);
2888 ins->type = STACK_R8;
2897 double *d = g_malloc (sizeof (double));
2898 CHECK_STACK_OVF (1);
2899 MONO_INST_NEW (cfg, ins, OP_R8CONST);
2900 ins->type = STACK_R8;
2909 MonoInst *temp, *store;
2911 CHECK_STACK_OVF (1);
2916 * small optimization: if the loaded value was from a local already,
2917 * just load it twice.
2919 if (ins->ssa_op == MONO_SSA_LOAD &&
2920 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
2922 MONO_INST_NEW (cfg, temp, 0);
2924 temp->cil_code = ip;
2927 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
2928 temp->cil_code = ip;
2929 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2930 store->cil_code = ip;
2931 MONO_ADD_INS (bblock, store);
2932 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
2935 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
2945 MONO_INST_NEW (cfg, ins, CEE_POP);
2946 MONO_ADD_INS (bblock, ins);
2947 ins->cil_code = ip++;
2952 if (stack_start != sp)
2954 MONO_INST_NEW (cfg, ins, CEE_JMP);
2955 token = read32 (ip + 1);
2956 /* FIXME: check the signature matches */
2957 cmethod = mono_get_method (image, token, NULL);
2958 ins->inst_p0 = cmethod;
2959 MONO_ADD_INS (bblock, ins);
2961 start_new_bblock = 1;
2965 case CEE_CALLVIRT: {
2966 MonoInst *addr = NULL;
2967 MonoMethodSignature *fsig = NULL;
2968 int temp, array_rank = 0;
2969 int virtual = *ip == CEE_CALLVIRT;
2971 token = read32 (ip + 1);
2973 if (*ip == CEE_CALLI) {
2978 if (method->wrapper_type != MONO_WRAPPER_NONE)
2979 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
2981 fsig = mono_metadata_parse_signature (image, token);
2983 n = fsig->param_count + fsig->hasthis;
2986 if (method->wrapper_type != MONO_WRAPPER_NONE) {
2987 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
2989 cmethod = mono_get_method (image, token, NULL);
2992 if (!cmethod->klass->inited)
2993 mono_class_init (cmethod->klass);
2995 if (cmethod->signature->pinvoke) {
2996 #ifdef MONO_USE_EXC_TABLES
2997 if (mono_method_blittable (cmethod)) {
2998 fsig = cmethod->signature;
3001 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
3002 fsig = wrapper->signature;
3003 #ifdef MONO_USE_EXC_TABLES
3007 fsig = mono_method_get_signature (cmethod, image, token);
3010 n = fsig->param_count + fsig->hasthis;
3012 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
3013 cmethod->klass->parent == mono_defaults.array_class) {
3014 array_rank = cmethod->klass->rank;
3017 if (cmethod->string_ctor)
3018 g_assert_not_reached ();
3024 //g_assert (!virtual || fsig->hasthis);
3028 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
3031 if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL)) {
3033 for (i = 0; i < n; ++i) {
3034 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3036 MONO_ADD_INS (bblock, ins);
3038 MONO_INST_NEW (cfg, ins, CEE_JMP);
3040 ins->inst_p0 = cmethod;
3041 MONO_ADD_INS (bblock, ins);
3042 start_new_bblock = 1;
3043 /* skip CEE_RET as well */
3048 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_opcode_for_method (cfg, cmethod, fsig, sp))) {
3051 if (MONO_TYPE_IS_VOID (fsig->ret)) {
3052 MONO_ADD_INS (bblock, ins);
3054 type_to_eval_stack_type (fsig->ret, ins);
3063 handle_loaded_temps (cfg, bblock, stack_start, sp);
3065 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3066 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
3067 mono_method_check_inlining (cfg, cmethod) &&
3068 !g_list_find (dont_inline, cmethod)) {
3070 MonoBasicBlock *ebblock;
3072 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3076 GET_BBLOCK (cfg, bbhash, bblock, ip);
3077 ebblock->next_bb = bblock;
3078 link_bblock (cfg, ebblock, bblock);
3080 if (!MONO_TYPE_IS_VOID (fsig->ret))
3083 /* indicates start of a new block, and triggers a load of all
3084 stack arguments at bb boundarie */
3087 inline_costs += costs;
3092 inline_costs += 10 * num_calls++;
3094 /* tail recursion elimination */
3095 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == cfg->method && ip [5] == CEE_RET) {
3096 gboolean has_vtargs = FALSE;
3099 /* keep it simple */
3100 for (i = fsig->param_count - 1; i >= 0; i--) {
3101 if (MONO_TYPE_ISSTRUCT (cmethod->signature->params [i]))
3106 for (i = 0; i < n; ++i) {
3107 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3109 MONO_ADD_INS (bblock, ins);
3111 MONO_INST_NEW (cfg, ins, CEE_BR);
3113 MONO_ADD_INS (bblock, ins);
3114 tblock = start_bblock->out_bb [0];
3115 link_bblock (cfg, bblock, tblock);
3116 ins->inst_target_bb = tblock;
3117 start_new_bblock = 1;
3120 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3121 /* just create a dummy - the value is never used */
3122 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
3123 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3131 if (*ip == CEE_CALLI) {
3133 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
3134 NEW_TEMPLOAD (cfg, *sp, temp);
3138 } else if (array_rank) {
3141 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
3142 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
3143 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
3145 if (ins->opcode == CEE_STOBJ) {
3146 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
3148 MONO_ADD_INS (bblock, ins);
3151 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
3152 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3153 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
3157 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
3158 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3161 g_assert_not_reached ();
3165 if (0 && CODE_IS_STLOC (ip + 5) && (!MONO_TYPE_ISSTRUCT (fsig->ret)) && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)) {
3166 /* no need to spill */
3167 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
3170 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) {
3171 NEW_TEMPLOAD (cfg, *sp, temp);
3181 if (cfg->method != method) {
3182 /* return from inlined methode */
3187 //g_assert (returnvar != -1);
3188 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
3189 store->cil_code = sp [0]->cil_code;
3190 if (store->opcode == CEE_STOBJ) {
3191 g_assert_not_reached ();
3192 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
3193 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
3195 MONO_ADD_INS (bblock, store);
3199 g_assert (!return_var);
3202 MONO_INST_NEW (cfg, ins, CEE_NOP);
3203 ins->opcode = mono_type_to_stind (method->signature->ret);
3204 if (ins->opcode == CEE_STOBJ) {
3205 NEW_RETLOADA (cfg, ins);
3206 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3208 ins->opcode = OP_SETRET;
3210 ins->inst_i0 = *sp;;
3211 ins->inst_i1 = NULL;
3212 MONO_ADD_INS (bblock, ins);
3216 if (sp != stack_start)
3218 MONO_INST_NEW (cfg, ins, CEE_BR);
3219 ins->cil_code = ip++;
3220 ins->inst_target_bb = end_bblock;
3221 MONO_ADD_INS (bblock, ins);
3222 link_bblock (cfg, bblock, end_bblock);
3223 start_new_bblock = 1;
3226 MONO_INST_NEW (cfg, ins, CEE_BR);
3227 ins->cil_code = ip++;
3228 MONO_ADD_INS (bblock, ins);
3229 target = ip + 1 + (signed char)(*ip);
3231 GET_BBLOCK (cfg, bbhash, tblock, target);
3232 link_bblock (cfg, bblock, tblock);
3233 CHECK_BBLOCK (target, ip, tblock);
3234 ins->inst_target_bb = tblock;
3235 if (sp != stack_start) {
3236 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3239 start_new_bblock = 1;
3245 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3246 ins->cil_code = ip++;
3247 target = ip + 1 + *(signed char*)ip;
3249 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
3250 if (sp != stack_start) {
3251 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3267 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3268 ins->cil_code = ip++;
3269 target = ip + 1 + *(signed char*)ip;
3272 if (sp != stack_start) {
3273 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3279 MONO_INST_NEW (cfg, ins, CEE_BR);
3280 ins->cil_code = ip++;
3281 MONO_ADD_INS (bblock, ins);
3282 target = ip + 4 + (gint32)read32(ip);
3284 GET_BBLOCK (cfg, bbhash, tblock, target);
3285 link_bblock (cfg, bblock, tblock);
3286 CHECK_BBLOCK (target, ip, tblock);
3287 ins->inst_target_bb = tblock;
3288 if (sp != stack_start) {
3289 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3292 start_new_bblock = 1;
3298 MONO_INST_NEW (cfg, ins, *ip);
3299 ins->cil_code = ip++;
3300 target = ip + 4 + (gint32)read32(ip);
3302 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
3303 if (sp != stack_start) {
3304 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3320 MONO_INST_NEW (cfg, ins, *ip);
3321 ins->cil_code = ip++;
3322 target = ip + 4 + (gint32)read32(ip);
3325 if (sp != stack_start) {
3326 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3333 n = read32 (ip + 1);
3334 MONO_INST_NEW (cfg, ins, *ip);
3336 ins->inst_left = *sp;
3337 if (ins->inst_left->type != STACK_I4) goto unverified;
3340 target = ip + n * sizeof (guint32);
3341 MONO_ADD_INS (bblock, ins);
3342 GET_BBLOCK (cfg, bbhash, tblock, target);
3343 link_bblock (cfg, bblock, tblock);
3344 ins->klass = GUINT_TO_POINTER (n);
3345 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
3346 ins->inst_many_bb [n] = tblock;
3348 for (i = 0; i < n; ++i) {
3349 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
3350 link_bblock (cfg, bblock, tblock);
3351 ins->inst_many_bb [i] = tblock;
3354 /* FIXME: handle stack args */
3369 MONO_INST_NEW (cfg, ins, *ip);
3374 ins->type = ldind_type [*ip - CEE_LDIND_I1];
3375 ins->flags |= ins_flag;
3387 MONO_INST_NEW (cfg, ins, *ip);
3388 ins->cil_code = ip++;
3390 handle_loaded_temps (cfg, bblock, stack_start, sp);
3391 MONO_ADD_INS (bblock, ins);
3392 ins->inst_i0 = sp [0];
3393 ins->inst_i1 = sp [1];
3394 ins->flags |= ins_flag;
3425 case CEE_CONV_OVF_I8:
3426 case CEE_CONV_OVF_U8:
3432 case CEE_CONV_OVF_I4:
3433 case CEE_CONV_OVF_I1:
3434 case CEE_CONV_OVF_I2:
3435 case CEE_CONV_OVF_I:
3436 case CEE_CONV_OVF_U:
3439 if (sp [-1]->type == STACK_R8) {
3440 ADD_UNOP (CEE_CONV_OVF_I8);
3448 case CEE_CONV_OVF_U1:
3449 case CEE_CONV_OVF_U2:
3450 case CEE_CONV_OVF_U4:
3453 if (sp [-1]->type == STACK_R8) {
3454 ADD_UNOP (CEE_CONV_OVF_U8);
3462 case CEE_CONV_OVF_I1_UN:
3463 case CEE_CONV_OVF_I2_UN:
3464 case CEE_CONV_OVF_I4_UN:
3465 case CEE_CONV_OVF_I8_UN:
3466 case CEE_CONV_OVF_U1_UN:
3467 case CEE_CONV_OVF_U2_UN:
3468 case CEE_CONV_OVF_U4_UN:
3469 case CEE_CONV_OVF_U8_UN:
3470 case CEE_CONV_OVF_I_UN:
3471 case CEE_CONV_OVF_U_UN:
3478 token = read32 (ip + 1);
3479 if (method->wrapper_type != MONO_WRAPPER_NONE)
3480 klass = mono_method_get_wrapper_data (method, token);
3482 klass = mono_class_get (image, token);
3484 mono_class_init (klass);
3485 if (klass->byval_arg.type == MONO_TYPE_VAR)
3486 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3488 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3489 MonoInst *store, *load;
3490 MONO_INST_NEW (cfg, load, CEE_LDIND_REF);
3491 load->cil_code = ip;
3492 load->inst_i0 = sp [1];
3493 load->type = ldind_type [CEE_LDIND_REF];
3494 load->flags |= ins_flag;
3495 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
3496 store->cil_code = ip;
3497 handle_loaded_temps (cfg, bblock, stack_start, sp);
3498 MONO_ADD_INS (bblock, store);
3499 store->inst_i0 = sp [0];
3500 store->inst_i1 = load;
3501 store->flags |= ins_flag;
3503 n = mono_class_value_size (klass, NULL);
3504 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3506 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3507 copy->inst_left = sp [0];
3508 copy->inst_right = sp [1];
3509 copy->cil_code = ip;
3511 MONO_ADD_INS (bblock, copy);
3513 MonoInst *iargs [3];
3516 NEW_ICONST (cfg, iargs [2], n);
3517 iargs [2]->cil_code = ip;
3519 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3526 MonoInst *iargs [3];
3529 token = read32 (ip + 1);
3530 if (method->wrapper_type != MONO_WRAPPER_NONE)
3531 klass = mono_method_get_wrapper_data (method, token);
3533 klass = mono_class_get (image, token);
3535 mono_class_init (klass);
3536 if (klass->byval_arg.type == MONO_TYPE_VAR)
3537 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3538 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3539 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3541 ins->inst_i0 = sp [0];
3542 ins->type = ldind_type [CEE_LDIND_REF];
3543 ins->flags |= ins_flag;
3549 n = mono_class_value_size (klass, NULL);
3550 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3551 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3552 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3554 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3555 copy->inst_left = iargs [0];
3556 copy->inst_right = *sp;
3557 copy->cil_code = ip;
3559 MONO_ADD_INS (bblock, copy);
3562 NEW_ICONST (cfg, iargs [2], n);
3563 iargs [2]->cil_code = ip;
3565 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3567 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3575 CHECK_STACK_OVF (1);
3576 n = read32 (ip + 1);
3578 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3580 MonoInst *iargs [1];
3582 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
3583 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
3584 NEW_TEMPLOAD (cfg, *sp, temp);
3588 if (cfg->opt & MONO_OPT_SHARED) {
3590 MonoInst *iargs [3];
3592 if (mono_compile_aot) {
3593 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n);
3596 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3597 NEW_IMAGECONST (cfg, iargs [1], image);
3598 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
3599 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
3600 NEW_TEMPLOAD (cfg, *sp, temp);
3601 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3603 if (mono_compile_aot)
3604 NEW_LDSTRCONST (cfg, ins, n);
3606 NEW_PCONST (cfg, ins, NULL);
3608 ins->type = STACK_OBJ;
3609 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3619 MonoInst *iargs [2];
3620 MonoMethodSignature *fsig;
3623 token = read32 (ip + 1);
3624 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3625 cmethod = mono_method_get_wrapper_data (method, token);
3627 cmethod = mono_get_method (image, token, NULL);
3628 fsig = mono_method_get_signature (cmethod, image, token);
3630 mono_class_init (cmethod->klass);
3632 n = fsig->param_count;
3635 /* move the args to allow room for 'this' in the first position */
3641 handle_loaded_temps (cfg, bblock, stack_start, sp);
3644 if (cmethod->klass->parent == mono_defaults.array_class) {
3645 NEW_METHODCONST (cfg, *sp, cmethod);
3646 temp = mono_emit_native_call (cfg, bblock, mono_array_new_va, fsig, sp, ip, FALSE);
3648 } else if (cmethod->string_ctor) {
3649 /* we simply pass a null pointer */
3650 NEW_PCONST (cfg, *sp, NULL);
3651 /* now call the string ctor */
3652 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
3654 if (cmethod->klass->valuetype) {
3655 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
3656 temp = iargs [0]->inst_c0;
3657 NEW_TEMPLOADA (cfg, *sp, temp);
3659 if (cfg->opt & MONO_OPT_SHARED) {
3660 NEW_DOMAINCONST (cfg, iargs [0]);
3661 NEW_CLASSCONST (cfg, iargs [1], cmethod->klass);
3663 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3665 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
3666 NEW_VTABLECONST (cfg, iargs [0], vtable);
3667 if (cmethod->klass->has_finalize || cmethod->klass->marshalbyref || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
3668 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
3670 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
3672 NEW_TEMPLOAD (cfg, *sp, temp);
3675 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3676 mono_method_check_inlining (cfg, cmethod) &&
3677 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
3678 !g_list_find (dont_inline, cmethod)) {
3680 MonoBasicBlock *ebblock;
3681 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3686 GET_BBLOCK (cfg, bbhash, bblock, ip);
3687 ebblock->next_bb = bblock;
3688 link_bblock (cfg, ebblock, bblock);
3690 NEW_TEMPLOAD (cfg, *sp, temp);
3693 /* indicates start of a new block, and triggers a load
3694 of all stack arguments at bb boundarie */
3697 inline_costs += costs;
3701 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3704 /* now call the actual ctor */
3705 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3709 NEW_TEMPLOAD (cfg, *sp, temp);
3718 MONO_INST_NEW (cfg, ins, *ip);
3720 klass = mono_class_get (image, read32 (ip + 1));
3721 mono_class_init (klass);
3722 ins->type = STACK_OBJ;
3723 ins->inst_left = *sp;
3724 ins->inst_newa_class = klass;
3729 case CEE_UNBOX_ANY: {
3730 MonoInst *add, *vtoffset;
3731 MonoInst *iargs [3];
3735 token = read32 (ip + 1);
3736 if (method->wrapper_type != MONO_WRAPPER_NONE)
3737 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3739 klass = mono_class_get (image, token);
3740 mono_class_init (klass);
3742 if (klass->byval_arg.type == MONO_TYPE_VAR)
3743 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3745 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3747 MONO_INST_NEW (cfg, ins, CEE_CASTCLASS);
3748 ins->type = STACK_OBJ;
3749 ins->inst_left = *sp;
3751 ins->inst_newa_class = klass;
3758 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
3759 ins->type = STACK_OBJ;
3760 ins->inst_left = *sp;
3762 ins->inst_newa_class = klass;
3765 MONO_INST_NEW (cfg, add, CEE_ADD);
3766 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3767 add->inst_left = ins;
3768 add->inst_right = vtoffset;
3769 add->type = STACK_MP;
3773 n = mono_class_value_size (klass, NULL);
3774 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3775 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3776 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3778 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3779 copy->inst_left = iargs [0];
3780 copy->inst_right = *sp;
3781 copy->cil_code = ip;
3783 MONO_ADD_INS (bblock, copy);
3786 NEW_ICONST (cfg, iargs [2], n);
3787 iargs [2]->cil_code = ip;
3789 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3791 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3797 MonoInst *add, *vtoffset;
3801 token = read32 (ip + 1);
3802 if (method->wrapper_type != MONO_WRAPPER_NONE)
3803 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3805 klass = mono_class_get (image, token);
3806 mono_class_init (klass);
3808 if (klass->byval_arg.type == MONO_TYPE_VAR)
3809 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3811 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
3812 ins->type = STACK_OBJ;
3813 ins->inst_left = *sp;
3815 ins->inst_newa_class = klass;
3818 MONO_INST_NEW (cfg, add, CEE_ADD);
3819 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3820 add->inst_left = ins;
3821 add->inst_right = vtoffset;
3822 add->type = STACK_MP;
3830 MONO_INST_NEW (cfg, ins, *ip);
3832 klass = mono_class_get (image, read32 (ip + 1));
3833 mono_class_init (klass);
3834 if (klass->byval_arg.type == MONO_TYPE_VAR)
3835 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3836 ins->type = STACK_OBJ;
3837 ins->inst_left = *sp;
3839 ins->inst_newa_class = klass;
3846 MONO_INST_NEW (cfg, ins, *ip);
3848 ins->inst_left = *sp;
3849 ins->cil_code = ip++;
3850 MONO_ADD_INS (bblock, ins);
3852 start_new_bblock = 1;
3857 MonoInst *offset_ins;
3858 MonoClassField *field;
3859 MonoBasicBlock *ebblock;
3863 if (*ip == CEE_STFLD) {
3870 // FIXME: enable this test later.
3871 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
3873 token = read32 (ip + 1);
3874 field = mono_field_from_token (image, token, &klass);
3875 if (field->parent->gen_params)
3876 field = get_generic_field_inst (field, method->klass, &klass);
3877 mono_class_init (klass);
3879 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
3880 /* FIXME: mark instructions for use in SSA */
3881 if (*ip == CEE_STFLD) {
3882 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
3883 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
3884 MonoInst *iargs [5];
3887 NEW_CLASSCONST (cfg, iargs [1], klass);
3888 NEW_FIELDCONST (cfg, iargs [2], field);
3889 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
3893 if (cfg->opt & MONO_OPT_INLINE) {
3894 costs = inline_method (cfg, stfld_wrapper, stfld_wrapper->signature, bblock,
3895 iargs, ip, real_offset, dont_inline, &ebblock);
3896 g_assert (costs > 0);
3901 GET_BBLOCK (cfg, bbhash, bblock, ip);
3902 ebblock->next_bb = bblock;
3903 link_bblock (cfg, ebblock, bblock);
3905 /* indicates start of a new block, and triggers a load
3906 of all stack arguments at bb boundarie */
3909 inline_costs += costs;
3912 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, stfld_wrapper->signature, iargs, ip, NULL);
3916 NEW_ICONST (cfg, offset_ins, foffset);
3917 MONO_INST_NEW (cfg, ins, CEE_ADD);
3919 ins->inst_left = *sp;
3920 ins->inst_right = offset_ins;
3921 ins->type = STACK_MP;
3923 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
3924 store->cil_code = ip;
3925 store->inst_left = ins;
3926 store->inst_right = sp [1];
3927 handle_loaded_temps (cfg, bblock, stack_start, sp);
3928 store->flags |= ins_flag;
3930 if (store->opcode == CEE_STOBJ) {
3931 handle_stobj (cfg, bblock, ins, sp [1], ip,
3932 mono_class_from_mono_type (field->type), FALSE, FALSE);
3934 MONO_ADD_INS (bblock, store);
3937 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
3938 /* fixme: we need to inline that call somehow */
3939 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
3940 MonoInst *iargs [4];
3944 NEW_CLASSCONST (cfg, iargs [1], klass);
3945 NEW_FIELDCONST (cfg, iargs [2], field);
3946 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
3947 if (cfg->opt & MONO_OPT_INLINE) {
3948 costs = inline_method (cfg, ldfld_wrapper, ldfld_wrapper->signature, bblock,
3949 iargs, ip, real_offset, dont_inline, &ebblock);
3950 g_assert (costs > 0);
3955 GET_BBLOCK (cfg, bbhash, bblock, ip);
3956 ebblock->next_bb = bblock;
3957 link_bblock (cfg, ebblock, bblock);
3959 temp = iargs [0]->inst_i0->inst_c0;
3961 if (*ip == CEE_LDFLDA) {
3962 /* not sure howto handle this */
3963 NEW_TEMPLOADA (cfg, *sp, temp);
3965 NEW_TEMPLOAD (cfg, *sp, temp);
3969 /* indicates start of a new block, and triggers a load of
3970 all stack arguments at bb boundarie */
3973 inline_costs += costs;
3976 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, ldfld_wrapper->signature, iargs, ip, NULL);
3977 if (*ip == CEE_LDFLDA) {
3978 /* not sure howto handle this */
3979 NEW_TEMPLOADA (cfg, *sp, temp);
3981 NEW_TEMPLOAD (cfg, *sp, temp);
3986 NEW_ICONST (cfg, offset_ins, foffset);
3987 MONO_INST_NEW (cfg, ins, CEE_ADD);
3989 ins->inst_left = *sp;
3990 ins->inst_right = offset_ins;
3991 ins->type = STACK_MP;
3993 if (*ip == CEE_LDFLDA) {
3997 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
3998 type_to_eval_stack_type (field->type, load);
3999 load->cil_code = ip;
4000 load->inst_left = ins;
4001 load->flags |= ins_flag;
4013 MonoClassField *field;
4014 gpointer addr = NULL;
4016 token = read32 (ip + 1);
4018 field = mono_field_from_token (image, token, &klass);
4019 mono_class_init (klass);
4021 handle_loaded_temps (cfg, bblock, stack_start, sp);
4023 if (cfg->domain->thread_static_fields)
4024 addr = g_hash_table_lookup (cfg->domain->thread_static_fields, field);
4026 if ((cfg->opt & MONO_OPT_SHARED) || (mono_compile_aot && addr)) {
4028 MonoInst *iargs [2];
4029 g_assert (field->parent);
4030 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
4031 NEW_FIELDCONST (cfg, iargs [1], field);
4032 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
4033 NEW_TEMPLOAD (cfg, ins, temp);
4036 vtable = mono_class_vtable (cfg->domain, klass);
4038 if ((!vtable->initialized || mono_compile_aot) && !(klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (klass, method)) {
4039 guint8 *tramp = mono_create_class_init_trampoline (vtable);
4040 mono_emit_native_call (cfg, bblock, tramp,
4041 helper_sig_class_init_trampoline,
4043 if (cfg->verbose_level > 2)
4044 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
4046 mono_runtime_class_init (vtable);
4048 addr = (char*)vtable->data + field->offset;
4050 if (mono_compile_aot)
4051 NEW_SFLDACONST (cfg, ins, field);
4053 NEW_PCONST (cfg, ins, addr);
4057 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
4058 * This could be later optimized to do just a couple of
4059 * memory dereferences with constant offsets.
4062 MonoInst *iargs [1];
4063 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
4064 temp = mono_emit_jit_icall (cfg, bblock, mono_threads_get_static_data, iargs, ip);
4065 NEW_TEMPLOAD (cfg, ins, temp);
4069 /* FIXME: mark instructions for use in SSA */
4070 if (*ip == CEE_LDSFLDA) {
4072 } else if (*ip == CEE_STSFLD) {
4076 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4077 store->cil_code = ip;
4078 store->inst_left = ins;
4079 store->inst_right = sp [0];
4080 store->flags |= ins_flag;
4083 if (store->opcode == CEE_STOBJ) {
4084 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
4086 MONO_ADD_INS (bblock, store);
4088 gboolean is_const = FALSE;
4089 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4090 if (!((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) &&
4091 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
4092 gpointer addr = (char*)vtable->data + field->offset;
4093 /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
4095 switch (field->type->type) {
4096 case MONO_TYPE_BOOLEAN:
4098 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
4102 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
4105 case MONO_TYPE_CHAR:
4107 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
4111 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
4116 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
4120 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
4125 case MONO_TYPE_STRING:
4126 case MONO_TYPE_OBJECT:
4127 case MONO_TYPE_CLASS:
4128 case MONO_TYPE_SZARRAY:
4130 case MONO_TYPE_FNPTR:
4131 case MONO_TYPE_ARRAY:
4132 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
4133 type_to_eval_stack_type (field->type, *sp);
4138 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
4139 sp [0]->type = STACK_I8;
4140 sp [0]->inst_l = *((gint64 *)addr);
4145 case MONO_TYPE_VALUETYPE:
4154 CHECK_STACK_OVF (1);
4155 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4156 type_to_eval_stack_type (field->type, load);
4157 load->cil_code = ip;
4158 load->inst_left = ins;
4160 load->flags |= ins_flag;
4162 /* fixme: dont see the problem why this does not work */
4163 //cfg->disable_aot = TRUE;
4172 token = read32 (ip + 1);
4173 if (method->wrapper_type != MONO_WRAPPER_NONE)
4174 klass = mono_method_get_wrapper_data (method, token);
4176 klass = mono_class_get (image, token);
4177 mono_class_init (klass);
4178 if (klass->byval_arg.type == MONO_TYPE_VAR)
4179 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4180 n = mono_type_to_stind (&klass->byval_arg);
4181 if (n == CEE_STOBJ) {
4182 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
4184 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
4186 MONO_INST_NEW (cfg, store, n);
4187 store->cil_code = ip;
4188 store->inst_left = sp [0];
4189 store->inst_right = sp [1];
4190 store->flags |= ins_flag;
4191 MONO_ADD_INS (bblock, store);
4198 MonoInst *iargs [2];
4199 MonoInst *load, *vtoffset, *add, *val, *vstore;
4204 token = read32 (ip + 1);
4205 if (method->wrapper_type != MONO_WRAPPER_NONE)
4206 klass = mono_method_get_wrapper_data (method, token);
4208 klass = mono_class_get (image, token);
4209 mono_class_init (klass);
4210 if (klass->byval_arg.type == MONO_TYPE_VAR)
4211 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4213 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4218 /* much like NEWOBJ */
4219 if (cfg->opt & MONO_OPT_SHARED) {
4220 NEW_DOMAINCONST (cfg, iargs [0]);
4221 NEW_CLASSCONST (cfg, iargs [1], klass);
4223 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
4225 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4226 NEW_VTABLECONST (cfg, iargs [0], vtable);
4227 if (1 || klass->has_finalize || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
4228 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
4230 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
4232 NEW_TEMPLOAD (cfg, load, temp);
4233 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4234 MONO_INST_NEW (cfg, add, CEE_ADD);
4235 add->inst_left = load;
4236 add->inst_right = vtoffset;
4239 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
4240 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
4241 vstore->cil_code = ip;
4242 vstore->inst_left = add;
4243 vstore->inst_right = val;
4245 if (vstore->opcode == CEE_STOBJ) {
4246 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
4248 MONO_ADD_INS (bblock, vstore);
4250 NEW_TEMPLOAD (cfg, load, temp);
4258 MONO_INST_NEW (cfg, ins, *ip);
4262 token = read32 (ip + 1);
4264 /* allocate the domainvar - becaus this is used in decompose_foreach */
4265 if (cfg->opt & MONO_OPT_SHARED)
4266 mono_get_domainvar (cfg);
4268 if (method->wrapper_type != MONO_WRAPPER_NONE)
4269 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4271 klass = mono_class_get (image, token);
4273 mono_class_init (klass);
4274 if (klass->byval_arg.type == MONO_TYPE_VAR)
4275 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4276 ins->inst_newa_class = klass;
4277 ins->inst_newa_len = *sp;
4278 ins->type = STACK_OBJ;
4285 MONO_INST_NEW (cfg, ins, *ip);
4286 ins->cil_code = ip++;
4288 ins->inst_left = *sp;
4289 ins->type = STACK_PTR;
4295 klass = mono_class_get (image, read32 (ip + 1));
4296 mono_class_init (klass);
4297 if (klass->byval_arg.type == MONO_TYPE_VAR)
4298 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4299 NEW_LDELEMA (cfg, ins, sp, klass);
4308 token = read32 (ip + 1);
4309 klass = mono_class_get (image, token);
4310 mono_class_init (klass);
4311 if (klass->byval_arg.type == MONO_TYPE_VAR)
4312 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4313 NEW_LDELEMA (cfg, load, sp, klass);
4314 load->cil_code = ip;
4315 MONO_INST_NEW (cfg, ins, mono_type_to_ldind (&klass->byval_arg));
4317 ins->inst_left = load;
4319 type_to_eval_stack_type (&klass->byval_arg, ins);
4333 case CEE_LDELEM_REF: {
4337 * ldind.x (ldelema (array, index))
4338 * ldelema does the bounds check
4342 klass = array_access_to_klass (*ip);
4343 NEW_LDELEMA (cfg, load, sp, klass);
4344 load->cil_code = ip;
4345 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
4347 ins->inst_left = load;
4349 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
4359 case CEE_STELEM_R8: {
4363 * stind.x (ldelema (array, index), val)
4364 * ldelema does the bounds check
4368 klass = array_access_to_klass (*ip);
4369 NEW_LDELEMA (cfg, load, sp, klass);
4370 load->cil_code = ip;
4371 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
4373 ins->inst_left = load;
4374 ins->inst_right = sp [2];
4376 handle_loaded_temps (cfg, bblock, stack_start, sp);
4377 MONO_ADD_INS (bblock, ins);
4379 cfg->disable_ssa = TRUE;
4386 * stind.x (ldelema (array, index), val)
4387 * ldelema does the bounds check
4391 token = read32 (ip + 1);
4392 klass = mono_class_get (image, token);
4393 mono_class_init (klass);
4394 if (klass->byval_arg.type == MONO_TYPE_VAR)
4395 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4396 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4397 MonoInst *iargs [3];
4398 handle_loaded_temps (cfg, bblock, stack_start, sp);
4404 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4406 NEW_LDELEMA (cfg, load, sp, klass);
4407 load->cil_code = ip;
4408 MONO_INST_NEW (cfg, ins, mono_type_to_stind (&klass->byval_arg));
4410 ins->inst_left = load;
4411 ins->inst_right = sp [2];
4412 handle_loaded_temps (cfg, bblock, stack_start, sp);
4413 MONO_ADD_INS (bblock, ins);
4417 cfg->disable_ssa = TRUE;
4420 case CEE_STELEM_REF: {
4421 MonoInst *iargs [3];
4426 handle_loaded_temps (cfg, bblock, stack_start, sp);
4432 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4436 NEW_GROUP (cfg, group, sp [0], sp [1]);
4437 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
4439 ins->inst_left = group;
4440 ins->inst_right = sp [2];
4441 MONO_ADD_INS (bblock, ins);
4446 cfg->disable_ssa = TRUE;
4449 case CEE_CKFINITE: {
4450 MonoInst *store, *temp;
4453 /* this instr. can throw exceptions as side effect,
4454 * so we cant eliminate dead code which contains CKFINITE opdodes.
4455 * Spilling to memory makes sure that we always perform
4459 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
4461 ins->inst_left = sp [-1];
4462 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
4464 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4465 store->cil_code = ip;
4466 MONO_ADD_INS (bblock, store);
4468 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
4475 g_error ("opcode 0x%02x not handled", *ip);
4479 MonoClass *handle_class;
4481 CHECK_STACK_OVF (1);
4483 n = read32 (ip + 1);
4485 handle = mono_ldtoken (image, n, &handle_class);
4486 mono_class_init (handle_class);
4488 if (cfg->opt & MONO_OPT_SHARED) {
4490 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
4492 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
4494 NEW_IMAGECONST (cfg, iargs [0], image);
4495 NEW_ICONST (cfg, iargs [1], n);
4496 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
4497 NEW_TEMPLOAD (cfg, res, temp);
4498 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4499 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
4500 MONO_ADD_INS (bblock, store);
4501 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4503 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method (image, read32 (ip + 6), NULL)) &&
4504 (cmethod->klass == mono_defaults.monotype_class->parent) &&
4505 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
4506 MonoClass *tclass = mono_class_from_mono_type (handle);
4507 mono_class_init (tclass);
4508 if (mono_compile_aot)
4509 NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, n);
4511 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
4512 ins->type = STACK_OBJ;
4513 ins->klass = cmethod->klass;
4516 if (mono_compile_aot)
4517 NEW_LDTOKENCONST (cfg, ins, n);
4519 NEW_PCONST (cfg, ins, handle);
4520 ins->type = STACK_VTYPE;
4521 ins->klass = handle_class;
4537 case CEE_ADD_OVF_UN:
4539 case CEE_MUL_OVF_UN:
4541 case CEE_SUB_OVF_UN:
4546 case CEE_ENDFINALLY:
4547 /* FIXME: check stack state */
4548 MONO_INST_NEW (cfg, ins, *ip);
4549 MONO_ADD_INS (bblock, ins);
4550 ins->cil_code = ip++;
4551 start_new_bblock = 1;
4556 if (*ip == CEE_LEAVE) {
4557 target = ip + 5 + (gint32)read32(ip + 1);
4559 target = ip + 2 + (signed char)(ip [1]);
4562 /* empty the stack */
4563 while (sp != stack_start) {
4564 MONO_INST_NEW (cfg, ins, CEE_POP);
4568 MONO_ADD_INS (bblock, ins);
4571 /* fixme: call fault handler ? */
4573 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
4575 for (tmp = handlers; tmp; tmp = tmp->next) {
4577 link_bblock (cfg, bblock, tblock);
4578 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
4580 ins->inst_target_bb = tblock;
4581 MONO_ADD_INS (bblock, ins);
4583 g_list_free (handlers);
4586 MONO_INST_NEW (cfg, ins, CEE_BR);
4588 MONO_ADD_INS (bblock, ins);
4589 GET_BBLOCK (cfg, bbhash, tblock, target);
4590 link_bblock (cfg, bblock, tblock);
4591 CHECK_BBLOCK (target, ip, tblock);
4592 ins->inst_target_bb = tblock;
4593 start_new_bblock = 1;
4595 if (*ip == CEE_LEAVE)
4604 MONO_INST_NEW (cfg, ins, *ip);
4606 handle_loaded_temps (cfg, bblock, stack_start, sp);
4607 MONO_ADD_INS (bblock, ins);
4608 ins->cil_code = ip++;
4609 ins->inst_i0 = sp [0];
4610 ins->inst_i1 = sp [1];
4618 /* trampoline mono specific opcodes */
4619 case MONO_CUSTOM_PREFIX: {
4621 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
4625 case CEE_MONO_FUNC1: {
4627 gpointer func = NULL;
4632 case MONO_MARSHAL_CONV_STR_LPWSTR:
4633 func = mono_string_to_utf16;
4635 case MONO_MARSHAL_CONV_LPWSTR_STR:
4636 func = mono_string_from_utf16;
4638 case MONO_MARSHAL_CONV_LPSTR_STR:
4639 func = mono_string_new_wrapper;
4641 case MONO_MARSHAL_CONV_STR_LPTSTR:
4642 case MONO_MARSHAL_CONV_STR_LPSTR:
4643 func = mono_string_to_utf8;
4645 case MONO_MARSHAL_CONV_STR_BSTR:
4646 func = mono_string_to_bstr;
4648 case MONO_MARSHAL_CONV_STR_TBSTR:
4649 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
4650 func = mono_string_to_ansibstr;
4652 case MONO_MARSHAL_CONV_SB_LPSTR:
4653 func = mono_string_builder_to_utf8;
4655 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
4656 func = mono_array_to_savearray;
4658 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
4659 func = mono_array_to_lparray;
4661 case MONO_MARSHAL_CONV_DEL_FTN:
4662 func = mono_delegate_to_ftnptr;
4664 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
4665 func = mono_marshal_string_array;
4668 g_warning ("unknown conversion %d\n", ip [2]);
4669 g_assert_not_reached ();
4672 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4673 NEW_TEMPLOAD (cfg, *sp, temp);
4677 inline_costs += 10 * num_calls++;
4680 case CEE_MONO_PROC2: {
4681 gpointer func = NULL;
4686 case MONO_MARSHAL_CONV_LPSTR_SB:
4687 func = mono_string_utf8_to_builder;
4689 case MONO_MARSHAL_FREE_ARRAY:
4690 func = mono_marshal_free_array;
4693 g_assert_not_reached ();
4696 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4698 inline_costs += 10 * num_calls++;
4701 case CEE_MONO_PROC3: {
4702 gpointer func = NULL;
4707 case MONO_MARSHAL_CONV_STR_BYVALSTR:
4708 func = mono_string_to_byvalstr;
4710 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
4711 func = mono_string_to_byvalwstr;
4714 g_assert_not_reached ();
4717 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4719 inline_costs += 10 * num_calls++;
4725 mono_emit_jit_icall (cfg, bblock, g_free, sp, ip);
4727 inline_costs += 10 * num_calls++;
4729 case CEE_MONO_LDPTR:
4730 CHECK_STACK_OVF (1);
4731 token = read32 (ip + 2);
4732 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
4736 inline_costs += 10 * num_calls++;
4738 case CEE_MONO_VTADDR:
4741 MONO_INST_NEW (cfg, ins, OP_VTADDR);
4743 ins->type = STACK_MP;
4744 ins->inst_left = *sp;
4748 case CEE_MONO_NEWOBJ: {
4749 MonoInst *iargs [2];
4751 CHECK_STACK_OVF (1);
4752 token = read32 (ip + 2);
4753 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4754 mono_class_init (klass);
4755 NEW_DOMAINCONST (cfg, iargs [0]);
4756 NEW_CLASSCONST (cfg, iargs [1], klass);
4757 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
4758 NEW_TEMPLOAD (cfg, *sp, temp);
4761 inline_costs += 10 * num_calls++;
4764 case CEE_MONO_OBJADDR:
4767 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
4769 ins->type = STACK_MP;
4770 ins->inst_left = *sp;
4774 case CEE_MONO_LDNATIVEOBJ:
4776 token = read32 (ip + 2);
4777 klass = mono_method_get_wrapper_data (method, token);
4778 g_assert (klass->valuetype);
4779 mono_class_init (klass);
4780 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
4784 case CEE_MONO_RETOBJ:
4785 g_assert (cfg->ret);
4786 g_assert (method->signature->pinvoke);
4790 token = read32 (ip + 2);
4791 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4793 NEW_RETLOADA (cfg, ins);
4794 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
4796 if (sp != stack_start)
4799 MONO_INST_NEW (cfg, ins, CEE_BR);
4801 ins->inst_target_bb = end_bblock;
4802 MONO_ADD_INS (bblock, ins);
4803 link_bblock (cfg, bblock, end_bblock);
4804 start_new_bblock = 1;
4808 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
4816 /* somewhat similar to LDTOKEN */
4817 MonoInst *addr, *vtvar;
4818 CHECK_STACK_OVF (1);
4819 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
4821 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4822 addr->cil_code = ip;
4823 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
4825 ins->inst_left = addr;
4826 MONO_ADD_INS (bblock, ins);
4827 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4840 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
4841 MONO_INST_NEW (cfg, ins, cmp->opcode);
4843 cmp->inst_i0 = sp [0];
4844 cmp->inst_i1 = sp [1];
4848 cmp->opcode = OP_COMPARE;
4850 ins->type = STACK_I4;
4860 CHECK_STACK_OVF (1);
4861 n = read32 (ip + 2);
4862 if (method->wrapper_type != MONO_WRAPPER_NONE)
4863 cmethod = mono_method_get_wrapper_data (method, n);
4865 cmethod = mono_get_method (image, n, NULL);
4868 * We can't do this in mono_ldftn, since it is used in
4869 * the synchronized wrapper, leading to an infinite loop.
4871 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
4872 cmethod = mono_marshal_get_synchronized_wrapper (cmethod);
4875 mono_class_init (cmethod->klass);
4876 handle_loaded_temps (cfg, bblock, stack_start, sp);
4878 NEW_METHODCONST (cfg, argconst, cmethod);
4879 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
4880 NEW_TEMPLOAD (cfg, *sp, temp);
4884 inline_costs += 10 * num_calls++;
4887 case CEE_LDVIRTFTN: {
4892 n = read32 (ip + 2);
4893 if (method->wrapper_type != MONO_WRAPPER_NONE)
4894 cmethod = mono_method_get_wrapper_data (method, n);
4896 cmethod = mono_get_method (image, n, NULL);
4898 mono_class_init (cmethod->klass);
4899 handle_loaded_temps (cfg, bblock, stack_start, sp);
4903 NEW_METHODCONST (cfg, args [1], cmethod);
4904 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
4905 NEW_TEMPLOAD (cfg, *sp, temp);
4909 inline_costs += 10 * num_calls++;
4913 CHECK_STACK_OVF (1);
4914 NEW_ARGLOAD (cfg, ins, read16 (ip + 2));
4920 CHECK_STACK_OVF (1);
4921 NEW_ARGLOADA (cfg, ins, read16 (ip + 2));
4929 handle_loaded_temps (cfg, bblock, stack_start, sp);
4930 n = read16 (ip + 2);
4931 NEW_ARGSTORE (cfg, ins, n, *sp);
4933 if (ins->opcode == CEE_STOBJ) {
4934 NEW_ARGLOADA (cfg, ins, n);
4935 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4937 MONO_ADD_INS (bblock, ins);
4941 CHECK_STACK_OVF (1);
4942 NEW_LOCLOAD (cfg, ins, read16 (ip + 2));
4948 CHECK_STACK_OVF (1);
4949 NEW_LOCLOADA (cfg, ins, read16 (ip + 2));
4957 n = read16 (ip + 2);
4958 handle_loaded_temps (cfg, bblock, stack_start, sp);
4959 NEW_LOCSTORE (cfg, ins, n, *sp);
4961 if (ins->opcode == CEE_STOBJ) {
4962 NEW_LOCLOADA (cfg, ins, n);
4963 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4965 MONO_ADD_INS (bblock, ins);
4972 if (sp != stack_start)
4974 MONO_INST_NEW (cfg, ins, 256 + ip [1]);
4975 ins->inst_left = *sp;
4978 if (header->init_locals)
4979 ins->flags |= MONO_INST_INIT;
4983 /* FIXME: set init flag if locals init is set in this method */
4985 case CEE_ENDFILTER: {
4986 MonoExceptionClause *clause, *nearest;
4987 int cc, nearest_num;
4991 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
4993 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
4994 ins->inst_left = *sp;
4996 MONO_ADD_INS (bblock, ins);
4997 start_new_bblock = 1;
5001 for (cc = 0; cc < header->num_clauses; ++cc) {
5002 clause = &header->clauses [cc];
5003 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
5004 (!nearest || (clause->token_or_filter > nearest->token_or_filter))) {
5010 filter_lengths [nearest_num] = (ip - header->code) - nearest->token_or_filter;
5014 case CEE_UNALIGNED_:
5015 ins_flag |= MONO_INST_UNALIGNED;
5019 ins_flag |= MONO_INST_VOLATILE;
5023 ins_flag |= MONO_INST_TAILCALL;
5029 token = read32 (ip + 2);
5030 if (method->wrapper_type != MONO_WRAPPER_NONE)
5031 klass = mono_method_get_wrapper_data (method, token);
5033 klass = mono_class_get (image, token);
5034 if (klass->byval_arg.type == MONO_TYPE_VAR)
5035 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
5036 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5037 MonoInst *store, *load;
5038 NEW_PCONST (cfg, load, NULL);
5039 load->cil_code = ip;
5040 load->type = STACK_OBJ;
5041 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
5042 store->cil_code = ip;
5043 handle_loaded_temps (cfg, bblock, stack_start, sp);
5044 MONO_ADD_INS (bblock, store);
5045 store->inst_i0 = sp [0];
5046 store->inst_i1 = load;
5049 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
5054 case CEE_CONSTRAINED_:
5055 /* FIXME: implement */
5056 token = read32 (ip + 2);
5061 MonoInst *iargs [3];
5067 handle_loaded_temps (cfg, bblock, stack_start, sp);
5068 if (ip [1] == CEE_CPBLK) {
5069 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
5071 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
5079 ins_flag |= MONO_INST_NOTYPECHECK;
5081 ins_flag |= MONO_INST_NORANGECHECK;
5082 /* we ignore the no-nullcheck for now since we
5083 * really do it explicitly only when doing callvirt->call
5089 /* FIXME: check we are in a catch handler */
5090 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
5091 load->cil_code = ip;
5092 MONO_INST_NEW (cfg, ins, CEE_THROW);
5093 ins->inst_left = load;
5095 MONO_ADD_INS (bblock, ins);
5097 start_new_bblock = 1;
5102 CHECK_STACK_OVF (1);
5103 token = read32 (ip + 2);
5104 /* FIXXME: handle generics. */
5105 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
5106 MonoType *type = mono_type_create_from_typespec (image, token);
5107 token = mono_type_size (type, &align);
5108 mono_metadata_free_type (type);
5110 MonoClass *szclass = mono_class_get (image, token);
5111 mono_class_init (szclass);
5112 token = mono_class_value_size (szclass, &align);
5114 NEW_ICONST (cfg, ins, token);
5119 case CEE_REFANYTYPE:
5120 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
5126 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
5131 g_error ("opcode 0x%02x not handled", *ip);
5134 if (start_new_bblock != 1)
5137 bblock->cil_length = ip - bblock->cil_code;
5138 bblock->next_bb = end_bblock;
5139 link_bblock (cfg, bblock, end_bblock);
5141 if (cfg->method == method && cfg->domainvar) {
5145 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
5146 call->signature = helper_sig_domain_get;
5147 call->inst.type = STACK_PTR;
5148 call->fptr = mono_domain_get;
5149 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, (MonoInst*)call);
5151 MONO_ADD_INS (init_localsbb, store);
5154 if (header->init_locals) {
5156 for (i = 0; i < header->num_locals; ++i) {
5157 int t = header->locals [i]->type;
5158 if (t == MONO_TYPE_VALUETYPE && header->locals [i]->data.klass->enumtype)
5159 t = header->locals [i]->data.klass->enum_basetype->type;
5160 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
5161 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
5162 NEW_ICONST (cfg, ins, 0);
5163 NEW_LOCSTORE (cfg, store, i, ins);
5164 MONO_ADD_INS (init_localsbb, store);
5165 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
5166 MONO_INST_NEW (cfg, ins, OP_I8CONST);
5167 ins->type = STACK_I8;
5169 NEW_LOCSTORE (cfg, store, i, ins);
5170 MONO_ADD_INS (init_localsbb, store);
5171 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
5172 MONO_INST_NEW (cfg, ins, OP_R8CONST);
5173 ins->type = STACK_R8;
5174 ins->inst_p0 = (void*)&r8_0;
5175 NEW_LOCSTORE (cfg, store, i, ins);
5176 MONO_ADD_INS (init_localsbb, store);
5177 } else if (t == MONO_TYPE_VALUETYPE) {
5178 NEW_LOCLOADA (cfg, ins, i);
5179 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (header->locals [i]), NULL, NULL);
5182 NEW_PCONST (cfg, ins, NULL);
5183 NEW_LOCSTORE (cfg, store, i, ins);
5184 MONO_ADD_INS (init_localsbb, store);
5190 /* resolve backward branches in the middle of an existing basic block */
5191 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
5193 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
5194 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
5195 if (tblock != start_bblock) {
5197 split_bblock (cfg, tblock, bblock);
5198 l = bblock->cil_code - header->code;
5199 bblock->cil_length = tblock->cil_length - l;
5200 tblock->cil_length = l;
5202 g_print ("recheck failed.\n");
5206 /* we compute regions here, because the length of filter clauses is not known in advance.
5207 * It is computed in the CEE_ENDFILTER case in the above switch statement*/
5208 if (cfg->method == method) {
5210 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5211 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
5212 if (cfg->verbose_level > 2)
5213 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
5216 g_hash_table_destroy (bbhash);
5219 dont_inline = g_list_remove (dont_inline, method);
5220 return inline_costs;
5223 if (cfg->method != method)
5224 g_hash_table_destroy (bbhash);
5225 dont_inline = g_list_remove (dont_inline, method);
5229 if (cfg->method != method)
5230 g_hash_table_destroy (bbhash);
5231 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
5232 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
5233 dont_inline = g_list_remove (dont_inline, method);
5238 mono_print_tree (MonoInst *tree) {
5244 arity = mono_burg_arity [tree->opcode];
5246 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
5248 switch (tree->opcode) {
5250 printf ("[%d]", tree->inst_c0);
5253 printf ("[%lld]", tree->inst_l);
5256 printf ("[%f]", *(double*)tree->inst_p0);
5259 printf ("[%f]", *(float*)tree->inst_p0);
5263 printf ("[%d]", tree->inst_c0);
5266 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5269 printf ("[%s]", mono_arch_regname (tree->dreg));
5272 printf ("[%s]", tree->inst_newa_class->name);
5273 mono_print_tree (tree->inst_newa_len);
5284 case OP_VOIDCALLVIRT: {
5285 MonoCallInst *call = (MonoCallInst*)tree;
5287 printf ("[%s]", call->method->name);
5292 printf ("[%d (", tree->inst_c0);
5293 for (i = 0; i < tree->inst_phi_args [0]; i++) {
5296 printf ("%d", tree->inst_phi_args [i + 1]);
5308 printf ("[B%d]", tree->inst_target_bb->block_num);
5318 case OP_VOIDCALL_REG:
5319 mono_print_tree (tree->inst_left);
5331 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
5332 mono_print_tree (tree->inst_left);
5336 mono_print_tree (tree->inst_left);
5338 mono_print_tree (tree->inst_right);
5348 mono_print_tree_nl (MonoInst *tree)
5350 mono_print_tree (tree);
5355 create_helper_signature (void)
5357 /* FIXME: set call conv */
5358 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
5359 helper_sig_newarr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5360 helper_sig_newarr->params [0] = helper_sig_newarr->params [1] = &mono_defaults.int_class->byval_arg;
5361 helper_sig_newarr->ret = &mono_defaults.object_class->byval_arg;
5362 helper_sig_newarr->params [2] = &mono_defaults.int32_class->byval_arg;
5363 helper_sig_newarr->pinvoke = 1;
5365 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
5366 helper_sig_newarr_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5367 helper_sig_newarr_specific->params [0] = &mono_defaults.int_class->byval_arg;
5368 helper_sig_newarr_specific->params [1] = &mono_defaults.int32_class->byval_arg;
5369 helper_sig_newarr_specific->ret = &mono_defaults.object_class->byval_arg;
5370 helper_sig_newarr_specific->pinvoke = 1;
5372 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
5373 helper_sig_object_new = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5374 helper_sig_object_new->params [0] = helper_sig_object_new->params [1] = &mono_defaults.int_class->byval_arg;
5375 helper_sig_object_new->ret = &mono_defaults.object_class->byval_arg;
5376 helper_sig_object_new->pinvoke = 1;
5378 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
5379 helper_sig_object_new_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5380 helper_sig_object_new_specific->params [0] = &mono_defaults.int_class->byval_arg;
5381 helper_sig_object_new_specific->ret = &mono_defaults.object_class->byval_arg;
5382 helper_sig_object_new_specific->pinvoke = 1;
5384 /* void* mono_method_compile (MonoMethod*) */
5385 helper_sig_compile = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5386 helper_sig_compile->params [0] = helper_sig_compile->ret = &mono_defaults.int_class->byval_arg;
5387 helper_sig_compile->pinvoke = 1;
5389 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
5390 helper_sig_compile_virt = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5391 helper_sig_compile_virt->params [0] = &mono_defaults.object_class->byval_arg;
5392 helper_sig_compile_virt->params [1] = helper_sig_compile_virt->ret = &mono_defaults.int_class->byval_arg;
5393 helper_sig_compile_virt->pinvoke = 1;
5395 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
5396 helper_sig_ldstr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5397 helper_sig_ldstr->params [0] = helper_sig_ldstr->params [1] = &mono_defaults.int_class->byval_arg;
5398 helper_sig_ldstr->params [2] = &mono_defaults.int32_class->byval_arg;
5399 helper_sig_ldstr->ret = &mono_defaults.object_class->byval_arg;
5400 helper_sig_ldstr->pinvoke = 1;
5402 /* MonoDomain *mono_domain_get (void) */
5403 helper_sig_domain_get = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5404 helper_sig_domain_get->ret = &mono_defaults.int_class->byval_arg;
5405 helper_sig_domain_get->pinvoke = 1;
5407 /* void* stelem_ref (MonoArray *, int index, MonoObject *) */
5408 helper_sig_stelem_ref = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5409 helper_sig_stelem_ref->params [0] = &mono_defaults.array_class->byval_arg;
5410 helper_sig_stelem_ref->params [1] = &mono_defaults.int32_class->byval_arg;
5411 helper_sig_stelem_ref->params [2] = &mono_defaults.object_class->byval_arg;
5412 helper_sig_stelem_ref->ret = &mono_defaults.void_class->byval_arg;
5413 helper_sig_stelem_ref->pinvoke = 1;
5415 /* long amethod (long, long) */
5416 helper_sig_long_long_long = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5417 helper_sig_long_long_long->params [0] = helper_sig_long_long_long->params [1] =
5418 &mono_defaults.int64_class->byval_arg;
5419 helper_sig_long_long_long->ret = &mono_defaults.int64_class->byval_arg;
5420 helper_sig_long_long_long->pinvoke = 1;
5422 /* object amethod (intptr) */
5423 helper_sig_obj_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5424 helper_sig_obj_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5425 helper_sig_obj_ptr->ret = &mono_defaults.object_class->byval_arg;
5426 helper_sig_obj_ptr->pinvoke = 1;
5428 /* void amethod (intptr) */
5429 helper_sig_void_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5430 helper_sig_void_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5431 helper_sig_void_ptr->ret = &mono_defaults.void_class->byval_arg;
5432 helper_sig_void_ptr->pinvoke = 1;
5434 /* void amethod (MonoObject *obj) */
5435 helper_sig_void_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5436 helper_sig_void_obj->params [0] = &mono_defaults.object_class->byval_arg;
5437 helper_sig_void_obj->ret = &mono_defaults.void_class->byval_arg;
5438 helper_sig_void_obj->pinvoke = 1;
5440 /* intptr amethod (void) */
5441 helper_sig_ptr_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5442 helper_sig_ptr_void->ret = &mono_defaults.int_class->byval_arg;
5443 helper_sig_ptr_void->pinvoke = 1;
5445 /* void amethod (intptr, intptr) */
5446 helper_sig_void_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5447 helper_sig_void_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5448 helper_sig_void_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5449 helper_sig_void_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
5450 helper_sig_void_ptr_ptr->pinvoke = 1;
5452 /* void amethod (intptr, intptr, intptr) */
5453 helper_sig_void_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5454 helper_sig_void_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5455 helper_sig_void_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5456 helper_sig_void_ptr_ptr_ptr->params [2] = &mono_defaults.int_class->byval_arg;
5457 helper_sig_void_ptr_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
5458 helper_sig_void_ptr_ptr_ptr->pinvoke = 1;
5460 /* intptr amethod (intptr, intptr) */
5461 helper_sig_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5462 helper_sig_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5463 helper_sig_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5464 helper_sig_ptr_ptr_ptr->ret = &mono_defaults.int_class->byval_arg;
5465 helper_sig_ptr_ptr_ptr->pinvoke = 1;
5467 /* IntPtr amethod (object) */
5468 helper_sig_ptr_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5469 helper_sig_ptr_obj->params [0] = &mono_defaults.object_class->byval_arg;
5470 helper_sig_ptr_obj->ret = &mono_defaults.int_class->byval_arg;
5471 helper_sig_ptr_obj->pinvoke = 1;
5473 /* IntPtr amethod (int) */
5474 helper_sig_ptr_int = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5475 helper_sig_ptr_int->params [0] = &mono_defaults.int32_class->byval_arg;
5476 helper_sig_ptr_int->ret = &mono_defaults.int_class->byval_arg;
5477 helper_sig_ptr_int->pinvoke = 1;
5479 /* long amethod (long, guint32) */
5480 helper_sig_long_long_int = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5481 helper_sig_long_long_int->params [0] = &mono_defaults.int64_class->byval_arg;
5482 helper_sig_long_long_int->params [1] = &mono_defaults.int32_class->byval_arg;
5483 helper_sig_long_long_int->ret = &mono_defaults.int64_class->byval_arg;
5484 helper_sig_long_long_int->pinvoke = 1;
5486 /* ulong amethod (double) */
5487 helper_sig_ulong_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5488 helper_sig_ulong_double->params [0] = &mono_defaults.double_class->byval_arg;
5489 helper_sig_ulong_double->ret = &mono_defaults.uint64_class->byval_arg;
5490 helper_sig_ulong_double->pinvoke = 1;
5492 /* long amethod (double) */
5493 helper_sig_long_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5494 helper_sig_long_double->params [0] = &mono_defaults.double_class->byval_arg;
5495 helper_sig_long_double->ret = &mono_defaults.int64_class->byval_arg;
5496 helper_sig_long_double->pinvoke = 1;
5498 /* uint amethod (double) */
5499 helper_sig_uint_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5500 helper_sig_uint_double->params [0] = &mono_defaults.double_class->byval_arg;
5501 helper_sig_uint_double->ret = &mono_defaults.uint32_class->byval_arg;
5502 helper_sig_uint_double->pinvoke = 1;
5504 /* int amethod (double) */
5505 helper_sig_int_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5506 helper_sig_int_double->params [0] = &mono_defaults.double_class->byval_arg;
5507 helper_sig_int_double->ret = &mono_defaults.int32_class->byval_arg;
5508 helper_sig_int_double->pinvoke = 1;
5510 /* void initobj (intptr, int size) */
5511 helper_sig_initobj = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5512 helper_sig_initobj->params [0] = &mono_defaults.int_class->byval_arg;
5513 helper_sig_initobj->params [1] = &mono_defaults.int32_class->byval_arg;
5514 helper_sig_initobj->ret = &mono_defaults.void_class->byval_arg;
5515 helper_sig_initobj->pinvoke = 1;
5517 /* void memcpy (intptr, intptr, int size) */
5518 helper_sig_memcpy = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5519 helper_sig_memcpy->params [0] = &mono_defaults.int_class->byval_arg;
5520 helper_sig_memcpy->params [1] = &mono_defaults.int_class->byval_arg;
5521 helper_sig_memcpy->params [2] = &mono_defaults.int32_class->byval_arg;
5522 helper_sig_memcpy->ret = &mono_defaults.void_class->byval_arg;
5523 helper_sig_memcpy->pinvoke = 1;
5525 /* void memset (intptr, int val, int size) */
5526 helper_sig_memset = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5527 helper_sig_memset->params [0] = &mono_defaults.int_class->byval_arg;
5528 helper_sig_memset->params [1] = &mono_defaults.int32_class->byval_arg;
5529 helper_sig_memset->params [2] = &mono_defaults.int32_class->byval_arg;
5530 helper_sig_memset->ret = &mono_defaults.void_class->byval_arg;
5531 helper_sig_memset->pinvoke = 1;
5533 helper_sig_class_init_trampoline = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5534 helper_sig_class_init_trampoline->ret = &mono_defaults.void_class->byval_arg;
5535 helper_sig_class_init_trampoline->pinvoke = 1;
5538 static GHashTable *jit_icall_hash_name = NULL;
5539 static GHashTable *jit_icall_hash_addr = NULL;
5542 mono_find_jit_icall_by_name (const char *name)
5544 g_assert (jit_icall_hash_name);
5546 //printf ("lookup addr %s %p\n", name, g_hash_table_lookup (jit_icall_hash_name, name));
5547 return g_hash_table_lookup (jit_icall_hash_name, name);
5551 mono_find_jit_icall_by_addr (gconstpointer addr)
5553 g_assert (jit_icall_hash_addr);
5555 return g_hash_table_lookup (jit_icall_hash_addr, (gpointer)addr);
5559 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
5562 MonoMethod *wrapper;
5564 if (callinfo->wrapper)
5565 return callinfo->wrapper;
5566 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
5567 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
5568 callinfo->wrapper = mono_jit_compile_method (wrapper);
5570 return callinfo->wrapper;
5574 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save)
5576 MonoJitICallInfo *info;
5581 if (!jit_icall_hash_name) {
5582 jit_icall_hash_name = g_hash_table_new (g_str_hash, g_str_equal);
5583 jit_icall_hash_addr = g_hash_table_new (NULL, NULL);
5586 if (g_hash_table_lookup (jit_icall_hash_name, name)) {
5587 g_warning ("jit icall already defined \"%s\"\n", name);
5588 g_assert_not_reached ();
5591 info = g_new (MonoJitICallInfo, 1);
5598 #ifdef MONO_USE_EXC_TABLES
5599 || mono_arch_has_unwind_info (func)
5602 info->wrapper = func;
5604 info->wrapper = NULL;
5605 mono_icall_get_wrapper (info);
5608 g_hash_table_insert (jit_icall_hash_name, (gpointer)info->name, info);
5609 g_hash_table_insert (jit_icall_hash_addr, (gpointer)func, info);
5610 if (func != info->wrapper)
5611 g_hash_table_insert (jit_icall_hash_addr, (gpointer)info->wrapper, info);
5616 static GHashTable *class_init_hash_addr = NULL;
5619 mono_create_class_init_trampoline (MonoVTable *vtable)
5623 /* previously created trampoline code */
5624 mono_domain_lock (vtable->domain);
5626 mono_g_hash_table_lookup (vtable->domain->class_init_trampoline_hash,
5628 mono_domain_unlock (vtable->domain);
5632 code = mono_arch_create_class_init_trampoline (vtable);
5634 /* store trampoline address */
5635 mono_domain_lock (vtable->domain);
5636 mono_g_hash_table_insert (vtable->domain->class_init_trampoline_hash,
5638 mono_domain_unlock (vtable->domain);
5640 /* FIXME: locking */
5641 if (!class_init_hash_addr)
5642 class_init_hash_addr = g_hash_table_new (NULL, NULL);
5643 g_hash_table_insert (class_init_hash_addr, code, vtable);
5649 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
5651 if (class_init_hash_addr)
5652 return g_hash_table_lookup (class_init_hash_addr, addr);
5657 static GHashTable *emul_opcode_hash = NULL;
5659 static MonoJitICallInfo *
5660 mono_find_jit_opcode_emulation (int opcode)
5662 if (emul_opcode_hash)
5663 return g_hash_table_lookup (emul_opcode_hash, (gpointer)opcode);
5669 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func, gboolean no_throw)
5671 MonoJitICallInfo *info;
5673 if (!emul_opcode_hash)
5674 emul_opcode_hash = g_hash_table_new (NULL, NULL);
5676 g_assert (!sig->hasthis);
5677 g_assert (sig->param_count < 3);
5679 info = mono_register_jit_icall (func, name, sig, no_throw);
5681 g_hash_table_insert (emul_opcode_hash, (gpointer)opcode, info);
5685 decompose_foreach (MonoInst *tree, gpointer data)
5687 static MonoJitICallInfo *newarr_info = NULL;
5688 static MonoJitICallInfo *newarr_specific_info = NULL;
5689 MonoJitICallInfo *info;
5691 switch (tree->opcode) {
5693 MonoCompile *cfg = data;
5694 MonoInst *iargs [3];
5697 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
5698 g_assert (newarr_info);
5699 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
5700 g_assert (newarr_specific_info);
5703 if (cfg->opt & MONO_OPT_SHARED) {
5704 NEW_DOMAINCONST (cfg, iargs [0]);
5705 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
5706 iargs [2] = tree->inst_newa_len;
5711 MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
5713 NEW_VTABLECONST (cfg, iargs [0], vtable);
5714 iargs [1] = tree->inst_newa_len;
5716 info = newarr_specific_info;
5719 mono_emulate_opcode (cfg, tree, iargs, info);
5729 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
5731 switch (mono_burg_arity [tree->opcode]) {
5734 mono_inst_foreach (tree->inst_left, func, data);
5737 mono_inst_foreach (tree->inst_left, func, data);
5738 mono_inst_foreach (tree->inst_right, func, data);
5741 g_assert_not_reached ();
5748 mono_print_bb_code (MonoBasicBlock *bb) {
5750 MonoInst *c = bb->code;
5752 mono_print_tree (c);
5761 print_dfn (MonoCompile *cfg) {
5766 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
5768 for (i = 0; i < cfg->num_bblocks; ++i) {
5769 bb = cfg->bblocks [i];
5771 char* code1, *code2;
5772 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
5773 if (bb->last_ins->cil_code)
5774 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
5776 code2 = g_strdup ("");
5778 code1 [strlen (code1) - 1] = 0;
5779 code = g_strdup_printf ("%s -> %s", code1, code2);
5783 code = g_strdup ("\n");
5784 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
5786 MonoInst *c = bb->code;
5788 mono_print_tree (c);
5796 g_print ("\tprev:");
5797 for (j = 0; j < bb->in_count; ++j) {
5798 g_print (" BB%d", bb->in_bb [j]->block_num);
5800 g_print ("\t\tsucc:");
5801 for (j = 0; j < bb->out_count; ++j) {
5802 g_print (" BB%d", bb->out_bb [j]->block_num);
5804 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
5807 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
5810 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
5812 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
5820 * returns the offset used by spillvar. It allocates a new
5821 * spill variable if necessary.
5824 mono_spillvar_offset (MonoCompile *cfg, int spillvar)
5826 MonoSpillInfo **si, *info;
5829 si = &cfg->spill_info;
5831 while (i <= spillvar) {
5834 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
5836 cfg->stack_offset -= sizeof (gpointer);
5837 info->offset = cfg->stack_offset;
5841 return (*si)->offset;
5847 g_assert_not_reached ();
5852 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
5856 g_assert (bb->code);
5857 bb->last_ins->next = inst;
5858 bb->last_ins = inst;
5860 bb->last_ins = bb->code = inst;
5865 mono_destroy_compile (MonoCompile *cfg)
5867 //mono_mempool_stats (cfg->mempool);
5868 g_hash_table_destroy (cfg->bb_hash);
5870 mono_regstate_free (cfg->rs);
5871 mono_mempool_destroy (cfg->mempool);
5872 g_list_free (cfg->ldstr_list);
5874 g_free (cfg->varinfo);
5880 mono_get_lmf_addr (void)
5882 MonoJitTlsData *jit_tls;
5884 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
5885 return &jit_tls->lmf;
5887 g_assert_not_reached ();
5892 * mono_thread_abort:
5893 * @obj: exception object
5895 * abort the thread, print exception information and stack trace
5898 mono_thread_abort (MonoObject *obj)
5900 /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
5902 /* handle_remove should be eventually called for this thread, too
5909 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
5911 MonoJitTlsData *jit_tls;
5914 jit_tls = g_new0 (MonoJitTlsData, 1);
5916 TlsSetValue (mono_jit_tls_id, jit_tls);
5918 jit_tls->abort_func = abort_func;
5919 jit_tls->end_of_stack = stack_start;
5921 lmf = g_new0 (MonoLMF, 1);
5924 jit_tls->lmf = jit_tls->first_lmf = lmf;
5929 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
5932 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
5933 thread = mono_thread_current ();
5935 thread->jit_data = jit_tls;
5938 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
5941 mono_thread_abort_dummy (MonoObject *obj)
5943 if (mono_thread_attach_aborted_cb)
5944 mono_thread_attach_aborted_cb (obj);
5946 mono_thread_abort (obj);
5950 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
5953 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
5954 thread = mono_thread_current ();
5956 thread->jit_data = jit_tls;
5960 mini_thread_cleanup (MonoThread *thread)
5962 MonoJitTlsData *jit_tls = thread->jit_data;
5965 g_free (jit_tls->first_lmf);
5967 thread->jit_data = NULL;
5972 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
5974 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
5978 ji->data.target = target;
5979 ji->next = cfg->patch_info;
5981 cfg->patch_info = ji;
5985 mono_remove_patch_info (MonoCompile *cfg, int ip)
5987 MonoJumpInfo **ji = &cfg->patch_info;
5990 if ((*ji)->ip.i == ip)
5993 ji = &((*ji)->next);
5998 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
5999 MonoJitICallInfo *info;
6001 switch (mono_burg_arity [tree->opcode]) {
6004 dec_foreach (tree->inst_left, cfg);
6006 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6007 MonoInst *iargs [2];
6009 iargs [0] = tree->inst_left;
6011 mono_emulate_opcode (cfg, tree, iargs, info);
6017 if (tree->opcode == OP_LMUL
6018 && (cfg->opt & MONO_OPT_INTRINS)
6019 && (tree->inst_left->opcode == CEE_CONV_I8
6020 || tree->inst_left->opcode == CEE_CONV_U8)
6021 && tree->inst_left->inst_left->type == STACK_I4
6022 && (tree->inst_right->opcode == CEE_CONV_I8
6023 || tree->inst_right->opcode == CEE_CONV_U8)
6024 && tree->inst_right->inst_left->type == STACK_I4) {
6025 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
6026 tree->inst_left = tree->inst_left->inst_left;
6027 tree->inst_right = tree->inst_right->inst_left;
6028 dec_foreach (tree, cfg);
6029 } else if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6030 MonoInst *iargs [2];
6032 iargs [0] = tree->inst_i0;
6033 iargs [1] = tree->inst_i1;
6035 mono_emulate_opcode (cfg, tree, iargs, info);
6037 dec_foreach (iargs [0], cfg);
6038 dec_foreach (iargs [1], cfg);
6041 dec_foreach (tree->inst_left, cfg);
6042 dec_foreach (tree->inst_right, cfg);
6046 g_assert_not_reached ();
6048 decompose_foreach (tree, cfg);
6052 decompose_pass (MonoCompile *cfg) {
6055 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6058 cfg->prev_ins = NULL;
6059 for (tree = cfg->cbb->code; tree; tree = tree->next) {
6060 dec_foreach (tree, cfg);
6061 cfg->prev_ins = tree;
6067 nullify_basic_block (MonoBasicBlock *bb)
6074 bb->code = bb->last_ins = NULL;
6075 bb->cil_code = NULL;
6079 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6083 for (i = 0; i < bb->out_count; i++) {
6084 MonoBasicBlock *ob = bb->out_bb [i];
6087 if (bb->out_count > 1) {
6088 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
6092 bb->out_bb [i] = repl;
6099 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6103 for (i = 0; i < bb->in_count; i++) {
6104 MonoBasicBlock *ib = bb->in_bb [i];
6107 if (bb->in_count > 1) {
6108 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
6112 bb->in_bb [i] = repl;
6119 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6123 for (i = 0; i < bb->out_count; i++) {
6124 MonoBasicBlock *ob = bb->out_bb [i];
6125 for (j = 0; j < ob->in_count; j++) {
6126 if (ob->in_bb [j] == orig) {
6127 ob->in_bb [j] = repl;
6136 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
6138 bb->out_count = bbn->out_count;
6139 bb->out_bb = bbn->out_bb;
6141 replace_basic_block (bb, bbn, bb);
6145 bb->last_ins->next = bbn->code;
6146 bb->last_ins = bbn->last_ins;
6149 bb->code = bbn->code;
6150 bb->last_ins = bbn->last_ins;
6152 bb->next_bb = bbn->next_bb;
6153 nullify_basic_block (bbn);
6157 optimize_branches (MonoCompile *cfg) {
6158 int i, changed = FALSE;
6159 MonoBasicBlock *bb, *bbn;
6164 /* we skip the entry block (exit is handled specially instead ) */
6165 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
6167 /* dont touch code inside exception clauses */
6168 if (bb->region != -1)
6171 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
6172 if (cfg->verbose_level > 2)
6173 g_print ("nullify block triggered %d\n", bbn->block_num);
6175 bb->next_bb = bbn->next_bb;
6177 for (i = 0; i < bbn->out_count; i++)
6178 replace_in_block (bbn->out_bb [i], bbn, NULL);
6180 nullify_basic_block (bbn);
6184 if (bb->out_count == 1) {
6185 bbn = bb->out_bb [0];
6187 /* conditional branches where true and false targets are the same can be also replaced with CEE_BR */
6188 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
6189 bb->last_ins->opcode = CEE_BR;
6190 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
6192 if (cfg->verbose_level > 2)
6193 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
6196 if (bb->region == bbn->region && bb->next_bb == bbn) {
6197 /* the block are in sequence anyway ... */
6199 /* branches to the following block can be removed */
6200 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
6201 bb->last_ins->opcode = CEE_NOP;
6203 if (cfg->verbose_level > 2)
6204 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
6207 if (bbn->in_count == 1) {
6209 if (bbn != cfg->bb_exit) {
6210 if (cfg->verbose_level > 2)
6211 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
6212 merge_basic_blocks (bb, bbn);
6216 //mono_print_bb_code (bb);
6226 /* we skip the entry block (exit is handled specially instead ) */
6227 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
6229 /* dont touch code inside exception clauses */
6230 if (bb->region != -1)
6233 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
6234 if (cfg->verbose_level > 2) {
6235 g_print ("nullify block triggered %d\n", bbn->block_num);
6237 bb->next_bb = bbn->next_bb;
6239 for (i = 0; i < bbn->out_count; i++)
6240 replace_in_block (bbn->out_bb [i], bbn, NULL);
6242 nullify_basic_block (bbn);
6248 if (bb->out_count == 1) {
6249 bbn = bb->out_bb [0];
6251 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
6252 bbn = bb->last_ins->inst_target_bb;
6253 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
6254 bbn->code->inst_target_bb->region == bb->region) {
6256 if (cfg->verbose_level > 2)
6257 g_print ("in %s branch to branch triggered %d -> %d\n", cfg->method->name,
6258 bb->block_num, bbn->block_num);
6260 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
6261 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
6266 } else if (bb->out_count == 2) {
6267 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
6268 bbn = bb->last_ins->inst_true_bb;
6269 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
6270 bbn->code->inst_target_bb->region == bb->region) {
6271 if (cfg->verbose_level > 2)
6272 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
6273 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
6276 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
6278 replace_in_block (bbn, bb, NULL);
6280 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
6281 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
6283 link_bblock (cfg, bb, bbn->code->inst_target_bb);
6289 bbn = bb->last_ins->inst_false_bb;
6290 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
6291 bbn->code->inst_target_bb->region == bb->region) {
6292 if (cfg->verbose_level > 2)
6293 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
6294 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
6297 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
6299 replace_in_block (bbn, bb, NULL);
6301 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
6302 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
6304 link_bblock (cfg, bb, bbn->code->inst_target_bb);
6317 mono_compile_create_vars (MonoCompile *cfg)
6319 MonoMethodSignature *sig;
6320 MonoMethodHeader *header;
6323 header = ((MonoMethodNormal *)cfg->method)->header;
6325 sig = cfg->method->signature;
6327 if (!MONO_TYPE_IS_VOID (sig->ret)) {
6328 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
6329 cfg->ret->opcode = OP_RETARG;
6330 cfg->ret->inst_vtype = sig->ret;
6331 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
6333 if (cfg->verbose_level > 2)
6334 g_print ("creating vars\n");
6337 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
6339 for (i = 0; i < sig->param_count; ++i)
6340 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
6342 cfg->locals_start = cfg->num_varinfo;
6344 if (cfg->verbose_level > 2)
6345 g_print ("creating locals\n");
6346 for (i = 0; i < header->num_locals; ++i)
6347 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
6348 if (cfg->verbose_level > 2)
6349 g_print ("locals done\n");
6353 mono_print_code (MonoCompile *cfg)
6357 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6358 MonoInst *tree = bb->code;
6363 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
6365 for (; tree; tree = tree->next) {
6366 mono_print_tree (tree);
6371 bb->last_ins->next = NULL;
6375 extern const char * const mono_burg_rule_string [];
6378 emit_state (MonoCompile *cfg, MBState *state, int goal)
6381 int ern = mono_burg_rule (state, goal);
6382 const guint16 *nts = mono_burg_nts [ern];
6385 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
6389 // state->reg1 = state->reg2; /* chain rule */
6391 state->reg1 = mono_regstate_next_int (cfg->rs);
6392 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
6395 state->reg1 = mono_regstate_next_int (cfg->rs);
6396 state->reg2 = mono_regstate_next_int (cfg->rs);
6399 state->reg1 = mono_regstate_next_float (cfg->rs);
6406 mono_burg_kids (state, ern, kids);
6408 emit_state (cfg, kids [0], nts [0]);
6410 emit_state (cfg, kids [1], nts [1]);
6412 g_assert (!nts [3]);
6413 emit_state (cfg, kids [2], nts [2]);
6418 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
6419 if ((emit = mono_burg_func [ern]))
6420 emit (state, state->tree, cfg);
6423 #define DEBUG_SELECTION
6426 mini_select_instructions (MonoCompile *cfg)
6428 static int reverse_map [] = {
6429 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
6430 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
6432 static int reverse_fmap [] = {
6433 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
6434 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
6436 static int reverse_lmap [] = {
6437 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
6438 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
6443 cfg->state_pool = mono_mempool_new ();
6444 cfg->rs = mono_regstate_new ();
6446 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6447 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode) &&
6448 bb->next_bb != bb->last_ins->inst_false_bb) {
6450 if (bb->next_bb == bb->last_ins->inst_true_bb) {
6451 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
6452 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
6453 bb->last_ins->inst_false_bb = tmp;
6455 if (bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
6456 bb->last_ins->opcode = reverse_map [bb->last_ins->opcode - CEE_BEQ];
6457 } else if (bb->last_ins->opcode >= OP_FBEQ && bb->last_ins->opcode <= OP_FBLT_UN) {
6458 bb->last_ins->opcode = reverse_fmap [bb->last_ins->opcode - OP_FBEQ];
6459 } else if (bb->last_ins->opcode >= OP_LBEQ && bb->last_ins->opcode <= OP_LBLT_UN) {
6460 bb->last_ins->opcode = reverse_lmap [bb->last_ins->opcode - OP_LBEQ];
6463 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
6464 inst->opcode = CEE_BR;
6465 inst->inst_target_bb = bb->last_ins->inst_false_bb;
6466 mono_bblock_add_inst (bb, inst);
6471 #ifdef DEBUG_SELECTION
6472 if (cfg->verbose_level >= 4) {
6473 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6474 MonoInst *tree = bb->code;
6475 g_print ("DUMP BLOCK %d:\n", bb->block_num);
6478 for (; tree; tree = tree->next) {
6479 mono_print_tree (tree);
6486 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6487 MonoInst *tree = bb->code, *next;
6493 bb->last_ins = NULL;
6496 mono_regstate_reset (cfg->rs);
6498 #ifdef DEBUG_SELECTION
6499 if (cfg->verbose_level >= 3)
6500 g_print ("LABEL BLOCK %d:\n", bb->block_num);
6502 for (; tree; tree = next) {
6504 #ifdef DEBUG_SELECTION
6505 if (cfg->verbose_level >= 3) {
6506 mono_print_tree (tree);
6511 if (!(mbstate = mono_burg_label (tree, cfg))) {
6512 g_warning ("unable to label tree %p", tree);
6513 mono_print_tree (tree);
6515 g_assert_not_reached ();
6517 emit_state (cfg, mbstate, MB_NTERM_stmt);
6519 bb->max_ireg = cfg->rs->next_vireg;
6520 bb->max_freg = cfg->rs->next_vfreg;
6523 bb->last_ins->next = NULL;
6525 mono_mempool_empty (cfg->state_pool);
6527 mono_mempool_destroy (cfg->state_pool);
6531 mono_codegen (MonoCompile *cfg)
6533 MonoJumpInfo *patch_info;
6535 int i, max_epilog_size;
6538 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6539 cfg->spill_count = 0;
6540 /* we reuse dfn here */
6541 /* bb->dfn = bb_count++; */
6542 mono_arch_local_regalloc (cfg, bb);
6545 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
6546 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
6548 code = mono_arch_emit_prolog (cfg);
6550 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6551 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
6553 cfg->code_len = code - cfg->native_code;
6554 cfg->prolog_end = cfg->code_len;
6556 mono_debug_open_method (cfg);
6558 /* emit code all basic blocks */
6559 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6560 bb->native_offset = cfg->code_len;
6561 mono_arch_output_basic_block (cfg, bb);
6563 cfg->bb_exit->native_offset = cfg->code_len;
6565 code = cfg->native_code + cfg->code_len;
6567 max_epilog_size = mono_arch_max_epilog_size (cfg);
6569 /* we always allocate code in cfg->domain->code_mp to increase locality */
6570 cfg->code_size = cfg->code_len + max_epilog_size;
6571 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
6572 code = mono_mempool_alloc (cfg->domain->code_mp, cfg->code_size);
6573 memcpy (code, cfg->native_code, cfg->code_len);
6574 g_free (cfg->native_code);
6575 cfg->native_code = code;
6576 code = cfg->native_code + cfg->code_len;
6578 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
6580 cfg->epilog_begin = cfg->code_len;
6582 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6583 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
6585 cfg->code_len = code - cfg->native_code;
6587 mono_arch_emit_epilog (cfg);
6589 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
6590 switch (patch_info->type) {
6591 case MONO_PATCH_INFO_ABS: {
6592 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
6594 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
6595 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
6596 patch_info->data.name = info->name;
6599 MonoVTable *vtable = mono_find_class_init_trampoline_by_addr (patch_info->data.target);
6601 patch_info->type = MONO_PATCH_INFO_CLASS_INIT;
6602 patch_info->data.klass = vtable->klass;
6607 case MONO_PATCH_INFO_SWITCH: {
6608 gpointer *table = mono_mempool_alloc (cfg->domain->code_mp, sizeof (gpointer) * patch_info->table_size);
6609 patch_info->ip.i = patch_info->ip.label->inst_c0;
6610 for (i = 0; i < patch_info->table_size; i++) {
6611 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
6613 patch_info->data.target = table;
6622 if (cfg->verbose_level > 1)
6623 g_print ("Method %s::%s emmitted at %p to %p\n", cfg->method->klass->name,
6624 cfg->method->name, cfg->native_code, cfg->native_code + cfg->code_len);
6626 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info);
6628 mono_debug_close_method (cfg);
6632 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
6637 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
6638 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
6640 if (cp->opcode == OP_ICONST) {
6641 if (cfg->opt & MONO_OPT_CONSPROP) {
6642 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
6646 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
6647 if (cfg->opt & MONO_OPT_COPYPROP) {
6648 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
6654 arity = mono_burg_arity [tree->opcode];
6657 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
6658 if (cfg->opt & MONO_OPT_CFOLD)
6659 mono_constant_fold_inst (tree, NULL);
6661 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
6662 if (cfg->opt & MONO_OPT_CFOLD)
6663 mono_constant_fold_inst (tree, NULL);
6665 mono_constant_fold_inst (tree, NULL);
6671 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
6675 switch (tree->opcode) {
6685 if (tree->ssa_op == MONO_SSA_NOP) {
6686 memset (acp, 0, sizeof (MonoInst *) * acp_size);
6703 case OP_VOIDCALL_REG:
6704 case OP_VOIDCALLVIRT:
6706 MonoCallInst *call = (MonoCallInst *)tree;
6707 MonoMethodSignature *sig = call->signature;
6708 int i, byref = FALSE;
6710 for (i = 0; i < sig->param_count; i++) {
6711 if (sig->params [i]->byref) {
6718 memset (acp, 0, sizeof (MonoInst *) * acp_size);
6726 arity = mono_burg_arity [tree->opcode];
6732 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
6735 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
6736 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
6739 g_assert_not_reached ();
6744 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
6746 MonoInst *tree = bb->code;
6752 for (; tree; tree = tree->next) {
6754 mono_cprop_copy_values (cfg, tree, acp);
6756 mono_cprop_invalidate_values (tree, acp, acp_size);
6758 if (tree->ssa_op == MONO_SSA_STORE &&
6759 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
6760 MonoInst *i1 = tree->inst_i1;
6762 acp [tree->inst_i0->inst_c0] = NULL;
6764 for (i = 0; i < acp_size; i++) {
6765 if (acp [i] && acp [i]->opcode != OP_ICONST &&
6766 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
6771 if (i1->opcode == OP_ICONST) {
6772 acp [tree->inst_i0->inst_c0] = i1;
6773 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
6775 if (i1->ssa_op == MONO_SSA_LOAD &&
6776 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
6777 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
6778 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
6779 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
6784 if (tree->opcode == CEE_BEQ) {
6785 g_assert (tree->inst_i0->opcode == OP_COMPARE);
6786 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
6787 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
6789 tree->opcode = CEE_BR;
6790 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
6791 tree->inst_target_bb = tree->inst_true_bb;
6793 tree->inst_target_bb = tree->inst_false_bb;
6802 mono_local_cprop (MonoCompile *cfg)
6807 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
6809 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6810 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
6811 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
6816 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, int parts)
6818 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
6819 guint8 *ip = (guint8 *)header->code;
6822 int dfn = 0, i, code_size_ratio;
6824 mono_jit_stats.methods_compiled++;
6825 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
6826 mono_profiler_method_jit (method);
6828 cfg = g_new0 (MonoCompile, 1);
6829 cfg->method = method;
6830 cfg->mempool = mono_mempool_new ();
6832 cfg->prof_options = mono_profiler_get_events ();
6833 cfg->bb_hash = g_hash_table_new (g_direct_hash, NULL);
6834 cfg->domain = domain;
6835 cfg->verbose_level = mini_verbose;
6836 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
6837 ((MonoMethodNormal *)method)->header->max_stack);
6839 if (cfg->verbose_level > 2)
6840 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
6843 * create MonoInst* which represents arguments and local variables
6845 mono_compile_create_vars (cfg);
6847 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
6848 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
6849 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
6850 mono_destroy_compile (cfg);
6854 mono_jit_stats.basic_blocks += cfg->num_bblocks;
6855 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
6857 if (cfg->num_varinfo > 2000) {
6859 * we disable some optimizations if there are too many variables
6860 * because JIT time may become too expensive. The actual number needs
6861 * to be tweaked and eventually the non-linear algorithms should be fixed.
6863 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
6864 cfg->disable_ssa = TRUE;
6866 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
6868 /* Depth-first ordering on basic blocks */
6869 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
6871 if (cfg->opt & MONO_OPT_BRANCH)
6872 optimize_branches (cfg);
6874 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
6875 if (cfg->num_bblocks != dfn + 1) {
6878 cfg->num_bblocks = dfn + 1;
6880 if (!header->clauses) {
6881 /* remove unreachable code, because the code in them may be
6882 * inconsistent (access to dead variables for example) */
6883 for (bb = cfg->bb_entry; bb;) {
6884 MonoBasicBlock *bbn = bb->next_bb;
6886 if (bbn && bbn->region == -1 && !bbn->dfn) {
6887 if (cfg->verbose_level > 1)
6888 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
6889 bb->next_bb = bbn->next_bb;
6890 nullify_basic_block (bbn);
6898 if (cfg->opt & MONO_OPT_LOOP) {
6899 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
6900 mono_compute_natural_loops (cfg);
6904 /* after method_to_ir */
6908 //#define DEBUGSSA "logic_run"
6909 #define DEBUGSSA_CLASS "Tests"
6912 if (!header->num_clauses && !cfg->disable_ssa) {
6913 mono_local_cprop (cfg);
6914 mono_ssa_compute (cfg);
6918 /* fixme: add all optimizations which requires SSA */
6919 if (cfg->opt & (MONO_OPT_DEADCE)) {
6920 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
6921 mono_local_cprop (cfg);
6922 mono_ssa_compute (cfg);
6924 if (cfg->verbose_level >= 2) {
6931 /* after SSA translation */
6935 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
6936 if (cfg->comp_done & MONO_COMP_SSA) {
6937 mono_ssa_cprop (cfg);
6939 mono_local_cprop (cfg);
6943 if (cfg->comp_done & MONO_COMP_SSA) {
6944 mono_ssa_deadce (cfg);
6946 //mono_ssa_strength_reduction (cfg);
6948 mono_ssa_remove (cfg);
6950 if (cfg->opt & MONO_OPT_BRANCH)
6951 optimize_branches (cfg);
6954 /* after SSA removal */
6958 decompose_pass (cfg);
6960 /* FIXME: disabled with exception clauses: bug #42136 */
6961 if ((!header->num_clauses) && (cfg->opt & MONO_OPT_LINEARS)) {
6964 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
6965 cfg->comp_done &= ~MONO_COMP_LIVENESS;
6966 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
6967 mono_analyze_liveness (cfg);
6969 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
6970 regs = mono_arch_get_global_int_regs (cfg);
6971 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
6975 //mono_print_code (cfg);
6979 /* variables are allocated after decompose, since decompose could create temps */
6980 mono_arch_allocate_vars (cfg);
6982 if (cfg->opt & MONO_OPT_CFOLD)
6983 mono_constant_fold (cfg);
6985 mini_select_instructions (cfg);
6988 if (cfg->verbose_level >= 2) {
6989 char *id = mono_method_full_name (cfg->method, FALSE);
6990 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
6994 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
6996 jinfo = g_new0 (MonoJitInfo, 1);
6997 jinfo->method = method;
6998 jinfo->code_start = cfg->native_code;
6999 jinfo->code_size = cfg->code_len;
7000 jinfo->used_regs = cfg->used_int_regs;
7001 jinfo->domain_neutral = (cfg->opt & MONO_OPT_SHARED) != 0;
7003 if (header->num_clauses) {
7006 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
7007 jinfo->num_clauses = header->num_clauses;
7008 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
7009 sizeof (MonoJitExceptionInfo) * header->num_clauses);
7011 for (i = 0; i < header->num_clauses; i++) {
7012 MonoExceptionClause *ec = &header->clauses [i];
7013 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
7014 MonoBasicBlock *tblock;
7016 ei->flags = ec->flags;
7018 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7019 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->token_or_filter);
7021 ei->data.filter = cfg->native_code + tblock->native_offset;
7023 ei->data.token = ec->token_or_filter;
7026 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
7028 ei->try_start = cfg->native_code + tblock->native_offset;
7029 g_assert (tblock->native_offset);
7030 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
7032 ei->try_end = cfg->native_code + tblock->native_offset;
7033 g_assert (tblock->native_offset);
7034 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
7036 ei->handler_start = cfg->native_code + tblock->native_offset;
7040 cfg->jit_info = jinfo;
7042 mono_jit_info_table_add (cfg->domain, jinfo);
7044 /* collect statistics */
7045 mono_jit_stats.allocated_code_size += cfg->code_len;
7046 code_size_ratio = cfg->code_len;
7047 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
7048 mono_jit_stats.biggest_method_size = code_size_ratio;
7049 mono_jit_stats.biggest_method = method;
7051 code_size_ratio = (code_size_ratio * 100) / ((MonoMethodNormal *)method)->header->code_size;
7052 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
7053 mono_jit_stats.max_code_size_ratio = code_size_ratio;
7054 mono_jit_stats.max_ratio_method = method;
7056 mono_jit_stats.native_code_size += cfg->code_len;
7058 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7059 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
7065 mono_jit_compile_method (MonoMethod *method)
7067 /* FIXME: later copy the code from mono */
7068 MonoDomain *target_domain, *domain = mono_domain_get ();
7070 GHashTable *jit_code_hash;
7074 if (default_opt & MONO_OPT_SHARED)
7075 target_domain = mono_root_domain;
7077 target_domain = domain;
7079 jit_code_hash = target_domain->jit_code_hash;
7081 if ((info = g_hash_table_lookup (jit_code_hash, method))) {
7082 /* We can't use a domain specific method in another domain */
7083 if (! ((domain != target_domain) && !info->domain_neutral)) {
7084 mono_jit_stats.methods_lookups++;
7085 return info->code_start;
7089 #ifdef MONO_USE_AOT_COMPILER
7090 if (!mono_compile_aot && (default_opt & MONO_OPT_AOT)) {
7093 mono_class_init (method->klass);
7094 if ((info = mono_aot_get_method (domain, method))) {
7096 g_hash_table_insert (domain->jit_code_hash, method, info);
7098 /* make sure runtime_init is called */
7099 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
7101 return info->code_start;
7106 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
7107 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
7108 if (!method->info) {
7111 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
7112 mono_lookup_pinvoke_call (method);
7113 #ifdef MONO_USE_EXC_TABLES
7114 if (mono_method_blittable (method)) {
7115 method->info = method->addr;
7118 nm = mono_marshal_get_native_wrapper (method);
7119 method->info = mono_compile_method (nm);
7121 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
7122 //mono_debug_add_wrapper (method, nm);
7123 #ifdef MONO_USE_EXC_TABLES
7127 return method->info;
7128 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
7129 const char *name = method->name;
7132 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
7133 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
7134 /* FIXME: uhm, we need a wrapper to handle exceptions? */
7135 return (gpointer)mono_delegate_ctor;
7136 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
7137 nm = mono_marshal_get_delegate_invoke (method);
7138 return mono_jit_compile_method (nm);
7139 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
7140 nm = mono_marshal_get_delegate_begin_invoke (method);
7141 return mono_jit_compile_method (nm);
7142 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
7143 nm = mono_marshal_get_delegate_end_invoke (method);
7144 return mono_jit_compile_method (nm);
7150 cfg = mini_method_compile (method, default_opt, target_domain, 0);
7151 code = cfg->native_code;
7152 mono_destroy_compile (cfg);
7154 g_hash_table_insert (jit_code_hash, method, cfg->jit_info);
7156 if (target_domain->jump_target_hash) {
7157 MonoJumpInfo patch_info;
7159 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
7161 patch_info.next = NULL;
7162 patch_info.ip.i = 0;
7163 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
7164 patch_info.data.method = method;
7165 g_hash_table_remove (target_domain->jump_target_hash, method);
7167 for (tmp = list; tmp; tmp = tmp->next)
7168 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info);
7169 g_slist_free (list);
7171 /* make sure runtime_init is called */
7172 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
7178 * mono_jit_runtime_invoke:
7179 * @method: the method to invoke
7180 * @obj: this pointer
7181 * @params: array of parameter values.
7182 * @exc: used to catch exceptions objects
7185 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
7188 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc);
7189 invoke = mono_marshal_get_runtime_invoke (method);
7190 runtime_invoke = mono_jit_compile_method (invoke);
7191 return runtime_invoke (obj, params, exc);
7194 #ifdef PLATFORM_WIN32
7195 #define GET_CONTEXT \
7196 struct sigcontext *ctx = (struct sigcontext*)_dummy;
7198 #define GET_CONTEXT \
7199 void **_p = (void **)&_dummy; \
7200 struct sigcontext *ctx = (struct sigcontext *)++_p;
7204 sigfpe_signal_handler (int _dummy)
7209 exc = mono_get_exception_divide_by_zero ();
7211 mono_arch_handle_exception (ctx, exc, FALSE);
7215 sigill_signal_handler (int _dummy)
7219 exc = mono_get_exception_execution_engine ("SIGILL");
7221 mono_arch_handle_exception (ctx, exc, FALSE);
7225 sigsegv_signal_handler (int _dummy)
7230 exc = mono_get_exception_null_reference ();
7232 mono_arch_handle_exception (ctx, exc, FALSE);
7236 sigusr1_signal_handler (int _dummy)
7241 thread = mono_thread_current ();
7243 g_assert (thread->abort_exc);
7245 mono_arch_handle_exception (ctx, thread->abort_exc, FALSE);
7249 sigquit_signal_handler (int _dummy)
7254 exc = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
7256 mono_arch_handle_exception (ctx, exc, FALSE);
7261 sigint_signal_handler (int _dummy)
7266 exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
7268 mono_arch_handle_exception (ctx, exc, FALSE);
7272 mono_runtime_install_handlers (void)
7274 #ifndef PLATFORM_WIN32
7275 struct sigaction sa;
7278 #ifdef PLATFORM_WIN32
7280 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
7281 win32_seh_set_handler(SIGILL, sigill_signal_handler);
7282 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
7283 if (getenv ("MONO_DEBUG"))
7284 win32_seh_set_handler(SIGINT, sigint_signal_handler);
7285 #else /* !PLATFORM_WIN32 */
7287 /* libpthreads has its own implementation of sigaction(),
7288 * but it seems to work well with our current exception
7289 * handlers. If not we must call syscall directly instead
7292 if (getenv ("MONO_DEBUG")) {
7294 sa.sa_handler = sigint_signal_handler;
7295 sigemptyset (&sa.sa_mask);
7297 //g_assert (syscall (SYS_sigaction, SIGINT, &sa, NULL) != -1);
7298 g_assert (sigaction (SIGINT, &sa, NULL) != -1);
7302 sa.sa_handler = sigfpe_signal_handler;
7303 sigemptyset (&sa.sa_mask);
7305 //g_assert (syscall (SYS_sigaction, SIGFPE, &sa, NULL) != -1);
7306 g_assert (sigaction (SIGFPE, &sa, NULL) != -1);
7309 sa.sa_handler = sigquit_signal_handler;
7310 sigemptyset (&sa.sa_mask);
7312 g_assert (sigaction (SIGQUIT, &sa, NULL) != -1);
7315 sa.sa_handler = sigill_signal_handler;
7316 sigemptyset (&sa.sa_mask);
7318 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
7319 g_assert (sigaction (SIGILL, &sa, NULL) != -1);
7321 /* catch thread abort signal */
7322 sa.sa_handler = sigusr1_signal_handler;
7323 sigemptyset (&sa.sa_mask);
7325 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
7326 if (!getenv ("MONO_VALGRIND"))
7327 /* valgrind 20030725 and earlier aborts on this call so we skip it */
7328 g_assert (sigaction (mono_thread_get_abort_signal (), &sa, NULL) != -1);
7332 sa.sa_handler = sigsegv_signal_handler;
7333 sigemptyset (&sa.sa_mask);
7335 //g_assert (syscall (SYS_sigaction, SIGSEGV, &sa, NULL) != -1);
7336 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
7338 #endif /* PLATFORM_WIN32 */
7341 /* mono_jit_create_remoting_trampoline:
7342 * @method: pointer to the method info
7344 * Creates a trampoline which calls the remoting functions. This
7345 * is used in the vtable of transparent proxies.
7347 * Returns: a pointer to the newly created code
7350 mono_jit_create_remoting_trampoline (MonoMethod *method)
7353 guint8 *addr = NULL;
7355 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
7356 (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
7357 nm = mono_marshal_get_remoting_invoke (method);
7358 addr = mono_compile_method (nm);
7360 addr = mono_compile_method (method);
7366 mini_init (const char *filename)
7370 mono_arch_cpu_init ();
7372 g_thread_init (NULL);
7374 mono_jit_tls_id = TlsAlloc ();
7375 setup_jit_tls_data ((gpointer)-1, mono_thread_abort);
7379 if (default_opt & MONO_OPT_AOT)
7382 mono_runtime_install_handlers ();
7383 mono_threads_install_cleanup (mini_thread_cleanup);
7385 mono_install_compile_method (mono_jit_compile_method);
7386 mono_install_trampoline (mono_arch_create_jit_trampoline);
7387 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
7388 mono_install_runtime_invoke (mono_jit_runtime_invoke);
7389 mono_install_handler (mono_arch_get_throw_exception ());
7390 mono_install_stack_walk (mono_jit_walk_stack);
7391 mono_install_get_config_dir ();
7393 domain = mono_init (filename);
7396 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
7397 ves_icall_get_frame_info);
7398 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
7399 ves_icall_get_trace);
7400 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
7401 mono_runtime_install_handlers);
7404 create_helper_signature ();
7406 /* Needs to be called here since register_jit_icall depends on it */
7407 mono_marshal_init ();
7409 mono_arch_register_lowlevel_calls ();
7410 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
7411 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
7413 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
7414 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
7416 /* fixme: we cant hanlde vararg methods this way, because the signature is not constant */
7417 //mono_register_jit_icall (ves_array_element_address, "ves_array_element_address", NULL);
7418 //mono_register_jit_icall (mono_array_new_va, "mono_array_new_va", NULL);
7420 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
7421 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
7422 helper_sig_void_ptr, TRUE);
7425 * NOTE, NOTE, NOTE, NOTE:
7426 * when adding emulation for some opcodes, remember to also add a dummy
7427 * rule to the burg files, because we need the arity information to be correct.
7429 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult, TRUE);
7430 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un, FALSE);
7431 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf, FALSE);
7432 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv, FALSE);
7433 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un, FALSE);
7434 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem, FALSE);
7435 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un, FALSE);
7437 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl, TRUE);
7438 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr, TRUE);
7439 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un, TRUE);
7441 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8, FALSE);
7442 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4, FALSE);
7443 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8, TRUE);
7444 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8, TRUE);
7446 #if SIZEOF_VOID_P == 4
7447 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4, TRUE);
7449 #warning "fixme: add opcode emulation"
7452 /* other jit icalls */
7453 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
7454 helper_sig_ptr_ptr_ptr, FALSE);
7455 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
7456 mono_register_jit_icall (mono_threads_get_static_data, "mono_threads_get_static_data", helper_sig_ptr_int, FALSE);
7457 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
7458 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
7459 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
7460 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
7461 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
7462 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
7463 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
7464 mono_register_jit_icall (mono_object_new_fast, "mono_object_new_fast", helper_sig_object_new_specific, FALSE);
7465 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
7466 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
7467 mono_register_jit_icall (mono_string_to_utf16, "mono_string_to_utf16", helper_sig_ptr_obj, FALSE);
7468 mono_register_jit_icall (mono_string_from_utf16, "mono_string_from_utf16", helper_sig_obj_ptr, FALSE);
7469 mono_register_jit_icall (mono_string_new_wrapper, "mono_string_new_wrapper", helper_sig_obj_ptr, FALSE);
7470 mono_register_jit_icall (mono_string_to_utf8, "mono_string_to_utf8", helper_sig_ptr_obj, FALSE);
7471 mono_register_jit_icall (mono_string_to_bstr, "mono_string_to_bstr", helper_sig_ptr_obj, FALSE);
7472 mono_register_jit_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", helper_sig_ptr_obj, FALSE);
7473 mono_register_jit_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", helper_sig_ptr_obj, FALSE);
7474 mono_register_jit_icall (mono_array_to_savearray, "mono_array_to_savearray", helper_sig_ptr_obj, FALSE);
7475 mono_register_jit_icall (mono_array_to_lparray, "mono_array_to_lparray", helper_sig_ptr_obj, FALSE);
7476 mono_register_jit_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", helper_sig_ptr_obj, FALSE);
7477 mono_register_jit_icall (mono_marshal_string_array, "mono_marshal_string_array", helper_sig_ptr_obj, FALSE);
7478 mono_register_jit_icall (mono_string_utf8_to_builder, "mono_string_utf8_to_builder", helper_sig_void_ptr_ptr, FALSE);
7479 mono_register_jit_icall (mono_marshal_free_array, "mono_marshal_free_array", helper_sig_void_ptr_ptr, FALSE);
7480 mono_register_jit_icall (mono_string_to_byvalstr, "mono_string_to_byvalstr", helper_sig_void_ptr_ptr_ptr, FALSE);
7481 mono_register_jit_icall (mono_string_to_byvalwstr, "mono_string_to_byvalwstr", helper_sig_void_ptr_ptr_ptr, FALSE);
7482 mono_register_jit_icall (g_free, "g_free", helper_sig_void_ptr, FALSE);
7483 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
7484 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
7485 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
7487 mono_runtime_install_cleanup ((MonoDomainFunc)mini_cleanup);
7488 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
7490 //mono_thread_attach (domain);
7494 MonoJitStats mono_jit_stats = {0};
7497 print_jit_stats (void)
7499 if (mono_jit_stats.enabled) {
7500 g_print ("Mono Jit statistics\n");
7501 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
7502 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
7503 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
7504 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
7505 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
7506 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
7507 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
7508 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
7509 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
7510 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
7511 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
7512 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
7513 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
7514 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
7515 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
7516 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
7517 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
7518 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
7520 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
7521 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
7522 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
7523 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
7524 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
7529 mini_cleanup (MonoDomain *domain)
7532 * mono_runtime_cleanup() and mono_domain_finalize () need to
7533 * be called early since they need the execution engine still
7534 * fully working (mono_domain_finalize may invoke managed finalizers
7535 * and mono_runtime_cleanup will wait for other threads to finish).
7537 mono_domain_finalize (domain);
7539 mono_runtime_cleanup (domain);
7541 mono_profiler_shutdown ();
7543 mono_debug_cleanup ();
7545 #ifdef PLATFORM_WIN32
7546 win32_seh_cleanup();
7549 mono_domain_unload (domain, TRUE);
7555 mono_set_defaults (int verbose_level, guint32 opts)
7557 mini_verbose = verbose_level;