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 gboolean mono_arch_handle_exception (struct sigcontext *ctx, gpointer obj, gboolean test_only);
49 static gpointer mono_jit_compile_method (MonoMethod *method);
51 static void handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src,
52 const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native);
54 static int mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
55 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
56 guint inline_offset, gboolean is_virtual_call);
58 extern guint8 mono_burg_arity [];
59 /* helper methods signature */
60 static MonoMethodSignature *helper_sig_long_long_long = NULL;
61 static MonoMethodSignature *helper_sig_long_long_int = NULL;
62 static MonoMethodSignature *helper_sig_newarr = NULL;
63 static MonoMethodSignature *helper_sig_newarr_specific = NULL;
64 static MonoMethodSignature *helper_sig_ldstr = NULL;
65 static MonoMethodSignature *helper_sig_domain_get = NULL;
66 static MonoMethodSignature *helper_sig_object_new = NULL;
67 static MonoMethodSignature *helper_sig_object_new_specific = NULL;
68 static MonoMethodSignature *helper_sig_compile = NULL;
69 static MonoMethodSignature *helper_sig_compile_virt = NULL;
70 static MonoMethodSignature *helper_sig_obj_ptr = NULL;
71 static MonoMethodSignature *helper_sig_ptr_void = NULL;
72 static MonoMethodSignature *helper_sig_void_ptr = NULL;
73 static MonoMethodSignature *helper_sig_void_obj = NULL;
74 static MonoMethodSignature *helper_sig_void_ptr_ptr = NULL;
75 static MonoMethodSignature *helper_sig_void_ptr_ptr_ptr = NULL;
76 static MonoMethodSignature *helper_sig_ptr_ptr_ptr = NULL;
77 static MonoMethodSignature *helper_sig_ptr_obj = NULL;
78 static MonoMethodSignature *helper_sig_ptr_int = NULL;
79 static MonoMethodSignature *helper_sig_initobj = NULL;
80 static MonoMethodSignature *helper_sig_memcpy = NULL;
81 static MonoMethodSignature *helper_sig_memset = NULL;
82 static MonoMethodSignature *helper_sig_ulong_double = NULL;
83 static MonoMethodSignature *helper_sig_long_double = NULL;
84 static MonoMethodSignature *helper_sig_uint_double = NULL;
85 static MonoMethodSignature *helper_sig_int_double = NULL;
86 static MonoMethodSignature *helper_sig_stelem_ref = NULL;
88 static guint32 default_opt = MONO_OPT_PEEPHOLE;
90 guint32 mono_jit_tls_id = 0;
91 gboolean mono_jit_trace_calls = FALSE;
92 gboolean mono_break_on_exc = FALSE;
93 gboolean mono_compile_aot = FALSE;
94 gboolean mono_trace_coverage = FALSE;
95 gboolean mono_jit_profile = FALSE;
97 CRITICAL_SECTION *metadata_section = NULL;
99 static int mini_verbose = 0;
101 #ifdef MONO_USE_EXC_TABLES
103 mono_type_blittable (MonoType *type)
122 case MONO_TYPE_OBJECT:
124 case MONO_TYPE_VALUETYPE:
125 case MONO_TYPE_CLASS:
126 return type->data.klass->blittable;
136 mono_method_blittable (MonoMethod *method)
138 MonoMethodSignature *sig;
144 if (!mono_arch_has_unwind_info (method->addr)) {
148 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
151 sig = method->signature;
153 if (!mono_type_blittable (sig->ret))
156 for (i = 0; i < sig->param_count; i++)
157 if (!mono_type_blittable (sig->params [i]))
165 G_GNUC_UNUSED static void
166 print_method_from_ip (void *ip)
171 MonoDomain *domain = mono_domain_get ();
173 ji = mono_jit_info_table_find (domain, ip);
175 g_print ("No method at %p\n", ip);
178 method = mono_method_full_name (ji->method, TRUE);
179 source = mono_debug_source_location_from_address (ji->method, (int) ip, NULL, domain);
181 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);
184 g_print ("%s\n", source);
191 #define MONO_INIT_VARINFO(vi,id) do { \
192 (vi)->range.first_use.pos.bid = 0xffff; \
198 * Basic blocks have two numeric identifiers:
199 * dfn: Depth First Number
200 * block_num: unique ID assigned at bblock creation
202 #define NEW_BBLOCK(cfg) (mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)))
203 #define ADD_BBLOCK(cfg,bbhash,b) do { \
204 g_hash_table_insert (bbhash, (b)->cil_code, (b)); \
205 (b)->block_num = cfg->num_bblocks++; \
206 (b)->real_offset = real_offset; \
209 #define GET_BBLOCK(cfg,bbhash,tblock,ip) do { \
210 (tblock) = g_hash_table_lookup (bbhash, (ip)); \
212 if ((ip) >= end || (ip) < header->code) goto unverified; \
213 (tblock) = NEW_BBLOCK (cfg); \
214 (tblock)->cil_code = (ip); \
215 ADD_BBLOCK (cfg, (bbhash), (tblock)); \
217 (tblock)->real_offset = real_offset; \
220 #define CHECK_BBLOCK(target,ip,tblock) do { \
221 if ((target) < (ip) && !(tblock)->code) { \
222 bb_recheck = g_list_prepend (bb_recheck, (tblock)); \
223 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); \
227 #define NEW_ICONST(cfg,dest,val) do { \
228 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
229 (dest)->opcode = OP_ICONST; \
230 (dest)->inst_c0 = (val); \
231 (dest)->type = STACK_I4; \
234 /* FIXME: have a different definition of NEW_PCONST for 64 bit systems */
235 #define NEW_PCONST(cfg,dest,val) do { \
236 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
237 (dest)->opcode = OP_ICONST; \
238 (dest)->inst_p0 = (val); \
239 (dest)->type = STACK_PTR; \
242 #define NEW_CLASSCONST(cfg,dest,val) do { \
243 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
244 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
245 (dest)->inst_p0 = (val); \
246 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_CLASS; \
247 (dest)->type = STACK_PTR; \
250 #define NEW_IMAGECONST(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_IMAGE; \
255 (dest)->type = STACK_PTR; \
258 #define NEW_FIELDCONST(cfg,dest,field) do { \
259 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
260 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
261 (dest)->inst_p0 = (field); \
262 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_FIELD; \
263 (dest)->type = STACK_PTR; \
266 #define NEW_METHODCONST(cfg,dest,val) do { \
267 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
268 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
269 (dest)->inst_p0 = (val); \
270 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_METHODCONST; \
271 (dest)->type = STACK_PTR; \
274 #define NEW_DOMAINCONST(cfg,dest) do { \
275 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) { \
276 NEW_TEMPLOAD (cfg, dest, mono_get_domainvar (cfg)->inst_c0); \
278 NEW_PCONST (cfg, dest, (cfg)->domain); \
282 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
284 #define NEW_ARGLOAD(cfg,dest,num) do { \
285 if (arg_array [(num)]->opcode == OP_ICONST) (dest) = arg_array [(num)]; else { \
286 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
287 (dest)->ssa_op = MONO_SSA_LOAD; \
288 (dest)->inst_i0 = arg_array [(num)]; \
289 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
290 type_to_eval_stack_type (param_types [(num)], (dest)); \
291 (dest)->klass = (dest)->inst_i0->klass; \
294 #define NEW_LOCLOAD(cfg,dest,num) do { \
295 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
296 (dest)->ssa_op = MONO_SSA_LOAD; \
297 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
298 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
299 type_to_eval_stack_type (header->locals [(num)], (dest)); \
300 (dest)->klass = (dest)->inst_i0->klass; \
303 #define NEW_LOCLOADA(cfg,dest,num) do { \
304 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
305 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
306 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
307 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
308 (dest)->opcode = OP_LDADDR; \
309 (dest)->type = STACK_MP; \
310 (dest)->klass = (dest)->inst_i0->klass; \
311 (cfg)->disable_ssa = TRUE; \
314 #define NEW_RETLOADA(cfg,dest) do { \
315 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
316 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
317 (dest)->inst_i0 = (cfg)->ret; \
318 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
319 (dest)->opcode = CEE_LDIND_I; \
320 (dest)->type = STACK_MP; \
321 (dest)->klass = (dest)->inst_i0->klass; \
322 (cfg)->disable_ssa = TRUE; \
325 #define NEW_ARGLOADA(cfg,dest,num) do { \
326 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
327 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
328 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
329 (dest)->inst_i0 = arg_array [(num)]; \
330 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
331 (dest)->opcode = OP_LDADDR; \
332 (dest)->type = STACK_MP; \
333 (dest)->klass = (dest)->inst_i0->klass; \
334 (cfg)->disable_ssa = TRUE; \
337 #define NEW_TEMPLOAD(cfg,dest,num) do { \
338 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
339 (dest)->ssa_op = MONO_SSA_LOAD; \
340 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
341 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
342 type_to_eval_stack_type ((dest)->inst_i0->inst_vtype, (dest)); \
343 (dest)->klass = (dest)->inst_i0->klass; \
346 #define NEW_TEMPLOADA(cfg,dest,num) do { \
347 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
348 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
349 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
350 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
351 (dest)->opcode = OP_LDADDR; \
352 (dest)->type = STACK_MP; \
353 (dest)->klass = (dest)->inst_i0->klass; \
354 (cfg)->disable_ssa = TRUE; \
358 #define NEW_INDLOAD(cfg,dest,addr,vtype) do { \
359 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
360 (dest)->inst_left = addr; \
361 (dest)->opcode = mono_type_to_ldind (vtype); \
362 type_to_eval_stack_type (vtype, (dest)); \
363 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
366 #define NEW_INDSTORE(cfg,dest,addr,value,vtype) do { \
367 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
368 (dest)->inst_i0 = addr; \
369 (dest)->opcode = mono_type_to_stind (vtype); \
370 (dest)->inst_i1 = (value); \
371 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
374 #define NEW_TEMPSTORE(cfg,dest,num,inst) do { \
375 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
376 (dest)->ssa_op = MONO_SSA_STORE; \
377 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
378 (dest)->opcode = mono_type_to_stind ((dest)->inst_i0->inst_vtype); \
379 (dest)->inst_i1 = (inst); \
380 (dest)->klass = (dest)->inst_i0->klass; \
383 #define NEW_LOCSTORE(cfg,dest,num,inst) do { \
384 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
385 (dest)->opcode = mono_type_to_stind (header->locals [(num)]); \
386 (dest)->ssa_op = MONO_SSA_STORE; \
387 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
388 (dest)->inst_i1 = (inst); \
389 (dest)->klass = (dest)->inst_i0->klass; \
392 #define NEW_ARGSTORE(cfg,dest,num,inst) do { \
393 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
394 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
395 (dest)->opcode = mono_type_to_stind (param_types [(num)]); \
396 (dest)->ssa_op = MONO_SSA_STORE; \
397 (dest)->inst_i0 = arg_array [(num)]; \
398 (dest)->inst_i1 = (inst); \
399 (dest)->klass = (dest)->inst_i0->klass; \
402 #define ADD_BINOP(op) do { \
403 MONO_INST_NEW (cfg, ins, (op)); \
404 ins->cil_code = ip; \
406 ins->inst_i0 = sp [0]; \
407 ins->inst_i1 = sp [1]; \
409 type_from_op (ins); \
413 #define ADD_UNOP(op) do { \
414 MONO_INST_NEW (cfg, ins, (op)); \
415 ins->cil_code = ip; \
417 ins->inst_i0 = sp [0]; \
419 type_from_op (ins); \
423 #define ADD_BINCOND(next_block) do { \
425 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
427 cmp->inst_i0 = sp [0]; \
428 cmp->inst_i1 = sp [1]; \
429 cmp->cil_code = ins->cil_code; \
430 type_from_op (cmp); \
432 ins->inst_i0 = cmp; \
433 MONO_ADD_INS (bblock, ins); \
434 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
435 GET_BBLOCK (cfg, bbhash, tblock, target); \
436 link_bblock (cfg, bblock, tblock); \
437 ins->inst_true_bb = tblock; \
438 CHECK_BBLOCK (target, ip, tblock); \
439 if ((next_block)) { \
440 link_bblock (cfg, bblock, (next_block)); \
441 ins->inst_false_bb = (next_block); \
442 start_new_bblock = 1; \
444 GET_BBLOCK (cfg, bbhash, tblock, ip); \
445 link_bblock (cfg, bblock, tblock); \
446 ins->inst_false_bb = tblock; \
447 start_new_bblock = 2; \
451 /* FIXME: handle float, long ... */
452 #define ADD_UNCOND(istrue) do { \
454 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
456 cmp->inst_i0 = sp [0]; \
457 switch (cmp->inst_i0->type) { \
459 cmp->inst_i1 = zero_int64; break; \
461 cmp->inst_i1 = zero_r8; break; \
464 cmp->inst_i1 = zero_ptr; break; \
466 cmp->inst_i1 = zero_obj; break; \
468 cmp->inst_i1 = zero_int32; \
470 cmp->cil_code = ins->cil_code; \
471 type_from_op (cmp); \
473 ins->inst_i0 = cmp; \
474 ins->opcode = (istrue)? CEE_BNE_UN: CEE_BEQ; \
475 MONO_ADD_INS (bblock, ins); \
476 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
477 GET_BBLOCK (cfg, bbhash, tblock, target); \
478 link_bblock (cfg, bblock, tblock); \
479 ins->inst_true_bb = tblock; \
480 CHECK_BBLOCK (target, ip, tblock); \
481 GET_BBLOCK (cfg, bbhash, tblock, ip); \
482 link_bblock (cfg, bblock, tblock); \
483 ins->inst_false_bb = tblock; \
484 start_new_bblock = 2; \
487 #define NEW_LDELEMA(cfg,dest,sp,k) do { \
488 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
489 (dest)->opcode = CEE_LDELEMA; \
490 (dest)->inst_left = (sp) [0]; \
491 (dest)->inst_right = (sp) [1]; \
492 (dest)->type = STACK_MP; \
493 (dest)->klass = (k); \
496 #define NEW_GROUP(cfg,dest,el1,el2) do { \
497 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
498 (dest)->opcode = OP_GROUP; \
499 (dest)->inst_left = (el1); \
500 (dest)->inst_right = (el2); \
503 static GHashTable *coverage_hash = NULL;
506 mono_allocate_coverage_info (MonoMethod *method, int size)
508 MonoCoverageInfo *res;
511 coverage_hash = g_hash_table_new (NULL, NULL);
513 res = g_malloc0 (sizeof (MonoCoverageInfo) + sizeof (int) * size * 2);
517 g_hash_table_insert (coverage_hash, method, res);
523 mono_get_coverage_info (MonoMethod *method)
528 return g_hash_table_lookup (coverage_hash, method);
533 compare_bblock (gconstpointer a, gconstpointer b)
535 const MonoBasicBlock *b1 = a;
536 const MonoBasicBlock *b2 = b;
538 return b2->cil_code - b1->cil_code;
543 * link_bblock: Links two basic blocks
545 * links two basic blocks in the control flow graph, the 'from'
546 * argument is the starting block and the 'to' argument is the block
547 * the control flow ends to after 'from'.
550 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
552 MonoBasicBlock **newa;
556 if (from->cil_code) {
558 g_print ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
560 g_print ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
563 g_print ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
565 g_print ("edge from entry to exit\n");
569 for (i = 0; i < from->out_count; ++i) {
570 if (to == from->out_bb [i]) {
576 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
577 for (i = 0; i < from->out_count; ++i) {
578 newa [i] = from->out_bb [i];
586 for (i = 0; i < to->in_count; ++i) {
587 if (from == to->in_bb [i]) {
593 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
594 for (i = 0; i < to->in_count; ++i) {
595 newa [i] = to->in_bb [i];
604 * We mark each basic block with a region ID. We use that to avoid BB
605 * optimizations when blocks are in different regions.
608 mono_find_block_region (MonoCompile *cfg, int offset, int *filter_lengths)
610 MonoMethod *method = cfg->method;
611 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
612 MonoExceptionClause *clause;
615 /* first search for handlers and filters */
616 for (i = 0; i < header->num_clauses; ++i) {
617 clause = &header->clauses [i];
618 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->token_or_filter) &&
619 (offset < (clause->token_or_filter + filter_lengths [i])))
620 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
622 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
623 if (clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)
624 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
626 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
630 /* search the try blocks */
631 for (i = 0; i < header->num_clauses; ++i) {
632 clause = &header->clauses [i];
633 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
634 return ((i + 1) << 8) | clause->flags;
641 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
643 MonoMethod *method = cfg->method;
644 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
645 MonoExceptionClause *clause;
646 MonoBasicBlock *handler;
650 for (i = 0; i < header->num_clauses; ++i) {
651 clause = &header->clauses [i];
652 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
653 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
654 if (clause->flags == type) {
655 handler = g_hash_table_lookup (cfg->bb_hash, header->code + clause->handler_offset);
657 res = g_list_append (res, handler);
666 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
670 array [*dfn] = start;
671 /*g_print ("visit %d at %p\n", *dfn, start->cil_code);*/
672 for (i = 0; i < start->out_count; ++i) {
673 if (start->out_bb [i]->dfn)
676 start->out_bb [i]->dfn = *dfn;
677 start->out_bb [i]->df_parent = start;
678 array [*dfn] = start->out_bb [i];
679 df_visit (start->out_bb [i], dfn, array);
685 MonoBasicBlock *best;
689 previous_foreach (gconstpointer key, gpointer val, gpointer data)
691 PrevStruct *p = data;
692 MonoBasicBlock *bb = val;
693 //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,
694 //bb->method == p->best->method, bb->cil_code < p->code, bb->cil_code > p->best->cil_code);
696 if (bb->cil_code && bb->cil_code < p->code && bb->cil_code > p->best->cil_code)
700 static MonoBasicBlock*
701 find_previous (GHashTable *bb_hash, MonoBasicBlock *start, const guchar *code) {
707 g_hash_table_foreach (bb_hash, (GHFunc)previous_foreach, &p);
712 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
721 * FIXME: take into account all the details:
722 * second may have been the target of more than one bblock
724 second->out_count = first->out_count;
725 second->out_bb = first->out_bb;
727 for (i = 0; i < first->out_count; ++i) {
728 bb = first->out_bb [i];
729 for (j = 0; j < bb->in_count; ++j) {
730 if (bb->in_bb [j] == first)
731 bb->in_bb [j] = second;
735 first->out_count = 0;
736 first->out_bb = NULL;
737 link_bblock (cfg, first, second);
739 second->last_ins = first->last_ins;
741 /*g_print ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
742 for (inst = first->code; inst && inst->next; inst = inst->next) {
743 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
744 g_print ("found %p: %s", inst->next->cil_code, code);
746 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
747 second->code = inst->next;
749 first->last_ins = inst;
750 second->next_bb = first->next_bb;
751 first->next_bb = second;
756 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
762 mono_type_to_ldind (MonoType *type)
774 case MONO_TYPE_BOOLEAN:
788 case MONO_TYPE_FNPTR:
790 case MONO_TYPE_CLASS:
791 case MONO_TYPE_STRING:
792 case MONO_TYPE_OBJECT:
793 case MONO_TYPE_SZARRAY:
794 case MONO_TYPE_ARRAY:
795 return CEE_LDIND_REF;
803 case MONO_TYPE_VALUETYPE:
804 if (type->data.klass->enumtype) {
805 t = type->data.klass->enum_basetype->type;
809 case MONO_TYPE_TYPEDBYREF:
812 g_error ("unknown type 0x%02x in type_to_ldind", type->type);
818 mono_type_to_stind (MonoType *type)
829 case MONO_TYPE_BOOLEAN:
841 case MONO_TYPE_FNPTR:
843 case MONO_TYPE_CLASS:
844 case MONO_TYPE_STRING:
845 case MONO_TYPE_OBJECT:
846 case MONO_TYPE_SZARRAY:
847 case MONO_TYPE_ARRAY:
848 return CEE_STIND_REF;
856 case MONO_TYPE_VALUETYPE:
857 if (type->data.klass->enumtype) {
858 t = type->data.klass->enum_basetype->type;
862 case MONO_TYPE_TYPEDBYREF:
865 g_error ("unknown type 0x%02x in type_to_stind", type->type);
871 * Returns the type used in the eval stack when @type is loaded.
872 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
875 type_to_eval_stack_type (MonoType *type, MonoInst *inst) {
879 inst->type = STACK_MP;
887 case MONO_TYPE_BOOLEAN:
893 inst->type = STACK_I4;
898 case MONO_TYPE_FNPTR:
899 inst->type = STACK_PTR;
901 case MONO_TYPE_CLASS:
902 case MONO_TYPE_STRING:
903 case MONO_TYPE_OBJECT:
904 case MONO_TYPE_SZARRAY:
905 case MONO_TYPE_ARRAY:
906 inst->type = STACK_OBJ;
910 inst->type = STACK_I8;
914 inst->type = STACK_R8;
916 case MONO_TYPE_VALUETYPE:
917 if (type->data.klass->enumtype) {
918 t = type->data.klass->enum_basetype->type;
921 inst->klass = type->data.klass;
922 inst->type = STACK_VTYPE;
925 case MONO_TYPE_TYPEDBYREF:
926 inst->klass = mono_defaults.typed_reference_class;
927 inst->type = STACK_VTYPE;
930 g_error ("unknown type 0x%02x in eval stack type", type->type);
935 * The following tables are used to quickly validate the IL code in type_from_op ().
938 bin_num_table [STACK_MAX] [STACK_MAX] = {
939 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
940 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
941 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
942 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
943 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
944 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
945 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
946 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
951 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
954 /* reduce the size of this table */
956 bin_int_table [STACK_MAX] [STACK_MAX] = {
957 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
958 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
959 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
960 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, 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},
963 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
964 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
968 bin_comp_table [STACK_MAX] [STACK_MAX] = {
970 {0, 1, 0, 1, 0, 0, 4, 0},
971 {0, 0, 1, 0, 0, 0, 0, 0},
972 {0, 1, 0, 1, 0, 2, 4, 0},
973 {0, 0, 0, 0, 1, 0, 0, 0},
974 {0, 0, 0, 2, 0, 1, 0, 0},
975 {0, 4, 0, 4, 0, 0, 3, 0},
976 {0, 0, 0, 0, 0, 0, 0, 0},
979 /* reduce the size of this table */
981 shift_table [STACK_MAX] [STACK_MAX] = {
982 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
983 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
984 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
985 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
986 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
987 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
988 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
989 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
993 * Tables to map from the non-specific opcode to the matching
994 * type-specific opcode.
996 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
998 binops_op_map [STACK_MAX] = {
999 0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, 0
1002 /* handles from CEE_NEG to CEE_CONV_U8 */
1003 static const guint16
1004 unops_op_map [STACK_MAX] = {
1005 0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, 0
1008 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
1009 static const guint16
1010 ovfops_op_map [STACK_MAX] = {
1011 0, 0, OP_LCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_FCONV_TO_U2-CEE_CONV_U2, 0
1014 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
1015 static const guint16
1016 ovf2ops_op_map [STACK_MAX] = {
1017 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
1020 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
1021 static const guint16
1022 ovf3ops_op_map [STACK_MAX] = {
1023 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
1026 /* handles from CEE_CEQ to CEE_CLT_UN */
1027 static const guint16
1028 ceqops_op_map [STACK_MAX] = {
1029 0, 0, OP_LCEQ-CEE_CEQ, OP_PCEQ-CEE_CEQ, OP_FCEQ-CEE_CEQ, 0
1033 * Sets ins->type (the type on the eval stack) according to the
1034 * type of the opcode and the arguments to it.
1035 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1037 * FIXME: this function sets ins->type unconditionally in some cases, but
1038 * it should set it to invalid for some types (a conv.x on an object)
1041 type_from_op (MonoInst *ins) {
1042 switch (ins->opcode) {
1049 /* FIXME: check unverifiable args for STACK_MP */
1050 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1051 ins->opcode += binops_op_map [ins->type];
1058 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1059 ins->opcode += binops_op_map [ins->type];
1064 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1065 ins->opcode += binops_op_map [ins->type];
1068 /* FIXME: handle some specifics with ins->next->type */
1069 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1073 case 256+CEE_CGT_UN:
1075 case 256+CEE_CLT_UN:
1076 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1077 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1081 ins->type = neg_table [ins->inst_i0->type];
1082 ins->opcode += unops_op_map [ins->type];
1085 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1086 ins->type = ins->inst_i0->type;
1088 ins->type = STACK_INV;
1089 ins->opcode += unops_op_map [ins->type];
1095 ins->type = STACK_I4;
1096 ins->opcode += unops_op_map [ins->inst_i0->type];
1099 ins->type = STACK_R8;
1100 switch (ins->inst_i0->type) {
1105 ins->opcode = OP_LCONV_TO_R_UN;
1109 case CEE_CONV_OVF_I1:
1110 case CEE_CONV_OVF_U1:
1111 case CEE_CONV_OVF_I2:
1112 case CEE_CONV_OVF_U2:
1113 case CEE_CONV_OVF_I4:
1114 case CEE_CONV_OVF_U4:
1115 ins->type = STACK_I4;
1116 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1118 case CEE_CONV_OVF_I_UN:
1119 case CEE_CONV_OVF_U_UN:
1120 ins->type = STACK_PTR;
1121 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1123 case CEE_CONV_OVF_I1_UN:
1124 case CEE_CONV_OVF_I2_UN:
1125 case CEE_CONV_OVF_I4_UN:
1126 case CEE_CONV_OVF_U1_UN:
1127 case CEE_CONV_OVF_U2_UN:
1128 case CEE_CONV_OVF_U4_UN:
1129 ins->type = STACK_I4;
1130 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1133 ins->type = STACK_PTR;
1134 switch (ins->inst_i0->type) {
1140 ins->opcode = OP_LCONV_TO_U;
1143 ins->opcode = OP_FCONV_TO_U;
1149 ins->type = STACK_I8;
1150 ins->opcode += unops_op_map [ins->inst_i0->type];
1152 case CEE_CONV_OVF_I8:
1153 case CEE_CONV_OVF_U8:
1154 ins->type = STACK_I8;
1155 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1157 case CEE_CONV_OVF_U8_UN:
1158 case CEE_CONV_OVF_I8_UN:
1159 ins->type = STACK_I8;
1160 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1164 ins->type = STACK_R8;
1165 ins->opcode += unops_op_map [ins->inst_i0->type];
1168 ins->type = STACK_R8;
1172 ins->type = STACK_I4;
1173 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1176 case CEE_CONV_OVF_I:
1177 case CEE_CONV_OVF_U:
1178 ins->type = STACK_PTR;
1179 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1182 case CEE_ADD_OVF_UN:
1184 case CEE_MUL_OVF_UN:
1186 case CEE_SUB_OVF_UN:
1187 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1188 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1191 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1198 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_MP, STACK_R8, STACK_R8, STACK_OBJ
1201 /* map ldelem.x to the matching ldind.x opcode */
1203 ldelem_to_ldind [] = {
1217 /* map stelem.x to the matching stind.x opcode */
1219 stelem_to_stind [] = {
1233 param_table [STACK_MAX] [STACK_MAX] = {
1238 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1242 switch (args->type) {
1252 for (i = 0; i < sig->param_count; ++i) {
1253 switch (args [i].type) {
1257 if (!sig->params [i]->byref)
1261 if (sig->params [i]->byref)
1263 switch (sig->params [i]->type) {
1264 case MONO_TYPE_CLASS:
1265 case MONO_TYPE_STRING:
1266 case MONO_TYPE_OBJECT:
1267 case MONO_TYPE_SZARRAY:
1268 case MONO_TYPE_ARRAY:
1275 if (sig->params [i]->byref)
1277 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1286 /*if (!param_table [args [i].type] [sig->params [i]->type])
1294 * When we need a pointer to the current domain many times in a method, we
1295 * call mono_domain_get() once and we store the result in a local variable.
1296 * This function returns the variable that represents the MonoDomain*.
1298 inline static MonoInst *
1299 mono_get_domainvar (MonoCompile *cfg)
1301 if (!cfg->domainvar)
1302 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1303 return cfg->domainvar;
1307 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
1310 int num = cfg->num_varinfo;
1312 if ((num + 1) >= cfg->varinfo_count) {
1313 cfg->varinfo_count = (cfg->varinfo_count + 2) * 2;
1314 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
1315 cfg->vars = (MonoMethodVar **)g_realloc (cfg->vars, sizeof (MonoMethodVar*) * cfg->varinfo_count);
1318 mono_jit_stats.allocate_var++;
1320 MONO_INST_NEW (cfg, inst, opcode);
1321 inst->inst_c0 = num;
1322 inst->inst_vtype = type;
1323 inst->klass = mono_class_from_mono_type (type);
1324 /* if set to 1 the variable is native */
1327 cfg->varinfo [num] = inst;
1329 cfg->vars [num] = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMethodVar));
1330 MONO_INIT_VARINFO (cfg->vars [num], num);
1333 //g_print ("created temp %d of type %s\n", num, mono_type_get_name (type));
1338 type_from_stack_type (MonoInst *ins) {
1339 switch (ins->type) {
1340 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1341 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1342 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1343 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1344 case STACK_MP: return &mono_defaults.int_class->byval_arg;
1345 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1346 case STACK_VTYPE: return &ins->klass->byval_arg;
1348 g_error ("stack type %d to montype not handled\n", ins->type);
1354 array_access_to_klass (int opcode)
1358 return mono_defaults.byte_class;
1360 return mono_defaults.uint16_class;
1363 return mono_defaults.int_class;
1366 return mono_defaults.sbyte_class;
1369 return mono_defaults.int16_class;
1372 return mono_defaults.int32_class;
1374 return mono_defaults.uint32_class;
1377 return mono_defaults.int64_class;
1380 return mono_defaults.single_class;
1383 return mono_defaults.double_class;
1384 case CEE_LDELEM_REF:
1385 case CEE_STELEM_REF:
1386 return mono_defaults.object_class;
1388 g_assert_not_reached ();
1394 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
1398 MONO_ADD_INS (bb, inst);
1401 switch (bb->last_ins->opcode) {
1414 while (prev->next && prev->next != bb->last_ins)
1416 if (prev == bb->code) {
1417 if (bb->last_ins == bb->code) {
1418 inst->next = bb->code;
1421 inst->next = prev->next;
1425 inst->next = bb->last_ins;
1429 // g_warning ("handle conditional jump in add_ins_to_end ()\n");
1431 MONO_ADD_INS (bb, inst);
1437 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
1439 MonoInst *inst, *load;
1441 NEW_TEMPLOAD (cfg, load, src);
1443 NEW_TEMPSTORE (cfg, inst, dest, load);
1444 if (inst->opcode == CEE_STOBJ) {
1445 NEW_TEMPLOADA (cfg, inst, dest);
1446 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE);
1448 inst->cil_code = NULL;
1449 mono_add_ins_to_end (bb, inst);
1454 * We try to share variables when possible
1457 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1462 /* inlining can result in deeper stacks */
1463 if (slot >= ((MonoMethodNormal *)cfg->method)->header->max_stack)
1464 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1466 pos = ins->type - 1 + slot * STACK_MAX;
1468 switch (ins->type) {
1475 if ((vnum = cfg->intvars [pos]))
1476 return cfg->varinfo [vnum];
1477 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1478 cfg->intvars [pos] = res->inst_c0;
1481 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1487 * This function is called to handle items that are left on the evaluation stack
1488 * at basic block boundaries. What happens is that we save the values to local variables
1489 * and we reload them later when first entering the target basic block (with the
1490 * handle_loaded_temps () function).
1491 * A single joint point will use the same variables (stored in the array bb->out_stack or
1492 * bb->in_stack, if the basic block is before or after the joint point).
1495 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count) {
1497 MonoBasicBlock *outb;
1498 MonoInst *inst, **locals;
1502 if (cfg->verbose_level > 3)
1503 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
1504 if (!bb->out_scount) {
1506 bb->out_scount = count;
1507 //g_print ("bblock %d has out:", bb->block_num);
1508 for (i = 0; i < bb->out_count; ++i) {
1509 outb = bb->out_bb [i];
1510 //g_print (" %d", outb->block_num);
1511 if (outb->in_stack) {
1513 bb->out_stack = outb->in_stack;
1519 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1520 for (i = 0; i < count; ++i) {
1522 /* try to reuse temps already allocated for this purpouse, if they occupy the same
1523 * stack slot and if they are of the same type. */
1524 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1526 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1531 locals = bb->out_stack;
1532 for (i = 0; i < count; ++i) {
1533 /* add store ops at the end of the bb, before the branch */
1534 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1535 if (inst->opcode == CEE_STOBJ) {
1536 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
1537 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE);
1539 inst->cil_code = sp [i]->cil_code;
1540 mono_add_ins_to_end (bb, inst);
1542 if (cfg->verbose_level > 3)
1543 g_print ("storing %d to temp %d\n", i, locals [i]->inst_c0);
1546 for (i = 0; i < bb->out_count; ++i) {
1547 outb = bb->out_bb [i];
1548 if (outb->in_scount)
1549 continue; /* check they are the same locals */
1550 outb->in_scount = count;
1551 outb->in_stack = locals;
1557 ret_type_to_call_opcode (MonoType *type, int calli, int virt)
1562 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1566 case MONO_TYPE_VOID:
1567 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
1570 case MONO_TYPE_BOOLEAN:
1573 case MONO_TYPE_CHAR:
1576 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1580 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1581 case MONO_TYPE_CLASS:
1582 case MONO_TYPE_STRING:
1583 case MONO_TYPE_OBJECT:
1584 case MONO_TYPE_SZARRAY:
1585 case MONO_TYPE_ARRAY:
1586 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1589 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
1592 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
1593 case MONO_TYPE_VALUETYPE:
1594 if (type->data.klass->enumtype) {
1595 t = type->data.klass->enum_basetype->type;
1598 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1599 case MONO_TYPE_TYPEDBYREF:
1600 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1602 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
1608 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
1610 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
1612 ji->ip.label = label;
1613 ji->type = MONO_PATCH_INFO_SWITCH;
1614 ji->data.table = bbs;
1615 ji->next = cfg->patch_info;
1616 ji->table_size = num_blocks;
1617 cfg->patch_info = ji;
1621 * When we add a tree of instructions, we need to ensure the instructions currently
1622 * on the stack are executed before (like, if we load a value from a local).
1623 * We ensure this by saving the currently loaded values to temps and rewriting the
1624 * instructions to load the values.
1625 * This is not done for opcodes that terminate a basic block (because it's handled already
1626 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
1629 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
1631 MonoInst *load, *store, *temp, *ins;
1633 while (stack < sp) {
1635 /* handle also other constants */
1636 if (ins->opcode != OP_ICONST) {
1637 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1638 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1639 store->cil_code = ins->cil_code;
1640 if (store->opcode == CEE_STOBJ) {
1641 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
1642 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE);
1644 MONO_ADD_INS (bblock, store);
1645 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1646 load->cil_code = ins->cil_code;
1654 * Prepare arguments for passing to a function call.
1655 * Return a non-zero value if the arguments can't be passed to the given
1657 * The type checks are not yet complete and some conversions may need
1658 * casts on 32 or 64 bit architectures.
1661 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
1666 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
1670 for (i = 0; i < sig->param_count; ++i) {
1671 if (sig->params [i]->byref) {
1673 * check the result of ldelema is only passed as an argument if the byref
1674 * type matches exactly the array element type.
1675 * FIXME: if the argument as been saved on the stack as part of the
1676 * interface variable code (the value was on the stack at a basic block boundary)
1677 * we need to add the check in that case, too.
1679 if (args [i]->opcode == CEE_LDELEMA) {
1681 MonoClass *exact_class = mono_class_from_mono_type (sig->params [i]);
1682 if (!exact_class->valuetype) {
1683 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
1684 check->cil_code = args [i]->cil_code;
1685 check->klass = exact_class;
1686 check->inst_left = args [i]->inst_left;
1687 check->type = STACK_OBJ;
1688 args [i]->inst_left = check;
1691 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
1695 simple_type = sig->params [i]->type;
1697 switch (simple_type) {
1698 case MONO_TYPE_VOID:
1703 case MONO_TYPE_BOOLEAN:
1706 case MONO_TYPE_CHAR:
1709 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
1715 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
1718 case MONO_TYPE_CLASS:
1719 case MONO_TYPE_STRING:
1720 case MONO_TYPE_OBJECT:
1721 case MONO_TYPE_SZARRAY:
1722 case MONO_TYPE_ARRAY:
1723 if (args [i]->type != STACK_OBJ)
1728 if (args [i]->type != STACK_I8)
1733 if (args [i]->type != STACK_R8)
1736 case MONO_TYPE_VALUETYPE:
1737 if (sig->params [i]->data.klass->enumtype) {
1738 simple_type = sig->params [i]->data.klass->enum_basetype->type;
1741 if (args [i]->type != STACK_VTYPE)
1744 case MONO_TYPE_TYPEDBYREF:
1745 if (args [i]->type != STACK_VTYPE)
1749 g_error ("unknown type 0x%02x in check_call_signature", simple_type);
1756 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
1757 const guint8 *ip, gboolean to_end)
1759 MonoInst *temp, *store, *ins = (MonoInst*)call;
1760 MonoType *ret = sig->ret;
1762 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
1764 call->inst.type = STACK_OBJ;
1765 call->inst.opcode = CEE_CALL;
1766 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
1768 type_to_eval_stack_type (ret, ins);
1769 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
1772 if (MONO_TYPE_ISSTRUCT (ret)) {
1775 /* we use this to allocate native sized structs */
1776 temp->unused = sig->pinvoke;
1778 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
1779 if (call->inst.opcode == OP_VCALL)
1780 ins->inst_left = loada;
1782 ins->inst_right = loada; /* a virtual or indirect call */
1785 mono_add_ins_to_end (bblock, ins);
1787 MONO_ADD_INS (bblock, ins);
1789 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1790 store->cil_code = ip;
1792 mono_add_ins_to_end (bblock, store);
1794 MONO_ADD_INS (bblock, store);
1796 return temp->inst_c0;
1799 mono_add_ins_to_end (bblock, ins);
1801 MONO_ADD_INS (bblock, ins);
1806 inline static MonoCallInst *
1807 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1808 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
1813 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual));
1815 call->inst.cil_code = ip;
1817 call->signature = sig;
1818 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
1820 for (i = 0; i < (sig->param_count + sig->hasthis); ++i) {
1821 if (call->args [i]) {
1822 if (!call->args [i]->cil_code)
1823 call->args [i]->cil_code = ip;
1825 mono_add_ins_to_end (bblock, call->args [i]);
1827 MONO_ADD_INS (bblock, call->args [i]);
1834 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1835 MonoInst **args, MonoInst *addr, const guint8 *ip)
1837 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
1839 call->inst.inst_i0 = addr;
1841 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
1844 static MonoCallInst*
1845 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
1846 MonoInst **args, const guint8 *ip, MonoInst *this)
1848 gboolean virtual = this != NULL;
1851 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, FALSE);
1853 if (this && sig->hasthis &&
1854 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
1855 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
1856 call->method = mono_marshal_get_remoting_invoke_with_check (method);
1858 call->method = method;
1860 call->inst.flags |= MONO_INST_HAS_METHOD;
1861 call->inst.inst_left = this;
1867 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
1868 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
1870 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, signature, args, ip, this);
1872 return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
1876 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
1877 MonoInst **args, const guint8 *ip, gboolean to_end)
1883 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
1885 return mono_spill_call (cfg, bblock, call, sig, func == mono_array_new_va, ip, to_end);
1889 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
1891 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
1894 g_warning ("unregistered JIT ICall");
1895 g_assert_not_reached ();
1898 return mono_emit_native_call (cfg, bblock, info->wrapper, info->sig, args, ip, FALSE);
1902 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
1904 MonoInst *ins, *temp = NULL, *store, *load;
1908 /*g_print ("emulating: ");
1909 mono_print_tree (tree);
1911 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE));
1912 ins = (MonoInst*)call;
1914 call->inst.cil_code = tree->cil_code;
1916 call->signature = info->sig;
1918 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
1920 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
1921 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
1922 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1923 store->cil_code = tree->cil_code;
1928 nargs = info->sig->param_count + info->sig->hasthis;
1930 for (i = 1; i < nargs; i++) {
1931 call->args [i - 1]->next = call->args [i];
1935 call->args [nargs - 1]->next = store;
1937 if (cfg->prev_ins) {
1938 store->next = cfg->prev_ins->next;
1940 cfg->prev_ins->next = call->args [0];
1942 cfg->prev_ins->next = store;
1944 store->next = cfg->cbb->code;
1946 cfg->cbb->code = call->args [0];
1948 cfg->cbb->code = store;
1952 call->fptr = info->wrapper;
1954 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
1955 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1960 static MonoMethodSignature *
1961 mono_get_element_address_signature (int arity)
1963 static GHashTable *sighash = NULL;
1964 MonoMethodSignature *res;
1968 sighash = g_hash_table_new (NULL, NULL);
1971 if ((res = g_hash_table_lookup (sighash, (gpointer)arity)))
1974 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
1976 res->params [0] = &mono_defaults.array_class->byval_arg;
1978 for (i = 1; i <= arity; i++)
1979 res->params [i] = &mono_defaults.int_class->byval_arg;
1981 res->ret = &mono_defaults.int_class->byval_arg;
1983 g_hash_table_insert (sighash, (gpointer)arity, res);
1989 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native) {
1990 MonoInst *iargs [3];
1996 * This check breaks with spilled vars... need to handle it during verification anyway.
1997 * g_assert (klass && klass == src->klass && klass == dest->klass);
2001 n = mono_class_native_size (klass, &align);
2003 n = mono_class_value_size (klass, &align);
2005 if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
2007 MONO_INST_NEW (cfg, inst, OP_MEMCPY);
2008 inst->inst_left = dest;
2009 inst->inst_right = src;
2010 inst->cil_code = ip;
2012 MONO_ADD_INS (bblock, inst);
2017 NEW_ICONST (cfg, iargs [2], n);
2019 mono_emit_native_call (cfg, bblock, helper_memcpy, helper_sig_memcpy, iargs, ip, to_end);
2023 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
2025 MonoInst *iargs [2];
2026 MonoInst *ins, *zero_int32;
2029 NEW_ICONST (cfg, zero_int32, 0);
2031 mono_class_init (klass);
2032 n = mono_class_value_size (klass, NULL);
2033 MONO_INST_NEW (cfg, ins, 0);
2035 ins->inst_left = dest;
2036 ins->inst_right = zero_int32;
2039 ins->opcode = CEE_STIND_I1;
2040 MONO_ADD_INS (bblock, ins);
2043 ins->opcode = CEE_STIND_I2;
2044 MONO_ADD_INS (bblock, ins);
2047 ins->opcode = CEE_STIND_I4;
2048 MONO_ADD_INS (bblock, ins);
2051 handle_loaded_temps (cfg, bblock, stack_start, sp);
2052 NEW_ICONST (cfg, ins, n);
2055 mono_emit_jit_icall (cfg, bblock, helper_initobj, iargs, ip);
2060 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
2063 needs_cctor_run (MonoClass *klass, MonoMethod *caller)
2068 for (i = 0; i < klass->method.count; ++i) {
2069 method = klass->methods [i];
2070 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
2071 (strcmp (".cctor", method->name) == 0)) {
2072 if (caller == method)
2081 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
2083 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
2084 MonoMethodSignature *signature = method->signature;
2088 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2089 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2090 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
2091 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
2092 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2093 (method->klass->marshalbyref) ||
2094 !header || header->num_clauses ||
2095 /* fixme: why cant we inline valuetype returns? */
2096 MONO_TYPE_ISSTRUCT (signature->ret))
2099 /* its not worth to inline methods with valuetype arguments?? */
2100 for (i = 0; i < signature->param_count; i++) {
2101 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
2107 * if we can initialize the class of the method right away, we do,
2108 * otherwise we don't allow inlining if the class needs initialization,
2109 * since it would mean inserting a call to mono_runtime_class_init()
2110 * inside the inlined code
2112 if (!(cfg->opt & MONO_OPT_SHARED)) {
2113 vtable = mono_class_vtable (cfg->domain, method->klass);
2114 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)
2115 mono_runtime_class_init (vtable);
2116 else if (!vtable->initialized && needs_cctor_run (method->klass, NULL))
2120 * If we're compiling for shared code
2121 * the cctor will need to be run at aot method load time, for example,
2122 * or at the end of the compilation of the inlining method.
2124 if (needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
2127 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
2129 /* also consider num_locals? */
2130 if (header->code_size < 20)
2137 mini_get_ldelema_ins (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
2141 MonoMethodSignature *esig;
2143 rank = cmethod->signature->param_count - (is_set? 1: 0);
2145 * FIXME: handle TypeMismatch for set or use the slow path
2148 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
2150 NEW_GROUP (cfg, indexes, sp [1], sp [2]);
2151 MONO_INST_NEW (cfg, addr, OP_LDELEMA2D);
2152 addr->inst_left = sp [0];
2153 addr->inst_right = indexes;
2154 addr->cil_code = ip;
2155 addr->type = STACK_MP;
2156 addr->klass = cmethod->klass;
2159 esig = mono_get_element_address_signature (rank);
2160 temp = mono_emit_native_call (cfg, bblock, ves_array_element_address, esig, sp, ip, FALSE);
2161 NEW_TEMPLOAD (cfg, addr, temp);
2166 mini_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
2171 if (cmethod->klass == mono_defaults.string_class) {
2172 if (cmethod->name [0] != 'g' || strcmp (cmethod->name, "get_Chars"))
2175 } else if (cmethod->klass == mono_defaults.math_class) {
2176 if (strcmp (cmethod->name, "Sin") == 0)
2178 else if (strcmp (cmethod->name, "Cos") == 0)
2180 else if (strcmp (cmethod->name, "Tan") == 0)
2182 else if (strcmp (cmethod->name, "Atan") == 0)
2184 else if (strcmp (cmethod->name, "Sqrt") == 0)
2186 else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8)
2189 /* OP_FREM is not IEEE compatible */
2190 else if (strcmp (cmethod->name, "IEEERemainder") == 0)
2198 pc = fsig->param_count + fsig->hasthis;
2199 MONO_INST_NEW (cfg, ins, op);
2202 ins->inst_i0 = args [0];
2204 ins->inst_i1 = args [1];
2211 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
2213 MonoInst *store, *temp;
2216 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
2218 if (!sig->hasthis && sig->param_count == 0)
2222 if (sp [0]->opcode == OP_ICONST) {
2225 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
2227 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2228 store->cil_code = sp [0]->cil_code;
2229 MONO_ADD_INS (bblock, store);
2234 for (i = 0; i < sig->param_count; ++i) {
2235 if (sp [0]->opcode == OP_ICONST) {
2238 temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
2240 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2241 store->cil_code = sp [0]->cil_code;
2242 if (store->opcode == CEE_STOBJ) {
2243 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2244 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE);
2246 MONO_ADD_INS (bblock, store);
2254 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
2255 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b)
2257 MonoInst *ins, *rvar = NULL;
2258 MonoMethodHeader *cheader;
2259 MonoBasicBlock *ebblock, *sbblock;
2260 int i, costs, new_locals_offset;
2262 if (cfg->verbose_level > 2)
2263 g_print ("INLINE START %p %s\n", cmethod, mono_method_full_name (cmethod, TRUE));
2265 cheader = ((MonoMethodNormal *)cmethod)->header;
2267 if (!cmethod->inline_info) {
2268 mono_jit_stats.inlineable_methods++;
2269 cmethod->inline_info = 1;
2271 /* allocate space to store the return value */
2272 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2273 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2276 /* allocate local variables */
2277 new_locals_offset = cfg->num_varinfo;
2278 for (i = 0; i < cheader->num_locals; ++i)
2279 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
2281 /* allocate starte and end blocks */
2282 sbblock = NEW_BBLOCK (cfg);
2283 sbblock->block_num = cfg->num_bblocks++;
2284 sbblock->real_offset = real_offset;
2286 ebblock = NEW_BBLOCK (cfg);
2287 ebblock->block_num = cfg->num_bblocks++;
2288 ebblock->real_offset = real_offset;
2290 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
2292 if (costs >= 0 && costs < 60) {
2293 if (cfg->verbose_level > 2)
2294 g_print ("INLINE END %s\n", mono_method_full_name (cmethod, TRUE));
2296 mono_jit_stats.inlined_methods++;
2298 /* always add some code to avoid block split failures */
2299 MONO_INST_NEW (cfg, ins, CEE_NOP);
2300 MONO_ADD_INS (bblock, ins);
2303 bblock->next_bb = sbblock;
2304 link_bblock (cfg, bblock, sbblock);
2307 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
2313 if (cfg->verbose_level > 2)
2314 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
2320 * Some of these comments may well be out-of-date.
2321 * Design decisions: we do a single pass over the IL code (and we do bblock
2322 * splitting/merging in the few cases when it's required: a back jump to an IL
2323 * address that was not already seen as bblock starting point).
2324 * Code is validated as we go (full verification is still better left to metadata/verify.c).
2325 * Complex operations are decomposed in simpler ones right away. We need to let the
2326 * arch-specific code peek and poke inside this process somehow (except when the
2327 * optimizations can take advantage of the full semantic info of coarse opcodes).
2328 * All the opcodes of the form opcode.s are 'normalized' to opcode.
2329 * MonoInst->opcode initially is the IL opcode or some simplification of that
2330 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
2331 * opcode with value bigger than OP_LAST.
2332 * At this point the IR can be handed over to an interpreter, a dumb code generator
2333 * or to the optimizing code generator that will translate it to SSA form.
2335 * Profiling directed optimizations.
2336 * We may compile by default with few or no optimizations and instrument the code
2337 * or the user may indicate what methods to optimize the most either in a config file
2338 * or through repeated runs where the compiler applies offline the optimizations to
2339 * each method and then decides if it was worth it.
2342 * * consider using an array instead of an hash table (bb_hash)
2345 #define CHECK_TYPE(ins) if (!(ins)->type) goto unverified
2346 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) goto unverified
2347 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) goto unverified
2349 /* offset from br.s -> br like opcodes */
2350 #define BIG_BRANCH_OFFSET 13
2353 get_basic_blocks (MonoCompile *cfg, GHashTable *bbhash, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end)
2355 unsigned char *ip = start;
2356 unsigned char *target;
2359 MonoBasicBlock *bblock;
2360 const MonoOpcode *opcode;
2363 cli_addr = ip - start;
2364 i = mono_opcode_value ((const guint8 **)&ip);
2365 opcode = &mono_opcodes [i];
2366 switch (opcode->argument) {
2367 case MonoInlineNone:
2370 case MonoInlineString:
2371 case MonoInlineType:
2372 case MonoInlineField:
2373 case MonoInlineMethod:
2376 case MonoShortInlineR:
2383 case MonoShortInlineVar:
2384 case MonoShortInlineI:
2387 case MonoShortInlineBrTarget:
2388 target = start + cli_addr + 2 + (signed char)ip [1];
2389 GET_BBLOCK (cfg, bbhash, bblock, target);
2392 case MonoInlineBrTarget:
2393 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
2394 GET_BBLOCK (cfg, bbhash, bblock, target);
2397 case MonoInlineSwitch: {
2398 guint32 n = read32 (ip + 1);
2401 cli_addr += 5 + 4 * n;
2402 target = start + cli_addr;
2403 GET_BBLOCK (cfg, bbhash, bblock, target);
2405 for (j = 0; j < n; ++j) {
2406 target = start + cli_addr + (gint32)read32 (ip);
2407 GET_BBLOCK (cfg, bbhash, bblock, target);
2417 g_assert_not_reached ();
2426 * mono_method_to_ir: translates IL into basic blocks containing trees
2429 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
2430 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
2431 guint inline_offset, gboolean is_virtual_call)
2433 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
2434 MonoInst *ins, **sp, **stack_start;
2435 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
2437 MonoMethod *cmethod;
2438 MonoInst **arg_array;
2439 MonoMethodHeader *header;
2441 guint32 token, ins_flag;
2443 unsigned char *ip, *end, *target;
2444 static double r8_0 = 0.0;
2445 MonoMethodSignature *sig;
2446 MonoType **param_types;
2447 GList *bb_recheck = NULL, *tmp;
2448 int i, n, start_new_bblock, align;
2449 int num_calls = 0, inline_costs = 0;
2450 int *filter_lengths = NULL;
2451 int breakpoint_id = 0;
2454 image = method->klass->image;
2455 header = ((MonoMethodNormal *)method)->header;
2456 sig = method->signature;
2457 ip = (unsigned char*)header->code;
2458 end = ip + header->code_size;
2459 mono_jit_stats.cil_code_size += header->code_size;
2461 if (cfg->method == method) {
2463 bbhash = cfg->bb_hash;
2465 real_offset = inline_offset;
2466 bbhash = g_hash_table_new (g_direct_hash, NULL);
2469 dont_inline = g_list_prepend (dont_inline, method);
2470 if (cfg->method == method) {
2473 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
2474 start_bblock->cil_code = NULL;
2475 start_bblock->cil_length = 0;
2476 start_bblock->block_num = cfg->num_bblocks++;
2479 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
2480 end_bblock->cil_code = NULL;
2481 end_bblock->cil_length = 0;
2482 end_bblock->block_num = cfg->num_bblocks++;
2483 g_assert (cfg->num_bblocks == 2);
2485 arg_array = alloca (sizeof (MonoInst *) * (sig->hasthis + sig->param_count));
2486 for (i = sig->hasthis + sig->param_count - 1; i >= 0; i--)
2487 arg_array [i] = cfg->varinfo [i];
2489 if (mono_compile_aot)
2490 cfg->opt |= MONO_OPT_SHARED;
2492 if (header->num_clauses) {
2493 int size = sizeof (int) * header->num_clauses;
2494 filter_lengths = alloca (size);
2495 memset (filter_lengths, 0, size);
2497 cfg->spvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2498 /* prevent it from being register allocated */
2499 cfg->spvar->flags |= MONO_INST_INDIRECT;
2501 /* handle exception clauses */
2502 for (i = 0; i < header->num_clauses; ++i) {
2503 //unsigned char *p = ip;
2504 MonoExceptionClause *clause = &header->clauses [i];
2505 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->try_offset);
2506 tblock->real_offset = clause->try_offset;
2507 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
2508 tblock->real_offset = clause->handler_offset;
2510 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
2511 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2512 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2513 MONO_ADD_INS (tblock, ins);
2516 /*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);
2518 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
2520 /* catch and filter blocks get the exception object on the stack */
2521 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
2522 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2523 /* mostly like handle_stack_args (), but just sets the input args */
2524 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
2526 cfg->exvar = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
2527 /* prevent it from being register allocated */
2528 cfg->exvar->flags |= MONO_INST_INDIRECT;
2530 tblock->in_scount = 1;
2531 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2532 tblock->in_stack [0] = cfg->exvar;
2534 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2535 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->token_or_filter);
2536 tblock->real_offset = clause->token_or_filter;
2537 tblock->in_scount = 1;
2538 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2539 tblock->in_stack [0] = cfg->exvar;
2540 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2541 MONO_ADD_INS (tblock, ins);
2547 arg_array = alloca (sizeof (MonoInst *) * (sig->hasthis + sig->param_count));
2548 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
2551 /* FIRST CODE BLOCK */
2552 bblock = NEW_BBLOCK (cfg);
2553 bblock->cil_code = ip;
2555 ADD_BBLOCK (cfg, bbhash, bblock);
2557 if (cfg->method == method) {
2558 breakpoint_id = mono_debugger_method_has_breakpoint (method);
2559 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
2560 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2561 MONO_ADD_INS (bblock, ins);
2565 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED)))) {
2566 /* we use a separate basic block for the initialization code */
2567 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
2568 init_localsbb->real_offset = real_offset;
2569 start_bblock->next_bb = init_localsbb;
2570 init_localsbb->next_bb = bblock;
2571 link_bblock (cfg, start_bblock, init_localsbb);
2572 link_bblock (cfg, init_localsbb, bblock);
2573 init_localsbb->block_num = cfg->num_bblocks++;
2575 start_bblock->next_bb = bblock;
2576 link_bblock (cfg, start_bblock, bblock);
2579 if (get_basic_blocks (cfg, bbhash, header, real_offset, ip, end))
2582 mono_debug_init_method (cfg, bblock, breakpoint_id);
2584 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * (sig->hasthis + sig->param_count));
2586 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
2587 for (n = 0; n < sig->param_count; ++n)
2588 param_types [n + sig->hasthis] = sig->params [n];
2590 /* do this somewhere outside - not here */
2591 NEW_ICONST (cfg, zero_int32, 0);
2592 NEW_ICONST (cfg, zero_int64, 0);
2593 zero_int64->type = STACK_I8;
2594 NEW_PCONST (cfg, zero_ptr, 0);
2595 NEW_PCONST (cfg, zero_obj, 0);
2596 zero_obj->type = STACK_OBJ;
2598 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
2599 zero_r8->type = STACK_R8;
2600 zero_r8->inst_p0 = &r8_0;
2602 /* add a check for this != NULL to inlined methods */
2603 if (is_virtual_call) {
2604 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
2605 NEW_ARGLOAD (cfg, ins->inst_left, 0);
2607 MONO_ADD_INS (bblock, ins);
2610 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
2611 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
2614 start_new_bblock = 0;
2617 if (cfg->method == method)
2618 real_offset = ip - header->code;
2620 real_offset = inline_offset;
2622 if (start_new_bblock) {
2623 bblock->cil_length = ip - bblock->cil_code;
2624 if (start_new_bblock == 2) {
2625 g_assert (ip == tblock->cil_code);
2627 GET_BBLOCK (cfg, bbhash, tblock, ip);
2629 bblock->next_bb = tblock;
2631 start_new_bblock = 0;
2632 for (i = 0; i < bblock->in_scount; ++i) {
2633 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2637 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
2638 link_bblock (cfg, bblock, tblock);
2639 if (sp != stack_start) {
2640 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2643 bblock->next_bb = tblock;
2645 for (i = 0; i < bblock->in_scount; ++i) {
2646 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2652 if (cfg->verbose_level > 3)
2653 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, sp-stack_start, mono_disasm_code_one (NULL, method, ip, NULL));
2660 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2661 ins->cil_code = ip++;
2662 MONO_ADD_INS (bblock, ins);
2668 CHECK_STACK_OVF (1);
2669 n = (*ip)-CEE_LDARG_0;
2670 NEW_ARGLOAD (cfg, ins, n);
2671 ins->cil_code = ip++;
2678 CHECK_STACK_OVF (1);
2679 n = (*ip)-CEE_LDLOC_0;
2680 NEW_LOCLOAD (cfg, ins, n);
2681 ins->cil_code = ip++;
2689 n = (*ip)-CEE_STLOC_0;
2691 handle_loaded_temps (cfg, bblock, stack_start, sp);
2692 NEW_LOCSTORE (cfg, ins, n, *sp);
2694 if (ins->opcode == CEE_STOBJ) {
2695 NEW_LOCLOADA (cfg, ins, n);
2696 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2698 MONO_ADD_INS (bblock, ins);
2703 CHECK_STACK_OVF (1);
2704 NEW_ARGLOAD (cfg, ins, ip [1]);
2710 CHECK_STACK_OVF (1);
2711 NEW_ARGLOADA (cfg, ins, ip [1]);
2719 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
2720 handle_loaded_temps (cfg, bblock, stack_start, sp);
2722 if (ins->opcode == CEE_STOBJ) {
2723 NEW_ARGLOADA (cfg, ins, ip [1]);
2724 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2726 MONO_ADD_INS (bblock, ins);
2730 CHECK_STACK_OVF (1);
2731 NEW_LOCLOAD (cfg, ins, ip [1]);
2737 CHECK_STACK_OVF (1);
2738 NEW_LOCLOADA (cfg, ins, ip [1]);
2746 handle_loaded_temps (cfg, bblock, stack_start, sp);
2747 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
2749 if (ins->opcode == CEE_STOBJ) {
2750 NEW_LOCLOADA (cfg, ins, ip [1]);
2751 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2753 MONO_ADD_INS (bblock, ins);
2758 CHECK_STACK_OVF (1);
2759 NEW_PCONST (cfg, ins, NULL);
2761 ins->type = STACK_OBJ;
2766 CHECK_STACK_OVF (1);
2767 NEW_ICONST (cfg, ins, -1);
2781 CHECK_STACK_OVF (1);
2782 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
2788 CHECK_STACK_OVF (1);
2790 NEW_ICONST (cfg, ins, *((signed char*)ip));
2796 CHECK_STACK_OVF (1);
2797 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
2803 CHECK_STACK_OVF (1);
2804 MONO_INST_NEW (cfg, ins, OP_I8CONST);
2806 ins->type = STACK_I8;
2808 ins->inst_l = (gint64)read64 (ip);
2813 float *f = g_malloc (sizeof (float));
2814 CHECK_STACK_OVF (1);
2815 MONO_INST_NEW (cfg, ins, OP_R4CONST);
2816 ins->type = STACK_R8;
2825 double *d = g_malloc (sizeof (double));
2826 CHECK_STACK_OVF (1);
2827 MONO_INST_NEW (cfg, ins, OP_R8CONST);
2828 ins->type = STACK_R8;
2837 MonoInst *temp, *store;
2839 CHECK_STACK_OVF (1);
2844 * small optimization: if the loaded value was from a local already,
2845 * just load it twice.
2847 if (ins->ssa_op == MONO_SSA_LOAD &&
2848 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
2850 MONO_INST_NEW (cfg, temp, 0);
2852 temp->cil_code = ip;
2855 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
2856 temp->cil_code = ip;
2857 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2858 store->cil_code = ip;
2859 MONO_ADD_INS (bblock, store);
2860 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
2863 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
2873 MONO_INST_NEW (cfg, ins, CEE_POP);
2874 MONO_ADD_INS (bblock, ins);
2875 ins->cil_code = ip++;
2880 if (stack_start != sp)
2882 MONO_INST_NEW (cfg, ins, CEE_JMP);
2883 token = read32 (ip + 1);
2884 /* FIXME: check the signature matches */
2885 cmethod = mono_get_method (image, token, NULL);
2886 ins->inst_p0 = cmethod;
2887 MONO_ADD_INS (bblock, ins);
2889 start_new_bblock = 1;
2893 case CEE_CALLVIRT: {
2894 MonoInst *addr = NULL;
2895 MonoMethodSignature *fsig = NULL;
2896 int temp, array_rank = 0;
2897 int virtual = *ip == CEE_CALLVIRT;
2899 token = read32 (ip + 1);
2901 if (*ip == CEE_CALLI) {
2906 if (method->wrapper_type != MONO_WRAPPER_NONE)
2907 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
2909 fsig = mono_metadata_parse_signature (image, token);
2911 n = fsig->param_count + fsig->hasthis;
2914 if (method->wrapper_type != MONO_WRAPPER_NONE) {
2915 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
2917 cmethod = mono_get_method (image, token, NULL);
2920 if (!cmethod->klass->inited)
2921 mono_class_init (cmethod->klass);
2923 if (cmethod->signature->pinvoke) {
2924 #ifdef MONO_USE_EXC_TABLES
2925 if (mono_method_blittable (cmethod)) {
2926 fsig = cmethod->signature;
2929 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
2930 fsig = wrapper->signature;
2931 #ifdef MONO_USE_EXC_TABLES
2935 fsig = mono_method_get_signature (cmethod, image, token);
2938 n = fsig->param_count + fsig->hasthis;
2940 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
2941 cmethod->klass->parent == mono_defaults.array_class) {
2942 array_rank = cmethod->klass->rank;
2945 if (cmethod->string_ctor)
2946 g_assert_not_reached ();
2952 //g_assert (!virtual || fsig->hasthis);
2956 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
2959 if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL)) {
2961 for (i = 0; i < n; ++i) {
2962 NEW_ARGSTORE (cfg, ins, i, sp [i]);
2964 MONO_ADD_INS (bblock, ins);
2966 MONO_INST_NEW (cfg, ins, CEE_JMP);
2968 ins->inst_p0 = cmethod;
2969 MONO_ADD_INS (bblock, ins);
2970 start_new_bblock = 1;
2971 /* skip CEE_RET as well */
2976 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_opcode_for_method (cfg, cmethod, fsig, sp))) {
2979 if (MONO_TYPE_IS_VOID (fsig->ret)) {
2980 MONO_ADD_INS (bblock, ins);
2982 type_to_eval_stack_type (fsig->ret, ins);
2991 handle_loaded_temps (cfg, bblock, stack_start, sp);
2993 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
2994 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
2995 mono_method_check_inlining (cfg, cmethod) &&
2996 !g_list_find (dont_inline, cmethod)) {
2998 MonoBasicBlock *ebblock;
3000 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3004 GET_BBLOCK (cfg, bbhash, bblock, ip);
3005 ebblock->next_bb = bblock;
3006 link_bblock (cfg, ebblock, bblock);
3008 if (!MONO_TYPE_IS_VOID (fsig->ret))
3011 /* indicates start of a new block, and triggers a load of all
3012 stack arguments at bb boundarie */
3015 inline_costs += costs;
3020 inline_costs += 10 * num_calls++;
3022 /* tail recursion elimination */
3023 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == cfg->method && ip [5] == CEE_RET) {
3024 gboolean has_vtargs = FALSE;
3027 /* keep it simple */
3028 for (i = fsig->param_count - 1; i >= 0; i--) {
3029 if (MONO_TYPE_ISSTRUCT (cmethod->signature->params [i]))
3034 for (i = 0; i < n; ++i) {
3035 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3037 MONO_ADD_INS (bblock, ins);
3039 MONO_INST_NEW (cfg, ins, CEE_BR);
3041 MONO_ADD_INS (bblock, ins);
3042 tblock = start_bblock->out_bb [0];
3043 link_bblock (cfg, bblock, tblock);
3044 ins->inst_target_bb = tblock;
3045 start_new_bblock = 1;
3048 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3049 /* just create a dummy - the value is never used */
3050 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
3051 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3059 if (*ip == CEE_CALLI) {
3061 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
3062 NEW_TEMPLOAD (cfg, *sp, temp);
3066 } else if (array_rank) {
3069 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
3070 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
3071 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
3073 if (ins->opcode == CEE_STOBJ) {
3074 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
3076 MONO_ADD_INS (bblock, ins);
3079 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
3080 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3081 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
3085 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
3086 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3089 g_assert_not_reached ();
3093 if (0 && CODE_IS_STLOC (ip + 5) && (!MONO_TYPE_ISSTRUCT (fsig->ret)) && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)) {
3094 /* no need to spill */
3095 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
3098 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) {
3099 NEW_TEMPLOAD (cfg, *sp, temp);
3109 if (cfg->method != method) {
3110 /* return from inlined methode */
3115 //g_assert (returnvar != -1);
3116 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
3117 store->cil_code = sp [0]->cil_code;
3118 if (store->opcode == CEE_STOBJ) {
3119 g_assert_not_reached ();
3120 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
3121 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
3123 MONO_ADD_INS (bblock, store);
3127 g_assert (!return_var);
3130 MONO_INST_NEW (cfg, ins, CEE_NOP);
3131 ins->opcode = mono_type_to_stind (method->signature->ret);
3132 if (ins->opcode == CEE_STOBJ) {
3133 NEW_RETLOADA (cfg, ins);
3134 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3136 ins->opcode = OP_SETRET;
3138 ins->inst_i0 = *sp;;
3139 ins->inst_i1 = NULL;
3140 MONO_ADD_INS (bblock, ins);
3144 if (sp != stack_start)
3146 MONO_INST_NEW (cfg, ins, CEE_BR);
3147 ins->cil_code = ip++;
3148 ins->inst_target_bb = end_bblock;
3149 MONO_ADD_INS (bblock, ins);
3150 link_bblock (cfg, bblock, end_bblock);
3151 start_new_bblock = 1;
3154 MONO_INST_NEW (cfg, ins, CEE_BR);
3155 ins->cil_code = ip++;
3156 MONO_ADD_INS (bblock, ins);
3157 target = ip + 1 + (signed char)(*ip);
3159 GET_BBLOCK (cfg, bbhash, tblock, target);
3160 link_bblock (cfg, bblock, tblock);
3161 CHECK_BBLOCK (target, ip, tblock);
3162 ins->inst_target_bb = tblock;
3163 if (sp != stack_start) {
3164 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3167 start_new_bblock = 1;
3173 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3174 ins->cil_code = ip++;
3175 target = ip + 1 + *(signed char*)ip;
3177 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
3178 if (sp != stack_start) {
3179 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3195 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3196 ins->cil_code = ip++;
3197 target = ip + 1 + *(signed char*)ip;
3200 if (sp != stack_start) {
3201 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3207 MONO_INST_NEW (cfg, ins, CEE_BR);
3208 ins->cil_code = ip++;
3209 MONO_ADD_INS (bblock, ins);
3210 target = ip + 4 + (gint32)read32(ip);
3212 GET_BBLOCK (cfg, bbhash, tblock, target);
3213 link_bblock (cfg, bblock, tblock);
3214 CHECK_BBLOCK (target, ip, tblock);
3215 ins->inst_target_bb = tblock;
3216 if (sp != stack_start) {
3217 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3220 start_new_bblock = 1;
3226 MONO_INST_NEW (cfg, ins, *ip);
3227 ins->cil_code = ip++;
3228 target = ip + 4 + (gint32)read32(ip);
3230 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
3231 if (sp != stack_start) {
3232 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3248 MONO_INST_NEW (cfg, ins, *ip);
3249 ins->cil_code = ip++;
3250 target = ip + 4 + (gint32)read32(ip);
3253 if (sp != stack_start) {
3254 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3261 n = read32 (ip + 1);
3262 MONO_INST_NEW (cfg, ins, *ip);
3264 ins->inst_left = *sp;
3265 if (ins->inst_left->type != STACK_I4) goto unverified;
3268 target = ip + n * sizeof (guint32);
3269 MONO_ADD_INS (bblock, ins);
3270 GET_BBLOCK (cfg, bbhash, tblock, target);
3271 link_bblock (cfg, bblock, tblock);
3272 ins->klass = GUINT_TO_POINTER (n);
3273 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
3274 ins->inst_many_bb [n] = tblock;
3276 for (i = 0; i < n; ++i) {
3277 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
3278 link_bblock (cfg, bblock, tblock);
3279 ins->inst_many_bb [i] = tblock;
3282 /* FIXME: handle stack args */
3297 MONO_INST_NEW (cfg, ins, *ip);
3302 ins->type = ldind_type [*ip - CEE_LDIND_I1];
3303 ins->flags |= ins_flag;
3315 MONO_INST_NEW (cfg, ins, *ip);
3316 ins->cil_code = ip++;
3318 handle_loaded_temps (cfg, bblock, stack_start, sp);
3319 MONO_ADD_INS (bblock, ins);
3320 ins->inst_i0 = sp [0];
3321 ins->inst_i1 = sp [1];
3322 ins->flags |= ins_flag;
3353 case CEE_CONV_OVF_I8:
3354 case CEE_CONV_OVF_U8:
3360 case CEE_CONV_OVF_I4:
3361 case CEE_CONV_OVF_I1:
3362 case CEE_CONV_OVF_I2:
3363 case CEE_CONV_OVF_I:
3364 case CEE_CONV_OVF_U:
3367 if (sp [-1]->type == STACK_R8) {
3368 ADD_UNOP (CEE_CONV_OVF_I8);
3376 case CEE_CONV_OVF_U1:
3377 case CEE_CONV_OVF_U2:
3378 case CEE_CONV_OVF_U4:
3381 if (sp [-1]->type == STACK_R8) {
3382 ADD_UNOP (CEE_CONV_OVF_U8);
3390 case CEE_CONV_OVF_I1_UN:
3391 case CEE_CONV_OVF_I2_UN:
3392 case CEE_CONV_OVF_I4_UN:
3393 case CEE_CONV_OVF_I8_UN:
3394 case CEE_CONV_OVF_U1_UN:
3395 case CEE_CONV_OVF_U2_UN:
3396 case CEE_CONV_OVF_U4_UN:
3397 case CEE_CONV_OVF_U8_UN:
3398 case CEE_CONV_OVF_I_UN:
3399 case CEE_CONV_OVF_U_UN:
3405 g_error ("opcode 0x%02x not handled", *ip);
3408 MonoInst *iargs [3];
3411 token = read32 (ip + 1);
3412 if (method->wrapper_type != MONO_WRAPPER_NONE)
3413 klass = mono_method_get_wrapper_data (method, token);
3415 klass = mono_class_get (image, token);
3417 mono_class_init (klass);
3418 n = mono_class_value_size (klass, NULL);
3419 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3420 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3421 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3423 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3424 copy->inst_left = iargs [0];
3425 copy->inst_right = *sp;
3426 copy->cil_code = ip;
3428 MONO_ADD_INS (bblock, copy);
3431 NEW_ICONST (cfg, iargs [2], n);
3432 iargs [2]->cil_code = ip;
3434 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3436 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3443 CHECK_STACK_OVF (1);
3444 n = read32 (ip + 1);
3446 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3448 MonoInst *iargs [1];
3450 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
3451 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
3452 NEW_TEMPLOAD (cfg, *sp, temp);
3456 if (mono_compile_aot) {
3457 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n);
3460 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
3462 MonoInst *iargs [3];
3463 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3464 NEW_IMAGECONST (cfg, iargs [1], image);
3465 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
3466 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
3467 NEW_TEMPLOAD (cfg, *sp, temp);
3468 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3470 NEW_PCONST (cfg, ins, NULL);
3472 ins->type = STACK_OBJ;
3473 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3482 MonoInst *iargs [2];
3483 MonoMethodSignature *fsig;
3486 token = read32 (ip + 1);
3487 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3488 cmethod = mono_method_get_wrapper_data (method, token);
3490 cmethod = mono_get_method (image, token, NULL);
3491 fsig = mono_method_get_signature (cmethod, image, token);
3493 mono_class_init (cmethod->klass);
3495 n = fsig->param_count;
3498 /* move the args to allow room for 'this' in the first position */
3504 handle_loaded_temps (cfg, bblock, stack_start, sp);
3507 if (cmethod->klass->parent == mono_defaults.array_class) {
3508 NEW_METHODCONST (cfg, *sp, cmethod);
3509 temp = mono_emit_native_call (cfg, bblock, mono_array_new_va, fsig, sp, ip, FALSE);
3511 } else if (cmethod->string_ctor) {
3512 /* we simply pass a null pointer */
3513 NEW_PCONST (cfg, *sp, NULL);
3514 /* now call the string ctor */
3515 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
3517 if (cmethod->klass->valuetype) {
3518 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
3519 temp = iargs [0]->inst_c0;
3520 NEW_TEMPLOADA (cfg, *sp, temp);
3522 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
3523 NEW_DOMAINCONST (cfg, iargs [0]);
3524 NEW_CLASSCONST (cfg, iargs [1], cmethod->klass);
3526 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3528 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
3529 NEW_PCONST (cfg, iargs [0], vtable);
3530 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
3532 NEW_TEMPLOAD (cfg, *sp, temp);
3535 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3536 mono_method_check_inlining (cfg, cmethod) &&
3537 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
3538 !g_list_find (dont_inline, cmethod)) {
3540 MonoBasicBlock *ebblock;
3541 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3546 GET_BBLOCK (cfg, bbhash, bblock, ip);
3547 ebblock->next_bb = bblock;
3548 link_bblock (cfg, ebblock, bblock);
3550 NEW_TEMPLOAD (cfg, *sp, temp);
3553 /* indicates start of a new block, and triggers a load
3554 of all stack arguments at bb boundarie */
3557 inline_costs += costs;
3561 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3564 /* now call the actual ctor */
3565 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3569 NEW_TEMPLOAD (cfg, *sp, temp);
3578 MONO_INST_NEW (cfg, ins, *ip);
3580 klass = mono_class_get (image, read32 (ip + 1));
3581 mono_class_init (klass);
3582 ins->type = STACK_OBJ;
3583 ins->inst_left = *sp;
3584 ins->inst_newa_class = klass;
3590 MonoInst *add, *vtoffset;
3594 token = read32 (ip + 1);
3595 if (method->wrapper_type != MONO_WRAPPER_NONE)
3596 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3598 klass = mono_class_get (image, token);
3599 mono_class_init (klass);
3602 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
3603 ins->type = STACK_OBJ;
3604 ins->inst_left = *sp;
3606 ins->inst_newa_class = klass;
3609 MONO_INST_NEW (cfg, add, CEE_ADD);
3610 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3611 add->inst_left = ins;
3612 add->inst_right = vtoffset;
3613 add->type = STACK_MP;
3621 MONO_INST_NEW (cfg, ins, *ip);
3623 klass = mono_class_get (image, read32 (ip + 1));
3624 mono_class_init (klass);
3625 ins->type = STACK_OBJ;
3626 ins->inst_left = *sp;
3628 ins->inst_newa_class = klass;
3635 MONO_INST_NEW (cfg, ins, *ip);
3637 ins->inst_left = *sp;
3638 ins->cil_code = ip++;
3639 MONO_ADD_INS (bblock, ins);
3641 start_new_bblock = 1;
3646 MonoInst *offset_ins;
3647 MonoClassField *field;
3648 MonoBasicBlock *ebblock;
3652 if (*ip == CEE_STFLD) {
3659 // FIXME: enable this test later.
3660 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
3662 token = read32 (ip + 1);
3663 field = mono_field_from_token (image, token, &klass);
3664 mono_class_init (klass);
3666 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
3667 /* FIXME: mark instructions for use in SSA */
3668 if (*ip == CEE_STFLD) {
3669 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
3670 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
3671 MonoInst *iargs [5];
3674 NEW_CLASSCONST (cfg, iargs [1], klass);
3675 NEW_FIELDCONST (cfg, iargs [2], field);
3676 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
3680 if (cfg->opt & MONO_OPT_INLINE) {
3681 costs = inline_method (cfg, stfld_wrapper, stfld_wrapper->signature, bblock,
3682 iargs, ip, real_offset, dont_inline, &ebblock);
3683 g_assert (costs > 0);
3688 GET_BBLOCK (cfg, bbhash, bblock, ip);
3689 ebblock->next_bb = bblock;
3690 link_bblock (cfg, ebblock, bblock);
3692 /* indicates start of a new block, and triggers a load
3693 of all stack arguments at bb boundarie */
3696 inline_costs += costs;
3699 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, stfld_wrapper->signature, iargs, ip, NULL);
3703 NEW_ICONST (cfg, offset_ins, foffset);
3704 MONO_INST_NEW (cfg, ins, CEE_ADD);
3706 ins->inst_left = *sp;
3707 ins->inst_right = offset_ins;
3708 ins->type = STACK_MP;
3710 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
3711 store->cil_code = ip;
3712 store->inst_left = ins;
3713 store->inst_right = sp [1];
3714 handle_loaded_temps (cfg, bblock, stack_start, sp);
3715 store->flags |= ins_flag;
3717 if (store->opcode == CEE_STOBJ) {
3718 handle_stobj (cfg, bblock, ins, sp [1], ip,
3719 mono_class_from_mono_type (field->type), FALSE, FALSE);
3721 MONO_ADD_INS (bblock, store);
3724 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
3725 /* fixme: we need to inline that call somehow */
3726 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
3727 MonoInst *iargs [4];
3731 NEW_CLASSCONST (cfg, iargs [1], klass);
3732 NEW_FIELDCONST (cfg, iargs [2], field);
3733 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
3734 if (cfg->opt & MONO_OPT_INLINE) {
3735 costs = inline_method (cfg, ldfld_wrapper, ldfld_wrapper->signature, bblock,
3736 iargs, ip, real_offset, dont_inline, &ebblock);
3737 g_assert (costs > 0);
3742 GET_BBLOCK (cfg, bbhash, bblock, ip);
3743 ebblock->next_bb = bblock;
3744 link_bblock (cfg, ebblock, bblock);
3746 temp = iargs [0]->inst_i0->inst_c0;
3748 if (*ip == CEE_LDFLDA) {
3749 /* not sure howto handle this */
3750 NEW_TEMPLOADA (cfg, *sp, temp);
3752 NEW_TEMPLOAD (cfg, *sp, temp);
3756 /* indicates start of a new block, and triggers a load of
3757 all stack arguments at bb boundarie */
3760 inline_costs += costs;
3763 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, ldfld_wrapper->signature, iargs, ip, NULL);
3764 if (*ip == CEE_LDFLDA) {
3765 /* not sure howto handle this */
3766 NEW_TEMPLOADA (cfg, *sp, temp);
3768 NEW_TEMPLOAD (cfg, *sp, temp);
3773 NEW_ICONST (cfg, offset_ins, foffset);
3774 MONO_INST_NEW (cfg, ins, CEE_ADD);
3776 ins->inst_left = *sp;
3777 ins->inst_right = offset_ins;
3778 ins->type = STACK_MP;
3780 if (*ip == CEE_LDFLDA) {
3784 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
3785 type_to_eval_stack_type (field->type, load);
3786 load->cil_code = ip;
3787 load->inst_left = ins;
3788 load->flags |= ins_flag;
3800 MonoClassField *field;
3802 token = read32 (ip + 1);
3804 field = mono_field_from_token (image, token, &klass);
3805 mono_class_init (klass);
3807 handle_loaded_temps (cfg, bblock, stack_start, sp);
3809 if (((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot)) {
3811 MonoInst *iargs [2];
3812 g_assert (field->parent);
3813 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3814 NEW_FIELDCONST (cfg, iargs [1], field);
3815 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
3816 NEW_TEMPLOAD (cfg, ins, temp);
3820 vtable = mono_class_vtable (cfg->domain, klass);
3821 if (!cfg->domain->thread_static_fields || !(addr = g_hash_table_lookup (cfg->domain->thread_static_fields, field))) {
3822 if (!vtable->initialized && !(klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && needs_cctor_run (klass, method)) {
3823 MonoInst *iargs [1];
3824 NEW_PCONST (cfg, iargs [0], vtable);
3825 mono_emit_jit_icall (cfg, bblock, mono_runtime_class_init, iargs, ip);
3826 if (cfg->verbose_level > 2)
3827 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
3829 mono_runtime_class_init (vtable);
3831 addr = (char*)vtable->data + field->offset;
3832 NEW_PCONST (cfg, ins, addr);
3836 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
3837 * This could be later optimized to do just a couple of
3838 * memory dereferences with constant offsets.
3841 MonoInst *iargs [1];
3842 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
3843 temp = mono_emit_jit_icall (cfg, bblock, mono_threads_get_static_data, iargs, ip);
3844 NEW_TEMPLOAD (cfg, ins, temp);
3848 /* FIXME: mark instructions for use in SSA */
3849 if (*ip == CEE_LDSFLDA) {
3851 } else if (*ip == CEE_STSFLD) {
3855 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
3856 store->cil_code = ip;
3857 store->inst_left = ins;
3858 store->inst_right = sp [0];
3859 store->flags |= ins_flag;
3862 if (store->opcode == CEE_STOBJ) {
3863 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
3865 MONO_ADD_INS (bblock, store);
3867 gboolean is_const = FALSE;
3868 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
3869 if (!((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) &&
3870 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
3871 gpointer addr = (char*)vtable->data + field->offset;
3872 /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
3874 switch (field->type->type) {
3875 case MONO_TYPE_BOOLEAN:
3877 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
3881 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
3884 case MONO_TYPE_CHAR:
3886 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
3890 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
3895 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
3899 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
3904 case MONO_TYPE_STRING:
3905 case MONO_TYPE_OBJECT:
3906 case MONO_TYPE_CLASS:
3907 case MONO_TYPE_SZARRAY:
3909 case MONO_TYPE_FNPTR:
3910 case MONO_TYPE_ARRAY:
3911 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
3912 type_to_eval_stack_type (field->type, *sp);
3917 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
3918 sp [0]->type = STACK_I8;
3919 sp [0]->inst_l = *((gint64 *)addr);
3924 case MONO_TYPE_VALUETYPE:
3933 CHECK_STACK_OVF (1);
3934 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
3935 type_to_eval_stack_type (field->type, load);
3936 load->cil_code = ip;
3937 load->inst_left = ins;
3939 load->flags |= ins_flag;
3941 /* fixme: dont see the problem why this does not work */
3942 //cfg->disable_aot = TRUE;
3951 token = read32 (ip + 1);
3952 if (method->wrapper_type != MONO_WRAPPER_NONE)
3953 klass = mono_method_get_wrapper_data (method, token);
3955 klass = mono_class_get (image, token);
3956 mono_class_init (klass);
3957 n = mono_type_to_stind (&klass->byval_arg);
3958 if (n == CEE_STOBJ) {
3959 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
3961 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
3963 MONO_INST_NEW (cfg, store, n);
3964 store->cil_code = ip;
3965 store->inst_left = sp [0];
3966 store->inst_right = sp [1];
3967 store->flags |= ins_flag;
3968 MONO_ADD_INS (bblock, store);
3975 MonoInst *iargs [2];
3976 MonoInst *load, *vtoffset, *add, *val, *vstore;
3981 token = read32 (ip + 1);
3982 if (method->wrapper_type != MONO_WRAPPER_NONE)
3983 klass = mono_method_get_wrapper_data (method, token);
3985 klass = mono_class_get (image, token);
3986 mono_class_init (klass);
3988 /* much like NEWOBJ */
3989 NEW_DOMAINCONST (cfg, iargs [0]);
3990 NEW_CLASSCONST (cfg, iargs [1], klass);
3992 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3993 NEW_TEMPLOAD (cfg, load, temp);
3994 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3995 MONO_INST_NEW (cfg, add, CEE_ADD);
3996 add->inst_left = load;
3997 add->inst_right = vtoffset;
4000 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
4001 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
4002 vstore->cil_code = ip;
4003 vstore->inst_left = add;
4004 vstore->inst_right = val;
4006 if (vstore->opcode == CEE_STOBJ) {
4007 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
4009 MONO_ADD_INS (bblock, vstore);
4011 NEW_TEMPLOAD (cfg, load, temp);
4019 MONO_INST_NEW (cfg, ins, *ip);
4023 token = read32 (ip + 1);
4025 /* allocate the domainvar - becaus this is used in decompose_foreach */
4026 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot)
4027 mono_get_domainvar (cfg);
4029 if (method->wrapper_type != MONO_WRAPPER_NONE)
4030 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4032 klass = mono_class_get (image, token);
4034 mono_class_init (klass);
4035 ins->inst_newa_class = klass;
4036 ins->inst_newa_len = *sp;
4037 ins->type = STACK_OBJ;
4044 MONO_INST_NEW (cfg, ins, *ip);
4045 ins->cil_code = ip++;
4047 ins->inst_left = *sp;
4048 ins->type = STACK_PTR;
4054 klass = mono_class_get (image, read32 (ip + 1));
4055 mono_class_init (klass);
4056 NEW_LDELEMA (cfg, ins, sp, klass);
4071 case CEE_LDELEM_REF: {
4075 * ldind.x (ldelema (array, index))
4076 * ldelema does the bounds check
4080 klass = array_access_to_klass (*ip);
4081 NEW_LDELEMA (cfg, load, sp, klass);
4082 load->cil_code = ip;
4083 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
4085 ins->inst_left = load;
4087 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
4097 case CEE_STELEM_R8: {
4101 * stind.x (ldelema (array, index), val)
4102 * ldelema does the bounds check
4106 klass = array_access_to_klass (*ip);
4107 NEW_LDELEMA (cfg, load, sp, klass);
4108 load->cil_code = ip;
4109 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
4111 ins->inst_left = load;
4112 ins->inst_right = sp [2];
4114 handle_loaded_temps (cfg, bblock, stack_start, sp);
4115 MONO_ADD_INS (bblock, ins);
4116 /* FIXME: add the implicit STELEM_REF castclass */
4118 cfg->disable_ssa = TRUE;
4121 case CEE_STELEM_REF: {
4122 MonoInst *iargs [3];
4127 handle_loaded_temps (cfg, bblock, stack_start, sp);
4133 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4137 NEW_GROUP (cfg, group, sp [0], sp [1]);
4138 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
4140 ins->inst_left = group;
4141 ins->inst_right = sp [2];
4142 MONO_ADD_INS (bblock, ins);
4147 cfg->disable_ssa = TRUE;
4150 case CEE_CKFINITE: {
4151 MonoInst *store, *temp;
4154 /* this instr. can throw exceptions as side effect,
4155 * so we cant eliminate dead code which contains CKFINITE opdodes.
4156 * Spilling to memory makes sure that we always perform
4160 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
4162 ins->inst_left = sp [-1];
4163 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
4165 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4166 store->cil_code = ip;
4167 MONO_ADD_INS (bblock, store);
4169 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
4176 g_error ("opcode 0x%02x not handled", *ip);
4180 MonoClass *handle_class;
4182 CHECK_STACK_OVF (1);
4184 n = read32 (ip + 1);
4186 handle = mono_ldtoken (image, n, &handle_class);
4187 mono_class_init (handle_class);
4189 if (((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot)) {
4191 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
4193 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
4195 NEW_IMAGECONST (cfg, iargs [0], image);
4196 NEW_ICONST (cfg, iargs [1], n);
4197 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
4198 NEW_TEMPLOAD (cfg, res, temp);
4199 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4200 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
4201 MONO_ADD_INS (bblock, store);
4202 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4204 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method (image, read32 (ip + 6), NULL)) &&
4205 (cmethod->klass == mono_defaults.monotype_class->parent) &&
4206 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
4207 MonoClass *tclass = mono_class_from_mono_type (handle);
4208 mono_class_init (tclass);
4209 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
4210 ins->type = STACK_OBJ;
4211 ins->klass = cmethod->klass;
4214 NEW_PCONST (cfg, ins, handle);
4215 ins->type = STACK_VTYPE;
4216 ins->klass = handle_class;
4232 case CEE_ADD_OVF_UN:
4234 case CEE_MUL_OVF_UN:
4236 case CEE_SUB_OVF_UN:
4241 case CEE_ENDFINALLY:
4242 /* FIXME: check stack state */
4243 MONO_INST_NEW (cfg, ins, *ip);
4244 MONO_ADD_INS (bblock, ins);
4245 ins->cil_code = ip++;
4246 start_new_bblock = 1;
4251 if (*ip == CEE_LEAVE) {
4252 target = ip + 5 + (gint32)read32(ip + 1);
4254 target = ip + 2 + (signed char)(ip [1]);
4257 /* empty the stack */
4258 while (sp != stack_start) {
4259 MONO_INST_NEW (cfg, ins, CEE_POP);
4263 MONO_ADD_INS (bblock, ins);
4266 /* fixme: call fault handler ? */
4268 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
4270 for (tmp = handlers; tmp; tmp = tmp->next) {
4272 link_bblock (cfg, bblock, tblock);
4273 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
4275 ins->inst_target_bb = tblock;
4276 MONO_ADD_INS (bblock, ins);
4278 g_list_free (handlers);
4281 MONO_INST_NEW (cfg, ins, CEE_BR);
4283 MONO_ADD_INS (bblock, ins);
4284 GET_BBLOCK (cfg, bbhash, tblock, target);
4285 link_bblock (cfg, bblock, tblock);
4286 CHECK_BBLOCK (target, ip, tblock);
4287 ins->inst_target_bb = tblock;
4288 start_new_bblock = 1;
4290 if (*ip == CEE_LEAVE)
4299 MONO_INST_NEW (cfg, ins, *ip);
4301 handle_loaded_temps (cfg, bblock, stack_start, sp);
4302 MONO_ADD_INS (bblock, ins);
4303 ins->cil_code = ip++;
4304 ins->inst_i0 = sp [0];
4305 ins->inst_i1 = sp [1];
4313 /* trampoline mono specific opcodes */
4314 case MONO_CUSTOM_PREFIX: {
4316 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
4320 case CEE_MONO_FUNC1: {
4322 gpointer func = NULL;
4327 case MONO_MARSHAL_CONV_STR_LPWSTR:
4328 func = mono_string_to_utf16;
4330 case MONO_MARSHAL_CONV_LPWSTR_STR:
4331 func = mono_string_from_utf16;
4333 case MONO_MARSHAL_CONV_LPSTR_STR:
4334 func = mono_string_new_wrapper;
4336 case MONO_MARSHAL_CONV_STR_LPTSTR:
4337 case MONO_MARSHAL_CONV_STR_LPSTR:
4338 func = mono_string_to_utf8;
4340 case MONO_MARSHAL_CONV_STR_BSTR:
4341 func = mono_string_to_bstr;
4343 case MONO_MARSHAL_CONV_STR_TBSTR:
4344 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
4345 func = mono_string_to_ansibstr;
4347 case MONO_MARSHAL_CONV_SB_LPSTR:
4348 func = mono_string_builder_to_utf8;
4350 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
4351 func = mono_array_to_savearray;
4353 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
4354 func = mono_array_to_lparray;
4356 case MONO_MARSHAL_CONV_DEL_FTN:
4357 func = mono_delegate_to_ftnptr;
4359 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
4360 func = mono_marshal_string_array;
4363 g_warning ("unknown conversion %d\n", ip [2]);
4364 g_assert_not_reached ();
4367 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4368 NEW_TEMPLOAD (cfg, *sp, temp);
4372 inline_costs += 10 * num_calls++;
4375 case CEE_MONO_PROC2: {
4376 gpointer func = NULL;
4381 case MONO_MARSHAL_CONV_LPSTR_SB:
4382 func = mono_string_utf8_to_builder;
4384 case MONO_MARSHAL_FREE_ARRAY:
4385 func = mono_marshal_free_array;
4388 g_assert_not_reached ();
4391 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4393 inline_costs += 10 * num_calls++;
4396 case CEE_MONO_PROC3: {
4397 gpointer func = NULL;
4402 case MONO_MARSHAL_CONV_STR_BYVALSTR:
4403 func = mono_string_to_byvalstr;
4405 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
4406 func = mono_string_to_byvalwstr;
4409 g_assert_not_reached ();
4412 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4414 inline_costs += 10 * num_calls++;
4420 mono_emit_jit_icall (cfg, bblock, g_free, sp, ip);
4422 inline_costs += 10 * num_calls++;
4424 case CEE_MONO_LDPTR:
4425 CHECK_STACK_OVF (1);
4426 token = read32 (ip + 2);
4427 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
4431 inline_costs += 10 * num_calls++;
4433 case CEE_MONO_VTADDR:
4436 MONO_INST_NEW (cfg, ins, OP_VTADDR);
4438 ins->type = STACK_MP;
4439 ins->inst_left = *sp;
4443 case CEE_MONO_NEWOBJ: {
4444 MonoInst *iargs [2];
4446 CHECK_STACK_OVF (1);
4447 token = read32 (ip + 2);
4448 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4449 mono_class_init (klass);
4450 NEW_DOMAINCONST (cfg, iargs [0]);
4451 NEW_CLASSCONST (cfg, iargs [1], klass);
4452 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
4453 NEW_TEMPLOAD (cfg, *sp, temp);
4456 inline_costs += 10 * num_calls++;
4459 case CEE_MONO_OBJADDR:
4462 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
4464 ins->type = STACK_MP;
4465 ins->inst_left = *sp;
4469 case CEE_MONO_LDNATIVEOBJ:
4471 token = read32 (ip + 2);
4472 klass = mono_method_get_wrapper_data (method, token);
4473 g_assert (klass->valuetype);
4474 mono_class_init (klass);
4475 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
4479 case CEE_MONO_RETOBJ:
4480 g_assert (cfg->ret);
4481 g_assert (method->signature->pinvoke);
4485 token = read32 (ip + 2);
4486 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4488 NEW_RETLOADA (cfg, ins);
4489 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
4491 if (sp != stack_start)
4494 MONO_INST_NEW (cfg, ins, CEE_BR);
4496 ins->inst_target_bb = end_bblock;
4497 MONO_ADD_INS (bblock, ins);
4498 link_bblock (cfg, bblock, end_bblock);
4499 start_new_bblock = 1;
4503 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
4511 /* somewhat similar to LDTOKEN */
4512 MonoInst *addr, *vtvar;
4514 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
4516 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4517 addr->cil_code = ip;
4518 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
4520 ins->inst_left = addr;
4521 MONO_ADD_INS (bblock, ins);
4522 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4535 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
4536 MONO_INST_NEW (cfg, ins, cmp->opcode);
4538 cmp->inst_i0 = sp [0];
4539 cmp->inst_i1 = sp [1];
4543 cmp->opcode = OP_COMPARE;
4545 ins->type = STACK_I4;
4555 CHECK_STACK_OVF (1);
4556 n = read32 (ip + 2);
4557 if (method->wrapper_type != MONO_WRAPPER_NONE)
4558 cmethod = mono_method_get_wrapper_data (method, n);
4560 cmethod = mono_get_method (image, n, NULL);
4563 * We can't do this in mono_ldftn, since it is used in
4564 * the synchronized wrapper, leading to an infinite loop.
4566 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
4567 cmethod = mono_marshal_get_synchronized_wrapper (cmethod);
4570 mono_class_init (cmethod->klass);
4571 handle_loaded_temps (cfg, bblock, stack_start, sp);
4573 NEW_METHODCONST (cfg, argconst, cmethod);
4574 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
4575 NEW_TEMPLOAD (cfg, *sp, temp);
4579 inline_costs += 10 * num_calls++;
4582 case CEE_LDVIRTFTN: {
4587 n = read32 (ip + 2);
4588 if (method->wrapper_type != MONO_WRAPPER_NONE)
4589 cmethod = mono_method_get_wrapper_data (method, n);
4591 cmethod = mono_get_method (image, n, NULL);
4593 mono_class_init (cmethod->klass);
4594 handle_loaded_temps (cfg, bblock, stack_start, sp);
4598 NEW_METHODCONST (cfg, args [1], cmethod);
4599 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
4600 NEW_TEMPLOAD (cfg, *sp, temp);
4604 inline_costs += 10 * num_calls++;
4608 CHECK_STACK_OVF (1);
4609 NEW_ARGLOAD (cfg, ins, read16 (ip + 2));
4615 CHECK_STACK_OVF (1);
4616 NEW_ARGLOADA (cfg, ins, read16 (ip + 2));
4624 handle_loaded_temps (cfg, bblock, stack_start, sp);
4625 n = read16 (ip + 2);
4626 NEW_ARGSTORE (cfg, ins, n, *sp);
4628 if (ins->opcode == CEE_STOBJ) {
4629 NEW_ARGLOADA (cfg, ins, n);
4630 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4632 MONO_ADD_INS (bblock, ins);
4636 CHECK_STACK_OVF (1);
4637 NEW_LOCLOAD (cfg, ins, read16 (ip + 2));
4643 CHECK_STACK_OVF (1);
4644 NEW_LOCLOADA (cfg, ins, read16 (ip + 2));
4652 n = read16 (ip + 2);
4653 handle_loaded_temps (cfg, bblock, stack_start, sp);
4654 NEW_LOCSTORE (cfg, ins, n, *sp);
4656 if (ins->opcode == CEE_STOBJ) {
4657 NEW_LOCLOADA (cfg, ins, n);
4658 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4660 MONO_ADD_INS (bblock, ins);
4667 if (sp != stack_start)
4669 MONO_INST_NEW (cfg, ins, 256 + ip [1]);
4670 ins->inst_left = *sp;
4673 if (header->init_locals)
4674 ins->flags |= MONO_INST_INIT;
4678 /* FIXME: set init flag if locals init is set in this method */
4680 case CEE_ENDFILTER: {
4681 MonoExceptionClause *clause, *nearest;
4682 int cc, nearest_num;
4686 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
4688 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
4689 ins->inst_left = *sp;
4691 MONO_ADD_INS (bblock, ins);
4692 start_new_bblock = 1;
4696 for (cc = 0; cc < header->num_clauses; ++cc) {
4697 clause = &header->clauses [cc];
4698 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
4699 (!nearest || (clause->token_or_filter > nearest->token_or_filter))) {
4705 filter_lengths [nearest_num] = (ip - header->code) - nearest->token_or_filter;
4709 case CEE_UNALIGNED_:
4710 ins_flag |= MONO_INST_UNALIGNED;
4714 ins_flag |= MONO_INST_VOLATILE;
4718 ins_flag |= MONO_INST_TAILCALL;
4724 token = read32 (ip + 2);
4725 if (method->wrapper_type != MONO_WRAPPER_NONE)
4726 klass = mono_method_get_wrapper_data (method, token);
4728 klass = mono_class_get (image, token);
4729 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
4735 MonoInst *iargs [3];
4741 handle_loaded_temps (cfg, bblock, stack_start, sp);
4742 if (ip [1] == CEE_CPBLK) {
4743 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
4745 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
4753 /* FIXME: check we are in a catch handler */
4754 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
4755 load->cil_code = ip;
4756 MONO_INST_NEW (cfg, ins, CEE_THROW);
4757 ins->inst_left = load;
4759 MONO_ADD_INS (bblock, ins);
4761 start_new_bblock = 1;
4766 CHECK_STACK_OVF (1);
4767 token = read32 (ip + 2);
4768 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
4769 MonoType *type = mono_type_create_from_typespec (image, token);
4770 token = mono_type_size (type, &align);
4771 mono_metadata_free_type (type);
4773 MonoClass *szclass = mono_class_get (image, token);
4774 mono_class_init (szclass);
4775 token = mono_class_value_size (szclass, &align);
4777 NEW_ICONST (cfg, ins, token);
4782 case CEE_REFANYTYPE:
4783 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
4786 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
4791 g_error ("opcode 0x%02x not handled", *ip);
4794 if (start_new_bblock != 1)
4797 bblock->cil_length = ip - bblock->cil_code;
4798 bblock->next_bb = end_bblock;
4799 link_bblock (cfg, bblock, end_bblock);
4801 if (cfg->method == method && cfg->domainvar) {
4805 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
4806 call->signature = helper_sig_domain_get;
4807 call->inst.type = STACK_PTR;
4808 call->fptr = mono_domain_get;
4809 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, (MonoInst*)call);
4811 MONO_ADD_INS (init_localsbb, store);
4814 if (header->init_locals) {
4816 for (i = 0; i < header->num_locals; ++i) {
4817 int t = header->locals [i]->type;
4818 if (t == MONO_TYPE_VALUETYPE && header->locals [i]->data.klass->enumtype)
4819 t = header->locals [i]->data.klass->enum_basetype->type;
4820 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
4821 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
4822 NEW_ICONST (cfg, ins, 0);
4823 NEW_LOCSTORE (cfg, store, i, ins);
4824 MONO_ADD_INS (init_localsbb, store);
4825 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
4826 MONO_INST_NEW (cfg, ins, OP_I8CONST);
4827 ins->type = STACK_I8;
4829 NEW_LOCSTORE (cfg, store, i, ins);
4830 MONO_ADD_INS (init_localsbb, store);
4831 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
4832 MONO_INST_NEW (cfg, ins, OP_R8CONST);
4833 ins->type = STACK_R8;
4834 ins->inst_p0 = (void*)&r8_0;
4835 NEW_LOCSTORE (cfg, store, i, ins);
4836 MONO_ADD_INS (init_localsbb, store);
4837 } else if (t == MONO_TYPE_VALUETYPE) {
4838 NEW_LOCLOADA (cfg, ins, i);
4839 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (header->locals [i]), NULL, NULL);
4842 NEW_PCONST (cfg, ins, NULL);
4843 NEW_LOCSTORE (cfg, store, i, ins);
4844 MONO_ADD_INS (init_localsbb, store);
4850 /* resolve backward branches in the middle of an existing basic block */
4851 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
4853 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
4854 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
4855 if (tblock != start_bblock) {
4857 split_bblock (cfg, tblock, bblock);
4858 l = bblock->cil_code - header->code;
4859 bblock->cil_length = tblock->cil_length - l;
4860 tblock->cil_length = l;
4862 g_print ("recheck failed.\n");
4866 /* we compute regions here, because the length of filter clauses is not known in advance.
4867 * It is computed in the CEE_ENDFILTER case in the above switch statement*/
4868 if (cfg->method == method) {
4870 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
4871 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
4872 if (cfg->verbose_level > 2)
4873 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
4876 g_hash_table_destroy (bbhash);
4879 dont_inline = g_list_remove (dont_inline, method);
4880 return inline_costs;
4883 if (cfg->method != method)
4884 g_hash_table_destroy (bbhash);
4885 dont_inline = g_list_remove (dont_inline, method);
4889 if (cfg->method != method)
4890 g_hash_table_destroy (bbhash);
4891 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
4892 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
4893 dont_inline = g_list_remove (dont_inline, method);
4898 mono_print_tree (MonoInst *tree) {
4904 arity = mono_burg_arity [tree->opcode];
4906 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
4908 switch (tree->opcode) {
4910 printf ("[%d]", tree->inst_c0);
4913 printf ("[%lld]", tree->inst_l);
4916 printf ("[%f]", *(double*)tree->inst_p0);
4919 printf ("[%f]", *(float*)tree->inst_p0);
4923 printf ("[%d]", tree->inst_c0);
4926 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
4929 printf ("[%s]", mono_arch_regname (tree->dreg));
4932 printf ("[%s]", tree->inst_newa_class->name);
4933 mono_print_tree (tree->inst_newa_len);
4944 case OP_VOIDCALLVIRT: {
4945 MonoCallInst *call = (MonoCallInst*)tree;
4947 printf ("[%s]", call->method->name);
4952 printf ("[%d (", tree->inst_c0);
4953 for (i = 0; i < tree->inst_phi_args [0]; i++) {
4956 printf ("%d", tree->inst_phi_args [i + 1]);
4968 printf ("[B%d]", tree->inst_target_bb->block_num);
4978 case OP_VOIDCALL_REG:
4979 mono_print_tree (tree->inst_left);
4991 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
4992 mono_print_tree (tree->inst_left);
4996 mono_print_tree (tree->inst_left);
4998 mono_print_tree (tree->inst_right);
5008 create_helper_signature (void)
5010 /* FIXME: set call conv */
5011 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
5012 helper_sig_newarr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5013 helper_sig_newarr->params [0] = helper_sig_newarr->params [1] = &mono_defaults.int_class->byval_arg;
5014 helper_sig_newarr->ret = &mono_defaults.object_class->byval_arg;
5015 helper_sig_newarr->params [2] = &mono_defaults.int32_class->byval_arg;
5016 helper_sig_newarr->pinvoke = 1;
5018 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
5019 helper_sig_newarr_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5020 helper_sig_newarr_specific->params [0] = &mono_defaults.int_class->byval_arg;
5021 helper_sig_newarr_specific->params [1] = &mono_defaults.int32_class->byval_arg;
5022 helper_sig_newarr_specific->ret = &mono_defaults.object_class->byval_arg;
5023 helper_sig_newarr_specific->pinvoke = 1;
5025 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
5026 helper_sig_object_new = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5027 helper_sig_object_new->params [0] = helper_sig_object_new->params [1] = &mono_defaults.int_class->byval_arg;
5028 helper_sig_object_new->ret = &mono_defaults.object_class->byval_arg;
5029 helper_sig_object_new->pinvoke = 1;
5031 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
5032 helper_sig_object_new_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5033 helper_sig_object_new_specific->params [0] = &mono_defaults.int_class->byval_arg;
5034 helper_sig_object_new_specific->ret = &mono_defaults.object_class->byval_arg;
5035 helper_sig_object_new_specific->pinvoke = 1;
5037 /* void* mono_method_compile (MonoMethod*) */
5038 helper_sig_compile = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5039 helper_sig_compile->params [0] = helper_sig_compile->ret = &mono_defaults.int_class->byval_arg;
5040 helper_sig_compile->pinvoke = 1;
5042 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
5043 helper_sig_compile_virt = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5044 helper_sig_compile_virt->params [0] = &mono_defaults.object_class->byval_arg;
5045 helper_sig_compile_virt->params [1] = helper_sig_compile_virt->ret = &mono_defaults.int_class->byval_arg;
5046 helper_sig_compile_virt->pinvoke = 1;
5048 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
5049 helper_sig_ldstr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5050 helper_sig_ldstr->params [0] = helper_sig_ldstr->params [1] = &mono_defaults.int_class->byval_arg;
5051 helper_sig_ldstr->params [2] = &mono_defaults.int32_class->byval_arg;
5052 helper_sig_ldstr->ret = &mono_defaults.object_class->byval_arg;
5053 helper_sig_ldstr->pinvoke = 1;
5055 /* MonoDomain *mono_domain_get (void) */
5056 helper_sig_domain_get = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5057 helper_sig_domain_get->ret = &mono_defaults.int_class->byval_arg;
5058 helper_sig_domain_get->pinvoke = 1;
5060 /* void* stelem_ref (MonoArray *, int index, MonoObject *) */
5061 helper_sig_stelem_ref = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5062 helper_sig_stelem_ref->params [0] = &mono_defaults.array_class->byval_arg;
5063 helper_sig_stelem_ref->params [1] = &mono_defaults.int32_class->byval_arg;
5064 helper_sig_stelem_ref->params [2] = &mono_defaults.object_class->byval_arg;
5065 helper_sig_stelem_ref->ret = &mono_defaults.void_class->byval_arg;
5066 helper_sig_stelem_ref->pinvoke = 1;
5068 /* long amethod (long, long) */
5069 helper_sig_long_long_long = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5070 helper_sig_long_long_long->params [0] = helper_sig_long_long_long->params [1] =
5071 &mono_defaults.int64_class->byval_arg;
5072 helper_sig_long_long_long->ret = &mono_defaults.int64_class->byval_arg;
5073 helper_sig_long_long_long->pinvoke = 1;
5075 /* object amethod (intptr) */
5076 helper_sig_obj_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5077 helper_sig_obj_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5078 helper_sig_obj_ptr->ret = &mono_defaults.object_class->byval_arg;
5079 helper_sig_obj_ptr->pinvoke = 1;
5081 /* void amethod (intptr) */
5082 helper_sig_void_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5083 helper_sig_void_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5084 helper_sig_void_ptr->ret = &mono_defaults.void_class->byval_arg;
5085 helper_sig_void_ptr->pinvoke = 1;
5087 /* void amethod (MonoObject *obj) */
5088 helper_sig_void_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5089 helper_sig_void_obj->params [0] = &mono_defaults.object_class->byval_arg;
5090 helper_sig_void_obj->ret = &mono_defaults.void_class->byval_arg;
5091 helper_sig_void_obj->pinvoke = 1;
5093 /* intptr amethod (void) */
5094 helper_sig_ptr_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5095 helper_sig_ptr_void->ret = &mono_defaults.int_class->byval_arg;
5096 helper_sig_ptr_void->pinvoke = 1;
5098 /* void amethod (intptr, intptr) */
5099 helper_sig_void_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5100 helper_sig_void_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5101 helper_sig_void_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5102 helper_sig_void_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
5103 helper_sig_void_ptr_ptr->pinvoke = 1;
5105 /* void amethod (intptr, intptr, intptr) */
5106 helper_sig_void_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5107 helper_sig_void_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5108 helper_sig_void_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5109 helper_sig_void_ptr_ptr_ptr->params [2] = &mono_defaults.int_class->byval_arg;
5110 helper_sig_void_ptr_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
5111 helper_sig_void_ptr_ptr_ptr->pinvoke = 1;
5113 /* intptr amethod (intptr, intptr) */
5114 helper_sig_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5115 helper_sig_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5116 helper_sig_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5117 helper_sig_ptr_ptr_ptr->ret = &mono_defaults.int_class->byval_arg;
5118 helper_sig_ptr_ptr_ptr->pinvoke = 1;
5120 /* IntPtr amethod (object) */
5121 helper_sig_ptr_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5122 helper_sig_ptr_obj->params [0] = &mono_defaults.object_class->byval_arg;
5123 helper_sig_ptr_obj->ret = &mono_defaults.int_class->byval_arg;
5124 helper_sig_ptr_obj->pinvoke = 1;
5126 /* IntPtr amethod (int) */
5127 helper_sig_ptr_int = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5128 helper_sig_ptr_int->params [0] = &mono_defaults.int32_class->byval_arg;
5129 helper_sig_ptr_int->ret = &mono_defaults.int_class->byval_arg;
5130 helper_sig_ptr_int->pinvoke = 1;
5132 /* long amethod (long, guint32) */
5133 helper_sig_long_long_int = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5134 helper_sig_long_long_int->params [0] = &mono_defaults.int64_class->byval_arg;
5135 helper_sig_long_long_int->params [1] = &mono_defaults.int32_class->byval_arg;
5136 helper_sig_long_long_int->ret = &mono_defaults.int64_class->byval_arg;
5137 helper_sig_long_long_int->pinvoke = 1;
5139 /* ulong amethod (double) */
5140 helper_sig_ulong_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5141 helper_sig_ulong_double->params [0] = &mono_defaults.double_class->byval_arg;
5142 helper_sig_ulong_double->ret = &mono_defaults.uint64_class->byval_arg;
5143 helper_sig_ulong_double->pinvoke = 1;
5145 /* long amethod (double) */
5146 helper_sig_long_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5147 helper_sig_long_double->params [0] = &mono_defaults.double_class->byval_arg;
5148 helper_sig_long_double->ret = &mono_defaults.int64_class->byval_arg;
5149 helper_sig_long_double->pinvoke = 1;
5151 /* uint amethod (double) */
5152 helper_sig_uint_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5153 helper_sig_uint_double->params [0] = &mono_defaults.double_class->byval_arg;
5154 helper_sig_uint_double->ret = &mono_defaults.uint32_class->byval_arg;
5155 helper_sig_uint_double->pinvoke = 1;
5157 /* int amethod (double) */
5158 helper_sig_int_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5159 helper_sig_int_double->params [0] = &mono_defaults.double_class->byval_arg;
5160 helper_sig_int_double->ret = &mono_defaults.int32_class->byval_arg;
5161 helper_sig_int_double->pinvoke = 1;
5163 /* void initobj (intptr, int size) */
5164 helper_sig_initobj = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5165 helper_sig_initobj->params [0] = &mono_defaults.int_class->byval_arg;
5166 helper_sig_initobj->params [1] = &mono_defaults.int32_class->byval_arg;
5167 helper_sig_initobj->ret = &mono_defaults.void_class->byval_arg;
5168 helper_sig_initobj->pinvoke = 1;
5170 /* void memcpy (intptr, intptr, int size) */
5171 helper_sig_memcpy = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5172 helper_sig_memcpy->params [0] = &mono_defaults.int_class->byval_arg;
5173 helper_sig_memcpy->params [1] = &mono_defaults.int_class->byval_arg;
5174 helper_sig_memcpy->params [2] = &mono_defaults.int32_class->byval_arg;
5175 helper_sig_memcpy->ret = &mono_defaults.void_class->byval_arg;
5176 helper_sig_memcpy->pinvoke = 1;
5178 /* void memset (intptr, int val, int size) */
5179 helper_sig_memset = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5180 helper_sig_memset->params [0] = &mono_defaults.int_class->byval_arg;
5181 helper_sig_memset->params [1] = &mono_defaults.int32_class->byval_arg;
5182 helper_sig_memset->params [2] = &mono_defaults.int32_class->byval_arg;
5183 helper_sig_memset->ret = &mono_defaults.void_class->byval_arg;
5184 helper_sig_memset->pinvoke = 1;
5187 static GHashTable *jit_icall_hash_name = NULL;
5188 static GHashTable *jit_icall_hash_addr = NULL;
5191 mono_find_jit_icall_by_name (const char *name)
5193 g_assert (jit_icall_hash_name);
5195 //printf ("lookup addr %s %p\n", name, g_hash_table_lookup (jit_icall_hash_name, name));
5196 return g_hash_table_lookup (jit_icall_hash_name, name);
5200 mono_find_jit_icall_by_addr (gconstpointer addr)
5202 g_assert (jit_icall_hash_addr);
5204 return g_hash_table_lookup (jit_icall_hash_addr, (gpointer)addr);
5208 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
5211 MonoMethod *wrapper;
5213 if (callinfo->wrapper)
5214 return callinfo->wrapper;
5215 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
5216 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
5217 callinfo->wrapper = mono_jit_compile_method (wrapper);
5219 return callinfo->wrapper;
5223 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save)
5225 MonoJitICallInfo *info;
5230 if (!jit_icall_hash_name) {
5231 jit_icall_hash_name = g_hash_table_new (g_str_hash, g_str_equal);
5232 jit_icall_hash_addr = g_hash_table_new (NULL, NULL);
5235 if (g_hash_table_lookup (jit_icall_hash_name, name)) {
5236 g_warning ("jit icall already defined \"%s\"\n", name);
5237 g_assert_not_reached ();
5240 info = g_new (MonoJitICallInfo, 1);
5247 #ifdef MONO_USE_EXC_TABLES
5248 || mono_arch_has_unwind_info (func)
5251 info->wrapper = func;
5253 info->wrapper = NULL;
5254 mono_icall_get_wrapper (info);
5257 g_hash_table_insert (jit_icall_hash_name, (gpointer)info->name, info);
5258 g_hash_table_insert (jit_icall_hash_addr, (gpointer)func, info);
5259 if (func != info->wrapper)
5260 g_hash_table_insert (jit_icall_hash_addr, (gpointer)info->wrapper, info);
5265 static GHashTable *emul_opcode_hash = NULL;
5267 static MonoJitICallInfo *
5268 mono_find_jit_opcode_emulation (int opcode)
5270 if (emul_opcode_hash)
5271 return g_hash_table_lookup (emul_opcode_hash, (gpointer)opcode);
5277 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func)
5279 MonoJitICallInfo *info;
5281 if (!emul_opcode_hash)
5282 emul_opcode_hash = g_hash_table_new (NULL, NULL);
5284 g_assert (!sig->hasthis);
5285 g_assert (sig->param_count < 3);
5287 info = mono_register_jit_icall (func, name, sig, FALSE);
5289 g_hash_table_insert (emul_opcode_hash, (gpointer)opcode, info);
5293 decompose_foreach (MonoInst *tree, gpointer data)
5295 static MonoJitICallInfo *newarr_info = NULL;
5296 static MonoJitICallInfo *newarr_specific_info = NULL;
5297 MonoJitICallInfo *info;
5299 switch (tree->opcode) {
5301 MonoCompile *cfg = data;
5302 MonoInst *iargs [3];
5305 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
5306 g_assert (newarr_info);
5307 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
5308 g_assert (newarr_specific_info);
5311 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
5312 NEW_DOMAINCONST (cfg, iargs [0]);
5313 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
5314 iargs [2] = tree->inst_newa_len;
5319 MonoVTable *vtable = mono_class_vtable (cfg->domain,
5320 mono_array_class_get (&tree->inst_newa_class->byval_arg, 1));
5322 NEW_PCONST (cfg, iargs [0], vtable);
5323 iargs [1] = tree->inst_newa_len;
5325 info = newarr_specific_info;
5328 mono_emulate_opcode (cfg, tree, iargs, info);
5338 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
5340 switch (mono_burg_arity [tree->opcode]) {
5343 mono_inst_foreach (tree->inst_left, func, data);
5346 mono_inst_foreach (tree->inst_left, func, data);
5347 mono_inst_foreach (tree->inst_right, func, data);
5350 g_assert_not_reached ();
5357 mono_print_bb_code (MonoBasicBlock *bb) {
5359 MonoInst *c = bb->code;
5361 mono_print_tree (c);
5370 print_dfn (MonoCompile *cfg) {
5375 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
5377 for (i = 0; i < cfg->num_bblocks; ++i) {
5378 bb = cfg->bblocks [i];
5380 char* code1, *code2;
5381 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
5382 if (bb->last_ins->cil_code)
5383 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
5385 code2 = g_strdup ("");
5387 code1 [strlen (code1) - 1] = 0;
5388 code = g_strdup_printf ("%s -> %s", code1, code2);
5392 code = g_strdup ("\n");
5393 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
5395 MonoInst *c = bb->code;
5397 mono_print_tree (c);
5405 g_print ("\tprev:");
5406 for (j = 0; j < bb->in_count; ++j) {
5407 g_print (" BB%d", bb->in_bb [j]->block_num);
5409 g_print ("\t\tsucc:");
5410 for (j = 0; j < bb->out_count; ++j) {
5411 g_print (" BB%d", bb->out_bb [j]->block_num);
5413 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
5416 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
5419 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
5421 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
5429 * returns the offset used by spillvar. It allocates a new
5430 * spill variable if necessary.
5433 mono_spillvar_offset (MonoCompile *cfg, int spillvar)
5435 MonoSpillInfo **si, *info;
5438 si = &cfg->spill_info;
5440 while (i <= spillvar) {
5443 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
5445 cfg->stack_offset -= sizeof (gpointer);
5446 info->offset = cfg->stack_offset;
5450 return (*si)->offset;
5456 g_assert_not_reached ();
5461 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
5465 g_assert (bb->code);
5466 bb->last_ins->next = inst;
5467 bb->last_ins = inst;
5469 bb->last_ins = bb->code = inst;
5474 mono_destroy_compile (MonoCompile *cfg)
5476 //mono_mempool_stats (cfg->mempool);
5477 g_hash_table_destroy (cfg->bb_hash);
5479 mono_regstate_free (cfg->rs);
5480 mono_mempool_destroy (cfg->mempool);
5481 g_list_free (cfg->ldstr_list);
5483 g_free (cfg->varinfo);
5489 mono_get_lmf_addr (void)
5491 MonoJitTlsData *jit_tls;
5493 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
5494 return &jit_tls->lmf;
5496 g_assert_not_reached ();
5501 * mono_thread_abort:
5502 * @obj: exception object
5504 * abort the thread, print exception information and stack trace
5507 mono_thread_abort (MonoObject *obj)
5509 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
5517 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
5519 MonoJitTlsData *jit_tls;
5522 jit_tls = g_new0 (MonoJitTlsData, 1);
5524 TlsSetValue (mono_jit_tls_id, jit_tls);
5526 jit_tls->abort_func = mono_thread_abort;
5527 jit_tls->end_of_stack = stack_start;
5529 lmf = g_new0 (MonoLMF, 1);
5535 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
5538 mono_thread_abort_dummy (MonoObject *obj)
5540 if (mono_thread_attach_aborted_cb)
5541 mono_thread_attach_aborted_cb (obj);
5543 mono_thread_abort (obj);
5547 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
5549 MonoJitTlsData *jit_tls;
5552 jit_tls = g_new0 (MonoJitTlsData, 1);
5554 TlsSetValue (mono_jit_tls_id, jit_tls);
5556 jit_tls->abort_func = mono_thread_abort_dummy;
5557 jit_tls->end_of_stack = stack_start;
5559 lmf = g_new0 (MonoLMF, 1);
5566 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
5568 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
5572 ji->data.target = target;
5573 ji->next = cfg->patch_info;
5575 cfg->patch_info = ji;
5579 mono_remove_patch_info (MonoCompile *cfg, int ip)
5581 MonoJumpInfo **ji = &cfg->patch_info;
5584 if ((*ji)->ip.i == ip)
5587 ji = &((*ji)->next);
5592 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
5593 MonoJitICallInfo *info;
5595 switch (mono_burg_arity [tree->opcode]) {
5598 dec_foreach (tree->inst_left, cfg);
5600 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
5601 MonoInst *iargs [2];
5603 iargs [0] = tree->inst_left;
5605 mono_emulate_opcode (cfg, tree, iargs, info);
5611 if (tree->opcode == OP_LMUL
5612 && (cfg->opt & MONO_OPT_INTRINS)
5613 && (tree->inst_left->opcode == CEE_CONV_I8
5614 || tree->inst_left->opcode == CEE_CONV_U8)
5615 && tree->inst_left->inst_left->type == STACK_I4
5616 && (tree->inst_right->opcode == CEE_CONV_I8
5617 || tree->inst_right->opcode == CEE_CONV_U8)
5618 && tree->inst_right->inst_left->type == STACK_I4) {
5619 tree->opcode = OP_BIGMUL;
5620 tree->inst_left = tree->inst_left->inst_left;
5621 tree->inst_right = tree->inst_right->inst_left;
5622 dec_foreach (tree, cfg);
5623 } else if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
5624 MonoInst *iargs [2];
5626 iargs [0] = tree->inst_i0;
5627 iargs [1] = tree->inst_i1;
5629 mono_emulate_opcode (cfg, tree, iargs, info);
5631 dec_foreach (iargs [0], cfg);
5632 dec_foreach (iargs [1], cfg);
5635 dec_foreach (tree->inst_left, cfg);
5636 dec_foreach (tree->inst_right, cfg);
5640 g_assert_not_reached ();
5642 decompose_foreach (tree, cfg);
5646 decompose_pass (MonoCompile *cfg) {
5649 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5652 cfg->prev_ins = NULL;
5653 for (tree = cfg->cbb->code; tree; tree = tree->next) {
5654 dec_foreach (tree, cfg);
5655 cfg->prev_ins = tree;
5661 nullify_basic_block (MonoBasicBlock *bb)
5668 bb->code = bb->last_ins = NULL;
5672 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
5676 for (i = 0; i < bb->out_count; i++) {
5677 MonoBasicBlock *ob = bb->out_bb [i];
5678 for (j = 0; j < ob->in_count; j++) {
5679 if (ob->in_bb [j] == orig)
5680 ob->in_bb [j] = repl;
5687 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
5689 bb->out_count = bbn->out_count;
5690 bb->out_bb = bbn->out_bb;
5692 replace_basic_block (bb, bbn, bb);
5696 bb->last_ins->next = bbn->code;
5697 bb->last_ins = bbn->last_ins;
5700 bb->code = bbn->code;
5701 bb->last_ins = bbn->last_ins;
5703 bb->next_bb = bbn->next_bb;
5704 nullify_basic_block (bbn);
5708 optimize_branches (MonoCompile *cfg) {
5709 int changed = FALSE;
5710 MonoBasicBlock *bb, *bbn;
5715 /* we skip the entry block (exit is handled specially instead ) */
5716 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
5718 /* dont touch code inside exception clauses */
5719 if (bb->region != -1)
5722 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
5723 if (cfg->verbose_level > 2)
5724 g_print ("nullify block triggered %d\n", bbn->block_num);
5726 bb->next_bb = bbn->next_bb;
5727 nullify_basic_block (bbn);
5731 if (bb->out_count == 1) {
5732 bbn = bb->out_bb [0];
5734 if (bb->region == bbn->region && bb->next_bb == bbn) {
5735 /* the block are in sequence anyway ... */
5738 * miguel: I do not understand what the test below does, could we
5739 * use a macro, or a comment here? opcode > CEE_BEQ && <= BLT_UN
5741 * It could also test for bb->last_in only once, and the value
5742 * could be cached (last_ins->opcode)
5744 if (bb->last_ins && (bb->last_ins->opcode == CEE_BR || (
5745 (bb->last_ins && bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN)))) {
5746 bb->last_ins->opcode = CEE_NOP;
5748 if (cfg->verbose_level > 2)
5749 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
5751 if (bbn->in_count == 1) {
5753 if (bbn != cfg->bb_exit) {
5754 if (cfg->verbose_level > 2)
5755 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
5756 merge_basic_blocks (bb, bbn);
5760 //mono_print_bb_code (bb);
5770 /* we skip the entry block (exit is handled specially instead ) */
5771 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
5773 /* dont touch code inside exception clauses */
5774 if (bb->region != -1)
5777 if (bb->out_count == 1) {
5778 bbn = bb->out_bb [0];
5780 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
5781 bbn = bb->last_ins->inst_target_bb;
5782 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
5783 bbn->code->inst_target_bb->region == bb->region) {
5785 if (cfg->verbose_level > 2)
5786 g_print ("in %s branch to branch triggered %d -> %d\n", cfg->method->name,
5787 bb->block_num, bbn->block_num);
5789 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
5790 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
5795 } else if (bb->out_count == 2) {
5796 /* fixme: this does not correctly - no idea whats wrong */
5797 if (0 && bb->last_ins && bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
5798 bbn = bb->last_ins->inst_true_bb;
5799 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
5800 bbn->code->inst_target_bb->region == bb->region) {
5801 if (cfg->verbose_level > 2)
5802 g_print ("cbranch to branch triggered %d -> %d (0x%02x)\n", bb->block_num,
5803 bbn->block_num, bbn->code->opcode);
5805 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
5806 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
5810 bbn = bb->last_ins->inst_false_bb;
5811 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
5812 bbn->code->inst_target_bb->region == bb->region) {
5813 if (cfg->verbose_level > 2)
5814 g_print ("cbranch to branch triggered %d -> %d (0x%02x)\n", bb->block_num,
5815 bbn->block_num, bbn->code->opcode);
5817 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
5818 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
5830 mono_compile_create_vars (MonoCompile *cfg)
5832 MonoMethodSignature *sig;
5833 MonoMethodHeader *header;
5836 header = ((MonoMethodNormal *)cfg->method)->header;
5838 sig = cfg->method->signature;
5840 if (!MONO_TYPE_IS_VOID (sig->ret)) {
5841 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
5842 cfg->ret->opcode = OP_RETARG;
5843 cfg->ret->inst_vtype = sig->ret;
5844 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
5848 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
5850 for (i = 0; i < sig->param_count; ++i)
5851 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
5853 cfg->locals_start = cfg->num_varinfo;
5855 for (i = 0; i < header->num_locals; ++i)
5856 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
5861 mono_print_code (MonoCompile *cfg)
5865 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5866 MonoInst *tree = bb->code;
5871 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
5873 for (; tree; tree = tree->next) {
5874 mono_print_tree (tree);
5879 bb->last_ins->next = NULL;
5884 extern const char * const mono_burg_rule_string [];
5887 emit_state (MonoCompile *cfg, MBState *state, int goal)
5890 int ern = mono_burg_rule (state, goal);
5891 const guint16 *nts = mono_burg_nts [ern];
5894 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
5898 // state->reg1 = state->reg2; /* chain rule */
5900 state->reg1 = mono_regstate_next_int (cfg->rs);
5901 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
5904 state->reg1 = mono_regstate_next_int (cfg->rs);
5905 state->reg2 = mono_regstate_next_int (cfg->rs);
5908 state->reg1 = mono_regstate_next_float (cfg->rs);
5915 mono_burg_kids (state, ern, kids);
5917 emit_state (cfg, kids [0], nts [0]);
5919 emit_state (cfg, kids [1], nts [1]);
5921 g_assert (!nts [3]);
5922 emit_state (cfg, kids [2], nts [2]);
5927 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
5928 if ((emit = mono_burg_func [ern]))
5929 emit (state, state->tree, cfg);
5932 #define DEBUG_SELECTION
5935 mini_select_instructions (MonoCompile *cfg)
5939 cfg->state_pool = mono_mempool_new ();
5940 cfg->rs = mono_regstate_new ();
5942 #ifdef DEBUG_SELECTION
5943 if (cfg->verbose_level >= 4) {
5944 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5945 MonoInst *tree = bb->code;
5946 g_print ("DUMP BLOCK %d:\n", bb->block_num);
5949 for (; tree; tree = tree->next) {
5950 mono_print_tree (tree);
5957 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5958 MonoInst *tree = bb->code, *next;
5964 bb->last_ins = NULL;
5967 mono_regstate_reset (cfg->rs);
5969 #ifdef DEBUG_SELECTION
5970 if (cfg->verbose_level >= 3)
5971 g_print ("LABEL BLOCK %d:\n", bb->block_num);
5973 for (; tree; tree = next) {
5975 #ifdef DEBUG_SELECTION
5976 if (cfg->verbose_level >= 3) {
5977 mono_print_tree (tree);
5982 if (!(mbstate = mono_burg_label (tree, cfg))) {
5983 g_warning ("unabled to label tree %p", tree);
5984 mono_print_tree (tree);
5986 g_assert_not_reached ();
5988 emit_state (cfg, mbstate, MB_NTERM_stmt);
5990 bb->max_ireg = cfg->rs->next_vireg;
5991 bb->max_freg = cfg->rs->next_vfreg;
5994 bb->last_ins->next = NULL;
5996 mono_mempool_empty (cfg->state_pool);
5998 mono_mempool_destroy (cfg->state_pool);
6002 mono_codegen (MonoCompile *cfg)
6004 MonoJumpInfo *patch_info;
6006 int i, max_epilog_size;
6009 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6010 cfg->spill_count = 0;
6011 /* we reuse dfn here */
6012 /* bb->dfn = bb_count++; */
6013 mono_arch_local_regalloc (cfg, bb);
6016 if (mono_trace_coverage)
6017 mono_allocate_coverage_info (cfg->method, cfg->num_bblocks);
6019 code = mono_arch_emit_prolog (cfg);
6021 if (mono_jit_profile)
6022 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
6024 cfg->code_len = code - cfg->native_code;
6025 cfg->prolog_end = cfg->code_len;
6027 mono_debug_open_method (cfg);
6029 /* emit code all basic blocks */
6030 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6031 bb->native_offset = cfg->code_len;
6032 mono_arch_output_basic_block (cfg, bb);
6034 cfg->bb_exit->native_offset = cfg->code_len;
6036 code = cfg->native_code + cfg->code_len;
6038 max_epilog_size = mono_arch_max_epilog_size (cfg);
6040 /* we always allocate code in cfg->domain->code_mp to increase locality */
6041 cfg->code_size = cfg->code_len + max_epilog_size;
6042 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
6043 code = mono_mempool_alloc (cfg->domain->code_mp, cfg->code_size);
6044 memcpy (code, cfg->native_code, cfg->code_len);
6045 g_free (cfg->native_code);
6046 cfg->native_code = code;
6047 code = cfg->native_code + cfg->code_len;
6049 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
6051 cfg->epilog_begin = cfg->code_len;
6053 if (mono_jit_profile)
6054 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
6056 cfg->code_len = code - cfg->native_code;
6058 mono_arch_emit_epilog (cfg);
6060 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
6061 switch (patch_info->type) {
6062 case MONO_PATCH_INFO_ABS: {
6063 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
6065 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
6066 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
6067 patch_info->data.name = info->name;
6071 case MONO_PATCH_INFO_SWITCH: {
6072 gpointer *table = g_new (gpointer, patch_info->table_size);
6073 patch_info->ip.i = patch_info->ip.label->inst_c0;
6074 for (i = 0; i < patch_info->table_size; i++) {
6075 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
6077 patch_info->data.target = table;
6086 if (cfg->verbose_level > 1)
6087 g_print ("Method %s::%s emmitted at %p to %p\n", cfg->method->klass->name,
6088 cfg->method->name, cfg->native_code, cfg->native_code + cfg->code_len);
6090 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info);
6092 mono_debug_close_method (cfg);
6096 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
6101 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
6102 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
6104 if (cp->opcode == OP_ICONST) {
6105 if (cfg->opt & MONO_OPT_CONSPROP) {
6106 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
6110 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
6111 if (cfg->opt & MONO_OPT_COPYPROP) {
6112 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
6118 arity = mono_burg_arity [tree->opcode];
6121 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
6122 if (cfg->opt & MONO_OPT_CFOLD)
6123 mono_constant_fold_inst (tree, NULL);
6125 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
6126 if (cfg->opt & MONO_OPT_CFOLD)
6127 mono_constant_fold_inst (tree, NULL);
6129 mono_constant_fold_inst (tree, NULL);
6135 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
6139 switch (tree->opcode) {
6149 if (tree->ssa_op == MONO_SSA_NOP) {
6150 memset (acp, 0, sizeof (MonoInst *) * acp_size);
6167 case OP_VOIDCALL_REG:
6168 case OP_VOIDCALLVIRT:
6170 MonoCallInst *call = (MonoCallInst *)tree;
6171 MonoMethodSignature *sig = call->signature;
6172 int i, byref = FALSE;
6174 for (i = 0; i < sig->param_count; i++) {
6175 if (sig->params [i]->byref) {
6182 memset (acp, 0, sizeof (MonoInst *) * acp_size);
6190 arity = mono_burg_arity [tree->opcode];
6196 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
6199 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
6200 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
6203 g_assert_not_reached ();
6208 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
6210 MonoInst *tree = bb->code;
6216 for (; tree; tree = tree->next) {
6218 mono_cprop_copy_values (cfg, tree, acp);
6220 mono_cprop_invalidate_values (tree, acp, acp_size);
6222 if (tree->ssa_op == MONO_SSA_STORE &&
6223 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
6224 MonoInst *i1 = tree->inst_i1;
6226 acp [tree->inst_i0->inst_c0] = NULL;
6228 for (i = 0; i < acp_size; i++) {
6229 if (acp [i] && acp [i]->opcode != OP_ICONST &&
6230 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
6235 if (i1->opcode == OP_ICONST) {
6236 acp [tree->inst_i0->inst_c0] = i1;
6237 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
6239 if (i1->ssa_op == MONO_SSA_LOAD &&
6240 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
6241 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
6242 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
6243 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
6248 if (tree->opcode == CEE_BEQ) {
6249 g_assert (tree->inst_i0->opcode == OP_COMPARE);
6250 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
6251 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
6253 tree->opcode = CEE_BR;
6254 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
6255 tree->inst_target_bb = tree->inst_true_bb;
6257 tree->inst_target_bb = tree->inst_false_bb;
6266 mono_local_cprop (MonoCompile *cfg)
6271 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
6273 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6274 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
6275 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
6280 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, int parts)
6282 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
6283 guint8 *ip = (guint8 *)header->code;
6286 int dfn = 0, i, code_size_ratio;
6288 mono_jit_stats.methods_compiled++;
6289 if (mono_jit_profile)
6290 mono_profiler_method_jit (method);
6292 cfg = g_new0 (MonoCompile, 1);
6293 cfg->method = method;
6294 cfg->mempool = mono_mempool_new ();
6296 cfg->bb_hash = g_hash_table_new (g_direct_hash, NULL);
6297 cfg->domain = domain;
6298 cfg->verbose_level = mini_verbose;
6299 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
6300 ((MonoMethodNormal *)method)->header->max_stack);
6303 * create MonoInst* which represents arguments and local variables
6305 mono_compile_create_vars (cfg);
6307 if (cfg->verbose_level > 2)
6308 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
6310 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
6311 mono_destroy_compile (cfg);
6312 if (mono_jit_profile)
6313 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
6317 mono_jit_stats.basic_blocks += cfg->num_bblocks;
6318 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
6320 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
6322 /* Depth-first ordering on basic blocks */
6323 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
6325 if (cfg->opt & MONO_OPT_BRANCH)
6326 optimize_branches (cfg);
6328 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
6329 if (cfg->num_bblocks != dfn + 1) {
6332 cfg->num_bblocks = dfn + 1;
6334 if (!header->clauses) {
6335 /* remove unreachable code, because the code in them may be
6336 * inconsistent (access to dead variables for example) */
6337 for (bb = cfg->bb_entry; bb;) {
6338 MonoBasicBlock *bbn = bb->next_bb;
6340 if (bbn && bbn->region == -1 && !bbn->dfn) {
6341 if (cfg->verbose_level > 1)
6342 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
6343 bb->next_bb = bbn->next_bb;
6344 nullify_basic_block (bbn);
6352 if (cfg->opt & MONO_OPT_LOOP) {
6353 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
6354 mono_compute_natural_loops (cfg);
6358 /* after method_to_ir */
6362 //#define DEBUGSSA "logic_run"
6363 #define DEBUGSSA_CLASS "Tests"
6367 if (!header->num_clauses && !cfg->disable_ssa) {
6368 mono_local_cprop (cfg);
6369 mono_ssa_compute (cfg);
6373 /* fixme: add all optimizations which requires SSA */
6374 if (cfg->opt & (MONO_OPT_DEADCE)) {
6375 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
6376 mono_local_cprop (cfg);
6377 mono_ssa_compute (cfg);
6379 if (cfg->verbose_level >= 2) {
6386 /* after SSA translation */
6390 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
6391 if (cfg->comp_done & MONO_COMP_SSA) {
6392 mono_ssa_cprop (cfg);
6394 mono_local_cprop (cfg);
6398 if (cfg->comp_done & MONO_COMP_SSA) {
6399 mono_ssa_deadce (cfg);
6401 //mono_ssa_strength_reduction (cfg);
6403 mono_ssa_remove (cfg);
6405 if (cfg->opt & MONO_OPT_BRANCH)
6406 optimize_branches (cfg);
6409 /* after SSA removal */
6413 decompose_pass (cfg);
6415 /* FIXME: disabled with exception clauses: bug #42136 */
6416 if ((!header->num_clauses) && (cfg->opt & MONO_OPT_LINEARS)) {
6419 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
6420 cfg->comp_done &= ~MONO_COMP_LIVENESS;
6421 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
6422 mono_analyze_liveness (cfg);
6424 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
6425 regs = mono_arch_get_global_int_regs (cfg);
6426 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
6430 //mono_print_code (cfg);
6434 /* variables are allocated after decompose, since decompose could create temps */
6435 mono_arch_allocate_vars (cfg);
6437 if (cfg->opt & MONO_OPT_CFOLD)
6438 mono_constant_fold (cfg);
6440 mini_select_instructions (cfg);
6443 if (cfg->verbose_level >= 2) {
6444 char *id = mono_method_full_name (cfg->method, FALSE);
6445 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
6449 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
6451 jinfo = g_new0 (MonoJitInfo, 1);
6452 jinfo->method = method;
6453 jinfo->code_start = cfg->native_code;
6454 jinfo->code_size = cfg->code_len;
6455 jinfo->used_regs = cfg->used_int_regs;
6457 if (header->num_clauses) {
6460 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
6461 jinfo->num_clauses = header->num_clauses;
6462 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
6463 sizeof (MonoJitExceptionInfo) * header->num_clauses);
6465 for (i = 0; i < header->num_clauses; i++) {
6466 MonoExceptionClause *ec = &header->clauses [i];
6467 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
6468 MonoBasicBlock *tblock;
6470 ei->flags = ec->flags;
6472 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
6473 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->token_or_filter);
6475 ei->data.filter = cfg->native_code + tblock->native_offset;
6477 ei->data.token = ec->token_or_filter;
6480 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
6482 ei->try_start = cfg->native_code + tblock->native_offset;
6483 g_assert (tblock->native_offset);
6484 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
6486 ei->try_end = cfg->native_code + tblock->native_offset;
6487 g_assert (tblock->native_offset);
6488 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
6490 ei->handler_start = cfg->native_code + tblock->native_offset;
6494 mono_jit_info_table_add (cfg->domain, jinfo);
6496 /* collect statistics */
6497 mono_jit_stats.allocated_code_size += cfg->code_len;
6498 code_size_ratio = cfg->code_len;
6499 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
6500 mono_jit_stats.biggest_method_size = code_size_ratio;
6501 mono_jit_stats.biggest_method = method;
6503 code_size_ratio = (code_size_ratio * 100) / ((MonoMethodNormal *)method)->header->code_size;
6504 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
6505 mono_jit_stats.max_code_size_ratio = code_size_ratio;
6506 mono_jit_stats.max_ratio_method = method;
6508 mono_jit_stats.native_code_size += cfg->code_len;
6510 if (mono_jit_profile)
6511 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
6517 mono_jit_compile_method (MonoMethod *method)
6519 /* FIXME: later copy the code from mono */
6520 MonoDomain *target_domain, *domain = mono_domain_get ();
6522 GHashTable *jit_code_hash;
6525 if (default_opt & MONO_OPT_SHARED)
6526 target_domain = mono_root_domain;
6528 target_domain = domain;
6530 jit_code_hash = target_domain->jit_code_hash;
6532 if ((code = g_hash_table_lookup (jit_code_hash, method))) {
6533 mono_jit_stats.methods_lookups++;
6537 #ifdef MONO_USE_AOT_COMPILER
6538 if (!mono_compile_aot) {
6539 mono_class_init (method->klass);
6540 if ((code = mono_aot_get_method (method))) {
6541 g_hash_table_insert (jit_code_hash, method, code);
6547 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
6548 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
6549 if (!method->info) {
6552 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
6553 mono_lookup_pinvoke_call (method);
6554 #ifdef MONO_USE_EXC_TABLES
6555 if (mono_method_blittable (method)) {
6556 method->info = method->addr;
6559 nm = mono_marshal_get_native_wrapper (method);
6560 method->info = mono_compile_method (nm);
6562 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
6563 //mono_debug_add_wrapper (method, nm);
6564 #ifdef MONO_USE_EXC_TABLES
6568 return method->info;
6569 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
6570 const char *name = method->name;
6573 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
6574 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
6575 /* FIXME: uhm, we need a wrapper to handle exceptions? */
6576 return (gpointer)mono_delegate_ctor;
6577 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
6578 nm = mono_marshal_get_delegate_invoke (method);
6579 return mono_jit_compile_method (nm);
6580 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
6581 nm = mono_marshal_get_delegate_begin_invoke (method);
6582 return mono_jit_compile_method (nm);
6583 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
6584 nm = mono_marshal_get_delegate_end_invoke (method);
6585 return mono_jit_compile_method (nm);
6591 cfg = mini_method_compile (method, default_opt, target_domain, 0);
6592 code = cfg->native_code;
6593 mono_destroy_compile (cfg);
6595 g_hash_table_insert (jit_code_hash, method, code);
6597 if (target_domain->jump_target_hash) {
6598 MonoJumpInfo patch_info;
6600 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
6602 patch_info.next = NULL;
6603 patch_info.ip.i = 0;
6604 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
6605 patch_info.data.method = method;
6606 g_hash_table_remove (target_domain->jump_target_hash, method);
6608 for (tmp = list; tmp; tmp = tmp->next)
6609 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info);
6610 g_slist_free (list);
6612 /* make sure runtime_init is called */
6613 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
6619 * mono_jit_runtime_invoke:
6620 * @method: the method to invoke
6621 * @obj: this pointer
6622 * @params: array of parameter values.
6623 * @exc: used to catch exceptions objects
6626 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
6629 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc);
6630 invoke = mono_marshal_get_runtime_invoke (method);
6631 runtime_invoke = mono_jit_compile_method (invoke);
6632 return runtime_invoke (obj, params, exc);
6635 #ifdef PLATFORM_WIN32
6636 #define GET_CONTEXT \
6637 struct sigcontext *ctx = (struct sigcontext*)_dummy;
6639 #define GET_CONTEXT \
6640 void **_p = (void **)&_dummy; \
6641 struct sigcontext *ctx = (struct sigcontext *)++_p;
6645 sigfpe_signal_handler (int _dummy)
6650 exc = mono_get_exception_divide_by_zero ();
6652 mono_arch_handle_exception (ctx, exc, FALSE);
6656 sigill_signal_handler (int _dummy)
6660 exc = mono_get_exception_execution_engine ("SIGILL");
6662 mono_arch_handle_exception (ctx, exc, FALSE);
6666 sigsegv_signal_handler (int _dummy)
6671 exc = mono_get_exception_null_reference ();
6673 mono_arch_handle_exception (ctx, exc, FALSE);
6677 sigusr1_signal_handler (int _dummy)
6682 thread = mono_thread_current ();
6684 g_assert (thread->abort_exc);
6686 mono_arch_handle_exception (ctx, thread->abort_exc, FALSE);
6690 mono_runtime_install_handlers (void)
6692 #ifndef PLATFORM_WIN32
6693 struct sigaction sa;
6696 #ifdef PLATFORM_WIN32
6698 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
6699 win32_seh_set_handler(SIGILL, sigill_signal_handler);
6700 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
6701 #else /* !PLATFORM_WIN32 */
6703 /* libpthreads has its own implementation of sigaction(),
6704 * but it seems to work well with our current exception
6705 * handlers. If not we must call syscall directly instead
6709 sa.sa_handler = sigfpe_signal_handler;
6710 sigemptyset (&sa.sa_mask);
6712 //g_assert (syscall (SYS_sigaction, SIGFPE, &sa, NULL) != -1);
6713 g_assert (sigaction (SIGFPE, &sa, NULL) != -1);
6716 sa.sa_handler = sigill_signal_handler;
6717 sigemptyset (&sa.sa_mask);
6719 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
6720 g_assert (sigaction (SIGILL, &sa, NULL) != -1);
6722 /* catch thread abort signal */
6723 sa.sa_handler = sigusr1_signal_handler;
6724 sigemptyset (&sa.sa_mask);
6726 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
6727 g_assert (sigaction (mono_thread_get_abort_signal (), &sa, NULL) != -1);
6731 sa.sa_handler = sigsegv_signal_handler;
6732 sigemptyset (&sa.sa_mask);
6734 //g_assert (syscall (SYS_sigaction, SIGSEGV, &sa, NULL) != -1);
6735 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
6737 #endif /* PLATFORM_WIN32 */
6740 /* mono_jit_create_remoting_trampoline:
6741 * @method: pointer to the method info
6743 * Creates a trampoline which calls the remoting functions. This
6744 * is used in the vtable of transparent proxies.
6746 * Returns: a pointer to the newly created code
6749 mono_jit_create_remoting_trampoline (MonoMethod *method)
6752 guint8 *addr = NULL;
6754 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
6755 (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
6756 nm = mono_marshal_get_remoting_invoke (method);
6757 addr = mono_compile_method (nm);
6759 addr = mono_compile_method (method);
6764 static CRITICAL_SECTION ms;
6767 mini_init (const char *filename)
6771 metadata_section = &ms;
6772 InitializeCriticalSection (metadata_section);
6774 mono_jit_tls_id = TlsAlloc ();
6775 mono_thread_start_cb (GetCurrentThreadId (), (gpointer)-1, NULL);
6779 mono_runtime_install_handlers ();
6781 mono_install_compile_method (mono_jit_compile_method);
6782 mono_install_trampoline (mono_arch_create_jit_trampoline);
6783 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
6784 mono_install_runtime_invoke (mono_jit_runtime_invoke);
6785 mono_install_handler (mono_arch_get_throw_exception ());
6786 mono_install_stack_walk (mono_jit_walk_stack);
6787 mono_install_get_config_dir ();
6789 domain = mono_init (filename);
6792 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
6793 ves_icall_get_frame_info);
6794 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
6795 ves_icall_get_trace);
6796 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
6797 mono_runtime_install_handlers);
6800 create_helper_signature ();
6802 mono_arch_register_lowlevel_calls ();
6803 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
6804 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
6806 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
6807 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
6809 /* fixme: we cant hanlde vararg methods this way, because the signature is not constant */
6810 //mono_register_jit_icall (ves_array_element_address, "ves_array_element_address", NULL);
6811 //mono_register_jit_icall (mono_array_new_va, "mono_array_new_va", NULL);
6813 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
6814 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
6815 helper_sig_void_ptr, TRUE);
6818 * NOTE, NOTE, NOTE, NOTE:
6819 * when adding emulation for some opcodes, remember to also add a dummy
6820 * rule to the burg files, because we need the arity information to be correct.
6822 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult);
6823 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un);
6824 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf);
6825 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv);
6826 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un);
6827 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem);
6828 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un);
6830 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl);
6831 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr);
6832 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un);
6834 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8);
6835 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4);
6836 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8);
6837 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8);
6839 #if SIZEOF_VOID_P == 4
6840 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4);
6842 #warning "fixme: add opcode emulation"
6845 /* other jit icalls */
6846 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
6847 helper_sig_ptr_ptr_ptr, FALSE);
6848 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
6849 mono_register_jit_icall (mono_threads_get_static_data, "mono_threads_get_static_data", helper_sig_ptr_int, FALSE);
6850 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
6851 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
6852 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
6853 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
6854 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
6855 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
6856 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
6857 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
6858 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
6859 mono_register_jit_icall (mono_string_to_utf16, "mono_string_to_utf16", helper_sig_ptr_obj, FALSE);
6860 mono_register_jit_icall (mono_string_from_utf16, "mono_string_from_utf16", helper_sig_obj_ptr, FALSE);
6861 mono_register_jit_icall (mono_string_new_wrapper, "mono_string_new_wrapper", helper_sig_obj_ptr, FALSE);
6862 mono_register_jit_icall (mono_string_to_utf8, "mono_string_to_utf8", helper_sig_ptr_obj, FALSE);
6863 mono_register_jit_icall (mono_string_to_bstr, "mono_string_to_bstr", helper_sig_ptr_obj, FALSE);
6864 mono_register_jit_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", helper_sig_ptr_obj, FALSE);
6865 mono_register_jit_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", helper_sig_ptr_obj, FALSE);
6866 mono_register_jit_icall (mono_array_to_savearray, "mono_array_to_savearray", helper_sig_ptr_obj, FALSE);
6867 mono_register_jit_icall (mono_array_to_lparray, "mono_array_to_lparray", helper_sig_ptr_obj, FALSE);
6868 mono_register_jit_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", helper_sig_ptr_obj, FALSE);
6869 mono_register_jit_icall (mono_marshal_string_array, "mono_marshal_string_array", helper_sig_ptr_obj, FALSE);
6870 mono_register_jit_icall (mono_string_utf8_to_builder, "mono_string_utf8_to_builder", helper_sig_void_ptr_ptr, FALSE);
6871 mono_register_jit_icall (mono_marshal_free_array, "mono_marshal_free_array", helper_sig_void_ptr_ptr, FALSE);
6872 mono_register_jit_icall (mono_string_to_byvalstr, "mono_string_to_byvalstr", helper_sig_void_ptr_ptr_ptr, FALSE);
6873 mono_register_jit_icall (mono_string_to_byvalwstr, "mono_string_to_byvalwstr", helper_sig_void_ptr_ptr_ptr, FALSE);
6874 mono_register_jit_icall (g_free, "g_free", helper_sig_void_ptr, FALSE);
6875 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
6876 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
6877 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
6879 mono_runtime_init (domain, mono_thread_start_cb,
6880 mono_thread_attach_cb);
6882 //mono_thread_attach (domain);
6886 MonoJitStats mono_jit_stats = {0};
6889 print_jit_stats (void)
6891 if (mono_jit_stats.enabled) {
6892 g_print ("Mono Jit statistics\n");
6893 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
6894 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
6895 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
6896 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
6897 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
6898 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
6899 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
6900 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
6901 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
6902 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
6903 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
6904 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
6905 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
6906 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
6907 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
6908 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
6909 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
6910 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
6912 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
6913 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
6914 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
6915 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
6916 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
6921 mini_cleanup (MonoDomain *domain)
6924 * mono_runtime_cleanup() and mono_domain_finalize () need to
6925 * be called early since they need the execution engine still
6926 * fully working (mono_domain_finalize may invoke managed finalizers
6927 * and mono_runtime_cleanup will wait for other threads to finish).
6929 mono_domain_finalize (domain);
6931 mono_runtime_cleanup (domain);
6933 mono_profiler_shutdown ();
6935 mono_debug_cleanup ();
6936 #ifdef PLATFORM_WIN32
6937 win32_seh_cleanup();
6940 mono_domain_unload (domain, TRUE);
6943 DeleteCriticalSection (metadata_section);
6947 mono_set_defaults (int verbose_level, guint32 opts)
6949 mini_verbose = verbose_level;