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;
96 MonoDebugFormat mono_debug_format = MONO_DEBUG_FORMAT_NONE;
98 CRITICAL_SECTION *metadata_section = NULL;
100 static int mini_verbose = 0;
102 #ifdef MONO_USE_EXC_TABLES
104 mono_type_blittable (MonoType *type)
123 case MONO_TYPE_OBJECT:
125 case MONO_TYPE_VALUETYPE:
126 case MONO_TYPE_CLASS:
127 return type->data.klass->blittable;
137 mono_method_blittable (MonoMethod *method)
139 MonoMethodSignature *sig;
145 if (!mono_arch_has_unwind_info (method->addr)) {
149 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
152 sig = method->signature;
154 if (!mono_type_blittable (sig->ret))
157 for (i = 0; i < sig->param_count; i++)
158 if (!mono_type_blittable (sig->params [i]))
166 G_GNUC_UNUSED static void
167 print_method_from_ip (void *ip)
172 MonoDomain *domain = mono_domain_get ();
174 ji = mono_jit_info_table_find (domain, ip);
176 g_print ("No method at %p\n", ip);
179 method = mono_method_full_name (ji->method, TRUE);
180 source = mono_debug_source_location_from_address (ji->method, (int) ip, NULL, domain);
182 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);
185 g_print ("%s\n", source);
192 #define MONO_INIT_VARINFO(vi,id) do { \
193 (vi)->range.first_use.pos.bid = 0xffff; \
199 * Basic blocks have two numeric identifiers:
200 * dfn: Depth First Number
201 * block_num: unique ID assigned at bblock creation
203 #define NEW_BBLOCK(cfg) (mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)))
204 #define ADD_BBLOCK(cfg,bbhash,b) do { \
205 g_hash_table_insert (bbhash, (b)->cil_code, (b)); \
206 (b)->block_num = cfg->num_bblocks++; \
207 (b)->real_offset = real_offset; \
210 #define GET_BBLOCK(cfg,bbhash,tblock,ip) do { \
211 (tblock) = g_hash_table_lookup (bbhash, (ip)); \
213 if ((ip) >= end || (ip) < header->code) goto unverified; \
214 (tblock) = NEW_BBLOCK (cfg); \
215 (tblock)->cil_code = (ip); \
216 ADD_BBLOCK (cfg, (bbhash), (tblock)); \
218 (tblock)->real_offset = real_offset; \
221 #define CHECK_BBLOCK(target,ip,tblock) do { \
222 if ((target) < (ip) && !(tblock)->code) { \
223 bb_recheck = g_list_prepend (bb_recheck, (tblock)); \
224 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); \
228 #define NEW_ICONST(cfg,dest,val) do { \
229 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
230 (dest)->opcode = OP_ICONST; \
231 (dest)->inst_c0 = (val); \
232 (dest)->type = STACK_I4; \
235 /* FIXME: have a different definition of NEW_PCONST for 64 bit systems */
236 #define NEW_PCONST(cfg,dest,val) do { \
237 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
238 (dest)->opcode = OP_ICONST; \
239 (dest)->inst_p0 = (val); \
240 (dest)->type = STACK_PTR; \
243 #define NEW_CLASSCONST(cfg,dest,val) do { \
244 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
245 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
246 (dest)->inst_p0 = (val); \
247 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_CLASS; \
248 (dest)->type = STACK_PTR; \
251 #define NEW_IMAGECONST(cfg,dest,val) do { \
252 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
253 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
254 (dest)->inst_p0 = (val); \
255 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_IMAGE; \
256 (dest)->type = STACK_PTR; \
259 #define NEW_FIELDCONST(cfg,dest,field) do { \
260 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
261 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
262 (dest)->inst_p0 = (field); \
263 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_FIELD; \
264 (dest)->type = STACK_PTR; \
267 #define NEW_METHODCONST(cfg,dest,val) do { \
268 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
269 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
270 (dest)->inst_p0 = (val); \
271 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_METHODCONST; \
272 (dest)->type = STACK_PTR; \
275 #define NEW_DOMAINCONST(cfg,dest) do { \
276 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) { \
277 NEW_TEMPLOAD (cfg, dest, mono_get_domainvar (cfg)->inst_c0); \
279 NEW_PCONST (cfg, dest, (cfg)->domain); \
283 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
285 #define NEW_ARGLOAD(cfg,dest,num) do { \
286 if (arg_array [(num)]->opcode == OP_ICONST) (dest) = arg_array [(num)]; else { \
287 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
288 (dest)->ssa_op = MONO_SSA_LOAD; \
289 (dest)->inst_i0 = arg_array [(num)]; \
290 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
291 type_to_eval_stack_type (param_types [(num)], (dest)); \
292 (dest)->klass = (dest)->inst_i0->klass; \
295 #define NEW_LOCLOAD(cfg,dest,num) do { \
296 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
297 (dest)->ssa_op = MONO_SSA_LOAD; \
298 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
299 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
300 type_to_eval_stack_type (header->locals [(num)], (dest)); \
301 (dest)->klass = (dest)->inst_i0->klass; \
304 #define NEW_LOCLOADA(cfg,dest,num) do { \
305 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
306 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
307 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
308 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
309 (dest)->opcode = OP_LDADDR; \
310 (dest)->type = STACK_MP; \
311 (dest)->klass = (dest)->inst_i0->klass; \
312 (cfg)->disable_ssa = TRUE; \
315 #define NEW_RETLOADA(cfg,dest) do { \
316 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
317 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
318 (dest)->inst_i0 = (cfg)->ret; \
319 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
320 (dest)->opcode = CEE_LDIND_I; \
321 (dest)->type = STACK_MP; \
322 (dest)->klass = (dest)->inst_i0->klass; \
323 (cfg)->disable_ssa = TRUE; \
326 #define NEW_ARGLOADA(cfg,dest,num) do { \
327 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
328 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
329 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
330 (dest)->inst_i0 = arg_array [(num)]; \
331 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
332 (dest)->opcode = OP_LDADDR; \
333 (dest)->type = STACK_MP; \
334 (dest)->klass = (dest)->inst_i0->klass; \
335 (cfg)->disable_ssa = TRUE; \
338 #define NEW_TEMPLOAD(cfg,dest,num) do { \
339 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
340 (dest)->ssa_op = MONO_SSA_LOAD; \
341 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
342 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
343 type_to_eval_stack_type ((dest)->inst_i0->inst_vtype, (dest)); \
344 (dest)->klass = (dest)->inst_i0->klass; \
347 #define NEW_TEMPLOADA(cfg,dest,num) do { \
348 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
349 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
350 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
351 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
352 (dest)->opcode = OP_LDADDR; \
353 (dest)->type = STACK_MP; \
354 (dest)->klass = (dest)->inst_i0->klass; \
355 (cfg)->disable_ssa = TRUE; \
359 #define NEW_INDLOAD(cfg,dest,addr,vtype) do { \
360 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
361 (dest)->inst_left = addr; \
362 (dest)->opcode = mono_type_to_ldind (vtype); \
363 type_to_eval_stack_type (vtype, (dest)); \
364 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
367 #define NEW_INDSTORE(cfg,dest,addr,value,vtype) do { \
368 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
369 (dest)->inst_i0 = addr; \
370 (dest)->opcode = mono_type_to_stind (vtype); \
371 (dest)->inst_i1 = (value); \
372 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
375 #define NEW_TEMPSTORE(cfg,dest,num,inst) do { \
376 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
377 (dest)->ssa_op = MONO_SSA_STORE; \
378 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
379 (dest)->opcode = mono_type_to_stind ((dest)->inst_i0->inst_vtype); \
380 (dest)->inst_i1 = (inst); \
381 (dest)->klass = (dest)->inst_i0->klass; \
384 #define NEW_LOCSTORE(cfg,dest,num,inst) do { \
385 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
386 (dest)->opcode = mono_type_to_stind (header->locals [(num)]); \
387 (dest)->ssa_op = MONO_SSA_STORE; \
388 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
389 (dest)->inst_i1 = (inst); \
390 (dest)->klass = (dest)->inst_i0->klass; \
393 #define NEW_ARGSTORE(cfg,dest,num,inst) do { \
394 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
395 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
396 (dest)->opcode = mono_type_to_stind (param_types [(num)]); \
397 (dest)->ssa_op = MONO_SSA_STORE; \
398 (dest)->inst_i0 = arg_array [(num)]; \
399 (dest)->inst_i1 = (inst); \
400 (dest)->klass = (dest)->inst_i0->klass; \
403 #define ADD_BINOP(op) do { \
404 MONO_INST_NEW (cfg, ins, (op)); \
405 ins->cil_code = ip; \
407 ins->inst_i0 = sp [0]; \
408 ins->inst_i1 = sp [1]; \
410 type_from_op (ins); \
414 #define ADD_UNOP(op) do { \
415 MONO_INST_NEW (cfg, ins, (op)); \
416 ins->cil_code = ip; \
418 ins->inst_i0 = sp [0]; \
420 type_from_op (ins); \
424 #define ADD_BINCOND(next_block) do { \
426 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
428 cmp->inst_i0 = sp [0]; \
429 cmp->inst_i1 = sp [1]; \
430 cmp->cil_code = ins->cil_code; \
431 type_from_op (cmp); \
433 ins->inst_i0 = cmp; \
434 MONO_ADD_INS (bblock, ins); \
435 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
436 GET_BBLOCK (cfg, bbhash, tblock, target); \
437 link_bblock (cfg, bblock, tblock); \
438 ins->inst_true_bb = tblock; \
439 CHECK_BBLOCK (target, ip, tblock); \
440 if ((next_block)) { \
441 link_bblock (cfg, bblock, (next_block)); \
442 ins->inst_false_bb = (next_block); \
443 start_new_bblock = 1; \
445 GET_BBLOCK (cfg, bbhash, tblock, ip); \
446 link_bblock (cfg, bblock, tblock); \
447 ins->inst_false_bb = tblock; \
448 start_new_bblock = 2; \
452 /* FIXME: handle float, long ... */
453 #define ADD_UNCOND(istrue) do { \
455 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
457 cmp->inst_i0 = sp [0]; \
458 switch (cmp->inst_i0->type) { \
460 cmp->inst_i1 = zero_int64; break; \
462 cmp->inst_i1 = zero_r8; break; \
465 cmp->inst_i1 = zero_ptr; break; \
467 cmp->inst_i1 = zero_obj; break; \
469 cmp->inst_i1 = zero_int32; \
471 cmp->cil_code = ins->cil_code; \
472 type_from_op (cmp); \
474 ins->inst_i0 = cmp; \
475 ins->opcode = (istrue)? CEE_BNE_UN: CEE_BEQ; \
476 MONO_ADD_INS (bblock, ins); \
477 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
478 GET_BBLOCK (cfg, bbhash, tblock, target); \
479 link_bblock (cfg, bblock, tblock); \
480 ins->inst_true_bb = tblock; \
481 CHECK_BBLOCK (target, ip, tblock); \
482 GET_BBLOCK (cfg, bbhash, tblock, ip); \
483 link_bblock (cfg, bblock, tblock); \
484 ins->inst_false_bb = tblock; \
485 start_new_bblock = 2; \
488 #define NEW_LDELEMA(cfg,dest,sp,k) do { \
489 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
490 (dest)->opcode = CEE_LDELEMA; \
491 (dest)->inst_left = (sp) [0]; \
492 (dest)->inst_right = (sp) [1]; \
493 (dest)->type = STACK_MP; \
494 (dest)->klass = (k); \
497 #define NEW_GROUP(cfg,dest,el1,el2) do { \
498 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
499 (dest)->opcode = OP_GROUP; \
500 (dest)->inst_left = (el1); \
501 (dest)->inst_right = (el2); \
504 static GHashTable *coverage_hash = NULL;
507 mono_allocate_coverage_info (MonoMethod *method, int size)
509 MonoCoverageInfo *res;
512 coverage_hash = g_hash_table_new (NULL, NULL);
514 res = g_malloc0 (sizeof (MonoCoverageInfo) + sizeof (int) * size * 2);
518 g_hash_table_insert (coverage_hash, method, res);
524 mono_get_coverage_info (MonoMethod *method)
529 return g_hash_table_lookup (coverage_hash, method);
534 compare_bblock (gconstpointer a, gconstpointer b)
536 const MonoBasicBlock *b1 = a;
537 const MonoBasicBlock *b2 = b;
539 return b2->cil_code - b1->cil_code;
544 * link_bblock: Links two basic blocks
546 * links two basic blocks in the control flow graph, the 'from'
547 * argument is the starting block and the 'to' argument is the block
548 * the control flow ends to after 'from'.
551 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
553 MonoBasicBlock **newa;
557 if (from->cil_code) {
559 g_print ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
561 g_print ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
564 g_print ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
566 g_print ("edge from entry to exit\n");
570 for (i = 0; i < from->out_count; ++i) {
571 if (to == from->out_bb [i]) {
577 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
578 for (i = 0; i < from->out_count; ++i) {
579 newa [i] = from->out_bb [i];
587 for (i = 0; i < to->in_count; ++i) {
588 if (from == to->in_bb [i]) {
594 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
595 for (i = 0; i < to->in_count; ++i) {
596 newa [i] = to->in_bb [i];
605 * We mark each basic block with a region ID. We use that to avoid BB
606 * optimizations when blocks are in different regions.
609 mono_find_block_region (MonoCompile *cfg, int offset, int *filter_lengths)
611 MonoMethod *method = cfg->method;
612 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
613 MonoExceptionClause *clause;
616 /* first search for handlers and filters */
617 for (i = 0; i < header->num_clauses; ++i) {
618 clause = &header->clauses [i];
619 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->token_or_filter) &&
620 (offset < (clause->token_or_filter + filter_lengths [i])))
621 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
623 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
624 if (clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)
625 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
627 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
631 /* search the try blocks */
632 for (i = 0; i < header->num_clauses; ++i) {
633 clause = &header->clauses [i];
634 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
635 return ((i + 1) << 8) | clause->flags;
642 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
644 MonoMethod *method = cfg->method;
645 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
646 MonoExceptionClause *clause;
647 MonoBasicBlock *handler;
651 for (i = 0; i < header->num_clauses; ++i) {
652 clause = &header->clauses [i];
653 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
654 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
655 if (clause->flags == type) {
656 handler = g_hash_table_lookup (cfg->bb_hash, header->code + clause->handler_offset);
658 res = g_list_append (res, handler);
667 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
671 array [*dfn] = start;
672 /*g_print ("visit %d at %p\n", *dfn, start->cil_code);*/
673 for (i = 0; i < start->out_count; ++i) {
674 if (start->out_bb [i]->dfn)
677 start->out_bb [i]->dfn = *dfn;
678 start->out_bb [i]->df_parent = start;
679 array [*dfn] = start->out_bb [i];
680 df_visit (start->out_bb [i], dfn, array);
686 MonoBasicBlock *best;
690 previous_foreach (gconstpointer key, gpointer val, gpointer data)
692 PrevStruct *p = data;
693 MonoBasicBlock *bb = val;
694 //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,
695 //bb->method == p->best->method, bb->cil_code < p->code, bb->cil_code > p->best->cil_code);
697 if (bb->cil_code && bb->cil_code < p->code && bb->cil_code > p->best->cil_code)
701 static MonoBasicBlock*
702 find_previous (GHashTable *bb_hash, MonoBasicBlock *start, const guchar *code) {
708 g_hash_table_foreach (bb_hash, (GHFunc)previous_foreach, &p);
713 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
722 * FIXME: take into account all the details:
723 * second may have been the target of more than one bblock
725 second->out_count = first->out_count;
726 second->out_bb = first->out_bb;
728 for (i = 0; i < first->out_count; ++i) {
729 bb = first->out_bb [i];
730 for (j = 0; j < bb->in_count; ++j) {
731 if (bb->in_bb [j] == first)
732 bb->in_bb [j] = second;
736 first->out_count = 0;
737 first->out_bb = NULL;
738 link_bblock (cfg, first, second);
740 second->last_ins = first->last_ins;
742 /*g_print ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
743 for (inst = first->code; inst && inst->next; inst = inst->next) {
744 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
745 g_print ("found %p: %s", inst->next->cil_code, code);
747 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
748 second->code = inst->next;
750 first->last_ins = inst;
751 second->next_bb = first->next_bb;
752 first->next_bb = second;
757 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
763 mono_type_to_ldind (MonoType *type)
775 case MONO_TYPE_BOOLEAN:
789 case MONO_TYPE_FNPTR:
791 case MONO_TYPE_CLASS:
792 case MONO_TYPE_STRING:
793 case MONO_TYPE_OBJECT:
794 case MONO_TYPE_SZARRAY:
795 case MONO_TYPE_ARRAY:
796 return CEE_LDIND_REF;
804 case MONO_TYPE_VALUETYPE:
805 if (type->data.klass->enumtype) {
806 t = type->data.klass->enum_basetype->type;
810 case MONO_TYPE_TYPEDBYREF:
813 g_error ("unknown type 0x%02x in type_to_ldind", type->type);
819 mono_type_to_stind (MonoType *type)
830 case MONO_TYPE_BOOLEAN:
842 case MONO_TYPE_FNPTR:
844 case MONO_TYPE_CLASS:
845 case MONO_TYPE_STRING:
846 case MONO_TYPE_OBJECT:
847 case MONO_TYPE_SZARRAY:
848 case MONO_TYPE_ARRAY:
849 return CEE_STIND_REF;
857 case MONO_TYPE_VALUETYPE:
858 if (type->data.klass->enumtype) {
859 t = type->data.klass->enum_basetype->type;
863 case MONO_TYPE_TYPEDBYREF:
866 g_error ("unknown type 0x%02x in type_to_stind", type->type);
872 * Returns the type used in the eval stack when @type is loaded.
873 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
876 type_to_eval_stack_type (MonoType *type, MonoInst *inst) {
880 inst->type = STACK_MP;
888 case MONO_TYPE_BOOLEAN:
894 inst->type = STACK_I4;
899 case MONO_TYPE_FNPTR:
900 inst->type = STACK_PTR;
902 case MONO_TYPE_CLASS:
903 case MONO_TYPE_STRING:
904 case MONO_TYPE_OBJECT:
905 case MONO_TYPE_SZARRAY:
906 case MONO_TYPE_ARRAY:
907 inst->type = STACK_OBJ;
911 inst->type = STACK_I8;
915 inst->type = STACK_R8;
917 case MONO_TYPE_VALUETYPE:
918 if (type->data.klass->enumtype) {
919 t = type->data.klass->enum_basetype->type;
922 inst->klass = type->data.klass;
923 inst->type = STACK_VTYPE;
926 case MONO_TYPE_TYPEDBYREF:
927 inst->klass = mono_defaults.typed_reference_class;
928 inst->type = STACK_VTYPE;
931 g_error ("unknown type 0x%02x in eval stack type", type->type);
936 * The following tables are used to quickly validate the IL code in type_from_op ().
939 bin_num_table [STACK_MAX] [STACK_MAX] = {
940 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
941 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
942 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
943 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
944 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
945 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
946 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
947 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
952 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
955 /* reduce the size of this table */
957 bin_int_table [STACK_MAX] [STACK_MAX] = {
958 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
959 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
960 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
961 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, 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},
965 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
969 bin_comp_table [STACK_MAX] [STACK_MAX] = {
971 {0, 1, 0, 1, 0, 0, 4, 0},
972 {0, 0, 1, 0, 0, 0, 0, 0},
973 {0, 1, 0, 1, 0, 2, 4, 0},
974 {0, 0, 0, 0, 1, 0, 0, 0},
975 {0, 0, 0, 2, 0, 1, 0, 0},
976 {0, 4, 0, 4, 0, 0, 3, 0},
977 {0, 0, 0, 0, 0, 0, 0, 0},
980 /* reduce the size of this table */
982 shift_table [STACK_MAX] [STACK_MAX] = {
983 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
984 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
985 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
986 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, 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},
990 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
994 * Tables to map from the non-specific opcode to the matching
995 * type-specific opcode.
997 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
999 binops_op_map [STACK_MAX] = {
1000 0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, 0
1003 /* handles from CEE_NEG to CEE_CONV_U8 */
1004 static const guint16
1005 unops_op_map [STACK_MAX] = {
1006 0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, 0
1009 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
1010 static const guint16
1011 ovfops_op_map [STACK_MAX] = {
1012 0, 0, OP_LCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_FCONV_TO_U2-CEE_CONV_U2, 0
1015 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
1016 static const guint16
1017 ovf2ops_op_map [STACK_MAX] = {
1018 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
1021 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
1022 static const guint16
1023 ovf3ops_op_map [STACK_MAX] = {
1024 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
1027 /* handles from CEE_CEQ to CEE_CLT_UN */
1028 static const guint16
1029 ceqops_op_map [STACK_MAX] = {
1030 0, 0, OP_LCEQ-CEE_CEQ, OP_PCEQ-CEE_CEQ, OP_FCEQ-CEE_CEQ, 0
1034 * Sets ins->type (the type on the eval stack) according to the
1035 * type of the opcode and the arguments to it.
1036 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1038 * FIXME: this function sets ins->type unconditionally in some cases, but
1039 * it should set it to invalid for some types (a conv.x on an object)
1042 type_from_op (MonoInst *ins) {
1043 switch (ins->opcode) {
1050 /* FIXME: check unverifiable args for STACK_MP */
1051 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1052 ins->opcode += binops_op_map [ins->type];
1059 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1060 ins->opcode += binops_op_map [ins->type];
1065 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1066 ins->opcode += binops_op_map [ins->type];
1069 /* FIXME: handle some specifics with ins->next->type */
1070 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1074 case 256+CEE_CGT_UN:
1076 case 256+CEE_CLT_UN:
1077 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1078 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1082 ins->type = neg_table [ins->inst_i0->type];
1083 ins->opcode += unops_op_map [ins->type];
1086 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1087 ins->type = ins->inst_i0->type;
1089 ins->type = STACK_INV;
1090 ins->opcode += unops_op_map [ins->type];
1096 ins->type = STACK_I4;
1097 ins->opcode += unops_op_map [ins->inst_i0->type];
1100 ins->type = STACK_R8;
1101 switch (ins->inst_i0->type) {
1106 ins->opcode = OP_LCONV_TO_R_UN;
1110 case CEE_CONV_OVF_I1:
1111 case CEE_CONV_OVF_U1:
1112 case CEE_CONV_OVF_I2:
1113 case CEE_CONV_OVF_U2:
1114 case CEE_CONV_OVF_I4:
1115 case CEE_CONV_OVF_U4:
1116 ins->type = STACK_I4;
1117 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1119 case CEE_CONV_OVF_I_UN:
1120 case CEE_CONV_OVF_U_UN:
1121 ins->type = STACK_PTR;
1122 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1124 case CEE_CONV_OVF_I1_UN:
1125 case CEE_CONV_OVF_I2_UN:
1126 case CEE_CONV_OVF_I4_UN:
1127 case CEE_CONV_OVF_U1_UN:
1128 case CEE_CONV_OVF_U2_UN:
1129 case CEE_CONV_OVF_U4_UN:
1130 ins->type = STACK_I4;
1131 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1134 ins->type = STACK_PTR;
1135 switch (ins->inst_i0->type) {
1141 ins->opcode = OP_LCONV_TO_U;
1144 ins->opcode = OP_FCONV_TO_U;
1150 ins->type = STACK_I8;
1151 ins->opcode += unops_op_map [ins->inst_i0->type];
1153 case CEE_CONV_OVF_I8:
1154 case CEE_CONV_OVF_U8:
1155 ins->type = STACK_I8;
1156 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1158 case CEE_CONV_OVF_U8_UN:
1159 case CEE_CONV_OVF_I8_UN:
1160 ins->type = STACK_I8;
1161 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1165 ins->type = STACK_R8;
1166 ins->opcode += unops_op_map [ins->inst_i0->type];
1169 ins->type = STACK_R8;
1173 ins->type = STACK_I4;
1174 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1177 case CEE_CONV_OVF_I:
1178 case CEE_CONV_OVF_U:
1179 ins->type = STACK_PTR;
1180 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1183 case CEE_ADD_OVF_UN:
1185 case CEE_MUL_OVF_UN:
1187 case CEE_SUB_OVF_UN:
1188 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1189 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1192 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1199 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_MP, STACK_R8, STACK_R8, STACK_OBJ
1202 /* map ldelem.x to the matching ldind.x opcode */
1204 ldelem_to_ldind [] = {
1218 /* map stelem.x to the matching stind.x opcode */
1220 stelem_to_stind [] = {
1234 param_table [STACK_MAX] [STACK_MAX] = {
1239 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1243 switch (args->type) {
1253 for (i = 0; i < sig->param_count; ++i) {
1254 switch (args [i].type) {
1258 if (!sig->params [i]->byref)
1262 if (sig->params [i]->byref)
1264 switch (sig->params [i]->type) {
1265 case MONO_TYPE_CLASS:
1266 case MONO_TYPE_STRING:
1267 case MONO_TYPE_OBJECT:
1268 case MONO_TYPE_SZARRAY:
1269 case MONO_TYPE_ARRAY:
1276 if (sig->params [i]->byref)
1278 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1287 /*if (!param_table [args [i].type] [sig->params [i]->type])
1295 * When we need a pointer to the current domain many times in a method, we
1296 * call mono_domain_get() once and we store the result in a local variable.
1297 * This function returns the variable that represents the MonoDomain*.
1299 inline static MonoInst *
1300 mono_get_domainvar (MonoCompile *cfg)
1302 if (!cfg->domainvar)
1303 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1304 return cfg->domainvar;
1308 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
1311 int num = cfg->num_varinfo;
1313 if ((num + 1) >= cfg->varinfo_count) {
1314 cfg->varinfo_count = (cfg->varinfo_count + 2) * 2;
1315 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
1316 cfg->vars = (MonoMethodVar **)g_realloc (cfg->vars, sizeof (MonoMethodVar*) * cfg->varinfo_count);
1319 mono_jit_stats.allocate_var++;
1321 MONO_INST_NEW (cfg, inst, opcode);
1322 inst->inst_c0 = num;
1323 inst->inst_vtype = type;
1324 inst->klass = mono_class_from_mono_type (type);
1325 /* if set to 1 the variable is native */
1328 cfg->varinfo [num] = inst;
1330 cfg->vars [num] = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMethodVar));
1331 MONO_INIT_VARINFO (cfg->vars [num], num);
1334 //g_print ("created temp %d of type %s\n", num, mono_type_get_name (type));
1339 type_from_stack_type (MonoInst *ins) {
1340 switch (ins->type) {
1341 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1342 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1343 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1344 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1345 case STACK_MP: return &mono_defaults.int_class->byval_arg;
1346 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1347 case STACK_VTYPE: return &ins->klass->byval_arg;
1349 g_error ("stack type %d to montype not handled\n", ins->type);
1355 array_access_to_klass (int opcode)
1359 return mono_defaults.byte_class;
1361 return mono_defaults.uint16_class;
1364 return mono_defaults.int_class;
1367 return mono_defaults.sbyte_class;
1370 return mono_defaults.int16_class;
1373 return mono_defaults.int32_class;
1375 return mono_defaults.uint32_class;
1378 return mono_defaults.int64_class;
1381 return mono_defaults.single_class;
1384 return mono_defaults.double_class;
1385 case CEE_LDELEM_REF:
1386 case CEE_STELEM_REF:
1387 return mono_defaults.object_class;
1389 g_assert_not_reached ();
1395 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
1399 MONO_ADD_INS (bb, inst);
1402 switch (bb->last_ins->opcode) {
1415 while (prev->next && prev->next != bb->last_ins)
1417 if (prev == bb->code) {
1418 if (bb->last_ins == bb->code) {
1419 inst->next = bb->code;
1422 inst->next = prev->next;
1426 inst->next = bb->last_ins;
1430 // g_warning ("handle conditional jump in add_ins_to_end ()\n");
1432 MONO_ADD_INS (bb, inst);
1438 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
1440 MonoInst *inst, *load;
1442 NEW_TEMPLOAD (cfg, load, src);
1444 NEW_TEMPSTORE (cfg, inst, dest, load);
1445 if (inst->opcode == CEE_STOBJ) {
1446 NEW_TEMPLOADA (cfg, inst, dest);
1447 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE);
1449 inst->cil_code = NULL;
1450 mono_add_ins_to_end (bb, inst);
1455 * We try to share variables when possible
1458 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1463 /* inlining can result in deeper stacks */
1464 if (slot >= ((MonoMethodNormal *)cfg->method)->header->max_stack)
1465 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1467 pos = ins->type - 1 + slot * STACK_MAX;
1469 switch (ins->type) {
1476 if ((vnum = cfg->intvars [pos]))
1477 return cfg->varinfo [vnum];
1478 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1479 cfg->intvars [pos] = res->inst_c0;
1482 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1488 * This function is called to handle items that are left on the evaluation stack
1489 * at basic block boundaries. What happens is that we save the values to local variables
1490 * and we reload them later when first entering the target basic block (with the
1491 * handle_loaded_temps () function).
1492 * A single joint point will use the same variables (stored in the array bb->out_stack or
1493 * bb->in_stack, if the basic block is before or after the joint point).
1496 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count) {
1498 MonoBasicBlock *outb;
1499 MonoInst *inst, **locals;
1503 if (cfg->verbose_level > 3)
1504 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
1505 if (!bb->out_scount) {
1507 bb->out_scount = count;
1508 //g_print ("bblock %d has out:", bb->block_num);
1509 for (i = 0; i < bb->out_count; ++i) {
1510 outb = bb->out_bb [i];
1511 //g_print (" %d", outb->block_num);
1512 if (outb->in_stack) {
1514 bb->out_stack = outb->in_stack;
1520 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1521 for (i = 0; i < count; ++i) {
1523 /* try to reuse temps already allocated for this purpouse, if they occupy the same
1524 * stack slot and if they are of the same type. */
1525 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1527 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1532 locals = bb->out_stack;
1533 for (i = 0; i < count; ++i) {
1534 /* add store ops at the end of the bb, before the branch */
1535 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1536 if (inst->opcode == CEE_STOBJ) {
1537 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
1538 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE);
1540 inst->cil_code = sp [i]->cil_code;
1541 mono_add_ins_to_end (bb, inst);
1543 if (cfg->verbose_level > 3)
1544 g_print ("storing %d to temp %d\n", i, locals [i]->inst_c0);
1547 for (i = 0; i < bb->out_count; ++i) {
1548 outb = bb->out_bb [i];
1549 if (outb->in_scount)
1550 continue; /* check they are the same locals */
1551 outb->in_scount = count;
1552 outb->in_stack = locals;
1558 ret_type_to_call_opcode (MonoType *type, int calli, int virt)
1563 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1567 case MONO_TYPE_VOID:
1568 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
1571 case MONO_TYPE_BOOLEAN:
1574 case MONO_TYPE_CHAR:
1577 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1581 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1582 case MONO_TYPE_CLASS:
1583 case MONO_TYPE_STRING:
1584 case MONO_TYPE_OBJECT:
1585 case MONO_TYPE_SZARRAY:
1586 case MONO_TYPE_ARRAY:
1587 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1590 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
1593 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
1594 case MONO_TYPE_VALUETYPE:
1595 if (type->data.klass->enumtype) {
1596 t = type->data.klass->enum_basetype->type;
1599 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1600 case MONO_TYPE_TYPEDBYREF:
1601 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1603 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
1609 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
1611 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
1613 ji->ip.label = label;
1614 ji->type = MONO_PATCH_INFO_SWITCH;
1615 ji->data.table = bbs;
1616 ji->next = cfg->patch_info;
1617 ji->table_size = num_blocks;
1618 cfg->patch_info = ji;
1622 * When we add a tree of instructions, we need to ensure the instructions currently
1623 * on the stack are executed before (like, if we load a value from a local).
1624 * We ensure this by saving the currently loaded values to temps and rewriting the
1625 * instructions to load the values.
1626 * This is not done for opcodes that terminate a basic block (because it's handled already
1627 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
1630 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
1632 MonoInst *load, *store, *temp, *ins;
1634 while (stack < sp) {
1636 /* handle also other constants */
1637 if (ins->opcode != OP_ICONST) {
1638 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1639 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1640 store->cil_code = ins->cil_code;
1641 if (store->opcode == CEE_STOBJ) {
1642 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
1643 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE);
1645 MONO_ADD_INS (bblock, store);
1646 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1647 load->cil_code = ins->cil_code;
1655 * Prepare arguments for passing to a function call.
1656 * Return a non-zero value if the arguments can't be passed to the given
1658 * The type checks are not yet complete and some conversions may need
1659 * casts on 32 or 64 bit architectures.
1662 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
1667 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
1671 for (i = 0; i < sig->param_count; ++i) {
1672 if (sig->params [i]->byref) {
1674 * check the result of ldelema is only passed as an argument if the byref
1675 * type matches exactly the array element type.
1676 * FIXME: if the argument as been saved on the stack as part of the
1677 * interface variable code (the value was on the stack at a basic block boundary)
1678 * we need to add the check in that case, too.
1680 if (args [i]->opcode == CEE_LDELEMA) {
1682 MonoClass *exact_class = mono_class_from_mono_type (sig->params [i]);
1683 if (!exact_class->valuetype) {
1684 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
1685 check->cil_code = args [i]->cil_code;
1686 check->klass = exact_class;
1687 check->inst_left = args [i]->inst_left;
1688 check->type = STACK_OBJ;
1689 args [i]->inst_left = check;
1692 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
1696 simple_type = sig->params [i]->type;
1698 switch (simple_type) {
1699 case MONO_TYPE_VOID:
1704 case MONO_TYPE_BOOLEAN:
1707 case MONO_TYPE_CHAR:
1710 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
1716 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
1719 case MONO_TYPE_CLASS:
1720 case MONO_TYPE_STRING:
1721 case MONO_TYPE_OBJECT:
1722 case MONO_TYPE_SZARRAY:
1723 case MONO_TYPE_ARRAY:
1724 if (args [i]->type != STACK_OBJ)
1729 if (args [i]->type != STACK_I8)
1734 if (args [i]->type != STACK_R8)
1737 case MONO_TYPE_VALUETYPE:
1738 if (sig->params [i]->data.klass->enumtype) {
1739 simple_type = sig->params [i]->data.klass->enum_basetype->type;
1742 if (args [i]->type != STACK_VTYPE)
1745 case MONO_TYPE_TYPEDBYREF:
1746 if (args [i]->type != STACK_VTYPE)
1750 g_error ("unknown type 0x%02x in check_call_signature", simple_type);
1757 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
1758 const guint8 *ip, gboolean to_end)
1760 MonoInst *temp, *store, *ins = (MonoInst*)call;
1761 MonoType *ret = sig->ret;
1763 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
1765 call->inst.type = STACK_OBJ;
1766 call->inst.opcode = CEE_CALL;
1767 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
1769 type_to_eval_stack_type (ret, ins);
1770 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
1773 if (MONO_TYPE_ISSTRUCT (ret)) {
1776 /* we use this to allocate native sized structs */
1777 temp->unused = sig->pinvoke;
1779 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
1780 if (call->inst.opcode == OP_VCALL)
1781 ins->inst_left = loada;
1783 ins->inst_right = loada; /* a virtual or indirect call */
1786 mono_add_ins_to_end (bblock, ins);
1788 MONO_ADD_INS (bblock, ins);
1790 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1791 store->cil_code = ip;
1793 mono_add_ins_to_end (bblock, store);
1795 MONO_ADD_INS (bblock, store);
1797 return temp->inst_c0;
1800 mono_add_ins_to_end (bblock, ins);
1802 MONO_ADD_INS (bblock, ins);
1807 inline static MonoCallInst *
1808 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1809 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
1814 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual));
1816 call->inst.cil_code = ip;
1818 call->signature = sig;
1819 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
1821 for (i = 0; i < (sig->param_count + sig->hasthis); ++i) {
1822 if (call->args [i]) {
1823 if (!call->args [i]->cil_code)
1824 call->args [i]->cil_code = ip;
1826 mono_add_ins_to_end (bblock, call->args [i]);
1828 MONO_ADD_INS (bblock, call->args [i]);
1835 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1836 MonoInst **args, MonoInst *addr, const guint8 *ip)
1838 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
1840 call->inst.inst_i0 = addr;
1842 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
1845 static MonoCallInst*
1846 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
1847 MonoInst **args, const guint8 *ip, MonoInst *this)
1849 gboolean virtual = this != NULL;
1852 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, FALSE);
1854 if (this && sig->hasthis &&
1855 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
1856 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
1857 call->method = mono_marshal_get_remoting_invoke_with_check (method);
1859 call->method = method;
1861 call->inst.flags |= MONO_INST_HAS_METHOD;
1862 call->inst.inst_left = this;
1868 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
1869 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
1871 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, signature, args, ip, this);
1873 return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
1877 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
1878 MonoInst **args, const guint8 *ip, gboolean to_end)
1884 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
1886 return mono_spill_call (cfg, bblock, call, sig, func == mono_array_new_va, ip, to_end);
1890 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
1892 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
1895 g_warning ("unregistered JIT ICall");
1896 g_assert_not_reached ();
1899 return mono_emit_native_call (cfg, bblock, info->wrapper, info->sig, args, ip, FALSE);
1903 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
1905 MonoInst *ins, *temp = NULL, *store, *load;
1909 /*g_print ("emulating: ");
1910 mono_print_tree (tree);
1912 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE));
1913 ins = (MonoInst*)call;
1915 call->inst.cil_code = tree->cil_code;
1917 call->signature = info->sig;
1919 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
1921 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
1922 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
1923 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1924 store->cil_code = tree->cil_code;
1929 nargs = info->sig->param_count + info->sig->hasthis;
1931 for (i = 1; i < nargs; i++) {
1932 call->args [i - 1]->next = call->args [i];
1936 call->args [nargs - 1]->next = store;
1938 if (cfg->prev_ins) {
1939 store->next = cfg->prev_ins->next;
1941 cfg->prev_ins->next = call->args [0];
1943 cfg->prev_ins->next = store;
1945 store->next = cfg->cbb->code;
1947 cfg->cbb->code = call->args [0];
1949 cfg->cbb->code = store;
1953 call->fptr = info->wrapper;
1955 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
1956 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1961 static MonoMethodSignature *
1962 mono_get_element_address_signature (int arity)
1964 static GHashTable *sighash = NULL;
1965 MonoMethodSignature *res;
1969 sighash = g_hash_table_new (NULL, NULL);
1972 if ((res = g_hash_table_lookup (sighash, (gpointer)arity)))
1975 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
1977 res->params [0] = &mono_defaults.array_class->byval_arg;
1979 for (i = 1; i <= arity; i++)
1980 res->params [i] = &mono_defaults.int_class->byval_arg;
1982 res->ret = &mono_defaults.int_class->byval_arg;
1984 g_hash_table_insert (sighash, (gpointer)arity, res);
1990 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native) {
1991 MonoInst *iargs [3];
1997 * This check breaks with spilled vars... need to handle it during verification anyway.
1998 * g_assert (klass && klass == src->klass && klass == dest->klass);
2002 n = mono_class_native_size (klass, &align);
2004 n = mono_class_value_size (klass, &align);
2006 if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
2008 MONO_INST_NEW (cfg, inst, OP_MEMCPY);
2009 inst->inst_left = dest;
2010 inst->inst_right = src;
2011 inst->cil_code = ip;
2013 MONO_ADD_INS (bblock, inst);
2018 NEW_ICONST (cfg, iargs [2], n);
2020 mono_emit_native_call (cfg, bblock, helper_memcpy, helper_sig_memcpy, iargs, ip, to_end);
2024 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
2026 MonoInst *iargs [2];
2027 MonoInst *ins, *zero_int32;
2030 NEW_ICONST (cfg, zero_int32, 0);
2032 mono_class_init (klass);
2033 n = mono_class_value_size (klass, NULL);
2034 MONO_INST_NEW (cfg, ins, 0);
2036 ins->inst_left = dest;
2037 ins->inst_right = zero_int32;
2040 ins->opcode = CEE_STIND_I1;
2041 MONO_ADD_INS (bblock, ins);
2044 ins->opcode = CEE_STIND_I2;
2045 MONO_ADD_INS (bblock, ins);
2048 ins->opcode = CEE_STIND_I4;
2049 MONO_ADD_INS (bblock, ins);
2052 handle_loaded_temps (cfg, bblock, stack_start, sp);
2053 NEW_ICONST (cfg, ins, n);
2056 mono_emit_jit_icall (cfg, bblock, helper_initobj, iargs, ip);
2061 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
2064 needs_cctor_run (MonoClass *klass, MonoMethod *caller)
2069 for (i = 0; i < klass->method.count; ++i) {
2070 method = klass->methods [i];
2071 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
2072 (strcmp (".cctor", method->name) == 0)) {
2073 if (caller == method)
2082 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
2084 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
2085 MonoMethodSignature *signature = method->signature;
2089 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2090 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2091 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
2092 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
2093 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2094 (method->klass->marshalbyref) ||
2095 !header || header->num_clauses ||
2096 /* fixme: why cant we inline valuetype returns? */
2097 MONO_TYPE_ISSTRUCT (signature->ret))
2100 /* its not worth to inline methods with valuetype arguments?? */
2101 for (i = 0; i < signature->param_count; i++) {
2102 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
2108 * if we can initialize the class of the method right away, we do,
2109 * otherwise we don't allow inlining if the class needs initialization,
2110 * since it would mean inserting a call to mono_runtime_class_init()
2111 * inside the inlined code
2113 if (!(cfg->opt & MONO_OPT_SHARED)) {
2114 vtable = mono_class_vtable (cfg->domain, method->klass);
2115 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)
2116 mono_runtime_class_init (vtable);
2117 else if (!vtable->initialized && needs_cctor_run (method->klass, NULL))
2121 * If we're compiling for shared code
2122 * the cctor will need to be run at aot method load time, for example,
2123 * or at the end of the compilation of the inlining method.
2125 if (needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
2128 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
2130 /* also consider num_locals? */
2131 if (header->code_size < 20)
2138 mini_get_ldelema_ins (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
2142 MonoMethodSignature *esig;
2144 rank = cmethod->signature->param_count - (is_set? 1: 0);
2146 * FIXME: handle TypeMismatch for set or use the slow path
2149 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
2151 NEW_GROUP (cfg, indexes, sp [1], sp [2]);
2152 MONO_INST_NEW (cfg, addr, OP_LDELEMA2D);
2153 addr->inst_left = sp [0];
2154 addr->inst_right = indexes;
2155 addr->cil_code = ip;
2156 addr->type = STACK_MP;
2157 addr->klass = cmethod->klass;
2160 esig = mono_get_element_address_signature (rank);
2161 temp = mono_emit_native_call (cfg, bblock, ves_array_element_address, esig, sp, ip, FALSE);
2162 NEW_TEMPLOAD (cfg, addr, temp);
2167 mini_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
2172 if (cmethod->klass == mono_defaults.string_class) {
2173 if (cmethod->name [0] != 'g' || strcmp (cmethod->name, "get_Chars"))
2176 } else if (cmethod->klass == mono_defaults.math_class) {
2177 if (strcmp (cmethod->name, "Sin") == 0)
2179 else if (strcmp (cmethod->name, "Cos") == 0)
2181 else if (strcmp (cmethod->name, "Tan") == 0)
2183 else if (strcmp (cmethod->name, "Atan") == 0)
2185 else if (strcmp (cmethod->name, "Sqrt") == 0)
2187 else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8)
2194 pc = fsig->param_count + fsig->hasthis;
2195 MONO_INST_NEW (cfg, ins, op);
2198 ins->inst_i0 = args [0];
2200 ins->inst_i1 = args [1];
2207 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
2209 MonoInst *store, *temp;
2212 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
2214 if (!sig->hasthis && sig->param_count == 0)
2218 if (sp [0]->opcode == OP_ICONST) {
2221 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
2223 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2224 store->cil_code = sp [0]->cil_code;
2225 MONO_ADD_INS (bblock, store);
2230 for (i = 0; i < sig->param_count; ++i) {
2231 if (sp [0]->opcode == OP_ICONST) {
2234 temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
2236 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2237 store->cil_code = sp [0]->cil_code;
2238 if (store->opcode == CEE_STOBJ) {
2239 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2240 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE);
2242 MONO_ADD_INS (bblock, store);
2250 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
2251 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b)
2253 MonoInst *ins, *rvar = NULL;
2254 MonoMethodHeader *cheader;
2255 MonoBasicBlock *ebblock, *sbblock;
2256 int i, costs, new_locals_offset;
2258 if (cfg->verbose_level > 2)
2259 g_print ("INLINE START %p %s\n", cmethod, mono_method_full_name (cmethod, TRUE));
2261 cheader = ((MonoMethodNormal *)cmethod)->header;
2263 if (!cmethod->inline_info) {
2264 mono_jit_stats.inlineable_methods++;
2265 cmethod->inline_info = 1;
2267 /* allocate space to store the return value */
2268 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2269 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2272 /* allocate local variables */
2273 new_locals_offset = cfg->num_varinfo;
2274 for (i = 0; i < cheader->num_locals; ++i)
2275 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
2277 /* allocate starte and end blocks */
2278 sbblock = NEW_BBLOCK (cfg);
2279 sbblock->block_num = cfg->num_bblocks++;
2280 sbblock->real_offset = real_offset;
2282 ebblock = NEW_BBLOCK (cfg);
2283 ebblock->block_num = cfg->num_bblocks++;
2284 ebblock->real_offset = real_offset;
2286 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
2288 if (costs >= 0 && costs < 60) {
2289 if (cfg->verbose_level > 2)
2290 g_print ("INLINE END %s\n", mono_method_full_name (cmethod, TRUE));
2292 mono_jit_stats.inlined_methods++;
2294 /* always add some code to avoid block split failures */
2295 MONO_INST_NEW (cfg, ins, CEE_NOP);
2296 MONO_ADD_INS (bblock, ins);
2299 bblock->next_bb = sbblock;
2300 link_bblock (cfg, bblock, sbblock);
2303 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
2309 if (cfg->verbose_level > 2)
2310 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
2316 * Some of these comments may well be out-of-date.
2317 * Design decisions: we do a single pass over the IL code (and we do bblock
2318 * splitting/merging in the few cases when it's required: a back jump to an IL
2319 * address that was not already seen as bblock starting point).
2320 * Code is validated as we go (full verification is still better left to metadata/verify.c).
2321 * Complex operations are decomposed in simpler ones right away. We need to let the
2322 * arch-specific code peek and poke inside this process somehow (except when the
2323 * optimizations can take advantage of the full semantic info of coarse opcodes).
2324 * All the opcodes of the form opcode.s are 'normalized' to opcode.
2325 * MonoInst->opcode initially is the IL opcode or some simplification of that
2326 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
2327 * opcode with value bigger than OP_LAST.
2328 * At this point the IR can be handed over to an interpreter, a dumb code generator
2329 * or to the optimizing code generator that will translate it to SSA form.
2331 * Profiling directed optimizations.
2332 * We may compile by default with few or no optimizations and instrument the code
2333 * or the user may indicate what methods to optimize the most either in a config file
2334 * or through repeated runs where the compiler applies offline the optimizations to
2335 * each method and then decides if it was worth it.
2338 * * consider using an array instead of an hash table (bb_hash)
2341 #define CHECK_TYPE(ins) if (!(ins)->type) goto unverified
2342 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) goto unverified
2343 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) goto unverified
2345 /* offset from br.s -> br like opcodes */
2346 #define BIG_BRANCH_OFFSET 13
2349 get_basic_blocks (MonoCompile *cfg, GHashTable *bbhash, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end)
2351 unsigned char *ip = start;
2352 unsigned char *target;
2355 MonoBasicBlock *bblock;
2356 const MonoOpcode *opcode;
2359 cli_addr = ip - start;
2360 i = mono_opcode_value (&ip);
2361 opcode = &mono_opcodes [i];
2362 switch (opcode->argument) {
2363 case MonoInlineNone:
2366 case MonoInlineString:
2367 case MonoInlineType:
2368 case MonoInlineField:
2369 case MonoInlineMethod:
2372 case MonoShortInlineR:
2379 case MonoShortInlineVar:
2380 case MonoShortInlineI:
2383 case MonoShortInlineBrTarget:
2384 target = start + cli_addr + 2 + (signed char)ip [1];
2385 GET_BBLOCK (cfg, bbhash, bblock, target);
2388 case MonoInlineBrTarget:
2389 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
2390 GET_BBLOCK (cfg, bbhash, bblock, target);
2393 case MonoInlineSwitch: {
2394 guint32 n = read32 (ip + 1);
2397 cli_addr += 5 + 4 * n;
2398 target = start + cli_addr;
2399 GET_BBLOCK (cfg, bbhash, bblock, target);
2401 for (j = 0; j < n; ++j) {
2402 target = start + cli_addr + (gint32)read32 (ip);
2403 GET_BBLOCK (cfg, bbhash, bblock, target);
2413 g_assert_not_reached ();
2422 * mono_method_to_ir: translates IL into basic blocks containing trees
2425 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
2426 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
2427 guint inline_offset, gboolean is_virtual_call)
2429 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
2430 MonoInst *ins, **sp, **stack_start;
2431 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
2433 MonoMethod *cmethod;
2434 MonoInst **arg_array;
2435 MonoMethodHeader *header;
2437 guint32 token, ins_flag;
2439 unsigned char *ip, *end, *target;
2440 static double r8_0 = 0.0;
2441 MonoMethodSignature *sig;
2442 MonoType **param_types;
2443 GList *bb_recheck = NULL, *tmp;
2444 int i, n, start_new_bblock, align;
2445 int num_calls = 0, inline_costs = 0;
2446 int *filter_lengths = NULL;
2447 int breakpoint_id = 0;
2450 image = method->klass->image;
2451 header = ((MonoMethodNormal *)method)->header;
2452 sig = method->signature;
2453 ip = (unsigned char*)header->code;
2454 end = ip + header->code_size;
2455 mono_jit_stats.cil_code_size += header->code_size;
2457 if (cfg->method == method) {
2459 bbhash = cfg->bb_hash;
2461 real_offset = inline_offset;
2462 bbhash = g_hash_table_new (g_direct_hash, NULL);
2465 dont_inline = g_list_prepend (dont_inline, method);
2466 if (cfg->method == method) {
2469 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
2470 start_bblock->cil_code = NULL;
2471 start_bblock->cil_length = 0;
2472 start_bblock->block_num = cfg->num_bblocks++;
2475 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
2476 end_bblock->cil_code = NULL;
2477 end_bblock->cil_length = 0;
2478 end_bblock->block_num = cfg->num_bblocks++;
2479 g_assert (cfg->num_bblocks == 2);
2481 arg_array = alloca (sizeof (MonoInst *) * (sig->hasthis + sig->param_count));
2482 for (i = sig->hasthis + sig->param_count - 1; i >= 0; i--)
2483 arg_array [i] = cfg->varinfo [i];
2485 if (mono_compile_aot)
2486 cfg->opt |= MONO_OPT_SHARED;
2488 if (header->num_clauses) {
2489 int size = sizeof (int) * header->num_clauses;
2490 filter_lengths = alloca (size);
2491 memset (filter_lengths, 0, size);
2493 cfg->spvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2494 /* prevent it from being register allocated */
2495 cfg->spvar->flags |= MONO_INST_INDIRECT;
2497 /* handle exception clauses */
2498 for (i = 0; i < header->num_clauses; ++i) {
2499 //unsigned char *p = ip;
2500 MonoExceptionClause *clause = &header->clauses [i];
2501 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->try_offset);
2502 tblock->real_offset = clause->try_offset;
2503 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
2504 tblock->real_offset = clause->handler_offset;
2506 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
2507 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2508 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2509 MONO_ADD_INS (tblock, ins);
2512 /*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);
2514 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
2516 /* catch and filter blocks get the exception object on the stack */
2517 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
2518 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2519 /* mostly like handle_stack_args (), but just sets the input args */
2520 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
2522 cfg->exvar = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
2523 /* prevent it from being register allocated */
2524 cfg->exvar->flags |= MONO_INST_INDIRECT;
2526 tblock->in_scount = 1;
2527 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2528 tblock->in_stack [0] = cfg->exvar;
2530 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2531 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->token_or_filter);
2532 tblock->real_offset = clause->token_or_filter;
2533 tblock->in_scount = 1;
2534 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2535 tblock->in_stack [0] = cfg->exvar;
2536 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2537 MONO_ADD_INS (tblock, ins);
2543 arg_array = alloca (sizeof (MonoInst *) * (sig->hasthis + sig->param_count));
2544 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
2547 /* FIRST CODE BLOCK */
2548 bblock = NEW_BBLOCK (cfg);
2549 bblock->cil_code = ip;
2551 ADD_BBLOCK (cfg, bbhash, bblock);
2553 if (cfg->method == method) {
2554 breakpoint_id = mono_debugger_method_has_breakpoint (method);
2555 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
2556 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2557 MONO_ADD_INS (bblock, ins);
2561 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED)))) {
2562 /* we use a separate basic block for the initialization code */
2563 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
2564 init_localsbb->real_offset = real_offset;
2565 start_bblock->next_bb = init_localsbb;
2566 init_localsbb->next_bb = bblock;
2567 link_bblock (cfg, start_bblock, init_localsbb);
2568 link_bblock (cfg, init_localsbb, bblock);
2569 init_localsbb->block_num = cfg->num_bblocks++;
2571 start_bblock->next_bb = bblock;
2572 link_bblock (cfg, start_bblock, bblock);
2575 if (get_basic_blocks (cfg, bbhash, header, real_offset, ip, end))
2578 mono_debug_init_method (cfg, bblock, breakpoint_id);
2580 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * (sig->hasthis + sig->param_count));
2582 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
2583 for (n = 0; n < sig->param_count; ++n)
2584 param_types [n + sig->hasthis] = sig->params [n];
2586 /* do this somewhere outside - not here */
2587 NEW_ICONST (cfg, zero_int32, 0);
2588 NEW_ICONST (cfg, zero_int64, 0);
2589 zero_int64->type = STACK_I8;
2590 NEW_PCONST (cfg, zero_ptr, 0);
2591 NEW_PCONST (cfg, zero_obj, 0);
2592 zero_obj->type = STACK_OBJ;
2594 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
2595 zero_r8->type = STACK_R8;
2596 zero_r8->inst_p0 = &r8_0;
2598 /* add a check for this != NULL to inlined methods */
2599 if (is_virtual_call) {
2600 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
2601 NEW_ARGLOAD (cfg, ins->inst_left, 0);
2603 MONO_ADD_INS (bblock, ins);
2606 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
2607 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
2610 start_new_bblock = 0;
2613 if (cfg->method == method)
2614 real_offset = ip - header->code;
2616 real_offset = inline_offset;
2618 if (start_new_bblock) {
2619 bblock->cil_length = ip - bblock->cil_code;
2620 if (start_new_bblock == 2) {
2621 g_assert (ip == tblock->cil_code);
2623 GET_BBLOCK (cfg, bbhash, tblock, ip);
2625 bblock->next_bb = tblock;
2627 start_new_bblock = 0;
2628 for (i = 0; i < bblock->in_scount; ++i) {
2629 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2633 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
2634 link_bblock (cfg, bblock, tblock);
2635 if (sp != stack_start) {
2636 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2639 bblock->next_bb = tblock;
2641 for (i = 0; i < bblock->in_scount; ++i) {
2642 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2648 if (cfg->verbose_level > 3)
2649 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, sp-stack_start, mono_disasm_code_one (NULL, method, ip, NULL));
2656 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2657 ins->cil_code = ip++;
2658 MONO_ADD_INS (bblock, ins);
2664 CHECK_STACK_OVF (1);
2665 n = (*ip)-CEE_LDARG_0;
2666 NEW_ARGLOAD (cfg, ins, n);
2667 ins->cil_code = ip++;
2674 CHECK_STACK_OVF (1);
2675 n = (*ip)-CEE_LDLOC_0;
2676 NEW_LOCLOAD (cfg, ins, n);
2677 ins->cil_code = ip++;
2685 n = (*ip)-CEE_STLOC_0;
2687 handle_loaded_temps (cfg, bblock, stack_start, sp);
2688 NEW_LOCSTORE (cfg, ins, n, *sp);
2690 if (ins->opcode == CEE_STOBJ) {
2691 NEW_LOCLOADA (cfg, ins, n);
2692 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2694 MONO_ADD_INS (bblock, ins);
2699 CHECK_STACK_OVF (1);
2700 NEW_ARGLOAD (cfg, ins, ip [1]);
2706 CHECK_STACK_OVF (1);
2707 NEW_ARGLOADA (cfg, ins, ip [1]);
2715 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
2716 handle_loaded_temps (cfg, bblock, stack_start, sp);
2718 if (ins->opcode == CEE_STOBJ) {
2719 NEW_ARGLOADA (cfg, ins, ip [1]);
2720 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2722 MONO_ADD_INS (bblock, ins);
2726 CHECK_STACK_OVF (1);
2727 NEW_LOCLOAD (cfg, ins, ip [1]);
2733 CHECK_STACK_OVF (1);
2734 NEW_LOCLOADA (cfg, ins, ip [1]);
2742 handle_loaded_temps (cfg, bblock, stack_start, sp);
2743 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
2745 if (ins->opcode == CEE_STOBJ) {
2746 NEW_LOCLOADA (cfg, ins, ip [1]);
2747 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2749 MONO_ADD_INS (bblock, ins);
2754 CHECK_STACK_OVF (1);
2755 NEW_PCONST (cfg, ins, NULL);
2757 ins->type = STACK_OBJ;
2762 CHECK_STACK_OVF (1);
2763 NEW_ICONST (cfg, ins, -1);
2777 CHECK_STACK_OVF (1);
2778 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
2784 CHECK_STACK_OVF (1);
2786 NEW_ICONST (cfg, ins, *((signed char*)ip));
2792 CHECK_STACK_OVF (1);
2793 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
2799 CHECK_STACK_OVF (1);
2800 MONO_INST_NEW (cfg, ins, OP_I8CONST);
2802 ins->type = STACK_I8;
2804 ins->inst_l = (gint64)read64 (ip);
2809 float *f = g_malloc (sizeof (float));
2810 CHECK_STACK_OVF (1);
2811 MONO_INST_NEW (cfg, ins, OP_R4CONST);
2812 ins->type = STACK_R8;
2821 double *d = g_malloc (sizeof (double));
2822 CHECK_STACK_OVF (1);
2823 MONO_INST_NEW (cfg, ins, OP_R8CONST);
2824 ins->type = STACK_R8;
2833 MonoInst *temp, *store;
2835 CHECK_STACK_OVF (1);
2840 * small optimization: if the loaded value was from a local already,
2841 * just load it twice.
2843 if (ins->ssa_op == MONO_SSA_LOAD &&
2844 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
2846 MONO_INST_NEW (cfg, temp, 0);
2848 temp->cil_code = ip;
2851 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
2852 temp->cil_code = ip;
2853 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2854 store->cil_code = ip;
2855 MONO_ADD_INS (bblock, store);
2856 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
2859 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
2869 MONO_INST_NEW (cfg, ins, CEE_POP);
2870 MONO_ADD_INS (bblock, ins);
2871 ins->cil_code = ip++;
2876 if (stack_start != sp)
2878 MONO_INST_NEW (cfg, ins, CEE_JMP);
2879 token = read32 (ip + 1);
2880 /* FIXME: check the signature matches */
2881 cmethod = mono_get_method (image, token, NULL);
2882 ins->inst_p0 = cmethod;
2883 MONO_ADD_INS (bblock, ins);
2885 start_new_bblock = 1;
2889 case CEE_CALLVIRT: {
2890 MonoInst *addr = NULL;
2891 MonoMethodSignature *fsig = NULL;
2892 int temp, array_rank = 0;
2893 int virtual = *ip == CEE_CALLVIRT;
2895 token = read32 (ip + 1);
2897 if (*ip == CEE_CALLI) {
2902 if (method->wrapper_type != MONO_WRAPPER_NONE)
2903 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
2905 fsig = mono_metadata_parse_signature (image, token);
2907 n = fsig->param_count + fsig->hasthis;
2910 if (method->wrapper_type != MONO_WRAPPER_NONE) {
2911 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
2913 cmethod = mono_get_method (image, token, NULL);
2916 if (!cmethod->klass->inited)
2917 mono_class_init (cmethod->klass);
2919 if (cmethod->signature->pinvoke) {
2920 #ifdef MONO_USE_EXC_TABLES
2921 if (mono_method_blittable (cmethod)) {
2922 fsig = cmethod->signature;
2925 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
2926 fsig = wrapper->signature;
2927 #ifdef MONO_USE_EXC_TABLES
2931 fsig = mono_method_get_signature (cmethod, image, token);
2934 n = fsig->param_count + fsig->hasthis;
2936 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
2937 cmethod->klass->parent == mono_defaults.array_class) {
2938 array_rank = cmethod->klass->rank;
2941 if (cmethod->string_ctor)
2942 g_assert_not_reached ();
2948 //g_assert (!virtual || fsig->hasthis);
2952 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
2955 if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL)) {
2957 for (i = 0; i < n; ++i) {
2958 NEW_ARGSTORE (cfg, ins, i, sp [i]);
2960 MONO_ADD_INS (bblock, ins);
2962 MONO_INST_NEW (cfg, ins, CEE_JMP);
2964 ins->inst_p0 = cmethod;
2965 MONO_ADD_INS (bblock, ins);
2966 start_new_bblock = 1;
2967 /* skip CEE_RET as well */
2972 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_opcode_for_method (cfg, cmethod, fsig, sp))) {
2975 if (MONO_TYPE_IS_VOID (fsig->ret)) {
2976 MONO_ADD_INS (bblock, ins);
2978 type_to_eval_stack_type (fsig->ret, ins);
2987 handle_loaded_temps (cfg, bblock, stack_start, sp);
2989 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
2990 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
2991 mono_method_check_inlining (cfg, cmethod) &&
2992 !g_list_find (dont_inline, cmethod)) {
2994 MonoBasicBlock *ebblock;
2996 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3000 GET_BBLOCK (cfg, bbhash, bblock, ip);
3001 ebblock->next_bb = bblock;
3002 link_bblock (cfg, ebblock, bblock);
3004 if (!MONO_TYPE_IS_VOID (fsig->ret))
3007 /* indicates start of a new block, and triggers a load of all
3008 stack arguments at bb boundarie */
3011 inline_costs += costs;
3016 inline_costs += 10 * num_calls++;
3018 /* tail recursion elimination */
3019 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == cfg->method && ip [5] == CEE_RET) {
3020 gboolean has_vtargs = FALSE;
3023 /* keep it simple */
3024 for (i = fsig->param_count - 1; i >= 0; i--) {
3025 if (MONO_TYPE_ISSTRUCT (cmethod->signature->params [i]))
3030 for (i = 0; i < n; ++i) {
3031 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3033 MONO_ADD_INS (bblock, ins);
3035 MONO_INST_NEW (cfg, ins, CEE_BR);
3037 MONO_ADD_INS (bblock, ins);
3038 tblock = start_bblock->out_bb [0];
3039 link_bblock (cfg, bblock, tblock);
3040 ins->inst_target_bb = tblock;
3041 start_new_bblock = 1;
3044 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3045 /* just create a dummy - the value is never used */
3046 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
3047 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3055 if (*ip == CEE_CALLI) {
3057 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
3058 NEW_TEMPLOAD (cfg, *sp, temp);
3062 } else if (array_rank) {
3065 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
3066 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
3067 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
3069 if (ins->opcode == CEE_STOBJ) {
3070 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
3072 MONO_ADD_INS (bblock, ins);
3075 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
3076 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3077 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
3081 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
3082 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3085 g_assert_not_reached ();
3089 if (0 && CODE_IS_STLOC (ip + 5) && (!MONO_TYPE_ISSTRUCT (fsig->ret)) && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)) {
3090 /* no need to spill */
3091 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
3094 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) {
3095 NEW_TEMPLOAD (cfg, *sp, temp);
3105 if (cfg->method != method) {
3106 /* return from inlined methode */
3111 //g_assert (returnvar != -1);
3112 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
3113 store->cil_code = sp [0]->cil_code;
3114 if (store->opcode == CEE_STOBJ) {
3115 g_assert_not_reached ();
3116 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
3117 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
3119 MONO_ADD_INS (bblock, store);
3123 g_assert (!return_var);
3126 MONO_INST_NEW (cfg, ins, CEE_NOP);
3127 ins->opcode = mono_type_to_stind (method->signature->ret);
3128 if (ins->opcode == CEE_STOBJ) {
3129 NEW_RETLOADA (cfg, ins);
3130 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3132 ins->opcode = OP_SETRET;
3134 ins->inst_i0 = *sp;;
3135 ins->inst_i1 = NULL;
3136 MONO_ADD_INS (bblock, ins);
3140 if (sp != stack_start)
3142 MONO_INST_NEW (cfg, ins, CEE_BR);
3143 ins->cil_code = ip++;
3144 ins->inst_target_bb = end_bblock;
3145 MONO_ADD_INS (bblock, ins);
3146 link_bblock (cfg, bblock, end_bblock);
3147 start_new_bblock = 1;
3150 MONO_INST_NEW (cfg, ins, CEE_BR);
3151 ins->cil_code = ip++;
3152 MONO_ADD_INS (bblock, ins);
3153 target = ip + 1 + (signed char)(*ip);
3155 GET_BBLOCK (cfg, bbhash, tblock, target);
3156 link_bblock (cfg, bblock, tblock);
3157 CHECK_BBLOCK (target, ip, tblock);
3158 ins->inst_target_bb = tblock;
3159 if (sp != stack_start) {
3160 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3163 start_new_bblock = 1;
3169 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3170 ins->cil_code = ip++;
3171 target = ip + 1 + *(signed char*)ip;
3173 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
3174 if (sp != stack_start) {
3175 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3191 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3192 ins->cil_code = ip++;
3193 target = ip + 1 + *(signed char*)ip;
3196 if (sp != stack_start) {
3197 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3203 MONO_INST_NEW (cfg, ins, CEE_BR);
3204 ins->cil_code = ip++;
3205 MONO_ADD_INS (bblock, ins);
3206 target = ip + 4 + (gint32)read32(ip);
3208 GET_BBLOCK (cfg, bbhash, tblock, target);
3209 link_bblock (cfg, bblock, tblock);
3210 CHECK_BBLOCK (target, ip, tblock);
3211 ins->inst_target_bb = tblock;
3212 if (sp != stack_start) {
3213 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3216 start_new_bblock = 1;
3222 MONO_INST_NEW (cfg, ins, *ip);
3223 ins->cil_code = ip++;
3224 target = ip + 4 + (gint32)read32(ip);
3226 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
3227 if (sp != stack_start) {
3228 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3244 MONO_INST_NEW (cfg, ins, *ip);
3245 ins->cil_code = ip++;
3246 target = ip + 4 + (gint32)read32(ip);
3249 if (sp != stack_start) {
3250 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3257 n = read32 (ip + 1);
3258 MONO_INST_NEW (cfg, ins, *ip);
3260 ins->inst_left = *sp;
3261 if (ins->inst_left->type != STACK_I4) goto unverified;
3264 target = ip + n * sizeof (guint32);
3265 MONO_ADD_INS (bblock, ins);
3266 GET_BBLOCK (cfg, bbhash, tblock, target);
3267 link_bblock (cfg, bblock, tblock);
3268 ins->klass = GUINT_TO_POINTER (n);
3269 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
3270 ins->inst_many_bb [n] = tblock;
3272 for (i = 0; i < n; ++i) {
3273 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
3274 link_bblock (cfg, bblock, tblock);
3275 ins->inst_many_bb [i] = tblock;
3278 /* FIXME: handle stack args */
3293 MONO_INST_NEW (cfg, ins, *ip);
3298 ins->type = ldind_type [*ip - CEE_LDIND_I1];
3299 ins->flags |= ins_flag;
3311 MONO_INST_NEW (cfg, ins, *ip);
3312 ins->cil_code = ip++;
3314 handle_loaded_temps (cfg, bblock, stack_start, sp);
3315 MONO_ADD_INS (bblock, ins);
3316 ins->inst_i0 = sp [0];
3317 ins->inst_i1 = sp [1];
3318 ins->flags |= ins_flag;
3349 case CEE_CONV_OVF_I8:
3350 case CEE_CONV_OVF_U8:
3356 case CEE_CONV_OVF_I4:
3357 case CEE_CONV_OVF_I1:
3358 case CEE_CONV_OVF_I2:
3359 case CEE_CONV_OVF_I:
3360 case CEE_CONV_OVF_U:
3363 if (sp [-1]->type == STACK_R8) {
3364 ADD_UNOP (CEE_CONV_OVF_I8);
3372 case CEE_CONV_OVF_U1:
3373 case CEE_CONV_OVF_U2:
3374 case CEE_CONV_OVF_U4:
3377 if (sp [-1]->type == STACK_R8) {
3378 ADD_UNOP (CEE_CONV_OVF_U8);
3386 case CEE_CONV_OVF_I1_UN:
3387 case CEE_CONV_OVF_I2_UN:
3388 case CEE_CONV_OVF_I4_UN:
3389 case CEE_CONV_OVF_I8_UN:
3390 case CEE_CONV_OVF_U1_UN:
3391 case CEE_CONV_OVF_U2_UN:
3392 case CEE_CONV_OVF_U4_UN:
3393 case CEE_CONV_OVF_U8_UN:
3394 case CEE_CONV_OVF_I_UN:
3395 case CEE_CONV_OVF_U_UN:
3401 g_error ("opcode 0x%02x not handled", *ip);
3404 MonoInst *iargs [3];
3407 token = read32 (ip + 1);
3408 if (method->wrapper_type != MONO_WRAPPER_NONE)
3409 klass = mono_method_get_wrapper_data (method, token);
3411 klass = mono_class_get (image, token);
3413 mono_class_init (klass);
3414 n = mono_class_value_size (klass, NULL);
3415 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3416 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3417 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3419 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3420 copy->inst_left = iargs [0];
3421 copy->inst_right = *sp;
3422 copy->cil_code = ip;
3424 MONO_ADD_INS (bblock, copy);
3427 NEW_ICONST (cfg, iargs [2], n);
3428 iargs [2]->cil_code = ip;
3430 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3432 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3439 CHECK_STACK_OVF (1);
3440 n = read32 (ip + 1);
3442 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3444 MonoInst *iargs [1];
3446 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
3447 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
3448 NEW_TEMPLOAD (cfg, *sp, temp);
3452 if (mono_compile_aot) {
3453 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n);
3456 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
3458 MonoInst *iargs [3];
3459 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3460 NEW_IMAGECONST (cfg, iargs [1], image);
3461 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
3462 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
3463 NEW_TEMPLOAD (cfg, *sp, temp);
3464 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3466 NEW_PCONST (cfg, ins, NULL);
3468 ins->type = STACK_OBJ;
3469 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3478 MonoInst *iargs [2];
3479 MonoMethodSignature *fsig;
3482 token = read32 (ip + 1);
3483 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3484 cmethod = mono_method_get_wrapper_data (method, token);
3486 cmethod = mono_get_method (image, token, NULL);
3487 fsig = mono_method_get_signature (cmethod, image, token);
3489 mono_class_init (cmethod->klass);
3491 n = fsig->param_count;
3494 /* move the args to allow room for 'this' in the first position */
3500 handle_loaded_temps (cfg, bblock, stack_start, sp);
3503 if (cmethod->klass->parent == mono_defaults.array_class) {
3504 NEW_METHODCONST (cfg, *sp, cmethod);
3505 temp = mono_emit_native_call (cfg, bblock, mono_array_new_va, fsig, sp, ip, FALSE);
3507 } else if (cmethod->string_ctor) {
3508 /* we simply pass a null pointer */
3509 NEW_PCONST (cfg, *sp, NULL);
3510 /* now call the string ctor */
3511 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
3513 if (cmethod->klass->valuetype) {
3514 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
3515 temp = iargs [0]->inst_c0;
3516 NEW_TEMPLOADA (cfg, *sp, temp);
3518 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
3519 NEW_DOMAINCONST (cfg, iargs [0]);
3520 NEW_CLASSCONST (cfg, iargs [1], cmethod->klass);
3522 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3524 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
3525 NEW_PCONST (cfg, iargs [0], vtable);
3526 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
3528 NEW_TEMPLOAD (cfg, *sp, temp);
3531 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3532 mono_method_check_inlining (cfg, cmethod) &&
3533 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
3534 !g_list_find (dont_inline, cmethod)) {
3536 MonoBasicBlock *ebblock;
3537 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3542 GET_BBLOCK (cfg, bbhash, bblock, ip);
3543 ebblock->next_bb = bblock;
3544 link_bblock (cfg, ebblock, bblock);
3546 NEW_TEMPLOAD (cfg, *sp, temp);
3549 /* indicates start of a new block, and triggers a load
3550 of all stack arguments at bb boundarie */
3553 inline_costs += costs;
3557 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3560 /* now call the actual ctor */
3561 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3565 NEW_TEMPLOAD (cfg, *sp, temp);
3574 MONO_INST_NEW (cfg, ins, *ip);
3576 klass = mono_class_get (image, read32 (ip + 1));
3577 mono_class_init (klass);
3578 ins->type = STACK_OBJ;
3579 ins->inst_left = *sp;
3580 ins->inst_newa_class = klass;
3586 MonoInst *add, *vtoffset;
3590 token = read32 (ip + 1);
3591 if (method->wrapper_type != MONO_WRAPPER_NONE)
3592 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3594 klass = mono_class_get (image, token);
3595 mono_class_init (klass);
3598 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
3599 ins->type = STACK_OBJ;
3600 ins->inst_left = *sp;
3602 ins->inst_newa_class = klass;
3605 MONO_INST_NEW (cfg, add, CEE_ADD);
3606 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3607 add->inst_left = ins;
3608 add->inst_right = vtoffset;
3609 add->type = STACK_MP;
3617 MONO_INST_NEW (cfg, ins, *ip);
3619 klass = mono_class_get (image, read32 (ip + 1));
3620 mono_class_init (klass);
3621 ins->type = STACK_OBJ;
3622 ins->inst_left = *sp;
3624 ins->inst_newa_class = klass;
3631 MONO_INST_NEW (cfg, ins, *ip);
3633 ins->inst_left = *sp;
3634 ins->cil_code = ip++;
3635 MONO_ADD_INS (bblock, ins);
3637 start_new_bblock = 1;
3642 MonoInst *offset_ins;
3643 MonoClassField *field;
3644 MonoBasicBlock *ebblock;
3648 if (*ip == CEE_STFLD) {
3655 // FIXME: enable this test later.
3656 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
3658 token = read32 (ip + 1);
3659 field = mono_field_from_token (image, token, &klass);
3660 mono_class_init (klass);
3662 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
3663 /* FIXME: mark instructions for use in SSA */
3664 if (*ip == CEE_STFLD) {
3665 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
3666 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
3667 MonoInst *iargs [5];
3670 NEW_CLASSCONST (cfg, iargs [1], klass);
3671 NEW_FIELDCONST (cfg, iargs [2], field);
3672 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
3676 if (cfg->opt & MONO_OPT_INLINE) {
3677 costs = inline_method (cfg, stfld_wrapper, stfld_wrapper->signature, bblock,
3678 iargs, ip, real_offset, dont_inline, &ebblock);
3679 g_assert (costs > 0);
3684 GET_BBLOCK (cfg, bbhash, bblock, ip);
3685 ebblock->next_bb = bblock;
3686 link_bblock (cfg, ebblock, bblock);
3688 /* indicates start of a new block, and triggers a load
3689 of all stack arguments at bb boundarie */
3692 inline_costs += costs;
3695 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, stfld_wrapper->signature, iargs, ip, NULL);
3699 NEW_ICONST (cfg, offset_ins, foffset);
3700 MONO_INST_NEW (cfg, ins, CEE_ADD);
3702 ins->inst_left = *sp;
3703 ins->inst_right = offset_ins;
3704 ins->type = STACK_MP;
3706 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
3707 store->cil_code = ip;
3708 store->inst_left = ins;
3709 store->inst_right = sp [1];
3710 handle_loaded_temps (cfg, bblock, stack_start, sp);
3711 store->flags |= ins_flag;
3713 if (store->opcode == CEE_STOBJ) {
3714 handle_stobj (cfg, bblock, ins, sp [1], ip,
3715 mono_class_from_mono_type (field->type), FALSE, FALSE);
3717 MONO_ADD_INS (bblock, store);
3720 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
3721 /* fixme: we need to inline that call somehow */
3722 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
3723 MonoInst *iargs [4];
3727 NEW_CLASSCONST (cfg, iargs [1], klass);
3728 NEW_FIELDCONST (cfg, iargs [2], field);
3729 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
3730 if (cfg->opt & MONO_OPT_INLINE) {
3731 costs = inline_method (cfg, ldfld_wrapper, ldfld_wrapper->signature, bblock,
3732 iargs, ip, real_offset, dont_inline, &ebblock);
3733 g_assert (costs > 0);
3738 GET_BBLOCK (cfg, bbhash, bblock, ip);
3739 ebblock->next_bb = bblock;
3740 link_bblock (cfg, ebblock, bblock);
3742 temp = iargs [0]->inst_i0->inst_c0;
3744 if (*ip == CEE_LDFLDA) {
3745 /* not sure howto handle this */
3746 NEW_TEMPLOADA (cfg, *sp, temp);
3748 NEW_TEMPLOAD (cfg, *sp, temp);
3752 /* indicates start of a new block, and triggers a load of
3753 all stack arguments at bb boundarie */
3756 inline_costs += costs;
3759 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, ldfld_wrapper->signature, iargs, ip, NULL);
3760 if (*ip == CEE_LDFLDA) {
3761 /* not sure howto handle this */
3762 NEW_TEMPLOADA (cfg, *sp, temp);
3764 NEW_TEMPLOAD (cfg, *sp, temp);
3769 NEW_ICONST (cfg, offset_ins, foffset);
3770 MONO_INST_NEW (cfg, ins, CEE_ADD);
3772 ins->inst_left = *sp;
3773 ins->inst_right = offset_ins;
3774 ins->type = STACK_MP;
3776 if (*ip == CEE_LDFLDA) {
3780 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
3781 type_to_eval_stack_type (field->type, load);
3782 load->cil_code = ip;
3783 load->inst_left = ins;
3784 load->flags |= ins_flag;
3796 MonoClassField *field;
3798 token = read32 (ip + 1);
3800 field = mono_field_from_token (image, token, &klass);
3801 mono_class_init (klass);
3803 handle_loaded_temps (cfg, bblock, stack_start, sp);
3805 if (((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot)) {
3807 MonoInst *iargs [2];
3808 g_assert (field->parent);
3809 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3810 NEW_FIELDCONST (cfg, iargs [1], field);
3811 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
3812 NEW_TEMPLOAD (cfg, ins, temp);
3816 vtable = mono_class_vtable (cfg->domain, klass);
3817 if (!cfg->domain->thread_static_fields || !(addr = g_hash_table_lookup (cfg->domain->thread_static_fields, field))) {
3818 if (!vtable->initialized && !(klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && needs_cctor_run (klass, method)) {
3819 MonoInst *iargs [1];
3820 NEW_PCONST (cfg, iargs [0], vtable);
3821 mono_emit_jit_icall (cfg, bblock, mono_runtime_class_init, iargs, ip);
3822 if (cfg->verbose_level > 2)
3823 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
3825 mono_runtime_class_init (vtable);
3827 addr = (char*)vtable->data + field->offset;
3828 NEW_PCONST (cfg, ins, addr);
3832 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
3833 * This could be later optimized to do just a couple of
3834 * memory dereferences with constant offsets.
3837 MonoInst *iargs [1];
3838 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
3839 temp = mono_emit_jit_icall (cfg, bblock, mono_threads_get_static_data, iargs, ip);
3840 NEW_TEMPLOAD (cfg, ins, temp);
3844 /* FIXME: mark instructions for use in SSA */
3845 if (*ip == CEE_LDSFLDA) {
3847 } else if (*ip == CEE_STSFLD) {
3851 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
3852 store->cil_code = ip;
3853 store->inst_left = ins;
3854 store->inst_right = sp [0];
3855 store->flags |= ins_flag;
3858 if (store->opcode == CEE_STOBJ) {
3859 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
3861 MONO_ADD_INS (bblock, store);
3864 CHECK_STACK_OVF (1);
3865 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
3866 type_to_eval_stack_type (field->type, load);
3867 load->cil_code = ip;
3868 load->inst_left = ins;
3870 load->flags |= ins_flag;
3872 /* fixme: dont see the problem why this does not work */
3873 //cfg->disable_aot = TRUE;
3881 token = read32 (ip + 1);
3882 if (method->wrapper_type != MONO_WRAPPER_NONE)
3883 klass = mono_method_get_wrapper_data (method, token);
3885 klass = mono_class_get (image, token);
3886 mono_class_init (klass);
3887 n = mono_type_to_stind (&klass->byval_arg);
3888 if (n == CEE_STOBJ) {
3889 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
3891 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
3893 MONO_INST_NEW (cfg, store, n);
3894 store->cil_code = ip;
3895 store->inst_left = sp [0];
3896 store->inst_right = sp [1];
3897 store->flags |= ins_flag;
3898 MONO_ADD_INS (bblock, store);
3905 MonoInst *iargs [2];
3906 MonoInst *load, *vtoffset, *add, *val, *vstore;
3911 token = read32 (ip + 1);
3912 if (method->wrapper_type != MONO_WRAPPER_NONE)
3913 klass = mono_method_get_wrapper_data (method, token);
3915 klass = mono_class_get (image, token);
3916 mono_class_init (klass);
3918 /* much like NEWOBJ */
3919 NEW_DOMAINCONST (cfg, iargs [0]);
3920 NEW_CLASSCONST (cfg, iargs [1], klass);
3922 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3923 NEW_TEMPLOAD (cfg, load, temp);
3924 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3925 MONO_INST_NEW (cfg, add, CEE_ADD);
3926 add->inst_left = load;
3927 add->inst_right = vtoffset;
3930 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
3931 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
3932 vstore->cil_code = ip;
3933 vstore->inst_left = add;
3934 vstore->inst_right = val;
3936 if (vstore->opcode == CEE_STOBJ) {
3937 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
3939 MONO_ADD_INS (bblock, vstore);
3941 NEW_TEMPLOAD (cfg, load, temp);
3949 MONO_INST_NEW (cfg, ins, *ip);
3953 token = read32 (ip + 1);
3955 /* allocate the domainvar - becaus this is used in decompose_foreach */
3956 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot)
3957 mono_get_domainvar (cfg);
3959 if (method->wrapper_type != MONO_WRAPPER_NONE)
3960 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3962 klass = mono_class_get (image, token);
3964 mono_class_init (klass);
3965 ins->inst_newa_class = klass;
3966 ins->inst_newa_len = *sp;
3967 ins->type = STACK_OBJ;
3974 MONO_INST_NEW (cfg, ins, *ip);
3975 ins->cil_code = ip++;
3977 ins->inst_left = *sp;
3978 ins->type = STACK_PTR;
3984 klass = mono_class_get (image, read32 (ip + 1));
3985 mono_class_init (klass);
3986 NEW_LDELEMA (cfg, ins, sp, klass);
4001 case CEE_LDELEM_REF: {
4005 * ldind.x (ldelema (array, index))
4006 * ldelema does the bounds check
4010 klass = array_access_to_klass (*ip);
4011 NEW_LDELEMA (cfg, load, sp, klass);
4012 load->cil_code = ip;
4013 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
4015 ins->inst_left = load;
4017 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
4027 case CEE_STELEM_R8: {
4031 * stind.x (ldelema (array, index), val)
4032 * ldelema does the bounds check
4036 klass = array_access_to_klass (*ip);
4037 NEW_LDELEMA (cfg, load, sp, klass);
4038 load->cil_code = ip;
4039 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
4041 ins->inst_left = load;
4042 ins->inst_right = sp [2];
4044 handle_loaded_temps (cfg, bblock, stack_start, sp);
4045 MONO_ADD_INS (bblock, ins);
4046 /* FIXME: add the implicit STELEM_REF castclass */
4048 cfg->disable_ssa = TRUE;
4051 case CEE_STELEM_REF: {
4052 MonoInst *iargs [3];
4057 handle_loaded_temps (cfg, bblock, stack_start, sp);
4063 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4067 NEW_GROUP (cfg, group, sp [0], sp [1]);
4068 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
4070 ins->inst_left = group;
4071 ins->inst_right = sp [2];
4072 MONO_ADD_INS (bblock, ins);
4077 cfg->disable_ssa = TRUE;
4080 case CEE_CKFINITE: {
4081 MonoInst *store, *temp;
4084 /* this instr. can throw exceptions as side effect,
4085 * so we cant eliminate dead code which contains CKFINITE opdodes.
4086 * Spilling to memory makes sure that we always perform
4090 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
4092 ins->inst_left = sp [-1];
4093 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
4095 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4096 store->cil_code = ip;
4097 MONO_ADD_INS (bblock, store);
4099 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
4106 g_error ("opcode 0x%02x not handled", *ip);
4110 MonoClass *handle_class;
4112 CHECK_STACK_OVF (1);
4114 n = read32 (ip + 1);
4116 handle = mono_ldtoken (image, n, &handle_class);
4117 mono_class_init (handle_class);
4119 if (((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot)) {
4121 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
4123 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
4125 NEW_IMAGECONST (cfg, iargs [0], image);
4126 NEW_ICONST (cfg, iargs [1], n);
4127 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
4128 NEW_TEMPLOAD (cfg, res, temp);
4129 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4130 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
4131 MONO_ADD_INS (bblock, store);
4132 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4134 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method (image, read32 (ip + 6), NULL)) &&
4135 (cmethod->klass == mono_defaults.monotype_class->parent) &&
4136 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
4137 MonoClass *tclass = mono_class_from_mono_type (handle);
4138 mono_class_init (tclass);
4139 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
4140 ins->type = STACK_OBJ;
4141 ins->klass = cmethod->klass;
4144 NEW_PCONST (cfg, ins, handle);
4145 ins->type = STACK_VTYPE;
4146 ins->klass = handle_class;
4162 case CEE_ADD_OVF_UN:
4164 case CEE_MUL_OVF_UN:
4166 case CEE_SUB_OVF_UN:
4171 case CEE_ENDFINALLY:
4172 /* FIXME: check stack state */
4173 MONO_INST_NEW (cfg, ins, *ip);
4174 MONO_ADD_INS (bblock, ins);
4175 ins->cil_code = ip++;
4176 start_new_bblock = 1;
4181 if (*ip == CEE_LEAVE) {
4182 target = ip + 5 + (gint32)read32(ip + 1);
4184 target = ip + 2 + (signed char)(ip [1]);
4187 /* empty the stack */
4188 while (sp != stack_start) {
4189 MONO_INST_NEW (cfg, ins, CEE_POP);
4193 MONO_ADD_INS (bblock, ins);
4196 /* fixme: call fault handler ? */
4198 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
4200 for (tmp = handlers; tmp; tmp = tmp->next) {
4202 link_bblock (cfg, bblock, tblock);
4203 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
4205 ins->inst_target_bb = tblock;
4206 MONO_ADD_INS (bblock, ins);
4208 g_list_free (handlers);
4211 MONO_INST_NEW (cfg, ins, CEE_BR);
4213 MONO_ADD_INS (bblock, ins);
4214 GET_BBLOCK (cfg, bbhash, tblock, target);
4215 link_bblock (cfg, bblock, tblock);
4216 CHECK_BBLOCK (target, ip, tblock);
4217 ins->inst_target_bb = tblock;
4218 start_new_bblock = 1;
4220 if (*ip == CEE_LEAVE)
4229 MONO_INST_NEW (cfg, ins, *ip);
4231 handle_loaded_temps (cfg, bblock, stack_start, sp);
4232 MONO_ADD_INS (bblock, ins);
4233 ins->cil_code = ip++;
4234 ins->inst_i0 = sp [0];
4235 ins->inst_i1 = sp [1];
4243 /* trampoline mono specific opcodes */
4244 case MONO_CUSTOM_PREFIX: {
4246 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
4250 case CEE_MONO_FUNC1: {
4252 gpointer func = NULL;
4257 case MONO_MARSHAL_CONV_STR_LPWSTR:
4258 func = mono_string_to_utf16;
4260 case MONO_MARSHAL_CONV_LPWSTR_STR:
4261 func = mono_string_from_utf16;
4263 case MONO_MARSHAL_CONV_LPSTR_STR:
4264 func = mono_string_new_wrapper;
4266 case MONO_MARSHAL_CONV_STR_LPTSTR:
4267 case MONO_MARSHAL_CONV_STR_LPSTR:
4268 func = mono_string_to_utf8;
4270 case MONO_MARSHAL_CONV_STR_BSTR:
4271 func = mono_string_to_bstr;
4273 case MONO_MARSHAL_CONV_STR_TBSTR:
4274 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
4275 func = mono_string_to_ansibstr;
4277 case MONO_MARSHAL_CONV_SB_LPSTR:
4278 func = mono_string_builder_to_utf8;
4280 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
4281 func = mono_array_to_savearray;
4283 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
4284 func = mono_array_to_lparray;
4286 case MONO_MARSHAL_CONV_DEL_FTN:
4287 func = mono_delegate_to_ftnptr;
4289 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
4290 func = mono_marshal_string_array;
4293 g_warning ("unknown conversion %d\n", ip [2]);
4294 g_assert_not_reached ();
4297 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4298 NEW_TEMPLOAD (cfg, *sp, temp);
4302 inline_costs += 10 * num_calls++;
4305 case CEE_MONO_PROC2: {
4306 gpointer func = NULL;
4311 case MONO_MARSHAL_CONV_LPSTR_SB:
4312 func = mono_string_utf8_to_builder;
4314 case MONO_MARSHAL_FREE_ARRAY:
4315 func = mono_marshal_free_array;
4318 g_assert_not_reached ();
4321 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4323 inline_costs += 10 * num_calls++;
4326 case CEE_MONO_PROC3: {
4327 gpointer func = NULL;
4332 case MONO_MARSHAL_CONV_STR_BYVALSTR:
4333 func = mono_string_to_byvalstr;
4335 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
4336 func = mono_string_to_byvalwstr;
4339 g_assert_not_reached ();
4342 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4344 inline_costs += 10 * num_calls++;
4350 mono_emit_jit_icall (cfg, bblock, g_free, sp, ip);
4352 inline_costs += 10 * num_calls++;
4354 case CEE_MONO_LDPTR:
4355 CHECK_STACK_OVF (1);
4356 token = read32 (ip + 2);
4357 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
4361 inline_costs += 10 * num_calls++;
4363 case CEE_MONO_VTADDR:
4366 MONO_INST_NEW (cfg, ins, OP_VTADDR);
4368 ins->type = STACK_MP;
4369 ins->inst_left = *sp;
4373 case CEE_MONO_NEWOBJ: {
4374 MonoInst *iargs [2];
4376 CHECK_STACK_OVF (1);
4377 token = read32 (ip + 2);
4378 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4379 mono_class_init (klass);
4380 NEW_DOMAINCONST (cfg, iargs [0]);
4381 NEW_CLASSCONST (cfg, iargs [1], klass);
4382 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
4383 NEW_TEMPLOAD (cfg, *sp, temp);
4386 inline_costs += 10 * num_calls++;
4389 case CEE_MONO_OBJADDR:
4392 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
4394 ins->type = STACK_MP;
4395 ins->inst_left = *sp;
4399 case CEE_MONO_LDNATIVEOBJ:
4401 token = read32 (ip + 2);
4402 klass = mono_method_get_wrapper_data (method, token);
4403 g_assert (klass->valuetype);
4404 mono_class_init (klass);
4405 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
4409 case CEE_MONO_RETOBJ:
4410 g_assert (cfg->ret);
4411 g_assert (method->signature->pinvoke);
4415 token = read32 (ip + 2);
4416 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4418 NEW_RETLOADA (cfg, ins);
4419 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
4421 if (sp != stack_start)
4424 MONO_INST_NEW (cfg, ins, CEE_BR);
4426 ins->inst_target_bb = end_bblock;
4427 MONO_ADD_INS (bblock, ins);
4428 link_bblock (cfg, bblock, end_bblock);
4429 start_new_bblock = 1;
4433 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
4441 /* somewhat similar to LDTOKEN */
4442 MonoInst *addr, *vtvar;
4444 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
4446 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4447 addr->cil_code = ip;
4448 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
4450 ins->inst_left = addr;
4451 MONO_ADD_INS (bblock, ins);
4452 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4465 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
4466 MONO_INST_NEW (cfg, ins, cmp->opcode);
4468 cmp->inst_i0 = sp [0];
4469 cmp->inst_i1 = sp [1];
4473 cmp->opcode = OP_COMPARE;
4475 ins->type = STACK_I4;
4485 CHECK_STACK_OVF (1);
4486 n = read32 (ip + 2);
4487 if (method->wrapper_type != MONO_WRAPPER_NONE)
4488 cmethod = mono_method_get_wrapper_data (method, n);
4490 cmethod = mono_get_method (image, n, NULL);
4493 * We can't do this in mono_ldftn, since it is used in
4494 * the synchronized wrapper, leading to an infinite loop.
4496 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
4497 cmethod = mono_marshal_get_synchronized_wrapper (cmethod);
4500 mono_class_init (cmethod->klass);
4501 handle_loaded_temps (cfg, bblock, stack_start, sp);
4503 NEW_METHODCONST (cfg, argconst, cmethod);
4504 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
4505 NEW_TEMPLOAD (cfg, *sp, temp);
4509 inline_costs += 10 * num_calls++;
4512 case CEE_LDVIRTFTN: {
4517 n = read32 (ip + 2);
4518 if (method->wrapper_type != MONO_WRAPPER_NONE)
4519 cmethod = mono_method_get_wrapper_data (method, n);
4521 cmethod = mono_get_method (image, n, NULL);
4523 mono_class_init (cmethod->klass);
4524 handle_loaded_temps (cfg, bblock, stack_start, sp);
4528 NEW_METHODCONST (cfg, args [1], cmethod);
4529 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
4530 NEW_TEMPLOAD (cfg, *sp, temp);
4534 inline_costs += 10 * num_calls++;
4538 CHECK_STACK_OVF (1);
4539 NEW_ARGLOAD (cfg, ins, read16 (ip + 2));
4545 CHECK_STACK_OVF (1);
4546 NEW_ARGLOADA (cfg, ins, read16 (ip + 2));
4554 handle_loaded_temps (cfg, bblock, stack_start, sp);
4555 n = read16 (ip + 2);
4556 NEW_ARGSTORE (cfg, ins, n, *sp);
4558 if (ins->opcode == CEE_STOBJ) {
4559 NEW_ARGLOADA (cfg, ins, n);
4560 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4562 MONO_ADD_INS (bblock, ins);
4566 CHECK_STACK_OVF (1);
4567 NEW_LOCLOAD (cfg, ins, read16 (ip + 2));
4573 CHECK_STACK_OVF (1);
4574 NEW_LOCLOADA (cfg, ins, read16 (ip + 2));
4582 n = read16 (ip + 2);
4583 handle_loaded_temps (cfg, bblock, stack_start, sp);
4584 NEW_LOCSTORE (cfg, ins, n, *sp);
4586 if (ins->opcode == CEE_STOBJ) {
4587 NEW_LOCLOADA (cfg, ins, n);
4588 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4590 MONO_ADD_INS (bblock, ins);
4597 if (sp != stack_start)
4599 MONO_INST_NEW (cfg, ins, 256 + ip [1]);
4600 ins->inst_left = *sp;
4603 if (header->init_locals)
4604 ins->flags |= MONO_INST_INIT;
4608 /* FIXME: set init flag if locals init is set in this method */
4610 case CEE_ENDFILTER: {
4611 MonoExceptionClause *clause, *nearest;
4612 int cc, nearest_num;
4616 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
4618 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
4619 ins->inst_left = *sp;
4621 MONO_ADD_INS (bblock, ins);
4622 start_new_bblock = 1;
4626 for (cc = 0; cc < header->num_clauses; ++cc) {
4627 clause = &header->clauses [cc];
4628 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
4629 (!nearest || (clause->token_or_filter > nearest->token_or_filter))) {
4635 filter_lengths [nearest_num] = (ip - header->code) - nearest->token_or_filter;
4639 case CEE_UNALIGNED_:
4640 ins_flag |= MONO_INST_UNALIGNED;
4644 ins_flag |= MONO_INST_VOLATILE;
4648 ins_flag |= MONO_INST_TAILCALL;
4654 token = read32 (ip + 2);
4655 if (method->wrapper_type != MONO_WRAPPER_NONE)
4656 klass = mono_method_get_wrapper_data (method, token);
4658 klass = mono_class_get (image, token);
4659 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
4665 MonoInst *iargs [3];
4671 handle_loaded_temps (cfg, bblock, stack_start, sp);
4672 if (ip [1] == CEE_CPBLK) {
4673 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
4675 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
4683 /* FIXME: check we are in a catch handler */
4684 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
4685 load->cil_code = ip;
4686 MONO_INST_NEW (cfg, ins, CEE_THROW);
4687 ins->inst_left = load;
4689 MONO_ADD_INS (bblock, ins);
4691 start_new_bblock = 1;
4696 CHECK_STACK_OVF (1);
4697 token = read32 (ip + 2);
4698 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
4699 MonoType *type = mono_type_create_from_typespec (image, token);
4700 token = mono_type_size (type, &align);
4701 mono_metadata_free_type (type);
4703 MonoClass *szclass = mono_class_get (image, token);
4704 mono_class_init (szclass);
4705 token = mono_class_value_size (szclass, &align);
4707 NEW_ICONST (cfg, ins, token);
4712 case CEE_REFANYTYPE:
4713 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
4716 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
4721 g_error ("opcode 0x%02x not handled", *ip);
4724 if (start_new_bblock != 1)
4727 bblock->cil_length = ip - bblock->cil_code;
4728 bblock->next_bb = end_bblock;
4729 link_bblock (cfg, bblock, end_bblock);
4731 if (cfg->method == method && cfg->domainvar) {
4735 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
4736 call->signature = helper_sig_domain_get;
4737 call->inst.type = STACK_PTR;
4738 call->fptr = mono_domain_get;
4739 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, (MonoInst*)call);
4741 MONO_ADD_INS (init_localsbb, store);
4744 if (header->init_locals) {
4746 for (i = 0; i < header->num_locals; ++i) {
4747 int t = header->locals [i]->type;
4748 if (t == MONO_TYPE_VALUETYPE && header->locals [i]->data.klass->enumtype)
4749 t = header->locals [i]->data.klass->enum_basetype->type;
4750 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
4751 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
4752 NEW_ICONST (cfg, ins, 0);
4753 NEW_LOCSTORE (cfg, store, i, ins);
4754 MONO_ADD_INS (init_localsbb, store);
4755 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
4756 MONO_INST_NEW (cfg, ins, OP_I8CONST);
4757 ins->type = STACK_I8;
4759 NEW_LOCSTORE (cfg, store, i, ins);
4760 MONO_ADD_INS (init_localsbb, store);
4761 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
4762 MONO_INST_NEW (cfg, ins, OP_R8CONST);
4763 ins->type = STACK_R8;
4764 ins->inst_p0 = (void*)&r8_0;
4765 NEW_LOCSTORE (cfg, store, i, ins);
4766 MONO_ADD_INS (init_localsbb, store);
4767 } else if (t == MONO_TYPE_VALUETYPE) {
4768 NEW_LOCLOADA (cfg, ins, i);
4769 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (header->locals [i]), NULL, NULL);
4772 NEW_PCONST (cfg, ins, NULL);
4773 NEW_LOCSTORE (cfg, store, i, ins);
4774 MONO_ADD_INS (init_localsbb, store);
4780 /* resolve backward branches in the middle of an existing basic block */
4781 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
4783 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
4784 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
4785 if (tblock != start_bblock) {
4787 split_bblock (cfg, tblock, bblock);
4788 l = bblock->cil_code - header->code;
4789 bblock->cil_length = tblock->cil_length - l;
4790 tblock->cil_length = l;
4792 g_print ("recheck failed.\n");
4796 /* we compute regions here, because the length of filter clauses is not known in advance.
4797 * It is computed in the CEE_ENDFILTER case in the above switch statement*/
4798 if (cfg->method == method) {
4800 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
4801 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
4802 if (cfg->verbose_level > 2)
4803 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
4806 g_hash_table_destroy (bbhash);
4809 dont_inline = g_list_remove (dont_inline, method);
4810 return inline_costs;
4813 if (cfg->method != method)
4814 g_hash_table_destroy (bbhash);
4815 dont_inline = g_list_remove (dont_inline, method);
4819 if (cfg->method != method)
4820 g_hash_table_destroy (bbhash);
4821 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
4822 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
4823 dont_inline = g_list_remove (dont_inline, method);
4828 mono_print_tree (MonoInst *tree) {
4834 arity = mono_burg_arity [tree->opcode];
4836 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
4838 switch (tree->opcode) {
4840 printf ("[%d]", tree->inst_c0);
4843 printf ("[%lld]", tree->inst_l);
4846 printf ("[%f]", *(double*)tree->inst_p0);
4849 printf ("[%f]", *(float*)tree->inst_p0);
4853 printf ("[%d]", tree->inst_c0);
4856 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
4859 printf ("[%s]", mono_arch_regname (tree->dreg));
4862 printf ("[%s]", tree->inst_newa_class->name);
4863 mono_print_tree (tree->inst_newa_len);
4874 case OP_VOIDCALLVIRT: {
4875 MonoCallInst *call = (MonoCallInst*)tree;
4877 printf ("[%s]", call->method->name);
4882 printf ("[%d (", tree->inst_c0);
4883 for (i = 0; i < tree->inst_phi_args [0]; i++) {
4886 printf ("%d", tree->inst_phi_args [i + 1]);
4898 printf ("[B%d]", tree->inst_target_bb->block_num);
4908 case OP_VOIDCALL_REG:
4909 mono_print_tree (tree->inst_left);
4921 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
4922 mono_print_tree (tree->inst_left);
4926 mono_print_tree (tree->inst_left);
4928 mono_print_tree (tree->inst_right);
4938 create_helper_signature (void)
4940 /* FIXME: set call conv */
4941 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
4942 helper_sig_newarr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4943 helper_sig_newarr->params [0] = helper_sig_newarr->params [1] = &mono_defaults.int_class->byval_arg;
4944 helper_sig_newarr->ret = &mono_defaults.object_class->byval_arg;
4945 helper_sig_newarr->params [2] = &mono_defaults.int32_class->byval_arg;
4946 helper_sig_newarr->pinvoke = 1;
4948 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
4949 helper_sig_newarr_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4950 helper_sig_newarr_specific->params [0] = &mono_defaults.int_class->byval_arg;
4951 helper_sig_newarr_specific->params [1] = &mono_defaults.int32_class->byval_arg;
4952 helper_sig_newarr_specific->ret = &mono_defaults.object_class->byval_arg;
4953 helper_sig_newarr_specific->pinvoke = 1;
4955 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
4956 helper_sig_object_new = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4957 helper_sig_object_new->params [0] = helper_sig_object_new->params [1] = &mono_defaults.int_class->byval_arg;
4958 helper_sig_object_new->ret = &mono_defaults.object_class->byval_arg;
4959 helper_sig_object_new->pinvoke = 1;
4961 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
4962 helper_sig_object_new_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4963 helper_sig_object_new_specific->params [0] = &mono_defaults.int_class->byval_arg;
4964 helper_sig_object_new_specific->ret = &mono_defaults.object_class->byval_arg;
4965 helper_sig_object_new_specific->pinvoke = 1;
4967 /* void* mono_method_compile (MonoMethod*) */
4968 helper_sig_compile = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4969 helper_sig_compile->params [0] = helper_sig_compile->ret = &mono_defaults.int_class->byval_arg;
4970 helper_sig_compile->pinvoke = 1;
4972 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
4973 helper_sig_compile_virt = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4974 helper_sig_compile_virt->params [0] = &mono_defaults.object_class->byval_arg;
4975 helper_sig_compile_virt->params [1] = helper_sig_compile_virt->ret = &mono_defaults.int_class->byval_arg;
4976 helper_sig_compile_virt->pinvoke = 1;
4978 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
4979 helper_sig_ldstr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4980 helper_sig_ldstr->params [0] = helper_sig_ldstr->params [1] = &mono_defaults.int_class->byval_arg;
4981 helper_sig_ldstr->params [2] = &mono_defaults.int32_class->byval_arg;
4982 helper_sig_ldstr->ret = &mono_defaults.object_class->byval_arg;
4983 helper_sig_ldstr->pinvoke = 1;
4985 /* MonoDomain *mono_domain_get (void) */
4986 helper_sig_domain_get = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
4987 helper_sig_domain_get->ret = &mono_defaults.int_class->byval_arg;
4988 helper_sig_domain_get->pinvoke = 1;
4990 /* void* stelem_ref (MonoArray *, int index, MonoObject *) */
4991 helper_sig_stelem_ref = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4992 helper_sig_stelem_ref->params [0] = &mono_defaults.array_class->byval_arg;
4993 helper_sig_stelem_ref->params [1] = &mono_defaults.int32_class->byval_arg;
4994 helper_sig_stelem_ref->params [2] = &mono_defaults.object_class->byval_arg;
4995 helper_sig_stelem_ref->ret = &mono_defaults.void_class->byval_arg;
4996 helper_sig_stelem_ref->pinvoke = 1;
4998 /* long amethod (long, long) */
4999 helper_sig_long_long_long = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5000 helper_sig_long_long_long->params [0] = helper_sig_long_long_long->params [1] =
5001 &mono_defaults.int64_class->byval_arg;
5002 helper_sig_long_long_long->ret = &mono_defaults.int64_class->byval_arg;
5003 helper_sig_long_long_long->pinvoke = 1;
5005 /* object amethod (intptr) */
5006 helper_sig_obj_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5007 helper_sig_obj_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5008 helper_sig_obj_ptr->ret = &mono_defaults.object_class->byval_arg;
5009 helper_sig_obj_ptr->pinvoke = 1;
5011 /* void amethod (intptr) */
5012 helper_sig_void_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5013 helper_sig_void_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5014 helper_sig_void_ptr->ret = &mono_defaults.void_class->byval_arg;
5015 helper_sig_void_ptr->pinvoke = 1;
5017 /* void amethod (MonoObject *obj) */
5018 helper_sig_void_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5019 helper_sig_void_obj->params [0] = &mono_defaults.object_class->byval_arg;
5020 helper_sig_void_obj->ret = &mono_defaults.void_class->byval_arg;
5021 helper_sig_void_obj->pinvoke = 1;
5023 /* intptr amethod (void) */
5024 helper_sig_ptr_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5025 helper_sig_ptr_void->ret = &mono_defaults.int_class->byval_arg;
5026 helper_sig_ptr_void->pinvoke = 1;
5028 /* void amethod (intptr, intptr) */
5029 helper_sig_void_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5030 helper_sig_void_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5031 helper_sig_void_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5032 helper_sig_void_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
5033 helper_sig_void_ptr_ptr->pinvoke = 1;
5035 /* void amethod (intptr, intptr, intptr) */
5036 helper_sig_void_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5037 helper_sig_void_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5038 helper_sig_void_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5039 helper_sig_void_ptr_ptr_ptr->params [2] = &mono_defaults.int_class->byval_arg;
5040 helper_sig_void_ptr_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
5041 helper_sig_void_ptr_ptr_ptr->pinvoke = 1;
5043 /* intptr amethod (intptr, intptr) */
5044 helper_sig_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5045 helper_sig_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5046 helper_sig_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5047 helper_sig_ptr_ptr_ptr->ret = &mono_defaults.int_class->byval_arg;
5048 helper_sig_ptr_ptr_ptr->pinvoke = 1;
5050 /* IntPtr amethod (object) */
5051 helper_sig_ptr_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5052 helper_sig_ptr_obj->params [0] = &mono_defaults.object_class->byval_arg;
5053 helper_sig_ptr_obj->ret = &mono_defaults.int_class->byval_arg;
5054 helper_sig_ptr_obj->pinvoke = 1;
5056 /* IntPtr amethod (int) */
5057 helper_sig_ptr_int = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5058 helper_sig_ptr_int->params [0] = &mono_defaults.int32_class->byval_arg;
5059 helper_sig_ptr_int->ret = &mono_defaults.int_class->byval_arg;
5060 helper_sig_ptr_int->pinvoke = 1;
5062 /* long amethod (long, guint32) */
5063 helper_sig_long_long_int = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5064 helper_sig_long_long_int->params [0] = &mono_defaults.int64_class->byval_arg;
5065 helper_sig_long_long_int->params [1] = &mono_defaults.int32_class->byval_arg;
5066 helper_sig_long_long_int->ret = &mono_defaults.int64_class->byval_arg;
5067 helper_sig_long_long_int->pinvoke = 1;
5069 /* ulong amethod (double) */
5070 helper_sig_ulong_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5071 helper_sig_ulong_double->params [0] = &mono_defaults.double_class->byval_arg;
5072 helper_sig_ulong_double->ret = &mono_defaults.uint64_class->byval_arg;
5073 helper_sig_ulong_double->pinvoke = 1;
5075 /* long amethod (double) */
5076 helper_sig_long_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5077 helper_sig_long_double->params [0] = &mono_defaults.double_class->byval_arg;
5078 helper_sig_long_double->ret = &mono_defaults.int64_class->byval_arg;
5079 helper_sig_long_double->pinvoke = 1;
5081 /* uint amethod (double) */
5082 helper_sig_uint_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5083 helper_sig_uint_double->params [0] = &mono_defaults.double_class->byval_arg;
5084 helper_sig_uint_double->ret = &mono_defaults.uint32_class->byval_arg;
5085 helper_sig_uint_double->pinvoke = 1;
5087 /* int amethod (double) */
5088 helper_sig_int_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5089 helper_sig_int_double->params [0] = &mono_defaults.double_class->byval_arg;
5090 helper_sig_int_double->ret = &mono_defaults.int32_class->byval_arg;
5091 helper_sig_int_double->pinvoke = 1;
5093 /* void initobj (intptr, int size) */
5094 helper_sig_initobj = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5095 helper_sig_initobj->params [0] = &mono_defaults.int_class->byval_arg;
5096 helper_sig_initobj->params [1] = &mono_defaults.int32_class->byval_arg;
5097 helper_sig_initobj->ret = &mono_defaults.void_class->byval_arg;
5098 helper_sig_initobj->pinvoke = 1;
5100 /* void memcpy (intptr, intptr, int size) */
5101 helper_sig_memcpy = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5102 helper_sig_memcpy->params [0] = &mono_defaults.int_class->byval_arg;
5103 helper_sig_memcpy->params [1] = &mono_defaults.int_class->byval_arg;
5104 helper_sig_memcpy->params [2] = &mono_defaults.int32_class->byval_arg;
5105 helper_sig_memcpy->ret = &mono_defaults.void_class->byval_arg;
5106 helper_sig_memcpy->pinvoke = 1;
5108 /* void memset (intptr, int val, int size) */
5109 helper_sig_memset = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5110 helper_sig_memset->params [0] = &mono_defaults.int_class->byval_arg;
5111 helper_sig_memset->params [1] = &mono_defaults.int32_class->byval_arg;
5112 helper_sig_memset->params [2] = &mono_defaults.int32_class->byval_arg;
5113 helper_sig_memset->ret = &mono_defaults.void_class->byval_arg;
5114 helper_sig_memset->pinvoke = 1;
5117 static GHashTable *jit_icall_hash_name = NULL;
5118 static GHashTable *jit_icall_hash_addr = NULL;
5121 mono_find_jit_icall_by_name (const char *name)
5123 g_assert (jit_icall_hash_name);
5125 //printf ("lookup addr %s %p\n", name, g_hash_table_lookup (jit_icall_hash_name, name));
5126 return g_hash_table_lookup (jit_icall_hash_name, name);
5130 mono_find_jit_icall_by_addr (gconstpointer addr)
5132 g_assert (jit_icall_hash_addr);
5134 return g_hash_table_lookup (jit_icall_hash_addr, (gpointer)addr);
5138 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
5141 MonoMethod *wrapper;
5143 if (callinfo->wrapper)
5144 return callinfo->wrapper;
5145 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
5146 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
5147 callinfo->wrapper = mono_jit_compile_method (wrapper);
5149 return callinfo->wrapper;
5153 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save)
5155 MonoJitICallInfo *info;
5160 if (!jit_icall_hash_name) {
5161 jit_icall_hash_name = g_hash_table_new (g_str_hash, g_str_equal);
5162 jit_icall_hash_addr = g_hash_table_new (NULL, NULL);
5165 if (g_hash_table_lookup (jit_icall_hash_name, name)) {
5166 g_warning ("jit icall already defined \"%s\"\n", name);
5167 g_assert_not_reached ();
5170 info = g_new (MonoJitICallInfo, 1);
5177 #ifdef MONO_USE_EXC_TABLES
5178 || mono_arch_has_unwind_info (func)
5181 info->wrapper = func;
5183 info->wrapper = NULL;
5184 mono_icall_get_wrapper (info);
5187 g_hash_table_insert (jit_icall_hash_name, (gpointer)info->name, info);
5188 g_hash_table_insert (jit_icall_hash_addr, (gpointer)func, info);
5189 if (func != info->wrapper)
5190 g_hash_table_insert (jit_icall_hash_addr, (gpointer)info->wrapper, info);
5195 static GHashTable *emul_opcode_hash = NULL;
5197 static MonoJitICallInfo *
5198 mono_find_jit_opcode_emulation (int opcode)
5200 if (emul_opcode_hash)
5201 return g_hash_table_lookup (emul_opcode_hash, (gpointer)opcode);
5207 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func)
5209 MonoJitICallInfo *info;
5211 if (!emul_opcode_hash)
5212 emul_opcode_hash = g_hash_table_new (NULL, NULL);
5214 g_assert (!sig->hasthis);
5215 g_assert (sig->param_count < 3);
5217 info = mono_register_jit_icall (func, name, sig, FALSE);
5219 g_hash_table_insert (emul_opcode_hash, (gpointer)opcode, info);
5223 decompose_foreach (MonoInst *tree, gpointer data)
5225 static MonoJitICallInfo *newarr_info = NULL;
5226 static MonoJitICallInfo *newarr_specific_info = NULL;
5227 MonoJitICallInfo *info;
5229 switch (tree->opcode) {
5231 MonoCompile *cfg = data;
5232 MonoInst *iargs [3];
5235 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
5236 g_assert (newarr_info);
5237 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
5238 g_assert (newarr_specific_info);
5241 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
5242 NEW_DOMAINCONST (cfg, iargs [0]);
5243 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
5244 iargs [2] = tree->inst_newa_len;
5249 MonoVTable *vtable = mono_class_vtable (cfg->domain,
5250 mono_array_class_get (&tree->inst_newa_class->byval_arg, 1));
5252 NEW_PCONST (cfg, iargs [0], vtable);
5253 iargs [1] = tree->inst_newa_len;
5255 info = newarr_specific_info;
5258 mono_emulate_opcode (cfg, tree, iargs, info);
5268 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
5270 switch (mono_burg_arity [tree->opcode]) {
5273 mono_inst_foreach (tree->inst_left, func, data);
5276 mono_inst_foreach (tree->inst_left, func, data);
5277 mono_inst_foreach (tree->inst_right, func, data);
5280 g_assert_not_reached ();
5287 mono_print_bb_code (MonoBasicBlock *bb) {
5289 MonoInst *c = bb->code;
5291 mono_print_tree (c);
5300 print_dfn (MonoCompile *cfg) {
5305 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
5307 for (i = 0; i < cfg->num_bblocks; ++i) {
5308 bb = cfg->bblocks [i];
5310 char* code1, *code2;
5311 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
5312 if (bb->last_ins->cil_code)
5313 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
5315 code2 = g_strdup ("");
5317 code1 [strlen (code1) - 1] = 0;
5318 code = g_strdup_printf ("%s -> %s", code1, code2);
5322 code = g_strdup ("\n");
5323 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
5325 MonoInst *c = bb->code;
5327 mono_print_tree (c);
5335 g_print ("\tprev:");
5336 for (j = 0; j < bb->in_count; ++j) {
5337 g_print (" BB%d", bb->in_bb [j]->block_num);
5339 g_print ("\t\tsucc:");
5340 for (j = 0; j < bb->out_count; ++j) {
5341 g_print (" BB%d", bb->out_bb [j]->block_num);
5343 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
5346 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
5349 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
5351 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
5359 * returns the offset used by spillvar. It allocates a new
5360 * spill variable if necessary.
5363 mono_spillvar_offset (MonoCompile *cfg, int spillvar)
5365 MonoSpillInfo **si, *info;
5368 si = &cfg->spill_info;
5370 while (i <= spillvar) {
5373 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
5375 cfg->stack_offset -= sizeof (gpointer);
5376 info->offset = cfg->stack_offset;
5380 return (*si)->offset;
5386 g_assert_not_reached ();
5391 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
5395 g_assert (bb->code);
5396 bb->last_ins->next = inst;
5397 bb->last_ins = inst;
5399 bb->last_ins = bb->code = inst;
5404 mono_destroy_compile (MonoCompile *cfg)
5406 //mono_mempool_stats (cfg->mempool);
5407 g_hash_table_destroy (cfg->bb_hash);
5409 mono_regstate_free (cfg->rs);
5410 mono_mempool_destroy (cfg->mempool);
5411 g_list_free (cfg->ldstr_list);
5413 g_free (cfg->varinfo);
5419 mono_get_lmf_addr (void)
5421 MonoJitTlsData *jit_tls;
5423 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
5424 return &jit_tls->lmf;
5426 g_assert_not_reached ();
5431 * mono_thread_abort:
5432 * @obj: exception object
5434 * abort the thread, print exception information and stack trace
5437 mono_thread_abort (MonoObject *obj)
5439 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
5447 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
5449 MonoJitTlsData *jit_tls;
5452 jit_tls = g_new0 (MonoJitTlsData, 1);
5454 TlsSetValue (mono_jit_tls_id, jit_tls);
5456 jit_tls->abort_func = mono_thread_abort;
5457 jit_tls->end_of_stack = stack_start;
5459 lmf = g_new0 (MonoLMF, 1);
5465 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
5468 mono_thread_abort_dummy (MonoObject *obj)
5470 if (mono_thread_attach_aborted_cb)
5471 mono_thread_attach_aborted_cb (obj);
5473 mono_thread_abort (obj);
5477 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
5479 MonoJitTlsData *jit_tls;
5482 jit_tls = g_new0 (MonoJitTlsData, 1);
5484 TlsSetValue (mono_jit_tls_id, jit_tls);
5486 jit_tls->abort_func = mono_thread_abort_dummy;
5487 jit_tls->end_of_stack = stack_start;
5489 lmf = g_new0 (MonoLMF, 1);
5496 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
5498 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
5502 ji->data.target = target;
5503 ji->next = cfg->patch_info;
5505 cfg->patch_info = ji;
5509 mono_remove_patch_info (MonoCompile *cfg, int ip)
5511 MonoJumpInfo **ji = &cfg->patch_info;
5514 if ((*ji)->ip.i == ip)
5517 ji = &((*ji)->next);
5522 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
5523 MonoJitICallInfo *info;
5525 switch (mono_burg_arity [tree->opcode]) {
5528 dec_foreach (tree->inst_left, cfg);
5530 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
5531 MonoInst *iargs [2];
5533 iargs [0] = tree->inst_left;
5535 mono_emulate_opcode (cfg, tree, iargs, info);
5541 if (tree->opcode == OP_LMUL
5542 && (cfg->opt & MONO_OPT_INTRINS)
5543 && (tree->inst_left->opcode == CEE_CONV_I8
5544 || tree->inst_left->opcode == CEE_CONV_U8)
5545 && tree->inst_left->inst_left->type == STACK_I4
5546 && (tree->inst_right->opcode == CEE_CONV_I8
5547 || tree->inst_right->opcode == CEE_CONV_U8)
5548 && tree->inst_right->inst_left->type == STACK_I4) {
5549 tree->opcode = OP_BIGMUL;
5550 tree->inst_left = tree->inst_left->inst_left;
5551 tree->inst_right = tree->inst_right->inst_left;
5552 dec_foreach (tree, cfg);
5553 } else if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
5554 MonoInst *iargs [2];
5556 iargs [0] = tree->inst_i0;
5557 iargs [1] = tree->inst_i1;
5559 mono_emulate_opcode (cfg, tree, iargs, info);
5561 dec_foreach (iargs [0], cfg);
5562 dec_foreach (iargs [1], cfg);
5565 dec_foreach (tree->inst_left, cfg);
5566 dec_foreach (tree->inst_right, cfg);
5570 g_assert_not_reached ();
5572 decompose_foreach (tree, cfg);
5576 decompose_pass (MonoCompile *cfg) {
5579 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5582 cfg->prev_ins = NULL;
5583 for (tree = cfg->cbb->code; tree; tree = tree->next) {
5584 dec_foreach (tree, cfg);
5585 cfg->prev_ins = tree;
5591 nullify_basic_block (MonoBasicBlock *bb)
5598 bb->code = bb->last_ins = NULL;
5602 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
5606 for (i = 0; i < bb->out_count; i++) {
5607 MonoBasicBlock *ob = bb->out_bb [i];
5608 for (j = 0; j < ob->in_count; j++) {
5609 if (ob->in_bb [j] == orig)
5610 ob->in_bb [j] = repl;
5617 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
5619 bb->out_count = bbn->out_count;
5620 bb->out_bb = bbn->out_bb;
5622 replace_basic_block (bb, bbn, bb);
5626 bb->last_ins->next = bbn->code;
5627 bb->last_ins = bbn->last_ins;
5630 bb->code = bbn->code;
5631 bb->last_ins = bbn->last_ins;
5633 bb->next_bb = bbn->next_bb;
5634 nullify_basic_block (bbn);
5638 optimize_branches (MonoCompile *cfg) {
5639 int changed = FALSE;
5640 MonoBasicBlock *bb, *bbn;
5645 /* we skip the entry block (exit is handled specially instead ) */
5646 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
5648 /* dont touch code inside exception clauses */
5649 if (bb->region != -1)
5652 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
5653 if (cfg->verbose_level > 2)
5654 g_print ("nullify block triggered %d\n", bbn->block_num);
5656 bb->next_bb = bbn->next_bb;
5657 nullify_basic_block (bbn);
5661 if (bb->out_count == 1) {
5662 bbn = bb->out_bb [0];
5664 if (bb->region == bbn->region && bb->next_bb == bbn) {
5665 /* the block are in sequence anyway ... */
5668 * miguel: I do not understand what the test below does, could we
5669 * use a macro, or a comment here? opcode > CEE_BEQ && <= BLT_UN
5671 * It could also test for bb->last_in only once, and the value
5672 * could be cached (last_ins->opcode)
5674 if (bb->last_ins && (bb->last_ins->opcode == CEE_BR || (
5675 (bb->last_ins && bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN)))) {
5676 bb->last_ins->opcode = CEE_NOP;
5678 if (cfg->verbose_level > 2)
5679 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
5681 if (bbn->in_count == 1) {
5683 if (bbn != cfg->bb_exit) {
5684 if (cfg->verbose_level > 2)
5685 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
5686 merge_basic_blocks (bb, bbn);
5690 //mono_print_bb_code (bb);
5700 /* we skip the entry block (exit is handled specially instead ) */
5701 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
5703 /* dont touch code inside exception clauses */
5704 if (bb->region != -1)
5707 if (bb->out_count == 1) {
5708 bbn = bb->out_bb [0];
5710 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
5711 bbn = bb->last_ins->inst_target_bb;
5712 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
5713 bbn->code->inst_target_bb->region == bb->region) {
5715 if (cfg->verbose_level > 2)
5716 g_print ("in %s branch to branch triggered %d -> %d\n", cfg->method->name,
5717 bb->block_num, bbn->block_num);
5719 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
5720 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
5725 } else if (bb->out_count == 2) {
5726 /* fixme: this does not correctly - no idea whats wrong */
5727 if (0 && bb->last_ins && bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
5728 bbn = bb->last_ins->inst_true_bb;
5729 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
5730 bbn->code->inst_target_bb->region == bb->region) {
5731 if (cfg->verbose_level > 2)
5732 g_print ("cbranch to branch triggered %d -> %d (0x%02x)\n", bb->block_num,
5733 bbn->block_num, bbn->code->opcode);
5735 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
5736 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
5740 bbn = bb->last_ins->inst_false_bb;
5741 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
5742 bbn->code->inst_target_bb->region == bb->region) {
5743 if (cfg->verbose_level > 2)
5744 g_print ("cbranch to branch triggered %d -> %d (0x%02x)\n", bb->block_num,
5745 bbn->block_num, bbn->code->opcode);
5747 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
5748 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
5760 mono_compile_create_vars (MonoCompile *cfg)
5762 MonoMethodSignature *sig;
5763 MonoMethodHeader *header;
5766 header = ((MonoMethodNormal *)cfg->method)->header;
5768 sig = cfg->method->signature;
5770 if (!MONO_TYPE_IS_VOID (sig->ret)) {
5771 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
5772 cfg->ret->opcode = OP_RETARG;
5773 cfg->ret->inst_vtype = sig->ret;
5774 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
5778 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
5780 for (i = 0; i < sig->param_count; ++i)
5781 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
5783 cfg->locals_start = cfg->num_varinfo;
5785 for (i = 0; i < header->num_locals; ++i)
5786 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
5791 mono_print_code (MonoCompile *cfg)
5795 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5796 MonoInst *tree = bb->code;
5801 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
5803 for (; tree; tree = tree->next) {
5804 mono_print_tree (tree);
5809 bb->last_ins->next = NULL;
5814 extern const char * const mono_burg_rule_string [];
5817 emit_state (MonoCompile *cfg, MBState *state, int goal)
5820 int ern = mono_burg_rule (state, goal);
5821 const guint16 *nts = mono_burg_nts [ern];
5824 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
5828 // state->reg1 = state->reg2; /* chain rule */
5830 state->reg1 = mono_regstate_next_int (cfg->rs);
5831 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
5834 state->reg1 = mono_regstate_next_int (cfg->rs);
5835 state->reg2 = mono_regstate_next_int (cfg->rs);
5838 state->reg1 = mono_regstate_next_float (cfg->rs);
5845 mono_burg_kids (state, ern, kids);
5847 emit_state (cfg, kids [0], nts [0]);
5849 emit_state (cfg, kids [1], nts [1]);
5851 g_assert (!nts [3]);
5852 emit_state (cfg, kids [2], nts [2]);
5857 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
5858 if ((emit = mono_burg_func [ern]))
5859 emit (state, state->tree, cfg);
5862 #define DEBUG_SELECTION
5865 mini_select_instructions (MonoCompile *cfg)
5869 cfg->state_pool = mono_mempool_new ();
5870 cfg->rs = mono_regstate_new ();
5872 #ifdef DEBUG_SELECTION
5873 if (cfg->verbose_level >= 4) {
5874 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5875 MonoInst *tree = bb->code;
5876 g_print ("DUMP BLOCK %d:\n", bb->block_num);
5879 for (; tree; tree = tree->next) {
5880 mono_print_tree (tree);
5887 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5888 MonoInst *tree = bb->code, *next;
5894 bb->last_ins = NULL;
5897 mono_regstate_reset (cfg->rs);
5899 #ifdef DEBUG_SELECTION
5900 if (cfg->verbose_level >= 3)
5901 g_print ("LABEL BLOCK %d:\n", bb->block_num);
5903 for (; tree; tree = next) {
5905 #ifdef DEBUG_SELECTION
5906 if (cfg->verbose_level >= 3) {
5907 mono_print_tree (tree);
5912 if (!(mbstate = mono_burg_label (tree, cfg))) {
5913 g_warning ("unabled to label tree %p", tree);
5914 mono_print_tree (tree);
5916 g_assert_not_reached ();
5918 emit_state (cfg, mbstate, MB_NTERM_stmt);
5920 bb->max_ireg = cfg->rs->next_vireg;
5921 bb->max_freg = cfg->rs->next_vfreg;
5924 bb->last_ins->next = NULL;
5926 mono_mempool_empty (cfg->state_pool);
5928 mono_mempool_destroy (cfg->state_pool);
5932 mono_codegen (MonoCompile *cfg)
5934 MonoJumpInfo *patch_info;
5936 int i, max_epilog_size;
5939 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5940 cfg->spill_count = 0;
5941 /* we reuse dfn here */
5942 /* bb->dfn = bb_count++; */
5943 mono_arch_local_regalloc (cfg, bb);
5946 if (mono_trace_coverage)
5947 mono_allocate_coverage_info (cfg->method, cfg->num_bblocks);
5949 code = mono_arch_emit_prolog (cfg);
5951 if (mono_jit_profile)
5952 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
5954 cfg->code_len = code - cfg->native_code;
5955 cfg->prolog_end = cfg->code_len;
5957 mono_debug_open_method (cfg);
5959 /* emit code all basic blocks */
5960 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5961 bb->native_offset = cfg->code_len;
5962 mono_arch_output_basic_block (cfg, bb);
5964 cfg->bb_exit->native_offset = cfg->code_len;
5966 code = cfg->native_code + cfg->code_len;
5968 max_epilog_size = mono_arch_max_epilog_size (cfg);
5970 /* we always allocate code in cfg->domain->code_mp to increase locality */
5971 cfg->code_size = cfg->code_len + max_epilog_size;
5972 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
5973 code = mono_mempool_alloc (cfg->domain->code_mp, cfg->code_size);
5974 memcpy (code, cfg->native_code, cfg->code_len);
5975 g_free (cfg->native_code);
5976 cfg->native_code = code;
5977 code = cfg->native_code + cfg->code_len;
5979 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
5981 cfg->epilog_begin = cfg->code_len;
5983 if (mono_jit_profile)
5984 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
5986 cfg->code_len = code - cfg->native_code;
5988 mono_arch_emit_epilog (cfg);
5990 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5991 switch (patch_info->type) {
5992 case MONO_PATCH_INFO_ABS: {
5993 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
5995 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
5996 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
5997 patch_info->data.name = info->name;
6001 case MONO_PATCH_INFO_SWITCH: {
6002 gpointer *table = g_new (gpointer, patch_info->table_size);
6003 patch_info->ip.i = patch_info->ip.label->inst_c0;
6004 for (i = 0; i < patch_info->table_size; i++) {
6005 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
6007 patch_info->data.target = table;
6016 if (cfg->verbose_level > 1)
6017 g_print ("Method %s::%s emmitted at %p to %p\n", cfg->method->klass->name,
6018 cfg->method->name, cfg->native_code, cfg->native_code + cfg->code_len);
6020 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info);
6022 mono_debug_close_method (cfg);
6026 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
6031 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
6032 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
6034 if (cp->opcode == OP_ICONST) {
6035 if (cfg->opt & MONO_OPT_CONSPROP) {
6036 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
6040 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
6041 if (cfg->opt & MONO_OPT_COPYPROP) {
6042 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
6048 arity = mono_burg_arity [tree->opcode];
6051 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
6052 if (cfg->opt & MONO_OPT_CFOLD)
6053 mono_constant_fold_inst (tree, NULL);
6055 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
6056 if (cfg->opt & MONO_OPT_CFOLD)
6057 mono_constant_fold_inst (tree, NULL);
6059 mono_constant_fold_inst (tree, NULL);
6065 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
6069 switch (tree->opcode) {
6079 if (tree->ssa_op == MONO_SSA_NOP) {
6080 memset (acp, 0, sizeof (MonoInst *) * acp_size);
6097 case OP_VOIDCALL_REG:
6098 case OP_VOIDCALLVIRT:
6100 MonoCallInst *call = (MonoCallInst *)tree;
6101 MonoMethodSignature *sig = call->signature;
6102 int i, byref = FALSE;
6104 for (i = 0; i < sig->param_count; i++) {
6105 if (sig->params [i]->byref) {
6112 memset (acp, 0, sizeof (MonoInst *) * acp_size);
6120 arity = mono_burg_arity [tree->opcode];
6126 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
6129 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
6130 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
6133 g_assert_not_reached ();
6138 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
6140 MonoInst *tree = bb->code;
6146 for (; tree; tree = tree->next) {
6148 mono_cprop_copy_values (cfg, tree, acp);
6150 mono_cprop_invalidate_values (tree, acp, acp_size);
6152 if (tree->ssa_op == MONO_SSA_STORE &&
6153 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
6154 MonoInst *i1 = tree->inst_i1;
6156 acp [tree->inst_i0->inst_c0] = NULL;
6158 for (i = 0; i < acp_size; i++) {
6159 if (acp [i] && acp [i]->opcode != OP_ICONST &&
6160 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
6165 if (i1->opcode == OP_ICONST) {
6166 acp [tree->inst_i0->inst_c0] = i1;
6167 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
6169 if (i1->ssa_op == MONO_SSA_LOAD &&
6170 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
6171 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
6172 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
6173 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
6178 if (tree->opcode == CEE_BEQ) {
6179 g_assert (tree->inst_i0->opcode == OP_COMPARE);
6180 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
6181 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
6183 tree->opcode = CEE_BR;
6184 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
6185 tree->inst_target_bb = tree->inst_true_bb;
6187 tree->inst_target_bb = tree->inst_false_bb;
6196 mono_local_cprop (MonoCompile *cfg)
6201 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
6203 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6204 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
6205 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
6210 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, int parts)
6212 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
6213 guint8 *ip = (guint8 *)header->code;
6216 int dfn = 0, i, code_size_ratio;
6218 mono_jit_stats.methods_compiled++;
6219 if (mono_jit_profile)
6220 mono_profiler_method_jit (method);
6222 cfg = g_new0 (MonoCompile, 1);
6223 cfg->method = method;
6224 cfg->mempool = mono_mempool_new ();
6226 cfg->bb_hash = g_hash_table_new (g_direct_hash, NULL);
6227 cfg->domain = domain;
6228 cfg->verbose_level = mini_verbose;
6229 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
6230 ((MonoMethodNormal *)method)->header->max_stack);
6233 * create MonoInst* which represents arguments and local variables
6235 mono_compile_create_vars (cfg);
6237 if (cfg->verbose_level > 2)
6238 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
6240 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
6241 mono_destroy_compile (cfg);
6242 if (mono_jit_profile)
6243 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
6247 mono_jit_stats.basic_blocks += cfg->num_bblocks;
6248 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
6250 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
6252 /* Depth-first ordering on basic blocks */
6253 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
6255 if (cfg->opt & MONO_OPT_BRANCH)
6256 optimize_branches (cfg);
6258 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
6259 if (cfg->num_bblocks != dfn + 1) {
6262 cfg->num_bblocks = dfn + 1;
6264 if (!header->clauses) {
6265 /* remove unreachable code, because the code in them may be
6266 * inconsistent (access to dead variables for example) */
6267 for (bb = cfg->bb_entry; bb;) {
6268 MonoBasicBlock *bbn = bb->next_bb;
6270 if (bbn && bbn->region == -1 && !bbn->dfn) {
6271 if (cfg->verbose_level > 1)
6272 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
6273 bb->next_bb = bbn->next_bb;
6274 nullify_basic_block (bbn);
6282 if (cfg->opt & MONO_OPT_LOOP) {
6283 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
6284 mono_compute_natural_loops (cfg);
6288 /* after method_to_ir */
6292 //#define DEBUGSSA "logic_run"
6293 #define DEBUGSSA_CLASS "Tests"
6297 if (!header->num_clauses && !cfg->disable_ssa) {
6298 mono_local_cprop (cfg);
6299 mono_ssa_compute (cfg);
6303 /* fixme: add all optimizations which requires SSA */
6304 if (cfg->opt & (MONO_OPT_DEADCE)) {
6305 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
6306 mono_local_cprop (cfg);
6307 mono_ssa_compute (cfg);
6309 if (cfg->verbose_level >= 2) {
6316 /* after SSA translation */
6320 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
6321 if (cfg->comp_done & MONO_COMP_SSA) {
6322 mono_ssa_cprop (cfg);
6324 mono_local_cprop (cfg);
6328 if (cfg->comp_done & MONO_COMP_SSA) {
6329 mono_ssa_deadce (cfg);
6331 //mono_ssa_strength_reduction (cfg);
6333 mono_ssa_remove (cfg);
6335 if (cfg->opt & MONO_OPT_BRANCH)
6336 optimize_branches (cfg);
6339 /* after SSA removal */
6343 decompose_pass (cfg);
6345 /* FIXME: disabled with exception clauses: bug #42136 */
6346 if ((!header->num_clauses) && (cfg->opt & MONO_OPT_LINEARS)) {
6349 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
6350 cfg->comp_done &= ~MONO_COMP_LIVENESS;
6351 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
6352 mono_analyze_liveness (cfg);
6354 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
6355 regs = mono_arch_get_global_int_regs (cfg);
6356 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
6360 //mono_print_code (cfg);
6364 /* variables are allocated after decompose, since decompose could create temps */
6365 mono_arch_allocate_vars (cfg);
6367 if (cfg->opt & MONO_OPT_CFOLD)
6368 mono_constant_fold (cfg);
6370 mini_select_instructions (cfg);
6373 if (cfg->verbose_level >= 2) {
6374 char *id = mono_method_full_name (cfg->method, FALSE);
6375 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
6379 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
6381 jinfo = g_new0 (MonoJitInfo, 1);
6382 jinfo->method = method;
6383 jinfo->code_start = cfg->native_code;
6384 jinfo->code_size = cfg->code_len;
6385 jinfo->used_regs = cfg->used_int_regs;
6387 if (header->num_clauses) {
6390 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
6391 jinfo->num_clauses = header->num_clauses;
6392 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
6393 sizeof (MonoJitExceptionInfo) * header->num_clauses);
6395 for (i = 0; i < header->num_clauses; i++) {
6396 MonoExceptionClause *ec = &header->clauses [i];
6397 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
6398 MonoBasicBlock *tblock;
6400 ei->flags = ec->flags;
6402 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
6403 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->token_or_filter);
6405 ei->data.filter = cfg->native_code + tblock->native_offset;
6407 ei->data.token = ec->token_or_filter;
6410 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
6412 ei->try_start = cfg->native_code + tblock->native_offset;
6413 g_assert (tblock->native_offset);
6414 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
6416 ei->try_end = cfg->native_code + tblock->native_offset;
6417 g_assert (tblock->native_offset);
6418 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
6420 ei->handler_start = cfg->native_code + tblock->native_offset;
6424 mono_jit_info_table_add (cfg->domain, jinfo);
6426 /* collect statistics */
6427 mono_jit_stats.allocated_code_size += cfg->code_len;
6428 code_size_ratio = cfg->code_len;
6429 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
6430 mono_jit_stats.biggest_method_size = code_size_ratio;
6431 mono_jit_stats.biggest_method = method;
6433 code_size_ratio = (code_size_ratio * 100) / ((MonoMethodNormal *)method)->header->code_size;
6434 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
6435 mono_jit_stats.max_code_size_ratio = code_size_ratio;
6436 mono_jit_stats.max_ratio_method = method;
6438 mono_jit_stats.native_code_size += cfg->code_len;
6440 if (mono_jit_profile)
6441 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
6447 mono_jit_compile_method (MonoMethod *method)
6449 /* FIXME: later copy the code from mono */
6450 MonoDomain *target_domain, *domain = mono_domain_get ();
6452 GHashTable *jit_code_hash;
6455 if (default_opt & MONO_OPT_SHARED)
6456 target_domain = mono_root_domain;
6458 target_domain = domain;
6460 jit_code_hash = target_domain->jit_code_hash;
6462 if ((code = g_hash_table_lookup (jit_code_hash, method))) {
6463 mono_jit_stats.methods_lookups++;
6467 #ifdef MONO_USE_AOT_COMPILER
6468 if (!mono_compile_aot) {
6469 mono_class_init (method->klass);
6470 if ((code = mono_aot_get_method (method))) {
6471 g_hash_table_insert (jit_code_hash, method, code);
6477 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
6478 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
6479 if (!method->info) {
6482 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
6483 mono_lookup_pinvoke_call (method);
6484 #ifdef MONO_USE_EXC_TABLES
6485 if (mono_method_blittable (method)) {
6486 method->info = method->addr;
6489 nm = mono_marshal_get_native_wrapper (method);
6490 method->info = mono_compile_method (nm);
6492 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
6493 //mono_debug_add_wrapper (method, nm);
6494 #ifdef MONO_USE_EXC_TABLES
6498 return method->info;
6499 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
6500 const char *name = method->name;
6503 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
6504 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
6505 /* FIXME: uhm, we need a wrapper to handle exceptions? */
6506 return (gpointer)mono_delegate_ctor;
6507 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
6508 nm = mono_marshal_get_delegate_invoke (method);
6509 return mono_jit_compile_method (nm);
6510 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
6511 nm = mono_marshal_get_delegate_begin_invoke (method);
6512 return mono_jit_compile_method (nm);
6513 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
6514 nm = mono_marshal_get_delegate_end_invoke (method);
6515 return mono_jit_compile_method (nm);
6521 cfg = mini_method_compile (method, default_opt, target_domain, 0);
6522 code = cfg->native_code;
6523 mono_destroy_compile (cfg);
6525 g_hash_table_insert (jit_code_hash, method, code);
6527 if (target_domain->jump_target_hash) {
6528 MonoJumpInfo patch_info;
6530 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
6532 patch_info.next = NULL;
6533 patch_info.ip.i = 0;
6534 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
6535 patch_info.data.method = method;
6536 g_hash_table_remove (target_domain->jump_target_hash, method);
6538 for (tmp = list; tmp; tmp = tmp->next)
6539 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info);
6540 g_slist_free (list);
6542 /* make sure runtime_init is called */
6543 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
6549 * mono_jit_runtime_invoke:
6550 * @method: the method to invoke
6551 * @obj: this pointer
6552 * @params: array of parameter values.
6553 * @exc: used to catch exceptions objects
6556 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
6559 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc);
6560 invoke = mono_marshal_get_runtime_invoke (method);
6561 runtime_invoke = mono_jit_compile_method (invoke);
6562 return runtime_invoke (obj, params, exc);
6565 #ifdef PLATFORM_WIN32
6566 #define GET_CONTEXT \
6567 struct sigcontext *ctx = (struct sigcontext*)_dummy;
6569 #define GET_CONTEXT \
6570 void **_p = (void **)&_dummy; \
6571 struct sigcontext *ctx = (struct sigcontext *)++_p;
6575 sigfpe_signal_handler (int _dummy)
6580 exc = mono_get_exception_divide_by_zero ();
6582 mono_arch_handle_exception (ctx, exc, FALSE);
6586 sigill_signal_handler (int _dummy)
6590 exc = mono_get_exception_execution_engine ("SIGILL");
6592 mono_arch_handle_exception (ctx, exc, FALSE);
6596 sigsegv_signal_handler (int _dummy)
6601 exc = mono_get_exception_null_reference ();
6603 mono_arch_handle_exception (ctx, exc, FALSE);
6607 sigusr1_signal_handler (int _dummy)
6612 thread = mono_thread_current ();
6614 g_assert (thread->abort_exc);
6616 mono_arch_handle_exception (ctx, thread->abort_exc, FALSE);
6620 mono_runtime_install_handlers (void)
6622 #ifndef PLATFORM_WIN32
6623 struct sigaction sa;
6626 #ifdef PLATFORM_WIN32
6628 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
6629 win32_seh_set_handler(SIGILL, sigill_signal_handler);
6630 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
6631 #else /* !PLATFORM_WIN32 */
6633 /* libpthreads has its own implementation of sigaction(),
6634 * but it seems to work well with our current exception
6635 * handlers. If not we must call syscall directly instead
6639 sa.sa_handler = sigfpe_signal_handler;
6640 sigemptyset (&sa.sa_mask);
6642 //g_assert (syscall (SYS_sigaction, SIGFPE, &sa, NULL) != -1);
6643 g_assert (sigaction (SIGFPE, &sa, NULL) != -1);
6646 sa.sa_handler = sigill_signal_handler;
6647 sigemptyset (&sa.sa_mask);
6649 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
6650 g_assert (sigaction (SIGILL, &sa, NULL) != -1);
6652 /* catch thread abort signal */
6653 sa.sa_handler = sigusr1_signal_handler;
6654 sigemptyset (&sa.sa_mask);
6656 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
6657 g_assert (sigaction (mono_thread_get_abort_signal (), &sa, NULL) != -1);
6661 sa.sa_handler = sigsegv_signal_handler;
6662 sigemptyset (&sa.sa_mask);
6664 //g_assert (syscall (SYS_sigaction, SIGSEGV, &sa, NULL) != -1);
6665 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
6667 #endif /* PLATFORM_WIN32 */
6670 /* mono_jit_create_remoting_trampoline:
6671 * @method: pointer to the method info
6673 * Creates a trampoline which calls the remoting functions. This
6674 * is used in the vtable of transparent proxies.
6676 * Returns: a pointer to the newly created code
6679 mono_jit_create_remoting_trampoline (MonoMethod *method)
6682 guint8 *addr = NULL;
6684 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
6685 (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
6686 nm = mono_marshal_get_remoting_invoke (method);
6687 addr = mono_compile_method (nm);
6689 addr = mono_compile_method (method);
6694 static CRITICAL_SECTION ms;
6697 mini_init (const char *filename)
6701 metadata_section = &ms;
6702 InitializeCriticalSection (metadata_section);
6704 mono_jit_tls_id = TlsAlloc ();
6705 mono_thread_start_cb (GetCurrentThreadId (), (gpointer)-1, NULL);
6709 mono_runtime_install_handlers ();
6711 mono_install_compile_method (mono_jit_compile_method);
6712 mono_install_trampoline (mono_arch_create_jit_trampoline);
6713 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
6714 mono_install_runtime_invoke (mono_jit_runtime_invoke);
6715 mono_install_handler (mono_arch_get_throw_exception ());
6716 mono_install_stack_walk (mono_jit_walk_stack);
6717 mono_install_get_config_dir ();
6719 domain = mono_init (filename);
6722 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
6723 ves_icall_get_frame_info);
6724 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
6725 ves_icall_get_trace);
6726 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
6727 mono_runtime_install_handlers);
6730 create_helper_signature ();
6732 mono_arch_register_lowlevel_calls ();
6733 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
6734 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
6736 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
6737 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
6739 /* fixme: we cant hanlde vararg methods this way, because the signature is not constant */
6740 //mono_register_jit_icall (ves_array_element_address, "ves_array_element_address", NULL);
6741 //mono_register_jit_icall (mono_array_new_va, "mono_array_new_va", NULL);
6743 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
6744 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
6745 helper_sig_void_ptr, TRUE);
6748 * NOTE, NOTE, NOTE, NOTE:
6749 * when adding emulation for some opcodes, remember to also add a dummy
6750 * rule to the burg files, because we need the arity information to be correct.
6752 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult);
6753 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un);
6754 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf);
6755 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv);
6756 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un);
6757 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem);
6758 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un);
6760 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl);
6761 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr);
6762 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un);
6764 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8);
6765 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4);
6766 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8);
6767 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8);
6769 #if SIZEOF_VOID_P == 4
6770 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4);
6772 #warning "fixme: add opcode emulation"
6775 /* other jit icalls */
6776 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
6777 helper_sig_ptr_ptr_ptr, FALSE);
6778 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
6779 mono_register_jit_icall (mono_threads_get_static_data, "mono_threads_get_static_data", helper_sig_ptr_int, FALSE);
6780 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
6781 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
6782 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
6783 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
6784 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
6785 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
6786 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
6787 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
6788 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
6789 mono_register_jit_icall (mono_string_to_utf16, "mono_string_to_utf16", helper_sig_ptr_obj, FALSE);
6790 mono_register_jit_icall (mono_string_from_utf16, "mono_string_from_utf16", helper_sig_obj_ptr, FALSE);
6791 mono_register_jit_icall (mono_string_new_wrapper, "mono_string_new_wrapper", helper_sig_obj_ptr, FALSE);
6792 mono_register_jit_icall (mono_string_to_utf8, "mono_string_to_utf8", helper_sig_ptr_obj, FALSE);
6793 mono_register_jit_icall (mono_string_to_bstr, "mono_string_to_bstr", helper_sig_ptr_obj, FALSE);
6794 mono_register_jit_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", helper_sig_ptr_obj, FALSE);
6795 mono_register_jit_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", helper_sig_ptr_obj, FALSE);
6796 mono_register_jit_icall (mono_array_to_savearray, "mono_array_to_savearray", helper_sig_ptr_obj, FALSE);
6797 mono_register_jit_icall (mono_array_to_lparray, "mono_array_to_lparray", helper_sig_ptr_obj, FALSE);
6798 mono_register_jit_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", helper_sig_ptr_obj, FALSE);
6799 mono_register_jit_icall (mono_marshal_string_array, "mono_marshal_string_array", helper_sig_ptr_obj, FALSE);
6800 mono_register_jit_icall (mono_string_utf8_to_builder, "mono_string_utf8_to_builder", helper_sig_void_ptr_ptr, FALSE);
6801 mono_register_jit_icall (mono_marshal_free_array, "mono_marshal_free_array", helper_sig_void_ptr_ptr, FALSE);
6802 mono_register_jit_icall (mono_string_to_byvalstr, "mono_string_to_byvalstr", helper_sig_void_ptr_ptr_ptr, FALSE);
6803 mono_register_jit_icall (mono_string_to_byvalwstr, "mono_string_to_byvalwstr", helper_sig_void_ptr_ptr_ptr, FALSE);
6804 mono_register_jit_icall (g_free, "g_free", helper_sig_void_ptr, FALSE);
6805 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
6806 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
6807 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
6809 mono_runtime_init (domain, mono_thread_start_cb,
6810 mono_thread_attach_cb);
6812 //mono_thread_attach (domain);
6816 MonoJitStats mono_jit_stats = {0};
6819 print_jit_stats (void)
6821 if (mono_jit_stats.enabled) {
6822 g_print ("Mono Jit statistics\n");
6823 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
6824 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
6825 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
6826 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
6827 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
6828 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
6829 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
6830 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
6831 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
6832 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
6833 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
6834 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
6835 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
6836 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
6837 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
6838 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
6839 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
6840 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
6842 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
6843 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
6844 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
6845 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
6846 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
6851 mini_cleanup (MonoDomain *domain)
6854 * mono_runtime_cleanup() and mono_domain_finalize () need to
6855 * be called early since they need the execution engine still
6856 * fully working (mono_domain_finalize may invoke managed finalizers
6857 * and mono_runtime_cleanup will wait for other threads to finish).
6859 mono_domain_finalize (domain);
6861 mono_runtime_cleanup (domain);
6863 mono_profiler_shutdown ();
6865 mono_debug_cleanup ();
6866 #ifdef PLATFORM_WIN32
6867 win32_seh_cleanup();
6870 mono_domain_unload (domain, TRUE);
6873 DeleteCriticalSection (metadata_section);
6877 mono_set_defaults (int verbose_level, guint32 opts)
6879 mini_verbose = verbose_level;