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.
16 #ifdef sun // Solaris x86
17 #include <sys/types.h>
18 #include <sys/ucontext.h>
21 #ifdef HAVE_VALGRIND_MEMCHECK_H
22 #include <valgrind/memcheck.h>
25 #include <mono/metadata/assembly.h>
26 #include <mono/metadata/loader.h>
27 #include <mono/metadata/cil-coff.h>
28 #include <mono/metadata/tabledefs.h>
29 #include <mono/metadata/class.h>
30 #include <mono/metadata/object.h>
31 #include <mono/metadata/exception.h>
32 #include <mono/metadata/opcodes.h>
33 #include <mono/metadata/mono-endian.h>
34 #include <mono/metadata/tokentype.h>
35 #include <mono/metadata/tabledefs.h>
36 #include <mono/metadata/threads.h>
37 #include <mono/metadata/marshal.h>
38 #include <mono/metadata/socket-io.h>
39 #include <mono/metadata/appdomain.h>
40 #include <mono/metadata/debug-helpers.h>
41 #include <mono/io-layer/io-layer.h>
42 #include "mono/metadata/profiler.h"
43 #include <mono/metadata/profiler-private.h>
44 #include <mono/metadata/mono-config.h>
45 #include <mono/metadata/environment.h>
46 #include <mono/metadata/mono-debug.h>
47 #include <mono/metadata/mono-debug-debugger.h>
48 #include <mono/metadata/monitor.h>
56 #include "jit-icalls.c"
59 * this is used to determine when some branch optimizations are possible: we exclude FP compares
60 * because they have weird semantics with NaNs.
62 #define MONO_IS_COND_BRANCH_OP(ins) (((ins)->opcode >= CEE_BEQ && (ins)->opcode <= CEE_BLT_UN) || ((ins)->opcode >= OP_LBEQ && (ins)->opcode <= OP_LBLT_UN) || ((ins)->opcode >= OP_FBEQ && (ins)->opcode <= OP_FBLT_UN))
63 #define MONO_IS_COND_BRANCH_NOFP(ins) (MONO_IS_COND_BRANCH_OP(ins) && (ins)->inst_left->inst_left->type != STACK_R8)
65 #define MONO_CHECK_THIS(ins) (cfg->method->signature->hasthis && (ins)->ssa_op == MONO_SSA_LOAD && (ins)->inst_left->inst_c0 == 0)
67 gboolean mono_arch_print_tree(MonoInst *tree, int arity);
68 static gpointer mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt);
69 static gpointer mono_jit_compile_method (MonoMethod *method);
70 static gpointer mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method);
72 static void handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src,
73 const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native);
75 static void dec_foreach (MonoInst *tree, MonoCompile *cfg);
77 static int mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
78 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
79 guint inline_offset, gboolean is_virtual_call);
81 extern guint8 mono_burg_arity [];
82 /* helper methods signature */
83 static MonoMethodSignature *helper_sig_long_long_long = NULL;
84 static MonoMethodSignature *helper_sig_long_long_int = NULL;
85 static MonoMethodSignature *helper_sig_newarr = NULL;
86 static MonoMethodSignature *helper_sig_newarr_specific = NULL;
87 static MonoMethodSignature *helper_sig_ldstr = NULL;
88 static MonoMethodSignature *helper_sig_domain_get = NULL;
89 static MonoMethodSignature *helper_sig_object_new = NULL;
90 static MonoMethodSignature *helper_sig_object_new_specific = NULL;
91 static MonoMethodSignature *helper_sig_compile = NULL;
92 static MonoMethodSignature *helper_sig_compile_virt = NULL;
93 static MonoMethodSignature *helper_sig_obj_ptr = NULL;
94 static MonoMethodSignature *helper_sig_obj_ptr_ptr = NULL;
95 static MonoMethodSignature *helper_sig_obj_void = NULL;
96 static MonoMethodSignature *helper_sig_ptr_void = NULL;
97 static MonoMethodSignature *helper_sig_void_ptr = NULL;
98 static MonoMethodSignature *helper_sig_void_obj = NULL;
99 static MonoMethodSignature *helper_sig_void_obj_ptr_int = NULL;
100 static MonoMethodSignature *helper_sig_void_ptr_ptr = NULL;
101 static MonoMethodSignature *helper_sig_void_ptr_ptr_ptr = NULL;
102 static MonoMethodSignature *helper_sig_ptr_ptr_ptr = NULL;
103 static MonoMethodSignature *helper_sig_ptr_obj = NULL;
104 static MonoMethodSignature *helper_sig_ptr_obj_int = NULL;
105 static MonoMethodSignature *helper_sig_ptr_int = NULL;
106 static MonoMethodSignature *helper_sig_initobj = NULL;
107 static MonoMethodSignature *helper_sig_memcpy = NULL;
108 static MonoMethodSignature *helper_sig_memset = NULL;
109 static MonoMethodSignature *helper_sig_ulong_double = NULL;
110 static MonoMethodSignature *helper_sig_long_double = NULL;
111 static MonoMethodSignature *helper_sig_double_long = NULL;
112 static MonoMethodSignature *helper_sig_double_int = NULL;
113 static MonoMethodSignature *helper_sig_float_long = NULL;
114 static MonoMethodSignature *helper_sig_double_double_double = NULL;
115 static MonoMethodSignature *helper_sig_uint_double = NULL;
116 static MonoMethodSignature *helper_sig_int_double = NULL;
117 static MonoMethodSignature *helper_sig_stelem_ref = NULL;
118 static MonoMethodSignature *helper_sig_stelem_ref_check = NULL;
119 static MonoMethodSignature *helper_sig_class_init_trampoline = NULL;
121 static guint32 default_opt = MONO_OPT_PEEPHOLE;
123 guint32 mono_jit_tls_id = -1;
124 MonoTraceSpec *mono_jit_trace_calls = NULL;
125 gboolean mono_break_on_exc = FALSE;
126 gboolean mono_compile_aot = FALSE;
128 static int mini_verbose = 0;
130 static CRITICAL_SECTION trampoline_hash_mutex;
132 static GHashTable *class_init_hash_addr = NULL;
134 static GHashTable *jump_trampoline_hash = NULL;
137 mono_running_on_valgrind (void)
139 #ifdef HAVE_VALGRIND_MEMCHECK_H
140 if (RUNNING_ON_VALGRIND)
150 G_GNUC_UNUSED static void
151 print_method_from_ip (void *ip)
156 MonoDomain *domain = mono_domain_get ();
158 ji = mono_jit_info_table_find (domain, ip);
160 g_print ("No method at %p\n", ip);
163 method = mono_method_full_name (ji->method, TRUE);
164 source = mono_debug_source_location_from_address (ji->method, (int) ip, NULL, domain);
166 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);
169 g_print ("%s\n", source);
177 * mono_method_same_domain:
179 * Determine whenever two compiled methods are in the same domain, thus
180 * the address of the callee can be embedded in the caller.
182 gboolean mono_method_same_domain (MonoJitInfo *caller, MonoJitInfo *callee)
184 if (!caller || !callee)
188 * If the call was made from domain-neutral to domain-specific
189 * code, we can't patch the call site.
191 if (caller->domain_neutral && !callee->domain_neutral)
194 if ((caller->method->klass == mono_defaults.appdomain_class) &&
195 (strstr (caller->method->name, "InvokeInDomain"))) {
196 /* The InvokeInDomain methods change the current appdomain */
204 mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token)
206 MonoJumpInfoToken *res = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoToken));
213 #define MONO_INIT_VARINFO(vi,id) do { \
214 (vi)->range.first_use.pos.bid = 0xffff; \
220 * Basic blocks have two numeric identifiers:
221 * dfn: Depth First Number
222 * block_num: unique ID assigned at bblock creation
224 #define NEW_BBLOCK(cfg) (mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)))
225 #define ADD_BBLOCK(cfg,bbhash,b) do { \
226 g_hash_table_insert (bbhash, (b)->cil_code, (b)); \
227 (b)->block_num = cfg->num_bblocks++; \
228 (b)->real_offset = real_offset; \
231 #define GET_BBLOCK(cfg,bbhash,tblock,ip) do { \
232 (tblock) = g_hash_table_lookup (bbhash, (ip)); \
234 if ((ip) >= end || (ip) < header->code) goto unverified; \
235 (tblock) = NEW_BBLOCK (cfg); \
236 (tblock)->cil_code = (ip); \
237 ADD_BBLOCK (cfg, (bbhash), (tblock)); \
241 #define CHECK_BBLOCK(target,ip,tblock) do { \
242 if ((target) < (ip) && !(tblock)->code) { \
243 bb_recheck = g_list_prepend (bb_recheck, (tblock)); \
244 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); \
248 #define NEW_ICONST(cfg,dest,val) do { \
249 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
250 (dest)->opcode = OP_ICONST; \
251 (dest)->inst_c0 = (val); \
252 (dest)->type = STACK_I4; \
255 /* FIXME: have a different definition of NEW_PCONST for 64 bit systems */
256 #define NEW_PCONST(cfg,dest,val) do { \
257 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
258 (dest)->opcode = OP_ICONST; \
259 (dest)->inst_p0 = (val); \
260 (dest)->type = STACK_PTR; \
263 #define NEW_CLASSCONST(cfg,dest,val) do { \
264 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
265 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
266 (dest)->inst_p0 = (val); \
267 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_CLASS; \
268 (dest)->type = STACK_PTR; \
271 #define NEW_IMAGECONST(cfg,dest,val) do { \
272 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
273 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
274 (dest)->inst_p0 = (val); \
275 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_IMAGE; \
276 (dest)->type = STACK_PTR; \
279 #define NEW_FIELDCONST(cfg,dest,field) do { \
280 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
281 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
282 (dest)->inst_p0 = (field); \
283 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_FIELD; \
284 (dest)->type = STACK_PTR; \
287 #define NEW_METHODCONST(cfg,dest,val) do { \
288 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
289 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
290 (dest)->inst_p0 = (val); \
291 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_METHODCONST; \
292 (dest)->type = STACK_PTR; \
295 #define NEW_VTABLECONST(cfg,dest,vtable) do { \
296 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
297 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
298 (dest)->inst_p0 = mono_compile_aot ? (gpointer)((vtable)->klass) : (vtable); \
299 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_VTABLE; \
300 (dest)->type = STACK_PTR; \
303 #define NEW_SFLDACONST(cfg,dest,field) do { \
304 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
305 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
306 (dest)->inst_p0 = (field); \
307 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_SFLDA; \
308 (dest)->type = STACK_PTR; \
311 #define NEW_LDSTRCONST(cfg,dest,image,token) do { \
312 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
313 (dest)->opcode = OP_AOTCONST; \
314 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
315 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_LDSTR; \
316 (dest)->type = STACK_OBJ; \
319 #define NEW_TYPE_FROM_HANDLE_CONST(cfg,dest,image,token) do { \
320 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
321 (dest)->opcode = OP_AOTCONST; \
322 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
323 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_TYPE_FROM_HANDLE; \
324 (dest)->type = STACK_OBJ; \
327 #define NEW_LDTOKENCONST(cfg,dest,image,token) do { \
328 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
329 (dest)->opcode = OP_AOTCONST; \
330 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
331 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_LDTOKEN; \
332 (dest)->type = STACK_PTR; \
335 #define NEW_DOMAINCONST(cfg,dest) do { \
336 if (cfg->opt & MONO_OPT_SHARED) { \
337 NEW_TEMPLOAD (cfg, dest, mono_get_domainvar (cfg)->inst_c0); \
339 NEW_PCONST (cfg, dest, (cfg)->domain); \
343 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
345 #define NEW_ARGLOAD(cfg,dest,num) do { \
346 if (arg_array [(num)]->opcode == OP_ICONST) (dest) = arg_array [(num)]; else { \
347 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
348 (dest)->ssa_op = MONO_SSA_LOAD; \
349 (dest)->inst_i0 = arg_array [(num)]; \
350 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
351 type_to_eval_stack_type (param_types [(num)], (dest)); \
352 (dest)->klass = (dest)->inst_i0->klass; \
355 #define NEW_LOCLOAD(cfg,dest,num) do { \
356 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
357 (dest)->ssa_op = MONO_SSA_LOAD; \
358 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
359 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
360 type_to_eval_stack_type (header->locals [(num)], (dest)); \
361 (dest)->klass = (dest)->inst_i0->klass; \
364 #define NEW_LOCLOADA(cfg,dest,num) do { \
365 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
366 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
367 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
368 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
369 (dest)->opcode = OP_LDADDR; \
370 (dest)->type = STACK_MP; \
371 (dest)->klass = (dest)->inst_i0->klass; \
372 if (!MONO_TYPE_ISSTRUCT (header->locals [(num)])) \
373 (cfg)->disable_ssa = TRUE; \
376 #define NEW_RETLOADA(cfg,dest) do { \
377 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
378 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
379 (dest)->inst_i0 = (cfg)->ret; \
380 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
381 (dest)->opcode = CEE_LDIND_I; \
382 (dest)->type = STACK_MP; \
383 (dest)->klass = (dest)->inst_i0->klass; \
384 (cfg)->disable_ssa = TRUE; \
387 #define NEW_ARGLOADA(cfg,dest,num) do { \
388 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
389 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
390 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
391 (dest)->inst_i0 = arg_array [(num)]; \
392 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
393 (dest)->opcode = OP_LDADDR; \
394 (dest)->type = STACK_MP; \
395 (dest)->klass = (dest)->inst_i0->klass; \
396 (cfg)->disable_ssa = TRUE; \
399 #define NEW_TEMPLOAD(cfg,dest,num) do { \
400 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
401 (dest)->ssa_op = MONO_SSA_LOAD; \
402 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
403 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
404 type_to_eval_stack_type ((dest)->inst_i0->inst_vtype, (dest)); \
405 (dest)->klass = (dest)->inst_i0->klass; \
408 #define NEW_TEMPLOADA(cfg,dest,num) do { \
409 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
410 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
411 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
412 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
413 (dest)->opcode = OP_LDADDR; \
414 (dest)->type = STACK_MP; \
415 (dest)->klass = (dest)->inst_i0->klass; \
416 if (!MONO_TYPE_ISSTRUCT (cfg->varinfo [(num)]->inst_vtype)) \
417 (cfg)->disable_ssa = TRUE; \
421 #define NEW_INDLOAD(cfg,dest,addr,vtype) do { \
422 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
423 (dest)->inst_left = addr; \
424 (dest)->opcode = mono_type_to_ldind (vtype); \
425 type_to_eval_stack_type (vtype, (dest)); \
426 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
429 #define NEW_INDSTORE(cfg,dest,addr,value,vtype) do { \
430 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
431 (dest)->inst_i0 = addr; \
432 (dest)->opcode = mono_type_to_stind (vtype); \
433 (dest)->inst_i1 = (value); \
434 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
437 #define NEW_TEMPSTORE(cfg,dest,num,inst) do { \
438 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
439 (dest)->ssa_op = MONO_SSA_STORE; \
440 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
441 (dest)->opcode = mono_type_to_stind ((dest)->inst_i0->inst_vtype); \
442 (dest)->inst_i1 = (inst); \
443 (dest)->klass = (dest)->inst_i0->klass; \
446 #define NEW_LOCSTORE(cfg,dest,num,inst) do { \
447 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
448 (dest)->opcode = mono_type_to_stind (header->locals [(num)]); \
449 (dest)->ssa_op = MONO_SSA_STORE; \
450 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
451 (dest)->inst_i1 = (inst); \
452 (dest)->klass = (dest)->inst_i0->klass; \
455 #define NEW_ARGSTORE(cfg,dest,num,inst) do { \
456 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
457 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
458 (dest)->opcode = mono_type_to_stind (param_types [(num)]); \
459 (dest)->ssa_op = MONO_SSA_STORE; \
460 (dest)->inst_i0 = arg_array [(num)]; \
461 (dest)->inst_i1 = (inst); \
462 (dest)->klass = (dest)->inst_i0->klass; \
465 #define ADD_BINOP(op) do { \
466 MONO_INST_NEW (cfg, ins, (op)); \
467 ins->cil_code = ip; \
469 ins->inst_i0 = sp [0]; \
470 ins->inst_i1 = sp [1]; \
472 type_from_op (ins); \
476 #define ADD_UNOP(op) do { \
477 MONO_INST_NEW (cfg, ins, (op)); \
478 ins->cil_code = ip; \
480 ins->inst_i0 = sp [0]; \
482 type_from_op (ins); \
486 #define ADD_BINCOND(next_block) do { \
488 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
490 cmp->inst_i0 = sp [0]; \
491 cmp->inst_i1 = sp [1]; \
492 cmp->cil_code = ins->cil_code; \
493 type_from_op (cmp); \
495 ins->inst_i0 = cmp; \
496 MONO_ADD_INS (bblock, ins); \
497 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
498 GET_BBLOCK (cfg, bbhash, tblock, target); \
499 link_bblock (cfg, bblock, tblock); \
500 ins->inst_true_bb = tblock; \
501 CHECK_BBLOCK (target, ip, tblock); \
502 if ((next_block)) { \
503 link_bblock (cfg, bblock, (next_block)); \
504 ins->inst_false_bb = (next_block); \
505 start_new_bblock = 1; \
507 GET_BBLOCK (cfg, bbhash, tblock, ip); \
508 link_bblock (cfg, bblock, tblock); \
509 ins->inst_false_bb = tblock; \
510 start_new_bblock = 2; \
514 /* FIXME: handle float, long ... */
515 #define ADD_UNCOND(istrue) do { \
517 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
519 cmp->inst_i0 = sp [0]; \
520 switch (cmp->inst_i0->type) { \
522 cmp->inst_i1 = zero_int64; break; \
524 cmp->inst_i1 = zero_r8; break; \
527 cmp->inst_i1 = zero_ptr; break; \
529 cmp->inst_i1 = zero_obj; break; \
531 cmp->inst_i1 = zero_int32; \
533 cmp->cil_code = ins->cil_code; \
534 type_from_op (cmp); \
536 ins->inst_i0 = cmp; \
537 ins->opcode = (istrue)? CEE_BNE_UN: CEE_BEQ; \
538 MONO_ADD_INS (bblock, ins); \
539 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
540 GET_BBLOCK (cfg, bbhash, tblock, target); \
541 link_bblock (cfg, bblock, tblock); \
542 ins->inst_true_bb = tblock; \
543 CHECK_BBLOCK (target, ip, tblock); \
544 GET_BBLOCK (cfg, bbhash, tblock, ip); \
545 link_bblock (cfg, bblock, tblock); \
546 ins->inst_false_bb = tblock; \
547 start_new_bblock = 2; \
550 #define NEW_LDELEMA(cfg,dest,sp,k) do { \
551 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
552 (dest)->opcode = CEE_LDELEMA; \
553 (dest)->inst_left = (sp) [0]; \
554 (dest)->inst_right = (sp) [1]; \
555 (dest)->type = STACK_MP; \
556 (dest)->klass = (k); \
557 (cfg)->flags |= MONO_CFG_HAS_LDELEMA; \
560 #define NEW_GROUP(cfg,dest,el1,el2) do { \
561 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
562 (dest)->opcode = OP_GROUP; \
563 (dest)->inst_left = (el1); \
564 (dest)->inst_right = (el2); \
569 compare_bblock (gconstpointer a, gconstpointer b)
571 const MonoBasicBlock *b1 = a;
572 const MonoBasicBlock *b2 = b;
574 return b2->cil_code - b1->cil_code;
579 * link_bblock: Links two basic blocks
581 * links two basic blocks in the control flow graph, the 'from'
582 * argument is the starting block and the 'to' argument is the block
583 * the control flow ends to after 'from'.
586 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
588 MonoBasicBlock **newa;
592 if (from->cil_code) {
594 g_print ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
596 g_print ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
599 g_print ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
601 g_print ("edge from entry to exit\n");
605 for (i = 0; i < from->out_count; ++i) {
606 if (to == from->out_bb [i]) {
612 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
613 for (i = 0; i < from->out_count; ++i) {
614 newa [i] = from->out_bb [i];
622 for (i = 0; i < to->in_count; ++i) {
623 if (from == to->in_bb [i]) {
629 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
630 for (i = 0; i < to->in_count; ++i) {
631 newa [i] = to->in_bb [i];
640 * mono_find_block_region:
642 * We mark each basic block with a region ID. We use that to avoid BB
643 * optimizations when blocks are in different regions.
646 * A region token that encodes where this region is, and information
647 * about the clause owner for this block.
649 * The region encodes the try/catch/filter clause that owns this block
650 * as well as the type. -1 is a special value that represents a block
651 * that is in none of try/catch/filter.
654 mono_find_block_region (MonoCompile *cfg, int offset, int *filter_lengths)
656 MonoMethod *method = cfg->method;
657 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
658 MonoExceptionClause *clause;
661 /* first search for handlers and filters */
662 for (i = 0; i < header->num_clauses; ++i) {
663 clause = &header->clauses [i];
664 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->token_or_filter) &&
665 (offset < (clause->token_or_filter + filter_lengths [i])))
666 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
668 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
669 if (clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)
670 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
672 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
676 /* search the try blocks */
677 for (i = 0; i < header->num_clauses; ++i) {
678 clause = &header->clauses [i];
679 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
680 return ((i + 1) << 8) | clause->flags;
687 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
689 MonoMethod *method = cfg->method;
690 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
691 MonoExceptionClause *clause;
692 MonoBasicBlock *handler;
696 for (i = 0; i < header->num_clauses; ++i) {
697 clause = &header->clauses [i];
698 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
699 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
700 if (clause->flags == type) {
701 handler = g_hash_table_lookup (cfg->bb_hash, header->code + clause->handler_offset);
703 res = g_list_append (res, handler);
711 mono_find_spvar_for_region (MonoCompile *cfg, int region)
713 return g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
717 mono_create_spvar_for_region (MonoCompile *cfg, int region)
721 var = g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
725 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
726 /* prevent it from being register allocated */
727 var->flags |= MONO_INST_INDIRECT;
729 g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
733 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
737 array [*dfn] = start;
738 /*g_print ("visit %d at %p\n", *dfn, start->cil_code);*/
739 for (i = 0; i < start->out_count; ++i) {
740 if (start->out_bb [i]->dfn)
743 start->out_bb [i]->dfn = *dfn;
744 start->out_bb [i]->df_parent = start;
745 array [*dfn] = start->out_bb [i];
746 df_visit (start->out_bb [i], dfn, array);
752 MonoBasicBlock *best;
756 previous_foreach (gconstpointer key, gpointer val, gpointer data)
758 PrevStruct *p = data;
759 MonoBasicBlock *bb = val;
760 //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,
761 //bb->method == p->best->method, bb->cil_code < p->code, bb->cil_code > p->best->cil_code);
763 if (bb->cil_code && bb->cil_code < p->code && bb->cil_code > p->best->cil_code)
767 static MonoBasicBlock*
768 find_previous (GHashTable *bb_hash, MonoBasicBlock *start, const guchar *code) {
774 g_hash_table_foreach (bb_hash, (GHFunc)previous_foreach, &p);
779 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
788 * FIXME: take into account all the details:
789 * second may have been the target of more than one bblock
791 second->out_count = first->out_count;
792 second->out_bb = first->out_bb;
794 for (i = 0; i < first->out_count; ++i) {
795 bb = first->out_bb [i];
796 for (j = 0; j < bb->in_count; ++j) {
797 if (bb->in_bb [j] == first)
798 bb->in_bb [j] = second;
802 first->out_count = 0;
803 first->out_bb = NULL;
804 link_bblock (cfg, first, second);
806 second->last_ins = first->last_ins;
808 /*g_print ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
809 for (inst = first->code; inst && inst->next; inst = inst->next) {
810 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
811 g_print ("found %p: %s", inst->next->cil_code, code);
813 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
814 second->code = inst->next;
816 first->last_ins = inst;
817 second->next_bb = first->next_bb;
818 first->next_bb = second;
823 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
829 mono_type_to_ldind (MonoType *type)
841 case MONO_TYPE_BOOLEAN:
855 case MONO_TYPE_FNPTR:
857 case MONO_TYPE_CLASS:
858 case MONO_TYPE_STRING:
859 case MONO_TYPE_OBJECT:
860 case MONO_TYPE_SZARRAY:
861 case MONO_TYPE_ARRAY:
862 return CEE_LDIND_REF;
870 case MONO_TYPE_VALUETYPE:
871 if (type->data.klass->enumtype) {
872 t = type->data.klass->enum_basetype->type;
876 case MONO_TYPE_TYPEDBYREF:
878 case MONO_TYPE_GENERICINST:
879 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE)
881 return CEE_LDIND_REF;
883 g_error ("unknown type 0x%02x in type_to_ldind", type->type);
889 mono_type_to_stind (MonoType *type)
900 case MONO_TYPE_BOOLEAN:
912 case MONO_TYPE_FNPTR:
914 case MONO_TYPE_CLASS:
915 case MONO_TYPE_STRING:
916 case MONO_TYPE_OBJECT:
917 case MONO_TYPE_SZARRAY:
918 case MONO_TYPE_ARRAY:
919 return CEE_STIND_REF;
927 case MONO_TYPE_VALUETYPE:
928 if (type->data.klass->enumtype) {
929 t = type->data.klass->enum_basetype->type;
933 case MONO_TYPE_TYPEDBYREF:
935 case MONO_TYPE_GENERICINST:
936 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE)
938 return CEE_STIND_REF;
940 g_error ("unknown type 0x%02x in type_to_stind", type->type);
946 * Returns the type used in the eval stack when @type is loaded.
947 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
950 type_to_eval_stack_type (MonoType *type, MonoInst *inst) {
954 inst->type = STACK_MP;
962 case MONO_TYPE_BOOLEAN:
968 inst->type = STACK_I4;
973 case MONO_TYPE_FNPTR:
974 inst->type = STACK_PTR;
976 case MONO_TYPE_CLASS:
977 case MONO_TYPE_STRING:
978 case MONO_TYPE_OBJECT:
979 case MONO_TYPE_SZARRAY:
980 case MONO_TYPE_ARRAY:
981 inst->type = STACK_OBJ;
985 inst->type = STACK_I8;
989 inst->type = STACK_R8;
991 case MONO_TYPE_VALUETYPE:
992 if (type->data.klass->enumtype) {
993 t = type->data.klass->enum_basetype->type;
996 inst->klass = type->data.klass;
997 inst->type = STACK_VTYPE;
1000 case MONO_TYPE_TYPEDBYREF:
1001 inst->klass = mono_defaults.typed_reference_class;
1002 inst->type = STACK_VTYPE;
1004 case MONO_TYPE_GENERICINST:
1005 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE) {
1006 inst->klass = mono_class_from_mono_type (type);
1007 inst->type = STACK_VTYPE;
1009 inst->type = STACK_OBJ;
1013 g_error ("unknown type 0x%02x in eval stack type", type->type);
1018 * The following tables are used to quickly validate the IL code in type_from_op ().
1021 bin_num_table [STACK_MAX] [STACK_MAX] = {
1022 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1023 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1024 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1025 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1026 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
1027 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
1028 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1029 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1034 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
1037 /* reduce the size of this table */
1039 bin_int_table [STACK_MAX] [STACK_MAX] = {
1040 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1041 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1042 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1043 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1044 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1045 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1046 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1047 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1051 bin_comp_table [STACK_MAX] [STACK_MAX] = {
1053 {0, 1, 0, 1, 0, 0, 4, 0},
1054 {0, 0, 1, 0, 0, 0, 0, 0},
1055 {0, 1, 0, 1, 0, 2, 4, 0},
1056 {0, 0, 0, 0, 1, 0, 0, 0},
1057 {0, 0, 0, 2, 0, 1, 0, 0},
1058 {0, 4, 0, 4, 0, 0, 3, 0},
1059 {0, 0, 0, 0, 0, 0, 0, 0},
1062 /* reduce the size of this table */
1064 shift_table [STACK_MAX] [STACK_MAX] = {
1065 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1066 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1067 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1068 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1069 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1070 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1071 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1072 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1076 * Tables to map from the non-specific opcode to the matching
1077 * type-specific opcode.
1079 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
1080 static const guint16
1081 binops_op_map [STACK_MAX] = {
1082 0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, 0
1085 /* handles from CEE_NEG to CEE_CONV_U8 */
1086 static const guint16
1087 unops_op_map [STACK_MAX] = {
1088 0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, 0
1091 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
1092 static const guint16
1093 ovfops_op_map [STACK_MAX] = {
1094 0, 0, OP_LCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_FCONV_TO_U2-CEE_CONV_U2, 0
1097 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
1098 static const guint16
1099 ovf2ops_op_map [STACK_MAX] = {
1100 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
1103 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
1104 static const guint16
1105 ovf3ops_op_map [STACK_MAX] = {
1106 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
1109 /* handles from CEE_CEQ to CEE_CLT_UN */
1110 static const guint16
1111 ceqops_op_map [STACK_MAX] = {
1112 0, 0, OP_LCEQ-CEE_CEQ, OP_PCEQ-CEE_CEQ, OP_FCEQ-CEE_CEQ, 0
1116 * Sets ins->type (the type on the eval stack) according to the
1117 * type of the opcode and the arguments to it.
1118 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1120 * FIXME: this function sets ins->type unconditionally in some cases, but
1121 * it should set it to invalid for some types (a conv.x on an object)
1124 type_from_op (MonoInst *ins) {
1125 switch (ins->opcode) {
1132 /* FIXME: check unverifiable args for STACK_MP */
1133 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1134 ins->opcode += binops_op_map [ins->type];
1141 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1142 ins->opcode += binops_op_map [ins->type];
1147 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1148 ins->opcode += binops_op_map [ins->type];
1151 /* FIXME: handle some specifics with ins->next->type */
1152 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1159 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1160 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1164 ins->type = neg_table [ins->inst_i0->type];
1165 ins->opcode += unops_op_map [ins->type];
1168 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1169 ins->type = ins->inst_i0->type;
1171 ins->type = STACK_INV;
1172 ins->opcode += unops_op_map [ins->type];
1178 ins->type = STACK_I4;
1179 ins->opcode += unops_op_map [ins->inst_i0->type];
1182 ins->type = STACK_R8;
1183 switch (ins->inst_i0->type) {
1188 ins->opcode = OP_LCONV_TO_R_UN;
1192 case CEE_CONV_OVF_I1:
1193 case CEE_CONV_OVF_U1:
1194 case CEE_CONV_OVF_I2:
1195 case CEE_CONV_OVF_U2:
1196 case CEE_CONV_OVF_I4:
1197 case CEE_CONV_OVF_U4:
1198 ins->type = STACK_I4;
1199 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1201 case CEE_CONV_OVF_I_UN:
1202 case CEE_CONV_OVF_U_UN:
1203 ins->type = STACK_PTR;
1204 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1206 case CEE_CONV_OVF_I1_UN:
1207 case CEE_CONV_OVF_I2_UN:
1208 case CEE_CONV_OVF_I4_UN:
1209 case CEE_CONV_OVF_U1_UN:
1210 case CEE_CONV_OVF_U2_UN:
1211 case CEE_CONV_OVF_U4_UN:
1212 ins->type = STACK_I4;
1213 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1216 ins->type = STACK_PTR;
1217 switch (ins->inst_i0->type) {
1223 ins->opcode = OP_LCONV_TO_U;
1226 ins->opcode = OP_FCONV_TO_U;
1232 ins->type = STACK_I8;
1233 ins->opcode += unops_op_map [ins->inst_i0->type];
1235 case CEE_CONV_OVF_I8:
1236 case CEE_CONV_OVF_U8:
1237 ins->type = STACK_I8;
1238 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1240 case CEE_CONV_OVF_U8_UN:
1241 case CEE_CONV_OVF_I8_UN:
1242 ins->type = STACK_I8;
1243 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1247 ins->type = STACK_R8;
1248 ins->opcode += unops_op_map [ins->inst_i0->type];
1251 ins->type = STACK_R8;
1255 ins->type = STACK_I4;
1256 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1259 case CEE_CONV_OVF_I:
1260 case CEE_CONV_OVF_U:
1261 ins->type = STACK_PTR;
1262 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1265 case CEE_ADD_OVF_UN:
1267 case CEE_MUL_OVF_UN:
1269 case CEE_SUB_OVF_UN:
1270 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1271 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1274 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1281 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_MP, STACK_R8, STACK_R8, STACK_OBJ
1284 /* map ldelem.x to the matching ldind.x opcode */
1286 ldelem_to_ldind [] = {
1300 /* map stelem.x to the matching stind.x opcode */
1302 stelem_to_stind [] = {
1316 param_table [STACK_MAX] [STACK_MAX] = {
1321 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1325 switch (args->type) {
1335 for (i = 0; i < sig->param_count; ++i) {
1336 switch (args [i].type) {
1340 if (!sig->params [i]->byref)
1344 if (sig->params [i]->byref)
1346 switch (sig->params [i]->type) {
1347 case MONO_TYPE_CLASS:
1348 case MONO_TYPE_STRING:
1349 case MONO_TYPE_OBJECT:
1350 case MONO_TYPE_SZARRAY:
1351 case MONO_TYPE_ARRAY:
1358 if (sig->params [i]->byref)
1360 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1369 /*if (!param_table [args [i].type] [sig->params [i]->type])
1377 * When we need a pointer to the current domain many times in a method, we
1378 * call mono_domain_get() once and we store the result in a local variable.
1379 * This function returns the variable that represents the MonoDomain*.
1381 inline static MonoInst *
1382 mono_get_domainvar (MonoCompile *cfg)
1384 if (!cfg->domainvar)
1385 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1386 return cfg->domainvar;
1390 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
1393 int num = cfg->num_varinfo;
1395 if ((num + 1) >= cfg->varinfo_count) {
1396 cfg->varinfo_count = (cfg->varinfo_count + 2) * 2;
1397 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
1398 cfg->vars = (MonoMethodVar **)g_realloc (cfg->vars, sizeof (MonoMethodVar*) * cfg->varinfo_count);
1401 /*g_print ("created temp %d of type 0x%x\n", num, type->type);*/
1402 mono_jit_stats.allocate_var++;
1404 MONO_INST_NEW (cfg, inst, opcode);
1405 inst->inst_c0 = num;
1406 inst->inst_vtype = type;
1407 inst->klass = mono_class_from_mono_type (type);
1408 /* if set to 1 the variable is native */
1411 cfg->varinfo [num] = inst;
1413 cfg->vars [num] = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMethodVar));
1414 MONO_INIT_VARINFO (cfg->vars [num], num);
1417 //g_print ("created temp %d of type %s\n", num, mono_type_get_name (type));
1422 type_from_stack_type (MonoInst *ins) {
1423 switch (ins->type) {
1424 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1425 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1426 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1427 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1428 case STACK_MP: return &mono_defaults.int_class->byval_arg;
1429 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1430 case STACK_VTYPE: return &ins->klass->byval_arg;
1432 g_error ("stack type %d to montype not handled\n", ins->type);
1438 array_access_to_klass (int opcode)
1442 return mono_defaults.byte_class;
1444 return mono_defaults.uint16_class;
1447 return mono_defaults.int_class;
1450 return mono_defaults.sbyte_class;
1453 return mono_defaults.int16_class;
1456 return mono_defaults.int32_class;
1458 return mono_defaults.uint32_class;
1461 return mono_defaults.int64_class;
1464 return mono_defaults.single_class;
1467 return mono_defaults.double_class;
1468 case CEE_LDELEM_REF:
1469 case CEE_STELEM_REF:
1470 return mono_defaults.object_class;
1472 g_assert_not_reached ();
1478 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
1482 MONO_ADD_INS (bb, inst);
1485 switch (bb->last_ins->opcode) {
1499 while (prev->next && prev->next != bb->last_ins)
1501 if (prev == bb->code) {
1502 if (bb->last_ins == bb->code) {
1503 inst->next = bb->code;
1506 inst->next = prev->next;
1510 inst->next = bb->last_ins;
1514 // g_warning ("handle conditional jump in add_ins_to_end ()\n");
1516 MONO_ADD_INS (bb, inst);
1522 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
1524 MonoInst *inst, *load;
1526 NEW_TEMPLOAD (cfg, load, src);
1528 NEW_TEMPSTORE (cfg, inst, dest, load);
1529 if (inst->opcode == CEE_STOBJ) {
1530 NEW_TEMPLOADA (cfg, inst, dest);
1531 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE);
1533 inst->cil_code = NULL;
1534 mono_add_ins_to_end (bb, inst);
1539 * We try to share variables when possible
1542 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1547 /* inlining can result in deeper stacks */
1548 if (slot >= ((MonoMethodNormal *)cfg->method)->header->max_stack)
1549 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1551 pos = ins->type - 1 + slot * STACK_MAX;
1553 switch (ins->type) {
1560 if ((vnum = cfg->intvars [pos]))
1561 return cfg->varinfo [vnum];
1562 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1563 cfg->intvars [pos] = res->inst_c0;
1566 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1572 * This function is called to handle items that are left on the evaluation stack
1573 * at basic block boundaries. What happens is that we save the values to local variables
1574 * and we reload them later when first entering the target basic block (with the
1575 * handle_loaded_temps () function).
1576 * It is also used to handle items on the stack in store opcodes, since it is
1577 * possible that the variable to be stored into is already on the stack, in
1578 * which case its old value should be used.
1579 * A single joint point will use the same variables (stored in the array bb->out_stack or
1580 * bb->in_stack, if the basic block is before or after the joint point).
1583 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count) {
1585 MonoBasicBlock *outb;
1586 MonoInst *inst, **locals;
1591 if (cfg->verbose_level > 3)
1592 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
1593 if (!bb->out_scount) {
1594 bb->out_scount = count;
1595 //g_print ("bblock %d has out:", bb->block_num);
1597 for (i = 0; i < bb->out_count; ++i) {
1598 outb = bb->out_bb [i];
1599 //g_print (" %d", outb->block_num);
1600 if (outb->in_stack) {
1602 bb->out_stack = outb->in_stack;
1608 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1609 for (i = 0; i < count; ++i) {
1611 * try to reuse temps already allocated for this purpouse, if they occupy the same
1612 * stack slot and if they are of the same type.
1613 * This won't cause conflicts since if 'local' is used to
1614 * store one of the values in the in_stack of a bblock, then
1615 * the same variable will be used for the same outgoing stack
1617 * This doesn't work when inlining methods, since the bblocks
1618 * in the inlined methods do not inherit their in_stack from
1619 * the bblock they are inlined to. See bug #58863 for an
1622 if (cfg->inlined_method)
1623 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1625 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1630 for (i = 0; i < bb->out_count; ++i) {
1631 outb = bb->out_bb [i];
1632 if (outb->in_scount)
1633 continue; /* check they are the same locals */
1634 outb->in_scount = count;
1635 outb->in_stack = bb->out_stack;
1638 locals = bb->out_stack;
1639 for (i = 0; i < count; ++i) {
1640 /* add store ops at the end of the bb, before the branch */
1641 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1642 if (inst->opcode == CEE_STOBJ) {
1643 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
1644 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE);
1646 inst->cil_code = sp [i]->cil_code;
1647 mono_add_ins_to_end (bb, inst);
1649 if (cfg->verbose_level > 3)
1650 g_print ("storing %d to temp %d\n", i, locals [i]->inst_c0);
1654 * It is possible that the out bblocks already have in_stack assigned, and
1655 * the in_stacks differ. In this case, we will store to all the different
1662 /* Find a bblock which has a different in_stack */
1664 while (bindex < bb->out_count) {
1665 outb = bb->out_bb [bindex];
1666 if (outb->in_stack != locals) {
1668 * Instead of storing sp [i] to locals [i], we need to store
1669 * locals [i] to <new locals>[i], since the sp [i] tree can't
1670 * be shared between trees.
1672 for (i = 0; i < count; ++i)
1673 mono_add_varcopy_to_end (cfg, bb, locals [i]->inst_c0, outb->in_stack [i]->inst_c0);
1674 locals = outb->in_stack;
1686 ret_type_to_call_opcode (MonoType *type, int calli, int virt)
1691 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1695 case MONO_TYPE_VOID:
1696 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
1699 case MONO_TYPE_BOOLEAN:
1702 case MONO_TYPE_CHAR:
1705 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1709 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1710 case MONO_TYPE_CLASS:
1711 case MONO_TYPE_STRING:
1712 case MONO_TYPE_OBJECT:
1713 case MONO_TYPE_SZARRAY:
1714 case MONO_TYPE_ARRAY:
1715 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1718 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
1721 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
1722 case MONO_TYPE_VALUETYPE:
1723 if (type->data.klass->enumtype) {
1724 t = type->data.klass->enum_basetype->type;
1727 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1728 case MONO_TYPE_TYPEDBYREF:
1729 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1730 case MONO_TYPE_GENERICINST:
1731 t = type->data.generic_inst->generic_type->type;
1734 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
1740 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
1742 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
1744 ji->ip.label = label;
1745 ji->type = MONO_PATCH_INFO_SWITCH;
1746 ji->data.table = bbs;
1747 ji->next = cfg->patch_info;
1748 ji->table_size = num_blocks;
1749 cfg->patch_info = ji;
1753 * When we add a tree of instructions, we need to ensure the instructions currently
1754 * on the stack are executed before (like, if we load a value from a local).
1755 * We ensure this by saving the currently loaded values to temps and rewriting the
1756 * instructions to load the values.
1757 * This is not done for opcodes that terminate a basic block (because it's handled already
1758 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
1761 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
1763 MonoInst *load, *store, *temp, *ins;
1765 while (stack < sp) {
1767 /* handle also other constants */
1768 if (ins->opcode != OP_ICONST) {
1769 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1770 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1771 store->cil_code = ins->cil_code;
1772 if (store->opcode == CEE_STOBJ) {
1773 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
1774 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE);
1776 MONO_ADD_INS (bblock, store);
1777 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1778 load->cil_code = ins->cil_code;
1786 * Prepare arguments for passing to a function call.
1787 * Return a non-zero value if the arguments can't be passed to the given
1789 * The type checks are not yet complete and some conversions may need
1790 * casts on 32 or 64 bit architectures.
1793 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
1798 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
1802 for (i = 0; i < sig->param_count; ++i) {
1803 if (sig->params [i]->byref) {
1805 * check the result of ldelema is only passed as an argument if the byref
1806 * type matches exactly the array element type.
1807 * FIXME: if the argument as been saved on the stack as part of the
1808 * interface variable code (the value was on the stack at a basic block boundary)
1809 * we need to add the check in that case, too.
1811 if (args [i]->opcode == CEE_LDELEMA) {
1813 MonoClass *exact_class = mono_class_from_mono_type (sig->params [i]);
1814 if (!exact_class->valuetype) {
1815 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
1816 check->cil_code = args [i]->cil_code;
1817 check->klass = exact_class;
1818 check->inst_left = args [i]->inst_left;
1819 check->type = STACK_OBJ;
1820 args [i]->inst_left = check;
1823 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
1827 simple_type = sig->params [i]->type;
1829 switch (simple_type) {
1830 case MONO_TYPE_VOID:
1835 case MONO_TYPE_BOOLEAN:
1838 case MONO_TYPE_CHAR:
1841 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
1847 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
1850 case MONO_TYPE_CLASS:
1851 case MONO_TYPE_STRING:
1852 case MONO_TYPE_OBJECT:
1853 case MONO_TYPE_SZARRAY:
1854 case MONO_TYPE_ARRAY:
1855 if (args [i]->type != STACK_OBJ)
1860 if (args [i]->type != STACK_I8)
1865 if (args [i]->type != STACK_R8)
1868 case MONO_TYPE_VALUETYPE:
1869 if (sig->params [i]->data.klass->enumtype) {
1870 simple_type = sig->params [i]->data.klass->enum_basetype->type;
1873 if (args [i]->type != STACK_VTYPE)
1876 case MONO_TYPE_TYPEDBYREF:
1877 if (args [i]->type != STACK_VTYPE)
1880 case MONO_TYPE_GENERICINST:
1881 simple_type = sig->params [i]->data.generic_inst->generic_type->type;
1885 g_error ("unknown type 0x%02x in check_call_signature", simple_type);
1892 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
1893 const guint8 *ip, gboolean to_end)
1895 MonoInst *temp, *store, *ins = (MonoInst*)call;
1896 MonoType *ret = sig->ret;
1898 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
1900 call->inst.type = STACK_OBJ;
1901 call->inst.opcode = CEE_CALL;
1902 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
1904 type_to_eval_stack_type (ret, ins);
1905 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
1908 if (MONO_TYPE_ISSTRUCT (ret)) {
1911 /* we use this to allocate native sized structs */
1912 temp->unused = sig->pinvoke;
1914 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
1915 if (call->inst.opcode == OP_VCALL)
1916 ins->inst_left = loada;
1918 ins->inst_right = loada; /* a virtual or indirect call */
1921 mono_add_ins_to_end (bblock, ins);
1923 MONO_ADD_INS (bblock, ins);
1925 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1926 store->cil_code = ip;
1928 mono_add_ins_to_end (bblock, store);
1930 MONO_ADD_INS (bblock, store);
1932 return temp->inst_c0;
1935 mono_add_ins_to_end (bblock, ins);
1937 MONO_ADD_INS (bblock, ins);
1942 inline static MonoCallInst *
1943 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1944 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
1949 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual));
1951 call->inst.cil_code = ip;
1953 call->signature = sig;
1954 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
1956 for (arg = call->out_args; arg;) {
1957 MonoInst *narg = arg->next;
1962 mono_add_ins_to_end (bblock, arg);
1964 MONO_ADD_INS (bblock, arg);
1971 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1972 MonoInst **args, MonoInst *addr, const guint8 *ip)
1974 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
1976 call->inst.inst_i0 = addr;
1978 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
1981 static MonoCallInst*
1982 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
1983 MonoInst **args, const guint8 *ip, MonoInst *this)
1985 gboolean virtual = this != NULL;
1988 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, FALSE);
1990 if (this && sig->hasthis &&
1991 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
1992 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
1993 call->method = mono_marshal_get_remoting_invoke_with_check (method);
1995 call->method = method;
1997 call->inst.flags |= MONO_INST_HAS_METHOD;
1998 call->inst.inst_left = this;
2004 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
2005 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
2007 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, signature, args, ip, this);
2009 return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
2013 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
2014 MonoInst **args, const guint8 *ip, gboolean to_end)
2020 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
2022 return mono_spill_call (cfg, bblock, call, sig, func == mono_array_new_va, ip, to_end);
2026 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
2028 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
2031 g_warning ("unregistered JIT ICall");
2032 g_assert_not_reached ();
2035 return mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, args, ip, FALSE);
2039 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
2041 MonoInst *ins, *temp = NULL, *store, *load, *begin;
2042 MonoInst *last_arg = NULL;
2046 //g_print ("emulating: ");
2047 //mono_print_tree_nl (tree);
2048 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE));
2049 ins = (MonoInst*)call;
2051 call->inst.cil_code = tree->cil_code;
2053 call->signature = info->sig;
2055 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
2057 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2058 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
2059 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2060 store->cil_code = tree->cil_code;
2065 nargs = info->sig->param_count + info->sig->hasthis;
2067 for (last_arg = call->out_args; last_arg && last_arg->next; last_arg = last_arg->next) ;
2070 last_arg->next = store;
2073 begin = call->out_args;
2077 if (cfg->prev_ins) {
2079 * This assumes that that in a tree, emulate_opcode is called for a
2080 * node before it is called for its children. dec_foreach needs to
2081 * take this into account.
2083 store->next = cfg->prev_ins->next;
2084 cfg->prev_ins->next = begin;
2086 store->next = cfg->cbb->code;
2087 cfg->cbb->code = begin;
2090 call->fptr = mono_icall_get_wrapper (info);
2092 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2093 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2098 static MonoMethodSignature *
2099 mono_get_element_address_signature (int arity)
2101 static GHashTable *sighash = NULL;
2102 MonoMethodSignature *res;
2105 EnterCriticalSection (&trampoline_hash_mutex);
2107 sighash = g_hash_table_new (NULL, NULL);
2109 else if ((res = g_hash_table_lookup (sighash, (gpointer)arity))) {
2110 LeaveCriticalSection (&trampoline_hash_mutex);
2114 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
2117 res->params [0] = &mono_defaults.array_class->byval_arg;
2119 for (i = 1; i <= arity; i++)
2120 res->params [i] = &mono_defaults.int_class->byval_arg;
2122 res->ret = &mono_defaults.int_class->byval_arg;
2124 g_hash_table_insert (sighash, (gpointer)arity, res);
2125 LeaveCriticalSection (&trampoline_hash_mutex);
2131 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native) {
2132 MonoInst *iargs [3];
2138 * This check breaks with spilled vars... need to handle it during verification anyway.
2139 * g_assert (klass && klass == src->klass && klass == dest->klass);
2143 n = mono_class_native_size (klass, &align);
2145 n = mono_class_value_size (klass, &align);
2147 if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
2149 MONO_INST_NEW (cfg, inst, OP_MEMCPY);
2150 inst->inst_left = dest;
2151 inst->inst_right = src;
2152 inst->cil_code = ip;
2154 MONO_ADD_INS (bblock, inst);
2159 NEW_ICONST (cfg, iargs [2], n);
2161 mono_emit_native_call (cfg, bblock, helper_memcpy, helper_sig_memcpy, iargs, ip, to_end);
2165 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
2167 MonoInst *iargs [2];
2168 MonoInst *ins, *zero_int32;
2171 NEW_ICONST (cfg, zero_int32, 0);
2173 mono_class_init (klass);
2174 n = mono_class_value_size (klass, NULL);
2175 MONO_INST_NEW (cfg, ins, 0);
2177 ins->inst_left = dest;
2178 ins->inst_right = zero_int32;
2181 ins->opcode = CEE_STIND_I1;
2182 MONO_ADD_INS (bblock, ins);
2185 ins->opcode = CEE_STIND_I2;
2186 MONO_ADD_INS (bblock, ins);
2189 ins->opcode = CEE_STIND_I4;
2190 MONO_ADD_INS (bblock, ins);
2193 if (n <= sizeof (gpointer) * 5) {
2194 ins->opcode = OP_MEMSET;
2197 MONO_ADD_INS (bblock, ins);
2200 handle_loaded_temps (cfg, bblock, stack_start, sp);
2201 NEW_ICONST (cfg, ins, n);
2204 mono_emit_jit_icall (cfg, bblock, helper_initobj, iargs, ip);
2210 handle_box (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *val, const guchar *ip, MonoClass *klass)
2212 MonoInst *iargs [2];
2213 MonoInst *dest, *vtoffset, *add, *vstore;
2216 /* much like NEWOBJ */
2217 if (cfg->opt & MONO_OPT_SHARED) {
2218 NEW_DOMAINCONST (cfg, iargs [0]);
2219 NEW_CLASSCONST (cfg, iargs [1], klass);
2221 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
2223 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
2224 NEW_VTABLECONST (cfg, iargs [0], vtable);
2225 if (klass->has_finalize || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
2226 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
2228 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
2230 NEW_TEMPLOAD (cfg, dest, temp);
2231 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
2232 MONO_INST_NEW (cfg, add, CEE_ADD);
2233 add->inst_left = dest;
2234 add->inst_right = vtoffset;
2237 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
2238 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
2239 vstore->cil_code = ip;
2240 vstore->inst_left = add;
2241 vstore->inst_right = val;
2243 if (vstore->opcode == CEE_STOBJ) {
2244 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
2246 MONO_ADD_INS (bblock, vstore);
2248 NEW_TEMPLOAD (cfg, dest, temp);
2252 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
2255 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
2257 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
2258 MonoMethodSignature *signature = method->signature;
2262 #ifdef MONO_ARCH_HAVE_LMF_OPS
2263 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
2264 !MONO_TYPE_ISSTRUCT (signature->ret) && (method->klass->parent != mono_defaults.array_class))
2268 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2269 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2270 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
2271 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
2272 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2273 (method->klass->marshalbyref) ||
2274 !header || header->num_clauses ||
2275 /* fixme: why cant we inline valuetype returns? */
2276 MONO_TYPE_ISSTRUCT (signature->ret))
2279 /* its not worth to inline methods with valuetype arguments?? */
2280 for (i = 0; i < signature->param_count; i++) {
2281 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
2287 * if we can initialize the class of the method right away, we do,
2288 * otherwise we don't allow inlining if the class needs initialization,
2289 * since it would mean inserting a call to mono_runtime_class_init()
2290 * inside the inlined code
2292 if (!(cfg->opt & MONO_OPT_SHARED)) {
2293 vtable = mono_class_vtable (cfg->domain, method->klass);
2294 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
2295 if (cfg->run_cctors)
2296 mono_runtime_class_init (vtable);
2298 else if (!vtable->initialized && mono_class_needs_cctor_run (method->klass, NULL))
2302 * If we're compiling for shared code
2303 * the cctor will need to be run at aot method load time, for example,
2304 * or at the end of the compilation of the inlining method.
2306 if (mono_class_needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
2309 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
2311 /* also consider num_locals? */
2312 if (getenv ("MONO_INLINELIMIT"))
2313 if (header->code_size < atoi (getenv ("MONO_INLINELIMIT"))) {
2317 if (header->code_size < 20)
2324 mini_get_ldelema_ins (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
2328 MonoMethodSignature *esig;
2329 char icall_name [256];
2330 MonoJitICallInfo *info;
2332 rank = cmethod->signature->param_count - (is_set? 1: 0);
2334 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
2336 NEW_GROUP (cfg, indexes, sp [1], sp [2]);
2337 MONO_INST_NEW (cfg, addr, OP_LDELEMA2D);
2338 addr->inst_left = sp [0];
2339 addr->inst_right = indexes;
2340 addr->cil_code = ip;
2341 addr->type = STACK_MP;
2342 addr->klass = cmethod->klass;
2346 /* Need to register the icall so it gets an icall wrapper */
2347 sprintf (icall_name, "ves_array_element_address_%d", rank);
2349 info = mono_find_jit_icall_by_name (icall_name);
2351 esig = mono_get_element_address_signature (rank);
2352 info = mono_register_jit_icall (ves_array_element_address, g_strdup (icall_name), esig, FALSE);
2355 temp = mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, sp, ip, FALSE);
2356 cfg->flags |= MONO_CFG_HAS_VARARGS;
2358 NEW_TEMPLOAD (cfg, addr, temp);
2362 static MonoJitICallInfo **emul_opcode_map = NULL;
2364 static inline MonoJitICallInfo *
2365 mono_find_jit_opcode_emulation (int opcode)
2367 if (emul_opcode_map)
2368 return emul_opcode_map [opcode];
2374 mini_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
2379 if (cmethod->klass == mono_defaults.string_class) {
2380 if (cmethod->name [0] != 'g')
2383 if (strcmp (cmethod->name, "get_Chars") == 0)
2385 else if (strcmp (cmethod->name, "get_Length") == 0)
2388 } else if (cmethod->klass == mono_defaults.object_class) {
2389 if (strcmp (cmethod->name, "GetType") == 0)
2393 } else if (cmethod->klass == mono_defaults.array_class) {
2394 if (strcmp (cmethod->name, "get_Rank") == 0)
2396 else if (strcmp (cmethod->name, "get_Length") == 0)
2401 op = mono_arch_get_opcode_for_method (cfg, cmethod, fsig, args);
2405 pc = fsig->param_count + fsig->hasthis;
2406 MONO_INST_NEW (cfg, ins, op);
2409 ins->inst_i0 = args [0];
2411 ins->inst_i1 = args [1];
2418 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
2420 MonoInst *store, *temp;
2423 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
2425 if (!sig->hasthis && sig->param_count == 0)
2429 if (sp [0]->opcode == OP_ICONST) {
2432 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
2434 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2435 store->cil_code = sp [0]->cil_code;
2436 MONO_ADD_INS (bblock, store);
2441 for (i = 0; i < sig->param_count; ++i) {
2442 if (sp [0]->opcode == OP_ICONST) {
2445 temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
2447 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2448 store->cil_code = sp [0]->cil_code;
2449 if (store->opcode == CEE_STOBJ) {
2450 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2451 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE);
2453 MONO_ADD_INS (bblock, store);
2461 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
2462 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b, gboolean inline_allways)
2464 MonoInst *ins, *rvar = NULL;
2465 MonoMethodHeader *cheader;
2466 MonoBasicBlock *ebblock, *sbblock;
2467 int i, costs, new_locals_offset;
2468 MonoMethod *prev_inlined_method;
2470 if (cfg->verbose_level > 2)
2471 g_print ("INLINE START %p %s -> %s\n", cmethod, mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2473 if (!cmethod->inline_info) {
2474 mono_jit_stats.inlineable_methods++;
2475 cmethod->inline_info = 1;
2477 /* allocate space to store the return value */
2478 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2479 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2482 /* allocate local variables */
2483 cheader = ((MonoMethodNormal *)cmethod)->header;
2484 new_locals_offset = cfg->num_varinfo;
2485 for (i = 0; i < cheader->num_locals; ++i)
2486 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
2488 /* allocate starte and end blocks */
2489 sbblock = NEW_BBLOCK (cfg);
2490 sbblock->block_num = cfg->num_bblocks++;
2491 sbblock->real_offset = real_offset;
2493 ebblock = NEW_BBLOCK (cfg);
2494 ebblock->block_num = cfg->num_bblocks++;
2495 ebblock->real_offset = real_offset;
2497 prev_inlined_method = cfg->inlined_method;
2498 cfg->inlined_method = cmethod;
2500 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
2502 cfg->inlined_method = prev_inlined_method;
2504 if ((costs >= 0 && costs < 60) || inline_allways) {
2505 if (cfg->verbose_level > 2)
2506 g_print ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2508 mono_jit_stats.inlined_methods++;
2510 /* always add some code to avoid block split failures */
2511 MONO_INST_NEW (cfg, ins, CEE_NOP);
2512 MONO_ADD_INS (bblock, ins);
2515 bblock->next_bb = sbblock;
2516 link_bblock (cfg, bblock, sbblock);
2519 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
2525 if (cfg->verbose_level > 2)
2526 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
2532 * Some of these comments may well be out-of-date.
2533 * Design decisions: we do a single pass over the IL code (and we do bblock
2534 * splitting/merging in the few cases when it's required: a back jump to an IL
2535 * address that was not already seen as bblock starting point).
2536 * Code is validated as we go (full verification is still better left to metadata/verify.c).
2537 * Complex operations are decomposed in simpler ones right away. We need to let the
2538 * arch-specific code peek and poke inside this process somehow (except when the
2539 * optimizations can take advantage of the full semantic info of coarse opcodes).
2540 * All the opcodes of the form opcode.s are 'normalized' to opcode.
2541 * MonoInst->opcode initially is the IL opcode or some simplification of that
2542 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
2543 * opcode with value bigger than OP_LAST.
2544 * At this point the IR can be handed over to an interpreter, a dumb code generator
2545 * or to the optimizing code generator that will translate it to SSA form.
2547 * Profiling directed optimizations.
2548 * We may compile by default with few or no optimizations and instrument the code
2549 * or the user may indicate what methods to optimize the most either in a config file
2550 * or through repeated runs where the compiler applies offline the optimizations to
2551 * each method and then decides if it was worth it.
2554 * * consider using an array instead of an hash table (bb_hash)
2557 #define CHECK_TYPE(ins) if (!(ins)->type) goto unverified
2558 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) goto unverified
2559 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) goto unverified
2560 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) goto unverified
2561 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) goto unverified
2562 #define CHECK_OPSIZE(size) if (ip + size > end) goto unverified
2565 /* offset from br.s -> br like opcodes */
2566 #define BIG_BRANCH_OFFSET 13
2569 get_basic_blocks (MonoCompile *cfg, GHashTable *bbhash, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
2571 unsigned char *ip = start;
2572 unsigned char *target;
2575 MonoBasicBlock *bblock;
2576 const MonoOpcode *opcode;
2579 cli_addr = ip - start;
2580 i = mono_opcode_value ((const guint8 **)&ip, end);
2583 opcode = &mono_opcodes [i];
2584 switch (opcode->argument) {
2585 case MonoInlineNone:
2588 case MonoInlineString:
2589 case MonoInlineType:
2590 case MonoInlineField:
2591 case MonoInlineMethod:
2594 case MonoShortInlineR:
2601 case MonoShortInlineVar:
2602 case MonoShortInlineI:
2605 case MonoShortInlineBrTarget:
2606 target = start + cli_addr + 2 + (signed char)ip [1];
2607 GET_BBLOCK (cfg, bbhash, bblock, target);
2610 case MonoInlineBrTarget:
2611 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
2612 GET_BBLOCK (cfg, bbhash, bblock, target);
2615 case MonoInlineSwitch: {
2616 guint32 n = read32 (ip + 1);
2619 cli_addr += 5 + 4 * n;
2620 target = start + cli_addr;
2621 GET_BBLOCK (cfg, bbhash, bblock, target);
2623 for (j = 0; j < n; ++j) {
2624 target = start + cli_addr + (gint32)read32 (ip);
2625 GET_BBLOCK (cfg, bbhash, bblock, target);
2635 g_assert_not_reached ();
2645 emit_tree (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *ins)
2647 MonoInst *store, *temp, *load;
2648 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
2649 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2650 store->cil_code = ins->cil_code;
2651 MONO_ADD_INS (bblock, store);
2652 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2653 load->cil_code = ins->cil_code;
2658 * mono_method_to_ir: translates IL into basic blocks containing trees
2661 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
2662 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
2663 guint inline_offset, gboolean is_virtual_call)
2665 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
2666 MonoInst *ins, **sp, **stack_start;
2667 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
2669 MonoMethod *cmethod;
2670 MonoInst **arg_array;
2671 MonoMethodHeader *header;
2673 guint32 token, ins_flag;
2675 MonoClass *constrained_call = NULL;
2676 unsigned char *ip, *end, *target, *err_pos;
2677 static double r8_0 = 0.0;
2678 MonoMethodSignature *sig;
2679 MonoGenericContext *generic_context = NULL;
2680 MonoType **param_types;
2681 GList *bb_recheck = NULL, *tmp;
2682 int i, n, start_new_bblock, align;
2683 int num_calls = 0, inline_costs = 0;
2684 int *filter_lengths = NULL;
2685 int breakpoint_id = 0;
2686 guint real_offset, num_args;
2688 image = method->klass->image;
2689 header = ((MonoMethodNormal *)method)->header;
2690 sig = method->signature;
2691 num_args = sig->hasthis + sig->param_count;
2692 ip = (unsigned char*)header->code;
2693 end = ip + header->code_size;
2694 mono_jit_stats.cil_code_size += header->code_size;
2696 if (method->signature->is_inflated)
2697 generic_context = ((MonoMethodInflated *) method)->context;
2699 if (cfg->method == method) {
2701 bbhash = cfg->bb_hash;
2703 real_offset = inline_offset;
2704 bbhash = g_hash_table_new (g_direct_hash, NULL);
2707 if (cfg->verbose_level > 2)
2708 g_print ("method to IR %s\n", mono_method_full_name (method, TRUE));
2710 dont_inline = g_list_prepend (dont_inline, method);
2711 if (cfg->method == method) {
2713 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
2714 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
2717 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
2718 start_bblock->cil_code = NULL;
2719 start_bblock->cil_length = 0;
2720 start_bblock->block_num = cfg->num_bblocks++;
2723 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
2724 end_bblock->cil_code = NULL;
2725 end_bblock->cil_length = 0;
2726 end_bblock->block_num = cfg->num_bblocks++;
2727 g_assert (cfg->num_bblocks == 2);
2729 arg_array = alloca (sizeof (MonoInst *) * num_args);
2730 for (i = num_args - 1; i >= 0; i--)
2731 arg_array [i] = cfg->varinfo [i];
2733 if (header->num_clauses) {
2734 int size = sizeof (int) * header->num_clauses;
2735 filter_lengths = alloca (size);
2736 memset (filter_lengths, 0, size);
2738 cfg->spvars = g_hash_table_new (NULL, NULL);
2740 /* handle exception clauses */
2741 for (i = 0; i < header->num_clauses; ++i) {
2742 //unsigned char *p = ip;
2743 MonoExceptionClause *clause = &header->clauses [i];
2744 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->try_offset);
2745 tblock->real_offset = clause->try_offset;
2746 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
2747 tblock->real_offset = clause->handler_offset;
2749 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
2750 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2751 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2752 MONO_ADD_INS (tblock, ins);
2755 /*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);
2757 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
2759 /* catch and filter blocks get the exception object on the stack */
2760 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
2761 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2762 /* mostly like handle_stack_args (), but just sets the input args */
2763 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
2765 cfg->exvar = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
2766 /* prevent it from being register allocated */
2767 cfg->exvar->flags |= MONO_INST_INDIRECT;
2769 tblock->in_scount = 1;
2770 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2771 tblock->in_stack [0] = cfg->exvar;
2773 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2774 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->token_or_filter);
2775 tblock->real_offset = clause->token_or_filter;
2776 tblock->in_scount = 1;
2777 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2778 tblock->in_stack [0] = cfg->exvar;
2779 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2780 MONO_ADD_INS (tblock, ins);
2786 arg_array = alloca (sizeof (MonoInst *) * num_args);
2787 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
2790 /* FIRST CODE BLOCK */
2791 bblock = NEW_BBLOCK (cfg);
2792 bblock->cil_code = ip;
2794 ADD_BBLOCK (cfg, bbhash, bblock);
2796 if (cfg->method == method) {
2797 breakpoint_id = mono_debugger_method_has_breakpoint (method);
2798 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
2799 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2800 MONO_ADD_INS (bblock, ins);
2804 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED))) || mono_compile_aot) {
2805 /* we use a separate basic block for the initialization code */
2806 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
2807 init_localsbb->real_offset = real_offset;
2808 start_bblock->next_bb = init_localsbb;
2809 init_localsbb->next_bb = bblock;
2810 link_bblock (cfg, start_bblock, init_localsbb);
2811 link_bblock (cfg, init_localsbb, bblock);
2812 init_localsbb->block_num = cfg->num_bblocks++;
2814 start_bblock->next_bb = bblock;
2815 link_bblock (cfg, start_bblock, bblock);
2818 if (get_basic_blocks (cfg, bbhash, header, real_offset, ip, end, &err_pos)) {
2823 mono_debug_init_method (cfg, bblock, breakpoint_id);
2825 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
2827 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
2828 for (n = 0; n < sig->param_count; ++n)
2829 param_types [n + sig->hasthis] = sig->params [n];
2831 /* do this somewhere outside - not here */
2832 NEW_ICONST (cfg, zero_int32, 0);
2833 NEW_ICONST (cfg, zero_int64, 0);
2834 zero_int64->type = STACK_I8;
2835 NEW_PCONST (cfg, zero_ptr, 0);
2836 NEW_PCONST (cfg, zero_obj, 0);
2837 zero_obj->type = STACK_OBJ;
2839 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
2840 zero_r8->type = STACK_R8;
2841 zero_r8->inst_p0 = &r8_0;
2843 /* add a check for this != NULL to inlined methods */
2844 if (is_virtual_call) {
2845 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
2846 NEW_ARGLOAD (cfg, ins->inst_left, 0);
2848 MONO_ADD_INS (bblock, ins);
2851 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
2852 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
2855 start_new_bblock = 0;
2858 if (cfg->method == method)
2859 real_offset = ip - header->code;
2861 real_offset = inline_offset;
2863 if (start_new_bblock) {
2864 bblock->cil_length = ip - bblock->cil_code;
2865 if (start_new_bblock == 2) {
2866 g_assert (ip == tblock->cil_code);
2868 GET_BBLOCK (cfg, bbhash, tblock, ip);
2870 bblock->next_bb = tblock;
2872 start_new_bblock = 0;
2873 for (i = 0; i < bblock->in_scount; ++i) {
2874 if (cfg->verbose_level > 3)
2875 g_print ("loading %d from temp %d\n", i, bblock->in_stack [i]->inst_c0);
2876 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2880 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
2881 link_bblock (cfg, bblock, tblock);
2882 if (sp != stack_start) {
2883 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2886 bblock->next_bb = tblock;
2888 for (i = 0; i < bblock->in_scount; ++i) {
2889 if (cfg->verbose_level > 3)
2890 g_print ("loading %d from temp %d\n", i, bblock->in_stack [i]->inst_c0);
2891 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2897 bblock->real_offset = real_offset;
2899 if ((cfg->method == method) && cfg->coverage_info) {
2900 MonoInst *store, *one;
2901 guint32 cil_offset = ip - header->code;
2902 cfg->coverage_info->data [cil_offset].cil_code = ip;
2904 /* TODO: Use an increment here */
2905 NEW_ICONST (cfg, one, 1);
2908 NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
2911 MONO_INST_NEW (cfg, store, CEE_STIND_I);
2912 store->cil_code = ip;
2913 store->inst_left = ins;
2914 store->inst_right = one;
2916 MONO_ADD_INS (bblock, store);
2919 if (cfg->verbose_level > 3)
2920 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, sp-stack_start, mono_disasm_code_one (NULL, method, ip, NULL));
2927 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2928 ins->cil_code = ip++;
2929 MONO_ADD_INS (bblock, ins);
2935 CHECK_STACK_OVF (1);
2936 n = (*ip)-CEE_LDARG_0;
2938 NEW_ARGLOAD (cfg, ins, n);
2939 ins->cil_code = ip++;
2946 CHECK_STACK_OVF (1);
2947 n = (*ip)-CEE_LDLOC_0;
2949 NEW_LOCLOAD (cfg, ins, n);
2950 ins->cil_code = ip++;
2958 n = (*ip)-CEE_STLOC_0;
2961 handle_loaded_temps (cfg, bblock, stack_start, sp);
2962 NEW_LOCSTORE (cfg, ins, n, *sp);
2964 if (ins->opcode == CEE_STOBJ) {
2965 NEW_LOCLOADA (cfg, ins, n);
2966 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2968 MONO_ADD_INS (bblock, ins);
2974 CHECK_STACK_OVF (1);
2976 NEW_ARGLOAD (cfg, ins, ip [1]);
2983 CHECK_STACK_OVF (1);
2985 NEW_ARGLOADA (cfg, ins, ip [1]);
2995 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
2996 handle_loaded_temps (cfg, bblock, stack_start, sp);
2998 if (ins->opcode == CEE_STOBJ) {
2999 NEW_ARGLOADA (cfg, ins, ip [1]);
3000 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3002 MONO_ADD_INS (bblock, ins);
3007 CHECK_STACK_OVF (1);
3008 CHECK_LOCAL (ip [1]);
3009 NEW_LOCLOAD (cfg, ins, ip [1]);
3016 CHECK_STACK_OVF (1);
3017 CHECK_LOCAL (ip [1]);
3018 NEW_LOCLOADA (cfg, ins, ip [1]);
3027 handle_loaded_temps (cfg, bblock, stack_start, sp);
3028 CHECK_LOCAL (ip [1]);
3029 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
3031 if (ins->opcode == CEE_STOBJ) {
3032 NEW_LOCLOADA (cfg, ins, ip [1]);
3033 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3035 MONO_ADD_INS (bblock, ins);
3040 CHECK_STACK_OVF (1);
3041 NEW_PCONST (cfg, ins, NULL);
3043 ins->type = STACK_OBJ;
3048 CHECK_STACK_OVF (1);
3049 NEW_ICONST (cfg, ins, -1);
3063 CHECK_STACK_OVF (1);
3064 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
3071 CHECK_STACK_OVF (1);
3073 NEW_ICONST (cfg, ins, *((signed char*)ip));
3080 CHECK_STACK_OVF (1);
3081 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
3088 CHECK_STACK_OVF (1);
3089 MONO_INST_NEW (cfg, ins, OP_I8CONST);
3091 ins->type = STACK_I8;
3093 ins->inst_l = (gint64)read64 (ip);
3098 float *f = mono_mempool_alloc (cfg->domain->mp, sizeof (float));
3100 CHECK_STACK_OVF (1);
3101 MONO_INST_NEW (cfg, ins, OP_R4CONST);
3102 ins->type = STACK_R8;
3111 double *d = mono_mempool_alloc (cfg->domain->mp, sizeof (double));
3113 CHECK_STACK_OVF (1);
3114 MONO_INST_NEW (cfg, ins, OP_R8CONST);
3115 ins->type = STACK_R8;
3124 MonoInst *temp, *store;
3126 CHECK_STACK_OVF (1);
3131 * small optimization: if the loaded value was from a local already,
3132 * just load it twice.
3134 if (ins->ssa_op == MONO_SSA_LOAD &&
3135 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
3137 MONO_INST_NEW (cfg, temp, 0);
3139 temp->cil_code = ip;
3142 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3143 temp->cil_code = ip;
3144 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3145 store->cil_code = ip;
3146 MONO_ADD_INS (bblock, store);
3147 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3150 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3160 MONO_INST_NEW (cfg, ins, CEE_POP);
3161 MONO_ADD_INS (bblock, ins);
3162 ins->cil_code = ip++;
3168 if (stack_start != sp)
3170 MONO_INST_NEW (cfg, ins, CEE_JMP);
3171 token = read32 (ip + 1);
3172 /* FIXME: check the signature matches */
3173 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3174 ins->inst_p0 = cmethod;
3175 MONO_ADD_INS (bblock, ins);
3177 start_new_bblock = 1;
3181 case CEE_CALLVIRT: {
3182 MonoInst *addr = NULL;
3183 MonoMethodSignature *fsig = NULL;
3184 int temp, array_rank = 0;
3185 int virtual = *ip == CEE_CALLVIRT;
3188 token = read32 (ip + 1);
3190 if (*ip == CEE_CALLI) {
3195 if (method->wrapper_type != MONO_WRAPPER_NONE)
3196 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
3198 fsig = mono_metadata_parse_signature (image, token);
3200 n = fsig->param_count + fsig->hasthis;
3202 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3203 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
3204 } else if (constrained_call) {
3205 cmethod = mono_get_method_constrained (image, token, constrained_call, generic_context);
3207 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3212 if (!cmethod->klass->inited)
3213 mono_class_init (cmethod->klass);
3215 if (cmethod->signature->pinvoke) {
3216 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
3217 fsig = wrapper->signature;
3219 fsig = mono_method_get_signature (cmethod, image, token);
3222 n = fsig->param_count + fsig->hasthis;
3224 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
3225 cmethod->klass->parent == mono_defaults.array_class) {
3226 array_rank = cmethod->klass->rank;
3229 if (cmethod->string_ctor)
3230 g_assert_not_reached ();
3236 //g_assert (!virtual || fsig->hasthis);
3240 if (constrained_call) {
3242 * We have the `constrained.' prefix opcode.
3244 if (constrained_call->valuetype && !cmethod->klass->valuetype) {
3246 * The type parameter is instantiated as a valuetype,
3247 * but that type doesn't override the method we're
3248 * calling, so we need to box `this'.
3250 sp [0] = handle_box (cfg, bblock, sp [0], ip, constrained_call);
3251 } else if (!constrained_call->valuetype) {
3255 * The type parameter is instantiated as a reference
3256 * type. We have a managed pointer on the stack, so
3257 * we need to dereference it here.
3260 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3262 ins->inst_i0 = sp [0];
3263 ins->type = STACK_OBJ;
3265 } else if (cmethod->klass->valuetype)
3267 constrained_call = NULL;
3270 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
3273 if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL) && (mono_metadata_signature_equal (method->signature, cmethod->signature))) {
3275 /* FIXME: This assumes the two methods has the same number and type of arguments */
3276 for (i = 0; i < n; ++i) {
3277 /* Check if argument is the same */
3278 NEW_ARGLOAD (cfg, ins, i);
3279 if ((ins->opcode == sp [i]->opcode) && (ins->inst_i0 == sp [i]->inst_i0))
3282 /* Prevent argument from being register allocated */
3283 arg_array [i]->flags |= MONO_INST_VOLATILE;
3284 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3286 if (ins->opcode == CEE_STOBJ) {
3287 NEW_ARGLOADA (cfg, ins, i);
3288 handle_stobj (cfg, bblock, ins, sp [i], sp [i]->cil_code, ins->klass, FALSE, FALSE);
3291 MONO_ADD_INS (bblock, ins);
3293 MONO_INST_NEW (cfg, ins, CEE_JMP);
3295 ins->inst_p0 = cmethod;
3296 ins->inst_p1 = arg_array [0];
3297 MONO_ADD_INS (bblock, ins);
3298 start_new_bblock = 1;
3299 /* skip CEE_RET as well */
3304 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_opcode_for_method (cfg, cmethod, fsig, sp))) {
3307 if (MONO_TYPE_IS_VOID (fsig->ret)) {
3308 MONO_ADD_INS (bblock, ins);
3310 type_to_eval_stack_type (fsig->ret, ins);
3319 handle_loaded_temps (cfg, bblock, stack_start, sp);
3321 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3322 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
3323 mono_method_check_inlining (cfg, cmethod) &&
3324 !g_list_find (dont_inline, cmethod)) {
3326 MonoBasicBlock *ebblock;
3328 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
3329 cmethod = mono_marshal_get_native_wrapper (cmethod);
3331 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, FALSE))) {
3335 GET_BBLOCK (cfg, bbhash, bblock, ip);
3336 ebblock->next_bb = bblock;
3337 link_bblock (cfg, ebblock, bblock);
3339 if (!MONO_TYPE_IS_VOID (fsig->ret))
3342 /* indicates start of a new block, and triggers a load of all
3343 stack arguments at bb boundarie */
3346 inline_costs += costs;
3351 inline_costs += 10 * num_calls++;
3353 /* tail recursion elimination */
3354 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == method && ip [5] == CEE_RET) {
3355 gboolean has_vtargs = FALSE;
3358 /* keep it simple */
3359 for (i = fsig->param_count - 1; i >= 0; i--) {
3360 if (MONO_TYPE_ISSTRUCT (cmethod->signature->params [i]))
3365 for (i = 0; i < n; ++i) {
3366 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3368 MONO_ADD_INS (bblock, ins);
3370 MONO_INST_NEW (cfg, ins, CEE_BR);
3372 MONO_ADD_INS (bblock, ins);
3373 tblock = start_bblock->out_bb [0];
3374 link_bblock (cfg, bblock, tblock);
3375 ins->inst_target_bb = tblock;
3376 start_new_bblock = 1;
3379 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3380 /* just create a dummy - the value is never used */
3381 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
3382 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3390 if (*ip == CEE_CALLI) {
3392 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
3393 NEW_TEMPLOAD (cfg, *sp, temp);
3397 } else if (array_rank) {
3400 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
3401 if (sp [fsig->param_count]->type == STACK_OBJ) {
3402 MonoInst *iargs [2];
3403 MonoInst *array, *to_store, *store;
3405 handle_loaded_temps (cfg, bblock, stack_start, sp);
3407 array = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
3408 NEW_TEMPSTORE (cfg, store, array->inst_c0, sp [0]);
3409 store->cil_code = ip;
3410 MONO_ADD_INS (bblock, store);
3411 NEW_TEMPLOAD (cfg, iargs [0], array->inst_c0);
3413 to_store = mono_compile_create_var (cfg, type_from_stack_type (sp [fsig->param_count]), OP_LOCAL);
3414 NEW_TEMPSTORE (cfg, store, to_store->inst_c0, sp [fsig->param_count]);
3415 store->cil_code = ip;
3416 MONO_ADD_INS (bblock, store);
3417 NEW_TEMPLOAD (cfg, iargs [1], to_store->inst_c0);
3420 * We first save the args for the call so that the args are copied to the stack
3421 * and a new instruction tree for them is created. If we don't do this,
3422 * the same MonoInst is added to two different trees and this is not
3425 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref_check, iargs, ip);
3427 NEW_TEMPLOAD (cfg, sp [0], array->inst_c0);
3428 NEW_TEMPLOAD (cfg, sp [fsig->param_count], to_store->inst_c0);
3431 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
3432 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
3434 if (ins->opcode == CEE_STOBJ) {
3435 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
3437 MONO_ADD_INS (bblock, ins);
3440 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
3441 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3442 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
3446 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
3447 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3450 g_assert_not_reached ();
3454 if (0 && CODE_IS_STLOC (ip + 5) && (!MONO_TYPE_ISSTRUCT (fsig->ret)) && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)) {
3455 /* no need to spill */
3456 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
3459 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) {
3460 NEW_TEMPLOAD (cfg, *sp, temp);
3470 if (cfg->method != method) {
3471 /* return from inlined methode */
3476 //g_assert (returnvar != -1);
3477 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
3478 store->cil_code = sp [0]->cil_code;
3479 if (store->opcode == CEE_STOBJ) {
3480 g_assert_not_reached ();
3481 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
3482 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
3484 MONO_ADD_INS (bblock, store);
3488 g_assert (!return_var);
3491 MONO_INST_NEW (cfg, ins, CEE_NOP);
3492 ins->opcode = mono_type_to_stind (method->signature->ret);
3493 if (ins->opcode == CEE_STOBJ) {
3494 NEW_RETLOADA (cfg, ins);
3495 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3497 ins->opcode = OP_SETRET;
3499 ins->inst_i0 = *sp;;
3500 ins->inst_i1 = NULL;
3501 MONO_ADD_INS (bblock, ins);
3505 if (sp != stack_start)
3507 MONO_INST_NEW (cfg, ins, CEE_BR);
3508 ins->cil_code = ip++;
3509 ins->inst_target_bb = end_bblock;
3510 MONO_ADD_INS (bblock, ins);
3511 link_bblock (cfg, bblock, end_bblock);
3512 start_new_bblock = 1;
3516 MONO_INST_NEW (cfg, ins, CEE_BR);
3517 ins->cil_code = ip++;
3518 MONO_ADD_INS (bblock, ins);
3519 target = ip + 1 + (signed char)(*ip);
3521 GET_BBLOCK (cfg, bbhash, tblock, target);
3522 link_bblock (cfg, bblock, tblock);
3523 CHECK_BBLOCK (target, ip, tblock);
3524 ins->inst_target_bb = tblock;
3525 if (sp != stack_start) {
3526 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3529 start_new_bblock = 1;
3536 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3537 ins->cil_code = ip++;
3538 target = ip + 1 + *(signed char*)ip;
3540 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
3541 if (sp != stack_start) {
3542 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3559 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3560 ins->cil_code = ip++;
3561 target = ip + 1 + *(signed char*)ip;
3564 if (sp != stack_start) {
3565 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3572 MONO_INST_NEW (cfg, ins, CEE_BR);
3573 ins->cil_code = ip++;
3574 MONO_ADD_INS (bblock, ins);
3575 target = ip + 4 + (gint32)read32(ip);
3577 GET_BBLOCK (cfg, bbhash, tblock, target);
3578 link_bblock (cfg, bblock, tblock);
3579 CHECK_BBLOCK (target, ip, tblock);
3580 ins->inst_target_bb = tblock;
3581 if (sp != stack_start) {
3582 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3585 start_new_bblock = 1;
3592 MONO_INST_NEW (cfg, ins, *ip);
3593 ins->cil_code = ip++;
3594 target = ip + 4 + (gint32)read32(ip);
3596 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
3597 if (sp != stack_start) {
3598 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3615 MONO_INST_NEW (cfg, ins, *ip);
3616 ins->cil_code = ip++;
3617 target = ip + 4 + (gint32)read32(ip);
3620 if (sp != stack_start) {
3621 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3629 n = read32 (ip + 1);
3630 MONO_INST_NEW (cfg, ins, *ip);
3632 ins->inst_left = *sp;
3633 if (ins->inst_left->type != STACK_I4) goto unverified;
3636 CHECK_OPSIZE (n * sizeof (guint32));
3637 target = ip + n * sizeof (guint32);
3638 MONO_ADD_INS (bblock, ins);
3639 GET_BBLOCK (cfg, bbhash, tblock, target);
3640 link_bblock (cfg, bblock, tblock);
3641 ins->klass = GUINT_TO_POINTER (n);
3642 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
3643 ins->inst_many_bb [n] = tblock;
3645 for (i = 0; i < n; ++i) {
3646 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
3647 link_bblock (cfg, bblock, tblock);
3648 ins->inst_many_bb [i] = tblock;
3651 if (sp != stack_start) {
3652 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3669 MONO_INST_NEW (cfg, ins, *ip);
3674 ins->type = ldind_type [*ip - CEE_LDIND_I1];
3675 ins->flags |= ins_flag;
3687 MONO_INST_NEW (cfg, ins, *ip);
3688 ins->cil_code = ip++;
3690 handle_loaded_temps (cfg, bblock, stack_start, sp);
3691 MONO_ADD_INS (bblock, ins);
3692 ins->inst_i0 = sp [0];
3693 ins->inst_i1 = sp [1];
3694 ins->flags |= ins_flag;
3713 /* special case that gives a nice speedup and happens to workaorund a ppc jit but (for the release)
3714 * later apply the speedup to the left shift as well
3717 if ((ins->opcode == OP_LSHR_UN) && (ins->type == STACK_I8)
3718 && (ins->inst_right->opcode == OP_ICONST) && (ins->inst_right->inst_c0 == 32)) {
3719 ins->opcode = OP_LONG_SHRUN_32;
3720 /*g_print ("applied long shr speedup to %s\n", cfg->method->name);*/
3724 if (mono_find_jit_opcode_emulation (ins->opcode)) {
3726 *sp++ = emit_tree (cfg, bblock, ins);
3740 case CEE_CONV_OVF_I8:
3741 case CEE_CONV_OVF_U8:
3745 if (mono_find_jit_opcode_emulation (ins->opcode)) {
3747 *sp++ = emit_tree (cfg, bblock, ins);
3751 case CEE_CONV_OVF_I4:
3752 case CEE_CONV_OVF_I1:
3753 case CEE_CONV_OVF_I2:
3754 case CEE_CONV_OVF_I:
3755 case CEE_CONV_OVF_U:
3758 if (sp [-1]->type == STACK_R8) {
3759 ADD_UNOP (CEE_CONV_OVF_I8);
3767 case CEE_CONV_OVF_U1:
3768 case CEE_CONV_OVF_U2:
3769 case CEE_CONV_OVF_U4:
3772 if (sp [-1]->type == STACK_R8) {
3773 ADD_UNOP (CEE_CONV_OVF_U8);
3781 case CEE_CONV_OVF_I1_UN:
3782 case CEE_CONV_OVF_I2_UN:
3783 case CEE_CONV_OVF_I4_UN:
3784 case CEE_CONV_OVF_I8_UN:
3785 case CEE_CONV_OVF_U1_UN:
3786 case CEE_CONV_OVF_U2_UN:
3787 case CEE_CONV_OVF_U4_UN:
3788 case CEE_CONV_OVF_U8_UN:
3789 case CEE_CONV_OVF_I_UN:
3790 case CEE_CONV_OVF_U_UN:
3798 token = read32 (ip + 1);
3799 if (method->wrapper_type != MONO_WRAPPER_NONE)
3800 klass = mono_method_get_wrapper_data (method, token);
3802 klass = mono_class_get_full (image, token, generic_context);
3804 mono_class_init (klass);
3806 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3807 MonoInst *store, *load;
3808 MONO_INST_NEW (cfg, load, CEE_LDIND_REF);
3809 load->cil_code = ip;
3810 load->inst_i0 = sp [1];
3811 load->type = ldind_type [CEE_LDIND_REF];
3812 load->flags |= ins_flag;
3813 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
3814 store->cil_code = ip;
3815 handle_loaded_temps (cfg, bblock, stack_start, sp);
3816 MONO_ADD_INS (bblock, store);
3817 store->inst_i0 = sp [0];
3818 store->inst_i1 = load;
3819 store->flags |= ins_flag;
3821 n = mono_class_value_size (klass, NULL);
3822 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3824 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3825 copy->inst_left = sp [0];
3826 copy->inst_right = sp [1];
3827 copy->cil_code = ip;
3829 MONO_ADD_INS (bblock, copy);
3831 MonoInst *iargs [3];
3834 NEW_ICONST (cfg, iargs [2], n);
3835 iargs [2]->cil_code = ip;
3837 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3844 MonoInst *iargs [3];
3848 token = read32 (ip + 1);
3849 if (method->wrapper_type != MONO_WRAPPER_NONE)
3850 klass = mono_method_get_wrapper_data (method, token);
3852 klass = mono_class_get_full (image, token, generic_context);
3854 mono_class_init (klass);
3855 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3856 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3858 ins->inst_i0 = sp [0];
3859 ins->type = ldind_type [CEE_LDIND_REF];
3860 ins->flags |= ins_flag;
3866 n = mono_class_value_size (klass, NULL);
3867 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3868 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3869 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3871 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3872 copy->inst_left = iargs [0];
3873 copy->inst_right = *sp;
3874 copy->cil_code = ip;
3876 MONO_ADD_INS (bblock, copy);
3879 NEW_ICONST (cfg, iargs [2], n);
3880 iargs [2]->cil_code = ip;
3882 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3884 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3892 CHECK_STACK_OVF (1);
3894 n = read32 (ip + 1);
3896 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
3897 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, n));
3899 ins->type = STACK_OBJ;
3902 else if (method->wrapper_type != MONO_WRAPPER_NONE) {
3904 MonoInst *iargs [1];
3906 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
3907 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
3908 NEW_TEMPLOAD (cfg, *sp, temp);
3912 if (cfg->opt & MONO_OPT_SHARED) {
3914 MonoInst *iargs [3];
3916 if (mono_compile_aot) {
3917 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n);
3920 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3921 NEW_IMAGECONST (cfg, iargs [1], image);
3922 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
3923 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
3924 NEW_TEMPLOAD (cfg, *sp, temp);
3925 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3927 if (mono_compile_aot)
3928 NEW_LDSTRCONST (cfg, ins, image, n);
3930 NEW_PCONST (cfg, ins, NULL);
3932 ins->type = STACK_OBJ;
3933 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3943 MonoInst *iargs [2];
3944 MonoMethodSignature *fsig;
3948 token = read32 (ip + 1);
3949 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3950 cmethod = mono_method_get_wrapper_data (method, token);
3952 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3953 fsig = mono_method_get_signature (cmethod, image, token);
3955 mono_class_init (cmethod->klass);
3957 n = fsig->param_count;
3960 /* move the args to allow room for 'this' in the first position */
3966 handle_loaded_temps (cfg, bblock, stack_start, sp);
3969 if (cmethod->klass->parent == mono_defaults.array_class) {
3970 NEW_METHODCONST (cfg, *sp, cmethod);
3971 temp = mono_emit_native_call (cfg, bblock, mono_array_new_va, fsig, sp, ip, FALSE);
3972 cfg->flags |= MONO_CFG_HAS_VARARGS;
3974 } else if (cmethod->string_ctor) {
3975 /* we simply pass a null pointer */
3976 NEW_PCONST (cfg, *sp, NULL);
3977 /* now call the string ctor */
3978 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
3980 if (cmethod->klass->valuetype) {
3981 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
3982 temp = iargs [0]->inst_c0;
3983 NEW_TEMPLOADA (cfg, *sp, temp);
3985 if (cfg->opt & MONO_OPT_SHARED) {
3986 NEW_DOMAINCONST (cfg, iargs [0]);
3987 NEW_CLASSCONST (cfg, iargs [1], cmethod->klass);
3989 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3991 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
3992 NEW_VTABLECONST (cfg, iargs [0], vtable);
3993 if (cmethod->klass->has_finalize || cmethod->klass->marshalbyref || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
3994 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
3996 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
3998 NEW_TEMPLOAD (cfg, *sp, temp);
4001 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
4002 mono_method_check_inlining (cfg, cmethod) &&
4003 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
4004 !g_list_find (dont_inline, cmethod)) {
4006 MonoBasicBlock *ebblock;
4007 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, FALSE))) {
4012 GET_BBLOCK (cfg, bbhash, bblock, ip);
4013 ebblock->next_bb = bblock;
4014 link_bblock (cfg, ebblock, bblock);
4016 NEW_TEMPLOAD (cfg, *sp, temp);
4019 /* indicates start of a new block, and triggers a load
4020 of all stack arguments at bb boundarie */
4023 inline_costs += costs;
4027 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
4030 /* now call the actual ctor */
4031 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
4035 NEW_TEMPLOAD (cfg, *sp, temp);
4046 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4047 mono_class_init (klass);
4049 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4051 MonoMethod *mono_isinst;
4052 MonoInst *iargs [1];
4053 MonoBasicBlock *ebblock;
4057 mono_isinst = mono_marshal_get_isinst (klass);
4060 costs = inline_method (cfg, mono_isinst, mono_isinst->signature, bblock,
4061 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4063 g_assert (costs > 0);
4068 GET_BBLOCK (cfg, bbhash, bblock, ip);
4069 ebblock->next_bb = bblock;
4070 link_bblock (cfg, ebblock, bblock);
4072 temp = iargs [0]->inst_i0->inst_c0;
4073 NEW_TEMPLOAD (cfg, *sp, temp);
4077 inline_costs += costs;
4081 MONO_INST_NEW (cfg, ins, *ip);
4082 ins->type = STACK_OBJ;
4083 ins->inst_left = *sp;
4084 ins->inst_newa_class = klass;
4086 *sp++ = emit_tree (cfg, bblock, ins);
4090 case CEE_UNBOX_ANY: {
4091 MonoInst *add, *vtoffset;
4092 MonoInst *iargs [3];
4097 token = read32 (ip + 1);
4098 if (method->wrapper_type != MONO_WRAPPER_NONE)
4099 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4101 klass = mono_class_get_full (image, token, generic_context);
4102 mono_class_init (klass);
4104 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4106 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4107 MonoMethod *mono_castclass;
4108 MonoInst *iargs [1];
4109 MonoBasicBlock *ebblock;
4113 mono_castclass = mono_marshal_get_castclass (klass);
4116 costs = inline_method (cfg, mono_castclass, mono_castclass->signature, bblock,
4117 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4119 g_assert (costs > 0);
4124 GET_BBLOCK (cfg, bbhash, bblock, ip);
4125 ebblock->next_bb = bblock;
4126 link_bblock (cfg, ebblock, bblock);
4128 temp = iargs [0]->inst_i0->inst_c0;
4129 NEW_TEMPLOAD (cfg, *sp, temp);
4133 inline_costs += costs;
4136 MONO_INST_NEW (cfg, ins, CEE_CASTCLASS);
4137 ins->type = STACK_OBJ;
4138 ins->inst_left = *sp;
4140 ins->inst_newa_class = klass;
4148 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
4149 ins->type = STACK_OBJ;
4150 ins->inst_left = *sp;
4152 ins->inst_newa_class = klass;
4155 MONO_INST_NEW (cfg, add, CEE_ADD);
4156 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4157 add->inst_left = ins;
4158 add->inst_right = vtoffset;
4159 add->type = STACK_MP;
4163 n = mono_class_value_size (klass, NULL);
4164 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
4165 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
4166 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
4168 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
4169 copy->inst_left = iargs [0];
4170 copy->inst_right = *sp;
4171 copy->cil_code = ip;
4173 MONO_ADD_INS (bblock, copy);
4176 NEW_ICONST (cfg, iargs [2], n);
4177 iargs [2]->cil_code = ip;
4179 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
4181 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
4187 MonoInst *add, *vtoffset;
4192 token = read32 (ip + 1);
4193 if (method->wrapper_type != MONO_WRAPPER_NONE)
4194 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4196 klass = mono_class_get_full (image, token, generic_context);
4197 mono_class_init (klass);
4199 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
4200 ins->type = STACK_OBJ;
4201 ins->inst_left = *sp;
4203 ins->inst_newa_class = klass;
4206 MONO_INST_NEW (cfg, add, CEE_ADD);
4207 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4208 add->inst_left = ins;
4209 add->inst_right = vtoffset;
4210 add->type = STACK_MP;
4220 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4221 mono_class_init (klass);
4223 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4225 MonoMethod *mono_castclass;
4226 MonoInst *iargs [1];
4227 MonoBasicBlock *ebblock;
4231 mono_castclass = mono_marshal_get_castclass (klass);
4234 costs = inline_method (cfg, mono_castclass, mono_castclass->signature, bblock,
4235 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4237 g_assert (costs > 0);
4242 GET_BBLOCK (cfg, bbhash, bblock, ip);
4243 ebblock->next_bb = bblock;
4244 link_bblock (cfg, ebblock, bblock);
4246 temp = iargs [0]->inst_i0->inst_c0;
4247 NEW_TEMPLOAD (cfg, *sp, temp);
4251 inline_costs += costs;
4254 MONO_INST_NEW (cfg, ins, *ip);
4255 ins->type = STACK_OBJ;
4256 ins->inst_left = *sp;
4258 ins->inst_newa_class = klass;
4260 *sp++ = emit_tree (cfg, bblock, ins);
4266 MONO_INST_NEW (cfg, ins, *ip);
4268 ins->inst_left = *sp;
4269 ins->cil_code = ip++;
4270 MONO_ADD_INS (bblock, ins);
4272 start_new_bblock = 1;
4277 MonoInst *offset_ins;
4278 MonoClassField *field;
4279 MonoBasicBlock *ebblock;
4283 if (*ip == CEE_STFLD) {
4290 // FIXME: enable this test later.
4291 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
4294 token = read32 (ip + 1);
4295 field = mono_field_from_token (image, token, &klass, generic_context);
4296 mono_class_init (klass);
4298 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
4299 /* FIXME: mark instructions for use in SSA */
4300 if (*ip == CEE_STFLD) {
4301 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound) {
4302 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
4303 MonoInst *iargs [5];
4306 NEW_CLASSCONST (cfg, iargs [1], klass);
4307 NEW_FIELDCONST (cfg, iargs [2], field);
4308 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
4312 if (cfg->opt & MONO_OPT_INLINE) {
4313 costs = inline_method (cfg, stfld_wrapper, stfld_wrapper->signature, bblock,
4314 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4315 g_assert (costs > 0);
4320 GET_BBLOCK (cfg, bbhash, bblock, ip);
4321 ebblock->next_bb = bblock;
4322 link_bblock (cfg, ebblock, bblock);
4324 /* indicates start of a new block, and triggers a load
4325 of all stack arguments at bb boundarie */
4328 inline_costs += costs;
4331 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, stfld_wrapper->signature, iargs, ip, NULL);
4335 NEW_ICONST (cfg, offset_ins, foffset);
4336 MONO_INST_NEW (cfg, ins, CEE_ADD);
4338 ins->inst_left = *sp;
4339 ins->inst_right = offset_ins;
4340 ins->type = STACK_MP;
4342 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4343 store->cil_code = ip;
4344 store->inst_left = ins;
4345 store->inst_right = sp [1];
4346 handle_loaded_temps (cfg, bblock, stack_start, sp);
4347 store->flags |= ins_flag;
4349 if (store->opcode == CEE_STOBJ) {
4350 handle_stobj (cfg, bblock, ins, sp [1], ip,
4351 mono_class_from_mono_type (field->type), FALSE, FALSE);
4353 MONO_ADD_INS (bblock, store);
4356 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound) {
4357 /* fixme: we need to inline that call somehow */
4358 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
4359 MonoInst *iargs [4];
4363 NEW_CLASSCONST (cfg, iargs [1], klass);
4364 NEW_FIELDCONST (cfg, iargs [2], field);
4365 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
4366 if (cfg->opt & MONO_OPT_INLINE) {
4367 costs = inline_method (cfg, ldfld_wrapper, ldfld_wrapper->signature, bblock,
4368 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4369 g_assert (costs > 0);
4374 GET_BBLOCK (cfg, bbhash, bblock, ip);
4375 ebblock->next_bb = bblock;
4376 link_bblock (cfg, ebblock, bblock);
4378 temp = iargs [0]->inst_i0->inst_c0;
4380 if (*ip == CEE_LDFLDA) {
4381 /* not sure howto handle this */
4382 NEW_TEMPLOADA (cfg, *sp, temp);
4384 NEW_TEMPLOAD (cfg, *sp, temp);
4388 /* indicates start of a new block, and triggers a load of
4389 all stack arguments at bb boundarie */
4392 inline_costs += costs;
4395 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, ldfld_wrapper->signature, iargs, ip, NULL);
4396 if (*ip == CEE_LDFLDA) {
4397 /* not sure howto handle this */
4398 NEW_TEMPLOADA (cfg, *sp, temp);
4400 NEW_TEMPLOAD (cfg, *sp, temp);
4405 NEW_ICONST (cfg, offset_ins, foffset);
4406 MONO_INST_NEW (cfg, ins, CEE_ADD);
4408 ins->inst_left = *sp;
4409 ins->inst_right = offset_ins;
4410 ins->type = STACK_MP;
4412 if (*ip == CEE_LDFLDA) {
4416 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4417 type_to_eval_stack_type (field->type, load);
4418 load->cil_code = ip;
4419 load->inst_left = ins;
4420 load->flags |= ins_flag;
4432 MonoClassField *field;
4433 gpointer addr = NULL;
4436 token = read32 (ip + 1);
4438 field = mono_field_from_token (image, token, &klass, generic_context);
4439 mono_class_init (klass);
4441 if ((*ip) == CEE_STSFLD)
4442 handle_loaded_temps (cfg, bblock, stack_start, sp);
4444 if (cfg->domain->special_static_fields)
4445 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
4447 if ((cfg->opt & MONO_OPT_SHARED) || (mono_compile_aot && addr)) {
4449 MonoInst *iargs [2];
4450 g_assert (field->parent);
4451 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
4452 NEW_FIELDCONST (cfg, iargs [1], field);
4453 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
4454 NEW_TEMPLOAD (cfg, ins, temp);
4457 vtable = mono_class_vtable (cfg->domain, klass);
4459 if ((!vtable->initialized || mono_compile_aot) && !(klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (klass, method)) {
4460 guint8 *tramp = mono_create_class_init_trampoline (vtable);
4461 mono_emit_native_call (cfg, bblock, tramp,
4462 helper_sig_class_init_trampoline,
4464 if (cfg->verbose_level > 2)
4465 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
4467 if (cfg->run_cctors)
4468 mono_runtime_class_init (vtable);
4470 addr = (char*)vtable->data + field->offset;
4472 if (mono_compile_aot)
4473 NEW_SFLDACONST (cfg, ins, field);
4475 NEW_PCONST (cfg, ins, addr);
4479 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
4480 * This could be later optimized to do just a couple of
4481 * memory dereferences with constant offsets.
4484 MonoInst *iargs [1];
4485 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
4486 temp = mono_emit_jit_icall (cfg, bblock, mono_get_special_static_data, iargs, ip);
4487 NEW_TEMPLOAD (cfg, ins, temp);
4491 /* FIXME: mark instructions for use in SSA */
4492 if (*ip == CEE_LDSFLDA) {
4494 } else if (*ip == CEE_STSFLD) {
4498 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4499 store->cil_code = ip;
4500 store->inst_left = ins;
4501 store->inst_right = sp [0];
4502 store->flags |= ins_flag;
4505 if (store->opcode == CEE_STOBJ) {
4506 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
4508 MONO_ADD_INS (bblock, store);
4510 gboolean is_const = FALSE;
4511 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4512 if (!((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) &&
4513 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
4514 gpointer addr = (char*)vtable->data + field->offset;
4515 /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
4517 switch (field->type->type) {
4518 case MONO_TYPE_BOOLEAN:
4520 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
4524 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
4527 case MONO_TYPE_CHAR:
4529 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
4533 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
4538 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
4542 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
4547 case MONO_TYPE_STRING:
4548 case MONO_TYPE_OBJECT:
4549 case MONO_TYPE_CLASS:
4550 case MONO_TYPE_SZARRAY:
4552 case MONO_TYPE_FNPTR:
4553 case MONO_TYPE_ARRAY:
4554 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
4555 type_to_eval_stack_type (field->type, *sp);
4560 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
4561 sp [0]->type = STACK_I8;
4562 sp [0]->inst_l = *((gint64 *)addr);
4567 case MONO_TYPE_VALUETYPE:
4576 CHECK_STACK_OVF (1);
4577 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4578 type_to_eval_stack_type (field->type, load);
4579 load->cil_code = ip;
4580 load->inst_left = ins;
4582 load->flags |= ins_flag;
4584 /* fixme: dont see the problem why this does not work */
4585 //cfg->disable_aot = TRUE;
4595 token = read32 (ip + 1);
4596 if (method->wrapper_type != MONO_WRAPPER_NONE)
4597 klass = mono_method_get_wrapper_data (method, token);
4599 klass = mono_class_get_full (image, token, generic_context);
4600 mono_class_init (klass);
4601 n = mono_type_to_stind (&klass->byval_arg);
4602 if (n == CEE_STOBJ) {
4603 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
4605 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
4607 MONO_INST_NEW (cfg, store, n);
4608 store->cil_code = ip;
4609 store->inst_left = sp [0];
4610 store->inst_right = sp [1];
4611 store->flags |= ins_flag;
4612 MONO_ADD_INS (bblock, store);
4624 token = read32 (ip + 1);
4625 if (method->wrapper_type != MONO_WRAPPER_NONE)
4626 klass = mono_method_get_wrapper_data (method, token);
4628 klass = mono_class_get_full (image, token, generic_context);
4629 mono_class_init (klass);
4631 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4636 *sp++ = handle_box (cfg, bblock, val, ip, klass);
4643 MONO_INST_NEW (cfg, ins, *ip);
4648 token = read32 (ip + 1);
4650 /* allocate the domainvar - becaus this is used in decompose_foreach */
4651 if (cfg->opt & MONO_OPT_SHARED)
4652 mono_get_domainvar (cfg);
4654 if (method->wrapper_type != MONO_WRAPPER_NONE)
4655 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4657 klass = mono_class_get_full (image, token, generic_context);
4659 mono_class_init (klass);
4660 ins->inst_newa_class = klass;
4661 ins->inst_newa_len = *sp;
4662 ins->type = STACK_OBJ;
4666 * we store the object so calls to create the array are not interleaved
4667 * with the arguments of other calls.
4670 MonoInst *store, *temp, *load;
4672 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
4673 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4674 store->cil_code = ins->cil_code;
4675 MONO_ADD_INS (bblock, store);
4676 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
4677 load->cil_code = ins->cil_code;
4684 MONO_INST_NEW (cfg, ins, *ip);
4685 ins->cil_code = ip++;
4687 ins->inst_left = *sp;
4688 ins->type = STACK_PTR;
4696 if (method->wrapper_type != MONO_WRAPPER_NONE)
4697 klass = (MonoClass*)mono_method_get_wrapper_data (method, read32 (ip + 1));
4699 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4700 mono_class_init (klass);
4701 NEW_LDELEMA (cfg, ins, sp, klass);
4711 token = read32 (ip + 1);
4712 klass = mono_class_get_full (image, token, generic_context);
4713 mono_class_init (klass);
4714 NEW_LDELEMA (cfg, load, sp, klass);
4715 load->cil_code = ip;
4716 MONO_INST_NEW (cfg, ins, mono_type_to_ldind (&klass->byval_arg));
4718 ins->inst_left = load;
4720 type_to_eval_stack_type (&klass->byval_arg, ins);
4734 case CEE_LDELEM_REF: {
4738 * ldind.x (ldelema (array, index))
4739 * ldelema does the bounds check
4743 klass = array_access_to_klass (*ip);
4744 NEW_LDELEMA (cfg, load, sp, klass);
4745 load->cil_code = ip;
4746 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
4748 ins->inst_left = load;
4750 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
4760 case CEE_STELEM_R8: {
4764 * stind.x (ldelema (array, index), val)
4765 * ldelema does the bounds check
4769 klass = array_access_to_klass (*ip);
4770 NEW_LDELEMA (cfg, load, sp, klass);
4771 load->cil_code = ip;
4772 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
4774 ins->inst_left = load;
4775 ins->inst_right = sp [2];
4777 handle_loaded_temps (cfg, bblock, stack_start, sp);
4778 MONO_ADD_INS (bblock, ins);
4786 * stind.x (ldelema (array, index), val)
4787 * ldelema does the bounds check
4792 token = read32 (ip + 1);
4793 klass = mono_class_get_full (image, token, generic_context);
4794 mono_class_init (klass);
4795 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4796 MonoInst *iargs [3];
4797 handle_loaded_temps (cfg, bblock, stack_start, sp);
4803 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4805 NEW_LDELEMA (cfg, load, sp, klass);
4806 load->cil_code = ip;
4808 n = mono_type_to_stind (&klass->byval_arg);
4810 handle_stobj (cfg, bblock, load, sp [2], ip, klass, FALSE, FALSE);
4812 MONO_INST_NEW (cfg, ins, n);
4814 ins->inst_left = load;
4815 ins->inst_right = sp [2];
4816 handle_loaded_temps (cfg, bblock, stack_start, sp);
4817 MONO_ADD_INS (bblock, ins);
4824 case CEE_STELEM_REF: {
4825 MonoInst *iargs [3];
4830 handle_loaded_temps (cfg, bblock, stack_start, sp);
4836 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4840 NEW_GROUP (cfg, group, sp [0], sp [1]);
4841 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
4843 ins->inst_left = group;
4844 ins->inst_right = sp [2];
4845 MONO_ADD_INS (bblock, ins);
4852 case CEE_CKFINITE: {
4853 MonoInst *store, *temp;
4856 /* this instr. can throw exceptions as side effect,
4857 * so we cant eliminate dead code which contains CKFINITE opdodes.
4858 * Spilling to memory makes sure that we always perform
4862 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
4864 ins->inst_left = sp [-1];
4865 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
4867 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4868 store->cil_code = ip;
4869 MONO_ADD_INS (bblock, store);
4871 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
4878 MONO_INST_NEW (cfg, ins, *ip);
4881 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4882 mono_class_init (klass);
4883 ins->type = STACK_MP;
4884 ins->inst_left = *sp;
4886 ins->inst_newa_class = klass;
4891 case CEE_MKREFANY: {
4892 MonoInst *loc, *klassconst;
4895 MONO_INST_NEW (cfg, ins, *ip);
4898 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4899 mono_class_init (klass);
4902 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
4903 NEW_TEMPLOADA (cfg, ins->inst_right, loc->inst_c0);
4905 NEW_PCONST (cfg, klassconst, klass);
4906 NEW_GROUP (cfg, ins->inst_left, *sp, klassconst);
4908 MONO_ADD_INS (bblock, ins);
4910 NEW_TEMPLOAD (cfg, *sp, loc->inst_c0);
4917 MonoClass *handle_class;
4919 CHECK_STACK_OVF (1);
4922 n = read32 (ip + 1);
4924 handle = mono_ldtoken (image, n, &handle_class, generic_context);
4925 mono_class_init (handle_class);
4927 if (cfg->opt & MONO_OPT_SHARED) {
4929 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
4931 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
4933 NEW_IMAGECONST (cfg, iargs [0], image);
4934 NEW_ICONST (cfg, iargs [1], n);
4935 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
4936 NEW_TEMPLOAD (cfg, res, temp);
4937 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4938 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
4939 MONO_ADD_INS (bblock, store);
4940 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4942 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method_full (image, read32 (ip + 6), NULL, generic_context)) &&
4943 (cmethod->klass == mono_defaults.monotype_class->parent) &&
4944 (strcmp (cmethod->name, "GetTypeFromHandle") == 0) &&
4945 ((g_hash_table_lookup (bbhash, ip + 5) == NULL) ||
4946 (g_hash_table_lookup (bbhash, ip + 5) == bblock))) {
4947 MonoClass *tclass = mono_class_from_mono_type (handle);
4948 mono_class_init (tclass);
4949 if (mono_compile_aot)
4950 NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n);
4952 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
4953 ins->type = STACK_OBJ;
4954 ins->klass = cmethod->klass;
4957 MonoInst *store, *addr, *vtvar;
4959 if (mono_compile_aot)
4960 NEW_LDTOKENCONST (cfg, ins, image, n);
4962 NEW_PCONST (cfg, ins, handle);
4963 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
4964 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4965 NEW_INDSTORE (cfg, store, addr, ins, &mono_defaults.int_class->byval_arg);
4966 MONO_ADD_INS (bblock, store);
4967 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4983 case CEE_ADD_OVF_UN:
4985 case CEE_MUL_OVF_UN:
4987 case CEE_SUB_OVF_UN:
4990 if (mono_find_jit_opcode_emulation (ins->opcode)) {
4992 *sp++ = emit_tree (cfg, bblock, ins);
4996 case CEE_ENDFINALLY:
4997 MONO_INST_NEW (cfg, ins, *ip);
4998 MONO_ADD_INS (bblock, ins);
4999 ins->cil_code = ip++;
5000 start_new_bblock = 1;
5003 * Control will leave the method so empty the stack, otherwise
5004 * the next basic block will start with a nonempty stack.
5006 while (sp != stack_start) {
5007 MONO_INST_NEW (cfg, ins, CEE_POP);
5011 MONO_ADD_INS (bblock, ins);
5017 if (*ip == CEE_LEAVE) {
5019 target = ip + 5 + (gint32)read32(ip + 1);
5022 target = ip + 2 + (signed char)(ip [1]);
5025 /* empty the stack */
5026 while (sp != stack_start) {
5027 MONO_INST_NEW (cfg, ins, CEE_POP);
5031 MONO_ADD_INS (bblock, ins);
5035 * If this leave statement is in a catch block, check for a
5036 * pending exception, and rethrow it if necessary.
5038 for (i = 0; i < header->num_clauses; ++i) {
5039 MonoExceptionClause *clause = &header->clauses [i];
5040 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code)) {
5043 temp = mono_emit_jit_icall (cfg, bblock, mono_thread_get_pending_exception, NULL, ip);
5044 NEW_TEMPLOAD (cfg, *sp, temp);
5046 MONO_INST_NEW (cfg, ins, OP_THROW_OR_NULL);
5047 ins->inst_left = *sp;
5049 MONO_ADD_INS (bblock, ins);
5053 /* fixme: call fault handler ? */
5055 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
5057 for (tmp = handlers; tmp; tmp = tmp->next) {
5059 link_bblock (cfg, bblock, tblock);
5060 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
5062 ins->inst_target_bb = tblock;
5063 MONO_ADD_INS (bblock, ins);
5065 g_list_free (handlers);
5068 MONO_INST_NEW (cfg, ins, CEE_BR);
5070 MONO_ADD_INS (bblock, ins);
5071 GET_BBLOCK (cfg, bbhash, tblock, target);
5072 link_bblock (cfg, bblock, tblock);
5073 CHECK_BBLOCK (target, ip, tblock);
5074 ins->inst_target_bb = tblock;
5075 start_new_bblock = 1;
5077 if (*ip == CEE_LEAVE)
5086 MONO_INST_NEW (cfg, ins, *ip);
5088 handle_loaded_temps (cfg, bblock, stack_start, sp);
5089 MONO_ADD_INS (bblock, ins);
5090 ins->cil_code = ip++;
5091 ins->inst_i0 = sp [0];
5092 ins->inst_i1 = sp [1];
5100 /* trampoline mono specific opcodes */
5101 case MONO_CUSTOM_PREFIX: {
5103 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
5108 case CEE_MONO_FUNC1: {
5110 gpointer func = NULL;
5116 case MONO_MARSHAL_CONV_STR_LPWSTR:
5117 func = mono_string_to_utf16;
5119 case MONO_MARSHAL_CONV_LPWSTR_STR:
5120 func = mono_string_from_utf16;
5122 case MONO_MARSHAL_CONV_LPSTR_STR:
5123 func = mono_string_new_wrapper;
5125 case MONO_MARSHAL_CONV_STR_LPTSTR:
5126 case MONO_MARSHAL_CONV_STR_LPSTR:
5127 func = mono_string_to_utf8;
5129 case MONO_MARSHAL_CONV_STR_BSTR:
5130 func = mono_string_to_bstr;
5132 case MONO_MARSHAL_CONV_STR_TBSTR:
5133 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
5134 func = mono_string_to_ansibstr;
5136 case MONO_MARSHAL_CONV_SB_LPSTR:
5137 case MONO_MARSHAL_CONV_SB_LPTSTR:
5138 func = mono_string_builder_to_utf8;
5140 case MONO_MARSHAL_CONV_SB_LPWSTR:
5141 func = mono_string_builder_to_utf16;
5143 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
5144 func = mono_array_to_savearray;
5146 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
5147 func = mono_array_to_lparray;
5149 case MONO_MARSHAL_CONV_DEL_FTN:
5150 func = mono_delegate_to_ftnptr;
5152 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
5153 func = mono_marshal_string_array;
5155 case MONO_MARSHAL_CONV_STRARRAY_STRWLPARRAY:
5156 func = mono_marshal_string_array_to_unicode;
5159 g_warning ("unknown conversion %d\n", ip [2]);
5160 g_assert_not_reached ();
5163 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5164 NEW_TEMPLOAD (cfg, *sp, temp);
5168 inline_costs += 10 * num_calls++;
5172 case CEE_MONO_FUNC2: {
5174 gpointer func = NULL;
5180 case MONO_MARSHAL_CONV_ASANY:
5181 func = mono_marshal_asany;
5183 case MONO_MARSHAL_CONV_FTN_DEL:
5184 func = mono_ftnptr_to_delegate;
5187 g_warning ("unknown conversion %d\n", ip [2]);
5188 g_assert_not_reached ();
5190 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5191 NEW_TEMPLOAD (cfg, *sp, temp);
5195 inline_costs += 10 * num_calls++;
5199 case CEE_MONO_PROC2: {
5200 gpointer func = NULL;
5206 case MONO_MARSHAL_CONV_LPSTR_SB:
5207 case MONO_MARSHAL_CONV_LPTSTR_SB:
5208 func = mono_string_utf8_to_builder;
5210 case MONO_MARSHAL_CONV_LPWSTR_SB:
5211 func = mono_string_utf16_to_builder;
5213 case MONO_MARSHAL_FREE_ARRAY:
5214 func = mono_marshal_free_array;
5216 case MONO_MARSHAL_CONV_ASANY:
5217 func = mono_marshal_asany;
5220 g_assert_not_reached ();
5223 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5225 inline_costs += 10 * num_calls++;
5228 case CEE_MONO_PROC3: {
5229 gpointer func = NULL;
5235 case MONO_MARSHAL_CONV_STR_BYVALSTR:
5236 func = mono_string_to_byvalstr;
5238 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
5239 func = mono_string_to_byvalwstr;
5241 case MONO_MARSHAL_FREE_ASANY:
5242 func = mono_marshal_free_asany;
5245 g_assert_not_reached ();
5248 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5250 inline_costs += 10 * num_calls++;
5256 mono_emit_jit_icall (cfg, bblock, g_free, sp, ip);
5258 inline_costs += 10 * num_calls++;
5260 case CEE_MONO_LDPTR:
5261 CHECK_STACK_OVF (1);
5263 token = read32 (ip + 2);
5264 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
5268 inline_costs += 10 * num_calls++;
5270 case CEE_MONO_VTADDR:
5273 MONO_INST_NEW (cfg, ins, OP_VTADDR);
5275 ins->type = STACK_MP;
5276 ins->inst_left = *sp;
5280 case CEE_MONO_NEWOBJ: {
5281 MonoInst *iargs [2];
5283 CHECK_STACK_OVF (1);
5285 token = read32 (ip + 2);
5286 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5287 mono_class_init (klass);
5288 NEW_DOMAINCONST (cfg, iargs [0]);
5289 NEW_CLASSCONST (cfg, iargs [1], klass);
5290 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
5291 NEW_TEMPLOAD (cfg, *sp, temp);
5294 inline_costs += 10 * num_calls++;
5297 case CEE_MONO_OBJADDR:
5300 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
5302 ins->type = STACK_MP;
5303 ins->inst_left = *sp;
5307 case CEE_MONO_LDNATIVEOBJ:
5310 token = read32 (ip + 2);
5311 klass = mono_method_get_wrapper_data (method, token);
5312 g_assert (klass->valuetype);
5313 mono_class_init (klass);
5314 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
5318 case CEE_MONO_RETOBJ:
5319 g_assert (cfg->ret);
5320 g_assert (method->signature->pinvoke);
5325 token = read32 (ip + 2);
5326 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5328 NEW_RETLOADA (cfg, ins);
5329 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
5331 if (sp != stack_start)
5334 MONO_INST_NEW (cfg, ins, CEE_BR);
5336 ins->inst_target_bb = end_bblock;
5337 MONO_ADD_INS (bblock, ins);
5338 link_bblock (cfg, bblock, end_bblock);
5339 start_new_bblock = 1;
5342 case CEE_MONO_CISINST:
5343 case CEE_MONO_CCASTCLASS: {
5348 token = read32 (ip + 2);
5349 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5350 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_CISINST) ? OP_CISINST : OP_CCASTCLASS);
5351 ins->type = STACK_I4;
5352 ins->inst_left = *sp;
5353 ins->inst_newa_class = klass;
5355 *sp++ = emit_tree (cfg, bblock, ins);
5359 case CEE_MONO_SAVE_LMF:
5360 case CEE_MONO_RESTORE_LMF:
5361 #ifdef MONO_ARCH_HAVE_LMF_OPS
5362 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_SAVE_LMF) ? OP_SAVE_LMF : OP_RESTORE_LMF);
5363 MONO_ADD_INS (bblock, ins);
5364 cfg->need_lmf_area = TRUE;
5369 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
5378 /* somewhat similar to LDTOKEN */
5379 MonoInst *addr, *vtvar;
5380 CHECK_STACK_OVF (1);
5381 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
5383 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5384 addr->cil_code = ip;
5385 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
5387 ins->inst_left = addr;
5388 MONO_ADD_INS (bblock, ins);
5389 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5403 * The following transforms:
5404 * CEE_CEQ into OP_CEQ
5405 * CEE_CGT into OP_CGT
5406 * CEE_CGT_UN into OP_CGT_UN
5407 * CEE_CLT into OP_CLT
5408 * CEE_CLT_UN into OP_CLT_UN
5410 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
5412 MONO_INST_NEW (cfg, ins, cmp->opcode);
5414 cmp->inst_i0 = sp [0];
5415 cmp->inst_i1 = sp [1];
5419 cmp->opcode = OP_COMPARE;
5421 ins->type = STACK_I4;
5424 /* spill it to reduce the expression complexity
5425 * and workaround bug 54209
5427 if (cmp->inst_left->type == STACK_I8) {
5429 *sp++ = emit_tree (cfg, bblock, ins);
5438 CHECK_STACK_OVF (1);
5440 n = read32 (ip + 2);
5441 if (method->wrapper_type != MONO_WRAPPER_NONE)
5442 cmethod = mono_method_get_wrapper_data (method, n);
5444 cmethod = mono_get_method_full (image, n, NULL, generic_context);
5447 mono_class_init (cmethod->klass);
5448 handle_loaded_temps (cfg, bblock, stack_start, sp);
5450 NEW_METHODCONST (cfg, argconst, cmethod);
5451 if (method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED)
5452 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
5454 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn_nosync, &argconst, ip);
5455 NEW_TEMPLOAD (cfg, *sp, temp);
5459 inline_costs += 10 * num_calls++;
5462 case CEE_LDVIRTFTN: {
5468 n = read32 (ip + 2);
5469 if (method->wrapper_type != MONO_WRAPPER_NONE)
5470 cmethod = mono_method_get_wrapper_data (method, n);
5472 cmethod = mono_get_method_full (image, n, NULL, generic_context);
5474 mono_class_init (cmethod->klass);
5475 handle_loaded_temps (cfg, bblock, stack_start, sp);
5479 NEW_METHODCONST (cfg, args [1], cmethod);
5480 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
5481 NEW_TEMPLOAD (cfg, *sp, temp);
5485 inline_costs += 10 * num_calls++;
5489 CHECK_STACK_OVF (1);
5491 n = read16 (ip + 2);
5493 NEW_ARGLOAD (cfg, ins, n);
5499 CHECK_STACK_OVF (1);
5501 n = read16 (ip + 2);
5503 NEW_ARGLOADA (cfg, ins, n);
5511 handle_loaded_temps (cfg, bblock, stack_start, sp);
5513 n = read16 (ip + 2);
5515 NEW_ARGSTORE (cfg, ins, n, *sp);
5517 if (ins->opcode == CEE_STOBJ) {
5518 NEW_ARGLOADA (cfg, ins, n);
5519 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5521 MONO_ADD_INS (bblock, ins);
5525 CHECK_STACK_OVF (1);
5527 n = read16 (ip + 2);
5529 NEW_LOCLOAD (cfg, ins, n);
5535 CHECK_STACK_OVF (1);
5537 n = read16 (ip + 2);
5539 NEW_LOCLOADA (cfg, ins, n);
5548 n = read16 (ip + 2);
5550 handle_loaded_temps (cfg, bblock, stack_start, sp);
5551 NEW_LOCSTORE (cfg, ins, n, *sp);
5553 if (ins->opcode == CEE_STOBJ) {
5554 NEW_LOCLOADA (cfg, ins, n);
5555 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5557 MONO_ADD_INS (bblock, ins);
5564 if (sp != stack_start)
5566 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
5567 ins->inst_left = *sp;
5570 cfg->flags |= MONO_CFG_HAS_ALLOCA;
5571 if (header->init_locals)
5572 ins->flags |= MONO_INST_INIT;
5576 /* FIXME: set init flag if locals init is set in this method */
5578 case CEE_ENDFILTER: {
5579 MonoExceptionClause *clause, *nearest;
5580 int cc, nearest_num;
5584 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
5586 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
5587 ins->inst_left = *sp;
5589 MONO_ADD_INS (bblock, ins);
5590 start_new_bblock = 1;
5595 for (cc = 0; cc < header->num_clauses; ++cc) {
5596 clause = &header->clauses [cc];
5597 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
5598 (!nearest || (clause->token_or_filter > nearest->token_or_filter))) {
5604 filter_lengths [nearest_num] = (ip - header->code) - nearest->token_or_filter;
5608 case CEE_UNALIGNED_:
5609 ins_flag |= MONO_INST_UNALIGNED;
5610 /* FIXME: record alignment? we can assume 1 for now */
5615 ins_flag |= MONO_INST_VOLATILE;
5619 ins_flag |= MONO_INST_TAILCALL;
5620 /* Can't inline tail calls at this time */
5621 inline_costs += 100000;
5628 token = read32 (ip + 2);
5629 if (method->wrapper_type != MONO_WRAPPER_NONE)
5630 klass = mono_method_get_wrapper_data (method, token);
5632 klass = mono_class_get_full (image, token, generic_context);
5633 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5634 MonoInst *store, *load;
5635 NEW_PCONST (cfg, load, NULL);
5636 load->cil_code = ip;
5637 load->type = STACK_OBJ;
5638 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
5639 store->cil_code = ip;
5640 handle_loaded_temps (cfg, bblock, stack_start, sp);
5641 MONO_ADD_INS (bblock, store);
5642 store->inst_i0 = sp [0];
5643 store->inst_i1 = load;
5645 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
5650 case CEE_CONSTRAINED_:
5651 /* FIXME: implement */
5653 token = read32 (ip + 2);
5654 constrained_call = mono_class_get_full (image, token, generic_context);
5659 MonoInst *iargs [3];
5662 if ((cfg->opt & MONO_OPT_INTRINS) && (ip [1] == CEE_CPBLK) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5)) {
5664 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
5665 copy->inst_left = sp [0];
5666 copy->inst_right = sp [1];
5667 copy->cil_code = ip;
5669 MONO_ADD_INS (bblock, copy);
5676 handle_loaded_temps (cfg, bblock, stack_start, sp);
5677 if (ip [1] == CEE_CPBLK) {
5678 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
5680 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
5689 ins_flag |= MONO_INST_NOTYPECHECK;
5691 ins_flag |= MONO_INST_NORANGECHECK;
5692 /* we ignore the no-nullcheck for now since we
5693 * really do it explicitly only when doing callvirt->call
5699 /* FIXME: check we are in a catch handler */
5700 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
5701 load->cil_code = ip;
5702 MONO_INST_NEW (cfg, ins, CEE_THROW);
5703 ins->inst_left = load;
5705 MONO_ADD_INS (bblock, ins);
5707 start_new_bblock = 1;
5712 CHECK_STACK_OVF (1);
5714 token = read32 (ip + 2);
5715 /* FIXXME: handle generics. */
5716 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
5717 MonoType *type = mono_type_create_from_typespec (image, token);
5718 token = mono_type_size (type, &align);
5720 MonoClass *szclass = mono_class_get_full (image, token, generic_context);
5721 mono_class_init (szclass);
5722 token = mono_class_value_size (szclass, &align);
5724 NEW_ICONST (cfg, ins, token);
5729 case CEE_REFANYTYPE:
5731 MONO_INST_NEW (cfg, ins, OP_REFANYTYPE);
5733 ins->type = STACK_MP;
5734 ins->inst_left = *sp;
5735 ins->type = STACK_VTYPE;
5736 ins->klass = mono_defaults.typehandle_class;
5745 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
5750 g_error ("opcode 0x%02x not handled", *ip);
5753 if (start_new_bblock != 1)
5756 bblock->cil_length = ip - bblock->cil_code;
5757 bblock->next_bb = end_bblock;
5758 link_bblock (cfg, bblock, end_bblock);
5760 if (cfg->method == method && cfg->domainvar) {
5764 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
5765 call->signature = helper_sig_domain_get;
5766 call->inst.type = STACK_PTR;
5767 call->fptr = mono_domain_get;
5768 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, (MonoInst*)call);
5770 MONO_ADD_INS (init_localsbb, store);
5773 if (header->init_locals) {
5775 for (i = 0; i < header->num_locals; ++i) {
5776 int t = header->locals [i]->type;
5777 if (t == MONO_TYPE_VALUETYPE && header->locals [i]->data.klass->enumtype)
5778 t = header->locals [i]->data.klass->enum_basetype->type;
5779 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
5780 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
5781 NEW_ICONST (cfg, ins, 0);
5782 NEW_LOCSTORE (cfg, store, i, ins);
5783 MONO_ADD_INS (init_localsbb, store);
5784 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
5785 MONO_INST_NEW (cfg, ins, OP_I8CONST);
5786 ins->type = STACK_I8;
5788 NEW_LOCSTORE (cfg, store, i, ins);
5789 MONO_ADD_INS (init_localsbb, store);
5790 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
5791 MONO_INST_NEW (cfg, ins, OP_R8CONST);
5792 ins->type = STACK_R8;
5793 ins->inst_p0 = (void*)&r8_0;
5794 NEW_LOCSTORE (cfg, store, i, ins);
5795 MONO_ADD_INS (init_localsbb, store);
5796 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF)) {
5797 NEW_LOCLOADA (cfg, ins, i);
5798 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (header->locals [i]), NULL, NULL);
5801 NEW_PCONST (cfg, ins, NULL);
5802 NEW_LOCSTORE (cfg, store, i, ins);
5803 MONO_ADD_INS (init_localsbb, store);
5809 /* resolve backward branches in the middle of an existing basic block */
5810 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
5812 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
5813 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
5814 if (tblock != start_bblock) {
5816 split_bblock (cfg, tblock, bblock);
5817 l = bblock->cil_code - header->code;
5818 bblock->cil_length = tblock->cil_length - l;
5819 tblock->cil_length = l;
5821 g_print ("recheck failed.\n");
5826 * we compute regions here, because the length of filter clauses is not known in advance.
5827 * It is computed in the CEE_ENDFILTER case in the above switch statement
5829 if (cfg->method == method) {
5831 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5832 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
5834 mono_create_spvar_for_region (cfg, bb->region);
5835 if (cfg->verbose_level > 2)
5836 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
5839 g_hash_table_destroy (bbhash);
5842 dont_inline = g_list_remove (dont_inline, method);
5843 return inline_costs;
5846 if (cfg->method != method)
5847 g_hash_table_destroy (bbhash);
5848 dont_inline = g_list_remove (dont_inline, method);
5852 if (cfg->method != method)
5853 g_hash_table_destroy (bbhash);
5854 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
5855 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
5856 dont_inline = g_list_remove (dont_inline, method);
5861 mono_print_tree (MonoInst *tree) {
5867 arity = mono_burg_arity [tree->opcode];
5869 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
5871 switch (tree->opcode) {
5873 printf ("[%d]", tree->inst_c0);
5876 printf ("[%lld]", tree->inst_l);
5879 printf ("[%f]", *(double*)tree->inst_p0);
5882 printf ("[%f]", *(float*)tree->inst_p0);
5886 printf ("[%d]", tree->inst_c0);
5889 if (tree->inst_offset < 0)
5890 printf ("[-0x%x(%s)]", -tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5892 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5895 printf ("[%s]", mono_arch_regname (tree->dreg));
5898 printf ("[%s]", tree->inst_newa_class->name);
5899 mono_print_tree (tree->inst_newa_len);
5910 case OP_VOIDCALLVIRT: {
5911 MonoCallInst *call = (MonoCallInst*)tree;
5913 printf ("[%s]", call->method->name);
5918 printf ("[%d (", tree->inst_c0);
5919 for (i = 0; i < tree->inst_phi_args [0]; i++) {
5922 printf ("%d", tree->inst_phi_args [i + 1]);
5933 case OP_LOAD_MEMBASE:
5934 case OP_LOADI4_MEMBASE:
5935 case OP_LOADU4_MEMBASE:
5936 case OP_LOADU1_MEMBASE:
5937 case OP_LOADI1_MEMBASE:
5938 case OP_LOADU2_MEMBASE:
5939 case OP_LOADI2_MEMBASE:
5940 printf ("[%s] <- [%s + 0x%x]", mono_arch_regname (tree->dreg), mono_arch_regname (tree->inst_basereg), tree->inst_offset);
5943 printf ("[B%d]", tree->inst_target_bb->block_num);
5953 case OP_VOIDCALL_REG:
5954 mono_print_tree (tree->inst_left);
5966 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
5967 mono_print_tree (tree->inst_left);
5970 if (!mono_arch_print_tree(tree, arity)) {
5972 mono_print_tree (tree->inst_left);
5974 mono_print_tree (tree->inst_right);
5985 mono_print_tree_nl (MonoInst *tree)
5987 mono_print_tree (tree);
5992 type_from_typename (char *typename)
5996 if (!strcmp (typename, "int"))
5997 klass = mono_defaults.int_class;
5998 else if (!strcmp (typename, "ptr"))
5999 klass = mono_defaults.int_class;
6000 else if (!strcmp (typename, "void"))
6001 klass = mono_defaults.void_class;
6002 else if (!strcmp (typename, "int32"))
6003 klass = mono_defaults.int32_class;
6004 else if (!strcmp (typename, "uint32"))
6005 klass = mono_defaults.uint32_class;
6006 else if (!strcmp (typename, "long"))
6007 klass = mono_defaults.int64_class;
6008 else if (!strcmp (typename, "ulong"))
6009 klass = mono_defaults.uint64_class;
6010 else if (!strcmp (typename, "float"))
6011 klass = mono_defaults.single_class;
6012 else if (!strcmp (typename, "double"))
6013 klass = mono_defaults.double_class;
6014 else if (!strcmp (typename, "object"))
6015 klass = mono_defaults.object_class;
6018 g_assert_not_reached ();
6020 return &klass->byval_arg;
6023 static MonoMethodSignature*
6024 make_icall_sig (const char *sigstr)
6029 MonoMethodSignature *res;
6031 parts = g_strsplit (sigstr, " ", 256);
6040 res = mono_metadata_signature_alloc (mono_defaults.corlib, len - 1);
6043 #ifdef PLATFORM_WIN32
6045 * Under windows, the default pinvoke calling convention is STDCALL but
6048 res->call_convention = MONO_CALL_C;
6051 res->ret = type_from_typename (parts [0]);
6052 for (i = 1; i < len; ++i) {
6053 res->params [i - 1] = type_from_typename (parts [i]);
6062 create_helper_signature (void)
6065 /* FIXME: set call conv */
6066 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
6067 helper_sig_newarr = make_icall_sig ("object ptr ptr int32");
6069 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
6070 helper_sig_newarr_specific = make_icall_sig ("object ptr int32");
6072 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
6073 helper_sig_object_new = make_icall_sig ("object ptr ptr");
6075 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
6076 helper_sig_object_new_specific = make_icall_sig ("object ptr");
6078 /* void* mono_method_compile (MonoMethod*) */
6079 helper_sig_compile = make_icall_sig ("ptr ptr");
6081 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
6082 helper_sig_compile_virt = make_icall_sig ("ptr object ptr");
6084 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
6085 helper_sig_ldstr = make_icall_sig ("object ptr ptr int32");
6087 /* MonoDomain *mono_domain_get (void) */
6088 helper_sig_domain_get = make_icall_sig ("ptr");
6090 /* void stelem_ref (MonoArray *, int index, MonoObject *) */
6091 helper_sig_stelem_ref = make_icall_sig ("void ptr int32 object");
6093 /* void stelem_ref_check (MonoArray *, MonoObject *) */
6094 helper_sig_stelem_ref_check = make_icall_sig ("void object object");
6096 /* long amethod (long, long) */
6097 helper_sig_long_long_long = make_icall_sig ("long long long");
6099 /* object amethod (intptr) */
6100 helper_sig_obj_ptr = make_icall_sig ("object ptr");
6102 helper_sig_obj_ptr_ptr = make_icall_sig ("object ptr ptr");
6104 /* void amethod (intptr) */
6105 helper_sig_void_ptr = make_icall_sig ("void ptr");
6107 /* void amethod (MonoObject *obj) */
6108 helper_sig_void_obj = make_icall_sig ("void object");
6110 /* void amethod (MonoObject *obj, void *ptr, int i) */
6111 helper_sig_void_obj_ptr_int = make_icall_sig ("void object ptr int");
6113 /* intptr amethod (void) */
6114 helper_sig_ptr_void = make_icall_sig ("ptr");
6116 /* object amethod (void) */
6117 helper_sig_obj_void = make_icall_sig ("object");
6119 /* void amethod (intptr, intptr) */
6120 helper_sig_void_ptr_ptr = make_icall_sig ("void ptr ptr");
6122 /* void amethod (intptr, intptr, intptr) */
6123 helper_sig_void_ptr_ptr_ptr = make_icall_sig ("void ptr ptr ptr");
6125 /* intptr amethod (intptr, intptr) */
6126 helper_sig_ptr_ptr_ptr = make_icall_sig ("ptr ptr ptr");
6128 /* IntPtr amethod (object) */
6129 helper_sig_ptr_obj = make_icall_sig ("ptr object");
6131 /* IntPtr amethod (object, int) */
6132 helper_sig_ptr_obj_int = make_icall_sig ("ptr object int");
6134 /* IntPtr amethod (int) */
6135 helper_sig_ptr_int = make_icall_sig ("ptr int32");
6137 /* long amethod (long, guint32) */
6138 helper_sig_long_long_int = make_icall_sig ("long long int32");
6140 /* ulong amethod (double) */
6141 helper_sig_ulong_double = make_icall_sig ("ulong double");
6143 /* long amethod (double) */
6144 helper_sig_long_double = make_icall_sig ("long double");
6146 /* double amethod (long) */
6147 helper_sig_double_long = make_icall_sig ("double long");
6149 /* double amethod (int) */
6150 helper_sig_double_int = make_icall_sig ("double int32");
6152 /* float amethod (long) */
6153 helper_sig_float_long = make_icall_sig ("float long");
6155 /* double amethod (double, double) */
6156 helper_sig_double_double_double = make_icall_sig ("double double double");
6158 /* uint amethod (double) */
6159 helper_sig_uint_double = make_icall_sig ("uint32 double");
6161 /* int amethod (double) */
6162 helper_sig_int_double = make_icall_sig ("int32 double");
6164 /* void initobj (intptr, int size) */
6165 helper_sig_initobj = make_icall_sig ("void ptr int32");
6167 /* void memcpy (intptr, intptr, int size) */
6168 helper_sig_memcpy = make_icall_sig ("void ptr ptr int32");
6170 /* void memset (intptr, int val, int size) */
6171 helper_sig_memset = make_icall_sig ("void ptr int32 int32");
6173 helper_sig_class_init_trampoline = make_icall_sig ("void");
6176 static GHashTable *jit_icall_hash_name = NULL;
6177 static GHashTable *jit_icall_hash_addr = NULL;
6180 mono_find_jit_icall_by_name (const char *name)
6182 MonoJitICallInfo *info;
6183 g_assert (jit_icall_hash_name);
6185 //printf ("lookup addr %s %p\n", name, g_hash_table_lookup (jit_icall_hash_name, name));
6186 EnterCriticalSection (&trampoline_hash_mutex);
6187 info = g_hash_table_lookup (jit_icall_hash_name, name);
6188 LeaveCriticalSection (&trampoline_hash_mutex);
6193 mono_find_jit_icall_by_addr (gconstpointer addr)
6195 MonoJitICallInfo *info;
6196 g_assert (jit_icall_hash_addr);
6198 EnterCriticalSection (&trampoline_hash_mutex);
6199 info = g_hash_table_lookup (jit_icall_hash_addr, (gpointer)addr);
6200 LeaveCriticalSection (&trampoline_hash_mutex);
6206 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
6209 MonoMethod *wrapper;
6212 if (callinfo->wrapper)
6213 return callinfo->wrapper;
6215 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
6216 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
6217 /* Must be domain neutral since there is only one copy */
6218 code = mono_jit_compile_method_with_opt (wrapper, default_opt | MONO_OPT_SHARED);
6220 EnterCriticalSection (&trampoline_hash_mutex);
6221 if (!callinfo->wrapper) {
6222 callinfo->wrapper = code;
6223 g_hash_table_insert (jit_icall_hash_addr, (gpointer)callinfo->wrapper, callinfo);
6225 LeaveCriticalSection (&trampoline_hash_mutex);
6228 return callinfo->wrapper;
6232 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save)
6234 MonoJitICallInfo *info;
6239 EnterCriticalSection (&trampoline_hash_mutex);
6241 if (!jit_icall_hash_name) {
6242 jit_icall_hash_name = g_hash_table_new (g_str_hash, g_str_equal);
6243 jit_icall_hash_addr = g_hash_table_new (NULL, NULL);
6246 if (g_hash_table_lookup (jit_icall_hash_name, name)) {
6247 g_warning ("jit icall already defined \"%s\"\n", name);
6248 g_assert_not_reached ();
6251 info = g_new (MonoJitICallInfo, 1);
6258 info->wrapper = func;
6260 info->wrapper = NULL;
6263 g_hash_table_insert (jit_icall_hash_name, (gpointer)info->name, info);
6264 g_hash_table_insert (jit_icall_hash_addr, (gpointer)func, info);
6266 LeaveCriticalSection (&trampoline_hash_mutex);
6271 mono_create_class_init_trampoline (MonoVTable *vtable)
6275 /* previously created trampoline code */
6276 mono_domain_lock (vtable->domain);
6278 mono_g_hash_table_lookup (vtable->domain->class_init_trampoline_hash,
6280 mono_domain_unlock (vtable->domain);
6284 code = mono_arch_create_class_init_trampoline (vtable);
6286 /* store trampoline address */
6287 mono_domain_lock (vtable->domain);
6288 mono_g_hash_table_insert (vtable->domain->class_init_trampoline_hash,
6290 mono_domain_unlock (vtable->domain);
6292 EnterCriticalSection (&trampoline_hash_mutex);
6293 if (!class_init_hash_addr)
6294 class_init_hash_addr = g_hash_table_new (NULL, NULL);
6295 g_hash_table_insert (class_init_hash_addr, code, vtable);
6296 LeaveCriticalSection (&trampoline_hash_mutex);
6302 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method,
6303 gboolean add_sync_wrapper)
6308 if (add_sync_wrapper && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
6309 return mono_create_jump_trampoline (domain, mono_marshal_get_synchronized_wrapper (method), FALSE);
6311 code = mono_jit_find_compiled_method (domain, method);
6315 EnterCriticalSection (&trampoline_hash_mutex);
6317 if (jump_trampoline_hash) {
6318 code = g_hash_table_lookup (jump_trampoline_hash, method);
6320 LeaveCriticalSection (&trampoline_hash_mutex);
6325 ji = mono_arch_create_jump_trampoline (method);
6328 * mono_delegate_ctor needs to find the method metadata from the
6329 * trampoline address, so we save it here.
6332 mono_jit_info_table_add (mono_get_root_domain (), ji);
6334 if (!jump_trampoline_hash)
6335 jump_trampoline_hash = g_hash_table_new (NULL, NULL);
6336 g_hash_table_insert (jump_trampoline_hash, method, ji->code_start);
6338 LeaveCriticalSection (&trampoline_hash_mutex);
6340 return ji->code_start;
6344 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
6348 EnterCriticalSection (&trampoline_hash_mutex);
6349 if (class_init_hash_addr)
6350 res = g_hash_table_lookup (class_init_hash_addr, addr);
6353 LeaveCriticalSection (&trampoline_hash_mutex);
6358 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func, gboolean no_throw)
6360 MonoJitICallInfo *info;
6362 if (!emul_opcode_map)
6363 emul_opcode_map = g_new0 (MonoJitICallInfo*, OP_LAST + 1);
6365 g_assert (!sig->hasthis);
6366 g_assert (sig->param_count < 3);
6368 info = mono_register_jit_icall (func, name, sig, no_throw);
6370 emul_opcode_map [opcode] = info;
6374 decompose_foreach (MonoInst *tree, gpointer data)
6376 static MonoJitICallInfo *newarr_info = NULL;
6377 static MonoJitICallInfo *newarr_specific_info = NULL;
6378 MonoJitICallInfo *info;
6381 switch (tree->opcode) {
6383 MonoCompile *cfg = data;
6384 MonoInst *iargs [3];
6387 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
6388 g_assert (newarr_info);
6389 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
6390 g_assert (newarr_specific_info);
6393 if (cfg->opt & MONO_OPT_SHARED) {
6394 NEW_DOMAINCONST (cfg, iargs [0]);
6395 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
6396 iargs [2] = tree->inst_newa_len;
6401 MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
6403 NEW_VTABLECONST (cfg, iargs [0], vtable);
6404 iargs [1] = tree->inst_newa_len;
6406 info = newarr_specific_info;
6409 mono_emulate_opcode (cfg, tree, iargs, info);
6411 /* Need to decompose arguments after the the opcode is decomposed */
6412 for (i = 0; i < info->sig->param_count; ++i)
6413 dec_foreach (iargs [i], cfg);
6423 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
6425 switch (mono_burg_arity [tree->opcode]) {
6428 mono_inst_foreach (tree->inst_left, func, data);
6431 mono_inst_foreach (tree->inst_left, func, data);
6432 mono_inst_foreach (tree->inst_right, func, data);
6435 g_assert_not_reached ();
6442 mono_print_bb_code (MonoBasicBlock *bb) {
6444 MonoInst *c = bb->code;
6446 mono_print_tree (c);
6454 print_dfn (MonoCompile *cfg) {
6459 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
6461 for (i = 0; i < cfg->num_bblocks; ++i) {
6462 bb = cfg->bblocks [i];
6464 char* code1, *code2;
6465 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
6466 if (bb->last_ins->cil_code)
6467 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
6469 code2 = g_strdup ("");
6471 code1 [strlen (code1) - 1] = 0;
6472 code = g_strdup_printf ("%s -> %s", code1, code2);
6476 code = g_strdup ("\n");
6477 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
6479 MonoInst *c = bb->code;
6481 mono_print_tree (c);
6489 g_print ("\tprev:");
6490 for (j = 0; j < bb->in_count; ++j) {
6491 g_print (" BB%d", bb->in_bb [j]->block_num);
6493 g_print ("\t\tsucc:");
6494 for (j = 0; j < bb->out_count; ++j) {
6495 g_print (" BB%d", bb->out_bb [j]->block_num);
6497 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
6500 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
6503 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
6505 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
6513 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
6517 g_assert (bb->code);
6518 bb->last_ins->next = inst;
6519 bb->last_ins = inst;
6521 bb->last_ins = bb->code = inst;
6526 mono_destroy_compile (MonoCompile *cfg)
6528 //mono_mempool_stats (cfg->mempool);
6529 g_hash_table_destroy (cfg->bb_hash);
6530 mono_free_loop_info (cfg);
6532 mono_regstate_free (cfg->rs);
6534 g_hash_table_destroy (cfg->spvars);
6535 mono_mempool_destroy (cfg->mempool);
6536 g_list_free (cfg->ldstr_list);
6538 g_free (cfg->varinfo);
6543 #ifdef HAVE_KW_THREAD
6544 static __thread gpointer mono_lmf_addr;
6548 mono_get_lmf_addr (void)
6550 #ifdef HAVE_KW_THREAD
6551 return mono_lmf_addr;
6553 MonoJitTlsData *jit_tls;
6555 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
6556 return &jit_tls->lmf;
6558 g_assert_not_reached ();
6564 * mono_thread_abort:
6565 * @obj: exception object
6567 * abort the thread, print exception information and stack trace
6570 mono_thread_abort (MonoObject *obj)
6572 /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
6574 /* handle_remove should be eventually called for this thread, too
6581 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
6583 MonoJitTlsData *jit_tls;
6586 jit_tls = TlsGetValue (mono_jit_tls_id);
6590 jit_tls = g_new0 (MonoJitTlsData, 1);
6592 TlsSetValue (mono_jit_tls_id, jit_tls);
6594 jit_tls->abort_func = abort_func;
6595 jit_tls->end_of_stack = stack_start;
6597 lmf = g_new0 (MonoLMF, 1);
6600 jit_tls->lmf = jit_tls->first_lmf = lmf;
6602 #ifdef HAVE_KW_THREAD
6603 mono_lmf_addr = &jit_tls->lmf;
6606 mono_arch_setup_jit_tls_data (jit_tls);
6612 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
6615 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
6616 thread = mono_thread_current ();
6618 thread->jit_data = jit_tls;
6621 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
6624 mono_thread_abort_dummy (MonoObject *obj)
6626 if (mono_thread_attach_aborted_cb)
6627 mono_thread_attach_aborted_cb (obj);
6629 mono_thread_abort (obj);
6633 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
6636 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
6637 thread = mono_thread_current ();
6639 thread->jit_data = jit_tls;
6643 mini_thread_cleanup (MonoThread *thread)
6645 MonoJitTlsData *jit_tls = thread->jit_data;
6648 mono_arch_free_jit_tls_data (jit_tls);
6649 g_free (jit_tls->first_lmf);
6651 thread->jit_data = NULL;
6656 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
6658 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
6662 ji->data.target = target;
6663 ji->next = cfg->patch_info;
6665 cfg->patch_info = ji;
6669 mono_remove_patch_info (MonoCompile *cfg, int ip)
6671 MonoJumpInfo **ji = &cfg->patch_info;
6674 if ((*ji)->ip.i == ip)
6677 ji = &((*ji)->next);
6682 mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors)
6684 unsigned char *ip = patch_info->ip.i + code;
6685 gconstpointer target = NULL;
6687 switch (patch_info->type) {
6688 case MONO_PATCH_INFO_BB:
6689 target = patch_info->data.bb->native_offset + code;
6691 case MONO_PATCH_INFO_ABS:
6692 target = patch_info->data.target;
6694 case MONO_PATCH_INFO_LABEL:
6695 target = patch_info->data.inst->inst_c0 + code;
6697 case MONO_PATCH_INFO_IP:
6700 case MONO_PATCH_INFO_METHOD_REL:
6701 target = code + patch_info->data.offset;
6703 case MONO_PATCH_INFO_INTERNAL_METHOD: {
6704 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
6706 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
6707 g_assert_not_reached ();
6709 target = mono_icall_get_wrapper (mi);
6712 case MONO_PATCH_INFO_METHOD_JUMP: {
6715 /* get the trampoline to the method from the domain */
6716 target = mono_create_jump_trampoline (domain, patch_info->data.method, TRUE);
6717 if (!domain->jump_target_hash)
6718 domain->jump_target_hash = g_hash_table_new (NULL, NULL);
6719 list = g_hash_table_lookup (domain->jump_target_hash, patch_info->data.method);
6720 list = g_slist_prepend (list, ip);
6721 g_hash_table_insert (domain->jump_target_hash, patch_info->data.method, list);
6724 case MONO_PATCH_INFO_METHOD:
6725 if (patch_info->data.method == method) {
6728 /* get the trampoline to the method from the domain */
6729 target = mono_arch_create_jit_trampoline (patch_info->data.method);
6731 case MONO_PATCH_INFO_SWITCH: {
6732 gpointer *jump_table;
6735 mono_domain_lock (domain);
6736 jump_table = mono_code_manager_reserve (domain->code_mp, sizeof (gpointer) * patch_info->table_size);
6737 mono_domain_unlock (domain);
6739 for (i = 0; i < patch_info->table_size; i++) {
6740 jump_table [i] = code + (int)patch_info->data.table [i];
6742 target = jump_table;
6745 case MONO_PATCH_INFO_METHODCONST:
6746 case MONO_PATCH_INFO_CLASS:
6747 case MONO_PATCH_INFO_IMAGE:
6748 case MONO_PATCH_INFO_FIELD:
6749 target = patch_info->data.target;
6751 case MONO_PATCH_INFO_IID:
6752 mono_class_init (patch_info->data.klass);
6753 target = (gpointer)patch_info->data.klass->interface_id;
6755 case MONO_PATCH_INFO_VTABLE:
6756 target = mono_class_vtable (domain, patch_info->data.klass);
6758 case MONO_PATCH_INFO_CLASS_INIT:
6759 target = mono_create_class_init_trampoline (mono_class_vtable (domain, patch_info->data.klass));
6761 case MONO_PATCH_INFO_SFLDA: {
6762 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
6763 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (vtable->klass, method))
6764 /* Done by the generated code */
6768 mono_runtime_class_init (vtable);
6770 target = (char*)vtable->data + patch_info->data.field->offset;
6773 case MONO_PATCH_INFO_R4:
6774 case MONO_PATCH_INFO_R8:
6775 target = patch_info->data.target;
6777 case MONO_PATCH_INFO_EXC_NAME:
6778 target = patch_info->data.name;
6780 case MONO_PATCH_INFO_LDSTR:
6782 mono_ldstr (domain, patch_info->data.token->image,
6783 mono_metadata_token_index (patch_info->data.token->token));
6785 case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
6787 MonoClass *handle_class;
6789 handle = mono_ldtoken (patch_info->data.token->image,
6790 patch_info->data.token->token, &handle_class, NULL);
6791 mono_class_init (handle_class);
6792 mono_class_init (mono_class_from_mono_type (handle));
6795 mono_type_get_object (domain, handle);
6798 case MONO_PATCH_INFO_LDTOKEN: {
6800 MonoClass *handle_class;
6802 handle = mono_ldtoken (patch_info->data.token->image,
6803 patch_info->data.token->token, &handle_class, NULL);
6804 mono_class_init (handle_class);
6809 case MONO_PATCH_INFO_BB_OVF:
6810 case MONO_PATCH_INFO_EXC_OVF:
6813 g_assert_not_reached ();
6816 return (gpointer)target;
6820 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
6821 MonoJitICallInfo *info;
6823 decompose_foreach (tree, cfg);
6825 switch (mono_burg_arity [tree->opcode]) {
6828 dec_foreach (tree->inst_left, cfg);
6830 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6831 MonoInst *iargs [2];
6833 iargs [0] = tree->inst_left;
6835 mono_emulate_opcode (cfg, tree, iargs, info);
6841 #ifdef MONO_ARCH_BIGMUL_INTRINS
6842 if (tree->opcode == OP_LMUL
6843 && (cfg->opt & MONO_OPT_INTRINS)
6844 && (tree->inst_left->opcode == CEE_CONV_I8
6845 || tree->inst_left->opcode == CEE_CONV_U8)
6846 && tree->inst_left->inst_left->type == STACK_I4
6847 && (tree->inst_right->opcode == CEE_CONV_I8
6848 || tree->inst_right->opcode == CEE_CONV_U8)
6849 && tree->inst_right->inst_left->type == STACK_I4
6850 && tree->inst_left->opcode == tree->inst_right->opcode) {
6851 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
6852 tree->inst_left = tree->inst_left->inst_left;
6853 tree->inst_right = tree->inst_right->inst_left;
6854 dec_foreach (tree, cfg);
6857 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6858 MonoInst *iargs [2];
6860 iargs [0] = tree->inst_i0;
6861 iargs [1] = tree->inst_i1;
6863 mono_emulate_opcode (cfg, tree, iargs, info);
6865 dec_foreach (iargs [0], cfg);
6866 dec_foreach (iargs [1], cfg);
6869 dec_foreach (tree->inst_left, cfg);
6870 dec_foreach (tree->inst_right, cfg);
6874 g_assert_not_reached ();
6879 decompose_pass (MonoCompile *cfg) {
6882 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6885 cfg->prev_ins = NULL;
6886 for (tree = cfg->cbb->code; tree; tree = tree->next) {
6887 dec_foreach (tree, cfg);
6888 cfg->prev_ins = tree;
6894 nullify_basic_block (MonoBasicBlock *bb)
6901 bb->code = bb->last_ins = NULL;
6902 bb->cil_code = NULL;
6906 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6910 for (i = 0; i < bb->out_count; i++) {
6911 MonoBasicBlock *ob = bb->out_bb [i];
6914 if (bb->out_count > 1) {
6915 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
6919 bb->out_bb [i] = repl;
6926 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6930 for (i = 0; i < bb->in_count; i++) {
6931 MonoBasicBlock *ib = bb->in_bb [i];
6934 if (bb->in_count > 1) {
6935 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
6939 bb->in_bb [i] = repl;
6946 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6950 for (i = 0; i < bb->out_count; i++) {
6951 MonoBasicBlock *ob = bb->out_bb [i];
6952 for (j = 0; j < ob->in_count; j++) {
6953 if (ob->in_bb [j] == orig) {
6954 ob->in_bb [j] = repl;
6963 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
6965 bb->out_count = bbn->out_count;
6966 bb->out_bb = bbn->out_bb;
6968 replace_basic_block (bb, bbn, bb);
6972 bb->last_ins->next = bbn->code;
6973 bb->last_ins = bbn->last_ins;
6976 bb->code = bbn->code;
6977 bb->last_ins = bbn->last_ins;
6979 bb->next_bb = bbn->next_bb;
6980 nullify_basic_block (bbn);
6984 * Optimizes the branches on the Control Flow Graph
6988 optimize_branches (MonoCompile *cfg)
6990 int i, changed = FALSE;
6991 MonoBasicBlock *bb, *bbn;
6992 guint32 niterations;
6995 * Some crazy loops could cause the code below to go into an infinite
6996 * loop, see bug #53003 for an example. To prevent this, we put an upper
6997 * bound on the number of iterations.
7004 /* we skip the entry block (exit is handled specially instead ) */
7005 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
7007 /* dont touch code inside exception clauses */
7008 if (bb->region != -1)
7011 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
7012 if (cfg->verbose_level > 2)
7013 g_print ("nullify block triggered %d\n", bbn->block_num);
7015 bb->next_bb = bbn->next_bb;
7017 for (i = 0; i < bbn->out_count; i++)
7018 replace_in_block (bbn->out_bb [i], bbn, NULL);
7020 nullify_basic_block (bbn);
7024 if (bb->out_count == 1) {
7025 bbn = bb->out_bb [0];
7027 /* conditional branches where true and false targets are the same can be also replaced with CEE_BR */
7028 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins)) {
7029 bb->last_ins->opcode = CEE_BR;
7030 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
7032 if (cfg->verbose_level > 2)
7033 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
7036 if (bb->region == bbn->region && bb->next_bb == bbn) {
7037 /* the block are in sequence anyway ... */
7039 /* branches to the following block can be removed */
7040 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
7041 bb->last_ins->opcode = CEE_NOP;
7043 if (cfg->verbose_level > 2)
7044 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
7047 if (bbn->in_count == 1) {
7049 if (bbn != cfg->bb_exit) {
7050 if (cfg->verbose_level > 2)
7051 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
7052 merge_basic_blocks (bb, bbn);
7056 //mono_print_bb_code (bb);
7061 } while (changed && (niterations > 0));
7068 /* we skip the entry block (exit is handled specially instead ) */
7069 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
7071 /* dont touch code inside exception clauses */
7072 if (bb->region != -1)
7075 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
7076 if (cfg->verbose_level > 2) {
7077 g_print ("nullify block triggered %d\n", bbn->block_num);
7079 bb->next_bb = bbn->next_bb;
7081 for (i = 0; i < bbn->out_count; i++)
7082 replace_in_block (bbn->out_bb [i], bbn, NULL);
7084 nullify_basic_block (bbn);
7090 if (bb->out_count == 1) {
7091 bbn = bb->out_bb [0];
7093 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
7094 bbn = bb->last_ins->inst_target_bb;
7095 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7096 bbn->code->inst_target_bb->region == bb->region) {
7098 if (cfg->verbose_level > 2)
7099 g_print ("in %s branch to branch triggered %d -> %d -> %d\n", cfg->method->name,
7100 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num);
7102 replace_in_block (bbn, bb, NULL);
7103 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7104 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7105 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
7110 } else if (bb->out_count == 2) {
7111 if (bb->last_ins && MONO_IS_COND_BRANCH_NOFP (bb->last_ins)) {
7112 bbn = bb->last_ins->inst_true_bb;
7113 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7114 bbn->code->inst_target_bb->region == bb->region) {
7115 if (cfg->verbose_level > 2)
7116 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
7117 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
7120 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
7122 replace_in_block (bbn, bb, NULL);
7124 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
7125 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7127 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7133 bbn = bb->last_ins->inst_false_bb;
7134 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7135 bbn->code->inst_target_bb->region == bb->region) {
7136 if (cfg->verbose_level > 2)
7137 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
7138 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
7141 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
7143 replace_in_block (bbn, bb, NULL);
7145 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
7146 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7148 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7156 } while (changed && (niterations > 0));
7161 mono_compile_create_vars (MonoCompile *cfg)
7163 MonoMethodSignature *sig;
7164 MonoMethodHeader *header;
7167 header = ((MonoMethodNormal *)cfg->method)->header;
7169 sig = cfg->method->signature;
7171 if (!MONO_TYPE_IS_VOID (sig->ret)) {
7172 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
7173 cfg->ret->opcode = OP_RETARG;
7174 cfg->ret->inst_vtype = sig->ret;
7175 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
7177 if (cfg->verbose_level > 2)
7178 g_print ("creating vars\n");
7181 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
7183 for (i = 0; i < sig->param_count; ++i)
7184 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
7186 cfg->locals_start = cfg->num_varinfo;
7188 if (cfg->verbose_level > 2)
7189 g_print ("creating locals\n");
7190 for (i = 0; i < header->num_locals; ++i)
7191 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
7192 if (cfg->verbose_level > 2)
7193 g_print ("locals done\n");
7197 mono_print_code (MonoCompile *cfg)
7201 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7202 MonoInst *tree = bb->code;
7207 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
7209 for (; tree; tree = tree->next) {
7210 mono_print_tree (tree);
7215 bb->last_ins->next = NULL;
7219 extern const char * const mono_burg_rule_string [];
7222 emit_state (MonoCompile *cfg, MBState *state, int goal)
7225 int ern = mono_burg_rule (state, goal);
7226 const guint16 *nts = mono_burg_nts [ern];
7229 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
7233 // state->reg1 = state->reg2; /* chain rule */
7235 state->reg1 = mono_regstate_next_int (cfg->rs);
7236 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
7239 state->reg1 = mono_regstate_next_int (cfg->rs);
7240 state->reg2 = mono_regstate_next_int (cfg->rs);
7243 state->reg1 = mono_regstate_next_float (cfg->rs);
7250 mono_burg_kids (state, ern, kids);
7252 emit_state (cfg, kids [0], nts [0]);
7254 emit_state (cfg, kids [1], nts [1]);
7256 g_assert (!nts [3]);
7257 emit_state (cfg, kids [2], nts [2]);
7262 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
7263 if ((emit = mono_burg_func [ern]))
7264 emit (state, state->tree, cfg);
7267 #define DEBUG_SELECTION
7270 mini_select_instructions (MonoCompile *cfg)
7272 static const int reverse_map [] = {
7273 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
7274 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
7276 static const int reverse_fmap [] = {
7277 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
7278 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
7280 static const int reverse_lmap [] = {
7281 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
7282 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
7287 cfg->state_pool = mono_mempool_new ();
7288 cfg->rs = mono_regstate_new ();
7290 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7291 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins) &&
7292 bb->next_bb != bb->last_ins->inst_false_bb) {
7294 /* we are careful when inverting, since bugs like #59580
7295 * could show up when dealing with NaNs.
7297 if (MONO_IS_COND_BRANCH_NOFP(bb->last_ins) && bb->next_bb == bb->last_ins->inst_true_bb) {
7298 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
7299 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
7300 bb->last_ins->inst_false_bb = tmp;
7302 if (bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
7303 bb->last_ins->opcode = reverse_map [bb->last_ins->opcode - CEE_BEQ];
7304 } else if (bb->last_ins->opcode >= OP_FBEQ && bb->last_ins->opcode <= OP_FBLT_UN) {
7305 bb->last_ins->opcode = reverse_fmap [bb->last_ins->opcode - OP_FBEQ];
7306 } else if (bb->last_ins->opcode >= OP_LBEQ && bb->last_ins->opcode <= OP_LBLT_UN) {
7307 bb->last_ins->opcode = reverse_lmap [bb->last_ins->opcode - OP_LBEQ];
7310 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
7311 inst->opcode = CEE_BR;
7312 inst->inst_target_bb = bb->last_ins->inst_false_bb;
7313 mono_bblock_add_inst (bb, inst);
7318 #ifdef DEBUG_SELECTION
7319 if (cfg->verbose_level >= 4) {
7320 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7321 MonoInst *tree = bb->code;
7322 g_print ("DUMP BLOCK %d:\n", bb->block_num);
7325 for (; tree; tree = tree->next) {
7326 mono_print_tree (tree);
7333 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7334 MonoInst *tree = bb->code, *next;
7340 bb->last_ins = NULL;
7343 mono_regstate_reset (cfg->rs);
7345 #ifdef DEBUG_SELECTION
7346 if (cfg->verbose_level >= 3)
7347 g_print ("LABEL BLOCK %d:\n", bb->block_num);
7349 for (; tree; tree = next) {
7351 #ifdef DEBUG_SELECTION
7352 if (cfg->verbose_level >= 3) {
7353 mono_print_tree (tree);
7358 if (!(mbstate = mono_burg_label (tree, cfg))) {
7359 g_warning ("unable to label tree %p", tree);
7360 mono_print_tree (tree);
7362 g_assert_not_reached ();
7364 emit_state (cfg, mbstate, MB_NTERM_stmt);
7366 bb->max_ireg = cfg->rs->next_vireg;
7367 bb->max_freg = cfg->rs->next_vfreg;
7370 bb->last_ins->next = NULL;
7372 mono_mempool_empty (cfg->state_pool);
7374 mono_mempool_destroy (cfg->state_pool);
7378 mono_codegen (MonoCompile *cfg)
7380 MonoJumpInfo *patch_info;
7382 int i, max_epilog_size;
7385 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7386 cfg->spill_count = 0;
7387 /* we reuse dfn here */
7388 /* bb->dfn = bb_count++; */
7389 mono_arch_local_regalloc (cfg, bb);
7392 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
7393 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
7395 code = mono_arch_emit_prolog (cfg);
7397 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7398 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
7400 cfg->code_len = code - cfg->native_code;
7401 cfg->prolog_end = cfg->code_len;
7403 mono_debug_open_method (cfg);
7405 /* emit code all basic blocks */
7406 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7407 bb->native_offset = cfg->code_len;
7408 mono_arch_output_basic_block (cfg, bb);
7410 cfg->bb_exit->native_offset = cfg->code_len;
7412 code = cfg->native_code + cfg->code_len;
7414 max_epilog_size = mono_arch_max_epilog_size (cfg);
7416 /* we always allocate code in cfg->domain->code_mp to increase locality */
7417 cfg->code_size = cfg->code_len + max_epilog_size;
7418 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
7420 mono_domain_lock (cfg->domain);
7421 code = mono_code_manager_reserve (cfg->domain->code_mp, cfg->code_size);
7422 mono_domain_unlock (cfg->domain);
7424 memcpy (code, cfg->native_code, cfg->code_len);
7425 g_free (cfg->native_code);
7426 cfg->native_code = code;
7427 code = cfg->native_code + cfg->code_len;
7429 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
7431 cfg->epilog_begin = cfg->code_len;
7433 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7434 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
7436 cfg->code_len = code - cfg->native_code;
7438 mono_arch_emit_epilog (cfg);
7440 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7441 switch (patch_info->type) {
7442 case MONO_PATCH_INFO_ABS: {
7443 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
7445 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
7446 if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) &&
7447 strstr (cfg->method->name, info->name))
7449 * This is an icall wrapper, and this is a call to the
7454 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
7455 patch_info->data.name = info->name;
7459 MonoVTable *vtable = mono_find_class_init_trampoline_by_addr (patch_info->data.target);
7461 patch_info->type = MONO_PATCH_INFO_CLASS_INIT;
7462 patch_info->data.klass = vtable->klass;
7467 case MONO_PATCH_INFO_SWITCH: {
7469 mono_domain_lock (cfg->domain);
7470 table = mono_code_manager_reserve (cfg->domain->code_mp, sizeof (gpointer) * patch_info->table_size);
7471 mono_domain_unlock (cfg->domain);
7473 patch_info->ip.i = patch_info->ip.label->inst_c0;
7474 for (i = 0; i < patch_info->table_size; i++) {
7475 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
7477 patch_info->data.target = table;
7486 if (cfg->verbose_level > 0)
7487 g_print ("Method %s emitted at %p to %p [%s]\n",
7488 mono_method_full_name (cfg->method, TRUE),
7489 cfg->native_code, cfg->native_code + cfg->code_len, cfg->domain->friendly_name);
7491 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info, cfg->run_cctors);
7493 mono_domain_lock (cfg->domain);
7494 mono_code_manager_commit (cfg->domain->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
7495 mono_domain_unlock (cfg->domain);
7497 mono_arch_flush_icache (cfg->native_code, cfg->code_len);
7499 mono_debug_close_method (cfg);
7503 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
7508 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
7509 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
7511 if (cp->opcode == OP_ICONST) {
7512 if (cfg->opt & MONO_OPT_CONSPROP) {
7513 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
7517 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
7518 if (cfg->opt & MONO_OPT_COPYPROP) {
7519 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
7525 arity = mono_burg_arity [tree->opcode];
7528 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
7529 if (cfg->opt & MONO_OPT_CFOLD)
7530 mono_constant_fold_inst (tree, NULL);
7531 /* The opcode may have changed */
7532 if (mono_burg_arity [tree->opcode] > 1) {
7533 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
7534 if (cfg->opt & MONO_OPT_CFOLD)
7535 mono_constant_fold_inst (tree, NULL);
7537 mono_constant_fold_inst (tree, NULL);
7543 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
7547 switch (tree->opcode) {
7557 if (tree->ssa_op == MONO_SSA_NOP) {
7558 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7575 case OP_VOIDCALL_REG:
7576 case OP_VOIDCALLVIRT:
7578 MonoCallInst *call = (MonoCallInst *)tree;
7579 MonoMethodSignature *sig = call->signature;
7580 int i, byref = FALSE;
7582 for (i = 0; i < sig->param_count; i++) {
7583 if (sig->params [i]->byref) {
7590 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7598 arity = mono_burg_arity [tree->opcode];
7604 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7607 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7608 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
7611 g_assert_not_reached ();
7616 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
7618 MonoInst *tree = bb->code;
7624 for (; tree; tree = tree->next) {
7626 mono_cprop_copy_values (cfg, tree, acp);
7628 mono_cprop_invalidate_values (tree, acp, acp_size);
7630 if (tree->ssa_op == MONO_SSA_STORE &&
7631 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
7632 MonoInst *i1 = tree->inst_i1;
7634 acp [tree->inst_i0->inst_c0] = NULL;
7636 for (i = 0; i < acp_size; i++) {
7637 if (acp [i] && acp [i]->opcode != OP_ICONST &&
7638 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
7643 if (i1->opcode == OP_ICONST) {
7644 acp [tree->inst_i0->inst_c0] = i1;
7645 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
7647 if (i1->ssa_op == MONO_SSA_LOAD &&
7648 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
7649 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
7650 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
7651 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
7656 if (tree->opcode == CEE_BEQ) {
7657 g_assert (tree->inst_i0->opcode == OP_COMPARE);
7658 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
7659 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
7661 tree->opcode = CEE_BR;
7662 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
7663 tree->inst_target_bb = tree->inst_true_bb;
7665 tree->inst_target_bb = tree->inst_false_bb;
7674 mono_local_cprop (MonoCompile *cfg)
7679 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
7681 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7682 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
7683 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
7688 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, int parts)
7690 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
7691 guint8 *ip = (guint8 *)header->code;
7694 int dfn = 0, i, code_size_ratio;
7696 mono_jit_stats.methods_compiled++;
7697 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
7698 mono_profiler_method_jit (method);
7700 cfg = g_new0 (MonoCompile, 1);
7701 cfg->method = method;
7702 cfg->mempool = mono_mempool_new ();
7704 cfg->prof_options = mono_profiler_get_events ();
7705 cfg->run_cctors = run_cctors;
7706 cfg->bb_hash = g_hash_table_new (NULL, NULL);
7707 cfg->domain = domain;
7708 cfg->verbose_level = mini_verbose;
7709 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
7710 ((MonoMethodNormal *)method)->header->max_stack);
7712 if (cfg->verbose_level > 2)
7713 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
7716 * create MonoInst* which represents arguments and local variables
7718 mono_compile_create_vars (cfg);
7720 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
7721 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7722 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
7723 mono_destroy_compile (cfg);
7727 mono_jit_stats.basic_blocks += cfg->num_bblocks;
7728 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
7730 if (cfg->num_varinfo > 2000) {
7732 * we disable some optimizations if there are too many variables
7733 * because JIT time may become too expensive. The actual number needs
7734 * to be tweaked and eventually the non-linear algorithms should be fixed.
7736 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
7737 cfg->disable_ssa = TRUE;
7739 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
7741 /* Depth-first ordering on basic blocks */
7742 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
7744 if (cfg->opt & MONO_OPT_BRANCH)
7745 optimize_branches (cfg);
7747 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
7748 if (cfg->num_bblocks != dfn + 1) {
7751 cfg->num_bblocks = dfn + 1;
7753 if (!header->clauses) {
7754 /* remove unreachable code, because the code in them may be
7755 * inconsistent (access to dead variables for example) */
7756 for (bb = cfg->bb_entry; bb;) {
7757 MonoBasicBlock *bbn = bb->next_bb;
7759 if (bbn && bbn->region == -1 && !bbn->dfn) {
7760 if (cfg->verbose_level > 1)
7761 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
7762 bb->next_bb = bbn->next_bb;
7763 nullify_basic_block (bbn);
7771 if (cfg->opt & MONO_OPT_LOOP) {
7772 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
7773 mono_compute_natural_loops (cfg);
7776 /* after method_to_ir */
7780 //#define DEBUGSSA "logic_run"
7781 #define DEBUGSSA_CLASS "Tests"
7784 if (!header->num_clauses && !cfg->disable_ssa) {
7785 mono_local_cprop (cfg);
7786 mono_ssa_compute (cfg);
7790 /* fixme: add all optimizations which requires SSA */
7791 if (cfg->opt & (MONO_OPT_DEADCE | MONO_OPT_ABCREM)) {
7792 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
7793 mono_local_cprop (cfg);
7794 mono_ssa_compute (cfg);
7796 if (cfg->verbose_level >= 2) {
7803 /* after SSA translation */
7807 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
7808 if (cfg->comp_done & MONO_COMP_SSA) {
7809 mono_ssa_cprop (cfg);
7811 mono_local_cprop (cfg);
7815 if (cfg->comp_done & MONO_COMP_SSA) {
7816 mono_ssa_deadce (cfg);
7818 //mono_ssa_strength_reduction (cfg);
7820 if ((cfg->flags & MONO_CFG_HAS_LDELEMA) && (cfg->opt & MONO_OPT_ABCREM))
7821 mono_perform_abc_removal (cfg);
7823 mono_ssa_remove (cfg);
7825 if (cfg->opt & MONO_OPT_BRANCH)
7826 optimize_branches (cfg);
7829 /* after SSA removal */
7833 decompose_pass (cfg);
7835 if (cfg->opt & MONO_OPT_LINEARS) {
7838 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
7839 cfg->comp_done &= ~MONO_COMP_LIVENESS;
7840 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
7841 mono_analyze_liveness (cfg);
7843 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
7844 regs = mono_arch_get_global_int_regs (cfg);
7845 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
7849 //mono_print_code (cfg);
7853 /* variables are allocated after decompose, since decompose could create temps */
7854 mono_arch_allocate_vars (cfg);
7856 if (cfg->opt & MONO_OPT_CFOLD)
7857 mono_constant_fold (cfg);
7859 mini_select_instructions (cfg);
7862 if (cfg->verbose_level >= 2) {
7863 char *id = mono_method_full_name (cfg->method, FALSE);
7864 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
7868 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
7870 jinfo->method = method;
7871 jinfo->code_start = cfg->native_code;
7872 jinfo->code_size = cfg->code_len;
7873 jinfo->used_regs = cfg->used_int_regs;
7874 jinfo->domain_neutral = (cfg->opt & MONO_OPT_SHARED) != 0;
7876 if (header->num_clauses) {
7879 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
7880 jinfo->num_clauses = header->num_clauses;
7881 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
7882 sizeof (MonoJitExceptionInfo) * header->num_clauses);
7884 for (i = 0; i < header->num_clauses; i++) {
7885 MonoExceptionClause *ec = &header->clauses [i];
7886 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
7887 MonoBasicBlock *tblock;
7889 ei->flags = ec->flags;
7891 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7892 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->token_or_filter);
7894 ei->data.filter = cfg->native_code + tblock->native_offset;
7896 ei->data.token = ec->token_or_filter;
7899 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
7901 ei->try_start = cfg->native_code + tblock->native_offset;
7902 g_assert (tblock->native_offset);
7903 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
7905 ei->try_end = cfg->native_code + tblock->native_offset;
7906 g_assert (tblock->native_offset);
7907 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
7909 ei->handler_start = cfg->native_code + tblock->native_offset;
7913 cfg->jit_info = jinfo;
7915 mono_jit_info_table_add (cfg->domain, jinfo);
7917 /* collect statistics */
7918 mono_jit_stats.allocated_code_size += cfg->code_len;
7919 code_size_ratio = cfg->code_len;
7920 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
7921 mono_jit_stats.biggest_method_size = code_size_ratio;
7922 mono_jit_stats.biggest_method = method;
7924 code_size_ratio = (code_size_ratio * 100) / ((MonoMethodNormal *)method)->header->code_size;
7925 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
7926 mono_jit_stats.max_code_size_ratio = code_size_ratio;
7927 mono_jit_stats.max_ratio_method = method;
7929 mono_jit_stats.native_code_size += cfg->code_len;
7931 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7932 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
7938 mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain)
7941 GHashTable *jit_code_hash;
7942 gpointer code = NULL;
7948 jit_code_hash = target_domain->jit_code_hash;
7950 #ifdef MONO_USE_AOT_COMPILER
7951 if (!mono_compile_aot && (opt & MONO_OPT_AOT)) {
7953 MonoDomain *domain = mono_domain_get ();
7955 mono_domain_lock (domain);
7957 mono_class_init (method->klass);
7958 if ((info = mono_aot_get_method (domain, method))) {
7959 g_hash_table_insert (domain->jit_code_hash, method, info);
7960 mono_domain_unlock (domain);
7961 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
7962 return info->code_start;
7965 mono_domain_unlock (domain);
7969 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
7970 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
7973 if (!method->addr) {
7974 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
7975 method->addr = mono_lookup_internal_call (method);
7977 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
7978 mono_lookup_pinvoke_call (method, NULL, NULL);
7980 nm = mono_marshal_get_native_wrapper (method);
7981 return mono_compile_method (nm);
7983 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
7984 //mono_debug_add_wrapper (method, nm);
7985 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
7986 const char *name = method->name;
7989 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
7990 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
7991 /* FIXME: uhm, we need a wrapper to handle exceptions? */
7992 return (gpointer)mono_delegate_ctor;
7993 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
7994 nm = mono_marshal_get_delegate_invoke (method);
7995 return mono_jit_compile_method (nm);
7996 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
7997 nm = mono_marshal_get_delegate_begin_invoke (method);
7998 return mono_jit_compile_method (nm);
7999 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
8000 nm = mono_marshal_get_delegate_end_invoke (method);
8001 return mono_jit_compile_method (nm);
8007 cfg = mini_method_compile (method, opt, target_domain, TRUE, 0);
8009 mono_domain_lock (target_domain);
8011 /* Check if some other thread already did the job. In this case, we can
8012 discard the code this thread generated. */
8014 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
8015 /* We can't use a domain specific method in another domain */
8016 if ((target_domain == mono_domain_get ()) || info->domain_neutral) {
8017 code = info->code_start;
8018 // printf("Discarding code for method %s\n", method->name);
8023 g_hash_table_insert (jit_code_hash, method, cfg->jit_info);
8024 code = cfg->native_code;
8027 mono_destroy_compile (cfg);
8029 if (target_domain->jump_target_hash) {
8030 MonoJumpInfo patch_info;
8032 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
8034 patch_info.next = NULL;
8035 patch_info.ip.i = 0;
8036 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
8037 patch_info.data.method = method;
8038 g_hash_table_remove (target_domain->jump_target_hash, method);
8040 for (tmp = list; tmp; tmp = tmp->next)
8041 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info, TRUE);
8042 g_slist_free (list);
8045 mono_domain_unlock (target_domain);
8047 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
8052 mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt)
8054 /* FIXME: later copy the code from mono */
8055 MonoDomain *target_domain, *domain = mono_domain_get ();
8059 if (opt & MONO_OPT_SHARED)
8060 target_domain = mono_get_root_domain ();
8062 target_domain = domain;
8064 mono_domain_lock (target_domain);
8066 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
8067 /* We can't use a domain specific method in another domain */
8068 if (! ((domain != target_domain) && !info->domain_neutral)) {
8069 mono_domain_unlock (target_domain);
8070 mono_jit_stats.methods_lookups++;
8071 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
8072 return info->code_start;
8076 mono_domain_unlock (target_domain);
8077 p = mono_jit_compile_method_inner (method, target_domain);
8082 mono_jit_compile_method (MonoMethod *method)
8084 return mono_jit_compile_method_with_opt (method, default_opt);
8088 mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method)
8090 MonoDomain *target_domain;
8093 if (default_opt & MONO_OPT_SHARED)
8094 target_domain = mono_get_root_domain ();
8096 target_domain = domain;
8098 mono_domain_lock (target_domain);
8100 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
8101 /* We can't use a domain specific method in another domain */
8102 if (! ((domain != target_domain) && !info->domain_neutral)) {
8103 mono_domain_unlock (target_domain);
8104 mono_jit_stats.methods_lookups++;
8105 return info->code_start;
8109 mono_domain_unlock (target_domain);
8115 * mono_jit_runtime_invoke:
8116 * @method: the method to invoke
8117 * @obj: this pointer
8118 * @params: array of parameter values.
8119 * @exc: used to catch exceptions objects
8122 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
8125 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc, void* compiled_method);
8126 void* compiled_method;
8128 if (obj == NULL && !(method->flags & METHOD_ATTRIBUTE_STATIC) && !method->string_ctor && (method->wrapper_type == 0)) {
8129 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
8133 invoke = mono_marshal_get_runtime_invoke (method);
8134 runtime_invoke = mono_jit_compile_method (invoke);
8136 /* We need this here becuase mono_marshal_get_runtime_invoke can be place
8137 * the helper method in System.Object and not the target class
8139 mono_runtime_class_init (mono_class_vtable (mono_domain_get (), method->klass));
8141 compiled_method = mono_jit_compile_method (method);
8142 return runtime_invoke (obj, params, exc, compiled_method);
8145 #ifdef PLATFORM_WIN32
8146 #define GET_CONTEXT \
8147 struct sigcontext *ctx = (struct sigcontext*)_dummy;
8150 #define GET_CONTEXT \
8151 void *ctx = context;
8152 #elif defined(sun) // Solaris x86
8153 #define GET_CONTEXT \
8154 ucontext_t *uctx = context; \
8155 struct sigcontext *ctx = (struct sigcontext *)&(uctx->uc_mcontext);
8156 #elif defined(__ppc__) || defined (__powerpc__) || defined (__s390__)
8157 #define GET_CONTEXT \
8158 void *ctx = context;
8160 #define GET_CONTEXT \
8161 void **_p = (void **)&_dummy; \
8162 struct sigcontext *ctx = (struct sigcontext *)++_p;
8166 #ifdef MONO_ARCH_USE_SIGACTION
8167 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, siginfo_t *info, void *context)
8169 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy)
8173 SIG_HANDLER_SIGNATURE (sigfpe_signal_handler)
8178 exc = mono_get_exception_divide_by_zero ();
8180 mono_arch_handle_exception (ctx, exc, FALSE);
8184 SIG_HANDLER_SIGNATURE (sigill_signal_handler)
8188 exc = mono_get_exception_execution_engine ("SIGILL");
8190 mono_arch_handle_exception (ctx, exc, FALSE);
8193 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8196 sigsegv_signal_handler (int _dummy, siginfo_t *info, void *context)
8199 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
8200 struct sigcontext *ctx = (struct sigcontext *)&(((ucontext_t*)context)->uc_mcontext);
8202 /* Can't allocate memory using Boehm GC on altstack */
8203 if (jit_tls->stack_size &&
8204 ((guint8*)info->si_addr >= (guint8*)jit_tls->end_of_stack - jit_tls->stack_size) &&
8205 ((guint8*)info->si_addr < (guint8*)jit_tls->end_of_stack))
8206 exc = mono_domain_get ()->stack_overflow_ex;
8208 exc = mono_domain_get ()->null_reference_ex;
8210 mono_arch_handle_exception (ctx, exc, FALSE);
8216 SIG_HANDLER_SIGNATURE (sigsegv_signal_handler)
8220 mono_arch_handle_exception (ctx, NULL, FALSE);
8226 SIG_HANDLER_SIGNATURE (sigusr1_signal_handler)
8228 gboolean running_managed;
8233 running_managed = (mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx)) != NULL);
8235 exc = mono_thread_request_interruption (running_managed);
8238 mono_arch_handle_exception (ctx, exc, FALSE);
8242 SIG_HANDLER_SIGNATURE (sigquit_signal_handler)
8247 exc = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
8249 mono_arch_handle_exception (ctx, exc, FALSE);
8253 SIG_HANDLER_SIGNATURE (sigint_signal_handler)
8258 exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
8260 mono_arch_handle_exception (ctx, exc, FALSE);
8263 #ifndef PLATFORM_WIN32
8265 add_signal_handler (int signo, gpointer handler)
8267 struct sigaction sa;
8269 #ifdef MONO_ARCH_USE_SIGACTION
8270 sa.sa_sigaction = handler;
8271 sigemptyset (&sa.sa_mask);
8272 sa.sa_flags = SA_SIGINFO;
8274 sa.sa_handler = handler;
8275 sigemptyset (&sa.sa_mask);
8278 g_assert (sigaction (signo, &sa, NULL) != -1);
8283 mono_runtime_install_handlers (void)
8285 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8286 struct sigaction sa;
8289 #ifdef PLATFORM_WIN32
8291 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
8292 win32_seh_set_handler(SIGILL, sigill_signal_handler);
8293 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
8294 if (getenv ("MONO_DEBUG"))
8295 win32_seh_set_handler(SIGINT, sigint_signal_handler);
8296 #else /* !PLATFORM_WIN32 */
8298 /* libpthreads has its own implementation of sigaction(),
8299 * but it seems to work well with our current exception
8300 * handlers. If not we must call syscall directly instead
8303 if (getenv ("MONO_DEBUG")) {
8304 add_signal_handler (SIGINT, sigint_signal_handler);
8307 add_signal_handler (SIGFPE, sigfpe_signal_handler);
8308 add_signal_handler (SIGQUIT, sigquit_signal_handler);
8309 add_signal_handler (SIGILL, sigill_signal_handler);
8310 add_signal_handler (SIGBUS, sigsegv_signal_handler);
8311 add_signal_handler (mono_thread_get_abort_signal (), sigusr1_signal_handler);
8314 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8315 sa.sa_sigaction = sigsegv_signal_handler;
8316 sigemptyset (&sa.sa_mask);
8317 sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
8318 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
8320 add_signal_handler (SIGSEGV, sigsegv_signal_handler);
8323 #endif /* PLATFORM_WIN32 */
8326 /* mono_jit_create_remoting_trampoline:
8327 * @method: pointer to the method info
8329 * Creates a trampoline which calls the remoting functions. This
8330 * is used in the vtable of transparent proxies.
8332 * Returns: a pointer to the newly created code
8335 mono_jit_create_remoting_trampoline (MonoMethod *method)
8338 guint8 *addr = NULL;
8340 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
8341 (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
8342 nm = mono_marshal_get_remoting_invoke (method);
8343 addr = mono_compile_method (nm);
8345 addr = mono_compile_method (method);
8351 mini_init (const char *filename)
8355 mono_arch_cpu_init ();
8357 if (!g_thread_supported ())
8358 g_thread_init (NULL);
8360 mono_jit_tls_id = TlsAlloc ();
8361 setup_jit_tls_data ((gpointer)-1, mono_thread_abort);
8363 InitializeCriticalSection (&trampoline_hash_mutex);
8367 if (default_opt & MONO_OPT_AOT)
8370 mono_runtime_install_handlers ();
8371 mono_threads_install_cleanup (mini_thread_cleanup);
8373 #define JIT_TRAMPOLINES_WORK
8374 #ifdef JIT_TRAMPOLINES_WORK
8375 mono_install_compile_method (mono_jit_compile_method);
8376 mono_install_trampoline (mono_arch_create_jit_trampoline);
8377 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
8379 #define JIT_INVOKE_WORKS
8380 #ifdef JIT_INVOKE_WORKS
8381 mono_install_runtime_invoke (mono_jit_runtime_invoke);
8382 mono_install_handler (mono_arch_get_throw_exception ());
8384 mono_install_stack_walk (mono_jit_walk_stack);
8386 domain = mono_init (filename);
8389 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
8390 ves_icall_get_frame_info);
8391 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
8392 ves_icall_get_trace);
8393 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
8394 mono_runtime_install_handlers);
8397 create_helper_signature ();
8399 #define JIT_CALLS_WORK
8400 #ifdef JIT_CALLS_WORK
8401 /* Needs to be called here since register_jit_icall depends on it */
8402 mono_marshal_init ();
8404 mono_arch_register_lowlevel_calls ();
8405 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
8406 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
8407 mono_register_jit_icall (mono_trace_enter_method, "mono_trace_enter_method", NULL, TRUE);
8408 mono_register_jit_icall (mono_trace_leave_method, "mono_trace_leave_method", NULL, TRUE);
8409 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
8410 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
8412 /* fixme: we cant handle vararg methods this way, because the signature is not constant */
8413 //mono_register_jit_icall (ves_array_element_address, "ves_array_element_address", NULL);
8414 //mono_register_jit_icall (mono_array_new_va, "mono_array_new_va", NULL);
8416 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
8417 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
8418 helper_sig_void_ptr, TRUE);
8419 mono_register_jit_icall (mono_thread_get_pending_exception, "mono_thread_get_pending_exception", helper_sig_obj_void, FALSE);
8422 * NOTE, NOTE, NOTE, NOTE:
8423 * when adding emulation for some opcodes, remember to also add a dummy
8424 * rule to the burg files, because we need the arity information to be correct.
8426 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult, TRUE);
8427 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un, FALSE);
8428 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf, FALSE);
8429 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv, FALSE);
8430 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un, FALSE);
8431 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem, FALSE);
8432 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un, FALSE);
8434 #ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
8435 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl, TRUE);
8436 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr, TRUE);
8437 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un, TRUE);
8440 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8, FALSE);
8441 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4, FALSE);
8442 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8, FALSE);
8443 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8, FALSE);
8445 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
8446 mono_register_opcode_emulation (OP_FCONV_TO_I8, "__emul_fconv_to_i8", helper_sig_long_double, mono_fconv_i8, FALSE);
8448 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
8449 mono_register_opcode_emulation (CEE_CONV_R_UN, "__emul_conv_r_un", helper_sig_double_int, mono_conv_to_r8_un, FALSE);
8451 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
8452 mono_register_opcode_emulation (OP_LCONV_TO_R8, "__emul_lconv_to_r8", helper_sig_double_long, mono_lconv_to_r8, FALSE);
8454 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
8455 mono_register_opcode_emulation (OP_LCONV_TO_R4, "__emul_lconv_to_r4", helper_sig_float_long, mono_lconv_to_r4, FALSE);
8457 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
8458 mono_register_opcode_emulation (OP_LCONV_TO_R_UN, "__emul_lconv_to_r8_un", helper_sig_double_long, mono_lconv_to_r8_un, FALSE);
8460 #ifdef MONO_ARCH_EMULATE_FREM
8461 mono_register_opcode_emulation (OP_FREM, "__emul_frem", helper_sig_double_double_double, fmod, FALSE);
8464 #if SIZEOF_VOID_P == 4
8465 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4, TRUE);
8468 #warning "fixme: add opcode emulation"
8472 /* other jit icalls */
8473 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
8474 helper_sig_ptr_ptr_ptr, FALSE);
8475 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
8476 mono_register_jit_icall (mono_get_special_static_data, "mono_get_special_static_data", helper_sig_ptr_int, FALSE);
8477 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
8478 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
8479 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
8480 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
8481 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
8482 mono_register_jit_icall (helper_stelem_ref_check, "helper_stelem_ref_check", helper_sig_stelem_ref_check, FALSE);
8483 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
8484 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
8485 mono_register_jit_icall (mono_object_new_fast, "mono_object_new_fast", helper_sig_object_new_specific, FALSE);
8486 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
8487 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
8488 mono_register_jit_icall (mono_string_to_utf16, "mono_string_to_utf16", helper_sig_ptr_obj, FALSE);
8489 mono_register_jit_icall (mono_string_from_utf16, "mono_string_from_utf16", helper_sig_obj_ptr, FALSE);
8490 mono_register_jit_icall (mono_string_new_wrapper, "mono_string_new_wrapper", helper_sig_obj_ptr, FALSE);
8491 mono_register_jit_icall (mono_string_to_utf8, "mono_string_to_utf8", helper_sig_ptr_obj, FALSE);
8492 mono_register_jit_icall (mono_string_to_bstr, "mono_string_to_bstr", helper_sig_ptr_obj, FALSE);
8493 mono_register_jit_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", helper_sig_ptr_obj, FALSE);
8494 mono_register_jit_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", helper_sig_ptr_obj, FALSE);
8495 mono_register_jit_icall (mono_string_builder_to_utf16, "mono_string_builder_to_utf16", helper_sig_ptr_obj, FALSE);
8496 mono_register_jit_icall (mono_array_to_savearray, "mono_array_to_savearray", helper_sig_ptr_obj, FALSE);
8497 mono_register_jit_icall (mono_array_to_lparray, "mono_array_to_lparray", helper_sig_ptr_obj, FALSE);
8498 mono_register_jit_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", helper_sig_ptr_obj, FALSE);
8499 mono_register_jit_icall (mono_ftnptr_to_delegate, "mono_ftnptr_to_delegate", helper_sig_obj_ptr_ptr, FALSE);
8500 mono_register_jit_icall (mono_marshal_string_array, "mono_marshal_string_array", helper_sig_ptr_obj, FALSE);
8501 mono_register_jit_icall (mono_marshal_string_array_to_unicode, "mono_marshal_string_array_to_unicode", helper_sig_ptr_obj, FALSE);
8502 mono_register_jit_icall (mono_marshal_asany, "mono_marshal_asany", helper_sig_ptr_obj_int, FALSE);
8503 mono_register_jit_icall (mono_marshal_free_asany, "mono_marshal_free_asany", helper_sig_void_obj_ptr_int, FALSE);
8504 mono_register_jit_icall (mono_string_utf8_to_builder, "mono_string_utf8_to_builder", helper_sig_void_ptr_ptr, FALSE);
8505 mono_register_jit_icall (mono_string_utf16_to_builder, "mono_string_utf16_to_builder", helper_sig_void_ptr_ptr, FALSE);
8506 mono_register_jit_icall (mono_marshal_free_array, "mono_marshal_free_array", helper_sig_void_ptr_ptr, FALSE);
8507 mono_register_jit_icall (mono_string_to_byvalstr, "mono_string_to_byvalstr", helper_sig_void_ptr_ptr_ptr, FALSE);
8508 mono_register_jit_icall (mono_string_to_byvalwstr, "mono_string_to_byvalwstr", helper_sig_void_ptr_ptr_ptr, FALSE);
8509 mono_register_jit_icall (g_free, "g_free", helper_sig_void_ptr, FALSE);
8510 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
8511 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
8512 mono_register_jit_icall (mono_ldftn_nosync, "mono_ldftn_nosync", helper_sig_compile, FALSE);
8513 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
8516 #define JIT_RUNTIME_WORKS
8517 #ifdef JIT_RUNTIME_WORKS
8518 mono_runtime_install_cleanup ((MonoDomainFunc)mini_cleanup);
8519 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
8522 mono_thread_attach (domain);
8526 MonoJitStats mono_jit_stats = {0};
8529 print_jit_stats (void)
8531 if (mono_jit_stats.enabled) {
8532 g_print ("Mono Jit statistics\n");
8533 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
8534 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
8535 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
8536 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
8537 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
8538 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
8539 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
8540 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
8541 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
8542 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
8543 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
8544 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
8545 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
8546 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
8547 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
8548 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
8549 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
8550 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
8552 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
8553 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
8554 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
8555 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
8556 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
8558 g_print ("\nGeneric instances: %ld\n", mono_stats.generic_instance_count);
8559 g_print ("Inflated methods: %ld\n", mono_stats.inflated_method_count);
8560 g_print ("Inflated types: %ld\n", mono_stats.inflated_type_count);
8561 g_print ("Generics metadata size: %ld\n", mono_stats.generics_metadata_size);
8566 mini_cleanup (MonoDomain *domain)
8569 * mono_runtime_cleanup() and mono_domain_finalize () need to
8570 * be called early since they need the execution engine still
8571 * fully working (mono_domain_finalize may invoke managed finalizers
8572 * and mono_runtime_cleanup will wait for other threads to finish).
8574 mono_domain_finalize (domain, 2000);
8576 mono_runtime_cleanup (domain);
8578 mono_profiler_shutdown ();
8580 mono_debug_cleanup ();
8582 #ifdef PLATFORM_WIN32
8583 win32_seh_cleanup();
8586 mono_domain_free (domain, TRUE);
8592 mono_set_defaults (int verbose_level, guint32 opts)
8594 mini_verbose = verbose_level;
8599 mono_precompile_assembly (MonoAssembly *ass, void *user_data)
8601 MonoImage *image = mono_assembly_get_image (ass);
8602 MonoMethod *method, *invoke;
8605 if (mini_verbose > 0)
8606 printf ("PRECOMPILE: %s.\n", mono_image_get_filename (image));
8608 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
8609 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
8610 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
8614 if (mini_verbose > 1) {
8615 char * desc = mono_method_full_name (method, TRUE);
8616 g_print ("Compiling %d %s\n", count, desc);
8619 mono_compile_method (method);
8620 if (strcmp (method->name, "Finalize") == 0) {
8621 invoke = mono_marshal_get_runtime_invoke (method);
8622 mono_compile_method (invoke);
8624 if (method->klass->marshalbyref && method->signature->hasthis) {
8625 invoke = mono_marshal_get_remoting_invoke_with_check (method);
8626 mono_compile_method (invoke);
8631 void mono_precompile_assemblies ()
8633 mono_assembly_foreach ((GFunc)mono_precompile_assembly, NULL);