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;
2064 int breakpoint_id = 0;
2067 image = method->klass->image;
2068 header = ((MonoMethodNormal *)method)->header;
2069 sig = method->signature;
2070 ip = (unsigned char*)header->code;
2071 end = ip + header->code_size;
2072 mono_jit_stats.cil_code_size += header->code_size;
2074 if (cfg->method == method) {
2076 bbhash = cfg->bb_hash;
2078 real_offset = inline_offset;
2079 bbhash = g_hash_table_new (g_direct_hash, NULL);
2082 if (cfg->method == method) {
2085 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
2086 start_bblock->cil_code = NULL;
2087 start_bblock->cil_length = 0;
2088 start_bblock->block_num = cfg->num_bblocks++;
2091 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
2092 end_bblock->cil_code = NULL;
2093 end_bblock->cil_length = 0;
2094 end_bblock->block_num = cfg->num_bblocks++;
2095 g_assert (cfg->num_bblocks == 2);
2097 arg_array = alloca (sizeof (MonoInst *) * (sig->hasthis + sig->param_count));
2098 for (i = sig->hasthis + sig->param_count - 1; i >= 0; i--)
2099 arg_array [i] = cfg->varinfo [i];
2101 if (mono_compile_aot)
2102 cfg->opt |= MONO_OPT_SAHRED;
2104 if (header->num_clauses) {
2105 int size = sizeof (int) * header->num_clauses;
2106 filter_lengths = alloca (size);
2107 memset (filter_lengths, 0, size);
2109 /* handle exception clauses */
2110 for (i = 0; i < header->num_clauses; ++i) {
2111 //unsigned char *p = ip;
2112 MonoExceptionClause *clause = &header->clauses [i];
2113 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->try_offset);
2114 tblock->real_offset = clause->try_offset;
2115 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
2116 tblock->real_offset = clause->handler_offset;
2117 /*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);
2119 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
2121 /* catch and filter blocks get the exception object on the stack */
2122 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
2123 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2124 /* mostly like handle_stack_args (), but just sets the input args */
2125 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
2127 cfg->exvar = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
2128 /* prevent it from being register allocated */
2129 cfg->exvar->flags |= MONO_INST_INDIRECT;
2131 tblock->in_scount = 1;
2132 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2133 tblock->in_stack [0] = cfg->exvar;
2134 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2135 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->token_or_filter);
2136 tblock->real_offset = clause->token_or_filter;
2137 tblock->in_scount = 1;
2138 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2139 tblock->in_stack [0] = cfg->exvar;
2145 arg_array = alloca (sizeof (MonoInst *) * (sig->hasthis + sig->param_count));
2146 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
2149 /* FIRST CODE BLOCK */
2150 bblock = NEW_BBLOCK (cfg);
2151 bblock->cil_code = ip;
2153 ADD_BBLOCK (cfg, bbhash, bblock);
2155 if (cfg->method == method) {
2156 breakpoint_id = mono_debugger_method_has_breakpoint (method);
2157 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
2158 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2159 MONO_ADD_INS (bblock, ins);
2163 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SAHRED)))) {
2164 /* we use a separate basic block for the initialization code */
2165 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
2166 init_localsbb->real_offset = real_offset;
2167 start_bblock->next_bb = init_localsbb;
2168 init_localsbb->next_bb = bblock;
2169 link_bblock (cfg, start_bblock, init_localsbb);
2170 link_bblock (cfg, init_localsbb, bblock);
2171 init_localsbb->block_num = cfg->num_bblocks++;
2173 start_bblock->next_bb = bblock;
2174 link_bblock (cfg, start_bblock, bblock);
2177 mono_debug_init_method (cfg, bblock, breakpoint_id);
2179 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * (sig->hasthis + sig->param_count));
2181 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
2182 for (n = 0; n < sig->param_count; ++n)
2183 param_types [n + sig->hasthis] = sig->params [n];
2185 /* do this somewhere outside - not here */
2186 NEW_ICONST (cfg, zero_int32, 0);
2187 NEW_ICONST (cfg, zero_int64, 0);
2188 zero_int64->type = STACK_I8;
2189 NEW_PCONST (cfg, zero_ptr, 0);
2190 NEW_PCONST (cfg, zero_obj, 0);
2191 zero_obj->type = STACK_OBJ;
2193 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
2194 zero_r8->type = STACK_R8;
2195 zero_r8->inst_p0 = &r8_0;
2197 /* add a check for this != NULL to inlined methods */
2198 if (cfg->method != method && sig->hasthis) {
2199 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
2200 NEW_ARGLOAD (cfg, ins->inst_left, 0);
2202 MONO_ADD_INS (bblock, ins);
2205 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
2206 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
2209 start_new_bblock = 0;
2212 if (cfg->method == method)
2213 real_offset = ip - header->code;
2215 real_offset = inline_offset;
2217 if (start_new_bblock) {
2218 bblock->cil_length = ip - bblock->cil_code;
2219 if (start_new_bblock == 2) {
2220 g_assert (ip == tblock->cil_code);
2222 GET_BBLOCK (cfg, bbhash, tblock, ip);
2224 bblock->next_bb = tblock;
2226 start_new_bblock = 0;
2227 for (i = 0; i < bblock->in_scount; ++i) {
2228 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2232 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
2233 link_bblock (cfg, bblock, tblock);
2234 if (sp != stack_start) {
2235 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2238 bblock->next_bb = tblock;
2240 for (i = 0; i < bblock->in_scount; ++i) {
2241 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2247 if (cfg->verbose_level > 3)
2248 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, sp-stack_start, mono_disasm_code_one (NULL, method, ip, NULL));
2255 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2256 ins->cil_code = ip++;
2257 MONO_ADD_INS (bblock, ins);
2263 CHECK_STACK_OVF (1);
2264 n = (*ip)-CEE_LDARG_0;
2265 NEW_ARGLOAD (cfg, ins, n);
2266 ins->cil_code = ip++;
2273 CHECK_STACK_OVF (1);
2274 n = (*ip)-CEE_LDLOC_0;
2275 NEW_LOCLOAD (cfg, ins, n);
2276 ins->cil_code = ip++;
2284 n = (*ip)-CEE_STLOC_0;
2286 handle_loaded_temps (cfg, bblock, stack_start, sp);
2287 NEW_LOCSTORE (cfg, ins, n, *sp);
2289 if (ins->opcode == CEE_STOBJ) {
2290 NEW_LOCLOADA (cfg, ins, n);
2291 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2293 MONO_ADD_INS (bblock, ins);
2298 CHECK_STACK_OVF (1);
2299 NEW_ARGLOAD (cfg, ins, ip [1]);
2305 CHECK_STACK_OVF (1);
2306 NEW_ARGLOADA (cfg, ins, ip [1]);
2314 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
2315 handle_loaded_temps (cfg, bblock, stack_start, sp);
2317 if (ins->opcode == CEE_STOBJ) {
2318 NEW_ARGLOADA (cfg, ins, ip [1]);
2319 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2321 MONO_ADD_INS (bblock, ins);
2325 CHECK_STACK_OVF (1);
2326 NEW_LOCLOAD (cfg, ins, ip [1]);
2332 CHECK_STACK_OVF (1);
2333 NEW_LOCLOADA (cfg, ins, ip [1]);
2341 handle_loaded_temps (cfg, bblock, stack_start, sp);
2342 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
2344 if (ins->opcode == CEE_STOBJ) {
2345 NEW_LOCLOADA (cfg, ins, ip [1]);
2346 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2348 MONO_ADD_INS (bblock, ins);
2353 CHECK_STACK_OVF (1);
2354 NEW_PCONST (cfg, ins, NULL);
2356 ins->type = STACK_OBJ;
2361 CHECK_STACK_OVF (1);
2362 NEW_ICONST (cfg, ins, -1);
2376 CHECK_STACK_OVF (1);
2377 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
2383 CHECK_STACK_OVF (1);
2385 NEW_ICONST (cfg, ins, *((signed char*)ip));
2391 CHECK_STACK_OVF (1);
2392 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
2398 CHECK_STACK_OVF (1);
2399 MONO_INST_NEW (cfg, ins, OP_I8CONST);
2401 ins->type = STACK_I8;
2403 ins->inst_l = (gint64)read64 (ip);
2408 float *f = g_malloc (sizeof (float));
2409 CHECK_STACK_OVF (1);
2410 MONO_INST_NEW (cfg, ins, OP_R4CONST);
2411 ins->type = STACK_R8;
2420 double *d = g_malloc (sizeof (double));
2421 CHECK_STACK_OVF (1);
2422 MONO_INST_NEW (cfg, ins, OP_R8CONST);
2423 ins->type = STACK_R8;
2432 MonoInst *temp, *store;
2434 CHECK_STACK_OVF (1);
2439 * small optimization: if the loaded value was from a local already,
2440 * just load it twice.
2442 if (ins->ssa_op == MONO_SSA_LOAD &&
2443 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
2445 MONO_INST_NEW (cfg, temp, 0);
2447 temp->cil_code = ip;
2450 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
2451 temp->cil_code = ip;
2452 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2453 store->cil_code = ip;
2454 MONO_ADD_INS (bblock, store);
2455 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
2458 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
2468 MONO_INST_NEW (cfg, ins, CEE_POP);
2469 MONO_ADD_INS (bblock, ins);
2470 ins->cil_code = ip++;
2475 if (stack_start != sp)
2477 MONO_INST_NEW (cfg, ins, CEE_JMP);
2478 token = read32 (ip + 1);
2479 /* FIXME: check the signature matches */
2480 cmethod = mono_get_method (image, token, NULL);
2482 * The current magic trampoline can't handle this
2483 * apparently, so we compile the method right away.
2484 * Later, we may need to fix the trampoline or use a different one.
2486 ins->inst_p0 = mono_compile_method (cmethod);
2487 MONO_ADD_INS (bblock, ins);
2489 start_new_bblock = 1;
2493 case CEE_CALLVIRT: {
2494 MonoInst *addr = NULL;
2495 MonoMethodSignature *fsig = NULL;
2496 MonoMethodHeader *cheader;
2497 int temp, array_rank = 0;
2498 int virtual = *ip == CEE_CALLVIRT;
2500 token = read32 (ip + 1);
2502 if (*ip == CEE_CALLI) {
2508 if (method->wrapper_type != MONO_WRAPPER_NONE)
2509 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
2511 fsig = mono_metadata_parse_signature (image, token);
2513 n = fsig->param_count + fsig->hasthis;
2516 cmethod = mono_get_method (image, token, NULL);
2517 cheader = ((MonoMethodNormal *)cmethod)->header;
2519 if (!cmethod->klass->inited)
2520 mono_class_init (cmethod->klass);
2522 if (cmethod->signature->pinvoke) {
2523 #ifdef MONO_USE_EXC_TABLES
2524 if (mono_method_blittable (cmethod)) {
2525 fsig = cmethod->signature;
2528 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
2529 fsig = wrapper->signature;
2530 #ifdef MONO_USE_EXC_TABLES
2534 fsig = cmethod->signature;
2537 n = fsig->param_count + fsig->hasthis;
2539 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
2540 cmethod->klass->parent == mono_defaults.array_class) {
2541 array_rank = cmethod->klass->rank;
2544 if (cmethod->string_ctor)
2545 g_assert_not_reached ();
2551 //g_assert (!virtual || fsig->hasthis);
2555 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_opcode_for_method (cfg, cmethod, fsig, sp))) {
2558 if (MONO_TYPE_IS_VOID (fsig->ret)) {
2559 MONO_ADD_INS (bblock, ins);
2561 type_to_eval_stack_type (fsig->ret, ins);
2570 if ((cfg->opt & MONO_OPT_INLINE) &&
2571 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
2572 cmethod && cheader && mono_method_check_inlining (cmethod) &&
2573 method != cmethod && !g_list_find (dont_inline, cmethod)) {
2574 MonoInst *rvar = NULL;
2575 MonoBasicBlock *ebblock, *sbblock;
2576 int costs, new_locals_offset;
2578 if (cfg->verbose_level > 2)
2579 g_print ("INLINE START %p %s\n", cmethod, mono_method_full_name (cmethod, TRUE));
2581 if (!cmethod->inline_info) {
2582 mono_jit_stats.inlineable_methods++;
2583 cmethod->inline_info = 1;
2585 /* allocate space to store the return value */
2586 if (!MONO_TYPE_IS_VOID (fsig->ret))
2587 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2589 /* allocate local variables */
2590 new_locals_offset = cfg->num_varinfo;
2591 for (i = 0; i < cheader->num_locals; ++i)
2592 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
2594 /* allocate starte and end blocks */
2595 sbblock = NEW_BBLOCK (cfg);
2596 sbblock->block_num = cfg->num_bblocks++;
2597 sbblock->real_offset = real_offset;
2599 ebblock = NEW_BBLOCK (cfg);
2600 ebblock->block_num = cfg->num_bblocks++;
2601 ebblock->real_offset = real_offset;
2603 dont_inline = g_list_prepend (dont_inline, method);
2604 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset);
2605 dont_inline = g_list_remove (dont_inline, method);
2607 if (costs >= 0 && costs < 60) {
2609 mono_jit_stats.inlined_methods++;
2611 /* always add some code to avoid block split failures */
2612 MONO_INST_NEW (cfg, ins, CEE_NOP);
2613 MONO_ADD_INS (bblock, ins);
2619 bblock->next_bb = sbblock;
2620 link_bblock (cfg, bblock, sbblock);
2622 GET_BBLOCK (cfg, bbhash, bblock, ip);
2623 ebblock->next_bb = bblock;
2624 link_bblock (cfg, ebblock, bblock);
2627 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
2630 if (sp != stack_start) {
2631 handle_stack_args (cfg, ebblock, stack_start, sp - stack_start);
2634 start_new_bblock = 1;
2635 if (cfg->verbose_level > 2)
2636 g_print ("INLINE END %s\n", mono_method_full_name (cmethod, TRUE));
2638 // { static int c = 0; printf ("ICOUNT %d %d %s\n", c++, costs, mono_method_full_name (cmethod, TRUE)); }
2640 inline_costs += costs;
2644 if (cfg->verbose_level > 2)
2645 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
2650 inline_costs += 10 * num_calls++;
2651 handle_loaded_temps (cfg, bblock, stack_start, sp);
2653 /* tail recursion elimination */
2654 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == cfg->method && ip [5] == CEE_RET) {
2655 gboolean has_vtargs = FALSE;
2658 /* keep it simple */
2659 for (i = fsig->param_count - 1; i >= 0; i--) {
2660 if (MONO_TYPE_ISSTRUCT (cmethod->signature->params [i]))
2665 for (i = 0; i < n; ++i) {
2666 NEW_ARGSTORE (cfg, ins, i, sp [i]);
2668 MONO_ADD_INS (bblock, ins);
2670 MONO_INST_NEW (cfg, ins, CEE_BR);
2672 MONO_ADD_INS (bblock, ins);
2673 tblock = start_bblock->out_bb [0];
2674 link_bblock (cfg, bblock, tblock);
2675 ins->inst_target_bb = tblock;
2676 start_new_bblock = 1;
2679 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2680 /* just create a dummy - the value is never used */
2681 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2682 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
2690 if (*ip == CEE_CALLI) {
2692 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
2693 NEW_TEMPLOAD (cfg, *sp, temp);
2697 } else if (array_rank) {
2698 MonoMethodSignature *esig;
2701 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
2702 esig = mono_get_element_address_signature (fsig->param_count - 1);
2704 temp = mono_emit_native_call (cfg, bblock, ves_array_element_address, esig, sp, ip, FALSE);
2705 NEW_TEMPLOAD (cfg, addr, temp);
2706 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
2708 if (ins->opcode == CEE_STOBJ) {
2709 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
2711 MONO_ADD_INS (bblock, ins);
2714 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
2715 esig = mono_get_element_address_signature (fsig->param_count);
2717 temp = mono_emit_native_call (cfg, bblock, ves_array_element_address, esig, sp, ip, FALSE);
2718 NEW_TEMPLOAD (cfg, addr, temp);
2719 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
2723 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
2725 esig = mono_get_element_address_signature (fsig->param_count);
2727 temp = mono_emit_native_call (cfg, bblock, ves_array_element_address, esig, sp, ip, FALSE);
2728 NEW_TEMPLOAD (cfg, *sp, temp);
2731 g_assert_not_reached ();
2735 if (0 && CODE_IS_STLOC (ip + 5) && (!MONO_TYPE_ISSTRUCT (fsig->ret)) && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)) {
2736 /* no need to spill */
2737 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
2740 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, sp, ip, virtual ? sp [0] : NULL)) != -1) {
2741 NEW_TEMPLOAD (cfg, *sp, temp);
2751 if (cfg->method != method) {
2752 /* return from inlined methode */
2757 //g_assert (returnvar != -1);
2758 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
2759 store->cil_code = sp [0]->cil_code;
2760 if (store->opcode == CEE_STOBJ) {
2761 g_assert_not_reached ();
2762 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
2763 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
2765 MONO_ADD_INS (bblock, store);
2769 g_assert (!return_var);
2772 MONO_INST_NEW (cfg, ins, CEE_NOP);
2773 ins->opcode = mono_type_to_stind (method->signature->ret);
2774 if (ins->opcode == CEE_STOBJ) {
2775 NEW_RETLOADA (cfg, ins);
2776 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2778 ins->opcode = OP_SETRET;
2780 ins->inst_i0 = *sp;;
2781 ins->inst_i1 = NULL;
2782 MONO_ADD_INS (bblock, ins);
2786 if (sp != stack_start)
2788 MONO_INST_NEW (cfg, ins, CEE_BR);
2789 ins->cil_code = ip++;
2790 ins->inst_target_bb = end_bblock;
2791 MONO_ADD_INS (bblock, ins);
2792 link_bblock (cfg, bblock, end_bblock);
2793 start_new_bblock = 1;
2796 MONO_INST_NEW (cfg, ins, CEE_BR);
2797 ins->cil_code = ip++;
2798 MONO_ADD_INS (bblock, ins);
2799 target = ip + 1 + (signed char)(*ip);
2801 GET_BBLOCK (cfg, bbhash, tblock, target);
2802 link_bblock (cfg, bblock, tblock);
2803 CHECK_BBLOCK (target, ip, tblock);
2804 ins->inst_target_bb = tblock;
2805 if (sp != stack_start) {
2806 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2809 start_new_bblock = 1;
2815 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
2816 ins->cil_code = ip++;
2817 target = ip + 1 + *(signed char*)ip;
2819 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
2820 if (sp != stack_start) {
2821 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2837 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
2838 ins->cil_code = ip++;
2839 target = ip + 1 + *(signed char*)ip;
2842 if (sp != stack_start) {
2843 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2849 MONO_INST_NEW (cfg, ins, CEE_BR);
2850 ins->cil_code = ip++;
2851 MONO_ADD_INS (bblock, ins);
2852 target = ip + 4 + (gint32)read32(ip);
2854 GET_BBLOCK (cfg, bbhash, tblock, target);
2855 link_bblock (cfg, bblock, tblock);
2856 CHECK_BBLOCK (target, ip, tblock);
2857 ins->inst_target_bb = tblock;
2858 if (sp != stack_start) {
2859 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2862 start_new_bblock = 1;
2868 MONO_INST_NEW (cfg, ins, *ip);
2869 ins->cil_code = ip++;
2870 target = ip + 4 + (gint32)read32(ip);
2872 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
2873 if (sp != stack_start) {
2874 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2890 MONO_INST_NEW (cfg, ins, *ip);
2891 ins->cil_code = ip++;
2892 target = ip + 4 + (gint32)read32(ip);
2895 if (sp != stack_start) {
2896 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2903 n = read32 (ip + 1);
2904 MONO_INST_NEW (cfg, ins, *ip);
2906 ins->inst_left = *sp;
2907 if (ins->inst_left->type != STACK_I4) goto unverified;
2910 target = ip + n * sizeof (guint32);
2911 MONO_ADD_INS (bblock, ins);
2912 GET_BBLOCK (cfg, bbhash, tblock, target);
2913 link_bblock (cfg, bblock, tblock);
2914 ins->klass = GUINT_TO_POINTER (n);
2915 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
2916 ins->inst_many_bb [n] = tblock;
2918 for (i = 0; i < n; ++i) {
2919 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
2920 link_bblock (cfg, bblock, tblock);
2921 ins->inst_many_bb [i] = tblock;
2924 /* FIXME: handle stack args */
2939 MONO_INST_NEW (cfg, ins, *ip);
2944 ins->type = ldind_type [*ip - CEE_LDIND_I1];
2945 ins->flags |= ins_flag;
2957 MONO_INST_NEW (cfg, ins, *ip);
2958 ins->cil_code = ip++;
2960 handle_loaded_temps (cfg, bblock, stack_start, sp);
2961 MONO_ADD_INS (bblock, ins);
2962 ins->inst_i0 = sp [0];
2963 ins->inst_i1 = sp [1];
2964 ins->flags |= ins_flag;
2995 case CEE_CONV_OVF_I8:
2996 case CEE_CONV_OVF_U8:
3002 case CEE_CONV_OVF_I4:
3003 case CEE_CONV_OVF_I1:
3004 case CEE_CONV_OVF_I2:
3005 case CEE_CONV_OVF_I:
3006 case CEE_CONV_OVF_U:
3009 if (sp [-1]->type == STACK_R8) {
3010 ADD_UNOP (CEE_CONV_OVF_I8);
3018 case CEE_CONV_OVF_U1:
3019 case CEE_CONV_OVF_U2:
3020 case CEE_CONV_OVF_U4:
3023 if (sp [-1]->type == STACK_R8) {
3024 ADD_UNOP (CEE_CONV_OVF_U8);
3032 case CEE_CONV_OVF_I1_UN:
3033 case CEE_CONV_OVF_I2_UN:
3034 case CEE_CONV_OVF_I4_UN:
3035 case CEE_CONV_OVF_I8_UN:
3036 case CEE_CONV_OVF_U1_UN:
3037 case CEE_CONV_OVF_U2_UN:
3038 case CEE_CONV_OVF_U4_UN:
3039 case CEE_CONV_OVF_U8_UN:
3040 case CEE_CONV_OVF_I_UN:
3041 case CEE_CONV_OVF_U_UN:
3047 g_error ("opcode 0x%02x not handled", *ip);
3050 MonoInst *iargs [3];
3053 token = read32 (ip + 1);
3054 if (method->wrapper_type != MONO_WRAPPER_NONE)
3055 klass = mono_method_get_wrapper_data (method, token);
3057 klass = mono_class_get (image, token);
3059 mono_class_init (klass);
3060 n = mono_class_value_size (klass, NULL);
3061 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3062 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3064 NEW_ICONST (cfg, iargs [2], n);
3065 iargs [2]->cil_code = ip;
3067 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3068 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3075 CHECK_STACK_OVF (1);
3076 n = read32 (ip + 1);
3078 if (mono_compile_aot) {
3079 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n);
3082 if ((cfg->opt & MONO_OPT_SAHRED) || mono_compile_aot) {
3084 MonoInst *iargs [3];
3085 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3086 NEW_IMAGECONST (cfg, iargs [1], image);
3087 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
3088 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
3089 NEW_TEMPLOAD (cfg, *sp, temp);
3090 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3092 NEW_PCONST (cfg, ins, NULL);
3094 ins->type = STACK_OBJ;
3095 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3102 MonoInst *iargs [2];
3105 token = read32 (ip + 1);
3106 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3107 cmethod = mono_method_get_wrapper_data (method, token);
3109 cmethod = mono_get_method (image, token, NULL);
3111 mono_class_init (cmethod->klass);
3113 n = cmethod->signature->param_count;
3116 /* move the args to allow room for 'this' in the first position */
3122 handle_loaded_temps (cfg, bblock, stack_start, sp);
3125 if (cmethod->klass->parent == mono_defaults.array_class) {
3126 NEW_METHODCONST (cfg, *sp, cmethod);
3127 temp = mono_emit_native_call (cfg, bblock, mono_array_new_va, cmethod->signature, sp, ip, FALSE);
3129 } else if (cmethod->string_ctor) {
3130 /* we simply pass a null pointer */
3131 NEW_PCONST (cfg, *sp, NULL);
3132 /* now call the string ctor */
3133 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, sp, ip, NULL);
3135 if (cmethod->klass->valuetype) {
3136 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
3137 temp = iargs [0]->inst_c0;
3138 NEW_TEMPLOADA (cfg, *sp, temp);
3140 NEW_DOMAINCONST (cfg, iargs [0]);
3141 NEW_CLASSCONST (cfg, iargs [1], cmethod->klass);
3143 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3144 NEW_TEMPLOAD (cfg, *sp, temp);
3147 /* now call the actual ctor */
3148 mono_emit_method_call_spilled (cfg, bblock, cmethod, sp, ip, NULL);
3151 NEW_TEMPLOAD (cfg, *sp, temp);
3160 MONO_INST_NEW (cfg, ins, *ip);
3162 klass = mono_class_get (image, read32 (ip + 1));
3163 mono_class_init (klass);
3164 ins->type = STACK_OBJ;
3165 ins->inst_left = *sp;
3166 ins->inst_newa_class = klass;
3172 MonoInst *add, *vtoffset;
3173 /* FIXME: need to check class: move to inssel.brg? */
3176 token = read32 (ip + 1);
3177 if (method->wrapper_type != MONO_WRAPPER_NONE)
3178 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3180 klass = mono_class_get (image, token);
3181 mono_class_init (klass);
3182 MONO_INST_NEW (cfg, add, CEE_ADD);
3183 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3184 add->inst_left = *sp;
3185 add->inst_right = vtoffset;
3186 add->type = STACK_MP;
3194 MONO_INST_NEW (cfg, ins, *ip);
3196 klass = mono_class_get (image, read32 (ip + 1));
3197 mono_class_init (klass);
3198 ins->type = STACK_OBJ;
3199 ins->inst_left = *sp;
3201 ins->inst_newa_class = klass;
3208 MONO_INST_NEW (cfg, ins, *ip);
3210 ins->inst_left = *sp;
3211 ins->cil_code = ip++;
3212 MONO_ADD_INS (bblock, ins);
3214 start_new_bblock = 1;
3219 MonoInst *offset_ins;
3220 MonoClassField *field;
3223 if (*ip == CEE_STFLD) {
3230 // FIXME: enable this test later.
3231 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
3233 token = read32 (ip + 1);
3234 field = mono_field_from_token (image, token, &klass);
3235 mono_class_init (klass);
3236 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
3237 /* FIXME: mark instructions for use in SSA */
3238 if (*ip == CEE_STFLD) {
3239 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
3240 /* fixme: we need to inline that call somehow */
3241 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
3242 MonoInst *iargs [5];
3244 NEW_CLASSCONST (cfg, iargs [1], klass);
3245 NEW_FIELDCONST (cfg, iargs [2], field);
3246 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
3248 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, iargs, ip, NULL);
3251 NEW_ICONST (cfg, offset_ins, foffset);
3252 MONO_INST_NEW (cfg, ins, CEE_ADD);
3254 ins->inst_left = *sp;
3255 ins->inst_right = offset_ins;
3256 ins->type = STACK_MP;
3258 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
3259 store->cil_code = ip;
3260 store->inst_left = ins;
3261 store->inst_right = sp [1];
3262 handle_loaded_temps (cfg, bblock, stack_start, sp);
3263 store->flags |= ins_flag;
3265 if (store->opcode == CEE_STOBJ) {
3266 handle_stobj (cfg, bblock, ins, sp [1], ip,
3267 mono_class_from_mono_type (field->type), FALSE, FALSE);
3269 MONO_ADD_INS (bblock, store);
3272 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
3273 /* fixme: we need to inline that call somehow */
3274 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
3275 MonoInst *iargs [4];
3278 NEW_CLASSCONST (cfg, iargs [1], klass);
3279 NEW_FIELDCONST (cfg, iargs [2], field);
3280 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
3281 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, iargs, ip, NULL);
3282 if (*ip == CEE_LDFLDA) {
3283 /* not sure howto handle this */
3284 NEW_TEMPLOADA (cfg, *sp, temp);
3286 NEW_TEMPLOAD (cfg, *sp, temp);
3290 NEW_ICONST (cfg, offset_ins, foffset);
3291 MONO_INST_NEW (cfg, ins, CEE_ADD);
3293 ins->inst_left = *sp;
3294 ins->inst_right = offset_ins;
3295 ins->type = STACK_MP;
3297 if (*ip == CEE_LDFLDA) {
3301 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
3302 type_to_eval_stack_type (field->type, load);
3303 load->cil_code = ip;
3304 load->inst_left = ins;
3305 load->flags |= ins_flag;
3317 MonoClassField *field;
3320 token = read32 (ip + 1);
3322 field = mono_field_from_token (image, token, &klass);
3323 mono_class_init (klass);
3325 handle_loaded_temps (cfg, bblock, stack_start, sp);
3327 if (((cfg->opt & MONO_OPT_SAHRED) || mono_compile_aot)) {
3329 MonoInst *iargs [2];
3330 g_assert (field->parent);
3331 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3332 NEW_FIELDCONST (cfg, iargs [1], field);
3333 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
3334 NEW_TEMPLOAD (cfg, ins, temp);
3336 vtable = mono_class_vtable (cfg->domain, klass);
3337 NEW_PCONST (cfg, ins, (char*)vtable->data + field->offset);
3341 /* FIXME: mark instructions for use in SSA */
3342 if (*ip == CEE_LDSFLDA) {
3344 } else if (*ip == CEE_STSFLD) {
3348 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
3349 store->cil_code = ip;
3350 store->inst_left = ins;
3351 store->inst_right = sp [0];
3352 store->flags |= ins_flag;
3355 if (store->opcode == CEE_STOBJ) {
3356 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
3358 MONO_ADD_INS (bblock, store);
3361 CHECK_STACK_OVF (1);
3362 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
3363 type_to_eval_stack_type (field->type, load);
3364 load->cil_code = ip;
3365 load->inst_left = ins;
3367 load->flags |= ins_flag;
3369 /* fixme: dont see the problem why this does not work */
3370 //cfg->disable_aot = TRUE;
3378 token = read32 (ip + 1);
3379 if (method->wrapper_type != MONO_WRAPPER_NONE)
3380 klass = mono_method_get_wrapper_data (method, token);
3382 klass = mono_class_get (image, token);
3383 mono_class_init (klass);
3384 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
3389 MonoInst *iargs [2];
3390 MonoInst *load, *vtoffset, *add, *val, *vstore;
3395 token = read32 (ip + 1);
3396 if (method->wrapper_type != MONO_WRAPPER_NONE)
3397 klass = mono_method_get_wrapper_data (method, token);
3399 klass = mono_class_get (image, token);
3400 mono_class_init (klass);
3402 /* much like NEWOBJ */
3403 NEW_DOMAINCONST (cfg, iargs [0]);
3404 NEW_CLASSCONST (cfg, iargs [1], klass);
3406 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3407 NEW_TEMPLOAD (cfg, load, temp);
3408 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3409 MONO_INST_NEW (cfg, add, CEE_ADD);
3410 add->inst_left = load;
3411 add->inst_right = vtoffset;
3414 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
3415 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
3416 vstore->cil_code = ip;
3417 vstore->inst_left = add;
3418 vstore->inst_right = val;
3420 if (vstore->opcode == CEE_STOBJ) {
3421 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
3423 MONO_ADD_INS (bblock, vstore);
3425 NEW_TEMPLOAD (cfg, load, temp);
3433 MONO_INST_NEW (cfg, ins, *ip);
3437 token = read32 (ip + 1);
3439 /* allocate the domainvar - becaus this is used in decompose_foreach */
3440 if ((cfg->opt & MONO_OPT_SAHRED) || mono_compile_aot)
3441 mono_get_domainvar (cfg);
3443 if (method->wrapper_type != MONO_WRAPPER_NONE)
3444 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3446 klass = mono_class_get (image, token);
3448 mono_class_init (klass);
3449 ins->inst_newa_class = klass;
3450 ins->inst_newa_len = *sp;
3451 ins->type = STACK_OBJ;
3458 MONO_INST_NEW (cfg, ins, *ip);
3459 ins->cil_code = ip++;
3461 ins->inst_left = *sp;
3462 ins->type = STACK_PTR;
3468 klass = mono_class_get (image, read32 (ip + 1));
3469 mono_class_init (klass);
3470 NEW_LDELEMA (cfg, ins, sp, klass);
3485 case CEE_LDELEM_REF: {
3489 * ldind.x (ldelema (array, index))
3490 * ldelema does the bounds check
3494 klass = array_access_to_klass (*ip);
3495 NEW_LDELEMA (cfg, load, sp, klass);
3496 load->cil_code = ip;
3497 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
3499 ins->inst_left = load;
3501 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
3511 case CEE_STELEM_R8: {
3515 * stind.x (ldelema (array, index), val)
3516 * ldelema does the bounds check
3520 klass = array_access_to_klass (*ip);
3521 NEW_LDELEMA (cfg, load, sp, klass);
3522 load->cil_code = ip;
3523 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
3525 ins->inst_left = load;
3526 ins->inst_right = sp [2];
3528 handle_loaded_temps (cfg, bblock, stack_start, sp);
3529 MONO_ADD_INS (bblock, ins);
3530 /* FIXME: add the implicit STELEM_REF castclass */
3532 cfg->disable_ssa = TRUE;
3535 case CEE_STELEM_REF: {
3536 MonoInst *iargs [3];
3541 handle_loaded_temps (cfg, bblock, stack_start, sp);
3547 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
3551 NEW_GROUP (cfg, group, sp [0], sp [1]);
3552 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
3554 ins->inst_left = group;
3555 ins->inst_right = sp [2];
3556 MONO_ADD_INS (bblock, ins);
3561 cfg->disable_ssa = TRUE;
3564 case CEE_CKFINITE: {
3565 MonoInst *store, *temp;
3568 /* this instr. can throw exceptions as side effect,
3569 * so we cant eliminate dead code which contains CKFINITE opdodes.
3570 * Spilling to memory makes sure that we always perform
3574 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
3576 ins->inst_left = sp [-1];
3577 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
3579 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3580 store->cil_code = ip;
3581 MONO_ADD_INS (bblock, store);
3583 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
3590 g_error ("opcode 0x%02x not handled", *ip);
3594 MonoClass *handle_class;
3596 CHECK_STACK_OVF (1);
3598 n = read32 (ip + 1);
3600 handle = mono_ldtoken (image, n, &handle_class);
3601 mono_class_init (handle_class);
3603 if (((cfg->opt & MONO_OPT_SAHRED) || mono_compile_aot)) {
3605 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
3607 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
3609 NEW_IMAGECONST (cfg, iargs [0], image);
3610 NEW_ICONST (cfg, iargs [1], n);
3611 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
3612 NEW_TEMPLOAD (cfg, res, temp);
3613 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
3614 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
3615 MONO_ADD_INS (bblock, store);
3616 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
3618 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method (image, read32 (ip + 6), NULL)) &&
3619 (cmethod->klass == mono_defaults.monotype_class->parent) &&
3620 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
3621 MonoClass *tclass = mono_class_from_mono_type (handle);
3622 mono_class_init (tclass);
3623 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
3624 ins->type = STACK_OBJ;
3625 ins->klass = cmethod->klass;
3628 NEW_PCONST (cfg, ins, handle);
3629 ins->type = STACK_VTYPE;
3630 ins->klass = handle_class;
3646 case CEE_ADD_OVF_UN:
3648 case CEE_MUL_OVF_UN:
3650 case CEE_SUB_OVF_UN:
3655 case CEE_ENDFINALLY:
3656 /* FIXME: check stack state */
3657 MONO_INST_NEW (cfg, ins, *ip);
3658 MONO_ADD_INS (bblock, ins);
3659 ins->cil_code = ip++;
3660 start_new_bblock = 1;
3664 if (*ip == CEE_LEAVE) {
3665 target = ip + 5 + (gint32)read32(ip + 1);
3667 target = ip + 2 + (signed char)(ip [1]);
3670 /* empty the stack */
3671 while (sp != stack_start) {
3672 MONO_INST_NEW (cfg, ins, CEE_POP);
3676 MONO_ADD_INS (bblock, ins);
3679 /* fixme: call fault handler ? */
3681 if ((tblock = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
3682 link_bblock (cfg, bblock, tblock);
3683 MONO_INST_NEW (cfg, ins, OP_HANDLER);
3685 ins->inst_target_bb = tblock;
3686 MONO_ADD_INS (bblock, ins);
3690 MONO_INST_NEW (cfg, ins, CEE_BR);
3692 MONO_ADD_INS (bblock, ins);
3693 GET_BBLOCK (cfg, bbhash, tblock, target);
3694 link_bblock (cfg, bblock, tblock);
3695 CHECK_BBLOCK (target, ip, tblock);
3696 ins->inst_target_bb = tblock;
3697 start_new_bblock = 1;
3699 if (*ip == CEE_LEAVE)
3708 MONO_INST_NEW (cfg, ins, *ip);
3710 handle_loaded_temps (cfg, bblock, stack_start, sp);
3711 MONO_ADD_INS (bblock, ins);
3712 ins->cil_code = ip++;
3713 ins->inst_i0 = sp [0];
3714 ins->inst_i1 = sp [1];
3722 /* trampoline mono specific opcodes */
3723 case MONO_CUSTOM_PREFIX: {
3725 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
3729 case CEE_MONO_FUNC1: {
3731 gpointer func = NULL;
3736 case MONO_MARSHAL_CONV_STR_LPWSTR:
3737 func = mono_string_to_utf16;
3739 case MONO_MARSHAL_CONV_LPWSTR_STR:
3740 func = mono_string_from_utf16;
3742 case MONO_MARSHAL_CONV_LPSTR_STR:
3743 func = mono_string_new_wrapper;
3745 case MONO_MARSHAL_CONV_STR_LPTSTR:
3746 case MONO_MARSHAL_CONV_STR_LPSTR:
3747 func = mono_string_to_utf8;
3749 case MONO_MARSHAL_CONV_STR_BSTR:
3750 func = mono_string_to_bstr;
3752 case MONO_MARSHAL_CONV_STR_TBSTR:
3753 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
3754 func = mono_string_to_ansibstr;
3756 case MONO_MARSHAL_CONV_SB_LPSTR:
3757 func = mono_string_builder_to_utf8;
3759 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
3760 func = mono_array_to_savearray;
3762 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
3763 func = mono_array_to_lparray;
3765 case MONO_MARSHAL_CONV_DEL_FTN:
3766 func = mono_delegate_to_ftnptr;
3768 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
3769 func = mono_marshal_string_array;
3772 g_warning ("unknown conversion %d\n", ip [2]);
3773 g_assert_not_reached ();
3776 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
3777 NEW_TEMPLOAD (cfg, *sp, temp);
3781 inline_costs += 10 * num_calls++;
3784 case CEE_MONO_PROC2: {
3785 gpointer func = NULL;
3790 case MONO_MARSHAL_CONV_LPSTR_SB:
3791 func = mono_string_utf8_to_builder;
3793 case MONO_MARSHAL_FREE_ARRAY:
3794 func = mono_marshal_free_array;
3797 g_assert_not_reached ();
3800 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
3802 inline_costs += 10 * num_calls++;
3805 case CEE_MONO_PROC3: {
3806 gpointer func = NULL;
3811 case MONO_MARSHAL_CONV_STR_BYVALSTR:
3812 func = mono_string_to_byvalstr;
3814 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
3815 func = mono_string_to_byvalwstr;
3818 g_assert_not_reached ();
3821 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
3823 inline_costs += 10 * num_calls++;
3829 mono_emit_jit_icall (cfg, bblock, g_free, sp, ip);
3831 inline_costs += 10 * num_calls++;
3833 case CEE_MONO_LDPTR:
3834 CHECK_STACK_OVF (1);
3835 token = read32 (ip + 2);
3836 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
3840 inline_costs += 10 * num_calls++;
3842 case CEE_MONO_VTADDR:
3845 MONO_INST_NEW (cfg, ins, OP_VTADDR);
3847 ins->type = STACK_MP;
3848 ins->inst_left = *sp;
3852 case CEE_MONO_NEWOBJ: {
3853 MonoInst *iargs [2];
3855 CHECK_STACK_OVF (1);
3856 token = read32 (ip + 2);
3857 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3858 mono_class_init (klass);
3859 NEW_DOMAINCONST (cfg, iargs [0]);
3860 NEW_CLASSCONST (cfg, iargs [1], klass);
3861 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3862 NEW_TEMPLOAD (cfg, *sp, temp);
3865 inline_costs += 10 * num_calls++;
3868 case CEE_MONO_OBJADDR:
3871 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
3873 ins->type = STACK_MP;
3874 ins->inst_left = *sp;
3878 case CEE_MONO_LDNATIVEOBJ:
3880 token = read32 (ip + 2);
3881 klass = mono_method_get_wrapper_data (method, token);
3882 g_assert (klass->valuetype);
3883 mono_class_init (klass);
3884 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
3888 case CEE_MONO_RETOBJ:
3889 g_assert (cfg->ret);
3890 g_assert (method->signature->pinvoke);
3894 token = read32 (ip + 2);
3895 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3897 NEW_RETLOADA (cfg, ins);
3898 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
3900 if (sp != stack_start)
3903 MONO_INST_NEW (cfg, ins, CEE_BR);
3905 ins->inst_target_bb = end_bblock;
3906 MONO_ADD_INS (bblock, ins);
3907 link_bblock (cfg, bblock, end_bblock);
3908 start_new_bblock = 1;
3912 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
3920 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
3929 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
3930 MONO_INST_NEW (cfg, ins, cmp->opcode);
3932 cmp->inst_i0 = sp [0];
3933 cmp->inst_i1 = sp [1];
3937 cmp->opcode = OP_COMPARE;
3939 ins->type = STACK_I4;
3949 CHECK_STACK_OVF (1);
3950 n = read32 (ip + 2);
3951 if (method->wrapper_type != MONO_WRAPPER_NONE)
3952 cmethod = mono_method_get_wrapper_data (method, n);
3954 cmethod = mono_get_method (image, n, NULL);
3957 * We can't do this in mono_ldftn, since it is used in
3958 * the synchronized wrapper, leading to an infinite loop.
3960 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
3961 cmethod = mono_marshal_get_synchronized_wrapper (cmethod);
3964 mono_class_init (cmethod->klass);
3965 handle_loaded_temps (cfg, bblock, stack_start, sp);
3967 NEW_METHODCONST (cfg, argconst, cmethod);
3968 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
3969 NEW_TEMPLOAD (cfg, *sp, temp);
3973 inline_costs += 10 * num_calls++;
3976 case CEE_LDVIRTFTN: {
3981 n = read32 (ip + 2);
3982 if (method->wrapper_type != MONO_WRAPPER_NONE)
3983 cmethod = mono_method_get_wrapper_data (method, n);
3985 cmethod = mono_get_method (image, n, NULL);
3987 mono_class_init (cmethod->klass);
3988 handle_loaded_temps (cfg, bblock, stack_start, sp);
3992 NEW_METHODCONST (cfg, args [1], cmethod);
3993 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
3994 NEW_TEMPLOAD (cfg, *sp, temp);
3998 inline_costs += 10 * num_calls++;
4002 CHECK_STACK_OVF (1);
4003 NEW_ARGLOAD (cfg, ins, read16 (ip + 2));
4009 CHECK_STACK_OVF (1);
4010 NEW_ARGLOADA (cfg, ins, read16 (ip + 2));
4018 handle_loaded_temps (cfg, bblock, stack_start, sp);
4019 n = read16 (ip + 2);
4020 NEW_ARGSTORE (cfg, ins, n, *sp);
4022 if (ins->opcode == CEE_STOBJ) {
4023 NEW_ARGLOADA (cfg, ins, n);
4024 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4026 MONO_ADD_INS (bblock, ins);
4030 CHECK_STACK_OVF (1);
4031 NEW_LOCLOAD (cfg, ins, read16 (ip + 2));
4037 CHECK_STACK_OVF (1);
4038 NEW_LOCLOADA (cfg, ins, read16 (ip + 2));
4046 n = read16 (ip + 2);
4047 handle_loaded_temps (cfg, bblock, stack_start, sp);
4048 NEW_LOCSTORE (cfg, ins, n, *sp);
4050 if (ins->opcode == CEE_STOBJ) {
4051 NEW_LOCLOADA (cfg, ins, n);
4052 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4054 MONO_ADD_INS (bblock, ins);
4061 if (sp != stack_start)
4063 MONO_INST_NEW (cfg, ins, 256 + ip [1]);
4064 ins->inst_left = *sp;
4067 if (header->init_locals)
4068 ins->flags |= MONO_INST_INIT;
4072 /* FIXME: set init flag if locals init is set in this method */
4074 case CEE_ENDFILTER: {
4075 MonoExceptionClause *clause, *nearest;
4076 int cc, nearest_num;
4080 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
4082 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
4083 ins->inst_left = *sp;
4085 MONO_ADD_INS (bblock, ins);
4086 start_new_bblock = 1;
4090 for (cc = 0; cc < header->num_clauses; ++cc) {
4091 clause = &header->clauses [cc];
4092 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
4093 (!nearest || (clause->token_or_filter > nearest->token_or_filter))) {
4099 filter_lengths [nearest_num] = (ip - header->code) - nearest->token_or_filter;
4103 case CEE_UNALIGNED_:
4104 ins_flag |= MONO_INST_UNALIGNED;
4108 ins_flag |= MONO_INST_VOLATILE;
4112 ins_flag |= MONO_INST_TAILCALL;
4118 token = read32 (ip + 2);
4119 if (method->wrapper_type != MONO_WRAPPER_NONE)
4120 klass = mono_method_get_wrapper_data (method, token);
4122 klass = mono_class_get (image, token);
4123 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
4129 MonoInst *iargs [3];
4135 handle_loaded_temps (cfg, bblock, stack_start, sp);
4136 if (ip [1] == CEE_CPBLK) {
4137 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
4139 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
4147 /* FIXME: check we are in a catch handler */
4148 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
4149 load->cil_code = ip;
4150 MONO_INST_NEW (cfg, ins, CEE_THROW);
4151 ins->inst_left = load;
4153 MONO_ADD_INS (bblock, ins);
4155 start_new_bblock = 1;
4160 CHECK_STACK_OVF (1);
4161 token = read32 (ip + 2);
4162 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
4163 MonoType *type = mono_type_create_from_typespec (image, token);
4164 token = mono_type_size (type, &align);
4165 mono_metadata_free_type (type);
4167 MonoClass *szclass = mono_class_get (image, token);
4168 mono_class_init (szclass);
4169 token = mono_class_value_size (szclass, &align);
4171 NEW_ICONST (cfg, ins, token);
4176 case CEE_REFANYTYPE:
4177 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
4180 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
4185 g_error ("opcode 0x%02x not handled", *ip);
4188 if (start_new_bblock != 1)
4191 bblock->cil_length = ip - bblock->cil_code;
4192 bblock->next_bb = end_bblock;
4193 link_bblock (cfg, bblock, end_bblock);
4195 if (cfg->method == method && cfg->domainvar) {
4199 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
4200 call->signature = helper_sig_domain_get;
4201 call->inst.type = STACK_PTR;
4202 call->fptr = mono_domain_get;
4203 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, (MonoInst*)call);
4205 MONO_ADD_INS (init_localsbb, store);
4208 if (header->init_locals) {
4210 for (i = 0; i < header->num_locals; ++i) {
4211 int t = header->locals [i]->type;
4212 if (t == MONO_TYPE_VALUETYPE && header->locals [i]->data.klass->enumtype)
4213 t = header->locals [i]->data.klass->enum_basetype->type;
4214 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
4215 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
4216 NEW_ICONST (cfg, ins, 0);
4217 NEW_LOCSTORE (cfg, store, i, ins);
4218 MONO_ADD_INS (init_localsbb, store);
4219 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
4220 MONO_INST_NEW (cfg, ins, OP_I8CONST);
4221 ins->type = STACK_I8;
4223 NEW_LOCSTORE (cfg, store, i, ins);
4224 MONO_ADD_INS (init_localsbb, store);
4225 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
4226 MONO_INST_NEW (cfg, ins, OP_R8CONST);
4227 ins->type = STACK_R8;
4228 ins->inst_p0 = (void*)&r8_0;
4229 NEW_LOCSTORE (cfg, store, i, ins);
4230 MONO_ADD_INS (init_localsbb, store);
4231 } else if (t == MONO_TYPE_VALUETYPE) {
4232 NEW_LOCLOADA (cfg, ins, i);
4233 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (header->locals [i]), NULL, NULL);
4236 NEW_PCONST (cfg, ins, NULL);
4237 NEW_LOCSTORE (cfg, store, i, ins);
4238 MONO_ADD_INS (init_localsbb, store);
4244 /* resolve backward branches in the middle of an existing basic block */
4245 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
4247 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
4248 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
4249 if (tblock != start_bblock) {
4251 split_bblock (cfg, tblock, bblock);
4252 l = bblock->cil_code - header->code;
4253 bblock->cil_length = tblock->cil_length - l;
4254 tblock->cil_length = l;
4256 g_print ("recheck failed.\n");
4260 /* we compute regions here, because the length of filter clauses is not known in advance.
4261 * It is computed in the CEE_ENDFILTER case in the above switch statement*/
4262 if (cfg->method == method) {
4264 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
4265 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
4266 if (cfg->verbose_level > 2)
4267 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
4270 g_hash_table_destroy (bbhash);
4273 return inline_costs;
4276 if (cfg->method != method)
4277 g_hash_table_destroy (bbhash);
4281 if (cfg->method != method)
4282 g_hash_table_destroy (bbhash);
4283 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
4284 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
4289 mono_print_tree (MonoInst *tree) {
4295 arity = mono_burg_arity [tree->opcode];
4297 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
4299 switch (tree->opcode) {
4301 printf ("[%d]", tree->inst_c0);
4304 printf ("[%lld]", tree->inst_l);
4307 printf ("[%f]", *(double*)tree->inst_p0);
4310 printf ("[%f]", *(float*)tree->inst_p0);
4314 printf ("[%d]", tree->inst_c0);
4317 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
4320 printf ("[%s]", mono_arch_regname (tree->dreg));
4323 printf ("[%s]", tree->inst_newa_class->name);
4324 mono_print_tree (tree->inst_newa_len);
4335 case OP_VOIDCALLVIRT: {
4336 MonoCallInst *call = (MonoCallInst*)tree;
4338 printf ("[%s]", call->method->name);
4343 printf ("[%d (", tree->inst_c0);
4344 for (i = 0; i < tree->inst_phi_args [0]; i++) {
4347 printf ("%d", tree->inst_phi_args [i + 1]);
4359 printf ("[B%d]", tree->inst_target_bb->block_num);
4369 case OP_VOIDCALL_REG:
4370 mono_print_tree (tree->inst_left);
4382 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
4383 mono_print_tree (tree->inst_left);
4387 mono_print_tree (tree->inst_left);
4389 mono_print_tree (tree->inst_right);
4399 create_helper_signature (void)
4401 /* FIXME: set call conv */
4402 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
4403 helper_sig_newarr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4404 helper_sig_newarr->params [0] = helper_sig_newarr->params [1] = &mono_defaults.int_class->byval_arg;
4405 helper_sig_newarr->ret = &mono_defaults.object_class->byval_arg;
4406 helper_sig_newarr->params [2] = &mono_defaults.int32_class->byval_arg;
4407 helper_sig_newarr->pinvoke = 1;
4409 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
4410 helper_sig_object_new = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4411 helper_sig_object_new->params [0] = helper_sig_object_new->params [1] = &mono_defaults.int_class->byval_arg;
4412 helper_sig_object_new->ret = &mono_defaults.object_class->byval_arg;
4413 helper_sig_object_new->pinvoke = 1;
4415 /* void* mono_method_compile (MonoMethod*) */
4416 helper_sig_compile = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4417 helper_sig_compile->params [0] = helper_sig_compile->ret = &mono_defaults.int_class->byval_arg;
4418 helper_sig_compile->pinvoke = 1;
4420 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
4421 helper_sig_compile_virt = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4422 helper_sig_compile_virt->params [0] = &mono_defaults.object_class->byval_arg;
4423 helper_sig_compile_virt->params [1] = helper_sig_compile_virt->ret = &mono_defaults.int_class->byval_arg;
4424 helper_sig_compile_virt->pinvoke = 1;
4426 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
4427 helper_sig_ldstr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4428 helper_sig_ldstr->params [0] = helper_sig_ldstr->params [1] = &mono_defaults.int_class->byval_arg;
4429 helper_sig_ldstr->params [2] = &mono_defaults.int32_class->byval_arg;
4430 helper_sig_ldstr->ret = &mono_defaults.object_class->byval_arg;
4431 helper_sig_ldstr->pinvoke = 1;
4433 /* MonoDomain *mono_domain_get (void) */
4434 helper_sig_domain_get = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
4435 helper_sig_domain_get->ret = &mono_defaults.int_class->byval_arg;
4436 helper_sig_domain_get->pinvoke = 1;
4438 /* void* stelem_ref (MonoArray *, int index, MonoObject *) */
4439 helper_sig_stelem_ref = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4440 helper_sig_stelem_ref->params [0] = &mono_defaults.array_class->byval_arg;
4441 helper_sig_stelem_ref->params [1] = &mono_defaults.int32_class->byval_arg;
4442 helper_sig_stelem_ref->params [2] = &mono_defaults.object_class->byval_arg;
4443 helper_sig_stelem_ref->ret = &mono_defaults.void_class->byval_arg;
4444 helper_sig_stelem_ref->pinvoke = 1;
4446 /* long amethod (long, long) */
4447 helper_sig_long_long_long = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4448 helper_sig_long_long_long->params [0] = helper_sig_long_long_long->params [1] =
4449 &mono_defaults.int64_class->byval_arg;
4450 helper_sig_long_long_long->ret = &mono_defaults.int64_class->byval_arg;
4451 helper_sig_long_long_long->pinvoke = 1;
4453 /* object amethod (intptr) */
4454 helper_sig_obj_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4455 helper_sig_obj_ptr->params [0] = &mono_defaults.int_class->byval_arg;
4456 helper_sig_obj_ptr->ret = &mono_defaults.object_class->byval_arg;
4457 helper_sig_obj_ptr->pinvoke = 1;
4459 /* void amethod (intptr) */
4460 helper_sig_void_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4461 helper_sig_void_ptr->params [0] = &mono_defaults.int_class->byval_arg;
4462 helper_sig_void_ptr->ret = &mono_defaults.void_class->byval_arg;
4463 helper_sig_void_ptr->pinvoke = 1;
4465 /* void amethod (MonoObject *obj) */
4466 helper_sig_void_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4467 helper_sig_void_obj->params [0] = &mono_defaults.object_class->byval_arg;
4468 helper_sig_void_obj->ret = &mono_defaults.void_class->byval_arg;
4469 helper_sig_void_obj->pinvoke = 1;
4471 /* intptr amethod (void) */
4472 helper_sig_ptr_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
4473 helper_sig_ptr_void->ret = &mono_defaults.int_class->byval_arg;
4474 helper_sig_ptr_void->pinvoke = 1;
4476 /* void amethod (intptr, intptr) */
4477 helper_sig_void_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4478 helper_sig_void_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
4479 helper_sig_void_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
4480 helper_sig_void_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
4481 helper_sig_void_ptr_ptr->pinvoke = 1;
4483 /* void amethod (intptr, intptr, intptr) */
4484 helper_sig_void_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4485 helper_sig_void_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
4486 helper_sig_void_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
4487 helper_sig_void_ptr_ptr_ptr->params [2] = &mono_defaults.int_class->byval_arg;
4488 helper_sig_void_ptr_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
4489 helper_sig_void_ptr_ptr_ptr->pinvoke = 1;
4491 /* intptr amethod (intptr, intptr) */
4492 helper_sig_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4493 helper_sig_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
4494 helper_sig_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
4495 helper_sig_ptr_ptr_ptr->ret = &mono_defaults.int_class->byval_arg;
4496 helper_sig_ptr_ptr_ptr->pinvoke = 1;
4498 /* IntPtr amethod (object) */
4499 helper_sig_ptr_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4500 helper_sig_ptr_obj->params [0] = &mono_defaults.object_class->byval_arg;
4501 helper_sig_ptr_obj->ret = &mono_defaults.int_class->byval_arg;
4502 helper_sig_ptr_obj->pinvoke = 1;
4504 /* long amethod (long, guint32) */
4505 helper_sig_long_long_int = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4506 helper_sig_long_long_int->params [0] = &mono_defaults.int64_class->byval_arg;
4507 helper_sig_long_long_int->params [1] = &mono_defaults.int32_class->byval_arg;
4508 helper_sig_long_long_int->ret = &mono_defaults.int64_class->byval_arg;
4509 helper_sig_long_long_int->pinvoke = 1;
4511 /* ulong amethod (double) */
4512 helper_sig_ulong_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4513 helper_sig_ulong_double->params [0] = &mono_defaults.double_class->byval_arg;
4514 helper_sig_ulong_double->ret = &mono_defaults.uint64_class->byval_arg;
4515 helper_sig_ulong_double->pinvoke = 1;
4517 /* long amethod (double) */
4518 helper_sig_long_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4519 helper_sig_long_double->params [0] = &mono_defaults.double_class->byval_arg;
4520 helper_sig_long_double->ret = &mono_defaults.int64_class->byval_arg;
4521 helper_sig_long_double->pinvoke = 1;
4523 /* uint amethod (double) */
4524 helper_sig_uint_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4525 helper_sig_uint_double->params [0] = &mono_defaults.double_class->byval_arg;
4526 helper_sig_uint_double->ret = &mono_defaults.uint32_class->byval_arg;
4527 helper_sig_uint_double->pinvoke = 1;
4529 /* int amethod (double) */
4530 helper_sig_int_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4531 helper_sig_int_double->params [0] = &mono_defaults.double_class->byval_arg;
4532 helper_sig_int_double->ret = &mono_defaults.int32_class->byval_arg;
4533 helper_sig_int_double->pinvoke = 1;
4535 /* void initobj (intptr, int size) */
4536 helper_sig_initobj = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4537 helper_sig_initobj->params [0] = &mono_defaults.int_class->byval_arg;
4538 helper_sig_initobj->params [1] = &mono_defaults.int32_class->byval_arg;
4539 helper_sig_initobj->ret = &mono_defaults.void_class->byval_arg;
4540 helper_sig_initobj->pinvoke = 1;
4542 /* void memcpy (intptr, intptr, int size) */
4543 helper_sig_memcpy = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4544 helper_sig_memcpy->params [0] = &mono_defaults.int_class->byval_arg;
4545 helper_sig_memcpy->params [1] = &mono_defaults.int_class->byval_arg;
4546 helper_sig_memcpy->params [2] = &mono_defaults.int32_class->byval_arg;
4547 helper_sig_memcpy->ret = &mono_defaults.void_class->byval_arg;
4548 helper_sig_memcpy->pinvoke = 1;
4550 /* void memset (intptr, int val, int size) */
4551 helper_sig_memset = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4552 helper_sig_memset->params [0] = &mono_defaults.int_class->byval_arg;
4553 helper_sig_memset->params [1] = &mono_defaults.int32_class->byval_arg;
4554 helper_sig_memset->params [2] = &mono_defaults.int32_class->byval_arg;
4555 helper_sig_memset->ret = &mono_defaults.void_class->byval_arg;
4556 helper_sig_memset->pinvoke = 1;
4559 static GHashTable *jit_icall_hash_name = NULL;
4560 static GHashTable *jit_icall_hash_addr = NULL;
4563 mono_find_jit_icall_by_name (const char *name)
4565 g_assert (jit_icall_hash_name);
4567 //printf ("lookup addr %s %p\n", name, g_hash_table_lookup (jit_icall_hash_name, name));
4568 return g_hash_table_lookup (jit_icall_hash_name, name);
4572 mono_find_jit_icall_by_addr (gconstpointer addr)
4574 g_assert (jit_icall_hash_addr);
4576 return g_hash_table_lookup (jit_icall_hash_addr, (gpointer)addr);
4580 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save)
4582 MonoJitICallInfo *info;
4583 MonoMethod *wrapper;
4589 if (!jit_icall_hash_name) {
4590 jit_icall_hash_name = g_hash_table_new (g_str_hash, g_str_equal);
4591 jit_icall_hash_addr = g_hash_table_new (NULL, NULL);
4594 if (g_hash_table_lookup (jit_icall_hash_name, name)) {
4595 g_warning ("jit icall already defined \"%s\"\n", name);
4596 g_assert_not_reached ();
4599 info = g_new (MonoJitICallInfo, 1);
4601 info->name = g_strdup (name);
4606 #ifdef MONO_USE_EXC_TABLES
4607 || mono_arch_has_unwind_info (func)
4610 info->wrapper = func;
4613 n = g_strdup_printf ("__icall_wrapper_%s", name);
4614 wrapper = mono_marshal_get_icall_wrapper (sig, n, func);
4615 info->wrapper = mono_jit_compile_method (wrapper);
4619 g_hash_table_insert (jit_icall_hash_name, info->name, info);
4620 g_hash_table_insert (jit_icall_hash_addr, (gpointer)func, info);
4621 if (func != info->wrapper)
4622 g_hash_table_insert (jit_icall_hash_addr, (gpointer)info->wrapper, info);
4627 static GHashTable *emul_opcode_hash = NULL;
4629 static MonoJitICallInfo *
4630 mono_find_jit_opcode_emulation (int opcode)
4632 if (emul_opcode_hash)
4633 return g_hash_table_lookup (emul_opcode_hash, (gpointer)opcode);
4639 mono_register_opcode_emulation (int opcode, MonoMethodSignature *sig, gpointer func)
4641 MonoJitICallInfo *info;
4644 if (!emul_opcode_hash)
4645 emul_opcode_hash = g_hash_table_new (NULL, NULL);
4647 g_assert (!sig->hasthis);
4648 g_assert (sig->param_count < 3);
4650 name = g_strdup_printf ("__emulate_%s", mono_inst_name (opcode));
4652 info = mono_register_jit_icall (func, name, sig, FALSE);
4656 g_hash_table_insert (emul_opcode_hash, (gpointer)opcode, info);
4660 decompose_foreach (MonoInst *tree, gpointer data)
4662 static MonoJitICallInfo *newarr_info = NULL;
4664 switch (tree->opcode) {
4666 MonoCompile *cfg = data;
4667 MonoInst *iargs [3];
4669 NEW_DOMAINCONST (cfg, iargs [0]);
4670 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
4671 iargs [2] = tree->inst_newa_len;
4674 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
4675 g_assert (newarr_info);
4678 mono_emulate_opcode (cfg, tree, iargs, newarr_info);
4688 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
4690 switch (mono_burg_arity [tree->opcode]) {
4693 mono_inst_foreach (tree->inst_left, func, data);
4696 mono_inst_foreach (tree->inst_left, func, data);
4697 mono_inst_foreach (tree->inst_right, func, data);
4700 g_assert_not_reached ();
4707 mono_print_bb_code (MonoBasicBlock *bb) {
4709 MonoInst *c = bb->code;
4711 mono_print_tree (c);
4720 print_dfn (MonoCompile *cfg) {
4725 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
4727 for (i = 0; i < cfg->num_bblocks; ++i) {
4728 bb = cfg->bblocks [i];
4730 char* code1, *code2;
4731 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
4732 if (bb->last_ins->cil_code)
4733 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
4735 code2 = g_strdup ("");
4737 code1 [strlen (code1) - 1] = 0;
4738 code = g_strdup_printf ("%s -> %s", code1, code2);
4742 code = g_strdup ("\n");
4743 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
4745 MonoInst *c = bb->code;
4747 mono_print_tree (c);
4755 g_print ("\tprev:");
4756 for (j = 0; j < bb->in_count; ++j) {
4757 g_print (" BB%d", bb->in_bb [j]->block_num);
4759 g_print ("\t\tsucc:");
4760 for (j = 0; j < bb->out_count; ++j) {
4761 g_print (" BB%d", bb->out_bb [j]->block_num);
4763 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
4766 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
4769 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
4771 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
4779 * returns the offset used by spillvar. It allocates a new
4780 * spill variable if necessary.
4783 mono_spillvar_offset (MonoCompile *cfg, int spillvar)
4785 MonoSpillInfo **si, *info;
4788 si = &cfg->spill_info;
4790 while (i <= spillvar) {
4793 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
4795 cfg->stack_offset -= sizeof (gpointer);
4796 info->offset = cfg->stack_offset;
4800 return (*si)->offset;
4806 g_assert_not_reached ();
4811 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
4815 g_assert (bb->code);
4816 bb->last_ins->next = inst;
4817 bb->last_ins = inst;
4819 bb->last_ins = bb->code = inst;
4824 mono_destroy_compile (MonoCompile *cfg)
4826 //mono_mempool_stats (cfg->mempool);
4827 g_hash_table_destroy (cfg->bb_hash);
4829 mono_regstate_free (cfg->rs);
4830 mono_mempool_destroy (cfg->mempool);
4831 g_list_free (cfg->ldstr_list);
4833 g_free (cfg->varinfo);
4839 mono_get_lmf_addr (void)
4841 MonoJitTlsData *jit_tls;
4843 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
4844 return &jit_tls->lmf;
4846 g_assert_not_reached ();
4851 * mono_thread_abort:
4852 * @obj: exception object
4854 * abort the thread, print exception information and stack trace
4857 mono_thread_abort (MonoObject *obj)
4859 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
4867 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
4869 MonoJitTlsData *jit_tls;
4872 jit_tls = g_new0 (MonoJitTlsData, 1);
4874 TlsSetValue (mono_jit_tls_id, jit_tls);
4876 jit_tls->abort_func = mono_thread_abort;
4877 jit_tls->end_of_stack = stack_start;
4879 lmf = g_new0 (MonoLMF, 1);
4885 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
4888 mono_thread_abort_dummy (MonoObject *obj)
4890 if (mono_thread_attach_aborted_cb)
4891 mono_thread_attach_aborted_cb (obj);
4893 mono_thread_abort (obj);
4897 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
4899 MonoJitTlsData *jit_tls;
4902 jit_tls = g_new0 (MonoJitTlsData, 1);
4904 TlsSetValue (mono_jit_tls_id, jit_tls);
4906 jit_tls->abort_func = mono_thread_abort_dummy;
4907 jit_tls->end_of_stack = stack_start;
4909 lmf = g_new0 (MonoLMF, 1);
4916 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
4918 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
4922 ji->data.target = target;
4923 ji->next = cfg->patch_info;
4925 cfg->patch_info = ji;
4929 mono_remove_patch_info (MonoCompile *cfg, int ip)
4931 MonoJumpInfo **ji = &cfg->patch_info;
4934 if ((*ji)->ip.i == ip)
4937 ji = &((*ji)->next);
4942 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
4943 MonoJitICallInfo *info;
4945 switch (mono_burg_arity [tree->opcode]) {
4948 dec_foreach (tree->inst_left, cfg);
4950 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
4951 MonoInst *iargs [2];
4953 iargs [0] = tree->inst_left;
4955 mono_emulate_opcode (cfg, tree, iargs, info);
4961 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
4962 MonoInst *iargs [2];
4964 iargs [0] = tree->inst_i0;
4965 iargs [1] = tree->inst_i1;
4967 mono_emulate_opcode (cfg, tree, iargs, info);
4969 dec_foreach (iargs [0], cfg);
4970 dec_foreach (iargs [1], cfg);
4973 dec_foreach (tree->inst_left, cfg);
4974 dec_foreach (tree->inst_right, cfg);
4978 g_assert_not_reached ();
4980 decompose_foreach (tree, cfg);
4984 decompose_pass (MonoCompile *cfg) {
4987 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
4990 cfg->prev_ins = NULL;
4991 for (tree = cfg->cbb->code; tree; tree = tree->next) {
4992 dec_foreach (tree, cfg);
4993 cfg->prev_ins = tree;
4999 nullify_basic_block (MonoBasicBlock *bb)
5006 bb->code = bb->last_ins = NULL;
5010 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
5014 for (i = 0; i < bb->out_count; i++) {
5015 MonoBasicBlock *ob = bb->out_bb [i];
5016 for (j = 0; j < ob->in_count; j++) {
5017 if (ob->in_bb [j] == orig)
5018 ob->in_bb [j] = repl;
5025 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
5027 bb->out_count = bbn->out_count;
5028 bb->out_bb = bbn->out_bb;
5030 replace_basic_block (bb, bbn, bb);
5034 bb->last_ins->next = bbn->code;
5035 bb->last_ins = bbn->last_ins;
5038 bb->code = bbn->code;
5039 bb->last_ins = bbn->last_ins;
5041 bb->next_bb = bbn->next_bb;
5042 nullify_basic_block (bbn);
5046 optimize_branches (MonoCompile *cfg) {
5047 int changed = FALSE;
5048 MonoBasicBlock *bb, *bbn;
5053 /* we skip the entry block (exit is handled specially instead ) */
5054 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
5056 if (bb->out_count == 1) {
5057 bbn = bb->out_bb [0];
5059 if (bb->region == bbn->region && bb->next_bb == bbn) {
5060 /* the block are in sequence anyway ... */
5063 * miguel: I do not understand what the test below does, could we
5064 * use a macro, or a comment here? opcode > CEE_BEQ && <= BLT_UN
5066 * It could also test for bb->last_in only once, and the value
5067 * could be cached (last_ins->opcode)
5069 if (bb->last_ins && (bb->last_ins->opcode == CEE_BR || (
5070 (bb->last_ins && bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN)))) {
5071 bb->last_ins->opcode = CEE_NOP;
5073 if (cfg->verbose_level > 2)
5074 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
5076 /* fixme: this causes problems with inlining */
5077 if (bbn->in_count == 1) {
5079 if (bbn != cfg->bb_exit) {
5080 if (cfg->verbose_level > 2)
5081 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
5082 merge_basic_blocks (bb, bbn);
5086 //mono_print_bb_code (bb);
5089 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
5090 bbn = bb->last_ins->inst_target_bb;
5091 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR) {
5093 if (cfg->verbose_level > 2)
5094 g_print ("in %s branch to branch triggered %d -> %d\n", cfg->method->name, bb->block_num, bbn->block_num);
5095 bb->out_bb [0] = bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
5100 } else if (bb->out_count == 2) {
5101 /* fixme: this does not correctly unlink the blocks, so we get serious problems in idom code */
5102 if (0 && bb->last_ins && bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
5103 bbn = bb->last_ins->inst_true_bb;
5104 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR) {
5105 if (cfg->verbose_level > 2)
5106 g_print ("cbranch to branch triggered %d -> %d (0x%02x)\n", bb->block_num,
5107 bbn->block_num, bbn->code->opcode);
5109 if (bb->out_bb [0] == bbn) {
5110 bb->out_bb [0] = bbn->code->inst_target_bb;
5111 } else if (bb->out_bb [1] == bbn) {
5112 bb->out_bb [1] = bbn->code->inst_target_bb;
5114 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
5124 mono_compile_create_vars (MonoCompile *cfg)
5126 MonoMethodSignature *sig;
5127 MonoMethodHeader *header;
5130 header = ((MonoMethodNormal *)cfg->method)->header;
5132 sig = cfg->method->signature;
5134 if (!MONO_TYPE_IS_VOID (sig->ret)) {
5135 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
5136 cfg->ret->opcode = OP_RETARG;
5137 cfg->ret->inst_vtype = sig->ret;
5138 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
5142 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
5144 for (i = 0; i < sig->param_count; ++i)
5145 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
5147 cfg->locals_start = cfg->num_varinfo;
5149 for (i = 0; i < header->num_locals; ++i)
5150 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
5155 mono_print_code (MonoCompile *cfg)
5159 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5160 MonoInst *tree = bb->code;
5165 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
5167 for (; tree; tree = tree->next) {
5168 mono_print_tree (tree);
5173 bb->last_ins->next = NULL;
5178 extern const char * const mono_burg_rule_string [];
5181 emit_state (MonoCompile *cfg, MBState *state, int goal)
5184 int ern = mono_burg_rule (state, goal);
5185 const guint16 *nts = mono_burg_nts [ern];
5188 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
5192 // state->reg1 = state->reg2; /* chain rule */
5194 state->reg1 = mono_regstate_next_int (cfg->rs);
5195 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
5198 state->reg1 = mono_regstate_next_int (cfg->rs);
5199 state->reg2 = mono_regstate_next_int (cfg->rs);
5202 state->reg1 = mono_regstate_next_float (cfg->rs);
5209 mono_burg_kids (state, ern, kids);
5211 emit_state (cfg, kids [0], nts [0]);
5213 emit_state (cfg, kids [1], nts [1]);
5215 g_assert (!nts [3]);
5216 emit_state (cfg, kids [2], nts [2]);
5221 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
5222 if ((emit = mono_burg_func [ern]))
5223 emit (state, state->tree, cfg);
5226 #define DEBUG_SELECTION
5229 mini_select_instructions (MonoCompile *cfg)
5233 cfg->state_pool = mono_mempool_new ();
5234 cfg->rs = mono_regstate_new ();
5236 #ifdef DEBUG_SELECTION
5237 if (cfg->verbose_level >= 4) {
5238 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5239 MonoInst *tree = bb->code;
5240 g_print ("DUMP BLOCK %d:\n", bb->block_num);
5243 for (; tree; tree = tree->next) {
5244 mono_print_tree (tree);
5251 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5252 MonoInst *tree = bb->code, *next;
5258 bb->last_ins = NULL;
5261 mono_regstate_reset (cfg->rs);
5263 #ifdef DEBUG_SELECTION
5264 if (cfg->verbose_level >= 3)
5265 g_print ("LABEL BLOCK %d:\n", bb->block_num);
5267 for (; tree; tree = next) {
5269 #ifdef DEBUG_SELECTION
5270 if (cfg->verbose_level >= 3) {
5271 mono_print_tree (tree);
5276 if (!(mbstate = mono_burg_label (tree, cfg))) {
5277 g_warning ("unabled to label tree %p", tree);
5278 mono_print_tree (tree);
5280 g_assert_not_reached ();
5282 emit_state (cfg, mbstate, MB_NTERM_stmt);
5284 bb->max_ireg = cfg->rs->next_vireg;
5285 bb->max_freg = cfg->rs->next_vfreg;
5288 bb->last_ins->next = NULL;
5290 mono_mempool_empty (cfg->state_pool);
5292 mono_mempool_destroy (cfg->state_pool);
5296 mono_codegen (MonoCompile *cfg)
5298 MonoJumpInfo *patch_info;
5300 int i, max_epilog_size;
5303 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5304 cfg->spill_count = 0;
5305 /* we reuse dfn here */
5306 /* bb->dfn = bb_count++; */
5307 mono_arch_local_regalloc (cfg, bb);
5310 if (mono_trace_coverage)
5311 mono_allocate_coverage_info (cfg->method, cfg->num_bblocks);
5313 code = mono_arch_emit_prolog (cfg);
5315 if (mono_jit_profile)
5316 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
5318 cfg->code_len = code - cfg->native_code;
5319 cfg->prolog_end = cfg->code_len;
5321 mono_debug_open_method (cfg);
5323 /* emit code all basic blocks */
5324 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5325 bb->native_offset = cfg->code_len;
5326 mono_arch_output_basic_block (cfg, bb);
5328 cfg->bb_exit->native_offset = cfg->code_len;
5330 code = cfg->native_code + cfg->code_len;
5332 max_epilog_size = mono_arch_max_epilog_size (cfg);
5334 /* we always allocate code in cfg->domain->code_mp to increase locality */
5335 cfg->code_size = cfg->code_len + max_epilog_size;
5336 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
5337 code = mono_mempool_alloc (cfg->domain->code_mp, cfg->code_size);
5338 memcpy (code, cfg->native_code, cfg->code_len);
5339 g_free (cfg->native_code);
5340 cfg->native_code = code;
5341 code = cfg->native_code + cfg->code_len;
5343 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
5345 cfg->epilog_begin = cfg->code_len;
5347 if (mono_jit_profile)
5348 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
5350 cfg->code_len = code - cfg->native_code;
5352 mono_arch_emit_epilog (cfg);
5354 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5355 switch (patch_info->type) {
5356 case MONO_PATCH_INFO_ABS: {
5357 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
5359 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
5360 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
5361 patch_info->data.name = info->name;
5365 case MONO_PATCH_INFO_SWITCH: {
5366 gpointer *table = g_new (gpointer, patch_info->table_size);
5367 patch_info->ip.i = patch_info->ip.label->inst_c0;
5368 for (i = 0; i < patch_info->table_size; i++) {
5369 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
5371 patch_info->data.target = table;
5380 if (cfg->verbose_level > 1)
5381 g_print ("Method %s::%s emmitted at %p to %p\n", cfg->method->klass->name,
5382 cfg->method->name, cfg->native_code, cfg->native_code + cfg->code_len);
5384 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info);
5386 mono_debug_close_method (cfg);
5390 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
5395 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
5396 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
5398 if (cp->opcode == OP_ICONST) {
5399 if (cfg->opt & MONO_OPT_CONSPROP) {
5400 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
5404 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
5405 if (cfg->opt & MONO_OPT_COPYPROP) {
5406 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
5412 arity = mono_burg_arity [tree->opcode];
5415 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
5416 if (cfg->opt & MONO_OPT_CFOLD)
5417 mono_constant_fold_inst (tree, NULL);
5419 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
5420 if (cfg->opt & MONO_OPT_CFOLD)
5421 mono_constant_fold_inst (tree, NULL);
5423 mono_constant_fold_inst (tree, NULL);
5429 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
5433 switch (tree->opcode) {
5443 if (tree->ssa_op == MONO_SSA_NOP) {
5444 memset (acp, 0, sizeof (MonoInst *) * acp_size);
5461 case OP_VOIDCALL_REG:
5462 case OP_VOIDCALLVIRT:
5464 MonoCallInst *call = (MonoCallInst *)tree;
5465 MonoMethodSignature *sig = call->signature;
5466 int i, byref = FALSE;
5468 for (i = 0; i < sig->param_count; i++) {
5469 if (sig->params [i]->byref) {
5476 memset (acp, 0, sizeof (MonoInst *) * acp_size);
5484 arity = mono_burg_arity [tree->opcode];
5490 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
5493 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
5494 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
5497 g_assert_not_reached ();
5502 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
5504 MonoInst *tree = bb->code;
5510 for (; tree; tree = tree->next) {
5512 mono_cprop_copy_values (cfg, tree, acp);
5514 mono_cprop_invalidate_values (tree, acp, acp_size);
5516 if (tree->ssa_op == MONO_SSA_STORE &&
5517 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
5518 MonoInst *i1 = tree->inst_i1;
5520 acp [tree->inst_i0->inst_c0] = NULL;
5522 for (i = 0; i < acp_size; i++) {
5523 if (acp [i] && acp [i]->opcode != OP_ICONST &&
5524 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
5529 if (i1->opcode == OP_ICONST) {
5530 acp [tree->inst_i0->inst_c0] = i1;
5531 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
5533 if (i1->ssa_op == MONO_SSA_LOAD &&
5534 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
5535 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
5536 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
5537 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
5542 if (tree->opcode == CEE_BEQ) {
5543 g_assert (tree->inst_i0->opcode == OP_COMPARE);
5544 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
5545 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
5547 tree->opcode = CEE_BR;
5548 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
5549 tree->inst_target_bb = tree->inst_true_bb;
5551 tree->inst_target_bb = tree->inst_false_bb;
5560 mono_local_cprop (MonoCompile *cfg)
5565 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
5567 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5568 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
5569 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
5574 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, int parts)
5576 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
5577 guint8 *ip = (guint8 *)header->code;
5580 int dfn = 0, i, code_size_ratio;
5582 mono_jit_stats.methods_compiled++;
5583 if (mono_jit_profile)
5584 mono_profiler_method_jit (method);
5586 cfg = g_new0 (MonoCompile, 1);
5587 cfg->method = method;
5588 cfg->mempool = mono_mempool_new ();
5590 cfg->bb_hash = g_hash_table_new (g_direct_hash, NULL);
5591 cfg->domain = domain;
5592 cfg->verbose_level = mini_verbose;
5595 * create MonoInst* which represents arguments and local variables
5597 mono_compile_create_vars (cfg);
5599 if (cfg->verbose_level > 2)
5600 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
5602 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0)) < 0) {
5603 mono_destroy_compile (cfg);
5604 if (mono_jit_profile)
5605 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
5609 mono_jit_stats.basic_blocks += cfg->num_bblocks;
5610 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
5612 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
5614 /* Depth-first ordering on basic blocks */
5615 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
5617 if (cfg->opt & MONO_OPT_BRANCH)
5618 optimize_branches (cfg);
5620 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
5621 if (cfg->num_bblocks != dfn + 1) {
5622 if (cfg->verbose_level > 1)
5623 g_print ("unreachable code?\n");
5624 cfg->num_bblocks = dfn + 1;
5627 if (cfg->opt & MONO_OPT_LOOP) {
5628 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
5629 mono_compute_natural_loops (cfg);
5633 /* after method_to_ir */
5637 //#define DEBUGSSA "logic_run"
5638 #define DEBUGSSA_CLASS "Tests"
5642 if (!header->num_clauses && !cfg->disable_ssa) {
5643 mono_local_cprop (cfg);
5644 mono_ssa_compute (cfg);
5648 /* fixme: add all optimizations which requires SSA */
5649 if (cfg->opt & (MONO_OPT_DEADCE)) {
5650 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
5651 mono_local_cprop (cfg);
5652 mono_ssa_compute (cfg);
5654 if (cfg->verbose_level >= 2) {
5661 /* after SSA translation */
5665 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
5666 if (cfg->comp_done & MONO_COMP_SSA) {
5667 mono_ssa_cprop (cfg);
5669 mono_local_cprop (cfg);
5673 if (cfg->comp_done & MONO_COMP_SSA) {
5674 mono_ssa_deadce (cfg);
5676 //mono_ssa_strength_reduction (cfg);
5678 mono_ssa_remove (cfg);
5680 if (cfg->opt & MONO_OPT_BRANCH)
5681 optimize_branches (cfg);
5684 /* after SSA removal */
5688 decompose_pass (cfg);
5690 if (cfg->opt & MONO_OPT_LINEARS) {
5693 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
5694 cfg->comp_done &= ~MONO_COMP_LIVENESS;
5695 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
5696 mono_analyze_liveness (cfg);
5698 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
5699 regs = mono_arch_get_global_int_regs (cfg);
5700 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
5704 //mono_print_code (cfg);
5708 /* variables are allocated after decompose, since decompose could create temps */
5709 mono_arch_allocate_vars (cfg);
5711 if (cfg->opt & MONO_OPT_CFOLD)
5712 mono_constant_fold (cfg);
5714 mini_select_instructions (cfg);
5717 if (cfg->verbose_level >= 2) {
5718 char *id = mono_method_full_name (cfg->method, FALSE);
5719 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
5723 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
5725 jinfo = g_new0 (MonoJitInfo, 1);
5726 jinfo->method = method;
5727 jinfo->code_start = cfg->native_code;
5728 jinfo->code_size = cfg->code_len;
5729 jinfo->used_regs = cfg->used_int_regs;
5731 if (header->num_clauses) {
5734 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
5735 jinfo->num_clauses = header->num_clauses;
5736 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
5737 sizeof (MonoJitExceptionInfo) * header->num_clauses);
5739 for (i = 0; i < header->num_clauses; i++) {
5740 MonoExceptionClause *ec = &header->clauses [i];
5741 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
5742 MonoBasicBlock *tblock;
5744 ei->flags = ec->flags;
5746 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
5747 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->token_or_filter);
5749 ei->data.filter = cfg->native_code + tblock->native_offset;
5751 ei->data.token = ec->token_or_filter;
5754 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
5756 ei->try_start = cfg->native_code + tblock->native_offset;
5757 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
5759 ei->try_end = cfg->native_code + tblock->native_offset;
5760 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
5762 ei->handler_start = cfg->native_code + tblock->native_offset;
5767 mono_jit_info_table_add (cfg->domain, jinfo);
5769 /* collect statistics */
5770 mono_jit_stats.allocated_code_size += cfg->code_len;
5771 code_size_ratio = cfg->code_len;
5772 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
5773 mono_jit_stats.biggest_method_size = code_size_ratio;
5774 mono_jit_stats.biggest_method = method;
5776 code_size_ratio = (code_size_ratio * 100) / ((MonoMethodNormal *)method)->header->code_size;
5777 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
5778 mono_jit_stats.max_code_size_ratio = code_size_ratio;
5779 mono_jit_stats.max_ratio_method = method;
5781 mono_jit_stats.native_code_size += cfg->code_len;
5783 if (mono_jit_profile)
5784 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
5790 mono_jit_compile_method (MonoMethod *method)
5792 /* FIXME: later copy the code from mono */
5793 MonoDomain *target_domain, *domain = mono_domain_get ();
5795 GHashTable *jit_code_hash;
5798 if (default_opt & MONO_OPT_SAHRED)
5799 target_domain = mono_root_domain;
5801 target_domain = domain;
5803 jit_code_hash = target_domain->jit_code_hash;
5805 if ((code = g_hash_table_lookup (jit_code_hash, method))) {
5806 mono_jit_stats.methods_lookups++;
5810 #ifdef MONO_USE_AOT_COMPILER
5811 if (!mono_compile_aot) {
5812 mono_class_init (method->klass);
5813 if ((code = mono_aot_get_method (method))) {
5814 g_hash_table_insert (jit_code_hash, method, code);
5820 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
5821 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
5822 if (!method->info) {
5825 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
5826 mono_lookup_pinvoke_call (method);
5827 #ifdef MONO_USE_EXC_TABLES
5828 if (mono_method_blittable (method)) {
5829 method->info = method->addr;
5832 nm = mono_marshal_get_native_wrapper (method);
5833 method->info = mono_compile_method (nm);
5835 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
5836 //mono_debug_add_wrapper (method, nm);
5837 #ifdef MONO_USE_EXC_TABLES
5841 return method->info;
5842 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
5843 const char *name = method->name;
5846 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
5847 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
5848 /* FIXME: uhm, we need a wrapper to handle exceptions? */
5849 return (gpointer)mono_delegate_ctor;
5850 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
5851 nm = mono_marshal_get_delegate_invoke (method);
5852 return mono_jit_compile_method (nm);
5853 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
5854 nm = mono_marshal_get_delegate_begin_invoke (method);
5855 return mono_jit_compile_method (nm);
5856 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
5857 nm = mono_marshal_get_delegate_end_invoke (method);
5858 return mono_jit_compile_method (nm);
5864 cfg = mini_method_compile (method, default_opt, target_domain, 0);
5865 code = cfg->native_code;
5866 mono_destroy_compile (cfg);
5868 g_hash_table_insert (jit_code_hash, method, code);
5870 /* make sure runtime_init is called */
5871 mono_class_vtable (target_domain, method->klass);
5877 * mono_jit_runtime_invoke:
5878 * @method: the method to invoke
5879 * @obj: this pointer
5880 * @params: array of parameter values.
5881 * @exc: used to catch exceptions objects
5884 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
5887 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc);
5888 invoke = mono_marshal_get_runtime_invoke (method);
5889 runtime_invoke = mono_jit_compile_method (invoke);
5890 return runtime_invoke (obj, params, exc);
5893 #ifdef PLATFORM_WIN32
5894 #define GET_CONTEXT \
5895 struct sigcontext *ctx = (struct sigcontext*)_dummy;
5897 #define GET_CONTEXT \
5898 void **_p = (void **)&_dummy; \
5899 struct sigcontext *ctx = (struct sigcontext *)++_p;
5903 sigfpe_signal_handler (int _dummy)
5908 exc = mono_get_exception_divide_by_zero ();
5910 mono_arch_handle_exception (ctx, exc, FALSE);
5914 sigill_signal_handler (int _dummy)
5918 exc = mono_get_exception_execution_engine ("SIGILL");
5920 mono_arch_handle_exception (ctx, exc, FALSE);
5924 sigsegv_signal_handler (int _dummy)
5929 exc = mono_get_exception_null_reference ();
5931 mono_arch_handle_exception (ctx, exc, FALSE);
5935 sigusr1_signal_handler (int _dummy)
5940 thread = mono_thread_current ();
5942 g_assert (thread->abort_exc);
5944 mono_arch_handle_exception (ctx, thread->abort_exc, FALSE);
5948 mono_runtime_install_handlers (void)
5950 #ifndef PLATFORM_WIN32
5951 struct sigaction sa;
5954 #ifdef PLATFORM_WIN32
5956 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
5957 win32_seh_set_handler(SIGILL, sigill_signal_handler);
5958 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
5959 #else /* !PLATFORM_WIN32 */
5961 /* libpthreads has its own implementation of sigaction(),
5962 * but it seems to work well with our current exception
5963 * handlers. If not we must call syscall directly instead
5967 sa.sa_handler = sigfpe_signal_handler;
5968 sigemptyset (&sa.sa_mask);
5970 //g_assert (syscall (SYS_sigaction, SIGFPE, &sa, NULL) != -1);
5971 g_assert (sigaction (SIGFPE, &sa, NULL) != -1);
5974 sa.sa_handler = sigill_signal_handler;
5975 sigemptyset (&sa.sa_mask);
5977 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
5978 g_assert (sigaction (SIGILL, &sa, NULL) != -1);
5980 /* catch thread abort signal */
5981 sa.sa_handler = sigusr1_signal_handler;
5982 sigemptyset (&sa.sa_mask);
5984 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
5985 g_assert (sigaction (mono_thread_get_abort_signal (), &sa, NULL) != -1);
5989 sa.sa_handler = sigsegv_signal_handler;
5990 sigemptyset (&sa.sa_mask);
5992 //g_assert (syscall (SYS_sigaction, SIGSEGV, &sa, NULL) != -1);
5993 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
5995 #endif /* PLATFORM_WIN32 */
5998 /* mono_jit_create_remoting_trampoline:
5999 * @method: pointer to the method info
6001 * Creates a trampoline which calls the remoting functions. This
6002 * is used in the vtable of transparent proxies.
6004 * Returns: a pointer to the newly created code
6007 mono_jit_create_remoting_trampoline (MonoMethod *method)
6010 guint8 *addr = NULL;
6012 if (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class)) {
6013 nm = mono_marshal_get_remoting_invoke (method);
6014 addr = mono_compile_method (nm);
6016 addr = mono_compile_method (method);
6021 static CRITICAL_SECTION ms;
6024 mini_init (const char *filename)
6028 metadata_section = &ms;
6029 InitializeCriticalSection (metadata_section);
6031 mono_jit_tls_id = TlsAlloc ();
6032 mono_thread_start_cb (GetCurrentThreadId (), (gpointer)-1, NULL);
6036 mono_runtime_install_handlers ();
6038 mono_install_compile_method (mono_jit_compile_method);
6039 mono_install_trampoline (mono_arch_create_jit_trampoline);
6040 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
6041 mono_install_runtime_invoke (mono_jit_runtime_invoke);
6042 mono_install_handler (mono_arch_get_throw_exception ());
6043 mono_install_stack_walk (mono_jit_walk_stack);
6044 mono_install_get_config_dir ();
6046 domain = mono_init (filename);
6049 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
6050 ves_icall_get_frame_info);
6051 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
6052 ves_icall_get_trace);
6053 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
6054 mono_runtime_install_handlers);
6057 create_helper_signature ();
6059 mono_arch_register_lowlevel_calls ();
6060 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
6061 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
6063 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
6064 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
6066 /* fixme: we cant hanlde vararg methods this way, because the signature is not constant */
6067 //mono_register_jit_icall (ves_array_element_address, "ves_array_element_address", NULL);
6068 //mono_register_jit_icall (mono_array_new_va, "mono_array_new_va", NULL);
6070 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
6071 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
6072 helper_sig_void_ptr, TRUE);
6075 * NOTE, NOTE, NOTE, NOTE:
6076 * when adding emulation for some opcodes, remember to also add a dummy
6077 * rule to the burg files, because we need the arity information to be correct.
6079 mono_register_opcode_emulation (OP_LMUL, helper_sig_long_long_long, mono_llmult);
6080 mono_register_opcode_emulation (OP_LMUL_OVF_UN, helper_sig_long_long_long, mono_llmult_ovf_un);
6081 mono_register_opcode_emulation (OP_LMUL_OVF, helper_sig_long_long_long, mono_llmult_ovf);
6082 mono_register_opcode_emulation (OP_LDIV, helper_sig_long_long_long, mono_lldiv);
6083 mono_register_opcode_emulation (OP_LDIV_UN, helper_sig_long_long_long, mono_lldiv_un);
6084 mono_register_opcode_emulation (OP_LREM, helper_sig_long_long_long, mono_llrem);
6085 mono_register_opcode_emulation (OP_LREM_UN, helper_sig_long_long_long, mono_llrem_un);
6087 mono_register_opcode_emulation (OP_LSHL, helper_sig_long_long_int, mono_lshl);
6088 mono_register_opcode_emulation (OP_LSHR, helper_sig_long_long_int, mono_lshr);
6089 mono_register_opcode_emulation (OP_LSHR_UN, helper_sig_long_long_int, mono_lshr_un);
6091 mono_register_opcode_emulation (OP_FCONV_TO_U8, helper_sig_ulong_double, mono_fconv_u8);
6092 mono_register_opcode_emulation (OP_FCONV_TO_U4, helper_sig_uint_double, mono_fconv_u4);
6093 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, helper_sig_long_double, mono_fconv_ovf_i8);
6094 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, helper_sig_ulong_double, mono_fconv_ovf_u8);
6096 #if SIZEOF_VOID_P == 4
6097 mono_register_opcode_emulation (OP_FCONV_TO_U, helper_sig_uint_double, mono_fconv_u4);
6099 #warning "fixme: add opcode emulation"
6102 /* other jit icalls */
6103 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
6104 helper_sig_ptr_ptr_ptr, FALSE);
6105 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
6106 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
6107 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
6108 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
6109 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
6110 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
6111 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
6112 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
6113 mono_register_jit_icall (mono_string_to_utf16, "mono_string_to_utf16", helper_sig_ptr_obj, FALSE);
6114 mono_register_jit_icall (mono_string_from_utf16, "mono_string_from_utf16", helper_sig_obj_ptr, FALSE);
6115 mono_register_jit_icall (mono_string_new_wrapper, "mono_string_new_wrapper", helper_sig_obj_ptr, FALSE);
6116 mono_register_jit_icall (mono_string_to_utf8, "mono_string_to_utf8", helper_sig_ptr_obj, FALSE);
6117 mono_register_jit_icall (mono_string_to_bstr, "mono_string_to_bstr", helper_sig_ptr_obj, FALSE);
6118 mono_register_jit_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", helper_sig_ptr_obj, FALSE);
6119 mono_register_jit_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", helper_sig_ptr_obj, FALSE);
6120 mono_register_jit_icall (mono_array_to_savearray, "mono_array_to_savearray", helper_sig_ptr_obj, FALSE);
6121 mono_register_jit_icall (mono_array_to_lparray, "mono_array_to_lparray", helper_sig_ptr_obj, FALSE);
6122 mono_register_jit_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", helper_sig_ptr_obj, FALSE);
6123 mono_register_jit_icall (mono_marshal_string_array, "mono_marshal_string_array", helper_sig_ptr_obj, FALSE);
6124 mono_register_jit_icall (mono_string_utf8_to_builder, "mono_string_utf8_to_builder", helper_sig_void_ptr_ptr, FALSE);
6125 mono_register_jit_icall (mono_marshal_free_array, "mono_marshal_free_array", helper_sig_void_ptr_ptr, FALSE);
6126 mono_register_jit_icall (mono_string_to_byvalstr, "mono_string_to_byvalstr", helper_sig_void_ptr_ptr_ptr, FALSE);
6127 mono_register_jit_icall (mono_string_to_byvalwstr, "mono_string_to_byvalwstr", helper_sig_void_ptr_ptr_ptr, FALSE);
6128 mono_register_jit_icall (g_free, "g_free", helper_sig_void_ptr, FALSE);
6129 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
6130 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
6132 mono_runtime_init (domain, mono_thread_start_cb,
6133 mono_thread_attach_cb);
6135 //mono_thread_attach (domain);
6139 MonoJitStats mono_jit_stats = {0};
6142 print_jit_stats (void)
6144 if (mono_jit_stats.enabled) {
6145 g_print ("Mono Jit statistics\n");
6146 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
6147 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
6148 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
6149 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
6150 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
6151 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
6152 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
6153 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
6154 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
6155 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
6156 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
6157 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
6158 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
6159 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
6160 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
6161 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
6162 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
6163 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
6165 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
6166 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
6167 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
6168 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
6169 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
6174 mini_cleanup (MonoDomain *domain)
6177 * mono_runtime_cleanup() and mono_domain_finalize () need to
6178 * be called early since they need the execution engine still
6179 * fully working (mono_domain_finalize may invoke managed finalizers
6180 * and mono_runtime_cleanup will wait for other threads to finish).
6182 mono_domain_finalize (domain);
6184 mono_runtime_cleanup (domain);
6186 mono_profiler_shutdown ();
6188 mono_debug_cleanup ();
6189 #ifdef PLATFORM_WIN32
6190 win32_seh_cleanup();
6193 mono_domain_unload (domain, TRUE);
6196 DeleteCriticalSection (metadata_section);
6200 mini_set_defaults (int verbose_level, guint32 opts)
6202 mini_verbose = verbose_level;