2 * mini.c: The new Mono code generator.
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2002 Ximian, Inc.
15 #include <mono/metadata/assembly.h>
16 #include <mono/metadata/loader.h>
17 #include <mono/metadata/cil-coff.h>
18 #include <mono/metadata/tabledefs.h>
19 #include <mono/metadata/class.h>
20 #include <mono/metadata/object.h>
21 #include <mono/metadata/exception.h>
22 #include <mono/metadata/opcodes.h>
23 #include <mono/metadata/mono-endian.h>
24 #include <mono/metadata/tokentype.h>
25 #include <mono/metadata/tabledefs.h>
26 #include <mono/metadata/threads.h>
27 #include <mono/metadata/marshal.h>
28 #include <mono/metadata/socket-io.h>
29 #include <mono/metadata/appdomain.h>
30 #include <mono/metadata/debug-helpers.h>
31 #include <mono/io-layer/io-layer.h>
32 #include "mono/metadata/profiler.h"
33 #include <mono/metadata/profiler-private.h>
34 #include <mono/metadata/mono-config.h>
35 #include <mono/metadata/environment.h>
36 #include <mono/metadata/mono-debug.h>
37 #include <mono/metadata/mono-debug-debugger.h>
44 #include "jit-icalls.c"
46 #define MONO_IS_COND_BRANCH(op) ((op >= CEE_BEQ && op <= CEE_BLT_UN) || (op >= OP_LBEQ && op <= OP_LBLT_UN) || (op >= OP_FBEQ && op <= OP_FBLT_UN))
48 #define MONO_CHECK_THIS(ins) (cfg->method->signature->hasthis && (ins)->ssa_op == MONO_SSA_LOAD && (ins)->inst_left->inst_c0 == 0)
50 gboolean mono_arch_handle_exception (struct sigcontext *ctx, gpointer obj, gboolean test_only);
51 static gpointer mono_jit_compile_method (MonoMethod *method);
53 static void handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src,
54 const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native);
56 static int mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
57 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
58 guint inline_offset, gboolean is_virtual_call);
60 extern guint8 mono_burg_arity [];
61 /* helper methods signature */
62 static MonoMethodSignature *helper_sig_long_long_long = NULL;
63 static MonoMethodSignature *helper_sig_long_long_int = NULL;
64 static MonoMethodSignature *helper_sig_newarr = NULL;
65 static MonoMethodSignature *helper_sig_newarr_specific = NULL;
66 static MonoMethodSignature *helper_sig_ldstr = NULL;
67 static MonoMethodSignature *helper_sig_domain_get = NULL;
68 static MonoMethodSignature *helper_sig_object_new = NULL;
69 static MonoMethodSignature *helper_sig_object_new_specific = NULL;
70 static MonoMethodSignature *helper_sig_compile = NULL;
71 static MonoMethodSignature *helper_sig_compile_virt = NULL;
72 static MonoMethodSignature *helper_sig_obj_ptr = NULL;
73 static MonoMethodSignature *helper_sig_ptr_void = NULL;
74 static MonoMethodSignature *helper_sig_void_ptr = NULL;
75 static MonoMethodSignature *helper_sig_void_obj = NULL;
76 static MonoMethodSignature *helper_sig_void_ptr_ptr = NULL;
77 static MonoMethodSignature *helper_sig_void_ptr_ptr_ptr = NULL;
78 static MonoMethodSignature *helper_sig_ptr_ptr_ptr = NULL;
79 static MonoMethodSignature *helper_sig_ptr_obj = NULL;
80 static MonoMethodSignature *helper_sig_ptr_int = NULL;
81 static MonoMethodSignature *helper_sig_initobj = NULL;
82 static MonoMethodSignature *helper_sig_memcpy = NULL;
83 static MonoMethodSignature *helper_sig_memset = NULL;
84 static MonoMethodSignature *helper_sig_ulong_double = NULL;
85 static MonoMethodSignature *helper_sig_long_double = NULL;
86 static MonoMethodSignature *helper_sig_uint_double = NULL;
87 static MonoMethodSignature *helper_sig_int_double = NULL;
88 static MonoMethodSignature *helper_sig_stelem_ref = NULL;
90 static guint32 default_opt = MONO_OPT_PEEPHOLE;
92 guint32 mono_jit_tls_id = 0;
93 gboolean mono_jit_trace_calls = FALSE;
94 gboolean mono_break_on_exc = FALSE;
95 gboolean mono_compile_aot = FALSE;
97 CRITICAL_SECTION *metadata_section = NULL;
99 static int mini_verbose = 0;
101 #ifdef MONO_USE_EXC_TABLES
103 mono_type_blittable (MonoType *type)
122 case MONO_TYPE_OBJECT:
124 case MONO_TYPE_VALUETYPE:
125 case MONO_TYPE_CLASS:
126 return type->data.klass->blittable;
136 mono_method_blittable (MonoMethod *method)
138 MonoMethodSignature *sig;
144 if (!mono_arch_has_unwind_info (method->addr)) {
148 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
151 sig = method->signature;
153 if (!mono_type_blittable (sig->ret))
156 for (i = 0; i < sig->param_count; i++)
157 if (!mono_type_blittable (sig->params [i]))
165 G_GNUC_UNUSED static void
166 print_method_from_ip (void *ip)
171 MonoDomain *domain = mono_domain_get ();
173 ji = mono_jit_info_table_find (domain, ip);
175 g_print ("No method at %p\n", ip);
178 method = mono_method_full_name (ji->method, TRUE);
179 source = mono_debug_source_location_from_address (ji->method, (int) ip, NULL, domain);
181 g_print ("IP %p at offset 0x%x of method %s (%p %p)\n", ip, (char*)ip - (char*)ji->code_start, method, ji->code_start, (char*)ji->code_start + ji->code_size);
184 g_print ("%s\n", source);
191 #define MONO_INIT_VARINFO(vi,id) do { \
192 (vi)->range.first_use.pos.bid = 0xffff; \
198 * Basic blocks have two numeric identifiers:
199 * dfn: Depth First Number
200 * block_num: unique ID assigned at bblock creation
202 #define NEW_BBLOCK(cfg) (mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)))
203 #define ADD_BBLOCK(cfg,bbhash,b) do { \
204 g_hash_table_insert (bbhash, (b)->cil_code, (b)); \
205 (b)->block_num = cfg->num_bblocks++; \
206 (b)->real_offset = real_offset; \
209 #define GET_BBLOCK(cfg,bbhash,tblock,ip) do { \
210 (tblock) = g_hash_table_lookup (bbhash, (ip)); \
212 if ((ip) >= end || (ip) < header->code) goto unverified; \
213 (tblock) = NEW_BBLOCK (cfg); \
214 (tblock)->cil_code = (ip); \
215 ADD_BBLOCK (cfg, (bbhash), (tblock)); \
217 (tblock)->real_offset = real_offset; \
220 #define CHECK_BBLOCK(target,ip,tblock) do { \
221 if ((target) < (ip) && !(tblock)->code) { \
222 bb_recheck = g_list_prepend (bb_recheck, (tblock)); \
223 if (cfg->verbose_level > 2) g_print ("queued block %d for check at IL%04x from IL%04x\n", (tblock)->block_num, (target) - header->code, (ip) - header->code); \
227 #define NEW_ICONST(cfg,dest,val) do { \
228 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
229 (dest)->opcode = OP_ICONST; \
230 (dest)->inst_c0 = (val); \
231 (dest)->type = STACK_I4; \
234 /* FIXME: have a different definition of NEW_PCONST for 64 bit systems */
235 #define NEW_PCONST(cfg,dest,val) do { \
236 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
237 (dest)->opcode = OP_ICONST; \
238 (dest)->inst_p0 = (val); \
239 (dest)->type = STACK_PTR; \
242 #define NEW_CLASSCONST(cfg,dest,val) do { \
243 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
244 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
245 (dest)->inst_p0 = (val); \
246 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_CLASS; \
247 (dest)->type = STACK_PTR; \
250 #define NEW_IMAGECONST(cfg,dest,val) do { \
251 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
252 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
253 (dest)->inst_p0 = (val); \
254 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_IMAGE; \
255 (dest)->type = STACK_PTR; \
258 #define NEW_FIELDCONST(cfg,dest,field) do { \
259 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
260 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
261 (dest)->inst_p0 = (field); \
262 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_FIELD; \
263 (dest)->type = STACK_PTR; \
266 #define NEW_METHODCONST(cfg,dest,val) do { \
267 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
268 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
269 (dest)->inst_p0 = (val); \
270 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_METHODCONST; \
271 (dest)->type = STACK_PTR; \
274 #define NEW_DOMAINCONST(cfg,dest) do { \
275 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) { \
276 NEW_TEMPLOAD (cfg, dest, mono_get_domainvar (cfg)->inst_c0); \
278 NEW_PCONST (cfg, dest, (cfg)->domain); \
282 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
284 #define NEW_ARGLOAD(cfg,dest,num) do { \
285 if (arg_array [(num)]->opcode == OP_ICONST) (dest) = arg_array [(num)]; else { \
286 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
287 (dest)->ssa_op = MONO_SSA_LOAD; \
288 (dest)->inst_i0 = arg_array [(num)]; \
289 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
290 type_to_eval_stack_type (param_types [(num)], (dest)); \
291 (dest)->klass = (dest)->inst_i0->klass; \
294 #define NEW_LOCLOAD(cfg,dest,num) do { \
295 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
296 (dest)->ssa_op = MONO_SSA_LOAD; \
297 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
298 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
299 type_to_eval_stack_type (header->locals [(num)], (dest)); \
300 (dest)->klass = (dest)->inst_i0->klass; \
303 #define NEW_LOCLOADA(cfg,dest,num) do { \
304 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
305 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
306 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
307 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
308 (dest)->opcode = OP_LDADDR; \
309 (dest)->type = STACK_MP; \
310 (dest)->klass = (dest)->inst_i0->klass; \
311 (cfg)->disable_ssa = TRUE; \
314 #define NEW_RETLOADA(cfg,dest) do { \
315 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
316 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
317 (dest)->inst_i0 = (cfg)->ret; \
318 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
319 (dest)->opcode = CEE_LDIND_I; \
320 (dest)->type = STACK_MP; \
321 (dest)->klass = (dest)->inst_i0->klass; \
322 (cfg)->disable_ssa = TRUE; \
325 #define NEW_ARGLOADA(cfg,dest,num) do { \
326 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
327 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
328 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
329 (dest)->inst_i0 = arg_array [(num)]; \
330 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
331 (dest)->opcode = OP_LDADDR; \
332 (dest)->type = STACK_MP; \
333 (dest)->klass = (dest)->inst_i0->klass; \
334 (cfg)->disable_ssa = TRUE; \
337 #define NEW_TEMPLOAD(cfg,dest,num) do { \
338 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
339 (dest)->ssa_op = MONO_SSA_LOAD; \
340 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
341 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
342 type_to_eval_stack_type ((dest)->inst_i0->inst_vtype, (dest)); \
343 (dest)->klass = (dest)->inst_i0->klass; \
346 #define NEW_TEMPLOADA(cfg,dest,num) do { \
347 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
348 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
349 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
350 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
351 (dest)->opcode = OP_LDADDR; \
352 (dest)->type = STACK_MP; \
353 (dest)->klass = (dest)->inst_i0->klass; \
354 (cfg)->disable_ssa = TRUE; \
358 #define NEW_INDLOAD(cfg,dest,addr,vtype) do { \
359 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
360 (dest)->inst_left = addr; \
361 (dest)->opcode = mono_type_to_ldind (vtype); \
362 type_to_eval_stack_type (vtype, (dest)); \
363 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
366 #define NEW_INDSTORE(cfg,dest,addr,value,vtype) do { \
367 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
368 (dest)->inst_i0 = addr; \
369 (dest)->opcode = mono_type_to_stind (vtype); \
370 (dest)->inst_i1 = (value); \
371 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
374 #define NEW_TEMPSTORE(cfg,dest,num,inst) do { \
375 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
376 (dest)->ssa_op = MONO_SSA_STORE; \
377 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
378 (dest)->opcode = mono_type_to_stind ((dest)->inst_i0->inst_vtype); \
379 (dest)->inst_i1 = (inst); \
380 (dest)->klass = (dest)->inst_i0->klass; \
383 #define NEW_LOCSTORE(cfg,dest,num,inst) do { \
384 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
385 (dest)->opcode = mono_type_to_stind (header->locals [(num)]); \
386 (dest)->ssa_op = MONO_SSA_STORE; \
387 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
388 (dest)->inst_i1 = (inst); \
389 (dest)->klass = (dest)->inst_i0->klass; \
392 #define NEW_ARGSTORE(cfg,dest,num,inst) do { \
393 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
394 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
395 (dest)->opcode = mono_type_to_stind (param_types [(num)]); \
396 (dest)->ssa_op = MONO_SSA_STORE; \
397 (dest)->inst_i0 = arg_array [(num)]; \
398 (dest)->inst_i1 = (inst); \
399 (dest)->klass = (dest)->inst_i0->klass; \
402 #define ADD_BINOP(op) do { \
403 MONO_INST_NEW (cfg, ins, (op)); \
404 ins->cil_code = ip; \
406 ins->inst_i0 = sp [0]; \
407 ins->inst_i1 = sp [1]; \
409 type_from_op (ins); \
413 #define ADD_UNOP(op) do { \
414 MONO_INST_NEW (cfg, ins, (op)); \
415 ins->cil_code = ip; \
417 ins->inst_i0 = sp [0]; \
419 type_from_op (ins); \
423 #define ADD_BINCOND(next_block) do { \
425 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
427 cmp->inst_i0 = sp [0]; \
428 cmp->inst_i1 = sp [1]; \
429 cmp->cil_code = ins->cil_code; \
430 type_from_op (cmp); \
432 ins->inst_i0 = cmp; \
433 MONO_ADD_INS (bblock, ins); \
434 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
435 GET_BBLOCK (cfg, bbhash, tblock, target); \
436 link_bblock (cfg, bblock, tblock); \
437 ins->inst_true_bb = tblock; \
438 CHECK_BBLOCK (target, ip, tblock); \
439 if ((next_block)) { \
440 link_bblock (cfg, bblock, (next_block)); \
441 ins->inst_false_bb = (next_block); \
442 start_new_bblock = 1; \
444 GET_BBLOCK (cfg, bbhash, tblock, ip); \
445 link_bblock (cfg, bblock, tblock); \
446 ins->inst_false_bb = tblock; \
447 start_new_bblock = 2; \
451 /* FIXME: handle float, long ... */
452 #define ADD_UNCOND(istrue) do { \
454 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
456 cmp->inst_i0 = sp [0]; \
457 switch (cmp->inst_i0->type) { \
459 cmp->inst_i1 = zero_int64; break; \
461 cmp->inst_i1 = zero_r8; break; \
464 cmp->inst_i1 = zero_ptr; break; \
466 cmp->inst_i1 = zero_obj; break; \
468 cmp->inst_i1 = zero_int32; \
470 cmp->cil_code = ins->cil_code; \
471 type_from_op (cmp); \
473 ins->inst_i0 = cmp; \
474 ins->opcode = (istrue)? CEE_BNE_UN: CEE_BEQ; \
475 MONO_ADD_INS (bblock, ins); \
476 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
477 GET_BBLOCK (cfg, bbhash, tblock, target); \
478 link_bblock (cfg, bblock, tblock); \
479 ins->inst_true_bb = tblock; \
480 CHECK_BBLOCK (target, ip, tblock); \
481 GET_BBLOCK (cfg, bbhash, tblock, ip); \
482 link_bblock (cfg, bblock, tblock); \
483 ins->inst_false_bb = tblock; \
484 start_new_bblock = 2; \
487 #define NEW_LDELEMA(cfg,dest,sp,k) do { \
488 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
489 (dest)->opcode = CEE_LDELEMA; \
490 (dest)->inst_left = (sp) [0]; \
491 (dest)->inst_right = (sp) [1]; \
492 (dest)->type = STACK_MP; \
493 (dest)->klass = (k); \
496 #define NEW_GROUP(cfg,dest,el1,el2) do { \
497 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
498 (dest)->opcode = OP_GROUP; \
499 (dest)->inst_left = (el1); \
500 (dest)->inst_right = (el2); \
505 compare_bblock (gconstpointer a, gconstpointer b)
507 const MonoBasicBlock *b1 = a;
508 const MonoBasicBlock *b2 = b;
510 return b2->cil_code - b1->cil_code;
515 * link_bblock: Links two basic blocks
517 * links two basic blocks in the control flow graph, the 'from'
518 * argument is the starting block and the 'to' argument is the block
519 * the control flow ends to after 'from'.
522 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
524 MonoBasicBlock **newa;
528 if (from->cil_code) {
530 g_print ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
532 g_print ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
535 g_print ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
537 g_print ("edge from entry to exit\n");
541 for (i = 0; i < from->out_count; ++i) {
542 if (to == from->out_bb [i]) {
548 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
549 for (i = 0; i < from->out_count; ++i) {
550 newa [i] = from->out_bb [i];
558 for (i = 0; i < to->in_count; ++i) {
559 if (from == to->in_bb [i]) {
565 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
566 for (i = 0; i < to->in_count; ++i) {
567 newa [i] = to->in_bb [i];
576 * We mark each basic block with a region ID. We use that to avoid BB
577 * optimizations when blocks are in different regions.
580 mono_find_block_region (MonoCompile *cfg, int offset, int *filter_lengths)
582 MonoMethod *method = cfg->method;
583 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
584 MonoExceptionClause *clause;
587 /* first search for handlers and filters */
588 for (i = 0; i < header->num_clauses; ++i) {
589 clause = &header->clauses [i];
590 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->token_or_filter) &&
591 (offset < (clause->token_or_filter + filter_lengths [i])))
592 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
594 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
595 if (clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)
596 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
598 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
602 /* search the try blocks */
603 for (i = 0; i < header->num_clauses; ++i) {
604 clause = &header->clauses [i];
605 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
606 return ((i + 1) << 8) | clause->flags;
613 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
615 MonoMethod *method = cfg->method;
616 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
617 MonoExceptionClause *clause;
618 MonoBasicBlock *handler;
622 for (i = 0; i < header->num_clauses; ++i) {
623 clause = &header->clauses [i];
624 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
625 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
626 if (clause->flags == type) {
627 handler = g_hash_table_lookup (cfg->bb_hash, header->code + clause->handler_offset);
629 res = g_list_append (res, handler);
638 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
642 array [*dfn] = start;
643 /*g_print ("visit %d at %p\n", *dfn, start->cil_code);*/
644 for (i = 0; i < start->out_count; ++i) {
645 if (start->out_bb [i]->dfn)
648 start->out_bb [i]->dfn = *dfn;
649 start->out_bb [i]->df_parent = start;
650 array [*dfn] = start->out_bb [i];
651 df_visit (start->out_bb [i], dfn, array);
657 MonoBasicBlock *best;
661 previous_foreach (gconstpointer key, gpointer val, gpointer data)
663 PrevStruct *p = data;
664 MonoBasicBlock *bb = val;
665 //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,
666 //bb->method == p->best->method, bb->cil_code < p->code, bb->cil_code > p->best->cil_code);
668 if (bb->cil_code && bb->cil_code < p->code && bb->cil_code > p->best->cil_code)
672 static MonoBasicBlock*
673 find_previous (GHashTable *bb_hash, MonoBasicBlock *start, const guchar *code) {
679 g_hash_table_foreach (bb_hash, (GHFunc)previous_foreach, &p);
684 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
693 * FIXME: take into account all the details:
694 * second may have been the target of more than one bblock
696 second->out_count = first->out_count;
697 second->out_bb = first->out_bb;
699 for (i = 0; i < first->out_count; ++i) {
700 bb = first->out_bb [i];
701 for (j = 0; j < bb->in_count; ++j) {
702 if (bb->in_bb [j] == first)
703 bb->in_bb [j] = second;
707 first->out_count = 0;
708 first->out_bb = NULL;
709 link_bblock (cfg, first, second);
711 second->last_ins = first->last_ins;
713 /*g_print ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
714 for (inst = first->code; inst && inst->next; inst = inst->next) {
715 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
716 g_print ("found %p: %s", inst->next->cil_code, code);
718 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
719 second->code = inst->next;
721 first->last_ins = inst;
722 second->next_bb = first->next_bb;
723 first->next_bb = second;
728 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
734 mono_type_to_ldind (MonoType *type)
746 case MONO_TYPE_BOOLEAN:
760 case MONO_TYPE_FNPTR:
762 case MONO_TYPE_CLASS:
763 case MONO_TYPE_STRING:
764 case MONO_TYPE_OBJECT:
765 case MONO_TYPE_SZARRAY:
766 case MONO_TYPE_ARRAY:
767 return CEE_LDIND_REF;
775 case MONO_TYPE_VALUETYPE:
776 if (type->data.klass->enumtype) {
777 t = type->data.klass->enum_basetype->type;
781 case MONO_TYPE_TYPEDBYREF:
784 g_error ("unknown type 0x%02x in type_to_ldind", type->type);
790 mono_type_to_stind (MonoType *type)
801 case MONO_TYPE_BOOLEAN:
813 case MONO_TYPE_FNPTR:
815 case MONO_TYPE_CLASS:
816 case MONO_TYPE_STRING:
817 case MONO_TYPE_OBJECT:
818 case MONO_TYPE_SZARRAY:
819 case MONO_TYPE_ARRAY:
820 return CEE_STIND_REF;
828 case MONO_TYPE_VALUETYPE:
829 if (type->data.klass->enumtype) {
830 t = type->data.klass->enum_basetype->type;
834 case MONO_TYPE_TYPEDBYREF:
837 g_error ("unknown type 0x%02x in type_to_stind", type->type);
843 * Returns the type used in the eval stack when @type is loaded.
844 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
847 type_to_eval_stack_type (MonoType *type, MonoInst *inst) {
851 inst->type = STACK_MP;
859 case MONO_TYPE_BOOLEAN:
865 inst->type = STACK_I4;
870 case MONO_TYPE_FNPTR:
871 inst->type = STACK_PTR;
873 case MONO_TYPE_CLASS:
874 case MONO_TYPE_STRING:
875 case MONO_TYPE_OBJECT:
876 case MONO_TYPE_SZARRAY:
877 case MONO_TYPE_ARRAY:
878 inst->type = STACK_OBJ;
882 inst->type = STACK_I8;
886 inst->type = STACK_R8;
888 case MONO_TYPE_VALUETYPE:
889 if (type->data.klass->enumtype) {
890 t = type->data.klass->enum_basetype->type;
893 inst->klass = type->data.klass;
894 inst->type = STACK_VTYPE;
897 case MONO_TYPE_TYPEDBYREF:
898 inst->klass = mono_defaults.typed_reference_class;
899 inst->type = STACK_VTYPE;
902 g_error ("unknown type 0x%02x in eval stack type", type->type);
907 * The following tables are used to quickly validate the IL code in type_from_op ().
910 bin_num_table [STACK_MAX] [STACK_MAX] = {
911 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
912 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
913 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
914 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
915 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
916 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
917 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
918 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
923 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
926 /* reduce the size of this table */
928 bin_int_table [STACK_MAX] [STACK_MAX] = {
929 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
930 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
931 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
932 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
933 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
934 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
935 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
936 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
940 bin_comp_table [STACK_MAX] [STACK_MAX] = {
942 {0, 1, 0, 1, 0, 0, 4, 0},
943 {0, 0, 1, 0, 0, 0, 0, 0},
944 {0, 1, 0, 1, 0, 2, 4, 0},
945 {0, 0, 0, 0, 1, 0, 0, 0},
946 {0, 0, 0, 2, 0, 1, 0, 0},
947 {0, 4, 0, 4, 0, 0, 3, 0},
948 {0, 0, 0, 0, 0, 0, 0, 0},
951 /* reduce the size of this table */
953 shift_table [STACK_MAX] [STACK_MAX] = {
954 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
955 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
956 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
957 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
958 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
959 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
960 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
961 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
965 * Tables to map from the non-specific opcode to the matching
966 * type-specific opcode.
968 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
970 binops_op_map [STACK_MAX] = {
971 0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, 0
974 /* handles from CEE_NEG to CEE_CONV_U8 */
976 unops_op_map [STACK_MAX] = {
977 0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, 0
980 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
982 ovfops_op_map [STACK_MAX] = {
983 0, 0, OP_LCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_FCONV_TO_U2-CEE_CONV_U2, 0
986 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
988 ovf2ops_op_map [STACK_MAX] = {
989 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
992 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
994 ovf3ops_op_map [STACK_MAX] = {
995 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
998 /* handles from CEE_CEQ to CEE_CLT_UN */
1000 ceqops_op_map [STACK_MAX] = {
1001 0, 0, OP_LCEQ-CEE_CEQ, OP_PCEQ-CEE_CEQ, OP_FCEQ-CEE_CEQ, 0
1005 * Sets ins->type (the type on the eval stack) according to the
1006 * type of the opcode and the arguments to it.
1007 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1009 * FIXME: this function sets ins->type unconditionally in some cases, but
1010 * it should set it to invalid for some types (a conv.x on an object)
1013 type_from_op (MonoInst *ins) {
1014 switch (ins->opcode) {
1021 /* FIXME: check unverifiable args for STACK_MP */
1022 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1023 ins->opcode += binops_op_map [ins->type];
1030 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1031 ins->opcode += binops_op_map [ins->type];
1036 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1037 ins->opcode += binops_op_map [ins->type];
1040 /* FIXME: handle some specifics with ins->next->type */
1041 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1045 case 256+CEE_CGT_UN:
1047 case 256+CEE_CLT_UN:
1048 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1049 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1053 ins->type = neg_table [ins->inst_i0->type];
1054 ins->opcode += unops_op_map [ins->type];
1057 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1058 ins->type = ins->inst_i0->type;
1060 ins->type = STACK_INV;
1061 ins->opcode += unops_op_map [ins->type];
1067 ins->type = STACK_I4;
1068 ins->opcode += unops_op_map [ins->inst_i0->type];
1071 ins->type = STACK_R8;
1072 switch (ins->inst_i0->type) {
1077 ins->opcode = OP_LCONV_TO_R_UN;
1081 case CEE_CONV_OVF_I1:
1082 case CEE_CONV_OVF_U1:
1083 case CEE_CONV_OVF_I2:
1084 case CEE_CONV_OVF_U2:
1085 case CEE_CONV_OVF_I4:
1086 case CEE_CONV_OVF_U4:
1087 ins->type = STACK_I4;
1088 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1090 case CEE_CONV_OVF_I_UN:
1091 case CEE_CONV_OVF_U_UN:
1092 ins->type = STACK_PTR;
1093 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1095 case CEE_CONV_OVF_I1_UN:
1096 case CEE_CONV_OVF_I2_UN:
1097 case CEE_CONV_OVF_I4_UN:
1098 case CEE_CONV_OVF_U1_UN:
1099 case CEE_CONV_OVF_U2_UN:
1100 case CEE_CONV_OVF_U4_UN:
1101 ins->type = STACK_I4;
1102 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1105 ins->type = STACK_PTR;
1106 switch (ins->inst_i0->type) {
1112 ins->opcode = OP_LCONV_TO_U;
1115 ins->opcode = OP_FCONV_TO_U;
1121 ins->type = STACK_I8;
1122 ins->opcode += unops_op_map [ins->inst_i0->type];
1124 case CEE_CONV_OVF_I8:
1125 case CEE_CONV_OVF_U8:
1126 ins->type = STACK_I8;
1127 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1129 case CEE_CONV_OVF_U8_UN:
1130 case CEE_CONV_OVF_I8_UN:
1131 ins->type = STACK_I8;
1132 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1136 ins->type = STACK_R8;
1137 ins->opcode += unops_op_map [ins->inst_i0->type];
1140 ins->type = STACK_R8;
1144 ins->type = STACK_I4;
1145 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1148 case CEE_CONV_OVF_I:
1149 case CEE_CONV_OVF_U:
1150 ins->type = STACK_PTR;
1151 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1154 case CEE_ADD_OVF_UN:
1156 case CEE_MUL_OVF_UN:
1158 case CEE_SUB_OVF_UN:
1159 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1160 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1163 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1170 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_MP, STACK_R8, STACK_R8, STACK_OBJ
1173 /* map ldelem.x to the matching ldind.x opcode */
1175 ldelem_to_ldind [] = {
1189 /* map stelem.x to the matching stind.x opcode */
1191 stelem_to_stind [] = {
1205 param_table [STACK_MAX] [STACK_MAX] = {
1210 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1214 switch (args->type) {
1224 for (i = 0; i < sig->param_count; ++i) {
1225 switch (args [i].type) {
1229 if (!sig->params [i]->byref)
1233 if (sig->params [i]->byref)
1235 switch (sig->params [i]->type) {
1236 case MONO_TYPE_CLASS:
1237 case MONO_TYPE_STRING:
1238 case MONO_TYPE_OBJECT:
1239 case MONO_TYPE_SZARRAY:
1240 case MONO_TYPE_ARRAY:
1247 if (sig->params [i]->byref)
1249 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1258 /*if (!param_table [args [i].type] [sig->params [i]->type])
1266 * When we need a pointer to the current domain many times in a method, we
1267 * call mono_domain_get() once and we store the result in a local variable.
1268 * This function returns the variable that represents the MonoDomain*.
1270 inline static MonoInst *
1271 mono_get_domainvar (MonoCompile *cfg)
1273 if (!cfg->domainvar)
1274 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1275 return cfg->domainvar;
1279 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
1282 int num = cfg->num_varinfo;
1284 if ((num + 1) >= cfg->varinfo_count) {
1285 cfg->varinfo_count = (cfg->varinfo_count + 2) * 2;
1286 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
1287 cfg->vars = (MonoMethodVar **)g_realloc (cfg->vars, sizeof (MonoMethodVar*) * cfg->varinfo_count);
1290 mono_jit_stats.allocate_var++;
1292 MONO_INST_NEW (cfg, inst, opcode);
1293 inst->inst_c0 = num;
1294 inst->inst_vtype = type;
1295 inst->klass = mono_class_from_mono_type (type);
1296 /* if set to 1 the variable is native */
1299 cfg->varinfo [num] = inst;
1301 cfg->vars [num] = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMethodVar));
1302 MONO_INIT_VARINFO (cfg->vars [num], num);
1305 //g_print ("created temp %d of type %s\n", num, mono_type_get_name (type));
1310 type_from_stack_type (MonoInst *ins) {
1311 switch (ins->type) {
1312 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1313 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1314 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1315 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1316 case STACK_MP: return &mono_defaults.int_class->byval_arg;
1317 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1318 case STACK_VTYPE: return &ins->klass->byval_arg;
1320 g_error ("stack type %d to montype not handled\n", ins->type);
1326 array_access_to_klass (int opcode)
1330 return mono_defaults.byte_class;
1332 return mono_defaults.uint16_class;
1335 return mono_defaults.int_class;
1338 return mono_defaults.sbyte_class;
1341 return mono_defaults.int16_class;
1344 return mono_defaults.int32_class;
1346 return mono_defaults.uint32_class;
1349 return mono_defaults.int64_class;
1352 return mono_defaults.single_class;
1355 return mono_defaults.double_class;
1356 case CEE_LDELEM_REF:
1357 case CEE_STELEM_REF:
1358 return mono_defaults.object_class;
1360 g_assert_not_reached ();
1366 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
1370 MONO_ADD_INS (bb, inst);
1373 switch (bb->last_ins->opcode) {
1386 while (prev->next && prev->next != bb->last_ins)
1388 if (prev == bb->code) {
1389 if (bb->last_ins == bb->code) {
1390 inst->next = bb->code;
1393 inst->next = prev->next;
1397 inst->next = bb->last_ins;
1401 // g_warning ("handle conditional jump in add_ins_to_end ()\n");
1403 MONO_ADD_INS (bb, inst);
1409 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
1411 MonoInst *inst, *load;
1413 NEW_TEMPLOAD (cfg, load, src);
1415 NEW_TEMPSTORE (cfg, inst, dest, load);
1416 if (inst->opcode == CEE_STOBJ) {
1417 NEW_TEMPLOADA (cfg, inst, dest);
1418 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE);
1420 inst->cil_code = NULL;
1421 mono_add_ins_to_end (bb, inst);
1426 * We try to share variables when possible
1429 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1434 /* inlining can result in deeper stacks */
1435 if (slot >= ((MonoMethodNormal *)cfg->method)->header->max_stack)
1436 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1438 pos = ins->type - 1 + slot * STACK_MAX;
1440 switch (ins->type) {
1447 if ((vnum = cfg->intvars [pos]))
1448 return cfg->varinfo [vnum];
1449 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1450 cfg->intvars [pos] = res->inst_c0;
1453 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1459 * This function is called to handle items that are left on the evaluation stack
1460 * at basic block boundaries. What happens is that we save the values to local variables
1461 * and we reload them later when first entering the target basic block (with the
1462 * handle_loaded_temps () function).
1463 * A single joint point will use the same variables (stored in the array bb->out_stack or
1464 * bb->in_stack, if the basic block is before or after the joint point).
1467 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count) {
1469 MonoBasicBlock *outb;
1470 MonoInst *inst, **locals;
1474 if (cfg->verbose_level > 3)
1475 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
1476 if (!bb->out_scount) {
1478 bb->out_scount = count;
1479 //g_print ("bblock %d has out:", bb->block_num);
1480 for (i = 0; i < bb->out_count; ++i) {
1481 outb = bb->out_bb [i];
1482 //g_print (" %d", outb->block_num);
1483 if (outb->in_stack) {
1485 bb->out_stack = outb->in_stack;
1491 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1492 for (i = 0; i < count; ++i) {
1494 /* try to reuse temps already allocated for this purpouse, if they occupy the same
1495 * stack slot and if they are of the same type. */
1496 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1498 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1503 locals = bb->out_stack;
1504 for (i = 0; i < count; ++i) {
1505 /* add store ops at the end of the bb, before the branch */
1506 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1507 if (inst->opcode == CEE_STOBJ) {
1508 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
1509 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE);
1511 inst->cil_code = sp [i]->cil_code;
1512 mono_add_ins_to_end (bb, inst);
1514 if (cfg->verbose_level > 3)
1515 g_print ("storing %d to temp %d\n", i, locals [i]->inst_c0);
1518 for (i = 0; i < bb->out_count; ++i) {
1519 outb = bb->out_bb [i];
1520 if (outb->in_scount)
1521 continue; /* check they are the same locals */
1522 outb->in_scount = count;
1523 outb->in_stack = locals;
1529 ret_type_to_call_opcode (MonoType *type, int calli, int virt)
1534 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1538 case MONO_TYPE_VOID:
1539 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
1542 case MONO_TYPE_BOOLEAN:
1545 case MONO_TYPE_CHAR:
1548 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1552 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1553 case MONO_TYPE_CLASS:
1554 case MONO_TYPE_STRING:
1555 case MONO_TYPE_OBJECT:
1556 case MONO_TYPE_SZARRAY:
1557 case MONO_TYPE_ARRAY:
1558 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1561 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
1564 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
1565 case MONO_TYPE_VALUETYPE:
1566 if (type->data.klass->enumtype) {
1567 t = type->data.klass->enum_basetype->type;
1570 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1571 case MONO_TYPE_TYPEDBYREF:
1572 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1574 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
1580 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
1582 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
1584 ji->ip.label = label;
1585 ji->type = MONO_PATCH_INFO_SWITCH;
1586 ji->data.table = bbs;
1587 ji->next = cfg->patch_info;
1588 ji->table_size = num_blocks;
1589 cfg->patch_info = ji;
1593 * When we add a tree of instructions, we need to ensure the instructions currently
1594 * on the stack are executed before (like, if we load a value from a local).
1595 * We ensure this by saving the currently loaded values to temps and rewriting the
1596 * instructions to load the values.
1597 * This is not done for opcodes that terminate a basic block (because it's handled already
1598 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
1601 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
1603 MonoInst *load, *store, *temp, *ins;
1605 while (stack < sp) {
1607 /* handle also other constants */
1608 if (ins->opcode != OP_ICONST) {
1609 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1610 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1611 store->cil_code = ins->cil_code;
1612 if (store->opcode == CEE_STOBJ) {
1613 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
1614 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE);
1616 MONO_ADD_INS (bblock, store);
1617 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1618 load->cil_code = ins->cil_code;
1626 * Prepare arguments for passing to a function call.
1627 * Return a non-zero value if the arguments can't be passed to the given
1629 * The type checks are not yet complete and some conversions may need
1630 * casts on 32 or 64 bit architectures.
1633 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
1638 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
1642 for (i = 0; i < sig->param_count; ++i) {
1643 if (sig->params [i]->byref) {
1645 * check the result of ldelema is only passed as an argument if the byref
1646 * type matches exactly the array element type.
1647 * FIXME: if the argument as been saved on the stack as part of the
1648 * interface variable code (the value was on the stack at a basic block boundary)
1649 * we need to add the check in that case, too.
1651 if (args [i]->opcode == CEE_LDELEMA) {
1653 MonoClass *exact_class = mono_class_from_mono_type (sig->params [i]);
1654 if (!exact_class->valuetype) {
1655 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
1656 check->cil_code = args [i]->cil_code;
1657 check->klass = exact_class;
1658 check->inst_left = args [i]->inst_left;
1659 check->type = STACK_OBJ;
1660 args [i]->inst_left = check;
1663 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
1667 simple_type = sig->params [i]->type;
1669 switch (simple_type) {
1670 case MONO_TYPE_VOID:
1675 case MONO_TYPE_BOOLEAN:
1678 case MONO_TYPE_CHAR:
1681 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
1687 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
1690 case MONO_TYPE_CLASS:
1691 case MONO_TYPE_STRING:
1692 case MONO_TYPE_OBJECT:
1693 case MONO_TYPE_SZARRAY:
1694 case MONO_TYPE_ARRAY:
1695 if (args [i]->type != STACK_OBJ)
1700 if (args [i]->type != STACK_I8)
1705 if (args [i]->type != STACK_R8)
1708 case MONO_TYPE_VALUETYPE:
1709 if (sig->params [i]->data.klass->enumtype) {
1710 simple_type = sig->params [i]->data.klass->enum_basetype->type;
1713 if (args [i]->type != STACK_VTYPE)
1716 case MONO_TYPE_TYPEDBYREF:
1717 if (args [i]->type != STACK_VTYPE)
1721 g_error ("unknown type 0x%02x in check_call_signature", simple_type);
1728 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
1729 const guint8 *ip, gboolean to_end)
1731 MonoInst *temp, *store, *ins = (MonoInst*)call;
1732 MonoType *ret = sig->ret;
1734 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
1736 call->inst.type = STACK_OBJ;
1737 call->inst.opcode = CEE_CALL;
1738 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
1740 type_to_eval_stack_type (ret, ins);
1741 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
1744 if (MONO_TYPE_ISSTRUCT (ret)) {
1747 /* we use this to allocate native sized structs */
1748 temp->unused = sig->pinvoke;
1750 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
1751 if (call->inst.opcode == OP_VCALL)
1752 ins->inst_left = loada;
1754 ins->inst_right = loada; /* a virtual or indirect call */
1757 mono_add_ins_to_end (bblock, ins);
1759 MONO_ADD_INS (bblock, ins);
1761 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1762 store->cil_code = ip;
1764 mono_add_ins_to_end (bblock, store);
1766 MONO_ADD_INS (bblock, store);
1768 return temp->inst_c0;
1771 mono_add_ins_to_end (bblock, ins);
1773 MONO_ADD_INS (bblock, ins);
1778 inline static MonoCallInst *
1779 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1780 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
1785 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual));
1787 call->inst.cil_code = ip;
1789 call->signature = sig;
1790 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
1792 for (i = 0; i < (sig->param_count + sig->hasthis); ++i) {
1793 if (call->args [i]) {
1794 if (!call->args [i]->cil_code)
1795 call->args [i]->cil_code = ip;
1797 mono_add_ins_to_end (bblock, call->args [i]);
1799 MONO_ADD_INS (bblock, call->args [i]);
1806 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1807 MonoInst **args, MonoInst *addr, const guint8 *ip)
1809 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
1811 call->inst.inst_i0 = addr;
1813 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
1816 static MonoCallInst*
1817 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
1818 MonoInst **args, const guint8 *ip, MonoInst *this)
1820 gboolean virtual = this != NULL;
1823 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, FALSE);
1825 if (this && sig->hasthis &&
1826 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
1827 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
1828 call->method = mono_marshal_get_remoting_invoke_with_check (method);
1830 call->method = method;
1832 call->inst.flags |= MONO_INST_HAS_METHOD;
1833 call->inst.inst_left = this;
1839 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
1840 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
1842 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, signature, args, ip, this);
1844 return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
1848 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
1849 MonoInst **args, const guint8 *ip, gboolean to_end)
1855 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
1857 return mono_spill_call (cfg, bblock, call, sig, func == mono_array_new_va, ip, to_end);
1861 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
1863 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
1866 g_warning ("unregistered JIT ICall");
1867 g_assert_not_reached ();
1870 return mono_emit_native_call (cfg, bblock, info->wrapper, info->sig, args, ip, FALSE);
1874 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
1876 MonoInst *ins, *temp = NULL, *store, *load;
1880 /*g_print ("emulating: ");
1881 mono_print_tree (tree);
1883 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE));
1884 ins = (MonoInst*)call;
1886 call->inst.cil_code = tree->cil_code;
1888 call->signature = info->sig;
1890 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
1892 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
1893 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
1894 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1895 store->cil_code = tree->cil_code;
1900 nargs = info->sig->param_count + info->sig->hasthis;
1902 for (i = 1; i < nargs; i++) {
1903 call->args [i - 1]->next = call->args [i];
1907 call->args [nargs - 1]->next = store;
1909 if (cfg->prev_ins) {
1910 store->next = cfg->prev_ins->next;
1912 cfg->prev_ins->next = call->args [0];
1914 cfg->prev_ins->next = store;
1916 store->next = cfg->cbb->code;
1918 cfg->cbb->code = call->args [0];
1920 cfg->cbb->code = store;
1924 call->fptr = info->wrapper;
1926 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
1927 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1932 static MonoMethodSignature *
1933 mono_get_element_address_signature (int arity)
1935 static GHashTable *sighash = NULL;
1936 MonoMethodSignature *res;
1940 sighash = g_hash_table_new (NULL, NULL);
1943 if ((res = g_hash_table_lookup (sighash, (gpointer)arity)))
1946 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
1948 res->params [0] = &mono_defaults.array_class->byval_arg;
1950 for (i = 1; i <= arity; i++)
1951 res->params [i] = &mono_defaults.int_class->byval_arg;
1953 res->ret = &mono_defaults.int_class->byval_arg;
1955 g_hash_table_insert (sighash, (gpointer)arity, res);
1961 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native) {
1962 MonoInst *iargs [3];
1968 * This check breaks with spilled vars... need to handle it during verification anyway.
1969 * g_assert (klass && klass == src->klass && klass == dest->klass);
1973 n = mono_class_native_size (klass, &align);
1975 n = mono_class_value_size (klass, &align);
1977 if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
1979 MONO_INST_NEW (cfg, inst, OP_MEMCPY);
1980 inst->inst_left = dest;
1981 inst->inst_right = src;
1982 inst->cil_code = ip;
1984 MONO_ADD_INS (bblock, inst);
1989 NEW_ICONST (cfg, iargs [2], n);
1991 mono_emit_native_call (cfg, bblock, helper_memcpy, helper_sig_memcpy, iargs, ip, to_end);
1995 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
1997 MonoInst *iargs [2];
1998 MonoInst *ins, *zero_int32;
2001 NEW_ICONST (cfg, zero_int32, 0);
2003 mono_class_init (klass);
2004 n = mono_class_value_size (klass, NULL);
2005 MONO_INST_NEW (cfg, ins, 0);
2007 ins->inst_left = dest;
2008 ins->inst_right = zero_int32;
2011 ins->opcode = CEE_STIND_I1;
2012 MONO_ADD_INS (bblock, ins);
2015 ins->opcode = CEE_STIND_I2;
2016 MONO_ADD_INS (bblock, ins);
2019 ins->opcode = CEE_STIND_I4;
2020 MONO_ADD_INS (bblock, ins);
2023 handle_loaded_temps (cfg, bblock, stack_start, sp);
2024 NEW_ICONST (cfg, ins, n);
2027 mono_emit_jit_icall (cfg, bblock, helper_initobj, iargs, ip);
2032 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
2035 needs_cctor_run (MonoClass *klass, MonoMethod *caller)
2040 for (i = 0; i < klass->method.count; ++i) {
2041 method = klass->methods [i];
2042 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
2043 (strcmp (".cctor", method->name) == 0)) {
2044 if (caller == method)
2053 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
2055 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
2056 MonoMethodSignature *signature = method->signature;
2060 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2061 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2062 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
2063 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
2064 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2065 (method->klass->marshalbyref) ||
2066 !header || header->num_clauses ||
2067 /* fixme: why cant we inline valuetype returns? */
2068 MONO_TYPE_ISSTRUCT (signature->ret))
2071 /* its not worth to inline methods with valuetype arguments?? */
2072 for (i = 0; i < signature->param_count; i++) {
2073 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
2079 * if we can initialize the class of the method right away, we do,
2080 * otherwise we don't allow inlining if the class needs initialization,
2081 * since it would mean inserting a call to mono_runtime_class_init()
2082 * inside the inlined code
2084 if (!(cfg->opt & MONO_OPT_SHARED)) {
2085 vtable = mono_class_vtable (cfg->domain, method->klass);
2086 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)
2087 mono_runtime_class_init (vtable);
2088 else if (!vtable->initialized && needs_cctor_run (method->klass, NULL))
2092 * If we're compiling for shared code
2093 * the cctor will need to be run at aot method load time, for example,
2094 * or at the end of the compilation of the inlining method.
2096 if (needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
2099 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
2101 /* also consider num_locals? */
2102 if (header->code_size < 20)
2109 mini_get_ldelema_ins (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
2113 MonoMethodSignature *esig;
2115 rank = cmethod->signature->param_count - (is_set? 1: 0);
2117 * FIXME: handle TypeMismatch for set or use the slow path
2120 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
2122 NEW_GROUP (cfg, indexes, sp [1], sp [2]);
2123 MONO_INST_NEW (cfg, addr, OP_LDELEMA2D);
2124 addr->inst_left = sp [0];
2125 addr->inst_right = indexes;
2126 addr->cil_code = ip;
2127 addr->type = STACK_MP;
2128 addr->klass = cmethod->klass;
2131 esig = mono_get_element_address_signature (rank);
2132 temp = mono_emit_native_call (cfg, bblock, ves_array_element_address, esig, sp, ip, FALSE);
2133 NEW_TEMPLOAD (cfg, addr, temp);
2138 mini_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
2143 if (cmethod->klass == mono_defaults.string_class) {
2144 if (cmethod->name [0] != 'g' || strcmp (cmethod->name, "get_Chars"))
2147 } else if (cmethod->klass == mono_defaults.math_class) {
2148 if (strcmp (cmethod->name, "Sin") == 0)
2150 else if (strcmp (cmethod->name, "Cos") == 0)
2152 else if (strcmp (cmethod->name, "Tan") == 0)
2154 else if (strcmp (cmethod->name, "Atan") == 0)
2156 else if (strcmp (cmethod->name, "Sqrt") == 0)
2158 else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8)
2161 /* OP_FREM is not IEEE compatible */
2162 else if (strcmp (cmethod->name, "IEEERemainder") == 0)
2170 pc = fsig->param_count + fsig->hasthis;
2171 MONO_INST_NEW (cfg, ins, op);
2174 ins->inst_i0 = args [0];
2176 ins->inst_i1 = args [1];
2183 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
2185 MonoInst *store, *temp;
2188 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
2190 if (!sig->hasthis && sig->param_count == 0)
2194 if (sp [0]->opcode == OP_ICONST) {
2197 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
2199 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2200 store->cil_code = sp [0]->cil_code;
2201 MONO_ADD_INS (bblock, store);
2206 for (i = 0; i < sig->param_count; ++i) {
2207 if (sp [0]->opcode == OP_ICONST) {
2210 temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
2212 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2213 store->cil_code = sp [0]->cil_code;
2214 if (store->opcode == CEE_STOBJ) {
2215 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2216 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE);
2218 MONO_ADD_INS (bblock, store);
2226 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
2227 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b)
2229 MonoInst *ins, *rvar = NULL;
2230 MonoMethodHeader *cheader;
2231 MonoBasicBlock *ebblock, *sbblock;
2232 int i, costs, new_locals_offset;
2234 if (cfg->verbose_level > 2)
2235 g_print ("INLINE START %p %s\n", cmethod, mono_method_full_name (cmethod, TRUE));
2237 cheader = ((MonoMethodNormal *)cmethod)->header;
2239 if (!cmethod->inline_info) {
2240 mono_jit_stats.inlineable_methods++;
2241 cmethod->inline_info = 1;
2243 /* allocate space to store the return value */
2244 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2245 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2248 /* allocate local variables */
2249 new_locals_offset = cfg->num_varinfo;
2250 for (i = 0; i < cheader->num_locals; ++i)
2251 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
2253 /* allocate starte and end blocks */
2254 sbblock = NEW_BBLOCK (cfg);
2255 sbblock->block_num = cfg->num_bblocks++;
2256 sbblock->real_offset = real_offset;
2258 ebblock = NEW_BBLOCK (cfg);
2259 ebblock->block_num = cfg->num_bblocks++;
2260 ebblock->real_offset = real_offset;
2262 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
2264 if (costs >= 0 && costs < 60) {
2265 if (cfg->verbose_level > 2)
2266 g_print ("INLINE END %s\n", mono_method_full_name (cmethod, TRUE));
2268 mono_jit_stats.inlined_methods++;
2270 /* always add some code to avoid block split failures */
2271 MONO_INST_NEW (cfg, ins, CEE_NOP);
2272 MONO_ADD_INS (bblock, ins);
2275 bblock->next_bb = sbblock;
2276 link_bblock (cfg, bblock, sbblock);
2279 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
2285 if (cfg->verbose_level > 2)
2286 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
2292 * Some of these comments may well be out-of-date.
2293 * Design decisions: we do a single pass over the IL code (and we do bblock
2294 * splitting/merging in the few cases when it's required: a back jump to an IL
2295 * address that was not already seen as bblock starting point).
2296 * Code is validated as we go (full verification is still better left to metadata/verify.c).
2297 * Complex operations are decomposed in simpler ones right away. We need to let the
2298 * arch-specific code peek and poke inside this process somehow (except when the
2299 * optimizations can take advantage of the full semantic info of coarse opcodes).
2300 * All the opcodes of the form opcode.s are 'normalized' to opcode.
2301 * MonoInst->opcode initially is the IL opcode or some simplification of that
2302 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
2303 * opcode with value bigger than OP_LAST.
2304 * At this point the IR can be handed over to an interpreter, a dumb code generator
2305 * or to the optimizing code generator that will translate it to SSA form.
2307 * Profiling directed optimizations.
2308 * We may compile by default with few or no optimizations and instrument the code
2309 * or the user may indicate what methods to optimize the most either in a config file
2310 * or through repeated runs where the compiler applies offline the optimizations to
2311 * each method and then decides if it was worth it.
2314 * * consider using an array instead of an hash table (bb_hash)
2317 #define CHECK_TYPE(ins) if (!(ins)->type) goto unverified
2318 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) goto unverified
2319 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) goto unverified
2321 /* offset from br.s -> br like opcodes */
2322 #define BIG_BRANCH_OFFSET 13
2325 get_basic_blocks (MonoCompile *cfg, GHashTable *bbhash, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end)
2327 unsigned char *ip = start;
2328 unsigned char *target;
2331 MonoBasicBlock *bblock;
2332 const MonoOpcode *opcode;
2335 cli_addr = ip - start;
2336 i = mono_opcode_value ((const guint8 **)&ip);
2337 opcode = &mono_opcodes [i];
2338 switch (opcode->argument) {
2339 case MonoInlineNone:
2342 case MonoInlineString:
2343 case MonoInlineType:
2344 case MonoInlineField:
2345 case MonoInlineMethod:
2348 case MonoShortInlineR:
2355 case MonoShortInlineVar:
2356 case MonoShortInlineI:
2359 case MonoShortInlineBrTarget:
2360 target = start + cli_addr + 2 + (signed char)ip [1];
2361 GET_BBLOCK (cfg, bbhash, bblock, target);
2364 case MonoInlineBrTarget:
2365 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
2366 GET_BBLOCK (cfg, bbhash, bblock, target);
2369 case MonoInlineSwitch: {
2370 guint32 n = read32 (ip + 1);
2373 cli_addr += 5 + 4 * n;
2374 target = start + cli_addr;
2375 GET_BBLOCK (cfg, bbhash, bblock, target);
2377 for (j = 0; j < n; ++j) {
2378 target = start + cli_addr + (gint32)read32 (ip);
2379 GET_BBLOCK (cfg, bbhash, bblock, target);
2389 g_assert_not_reached ();
2398 * mono_method_to_ir: translates IL into basic blocks containing trees
2401 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
2402 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
2403 guint inline_offset, gboolean is_virtual_call)
2405 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
2406 MonoInst *ins, **sp, **stack_start;
2407 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
2409 MonoMethod *cmethod;
2410 MonoInst **arg_array;
2411 MonoMethodHeader *header;
2413 guint32 token, ins_flag;
2415 unsigned char *ip, *end, *target;
2416 static double r8_0 = 0.0;
2417 MonoMethodSignature *sig;
2418 MonoType **param_types;
2419 GList *bb_recheck = NULL, *tmp;
2420 int i, n, start_new_bblock, align;
2421 int num_calls = 0, inline_costs = 0;
2422 int *filter_lengths = NULL;
2423 int breakpoint_id = 0;
2426 image = method->klass->image;
2427 header = ((MonoMethodNormal *)method)->header;
2428 sig = method->signature;
2429 ip = (unsigned char*)header->code;
2430 end = ip + header->code_size;
2431 mono_jit_stats.cil_code_size += header->code_size;
2433 if (cfg->method == method) {
2435 bbhash = cfg->bb_hash;
2437 real_offset = inline_offset;
2438 bbhash = g_hash_table_new (g_direct_hash, NULL);
2441 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
2442 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
2444 dont_inline = g_list_prepend (dont_inline, method);
2445 if (cfg->method == method) {
2448 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
2449 start_bblock->cil_code = NULL;
2450 start_bblock->cil_length = 0;
2451 start_bblock->block_num = cfg->num_bblocks++;
2454 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
2455 end_bblock->cil_code = NULL;
2456 end_bblock->cil_length = 0;
2457 end_bblock->block_num = cfg->num_bblocks++;
2458 g_assert (cfg->num_bblocks == 2);
2460 arg_array = alloca (sizeof (MonoInst *) * (sig->hasthis + sig->param_count));
2461 for (i = sig->hasthis + sig->param_count - 1; i >= 0; i--)
2462 arg_array [i] = cfg->varinfo [i];
2464 if (mono_compile_aot)
2465 cfg->opt |= MONO_OPT_SHARED;
2467 if (header->num_clauses) {
2468 int size = sizeof (int) * header->num_clauses;
2469 filter_lengths = alloca (size);
2470 memset (filter_lengths, 0, size);
2472 cfg->spvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2473 /* prevent it from being register allocated */
2474 cfg->spvar->flags |= MONO_INST_INDIRECT;
2476 /* handle exception clauses */
2477 for (i = 0; i < header->num_clauses; ++i) {
2478 //unsigned char *p = ip;
2479 MonoExceptionClause *clause = &header->clauses [i];
2480 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->try_offset);
2481 tblock->real_offset = clause->try_offset;
2482 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
2483 tblock->real_offset = clause->handler_offset;
2485 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
2486 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2487 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2488 MONO_ADD_INS (tblock, ins);
2491 /*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);
2493 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
2495 /* catch and filter blocks get the exception object on the stack */
2496 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
2497 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2498 /* mostly like handle_stack_args (), but just sets the input args */
2499 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
2501 cfg->exvar = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
2502 /* prevent it from being register allocated */
2503 cfg->exvar->flags |= MONO_INST_INDIRECT;
2505 tblock->in_scount = 1;
2506 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2507 tblock->in_stack [0] = cfg->exvar;
2509 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2510 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->token_or_filter);
2511 tblock->real_offset = clause->token_or_filter;
2512 tblock->in_scount = 1;
2513 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2514 tblock->in_stack [0] = cfg->exvar;
2515 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2516 MONO_ADD_INS (tblock, ins);
2522 arg_array = alloca (sizeof (MonoInst *) * (sig->hasthis + sig->param_count));
2523 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
2526 /* FIRST CODE BLOCK */
2527 bblock = NEW_BBLOCK (cfg);
2528 bblock->cil_code = ip;
2530 ADD_BBLOCK (cfg, bbhash, bblock);
2532 if (cfg->method == method) {
2533 breakpoint_id = mono_debugger_method_has_breakpoint (method);
2534 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
2535 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2536 MONO_ADD_INS (bblock, ins);
2540 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED)))) {
2541 /* we use a separate basic block for the initialization code */
2542 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
2543 init_localsbb->real_offset = real_offset;
2544 start_bblock->next_bb = init_localsbb;
2545 init_localsbb->next_bb = bblock;
2546 link_bblock (cfg, start_bblock, init_localsbb);
2547 link_bblock (cfg, init_localsbb, bblock);
2548 init_localsbb->block_num = cfg->num_bblocks++;
2550 start_bblock->next_bb = bblock;
2551 link_bblock (cfg, start_bblock, bblock);
2554 if (get_basic_blocks (cfg, bbhash, header, real_offset, ip, end))
2557 mono_debug_init_method (cfg, bblock, breakpoint_id);
2559 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * (sig->hasthis + sig->param_count));
2561 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
2562 for (n = 0; n < sig->param_count; ++n)
2563 param_types [n + sig->hasthis] = sig->params [n];
2565 /* do this somewhere outside - not here */
2566 NEW_ICONST (cfg, zero_int32, 0);
2567 NEW_ICONST (cfg, zero_int64, 0);
2568 zero_int64->type = STACK_I8;
2569 NEW_PCONST (cfg, zero_ptr, 0);
2570 NEW_PCONST (cfg, zero_obj, 0);
2571 zero_obj->type = STACK_OBJ;
2573 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
2574 zero_r8->type = STACK_R8;
2575 zero_r8->inst_p0 = &r8_0;
2577 /* add a check for this != NULL to inlined methods */
2578 if (is_virtual_call) {
2579 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
2580 NEW_ARGLOAD (cfg, ins->inst_left, 0);
2582 MONO_ADD_INS (bblock, ins);
2585 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
2586 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
2589 start_new_bblock = 0;
2592 if (cfg->method == method)
2593 real_offset = ip - header->code;
2595 real_offset = inline_offset;
2597 if (start_new_bblock) {
2598 bblock->cil_length = ip - bblock->cil_code;
2599 if (start_new_bblock == 2) {
2600 g_assert (ip == tblock->cil_code);
2602 GET_BBLOCK (cfg, bbhash, tblock, ip);
2604 bblock->next_bb = tblock;
2606 start_new_bblock = 0;
2607 for (i = 0; i < bblock->in_scount; ++i) {
2608 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2612 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
2613 link_bblock (cfg, bblock, tblock);
2614 if (sp != stack_start) {
2615 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2618 bblock->next_bb = tblock;
2620 for (i = 0; i < bblock->in_scount; ++i) {
2621 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2627 if (cfg->coverage_info) {
2628 MonoInst *store, *one;
2629 guint32 cil_offset = ip - header->code;
2630 cfg->coverage_info->data [cil_offset].cil_code = ip;
2632 /* TODO: Use an increment here */
2633 NEW_ICONST (cfg, one, 1);
2636 NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
2639 MONO_INST_NEW (cfg, store, CEE_STIND_I);
2640 store->cil_code = ip;
2641 store->inst_left = ins;
2642 store->inst_right = one;
2644 MONO_ADD_INS (bblock, store);
2647 if (cfg->verbose_level > 3)
2648 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, sp-stack_start, mono_disasm_code_one (NULL, method, ip, NULL));
2655 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2656 ins->cil_code = ip++;
2657 MONO_ADD_INS (bblock, ins);
2663 CHECK_STACK_OVF (1);
2664 n = (*ip)-CEE_LDARG_0;
2665 NEW_ARGLOAD (cfg, ins, n);
2666 ins->cil_code = ip++;
2673 CHECK_STACK_OVF (1);
2674 n = (*ip)-CEE_LDLOC_0;
2675 NEW_LOCLOAD (cfg, ins, n);
2676 ins->cil_code = ip++;
2684 n = (*ip)-CEE_STLOC_0;
2686 handle_loaded_temps (cfg, bblock, stack_start, sp);
2687 NEW_LOCSTORE (cfg, ins, n, *sp);
2689 if (ins->opcode == CEE_STOBJ) {
2690 NEW_LOCLOADA (cfg, ins, n);
2691 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2693 MONO_ADD_INS (bblock, ins);
2698 CHECK_STACK_OVF (1);
2699 NEW_ARGLOAD (cfg, ins, ip [1]);
2705 CHECK_STACK_OVF (1);
2706 NEW_ARGLOADA (cfg, ins, ip [1]);
2714 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
2715 handle_loaded_temps (cfg, bblock, stack_start, sp);
2717 if (ins->opcode == CEE_STOBJ) {
2718 NEW_ARGLOADA (cfg, ins, ip [1]);
2719 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2721 MONO_ADD_INS (bblock, ins);
2725 CHECK_STACK_OVF (1);
2726 NEW_LOCLOAD (cfg, ins, ip [1]);
2732 CHECK_STACK_OVF (1);
2733 NEW_LOCLOADA (cfg, ins, ip [1]);
2741 handle_loaded_temps (cfg, bblock, stack_start, sp);
2742 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
2744 if (ins->opcode == CEE_STOBJ) {
2745 NEW_LOCLOADA (cfg, ins, ip [1]);
2746 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2748 MONO_ADD_INS (bblock, ins);
2753 CHECK_STACK_OVF (1);
2754 NEW_PCONST (cfg, ins, NULL);
2756 ins->type = STACK_OBJ;
2761 CHECK_STACK_OVF (1);
2762 NEW_ICONST (cfg, ins, -1);
2776 CHECK_STACK_OVF (1);
2777 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
2783 CHECK_STACK_OVF (1);
2785 NEW_ICONST (cfg, ins, *((signed char*)ip));
2791 CHECK_STACK_OVF (1);
2792 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
2798 CHECK_STACK_OVF (1);
2799 MONO_INST_NEW (cfg, ins, OP_I8CONST);
2801 ins->type = STACK_I8;
2803 ins->inst_l = (gint64)read64 (ip);
2808 float *f = g_malloc (sizeof (float));
2809 CHECK_STACK_OVF (1);
2810 MONO_INST_NEW (cfg, ins, OP_R4CONST);
2811 ins->type = STACK_R8;
2820 double *d = g_malloc (sizeof (double));
2821 CHECK_STACK_OVF (1);
2822 MONO_INST_NEW (cfg, ins, OP_R8CONST);
2823 ins->type = STACK_R8;
2832 MonoInst *temp, *store;
2834 CHECK_STACK_OVF (1);
2839 * small optimization: if the loaded value was from a local already,
2840 * just load it twice.
2842 if (ins->ssa_op == MONO_SSA_LOAD &&
2843 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
2845 MONO_INST_NEW (cfg, temp, 0);
2847 temp->cil_code = ip;
2850 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
2851 temp->cil_code = ip;
2852 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2853 store->cil_code = ip;
2854 MONO_ADD_INS (bblock, store);
2855 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
2858 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
2868 MONO_INST_NEW (cfg, ins, CEE_POP);
2869 MONO_ADD_INS (bblock, ins);
2870 ins->cil_code = ip++;
2875 if (stack_start != sp)
2877 MONO_INST_NEW (cfg, ins, CEE_JMP);
2878 token = read32 (ip + 1);
2879 /* FIXME: check the signature matches */
2880 cmethod = mono_get_method (image, token, NULL);
2881 ins->inst_p0 = cmethod;
2882 MONO_ADD_INS (bblock, ins);
2884 start_new_bblock = 1;
2888 case CEE_CALLVIRT: {
2889 MonoInst *addr = NULL;
2890 MonoMethodSignature *fsig = NULL;
2891 int temp, array_rank = 0;
2892 int virtual = *ip == CEE_CALLVIRT;
2894 token = read32 (ip + 1);
2896 if (*ip == CEE_CALLI) {
2901 if (method->wrapper_type != MONO_WRAPPER_NONE)
2902 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
2904 fsig = mono_metadata_parse_signature (image, token);
2906 n = fsig->param_count + fsig->hasthis;
2909 if (method->wrapper_type != MONO_WRAPPER_NONE) {
2910 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
2912 cmethod = mono_get_method (image, token, NULL);
2915 if (!cmethod->klass->inited)
2916 mono_class_init (cmethod->klass);
2918 if (cmethod->signature->pinvoke) {
2919 #ifdef MONO_USE_EXC_TABLES
2920 if (mono_method_blittable (cmethod)) {
2921 fsig = cmethod->signature;
2924 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
2925 fsig = wrapper->signature;
2926 #ifdef MONO_USE_EXC_TABLES
2930 fsig = mono_method_get_signature (cmethod, image, token);
2933 n = fsig->param_count + fsig->hasthis;
2935 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
2936 cmethod->klass->parent == mono_defaults.array_class) {
2937 array_rank = cmethod->klass->rank;
2940 if (cmethod->string_ctor)
2941 g_assert_not_reached ();
2947 //g_assert (!virtual || fsig->hasthis);
2951 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
2954 if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL)) {
2956 for (i = 0; i < n; ++i) {
2957 NEW_ARGSTORE (cfg, ins, i, sp [i]);
2959 MONO_ADD_INS (bblock, ins);
2961 MONO_INST_NEW (cfg, ins, CEE_JMP);
2963 ins->inst_p0 = cmethod;
2964 MONO_ADD_INS (bblock, ins);
2965 start_new_bblock = 1;
2966 /* skip CEE_RET as well */
2971 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_opcode_for_method (cfg, cmethod, fsig, sp))) {
2974 if (MONO_TYPE_IS_VOID (fsig->ret)) {
2975 MONO_ADD_INS (bblock, ins);
2977 type_to_eval_stack_type (fsig->ret, ins);
2986 handle_loaded_temps (cfg, bblock, stack_start, sp);
2988 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
2989 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
2990 mono_method_check_inlining (cfg, cmethod) &&
2991 !g_list_find (dont_inline, cmethod)) {
2993 MonoBasicBlock *ebblock;
2995 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
2999 GET_BBLOCK (cfg, bbhash, bblock, ip);
3000 ebblock->next_bb = bblock;
3001 link_bblock (cfg, ebblock, bblock);
3003 if (!MONO_TYPE_IS_VOID (fsig->ret))
3006 /* indicates start of a new block, and triggers a load of all
3007 stack arguments at bb boundarie */
3010 inline_costs += costs;
3015 inline_costs += 10 * num_calls++;
3017 /* tail recursion elimination */
3018 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == cfg->method && ip [5] == CEE_RET) {
3019 gboolean has_vtargs = FALSE;
3022 /* keep it simple */
3023 for (i = fsig->param_count - 1; i >= 0; i--) {
3024 if (MONO_TYPE_ISSTRUCT (cmethod->signature->params [i]))
3029 for (i = 0; i < n; ++i) {
3030 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3032 MONO_ADD_INS (bblock, ins);
3034 MONO_INST_NEW (cfg, ins, CEE_BR);
3036 MONO_ADD_INS (bblock, ins);
3037 tblock = start_bblock->out_bb [0];
3038 link_bblock (cfg, bblock, tblock);
3039 ins->inst_target_bb = tblock;
3040 start_new_bblock = 1;
3043 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3044 /* just create a dummy - the value is never used */
3045 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
3046 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3054 if (*ip == CEE_CALLI) {
3056 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
3057 NEW_TEMPLOAD (cfg, *sp, temp);
3061 } else if (array_rank) {
3064 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
3065 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
3066 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
3068 if (ins->opcode == CEE_STOBJ) {
3069 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
3071 MONO_ADD_INS (bblock, ins);
3074 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
3075 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3076 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
3080 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
3081 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3084 g_assert_not_reached ();
3088 if (0 && CODE_IS_STLOC (ip + 5) && (!MONO_TYPE_ISSTRUCT (fsig->ret)) && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)) {
3089 /* no need to spill */
3090 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
3093 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) {
3094 NEW_TEMPLOAD (cfg, *sp, temp);
3104 if (cfg->method != method) {
3105 /* return from inlined methode */
3110 //g_assert (returnvar != -1);
3111 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
3112 store->cil_code = sp [0]->cil_code;
3113 if (store->opcode == CEE_STOBJ) {
3114 g_assert_not_reached ();
3115 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
3116 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
3118 MONO_ADD_INS (bblock, store);
3122 g_assert (!return_var);
3125 MONO_INST_NEW (cfg, ins, CEE_NOP);
3126 ins->opcode = mono_type_to_stind (method->signature->ret);
3127 if (ins->opcode == CEE_STOBJ) {
3128 NEW_RETLOADA (cfg, ins);
3129 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3131 ins->opcode = OP_SETRET;
3133 ins->inst_i0 = *sp;;
3134 ins->inst_i1 = NULL;
3135 MONO_ADD_INS (bblock, ins);
3139 if (sp != stack_start)
3141 MONO_INST_NEW (cfg, ins, CEE_BR);
3142 ins->cil_code = ip++;
3143 ins->inst_target_bb = end_bblock;
3144 MONO_ADD_INS (bblock, ins);
3145 link_bblock (cfg, bblock, end_bblock);
3146 start_new_bblock = 1;
3149 MONO_INST_NEW (cfg, ins, CEE_BR);
3150 ins->cil_code = ip++;
3151 MONO_ADD_INS (bblock, ins);
3152 target = ip + 1 + (signed char)(*ip);
3154 GET_BBLOCK (cfg, bbhash, tblock, target);
3155 link_bblock (cfg, bblock, tblock);
3156 CHECK_BBLOCK (target, ip, tblock);
3157 ins->inst_target_bb = tblock;
3158 if (sp != stack_start) {
3159 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3162 start_new_bblock = 1;
3168 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3169 ins->cil_code = ip++;
3170 target = ip + 1 + *(signed char*)ip;
3172 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
3173 if (sp != stack_start) {
3174 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3190 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3191 ins->cil_code = ip++;
3192 target = ip + 1 + *(signed char*)ip;
3195 if (sp != stack_start) {
3196 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3202 MONO_INST_NEW (cfg, ins, CEE_BR);
3203 ins->cil_code = ip++;
3204 MONO_ADD_INS (bblock, ins);
3205 target = ip + 4 + (gint32)read32(ip);
3207 GET_BBLOCK (cfg, bbhash, tblock, target);
3208 link_bblock (cfg, bblock, tblock);
3209 CHECK_BBLOCK (target, ip, tblock);
3210 ins->inst_target_bb = tblock;
3211 if (sp != stack_start) {
3212 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3215 start_new_bblock = 1;
3221 MONO_INST_NEW (cfg, ins, *ip);
3222 ins->cil_code = ip++;
3223 target = ip + 4 + (gint32)read32(ip);
3225 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
3226 if (sp != stack_start) {
3227 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3243 MONO_INST_NEW (cfg, ins, *ip);
3244 ins->cil_code = ip++;
3245 target = ip + 4 + (gint32)read32(ip);
3248 if (sp != stack_start) {
3249 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3256 n = read32 (ip + 1);
3257 MONO_INST_NEW (cfg, ins, *ip);
3259 ins->inst_left = *sp;
3260 if (ins->inst_left->type != STACK_I4) goto unverified;
3263 target = ip + n * sizeof (guint32);
3264 MONO_ADD_INS (bblock, ins);
3265 GET_BBLOCK (cfg, bbhash, tblock, target);
3266 link_bblock (cfg, bblock, tblock);
3267 ins->klass = GUINT_TO_POINTER (n);
3268 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
3269 ins->inst_many_bb [n] = tblock;
3271 for (i = 0; i < n; ++i) {
3272 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
3273 link_bblock (cfg, bblock, tblock);
3274 ins->inst_many_bb [i] = tblock;
3277 /* FIXME: handle stack args */
3292 MONO_INST_NEW (cfg, ins, *ip);
3297 ins->type = ldind_type [*ip - CEE_LDIND_I1];
3298 ins->flags |= ins_flag;
3310 MONO_INST_NEW (cfg, ins, *ip);
3311 ins->cil_code = ip++;
3313 handle_loaded_temps (cfg, bblock, stack_start, sp);
3314 MONO_ADD_INS (bblock, ins);
3315 ins->inst_i0 = sp [0];
3316 ins->inst_i1 = sp [1];
3317 ins->flags |= ins_flag;
3348 case CEE_CONV_OVF_I8:
3349 case CEE_CONV_OVF_U8:
3355 case CEE_CONV_OVF_I4:
3356 case CEE_CONV_OVF_I1:
3357 case CEE_CONV_OVF_I2:
3358 case CEE_CONV_OVF_I:
3359 case CEE_CONV_OVF_U:
3362 if (sp [-1]->type == STACK_R8) {
3363 ADD_UNOP (CEE_CONV_OVF_I8);
3371 case CEE_CONV_OVF_U1:
3372 case CEE_CONV_OVF_U2:
3373 case CEE_CONV_OVF_U4:
3376 if (sp [-1]->type == STACK_R8) {
3377 ADD_UNOP (CEE_CONV_OVF_U8);
3385 case CEE_CONV_OVF_I1_UN:
3386 case CEE_CONV_OVF_I2_UN:
3387 case CEE_CONV_OVF_I4_UN:
3388 case CEE_CONV_OVF_I8_UN:
3389 case CEE_CONV_OVF_U1_UN:
3390 case CEE_CONV_OVF_U2_UN:
3391 case CEE_CONV_OVF_U4_UN:
3392 case CEE_CONV_OVF_U8_UN:
3393 case CEE_CONV_OVF_I_UN:
3394 case CEE_CONV_OVF_U_UN:
3400 g_error ("opcode 0x%02x not handled", *ip);
3403 MonoInst *iargs [3];
3406 token = read32 (ip + 1);
3407 if (method->wrapper_type != MONO_WRAPPER_NONE)
3408 klass = mono_method_get_wrapper_data (method, token);
3410 klass = mono_class_get (image, token);
3412 mono_class_init (klass);
3413 n = mono_class_value_size (klass, NULL);
3414 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3415 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3416 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3418 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3419 copy->inst_left = iargs [0];
3420 copy->inst_right = *sp;
3421 copy->cil_code = ip;
3423 MONO_ADD_INS (bblock, copy);
3426 NEW_ICONST (cfg, iargs [2], n);
3427 iargs [2]->cil_code = ip;
3429 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3431 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3438 CHECK_STACK_OVF (1);
3439 n = read32 (ip + 1);
3441 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3443 MonoInst *iargs [1];
3445 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
3446 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
3447 NEW_TEMPLOAD (cfg, *sp, temp);
3451 if (mono_compile_aot) {
3452 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n);
3455 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
3457 MonoInst *iargs [3];
3458 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3459 NEW_IMAGECONST (cfg, iargs [1], image);
3460 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
3461 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
3462 NEW_TEMPLOAD (cfg, *sp, temp);
3463 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3465 NEW_PCONST (cfg, ins, NULL);
3467 ins->type = STACK_OBJ;
3468 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3477 MonoInst *iargs [2];
3478 MonoMethodSignature *fsig;
3481 token = read32 (ip + 1);
3482 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3483 cmethod = mono_method_get_wrapper_data (method, token);
3485 cmethod = mono_get_method (image, token, NULL);
3486 fsig = mono_method_get_signature (cmethod, image, token);
3488 mono_class_init (cmethod->klass);
3490 n = fsig->param_count;
3493 /* move the args to allow room for 'this' in the first position */
3499 handle_loaded_temps (cfg, bblock, stack_start, sp);
3502 if (cmethod->klass->parent == mono_defaults.array_class) {
3503 NEW_METHODCONST (cfg, *sp, cmethod);
3504 temp = mono_emit_native_call (cfg, bblock, mono_array_new_va, fsig, sp, ip, FALSE);
3506 } else if (cmethod->string_ctor) {
3507 /* we simply pass a null pointer */
3508 NEW_PCONST (cfg, *sp, NULL);
3509 /* now call the string ctor */
3510 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
3512 if (cmethod->klass->valuetype) {
3513 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
3514 temp = iargs [0]->inst_c0;
3515 NEW_TEMPLOADA (cfg, *sp, temp);
3517 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
3518 NEW_DOMAINCONST (cfg, iargs [0]);
3519 NEW_CLASSCONST (cfg, iargs [1], cmethod->klass);
3521 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3523 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
3524 NEW_PCONST (cfg, iargs [0], vtable);
3525 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
3527 NEW_TEMPLOAD (cfg, *sp, temp);
3530 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3531 mono_method_check_inlining (cfg, cmethod) &&
3532 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
3533 !g_list_find (dont_inline, cmethod)) {
3535 MonoBasicBlock *ebblock;
3536 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3541 GET_BBLOCK (cfg, bbhash, bblock, ip);
3542 ebblock->next_bb = bblock;
3543 link_bblock (cfg, ebblock, bblock);
3545 NEW_TEMPLOAD (cfg, *sp, temp);
3548 /* indicates start of a new block, and triggers a load
3549 of all stack arguments at bb boundarie */
3552 inline_costs += costs;
3556 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3559 /* now call the actual ctor */
3560 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3564 NEW_TEMPLOAD (cfg, *sp, temp);
3573 MONO_INST_NEW (cfg, ins, *ip);
3575 klass = mono_class_get (image, read32 (ip + 1));
3576 mono_class_init (klass);
3577 ins->type = STACK_OBJ;
3578 ins->inst_left = *sp;
3579 ins->inst_newa_class = klass;
3585 MonoInst *add, *vtoffset;
3589 token = read32 (ip + 1);
3590 if (method->wrapper_type != MONO_WRAPPER_NONE)
3591 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3593 klass = mono_class_get (image, token);
3594 mono_class_init (klass);
3597 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
3598 ins->type = STACK_OBJ;
3599 ins->inst_left = *sp;
3601 ins->inst_newa_class = klass;
3604 MONO_INST_NEW (cfg, add, CEE_ADD);
3605 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3606 add->inst_left = ins;
3607 add->inst_right = vtoffset;
3608 add->type = STACK_MP;
3616 MONO_INST_NEW (cfg, ins, *ip);
3618 klass = mono_class_get (image, read32 (ip + 1));
3619 mono_class_init (klass);
3620 ins->type = STACK_OBJ;
3621 ins->inst_left = *sp;
3623 ins->inst_newa_class = klass;
3630 MONO_INST_NEW (cfg, ins, *ip);
3632 ins->inst_left = *sp;
3633 ins->cil_code = ip++;
3634 MONO_ADD_INS (bblock, ins);
3636 start_new_bblock = 1;
3641 MonoInst *offset_ins;
3642 MonoClassField *field;
3643 MonoBasicBlock *ebblock;
3647 if (*ip == CEE_STFLD) {
3654 // FIXME: enable this test later.
3655 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
3657 token = read32 (ip + 1);
3658 field = mono_field_from_token (image, token, &klass);
3659 mono_class_init (klass);
3661 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
3662 /* FIXME: mark instructions for use in SSA */
3663 if (*ip == CEE_STFLD) {
3664 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
3665 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
3666 MonoInst *iargs [5];
3669 NEW_CLASSCONST (cfg, iargs [1], klass);
3670 NEW_FIELDCONST (cfg, iargs [2], field);
3671 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
3675 if (cfg->opt & MONO_OPT_INLINE) {
3676 costs = inline_method (cfg, stfld_wrapper, stfld_wrapper->signature, bblock,
3677 iargs, ip, real_offset, dont_inline, &ebblock);
3678 g_assert (costs > 0);
3683 GET_BBLOCK (cfg, bbhash, bblock, ip);
3684 ebblock->next_bb = bblock;
3685 link_bblock (cfg, ebblock, bblock);
3687 /* indicates start of a new block, and triggers a load
3688 of all stack arguments at bb boundarie */
3691 inline_costs += costs;
3694 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, stfld_wrapper->signature, iargs, ip, NULL);
3698 NEW_ICONST (cfg, offset_ins, foffset);
3699 MONO_INST_NEW (cfg, ins, CEE_ADD);
3701 ins->inst_left = *sp;
3702 ins->inst_right = offset_ins;
3703 ins->type = STACK_MP;
3705 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
3706 store->cil_code = ip;
3707 store->inst_left = ins;
3708 store->inst_right = sp [1];
3709 handle_loaded_temps (cfg, bblock, stack_start, sp);
3710 store->flags |= ins_flag;
3712 if (store->opcode == CEE_STOBJ) {
3713 handle_stobj (cfg, bblock, ins, sp [1], ip,
3714 mono_class_from_mono_type (field->type), FALSE, FALSE);
3716 MONO_ADD_INS (bblock, store);
3719 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
3720 /* fixme: we need to inline that call somehow */
3721 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
3722 MonoInst *iargs [4];
3726 NEW_CLASSCONST (cfg, iargs [1], klass);
3727 NEW_FIELDCONST (cfg, iargs [2], field);
3728 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
3729 if (cfg->opt & MONO_OPT_INLINE) {
3730 costs = inline_method (cfg, ldfld_wrapper, ldfld_wrapper->signature, bblock,
3731 iargs, ip, real_offset, dont_inline, &ebblock);
3732 g_assert (costs > 0);
3737 GET_BBLOCK (cfg, bbhash, bblock, ip);
3738 ebblock->next_bb = bblock;
3739 link_bblock (cfg, ebblock, bblock);
3741 temp = iargs [0]->inst_i0->inst_c0;
3743 if (*ip == CEE_LDFLDA) {
3744 /* not sure howto handle this */
3745 NEW_TEMPLOADA (cfg, *sp, temp);
3747 NEW_TEMPLOAD (cfg, *sp, temp);
3751 /* indicates start of a new block, and triggers a load of
3752 all stack arguments at bb boundarie */
3755 inline_costs += costs;
3758 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, ldfld_wrapper->signature, iargs, ip, NULL);
3759 if (*ip == CEE_LDFLDA) {
3760 /* not sure howto handle this */
3761 NEW_TEMPLOADA (cfg, *sp, temp);
3763 NEW_TEMPLOAD (cfg, *sp, temp);
3768 NEW_ICONST (cfg, offset_ins, foffset);
3769 MONO_INST_NEW (cfg, ins, CEE_ADD);
3771 ins->inst_left = *sp;
3772 ins->inst_right = offset_ins;
3773 ins->type = STACK_MP;
3775 if (*ip == CEE_LDFLDA) {
3779 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
3780 type_to_eval_stack_type (field->type, load);
3781 load->cil_code = ip;
3782 load->inst_left = ins;
3783 load->flags |= ins_flag;
3795 MonoClassField *field;
3797 token = read32 (ip + 1);
3799 field = mono_field_from_token (image, token, &klass);
3800 mono_class_init (klass);
3802 handle_loaded_temps (cfg, bblock, stack_start, sp);
3804 if (((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot)) {
3806 MonoInst *iargs [2];
3807 g_assert (field->parent);
3808 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3809 NEW_FIELDCONST (cfg, iargs [1], field);
3810 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
3811 NEW_TEMPLOAD (cfg, ins, temp);
3815 vtable = mono_class_vtable (cfg->domain, klass);
3816 if (!cfg->domain->thread_static_fields || !(addr = g_hash_table_lookup (cfg->domain->thread_static_fields, field))) {
3817 if (!vtable->initialized && !(klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && needs_cctor_run (klass, method)) {
3818 MonoInst *iargs [1];
3819 NEW_PCONST (cfg, iargs [0], vtable);
3820 mono_emit_jit_icall (cfg, bblock, mono_runtime_class_init, iargs, ip);
3821 if (cfg->verbose_level > 2)
3822 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
3824 mono_runtime_class_init (vtable);
3826 addr = (char*)vtable->data + field->offset;
3827 NEW_PCONST (cfg, ins, addr);
3831 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
3832 * This could be later optimized to do just a couple of
3833 * memory dereferences with constant offsets.
3836 MonoInst *iargs [1];
3837 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
3838 temp = mono_emit_jit_icall (cfg, bblock, mono_threads_get_static_data, iargs, ip);
3839 NEW_TEMPLOAD (cfg, ins, temp);
3843 /* FIXME: mark instructions for use in SSA */
3844 if (*ip == CEE_LDSFLDA) {
3846 } else if (*ip == CEE_STSFLD) {
3850 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
3851 store->cil_code = ip;
3852 store->inst_left = ins;
3853 store->inst_right = sp [0];
3854 store->flags |= ins_flag;
3857 if (store->opcode == CEE_STOBJ) {
3858 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
3860 MONO_ADD_INS (bblock, store);
3862 gboolean is_const = FALSE;
3863 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
3864 if (!((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) &&
3865 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
3866 gpointer addr = (char*)vtable->data + field->offset;
3867 /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
3869 switch (field->type->type) {
3870 case MONO_TYPE_BOOLEAN:
3872 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
3876 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
3879 case MONO_TYPE_CHAR:
3881 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
3885 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
3890 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
3894 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
3899 case MONO_TYPE_STRING:
3900 case MONO_TYPE_OBJECT:
3901 case MONO_TYPE_CLASS:
3902 case MONO_TYPE_SZARRAY:
3904 case MONO_TYPE_FNPTR:
3905 case MONO_TYPE_ARRAY:
3906 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
3907 type_to_eval_stack_type (field->type, *sp);
3912 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
3913 sp [0]->type = STACK_I8;
3914 sp [0]->inst_l = *((gint64 *)addr);
3919 case MONO_TYPE_VALUETYPE:
3928 CHECK_STACK_OVF (1);
3929 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
3930 type_to_eval_stack_type (field->type, load);
3931 load->cil_code = ip;
3932 load->inst_left = ins;
3934 load->flags |= ins_flag;
3936 /* fixme: dont see the problem why this does not work */
3937 //cfg->disable_aot = TRUE;
3946 token = read32 (ip + 1);
3947 if (method->wrapper_type != MONO_WRAPPER_NONE)
3948 klass = mono_method_get_wrapper_data (method, token);
3950 klass = mono_class_get (image, token);
3951 mono_class_init (klass);
3952 n = mono_type_to_stind (&klass->byval_arg);
3953 if (n == CEE_STOBJ) {
3954 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
3956 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
3958 MONO_INST_NEW (cfg, store, n);
3959 store->cil_code = ip;
3960 store->inst_left = sp [0];
3961 store->inst_right = sp [1];
3962 store->flags |= ins_flag;
3963 MONO_ADD_INS (bblock, store);
3970 MonoInst *iargs [2];
3971 MonoInst *load, *vtoffset, *add, *val, *vstore;
3976 token = read32 (ip + 1);
3977 if (method->wrapper_type != MONO_WRAPPER_NONE)
3978 klass = mono_method_get_wrapper_data (method, token);
3980 klass = mono_class_get (image, token);
3981 mono_class_init (klass);
3983 /* much like NEWOBJ */
3984 NEW_DOMAINCONST (cfg, iargs [0]);
3985 NEW_CLASSCONST (cfg, iargs [1], klass);
3987 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3988 NEW_TEMPLOAD (cfg, load, temp);
3989 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3990 MONO_INST_NEW (cfg, add, CEE_ADD);
3991 add->inst_left = load;
3992 add->inst_right = vtoffset;
3995 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
3996 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
3997 vstore->cil_code = ip;
3998 vstore->inst_left = add;
3999 vstore->inst_right = val;
4001 if (vstore->opcode == CEE_STOBJ) {
4002 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
4004 MONO_ADD_INS (bblock, vstore);
4006 NEW_TEMPLOAD (cfg, load, temp);
4014 MONO_INST_NEW (cfg, ins, *ip);
4018 token = read32 (ip + 1);
4020 /* allocate the domainvar - becaus this is used in decompose_foreach */
4021 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot)
4022 mono_get_domainvar (cfg);
4024 if (method->wrapper_type != MONO_WRAPPER_NONE)
4025 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4027 klass = mono_class_get (image, token);
4029 mono_class_init (klass);
4030 ins->inst_newa_class = klass;
4031 ins->inst_newa_len = *sp;
4032 ins->type = STACK_OBJ;
4039 MONO_INST_NEW (cfg, ins, *ip);
4040 ins->cil_code = ip++;
4042 ins->inst_left = *sp;
4043 ins->type = STACK_PTR;
4049 klass = mono_class_get (image, read32 (ip + 1));
4050 mono_class_init (klass);
4051 NEW_LDELEMA (cfg, ins, sp, klass);
4066 case CEE_LDELEM_REF: {
4070 * ldind.x (ldelema (array, index))
4071 * ldelema does the bounds check
4075 klass = array_access_to_klass (*ip);
4076 NEW_LDELEMA (cfg, load, sp, klass);
4077 load->cil_code = ip;
4078 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
4080 ins->inst_left = load;
4082 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
4092 case CEE_STELEM_R8: {
4096 * stind.x (ldelema (array, index), val)
4097 * ldelema does the bounds check
4101 klass = array_access_to_klass (*ip);
4102 NEW_LDELEMA (cfg, load, sp, klass);
4103 load->cil_code = ip;
4104 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
4106 ins->inst_left = load;
4107 ins->inst_right = sp [2];
4109 handle_loaded_temps (cfg, bblock, stack_start, sp);
4110 MONO_ADD_INS (bblock, ins);
4111 /* FIXME: add the implicit STELEM_REF castclass */
4113 cfg->disable_ssa = TRUE;
4116 case CEE_STELEM_REF: {
4117 MonoInst *iargs [3];
4122 handle_loaded_temps (cfg, bblock, stack_start, sp);
4128 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4132 NEW_GROUP (cfg, group, sp [0], sp [1]);
4133 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
4135 ins->inst_left = group;
4136 ins->inst_right = sp [2];
4137 MONO_ADD_INS (bblock, ins);
4142 cfg->disable_ssa = TRUE;
4145 case CEE_CKFINITE: {
4146 MonoInst *store, *temp;
4149 /* this instr. can throw exceptions as side effect,
4150 * so we cant eliminate dead code which contains CKFINITE opdodes.
4151 * Spilling to memory makes sure that we always perform
4155 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
4157 ins->inst_left = sp [-1];
4158 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
4160 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4161 store->cil_code = ip;
4162 MONO_ADD_INS (bblock, store);
4164 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
4171 g_error ("opcode 0x%02x not handled", *ip);
4175 MonoClass *handle_class;
4177 CHECK_STACK_OVF (1);
4179 n = read32 (ip + 1);
4181 handle = mono_ldtoken (image, n, &handle_class);
4182 mono_class_init (handle_class);
4184 if (((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot)) {
4186 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
4188 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
4190 NEW_IMAGECONST (cfg, iargs [0], image);
4191 NEW_ICONST (cfg, iargs [1], n);
4192 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
4193 NEW_TEMPLOAD (cfg, res, temp);
4194 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4195 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
4196 MONO_ADD_INS (bblock, store);
4197 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4199 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method (image, read32 (ip + 6), NULL)) &&
4200 (cmethod->klass == mono_defaults.monotype_class->parent) &&
4201 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
4202 MonoClass *tclass = mono_class_from_mono_type (handle);
4203 mono_class_init (tclass);
4204 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
4205 ins->type = STACK_OBJ;
4206 ins->klass = cmethod->klass;
4209 NEW_PCONST (cfg, ins, handle);
4210 ins->type = STACK_VTYPE;
4211 ins->klass = handle_class;
4227 case CEE_ADD_OVF_UN:
4229 case CEE_MUL_OVF_UN:
4231 case CEE_SUB_OVF_UN:
4236 case CEE_ENDFINALLY:
4237 /* FIXME: check stack state */
4238 MONO_INST_NEW (cfg, ins, *ip);
4239 MONO_ADD_INS (bblock, ins);
4240 ins->cil_code = ip++;
4241 start_new_bblock = 1;
4246 if (*ip == CEE_LEAVE) {
4247 target = ip + 5 + (gint32)read32(ip + 1);
4249 target = ip + 2 + (signed char)(ip [1]);
4252 /* empty the stack */
4253 while (sp != stack_start) {
4254 MONO_INST_NEW (cfg, ins, CEE_POP);
4258 MONO_ADD_INS (bblock, ins);
4261 /* fixme: call fault handler ? */
4263 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
4265 for (tmp = handlers; tmp; tmp = tmp->next) {
4267 link_bblock (cfg, bblock, tblock);
4268 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
4270 ins->inst_target_bb = tblock;
4271 MONO_ADD_INS (bblock, ins);
4273 g_list_free (handlers);
4276 MONO_INST_NEW (cfg, ins, CEE_BR);
4278 MONO_ADD_INS (bblock, ins);
4279 GET_BBLOCK (cfg, bbhash, tblock, target);
4280 link_bblock (cfg, bblock, tblock);
4281 CHECK_BBLOCK (target, ip, tblock);
4282 ins->inst_target_bb = tblock;
4283 start_new_bblock = 1;
4285 if (*ip == CEE_LEAVE)
4294 MONO_INST_NEW (cfg, ins, *ip);
4296 handle_loaded_temps (cfg, bblock, stack_start, sp);
4297 MONO_ADD_INS (bblock, ins);
4298 ins->cil_code = ip++;
4299 ins->inst_i0 = sp [0];
4300 ins->inst_i1 = sp [1];
4308 /* trampoline mono specific opcodes */
4309 case MONO_CUSTOM_PREFIX: {
4311 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
4315 case CEE_MONO_FUNC1: {
4317 gpointer func = NULL;
4322 case MONO_MARSHAL_CONV_STR_LPWSTR:
4323 func = mono_string_to_utf16;
4325 case MONO_MARSHAL_CONV_LPWSTR_STR:
4326 func = mono_string_from_utf16;
4328 case MONO_MARSHAL_CONV_LPSTR_STR:
4329 func = mono_string_new_wrapper;
4331 case MONO_MARSHAL_CONV_STR_LPTSTR:
4332 case MONO_MARSHAL_CONV_STR_LPSTR:
4333 func = mono_string_to_utf8;
4335 case MONO_MARSHAL_CONV_STR_BSTR:
4336 func = mono_string_to_bstr;
4338 case MONO_MARSHAL_CONV_STR_TBSTR:
4339 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
4340 func = mono_string_to_ansibstr;
4342 case MONO_MARSHAL_CONV_SB_LPSTR:
4343 func = mono_string_builder_to_utf8;
4345 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
4346 func = mono_array_to_savearray;
4348 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
4349 func = mono_array_to_lparray;
4351 case MONO_MARSHAL_CONV_DEL_FTN:
4352 func = mono_delegate_to_ftnptr;
4354 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
4355 func = mono_marshal_string_array;
4358 g_warning ("unknown conversion %d\n", ip [2]);
4359 g_assert_not_reached ();
4362 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4363 NEW_TEMPLOAD (cfg, *sp, temp);
4367 inline_costs += 10 * num_calls++;
4370 case CEE_MONO_PROC2: {
4371 gpointer func = NULL;
4376 case MONO_MARSHAL_CONV_LPSTR_SB:
4377 func = mono_string_utf8_to_builder;
4379 case MONO_MARSHAL_FREE_ARRAY:
4380 func = mono_marshal_free_array;
4383 g_assert_not_reached ();
4386 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4388 inline_costs += 10 * num_calls++;
4391 case CEE_MONO_PROC3: {
4392 gpointer func = NULL;
4397 case MONO_MARSHAL_CONV_STR_BYVALSTR:
4398 func = mono_string_to_byvalstr;
4400 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
4401 func = mono_string_to_byvalwstr;
4404 g_assert_not_reached ();
4407 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4409 inline_costs += 10 * num_calls++;
4415 mono_emit_jit_icall (cfg, bblock, g_free, sp, ip);
4417 inline_costs += 10 * num_calls++;
4419 case CEE_MONO_LDPTR:
4420 CHECK_STACK_OVF (1);
4421 token = read32 (ip + 2);
4422 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
4426 inline_costs += 10 * num_calls++;
4428 case CEE_MONO_VTADDR:
4431 MONO_INST_NEW (cfg, ins, OP_VTADDR);
4433 ins->type = STACK_MP;
4434 ins->inst_left = *sp;
4438 case CEE_MONO_NEWOBJ: {
4439 MonoInst *iargs [2];
4441 CHECK_STACK_OVF (1);
4442 token = read32 (ip + 2);
4443 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4444 mono_class_init (klass);
4445 NEW_DOMAINCONST (cfg, iargs [0]);
4446 NEW_CLASSCONST (cfg, iargs [1], klass);
4447 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
4448 NEW_TEMPLOAD (cfg, *sp, temp);
4451 inline_costs += 10 * num_calls++;
4454 case CEE_MONO_OBJADDR:
4457 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
4459 ins->type = STACK_MP;
4460 ins->inst_left = *sp;
4464 case CEE_MONO_LDNATIVEOBJ:
4466 token = read32 (ip + 2);
4467 klass = mono_method_get_wrapper_data (method, token);
4468 g_assert (klass->valuetype);
4469 mono_class_init (klass);
4470 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
4474 case CEE_MONO_RETOBJ:
4475 g_assert (cfg->ret);
4476 g_assert (method->signature->pinvoke);
4480 token = read32 (ip + 2);
4481 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4483 NEW_RETLOADA (cfg, ins);
4484 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
4486 if (sp != stack_start)
4489 MONO_INST_NEW (cfg, ins, CEE_BR);
4491 ins->inst_target_bb = end_bblock;
4492 MONO_ADD_INS (bblock, ins);
4493 link_bblock (cfg, bblock, end_bblock);
4494 start_new_bblock = 1;
4498 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
4506 /* somewhat similar to LDTOKEN */
4507 MonoInst *addr, *vtvar;
4509 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
4511 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4512 addr->cil_code = ip;
4513 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
4515 ins->inst_left = addr;
4516 MONO_ADD_INS (bblock, ins);
4517 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4530 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
4531 MONO_INST_NEW (cfg, ins, cmp->opcode);
4533 cmp->inst_i0 = sp [0];
4534 cmp->inst_i1 = sp [1];
4538 cmp->opcode = OP_COMPARE;
4540 ins->type = STACK_I4;
4550 CHECK_STACK_OVF (1);
4551 n = read32 (ip + 2);
4552 if (method->wrapper_type != MONO_WRAPPER_NONE)
4553 cmethod = mono_method_get_wrapper_data (method, n);
4555 cmethod = mono_get_method (image, n, NULL);
4558 * We can't do this in mono_ldftn, since it is used in
4559 * the synchronized wrapper, leading to an infinite loop.
4561 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
4562 cmethod = mono_marshal_get_synchronized_wrapper (cmethod);
4565 mono_class_init (cmethod->klass);
4566 handle_loaded_temps (cfg, bblock, stack_start, sp);
4568 NEW_METHODCONST (cfg, argconst, cmethod);
4569 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
4570 NEW_TEMPLOAD (cfg, *sp, temp);
4574 inline_costs += 10 * num_calls++;
4577 case CEE_LDVIRTFTN: {
4582 n = read32 (ip + 2);
4583 if (method->wrapper_type != MONO_WRAPPER_NONE)
4584 cmethod = mono_method_get_wrapper_data (method, n);
4586 cmethod = mono_get_method (image, n, NULL);
4588 mono_class_init (cmethod->klass);
4589 handle_loaded_temps (cfg, bblock, stack_start, sp);
4593 NEW_METHODCONST (cfg, args [1], cmethod);
4594 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
4595 NEW_TEMPLOAD (cfg, *sp, temp);
4599 inline_costs += 10 * num_calls++;
4603 CHECK_STACK_OVF (1);
4604 NEW_ARGLOAD (cfg, ins, read16 (ip + 2));
4610 CHECK_STACK_OVF (1);
4611 NEW_ARGLOADA (cfg, ins, read16 (ip + 2));
4619 handle_loaded_temps (cfg, bblock, stack_start, sp);
4620 n = read16 (ip + 2);
4621 NEW_ARGSTORE (cfg, ins, n, *sp);
4623 if (ins->opcode == CEE_STOBJ) {
4624 NEW_ARGLOADA (cfg, ins, n);
4625 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4627 MONO_ADD_INS (bblock, ins);
4631 CHECK_STACK_OVF (1);
4632 NEW_LOCLOAD (cfg, ins, read16 (ip + 2));
4638 CHECK_STACK_OVF (1);
4639 NEW_LOCLOADA (cfg, ins, read16 (ip + 2));
4647 n = read16 (ip + 2);
4648 handle_loaded_temps (cfg, bblock, stack_start, sp);
4649 NEW_LOCSTORE (cfg, ins, n, *sp);
4651 if (ins->opcode == CEE_STOBJ) {
4652 NEW_LOCLOADA (cfg, ins, n);
4653 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4655 MONO_ADD_INS (bblock, ins);
4662 if (sp != stack_start)
4664 MONO_INST_NEW (cfg, ins, 256 + ip [1]);
4665 ins->inst_left = *sp;
4668 if (header->init_locals)
4669 ins->flags |= MONO_INST_INIT;
4673 /* FIXME: set init flag if locals init is set in this method */
4675 case CEE_ENDFILTER: {
4676 MonoExceptionClause *clause, *nearest;
4677 int cc, nearest_num;
4681 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
4683 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
4684 ins->inst_left = *sp;
4686 MONO_ADD_INS (bblock, ins);
4687 start_new_bblock = 1;
4691 for (cc = 0; cc < header->num_clauses; ++cc) {
4692 clause = &header->clauses [cc];
4693 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
4694 (!nearest || (clause->token_or_filter > nearest->token_or_filter))) {
4700 filter_lengths [nearest_num] = (ip - header->code) - nearest->token_or_filter;
4704 case CEE_UNALIGNED_:
4705 ins_flag |= MONO_INST_UNALIGNED;
4709 ins_flag |= MONO_INST_VOLATILE;
4713 ins_flag |= MONO_INST_TAILCALL;
4719 token = read32 (ip + 2);
4720 if (method->wrapper_type != MONO_WRAPPER_NONE)
4721 klass = mono_method_get_wrapper_data (method, token);
4723 klass = mono_class_get (image, token);
4724 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
4730 MonoInst *iargs [3];
4736 handle_loaded_temps (cfg, bblock, stack_start, sp);
4737 if (ip [1] == CEE_CPBLK) {
4738 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
4740 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
4748 /* FIXME: check we are in a catch handler */
4749 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
4750 load->cil_code = ip;
4751 MONO_INST_NEW (cfg, ins, CEE_THROW);
4752 ins->inst_left = load;
4754 MONO_ADD_INS (bblock, ins);
4756 start_new_bblock = 1;
4761 CHECK_STACK_OVF (1);
4762 token = read32 (ip + 2);
4763 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
4764 MonoType *type = mono_type_create_from_typespec (image, token);
4765 token = mono_type_size (type, &align);
4766 mono_metadata_free_type (type);
4768 MonoClass *szclass = mono_class_get (image, token);
4769 mono_class_init (szclass);
4770 token = mono_class_value_size (szclass, &align);
4772 NEW_ICONST (cfg, ins, token);
4777 case CEE_REFANYTYPE:
4778 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
4781 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
4786 g_error ("opcode 0x%02x not handled", *ip);
4789 if (start_new_bblock != 1)
4792 bblock->cil_length = ip - bblock->cil_code;
4793 bblock->next_bb = end_bblock;
4794 link_bblock (cfg, bblock, end_bblock);
4796 if (cfg->method == method && cfg->domainvar) {
4800 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
4801 call->signature = helper_sig_domain_get;
4802 call->inst.type = STACK_PTR;
4803 call->fptr = mono_domain_get;
4804 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, (MonoInst*)call);
4806 MONO_ADD_INS (init_localsbb, store);
4809 if (header->init_locals) {
4811 for (i = 0; i < header->num_locals; ++i) {
4812 int t = header->locals [i]->type;
4813 if (t == MONO_TYPE_VALUETYPE && header->locals [i]->data.klass->enumtype)
4814 t = header->locals [i]->data.klass->enum_basetype->type;
4815 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
4816 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
4817 NEW_ICONST (cfg, ins, 0);
4818 NEW_LOCSTORE (cfg, store, i, ins);
4819 MONO_ADD_INS (init_localsbb, store);
4820 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
4821 MONO_INST_NEW (cfg, ins, OP_I8CONST);
4822 ins->type = STACK_I8;
4824 NEW_LOCSTORE (cfg, store, i, ins);
4825 MONO_ADD_INS (init_localsbb, store);
4826 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
4827 MONO_INST_NEW (cfg, ins, OP_R8CONST);
4828 ins->type = STACK_R8;
4829 ins->inst_p0 = (void*)&r8_0;
4830 NEW_LOCSTORE (cfg, store, i, ins);
4831 MONO_ADD_INS (init_localsbb, store);
4832 } else if (t == MONO_TYPE_VALUETYPE) {
4833 NEW_LOCLOADA (cfg, ins, i);
4834 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (header->locals [i]), NULL, NULL);
4837 NEW_PCONST (cfg, ins, NULL);
4838 NEW_LOCSTORE (cfg, store, i, ins);
4839 MONO_ADD_INS (init_localsbb, store);
4845 /* resolve backward branches in the middle of an existing basic block */
4846 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
4848 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
4849 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
4850 if (tblock != start_bblock) {
4852 split_bblock (cfg, tblock, bblock);
4853 l = bblock->cil_code - header->code;
4854 bblock->cil_length = tblock->cil_length - l;
4855 tblock->cil_length = l;
4857 g_print ("recheck failed.\n");
4861 /* we compute regions here, because the length of filter clauses is not known in advance.
4862 * It is computed in the CEE_ENDFILTER case in the above switch statement*/
4863 if (cfg->method == method) {
4865 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
4866 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
4867 if (cfg->verbose_level > 2)
4868 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
4871 g_hash_table_destroy (bbhash);
4874 dont_inline = g_list_remove (dont_inline, method);
4875 return inline_costs;
4878 if (cfg->method != method)
4879 g_hash_table_destroy (bbhash);
4880 dont_inline = g_list_remove (dont_inline, method);
4884 if (cfg->method != method)
4885 g_hash_table_destroy (bbhash);
4886 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
4887 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
4888 dont_inline = g_list_remove (dont_inline, method);
4893 mono_print_tree (MonoInst *tree) {
4899 arity = mono_burg_arity [tree->opcode];
4901 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
4903 switch (tree->opcode) {
4905 printf ("[%d]", tree->inst_c0);
4908 printf ("[%lld]", tree->inst_l);
4911 printf ("[%f]", *(double*)tree->inst_p0);
4914 printf ("[%f]", *(float*)tree->inst_p0);
4918 printf ("[%d]", tree->inst_c0);
4921 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
4924 printf ("[%s]", mono_arch_regname (tree->dreg));
4927 printf ("[%s]", tree->inst_newa_class->name);
4928 mono_print_tree (tree->inst_newa_len);
4939 case OP_VOIDCALLVIRT: {
4940 MonoCallInst *call = (MonoCallInst*)tree;
4942 printf ("[%s]", call->method->name);
4947 printf ("[%d (", tree->inst_c0);
4948 for (i = 0; i < tree->inst_phi_args [0]; i++) {
4951 printf ("%d", tree->inst_phi_args [i + 1]);
4963 printf ("[B%d]", tree->inst_target_bb->block_num);
4973 case OP_VOIDCALL_REG:
4974 mono_print_tree (tree->inst_left);
4986 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
4987 mono_print_tree (tree->inst_left);
4991 mono_print_tree (tree->inst_left);
4993 mono_print_tree (tree->inst_right);
5003 create_helper_signature (void)
5005 /* FIXME: set call conv */
5006 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
5007 helper_sig_newarr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5008 helper_sig_newarr->params [0] = helper_sig_newarr->params [1] = &mono_defaults.int_class->byval_arg;
5009 helper_sig_newarr->ret = &mono_defaults.object_class->byval_arg;
5010 helper_sig_newarr->params [2] = &mono_defaults.int32_class->byval_arg;
5011 helper_sig_newarr->pinvoke = 1;
5013 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
5014 helper_sig_newarr_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5015 helper_sig_newarr_specific->params [0] = &mono_defaults.int_class->byval_arg;
5016 helper_sig_newarr_specific->params [1] = &mono_defaults.int32_class->byval_arg;
5017 helper_sig_newarr_specific->ret = &mono_defaults.object_class->byval_arg;
5018 helper_sig_newarr_specific->pinvoke = 1;
5020 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
5021 helper_sig_object_new = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5022 helper_sig_object_new->params [0] = helper_sig_object_new->params [1] = &mono_defaults.int_class->byval_arg;
5023 helper_sig_object_new->ret = &mono_defaults.object_class->byval_arg;
5024 helper_sig_object_new->pinvoke = 1;
5026 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
5027 helper_sig_object_new_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5028 helper_sig_object_new_specific->params [0] = &mono_defaults.int_class->byval_arg;
5029 helper_sig_object_new_specific->ret = &mono_defaults.object_class->byval_arg;
5030 helper_sig_object_new_specific->pinvoke = 1;
5032 /* void* mono_method_compile (MonoMethod*) */
5033 helper_sig_compile = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5034 helper_sig_compile->params [0] = helper_sig_compile->ret = &mono_defaults.int_class->byval_arg;
5035 helper_sig_compile->pinvoke = 1;
5037 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
5038 helper_sig_compile_virt = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5039 helper_sig_compile_virt->params [0] = &mono_defaults.object_class->byval_arg;
5040 helper_sig_compile_virt->params [1] = helper_sig_compile_virt->ret = &mono_defaults.int_class->byval_arg;
5041 helper_sig_compile_virt->pinvoke = 1;
5043 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
5044 helper_sig_ldstr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5045 helper_sig_ldstr->params [0] = helper_sig_ldstr->params [1] = &mono_defaults.int_class->byval_arg;
5046 helper_sig_ldstr->params [2] = &mono_defaults.int32_class->byval_arg;
5047 helper_sig_ldstr->ret = &mono_defaults.object_class->byval_arg;
5048 helper_sig_ldstr->pinvoke = 1;
5050 /* MonoDomain *mono_domain_get (void) */
5051 helper_sig_domain_get = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5052 helper_sig_domain_get->ret = &mono_defaults.int_class->byval_arg;
5053 helper_sig_domain_get->pinvoke = 1;
5055 /* void* stelem_ref (MonoArray *, int index, MonoObject *) */
5056 helper_sig_stelem_ref = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5057 helper_sig_stelem_ref->params [0] = &mono_defaults.array_class->byval_arg;
5058 helper_sig_stelem_ref->params [1] = &mono_defaults.int32_class->byval_arg;
5059 helper_sig_stelem_ref->params [2] = &mono_defaults.object_class->byval_arg;
5060 helper_sig_stelem_ref->ret = &mono_defaults.void_class->byval_arg;
5061 helper_sig_stelem_ref->pinvoke = 1;
5063 /* long amethod (long, long) */
5064 helper_sig_long_long_long = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5065 helper_sig_long_long_long->params [0] = helper_sig_long_long_long->params [1] =
5066 &mono_defaults.int64_class->byval_arg;
5067 helper_sig_long_long_long->ret = &mono_defaults.int64_class->byval_arg;
5068 helper_sig_long_long_long->pinvoke = 1;
5070 /* object amethod (intptr) */
5071 helper_sig_obj_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5072 helper_sig_obj_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5073 helper_sig_obj_ptr->ret = &mono_defaults.object_class->byval_arg;
5074 helper_sig_obj_ptr->pinvoke = 1;
5076 /* void amethod (intptr) */
5077 helper_sig_void_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5078 helper_sig_void_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5079 helper_sig_void_ptr->ret = &mono_defaults.void_class->byval_arg;
5080 helper_sig_void_ptr->pinvoke = 1;
5082 /* void amethod (MonoObject *obj) */
5083 helper_sig_void_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5084 helper_sig_void_obj->params [0] = &mono_defaults.object_class->byval_arg;
5085 helper_sig_void_obj->ret = &mono_defaults.void_class->byval_arg;
5086 helper_sig_void_obj->pinvoke = 1;
5088 /* intptr amethod (void) */
5089 helper_sig_ptr_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5090 helper_sig_ptr_void->ret = &mono_defaults.int_class->byval_arg;
5091 helper_sig_ptr_void->pinvoke = 1;
5093 /* void amethod (intptr, intptr) */
5094 helper_sig_void_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5095 helper_sig_void_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5096 helper_sig_void_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5097 helper_sig_void_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
5098 helper_sig_void_ptr_ptr->pinvoke = 1;
5100 /* void amethod (intptr, intptr, intptr) */
5101 helper_sig_void_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5102 helper_sig_void_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5103 helper_sig_void_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5104 helper_sig_void_ptr_ptr_ptr->params [2] = &mono_defaults.int_class->byval_arg;
5105 helper_sig_void_ptr_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
5106 helper_sig_void_ptr_ptr_ptr->pinvoke = 1;
5108 /* intptr amethod (intptr, intptr) */
5109 helper_sig_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5110 helper_sig_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5111 helper_sig_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5112 helper_sig_ptr_ptr_ptr->ret = &mono_defaults.int_class->byval_arg;
5113 helper_sig_ptr_ptr_ptr->pinvoke = 1;
5115 /* IntPtr amethod (object) */
5116 helper_sig_ptr_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5117 helper_sig_ptr_obj->params [0] = &mono_defaults.object_class->byval_arg;
5118 helper_sig_ptr_obj->ret = &mono_defaults.int_class->byval_arg;
5119 helper_sig_ptr_obj->pinvoke = 1;
5121 /* IntPtr amethod (int) */
5122 helper_sig_ptr_int = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5123 helper_sig_ptr_int->params [0] = &mono_defaults.int32_class->byval_arg;
5124 helper_sig_ptr_int->ret = &mono_defaults.int_class->byval_arg;
5125 helper_sig_ptr_int->pinvoke = 1;
5127 /* long amethod (long, guint32) */
5128 helper_sig_long_long_int = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5129 helper_sig_long_long_int->params [0] = &mono_defaults.int64_class->byval_arg;
5130 helper_sig_long_long_int->params [1] = &mono_defaults.int32_class->byval_arg;
5131 helper_sig_long_long_int->ret = &mono_defaults.int64_class->byval_arg;
5132 helper_sig_long_long_int->pinvoke = 1;
5134 /* ulong amethod (double) */
5135 helper_sig_ulong_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5136 helper_sig_ulong_double->params [0] = &mono_defaults.double_class->byval_arg;
5137 helper_sig_ulong_double->ret = &mono_defaults.uint64_class->byval_arg;
5138 helper_sig_ulong_double->pinvoke = 1;
5140 /* long amethod (double) */
5141 helper_sig_long_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5142 helper_sig_long_double->params [0] = &mono_defaults.double_class->byval_arg;
5143 helper_sig_long_double->ret = &mono_defaults.int64_class->byval_arg;
5144 helper_sig_long_double->pinvoke = 1;
5146 /* uint amethod (double) */
5147 helper_sig_uint_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5148 helper_sig_uint_double->params [0] = &mono_defaults.double_class->byval_arg;
5149 helper_sig_uint_double->ret = &mono_defaults.uint32_class->byval_arg;
5150 helper_sig_uint_double->pinvoke = 1;
5152 /* int amethod (double) */
5153 helper_sig_int_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5154 helper_sig_int_double->params [0] = &mono_defaults.double_class->byval_arg;
5155 helper_sig_int_double->ret = &mono_defaults.int32_class->byval_arg;
5156 helper_sig_int_double->pinvoke = 1;
5158 /* void initobj (intptr, int size) */
5159 helper_sig_initobj = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5160 helper_sig_initobj->params [0] = &mono_defaults.int_class->byval_arg;
5161 helper_sig_initobj->params [1] = &mono_defaults.int32_class->byval_arg;
5162 helper_sig_initobj->ret = &mono_defaults.void_class->byval_arg;
5163 helper_sig_initobj->pinvoke = 1;
5165 /* void memcpy (intptr, intptr, int size) */
5166 helper_sig_memcpy = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5167 helper_sig_memcpy->params [0] = &mono_defaults.int_class->byval_arg;
5168 helper_sig_memcpy->params [1] = &mono_defaults.int_class->byval_arg;
5169 helper_sig_memcpy->params [2] = &mono_defaults.int32_class->byval_arg;
5170 helper_sig_memcpy->ret = &mono_defaults.void_class->byval_arg;
5171 helper_sig_memcpy->pinvoke = 1;
5173 /* void memset (intptr, int val, int size) */
5174 helper_sig_memset = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5175 helper_sig_memset->params [0] = &mono_defaults.int_class->byval_arg;
5176 helper_sig_memset->params [1] = &mono_defaults.int32_class->byval_arg;
5177 helper_sig_memset->params [2] = &mono_defaults.int32_class->byval_arg;
5178 helper_sig_memset->ret = &mono_defaults.void_class->byval_arg;
5179 helper_sig_memset->pinvoke = 1;
5182 static GHashTable *jit_icall_hash_name = NULL;
5183 static GHashTable *jit_icall_hash_addr = NULL;
5186 mono_find_jit_icall_by_name (const char *name)
5188 g_assert (jit_icall_hash_name);
5190 //printf ("lookup addr %s %p\n", name, g_hash_table_lookup (jit_icall_hash_name, name));
5191 return g_hash_table_lookup (jit_icall_hash_name, name);
5195 mono_find_jit_icall_by_addr (gconstpointer addr)
5197 g_assert (jit_icall_hash_addr);
5199 return g_hash_table_lookup (jit_icall_hash_addr, (gpointer)addr);
5203 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
5206 MonoMethod *wrapper;
5208 if (callinfo->wrapper)
5209 return callinfo->wrapper;
5210 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
5211 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
5212 callinfo->wrapper = mono_jit_compile_method (wrapper);
5214 return callinfo->wrapper;
5218 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save)
5220 MonoJitICallInfo *info;
5225 if (!jit_icall_hash_name) {
5226 jit_icall_hash_name = g_hash_table_new (g_str_hash, g_str_equal);
5227 jit_icall_hash_addr = g_hash_table_new (NULL, NULL);
5230 if (g_hash_table_lookup (jit_icall_hash_name, name)) {
5231 g_warning ("jit icall already defined \"%s\"\n", name);
5232 g_assert_not_reached ();
5235 info = g_new (MonoJitICallInfo, 1);
5242 #ifdef MONO_USE_EXC_TABLES
5243 || mono_arch_has_unwind_info (func)
5246 info->wrapper = func;
5248 info->wrapper = NULL;
5249 mono_icall_get_wrapper (info);
5252 g_hash_table_insert (jit_icall_hash_name, (gpointer)info->name, info);
5253 g_hash_table_insert (jit_icall_hash_addr, (gpointer)func, info);
5254 if (func != info->wrapper)
5255 g_hash_table_insert (jit_icall_hash_addr, (gpointer)info->wrapper, info);
5260 static GHashTable *emul_opcode_hash = NULL;
5262 static MonoJitICallInfo *
5263 mono_find_jit_opcode_emulation (int opcode)
5265 if (emul_opcode_hash)
5266 return g_hash_table_lookup (emul_opcode_hash, (gpointer)opcode);
5272 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func, gboolean no_throw)
5274 MonoJitICallInfo *info;
5276 if (!emul_opcode_hash)
5277 emul_opcode_hash = g_hash_table_new (NULL, NULL);
5279 g_assert (!sig->hasthis);
5280 g_assert (sig->param_count < 3);
5282 info = mono_register_jit_icall (func, name, sig, no_throw);
5284 g_hash_table_insert (emul_opcode_hash, (gpointer)opcode, info);
5288 decompose_foreach (MonoInst *tree, gpointer data)
5290 static MonoJitICallInfo *newarr_info = NULL;
5291 static MonoJitICallInfo *newarr_specific_info = NULL;
5292 MonoJitICallInfo *info;
5294 switch (tree->opcode) {
5296 MonoCompile *cfg = data;
5297 MonoInst *iargs [3];
5300 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
5301 g_assert (newarr_info);
5302 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
5303 g_assert (newarr_specific_info);
5306 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
5307 NEW_DOMAINCONST (cfg, iargs [0]);
5308 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
5309 iargs [2] = tree->inst_newa_len;
5314 MonoVTable *vtable = mono_class_vtable (cfg->domain,
5315 mono_array_class_get (&tree->inst_newa_class->byval_arg, 1));
5317 NEW_PCONST (cfg, iargs [0], vtable);
5318 iargs [1] = tree->inst_newa_len;
5320 info = newarr_specific_info;
5323 mono_emulate_opcode (cfg, tree, iargs, info);
5333 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
5335 switch (mono_burg_arity [tree->opcode]) {
5338 mono_inst_foreach (tree->inst_left, func, data);
5341 mono_inst_foreach (tree->inst_left, func, data);
5342 mono_inst_foreach (tree->inst_right, func, data);
5345 g_assert_not_reached ();
5352 mono_print_bb_code (MonoBasicBlock *bb) {
5354 MonoInst *c = bb->code;
5356 mono_print_tree (c);
5365 print_dfn (MonoCompile *cfg) {
5370 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
5372 for (i = 0; i < cfg->num_bblocks; ++i) {
5373 bb = cfg->bblocks [i];
5375 char* code1, *code2;
5376 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
5377 if (bb->last_ins->cil_code)
5378 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
5380 code2 = g_strdup ("");
5382 code1 [strlen (code1) - 1] = 0;
5383 code = g_strdup_printf ("%s -> %s", code1, code2);
5387 code = g_strdup ("\n");
5388 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
5390 MonoInst *c = bb->code;
5392 mono_print_tree (c);
5400 g_print ("\tprev:");
5401 for (j = 0; j < bb->in_count; ++j) {
5402 g_print (" BB%d", bb->in_bb [j]->block_num);
5404 g_print ("\t\tsucc:");
5405 for (j = 0; j < bb->out_count; ++j) {
5406 g_print (" BB%d", bb->out_bb [j]->block_num);
5408 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
5411 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
5414 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
5416 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
5424 * returns the offset used by spillvar. It allocates a new
5425 * spill variable if necessary.
5428 mono_spillvar_offset (MonoCompile *cfg, int spillvar)
5430 MonoSpillInfo **si, *info;
5433 si = &cfg->spill_info;
5435 while (i <= spillvar) {
5438 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
5440 cfg->stack_offset -= sizeof (gpointer);
5441 info->offset = cfg->stack_offset;
5445 return (*si)->offset;
5451 g_assert_not_reached ();
5456 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
5460 g_assert (bb->code);
5461 bb->last_ins->next = inst;
5462 bb->last_ins = inst;
5464 bb->last_ins = bb->code = inst;
5469 mono_destroy_compile (MonoCompile *cfg)
5471 //mono_mempool_stats (cfg->mempool);
5472 g_hash_table_destroy (cfg->bb_hash);
5474 mono_regstate_free (cfg->rs);
5475 mono_mempool_destroy (cfg->mempool);
5476 g_list_free (cfg->ldstr_list);
5478 g_free (cfg->varinfo);
5484 mono_get_lmf_addr (void)
5486 MonoJitTlsData *jit_tls;
5488 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
5489 return &jit_tls->lmf;
5491 g_assert_not_reached ();
5496 * mono_thread_abort:
5497 * @obj: exception object
5499 * abort the thread, print exception information and stack trace
5502 mono_thread_abort (MonoObject *obj)
5504 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
5512 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
5514 MonoJitTlsData *jit_tls;
5517 jit_tls = g_new0 (MonoJitTlsData, 1);
5519 TlsSetValue (mono_jit_tls_id, jit_tls);
5521 jit_tls->abort_func = mono_thread_abort;
5522 jit_tls->end_of_stack = stack_start;
5524 lmf = g_new0 (MonoLMF, 1);
5530 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
5533 mono_thread_abort_dummy (MonoObject *obj)
5535 if (mono_thread_attach_aborted_cb)
5536 mono_thread_attach_aborted_cb (obj);
5538 mono_thread_abort (obj);
5542 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
5544 MonoJitTlsData *jit_tls;
5547 jit_tls = g_new0 (MonoJitTlsData, 1);
5549 TlsSetValue (mono_jit_tls_id, jit_tls);
5551 jit_tls->abort_func = mono_thread_abort_dummy;
5552 jit_tls->end_of_stack = stack_start;
5554 lmf = g_new0 (MonoLMF, 1);
5561 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
5563 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
5567 ji->data.target = target;
5568 ji->next = cfg->patch_info;
5570 cfg->patch_info = ji;
5574 mono_remove_patch_info (MonoCompile *cfg, int ip)
5576 MonoJumpInfo **ji = &cfg->patch_info;
5579 if ((*ji)->ip.i == ip)
5582 ji = &((*ji)->next);
5587 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
5588 MonoJitICallInfo *info;
5590 switch (mono_burg_arity [tree->opcode]) {
5593 dec_foreach (tree->inst_left, cfg);
5595 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
5596 MonoInst *iargs [2];
5598 iargs [0] = tree->inst_left;
5600 mono_emulate_opcode (cfg, tree, iargs, info);
5606 if (tree->opcode == OP_LMUL
5607 && (cfg->opt & MONO_OPT_INTRINS)
5608 && (tree->inst_left->opcode == CEE_CONV_I8
5609 || tree->inst_left->opcode == CEE_CONV_U8)
5610 && tree->inst_left->inst_left->type == STACK_I4
5611 && (tree->inst_right->opcode == CEE_CONV_I8
5612 || tree->inst_right->opcode == CEE_CONV_U8)
5613 && tree->inst_right->inst_left->type == STACK_I4) {
5614 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
5615 tree->inst_left = tree->inst_left->inst_left;
5616 tree->inst_right = tree->inst_right->inst_left;
5617 dec_foreach (tree, cfg);
5618 } else if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
5619 MonoInst *iargs [2];
5621 iargs [0] = tree->inst_i0;
5622 iargs [1] = tree->inst_i1;
5624 mono_emulate_opcode (cfg, tree, iargs, info);
5626 dec_foreach (iargs [0], cfg);
5627 dec_foreach (iargs [1], cfg);
5630 dec_foreach (tree->inst_left, cfg);
5631 dec_foreach (tree->inst_right, cfg);
5635 g_assert_not_reached ();
5637 decompose_foreach (tree, cfg);
5641 decompose_pass (MonoCompile *cfg) {
5644 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5647 cfg->prev_ins = NULL;
5648 for (tree = cfg->cbb->code; tree; tree = tree->next) {
5649 dec_foreach (tree, cfg);
5650 cfg->prev_ins = tree;
5656 nullify_basic_block (MonoBasicBlock *bb)
5663 bb->code = bb->last_ins = NULL;
5664 bb->cil_code = NULL;
5668 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
5672 for (i = 0; i < bb->out_count; i++) {
5673 MonoBasicBlock *ob = bb->out_bb [i];
5676 if (bb->out_count > 1) {
5677 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
5681 bb->out_bb [i] = repl;
5688 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
5692 for (i = 0; i < bb->in_count; i++) {
5693 MonoBasicBlock *ib = bb->in_bb [i];
5696 if (bb->in_count > 1) {
5697 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
5701 bb->in_bb [i] = repl;
5708 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
5712 for (i = 0; i < bb->out_count; i++) {
5713 MonoBasicBlock *ob = bb->out_bb [i];
5714 for (j = 0; j < ob->in_count; j++) {
5715 if (ob->in_bb [j] == orig) {
5716 ob->in_bb [j] = repl;
5725 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
5727 bb->out_count = bbn->out_count;
5728 bb->out_bb = bbn->out_bb;
5730 replace_basic_block (bb, bbn, bb);
5734 bb->last_ins->next = bbn->code;
5735 bb->last_ins = bbn->last_ins;
5738 bb->code = bbn->code;
5739 bb->last_ins = bbn->last_ins;
5741 bb->next_bb = bbn->next_bb;
5742 nullify_basic_block (bbn);
5746 optimize_branches (MonoCompile *cfg) {
5747 int i, changed = FALSE;
5748 MonoBasicBlock *bb, *bbn;
5753 /* we skip the entry block (exit is handled specially instead ) */
5754 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
5756 /* dont touch code inside exception clauses */
5757 if (bb->region != -1)
5760 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
5761 if (cfg->verbose_level > 2)
5762 g_print ("nullify block triggered %d\n", bbn->block_num);
5764 bb->next_bb = bbn->next_bb;
5766 for (i = 0; i < bbn->out_count; i++)
5767 replace_in_block (bbn->out_bb [i], bbn, NULL);
5769 nullify_basic_block (bbn);
5773 if (bb->out_count == 1) {
5774 bbn = bb->out_bb [0];
5776 /* conditional branches where true and false targets are the same can be also replaced with CEE_BR */
5777 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
5778 bb->last_ins->opcode = CEE_BR;
5779 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
5781 if (cfg->verbose_level > 2)
5782 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
5785 if (bb->region == bbn->region && bb->next_bb == bbn) {
5786 /* the block are in sequence anyway ... */
5788 /* branches to the following block can be removed */
5789 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
5790 bb->last_ins->opcode = CEE_NOP;
5792 if (cfg->verbose_level > 2)
5793 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
5796 if (bbn->in_count == 1) {
5798 if (bbn != cfg->bb_exit) {
5799 if (cfg->verbose_level > 2)
5800 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
5801 merge_basic_blocks (bb, bbn);
5805 //mono_print_bb_code (bb);
5815 /* we skip the entry block (exit is handled specially instead ) */
5816 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
5818 /* dont touch code inside exception clauses */
5819 if (bb->region != -1)
5822 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
5823 if (cfg->verbose_level > 2) {
5824 g_print ("nullify block triggered %d\n", bbn->block_num);
5826 bb->next_bb = bbn->next_bb;
5828 for (i = 0; i < bbn->out_count; i++)
5829 replace_in_block (bbn->out_bb [i], bbn, NULL);
5831 nullify_basic_block (bbn);
5837 if (bb->out_count == 1) {
5838 bbn = bb->out_bb [0];
5840 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
5841 bbn = bb->last_ins->inst_target_bb;
5842 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
5843 bbn->code->inst_target_bb->region == bb->region) {
5845 if (cfg->verbose_level > 2)
5846 g_print ("in %s branch to branch triggered %d -> %d\n", cfg->method->name,
5847 bb->block_num, bbn->block_num);
5849 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
5850 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
5855 } else if (bb->out_count == 2) {
5856 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
5857 bbn = bb->last_ins->inst_true_bb;
5858 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
5859 bbn->code->inst_target_bb->region == bb->region) {
5860 if (cfg->verbose_level > 2)
5861 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
5862 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
5865 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
5867 replace_in_block (bbn, bb, NULL);
5869 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
5870 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
5872 link_bblock (cfg, bb, bbn->code->inst_target_bb);
5878 bbn = bb->last_ins->inst_false_bb;
5879 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
5880 bbn->code->inst_target_bb->region == bb->region) {
5881 if (cfg->verbose_level > 2)
5882 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
5883 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
5886 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
5888 replace_in_block (bbn, bb, NULL);
5890 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
5891 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
5893 link_bblock (cfg, bb, bbn->code->inst_target_bb);
5906 mono_compile_create_vars (MonoCompile *cfg)
5908 MonoMethodSignature *sig;
5909 MonoMethodHeader *header;
5912 header = ((MonoMethodNormal *)cfg->method)->header;
5914 sig = cfg->method->signature;
5916 if (!MONO_TYPE_IS_VOID (sig->ret)) {
5917 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
5918 cfg->ret->opcode = OP_RETARG;
5919 cfg->ret->inst_vtype = sig->ret;
5920 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
5924 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
5926 for (i = 0; i < sig->param_count; ++i)
5927 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
5929 cfg->locals_start = cfg->num_varinfo;
5931 for (i = 0; i < header->num_locals; ++i)
5932 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
5937 mono_print_code (MonoCompile *cfg)
5941 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5942 MonoInst *tree = bb->code;
5947 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
5949 for (; tree; tree = tree->next) {
5950 mono_print_tree (tree);
5955 bb->last_ins->next = NULL;
5960 extern const char * const mono_burg_rule_string [];
5963 emit_state (MonoCompile *cfg, MBState *state, int goal)
5966 int ern = mono_burg_rule (state, goal);
5967 const guint16 *nts = mono_burg_nts [ern];
5970 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
5974 // state->reg1 = state->reg2; /* chain rule */
5976 state->reg1 = mono_regstate_next_int (cfg->rs);
5977 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
5980 state->reg1 = mono_regstate_next_int (cfg->rs);
5981 state->reg2 = mono_regstate_next_int (cfg->rs);
5984 state->reg1 = mono_regstate_next_float (cfg->rs);
5991 mono_burg_kids (state, ern, kids);
5993 emit_state (cfg, kids [0], nts [0]);
5995 emit_state (cfg, kids [1], nts [1]);
5997 g_assert (!nts [3]);
5998 emit_state (cfg, kids [2], nts [2]);
6003 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
6004 if ((emit = mono_burg_func [ern]))
6005 emit (state, state->tree, cfg);
6008 #define DEBUG_SELECTION
6011 mini_select_instructions (MonoCompile *cfg)
6013 static int reverse_map [] = {
6014 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
6015 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
6017 static int reverse_fmap [] = {
6018 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
6019 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
6021 static int reverse_lmap [] = {
6022 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
6023 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
6028 cfg->state_pool = mono_mempool_new ();
6029 cfg->rs = mono_regstate_new ();
6031 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6032 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode) &&
6033 bb->next_bb != bb->last_ins->inst_false_bb) {
6035 if (bb->next_bb == bb->last_ins->inst_true_bb) {
6036 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
6037 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
6038 bb->last_ins->inst_false_bb = tmp;
6040 if (bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
6041 bb->last_ins->opcode = reverse_map [bb->last_ins->opcode - CEE_BEQ];
6042 } else if (bb->last_ins->opcode >= OP_FBEQ && bb->last_ins->opcode <= OP_FBLT_UN) {
6043 bb->last_ins->opcode = reverse_fmap [bb->last_ins->opcode - OP_FBEQ];
6044 } else if (bb->last_ins->opcode >= OP_LBEQ && bb->last_ins->opcode <= OP_LBLT_UN) {
6045 bb->last_ins->opcode = reverse_lmap [bb->last_ins->opcode - OP_LBEQ];
6048 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
6049 inst->opcode = CEE_BR;
6050 inst->inst_target_bb = bb->last_ins->inst_false_bb;
6051 mono_bblock_add_inst (bb, inst);
6056 #ifdef DEBUG_SELECTION
6057 if (cfg->verbose_level >= 4) {
6058 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6059 MonoInst *tree = bb->code;
6060 g_print ("DUMP BLOCK %d:\n", bb->block_num);
6063 for (; tree; tree = tree->next) {
6064 mono_print_tree (tree);
6071 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6072 MonoInst *tree = bb->code, *next;
6078 bb->last_ins = NULL;
6081 mono_regstate_reset (cfg->rs);
6083 #ifdef DEBUG_SELECTION
6084 if (cfg->verbose_level >= 3)
6085 g_print ("LABEL BLOCK %d:\n", bb->block_num);
6087 for (; tree; tree = next) {
6089 #ifdef DEBUG_SELECTION
6090 if (cfg->verbose_level >= 3) {
6091 mono_print_tree (tree);
6096 if (!(mbstate = mono_burg_label (tree, cfg))) {
6097 g_warning ("unabled to label tree %p", tree);
6098 mono_print_tree (tree);
6100 g_assert_not_reached ();
6102 emit_state (cfg, mbstate, MB_NTERM_stmt);
6104 bb->max_ireg = cfg->rs->next_vireg;
6105 bb->max_freg = cfg->rs->next_vfreg;
6108 bb->last_ins->next = NULL;
6110 mono_mempool_empty (cfg->state_pool);
6112 mono_mempool_destroy (cfg->state_pool);
6116 mono_codegen (MonoCompile *cfg)
6118 MonoJumpInfo *patch_info;
6120 int i, max_epilog_size;
6123 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6124 cfg->spill_count = 0;
6125 /* we reuse dfn here */
6126 /* bb->dfn = bb_count++; */
6127 mono_arch_local_regalloc (cfg, bb);
6130 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
6131 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
6133 code = mono_arch_emit_prolog (cfg);
6135 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6136 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
6138 cfg->code_len = code - cfg->native_code;
6139 cfg->prolog_end = cfg->code_len;
6141 mono_debug_open_method (cfg);
6143 /* emit code all basic blocks */
6144 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6145 bb->native_offset = cfg->code_len;
6146 mono_arch_output_basic_block (cfg, bb);
6148 cfg->bb_exit->native_offset = cfg->code_len;
6150 code = cfg->native_code + cfg->code_len;
6152 max_epilog_size = mono_arch_max_epilog_size (cfg);
6154 /* we always allocate code in cfg->domain->code_mp to increase locality */
6155 cfg->code_size = cfg->code_len + max_epilog_size;
6156 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
6157 code = mono_mempool_alloc (cfg->domain->code_mp, cfg->code_size);
6158 memcpy (code, cfg->native_code, cfg->code_len);
6159 g_free (cfg->native_code);
6160 cfg->native_code = code;
6161 code = cfg->native_code + cfg->code_len;
6163 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
6165 cfg->epilog_begin = cfg->code_len;
6167 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6168 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
6170 cfg->code_len = code - cfg->native_code;
6172 mono_arch_emit_epilog (cfg);
6174 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
6175 switch (patch_info->type) {
6176 case MONO_PATCH_INFO_ABS: {
6177 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
6179 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
6180 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
6181 patch_info->data.name = info->name;
6185 case MONO_PATCH_INFO_SWITCH: {
6186 gpointer *table = g_new (gpointer, patch_info->table_size);
6187 patch_info->ip.i = patch_info->ip.label->inst_c0;
6188 for (i = 0; i < patch_info->table_size; i++) {
6189 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
6191 patch_info->data.target = table;
6200 if (cfg->verbose_level > 1)
6201 g_print ("Method %s::%s emmitted at %p to %p\n", cfg->method->klass->name,
6202 cfg->method->name, cfg->native_code, cfg->native_code + cfg->code_len);
6204 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info);
6206 mono_debug_close_method (cfg);
6210 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
6215 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
6216 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
6218 if (cp->opcode == OP_ICONST) {
6219 if (cfg->opt & MONO_OPT_CONSPROP) {
6220 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
6224 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
6225 if (cfg->opt & MONO_OPT_COPYPROP) {
6226 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
6232 arity = mono_burg_arity [tree->opcode];
6235 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
6236 if (cfg->opt & MONO_OPT_CFOLD)
6237 mono_constant_fold_inst (tree, NULL);
6239 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
6240 if (cfg->opt & MONO_OPT_CFOLD)
6241 mono_constant_fold_inst (tree, NULL);
6243 mono_constant_fold_inst (tree, NULL);
6249 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
6253 switch (tree->opcode) {
6263 if (tree->ssa_op == MONO_SSA_NOP) {
6264 memset (acp, 0, sizeof (MonoInst *) * acp_size);
6281 case OP_VOIDCALL_REG:
6282 case OP_VOIDCALLVIRT:
6284 MonoCallInst *call = (MonoCallInst *)tree;
6285 MonoMethodSignature *sig = call->signature;
6286 int i, byref = FALSE;
6288 for (i = 0; i < sig->param_count; i++) {
6289 if (sig->params [i]->byref) {
6296 memset (acp, 0, sizeof (MonoInst *) * acp_size);
6304 arity = mono_burg_arity [tree->opcode];
6310 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
6313 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
6314 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
6317 g_assert_not_reached ();
6322 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
6324 MonoInst *tree = bb->code;
6330 for (; tree; tree = tree->next) {
6332 mono_cprop_copy_values (cfg, tree, acp);
6334 mono_cprop_invalidate_values (tree, acp, acp_size);
6336 if (tree->ssa_op == MONO_SSA_STORE &&
6337 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
6338 MonoInst *i1 = tree->inst_i1;
6340 acp [tree->inst_i0->inst_c0] = NULL;
6342 for (i = 0; i < acp_size; i++) {
6343 if (acp [i] && acp [i]->opcode != OP_ICONST &&
6344 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
6349 if (i1->opcode == OP_ICONST) {
6350 acp [tree->inst_i0->inst_c0] = i1;
6351 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
6353 if (i1->ssa_op == MONO_SSA_LOAD &&
6354 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
6355 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
6356 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
6357 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
6362 if (tree->opcode == CEE_BEQ) {
6363 g_assert (tree->inst_i0->opcode == OP_COMPARE);
6364 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
6365 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
6367 tree->opcode = CEE_BR;
6368 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
6369 tree->inst_target_bb = tree->inst_true_bb;
6371 tree->inst_target_bb = tree->inst_false_bb;
6380 mono_local_cprop (MonoCompile *cfg)
6385 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
6387 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6388 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
6389 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
6394 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, int parts)
6396 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
6397 guint8 *ip = (guint8 *)header->code;
6400 int dfn = 0, i, code_size_ratio;
6402 mono_jit_stats.methods_compiled++;
6403 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
6404 mono_profiler_method_jit (method);
6406 cfg = g_new0 (MonoCompile, 1);
6407 cfg->method = method;
6408 cfg->mempool = mono_mempool_new ();
6410 cfg->prof_options = mono_profiler_get_events ();
6411 cfg->bb_hash = g_hash_table_new (g_direct_hash, NULL);
6412 cfg->domain = domain;
6413 cfg->verbose_level = mini_verbose;
6414 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
6415 ((MonoMethodNormal *)method)->header->max_stack);
6418 * create MonoInst* which represents arguments and local variables
6420 mono_compile_create_vars (cfg);
6422 if (cfg->verbose_level > 2)
6423 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
6425 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
6426 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
6427 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
6428 mono_destroy_compile (cfg);
6432 mono_jit_stats.basic_blocks += cfg->num_bblocks;
6433 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
6435 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
6437 /* Depth-first ordering on basic blocks */
6438 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
6440 if (cfg->opt & MONO_OPT_BRANCH)
6441 optimize_branches (cfg);
6443 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
6444 if (cfg->num_bblocks != dfn + 1) {
6447 cfg->num_bblocks = dfn + 1;
6449 if (!header->clauses) {
6450 /* remove unreachable code, because the code in them may be
6451 * inconsistent (access to dead variables for example) */
6452 for (bb = cfg->bb_entry; bb;) {
6453 MonoBasicBlock *bbn = bb->next_bb;
6455 if (bbn && bbn->region == -1 && !bbn->dfn) {
6456 if (cfg->verbose_level > 1)
6457 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
6458 bb->next_bb = bbn->next_bb;
6459 nullify_basic_block (bbn);
6467 if (cfg->opt & MONO_OPT_LOOP) {
6468 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
6469 mono_compute_natural_loops (cfg);
6473 /* after method_to_ir */
6477 //#define DEBUGSSA "logic_run"
6478 #define DEBUGSSA_CLASS "Tests"
6482 if (!header->num_clauses && !cfg->disable_ssa) {
6483 mono_local_cprop (cfg);
6484 mono_ssa_compute (cfg);
6488 /* fixme: add all optimizations which requires SSA */
6489 if (cfg->opt & (MONO_OPT_DEADCE)) {
6490 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
6491 mono_local_cprop (cfg);
6492 mono_ssa_compute (cfg);
6494 if (cfg->verbose_level >= 2) {
6501 /* after SSA translation */
6505 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
6506 if (cfg->comp_done & MONO_COMP_SSA) {
6507 mono_ssa_cprop (cfg);
6509 mono_local_cprop (cfg);
6513 if (cfg->comp_done & MONO_COMP_SSA) {
6514 mono_ssa_deadce (cfg);
6516 //mono_ssa_strength_reduction (cfg);
6518 mono_ssa_remove (cfg);
6520 if (cfg->opt & MONO_OPT_BRANCH)
6521 optimize_branches (cfg);
6524 /* after SSA removal */
6528 decompose_pass (cfg);
6530 /* FIXME: disabled with exception clauses: bug #42136 */
6531 if ((!header->num_clauses) && (cfg->opt & MONO_OPT_LINEARS)) {
6534 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
6535 cfg->comp_done &= ~MONO_COMP_LIVENESS;
6536 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
6537 mono_analyze_liveness (cfg);
6539 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
6540 regs = mono_arch_get_global_int_regs (cfg);
6541 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
6545 //mono_print_code (cfg);
6549 /* variables are allocated after decompose, since decompose could create temps */
6550 mono_arch_allocate_vars (cfg);
6552 if (cfg->opt & MONO_OPT_CFOLD)
6553 mono_constant_fold (cfg);
6555 mini_select_instructions (cfg);
6558 if (cfg->verbose_level >= 2) {
6559 char *id = mono_method_full_name (cfg->method, FALSE);
6560 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
6564 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
6566 jinfo = g_new0 (MonoJitInfo, 1);
6567 jinfo->method = method;
6568 jinfo->code_start = cfg->native_code;
6569 jinfo->code_size = cfg->code_len;
6570 jinfo->used_regs = cfg->used_int_regs;
6572 if (header->num_clauses) {
6575 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
6576 jinfo->num_clauses = header->num_clauses;
6577 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
6578 sizeof (MonoJitExceptionInfo) * header->num_clauses);
6580 for (i = 0; i < header->num_clauses; i++) {
6581 MonoExceptionClause *ec = &header->clauses [i];
6582 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
6583 MonoBasicBlock *tblock;
6585 ei->flags = ec->flags;
6587 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
6588 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->token_or_filter);
6590 ei->data.filter = cfg->native_code + tblock->native_offset;
6592 ei->data.token = ec->token_or_filter;
6595 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
6597 ei->try_start = cfg->native_code + tblock->native_offset;
6598 g_assert (tblock->native_offset);
6599 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
6601 ei->try_end = cfg->native_code + tblock->native_offset;
6602 g_assert (tblock->native_offset);
6603 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
6605 ei->handler_start = cfg->native_code + tblock->native_offset;
6609 mono_jit_info_table_add (cfg->domain, jinfo);
6611 /* collect statistics */
6612 mono_jit_stats.allocated_code_size += cfg->code_len;
6613 code_size_ratio = cfg->code_len;
6614 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
6615 mono_jit_stats.biggest_method_size = code_size_ratio;
6616 mono_jit_stats.biggest_method = method;
6618 code_size_ratio = (code_size_ratio * 100) / ((MonoMethodNormal *)method)->header->code_size;
6619 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
6620 mono_jit_stats.max_code_size_ratio = code_size_ratio;
6621 mono_jit_stats.max_ratio_method = method;
6623 mono_jit_stats.native_code_size += cfg->code_len;
6625 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
6626 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
6632 mono_jit_compile_method (MonoMethod *method)
6634 /* FIXME: later copy the code from mono */
6635 MonoDomain *target_domain, *domain = mono_domain_get ();
6637 GHashTable *jit_code_hash;
6640 if (default_opt & MONO_OPT_SHARED)
6641 target_domain = mono_root_domain;
6643 target_domain = domain;
6645 jit_code_hash = target_domain->jit_code_hash;
6647 if ((code = g_hash_table_lookup (jit_code_hash, method))) {
6648 mono_jit_stats.methods_lookups++;
6652 #ifdef MONO_USE_AOT_COMPILER
6653 if (!mono_compile_aot) {
6654 mono_class_init (method->klass);
6655 if ((code = mono_aot_get_method (method))) {
6656 g_hash_table_insert (jit_code_hash, method, code);
6662 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
6663 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
6664 if (!method->info) {
6667 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
6668 mono_lookup_pinvoke_call (method);
6669 #ifdef MONO_USE_EXC_TABLES
6670 if (mono_method_blittable (method)) {
6671 method->info = method->addr;
6674 nm = mono_marshal_get_native_wrapper (method);
6675 method->info = mono_compile_method (nm);
6677 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
6678 //mono_debug_add_wrapper (method, nm);
6679 #ifdef MONO_USE_EXC_TABLES
6683 return method->info;
6684 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
6685 const char *name = method->name;
6688 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
6689 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
6690 /* FIXME: uhm, we need a wrapper to handle exceptions? */
6691 return (gpointer)mono_delegate_ctor;
6692 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
6693 nm = mono_marshal_get_delegate_invoke (method);
6694 return mono_jit_compile_method (nm);
6695 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
6696 nm = mono_marshal_get_delegate_begin_invoke (method);
6697 return mono_jit_compile_method (nm);
6698 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
6699 nm = mono_marshal_get_delegate_end_invoke (method);
6700 return mono_jit_compile_method (nm);
6706 cfg = mini_method_compile (method, default_opt, target_domain, 0);
6707 code = cfg->native_code;
6708 mono_destroy_compile (cfg);
6710 g_hash_table_insert (jit_code_hash, method, code);
6712 if (target_domain->jump_target_hash) {
6713 MonoJumpInfo patch_info;
6715 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
6717 patch_info.next = NULL;
6718 patch_info.ip.i = 0;
6719 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
6720 patch_info.data.method = method;
6721 g_hash_table_remove (target_domain->jump_target_hash, method);
6723 for (tmp = list; tmp; tmp = tmp->next)
6724 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info);
6725 g_slist_free (list);
6727 /* make sure runtime_init is called */
6728 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
6734 * mono_jit_runtime_invoke:
6735 * @method: the method to invoke
6736 * @obj: this pointer
6737 * @params: array of parameter values.
6738 * @exc: used to catch exceptions objects
6741 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
6744 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc);
6745 invoke = mono_marshal_get_runtime_invoke (method);
6746 runtime_invoke = mono_jit_compile_method (invoke);
6747 return runtime_invoke (obj, params, exc);
6750 #ifdef PLATFORM_WIN32
6751 #define GET_CONTEXT \
6752 struct sigcontext *ctx = (struct sigcontext*)_dummy;
6754 #define GET_CONTEXT \
6755 void **_p = (void **)&_dummy; \
6756 struct sigcontext *ctx = (struct sigcontext *)++_p;
6760 sigfpe_signal_handler (int _dummy)
6765 exc = mono_get_exception_divide_by_zero ();
6767 mono_arch_handle_exception (ctx, exc, FALSE);
6771 sigill_signal_handler (int _dummy)
6775 exc = mono_get_exception_execution_engine ("SIGILL");
6777 mono_arch_handle_exception (ctx, exc, FALSE);
6781 sigsegv_signal_handler (int _dummy)
6786 exc = mono_get_exception_null_reference ();
6788 mono_arch_handle_exception (ctx, exc, FALSE);
6792 sigusr1_signal_handler (int _dummy)
6797 thread = mono_thread_current ();
6799 g_assert (thread->abort_exc);
6801 mono_arch_handle_exception (ctx, thread->abort_exc, FALSE);
6805 mono_runtime_install_handlers (void)
6807 #ifndef PLATFORM_WIN32
6808 struct sigaction sa;
6811 #ifdef PLATFORM_WIN32
6813 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
6814 win32_seh_set_handler(SIGILL, sigill_signal_handler);
6815 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
6816 #else /* !PLATFORM_WIN32 */
6818 /* libpthreads has its own implementation of sigaction(),
6819 * but it seems to work well with our current exception
6820 * handlers. If not we must call syscall directly instead
6824 sa.sa_handler = sigfpe_signal_handler;
6825 sigemptyset (&sa.sa_mask);
6827 //g_assert (syscall (SYS_sigaction, SIGFPE, &sa, NULL) != -1);
6828 g_assert (sigaction (SIGFPE, &sa, NULL) != -1);
6831 sa.sa_handler = sigill_signal_handler;
6832 sigemptyset (&sa.sa_mask);
6834 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
6835 g_assert (sigaction (SIGILL, &sa, NULL) != -1);
6837 /* catch thread abort signal */
6838 sa.sa_handler = sigusr1_signal_handler;
6839 sigemptyset (&sa.sa_mask);
6841 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
6842 g_assert (sigaction (mono_thread_get_abort_signal (), &sa, NULL) != -1);
6846 sa.sa_handler = sigsegv_signal_handler;
6847 sigemptyset (&sa.sa_mask);
6849 //g_assert (syscall (SYS_sigaction, SIGSEGV, &sa, NULL) != -1);
6850 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
6852 #endif /* PLATFORM_WIN32 */
6855 /* mono_jit_create_remoting_trampoline:
6856 * @method: pointer to the method info
6858 * Creates a trampoline which calls the remoting functions. This
6859 * is used in the vtable of transparent proxies.
6861 * Returns: a pointer to the newly created code
6864 mono_jit_create_remoting_trampoline (MonoMethod *method)
6867 guint8 *addr = NULL;
6869 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
6870 (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
6871 nm = mono_marshal_get_remoting_invoke (method);
6872 addr = mono_compile_method (nm);
6874 addr = mono_compile_method (method);
6879 static CRITICAL_SECTION ms;
6882 mini_init (const char *filename)
6886 metadata_section = &ms;
6887 InitializeCriticalSection (metadata_section);
6889 mono_jit_tls_id = TlsAlloc ();
6890 mono_thread_start_cb (GetCurrentThreadId (), (gpointer)-1, NULL);
6894 mono_runtime_install_handlers ();
6896 mono_install_compile_method (mono_jit_compile_method);
6897 mono_install_trampoline (mono_arch_create_jit_trampoline);
6898 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
6899 mono_install_runtime_invoke (mono_jit_runtime_invoke);
6900 mono_install_handler (mono_arch_get_throw_exception ());
6901 mono_install_stack_walk (mono_jit_walk_stack);
6902 mono_install_get_config_dir ();
6904 domain = mono_init (filename);
6907 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
6908 ves_icall_get_frame_info);
6909 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
6910 ves_icall_get_trace);
6911 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
6912 mono_runtime_install_handlers);
6915 create_helper_signature ();
6917 mono_arch_register_lowlevel_calls ();
6918 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
6919 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
6921 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
6922 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
6924 /* fixme: we cant hanlde vararg methods this way, because the signature is not constant */
6925 //mono_register_jit_icall (ves_array_element_address, "ves_array_element_address", NULL);
6926 //mono_register_jit_icall (mono_array_new_va, "mono_array_new_va", NULL);
6928 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
6929 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
6930 helper_sig_void_ptr, TRUE);
6933 * NOTE, NOTE, NOTE, NOTE:
6934 * when adding emulation for some opcodes, remember to also add a dummy
6935 * rule to the burg files, because we need the arity information to be correct.
6937 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult, TRUE);
6938 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un, FALSE);
6939 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf, FALSE);
6940 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv, FALSE);
6941 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un, FALSE);
6942 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem, FALSE);
6943 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un, FALSE);
6945 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl, TRUE);
6946 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr, TRUE);
6947 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un, TRUE);
6949 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8, FALSE);
6950 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4, FALSE);
6951 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8, TRUE);
6952 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8, TRUE);
6954 #if SIZEOF_VOID_P == 4
6955 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4, TRUE);
6957 #warning "fixme: add opcode emulation"
6960 /* other jit icalls */
6961 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
6962 helper_sig_ptr_ptr_ptr, FALSE);
6963 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
6964 mono_register_jit_icall (mono_threads_get_static_data, "mono_threads_get_static_data", helper_sig_ptr_int, FALSE);
6965 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
6966 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
6967 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
6968 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
6969 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
6970 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
6971 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
6972 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
6973 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
6974 mono_register_jit_icall (mono_string_to_utf16, "mono_string_to_utf16", helper_sig_ptr_obj, FALSE);
6975 mono_register_jit_icall (mono_string_from_utf16, "mono_string_from_utf16", helper_sig_obj_ptr, FALSE);
6976 mono_register_jit_icall (mono_string_new_wrapper, "mono_string_new_wrapper", helper_sig_obj_ptr, FALSE);
6977 mono_register_jit_icall (mono_string_to_utf8, "mono_string_to_utf8", helper_sig_ptr_obj, FALSE);
6978 mono_register_jit_icall (mono_string_to_bstr, "mono_string_to_bstr", helper_sig_ptr_obj, FALSE);
6979 mono_register_jit_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", helper_sig_ptr_obj, FALSE);
6980 mono_register_jit_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", helper_sig_ptr_obj, FALSE);
6981 mono_register_jit_icall (mono_array_to_savearray, "mono_array_to_savearray", helper_sig_ptr_obj, FALSE);
6982 mono_register_jit_icall (mono_array_to_lparray, "mono_array_to_lparray", helper_sig_ptr_obj, FALSE);
6983 mono_register_jit_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", helper_sig_ptr_obj, FALSE);
6984 mono_register_jit_icall (mono_marshal_string_array, "mono_marshal_string_array", helper_sig_ptr_obj, FALSE);
6985 mono_register_jit_icall (mono_string_utf8_to_builder, "mono_string_utf8_to_builder", helper_sig_void_ptr_ptr, FALSE);
6986 mono_register_jit_icall (mono_marshal_free_array, "mono_marshal_free_array", helper_sig_void_ptr_ptr, FALSE);
6987 mono_register_jit_icall (mono_string_to_byvalstr, "mono_string_to_byvalstr", helper_sig_void_ptr_ptr_ptr, FALSE);
6988 mono_register_jit_icall (mono_string_to_byvalwstr, "mono_string_to_byvalwstr", helper_sig_void_ptr_ptr_ptr, FALSE);
6989 mono_register_jit_icall (g_free, "g_free", helper_sig_void_ptr, FALSE);
6990 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
6991 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
6992 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
6994 mono_runtime_init (domain, mono_thread_start_cb,
6995 mono_thread_attach_cb);
6997 //mono_thread_attach (domain);
7001 MonoJitStats mono_jit_stats = {0};
7004 print_jit_stats (void)
7006 if (mono_jit_stats.enabled) {
7007 g_print ("Mono Jit statistics\n");
7008 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
7009 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
7010 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
7011 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
7012 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
7013 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
7014 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
7015 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
7016 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
7017 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
7018 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
7019 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
7020 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
7021 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
7022 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
7023 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
7024 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
7025 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
7027 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
7028 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
7029 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
7030 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
7031 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
7036 mini_cleanup (MonoDomain *domain)
7039 * mono_runtime_cleanup() and mono_domain_finalize () need to
7040 * be called early since they need the execution engine still
7041 * fully working (mono_domain_finalize may invoke managed finalizers
7042 * and mono_runtime_cleanup will wait for other threads to finish).
7044 mono_domain_finalize (domain);
7046 mono_runtime_cleanup (domain);
7048 mono_profiler_shutdown ();
7050 mono_debug_cleanup ();
7052 #ifdef PLATFORM_WIN32
7053 win32_seh_cleanup();
7056 mono_domain_unload (domain, TRUE);
7059 DeleteCriticalSection (metadata_section);
7063 mono_set_defaults (int verbose_level, guint32 opts)
7065 mini_verbose = verbose_level;