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_CHECK_THIS(ins) (cfg->method->signature->hasthis && (ins)->ssa_op == MONO_SSA_LOAD && (ins)->inst_left->inst_c0 == 0)
48 static gpointer mono_jit_compile_method (MonoMethod *method);
50 static void handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src,
51 const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native);
53 extern guint8 mono_burg_arity [];
54 /* helper methods signature */
55 static MonoMethodSignature *helper_sig_long_long_long = NULL;
56 static MonoMethodSignature *helper_sig_long_long_int = NULL;
57 static MonoMethodSignature *helper_sig_newarr = NULL;
58 static MonoMethodSignature *helper_sig_ldstr = NULL;
59 static MonoMethodSignature *helper_sig_domain_get = NULL;
60 static MonoMethodSignature *helper_sig_object_new = NULL;
61 static MonoMethodSignature *helper_sig_compile = NULL;
62 static MonoMethodSignature *helper_sig_compile_virt = NULL;
63 static MonoMethodSignature *helper_sig_obj_ptr = NULL;
64 static MonoMethodSignature *helper_sig_ptr_void = NULL;
65 static MonoMethodSignature *helper_sig_void_ptr = NULL;
66 static MonoMethodSignature *helper_sig_void_obj = NULL;
67 static MonoMethodSignature *helper_sig_void_ptr_ptr = NULL;
68 static MonoMethodSignature *helper_sig_void_ptr_ptr_ptr = NULL;
69 static MonoMethodSignature *helper_sig_ptr_ptr_ptr = NULL;
70 static MonoMethodSignature *helper_sig_ptr_obj = NULL;
71 static MonoMethodSignature *helper_sig_initobj = NULL;
72 static MonoMethodSignature *helper_sig_memcpy = NULL;
73 static MonoMethodSignature *helper_sig_memset = NULL;
74 static MonoMethodSignature *helper_sig_ulong_double = NULL;
75 static MonoMethodSignature *helper_sig_long_double = NULL;
76 static MonoMethodSignature *helper_sig_uint_double = NULL;
77 static MonoMethodSignature *helper_sig_int_double = NULL;
78 static MonoMethodSignature *helper_sig_stelem_ref = NULL;
80 static guint32 default_opt = MONO_OPT_PEEPHOLE;
82 guint32 mono_jit_tls_id = 0;
83 gboolean mono_jit_trace_calls = FALSE;
84 gboolean mono_break_on_exc = FALSE;
85 gboolean mono_compile_aot = FALSE;
86 gboolean mono_trace_coverage = FALSE;
87 gboolean mono_jit_profile = FALSE;
88 MonoDebugFormat mono_debug_format = MONO_DEBUG_FORMAT_NONE;
90 CRITICAL_SECTION *metadata_section = NULL;
92 static int mini_verbose = 0;
94 #ifdef MONO_USE_EXC_TABLES
96 mono_type_blittable (MonoType *type)
115 case MONO_TYPE_OBJECT:
117 case MONO_TYPE_VALUETYPE:
118 case MONO_TYPE_CLASS:
119 return type->data.klass->blittable;
129 mono_method_blittable (MonoMethod *method)
131 MonoMethodSignature *sig;
137 if (!mono_arch_has_unwind_info (method->addr)) {
141 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
144 sig = method->signature;
146 if (!mono_type_blittable (sig->ret))
149 for (i = 0; i < sig->param_count; i++)
150 if (!mono_type_blittable (sig->params [i]))
159 print_method_from_ip (void *ip)
164 ji = mono_jit_info_table_find (mono_domain_get (), ip);
166 g_print ("No method at %p\n", ip);
169 method = mono_method_full_name (ji->method, TRUE);
170 g_print ("IP at offset 0x%x of method %s (%p %p)\n", (char*)ip - (char*)ji->code_start, method, ji->code_start, (char*)ji->code_start + ji->code_size);
175 #define MONO_INIT_VARINFO(vi,id) do { \
176 (vi)->range.first_use.pos.bid = 0xffff; \
182 * Basic blocks have two numeric identifiers:
183 * dfn: Depth First Number
184 * block_num: unique ID assigned at bblock creation
186 #define NEW_BBLOCK(cfg) (mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)))
187 #define ADD_BBLOCK(cfg,bbhash,b) do { \
188 g_hash_table_insert (bbhash, (b)->cil_code, (b)); \
189 (b)->block_num = cfg->num_bblocks++; \
190 (b)->real_offset = real_offset; \
193 #define GET_BBLOCK(cfg,bbhash,tblock,ip) do { \
194 (tblock) = g_hash_table_lookup (bbhash, (ip)); \
196 if ((ip) >= end || (ip) < header->code) goto unverified; \
197 (tblock) = NEW_BBLOCK (cfg); \
198 (tblock)->cil_code = (ip); \
199 ADD_BBLOCK (cfg, (bbhash), (tblock)); \
201 (tblock)->real_offset = real_offset; \
204 #define CHECK_BBLOCK(target,ip,tblock) do { \
205 if ((target) < (ip) && !(tblock)->code) { \
206 bb_recheck = g_list_prepend (bb_recheck, (tblock)); \
207 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); \
211 #define NEW_ICONST(cfg,dest,val) do { \
212 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
213 (dest)->opcode = OP_ICONST; \
214 (dest)->inst_c0 = (val); \
215 (dest)->type = STACK_I4; \
218 /* FIXME: have a different definition of NEW_PCONST for 64 bit systems */
219 #define NEW_PCONST(cfg,dest,val) do { \
220 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
221 (dest)->opcode = OP_ICONST; \
222 (dest)->inst_p0 = (val); \
223 (dest)->type = STACK_PTR; \
226 #define NEW_CLASSCONST(cfg,dest,val) do { \
227 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
228 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
229 (dest)->inst_p0 = (val); \
230 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_CLASS; \
231 (dest)->type = STACK_PTR; \
234 #define NEW_IMAGECONST(cfg,dest,val) do { \
235 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
236 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
237 (dest)->inst_p0 = (val); \
238 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_IMAGE; \
239 (dest)->type = STACK_PTR; \
242 #define NEW_FIELDCONST(cfg,dest,field) do { \
243 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
244 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
245 (dest)->inst_p0 = (field); \
246 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_FIELD; \
247 (dest)->type = STACK_PTR; \
250 #define NEW_METHODCONST(cfg,dest,val) do { \
251 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
252 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
253 (dest)->inst_p0 = (val); \
254 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_METHODCONST; \
255 (dest)->type = STACK_PTR; \
258 #define NEW_DOMAINCONST(cfg,dest) do { \
259 if ((cfg->opt & MONO_OPT_SAHRED) || mono_compile_aot) { \
260 NEW_TEMPLOAD (cfg, dest, mono_get_domainvar (cfg)->inst_c0); \
262 NEW_PCONST (cfg, dest, (cfg)->domain); \
266 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
268 #define NEW_ARGLOAD(cfg,dest,num) do { \
269 if (arg_array [(num)]->opcode == OP_ICONST) (dest) = arg_array [(num)]; else { \
270 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
271 (dest)->ssa_op = MONO_SSA_LOAD; \
272 (dest)->inst_i0 = arg_array [(num)]; \
273 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
274 type_to_eval_stack_type (param_types [(num)], (dest)); \
275 (dest)->klass = (dest)->inst_i0->klass; \
278 #define NEW_LOCLOAD(cfg,dest,num) do { \
279 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
280 (dest)->ssa_op = MONO_SSA_LOAD; \
281 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
282 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
283 type_to_eval_stack_type (header->locals [(num)], (dest)); \
284 (dest)->klass = (dest)->inst_i0->klass; \
287 #define NEW_LOCLOADA(cfg,dest,num) do { \
288 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
289 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
290 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
291 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
292 (dest)->opcode = OP_LDADDR; \
293 (dest)->type = STACK_MP; \
294 (dest)->klass = (dest)->inst_i0->klass; \
295 (cfg)->disable_ssa = TRUE; \
298 #define NEW_RETLOADA(cfg,dest) do { \
299 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
300 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
301 (dest)->inst_i0 = (cfg)->ret; \
302 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
303 (dest)->opcode = CEE_LDIND_I; \
304 (dest)->type = STACK_MP; \
305 (dest)->klass = (dest)->inst_i0->klass; \
306 (cfg)->disable_ssa = TRUE; \
309 #define NEW_ARGLOADA(cfg,dest,num) do { \
310 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
311 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
312 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
313 (dest)->inst_i0 = arg_array [(num)]; \
314 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
315 (dest)->opcode = OP_LDADDR; \
316 (dest)->type = STACK_MP; \
317 (dest)->klass = (dest)->inst_i0->klass; \
318 (cfg)->disable_ssa = TRUE; \
321 #define NEW_TEMPLOAD(cfg,dest,num) do { \
322 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
323 (dest)->ssa_op = MONO_SSA_LOAD; \
324 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
325 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
326 type_to_eval_stack_type ((dest)->inst_i0->inst_vtype, (dest)); \
327 (dest)->klass = (dest)->inst_i0->klass; \
330 #define NEW_TEMPLOADA(cfg,dest,num) do { \
331 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
332 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
333 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
334 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
335 (dest)->opcode = OP_LDADDR; \
336 (dest)->type = STACK_MP; \
337 (dest)->klass = (dest)->inst_i0->klass; \
338 (cfg)->disable_ssa = TRUE; \
342 #define NEW_INDLOAD(cfg,dest,addr,vtype) do { \
343 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
344 (dest)->inst_left = addr; \
345 (dest)->opcode = mono_type_to_ldind (vtype); \
346 type_to_eval_stack_type (vtype, (dest)); \
347 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
350 #define NEW_INDSTORE(cfg,dest,addr,value,vtype) do { \
351 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
352 (dest)->inst_i0 = addr; \
353 (dest)->opcode = mono_type_to_stind (vtype); \
354 (dest)->inst_i1 = (value); \
355 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
358 #define NEW_TEMPSTORE(cfg,dest,num,inst) do { \
359 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
360 (dest)->ssa_op = MONO_SSA_STORE; \
361 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
362 (dest)->opcode = mono_type_to_stind ((dest)->inst_i0->inst_vtype); \
363 (dest)->inst_i1 = (inst); \
364 (dest)->klass = (dest)->inst_i0->klass; \
367 #define NEW_LOCSTORE(cfg,dest,num,inst) do { \
368 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
369 (dest)->opcode = mono_type_to_stind (header->locals [(num)]); \
370 (dest)->ssa_op = MONO_SSA_STORE; \
371 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
372 (dest)->inst_i1 = (inst); \
373 (dest)->klass = (dest)->inst_i0->klass; \
376 #define NEW_ARGSTORE(cfg,dest,num,inst) do { \
377 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
378 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
379 (dest)->opcode = mono_type_to_stind (param_types [(num)]); \
380 (dest)->ssa_op = MONO_SSA_STORE; \
381 (dest)->inst_i0 = arg_array [(num)]; \
382 (dest)->inst_i1 = (inst); \
383 (dest)->klass = (dest)->inst_i0->klass; \
386 #define ADD_BINOP(op) do { \
387 MONO_INST_NEW (cfg, ins, (op)); \
388 ins->cil_code = ip; \
390 ins->inst_i0 = sp [0]; \
391 ins->inst_i1 = sp [1]; \
393 type_from_op (ins); \
397 #define ADD_UNOP(op) do { \
398 MONO_INST_NEW (cfg, ins, (op)); \
399 ins->cil_code = ip; \
401 ins->inst_i0 = sp [0]; \
403 type_from_op (ins); \
407 #define ADD_BINCOND(next_block) do { \
409 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
411 cmp->inst_i0 = sp [0]; \
412 cmp->inst_i1 = sp [1]; \
413 cmp->cil_code = ins->cil_code; \
414 type_from_op (cmp); \
416 ins->inst_i0 = cmp; \
417 MONO_ADD_INS (bblock, ins); \
418 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
419 GET_BBLOCK (cfg, bbhash, tblock, target); \
420 link_bblock (cfg, bblock, tblock); \
421 ins->inst_true_bb = tblock; \
422 CHECK_BBLOCK (target, ip, tblock); \
423 if ((next_block)) { \
424 link_bblock (cfg, bblock, (next_block)); \
425 ins->inst_false_bb = (next_block); \
426 start_new_bblock = 1; \
428 GET_BBLOCK (cfg, bbhash, tblock, ip); \
429 link_bblock (cfg, bblock, tblock); \
430 ins->inst_false_bb = tblock; \
431 start_new_bblock = 2; \
435 /* FIXME: handle float, long ... */
436 #define ADD_UNCOND(istrue) do { \
438 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
440 cmp->inst_i0 = sp [0]; \
441 switch (cmp->inst_i0->type) { \
443 cmp->inst_i1 = zero_int64; break; \
445 cmp->inst_i1 = zero_r8; break; \
448 cmp->inst_i1 = zero_ptr; break; \
450 cmp->inst_i1 = zero_obj; break; \
452 cmp->inst_i1 = zero_int32; \
454 cmp->cil_code = ins->cil_code; \
455 type_from_op (cmp); \
457 ins->inst_i0 = cmp; \
458 ins->opcode = (istrue)? CEE_BNE_UN: CEE_BEQ; \
459 MONO_ADD_INS (bblock, ins); \
460 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
461 GET_BBLOCK (cfg, bbhash, tblock, target); \
462 link_bblock (cfg, bblock, tblock); \
463 ins->inst_true_bb = tblock; \
464 CHECK_BBLOCK (target, ip, tblock); \
465 GET_BBLOCK (cfg, bbhash, tblock, ip); \
466 link_bblock (cfg, bblock, tblock); \
467 ins->inst_false_bb = tblock; \
468 start_new_bblock = 2; \
471 #define NEW_LDELEMA(cfg,dest,sp,k) do { \
472 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
473 (dest)->opcode = CEE_LDELEMA; \
474 (dest)->inst_left = (sp) [0]; \
475 (dest)->inst_right = (sp) [1]; \
476 (dest)->type = STACK_MP; \
477 (dest)->klass = (k); \
480 #define NEW_GROUP(cfg,dest,el1,el2) do { \
481 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
482 (dest)->opcode = OP_GROUP; \
483 (dest)->inst_left = (el1); \
484 (dest)->inst_right = (el2); \
487 static GHashTable *coverage_hash = NULL;
490 mono_allocate_coverage_info (MonoMethod *method, int size)
492 MonoCoverageInfo *res;
495 coverage_hash = g_hash_table_new (NULL, NULL);
497 res = g_malloc0 (sizeof (MonoCoverageInfo) + sizeof (int) * size * 2);
501 g_hash_table_insert (coverage_hash, method, res);
507 mono_get_coverage_info (MonoMethod *method)
512 return g_hash_table_lookup (coverage_hash, method);
517 compare_bblock (gconstpointer a, gconstpointer b)
519 const MonoBasicBlock *b1 = a;
520 const MonoBasicBlock *b2 = b;
522 return b2->cil_code - b1->cil_code;
527 * link_bblock: Links two basic blocks
529 * links two basic blocks in the control flow graph, the 'from'
530 * argument is the starting block and the 'to' argument is the block
531 * the control flow ends to after 'from'.
534 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
536 MonoBasicBlock **newa;
540 if (from->cil_code) {
542 g_print ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
544 g_print ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
547 g_print ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
549 g_print ("edge from entry to exit\n");
553 for (i = 0; i < from->out_count; ++i) {
554 if (to == from->out_bb [i]) {
560 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
561 for (i = 0; i < from->out_count; ++i) {
562 newa [i] = from->out_bb [i];
570 for (i = 0; i < to->in_count; ++i) {
571 if (from == to->in_bb [i]) {
577 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
578 for (i = 0; i < to->in_count; ++i) {
579 newa [i] = to->in_bb [i];
588 * We mark each basic block with a region ID. We use that to avoid BB
589 * optimizations when blocks are in different regions.
592 mono_find_block_region (MonoCompile *cfg, int offset, int *filter_lengths)
594 MonoMethod *method = cfg->method;
595 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
596 MonoExceptionClause *clause;
599 /* first search for handlers and filters */
600 for (i = 0; i < header->num_clauses; ++i) {
601 clause = &header->clauses [i];
602 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->token_or_filter) &&
603 (offset < (clause->token_or_filter + filter_lengths [i])))
604 return (i << 8) | 128 | clause->flags;
606 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
607 return (i << 8) | 64 | clause->flags;
611 /* search the try blocks */
612 for (i = 0; i < header->num_clauses; ++i) {
613 clause = &header->clauses [i];
614 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
615 return (i << 8) | clause->flags;
621 static MonoBasicBlock *
622 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
624 MonoMethod *method = cfg->method;
625 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
626 MonoExceptionClause *clause;
627 MonoBasicBlock *handler;
630 for (i = 0; i < header->num_clauses; ++i) {
631 clause = &header->clauses [i];
632 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
633 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
634 if (clause->flags & type) {
635 handler = g_hash_table_lookup (cfg->bb_hash, header->code + clause->handler_offset);
646 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
650 array [*dfn] = start;
651 /*g_print ("visit %d at %p\n", *dfn, start->cil_code);*/
652 for (i = 0; i < start->out_count; ++i) {
653 if (start->out_bb [i]->dfn)
656 start->out_bb [i]->dfn = *dfn;
657 start->out_bb [i]->df_parent = start;
658 array [*dfn] = start->out_bb [i];
659 df_visit (start->out_bb [i], dfn, array);
665 MonoBasicBlock *best;
669 previous_foreach (gconstpointer key, gpointer val, gpointer data)
671 PrevStruct *p = data;
672 MonoBasicBlock *bb = val;
673 //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,
674 //bb->method == p->best->method, bb->cil_code < p->code, bb->cil_code > p->best->cil_code);
676 if (bb->cil_code && bb->cil_code < p->code && bb->cil_code > p->best->cil_code)
680 static MonoBasicBlock*
681 find_previous (GHashTable *bb_hash, MonoBasicBlock *start, const guchar *code) {
687 g_hash_table_foreach (bb_hash, (GHFunc)previous_foreach, &p);
692 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
701 * FIXME: take into account all the details:
702 * second may have been the target of more than one bblock
704 second->out_count = first->out_count;
705 second->out_bb = first->out_bb;
707 for (i = 0; i < first->out_count; ++i) {
708 bb = first->out_bb [i];
709 for (j = 0; j < bb->in_count; ++j) {
710 if (bb->in_bb [j] == first)
711 bb->in_bb [j] = second;
715 first->out_count = 0;
716 first->out_bb = NULL;
717 link_bblock (cfg, first, second);
719 second->last_ins = first->last_ins;
721 /*g_print ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
722 for (inst = first->code; inst && inst->next; inst = inst->next) {
723 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
724 g_print ("found %p: %s", inst->next->cil_code, code);
726 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
727 second->code = inst->next;
729 first->last_ins = inst;
730 second->next_bb = first->next_bb;
731 first->next_bb = second;
736 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
742 mono_type_to_ldind (MonoType *type)
754 case MONO_TYPE_BOOLEAN:
768 case MONO_TYPE_FNPTR:
770 case MONO_TYPE_CLASS:
771 case MONO_TYPE_STRING:
772 case MONO_TYPE_OBJECT:
773 case MONO_TYPE_SZARRAY:
774 case MONO_TYPE_ARRAY:
775 return CEE_LDIND_REF;
783 case MONO_TYPE_VALUETYPE:
784 if (type->data.klass->enumtype) {
785 t = type->data.klass->enum_basetype->type;
790 g_error ("unknown type 0x%02x in type_to_ldind", type->type);
796 mono_type_to_stind (MonoType *type)
807 case MONO_TYPE_BOOLEAN:
819 case MONO_TYPE_FNPTR:
821 case MONO_TYPE_CLASS:
822 case MONO_TYPE_STRING:
823 case MONO_TYPE_OBJECT:
824 case MONO_TYPE_SZARRAY:
825 case MONO_TYPE_ARRAY:
826 return CEE_STIND_REF;
834 case MONO_TYPE_VALUETYPE:
835 if (type->data.klass->enumtype) {
836 t = type->data.klass->enum_basetype->type;
842 g_error ("unknown type %02x in type_to_stind", type->type);
848 * Returns the type used in the eval stack when @type is loaded.
849 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
852 type_to_eval_stack_type (MonoType *type, MonoInst *inst) {
856 inst->type = STACK_MP;
864 case MONO_TYPE_BOOLEAN:
870 inst->type = STACK_I4;
875 case MONO_TYPE_FNPTR:
876 inst->type = STACK_PTR;
878 case MONO_TYPE_CLASS:
879 case MONO_TYPE_STRING:
880 case MONO_TYPE_OBJECT:
881 case MONO_TYPE_SZARRAY:
882 case MONO_TYPE_ARRAY:
883 inst->type = STACK_OBJ;
887 inst->type = STACK_I8;
891 inst->type = STACK_R8;
893 case MONO_TYPE_VALUETYPE:
894 if (type->data.klass->enumtype) {
895 t = type->data.klass->enum_basetype->type;
898 inst->klass = type->data.klass;
899 inst->type = STACK_VTYPE;
903 g_error ("unknown type 0x%02x in eval stack type", type->type);
908 * The following tables are used to quickly validate the IL code in type_from_op ().
911 bin_num_table [STACK_MAX] [STACK_MAX] = {
912 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
913 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
914 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
915 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
916 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
917 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
918 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
919 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
924 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
927 /* reduce the size of this table */
929 bin_int_table [STACK_MAX] [STACK_MAX] = {
930 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
931 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
932 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
933 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
934 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
935 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
936 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
937 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
941 bin_comp_table [STACK_MAX] [STACK_MAX] = {
943 {0, 1, 0, 1, 0, 0, 4, 0},
944 {0, 0, 1, 0, 0, 0, 0, 0},
945 {0, 1, 0, 1, 0, 2, 4, 0},
946 {0, 0, 0, 0, 1, 0, 0, 0},
947 {0, 0, 0, 2, 0, 1, 0, 0},
948 {0, 4, 0, 4, 0, 0, 3, 0},
949 {0, 0, 0, 0, 0, 0, 0, 0},
952 /* reduce the size of this table */
954 shift_table [STACK_MAX] [STACK_MAX] = {
955 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
956 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
957 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
958 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
959 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
960 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
961 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
962 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
966 * Tables to map from the non-specific opcode to the matching
967 * type-specific opcode.
969 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
971 binops_op_map [STACK_MAX] = {
972 0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, 0
975 /* handles from CEE_NEG to CEE_CONV_U8 */
977 unops_op_map [STACK_MAX] = {
978 0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, 0
981 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
983 ovfops_op_map [STACK_MAX] = {
984 0, 0, OP_LCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_FCONV_TO_U2-CEE_CONV_U2, 0
987 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
989 ovf2ops_op_map [STACK_MAX] = {
990 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
993 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
995 ovf3ops_op_map [STACK_MAX] = {
996 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
999 /* handles from CEE_CEQ to CEE_CLT_UN */
1000 static const guint16
1001 ceqops_op_map [STACK_MAX] = {
1002 0, 0, OP_LCEQ-CEE_CEQ, OP_PCEQ-CEE_CEQ, OP_FCEQ-CEE_CEQ, 0
1006 * Sets ins->type (the type on the eval stack) according to the
1007 * type of the opcode and the arguments to it.
1008 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1010 * FIXME: this function sets ins->type unconditionally in some cases, but
1011 * it should set it to invalid for some types (a conv.x on an object)
1014 type_from_op (MonoInst *ins) {
1015 switch (ins->opcode) {
1022 /* FIXME: check unverifiable args for STACK_MP */
1023 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1024 ins->opcode += binops_op_map [ins->type];
1031 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1032 ins->opcode += binops_op_map [ins->type];
1037 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1038 ins->opcode += binops_op_map [ins->type];
1041 /* FIXME: handle some specifics with ins->next->type */
1042 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1046 case 256+CEE_CGT_UN:
1048 case 256+CEE_CLT_UN:
1049 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1050 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1054 ins->type = neg_table [ins->inst_i0->type];
1055 ins->opcode += unops_op_map [ins->type];
1058 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1059 ins->type = ins->inst_i0->type;
1061 ins->type = STACK_INV;
1062 ins->opcode += unops_op_map [ins->type];
1068 ins->type = STACK_I4;
1069 ins->opcode += unops_op_map [ins->inst_i0->type];
1072 ins->type = STACK_R8;
1073 switch (ins->inst_i0->type) {
1078 ins->opcode = OP_LCONV_TO_R_UN;
1082 case CEE_CONV_OVF_I1:
1083 case CEE_CONV_OVF_U1:
1084 case CEE_CONV_OVF_I2:
1085 case CEE_CONV_OVF_U2:
1086 case CEE_CONV_OVF_I4:
1087 case CEE_CONV_OVF_U4:
1088 ins->type = STACK_I4;
1089 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1091 case CEE_CONV_OVF_I_UN:
1092 case CEE_CONV_OVF_U_UN:
1093 ins->type = STACK_PTR;
1094 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1096 case CEE_CONV_OVF_I1_UN:
1097 case CEE_CONV_OVF_I2_UN:
1098 case CEE_CONV_OVF_I4_UN:
1099 case CEE_CONV_OVF_U1_UN:
1100 case CEE_CONV_OVF_U2_UN:
1101 case CEE_CONV_OVF_U4_UN:
1102 ins->type = STACK_I4;
1103 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1106 ins->type = STACK_PTR;
1107 switch (ins->inst_i0->type) {
1113 ins->opcode = OP_LCONV_TO_U;
1116 ins->opcode = OP_FCONV_TO_U;
1122 ins->type = STACK_I8;
1123 ins->opcode += unops_op_map [ins->inst_i0->type];
1125 case CEE_CONV_OVF_I8:
1126 case CEE_CONV_OVF_U8:
1127 ins->type = STACK_I8;
1128 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1130 case CEE_CONV_OVF_U8_UN:
1131 case CEE_CONV_OVF_I8_UN:
1132 ins->type = STACK_I8;
1133 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1137 ins->type = STACK_R8;
1138 ins->opcode += unops_op_map [ins->inst_i0->type];
1141 ins->type = STACK_R8;
1145 ins->type = STACK_I4;
1146 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1149 case CEE_CONV_OVF_I:
1150 case CEE_CONV_OVF_U:
1151 ins->type = STACK_PTR;
1152 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1155 case CEE_ADD_OVF_UN:
1157 case CEE_MUL_OVF_UN:
1159 case CEE_SUB_OVF_UN:
1160 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1161 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1164 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1171 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_MP, STACK_R8, STACK_R8, STACK_OBJ
1174 /* map ldelem.x to the matching ldind.x opcode */
1176 ldelem_to_ldind [] = {
1190 /* map stelem.x to the matching stind.x opcode */
1192 stelem_to_stind [] = {
1206 param_table [STACK_MAX] [STACK_MAX] = {
1211 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1215 switch (args->type) {
1225 for (i = 0; i < sig->param_count; ++i) {
1226 switch (args [i].type) {
1230 if (!sig->params [i]->byref)
1234 if (sig->params [i]->byref)
1236 switch (sig->params [i]->type) {
1237 case MONO_TYPE_CLASS:
1238 case MONO_TYPE_STRING:
1239 case MONO_TYPE_OBJECT:
1240 case MONO_TYPE_SZARRAY:
1241 case MONO_TYPE_ARRAY:
1248 if (sig->params [i]->byref)
1250 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1259 /*if (!param_table [args [i].type] [sig->params [i]->type])
1267 * When we need a pointer to the current domain many times in a method, we
1268 * call mono_domain_get() once and we store the result in a local variable.
1269 * This function returns the variable that represents the MonoDomain*.
1271 inline static MonoInst *
1272 mono_get_domainvar (MonoCompile *cfg)
1274 if (!cfg->domainvar)
1275 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1276 return cfg->domainvar;
1280 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
1283 int num = cfg->num_varinfo;
1285 if ((num + 1) >= cfg->varinfo_count) {
1286 cfg->varinfo_count = (cfg->varinfo_count + 2) * 2;
1287 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
1288 cfg->vars = (MonoMethodVar **)g_realloc (cfg->vars, sizeof (MonoMethodVar*) * cfg->varinfo_count);
1291 mono_jit_stats.allocate_var++;
1293 MONO_INST_NEW (cfg, inst, opcode);
1294 inst->inst_c0 = num;
1295 inst->inst_vtype = type;
1296 inst->klass = mono_class_from_mono_type (type);
1297 /* if set to 1 the variable is native */
1300 cfg->varinfo [num] = inst;
1302 cfg->vars [num] = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMethodVar));
1303 MONO_INIT_VARINFO (cfg->vars [num], num);
1306 //g_print ("created temp %d of type %s\n", num, mono_type_get_name (type));
1311 type_from_stack_type (MonoInst *ins) {
1312 switch (ins->type) {
1313 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1314 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1315 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1316 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1317 case STACK_MP: return &mono_defaults.int_class->byval_arg;
1318 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1319 case STACK_VTYPE: return &ins->klass->byval_arg;
1321 g_error ("stack type %d to montype not handled\n", ins->type);
1327 array_access_to_klass (int opcode)
1331 return mono_defaults.byte_class;
1333 return mono_defaults.uint16_class;
1336 return mono_defaults.int_class;
1339 return mono_defaults.sbyte_class;
1342 return mono_defaults.int16_class;
1345 return mono_defaults.int32_class;
1347 return mono_defaults.uint32_class;
1350 return mono_defaults.int64_class;
1353 return mono_defaults.single_class;
1356 return mono_defaults.double_class;
1357 case CEE_LDELEM_REF:
1358 case CEE_STELEM_REF:
1359 return mono_defaults.object_class;
1361 g_assert_not_reached ();
1367 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
1371 MONO_ADD_INS (bb, inst);
1374 switch (bb->last_ins->opcode) {
1387 while (prev->next && prev->next != bb->last_ins)
1389 if (prev == bb->code) {
1390 if (bb->last_ins == bb->code) {
1391 inst->next = bb->code;
1394 inst->next = prev->next;
1398 inst->next = bb->last_ins;
1402 // g_warning ("handle conditional jump in add_ins_to_end ()\n");
1404 MONO_ADD_INS (bb, inst);
1410 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
1412 MonoInst *inst, *load;
1414 NEW_TEMPLOAD (cfg, load, src);
1416 NEW_TEMPSTORE (cfg, inst, dest, load);
1417 if (inst->opcode == CEE_STOBJ) {
1418 NEW_TEMPLOADA (cfg, inst, dest);
1419 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE);
1421 inst->cil_code = NULL;
1422 mono_add_ins_to_end (bb, inst);
1427 * This function is called to handle items that are left on the evaluation stack
1428 * at basic block boundaries. What happens is that we save the values to local variables
1429 * and we reload them later when first entering the target basic block (with the
1430 * handle_loaded_temps () function).
1431 * A single joint point will use the same variables (stored in the array bb->out_stack or
1432 * bb->in_stack, if the basic block is before or after the joint point).
1435 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count) {
1437 MonoBasicBlock *outb;
1438 MonoInst *inst, **locals;
1442 if (cfg->verbose_level > 3)
1443 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
1444 if (!bb->out_scount) {
1446 bb->out_scount = count;
1447 //g_print ("bblock %d has out:", bb->block_num);
1448 for (i = 0; i < bb->out_count; ++i) {
1449 outb = bb->out_bb [i];
1450 //g_print (" %d", outb->block_num);
1451 if (outb->in_stack) {
1453 bb->out_stack = outb->in_stack;
1459 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1460 for (i = 0; i < count; ++i) {
1462 * dietmar suggests that we can reuse temps already allocated
1463 * for this purpouse, if they occupy the same stack slot and if
1464 * they are of the same type.
1466 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1470 locals = bb->out_stack;
1471 for (i = 0; i < count; ++i) {
1472 /* add store ops at the end of the bb, before the branch */
1473 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1474 if (inst->opcode == CEE_STOBJ) {
1475 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
1476 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE);
1478 inst->cil_code = sp [i]->cil_code;
1479 mono_add_ins_to_end (bb, inst);
1481 if (cfg->verbose_level > 3)
1482 g_print ("storing %d to temp %d\n", i, locals [i]->inst_c0);
1485 for (i = 0; i < bb->out_count; ++i) {
1486 outb = bb->out_bb [i];
1487 if (outb->in_scount)
1488 continue; /* check they are the same locals */
1489 outb->in_scount = count;
1490 outb->in_stack = locals;
1496 ret_type_to_call_opcode (MonoType *type, int calli, int virt)
1501 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1505 case MONO_TYPE_VOID:
1506 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
1509 case MONO_TYPE_BOOLEAN:
1512 case MONO_TYPE_CHAR:
1515 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1519 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1520 case MONO_TYPE_CLASS:
1521 case MONO_TYPE_STRING:
1522 case MONO_TYPE_OBJECT:
1523 case MONO_TYPE_SZARRAY:
1524 case MONO_TYPE_ARRAY:
1525 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1528 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
1531 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
1532 case MONO_TYPE_VALUETYPE:
1533 if (type->data.klass->enumtype) {
1534 t = type->data.klass->enum_basetype->type;
1537 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1539 g_error ("unknown type %02x in ret_type_to_call_opcode", type->type);
1545 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
1547 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
1549 ji->ip.label = label;
1550 ji->type = MONO_PATCH_INFO_SWITCH;
1551 ji->data.table = bbs;
1552 ji->next = cfg->patch_info;
1553 ji->table_size = num_blocks;
1554 cfg->patch_info = ji;
1558 * When we add a tree of instructions, we need to ensure the instructions currently
1559 * on the stack are executed before (like, if we load a value from a local).
1560 * We ensure this by saving the currently loaded values to temps and rewriting the
1561 * instructions to load the values.
1562 * This is not done for opcodes that terminate a basic block (because it's handled already
1563 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
1566 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
1568 MonoInst *load, *store, *temp, *ins;
1570 while (stack < sp) {
1572 /* handle also other constants */
1573 if (ins->opcode != OP_ICONST) {
1574 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1575 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1576 store->cil_code = ins->cil_code;
1577 if (store->opcode == CEE_STOBJ) {
1578 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
1579 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE);
1581 MONO_ADD_INS (bblock, store);
1582 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1583 load->cil_code = ins->cil_code;
1591 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
1592 const guint8 *ip, gboolean to_end)
1594 MonoInst *temp, *store, *ins = (MonoInst*)call;
1595 MonoType *ret = sig->ret;
1597 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
1599 call->inst.type = STACK_OBJ;
1600 call->inst.opcode = CEE_CALL;
1601 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
1603 type_to_eval_stack_type (ret, ins);
1604 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
1607 if (MONO_TYPE_ISSTRUCT (ret)) {
1610 /* we use this to allocate native sized structs */
1611 temp->unused = sig->pinvoke;
1613 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
1614 if (call->inst.opcode == OP_VCALL)
1615 ins->inst_left = loada;
1617 ins->inst_right = loada; /* a virtual or indirect call */
1620 mono_add_ins_to_end (bblock, ins);
1622 MONO_ADD_INS (bblock, ins);
1624 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1625 store->cil_code = ip;
1627 mono_add_ins_to_end (bblock, store);
1629 MONO_ADD_INS (bblock, store);
1631 return temp->inst_c0;
1634 mono_add_ins_to_end (bblock, ins);
1636 MONO_ADD_INS (bblock, ins);
1641 inline static MonoCallInst *
1642 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1643 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
1648 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual));
1650 call->inst.cil_code = ip;
1652 call->signature = sig;
1653 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
1655 for (i = 0; i < (sig->param_count + sig->hasthis); ++i) {
1656 if (call->args [i]) {
1658 mono_add_ins_to_end (bblock, call->args [i]);
1660 MONO_ADD_INS (bblock, call->args [i]);
1667 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1668 MonoInst **args, MonoInst *addr, const guint8 *ip)
1670 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
1672 call->inst.inst_i0 = addr;
1674 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
1677 static MonoCallInst*
1678 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
1679 MonoInst **args, const guint8 *ip, MonoInst *this)
1681 gboolean virtual = this != NULL;
1684 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, FALSE);
1686 if (this && sig->hasthis &&
1687 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
1688 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
1689 call->method = mono_marshal_get_remoting_invoke_with_check (method);
1691 call->method = method;
1693 call->inst.flags |= MONO_INST_HAS_METHOD;
1694 call->inst.inst_left = this;
1700 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
1701 MonoInst **args, const guint8 *ip, MonoInst *this)
1703 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, method->signature, args, ip, this);
1705 return mono_spill_call (cfg, bblock, call, method->signature, method->string_ctor, ip, FALSE);
1709 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
1710 MonoInst **args, const guint8 *ip, gboolean to_end)
1716 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
1718 return mono_spill_call (cfg, bblock, call, sig, func == mono_array_new_va, ip, to_end);
1722 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
1724 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
1727 g_warning ("unregistered JIT ICall");
1728 g_assert_not_reached ();
1731 return mono_emit_native_call (cfg, bblock, info->wrapper, info->sig, args, ip, FALSE);
1735 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
1737 MonoInst *ins, *temp = NULL, *store, *load;
1741 /*g_print ("emulating: ");
1742 mono_print_tree (tree);
1744 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE));
1745 ins = (MonoInst*)call;
1747 call->inst.cil_code = tree->cil_code;
1749 call->signature = info->sig;
1751 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
1753 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
1754 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
1755 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1756 store->cil_code = tree->cil_code;
1761 nargs = info->sig->param_count + info->sig->hasthis;
1763 for (i = 1; i < nargs; i++) {
1764 call->args [i - 1]->next = call->args [i];
1768 call->args [nargs - 1]->next = store;
1770 if (cfg->prev_ins) {
1771 store->next = cfg->prev_ins->next;
1773 cfg->prev_ins->next = call->args [0];
1775 cfg->prev_ins->next = store;
1777 store->next = cfg->cbb->code;
1779 cfg->cbb->code = call->args [0];
1781 cfg->cbb->code = store;
1785 call->fptr = info->wrapper;
1787 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
1788 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1793 static MonoMethodSignature *
1794 mono_get_element_address_signature (int arity)
1796 static GHashTable *sighash = NULL;
1797 MonoMethodSignature *res;
1801 sighash = g_hash_table_new (NULL, NULL);
1804 if ((res = g_hash_table_lookup (sighash, (gpointer)arity)))
1807 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
1809 res->params [0] = &mono_defaults.array_class->byval_arg;
1811 for (i = 1; i <= arity; i++)
1812 res->params [i] = &mono_defaults.int_class->byval_arg;
1814 res->ret = &mono_defaults.int_class->byval_arg;
1816 g_hash_table_insert (sighash, (gpointer)arity, res);
1822 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native) {
1823 MonoInst *iargs [3];
1828 * This check breaks with spilled vars... need to handle it during verification anyway.
1829 * g_assert (klass && klass == src->klass && klass == dest->klass);
1833 n = mono_class_native_size (klass, NULL);
1835 n = mono_class_value_size (klass, NULL);
1839 NEW_ICONST (cfg, iargs [2], n);
1841 mono_emit_native_call (cfg, bblock, helper_memcpy, helper_sig_memcpy, iargs, ip, to_end);
1845 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
1847 MonoInst *iargs [2];
1848 MonoInst *ins, *zero_int32;
1851 NEW_ICONST (cfg, zero_int32, 0);
1853 mono_class_init (klass);
1854 n = mono_class_value_size (klass, NULL);
1855 MONO_INST_NEW (cfg, ins, 0);
1857 ins->inst_left = dest;
1858 ins->inst_right = zero_int32;
1861 ins->opcode = CEE_STIND_I1;
1862 MONO_ADD_INS (bblock, ins);
1865 ins->opcode = CEE_STIND_I2;
1866 MONO_ADD_INS (bblock, ins);
1869 ins->opcode = CEE_STIND_I4;
1870 MONO_ADD_INS (bblock, ins);
1873 handle_loaded_temps (cfg, bblock, stack_start, sp);
1874 NEW_ICONST (cfg, ins, n);
1877 mono_emit_jit_icall (cfg, bblock, helper_initobj, iargs, ip);
1882 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
1885 mono_method_check_inlining (MonoMethod *method)
1887 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
1888 MonoMethodSignature *signature = method->signature;
1891 /* fixme: we should inline wrappers */
1892 if (method->wrapper_type != MONO_WRAPPER_NONE)
1895 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
1896 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
1897 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
1898 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
1899 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1900 (method->klass->marshalbyref) ||
1901 !header || header->num_clauses ||
1902 /* fixme: why cant we inline valuetype returns? */
1903 MONO_TYPE_ISSTRUCT (signature->ret))
1906 /* its not worth to inline methods with valuetype arguments?? */
1907 for (i = 0; i < signature->param_count; i++) {
1908 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
1913 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
1915 /* also consider num_locals? */
1916 if (header->code_size < 20)
1923 mini_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
1928 if (cmethod->klass == mono_defaults.string_class) {
1929 if (cmethod->name [0] != 'g' || strcmp (cmethod->name, "get_Chars"))
1932 } else if (cmethod->klass == mono_defaults.math_class) {
1933 if (strcmp (cmethod->name, "Sin") == 0)
1935 else if (strcmp (cmethod->name, "Cos") == 0)
1937 else if (strcmp (cmethod->name, "Tan") == 0)
1939 else if (strcmp (cmethod->name, "Atan") == 0)
1941 else if (strcmp (cmethod->name, "Sqrt") == 0)
1943 else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8)
1950 pc = fsig->param_count + fsig->hasthis;
1951 MONO_INST_NEW (cfg, ins, op);
1954 ins->inst_i0 = args [0];
1956 ins->inst_i1 = args [1];
1963 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
1965 MonoInst *store, *temp;
1968 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
1970 if (!sig->hasthis && sig->param_count == 0)
1974 if (sp [0]->opcode == OP_ICONST) {
1977 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
1979 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
1980 store->cil_code = sp [0]->cil_code;
1981 MONO_ADD_INS (bblock, store);
1986 for (i = 0; i < sig->param_count; ++i) {
1987 if (sp [0]->opcode == OP_ICONST) {
1990 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
1992 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
1993 store->cil_code = sp [0]->cil_code;
1994 if (store->opcode == CEE_STOBJ) {
1995 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
1996 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE);
1998 MONO_ADD_INS (bblock, store);
2006 * Some of these comments may well be out-of-date.
2007 * Design decisions: we do a single pass over the IL code (and we do bblock
2008 * splitting/merging in the few cases when it's required: a back jump to an IL
2009 * address that was not already seen as bblock starting point).
2010 * Code is validated as we go (full verification is still better left to metadata/verify.c).
2011 * Complex operations are decomposed in simpler ones right away. We need to let the
2012 * arch-specific code peek and poke inside this process somehow (except when the
2013 * optimizations can take advantage of the full semantic info of coarse opcodes).
2014 * All the opcodes of the form opcode.s are 'normalized' to opcode.
2015 * MonoInst->opcode initially is the IL opcode or some simplification of that
2016 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
2017 * opcode with value bigger than OP_LAST.
2018 * At this point the IR can be handed over to an interpreter, a dumb code generator
2019 * or to the optimizing code generator that will translate it to SSA form.
2021 * Profiling directed optimizations.
2022 * We may compile by default with few or no optimizations and instrument the code
2023 * or the user may indicate what methods to optimize the most either in a config file
2024 * or through repeated runs where the compiler applies offline the optimizations to
2025 * each method and then decides if it was worth it.
2028 * * consider using an array instead of an hash table (bb_hash)
2031 #define CHECK_TYPE(ins) if (!(ins)->type) goto unverified
2032 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) goto unverified
2033 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) goto unverified
2035 /* offset from br.s -> br like opcodes */
2036 #define BIG_BRANCH_OFFSET 13
2039 * mono_method_to_ir: translates IL into basic blocks containing trees
2042 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
2043 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
2044 guint inline_offset)
2046 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
2047 MonoInst *ins, **sp, **stack_start;
2048 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
2050 MonoMethod *cmethod;
2051 MonoInst **arg_array;
2052 MonoMethodHeader *header;
2054 guint32 token, ins_flag;
2056 unsigned char *ip, *end, *target;
2057 static double r8_0 = 0.0;
2058 MonoMethodSignature *sig;
2059 MonoType **param_types;
2060 GList *bb_recheck = NULL, *tmp;
2061 int i, n, start_new_bblock, align;
2062 int num_calls = 0, inline_costs = 0;
2063 int *filter_lengths = NULL;
2066 image = method->klass->image;
2067 header = ((MonoMethodNormal *)method)->header;
2068 sig = method->signature;
2069 ip = (unsigned char*)header->code;
2070 end = ip + header->code_size;
2071 mono_jit_stats.cil_code_size += header->code_size;
2073 if (cfg->method == method) {
2075 bbhash = cfg->bb_hash;
2077 real_offset = inline_offset;
2078 bbhash = g_hash_table_new (g_direct_hash, NULL);
2081 if (cfg->method == method) {
2084 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
2085 start_bblock->cil_code = NULL;
2086 start_bblock->cil_length = 0;
2087 start_bblock->block_num = cfg->num_bblocks++;
2090 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
2091 end_bblock->cil_code = NULL;
2092 end_bblock->cil_length = 0;
2093 end_bblock->block_num = cfg->num_bblocks++;
2094 g_assert (cfg->num_bblocks == 2);
2096 arg_array = alloca (sizeof (MonoInst *) * (sig->hasthis + sig->param_count));
2097 for (i = sig->hasthis + sig->param_count - 1; i >= 0; i--)
2098 arg_array [i] = cfg->varinfo [i];
2100 if (mono_compile_aot)
2101 cfg->opt |= MONO_OPT_SAHRED;
2103 if (header->num_clauses) {
2104 int size = sizeof (int) * header->num_clauses;
2105 filter_lengths = alloca (size);
2106 memset (filter_lengths, 0, size);
2108 /* handle exception clauses */
2109 for (i = 0; i < header->num_clauses; ++i) {
2110 //unsigned char *p = ip;
2111 MonoExceptionClause *clause = &header->clauses [i];
2112 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->try_offset);
2113 tblock->real_offset = clause->try_offset;
2114 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
2115 tblock->real_offset = clause->handler_offset;
2116 /*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);
2118 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
2120 /* catch and filter blocks get the exception object on the stack */
2121 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
2122 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2123 /* mostly like handle_stack_args (), but just sets the input args */
2124 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
2126 cfg->exvar = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
2127 /* prevent it from being register allocated */
2128 cfg->exvar->flags |= MONO_INST_INDIRECT;
2130 tblock->in_scount = 1;
2131 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2132 tblock->in_stack [0] = cfg->exvar;
2133 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2134 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->token_or_filter);
2135 tblock->real_offset = clause->token_or_filter;
2136 tblock->in_scount = 1;
2137 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2138 tblock->in_stack [0] = cfg->exvar;
2144 arg_array = alloca (sizeof (MonoInst *) * (sig->hasthis + sig->param_count));
2145 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
2148 /* FIRST CODE BLOCK */
2149 bblock = NEW_BBLOCK (cfg);
2150 bblock->cil_code = ip;
2152 ADD_BBLOCK (cfg, bbhash, bblock);
2154 if (cfg->method == method) {
2155 if (mono_debugger_method_has_breakpoint (method, FALSE)) {
2156 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2157 MONO_ADD_INS (bblock, ins);
2161 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SAHRED)))) {
2162 /* we use a separate basic block for the initialization code */
2163 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
2164 init_localsbb->real_offset = real_offset;
2165 start_bblock->next_bb = init_localsbb;
2166 init_localsbb->next_bb = bblock;
2167 link_bblock (cfg, start_bblock, init_localsbb);
2168 link_bblock (cfg, init_localsbb, bblock);
2169 init_localsbb->block_num = cfg->num_bblocks++;
2171 start_bblock->next_bb = bblock;
2172 link_bblock (cfg, start_bblock, bblock);
2175 mono_debug_init_method (cfg, bblock);
2177 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * (sig->hasthis + sig->param_count));
2179 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
2180 for (n = 0; n < sig->param_count; ++n)
2181 param_types [n + sig->hasthis] = sig->params [n];
2183 /* do this somewhere outside - not here */
2184 NEW_ICONST (cfg, zero_int32, 0);
2185 NEW_ICONST (cfg, zero_int64, 0);
2186 zero_int64->type = STACK_I8;
2187 NEW_PCONST (cfg, zero_ptr, 0);
2188 NEW_PCONST (cfg, zero_obj, 0);
2189 zero_obj->type = STACK_OBJ;
2191 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
2192 zero_r8->type = STACK_R8;
2193 zero_r8->inst_p0 = &r8_0;
2195 /* add a check for this != NULL to inlined methods */
2196 if (cfg->method != method && sig->hasthis) {
2197 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
2198 NEW_ARGLOAD (cfg, ins->inst_left, 0);
2200 MONO_ADD_INS (bblock, ins);
2203 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
2204 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
2207 start_new_bblock = 0;
2210 if (cfg->method == method)
2211 real_offset = ip - header->code;
2213 real_offset = inline_offset;
2215 if (start_new_bblock) {
2216 bblock->cil_length = ip - bblock->cil_code;
2217 if (start_new_bblock == 2) {
2218 g_assert (ip == tblock->cil_code);
2220 GET_BBLOCK (cfg, bbhash, tblock, ip);
2222 bblock->next_bb = tblock;
2224 start_new_bblock = 0;
2225 for (i = 0; i < bblock->in_scount; ++i) {
2226 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2230 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
2231 link_bblock (cfg, bblock, tblock);
2232 if (sp != stack_start) {
2233 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2236 bblock->next_bb = tblock;
2238 for (i = 0; i < bblock->in_scount; ++i) {
2239 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2245 if (cfg->verbose_level > 3)
2246 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, sp-stack_start, mono_disasm_code_one (NULL, method, ip, NULL));
2253 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2254 ins->cil_code = ip++;
2255 MONO_ADD_INS (bblock, ins);
2261 CHECK_STACK_OVF (1);
2262 n = (*ip)-CEE_LDARG_0;
2263 NEW_ARGLOAD (cfg, ins, n);
2264 ins->cil_code = ip++;
2271 CHECK_STACK_OVF (1);
2272 n = (*ip)-CEE_LDLOC_0;
2273 NEW_LOCLOAD (cfg, ins, n);
2274 ins->cil_code = ip++;
2282 n = (*ip)-CEE_STLOC_0;
2284 handle_loaded_temps (cfg, bblock, stack_start, sp);
2285 NEW_LOCSTORE (cfg, ins, n, *sp);
2287 if (ins->opcode == CEE_STOBJ) {
2288 NEW_LOCLOADA (cfg, ins, n);
2289 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2291 MONO_ADD_INS (bblock, ins);
2296 CHECK_STACK_OVF (1);
2297 NEW_ARGLOAD (cfg, ins, ip [1]);
2303 CHECK_STACK_OVF (1);
2304 NEW_ARGLOADA (cfg, ins, ip [1]);
2312 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
2313 handle_loaded_temps (cfg, bblock, stack_start, sp);
2315 if (ins->opcode == CEE_STOBJ) {
2316 NEW_ARGLOADA (cfg, ins, ip [1]);
2317 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2319 MONO_ADD_INS (bblock, ins);
2323 CHECK_STACK_OVF (1);
2324 NEW_LOCLOAD (cfg, ins, ip [1]);
2330 CHECK_STACK_OVF (1);
2331 NEW_LOCLOADA (cfg, ins, ip [1]);
2339 handle_loaded_temps (cfg, bblock, stack_start, sp);
2340 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
2342 if (ins->opcode == CEE_STOBJ) {
2343 NEW_LOCLOADA (cfg, ins, ip [1]);
2344 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2346 MONO_ADD_INS (bblock, ins);
2351 CHECK_STACK_OVF (1);
2352 NEW_PCONST (cfg, ins, NULL);
2354 ins->type = STACK_OBJ;
2359 CHECK_STACK_OVF (1);
2360 NEW_ICONST (cfg, ins, -1);
2374 CHECK_STACK_OVF (1);
2375 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
2381 CHECK_STACK_OVF (1);
2383 NEW_ICONST (cfg, ins, *((signed char*)ip));
2389 CHECK_STACK_OVF (1);
2390 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
2396 CHECK_STACK_OVF (1);
2397 MONO_INST_NEW (cfg, ins, OP_I8CONST);
2399 ins->type = STACK_I8;
2401 ins->inst_l = (gint64)read64 (ip);
2406 float *f = g_malloc (sizeof (float));
2407 CHECK_STACK_OVF (1);
2408 MONO_INST_NEW (cfg, ins, OP_R4CONST);
2409 ins->type = STACK_R8;
2418 double *d = g_malloc (sizeof (double));
2419 CHECK_STACK_OVF (1);
2420 MONO_INST_NEW (cfg, ins, OP_R8CONST);
2421 ins->type = STACK_R8;
2430 MonoInst *temp, *store;
2432 CHECK_STACK_OVF (1);
2437 * small optimization: if the loaded value was from a local already,
2438 * just load it twice.
2440 if (ins->ssa_op == MONO_SSA_LOAD &&
2441 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
2443 MONO_INST_NEW (cfg, temp, 0);
2445 temp->cil_code = ip;
2448 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
2449 temp->cil_code = ip;
2450 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2451 store->cil_code = ip;
2452 MONO_ADD_INS (bblock, store);
2453 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
2456 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
2466 MONO_INST_NEW (cfg, ins, CEE_POP);
2467 MONO_ADD_INS (bblock, ins);
2468 ins->cil_code = ip++;
2473 if (stack_start != sp)
2475 MONO_INST_NEW (cfg, ins, CEE_JMP);
2476 token = read32 (ip + 1);
2477 /* FIXME: check the signature matches */
2478 cmethod = mono_get_method (image, token, NULL);
2480 * The current magic trampoline can't handle this
2481 * apparently, so we compile the method right away.
2482 * Later, we may need to fix the trampoline or use a different one.
2484 ins->inst_p0 = mono_compile_method (cmethod);
2485 MONO_ADD_INS (bblock, ins);
2487 start_new_bblock = 1;
2491 case CEE_CALLVIRT: {
2492 MonoInst *addr = NULL;
2493 MonoMethodSignature *fsig = NULL;
2494 MonoMethodHeader *cheader;
2495 int temp, array_rank = 0;
2496 int virtual = *ip == CEE_CALLVIRT;
2498 token = read32 (ip + 1);
2500 if (*ip == CEE_CALLI) {
2506 if (method->wrapper_type != MONO_WRAPPER_NONE)
2507 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
2509 fsig = mono_metadata_parse_signature (image, token);
2511 n = fsig->param_count + fsig->hasthis;
2514 cmethod = mono_get_method (image, token, NULL);
2515 cheader = ((MonoMethodNormal *)cmethod)->header;
2517 if (!cmethod->klass->inited)
2518 mono_class_init (cmethod->klass);
2520 if (cmethod->signature->pinvoke) {
2521 #ifdef MONO_USE_EXC_TABLES
2522 if (mono_method_blittable (cmethod)) {
2523 fsig = cmethod->signature;
2526 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
2527 fsig = wrapper->signature;
2528 #ifdef MONO_USE_EXC_TABLES
2532 fsig = cmethod->signature;
2535 n = fsig->param_count + fsig->hasthis;
2537 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
2538 cmethod->klass->parent == mono_defaults.array_class) {
2539 array_rank = cmethod->klass->rank;
2542 if (cmethod->string_ctor)
2543 g_assert_not_reached ();
2549 //g_assert (!virtual || fsig->hasthis);
2553 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_opcode_for_method (cfg, cmethod, fsig, sp))) {
2556 if (MONO_TYPE_IS_VOID (fsig->ret)) {
2557 MONO_ADD_INS (bblock, ins);
2559 type_to_eval_stack_type (fsig->ret, ins);
2568 if ((cfg->opt & MONO_OPT_INLINE) &&
2569 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
2570 cmethod && cheader && mono_method_check_inlining (cmethod) &&
2571 method != cmethod && !g_list_find (dont_inline, cmethod)) {
2572 MonoInst *rvar = NULL;
2573 MonoBasicBlock *ebblock, *sbblock;
2574 int costs, new_locals_offset;
2576 if (cfg->verbose_level > 2)
2577 g_print ("INLINE START %p %s\n", cmethod, mono_method_full_name (cmethod, TRUE));
2579 if (!cmethod->inline_info) {
2580 mono_jit_stats.inlineable_methods++;
2581 cmethod->inline_info = 1;
2583 /* allocate space to store the return value */
2584 if (!MONO_TYPE_IS_VOID (fsig->ret))
2585 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2587 /* allocate local variables */
2588 new_locals_offset = cfg->num_varinfo;
2589 for (i = 0; i < cheader->num_locals; ++i)
2590 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
2592 /* allocate starte and end blocks */
2593 sbblock = NEW_BBLOCK (cfg);
2594 sbblock->block_num = cfg->num_bblocks++;
2595 sbblock->real_offset = real_offset;
2597 ebblock = NEW_BBLOCK (cfg);
2598 ebblock->block_num = cfg->num_bblocks++;
2599 ebblock->real_offset = real_offset;
2601 dont_inline = g_list_prepend (dont_inline, method);
2602 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset);
2603 dont_inline = g_list_remove (dont_inline, method);
2605 if (costs >= 0 && costs < 60) {
2607 mono_jit_stats.inlined_methods++;
2609 /* always add some code to avoid block split failures */
2610 MONO_INST_NEW (cfg, ins, CEE_NOP);
2611 MONO_ADD_INS (bblock, ins);
2617 bblock->next_bb = sbblock;
2618 link_bblock (cfg, bblock, sbblock);
2620 GET_BBLOCK (cfg, bbhash, bblock, ip);
2621 ebblock->next_bb = bblock;
2622 link_bblock (cfg, ebblock, bblock);
2625 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
2628 if (sp != stack_start) {
2629 handle_stack_args (cfg, ebblock, stack_start, sp - stack_start);
2632 start_new_bblock = 1;
2633 if (cfg->verbose_level > 2)
2634 g_print ("INLINE END %s\n", mono_method_full_name (cmethod, TRUE));
2636 // { static int c = 0; printf ("ICOUNT %d %d %s\n", c++, costs, mono_method_full_name (cmethod, TRUE)); }
2638 inline_costs += costs;
2642 if (cfg->verbose_level > 2)
2643 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
2648 inline_costs += 10 * num_calls++;
2649 handle_loaded_temps (cfg, bblock, stack_start, sp);
2651 /* tail recursion elimination */
2652 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == cfg->method && ip [5] == CEE_RET) {
2653 gboolean has_vtargs = FALSE;
2656 /* keep it simple */
2657 for (i = fsig->param_count - 1; i >= 0; i--) {
2658 if (MONO_TYPE_ISSTRUCT (cmethod->signature->params [i]))
2663 for (i = 0; i < n; ++i) {
2664 NEW_ARGSTORE (cfg, ins, i, sp [i]);
2666 MONO_ADD_INS (bblock, ins);
2668 MONO_INST_NEW (cfg, ins, CEE_BR);
2670 MONO_ADD_INS (bblock, ins);
2671 tblock = start_bblock->out_bb [0];
2672 link_bblock (cfg, bblock, tblock);
2673 ins->inst_target_bb = tblock;
2674 start_new_bblock = 1;
2677 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2678 /* just create a dummy - the value is never used */
2679 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2680 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
2688 if (*ip == CEE_CALLI) {
2690 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
2691 NEW_TEMPLOAD (cfg, *sp, temp);
2695 } else if (array_rank) {
2696 MonoMethodSignature *esig;
2699 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
2700 esig = mono_get_element_address_signature (fsig->param_count - 1);
2702 temp = mono_emit_native_call (cfg, bblock, ves_array_element_address, esig, sp, ip, FALSE);
2703 NEW_TEMPLOAD (cfg, addr, temp);
2704 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
2706 if (ins->opcode == CEE_STOBJ) {
2707 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
2709 MONO_ADD_INS (bblock, ins);
2712 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
2713 esig = mono_get_element_address_signature (fsig->param_count);
2715 temp = mono_emit_native_call (cfg, bblock, ves_array_element_address, esig, sp, ip, FALSE);
2716 NEW_TEMPLOAD (cfg, addr, temp);
2717 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
2721 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
2723 esig = mono_get_element_address_signature (fsig->param_count);
2725 temp = mono_emit_native_call (cfg, bblock, ves_array_element_address, esig, sp, ip, FALSE);
2726 NEW_TEMPLOAD (cfg, *sp, temp);
2729 g_assert_not_reached ();
2733 if (0 && CODE_IS_STLOC (ip + 5) && (!MONO_TYPE_ISSTRUCT (fsig->ret)) && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)) {
2734 /* no need to spill */
2735 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
2738 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, sp, ip, virtual ? sp [0] : NULL)) != -1) {
2739 NEW_TEMPLOAD (cfg, *sp, temp);
2749 if (cfg->method != method) {
2750 /* return from inlined methode */
2755 //g_assert (returnvar != -1);
2756 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
2757 store->cil_code = sp [0]->cil_code;
2758 if (store->opcode == CEE_STOBJ) {
2759 g_assert_not_reached ();
2760 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
2761 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
2763 MONO_ADD_INS (bblock, store);
2767 g_assert (!return_var);
2770 MONO_INST_NEW (cfg, ins, CEE_NOP);
2771 ins->opcode = mono_type_to_stind (method->signature->ret);
2772 if (ins->opcode == CEE_STOBJ) {
2773 NEW_RETLOADA (cfg, ins);
2774 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2776 ins->opcode = OP_SETRET;
2778 ins->inst_i0 = *sp;;
2779 ins->inst_i1 = NULL;
2780 MONO_ADD_INS (bblock, ins);
2784 if (sp != stack_start)
2786 MONO_INST_NEW (cfg, ins, CEE_BR);
2787 ins->cil_code = ip++;
2788 ins->inst_target_bb = end_bblock;
2789 MONO_ADD_INS (bblock, ins);
2790 link_bblock (cfg, bblock, end_bblock);
2791 start_new_bblock = 1;
2794 MONO_INST_NEW (cfg, ins, CEE_BR);
2795 ins->cil_code = ip++;
2796 MONO_ADD_INS (bblock, ins);
2797 target = ip + 1 + (signed char)(*ip);
2799 GET_BBLOCK (cfg, bbhash, tblock, target);
2800 link_bblock (cfg, bblock, tblock);
2801 CHECK_BBLOCK (target, ip, tblock);
2802 ins->inst_target_bb = tblock;
2803 if (sp != stack_start) {
2804 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2807 start_new_bblock = 1;
2813 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
2814 ins->cil_code = ip++;
2815 target = ip + 1 + *(signed char*)ip;
2817 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
2818 if (sp != stack_start) {
2819 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2835 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
2836 ins->cil_code = ip++;
2837 target = ip + 1 + *(signed char*)ip;
2840 if (sp != stack_start) {
2841 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2847 MONO_INST_NEW (cfg, ins, CEE_BR);
2848 ins->cil_code = ip++;
2849 MONO_ADD_INS (bblock, ins);
2850 target = ip + 4 + (gint32)read32(ip);
2852 GET_BBLOCK (cfg, bbhash, tblock, target);
2853 link_bblock (cfg, bblock, tblock);
2854 CHECK_BBLOCK (target, ip, tblock);
2855 ins->inst_target_bb = tblock;
2856 if (sp != stack_start) {
2857 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2860 start_new_bblock = 1;
2866 MONO_INST_NEW (cfg, ins, *ip);
2867 ins->cil_code = ip++;
2868 target = ip + 4 + (gint32)read32(ip);
2870 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
2871 if (sp != stack_start) {
2872 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2888 MONO_INST_NEW (cfg, ins, *ip);
2889 ins->cil_code = ip++;
2890 target = ip + 4 + (gint32)read32(ip);
2893 if (sp != stack_start) {
2894 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2901 n = read32 (ip + 1);
2902 MONO_INST_NEW (cfg, ins, *ip);
2904 ins->inst_left = *sp;
2905 if (ins->inst_left->type != STACK_I4) goto unverified;
2908 target = ip + n * sizeof (guint32);
2909 MONO_ADD_INS (bblock, ins);
2910 GET_BBLOCK (cfg, bbhash, tblock, target);
2911 link_bblock (cfg, bblock, tblock);
2912 ins->klass = GUINT_TO_POINTER (n);
2913 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
2914 ins->inst_many_bb [n] = tblock;
2916 for (i = 0; i < n; ++i) {
2917 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
2918 link_bblock (cfg, bblock, tblock);
2919 ins->inst_many_bb [i] = tblock;
2922 /* FIXME: handle stack args */
2937 MONO_INST_NEW (cfg, ins, *ip);
2942 ins->type = ldind_type [*ip - CEE_LDIND_I1];
2943 ins->flags |= ins_flag;
2955 MONO_INST_NEW (cfg, ins, *ip);
2956 ins->cil_code = ip++;
2958 handle_loaded_temps (cfg, bblock, stack_start, sp);
2959 MONO_ADD_INS (bblock, ins);
2960 ins->inst_i0 = sp [0];
2961 ins->inst_i1 = sp [1];
2962 ins->flags |= ins_flag;
2993 case CEE_CONV_OVF_I8:
2994 case CEE_CONV_OVF_U8:
3000 case CEE_CONV_OVF_I4:
3001 case CEE_CONV_OVF_I1:
3002 case CEE_CONV_OVF_I2:
3003 case CEE_CONV_OVF_I:
3004 case CEE_CONV_OVF_U:
3007 if (sp [-1]->type == STACK_R8) {
3008 ADD_UNOP (CEE_CONV_OVF_I8);
3016 case CEE_CONV_OVF_U1:
3017 case CEE_CONV_OVF_U2:
3018 case CEE_CONV_OVF_U4:
3021 if (sp [-1]->type == STACK_R8) {
3022 ADD_UNOP (CEE_CONV_OVF_U8);
3030 case CEE_CONV_OVF_I1_UN:
3031 case CEE_CONV_OVF_I2_UN:
3032 case CEE_CONV_OVF_I4_UN:
3033 case CEE_CONV_OVF_I8_UN:
3034 case CEE_CONV_OVF_U1_UN:
3035 case CEE_CONV_OVF_U2_UN:
3036 case CEE_CONV_OVF_U4_UN:
3037 case CEE_CONV_OVF_U8_UN:
3038 case CEE_CONV_OVF_I_UN:
3039 case CEE_CONV_OVF_U_UN:
3045 g_error ("opcode 0x%02x not handled", *ip);
3048 MonoInst *iargs [3];
3051 token = read32 (ip + 1);
3052 if (method->wrapper_type != MONO_WRAPPER_NONE)
3053 klass = mono_method_get_wrapper_data (method, token);
3055 klass = mono_class_get (image, token);
3057 mono_class_init (klass);
3058 n = mono_class_value_size (klass, NULL);
3059 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3060 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3062 NEW_ICONST (cfg, iargs [2], n);
3063 iargs [2]->cil_code = ip;
3065 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3066 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3073 CHECK_STACK_OVF (1);
3074 n = read32 (ip + 1);
3076 if (mono_compile_aot) {
3077 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n);
3080 if ((cfg->opt & MONO_OPT_SAHRED) || mono_compile_aot) {
3082 MonoInst *iargs [3];
3083 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3084 NEW_IMAGECONST (cfg, iargs [1], image);
3085 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
3086 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
3087 NEW_TEMPLOAD (cfg, *sp, temp);
3088 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3090 NEW_PCONST (cfg, ins, NULL);
3092 ins->type = STACK_OBJ;
3093 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3100 MonoInst *iargs [2];
3103 token = read32 (ip + 1);
3104 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3105 cmethod = mono_method_get_wrapper_data (method, token);
3107 cmethod = mono_get_method (image, token, NULL);
3109 mono_class_init (cmethod->klass);
3111 n = cmethod->signature->param_count;
3114 /* move the args to allow room for 'this' in the first position */
3120 handle_loaded_temps (cfg, bblock, stack_start, sp);
3123 if (cmethod->klass->parent == mono_defaults.array_class) {
3124 NEW_METHODCONST (cfg, *sp, cmethod);
3125 temp = mono_emit_native_call (cfg, bblock, mono_array_new_va, cmethod->signature, sp, ip, FALSE);
3127 } else if (cmethod->string_ctor) {
3128 /* we simply pass a null pointer */
3129 NEW_PCONST (cfg, *sp, NULL);
3130 /* now call the string ctor */
3131 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, sp, ip, NULL);
3133 if (cmethod->klass->valuetype) {
3134 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
3135 temp = iargs [0]->inst_c0;
3136 NEW_TEMPLOADA (cfg, *sp, temp);
3138 NEW_DOMAINCONST (cfg, iargs [0]);
3139 NEW_CLASSCONST (cfg, iargs [1], cmethod->klass);
3141 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3142 NEW_TEMPLOAD (cfg, *sp, temp);
3145 /* now call the actual ctor */
3146 mono_emit_method_call_spilled (cfg, bblock, cmethod, sp, ip, NULL);
3149 NEW_TEMPLOAD (cfg, *sp, temp);
3158 MONO_INST_NEW (cfg, ins, *ip);
3160 klass = mono_class_get (image, read32 (ip + 1));
3161 mono_class_init (klass);
3162 ins->type = STACK_OBJ;
3163 ins->inst_left = *sp;
3164 ins->inst_newa_class = klass;
3170 MonoInst *add, *vtoffset;
3171 /* FIXME: need to check class: move to inssel.brg? */
3174 token = read32 (ip + 1);
3175 if (method->wrapper_type != MONO_WRAPPER_NONE)
3176 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3178 klass = mono_class_get (image, token);
3179 mono_class_init (klass);
3180 MONO_INST_NEW (cfg, add, CEE_ADD);
3181 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3182 add->inst_left = *sp;
3183 add->inst_right = vtoffset;
3184 add->type = STACK_MP;
3192 MONO_INST_NEW (cfg, ins, *ip);
3194 klass = mono_class_get (image, read32 (ip + 1));
3195 mono_class_init (klass);
3196 ins->type = STACK_OBJ;
3197 ins->inst_left = *sp;
3199 ins->inst_newa_class = klass;
3206 MONO_INST_NEW (cfg, ins, *ip);
3208 ins->inst_left = *sp;
3209 ins->cil_code = ip++;
3210 MONO_ADD_INS (bblock, ins);
3212 start_new_bblock = 1;
3217 MonoInst *offset_ins;
3218 MonoClassField *field;
3221 if (*ip == CEE_STFLD) {
3228 // FIXME: enable this test later.
3229 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
3231 token = read32 (ip + 1);
3232 field = mono_field_from_token (image, token, &klass);
3233 mono_class_init (klass);
3234 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
3235 /* FIXME: mark instructions for use in SSA */
3236 if (*ip == CEE_STFLD) {
3237 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
3238 /* fixme: we need to inline that call somehow */
3239 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
3240 MonoInst *iargs [5];
3242 NEW_CLASSCONST (cfg, iargs [1], klass);
3243 NEW_FIELDCONST (cfg, iargs [2], field);
3244 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
3246 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, iargs, ip, NULL);
3249 NEW_ICONST (cfg, offset_ins, foffset);
3250 MONO_INST_NEW (cfg, ins, CEE_ADD);
3252 ins->inst_left = *sp;
3253 ins->inst_right = offset_ins;
3254 ins->type = STACK_MP;
3256 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
3257 store->cil_code = ip;
3258 store->inst_left = ins;
3259 store->inst_right = sp [1];
3260 handle_loaded_temps (cfg, bblock, stack_start, sp);
3261 store->flags |= ins_flag;
3263 if (store->opcode == CEE_STOBJ) {
3264 handle_stobj (cfg, bblock, ins, sp [1], ip,
3265 mono_class_from_mono_type (field->type), FALSE, FALSE);
3267 MONO_ADD_INS (bblock, store);
3270 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
3271 /* fixme: we need to inline that call somehow */
3272 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
3273 MonoInst *iargs [4];
3276 NEW_CLASSCONST (cfg, iargs [1], klass);
3277 NEW_FIELDCONST (cfg, iargs [2], field);
3278 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
3279 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, iargs, ip, NULL);
3280 if (*ip == CEE_LDFLDA) {
3281 /* not sure howto handle this */
3282 NEW_TEMPLOADA (cfg, *sp, temp);
3284 NEW_TEMPLOAD (cfg, *sp, temp);
3288 NEW_ICONST (cfg, offset_ins, foffset);
3289 MONO_INST_NEW (cfg, ins, CEE_ADD);
3291 ins->inst_left = *sp;
3292 ins->inst_right = offset_ins;
3293 ins->type = STACK_MP;
3295 if (*ip == CEE_LDFLDA) {
3299 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
3300 type_to_eval_stack_type (field->type, load);
3301 load->cil_code = ip;
3302 load->inst_left = ins;
3303 load->flags |= ins_flag;
3315 MonoClassField *field;
3318 token = read32 (ip + 1);
3320 field = mono_field_from_token (image, token, &klass);
3321 mono_class_init (klass);
3323 handle_loaded_temps (cfg, bblock, stack_start, sp);
3325 if (((cfg->opt & MONO_OPT_SAHRED) || mono_compile_aot)) {
3327 MonoInst *iargs [2];
3328 g_assert (field->parent);
3329 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3330 NEW_FIELDCONST (cfg, iargs [1], field);
3331 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
3332 NEW_TEMPLOAD (cfg, ins, temp);
3334 vtable = mono_class_vtable (cfg->domain, klass);
3335 NEW_PCONST (cfg, ins, (char*)vtable->data + field->offset);
3339 /* FIXME: mark instructions for use in SSA */
3340 if (*ip == CEE_LDSFLDA) {
3342 } else if (*ip == CEE_STSFLD) {
3346 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
3347 store->cil_code = ip;
3348 store->inst_left = ins;
3349 store->inst_right = sp [0];
3350 store->flags |= ins_flag;
3353 if (store->opcode == CEE_STOBJ) {
3354 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
3356 MONO_ADD_INS (bblock, store);
3359 CHECK_STACK_OVF (1);
3360 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
3361 type_to_eval_stack_type (field->type, load);
3362 load->cil_code = ip;
3363 load->inst_left = ins;
3365 load->flags |= ins_flag;
3367 /* fixme: dont see the problem why this does not work */
3368 //cfg->disable_aot = TRUE;
3376 token = read32 (ip + 1);
3377 if (method->wrapper_type != MONO_WRAPPER_NONE)
3378 klass = mono_method_get_wrapper_data (method, token);
3380 klass = mono_class_get (image, token);
3381 mono_class_init (klass);
3382 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
3387 MonoInst *iargs [2];
3388 MonoInst *load, *vtoffset, *add, *val, *vstore;
3393 token = read32 (ip + 1);
3394 if (method->wrapper_type != MONO_WRAPPER_NONE)
3395 klass = mono_method_get_wrapper_data (method, token);
3397 klass = mono_class_get (image, token);
3398 mono_class_init (klass);
3400 /* much like NEWOBJ */
3401 NEW_DOMAINCONST (cfg, iargs [0]);
3402 NEW_CLASSCONST (cfg, iargs [1], klass);
3404 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3405 NEW_TEMPLOAD (cfg, load, temp);
3406 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3407 MONO_INST_NEW (cfg, add, CEE_ADD);
3408 add->inst_left = load;
3409 add->inst_right = vtoffset;
3412 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
3413 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
3414 vstore->cil_code = ip;
3415 vstore->inst_left = add;
3416 vstore->inst_right = val;
3418 if (vstore->opcode == CEE_STOBJ) {
3419 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
3421 MONO_ADD_INS (bblock, vstore);
3423 NEW_TEMPLOAD (cfg, load, temp);
3431 MONO_INST_NEW (cfg, ins, *ip);
3435 token = read32 (ip + 1);
3437 /* allocate the domainvar - becaus this is used in decompose_foreach */
3438 if ((cfg->opt & MONO_OPT_SAHRED) || mono_compile_aot)
3439 mono_get_domainvar (cfg);
3441 if (method->wrapper_type != MONO_WRAPPER_NONE)
3442 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3444 klass = mono_class_get (image, token);
3446 mono_class_init (klass);
3447 ins->inst_newa_class = klass;
3448 ins->inst_newa_len = *sp;
3449 ins->type = STACK_OBJ;
3456 MONO_INST_NEW (cfg, ins, *ip);
3457 ins->cil_code = ip++;
3459 ins->inst_left = *sp;
3460 ins->type = STACK_PTR;
3466 klass = mono_class_get (image, read32 (ip + 1));
3467 mono_class_init (klass);
3468 NEW_LDELEMA (cfg, ins, sp, klass);
3483 case CEE_LDELEM_REF: {
3487 * ldind.x (ldelema (array, index))
3488 * ldelema does the bounds check
3492 klass = array_access_to_klass (*ip);
3493 NEW_LDELEMA (cfg, load, sp, klass);
3494 load->cil_code = ip;
3495 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
3497 ins->inst_left = load;
3499 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
3509 case CEE_STELEM_R8: {
3513 * stind.x (ldelema (array, index), val)
3514 * ldelema does the bounds check
3518 klass = array_access_to_klass (*ip);
3519 NEW_LDELEMA (cfg, load, sp, klass);
3520 load->cil_code = ip;
3521 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
3523 ins->inst_left = load;
3524 ins->inst_right = sp [2];
3526 handle_loaded_temps (cfg, bblock, stack_start, sp);
3527 MONO_ADD_INS (bblock, ins);
3528 /* FIXME: add the implicit STELEM_REF castclass */
3530 cfg->disable_ssa = TRUE;
3533 case CEE_STELEM_REF: {
3534 MonoInst *iargs [3];
3539 handle_loaded_temps (cfg, bblock, stack_start, sp);
3545 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
3549 NEW_GROUP (cfg, group, sp [0], sp [1]);
3550 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
3552 ins->inst_left = group;
3553 ins->inst_right = sp [2];
3554 MONO_ADD_INS (bblock, ins);
3559 cfg->disable_ssa = TRUE;
3562 case CEE_CKFINITE: {
3563 MonoInst *store, *temp;
3566 /* this instr. can throw exceptions as side effect,
3567 * so we cant eliminate dead code which contains CKFINITE opdodes.
3568 * Spilling to memory makes sure that we always perform
3572 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
3574 ins->inst_left = sp [-1];
3575 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
3577 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3578 store->cil_code = ip;
3579 MONO_ADD_INS (bblock, store);
3581 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
3588 g_error ("opcode 0x%02x not handled", *ip);
3592 MonoClass *handle_class;
3594 CHECK_STACK_OVF (1);
3596 n = read32 (ip + 1);
3598 handle = mono_ldtoken (image, n, &handle_class);
3599 mono_class_init (handle_class);
3601 if (((cfg->opt & MONO_OPT_SAHRED) || mono_compile_aot)) {
3603 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
3605 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
3607 NEW_IMAGECONST (cfg, iargs [0], image);
3608 NEW_ICONST (cfg, iargs [1], n);
3609 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
3610 NEW_TEMPLOAD (cfg, res, temp);
3611 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
3612 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
3613 MONO_ADD_INS (bblock, store);
3614 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
3616 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method (image, read32 (ip + 6), NULL)) &&
3617 (cmethod->klass == mono_defaults.monotype_class->parent) &&
3618 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
3619 MonoClass *tclass = mono_class_from_mono_type (handle);
3620 mono_class_init (tclass);
3621 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
3622 ins->type = STACK_OBJ;
3623 ins->klass = cmethod->klass;
3626 NEW_PCONST (cfg, ins, handle);
3627 ins->type = STACK_VTYPE;
3628 ins->klass = handle_class;
3644 case CEE_ADD_OVF_UN:
3646 case CEE_MUL_OVF_UN:
3648 case CEE_SUB_OVF_UN:
3653 case CEE_ENDFINALLY:
3654 /* FIXME: check stack state */
3655 MONO_INST_NEW (cfg, ins, *ip);
3656 MONO_ADD_INS (bblock, ins);
3657 ins->cil_code = ip++;
3658 start_new_bblock = 1;
3662 if (*ip == CEE_LEAVE) {
3663 target = ip + 5 + (gint32)read32(ip + 1);
3665 target = ip + 2 + (signed char)(ip [1]);
3668 /* empty the stack */
3669 while (sp != stack_start) {
3670 MONO_INST_NEW (cfg, ins, CEE_POP);
3674 MONO_ADD_INS (bblock, ins);
3677 /* fixme: call fault handler ? */
3679 if ((tblock = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
3680 link_bblock (cfg, bblock, tblock);
3681 MONO_INST_NEW (cfg, ins, OP_HANDLER);
3683 ins->inst_target_bb = tblock;
3684 MONO_ADD_INS (bblock, ins);
3688 MONO_INST_NEW (cfg, ins, CEE_BR);
3690 MONO_ADD_INS (bblock, ins);
3691 GET_BBLOCK (cfg, bbhash, tblock, target);
3692 link_bblock (cfg, bblock, tblock);
3693 CHECK_BBLOCK (target, ip, tblock);
3694 ins->inst_target_bb = tblock;
3695 start_new_bblock = 1;
3697 if (*ip == CEE_LEAVE)
3706 MONO_INST_NEW (cfg, ins, *ip);
3708 handle_loaded_temps (cfg, bblock, stack_start, sp);
3709 MONO_ADD_INS (bblock, ins);
3710 ins->cil_code = ip++;
3711 ins->inst_i0 = sp [0];
3712 ins->inst_i1 = sp [1];
3720 /* trampoline mono specific opcodes */
3721 case MONO_CUSTOM_PREFIX: {
3723 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
3727 case CEE_MONO_FUNC1: {
3729 gpointer func = NULL;
3734 case MONO_MARSHAL_CONV_STR_LPWSTR:
3735 func = mono_string_to_utf16;
3737 case MONO_MARSHAL_CONV_LPWSTR_STR:
3738 func = mono_string_from_utf16;
3740 case MONO_MARSHAL_CONV_LPSTR_STR:
3741 func = mono_string_new_wrapper;
3743 case MONO_MARSHAL_CONV_STR_LPTSTR:
3744 case MONO_MARSHAL_CONV_STR_LPSTR:
3745 func = mono_string_to_utf8;
3747 case MONO_MARSHAL_CONV_STR_BSTR:
3748 func = mono_string_to_bstr;
3750 case MONO_MARSHAL_CONV_STR_TBSTR:
3751 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
3752 func = mono_string_to_ansibstr;
3754 case MONO_MARSHAL_CONV_SB_LPSTR:
3755 func = mono_string_builder_to_utf8;
3757 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
3758 func = mono_array_to_savearray;
3760 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
3761 func = mono_array_to_lparray;
3763 case MONO_MARSHAL_CONV_DEL_FTN:
3764 func = mono_delegate_to_ftnptr;
3766 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
3767 func = mono_marshal_string_array;
3770 g_warning ("unknown conversion %d\n", ip [2]);
3771 g_assert_not_reached ();
3774 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
3775 NEW_TEMPLOAD (cfg, *sp, temp);
3779 inline_costs += 10 * num_calls++;
3782 case CEE_MONO_PROC2: {
3783 gpointer func = NULL;
3788 case MONO_MARSHAL_CONV_LPSTR_SB:
3789 func = mono_string_utf8_to_builder;
3791 case MONO_MARSHAL_FREE_ARRAY:
3792 func = mono_marshal_free_array;
3795 g_assert_not_reached ();
3798 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
3800 inline_costs += 10 * num_calls++;
3803 case CEE_MONO_PROC3: {
3804 gpointer func = NULL;
3809 case MONO_MARSHAL_CONV_STR_BYVALSTR:
3810 func = mono_string_to_byvalstr;
3812 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
3813 func = mono_string_to_byvalwstr;
3816 g_assert_not_reached ();
3819 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
3821 inline_costs += 10 * num_calls++;
3827 mono_emit_jit_icall (cfg, bblock, g_free, sp, ip);
3829 inline_costs += 10 * num_calls++;
3831 case CEE_MONO_LDPTR:
3832 CHECK_STACK_OVF (1);
3833 token = read32 (ip + 2);
3834 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
3838 inline_costs += 10 * num_calls++;
3840 case CEE_MONO_VTADDR:
3843 MONO_INST_NEW (cfg, ins, OP_VTADDR);
3845 ins->type = STACK_MP;
3846 ins->inst_left = *sp;
3850 case CEE_MONO_NEWOBJ: {
3851 MonoInst *iargs [2];
3853 CHECK_STACK_OVF (1);
3854 token = read32 (ip + 2);
3855 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3856 mono_class_init (klass);
3857 NEW_DOMAINCONST (cfg, iargs [0]);
3858 NEW_CLASSCONST (cfg, iargs [1], klass);
3859 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3860 NEW_TEMPLOAD (cfg, *sp, temp);
3863 inline_costs += 10 * num_calls++;
3866 case CEE_MONO_OBJADDR:
3869 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
3871 ins->type = STACK_MP;
3872 ins->inst_left = *sp;
3876 case CEE_MONO_LDNATIVEOBJ:
3878 token = read32 (ip + 2);
3879 klass = mono_method_get_wrapper_data (method, token);
3880 g_assert (klass->valuetype);
3881 mono_class_init (klass);
3882 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
3886 case CEE_MONO_RETOBJ:
3887 g_assert (cfg->ret);
3888 g_assert (method->signature->pinvoke);
3892 token = read32 (ip + 2);
3893 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3895 NEW_RETLOADA (cfg, ins);
3896 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
3898 if (sp != stack_start)
3901 MONO_INST_NEW (cfg, ins, CEE_BR);
3903 ins->inst_target_bb = end_bblock;
3904 MONO_ADD_INS (bblock, ins);
3905 link_bblock (cfg, bblock, end_bblock);
3906 start_new_bblock = 1;
3910 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
3918 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
3927 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
3928 MONO_INST_NEW (cfg, ins, cmp->opcode);
3930 cmp->inst_i0 = sp [0];
3931 cmp->inst_i1 = sp [1];
3935 cmp->opcode = OP_COMPARE;
3937 ins->type = STACK_I4;
3947 CHECK_STACK_OVF (1);
3948 n = read32 (ip + 2);
3949 if (method->wrapper_type != MONO_WRAPPER_NONE)
3950 cmethod = mono_method_get_wrapper_data (method, n);
3952 cmethod = mono_get_method (image, n, NULL);
3955 * We can't do this in mono_ldftn, since it is used in
3956 * the synchronized wrapper, leading to an infinite loop.
3958 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
3959 cmethod = mono_marshal_get_synchronized_wrapper (cmethod);
3962 mono_class_init (cmethod->klass);
3963 handle_loaded_temps (cfg, bblock, stack_start, sp);
3965 NEW_METHODCONST (cfg, argconst, cmethod);
3966 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
3967 NEW_TEMPLOAD (cfg, *sp, temp);
3971 inline_costs += 10 * num_calls++;
3974 case CEE_LDVIRTFTN: {
3979 n = read32 (ip + 2);
3980 if (method->wrapper_type != MONO_WRAPPER_NONE)
3981 cmethod = mono_method_get_wrapper_data (method, n);
3983 cmethod = mono_get_method (image, n, NULL);
3985 mono_class_init (cmethod->klass);
3986 handle_loaded_temps (cfg, bblock, stack_start, sp);
3990 NEW_METHODCONST (cfg, args [1], cmethod);
3991 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
3992 NEW_TEMPLOAD (cfg, *sp, temp);
3996 inline_costs += 10 * num_calls++;
4000 CHECK_STACK_OVF (1);
4001 NEW_ARGLOAD (cfg, ins, read16 (ip + 2));
4007 CHECK_STACK_OVF (1);
4008 NEW_ARGLOADA (cfg, ins, read16 (ip + 2));
4016 handle_loaded_temps (cfg, bblock, stack_start, sp);
4017 n = read16 (ip + 2);
4018 NEW_ARGSTORE (cfg, ins, n, *sp);
4020 if (ins->opcode == CEE_STOBJ) {
4021 NEW_ARGLOADA (cfg, ins, n);
4022 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4024 MONO_ADD_INS (bblock, ins);
4028 CHECK_STACK_OVF (1);
4029 NEW_LOCLOAD (cfg, ins, read16 (ip + 2));
4035 CHECK_STACK_OVF (1);
4036 NEW_LOCLOADA (cfg, ins, read16 (ip + 2));
4044 n = read16 (ip + 2);
4045 handle_loaded_temps (cfg, bblock, stack_start, sp);
4046 NEW_LOCSTORE (cfg, ins, n, *sp);
4048 if (ins->opcode == CEE_STOBJ) {
4049 NEW_LOCLOADA (cfg, ins, n);
4050 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4052 MONO_ADD_INS (bblock, ins);
4059 if (sp != stack_start)
4061 MONO_INST_NEW (cfg, ins, 256 + ip [1]);
4062 ins->inst_left = *sp;
4065 if (header->init_locals)
4066 ins->flags |= MONO_INST_INIT;
4070 /* FIXME: set init flag if locals init is set in this method */
4072 case CEE_ENDFILTER: {
4073 MonoExceptionClause *clause, *nearest;
4074 int cc, nearest_num;
4078 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
4080 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
4081 ins->inst_left = *sp;
4083 MONO_ADD_INS (bblock, ins);
4084 start_new_bblock = 1;
4088 for (cc = 0; cc < header->num_clauses; ++cc) {
4089 clause = &header->clauses [cc];
4090 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
4091 (!nearest || (clause->token_or_filter > nearest->token_or_filter))) {
4097 filter_lengths [nearest_num] = (ip - header->code) - nearest->token_or_filter;
4101 case CEE_UNALIGNED_:
4102 ins_flag |= MONO_INST_UNALIGNED;
4106 ins_flag |= MONO_INST_VOLATILE;
4110 ins_flag |= MONO_INST_TAILCALL;
4116 token = read32 (ip + 2);
4117 if (method->wrapper_type != MONO_WRAPPER_NONE)
4118 klass = mono_method_get_wrapper_data (method, token);
4120 klass = mono_class_get (image, token);
4121 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
4127 MonoInst *iargs [3];
4133 handle_loaded_temps (cfg, bblock, stack_start, sp);
4134 if (ip [1] == CEE_CPBLK) {
4135 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
4137 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
4145 /* FIXME: check we are in a catch handler */
4146 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
4147 load->cil_code = ip;
4148 MONO_INST_NEW (cfg, ins, CEE_THROW);
4149 ins->inst_left = load;
4151 MONO_ADD_INS (bblock, ins);
4153 start_new_bblock = 1;
4158 CHECK_STACK_OVF (1);
4159 token = read32 (ip + 2);
4160 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
4161 MonoType *type = mono_type_create_from_typespec (image, token);
4162 token = mono_type_size (type, &align);
4163 mono_metadata_free_type (type);
4165 MonoClass *szclass = mono_class_get (image, token);
4166 mono_class_init (szclass);
4167 token = mono_class_value_size (szclass, &align);
4169 NEW_ICONST (cfg, ins, token);
4174 case CEE_REFANYTYPE:
4175 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
4178 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
4183 g_error ("opcode 0x%02x not handled", *ip);
4186 if (start_new_bblock != 1)
4189 bblock->cil_length = ip - bblock->cil_code;
4190 bblock->next_bb = end_bblock;
4191 link_bblock (cfg, bblock, end_bblock);
4193 if (cfg->method == method && cfg->domainvar) {
4197 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
4198 call->signature = helper_sig_domain_get;
4199 call->inst.type = STACK_PTR;
4200 call->fptr = mono_domain_get;
4201 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, (MonoInst*)call);
4203 MONO_ADD_INS (init_localsbb, store);
4206 if (header->init_locals) {
4208 for (i = 0; i < header->num_locals; ++i) {
4209 int t = header->locals [i]->type;
4210 if (t == MONO_TYPE_VALUETYPE && header->locals [i]->data.klass->enumtype)
4211 t = header->locals [i]->data.klass->enum_basetype->type;
4212 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
4213 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
4214 NEW_ICONST (cfg, ins, 0);
4215 NEW_LOCSTORE (cfg, store, i, ins);
4216 MONO_ADD_INS (init_localsbb, store);
4217 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
4218 MONO_INST_NEW (cfg, ins, OP_I8CONST);
4219 ins->type = STACK_I8;
4221 NEW_LOCSTORE (cfg, store, i, ins);
4222 MONO_ADD_INS (init_localsbb, store);
4223 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
4224 MONO_INST_NEW (cfg, ins, OP_R8CONST);
4225 ins->type = STACK_R8;
4226 ins->inst_p0 = (void*)&r8_0;
4227 NEW_LOCSTORE (cfg, store, i, ins);
4228 MONO_ADD_INS (init_localsbb, store);
4229 } else if (t == MONO_TYPE_VALUETYPE) {
4230 NEW_LOCLOADA (cfg, ins, i);
4231 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (header->locals [i]), NULL, NULL);
4234 NEW_PCONST (cfg, ins, NULL);
4235 NEW_LOCSTORE (cfg, store, i, ins);
4236 MONO_ADD_INS (init_localsbb, store);
4242 /* resolve backward branches in the middle of an existing basic block */
4243 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
4245 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
4246 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
4247 if (tblock != start_bblock) {
4249 split_bblock (cfg, tblock, bblock);
4250 l = bblock->cil_code - header->code;
4251 bblock->cil_length = tblock->cil_length - l;
4252 tblock->cil_length = l;
4254 g_print ("recheck failed.\n");
4258 /* we compute regions here, because the length of filter clauses is not known in advance.
4259 * It is computed in the CEE_ENDFILTER case in the above switch statement*/
4260 if (cfg->method == method) {
4262 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
4263 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
4264 if (cfg->verbose_level > 2)
4265 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
4268 g_hash_table_destroy (bbhash);
4271 return inline_costs;
4274 if (cfg->method != method)
4275 g_hash_table_destroy (bbhash);
4279 if (cfg->method != method)
4280 g_hash_table_destroy (bbhash);
4281 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
4282 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
4287 mono_print_tree (MonoInst *tree) {
4293 arity = mono_burg_arity [tree->opcode];
4295 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
4297 switch (tree->opcode) {
4299 printf ("[%d]", tree->inst_c0);
4302 printf ("[%lld]", tree->inst_l);
4305 printf ("[%f]", *(double*)tree->inst_p0);
4308 printf ("[%f]", *(float*)tree->inst_p0);
4312 printf ("[%d]", tree->inst_c0);
4315 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
4318 printf ("[%s]", mono_arch_regname (tree->dreg));
4321 printf ("[%s]", tree->inst_newa_class->name);
4322 mono_print_tree (tree->inst_newa_len);
4333 case OP_VOIDCALLVIRT: {
4334 MonoCallInst *call = (MonoCallInst*)tree;
4336 printf ("[%s]", call->method->name);
4341 printf ("[%d (", tree->inst_c0);
4342 for (i = 0; i < tree->inst_phi_args [0]; i++) {
4345 printf ("%d", tree->inst_phi_args [i + 1]);
4357 printf ("[B%d]", tree->inst_target_bb->block_num);
4367 case OP_VOIDCALL_REG:
4368 mono_print_tree (tree->inst_left);
4380 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
4381 mono_print_tree (tree->inst_left);
4385 mono_print_tree (tree->inst_left);
4387 mono_print_tree (tree->inst_right);
4397 create_helper_signature (void)
4399 /* FIXME: set call conv */
4400 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
4401 helper_sig_newarr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4402 helper_sig_newarr->params [0] = helper_sig_newarr->params [1] = &mono_defaults.int_class->byval_arg;
4403 helper_sig_newarr->ret = &mono_defaults.object_class->byval_arg;
4404 helper_sig_newarr->params [2] = &mono_defaults.int32_class->byval_arg;
4405 helper_sig_newarr->pinvoke = 1;
4407 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
4408 helper_sig_object_new = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4409 helper_sig_object_new->params [0] = helper_sig_object_new->params [1] = &mono_defaults.int_class->byval_arg;
4410 helper_sig_object_new->ret = &mono_defaults.object_class->byval_arg;
4411 helper_sig_object_new->pinvoke = 1;
4413 /* void* mono_method_compile (MonoMethod*) */
4414 helper_sig_compile = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4415 helper_sig_compile->params [0] = helper_sig_compile->ret = &mono_defaults.int_class->byval_arg;
4416 helper_sig_compile->pinvoke = 1;
4418 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
4419 helper_sig_compile_virt = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4420 helper_sig_compile_virt->params [0] = &mono_defaults.object_class->byval_arg;
4421 helper_sig_compile_virt->params [1] = helper_sig_compile_virt->ret = &mono_defaults.int_class->byval_arg;
4422 helper_sig_compile_virt->pinvoke = 1;
4424 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
4425 helper_sig_ldstr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4426 helper_sig_ldstr->params [0] = helper_sig_ldstr->params [1] = &mono_defaults.int_class->byval_arg;
4427 helper_sig_ldstr->params [2] = &mono_defaults.int32_class->byval_arg;
4428 helper_sig_ldstr->ret = &mono_defaults.object_class->byval_arg;
4429 helper_sig_ldstr->pinvoke = 1;
4431 /* MonoDomain *mono_domain_get (void) */
4432 helper_sig_domain_get = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
4433 helper_sig_domain_get->ret = &mono_defaults.int_class->byval_arg;
4434 helper_sig_domain_get->pinvoke = 1;
4436 /* void* stelem_ref (MonoArray *, int index, MonoObject *) */
4437 helper_sig_stelem_ref = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4438 helper_sig_stelem_ref->params [0] = &mono_defaults.array_class->byval_arg;
4439 helper_sig_stelem_ref->params [1] = &mono_defaults.int32_class->byval_arg;
4440 helper_sig_stelem_ref->params [2] = &mono_defaults.object_class->byval_arg;
4441 helper_sig_stelem_ref->ret = &mono_defaults.void_class->byval_arg;
4442 helper_sig_stelem_ref->pinvoke = 1;
4444 /* long amethod (long, long) */
4445 helper_sig_long_long_long = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4446 helper_sig_long_long_long->params [0] = helper_sig_long_long_long->params [1] =
4447 &mono_defaults.int64_class->byval_arg;
4448 helper_sig_long_long_long->ret = &mono_defaults.int64_class->byval_arg;
4449 helper_sig_long_long_long->pinvoke = 1;
4451 /* object amethod (intptr) */
4452 helper_sig_obj_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4453 helper_sig_obj_ptr->params [0] = &mono_defaults.int_class->byval_arg;
4454 helper_sig_obj_ptr->ret = &mono_defaults.object_class->byval_arg;
4455 helper_sig_obj_ptr->pinvoke = 1;
4457 /* void amethod (intptr) */
4458 helper_sig_void_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4459 helper_sig_void_ptr->params [0] = &mono_defaults.int_class->byval_arg;
4460 helper_sig_void_ptr->ret = &mono_defaults.void_class->byval_arg;
4461 helper_sig_void_ptr->pinvoke = 1;
4463 /* void amethod (MonoObject *obj) */
4464 helper_sig_void_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4465 helper_sig_void_obj->params [0] = &mono_defaults.object_class->byval_arg;
4466 helper_sig_void_obj->ret = &mono_defaults.void_class->byval_arg;
4467 helper_sig_void_obj->pinvoke = 1;
4469 /* intptr amethod (void) */
4470 helper_sig_ptr_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
4471 helper_sig_ptr_void->ret = &mono_defaults.int_class->byval_arg;
4472 helper_sig_ptr_void->pinvoke = 1;
4474 /* void amethod (intptr, intptr) */
4475 helper_sig_void_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4476 helper_sig_void_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
4477 helper_sig_void_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
4478 helper_sig_void_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
4479 helper_sig_void_ptr_ptr->pinvoke = 1;
4481 /* void amethod (intptr, intptr, intptr) */
4482 helper_sig_void_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4483 helper_sig_void_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
4484 helper_sig_void_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
4485 helper_sig_void_ptr_ptr_ptr->params [2] = &mono_defaults.int_class->byval_arg;
4486 helper_sig_void_ptr_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
4487 helper_sig_void_ptr_ptr_ptr->pinvoke = 1;
4489 /* intptr amethod (intptr, intptr) */
4490 helper_sig_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4491 helper_sig_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
4492 helper_sig_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
4493 helper_sig_ptr_ptr_ptr->ret = &mono_defaults.int_class->byval_arg;
4494 helper_sig_ptr_ptr_ptr->pinvoke = 1;
4496 /* IntPtr amethod (object) */
4497 helper_sig_ptr_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4498 helper_sig_ptr_obj->params [0] = &mono_defaults.object_class->byval_arg;
4499 helper_sig_ptr_obj->ret = &mono_defaults.int_class->byval_arg;
4500 helper_sig_ptr_obj->pinvoke = 1;
4502 /* long amethod (long, guint32) */
4503 helper_sig_long_long_int = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4504 helper_sig_long_long_int->params [0] = &mono_defaults.int64_class->byval_arg;
4505 helper_sig_long_long_int->params [1] = &mono_defaults.int32_class->byval_arg;
4506 helper_sig_long_long_int->ret = &mono_defaults.int64_class->byval_arg;
4507 helper_sig_long_long_int->pinvoke = 1;
4509 /* ulong amethod (double) */
4510 helper_sig_ulong_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4511 helper_sig_ulong_double->params [0] = &mono_defaults.double_class->byval_arg;
4512 helper_sig_ulong_double->ret = &mono_defaults.uint64_class->byval_arg;
4513 helper_sig_ulong_double->pinvoke = 1;
4515 /* long amethod (double) */
4516 helper_sig_long_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4517 helper_sig_long_double->params [0] = &mono_defaults.double_class->byval_arg;
4518 helper_sig_long_double->ret = &mono_defaults.int64_class->byval_arg;
4519 helper_sig_long_double->pinvoke = 1;
4521 /* uint amethod (double) */
4522 helper_sig_uint_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4523 helper_sig_uint_double->params [0] = &mono_defaults.double_class->byval_arg;
4524 helper_sig_uint_double->ret = &mono_defaults.uint32_class->byval_arg;
4525 helper_sig_uint_double->pinvoke = 1;
4527 /* int amethod (double) */
4528 helper_sig_int_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4529 helper_sig_int_double->params [0] = &mono_defaults.double_class->byval_arg;
4530 helper_sig_int_double->ret = &mono_defaults.int32_class->byval_arg;
4531 helper_sig_int_double->pinvoke = 1;
4533 /* void initobj (intptr, int size) */
4534 helper_sig_initobj = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4535 helper_sig_initobj->params [0] = &mono_defaults.int_class->byval_arg;
4536 helper_sig_initobj->params [1] = &mono_defaults.int32_class->byval_arg;
4537 helper_sig_initobj->ret = &mono_defaults.void_class->byval_arg;
4538 helper_sig_initobj->pinvoke = 1;
4540 /* void memcpy (intptr, intptr, int size) */
4541 helper_sig_memcpy = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4542 helper_sig_memcpy->params [0] = &mono_defaults.int_class->byval_arg;
4543 helper_sig_memcpy->params [1] = &mono_defaults.int_class->byval_arg;
4544 helper_sig_memcpy->params [2] = &mono_defaults.int32_class->byval_arg;
4545 helper_sig_memcpy->ret = &mono_defaults.void_class->byval_arg;
4546 helper_sig_memcpy->pinvoke = 1;
4548 /* void memset (intptr, int val, int size) */
4549 helper_sig_memset = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4550 helper_sig_memset->params [0] = &mono_defaults.int_class->byval_arg;
4551 helper_sig_memset->params [1] = &mono_defaults.int32_class->byval_arg;
4552 helper_sig_memset->params [2] = &mono_defaults.int32_class->byval_arg;
4553 helper_sig_memset->ret = &mono_defaults.void_class->byval_arg;
4554 helper_sig_memset->pinvoke = 1;
4557 static GHashTable *jit_icall_hash_name = NULL;
4558 static GHashTable *jit_icall_hash_addr = NULL;
4561 mono_find_jit_icall_by_name (const char *name)
4563 g_assert (jit_icall_hash_name);
4565 //printf ("lookup addr %s %p\n", name, g_hash_table_lookup (jit_icall_hash_name, name));
4566 return g_hash_table_lookup (jit_icall_hash_name, name);
4570 mono_find_jit_icall_by_addr (gconstpointer addr)
4572 g_assert (jit_icall_hash_addr);
4574 return g_hash_table_lookup (jit_icall_hash_addr, (gpointer)addr);
4578 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save)
4580 MonoJitICallInfo *info;
4581 MonoMethod *wrapper;
4587 if (!jit_icall_hash_name) {
4588 jit_icall_hash_name = g_hash_table_new (g_str_hash, g_str_equal);
4589 jit_icall_hash_addr = g_hash_table_new (NULL, NULL);
4592 if (g_hash_table_lookup (jit_icall_hash_name, name)) {
4593 g_warning ("jit icall already defined \"%s\"\n", name);
4594 g_assert_not_reached ();
4597 info = g_new (MonoJitICallInfo, 1);
4599 info->name = g_strdup (name);
4604 #ifdef MONO_USE_EXC_TABLES
4605 || mono_arch_has_unwind_info (func)
4608 info->wrapper = func;
4611 n = g_strdup_printf ("__icall_wrapper_%s", name);
4612 wrapper = mono_marshal_get_icall_wrapper (sig, n, func);
4613 info->wrapper = mono_jit_compile_method (wrapper);
4617 g_hash_table_insert (jit_icall_hash_name, info->name, info);
4618 g_hash_table_insert (jit_icall_hash_addr, (gpointer)func, info);
4619 if (func != info->wrapper)
4620 g_hash_table_insert (jit_icall_hash_addr, (gpointer)info->wrapper, info);
4625 static GHashTable *emul_opcode_hash = NULL;
4627 static MonoJitICallInfo *
4628 mono_find_jit_opcode_emulation (int opcode)
4630 if (emul_opcode_hash)
4631 return g_hash_table_lookup (emul_opcode_hash, (gpointer)opcode);
4637 mono_register_opcode_emulation (int opcode, MonoMethodSignature *sig, gpointer func)
4639 MonoJitICallInfo *info;
4642 if (!emul_opcode_hash)
4643 emul_opcode_hash = g_hash_table_new (NULL, NULL);
4645 g_assert (!sig->hasthis);
4646 g_assert (sig->param_count < 3);
4648 name = g_strdup_printf ("__emulate_%s", mono_inst_name (opcode));
4650 info = mono_register_jit_icall (func, name, sig, FALSE);
4654 g_hash_table_insert (emul_opcode_hash, (gpointer)opcode, info);
4658 decompose_foreach (MonoInst *tree, gpointer data)
4660 static MonoJitICallInfo *newarr_info = NULL;
4662 switch (tree->opcode) {
4664 MonoCompile *cfg = data;
4665 MonoInst *iargs [3];
4667 NEW_DOMAINCONST (cfg, iargs [0]);
4668 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
4669 iargs [2] = tree->inst_newa_len;
4672 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
4673 g_assert (newarr_info);
4676 mono_emulate_opcode (cfg, tree, iargs, newarr_info);
4686 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
4688 switch (mono_burg_arity [tree->opcode]) {
4691 mono_inst_foreach (tree->inst_left, func, data);
4694 mono_inst_foreach (tree->inst_left, func, data);
4695 mono_inst_foreach (tree->inst_right, func, data);
4698 g_assert_not_reached ();
4705 mono_print_bb_code (MonoBasicBlock *bb) {
4707 MonoInst *c = bb->code;
4709 mono_print_tree (c);
4718 print_dfn (MonoCompile *cfg) {
4723 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
4725 for (i = 0; i < cfg->num_bblocks; ++i) {
4726 bb = cfg->bblocks [i];
4728 char* code1, *code2;
4729 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
4730 if (bb->last_ins->cil_code)
4731 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
4733 code2 = g_strdup ("");
4735 code1 [strlen (code1) - 1] = 0;
4736 code = g_strdup_printf ("%s -> %s", code1, code2);
4740 code = g_strdup ("\n");
4741 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
4743 MonoInst *c = bb->code;
4745 mono_print_tree (c);
4753 g_print ("\tprev:");
4754 for (j = 0; j < bb->in_count; ++j) {
4755 g_print (" BB%d", bb->in_bb [j]->block_num);
4757 g_print ("\t\tsucc:");
4758 for (j = 0; j < bb->out_count; ++j) {
4759 g_print (" BB%d", bb->out_bb [j]->block_num);
4761 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
4764 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
4767 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
4769 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
4777 * returns the offset used by spillvar. It allocates a new
4778 * spill variable if necessary.
4781 mono_spillvar_offset (MonoCompile *cfg, int spillvar)
4783 MonoSpillInfo **si, *info;
4786 si = &cfg->spill_info;
4788 while (i <= spillvar) {
4791 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
4793 cfg->stack_offset -= sizeof (gpointer);
4794 info->offset = cfg->stack_offset;
4798 return (*si)->offset;
4804 g_assert_not_reached ();
4809 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
4813 g_assert (bb->code);
4814 bb->last_ins->next = inst;
4815 bb->last_ins = inst;
4817 bb->last_ins = bb->code = inst;
4822 mono_destroy_compile (MonoCompile *cfg)
4824 //mono_mempool_stats (cfg->mempool);
4825 g_hash_table_destroy (cfg->bb_hash);
4827 mono_regstate_free (cfg->rs);
4828 mono_mempool_destroy (cfg->mempool);
4829 g_list_free (cfg->ldstr_list);
4831 g_free (cfg->varinfo);
4837 mono_get_lmf_addr (void)
4839 MonoJitTlsData *jit_tls;
4841 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
4842 return &jit_tls->lmf;
4844 g_assert_not_reached ();
4849 * mono_thread_abort:
4850 * @obj: exception object
4852 * abort the thread, print exception information and stack trace
4855 mono_thread_abort (MonoObject *obj)
4857 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
4865 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
4867 MonoJitTlsData *jit_tls;
4870 jit_tls = g_new0 (MonoJitTlsData, 1);
4872 TlsSetValue (mono_jit_tls_id, jit_tls);
4874 jit_tls->abort_func = mono_thread_abort;
4875 jit_tls->end_of_stack = stack_start;
4877 lmf = g_new0 (MonoLMF, 1);
4883 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
4886 mono_thread_abort_dummy (MonoObject *obj)
4888 if (mono_thread_attach_aborted_cb)
4889 mono_thread_attach_aborted_cb (obj);
4891 mono_thread_abort (obj);
4895 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
4897 MonoJitTlsData *jit_tls;
4900 jit_tls = g_new0 (MonoJitTlsData, 1);
4902 TlsSetValue (mono_jit_tls_id, jit_tls);
4904 jit_tls->abort_func = mono_thread_abort_dummy;
4905 jit_tls->end_of_stack = stack_start;
4907 lmf = g_new0 (MonoLMF, 1);
4914 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
4916 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
4920 ji->data.target = target;
4921 ji->next = cfg->patch_info;
4923 cfg->patch_info = ji;
4927 mono_remove_patch_info (MonoCompile *cfg, int ip)
4929 MonoJumpInfo **ji = &cfg->patch_info;
4932 if ((*ji)->ip.i == ip)
4935 ji = &((*ji)->next);
4940 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
4941 MonoJitICallInfo *info;
4943 switch (mono_burg_arity [tree->opcode]) {
4946 dec_foreach (tree->inst_left, cfg);
4948 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
4949 MonoInst *iargs [2];
4951 iargs [0] = tree->inst_left;
4953 mono_emulate_opcode (cfg, tree, iargs, info);
4959 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
4960 MonoInst *iargs [2];
4962 iargs [0] = tree->inst_i0;
4963 iargs [1] = tree->inst_i1;
4965 mono_emulate_opcode (cfg, tree, iargs, info);
4967 dec_foreach (iargs [0], cfg);
4968 dec_foreach (iargs [1], cfg);
4971 dec_foreach (tree->inst_left, cfg);
4972 dec_foreach (tree->inst_right, cfg);
4976 g_assert_not_reached ();
4978 decompose_foreach (tree, cfg);
4982 decompose_pass (MonoCompile *cfg) {
4985 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
4988 cfg->prev_ins = NULL;
4989 for (tree = cfg->cbb->code; tree; tree = tree->next) {
4990 dec_foreach (tree, cfg);
4991 cfg->prev_ins = tree;
4997 nullify_basic_block (MonoBasicBlock *bb)
5004 bb->code = bb->last_ins = NULL;
5008 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
5012 for (i = 0; i < bb->out_count; i++) {
5013 MonoBasicBlock *ob = bb->out_bb [i];
5014 for (j = 0; j < ob->in_count; j++) {
5015 if (ob->in_bb [j] == orig)
5016 ob->in_bb [j] = repl;
5023 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
5025 bb->out_count = bbn->out_count;
5026 bb->out_bb = bbn->out_bb;
5028 replace_basic_block (bb, bbn, bb);
5032 bb->last_ins->next = bbn->code;
5033 bb->last_ins = bbn->last_ins;
5036 bb->code = bbn->code;
5037 bb->last_ins = bbn->last_ins;
5039 bb->next_bb = bbn->next_bb;
5040 nullify_basic_block (bbn);
5044 optimize_branches (MonoCompile *cfg) {
5045 int changed = FALSE;
5046 MonoBasicBlock *bb, *bbn;
5051 /* we skip the entry block (exit is handled specially instead ) */
5052 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
5054 if (bb->out_count == 1) {
5055 bbn = bb->out_bb [0];
5057 if (bb->region == bbn->region && bb->next_bb == bbn) {
5058 /* the block are in sequence anyway ... */
5061 * miguel: I do not understand what the test below does, could we
5062 * use a macro, or a comment here? opcode > CEE_BEQ && <= BLT_UN
5064 * It could also test for bb->last_in only once, and the value
5065 * could be cached (last_ins->opcode)
5067 if (bb->last_ins && (bb->last_ins->opcode == CEE_BR || (
5068 (bb->last_ins && bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN)))) {
5069 bb->last_ins->opcode = CEE_NOP;
5071 if (cfg->verbose_level > 2)
5072 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
5074 /* fixme: this causes problems with inlining */
5075 if (bbn->in_count == 1) {
5077 if (bbn != cfg->bb_exit) {
5078 if (cfg->verbose_level > 2)
5079 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
5080 merge_basic_blocks (bb, bbn);
5084 //mono_print_bb_code (bb);
5087 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
5088 bbn = bb->last_ins->inst_target_bb;
5089 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR) {
5091 if (cfg->verbose_level > 2)
5092 g_print ("in %s branch to branch triggered %d -> %d\n", cfg->method->name, bb->block_num, bbn->block_num);
5093 bb->out_bb [0] = bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
5098 } else if (bb->out_count == 2) {
5099 /* fixme: this does not correctly unlink the blocks, so we get serious problems in idom code */
5100 if (0 && bb->last_ins && bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
5101 bbn = bb->last_ins->inst_true_bb;
5102 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR) {
5103 if (cfg->verbose_level > 2)
5104 g_print ("cbranch to branch triggered %d -> %d (0x%02x)\n", bb->block_num,
5105 bbn->block_num, bbn->code->opcode);
5107 if (bb->out_bb [0] == bbn) {
5108 bb->out_bb [0] = bbn->code->inst_target_bb;
5109 } else if (bb->out_bb [1] == bbn) {
5110 bb->out_bb [1] = bbn->code->inst_target_bb;
5112 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
5122 mono_compile_create_vars (MonoCompile *cfg)
5124 MonoMethodSignature *sig;
5125 MonoMethodHeader *header;
5128 header = ((MonoMethodNormal *)cfg->method)->header;
5130 sig = cfg->method->signature;
5132 if (!MONO_TYPE_IS_VOID (sig->ret)) {
5133 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
5134 cfg->ret->opcode = OP_RETARG;
5135 cfg->ret->inst_vtype = sig->ret;
5136 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
5140 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
5142 for (i = 0; i < sig->param_count; ++i)
5143 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
5145 cfg->locals_start = cfg->num_varinfo;
5147 for (i = 0; i < header->num_locals; ++i)
5148 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
5153 mono_print_code (MonoCompile *cfg)
5157 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5158 MonoInst *tree = bb->code;
5163 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
5165 for (; tree; tree = tree->next) {
5166 mono_print_tree (tree);
5171 bb->last_ins->next = NULL;
5176 extern const char * const mono_burg_rule_string [];
5179 emit_state (MonoCompile *cfg, MBState *state, int goal)
5182 int ern = mono_burg_rule (state, goal);
5183 const guint16 *nts = mono_burg_nts [ern];
5186 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
5190 // state->reg1 = state->reg2; /* chain rule */
5192 state->reg1 = mono_regstate_next_int (cfg->rs);
5193 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
5196 state->reg1 = mono_regstate_next_int (cfg->rs);
5197 state->reg2 = mono_regstate_next_int (cfg->rs);
5200 state->reg1 = mono_regstate_next_float (cfg->rs);
5207 mono_burg_kids (state, ern, kids);
5209 emit_state (cfg, kids [0], nts [0]);
5211 emit_state (cfg, kids [1], nts [1]);
5213 g_assert (!nts [3]);
5214 emit_state (cfg, kids [2], nts [2]);
5219 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
5220 if ((emit = mono_burg_func [ern]))
5221 emit (state, state->tree, cfg);
5224 #define DEBUG_SELECTION
5227 mini_select_instructions (MonoCompile *cfg)
5231 cfg->state_pool = mono_mempool_new ();
5232 cfg->rs = mono_regstate_new ();
5234 #ifdef DEBUG_SELECTION
5235 if (cfg->verbose_level >= 4) {
5236 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5237 MonoInst *tree = bb->code;
5238 g_print ("DUMP BLOCK %d:\n", bb->block_num);
5241 for (; tree; tree = tree->next) {
5242 mono_print_tree (tree);
5249 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5250 MonoInst *tree = bb->code, *next;
5256 bb->last_ins = NULL;
5259 mono_regstate_reset (cfg->rs);
5261 #ifdef DEBUG_SELECTION
5262 if (cfg->verbose_level >= 3)
5263 g_print ("LABEL BLOCK %d:\n", bb->block_num);
5265 for (; tree; tree = next) {
5267 #ifdef DEBUG_SELECTION
5268 if (cfg->verbose_level >= 3) {
5269 mono_print_tree (tree);
5274 if (!(mbstate = mono_burg_label (tree, cfg))) {
5275 g_warning ("unabled to label tree %p", tree);
5276 mono_print_tree (tree);
5278 g_assert_not_reached ();
5280 emit_state (cfg, mbstate, MB_NTERM_stmt);
5282 bb->max_ireg = cfg->rs->next_vireg;
5283 bb->max_freg = cfg->rs->next_vfreg;
5286 bb->last_ins->next = NULL;
5288 mono_mempool_empty (cfg->state_pool);
5290 mono_mempool_destroy (cfg->state_pool);
5294 mono_codegen (MonoCompile *cfg)
5296 MonoJumpInfo *patch_info;
5298 int i, max_epilog_size;
5301 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5302 cfg->spill_count = 0;
5303 /* we reuse dfn here */
5304 /* bb->dfn = bb_count++; */
5305 mono_arch_local_regalloc (cfg, bb);
5308 if (mono_trace_coverage)
5309 mono_allocate_coverage_info (cfg->method, cfg->num_bblocks);
5311 code = mono_arch_emit_prolog (cfg);
5313 if (mono_jit_profile)
5314 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
5316 cfg->code_len = code - cfg->native_code;
5317 cfg->prolog_end = cfg->code_len;
5319 mono_debug_open_method (cfg);
5321 /* emit code all basic blocks */
5322 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5323 bb->native_offset = cfg->code_len;
5324 mono_arch_output_basic_block (cfg, bb);
5326 cfg->bb_exit->native_offset = cfg->code_len;
5328 code = cfg->native_code + cfg->code_len;
5330 max_epilog_size = mono_arch_max_epilog_size (cfg);
5332 /* we always allocate code in cfg->domain->code_mp to increase locality */
5333 cfg->code_size = cfg->code_len + max_epilog_size;
5334 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
5335 code = mono_mempool_alloc (cfg->domain->code_mp, cfg->code_size);
5336 memcpy (code, cfg->native_code, cfg->code_len);
5337 g_free (cfg->native_code);
5338 cfg->native_code = code;
5339 code = cfg->native_code + cfg->code_len;
5341 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
5343 cfg->epilog_begin = cfg->code_len;
5345 if (mono_jit_profile)
5346 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
5348 cfg->code_len = code - cfg->native_code;
5350 mono_arch_emit_epilog (cfg);
5352 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5353 switch (patch_info->type) {
5354 case MONO_PATCH_INFO_ABS: {
5355 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
5357 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
5358 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
5359 patch_info->data.name = info->name;
5363 case MONO_PATCH_INFO_SWITCH: {
5364 gpointer *table = g_new (gpointer, patch_info->table_size);
5365 patch_info->ip.i = patch_info->ip.label->inst_c0;
5366 for (i = 0; i < patch_info->table_size; i++) {
5367 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
5369 patch_info->data.target = table;
5378 if (cfg->verbose_level > 1)
5379 g_print ("Method %s::%s emmitted at %p to %p\n", cfg->method->klass->name,
5380 cfg->method->name, cfg->native_code, cfg->native_code + cfg->code_len);
5382 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info);
5384 mono_debug_close_method (cfg);
5388 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
5393 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
5394 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
5396 if (cp->opcode == OP_ICONST) {
5397 if (cfg->opt & MONO_OPT_CONSPROP) {
5398 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
5402 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
5403 if (cfg->opt & MONO_OPT_COPYPROP) {
5404 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
5410 arity = mono_burg_arity [tree->opcode];
5413 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
5414 if (cfg->opt & MONO_OPT_CFOLD)
5415 mono_constant_fold_inst (tree, NULL);
5417 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
5418 if (cfg->opt & MONO_OPT_CFOLD)
5419 mono_constant_fold_inst (tree, NULL);
5421 mono_constant_fold_inst (tree, NULL);
5427 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
5431 switch (tree->opcode) {
5441 if (tree->ssa_op == MONO_SSA_NOP) {
5442 memset (acp, 0, sizeof (MonoInst *) * acp_size);
5459 case OP_VOIDCALL_REG:
5460 case OP_VOIDCALLVIRT:
5462 MonoCallInst *call = (MonoCallInst *)tree;
5463 MonoMethodSignature *sig = call->signature;
5464 int i, byref = FALSE;
5466 for (i = 0; i < sig->param_count; i++) {
5467 if (sig->params [i]->byref) {
5474 memset (acp, 0, sizeof (MonoInst *) * acp_size);
5482 arity = mono_burg_arity [tree->opcode];
5488 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
5491 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
5492 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
5495 g_assert_not_reached ();
5500 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
5502 MonoInst *tree = bb->code;
5508 for (; tree; tree = tree->next) {
5510 mono_cprop_copy_values (cfg, tree, acp);
5512 mono_cprop_invalidate_values (tree, acp, acp_size);
5514 if (tree->ssa_op == MONO_SSA_STORE &&
5515 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
5516 MonoInst *i1 = tree->inst_i1;
5518 acp [tree->inst_i0->inst_c0] = NULL;
5520 for (i = 0; i < acp_size; i++) {
5521 if (acp [i] && acp [i]->opcode != OP_ICONST &&
5522 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
5527 if (i1->opcode == OP_ICONST) {
5528 acp [tree->inst_i0->inst_c0] = i1;
5529 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
5531 if (i1->ssa_op == MONO_SSA_LOAD &&
5532 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
5533 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
5534 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
5535 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
5540 if (tree->opcode == CEE_BEQ) {
5541 g_assert (tree->inst_i0->opcode == OP_COMPARE);
5542 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
5543 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
5545 tree->opcode = CEE_BR;
5546 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
5547 tree->inst_target_bb = tree->inst_true_bb;
5549 tree->inst_target_bb = tree->inst_false_bb;
5558 mono_local_cprop (MonoCompile *cfg)
5563 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
5565 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5566 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
5567 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
5572 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, int parts)
5574 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
5575 guint8 *ip = (guint8 *)header->code;
5578 int dfn = 0, i, code_size_ratio;
5580 mono_jit_stats.methods_compiled++;
5581 if (mono_jit_profile)
5582 mono_profiler_method_jit (method);
5584 cfg = g_new0 (MonoCompile, 1);
5585 cfg->method = method;
5586 cfg->mempool = mono_mempool_new ();
5588 cfg->bb_hash = g_hash_table_new (g_direct_hash, NULL);
5589 cfg->domain = domain;
5590 cfg->verbose_level = mini_verbose;
5593 * create MonoInst* which represents arguments and local variables
5595 mono_compile_create_vars (cfg);
5597 if (cfg->verbose_level > 2)
5598 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
5600 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0)) < 0) {
5601 mono_destroy_compile (cfg);
5602 if (mono_jit_profile)
5603 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
5607 mono_jit_stats.basic_blocks += cfg->num_bblocks;
5608 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
5610 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
5612 /* Depth-first ordering on basic blocks */
5613 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
5615 if (cfg->opt & MONO_OPT_BRANCH)
5616 optimize_branches (cfg);
5618 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
5619 if (cfg->num_bblocks != dfn + 1) {
5620 if (cfg->verbose_level > 1)
5621 g_print ("unreachable code?\n");
5622 cfg->num_bblocks = dfn + 1;
5625 if (cfg->opt & MONO_OPT_LOOP) {
5626 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
5627 mono_compute_natural_loops (cfg);
5631 /* after method_to_ir */
5635 //#define DEBUGSSA "logic_run"
5636 #define DEBUGSSA_CLASS "Tests"
5640 if (!header->num_clauses && !cfg->disable_ssa) {
5641 mono_local_cprop (cfg);
5642 mono_ssa_compute (cfg);
5646 /* fixme: add all optimizations which requires SSA */
5647 if (cfg->opt & (MONO_OPT_DEADCE)) {
5648 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
5649 mono_local_cprop (cfg);
5650 mono_ssa_compute (cfg);
5652 if (cfg->verbose_level >= 2) {
5659 /* after SSA translation */
5663 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
5664 if (cfg->comp_done & MONO_COMP_SSA) {
5665 mono_ssa_cprop (cfg);
5667 mono_local_cprop (cfg);
5671 if (cfg->comp_done & MONO_COMP_SSA) {
5672 mono_ssa_deadce (cfg);
5674 //mono_ssa_strength_reduction (cfg);
5676 mono_ssa_remove (cfg);
5678 if (cfg->opt & MONO_OPT_BRANCH)
5679 optimize_branches (cfg);
5682 /* after SSA removal */
5686 decompose_pass (cfg);
5688 if (cfg->opt & MONO_OPT_LINEARS) {
5691 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
5692 cfg->comp_done &= ~MONO_COMP_LIVENESS;
5693 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
5694 mono_analyze_liveness (cfg);
5696 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
5697 regs = mono_arch_get_global_int_regs (cfg);
5698 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
5702 //mono_print_code (cfg);
5706 /* variables are allocated after decompose, since decompose could create temps */
5707 mono_arch_allocate_vars (cfg);
5709 if (cfg->opt & MONO_OPT_CFOLD)
5710 mono_constant_fold (cfg);
5712 mini_select_instructions (cfg);
5715 if (cfg->verbose_level >= 2) {
5716 char *id = mono_method_full_name (cfg->method, FALSE);
5717 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
5721 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
5723 jinfo = g_new0 (MonoJitInfo, 1);
5724 jinfo->method = method;
5725 jinfo->code_start = cfg->native_code;
5726 jinfo->code_size = cfg->code_len;
5727 jinfo->used_regs = cfg->used_int_regs;
5729 if (header->num_clauses) {
5732 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
5733 jinfo->num_clauses = header->num_clauses;
5734 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
5735 sizeof (MonoJitExceptionInfo) * header->num_clauses);
5737 for (i = 0; i < header->num_clauses; i++) {
5738 MonoExceptionClause *ec = &header->clauses [i];
5739 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
5740 MonoBasicBlock *tblock;
5742 ei->flags = ec->flags;
5744 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
5745 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->token_or_filter);
5747 ei->data.filter = cfg->native_code + tblock->native_offset;
5749 ei->data.token = ec->token_or_filter;
5752 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
5754 ei->try_start = cfg->native_code + tblock->native_offset;
5755 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
5757 ei->try_end = cfg->native_code + tblock->native_offset;
5758 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
5760 ei->handler_start = cfg->native_code + tblock->native_offset;
5765 mono_jit_info_table_add (cfg->domain, jinfo);
5767 /* collect statistics */
5768 mono_jit_stats.allocated_code_size += cfg->code_len;
5769 code_size_ratio = cfg->code_len;
5770 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
5771 mono_jit_stats.biggest_method_size = code_size_ratio;
5772 mono_jit_stats.biggest_method = method;
5774 code_size_ratio = (code_size_ratio * 100) / ((MonoMethodNormal *)method)->header->code_size;
5775 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
5776 mono_jit_stats.max_code_size_ratio = code_size_ratio;
5777 mono_jit_stats.max_ratio_method = method;
5779 mono_jit_stats.native_code_size += cfg->code_len;
5781 if (mono_jit_profile)
5782 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
5788 mono_jit_compile_method (MonoMethod *method)
5790 /* FIXME: later copy the code from mono */
5791 MonoDomain *target_domain, *domain = mono_domain_get ();
5793 GHashTable *jit_code_hash;
5796 if (default_opt & MONO_OPT_SAHRED)
5797 target_domain = mono_root_domain;
5799 target_domain = domain;
5801 jit_code_hash = target_domain->jit_code_hash;
5803 if ((code = g_hash_table_lookup (jit_code_hash, method))) {
5804 mono_jit_stats.methods_lookups++;
5808 #ifdef MONO_USE_AOT_COMPILER
5809 if (!mono_compile_aot) {
5810 mono_class_init (method->klass);
5811 if ((code = mono_aot_get_method (method))) {
5812 g_hash_table_insert (jit_code_hash, method, code);
5818 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
5819 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
5820 if (!method->info) {
5823 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
5824 mono_lookup_pinvoke_call (method);
5825 #ifdef MONO_USE_EXC_TABLES
5826 if (mono_method_blittable (method)) {
5827 method->info = method->addr;
5830 nm = mono_marshal_get_native_wrapper (method);
5831 method->info = mono_compile_method (nm);
5833 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
5834 //mono_debug_add_wrapper (method, nm);
5835 #ifdef MONO_USE_EXC_TABLES
5839 return method->info;
5840 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
5841 const char *name = method->name;
5844 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
5845 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
5846 /* FIXME: uhm, we need a wrapper to handle exceptions? */
5847 return (gpointer)mono_delegate_ctor;
5848 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
5849 nm = mono_marshal_get_delegate_invoke (method);
5850 return mono_jit_compile_method (nm);
5851 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
5852 nm = mono_marshal_get_delegate_begin_invoke (method);
5853 return mono_jit_compile_method (nm);
5854 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
5855 nm = mono_marshal_get_delegate_end_invoke (method);
5856 return mono_jit_compile_method (nm);
5862 cfg = mini_method_compile (method, default_opt, target_domain, 0);
5863 code = cfg->native_code;
5864 mono_destroy_compile (cfg);
5866 g_hash_table_insert (jit_code_hash, method, code);
5868 /* make sure runtime_init is called */
5869 mono_class_vtable (target_domain, method->klass);
5875 * mono_jit_runtime_invoke:
5876 * @method: the method to invoke
5877 * @obj: this pointer
5878 * @params: array of parameter values.
5879 * @exc: used to catch exceptions objects
5882 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
5885 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc);
5886 invoke = mono_marshal_get_runtime_invoke (method);
5887 runtime_invoke = mono_jit_compile_method (invoke);
5888 return runtime_invoke (obj, params, exc);
5891 #ifdef PLATFORM_WIN32
5892 #define GET_CONTEXT \
5893 struct sigcontext *ctx = (struct sigcontext*)_dummy;
5895 #define GET_CONTEXT \
5896 void **_p = (void **)&_dummy; \
5897 struct sigcontext *ctx = (struct sigcontext *)++_p;
5901 sigfpe_signal_handler (int _dummy)
5906 exc = mono_get_exception_divide_by_zero ();
5908 mono_arch_handle_exception (ctx, exc, FALSE);
5912 sigill_signal_handler (int _dummy)
5916 exc = mono_get_exception_execution_engine ("SIGILL");
5918 mono_arch_handle_exception (ctx, exc, FALSE);
5922 sigsegv_signal_handler (int _dummy)
5927 exc = mono_get_exception_null_reference ();
5929 mono_arch_handle_exception (ctx, exc, FALSE);
5933 sigusr1_signal_handler (int _dummy)
5938 thread = mono_thread_current ();
5940 g_assert (thread->abort_exc);
5942 mono_arch_handle_exception (ctx, thread->abort_exc, FALSE);
5946 mono_runtime_install_handlers (void)
5948 #ifndef PLATFORM_WIN32
5949 struct sigaction sa;
5952 #ifdef PLATFORM_WIN32
5954 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
5955 win32_seh_set_handler(SIGILL, sigill_signal_handler);
5956 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
5957 #else /* !PLATFORM_WIN32 */
5959 /* libpthreads has its own implementation of sigaction(),
5960 * but it seems to work well with our current exception
5961 * handlers. If not we must call syscall directly instead
5965 sa.sa_handler = sigfpe_signal_handler;
5966 sigemptyset (&sa.sa_mask);
5968 //g_assert (syscall (SYS_sigaction, SIGFPE, &sa, NULL) != -1);
5969 g_assert (sigaction (SIGFPE, &sa, NULL) != -1);
5972 sa.sa_handler = sigill_signal_handler;
5973 sigemptyset (&sa.sa_mask);
5975 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
5976 g_assert (sigaction (SIGILL, &sa, NULL) != -1);
5978 /* catch thread abort signal */
5979 sa.sa_handler = sigusr1_signal_handler;
5980 sigemptyset (&sa.sa_mask);
5982 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
5983 g_assert (sigaction (mono_thread_get_abort_signal (), &sa, NULL) != -1);
5987 sa.sa_handler = sigsegv_signal_handler;
5988 sigemptyset (&sa.sa_mask);
5990 //g_assert (syscall (SYS_sigaction, SIGSEGV, &sa, NULL) != -1);
5991 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
5993 #endif /* PLATFORM_WIN32 */
5996 /* mono_jit_create_remoting_trampoline:
5997 * @method: pointer to the method info
5999 * Creates a trampoline which calls the remoting functions. This
6000 * is used in the vtable of transparent proxies.
6002 * Returns: a pointer to the newly created code
6005 mono_jit_create_remoting_trampoline (MonoMethod *method)
6008 guint8 *addr = NULL;
6010 if (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class)) {
6011 nm = mono_marshal_get_remoting_invoke (method);
6012 addr = mono_compile_method (nm);
6014 addr = mono_compile_method (method);
6019 static CRITICAL_SECTION ms;
6022 mini_init (const char *filename)
6026 metadata_section = &ms;
6027 InitializeCriticalSection (metadata_section);
6029 mono_jit_tls_id = TlsAlloc ();
6030 mono_thread_start_cb (GetCurrentThreadId (), (gpointer)-1, NULL);
6034 mono_runtime_install_handlers ();
6036 mono_install_compile_method (mono_jit_compile_method);
6037 mono_install_trampoline (mono_arch_create_jit_trampoline);
6038 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
6039 mono_install_runtime_invoke (mono_jit_runtime_invoke);
6040 mono_install_handler (mono_arch_get_throw_exception ());
6041 mono_install_stack_walk (mono_jit_walk_stack);
6042 mono_install_get_config_dir ();
6044 domain = mono_init (filename);
6047 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
6048 ves_icall_get_frame_info);
6049 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
6050 ves_icall_get_trace);
6051 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
6052 mono_runtime_install_handlers);
6055 create_helper_signature ();
6057 mono_arch_register_lowlevel_calls ();
6058 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
6059 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
6061 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
6062 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
6064 /* fixme: we cant hanlde vararg methods this way, because the signature is not constant */
6065 //mono_register_jit_icall (ves_array_element_address, "ves_array_element_address", NULL);
6066 //mono_register_jit_icall (mono_array_new_va, "mono_array_new_va", NULL);
6068 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
6069 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
6070 helper_sig_void_ptr, TRUE);
6073 * NOTE, NOTE, NOTE, NOTE:
6074 * when adding emulation for some opcodes, remember to also add a dummy
6075 * rule to the burg files, because we need the arity information to be correct.
6077 mono_register_opcode_emulation (OP_LMUL, helper_sig_long_long_long, mono_llmult);
6078 mono_register_opcode_emulation (OP_LMUL_OVF_UN, helper_sig_long_long_long, mono_llmult_ovf_un);
6079 mono_register_opcode_emulation (OP_LMUL_OVF, helper_sig_long_long_long, mono_llmult_ovf);
6080 mono_register_opcode_emulation (OP_LDIV, helper_sig_long_long_long, mono_lldiv);
6081 mono_register_opcode_emulation (OP_LDIV_UN, helper_sig_long_long_long, mono_lldiv_un);
6082 mono_register_opcode_emulation (OP_LREM, helper_sig_long_long_long, mono_llrem);
6083 mono_register_opcode_emulation (OP_LREM_UN, helper_sig_long_long_long, mono_llrem_un);
6085 mono_register_opcode_emulation (OP_LSHL, helper_sig_long_long_int, mono_lshl);
6086 mono_register_opcode_emulation (OP_LSHR, helper_sig_long_long_int, mono_lshr);
6087 mono_register_opcode_emulation (OP_LSHR_UN, helper_sig_long_long_int, mono_lshr_un);
6089 mono_register_opcode_emulation (OP_FCONV_TO_U8, helper_sig_ulong_double, mono_fconv_u8);
6090 mono_register_opcode_emulation (OP_FCONV_TO_U4, helper_sig_uint_double, mono_fconv_u4);
6091 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, helper_sig_long_double, mono_fconv_ovf_i8);
6092 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, helper_sig_ulong_double, mono_fconv_ovf_u8);
6094 #if SIZEOF_VOID_P == 4
6095 mono_register_opcode_emulation (OP_FCONV_TO_U, helper_sig_uint_double, mono_fconv_u4);
6097 #warning "fixme: add opcode emulation"
6100 /* other jit icalls */
6101 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
6102 helper_sig_ptr_ptr_ptr, FALSE);
6103 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
6104 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
6105 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
6106 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
6107 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
6108 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
6109 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
6110 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
6111 mono_register_jit_icall (mono_string_to_utf16, "mono_string_to_utf16", helper_sig_ptr_obj, FALSE);
6112 mono_register_jit_icall (mono_string_from_utf16, "mono_string_from_utf16", helper_sig_obj_ptr, FALSE);
6113 mono_register_jit_icall (mono_string_new_wrapper, "mono_string_new_wrapper", helper_sig_obj_ptr, FALSE);
6114 mono_register_jit_icall (mono_string_to_utf8, "mono_string_to_utf8", helper_sig_ptr_obj, FALSE);
6115 mono_register_jit_icall (mono_string_to_bstr, "mono_string_to_bstr", helper_sig_ptr_obj, FALSE);
6116 mono_register_jit_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", helper_sig_ptr_obj, FALSE);
6117 mono_register_jit_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", helper_sig_ptr_obj, FALSE);
6118 mono_register_jit_icall (mono_array_to_savearray, "mono_array_to_savearray", helper_sig_ptr_obj, FALSE);
6119 mono_register_jit_icall (mono_array_to_lparray, "mono_array_to_lparray", helper_sig_ptr_obj, FALSE);
6120 mono_register_jit_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", helper_sig_ptr_obj, FALSE);
6121 mono_register_jit_icall (mono_marshal_string_array, "mono_marshal_string_array", helper_sig_ptr_obj, FALSE);
6122 mono_register_jit_icall (mono_string_utf8_to_builder, "mono_string_utf8_to_builder", helper_sig_void_ptr_ptr, FALSE);
6123 mono_register_jit_icall (mono_marshal_free_array, "mono_marshal_free_array", helper_sig_void_ptr_ptr, FALSE);
6124 mono_register_jit_icall (mono_string_to_byvalstr, "mono_string_to_byvalstr", helper_sig_void_ptr_ptr_ptr, FALSE);
6125 mono_register_jit_icall (mono_string_to_byvalwstr, "mono_string_to_byvalwstr", helper_sig_void_ptr_ptr_ptr, FALSE);
6126 mono_register_jit_icall (g_free, "g_free", helper_sig_void_ptr, FALSE);
6127 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
6128 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
6130 mono_runtime_init (domain, mono_thread_start_cb,
6131 mono_thread_attach_cb);
6133 //mono_thread_attach (domain);
6137 MonoJitStats mono_jit_stats = {0};
6140 print_jit_stats (void)
6142 if (mono_jit_stats.enabled) {
6143 g_print ("Mono Jit statistics\n");
6144 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
6145 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
6146 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
6147 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
6148 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
6149 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
6150 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
6151 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
6152 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
6153 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
6154 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
6155 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
6156 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
6157 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
6158 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
6159 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
6160 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
6161 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
6163 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
6164 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
6165 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
6166 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
6167 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
6172 mini_cleanup (MonoDomain *domain)
6175 * mono_runtime_cleanup() and mono_domain_finalize () need to
6176 * be called early since they need the execution engine still
6177 * fully working (mono_domain_finalize may invoke managed finalizers
6178 * and mono_runtime_cleanup will wait for other threads to finish).
6180 mono_domain_finalize (domain);
6182 mono_runtime_cleanup (domain);
6184 mono_profiler_shutdown ();
6186 mono_debug_cleanup ();
6187 #ifdef PLATFORM_WIN32
6188 win32_seh_cleanup();
6191 mono_domain_unload (domain, TRUE);
6194 DeleteCriticalSection (metadata_section);
6198 mini_set_defaults (int verbose_level, guint32 opts)
6200 mini_verbose = verbose_level;