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 dont_inline = g_list_prepend (dont_inline, method);
2442 if (cfg->method == method) {
2445 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
2446 start_bblock->cil_code = NULL;
2447 start_bblock->cil_length = 0;
2448 start_bblock->block_num = cfg->num_bblocks++;
2451 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
2452 end_bblock->cil_code = NULL;
2453 end_bblock->cil_length = 0;
2454 end_bblock->block_num = cfg->num_bblocks++;
2455 g_assert (cfg->num_bblocks == 2);
2457 arg_array = alloca (sizeof (MonoInst *) * (sig->hasthis + sig->param_count));
2458 for (i = sig->hasthis + sig->param_count - 1; i >= 0; i--)
2459 arg_array [i] = cfg->varinfo [i];
2461 if (mono_compile_aot)
2462 cfg->opt |= MONO_OPT_SHARED;
2464 if (header->num_clauses) {
2465 int size = sizeof (int) * header->num_clauses;
2466 filter_lengths = alloca (size);
2467 memset (filter_lengths, 0, size);
2469 cfg->spvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2470 /* prevent it from being register allocated */
2471 cfg->spvar->flags |= MONO_INST_INDIRECT;
2473 /* handle exception clauses */
2474 for (i = 0; i < header->num_clauses; ++i) {
2475 //unsigned char *p = ip;
2476 MonoExceptionClause *clause = &header->clauses [i];
2477 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->try_offset);
2478 tblock->real_offset = clause->try_offset;
2479 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
2480 tblock->real_offset = clause->handler_offset;
2482 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
2483 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2484 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2485 MONO_ADD_INS (tblock, ins);
2488 /*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);
2490 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
2492 /* catch and filter blocks get the exception object on the stack */
2493 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
2494 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2495 /* mostly like handle_stack_args (), but just sets the input args */
2496 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
2498 cfg->exvar = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
2499 /* prevent it from being register allocated */
2500 cfg->exvar->flags |= MONO_INST_INDIRECT;
2502 tblock->in_scount = 1;
2503 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2504 tblock->in_stack [0] = cfg->exvar;
2506 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2507 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->token_or_filter);
2508 tblock->real_offset = clause->token_or_filter;
2509 tblock->in_scount = 1;
2510 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2511 tblock->in_stack [0] = cfg->exvar;
2512 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2513 MONO_ADD_INS (tblock, ins);
2519 arg_array = alloca (sizeof (MonoInst *) * (sig->hasthis + sig->param_count));
2520 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
2523 /* FIRST CODE BLOCK */
2524 bblock = NEW_BBLOCK (cfg);
2525 bblock->cil_code = ip;
2527 ADD_BBLOCK (cfg, bbhash, bblock);
2529 if (cfg->method == method) {
2530 breakpoint_id = mono_debugger_method_has_breakpoint (method);
2531 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
2532 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2533 MONO_ADD_INS (bblock, ins);
2537 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED)))) {
2538 /* we use a separate basic block for the initialization code */
2539 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
2540 init_localsbb->real_offset = real_offset;
2541 start_bblock->next_bb = init_localsbb;
2542 init_localsbb->next_bb = bblock;
2543 link_bblock (cfg, start_bblock, init_localsbb);
2544 link_bblock (cfg, init_localsbb, bblock);
2545 init_localsbb->block_num = cfg->num_bblocks++;
2547 start_bblock->next_bb = bblock;
2548 link_bblock (cfg, start_bblock, bblock);
2551 if (get_basic_blocks (cfg, bbhash, header, real_offset, ip, end))
2554 mono_debug_init_method (cfg, bblock, breakpoint_id);
2556 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * (sig->hasthis + sig->param_count));
2558 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
2559 for (n = 0; n < sig->param_count; ++n)
2560 param_types [n + sig->hasthis] = sig->params [n];
2562 /* do this somewhere outside - not here */
2563 NEW_ICONST (cfg, zero_int32, 0);
2564 NEW_ICONST (cfg, zero_int64, 0);
2565 zero_int64->type = STACK_I8;
2566 NEW_PCONST (cfg, zero_ptr, 0);
2567 NEW_PCONST (cfg, zero_obj, 0);
2568 zero_obj->type = STACK_OBJ;
2570 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
2571 zero_r8->type = STACK_R8;
2572 zero_r8->inst_p0 = &r8_0;
2574 /* add a check for this != NULL to inlined methods */
2575 if (is_virtual_call) {
2576 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
2577 NEW_ARGLOAD (cfg, ins->inst_left, 0);
2579 MONO_ADD_INS (bblock, ins);
2582 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
2583 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
2586 start_new_bblock = 0;
2589 if (cfg->method == method)
2590 real_offset = ip - header->code;
2592 real_offset = inline_offset;
2594 if (start_new_bblock) {
2595 bblock->cil_length = ip - bblock->cil_code;
2596 if (start_new_bblock == 2) {
2597 g_assert (ip == tblock->cil_code);
2599 GET_BBLOCK (cfg, bbhash, tblock, ip);
2601 bblock->next_bb = tblock;
2603 start_new_bblock = 0;
2604 for (i = 0; i < bblock->in_scount; ++i) {
2605 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2609 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
2610 link_bblock (cfg, bblock, tblock);
2611 if (sp != stack_start) {
2612 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2615 bblock->next_bb = tblock;
2617 for (i = 0; i < bblock->in_scount; ++i) {
2618 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2624 if (cfg->verbose_level > 3)
2625 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, sp-stack_start, mono_disasm_code_one (NULL, method, ip, NULL));
2632 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2633 ins->cil_code = ip++;
2634 MONO_ADD_INS (bblock, ins);
2640 CHECK_STACK_OVF (1);
2641 n = (*ip)-CEE_LDARG_0;
2642 NEW_ARGLOAD (cfg, ins, n);
2643 ins->cil_code = ip++;
2650 CHECK_STACK_OVF (1);
2651 n = (*ip)-CEE_LDLOC_0;
2652 NEW_LOCLOAD (cfg, ins, n);
2653 ins->cil_code = ip++;
2661 n = (*ip)-CEE_STLOC_0;
2663 handle_loaded_temps (cfg, bblock, stack_start, sp);
2664 NEW_LOCSTORE (cfg, ins, n, *sp);
2666 if (ins->opcode == CEE_STOBJ) {
2667 NEW_LOCLOADA (cfg, ins, n);
2668 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2670 MONO_ADD_INS (bblock, ins);
2675 CHECK_STACK_OVF (1);
2676 NEW_ARGLOAD (cfg, ins, ip [1]);
2682 CHECK_STACK_OVF (1);
2683 NEW_ARGLOADA (cfg, ins, ip [1]);
2691 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
2692 handle_loaded_temps (cfg, bblock, stack_start, sp);
2694 if (ins->opcode == CEE_STOBJ) {
2695 NEW_ARGLOADA (cfg, ins, ip [1]);
2696 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2698 MONO_ADD_INS (bblock, ins);
2702 CHECK_STACK_OVF (1);
2703 NEW_LOCLOAD (cfg, ins, ip [1]);
2709 CHECK_STACK_OVF (1);
2710 NEW_LOCLOADA (cfg, ins, ip [1]);
2718 handle_loaded_temps (cfg, bblock, stack_start, sp);
2719 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
2721 if (ins->opcode == CEE_STOBJ) {
2722 NEW_LOCLOADA (cfg, ins, ip [1]);
2723 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2725 MONO_ADD_INS (bblock, ins);
2730 CHECK_STACK_OVF (1);
2731 NEW_PCONST (cfg, ins, NULL);
2733 ins->type = STACK_OBJ;
2738 CHECK_STACK_OVF (1);
2739 NEW_ICONST (cfg, ins, -1);
2753 CHECK_STACK_OVF (1);
2754 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
2760 CHECK_STACK_OVF (1);
2762 NEW_ICONST (cfg, ins, *((signed char*)ip));
2768 CHECK_STACK_OVF (1);
2769 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
2775 CHECK_STACK_OVF (1);
2776 MONO_INST_NEW (cfg, ins, OP_I8CONST);
2778 ins->type = STACK_I8;
2780 ins->inst_l = (gint64)read64 (ip);
2785 float *f = g_malloc (sizeof (float));
2786 CHECK_STACK_OVF (1);
2787 MONO_INST_NEW (cfg, ins, OP_R4CONST);
2788 ins->type = STACK_R8;
2797 double *d = g_malloc (sizeof (double));
2798 CHECK_STACK_OVF (1);
2799 MONO_INST_NEW (cfg, ins, OP_R8CONST);
2800 ins->type = STACK_R8;
2809 MonoInst *temp, *store;
2811 CHECK_STACK_OVF (1);
2816 * small optimization: if the loaded value was from a local already,
2817 * just load it twice.
2819 if (ins->ssa_op == MONO_SSA_LOAD &&
2820 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
2822 MONO_INST_NEW (cfg, temp, 0);
2824 temp->cil_code = ip;
2827 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
2828 temp->cil_code = ip;
2829 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2830 store->cil_code = ip;
2831 MONO_ADD_INS (bblock, store);
2832 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
2835 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
2845 MONO_INST_NEW (cfg, ins, CEE_POP);
2846 MONO_ADD_INS (bblock, ins);
2847 ins->cil_code = ip++;
2852 if (stack_start != sp)
2854 MONO_INST_NEW (cfg, ins, CEE_JMP);
2855 token = read32 (ip + 1);
2856 /* FIXME: check the signature matches */
2857 cmethod = mono_get_method (image, token, NULL);
2858 ins->inst_p0 = cmethod;
2859 MONO_ADD_INS (bblock, ins);
2861 start_new_bblock = 1;
2865 case CEE_CALLVIRT: {
2866 MonoInst *addr = NULL;
2867 MonoMethodSignature *fsig = NULL;
2868 int temp, array_rank = 0;
2869 int virtual = *ip == CEE_CALLVIRT;
2871 token = read32 (ip + 1);
2873 if (*ip == CEE_CALLI) {
2878 if (method->wrapper_type != MONO_WRAPPER_NONE)
2879 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
2881 fsig = mono_metadata_parse_signature (image, token);
2883 n = fsig->param_count + fsig->hasthis;
2886 if (method->wrapper_type != MONO_WRAPPER_NONE) {
2887 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
2889 cmethod = mono_get_method (image, token, NULL);
2892 if (!cmethod->klass->inited)
2893 mono_class_init (cmethod->klass);
2895 if (cmethod->signature->pinvoke) {
2896 #ifdef MONO_USE_EXC_TABLES
2897 if (mono_method_blittable (cmethod)) {
2898 fsig = cmethod->signature;
2901 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
2902 fsig = wrapper->signature;
2903 #ifdef MONO_USE_EXC_TABLES
2907 fsig = mono_method_get_signature (cmethod, image, token);
2910 n = fsig->param_count + fsig->hasthis;
2912 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
2913 cmethod->klass->parent == mono_defaults.array_class) {
2914 array_rank = cmethod->klass->rank;
2917 if (cmethod->string_ctor)
2918 g_assert_not_reached ();
2924 //g_assert (!virtual || fsig->hasthis);
2928 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
2931 if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL)) {
2933 for (i = 0; i < n; ++i) {
2934 NEW_ARGSTORE (cfg, ins, i, sp [i]);
2936 MONO_ADD_INS (bblock, ins);
2938 MONO_INST_NEW (cfg, ins, CEE_JMP);
2940 ins->inst_p0 = cmethod;
2941 MONO_ADD_INS (bblock, ins);
2942 start_new_bblock = 1;
2943 /* skip CEE_RET as well */
2948 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_opcode_for_method (cfg, cmethod, fsig, sp))) {
2951 if (MONO_TYPE_IS_VOID (fsig->ret)) {
2952 MONO_ADD_INS (bblock, ins);
2954 type_to_eval_stack_type (fsig->ret, ins);
2963 handle_loaded_temps (cfg, bblock, stack_start, sp);
2965 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
2966 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
2967 mono_method_check_inlining (cfg, cmethod) &&
2968 !g_list_find (dont_inline, cmethod)) {
2970 MonoBasicBlock *ebblock;
2972 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
2976 GET_BBLOCK (cfg, bbhash, bblock, ip);
2977 ebblock->next_bb = bblock;
2978 link_bblock (cfg, ebblock, bblock);
2980 if (!MONO_TYPE_IS_VOID (fsig->ret))
2983 /* indicates start of a new block, and triggers a load of all
2984 stack arguments at bb boundarie */
2987 inline_costs += costs;
2992 inline_costs += 10 * num_calls++;
2994 /* tail recursion elimination */
2995 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == cfg->method && ip [5] == CEE_RET) {
2996 gboolean has_vtargs = FALSE;
2999 /* keep it simple */
3000 for (i = fsig->param_count - 1; i >= 0; i--) {
3001 if (MONO_TYPE_ISSTRUCT (cmethod->signature->params [i]))
3006 for (i = 0; i < n; ++i) {
3007 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3009 MONO_ADD_INS (bblock, ins);
3011 MONO_INST_NEW (cfg, ins, CEE_BR);
3013 MONO_ADD_INS (bblock, ins);
3014 tblock = start_bblock->out_bb [0];
3015 link_bblock (cfg, bblock, tblock);
3016 ins->inst_target_bb = tblock;
3017 start_new_bblock = 1;
3020 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3021 /* just create a dummy - the value is never used */
3022 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
3023 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3031 if (*ip == CEE_CALLI) {
3033 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
3034 NEW_TEMPLOAD (cfg, *sp, temp);
3038 } else if (array_rank) {
3041 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
3042 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
3043 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
3045 if (ins->opcode == CEE_STOBJ) {
3046 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
3048 MONO_ADD_INS (bblock, ins);
3051 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
3052 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3053 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
3057 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
3058 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3061 g_assert_not_reached ();
3065 if (0 && CODE_IS_STLOC (ip + 5) && (!MONO_TYPE_ISSTRUCT (fsig->ret)) && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)) {
3066 /* no need to spill */
3067 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
3070 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) {
3071 NEW_TEMPLOAD (cfg, *sp, temp);
3081 if (cfg->method != method) {
3082 /* return from inlined methode */
3087 //g_assert (returnvar != -1);
3088 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
3089 store->cil_code = sp [0]->cil_code;
3090 if (store->opcode == CEE_STOBJ) {
3091 g_assert_not_reached ();
3092 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
3093 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
3095 MONO_ADD_INS (bblock, store);
3099 g_assert (!return_var);
3102 MONO_INST_NEW (cfg, ins, CEE_NOP);
3103 ins->opcode = mono_type_to_stind (method->signature->ret);
3104 if (ins->opcode == CEE_STOBJ) {
3105 NEW_RETLOADA (cfg, ins);
3106 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3108 ins->opcode = OP_SETRET;
3110 ins->inst_i0 = *sp;;
3111 ins->inst_i1 = NULL;
3112 MONO_ADD_INS (bblock, ins);
3116 if (sp != stack_start)
3118 MONO_INST_NEW (cfg, ins, CEE_BR);
3119 ins->cil_code = ip++;
3120 ins->inst_target_bb = end_bblock;
3121 MONO_ADD_INS (bblock, ins);
3122 link_bblock (cfg, bblock, end_bblock);
3123 start_new_bblock = 1;
3126 MONO_INST_NEW (cfg, ins, CEE_BR);
3127 ins->cil_code = ip++;
3128 MONO_ADD_INS (bblock, ins);
3129 target = ip + 1 + (signed char)(*ip);
3131 GET_BBLOCK (cfg, bbhash, tblock, target);
3132 link_bblock (cfg, bblock, tblock);
3133 CHECK_BBLOCK (target, ip, tblock);
3134 ins->inst_target_bb = tblock;
3135 if (sp != stack_start) {
3136 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3139 start_new_bblock = 1;
3145 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3146 ins->cil_code = ip++;
3147 target = ip + 1 + *(signed char*)ip;
3149 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
3150 if (sp != stack_start) {
3151 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3167 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3168 ins->cil_code = ip++;
3169 target = ip + 1 + *(signed char*)ip;
3172 if (sp != stack_start) {
3173 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3179 MONO_INST_NEW (cfg, ins, CEE_BR);
3180 ins->cil_code = ip++;
3181 MONO_ADD_INS (bblock, ins);
3182 target = ip + 4 + (gint32)read32(ip);
3184 GET_BBLOCK (cfg, bbhash, tblock, target);
3185 link_bblock (cfg, bblock, tblock);
3186 CHECK_BBLOCK (target, ip, tblock);
3187 ins->inst_target_bb = tblock;
3188 if (sp != stack_start) {
3189 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3192 start_new_bblock = 1;
3198 MONO_INST_NEW (cfg, ins, *ip);
3199 ins->cil_code = ip++;
3200 target = ip + 4 + (gint32)read32(ip);
3202 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
3203 if (sp != stack_start) {
3204 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3220 MONO_INST_NEW (cfg, ins, *ip);
3221 ins->cil_code = ip++;
3222 target = ip + 4 + (gint32)read32(ip);
3225 if (sp != stack_start) {
3226 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3233 n = read32 (ip + 1);
3234 MONO_INST_NEW (cfg, ins, *ip);
3236 ins->inst_left = *sp;
3237 if (ins->inst_left->type != STACK_I4) goto unverified;
3240 target = ip + n * sizeof (guint32);
3241 MONO_ADD_INS (bblock, ins);
3242 GET_BBLOCK (cfg, bbhash, tblock, target);
3243 link_bblock (cfg, bblock, tblock);
3244 ins->klass = GUINT_TO_POINTER (n);
3245 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
3246 ins->inst_many_bb [n] = tblock;
3248 for (i = 0; i < n; ++i) {
3249 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
3250 link_bblock (cfg, bblock, tblock);
3251 ins->inst_many_bb [i] = tblock;
3254 /* FIXME: handle stack args */
3269 MONO_INST_NEW (cfg, ins, *ip);
3274 ins->type = ldind_type [*ip - CEE_LDIND_I1];
3275 ins->flags |= ins_flag;
3287 MONO_INST_NEW (cfg, ins, *ip);
3288 ins->cil_code = ip++;
3290 handle_loaded_temps (cfg, bblock, stack_start, sp);
3291 MONO_ADD_INS (bblock, ins);
3292 ins->inst_i0 = sp [0];
3293 ins->inst_i1 = sp [1];
3294 ins->flags |= ins_flag;
3325 case CEE_CONV_OVF_I8:
3326 case CEE_CONV_OVF_U8:
3332 case CEE_CONV_OVF_I4:
3333 case CEE_CONV_OVF_I1:
3334 case CEE_CONV_OVF_I2:
3335 case CEE_CONV_OVF_I:
3336 case CEE_CONV_OVF_U:
3339 if (sp [-1]->type == STACK_R8) {
3340 ADD_UNOP (CEE_CONV_OVF_I8);
3348 case CEE_CONV_OVF_U1:
3349 case CEE_CONV_OVF_U2:
3350 case CEE_CONV_OVF_U4:
3353 if (sp [-1]->type == STACK_R8) {
3354 ADD_UNOP (CEE_CONV_OVF_U8);
3362 case CEE_CONV_OVF_I1_UN:
3363 case CEE_CONV_OVF_I2_UN:
3364 case CEE_CONV_OVF_I4_UN:
3365 case CEE_CONV_OVF_I8_UN:
3366 case CEE_CONV_OVF_U1_UN:
3367 case CEE_CONV_OVF_U2_UN:
3368 case CEE_CONV_OVF_U4_UN:
3369 case CEE_CONV_OVF_U8_UN:
3370 case CEE_CONV_OVF_I_UN:
3371 case CEE_CONV_OVF_U_UN:
3377 g_error ("opcode 0x%02x not handled", *ip);
3380 MonoInst *iargs [3];
3383 token = read32 (ip + 1);
3384 if (method->wrapper_type != MONO_WRAPPER_NONE)
3385 klass = mono_method_get_wrapper_data (method, token);
3387 klass = mono_class_get (image, token);
3389 mono_class_init (klass);
3390 n = mono_class_value_size (klass, NULL);
3391 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3392 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3393 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3395 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3396 copy->inst_left = iargs [0];
3397 copy->inst_right = *sp;
3398 copy->cil_code = ip;
3400 MONO_ADD_INS (bblock, copy);
3403 NEW_ICONST (cfg, iargs [2], n);
3404 iargs [2]->cil_code = ip;
3406 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3408 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3415 CHECK_STACK_OVF (1);
3416 n = read32 (ip + 1);
3418 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3420 MonoInst *iargs [1];
3422 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
3423 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
3424 NEW_TEMPLOAD (cfg, *sp, temp);
3428 if (mono_compile_aot) {
3429 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n);
3432 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
3434 MonoInst *iargs [3];
3435 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3436 NEW_IMAGECONST (cfg, iargs [1], image);
3437 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
3438 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
3439 NEW_TEMPLOAD (cfg, *sp, temp);
3440 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3442 NEW_PCONST (cfg, ins, NULL);
3444 ins->type = STACK_OBJ;
3445 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3454 MonoInst *iargs [2];
3455 MonoMethodSignature *fsig;
3458 token = read32 (ip + 1);
3459 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3460 cmethod = mono_method_get_wrapper_data (method, token);
3462 cmethod = mono_get_method (image, token, NULL);
3463 fsig = mono_method_get_signature (cmethod, image, token);
3465 mono_class_init (cmethod->klass);
3467 n = fsig->param_count;
3470 /* move the args to allow room for 'this' in the first position */
3476 handle_loaded_temps (cfg, bblock, stack_start, sp);
3479 if (cmethod->klass->parent == mono_defaults.array_class) {
3480 NEW_METHODCONST (cfg, *sp, cmethod);
3481 temp = mono_emit_native_call (cfg, bblock, mono_array_new_va, fsig, sp, ip, FALSE);
3483 } else if (cmethod->string_ctor) {
3484 /* we simply pass a null pointer */
3485 NEW_PCONST (cfg, *sp, NULL);
3486 /* now call the string ctor */
3487 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
3489 if (cmethod->klass->valuetype) {
3490 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
3491 temp = iargs [0]->inst_c0;
3492 NEW_TEMPLOADA (cfg, *sp, temp);
3494 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
3495 NEW_DOMAINCONST (cfg, iargs [0]);
3496 NEW_CLASSCONST (cfg, iargs [1], cmethod->klass);
3498 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3500 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
3501 NEW_PCONST (cfg, iargs [0], vtable);
3502 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
3504 NEW_TEMPLOAD (cfg, *sp, temp);
3507 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3508 mono_method_check_inlining (cfg, cmethod) &&
3509 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
3510 !g_list_find (dont_inline, cmethod)) {
3512 MonoBasicBlock *ebblock;
3513 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3518 GET_BBLOCK (cfg, bbhash, bblock, ip);
3519 ebblock->next_bb = bblock;
3520 link_bblock (cfg, ebblock, bblock);
3522 NEW_TEMPLOAD (cfg, *sp, temp);
3525 /* indicates start of a new block, and triggers a load
3526 of all stack arguments at bb boundarie */
3529 inline_costs += costs;
3533 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3536 /* now call the actual ctor */
3537 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3541 NEW_TEMPLOAD (cfg, *sp, temp);
3550 MONO_INST_NEW (cfg, ins, *ip);
3552 klass = mono_class_get (image, read32 (ip + 1));
3553 mono_class_init (klass);
3554 ins->type = STACK_OBJ;
3555 ins->inst_left = *sp;
3556 ins->inst_newa_class = klass;
3562 MonoInst *add, *vtoffset;
3566 token = read32 (ip + 1);
3567 if (method->wrapper_type != MONO_WRAPPER_NONE)
3568 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3570 klass = mono_class_get (image, token);
3571 mono_class_init (klass);
3574 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
3575 ins->type = STACK_OBJ;
3576 ins->inst_left = *sp;
3578 ins->inst_newa_class = klass;
3581 MONO_INST_NEW (cfg, add, CEE_ADD);
3582 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3583 add->inst_left = ins;
3584 add->inst_right = vtoffset;
3585 add->type = STACK_MP;
3593 MONO_INST_NEW (cfg, ins, *ip);
3595 klass = mono_class_get (image, read32 (ip + 1));
3596 mono_class_init (klass);
3597 ins->type = STACK_OBJ;
3598 ins->inst_left = *sp;
3600 ins->inst_newa_class = klass;
3607 MONO_INST_NEW (cfg, ins, *ip);
3609 ins->inst_left = *sp;
3610 ins->cil_code = ip++;
3611 MONO_ADD_INS (bblock, ins);
3613 start_new_bblock = 1;
3618 MonoInst *offset_ins;
3619 MonoClassField *field;
3620 MonoBasicBlock *ebblock;
3624 if (*ip == CEE_STFLD) {
3631 // FIXME: enable this test later.
3632 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
3634 token = read32 (ip + 1);
3635 field = mono_field_from_token (image, token, &klass);
3636 mono_class_init (klass);
3638 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
3639 /* FIXME: mark instructions for use in SSA */
3640 if (*ip == CEE_STFLD) {
3641 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
3642 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
3643 MonoInst *iargs [5];
3646 NEW_CLASSCONST (cfg, iargs [1], klass);
3647 NEW_FIELDCONST (cfg, iargs [2], field);
3648 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
3652 if (cfg->opt & MONO_OPT_INLINE) {
3653 costs = inline_method (cfg, stfld_wrapper, stfld_wrapper->signature, bblock,
3654 iargs, ip, real_offset, dont_inline, &ebblock);
3655 g_assert (costs > 0);
3660 GET_BBLOCK (cfg, bbhash, bblock, ip);
3661 ebblock->next_bb = bblock;
3662 link_bblock (cfg, ebblock, bblock);
3664 /* indicates start of a new block, and triggers a load
3665 of all stack arguments at bb boundarie */
3668 inline_costs += costs;
3671 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, stfld_wrapper->signature, iargs, ip, NULL);
3675 NEW_ICONST (cfg, offset_ins, foffset);
3676 MONO_INST_NEW (cfg, ins, CEE_ADD);
3678 ins->inst_left = *sp;
3679 ins->inst_right = offset_ins;
3680 ins->type = STACK_MP;
3682 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
3683 store->cil_code = ip;
3684 store->inst_left = ins;
3685 store->inst_right = sp [1];
3686 handle_loaded_temps (cfg, bblock, stack_start, sp);
3687 store->flags |= ins_flag;
3689 if (store->opcode == CEE_STOBJ) {
3690 handle_stobj (cfg, bblock, ins, sp [1], ip,
3691 mono_class_from_mono_type (field->type), FALSE, FALSE);
3693 MONO_ADD_INS (bblock, store);
3696 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
3697 /* fixme: we need to inline that call somehow */
3698 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
3699 MonoInst *iargs [4];
3703 NEW_CLASSCONST (cfg, iargs [1], klass);
3704 NEW_FIELDCONST (cfg, iargs [2], field);
3705 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
3706 if (cfg->opt & MONO_OPT_INLINE) {
3707 costs = inline_method (cfg, ldfld_wrapper, ldfld_wrapper->signature, bblock,
3708 iargs, ip, real_offset, dont_inline, &ebblock);
3709 g_assert (costs > 0);
3714 GET_BBLOCK (cfg, bbhash, bblock, ip);
3715 ebblock->next_bb = bblock;
3716 link_bblock (cfg, ebblock, bblock);
3718 temp = iargs [0]->inst_i0->inst_c0;
3720 if (*ip == CEE_LDFLDA) {
3721 /* not sure howto handle this */
3722 NEW_TEMPLOADA (cfg, *sp, temp);
3724 NEW_TEMPLOAD (cfg, *sp, temp);
3728 /* indicates start of a new block, and triggers a load of
3729 all stack arguments at bb boundarie */
3732 inline_costs += costs;
3735 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, ldfld_wrapper->signature, iargs, ip, NULL);
3736 if (*ip == CEE_LDFLDA) {
3737 /* not sure howto handle this */
3738 NEW_TEMPLOADA (cfg, *sp, temp);
3740 NEW_TEMPLOAD (cfg, *sp, temp);
3745 NEW_ICONST (cfg, offset_ins, foffset);
3746 MONO_INST_NEW (cfg, ins, CEE_ADD);
3748 ins->inst_left = *sp;
3749 ins->inst_right = offset_ins;
3750 ins->type = STACK_MP;
3752 if (*ip == CEE_LDFLDA) {
3756 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
3757 type_to_eval_stack_type (field->type, load);
3758 load->cil_code = ip;
3759 load->inst_left = ins;
3760 load->flags |= ins_flag;
3772 MonoClassField *field;
3774 token = read32 (ip + 1);
3776 field = mono_field_from_token (image, token, &klass);
3777 mono_class_init (klass);
3779 handle_loaded_temps (cfg, bblock, stack_start, sp);
3781 if (((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot)) {
3783 MonoInst *iargs [2];
3784 g_assert (field->parent);
3785 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3786 NEW_FIELDCONST (cfg, iargs [1], field);
3787 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
3788 NEW_TEMPLOAD (cfg, ins, temp);
3792 vtable = mono_class_vtable (cfg->domain, klass);
3793 if (!cfg->domain->thread_static_fields || !(addr = g_hash_table_lookup (cfg->domain->thread_static_fields, field))) {
3794 if (!vtable->initialized && !(klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && needs_cctor_run (klass, method)) {
3795 MonoInst *iargs [1];
3796 NEW_PCONST (cfg, iargs [0], vtable);
3797 mono_emit_jit_icall (cfg, bblock, mono_runtime_class_init, iargs, ip);
3798 if (cfg->verbose_level > 2)
3799 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
3801 mono_runtime_class_init (vtable);
3803 addr = (char*)vtable->data + field->offset;
3804 NEW_PCONST (cfg, ins, addr);
3808 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
3809 * This could be later optimized to do just a couple of
3810 * memory dereferences with constant offsets.
3813 MonoInst *iargs [1];
3814 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
3815 temp = mono_emit_jit_icall (cfg, bblock, mono_threads_get_static_data, iargs, ip);
3816 NEW_TEMPLOAD (cfg, ins, temp);
3820 /* FIXME: mark instructions for use in SSA */
3821 if (*ip == CEE_LDSFLDA) {
3823 } else if (*ip == CEE_STSFLD) {
3827 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
3828 store->cil_code = ip;
3829 store->inst_left = ins;
3830 store->inst_right = sp [0];
3831 store->flags |= ins_flag;
3834 if (store->opcode == CEE_STOBJ) {
3835 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
3837 MONO_ADD_INS (bblock, store);
3839 gboolean is_const = FALSE;
3840 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
3841 if (!((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) &&
3842 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
3843 gpointer addr = (char*)vtable->data + field->offset;
3844 /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
3846 switch (field->type->type) {
3847 case MONO_TYPE_BOOLEAN:
3849 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
3853 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
3856 case MONO_TYPE_CHAR:
3858 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
3862 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
3867 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
3871 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
3876 case MONO_TYPE_STRING:
3877 case MONO_TYPE_OBJECT:
3878 case MONO_TYPE_CLASS:
3879 case MONO_TYPE_SZARRAY:
3881 case MONO_TYPE_FNPTR:
3882 case MONO_TYPE_ARRAY:
3883 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
3884 type_to_eval_stack_type (field->type, *sp);
3889 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
3890 sp [0]->type = STACK_I8;
3891 sp [0]->inst_l = *((gint64 *)addr);
3896 case MONO_TYPE_VALUETYPE:
3905 CHECK_STACK_OVF (1);
3906 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
3907 type_to_eval_stack_type (field->type, load);
3908 load->cil_code = ip;
3909 load->inst_left = ins;
3911 load->flags |= ins_flag;
3913 /* fixme: dont see the problem why this does not work */
3914 //cfg->disable_aot = TRUE;
3923 token = read32 (ip + 1);
3924 if (method->wrapper_type != MONO_WRAPPER_NONE)
3925 klass = mono_method_get_wrapper_data (method, token);
3927 klass = mono_class_get (image, token);
3928 mono_class_init (klass);
3929 n = mono_type_to_stind (&klass->byval_arg);
3930 if (n == CEE_STOBJ) {
3931 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
3933 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
3935 MONO_INST_NEW (cfg, store, n);
3936 store->cil_code = ip;
3937 store->inst_left = sp [0];
3938 store->inst_right = sp [1];
3939 store->flags |= ins_flag;
3940 MONO_ADD_INS (bblock, store);
3947 MonoInst *iargs [2];
3948 MonoInst *load, *vtoffset, *add, *val, *vstore;
3953 token = read32 (ip + 1);
3954 if (method->wrapper_type != MONO_WRAPPER_NONE)
3955 klass = mono_method_get_wrapper_data (method, token);
3957 klass = mono_class_get (image, token);
3958 mono_class_init (klass);
3960 /* much like NEWOBJ */
3961 NEW_DOMAINCONST (cfg, iargs [0]);
3962 NEW_CLASSCONST (cfg, iargs [1], klass);
3964 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3965 NEW_TEMPLOAD (cfg, load, temp);
3966 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3967 MONO_INST_NEW (cfg, add, CEE_ADD);
3968 add->inst_left = load;
3969 add->inst_right = vtoffset;
3972 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
3973 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
3974 vstore->cil_code = ip;
3975 vstore->inst_left = add;
3976 vstore->inst_right = val;
3978 if (vstore->opcode == CEE_STOBJ) {
3979 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
3981 MONO_ADD_INS (bblock, vstore);
3983 NEW_TEMPLOAD (cfg, load, temp);
3991 MONO_INST_NEW (cfg, ins, *ip);
3995 token = read32 (ip + 1);
3997 /* allocate the domainvar - becaus this is used in decompose_foreach */
3998 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot)
3999 mono_get_domainvar (cfg);
4001 if (method->wrapper_type != MONO_WRAPPER_NONE)
4002 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4004 klass = mono_class_get (image, token);
4006 mono_class_init (klass);
4007 ins->inst_newa_class = klass;
4008 ins->inst_newa_len = *sp;
4009 ins->type = STACK_OBJ;
4016 MONO_INST_NEW (cfg, ins, *ip);
4017 ins->cil_code = ip++;
4019 ins->inst_left = *sp;
4020 ins->type = STACK_PTR;
4026 klass = mono_class_get (image, read32 (ip + 1));
4027 mono_class_init (klass);
4028 NEW_LDELEMA (cfg, ins, sp, klass);
4043 case CEE_LDELEM_REF: {
4047 * ldind.x (ldelema (array, index))
4048 * ldelema does the bounds check
4052 klass = array_access_to_klass (*ip);
4053 NEW_LDELEMA (cfg, load, sp, klass);
4054 load->cil_code = ip;
4055 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
4057 ins->inst_left = load;
4059 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
4069 case CEE_STELEM_R8: {
4073 * stind.x (ldelema (array, index), val)
4074 * ldelema does the bounds check
4078 klass = array_access_to_klass (*ip);
4079 NEW_LDELEMA (cfg, load, sp, klass);
4080 load->cil_code = ip;
4081 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
4083 ins->inst_left = load;
4084 ins->inst_right = sp [2];
4086 handle_loaded_temps (cfg, bblock, stack_start, sp);
4087 MONO_ADD_INS (bblock, ins);
4088 /* FIXME: add the implicit STELEM_REF castclass */
4090 cfg->disable_ssa = TRUE;
4093 case CEE_STELEM_REF: {
4094 MonoInst *iargs [3];
4099 handle_loaded_temps (cfg, bblock, stack_start, sp);
4105 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4109 NEW_GROUP (cfg, group, sp [0], sp [1]);
4110 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
4112 ins->inst_left = group;
4113 ins->inst_right = sp [2];
4114 MONO_ADD_INS (bblock, ins);
4119 cfg->disable_ssa = TRUE;
4122 case CEE_CKFINITE: {
4123 MonoInst *store, *temp;
4126 /* this instr. can throw exceptions as side effect,
4127 * so we cant eliminate dead code which contains CKFINITE opdodes.
4128 * Spilling to memory makes sure that we always perform
4132 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
4134 ins->inst_left = sp [-1];
4135 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
4137 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4138 store->cil_code = ip;
4139 MONO_ADD_INS (bblock, store);
4141 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
4148 g_error ("opcode 0x%02x not handled", *ip);
4152 MonoClass *handle_class;
4154 CHECK_STACK_OVF (1);
4156 n = read32 (ip + 1);
4158 handle = mono_ldtoken (image, n, &handle_class);
4159 mono_class_init (handle_class);
4161 if (((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot)) {
4163 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
4165 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
4167 NEW_IMAGECONST (cfg, iargs [0], image);
4168 NEW_ICONST (cfg, iargs [1], n);
4169 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
4170 NEW_TEMPLOAD (cfg, res, temp);
4171 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4172 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
4173 MONO_ADD_INS (bblock, store);
4174 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4176 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method (image, read32 (ip + 6), NULL)) &&
4177 (cmethod->klass == mono_defaults.monotype_class->parent) &&
4178 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
4179 MonoClass *tclass = mono_class_from_mono_type (handle);
4180 mono_class_init (tclass);
4181 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
4182 ins->type = STACK_OBJ;
4183 ins->klass = cmethod->klass;
4186 NEW_PCONST (cfg, ins, handle);
4187 ins->type = STACK_VTYPE;
4188 ins->klass = handle_class;
4204 case CEE_ADD_OVF_UN:
4206 case CEE_MUL_OVF_UN:
4208 case CEE_SUB_OVF_UN:
4213 case CEE_ENDFINALLY:
4214 /* FIXME: check stack state */
4215 MONO_INST_NEW (cfg, ins, *ip);
4216 MONO_ADD_INS (bblock, ins);
4217 ins->cil_code = ip++;
4218 start_new_bblock = 1;
4223 if (*ip == CEE_LEAVE) {
4224 target = ip + 5 + (gint32)read32(ip + 1);
4226 target = ip + 2 + (signed char)(ip [1]);
4229 /* empty the stack */
4230 while (sp != stack_start) {
4231 MONO_INST_NEW (cfg, ins, CEE_POP);
4235 MONO_ADD_INS (bblock, ins);
4238 /* fixme: call fault handler ? */
4240 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
4242 for (tmp = handlers; tmp; tmp = tmp->next) {
4244 link_bblock (cfg, bblock, tblock);
4245 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
4247 ins->inst_target_bb = tblock;
4248 MONO_ADD_INS (bblock, ins);
4250 g_list_free (handlers);
4253 MONO_INST_NEW (cfg, ins, CEE_BR);
4255 MONO_ADD_INS (bblock, ins);
4256 GET_BBLOCK (cfg, bbhash, tblock, target);
4257 link_bblock (cfg, bblock, tblock);
4258 CHECK_BBLOCK (target, ip, tblock);
4259 ins->inst_target_bb = tblock;
4260 start_new_bblock = 1;
4262 if (*ip == CEE_LEAVE)
4271 MONO_INST_NEW (cfg, ins, *ip);
4273 handle_loaded_temps (cfg, bblock, stack_start, sp);
4274 MONO_ADD_INS (bblock, ins);
4275 ins->cil_code = ip++;
4276 ins->inst_i0 = sp [0];
4277 ins->inst_i1 = sp [1];
4285 /* trampoline mono specific opcodes */
4286 case MONO_CUSTOM_PREFIX: {
4288 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
4292 case CEE_MONO_FUNC1: {
4294 gpointer func = NULL;
4299 case MONO_MARSHAL_CONV_STR_LPWSTR:
4300 func = mono_string_to_utf16;
4302 case MONO_MARSHAL_CONV_LPWSTR_STR:
4303 func = mono_string_from_utf16;
4305 case MONO_MARSHAL_CONV_LPSTR_STR:
4306 func = mono_string_new_wrapper;
4308 case MONO_MARSHAL_CONV_STR_LPTSTR:
4309 case MONO_MARSHAL_CONV_STR_LPSTR:
4310 func = mono_string_to_utf8;
4312 case MONO_MARSHAL_CONV_STR_BSTR:
4313 func = mono_string_to_bstr;
4315 case MONO_MARSHAL_CONV_STR_TBSTR:
4316 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
4317 func = mono_string_to_ansibstr;
4319 case MONO_MARSHAL_CONV_SB_LPSTR:
4320 func = mono_string_builder_to_utf8;
4322 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
4323 func = mono_array_to_savearray;
4325 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
4326 func = mono_array_to_lparray;
4328 case MONO_MARSHAL_CONV_DEL_FTN:
4329 func = mono_delegate_to_ftnptr;
4331 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
4332 func = mono_marshal_string_array;
4335 g_warning ("unknown conversion %d\n", ip [2]);
4336 g_assert_not_reached ();
4339 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4340 NEW_TEMPLOAD (cfg, *sp, temp);
4344 inline_costs += 10 * num_calls++;
4347 case CEE_MONO_PROC2: {
4348 gpointer func = NULL;
4353 case MONO_MARSHAL_CONV_LPSTR_SB:
4354 func = mono_string_utf8_to_builder;
4356 case MONO_MARSHAL_FREE_ARRAY:
4357 func = mono_marshal_free_array;
4360 g_assert_not_reached ();
4363 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4365 inline_costs += 10 * num_calls++;
4368 case CEE_MONO_PROC3: {
4369 gpointer func = NULL;
4374 case MONO_MARSHAL_CONV_STR_BYVALSTR:
4375 func = mono_string_to_byvalstr;
4377 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
4378 func = mono_string_to_byvalwstr;
4381 g_assert_not_reached ();
4384 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4386 inline_costs += 10 * num_calls++;
4392 mono_emit_jit_icall (cfg, bblock, g_free, sp, ip);
4394 inline_costs += 10 * num_calls++;
4396 case CEE_MONO_LDPTR:
4397 CHECK_STACK_OVF (1);
4398 token = read32 (ip + 2);
4399 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
4403 inline_costs += 10 * num_calls++;
4405 case CEE_MONO_VTADDR:
4408 MONO_INST_NEW (cfg, ins, OP_VTADDR);
4410 ins->type = STACK_MP;
4411 ins->inst_left = *sp;
4415 case CEE_MONO_NEWOBJ: {
4416 MonoInst *iargs [2];
4418 CHECK_STACK_OVF (1);
4419 token = read32 (ip + 2);
4420 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4421 mono_class_init (klass);
4422 NEW_DOMAINCONST (cfg, iargs [0]);
4423 NEW_CLASSCONST (cfg, iargs [1], klass);
4424 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
4425 NEW_TEMPLOAD (cfg, *sp, temp);
4428 inline_costs += 10 * num_calls++;
4431 case CEE_MONO_OBJADDR:
4434 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
4436 ins->type = STACK_MP;
4437 ins->inst_left = *sp;
4441 case CEE_MONO_LDNATIVEOBJ:
4443 token = read32 (ip + 2);
4444 klass = mono_method_get_wrapper_data (method, token);
4445 g_assert (klass->valuetype);
4446 mono_class_init (klass);
4447 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
4451 case CEE_MONO_RETOBJ:
4452 g_assert (cfg->ret);
4453 g_assert (method->signature->pinvoke);
4457 token = read32 (ip + 2);
4458 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4460 NEW_RETLOADA (cfg, ins);
4461 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
4463 if (sp != stack_start)
4466 MONO_INST_NEW (cfg, ins, CEE_BR);
4468 ins->inst_target_bb = end_bblock;
4469 MONO_ADD_INS (bblock, ins);
4470 link_bblock (cfg, bblock, end_bblock);
4471 start_new_bblock = 1;
4475 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
4483 /* somewhat similar to LDTOKEN */
4484 MonoInst *addr, *vtvar;
4486 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
4488 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4489 addr->cil_code = ip;
4490 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
4492 ins->inst_left = addr;
4493 MONO_ADD_INS (bblock, ins);
4494 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4507 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
4508 MONO_INST_NEW (cfg, ins, cmp->opcode);
4510 cmp->inst_i0 = sp [0];
4511 cmp->inst_i1 = sp [1];
4515 cmp->opcode = OP_COMPARE;
4517 ins->type = STACK_I4;
4527 CHECK_STACK_OVF (1);
4528 n = read32 (ip + 2);
4529 if (method->wrapper_type != MONO_WRAPPER_NONE)
4530 cmethod = mono_method_get_wrapper_data (method, n);
4532 cmethod = mono_get_method (image, n, NULL);
4535 * We can't do this in mono_ldftn, since it is used in
4536 * the synchronized wrapper, leading to an infinite loop.
4538 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
4539 cmethod = mono_marshal_get_synchronized_wrapper (cmethod);
4542 mono_class_init (cmethod->klass);
4543 handle_loaded_temps (cfg, bblock, stack_start, sp);
4545 NEW_METHODCONST (cfg, argconst, cmethod);
4546 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
4547 NEW_TEMPLOAD (cfg, *sp, temp);
4551 inline_costs += 10 * num_calls++;
4554 case CEE_LDVIRTFTN: {
4559 n = read32 (ip + 2);
4560 if (method->wrapper_type != MONO_WRAPPER_NONE)
4561 cmethod = mono_method_get_wrapper_data (method, n);
4563 cmethod = mono_get_method (image, n, NULL);
4565 mono_class_init (cmethod->klass);
4566 handle_loaded_temps (cfg, bblock, stack_start, sp);
4570 NEW_METHODCONST (cfg, args [1], cmethod);
4571 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
4572 NEW_TEMPLOAD (cfg, *sp, temp);
4576 inline_costs += 10 * num_calls++;
4580 CHECK_STACK_OVF (1);
4581 NEW_ARGLOAD (cfg, ins, read16 (ip + 2));
4587 CHECK_STACK_OVF (1);
4588 NEW_ARGLOADA (cfg, ins, read16 (ip + 2));
4596 handle_loaded_temps (cfg, bblock, stack_start, sp);
4597 n = read16 (ip + 2);
4598 NEW_ARGSTORE (cfg, ins, n, *sp);
4600 if (ins->opcode == CEE_STOBJ) {
4601 NEW_ARGLOADA (cfg, ins, n);
4602 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4604 MONO_ADD_INS (bblock, ins);
4608 CHECK_STACK_OVF (1);
4609 NEW_LOCLOAD (cfg, ins, read16 (ip + 2));
4615 CHECK_STACK_OVF (1);
4616 NEW_LOCLOADA (cfg, ins, read16 (ip + 2));
4624 n = read16 (ip + 2);
4625 handle_loaded_temps (cfg, bblock, stack_start, sp);
4626 NEW_LOCSTORE (cfg, ins, n, *sp);
4628 if (ins->opcode == CEE_STOBJ) {
4629 NEW_LOCLOADA (cfg, ins, n);
4630 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4632 MONO_ADD_INS (bblock, ins);
4639 if (sp != stack_start)
4641 MONO_INST_NEW (cfg, ins, 256 + ip [1]);
4642 ins->inst_left = *sp;
4645 if (header->init_locals)
4646 ins->flags |= MONO_INST_INIT;
4650 /* FIXME: set init flag if locals init is set in this method */
4652 case CEE_ENDFILTER: {
4653 MonoExceptionClause *clause, *nearest;
4654 int cc, nearest_num;
4658 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
4660 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
4661 ins->inst_left = *sp;
4663 MONO_ADD_INS (bblock, ins);
4664 start_new_bblock = 1;
4668 for (cc = 0; cc < header->num_clauses; ++cc) {
4669 clause = &header->clauses [cc];
4670 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
4671 (!nearest || (clause->token_or_filter > nearest->token_or_filter))) {
4677 filter_lengths [nearest_num] = (ip - header->code) - nearest->token_or_filter;
4681 case CEE_UNALIGNED_:
4682 ins_flag |= MONO_INST_UNALIGNED;
4686 ins_flag |= MONO_INST_VOLATILE;
4690 ins_flag |= MONO_INST_TAILCALL;
4696 token = read32 (ip + 2);
4697 if (method->wrapper_type != MONO_WRAPPER_NONE)
4698 klass = mono_method_get_wrapper_data (method, token);
4700 klass = mono_class_get (image, token);
4701 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
4707 MonoInst *iargs [3];
4713 handle_loaded_temps (cfg, bblock, stack_start, sp);
4714 if (ip [1] == CEE_CPBLK) {
4715 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
4717 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
4725 /* FIXME: check we are in a catch handler */
4726 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
4727 load->cil_code = ip;
4728 MONO_INST_NEW (cfg, ins, CEE_THROW);
4729 ins->inst_left = load;
4731 MONO_ADD_INS (bblock, ins);
4733 start_new_bblock = 1;
4738 CHECK_STACK_OVF (1);
4739 token = read32 (ip + 2);
4740 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
4741 MonoType *type = mono_type_create_from_typespec (image, token);
4742 token = mono_type_size (type, &align);
4743 mono_metadata_free_type (type);
4745 MonoClass *szclass = mono_class_get (image, token);
4746 mono_class_init (szclass);
4747 token = mono_class_value_size (szclass, &align);
4749 NEW_ICONST (cfg, ins, token);
4754 case CEE_REFANYTYPE:
4755 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
4758 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
4763 g_error ("opcode 0x%02x not handled", *ip);
4766 if (start_new_bblock != 1)
4769 bblock->cil_length = ip - bblock->cil_code;
4770 bblock->next_bb = end_bblock;
4771 link_bblock (cfg, bblock, end_bblock);
4773 if (cfg->method == method && cfg->domainvar) {
4777 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
4778 call->signature = helper_sig_domain_get;
4779 call->inst.type = STACK_PTR;
4780 call->fptr = mono_domain_get;
4781 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, (MonoInst*)call);
4783 MONO_ADD_INS (init_localsbb, store);
4786 if (header->init_locals) {
4788 for (i = 0; i < header->num_locals; ++i) {
4789 int t = header->locals [i]->type;
4790 if (t == MONO_TYPE_VALUETYPE && header->locals [i]->data.klass->enumtype)
4791 t = header->locals [i]->data.klass->enum_basetype->type;
4792 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
4793 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
4794 NEW_ICONST (cfg, ins, 0);
4795 NEW_LOCSTORE (cfg, store, i, ins);
4796 MONO_ADD_INS (init_localsbb, store);
4797 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
4798 MONO_INST_NEW (cfg, ins, OP_I8CONST);
4799 ins->type = STACK_I8;
4801 NEW_LOCSTORE (cfg, store, i, ins);
4802 MONO_ADD_INS (init_localsbb, store);
4803 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
4804 MONO_INST_NEW (cfg, ins, OP_R8CONST);
4805 ins->type = STACK_R8;
4806 ins->inst_p0 = (void*)&r8_0;
4807 NEW_LOCSTORE (cfg, store, i, ins);
4808 MONO_ADD_INS (init_localsbb, store);
4809 } else if (t == MONO_TYPE_VALUETYPE) {
4810 NEW_LOCLOADA (cfg, ins, i);
4811 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (header->locals [i]), NULL, NULL);
4814 NEW_PCONST (cfg, ins, NULL);
4815 NEW_LOCSTORE (cfg, store, i, ins);
4816 MONO_ADD_INS (init_localsbb, store);
4822 /* resolve backward branches in the middle of an existing basic block */
4823 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
4825 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
4826 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
4827 if (tblock != start_bblock) {
4829 split_bblock (cfg, tblock, bblock);
4830 l = bblock->cil_code - header->code;
4831 bblock->cil_length = tblock->cil_length - l;
4832 tblock->cil_length = l;
4834 g_print ("recheck failed.\n");
4838 /* we compute regions here, because the length of filter clauses is not known in advance.
4839 * It is computed in the CEE_ENDFILTER case in the above switch statement*/
4840 if (cfg->method == method) {
4842 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
4843 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
4844 if (cfg->verbose_level > 2)
4845 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
4848 g_hash_table_destroy (bbhash);
4851 dont_inline = g_list_remove (dont_inline, method);
4852 return inline_costs;
4855 if (cfg->method != method)
4856 g_hash_table_destroy (bbhash);
4857 dont_inline = g_list_remove (dont_inline, method);
4861 if (cfg->method != method)
4862 g_hash_table_destroy (bbhash);
4863 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
4864 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
4865 dont_inline = g_list_remove (dont_inline, method);
4870 mono_print_tree (MonoInst *tree) {
4876 arity = mono_burg_arity [tree->opcode];
4878 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
4880 switch (tree->opcode) {
4882 printf ("[%d]", tree->inst_c0);
4885 printf ("[%lld]", tree->inst_l);
4888 printf ("[%f]", *(double*)tree->inst_p0);
4891 printf ("[%f]", *(float*)tree->inst_p0);
4895 printf ("[%d]", tree->inst_c0);
4898 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
4901 printf ("[%s]", mono_arch_regname (tree->dreg));
4904 printf ("[%s]", tree->inst_newa_class->name);
4905 mono_print_tree (tree->inst_newa_len);
4916 case OP_VOIDCALLVIRT: {
4917 MonoCallInst *call = (MonoCallInst*)tree;
4919 printf ("[%s]", call->method->name);
4924 printf ("[%d (", tree->inst_c0);
4925 for (i = 0; i < tree->inst_phi_args [0]; i++) {
4928 printf ("%d", tree->inst_phi_args [i + 1]);
4940 printf ("[B%d]", tree->inst_target_bb->block_num);
4950 case OP_VOIDCALL_REG:
4951 mono_print_tree (tree->inst_left);
4963 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
4964 mono_print_tree (tree->inst_left);
4968 mono_print_tree (tree->inst_left);
4970 mono_print_tree (tree->inst_right);
4980 create_helper_signature (void)
4982 /* FIXME: set call conv */
4983 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
4984 helper_sig_newarr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4985 helper_sig_newarr->params [0] = helper_sig_newarr->params [1] = &mono_defaults.int_class->byval_arg;
4986 helper_sig_newarr->ret = &mono_defaults.object_class->byval_arg;
4987 helper_sig_newarr->params [2] = &mono_defaults.int32_class->byval_arg;
4988 helper_sig_newarr->pinvoke = 1;
4990 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
4991 helper_sig_newarr_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4992 helper_sig_newarr_specific->params [0] = &mono_defaults.int_class->byval_arg;
4993 helper_sig_newarr_specific->params [1] = &mono_defaults.int32_class->byval_arg;
4994 helper_sig_newarr_specific->ret = &mono_defaults.object_class->byval_arg;
4995 helper_sig_newarr_specific->pinvoke = 1;
4997 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
4998 helper_sig_object_new = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4999 helper_sig_object_new->params [0] = helper_sig_object_new->params [1] = &mono_defaults.int_class->byval_arg;
5000 helper_sig_object_new->ret = &mono_defaults.object_class->byval_arg;
5001 helper_sig_object_new->pinvoke = 1;
5003 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
5004 helper_sig_object_new_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5005 helper_sig_object_new_specific->params [0] = &mono_defaults.int_class->byval_arg;
5006 helper_sig_object_new_specific->ret = &mono_defaults.object_class->byval_arg;
5007 helper_sig_object_new_specific->pinvoke = 1;
5009 /* void* mono_method_compile (MonoMethod*) */
5010 helper_sig_compile = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5011 helper_sig_compile->params [0] = helper_sig_compile->ret = &mono_defaults.int_class->byval_arg;
5012 helper_sig_compile->pinvoke = 1;
5014 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
5015 helper_sig_compile_virt = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5016 helper_sig_compile_virt->params [0] = &mono_defaults.object_class->byval_arg;
5017 helper_sig_compile_virt->params [1] = helper_sig_compile_virt->ret = &mono_defaults.int_class->byval_arg;
5018 helper_sig_compile_virt->pinvoke = 1;
5020 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
5021 helper_sig_ldstr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5022 helper_sig_ldstr->params [0] = helper_sig_ldstr->params [1] = &mono_defaults.int_class->byval_arg;
5023 helper_sig_ldstr->params [2] = &mono_defaults.int32_class->byval_arg;
5024 helper_sig_ldstr->ret = &mono_defaults.object_class->byval_arg;
5025 helper_sig_ldstr->pinvoke = 1;
5027 /* MonoDomain *mono_domain_get (void) */
5028 helper_sig_domain_get = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5029 helper_sig_domain_get->ret = &mono_defaults.int_class->byval_arg;
5030 helper_sig_domain_get->pinvoke = 1;
5032 /* void* stelem_ref (MonoArray *, int index, MonoObject *) */
5033 helper_sig_stelem_ref = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5034 helper_sig_stelem_ref->params [0] = &mono_defaults.array_class->byval_arg;
5035 helper_sig_stelem_ref->params [1] = &mono_defaults.int32_class->byval_arg;
5036 helper_sig_stelem_ref->params [2] = &mono_defaults.object_class->byval_arg;
5037 helper_sig_stelem_ref->ret = &mono_defaults.void_class->byval_arg;
5038 helper_sig_stelem_ref->pinvoke = 1;
5040 /* long amethod (long, long) */
5041 helper_sig_long_long_long = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5042 helper_sig_long_long_long->params [0] = helper_sig_long_long_long->params [1] =
5043 &mono_defaults.int64_class->byval_arg;
5044 helper_sig_long_long_long->ret = &mono_defaults.int64_class->byval_arg;
5045 helper_sig_long_long_long->pinvoke = 1;
5047 /* object amethod (intptr) */
5048 helper_sig_obj_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5049 helper_sig_obj_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5050 helper_sig_obj_ptr->ret = &mono_defaults.object_class->byval_arg;
5051 helper_sig_obj_ptr->pinvoke = 1;
5053 /* void amethod (intptr) */
5054 helper_sig_void_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5055 helper_sig_void_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5056 helper_sig_void_ptr->ret = &mono_defaults.void_class->byval_arg;
5057 helper_sig_void_ptr->pinvoke = 1;
5059 /* void amethod (MonoObject *obj) */
5060 helper_sig_void_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5061 helper_sig_void_obj->params [0] = &mono_defaults.object_class->byval_arg;
5062 helper_sig_void_obj->ret = &mono_defaults.void_class->byval_arg;
5063 helper_sig_void_obj->pinvoke = 1;
5065 /* intptr amethod (void) */
5066 helper_sig_ptr_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5067 helper_sig_ptr_void->ret = &mono_defaults.int_class->byval_arg;
5068 helper_sig_ptr_void->pinvoke = 1;
5070 /* void amethod (intptr, intptr) */
5071 helper_sig_void_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5072 helper_sig_void_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5073 helper_sig_void_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5074 helper_sig_void_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
5075 helper_sig_void_ptr_ptr->pinvoke = 1;
5077 /* void amethod (intptr, intptr, intptr) */
5078 helper_sig_void_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5079 helper_sig_void_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5080 helper_sig_void_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5081 helper_sig_void_ptr_ptr_ptr->params [2] = &mono_defaults.int_class->byval_arg;
5082 helper_sig_void_ptr_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
5083 helper_sig_void_ptr_ptr_ptr->pinvoke = 1;
5085 /* intptr amethod (intptr, intptr) */
5086 helper_sig_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5087 helper_sig_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5088 helper_sig_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5089 helper_sig_ptr_ptr_ptr->ret = &mono_defaults.int_class->byval_arg;
5090 helper_sig_ptr_ptr_ptr->pinvoke = 1;
5092 /* IntPtr amethod (object) */
5093 helper_sig_ptr_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5094 helper_sig_ptr_obj->params [0] = &mono_defaults.object_class->byval_arg;
5095 helper_sig_ptr_obj->ret = &mono_defaults.int_class->byval_arg;
5096 helper_sig_ptr_obj->pinvoke = 1;
5098 /* IntPtr amethod (int) */
5099 helper_sig_ptr_int = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5100 helper_sig_ptr_int->params [0] = &mono_defaults.int32_class->byval_arg;
5101 helper_sig_ptr_int->ret = &mono_defaults.int_class->byval_arg;
5102 helper_sig_ptr_int->pinvoke = 1;
5104 /* long amethod (long, guint32) */
5105 helper_sig_long_long_int = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5106 helper_sig_long_long_int->params [0] = &mono_defaults.int64_class->byval_arg;
5107 helper_sig_long_long_int->params [1] = &mono_defaults.int32_class->byval_arg;
5108 helper_sig_long_long_int->ret = &mono_defaults.int64_class->byval_arg;
5109 helper_sig_long_long_int->pinvoke = 1;
5111 /* ulong amethod (double) */
5112 helper_sig_ulong_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5113 helper_sig_ulong_double->params [0] = &mono_defaults.double_class->byval_arg;
5114 helper_sig_ulong_double->ret = &mono_defaults.uint64_class->byval_arg;
5115 helper_sig_ulong_double->pinvoke = 1;
5117 /* long amethod (double) */
5118 helper_sig_long_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5119 helper_sig_long_double->params [0] = &mono_defaults.double_class->byval_arg;
5120 helper_sig_long_double->ret = &mono_defaults.int64_class->byval_arg;
5121 helper_sig_long_double->pinvoke = 1;
5123 /* uint amethod (double) */
5124 helper_sig_uint_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5125 helper_sig_uint_double->params [0] = &mono_defaults.double_class->byval_arg;
5126 helper_sig_uint_double->ret = &mono_defaults.uint32_class->byval_arg;
5127 helper_sig_uint_double->pinvoke = 1;
5129 /* int amethod (double) */
5130 helper_sig_int_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5131 helper_sig_int_double->params [0] = &mono_defaults.double_class->byval_arg;
5132 helper_sig_int_double->ret = &mono_defaults.int32_class->byval_arg;
5133 helper_sig_int_double->pinvoke = 1;
5135 /* void initobj (intptr, int size) */
5136 helper_sig_initobj = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5137 helper_sig_initobj->params [0] = &mono_defaults.int_class->byval_arg;
5138 helper_sig_initobj->params [1] = &mono_defaults.int32_class->byval_arg;
5139 helper_sig_initobj->ret = &mono_defaults.void_class->byval_arg;
5140 helper_sig_initobj->pinvoke = 1;
5142 /* void memcpy (intptr, intptr, int size) */
5143 helper_sig_memcpy = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5144 helper_sig_memcpy->params [0] = &mono_defaults.int_class->byval_arg;
5145 helper_sig_memcpy->params [1] = &mono_defaults.int_class->byval_arg;
5146 helper_sig_memcpy->params [2] = &mono_defaults.int32_class->byval_arg;
5147 helper_sig_memcpy->ret = &mono_defaults.void_class->byval_arg;
5148 helper_sig_memcpy->pinvoke = 1;
5150 /* void memset (intptr, int val, int size) */
5151 helper_sig_memset = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5152 helper_sig_memset->params [0] = &mono_defaults.int_class->byval_arg;
5153 helper_sig_memset->params [1] = &mono_defaults.int32_class->byval_arg;
5154 helper_sig_memset->params [2] = &mono_defaults.int32_class->byval_arg;
5155 helper_sig_memset->ret = &mono_defaults.void_class->byval_arg;
5156 helper_sig_memset->pinvoke = 1;
5159 static GHashTable *jit_icall_hash_name = NULL;
5160 static GHashTable *jit_icall_hash_addr = NULL;
5163 mono_find_jit_icall_by_name (const char *name)
5165 g_assert (jit_icall_hash_name);
5167 //printf ("lookup addr %s %p\n", name, g_hash_table_lookup (jit_icall_hash_name, name));
5168 return g_hash_table_lookup (jit_icall_hash_name, name);
5172 mono_find_jit_icall_by_addr (gconstpointer addr)
5174 g_assert (jit_icall_hash_addr);
5176 return g_hash_table_lookup (jit_icall_hash_addr, (gpointer)addr);
5180 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
5183 MonoMethod *wrapper;
5185 if (callinfo->wrapper)
5186 return callinfo->wrapper;
5187 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
5188 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
5189 callinfo->wrapper = mono_jit_compile_method (wrapper);
5191 return callinfo->wrapper;
5195 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save)
5197 MonoJitICallInfo *info;
5202 if (!jit_icall_hash_name) {
5203 jit_icall_hash_name = g_hash_table_new (g_str_hash, g_str_equal);
5204 jit_icall_hash_addr = g_hash_table_new (NULL, NULL);
5207 if (g_hash_table_lookup (jit_icall_hash_name, name)) {
5208 g_warning ("jit icall already defined \"%s\"\n", name);
5209 g_assert_not_reached ();
5212 info = g_new (MonoJitICallInfo, 1);
5219 #ifdef MONO_USE_EXC_TABLES
5220 || mono_arch_has_unwind_info (func)
5223 info->wrapper = func;
5225 info->wrapper = NULL;
5226 mono_icall_get_wrapper (info);
5229 g_hash_table_insert (jit_icall_hash_name, (gpointer)info->name, info);
5230 g_hash_table_insert (jit_icall_hash_addr, (gpointer)func, info);
5231 if (func != info->wrapper)
5232 g_hash_table_insert (jit_icall_hash_addr, (gpointer)info->wrapper, info);
5237 static GHashTable *emul_opcode_hash = NULL;
5239 static MonoJitICallInfo *
5240 mono_find_jit_opcode_emulation (int opcode)
5242 if (emul_opcode_hash)
5243 return g_hash_table_lookup (emul_opcode_hash, (gpointer)opcode);
5249 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func, gboolean no_throw)
5251 MonoJitICallInfo *info;
5253 if (!emul_opcode_hash)
5254 emul_opcode_hash = g_hash_table_new (NULL, NULL);
5256 g_assert (!sig->hasthis);
5257 g_assert (sig->param_count < 3);
5259 info = mono_register_jit_icall (func, name, sig, no_throw);
5261 g_hash_table_insert (emul_opcode_hash, (gpointer)opcode, info);
5265 decompose_foreach (MonoInst *tree, gpointer data)
5267 static MonoJitICallInfo *newarr_info = NULL;
5268 static MonoJitICallInfo *newarr_specific_info = NULL;
5269 MonoJitICallInfo *info;
5271 switch (tree->opcode) {
5273 MonoCompile *cfg = data;
5274 MonoInst *iargs [3];
5277 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
5278 g_assert (newarr_info);
5279 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
5280 g_assert (newarr_specific_info);
5283 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
5284 NEW_DOMAINCONST (cfg, iargs [0]);
5285 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
5286 iargs [2] = tree->inst_newa_len;
5291 MonoVTable *vtable = mono_class_vtable (cfg->domain,
5292 mono_array_class_get (&tree->inst_newa_class->byval_arg, 1));
5294 NEW_PCONST (cfg, iargs [0], vtable);
5295 iargs [1] = tree->inst_newa_len;
5297 info = newarr_specific_info;
5300 mono_emulate_opcode (cfg, tree, iargs, info);
5310 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
5312 switch (mono_burg_arity [tree->opcode]) {
5315 mono_inst_foreach (tree->inst_left, func, data);
5318 mono_inst_foreach (tree->inst_left, func, data);
5319 mono_inst_foreach (tree->inst_right, func, data);
5322 g_assert_not_reached ();
5329 mono_print_bb_code (MonoBasicBlock *bb) {
5331 MonoInst *c = bb->code;
5333 mono_print_tree (c);
5342 print_dfn (MonoCompile *cfg) {
5347 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
5349 for (i = 0; i < cfg->num_bblocks; ++i) {
5350 bb = cfg->bblocks [i];
5352 char* code1, *code2;
5353 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
5354 if (bb->last_ins->cil_code)
5355 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
5357 code2 = g_strdup ("");
5359 code1 [strlen (code1) - 1] = 0;
5360 code = g_strdup_printf ("%s -> %s", code1, code2);
5364 code = g_strdup ("\n");
5365 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
5367 MonoInst *c = bb->code;
5369 mono_print_tree (c);
5377 g_print ("\tprev:");
5378 for (j = 0; j < bb->in_count; ++j) {
5379 g_print (" BB%d", bb->in_bb [j]->block_num);
5381 g_print ("\t\tsucc:");
5382 for (j = 0; j < bb->out_count; ++j) {
5383 g_print (" BB%d", bb->out_bb [j]->block_num);
5385 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
5388 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
5391 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
5393 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
5401 * returns the offset used by spillvar. It allocates a new
5402 * spill variable if necessary.
5405 mono_spillvar_offset (MonoCompile *cfg, int spillvar)
5407 MonoSpillInfo **si, *info;
5410 si = &cfg->spill_info;
5412 while (i <= spillvar) {
5415 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
5417 cfg->stack_offset -= sizeof (gpointer);
5418 info->offset = cfg->stack_offset;
5422 return (*si)->offset;
5428 g_assert_not_reached ();
5433 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
5437 g_assert (bb->code);
5438 bb->last_ins->next = inst;
5439 bb->last_ins = inst;
5441 bb->last_ins = bb->code = inst;
5446 mono_destroy_compile (MonoCompile *cfg)
5448 //mono_mempool_stats (cfg->mempool);
5449 g_hash_table_destroy (cfg->bb_hash);
5451 mono_regstate_free (cfg->rs);
5452 mono_mempool_destroy (cfg->mempool);
5453 g_list_free (cfg->ldstr_list);
5455 g_free (cfg->varinfo);
5461 mono_get_lmf_addr (void)
5463 MonoJitTlsData *jit_tls;
5465 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
5466 return &jit_tls->lmf;
5468 g_assert_not_reached ();
5473 * mono_thread_abort:
5474 * @obj: exception object
5476 * abort the thread, print exception information and stack trace
5479 mono_thread_abort (MonoObject *obj)
5481 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
5489 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
5491 MonoJitTlsData *jit_tls;
5494 jit_tls = g_new0 (MonoJitTlsData, 1);
5496 TlsSetValue (mono_jit_tls_id, jit_tls);
5498 jit_tls->abort_func = mono_thread_abort;
5499 jit_tls->end_of_stack = stack_start;
5501 lmf = g_new0 (MonoLMF, 1);
5507 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
5510 mono_thread_abort_dummy (MonoObject *obj)
5512 if (mono_thread_attach_aborted_cb)
5513 mono_thread_attach_aborted_cb (obj);
5515 mono_thread_abort (obj);
5519 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
5521 MonoJitTlsData *jit_tls;
5524 jit_tls = g_new0 (MonoJitTlsData, 1);
5526 TlsSetValue (mono_jit_tls_id, jit_tls);
5528 jit_tls->abort_func = mono_thread_abort_dummy;
5529 jit_tls->end_of_stack = stack_start;
5531 lmf = g_new0 (MonoLMF, 1);
5538 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
5540 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
5544 ji->data.target = target;
5545 ji->next = cfg->patch_info;
5547 cfg->patch_info = ji;
5551 mono_remove_patch_info (MonoCompile *cfg, int ip)
5553 MonoJumpInfo **ji = &cfg->patch_info;
5556 if ((*ji)->ip.i == ip)
5559 ji = &((*ji)->next);
5564 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
5565 MonoJitICallInfo *info;
5567 switch (mono_burg_arity [tree->opcode]) {
5570 dec_foreach (tree->inst_left, cfg);
5572 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
5573 MonoInst *iargs [2];
5575 iargs [0] = tree->inst_left;
5577 mono_emulate_opcode (cfg, tree, iargs, info);
5583 if (tree->opcode == OP_LMUL
5584 && (cfg->opt & MONO_OPT_INTRINS)
5585 && (tree->inst_left->opcode == CEE_CONV_I8
5586 || tree->inst_left->opcode == CEE_CONV_U8)
5587 && tree->inst_left->inst_left->type == STACK_I4
5588 && (tree->inst_right->opcode == CEE_CONV_I8
5589 || tree->inst_right->opcode == CEE_CONV_U8)
5590 && tree->inst_right->inst_left->type == STACK_I4) {
5591 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
5592 tree->inst_left = tree->inst_left->inst_left;
5593 tree->inst_right = tree->inst_right->inst_left;
5594 dec_foreach (tree, cfg);
5595 } else if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
5596 MonoInst *iargs [2];
5598 iargs [0] = tree->inst_i0;
5599 iargs [1] = tree->inst_i1;
5601 mono_emulate_opcode (cfg, tree, iargs, info);
5603 dec_foreach (iargs [0], cfg);
5604 dec_foreach (iargs [1], cfg);
5607 dec_foreach (tree->inst_left, cfg);
5608 dec_foreach (tree->inst_right, cfg);
5612 g_assert_not_reached ();
5614 decompose_foreach (tree, cfg);
5618 decompose_pass (MonoCompile *cfg) {
5621 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5624 cfg->prev_ins = NULL;
5625 for (tree = cfg->cbb->code; tree; tree = tree->next) {
5626 dec_foreach (tree, cfg);
5627 cfg->prev_ins = tree;
5633 nullify_basic_block (MonoBasicBlock *bb)
5640 bb->code = bb->last_ins = NULL;
5641 bb->cil_code = NULL;
5645 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
5649 for (i = 0; i < bb->out_count; i++) {
5650 MonoBasicBlock *ob = bb->out_bb [i];
5653 if (bb->out_count > 1) {
5654 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
5658 bb->out_bb [i] = repl;
5665 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
5669 for (i = 0; i < bb->in_count; i++) {
5670 MonoBasicBlock *ib = bb->in_bb [i];
5673 if (bb->in_count > 1) {
5674 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
5678 bb->in_bb [i] = repl;
5685 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
5689 for (i = 0; i < bb->out_count; i++) {
5690 MonoBasicBlock *ob = bb->out_bb [i];
5691 for (j = 0; j < ob->in_count; j++) {
5692 if (ob->in_bb [j] == orig) {
5693 ob->in_bb [j] = repl;
5702 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
5704 bb->out_count = bbn->out_count;
5705 bb->out_bb = bbn->out_bb;
5707 replace_basic_block (bb, bbn, bb);
5711 bb->last_ins->next = bbn->code;
5712 bb->last_ins = bbn->last_ins;
5715 bb->code = bbn->code;
5716 bb->last_ins = bbn->last_ins;
5718 bb->next_bb = bbn->next_bb;
5719 nullify_basic_block (bbn);
5723 optimize_branches (MonoCompile *cfg) {
5724 int i, changed = FALSE;
5725 MonoBasicBlock *bb, *bbn;
5730 /* we skip the entry block (exit is handled specially instead ) */
5731 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
5733 /* dont touch code inside exception clauses */
5734 if (bb->region != -1)
5737 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
5738 if (cfg->verbose_level > 2)
5739 g_print ("nullify block triggered %d\n", bbn->block_num);
5741 bb->next_bb = bbn->next_bb;
5743 for (i = 0; i < bbn->out_count; i++)
5744 replace_in_block (bbn->out_bb [i], bbn, NULL);
5746 nullify_basic_block (bbn);
5750 if (bb->out_count == 1) {
5751 bbn = bb->out_bb [0];
5753 /* conditional branches where true and false targets are the same can be also replaced with CEE_BR */
5754 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
5755 bb->last_ins->opcode = CEE_BR;
5756 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
5758 if (cfg->verbose_level > 2)
5759 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
5762 if (bb->region == bbn->region && bb->next_bb == bbn) {
5763 /* the block are in sequence anyway ... */
5765 /* branches to the following block can be removed */
5766 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
5767 bb->last_ins->opcode = CEE_NOP;
5769 if (cfg->verbose_level > 2)
5770 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
5773 if (bbn->in_count == 1) {
5775 if (bbn != cfg->bb_exit) {
5776 if (cfg->verbose_level > 2)
5777 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
5778 merge_basic_blocks (bb, bbn);
5782 //mono_print_bb_code (bb);
5792 /* we skip the entry block (exit is handled specially instead ) */
5793 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
5795 /* dont touch code inside exception clauses */
5796 if (bb->region != -1)
5799 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
5800 if (cfg->verbose_level > 2) {
5801 g_print ("nullify block triggered %d\n", bbn->block_num);
5803 bb->next_bb = bbn->next_bb;
5805 for (i = 0; i < bbn->out_count; i++)
5806 replace_in_block (bbn->out_bb [i], bbn, NULL);
5808 nullify_basic_block (bbn);
5814 if (bb->out_count == 1) {
5815 bbn = bb->out_bb [0];
5817 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
5818 bbn = bb->last_ins->inst_target_bb;
5819 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
5820 bbn->code->inst_target_bb->region == bb->region) {
5822 if (cfg->verbose_level > 2)
5823 g_print ("in %s branch to branch triggered %d -> %d\n", cfg->method->name,
5824 bb->block_num, bbn->block_num);
5826 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
5827 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
5832 } else if (bb->out_count == 2) {
5833 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
5834 bbn = bb->last_ins->inst_true_bb;
5835 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
5836 bbn->code->inst_target_bb->region == bb->region) {
5837 if (cfg->verbose_level > 2)
5838 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
5839 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
5842 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
5844 replace_in_block (bbn, bb, NULL);
5846 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
5847 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
5849 link_bblock (cfg, bb, bbn->code->inst_target_bb);
5855 bbn = bb->last_ins->inst_false_bb;
5856 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
5857 bbn->code->inst_target_bb->region == bb->region) {
5858 if (cfg->verbose_level > 2)
5859 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
5860 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
5863 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
5865 replace_in_block (bbn, bb, NULL);
5867 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
5868 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
5870 link_bblock (cfg, bb, bbn->code->inst_target_bb);
5883 mono_compile_create_vars (MonoCompile *cfg)
5885 MonoMethodSignature *sig;
5886 MonoMethodHeader *header;
5889 header = ((MonoMethodNormal *)cfg->method)->header;
5891 sig = cfg->method->signature;
5893 if (!MONO_TYPE_IS_VOID (sig->ret)) {
5894 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
5895 cfg->ret->opcode = OP_RETARG;
5896 cfg->ret->inst_vtype = sig->ret;
5897 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
5901 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
5903 for (i = 0; i < sig->param_count; ++i)
5904 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
5906 cfg->locals_start = cfg->num_varinfo;
5908 for (i = 0; i < header->num_locals; ++i)
5909 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
5914 mono_print_code (MonoCompile *cfg)
5918 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5919 MonoInst *tree = bb->code;
5924 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
5926 for (; tree; tree = tree->next) {
5927 mono_print_tree (tree);
5932 bb->last_ins->next = NULL;
5937 extern const char * const mono_burg_rule_string [];
5940 emit_state (MonoCompile *cfg, MBState *state, int goal)
5943 int ern = mono_burg_rule (state, goal);
5944 const guint16 *nts = mono_burg_nts [ern];
5947 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
5951 // state->reg1 = state->reg2; /* chain rule */
5953 state->reg1 = mono_regstate_next_int (cfg->rs);
5954 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
5957 state->reg1 = mono_regstate_next_int (cfg->rs);
5958 state->reg2 = mono_regstate_next_int (cfg->rs);
5961 state->reg1 = mono_regstate_next_float (cfg->rs);
5968 mono_burg_kids (state, ern, kids);
5970 emit_state (cfg, kids [0], nts [0]);
5972 emit_state (cfg, kids [1], nts [1]);
5974 g_assert (!nts [3]);
5975 emit_state (cfg, kids [2], nts [2]);
5980 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
5981 if ((emit = mono_burg_func [ern]))
5982 emit (state, state->tree, cfg);
5985 #define DEBUG_SELECTION
5988 mini_select_instructions (MonoCompile *cfg)
5990 static int reverse_map [] = {
5991 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
5992 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
5994 static int reverse_fmap [] = {
5995 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
5996 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
5998 static int reverse_lmap [] = {
5999 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
6000 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
6005 cfg->state_pool = mono_mempool_new ();
6006 cfg->rs = mono_regstate_new ();
6008 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6009 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode) &&
6010 bb->next_bb != bb->last_ins->inst_false_bb) {
6012 if (bb->next_bb == bb->last_ins->inst_true_bb) {
6013 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
6014 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
6015 bb->last_ins->inst_false_bb = tmp;
6017 if (bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
6018 bb->last_ins->opcode = reverse_map [bb->last_ins->opcode - CEE_BEQ];
6019 } else if (bb->last_ins->opcode >= OP_FBEQ && bb->last_ins->opcode <= OP_FBLT_UN) {
6020 bb->last_ins->opcode = reverse_fmap [bb->last_ins->opcode - OP_FBEQ];
6021 } else if (bb->last_ins->opcode >= OP_LBEQ && bb->last_ins->opcode <= OP_LBLT_UN) {
6022 bb->last_ins->opcode = reverse_lmap [bb->last_ins->opcode - OP_LBEQ];
6025 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
6026 inst->opcode = CEE_BR;
6027 inst->inst_target_bb = bb->last_ins->inst_false_bb;
6028 mono_bblock_add_inst (bb, inst);
6033 #ifdef DEBUG_SELECTION
6034 if (cfg->verbose_level >= 4) {
6035 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6036 MonoInst *tree = bb->code;
6037 g_print ("DUMP BLOCK %d:\n", bb->block_num);
6040 for (; tree; tree = tree->next) {
6041 mono_print_tree (tree);
6048 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6049 MonoInst *tree = bb->code, *next;
6055 bb->last_ins = NULL;
6058 mono_regstate_reset (cfg->rs);
6060 #ifdef DEBUG_SELECTION
6061 if (cfg->verbose_level >= 3)
6062 g_print ("LABEL BLOCK %d:\n", bb->block_num);
6064 for (; tree; tree = next) {
6066 #ifdef DEBUG_SELECTION
6067 if (cfg->verbose_level >= 3) {
6068 mono_print_tree (tree);
6073 if (!(mbstate = mono_burg_label (tree, cfg))) {
6074 g_warning ("unabled to label tree %p", tree);
6075 mono_print_tree (tree);
6077 g_assert_not_reached ();
6079 emit_state (cfg, mbstate, MB_NTERM_stmt);
6081 bb->max_ireg = cfg->rs->next_vireg;
6082 bb->max_freg = cfg->rs->next_vfreg;
6085 bb->last_ins->next = NULL;
6087 mono_mempool_empty (cfg->state_pool);
6089 mono_mempool_destroy (cfg->state_pool);
6093 mono_codegen (MonoCompile *cfg)
6095 MonoJumpInfo *patch_info;
6097 int i, max_epilog_size;
6100 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6101 cfg->spill_count = 0;
6102 /* we reuse dfn here */
6103 /* bb->dfn = bb_count++; */
6104 mono_arch_local_regalloc (cfg, bb);
6107 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
6108 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
6110 code = mono_arch_emit_prolog (cfg);
6112 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6113 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
6115 cfg->code_len = code - cfg->native_code;
6116 cfg->prolog_end = cfg->code_len;
6118 mono_debug_open_method (cfg);
6120 /* emit code all basic blocks */
6121 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6122 bb->native_offset = cfg->code_len;
6123 mono_arch_output_basic_block (cfg, bb);
6125 cfg->bb_exit->native_offset = cfg->code_len;
6127 code = cfg->native_code + cfg->code_len;
6129 max_epilog_size = mono_arch_max_epilog_size (cfg);
6131 /* we always allocate code in cfg->domain->code_mp to increase locality */
6132 cfg->code_size = cfg->code_len + max_epilog_size;
6133 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
6134 code = mono_mempool_alloc (cfg->domain->code_mp, cfg->code_size);
6135 memcpy (code, cfg->native_code, cfg->code_len);
6136 g_free (cfg->native_code);
6137 cfg->native_code = code;
6138 code = cfg->native_code + cfg->code_len;
6140 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
6142 cfg->epilog_begin = cfg->code_len;
6144 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6145 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
6147 cfg->code_len = code - cfg->native_code;
6149 mono_arch_emit_epilog (cfg);
6151 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
6152 switch (patch_info->type) {
6153 case MONO_PATCH_INFO_ABS: {
6154 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
6156 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
6157 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
6158 patch_info->data.name = info->name;
6162 case MONO_PATCH_INFO_SWITCH: {
6163 gpointer *table = g_new (gpointer, patch_info->table_size);
6164 patch_info->ip.i = patch_info->ip.label->inst_c0;
6165 for (i = 0; i < patch_info->table_size; i++) {
6166 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
6168 patch_info->data.target = table;
6177 if (cfg->verbose_level > 1)
6178 g_print ("Method %s::%s emmitted at %p to %p\n", cfg->method->klass->name,
6179 cfg->method->name, cfg->native_code, cfg->native_code + cfg->code_len);
6181 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info);
6183 mono_debug_close_method (cfg);
6187 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
6192 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
6193 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
6195 if (cp->opcode == OP_ICONST) {
6196 if (cfg->opt & MONO_OPT_CONSPROP) {
6197 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
6201 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
6202 if (cfg->opt & MONO_OPT_COPYPROP) {
6203 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
6209 arity = mono_burg_arity [tree->opcode];
6212 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
6213 if (cfg->opt & MONO_OPT_CFOLD)
6214 mono_constant_fold_inst (tree, NULL);
6216 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
6217 if (cfg->opt & MONO_OPT_CFOLD)
6218 mono_constant_fold_inst (tree, NULL);
6220 mono_constant_fold_inst (tree, NULL);
6226 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
6230 switch (tree->opcode) {
6240 if (tree->ssa_op == MONO_SSA_NOP) {
6241 memset (acp, 0, sizeof (MonoInst *) * acp_size);
6258 case OP_VOIDCALL_REG:
6259 case OP_VOIDCALLVIRT:
6261 MonoCallInst *call = (MonoCallInst *)tree;
6262 MonoMethodSignature *sig = call->signature;
6263 int i, byref = FALSE;
6265 for (i = 0; i < sig->param_count; i++) {
6266 if (sig->params [i]->byref) {
6273 memset (acp, 0, sizeof (MonoInst *) * acp_size);
6281 arity = mono_burg_arity [tree->opcode];
6287 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
6290 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
6291 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
6294 g_assert_not_reached ();
6299 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
6301 MonoInst *tree = bb->code;
6307 for (; tree; tree = tree->next) {
6309 mono_cprop_copy_values (cfg, tree, acp);
6311 mono_cprop_invalidate_values (tree, acp, acp_size);
6313 if (tree->ssa_op == MONO_SSA_STORE &&
6314 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
6315 MonoInst *i1 = tree->inst_i1;
6317 acp [tree->inst_i0->inst_c0] = NULL;
6319 for (i = 0; i < acp_size; i++) {
6320 if (acp [i] && acp [i]->opcode != OP_ICONST &&
6321 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
6326 if (i1->opcode == OP_ICONST) {
6327 acp [tree->inst_i0->inst_c0] = i1;
6328 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
6330 if (i1->ssa_op == MONO_SSA_LOAD &&
6331 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
6332 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
6333 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
6334 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
6339 if (tree->opcode == CEE_BEQ) {
6340 g_assert (tree->inst_i0->opcode == OP_COMPARE);
6341 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
6342 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
6344 tree->opcode = CEE_BR;
6345 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
6346 tree->inst_target_bb = tree->inst_true_bb;
6348 tree->inst_target_bb = tree->inst_false_bb;
6357 mono_local_cprop (MonoCompile *cfg)
6362 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
6364 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6365 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
6366 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
6371 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, int parts)
6373 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
6374 guint8 *ip = (guint8 *)header->code;
6377 int dfn = 0, i, code_size_ratio;
6379 mono_jit_stats.methods_compiled++;
6380 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
6381 mono_profiler_method_jit (method);
6383 cfg = g_new0 (MonoCompile, 1);
6384 cfg->method = method;
6385 cfg->mempool = mono_mempool_new ();
6387 cfg->prof_options = mono_profiler_get_events ();
6388 cfg->bb_hash = g_hash_table_new (g_direct_hash, NULL);
6389 cfg->domain = domain;
6390 cfg->verbose_level = mini_verbose;
6391 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
6392 ((MonoMethodNormal *)method)->header->max_stack);
6395 * create MonoInst* which represents arguments and local variables
6397 mono_compile_create_vars (cfg);
6399 if (cfg->verbose_level > 2)
6400 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
6402 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
6403 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
6404 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
6405 mono_destroy_compile (cfg);
6409 mono_jit_stats.basic_blocks += cfg->num_bblocks;
6410 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
6412 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
6414 /* Depth-first ordering on basic blocks */
6415 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
6417 if (cfg->opt & MONO_OPT_BRANCH)
6418 optimize_branches (cfg);
6420 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
6421 if (cfg->num_bblocks != dfn + 1) {
6424 cfg->num_bblocks = dfn + 1;
6426 if (!header->clauses) {
6427 /* remove unreachable code, because the code in them may be
6428 * inconsistent (access to dead variables for example) */
6429 for (bb = cfg->bb_entry; bb;) {
6430 MonoBasicBlock *bbn = bb->next_bb;
6432 if (bbn && bbn->region == -1 && !bbn->dfn) {
6433 if (cfg->verbose_level > 1)
6434 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
6435 bb->next_bb = bbn->next_bb;
6436 nullify_basic_block (bbn);
6444 if (cfg->opt & MONO_OPT_LOOP) {
6445 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
6446 mono_compute_natural_loops (cfg);
6450 /* after method_to_ir */
6454 //#define DEBUGSSA "logic_run"
6455 #define DEBUGSSA_CLASS "Tests"
6459 if (!header->num_clauses && !cfg->disable_ssa) {
6460 mono_local_cprop (cfg);
6461 mono_ssa_compute (cfg);
6465 /* fixme: add all optimizations which requires SSA */
6466 if (cfg->opt & (MONO_OPT_DEADCE)) {
6467 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
6468 mono_local_cprop (cfg);
6469 mono_ssa_compute (cfg);
6471 if (cfg->verbose_level >= 2) {
6478 /* after SSA translation */
6482 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
6483 if (cfg->comp_done & MONO_COMP_SSA) {
6484 mono_ssa_cprop (cfg);
6486 mono_local_cprop (cfg);
6490 if (cfg->comp_done & MONO_COMP_SSA) {
6491 mono_ssa_deadce (cfg);
6493 //mono_ssa_strength_reduction (cfg);
6495 mono_ssa_remove (cfg);
6497 if (cfg->opt & MONO_OPT_BRANCH)
6498 optimize_branches (cfg);
6501 /* after SSA removal */
6505 decompose_pass (cfg);
6507 /* FIXME: disabled with exception clauses: bug #42136 */
6508 if ((!header->num_clauses) && (cfg->opt & MONO_OPT_LINEARS)) {
6511 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
6512 cfg->comp_done &= ~MONO_COMP_LIVENESS;
6513 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
6514 mono_analyze_liveness (cfg);
6516 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
6517 regs = mono_arch_get_global_int_regs (cfg);
6518 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
6522 //mono_print_code (cfg);
6526 /* variables are allocated after decompose, since decompose could create temps */
6527 mono_arch_allocate_vars (cfg);
6529 if (cfg->opt & MONO_OPT_CFOLD)
6530 mono_constant_fold (cfg);
6532 mini_select_instructions (cfg);
6535 if (cfg->verbose_level >= 2) {
6536 char *id = mono_method_full_name (cfg->method, FALSE);
6537 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
6541 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
6543 jinfo = g_new0 (MonoJitInfo, 1);
6544 jinfo->method = method;
6545 jinfo->code_start = cfg->native_code;
6546 jinfo->code_size = cfg->code_len;
6547 jinfo->used_regs = cfg->used_int_regs;
6549 if (header->num_clauses) {
6552 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
6553 jinfo->num_clauses = header->num_clauses;
6554 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
6555 sizeof (MonoJitExceptionInfo) * header->num_clauses);
6557 for (i = 0; i < header->num_clauses; i++) {
6558 MonoExceptionClause *ec = &header->clauses [i];
6559 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
6560 MonoBasicBlock *tblock;
6562 ei->flags = ec->flags;
6564 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
6565 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->token_or_filter);
6567 ei->data.filter = cfg->native_code + tblock->native_offset;
6569 ei->data.token = ec->token_or_filter;
6572 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
6574 ei->try_start = cfg->native_code + tblock->native_offset;
6575 g_assert (tblock->native_offset);
6576 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
6578 ei->try_end = cfg->native_code + tblock->native_offset;
6579 g_assert (tblock->native_offset);
6580 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
6582 ei->handler_start = cfg->native_code + tblock->native_offset;
6586 mono_jit_info_table_add (cfg->domain, jinfo);
6588 /* collect statistics */
6589 mono_jit_stats.allocated_code_size += cfg->code_len;
6590 code_size_ratio = cfg->code_len;
6591 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
6592 mono_jit_stats.biggest_method_size = code_size_ratio;
6593 mono_jit_stats.biggest_method = method;
6595 code_size_ratio = (code_size_ratio * 100) / ((MonoMethodNormal *)method)->header->code_size;
6596 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
6597 mono_jit_stats.max_code_size_ratio = code_size_ratio;
6598 mono_jit_stats.max_ratio_method = method;
6600 mono_jit_stats.native_code_size += cfg->code_len;
6602 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
6603 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
6609 mono_jit_compile_method (MonoMethod *method)
6611 /* FIXME: later copy the code from mono */
6612 MonoDomain *target_domain, *domain = mono_domain_get ();
6614 GHashTable *jit_code_hash;
6617 if (default_opt & MONO_OPT_SHARED)
6618 target_domain = mono_root_domain;
6620 target_domain = domain;
6622 jit_code_hash = target_domain->jit_code_hash;
6624 if ((code = g_hash_table_lookup (jit_code_hash, method))) {
6625 mono_jit_stats.methods_lookups++;
6629 #ifdef MONO_USE_AOT_COMPILER
6630 if (!mono_compile_aot) {
6631 mono_class_init (method->klass);
6632 if ((code = mono_aot_get_method (method))) {
6633 g_hash_table_insert (jit_code_hash, method, code);
6639 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
6640 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
6641 if (!method->info) {
6644 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
6645 mono_lookup_pinvoke_call (method);
6646 #ifdef MONO_USE_EXC_TABLES
6647 if (mono_method_blittable (method)) {
6648 method->info = method->addr;
6651 nm = mono_marshal_get_native_wrapper (method);
6652 method->info = mono_compile_method (nm);
6654 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
6655 //mono_debug_add_wrapper (method, nm);
6656 #ifdef MONO_USE_EXC_TABLES
6660 return method->info;
6661 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
6662 const char *name = method->name;
6665 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
6666 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
6667 /* FIXME: uhm, we need a wrapper to handle exceptions? */
6668 return (gpointer)mono_delegate_ctor;
6669 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
6670 nm = mono_marshal_get_delegate_invoke (method);
6671 return mono_jit_compile_method (nm);
6672 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
6673 nm = mono_marshal_get_delegate_begin_invoke (method);
6674 return mono_jit_compile_method (nm);
6675 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
6676 nm = mono_marshal_get_delegate_end_invoke (method);
6677 return mono_jit_compile_method (nm);
6683 cfg = mini_method_compile (method, default_opt, target_domain, 0);
6684 code = cfg->native_code;
6685 mono_destroy_compile (cfg);
6687 g_hash_table_insert (jit_code_hash, method, code);
6689 if (target_domain->jump_target_hash) {
6690 MonoJumpInfo patch_info;
6692 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
6694 patch_info.next = NULL;
6695 patch_info.ip.i = 0;
6696 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
6697 patch_info.data.method = method;
6698 g_hash_table_remove (target_domain->jump_target_hash, method);
6700 for (tmp = list; tmp; tmp = tmp->next)
6701 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info);
6702 g_slist_free (list);
6704 /* make sure runtime_init is called */
6705 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
6711 * mono_jit_runtime_invoke:
6712 * @method: the method to invoke
6713 * @obj: this pointer
6714 * @params: array of parameter values.
6715 * @exc: used to catch exceptions objects
6718 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
6721 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc);
6722 invoke = mono_marshal_get_runtime_invoke (method);
6723 runtime_invoke = mono_jit_compile_method (invoke);
6724 return runtime_invoke (obj, params, exc);
6727 #ifdef PLATFORM_WIN32
6728 #define GET_CONTEXT \
6729 struct sigcontext *ctx = (struct sigcontext*)_dummy;
6731 #define GET_CONTEXT \
6732 void **_p = (void **)&_dummy; \
6733 struct sigcontext *ctx = (struct sigcontext *)++_p;
6737 sigfpe_signal_handler (int _dummy)
6742 exc = mono_get_exception_divide_by_zero ();
6744 mono_arch_handle_exception (ctx, exc, FALSE);
6748 sigill_signal_handler (int _dummy)
6752 exc = mono_get_exception_execution_engine ("SIGILL");
6754 mono_arch_handle_exception (ctx, exc, FALSE);
6758 sigsegv_signal_handler (int _dummy)
6763 exc = mono_get_exception_null_reference ();
6765 mono_arch_handle_exception (ctx, exc, FALSE);
6769 sigusr1_signal_handler (int _dummy)
6774 thread = mono_thread_current ();
6776 g_assert (thread->abort_exc);
6778 mono_arch_handle_exception (ctx, thread->abort_exc, FALSE);
6782 mono_runtime_install_handlers (void)
6784 #ifndef PLATFORM_WIN32
6785 struct sigaction sa;
6788 #ifdef PLATFORM_WIN32
6790 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
6791 win32_seh_set_handler(SIGILL, sigill_signal_handler);
6792 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
6793 #else /* !PLATFORM_WIN32 */
6795 /* libpthreads has its own implementation of sigaction(),
6796 * but it seems to work well with our current exception
6797 * handlers. If not we must call syscall directly instead
6801 sa.sa_handler = sigfpe_signal_handler;
6802 sigemptyset (&sa.sa_mask);
6804 //g_assert (syscall (SYS_sigaction, SIGFPE, &sa, NULL) != -1);
6805 g_assert (sigaction (SIGFPE, &sa, NULL) != -1);
6808 sa.sa_handler = sigill_signal_handler;
6809 sigemptyset (&sa.sa_mask);
6811 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
6812 g_assert (sigaction (SIGILL, &sa, NULL) != -1);
6814 /* catch thread abort signal */
6815 sa.sa_handler = sigusr1_signal_handler;
6816 sigemptyset (&sa.sa_mask);
6818 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
6819 g_assert (sigaction (mono_thread_get_abort_signal (), &sa, NULL) != -1);
6823 sa.sa_handler = sigsegv_signal_handler;
6824 sigemptyset (&sa.sa_mask);
6826 //g_assert (syscall (SYS_sigaction, SIGSEGV, &sa, NULL) != -1);
6827 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
6829 #endif /* PLATFORM_WIN32 */
6832 /* mono_jit_create_remoting_trampoline:
6833 * @method: pointer to the method info
6835 * Creates a trampoline which calls the remoting functions. This
6836 * is used in the vtable of transparent proxies.
6838 * Returns: a pointer to the newly created code
6841 mono_jit_create_remoting_trampoline (MonoMethod *method)
6844 guint8 *addr = NULL;
6846 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
6847 (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
6848 nm = mono_marshal_get_remoting_invoke (method);
6849 addr = mono_compile_method (nm);
6851 addr = mono_compile_method (method);
6856 static CRITICAL_SECTION ms;
6859 mini_init (const char *filename)
6863 metadata_section = &ms;
6864 InitializeCriticalSection (metadata_section);
6866 mono_jit_tls_id = TlsAlloc ();
6867 mono_thread_start_cb (GetCurrentThreadId (), (gpointer)-1, NULL);
6871 mono_runtime_install_handlers ();
6873 mono_install_compile_method (mono_jit_compile_method);
6874 mono_install_trampoline (mono_arch_create_jit_trampoline);
6875 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
6876 mono_install_runtime_invoke (mono_jit_runtime_invoke);
6877 mono_install_handler (mono_arch_get_throw_exception ());
6878 mono_install_stack_walk (mono_jit_walk_stack);
6879 mono_install_get_config_dir ();
6881 domain = mono_init (filename);
6884 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
6885 ves_icall_get_frame_info);
6886 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
6887 ves_icall_get_trace);
6888 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
6889 mono_runtime_install_handlers);
6892 create_helper_signature ();
6894 mono_arch_register_lowlevel_calls ();
6895 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
6896 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
6898 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
6899 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
6901 /* fixme: we cant hanlde vararg methods this way, because the signature is not constant */
6902 //mono_register_jit_icall (ves_array_element_address, "ves_array_element_address", NULL);
6903 //mono_register_jit_icall (mono_array_new_va, "mono_array_new_va", NULL);
6905 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
6906 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
6907 helper_sig_void_ptr, TRUE);
6910 * NOTE, NOTE, NOTE, NOTE:
6911 * when adding emulation for some opcodes, remember to also add a dummy
6912 * rule to the burg files, because we need the arity information to be correct.
6914 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult, TRUE);
6915 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un, FALSE);
6916 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf, FALSE);
6917 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv, FALSE);
6918 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un, FALSE);
6919 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem, FALSE);
6920 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un, FALSE);
6922 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl, TRUE);
6923 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr, TRUE);
6924 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un, TRUE);
6926 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8, FALSE);
6927 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4, FALSE);
6928 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8, TRUE);
6929 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8, TRUE);
6931 #if SIZEOF_VOID_P == 4
6932 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4, TRUE);
6934 #warning "fixme: add opcode emulation"
6937 /* other jit icalls */
6938 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
6939 helper_sig_ptr_ptr_ptr, FALSE);
6940 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
6941 mono_register_jit_icall (mono_threads_get_static_data, "mono_threads_get_static_data", helper_sig_ptr_int, FALSE);
6942 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
6943 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
6944 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
6945 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
6946 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
6947 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
6948 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
6949 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
6950 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
6951 mono_register_jit_icall (mono_string_to_utf16, "mono_string_to_utf16", helper_sig_ptr_obj, FALSE);
6952 mono_register_jit_icall (mono_string_from_utf16, "mono_string_from_utf16", helper_sig_obj_ptr, FALSE);
6953 mono_register_jit_icall (mono_string_new_wrapper, "mono_string_new_wrapper", helper_sig_obj_ptr, FALSE);
6954 mono_register_jit_icall (mono_string_to_utf8, "mono_string_to_utf8", helper_sig_ptr_obj, FALSE);
6955 mono_register_jit_icall (mono_string_to_bstr, "mono_string_to_bstr", helper_sig_ptr_obj, FALSE);
6956 mono_register_jit_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", helper_sig_ptr_obj, FALSE);
6957 mono_register_jit_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", helper_sig_ptr_obj, FALSE);
6958 mono_register_jit_icall (mono_array_to_savearray, "mono_array_to_savearray", helper_sig_ptr_obj, FALSE);
6959 mono_register_jit_icall (mono_array_to_lparray, "mono_array_to_lparray", helper_sig_ptr_obj, FALSE);
6960 mono_register_jit_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", helper_sig_ptr_obj, FALSE);
6961 mono_register_jit_icall (mono_marshal_string_array, "mono_marshal_string_array", helper_sig_ptr_obj, FALSE);
6962 mono_register_jit_icall (mono_string_utf8_to_builder, "mono_string_utf8_to_builder", helper_sig_void_ptr_ptr, FALSE);
6963 mono_register_jit_icall (mono_marshal_free_array, "mono_marshal_free_array", helper_sig_void_ptr_ptr, FALSE);
6964 mono_register_jit_icall (mono_string_to_byvalstr, "mono_string_to_byvalstr", helper_sig_void_ptr_ptr_ptr, FALSE);
6965 mono_register_jit_icall (mono_string_to_byvalwstr, "mono_string_to_byvalwstr", helper_sig_void_ptr_ptr_ptr, FALSE);
6966 mono_register_jit_icall (g_free, "g_free", helper_sig_void_ptr, FALSE);
6967 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
6968 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
6969 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
6971 mono_runtime_init (domain, mono_thread_start_cb,
6972 mono_thread_attach_cb);
6974 //mono_thread_attach (domain);
6978 MonoJitStats mono_jit_stats = {0};
6981 print_jit_stats (void)
6983 if (mono_jit_stats.enabled) {
6984 g_print ("Mono Jit statistics\n");
6985 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
6986 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
6987 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
6988 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
6989 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
6990 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
6991 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
6992 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
6993 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
6994 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
6995 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
6996 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
6997 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
6998 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
6999 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
7000 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
7001 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
7002 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
7004 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
7005 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
7006 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
7007 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
7008 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
7013 mini_cleanup (MonoDomain *domain)
7016 * mono_runtime_cleanup() and mono_domain_finalize () need to
7017 * be called early since they need the execution engine still
7018 * fully working (mono_domain_finalize may invoke managed finalizers
7019 * and mono_runtime_cleanup will wait for other threads to finish).
7021 mono_domain_finalize (domain);
7023 mono_runtime_cleanup (domain);
7025 mono_profiler_shutdown ();
7027 mono_debug_cleanup ();
7028 #ifdef PLATFORM_WIN32
7029 win32_seh_cleanup();
7032 mono_domain_unload (domain, TRUE);
7035 DeleteCriticalSection (metadata_section);
7039 mono_set_defaults (int verbose_level, guint32 opts)
7041 mini_verbose = verbose_level;