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;
1590 if (cfg->verbose_level > 3)
1591 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
1592 if (!bb->out_scount) {
1594 bb->out_scount = count;
1595 //g_print ("bblock %d has out:", bb->block_num);
1596 for (i = 0; i < bb->out_count; ++i) {
1597 outb = bb->out_bb [i];
1598 //g_print (" %d", outb->block_num);
1599 if (outb->in_stack) {
1601 bb->out_stack = outb->in_stack;
1607 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1608 for (i = 0; i < count; ++i) {
1609 /* see bug#58863, but removing this code causes regressions in gtk-sharp build
1610 * (SEGV running Method::Initialize() in gapi_codegen.exe)
1613 /* try to reuse temps already allocated for this purpouse, if they occupy the same
1614 * stack slot and if they are of the same type. */
1615 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1617 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1622 locals = bb->out_stack;
1623 for (i = 0; i < count; ++i) {
1624 /* add store ops at the end of the bb, before the branch */
1625 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1626 if (inst->opcode == CEE_STOBJ) {
1627 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
1628 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE);
1630 inst->cil_code = sp [i]->cil_code;
1631 mono_add_ins_to_end (bb, inst);
1633 if (cfg->verbose_level > 3)
1634 g_print ("storing %d to temp %d\n", i, locals [i]->inst_c0);
1637 for (i = 0; i < bb->out_count; ++i) {
1638 outb = bb->out_bb [i];
1639 if (outb->in_scount)
1640 continue; /* check they are the same locals */
1641 outb->in_scount = count;
1642 outb->in_stack = locals;
1648 ret_type_to_call_opcode (MonoType *type, int calli, int virt)
1653 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1657 case MONO_TYPE_VOID:
1658 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
1661 case MONO_TYPE_BOOLEAN:
1664 case MONO_TYPE_CHAR:
1667 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1671 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1672 case MONO_TYPE_CLASS:
1673 case MONO_TYPE_STRING:
1674 case MONO_TYPE_OBJECT:
1675 case MONO_TYPE_SZARRAY:
1676 case MONO_TYPE_ARRAY:
1677 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1680 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
1683 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
1684 case MONO_TYPE_VALUETYPE:
1685 if (type->data.klass->enumtype) {
1686 t = type->data.klass->enum_basetype->type;
1689 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1690 case MONO_TYPE_TYPEDBYREF:
1691 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1692 case MONO_TYPE_GENERICINST:
1693 t = type->data.generic_inst->generic_type->type;
1696 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
1702 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
1704 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
1706 ji->ip.label = label;
1707 ji->type = MONO_PATCH_INFO_SWITCH;
1708 ji->data.table = bbs;
1709 ji->next = cfg->patch_info;
1710 ji->table_size = num_blocks;
1711 cfg->patch_info = ji;
1715 * When we add a tree of instructions, we need to ensure the instructions currently
1716 * on the stack are executed before (like, if we load a value from a local).
1717 * We ensure this by saving the currently loaded values to temps and rewriting the
1718 * instructions to load the values.
1719 * This is not done for opcodes that terminate a basic block (because it's handled already
1720 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
1723 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
1725 MonoInst *load, *store, *temp, *ins;
1727 while (stack < sp) {
1729 /* handle also other constants */
1730 if (ins->opcode != OP_ICONST) {
1731 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1732 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1733 store->cil_code = ins->cil_code;
1734 if (store->opcode == CEE_STOBJ) {
1735 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
1736 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE);
1738 MONO_ADD_INS (bblock, store);
1739 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1740 load->cil_code = ins->cil_code;
1748 * Prepare arguments for passing to a function call.
1749 * Return a non-zero value if the arguments can't be passed to the given
1751 * The type checks are not yet complete and some conversions may need
1752 * casts on 32 or 64 bit architectures.
1755 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
1760 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
1764 for (i = 0; i < sig->param_count; ++i) {
1765 if (sig->params [i]->byref) {
1767 * check the result of ldelema is only passed as an argument if the byref
1768 * type matches exactly the array element type.
1769 * FIXME: if the argument as been saved on the stack as part of the
1770 * interface variable code (the value was on the stack at a basic block boundary)
1771 * we need to add the check in that case, too.
1773 if (args [i]->opcode == CEE_LDELEMA) {
1775 MonoClass *exact_class = mono_class_from_mono_type (sig->params [i]);
1776 if (!exact_class->valuetype) {
1777 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
1778 check->cil_code = args [i]->cil_code;
1779 check->klass = exact_class;
1780 check->inst_left = args [i]->inst_left;
1781 check->type = STACK_OBJ;
1782 args [i]->inst_left = check;
1785 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
1789 simple_type = sig->params [i]->type;
1791 switch (simple_type) {
1792 case MONO_TYPE_VOID:
1797 case MONO_TYPE_BOOLEAN:
1800 case MONO_TYPE_CHAR:
1803 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
1809 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
1812 case MONO_TYPE_CLASS:
1813 case MONO_TYPE_STRING:
1814 case MONO_TYPE_OBJECT:
1815 case MONO_TYPE_SZARRAY:
1816 case MONO_TYPE_ARRAY:
1817 if (args [i]->type != STACK_OBJ)
1822 if (args [i]->type != STACK_I8)
1827 if (args [i]->type != STACK_R8)
1830 case MONO_TYPE_VALUETYPE:
1831 if (sig->params [i]->data.klass->enumtype) {
1832 simple_type = sig->params [i]->data.klass->enum_basetype->type;
1835 if (args [i]->type != STACK_VTYPE)
1838 case MONO_TYPE_TYPEDBYREF:
1839 if (args [i]->type != STACK_VTYPE)
1842 case MONO_TYPE_GENERICINST:
1843 simple_type = sig->params [i]->data.generic_inst->generic_type->type;
1847 g_error ("unknown type 0x%02x in check_call_signature", simple_type);
1854 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
1855 const guint8 *ip, gboolean to_end)
1857 MonoInst *temp, *store, *ins = (MonoInst*)call;
1858 MonoType *ret = sig->ret;
1860 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
1862 call->inst.type = STACK_OBJ;
1863 call->inst.opcode = CEE_CALL;
1864 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
1866 type_to_eval_stack_type (ret, ins);
1867 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
1870 if (MONO_TYPE_ISSTRUCT (ret)) {
1873 /* we use this to allocate native sized structs */
1874 temp->unused = sig->pinvoke;
1876 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
1877 if (call->inst.opcode == OP_VCALL)
1878 ins->inst_left = loada;
1880 ins->inst_right = loada; /* a virtual or indirect call */
1883 mono_add_ins_to_end (bblock, ins);
1885 MONO_ADD_INS (bblock, ins);
1887 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1888 store->cil_code = ip;
1890 mono_add_ins_to_end (bblock, store);
1892 MONO_ADD_INS (bblock, store);
1894 return temp->inst_c0;
1897 mono_add_ins_to_end (bblock, ins);
1899 MONO_ADD_INS (bblock, ins);
1904 inline static MonoCallInst *
1905 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1906 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
1911 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual));
1913 call->inst.cil_code = ip;
1915 call->signature = sig;
1916 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
1918 for (arg = call->out_args; arg;) {
1919 MonoInst *narg = arg->next;
1924 mono_add_ins_to_end (bblock, arg);
1926 MONO_ADD_INS (bblock, arg);
1933 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1934 MonoInst **args, MonoInst *addr, const guint8 *ip)
1936 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
1938 call->inst.inst_i0 = addr;
1940 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
1943 static MonoCallInst*
1944 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
1945 MonoInst **args, const guint8 *ip, MonoInst *this)
1947 gboolean virtual = this != NULL;
1950 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, FALSE);
1952 if (this && sig->hasthis &&
1953 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
1954 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
1955 call->method = mono_marshal_get_remoting_invoke_with_check (method);
1957 call->method = method;
1959 call->inst.flags |= MONO_INST_HAS_METHOD;
1960 call->inst.inst_left = this;
1966 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
1967 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
1969 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, signature, args, ip, this);
1971 return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
1975 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
1976 MonoInst **args, const guint8 *ip, gboolean to_end)
1982 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
1984 return mono_spill_call (cfg, bblock, call, sig, func == mono_array_new_va, ip, to_end);
1988 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
1990 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
1993 g_warning ("unregistered JIT ICall");
1994 g_assert_not_reached ();
1997 return mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, args, ip, FALSE);
2001 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
2003 MonoInst *ins, *temp = NULL, *store, *load, *begin;
2004 MonoInst *last_arg = NULL;
2008 //g_print ("emulating: ");
2009 //mono_print_tree_nl (tree);
2010 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE));
2011 ins = (MonoInst*)call;
2013 call->inst.cil_code = tree->cil_code;
2015 call->signature = info->sig;
2017 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
2019 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2020 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
2021 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2022 store->cil_code = tree->cil_code;
2027 nargs = info->sig->param_count + info->sig->hasthis;
2029 for (last_arg = call->out_args; last_arg && last_arg->next; last_arg = last_arg->next) ;
2032 last_arg->next = store;
2035 begin = call->out_args;
2039 if (cfg->prev_ins) {
2041 * This assumes that that in a tree, emulate_opcode is called for a
2042 * node before it is called for its children. dec_foreach needs to
2043 * take this into account.
2045 store->next = cfg->prev_ins->next;
2046 cfg->prev_ins->next = begin;
2048 store->next = cfg->cbb->code;
2049 cfg->cbb->code = begin;
2052 call->fptr = mono_icall_get_wrapper (info);
2054 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2055 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2060 static MonoMethodSignature *
2061 mono_get_element_address_signature (int arity)
2063 static GHashTable *sighash = NULL;
2064 MonoMethodSignature *res;
2067 EnterCriticalSection (&trampoline_hash_mutex);
2069 sighash = g_hash_table_new (NULL, NULL);
2071 else if ((res = g_hash_table_lookup (sighash, (gpointer)arity))) {
2072 LeaveCriticalSection (&trampoline_hash_mutex);
2076 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
2079 res->params [0] = &mono_defaults.array_class->byval_arg;
2081 for (i = 1; i <= arity; i++)
2082 res->params [i] = &mono_defaults.int_class->byval_arg;
2084 res->ret = &mono_defaults.int_class->byval_arg;
2086 g_hash_table_insert (sighash, (gpointer)arity, res);
2087 LeaveCriticalSection (&trampoline_hash_mutex);
2093 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native) {
2094 MonoInst *iargs [3];
2100 * This check breaks with spilled vars... need to handle it during verification anyway.
2101 * g_assert (klass && klass == src->klass && klass == dest->klass);
2105 n = mono_class_native_size (klass, &align);
2107 n = mono_class_value_size (klass, &align);
2109 if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
2111 MONO_INST_NEW (cfg, inst, OP_MEMCPY);
2112 inst->inst_left = dest;
2113 inst->inst_right = src;
2114 inst->cil_code = ip;
2116 MONO_ADD_INS (bblock, inst);
2121 NEW_ICONST (cfg, iargs [2], n);
2123 mono_emit_native_call (cfg, bblock, helper_memcpy, helper_sig_memcpy, iargs, ip, to_end);
2127 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
2129 MonoInst *iargs [2];
2130 MonoInst *ins, *zero_int32;
2133 NEW_ICONST (cfg, zero_int32, 0);
2135 mono_class_init (klass);
2136 n = mono_class_value_size (klass, NULL);
2137 MONO_INST_NEW (cfg, ins, 0);
2139 ins->inst_left = dest;
2140 ins->inst_right = zero_int32;
2143 ins->opcode = CEE_STIND_I1;
2144 MONO_ADD_INS (bblock, ins);
2147 ins->opcode = CEE_STIND_I2;
2148 MONO_ADD_INS (bblock, ins);
2151 ins->opcode = CEE_STIND_I4;
2152 MONO_ADD_INS (bblock, ins);
2155 if (n <= sizeof (gpointer) * 5) {
2156 ins->opcode = OP_MEMSET;
2159 MONO_ADD_INS (bblock, ins);
2162 handle_loaded_temps (cfg, bblock, stack_start, sp);
2163 NEW_ICONST (cfg, ins, n);
2166 mono_emit_jit_icall (cfg, bblock, helper_initobj, iargs, ip);
2172 handle_box (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *val, const guchar *ip, MonoClass *klass)
2174 MonoInst *iargs [2];
2175 MonoInst *dest, *vtoffset, *add, *vstore;
2178 /* much like NEWOBJ */
2179 if (cfg->opt & MONO_OPT_SHARED) {
2180 NEW_DOMAINCONST (cfg, iargs [0]);
2181 NEW_CLASSCONST (cfg, iargs [1], klass);
2183 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
2185 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
2186 NEW_VTABLECONST (cfg, iargs [0], vtable);
2187 if (klass->has_finalize || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
2188 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
2190 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
2192 NEW_TEMPLOAD (cfg, dest, temp);
2193 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
2194 MONO_INST_NEW (cfg, add, CEE_ADD);
2195 add->inst_left = dest;
2196 add->inst_right = vtoffset;
2199 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
2200 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
2201 vstore->cil_code = ip;
2202 vstore->inst_left = add;
2203 vstore->inst_right = val;
2205 if (vstore->opcode == CEE_STOBJ) {
2206 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
2208 MONO_ADD_INS (bblock, vstore);
2210 NEW_TEMPLOAD (cfg, dest, temp);
2214 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
2217 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
2219 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
2220 MonoMethodSignature *signature = method->signature;
2224 #ifdef MONO_ARCH_HAVE_LMF_OPS
2225 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
2226 !MONO_TYPE_ISSTRUCT (signature->ret) && (method->klass->parent != mono_defaults.array_class))
2230 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2231 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2232 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
2233 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
2234 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2235 (method->klass->marshalbyref) ||
2236 !header || header->num_clauses ||
2237 /* fixme: why cant we inline valuetype returns? */
2238 MONO_TYPE_ISSTRUCT (signature->ret))
2241 /* its not worth to inline methods with valuetype arguments?? */
2242 for (i = 0; i < signature->param_count; i++) {
2243 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
2249 * if we can initialize the class of the method right away, we do,
2250 * otherwise we don't allow inlining if the class needs initialization,
2251 * since it would mean inserting a call to mono_runtime_class_init()
2252 * inside the inlined code
2254 if (!(cfg->opt & MONO_OPT_SHARED)) {
2255 vtable = mono_class_vtable (cfg->domain, method->klass);
2256 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
2257 if (cfg->run_cctors)
2258 mono_runtime_class_init (vtable);
2260 else if (!vtable->initialized && mono_class_needs_cctor_run (method->klass, NULL))
2264 * If we're compiling for shared code
2265 * the cctor will need to be run at aot method load time, for example,
2266 * or at the end of the compilation of the inlining method.
2268 if (mono_class_needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
2271 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
2273 /* also consider num_locals? */
2274 if (getenv ("MONO_INLINELIMIT"))
2275 if (header->code_size < atoi (getenv ("MONO_INLINELIMIT"))) {
2279 if (header->code_size < 20)
2286 mini_get_ldelema_ins (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
2290 MonoMethodSignature *esig;
2291 char icall_name [256];
2292 MonoJitICallInfo *info;
2294 rank = cmethod->signature->param_count - (is_set? 1: 0);
2296 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
2298 NEW_GROUP (cfg, indexes, sp [1], sp [2]);
2299 MONO_INST_NEW (cfg, addr, OP_LDELEMA2D);
2300 addr->inst_left = sp [0];
2301 addr->inst_right = indexes;
2302 addr->cil_code = ip;
2303 addr->type = STACK_MP;
2304 addr->klass = cmethod->klass;
2308 /* Need to register the icall so it gets an icall wrapper */
2309 sprintf (icall_name, "ves_array_element_address_%d", rank);
2311 info = mono_find_jit_icall_by_name (icall_name);
2313 esig = mono_get_element_address_signature (rank);
2314 info = mono_register_jit_icall (ves_array_element_address, g_strdup (icall_name), esig, FALSE);
2317 temp = mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, sp, ip, FALSE);
2318 cfg->flags |= MONO_CFG_HAS_VARARGS;
2320 NEW_TEMPLOAD (cfg, addr, temp);
2324 static MonoJitICallInfo **emul_opcode_map = NULL;
2326 static inline MonoJitICallInfo *
2327 mono_find_jit_opcode_emulation (int opcode)
2329 if (emul_opcode_map)
2330 return emul_opcode_map [opcode];
2336 mini_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
2341 if (cmethod->klass == mono_defaults.string_class) {
2342 if (cmethod->name [0] != 'g')
2345 if (strcmp (cmethod->name, "get_Chars") == 0)
2347 else if (strcmp (cmethod->name, "get_Length") == 0)
2350 } else if (cmethod->klass == mono_defaults.object_class) {
2351 if (strcmp (cmethod->name, "GetType") == 0)
2355 } else if (cmethod->klass == mono_defaults.array_class) {
2356 if (strcmp (cmethod->name, "get_Rank") == 0)
2358 else if (strcmp (cmethod->name, "get_Length") == 0)
2363 op = mono_arch_get_opcode_for_method (cfg, cmethod, fsig, args);
2367 pc = fsig->param_count + fsig->hasthis;
2368 MONO_INST_NEW (cfg, ins, op);
2371 ins->inst_i0 = args [0];
2373 ins->inst_i1 = args [1];
2380 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
2382 MonoInst *store, *temp;
2385 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
2387 if (!sig->hasthis && sig->param_count == 0)
2391 if (sp [0]->opcode == OP_ICONST) {
2394 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
2396 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2397 store->cil_code = sp [0]->cil_code;
2398 MONO_ADD_INS (bblock, store);
2403 for (i = 0; i < sig->param_count; ++i) {
2404 if (sp [0]->opcode == OP_ICONST) {
2407 temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
2409 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2410 store->cil_code = sp [0]->cil_code;
2411 if (store->opcode == CEE_STOBJ) {
2412 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2413 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE);
2415 MONO_ADD_INS (bblock, store);
2423 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
2424 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b, gboolean inline_allways)
2426 MonoInst *ins, *rvar = NULL;
2427 MonoMethodHeader *cheader;
2428 MonoBasicBlock *ebblock, *sbblock;
2429 int i, costs, new_locals_offset;
2431 if (cfg->verbose_level > 2)
2432 g_print ("INLINE START %p %s -> %s\n", cmethod, mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2434 if (!cmethod->inline_info) {
2435 mono_jit_stats.inlineable_methods++;
2436 cmethod->inline_info = 1;
2438 /* allocate space to store the return value */
2439 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2440 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2443 /* allocate local variables */
2444 cheader = ((MonoMethodNormal *)cmethod)->header;
2445 new_locals_offset = cfg->num_varinfo;
2446 for (i = 0; i < cheader->num_locals; ++i)
2447 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
2449 /* allocate starte and end blocks */
2450 sbblock = NEW_BBLOCK (cfg);
2451 sbblock->block_num = cfg->num_bblocks++;
2452 sbblock->real_offset = real_offset;
2454 ebblock = NEW_BBLOCK (cfg);
2455 ebblock->block_num = cfg->num_bblocks++;
2456 ebblock->real_offset = real_offset;
2458 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
2460 if ((costs >= 0 && costs < 60) || inline_allways) {
2461 if (cfg->verbose_level > 2)
2462 g_print ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2464 mono_jit_stats.inlined_methods++;
2466 /* always add some code to avoid block split failures */
2467 MONO_INST_NEW (cfg, ins, CEE_NOP);
2468 MONO_ADD_INS (bblock, ins);
2471 bblock->next_bb = sbblock;
2472 link_bblock (cfg, bblock, sbblock);
2475 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
2481 if (cfg->verbose_level > 2)
2482 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
2488 * Some of these comments may well be out-of-date.
2489 * Design decisions: we do a single pass over the IL code (and we do bblock
2490 * splitting/merging in the few cases when it's required: a back jump to an IL
2491 * address that was not already seen as bblock starting point).
2492 * Code is validated as we go (full verification is still better left to metadata/verify.c).
2493 * Complex operations are decomposed in simpler ones right away. We need to let the
2494 * arch-specific code peek and poke inside this process somehow (except when the
2495 * optimizations can take advantage of the full semantic info of coarse opcodes).
2496 * All the opcodes of the form opcode.s are 'normalized' to opcode.
2497 * MonoInst->opcode initially is the IL opcode or some simplification of that
2498 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
2499 * opcode with value bigger than OP_LAST.
2500 * At this point the IR can be handed over to an interpreter, a dumb code generator
2501 * or to the optimizing code generator that will translate it to SSA form.
2503 * Profiling directed optimizations.
2504 * We may compile by default with few or no optimizations and instrument the code
2505 * or the user may indicate what methods to optimize the most either in a config file
2506 * or through repeated runs where the compiler applies offline the optimizations to
2507 * each method and then decides if it was worth it.
2510 * * consider using an array instead of an hash table (bb_hash)
2513 #define CHECK_TYPE(ins) if (!(ins)->type) goto unverified
2514 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) goto unverified
2515 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) goto unverified
2516 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) goto unverified
2517 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) goto unverified
2518 #define CHECK_OPSIZE(size) if (ip + size > end) goto unverified
2521 /* offset from br.s -> br like opcodes */
2522 #define BIG_BRANCH_OFFSET 13
2525 get_basic_blocks (MonoCompile *cfg, GHashTable *bbhash, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
2527 unsigned char *ip = start;
2528 unsigned char *target;
2531 MonoBasicBlock *bblock;
2532 const MonoOpcode *opcode;
2535 cli_addr = ip - start;
2536 i = mono_opcode_value ((const guint8 **)&ip, end);
2539 opcode = &mono_opcodes [i];
2540 switch (opcode->argument) {
2541 case MonoInlineNone:
2544 case MonoInlineString:
2545 case MonoInlineType:
2546 case MonoInlineField:
2547 case MonoInlineMethod:
2550 case MonoShortInlineR:
2557 case MonoShortInlineVar:
2558 case MonoShortInlineI:
2561 case MonoShortInlineBrTarget:
2562 target = start + cli_addr + 2 + (signed char)ip [1];
2563 GET_BBLOCK (cfg, bbhash, bblock, target);
2566 case MonoInlineBrTarget:
2567 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
2568 GET_BBLOCK (cfg, bbhash, bblock, target);
2571 case MonoInlineSwitch: {
2572 guint32 n = read32 (ip + 1);
2575 cli_addr += 5 + 4 * n;
2576 target = start + cli_addr;
2577 GET_BBLOCK (cfg, bbhash, bblock, target);
2579 for (j = 0; j < n; ++j) {
2580 target = start + cli_addr + (gint32)read32 (ip);
2581 GET_BBLOCK (cfg, bbhash, bblock, target);
2591 g_assert_not_reached ();
2601 emit_tree (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *ins)
2603 MonoInst *store, *temp, *load;
2604 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
2605 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2606 store->cil_code = ins->cil_code;
2607 MONO_ADD_INS (bblock, store);
2608 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2609 load->cil_code = ins->cil_code;
2614 * mono_method_to_ir: translates IL into basic blocks containing trees
2617 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
2618 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
2619 guint inline_offset, gboolean is_virtual_call)
2621 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
2622 MonoInst *ins, **sp, **stack_start;
2623 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
2625 MonoMethod *cmethod;
2626 MonoInst **arg_array;
2627 MonoMethodHeader *header;
2629 guint32 token, ins_flag;
2631 MonoClass *constrained_call = NULL;
2632 unsigned char *ip, *end, *target, *err_pos;
2633 static double r8_0 = 0.0;
2634 MonoMethodSignature *sig;
2635 MonoGenericContext *generic_context = NULL;
2636 MonoType **param_types;
2637 GList *bb_recheck = NULL, *tmp;
2638 int i, n, start_new_bblock, align;
2639 int num_calls = 0, inline_costs = 0;
2640 int *filter_lengths = NULL;
2641 int breakpoint_id = 0;
2642 guint real_offset, num_args;
2644 image = method->klass->image;
2645 header = ((MonoMethodNormal *)method)->header;
2646 sig = method->signature;
2647 num_args = sig->hasthis + sig->param_count;
2648 ip = (unsigned char*)header->code;
2649 end = ip + header->code_size;
2650 mono_jit_stats.cil_code_size += header->code_size;
2652 if (method->signature->is_inflated)
2653 generic_context = ((MonoMethodInflated *) method)->context;
2655 if (cfg->method == method) {
2657 bbhash = cfg->bb_hash;
2659 real_offset = inline_offset;
2660 bbhash = g_hash_table_new (g_direct_hash, NULL);
2663 if (cfg->verbose_level > 2)
2664 g_print ("method to IR %s\n", mono_method_full_name (method, TRUE));
2666 dont_inline = g_list_prepend (dont_inline, method);
2667 if (cfg->method == method) {
2669 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
2670 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
2673 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
2674 start_bblock->cil_code = NULL;
2675 start_bblock->cil_length = 0;
2676 start_bblock->block_num = cfg->num_bblocks++;
2679 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
2680 end_bblock->cil_code = NULL;
2681 end_bblock->cil_length = 0;
2682 end_bblock->block_num = cfg->num_bblocks++;
2683 g_assert (cfg->num_bblocks == 2);
2685 arg_array = alloca (sizeof (MonoInst *) * num_args);
2686 for (i = num_args - 1; i >= 0; i--)
2687 arg_array [i] = cfg->varinfo [i];
2689 if (header->num_clauses) {
2690 int size = sizeof (int) * header->num_clauses;
2691 filter_lengths = alloca (size);
2692 memset (filter_lengths, 0, size);
2694 cfg->spvars = g_hash_table_new (NULL, NULL);
2696 /* handle exception clauses */
2697 for (i = 0; i < header->num_clauses; ++i) {
2698 //unsigned char *p = ip;
2699 MonoExceptionClause *clause = &header->clauses [i];
2700 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->try_offset);
2701 tblock->real_offset = clause->try_offset;
2702 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
2703 tblock->real_offset = clause->handler_offset;
2705 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
2706 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2707 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2708 MONO_ADD_INS (tblock, ins);
2711 /*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);
2713 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
2715 /* catch and filter blocks get the exception object on the stack */
2716 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
2717 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2718 /* mostly like handle_stack_args (), but just sets the input args */
2719 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
2721 cfg->exvar = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
2722 /* prevent it from being register allocated */
2723 cfg->exvar->flags |= MONO_INST_INDIRECT;
2725 tblock->in_scount = 1;
2726 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2727 tblock->in_stack [0] = cfg->exvar;
2729 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2730 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->token_or_filter);
2731 tblock->real_offset = clause->token_or_filter;
2732 tblock->in_scount = 1;
2733 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2734 tblock->in_stack [0] = cfg->exvar;
2735 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2736 MONO_ADD_INS (tblock, ins);
2742 arg_array = alloca (sizeof (MonoInst *) * num_args);
2743 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
2746 /* FIRST CODE BLOCK */
2747 bblock = NEW_BBLOCK (cfg);
2748 bblock->cil_code = ip;
2750 ADD_BBLOCK (cfg, bbhash, bblock);
2752 if (cfg->method == method) {
2753 breakpoint_id = mono_debugger_method_has_breakpoint (method);
2754 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
2755 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2756 MONO_ADD_INS (bblock, ins);
2760 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED))) || mono_compile_aot) {
2761 /* we use a separate basic block for the initialization code */
2762 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
2763 init_localsbb->real_offset = real_offset;
2764 start_bblock->next_bb = init_localsbb;
2765 init_localsbb->next_bb = bblock;
2766 link_bblock (cfg, start_bblock, init_localsbb);
2767 link_bblock (cfg, init_localsbb, bblock);
2768 init_localsbb->block_num = cfg->num_bblocks++;
2770 start_bblock->next_bb = bblock;
2771 link_bblock (cfg, start_bblock, bblock);
2774 if (get_basic_blocks (cfg, bbhash, header, real_offset, ip, end, &err_pos)) {
2779 mono_debug_init_method (cfg, bblock, breakpoint_id);
2781 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
2783 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
2784 for (n = 0; n < sig->param_count; ++n)
2785 param_types [n + sig->hasthis] = sig->params [n];
2787 /* do this somewhere outside - not here */
2788 NEW_ICONST (cfg, zero_int32, 0);
2789 NEW_ICONST (cfg, zero_int64, 0);
2790 zero_int64->type = STACK_I8;
2791 NEW_PCONST (cfg, zero_ptr, 0);
2792 NEW_PCONST (cfg, zero_obj, 0);
2793 zero_obj->type = STACK_OBJ;
2795 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
2796 zero_r8->type = STACK_R8;
2797 zero_r8->inst_p0 = &r8_0;
2799 /* add a check for this != NULL to inlined methods */
2800 if (is_virtual_call) {
2801 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
2802 NEW_ARGLOAD (cfg, ins->inst_left, 0);
2804 MONO_ADD_INS (bblock, ins);
2807 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
2808 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
2811 start_new_bblock = 0;
2814 if (cfg->method == method)
2815 real_offset = ip - header->code;
2817 real_offset = inline_offset;
2819 if (start_new_bblock) {
2820 bblock->cil_length = ip - bblock->cil_code;
2821 if (start_new_bblock == 2) {
2822 g_assert (ip == tblock->cil_code);
2824 GET_BBLOCK (cfg, bbhash, tblock, ip);
2826 bblock->next_bb = tblock;
2828 start_new_bblock = 0;
2829 for (i = 0; i < bblock->in_scount; ++i) {
2830 if (cfg->verbose_level > 3)
2831 g_print ("loading %d from temp %d\n", i, bblock->in_stack [i]->inst_c0);
2832 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2836 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
2837 link_bblock (cfg, bblock, tblock);
2838 if (sp != stack_start) {
2839 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2842 bblock->next_bb = tblock;
2844 for (i = 0; i < bblock->in_scount; ++i) {
2845 if (cfg->verbose_level > 3)
2846 g_print ("loading %d from temp %d\n", i, bblock->in_stack [i]->inst_c0);
2847 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2853 bblock->real_offset = real_offset;
2855 if ((cfg->method == method) && cfg->coverage_info) {
2856 MonoInst *store, *one;
2857 guint32 cil_offset = ip - header->code;
2858 cfg->coverage_info->data [cil_offset].cil_code = ip;
2860 /* TODO: Use an increment here */
2861 NEW_ICONST (cfg, one, 1);
2864 NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
2867 MONO_INST_NEW (cfg, store, CEE_STIND_I);
2868 store->cil_code = ip;
2869 store->inst_left = ins;
2870 store->inst_right = one;
2872 MONO_ADD_INS (bblock, store);
2875 if (cfg->verbose_level > 3)
2876 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, sp-stack_start, mono_disasm_code_one (NULL, method, ip, NULL));
2883 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2884 ins->cil_code = ip++;
2885 MONO_ADD_INS (bblock, ins);
2891 CHECK_STACK_OVF (1);
2892 n = (*ip)-CEE_LDARG_0;
2894 NEW_ARGLOAD (cfg, ins, n);
2895 ins->cil_code = ip++;
2902 CHECK_STACK_OVF (1);
2903 n = (*ip)-CEE_LDLOC_0;
2905 NEW_LOCLOAD (cfg, ins, n);
2906 ins->cil_code = ip++;
2914 n = (*ip)-CEE_STLOC_0;
2917 handle_loaded_temps (cfg, bblock, stack_start, sp);
2918 NEW_LOCSTORE (cfg, ins, n, *sp);
2920 if (ins->opcode == CEE_STOBJ) {
2921 NEW_LOCLOADA (cfg, ins, n);
2922 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2924 MONO_ADD_INS (bblock, ins);
2930 CHECK_STACK_OVF (1);
2932 NEW_ARGLOAD (cfg, ins, ip [1]);
2939 CHECK_STACK_OVF (1);
2941 NEW_ARGLOADA (cfg, ins, ip [1]);
2951 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
2952 handle_loaded_temps (cfg, bblock, stack_start, sp);
2954 if (ins->opcode == CEE_STOBJ) {
2955 NEW_ARGLOADA (cfg, ins, ip [1]);
2956 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2958 MONO_ADD_INS (bblock, ins);
2963 CHECK_STACK_OVF (1);
2964 CHECK_LOCAL (ip [1]);
2965 NEW_LOCLOAD (cfg, ins, ip [1]);
2972 CHECK_STACK_OVF (1);
2973 CHECK_LOCAL (ip [1]);
2974 NEW_LOCLOADA (cfg, ins, ip [1]);
2983 handle_loaded_temps (cfg, bblock, stack_start, sp);
2984 CHECK_LOCAL (ip [1]);
2985 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
2987 if (ins->opcode == CEE_STOBJ) {
2988 NEW_LOCLOADA (cfg, ins, ip [1]);
2989 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2991 MONO_ADD_INS (bblock, ins);
2996 CHECK_STACK_OVF (1);
2997 NEW_PCONST (cfg, ins, NULL);
2999 ins->type = STACK_OBJ;
3004 CHECK_STACK_OVF (1);
3005 NEW_ICONST (cfg, ins, -1);
3019 CHECK_STACK_OVF (1);
3020 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
3027 CHECK_STACK_OVF (1);
3029 NEW_ICONST (cfg, ins, *((signed char*)ip));
3036 CHECK_STACK_OVF (1);
3037 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
3044 CHECK_STACK_OVF (1);
3045 MONO_INST_NEW (cfg, ins, OP_I8CONST);
3047 ins->type = STACK_I8;
3049 ins->inst_l = (gint64)read64 (ip);
3054 float *f = mono_mempool_alloc (cfg->domain->mp, sizeof (float));
3056 CHECK_STACK_OVF (1);
3057 MONO_INST_NEW (cfg, ins, OP_R4CONST);
3058 ins->type = STACK_R8;
3067 double *d = mono_mempool_alloc (cfg->domain->mp, sizeof (double));
3069 CHECK_STACK_OVF (1);
3070 MONO_INST_NEW (cfg, ins, OP_R8CONST);
3071 ins->type = STACK_R8;
3080 MonoInst *temp, *store;
3082 CHECK_STACK_OVF (1);
3087 * small optimization: if the loaded value was from a local already,
3088 * just load it twice.
3090 if (ins->ssa_op == MONO_SSA_LOAD &&
3091 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
3093 MONO_INST_NEW (cfg, temp, 0);
3095 temp->cil_code = ip;
3098 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3099 temp->cil_code = ip;
3100 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3101 store->cil_code = ip;
3102 MONO_ADD_INS (bblock, store);
3103 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3106 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3116 MONO_INST_NEW (cfg, ins, CEE_POP);
3117 MONO_ADD_INS (bblock, ins);
3118 ins->cil_code = ip++;
3124 if (stack_start != sp)
3126 MONO_INST_NEW (cfg, ins, CEE_JMP);
3127 token = read32 (ip + 1);
3128 /* FIXME: check the signature matches */
3129 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3130 ins->inst_p0 = cmethod;
3131 MONO_ADD_INS (bblock, ins);
3133 start_new_bblock = 1;
3137 case CEE_CALLVIRT: {
3138 MonoInst *addr = NULL;
3139 MonoMethodSignature *fsig = NULL;
3140 int temp, array_rank = 0;
3141 int virtual = *ip == CEE_CALLVIRT;
3144 token = read32 (ip + 1);
3146 if (*ip == CEE_CALLI) {
3151 if (method->wrapper_type != MONO_WRAPPER_NONE)
3152 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
3154 fsig = mono_metadata_parse_signature (image, token);
3156 n = fsig->param_count + fsig->hasthis;
3158 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3159 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
3160 } else if (constrained_call) {
3161 cmethod = mono_get_method_constrained (image, token, constrained_call, generic_context);
3163 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3168 if (!cmethod->klass->inited)
3169 mono_class_init (cmethod->klass);
3171 if (cmethod->signature->pinvoke) {
3172 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
3173 fsig = wrapper->signature;
3175 fsig = mono_method_get_signature (cmethod, image, token);
3178 n = fsig->param_count + fsig->hasthis;
3180 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
3181 cmethod->klass->parent == mono_defaults.array_class) {
3182 array_rank = cmethod->klass->rank;
3185 if (cmethod->string_ctor)
3186 g_assert_not_reached ();
3192 //g_assert (!virtual || fsig->hasthis);
3196 if (constrained_call) {
3198 * We have the `constrained.' prefix opcode.
3200 if (constrained_call->valuetype && !cmethod->klass->valuetype) {
3202 * The type parameter is instantiated as a valuetype,
3203 * but that type doesn't override the method we're
3204 * calling, so we need to box `this'.
3206 sp [0] = handle_box (cfg, bblock, sp [0], ip, constrained_call);
3207 } else if (!constrained_call->valuetype) {
3211 * The type parameter is instantiated as a reference
3212 * type. We have a managed pointer on the stack, so
3213 * we need to dereference it here.
3216 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3218 ins->inst_i0 = sp [0];
3219 ins->type = STACK_OBJ;
3221 } else if (cmethod->klass->valuetype)
3223 constrained_call = NULL;
3226 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
3229 if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL) && (mono_metadata_signature_equal (method->signature, cmethod->signature))) {
3231 /* FIXME: This assumes the two methods has the same number and type of arguments */
3232 for (i = 0; i < n; ++i) {
3233 /* Check if argument is the same */
3234 NEW_ARGLOAD (cfg, ins, i);
3235 if ((ins->opcode == sp [i]->opcode) && (ins->inst_i0 == sp [i]->inst_i0))
3238 /* Prevent argument from being register allocated */
3239 arg_array [i]->flags |= MONO_INST_VOLATILE;
3240 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3242 if (ins->opcode == CEE_STOBJ) {
3243 NEW_ARGLOADA (cfg, ins, i);
3244 handle_stobj (cfg, bblock, ins, sp [i], sp [i]->cil_code, ins->klass, FALSE, FALSE);
3247 MONO_ADD_INS (bblock, ins);
3249 MONO_INST_NEW (cfg, ins, CEE_JMP);
3251 ins->inst_p0 = cmethod;
3252 ins->inst_p1 = arg_array [0];
3253 MONO_ADD_INS (bblock, ins);
3254 start_new_bblock = 1;
3255 /* skip CEE_RET as well */
3260 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_opcode_for_method (cfg, cmethod, fsig, sp))) {
3263 if (MONO_TYPE_IS_VOID (fsig->ret)) {
3264 MONO_ADD_INS (bblock, ins);
3266 type_to_eval_stack_type (fsig->ret, ins);
3275 handle_loaded_temps (cfg, bblock, stack_start, sp);
3277 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3278 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
3279 mono_method_check_inlining (cfg, cmethod) &&
3280 !g_list_find (dont_inline, cmethod)) {
3282 MonoBasicBlock *ebblock;
3284 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
3285 cmethod = mono_marshal_get_native_wrapper (cmethod);
3287 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, FALSE))) {
3291 GET_BBLOCK (cfg, bbhash, bblock, ip);
3292 ebblock->next_bb = bblock;
3293 link_bblock (cfg, ebblock, bblock);
3295 if (!MONO_TYPE_IS_VOID (fsig->ret))
3298 /* indicates start of a new block, and triggers a load of all
3299 stack arguments at bb boundarie */
3302 inline_costs += costs;
3307 inline_costs += 10 * num_calls++;
3309 /* tail recursion elimination */
3310 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == method && ip [5] == CEE_RET) {
3311 gboolean has_vtargs = FALSE;
3314 /* keep it simple */
3315 for (i = fsig->param_count - 1; i >= 0; i--) {
3316 if (MONO_TYPE_ISSTRUCT (cmethod->signature->params [i]))
3321 for (i = 0; i < n; ++i) {
3322 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3324 MONO_ADD_INS (bblock, ins);
3326 MONO_INST_NEW (cfg, ins, CEE_BR);
3328 MONO_ADD_INS (bblock, ins);
3329 tblock = start_bblock->out_bb [0];
3330 link_bblock (cfg, bblock, tblock);
3331 ins->inst_target_bb = tblock;
3332 start_new_bblock = 1;
3335 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3336 /* just create a dummy - the value is never used */
3337 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
3338 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3346 if (*ip == CEE_CALLI) {
3348 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
3349 NEW_TEMPLOAD (cfg, *sp, temp);
3353 } else if (array_rank) {
3356 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
3357 if (sp [fsig->param_count]->type == STACK_OBJ) {
3358 MonoInst *iargs [2];
3359 MonoInst *array, *to_store, *store;
3361 handle_loaded_temps (cfg, bblock, stack_start, sp);
3363 array = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
3364 NEW_TEMPSTORE (cfg, store, array->inst_c0, sp [0]);
3365 store->cil_code = ip;
3366 MONO_ADD_INS (bblock, store);
3367 NEW_TEMPLOAD (cfg, iargs [0], array->inst_c0);
3369 to_store = mono_compile_create_var (cfg, type_from_stack_type (sp [fsig->param_count]), OP_LOCAL);
3370 NEW_TEMPSTORE (cfg, store, to_store->inst_c0, sp [fsig->param_count]);
3371 store->cil_code = ip;
3372 MONO_ADD_INS (bblock, store);
3373 NEW_TEMPLOAD (cfg, iargs [1], to_store->inst_c0);
3376 * We first save the args for the call so that the args are copied to the stack
3377 * and a new instruction tree for them is created. If we don't do this,
3378 * the same MonoInst is added to two different trees and this is not
3381 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref_check, iargs, ip);
3383 NEW_TEMPLOAD (cfg, sp [0], array->inst_c0);
3384 NEW_TEMPLOAD (cfg, sp [fsig->param_count], to_store->inst_c0);
3387 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
3388 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
3390 if (ins->opcode == CEE_STOBJ) {
3391 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
3393 MONO_ADD_INS (bblock, ins);
3396 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
3397 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3398 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
3402 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
3403 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3406 g_assert_not_reached ();
3410 if (0 && CODE_IS_STLOC (ip + 5) && (!MONO_TYPE_ISSTRUCT (fsig->ret)) && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)) {
3411 /* no need to spill */
3412 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
3415 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) {
3416 NEW_TEMPLOAD (cfg, *sp, temp);
3426 if (cfg->method != method) {
3427 /* return from inlined methode */
3432 //g_assert (returnvar != -1);
3433 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
3434 store->cil_code = sp [0]->cil_code;
3435 if (store->opcode == CEE_STOBJ) {
3436 g_assert_not_reached ();
3437 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
3438 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
3440 MONO_ADD_INS (bblock, store);
3444 g_assert (!return_var);
3447 MONO_INST_NEW (cfg, ins, CEE_NOP);
3448 ins->opcode = mono_type_to_stind (method->signature->ret);
3449 if (ins->opcode == CEE_STOBJ) {
3450 NEW_RETLOADA (cfg, ins);
3451 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3453 ins->opcode = OP_SETRET;
3455 ins->inst_i0 = *sp;;
3456 ins->inst_i1 = NULL;
3457 MONO_ADD_INS (bblock, ins);
3461 if (sp != stack_start)
3463 MONO_INST_NEW (cfg, ins, CEE_BR);
3464 ins->cil_code = ip++;
3465 ins->inst_target_bb = end_bblock;
3466 MONO_ADD_INS (bblock, ins);
3467 link_bblock (cfg, bblock, end_bblock);
3468 start_new_bblock = 1;
3472 MONO_INST_NEW (cfg, ins, CEE_BR);
3473 ins->cil_code = ip++;
3474 MONO_ADD_INS (bblock, ins);
3475 target = ip + 1 + (signed char)(*ip);
3477 GET_BBLOCK (cfg, bbhash, tblock, target);
3478 link_bblock (cfg, bblock, tblock);
3479 CHECK_BBLOCK (target, ip, tblock);
3480 ins->inst_target_bb = tblock;
3481 if (sp != stack_start) {
3482 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3485 start_new_bblock = 1;
3492 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3493 ins->cil_code = ip++;
3494 target = ip + 1 + *(signed char*)ip;
3496 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
3497 if (sp != stack_start) {
3498 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3515 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3516 ins->cil_code = ip++;
3517 target = ip + 1 + *(signed char*)ip;
3520 if (sp != stack_start) {
3521 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3528 MONO_INST_NEW (cfg, ins, CEE_BR);
3529 ins->cil_code = ip++;
3530 MONO_ADD_INS (bblock, ins);
3531 target = ip + 4 + (gint32)read32(ip);
3533 GET_BBLOCK (cfg, bbhash, tblock, target);
3534 link_bblock (cfg, bblock, tblock);
3535 CHECK_BBLOCK (target, ip, tblock);
3536 ins->inst_target_bb = tblock;
3537 if (sp != stack_start) {
3538 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3541 start_new_bblock = 1;
3548 MONO_INST_NEW (cfg, ins, *ip);
3549 ins->cil_code = ip++;
3550 target = ip + 4 + (gint32)read32(ip);
3552 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
3553 if (sp != stack_start) {
3554 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3571 MONO_INST_NEW (cfg, ins, *ip);
3572 ins->cil_code = ip++;
3573 target = ip + 4 + (gint32)read32(ip);
3576 if (sp != stack_start) {
3577 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3585 n = read32 (ip + 1);
3586 MONO_INST_NEW (cfg, ins, *ip);
3588 ins->inst_left = *sp;
3589 if (ins->inst_left->type != STACK_I4) goto unverified;
3592 CHECK_OPSIZE (n * sizeof (guint32));
3593 target = ip + n * sizeof (guint32);
3594 MONO_ADD_INS (bblock, ins);
3595 GET_BBLOCK (cfg, bbhash, tblock, target);
3596 link_bblock (cfg, bblock, tblock);
3597 ins->klass = GUINT_TO_POINTER (n);
3598 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
3599 ins->inst_many_bb [n] = tblock;
3601 for (i = 0; i < n; ++i) {
3602 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
3603 link_bblock (cfg, bblock, tblock);
3604 ins->inst_many_bb [i] = tblock;
3607 if (sp != stack_start) {
3608 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3625 MONO_INST_NEW (cfg, ins, *ip);
3630 ins->type = ldind_type [*ip - CEE_LDIND_I1];
3631 ins->flags |= ins_flag;
3643 MONO_INST_NEW (cfg, ins, *ip);
3644 ins->cil_code = ip++;
3646 handle_loaded_temps (cfg, bblock, stack_start, sp);
3647 MONO_ADD_INS (bblock, ins);
3648 ins->inst_i0 = sp [0];
3649 ins->inst_i1 = sp [1];
3650 ins->flags |= ins_flag;
3669 /* special case that gives a nice speedup and happens to workaorund a ppc jit but (for the release)
3670 * later apply the speedup to the left shift as well
3673 if ((ins->opcode == OP_LSHR_UN) && (ins->type == STACK_I8)
3674 && (ins->inst_right->opcode == OP_ICONST) && (ins->inst_right->inst_c0 == 32)) {
3675 ins->opcode = OP_LONG_SHRUN_32;
3676 /*g_print ("applied long shr speedup to %s\n", cfg->method->name);*/
3680 if (mono_find_jit_opcode_emulation (ins->opcode)) {
3682 *sp++ = emit_tree (cfg, bblock, ins);
3696 case CEE_CONV_OVF_I8:
3697 case CEE_CONV_OVF_U8:
3701 if (mono_find_jit_opcode_emulation (ins->opcode)) {
3703 *sp++ = emit_tree (cfg, bblock, ins);
3707 case CEE_CONV_OVF_I4:
3708 case CEE_CONV_OVF_I1:
3709 case CEE_CONV_OVF_I2:
3710 case CEE_CONV_OVF_I:
3711 case CEE_CONV_OVF_U:
3714 if (sp [-1]->type == STACK_R8) {
3715 ADD_UNOP (CEE_CONV_OVF_I8);
3723 case CEE_CONV_OVF_U1:
3724 case CEE_CONV_OVF_U2:
3725 case CEE_CONV_OVF_U4:
3728 if (sp [-1]->type == STACK_R8) {
3729 ADD_UNOP (CEE_CONV_OVF_U8);
3737 case CEE_CONV_OVF_I1_UN:
3738 case CEE_CONV_OVF_I2_UN:
3739 case CEE_CONV_OVF_I4_UN:
3740 case CEE_CONV_OVF_I8_UN:
3741 case CEE_CONV_OVF_U1_UN:
3742 case CEE_CONV_OVF_U2_UN:
3743 case CEE_CONV_OVF_U4_UN:
3744 case CEE_CONV_OVF_U8_UN:
3745 case CEE_CONV_OVF_I_UN:
3746 case CEE_CONV_OVF_U_UN:
3754 token = read32 (ip + 1);
3755 if (method->wrapper_type != MONO_WRAPPER_NONE)
3756 klass = mono_method_get_wrapper_data (method, token);
3758 klass = mono_class_get_full (image, token, generic_context);
3760 mono_class_init (klass);
3762 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3763 MonoInst *store, *load;
3764 MONO_INST_NEW (cfg, load, CEE_LDIND_REF);
3765 load->cil_code = ip;
3766 load->inst_i0 = sp [1];
3767 load->type = ldind_type [CEE_LDIND_REF];
3768 load->flags |= ins_flag;
3769 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
3770 store->cil_code = ip;
3771 handle_loaded_temps (cfg, bblock, stack_start, sp);
3772 MONO_ADD_INS (bblock, store);
3773 store->inst_i0 = sp [0];
3774 store->inst_i1 = load;
3775 store->flags |= ins_flag;
3777 n = mono_class_value_size (klass, NULL);
3778 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3780 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3781 copy->inst_left = sp [0];
3782 copy->inst_right = sp [1];
3783 copy->cil_code = ip;
3785 MONO_ADD_INS (bblock, copy);
3787 MonoInst *iargs [3];
3790 NEW_ICONST (cfg, iargs [2], n);
3791 iargs [2]->cil_code = ip;
3793 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3800 MonoInst *iargs [3];
3804 token = read32 (ip + 1);
3805 if (method->wrapper_type != MONO_WRAPPER_NONE)
3806 klass = mono_method_get_wrapper_data (method, token);
3808 klass = mono_class_get_full (image, token, generic_context);
3810 mono_class_init (klass);
3811 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3812 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3814 ins->inst_i0 = sp [0];
3815 ins->type = ldind_type [CEE_LDIND_REF];
3816 ins->flags |= ins_flag;
3822 n = mono_class_value_size (klass, NULL);
3823 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3824 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3825 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3827 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3828 copy->inst_left = iargs [0];
3829 copy->inst_right = *sp;
3830 copy->cil_code = ip;
3832 MONO_ADD_INS (bblock, copy);
3835 NEW_ICONST (cfg, iargs [2], n);
3836 iargs [2]->cil_code = ip;
3838 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3840 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3848 CHECK_STACK_OVF (1);
3850 n = read32 (ip + 1);
3852 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3854 MonoInst *iargs [1];
3856 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
3857 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
3858 NEW_TEMPLOAD (cfg, *sp, temp);
3862 if (cfg->opt & MONO_OPT_SHARED) {
3864 MonoInst *iargs [3];
3866 if (mono_compile_aot) {
3867 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n);
3870 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3871 NEW_IMAGECONST (cfg, iargs [1], image);
3872 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
3873 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
3874 NEW_TEMPLOAD (cfg, *sp, temp);
3875 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3877 if (mono_compile_aot)
3878 NEW_LDSTRCONST (cfg, ins, image, n);
3880 NEW_PCONST (cfg, ins, NULL);
3882 ins->type = STACK_OBJ;
3883 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3893 MonoInst *iargs [2];
3894 MonoMethodSignature *fsig;
3898 token = read32 (ip + 1);
3899 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3900 cmethod = mono_method_get_wrapper_data (method, token);
3902 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3903 fsig = mono_method_get_signature (cmethod, image, token);
3905 mono_class_init (cmethod->klass);
3907 n = fsig->param_count;
3910 /* move the args to allow room for 'this' in the first position */
3916 handle_loaded_temps (cfg, bblock, stack_start, sp);
3919 if (cmethod->klass->parent == mono_defaults.array_class) {
3920 NEW_METHODCONST (cfg, *sp, cmethod);
3921 temp = mono_emit_native_call (cfg, bblock, mono_array_new_va, fsig, sp, ip, FALSE);
3922 cfg->flags |= MONO_CFG_HAS_VARARGS;
3924 } else if (cmethod->string_ctor) {
3925 /* we simply pass a null pointer */
3926 NEW_PCONST (cfg, *sp, NULL);
3927 /* now call the string ctor */
3928 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
3930 if (cmethod->klass->valuetype) {
3931 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
3932 temp = iargs [0]->inst_c0;
3933 NEW_TEMPLOADA (cfg, *sp, temp);
3935 if (cfg->opt & MONO_OPT_SHARED) {
3936 NEW_DOMAINCONST (cfg, iargs [0]);
3937 NEW_CLASSCONST (cfg, iargs [1], cmethod->klass);
3939 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3941 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
3942 NEW_VTABLECONST (cfg, iargs [0], vtable);
3943 if (cmethod->klass->has_finalize || cmethod->klass->marshalbyref || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
3944 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
3946 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
3948 NEW_TEMPLOAD (cfg, *sp, temp);
3951 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3952 mono_method_check_inlining (cfg, cmethod) &&
3953 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
3954 !g_list_find (dont_inline, cmethod)) {
3956 MonoBasicBlock *ebblock;
3957 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, FALSE))) {
3962 GET_BBLOCK (cfg, bbhash, bblock, ip);
3963 ebblock->next_bb = bblock;
3964 link_bblock (cfg, ebblock, bblock);
3966 NEW_TEMPLOAD (cfg, *sp, temp);
3969 /* indicates start of a new block, and triggers a load
3970 of all stack arguments at bb boundarie */
3973 inline_costs += costs;
3977 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3980 /* now call the actual ctor */
3981 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3985 NEW_TEMPLOAD (cfg, *sp, temp);
3996 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
3997 mono_class_init (klass);
3999 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4001 MonoMethod *mono_isinst;
4002 MonoInst *iargs [1];
4003 MonoBasicBlock *ebblock;
4007 mono_isinst = mono_marshal_get_isinst (klass);
4010 costs = inline_method (cfg, mono_isinst, mono_isinst->signature, bblock,
4011 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4013 g_assert (costs > 0);
4018 GET_BBLOCK (cfg, bbhash, bblock, ip);
4019 ebblock->next_bb = bblock;
4020 link_bblock (cfg, ebblock, bblock);
4022 temp = iargs [0]->inst_i0->inst_c0;
4023 NEW_TEMPLOAD (cfg, *sp, temp);
4027 inline_costs += costs;
4031 MONO_INST_NEW (cfg, ins, *ip);
4032 ins->type = STACK_OBJ;
4033 ins->inst_left = *sp;
4034 ins->inst_newa_class = klass;
4036 *sp++ = emit_tree (cfg, bblock, ins);
4040 case CEE_UNBOX_ANY: {
4041 MonoInst *add, *vtoffset;
4042 MonoInst *iargs [3];
4047 token = read32 (ip + 1);
4048 if (method->wrapper_type != MONO_WRAPPER_NONE)
4049 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4051 klass = mono_class_get_full (image, token, generic_context);
4052 mono_class_init (klass);
4054 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4056 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4057 MonoMethod *mono_castclass;
4058 MonoInst *iargs [1];
4059 MonoBasicBlock *ebblock;
4063 mono_castclass = mono_marshal_get_castclass (klass);
4066 costs = inline_method (cfg, mono_castclass, mono_castclass->signature, bblock,
4067 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4069 g_assert (costs > 0);
4074 GET_BBLOCK (cfg, bbhash, bblock, ip);
4075 ebblock->next_bb = bblock;
4076 link_bblock (cfg, ebblock, bblock);
4078 temp = iargs [0]->inst_i0->inst_c0;
4079 NEW_TEMPLOAD (cfg, *sp, temp);
4083 inline_costs += costs;
4086 MONO_INST_NEW (cfg, ins, CEE_CASTCLASS);
4087 ins->type = STACK_OBJ;
4088 ins->inst_left = *sp;
4090 ins->inst_newa_class = klass;
4098 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
4099 ins->type = STACK_OBJ;
4100 ins->inst_left = *sp;
4102 ins->inst_newa_class = klass;
4105 MONO_INST_NEW (cfg, add, CEE_ADD);
4106 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4107 add->inst_left = ins;
4108 add->inst_right = vtoffset;
4109 add->type = STACK_MP;
4113 n = mono_class_value_size (klass, NULL);
4114 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
4115 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
4116 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
4118 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
4119 copy->inst_left = iargs [0];
4120 copy->inst_right = *sp;
4121 copy->cil_code = ip;
4123 MONO_ADD_INS (bblock, copy);
4126 NEW_ICONST (cfg, iargs [2], n);
4127 iargs [2]->cil_code = ip;
4129 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
4131 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
4137 MonoInst *add, *vtoffset;
4142 token = read32 (ip + 1);
4143 if (method->wrapper_type != MONO_WRAPPER_NONE)
4144 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4146 klass = mono_class_get_full (image, token, generic_context);
4147 mono_class_init (klass);
4149 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
4150 ins->type = STACK_OBJ;
4151 ins->inst_left = *sp;
4153 ins->inst_newa_class = klass;
4156 MONO_INST_NEW (cfg, add, CEE_ADD);
4157 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4158 add->inst_left = ins;
4159 add->inst_right = vtoffset;
4160 add->type = STACK_MP;
4170 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4171 mono_class_init (klass);
4173 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4175 MonoMethod *mono_castclass;
4176 MonoInst *iargs [1];
4177 MonoBasicBlock *ebblock;
4181 mono_castclass = mono_marshal_get_castclass (klass);
4184 costs = inline_method (cfg, mono_castclass, mono_castclass->signature, bblock,
4185 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4187 g_assert (costs > 0);
4192 GET_BBLOCK (cfg, bbhash, bblock, ip);
4193 ebblock->next_bb = bblock;
4194 link_bblock (cfg, ebblock, bblock);
4196 temp = iargs [0]->inst_i0->inst_c0;
4197 NEW_TEMPLOAD (cfg, *sp, temp);
4201 inline_costs += costs;
4204 MONO_INST_NEW (cfg, ins, *ip);
4205 ins->type = STACK_OBJ;
4206 ins->inst_left = *sp;
4208 ins->inst_newa_class = klass;
4210 *sp++ = emit_tree (cfg, bblock, ins);
4216 MONO_INST_NEW (cfg, ins, *ip);
4218 ins->inst_left = *sp;
4219 ins->cil_code = ip++;
4220 MONO_ADD_INS (bblock, ins);
4222 start_new_bblock = 1;
4227 MonoInst *offset_ins;
4228 MonoClassField *field;
4229 MonoBasicBlock *ebblock;
4233 if (*ip == CEE_STFLD) {
4240 // FIXME: enable this test later.
4241 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
4244 token = read32 (ip + 1);
4245 field = mono_field_from_token (image, token, &klass, generic_context);
4246 mono_class_init (klass);
4248 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
4249 /* FIXME: mark instructions for use in SSA */
4250 if (*ip == CEE_STFLD) {
4251 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound) {
4252 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
4253 MonoInst *iargs [5];
4256 NEW_CLASSCONST (cfg, iargs [1], klass);
4257 NEW_FIELDCONST (cfg, iargs [2], field);
4258 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
4262 if (cfg->opt & MONO_OPT_INLINE) {
4263 costs = inline_method (cfg, stfld_wrapper, stfld_wrapper->signature, bblock,
4264 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4265 g_assert (costs > 0);
4270 GET_BBLOCK (cfg, bbhash, bblock, ip);
4271 ebblock->next_bb = bblock;
4272 link_bblock (cfg, ebblock, bblock);
4274 /* indicates start of a new block, and triggers a load
4275 of all stack arguments at bb boundarie */
4278 inline_costs += costs;
4281 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, stfld_wrapper->signature, iargs, ip, NULL);
4285 NEW_ICONST (cfg, offset_ins, foffset);
4286 MONO_INST_NEW (cfg, ins, CEE_ADD);
4288 ins->inst_left = *sp;
4289 ins->inst_right = offset_ins;
4290 ins->type = STACK_MP;
4292 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4293 store->cil_code = ip;
4294 store->inst_left = ins;
4295 store->inst_right = sp [1];
4296 handle_loaded_temps (cfg, bblock, stack_start, sp);
4297 store->flags |= ins_flag;
4299 if (store->opcode == CEE_STOBJ) {
4300 handle_stobj (cfg, bblock, ins, sp [1], ip,
4301 mono_class_from_mono_type (field->type), FALSE, FALSE);
4303 MONO_ADD_INS (bblock, store);
4306 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound) {
4307 /* fixme: we need to inline that call somehow */
4308 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
4309 MonoInst *iargs [4];
4313 NEW_CLASSCONST (cfg, iargs [1], klass);
4314 NEW_FIELDCONST (cfg, iargs [2], field);
4315 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
4316 if (cfg->opt & MONO_OPT_INLINE) {
4317 costs = inline_method (cfg, ldfld_wrapper, ldfld_wrapper->signature, bblock,
4318 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4319 g_assert (costs > 0);
4324 GET_BBLOCK (cfg, bbhash, bblock, ip);
4325 ebblock->next_bb = bblock;
4326 link_bblock (cfg, ebblock, bblock);
4328 temp = iargs [0]->inst_i0->inst_c0;
4330 if (*ip == CEE_LDFLDA) {
4331 /* not sure howto handle this */
4332 NEW_TEMPLOADA (cfg, *sp, temp);
4334 NEW_TEMPLOAD (cfg, *sp, temp);
4338 /* indicates start of a new block, and triggers a load of
4339 all stack arguments at bb boundarie */
4342 inline_costs += costs;
4345 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, ldfld_wrapper->signature, iargs, ip, NULL);
4346 if (*ip == CEE_LDFLDA) {
4347 /* not sure howto handle this */
4348 NEW_TEMPLOADA (cfg, *sp, temp);
4350 NEW_TEMPLOAD (cfg, *sp, temp);
4355 NEW_ICONST (cfg, offset_ins, foffset);
4356 MONO_INST_NEW (cfg, ins, CEE_ADD);
4358 ins->inst_left = *sp;
4359 ins->inst_right = offset_ins;
4360 ins->type = STACK_MP;
4362 if (*ip == CEE_LDFLDA) {
4366 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4367 type_to_eval_stack_type (field->type, load);
4368 load->cil_code = ip;
4369 load->inst_left = ins;
4370 load->flags |= ins_flag;
4382 MonoClassField *field;
4383 gpointer addr = NULL;
4386 token = read32 (ip + 1);
4388 field = mono_field_from_token (image, token, &klass, generic_context);
4389 mono_class_init (klass);
4391 if ((*ip) == CEE_STSFLD)
4392 handle_loaded_temps (cfg, bblock, stack_start, sp);
4394 if (cfg->domain->special_static_fields)
4395 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
4397 if ((cfg->opt & MONO_OPT_SHARED) || (mono_compile_aot && addr)) {
4399 MonoInst *iargs [2];
4400 g_assert (field->parent);
4401 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
4402 NEW_FIELDCONST (cfg, iargs [1], field);
4403 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
4404 NEW_TEMPLOAD (cfg, ins, temp);
4407 vtable = mono_class_vtable (cfg->domain, klass);
4409 if ((!vtable->initialized || mono_compile_aot) && !(klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (klass, method)) {
4410 guint8 *tramp = mono_create_class_init_trampoline (vtable);
4411 mono_emit_native_call (cfg, bblock, tramp,
4412 helper_sig_class_init_trampoline,
4414 if (cfg->verbose_level > 2)
4415 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
4417 if (cfg->run_cctors)
4418 mono_runtime_class_init (vtable);
4420 addr = (char*)vtable->data + field->offset;
4422 if (mono_compile_aot)
4423 NEW_SFLDACONST (cfg, ins, field);
4425 NEW_PCONST (cfg, ins, addr);
4429 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
4430 * This could be later optimized to do just a couple of
4431 * memory dereferences with constant offsets.
4434 MonoInst *iargs [1];
4435 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
4436 temp = mono_emit_jit_icall (cfg, bblock, mono_get_special_static_data, iargs, ip);
4437 NEW_TEMPLOAD (cfg, ins, temp);
4441 /* FIXME: mark instructions for use in SSA */
4442 if (*ip == CEE_LDSFLDA) {
4444 } else if (*ip == CEE_STSFLD) {
4448 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4449 store->cil_code = ip;
4450 store->inst_left = ins;
4451 store->inst_right = sp [0];
4452 store->flags |= ins_flag;
4455 if (store->opcode == CEE_STOBJ) {
4456 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
4458 MONO_ADD_INS (bblock, store);
4460 gboolean is_const = FALSE;
4461 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4462 if (!((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) &&
4463 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
4464 gpointer addr = (char*)vtable->data + field->offset;
4465 /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
4467 switch (field->type->type) {
4468 case MONO_TYPE_BOOLEAN:
4470 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
4474 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
4477 case MONO_TYPE_CHAR:
4479 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
4483 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
4488 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
4492 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
4497 case MONO_TYPE_STRING:
4498 case MONO_TYPE_OBJECT:
4499 case MONO_TYPE_CLASS:
4500 case MONO_TYPE_SZARRAY:
4502 case MONO_TYPE_FNPTR:
4503 case MONO_TYPE_ARRAY:
4504 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
4505 type_to_eval_stack_type (field->type, *sp);
4510 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
4511 sp [0]->type = STACK_I8;
4512 sp [0]->inst_l = *((gint64 *)addr);
4517 case MONO_TYPE_VALUETYPE:
4526 CHECK_STACK_OVF (1);
4527 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4528 type_to_eval_stack_type (field->type, load);
4529 load->cil_code = ip;
4530 load->inst_left = ins;
4532 load->flags |= ins_flag;
4534 /* fixme: dont see the problem why this does not work */
4535 //cfg->disable_aot = TRUE;
4545 token = read32 (ip + 1);
4546 if (method->wrapper_type != MONO_WRAPPER_NONE)
4547 klass = mono_method_get_wrapper_data (method, token);
4549 klass = mono_class_get_full (image, token, generic_context);
4550 mono_class_init (klass);
4551 n = mono_type_to_stind (&klass->byval_arg);
4552 if (n == CEE_STOBJ) {
4553 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
4555 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
4557 MONO_INST_NEW (cfg, store, n);
4558 store->cil_code = ip;
4559 store->inst_left = sp [0];
4560 store->inst_right = sp [1];
4561 store->flags |= ins_flag;
4562 MONO_ADD_INS (bblock, store);
4574 token = read32 (ip + 1);
4575 if (method->wrapper_type != MONO_WRAPPER_NONE)
4576 klass = mono_method_get_wrapper_data (method, token);
4578 klass = mono_class_get_full (image, token, generic_context);
4579 mono_class_init (klass);
4581 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4586 *sp++ = handle_box (cfg, bblock, val, ip, klass);
4593 MONO_INST_NEW (cfg, ins, *ip);
4598 token = read32 (ip + 1);
4600 /* allocate the domainvar - becaus this is used in decompose_foreach */
4601 if (cfg->opt & MONO_OPT_SHARED)
4602 mono_get_domainvar (cfg);
4604 if (method->wrapper_type != MONO_WRAPPER_NONE)
4605 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4607 klass = mono_class_get_full (image, token, generic_context);
4609 mono_class_init (klass);
4610 ins->inst_newa_class = klass;
4611 ins->inst_newa_len = *sp;
4612 ins->type = STACK_OBJ;
4616 * we store the object so calls to create the array are not interleaved
4617 * with the arguments of other calls.
4620 MonoInst *store, *temp, *load;
4622 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
4623 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4624 store->cil_code = ins->cil_code;
4625 MONO_ADD_INS (bblock, store);
4626 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
4627 load->cil_code = ins->cil_code;
4634 MONO_INST_NEW (cfg, ins, *ip);
4635 ins->cil_code = ip++;
4637 ins->inst_left = *sp;
4638 ins->type = STACK_PTR;
4646 if (method->wrapper_type != MONO_WRAPPER_NONE)
4647 klass = (MonoClass*)mono_method_get_wrapper_data (method, read32 (ip + 1));
4649 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4650 mono_class_init (klass);
4651 NEW_LDELEMA (cfg, ins, sp, klass);
4661 token = read32 (ip + 1);
4662 klass = mono_class_get_full (image, token, generic_context);
4663 mono_class_init (klass);
4664 NEW_LDELEMA (cfg, load, sp, klass);
4665 load->cil_code = ip;
4666 MONO_INST_NEW (cfg, ins, mono_type_to_ldind (&klass->byval_arg));
4668 ins->inst_left = load;
4670 type_to_eval_stack_type (&klass->byval_arg, ins);
4684 case CEE_LDELEM_REF: {
4688 * ldind.x (ldelema (array, index))
4689 * ldelema does the bounds check
4693 klass = array_access_to_klass (*ip);
4694 NEW_LDELEMA (cfg, load, sp, klass);
4695 load->cil_code = ip;
4696 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
4698 ins->inst_left = load;
4700 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
4710 case CEE_STELEM_R8: {
4714 * stind.x (ldelema (array, index), val)
4715 * ldelema does the bounds check
4719 klass = array_access_to_klass (*ip);
4720 NEW_LDELEMA (cfg, load, sp, klass);
4721 load->cil_code = ip;
4722 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
4724 ins->inst_left = load;
4725 ins->inst_right = sp [2];
4727 handle_loaded_temps (cfg, bblock, stack_start, sp);
4728 MONO_ADD_INS (bblock, ins);
4736 * stind.x (ldelema (array, index), val)
4737 * ldelema does the bounds check
4742 token = read32 (ip + 1);
4743 klass = mono_class_get_full (image, token, generic_context);
4744 mono_class_init (klass);
4745 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4746 MonoInst *iargs [3];
4747 handle_loaded_temps (cfg, bblock, stack_start, sp);
4753 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4755 NEW_LDELEMA (cfg, load, sp, klass);
4756 load->cil_code = ip;
4758 n = mono_type_to_stind (&klass->byval_arg);
4760 handle_stobj (cfg, bblock, load, sp [2], ip, klass, FALSE, FALSE);
4762 MONO_INST_NEW (cfg, ins, n);
4764 ins->inst_left = load;
4765 ins->inst_right = sp [2];
4766 handle_loaded_temps (cfg, bblock, stack_start, sp);
4767 MONO_ADD_INS (bblock, ins);
4774 case CEE_STELEM_REF: {
4775 MonoInst *iargs [3];
4780 handle_loaded_temps (cfg, bblock, stack_start, sp);
4786 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4790 NEW_GROUP (cfg, group, sp [0], sp [1]);
4791 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
4793 ins->inst_left = group;
4794 ins->inst_right = sp [2];
4795 MONO_ADD_INS (bblock, ins);
4802 case CEE_CKFINITE: {
4803 MonoInst *store, *temp;
4806 /* this instr. can throw exceptions as side effect,
4807 * so we cant eliminate dead code which contains CKFINITE opdodes.
4808 * Spilling to memory makes sure that we always perform
4812 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
4814 ins->inst_left = sp [-1];
4815 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
4817 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4818 store->cil_code = ip;
4819 MONO_ADD_INS (bblock, store);
4821 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
4828 MONO_INST_NEW (cfg, ins, *ip);
4831 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4832 mono_class_init (klass);
4833 ins->type = STACK_MP;
4834 ins->inst_left = *sp;
4836 ins->inst_newa_class = klass;
4841 case CEE_MKREFANY: {
4842 MonoInst *loc, *klassconst;
4845 MONO_INST_NEW (cfg, ins, *ip);
4848 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4849 mono_class_init (klass);
4852 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
4853 NEW_TEMPLOADA (cfg, ins->inst_right, loc->inst_c0);
4855 NEW_PCONST (cfg, klassconst, klass);
4856 NEW_GROUP (cfg, ins->inst_left, *sp, klassconst);
4858 MONO_ADD_INS (bblock, ins);
4860 NEW_TEMPLOAD (cfg, *sp, loc->inst_c0);
4867 MonoClass *handle_class;
4869 CHECK_STACK_OVF (1);
4872 n = read32 (ip + 1);
4874 handle = mono_ldtoken (image, n, &handle_class, generic_context);
4875 mono_class_init (handle_class);
4877 if (cfg->opt & MONO_OPT_SHARED) {
4879 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
4881 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
4883 NEW_IMAGECONST (cfg, iargs [0], image);
4884 NEW_ICONST (cfg, iargs [1], n);
4885 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
4886 NEW_TEMPLOAD (cfg, res, temp);
4887 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4888 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
4889 MONO_ADD_INS (bblock, store);
4890 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4892 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method_full (image, read32 (ip + 6), NULL, generic_context)) &&
4893 (cmethod->klass == mono_defaults.monotype_class->parent) &&
4894 (strcmp (cmethod->name, "GetTypeFromHandle") == 0) &&
4895 ((g_hash_table_lookup (bbhash, ip + 5) == NULL) ||
4896 (g_hash_table_lookup (bbhash, ip + 5) == bblock))) {
4897 MonoClass *tclass = mono_class_from_mono_type (handle);
4898 mono_class_init (tclass);
4899 if (mono_compile_aot)
4900 NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n);
4902 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
4903 ins->type = STACK_OBJ;
4904 ins->klass = cmethod->klass;
4907 MonoInst *store, *addr, *vtvar;
4909 if (mono_compile_aot)
4910 NEW_LDTOKENCONST (cfg, ins, image, n);
4912 NEW_PCONST (cfg, ins, handle);
4913 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
4914 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4915 NEW_INDSTORE (cfg, store, addr, ins, &mono_defaults.int_class->byval_arg);
4916 MONO_ADD_INS (bblock, store);
4917 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4933 case CEE_ADD_OVF_UN:
4935 case CEE_MUL_OVF_UN:
4937 case CEE_SUB_OVF_UN:
4940 if (mono_find_jit_opcode_emulation (ins->opcode)) {
4942 *sp++ = emit_tree (cfg, bblock, ins);
4946 case CEE_ENDFINALLY:
4947 MONO_INST_NEW (cfg, ins, *ip);
4948 MONO_ADD_INS (bblock, ins);
4949 ins->cil_code = ip++;
4950 start_new_bblock = 1;
4953 * Control will leave the method so empty the stack, otherwise
4954 * the next basic block will start with a nonempty stack.
4956 while (sp != stack_start) {
4957 MONO_INST_NEW (cfg, ins, CEE_POP);
4961 MONO_ADD_INS (bblock, ins);
4967 if (*ip == CEE_LEAVE) {
4969 target = ip + 5 + (gint32)read32(ip + 1);
4972 target = ip + 2 + (signed char)(ip [1]);
4975 /* empty the stack */
4976 while (sp != stack_start) {
4977 MONO_INST_NEW (cfg, ins, CEE_POP);
4981 MONO_ADD_INS (bblock, ins);
4985 * If this leave statement is in a catch block, check for a
4986 * pending exception, and rethrow it if necessary.
4988 for (i = 0; i < header->num_clauses; ++i) {
4989 MonoExceptionClause *clause = &header->clauses [i];
4990 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code)) {
4993 temp = mono_emit_jit_icall (cfg, bblock, mono_thread_get_pending_exception, NULL, ip);
4994 NEW_TEMPLOAD (cfg, *sp, temp);
4996 MONO_INST_NEW (cfg, ins, OP_THROW_OR_NULL);
4997 ins->inst_left = *sp;
4999 MONO_ADD_INS (bblock, ins);
5003 /* fixme: call fault handler ? */
5005 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
5007 for (tmp = handlers; tmp; tmp = tmp->next) {
5009 link_bblock (cfg, bblock, tblock);
5010 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
5012 ins->inst_target_bb = tblock;
5013 MONO_ADD_INS (bblock, ins);
5015 g_list_free (handlers);
5018 MONO_INST_NEW (cfg, ins, CEE_BR);
5020 MONO_ADD_INS (bblock, ins);
5021 GET_BBLOCK (cfg, bbhash, tblock, target);
5022 link_bblock (cfg, bblock, tblock);
5023 CHECK_BBLOCK (target, ip, tblock);
5024 ins->inst_target_bb = tblock;
5025 start_new_bblock = 1;
5027 if (*ip == CEE_LEAVE)
5036 MONO_INST_NEW (cfg, ins, *ip);
5038 handle_loaded_temps (cfg, bblock, stack_start, sp);
5039 MONO_ADD_INS (bblock, ins);
5040 ins->cil_code = ip++;
5041 ins->inst_i0 = sp [0];
5042 ins->inst_i1 = sp [1];
5050 /* trampoline mono specific opcodes */
5051 case MONO_CUSTOM_PREFIX: {
5053 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
5058 case CEE_MONO_FUNC1: {
5060 gpointer func = NULL;
5066 case MONO_MARSHAL_CONV_STR_LPWSTR:
5067 func = mono_string_to_utf16;
5069 case MONO_MARSHAL_CONV_LPWSTR_STR:
5070 func = mono_string_from_utf16;
5072 case MONO_MARSHAL_CONV_LPSTR_STR:
5073 func = mono_string_new_wrapper;
5075 case MONO_MARSHAL_CONV_STR_LPTSTR:
5076 case MONO_MARSHAL_CONV_STR_LPSTR:
5077 func = mono_string_to_utf8;
5079 case MONO_MARSHAL_CONV_STR_BSTR:
5080 func = mono_string_to_bstr;
5082 case MONO_MARSHAL_CONV_STR_TBSTR:
5083 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
5084 func = mono_string_to_ansibstr;
5086 case MONO_MARSHAL_CONV_SB_LPSTR:
5087 case MONO_MARSHAL_CONV_SB_LPTSTR:
5088 func = mono_string_builder_to_utf8;
5090 case MONO_MARSHAL_CONV_SB_LPWSTR:
5091 func = mono_string_builder_to_utf16;
5093 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
5094 func = mono_array_to_savearray;
5096 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
5097 func = mono_array_to_lparray;
5099 case MONO_MARSHAL_CONV_DEL_FTN:
5100 func = mono_delegate_to_ftnptr;
5102 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
5103 func = mono_marshal_string_array;
5105 case MONO_MARSHAL_CONV_STRARRAY_STRWLPARRAY:
5106 func = mono_marshal_string_array_to_unicode;
5109 g_warning ("unknown conversion %d\n", ip [2]);
5110 g_assert_not_reached ();
5113 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5114 NEW_TEMPLOAD (cfg, *sp, temp);
5118 inline_costs += 10 * num_calls++;
5122 case CEE_MONO_FUNC2: {
5124 gpointer func = NULL;
5130 case MONO_MARSHAL_CONV_ASANY:
5131 func = mono_marshal_asany;
5133 case MONO_MARSHAL_CONV_FTN_DEL:
5134 func = mono_ftnptr_to_delegate;
5137 g_warning ("unknown conversion %d\n", ip [2]);
5138 g_assert_not_reached ();
5140 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5141 NEW_TEMPLOAD (cfg, *sp, temp);
5145 inline_costs += 10 * num_calls++;
5149 case CEE_MONO_PROC2: {
5150 gpointer func = NULL;
5156 case MONO_MARSHAL_CONV_LPSTR_SB:
5157 case MONO_MARSHAL_CONV_LPTSTR_SB:
5158 func = mono_string_utf8_to_builder;
5160 case MONO_MARSHAL_CONV_LPWSTR_SB:
5161 func = mono_string_utf16_to_builder;
5163 case MONO_MARSHAL_FREE_ARRAY:
5164 func = mono_marshal_free_array;
5166 case MONO_MARSHAL_CONV_ASANY:
5167 func = mono_marshal_asany;
5170 g_assert_not_reached ();
5173 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5175 inline_costs += 10 * num_calls++;
5178 case CEE_MONO_PROC3: {
5179 gpointer func = NULL;
5185 case MONO_MARSHAL_CONV_STR_BYVALSTR:
5186 func = mono_string_to_byvalstr;
5188 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
5189 func = mono_string_to_byvalwstr;
5191 case MONO_MARSHAL_FREE_ASANY:
5192 func = mono_marshal_free_asany;
5195 g_assert_not_reached ();
5198 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5200 inline_costs += 10 * num_calls++;
5206 mono_emit_jit_icall (cfg, bblock, g_free, sp, ip);
5208 inline_costs += 10 * num_calls++;
5210 case CEE_MONO_LDPTR:
5211 CHECK_STACK_OVF (1);
5213 token = read32 (ip + 2);
5214 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
5218 inline_costs += 10 * num_calls++;
5220 case CEE_MONO_VTADDR:
5223 MONO_INST_NEW (cfg, ins, OP_VTADDR);
5225 ins->type = STACK_MP;
5226 ins->inst_left = *sp;
5230 case CEE_MONO_NEWOBJ: {
5231 MonoInst *iargs [2];
5233 CHECK_STACK_OVF (1);
5235 token = read32 (ip + 2);
5236 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5237 mono_class_init (klass);
5238 NEW_DOMAINCONST (cfg, iargs [0]);
5239 NEW_CLASSCONST (cfg, iargs [1], klass);
5240 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
5241 NEW_TEMPLOAD (cfg, *sp, temp);
5244 inline_costs += 10 * num_calls++;
5247 case CEE_MONO_OBJADDR:
5250 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
5252 ins->type = STACK_MP;
5253 ins->inst_left = *sp;
5257 case CEE_MONO_LDNATIVEOBJ:
5260 token = read32 (ip + 2);
5261 klass = mono_method_get_wrapper_data (method, token);
5262 g_assert (klass->valuetype);
5263 mono_class_init (klass);
5264 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
5268 case CEE_MONO_RETOBJ:
5269 g_assert (cfg->ret);
5270 g_assert (method->signature->pinvoke);
5275 token = read32 (ip + 2);
5276 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5278 NEW_RETLOADA (cfg, ins);
5279 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
5281 if (sp != stack_start)
5284 MONO_INST_NEW (cfg, ins, CEE_BR);
5286 ins->inst_target_bb = end_bblock;
5287 MONO_ADD_INS (bblock, ins);
5288 link_bblock (cfg, bblock, end_bblock);
5289 start_new_bblock = 1;
5292 case CEE_MONO_CISINST:
5293 case CEE_MONO_CCASTCLASS: {
5298 token = read32 (ip + 2);
5299 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5300 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_CISINST) ? OP_CISINST : OP_CCASTCLASS);
5301 ins->type = STACK_I4;
5302 ins->inst_left = *sp;
5303 ins->inst_newa_class = klass;
5305 *sp++ = emit_tree (cfg, bblock, ins);
5309 case CEE_MONO_SAVE_LMF:
5310 case CEE_MONO_RESTORE_LMF:
5311 #ifdef MONO_ARCH_HAVE_LMF_OPS
5312 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_SAVE_LMF) ? OP_SAVE_LMF : OP_RESTORE_LMF);
5313 MONO_ADD_INS (bblock, ins);
5314 cfg->need_lmf_area = TRUE;
5319 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
5328 /* somewhat similar to LDTOKEN */
5329 MonoInst *addr, *vtvar;
5330 CHECK_STACK_OVF (1);
5331 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
5333 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5334 addr->cil_code = ip;
5335 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
5337 ins->inst_left = addr;
5338 MONO_ADD_INS (bblock, ins);
5339 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5353 * The following transforms:
5354 * CEE_CEQ into OP_CEQ
5355 * CEE_CGT into OP_CGT
5356 * CEE_CGT_UN into OP_CGT_UN
5357 * CEE_CLT into OP_CLT
5358 * CEE_CLT_UN into OP_CLT_UN
5360 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
5362 MONO_INST_NEW (cfg, ins, cmp->opcode);
5364 cmp->inst_i0 = sp [0];
5365 cmp->inst_i1 = sp [1];
5369 cmp->opcode = OP_COMPARE;
5371 ins->type = STACK_I4;
5374 /* spill it to reduce the expression complexity
5375 * and workaround bug 54209
5377 if (cmp->inst_left->type == STACK_I8) {
5379 *sp++ = emit_tree (cfg, bblock, ins);
5388 CHECK_STACK_OVF (1);
5390 n = read32 (ip + 2);
5391 if (method->wrapper_type != MONO_WRAPPER_NONE)
5392 cmethod = mono_method_get_wrapper_data (method, n);
5394 cmethod = mono_get_method_full (image, n, NULL, generic_context);
5397 mono_class_init (cmethod->klass);
5398 handle_loaded_temps (cfg, bblock, stack_start, sp);
5400 NEW_METHODCONST (cfg, argconst, cmethod);
5401 if (method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED)
5402 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
5404 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn_nosync, &argconst, ip);
5405 NEW_TEMPLOAD (cfg, *sp, temp);
5409 inline_costs += 10 * num_calls++;
5412 case CEE_LDVIRTFTN: {
5418 n = read32 (ip + 2);
5419 if (method->wrapper_type != MONO_WRAPPER_NONE)
5420 cmethod = mono_method_get_wrapper_data (method, n);
5422 cmethod = mono_get_method_full (image, n, NULL, generic_context);
5424 mono_class_init (cmethod->klass);
5425 handle_loaded_temps (cfg, bblock, stack_start, sp);
5429 NEW_METHODCONST (cfg, args [1], cmethod);
5430 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
5431 NEW_TEMPLOAD (cfg, *sp, temp);
5435 inline_costs += 10 * num_calls++;
5439 CHECK_STACK_OVF (1);
5441 n = read16 (ip + 2);
5443 NEW_ARGLOAD (cfg, ins, n);
5449 CHECK_STACK_OVF (1);
5451 n = read16 (ip + 2);
5453 NEW_ARGLOADA (cfg, ins, n);
5461 handle_loaded_temps (cfg, bblock, stack_start, sp);
5463 n = read16 (ip + 2);
5465 NEW_ARGSTORE (cfg, ins, n, *sp);
5467 if (ins->opcode == CEE_STOBJ) {
5468 NEW_ARGLOADA (cfg, ins, n);
5469 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5471 MONO_ADD_INS (bblock, ins);
5475 CHECK_STACK_OVF (1);
5477 n = read16 (ip + 2);
5479 NEW_LOCLOAD (cfg, ins, n);
5485 CHECK_STACK_OVF (1);
5487 n = read16 (ip + 2);
5489 NEW_LOCLOADA (cfg, ins, n);
5498 n = read16 (ip + 2);
5500 handle_loaded_temps (cfg, bblock, stack_start, sp);
5501 NEW_LOCSTORE (cfg, ins, n, *sp);
5503 if (ins->opcode == CEE_STOBJ) {
5504 NEW_LOCLOADA (cfg, ins, n);
5505 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5507 MONO_ADD_INS (bblock, ins);
5514 if (sp != stack_start)
5516 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
5517 ins->inst_left = *sp;
5520 cfg->flags |= MONO_CFG_HAS_ALLOCA;
5521 if (header->init_locals)
5522 ins->flags |= MONO_INST_INIT;
5526 /* FIXME: set init flag if locals init is set in this method */
5528 case CEE_ENDFILTER: {
5529 MonoExceptionClause *clause, *nearest;
5530 int cc, nearest_num;
5534 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
5536 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
5537 ins->inst_left = *sp;
5539 MONO_ADD_INS (bblock, ins);
5540 start_new_bblock = 1;
5545 for (cc = 0; cc < header->num_clauses; ++cc) {
5546 clause = &header->clauses [cc];
5547 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
5548 (!nearest || (clause->token_or_filter > nearest->token_or_filter))) {
5554 filter_lengths [nearest_num] = (ip - header->code) - nearest->token_or_filter;
5558 case CEE_UNALIGNED_:
5559 ins_flag |= MONO_INST_UNALIGNED;
5560 /* FIXME: record alignment? we can assume 1 for now */
5565 ins_flag |= MONO_INST_VOLATILE;
5569 ins_flag |= MONO_INST_TAILCALL;
5570 /* Can't inline tail calls at this time */
5571 inline_costs += 100000;
5578 token = read32 (ip + 2);
5579 if (method->wrapper_type != MONO_WRAPPER_NONE)
5580 klass = mono_method_get_wrapper_data (method, token);
5582 klass = mono_class_get_full (image, token, generic_context);
5583 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5584 MonoInst *store, *load;
5585 NEW_PCONST (cfg, load, NULL);
5586 load->cil_code = ip;
5587 load->type = STACK_OBJ;
5588 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
5589 store->cil_code = ip;
5590 handle_loaded_temps (cfg, bblock, stack_start, sp);
5591 MONO_ADD_INS (bblock, store);
5592 store->inst_i0 = sp [0];
5593 store->inst_i1 = load;
5595 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
5600 case CEE_CONSTRAINED_:
5601 /* FIXME: implement */
5603 token = read32 (ip + 2);
5604 constrained_call = mono_class_get_full (image, token, generic_context);
5609 MonoInst *iargs [3];
5612 if ((cfg->opt & MONO_OPT_INTRINS) && (ip [1] == CEE_CPBLK) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5)) {
5614 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
5615 copy->inst_left = sp [0];
5616 copy->inst_right = sp [1];
5617 copy->cil_code = ip;
5619 MONO_ADD_INS (bblock, copy);
5626 handle_loaded_temps (cfg, bblock, stack_start, sp);
5627 if (ip [1] == CEE_CPBLK) {
5628 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
5630 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
5639 ins_flag |= MONO_INST_NOTYPECHECK;
5641 ins_flag |= MONO_INST_NORANGECHECK;
5642 /* we ignore the no-nullcheck for now since we
5643 * really do it explicitly only when doing callvirt->call
5649 /* FIXME: check we are in a catch handler */
5650 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
5651 load->cil_code = ip;
5652 MONO_INST_NEW (cfg, ins, CEE_THROW);
5653 ins->inst_left = load;
5655 MONO_ADD_INS (bblock, ins);
5657 start_new_bblock = 1;
5662 CHECK_STACK_OVF (1);
5664 token = read32 (ip + 2);
5665 /* FIXXME: handle generics. */
5666 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
5667 MonoType *type = mono_type_create_from_typespec (image, token);
5668 token = mono_type_size (type, &align);
5670 MonoClass *szclass = mono_class_get_full (image, token, generic_context);
5671 mono_class_init (szclass);
5672 token = mono_class_value_size (szclass, &align);
5674 NEW_ICONST (cfg, ins, token);
5679 case CEE_REFANYTYPE:
5681 MONO_INST_NEW (cfg, ins, OP_REFANYTYPE);
5683 ins->type = STACK_MP;
5684 ins->inst_left = *sp;
5685 ins->type = STACK_VTYPE;
5686 ins->klass = mono_defaults.typehandle_class;
5695 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
5700 g_error ("opcode 0x%02x not handled", *ip);
5703 if (start_new_bblock != 1)
5706 bblock->cil_length = ip - bblock->cil_code;
5707 bblock->next_bb = end_bblock;
5708 link_bblock (cfg, bblock, end_bblock);
5710 if (cfg->method == method && cfg->domainvar) {
5714 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
5715 call->signature = helper_sig_domain_get;
5716 call->inst.type = STACK_PTR;
5717 call->fptr = mono_domain_get;
5718 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, (MonoInst*)call);
5720 MONO_ADD_INS (init_localsbb, store);
5723 if (header->init_locals) {
5725 for (i = 0; i < header->num_locals; ++i) {
5726 int t = header->locals [i]->type;
5727 if (t == MONO_TYPE_VALUETYPE && header->locals [i]->data.klass->enumtype)
5728 t = header->locals [i]->data.klass->enum_basetype->type;
5729 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
5730 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
5731 NEW_ICONST (cfg, ins, 0);
5732 NEW_LOCSTORE (cfg, store, i, ins);
5733 MONO_ADD_INS (init_localsbb, store);
5734 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
5735 MONO_INST_NEW (cfg, ins, OP_I8CONST);
5736 ins->type = STACK_I8;
5738 NEW_LOCSTORE (cfg, store, i, ins);
5739 MONO_ADD_INS (init_localsbb, store);
5740 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
5741 MONO_INST_NEW (cfg, ins, OP_R8CONST);
5742 ins->type = STACK_R8;
5743 ins->inst_p0 = (void*)&r8_0;
5744 NEW_LOCSTORE (cfg, store, i, ins);
5745 MONO_ADD_INS (init_localsbb, store);
5746 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF)) {
5747 NEW_LOCLOADA (cfg, ins, i);
5748 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (header->locals [i]), NULL, NULL);
5751 NEW_PCONST (cfg, ins, NULL);
5752 NEW_LOCSTORE (cfg, store, i, ins);
5753 MONO_ADD_INS (init_localsbb, store);
5759 /* resolve backward branches in the middle of an existing basic block */
5760 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
5762 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
5763 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
5764 if (tblock != start_bblock) {
5766 split_bblock (cfg, tblock, bblock);
5767 l = bblock->cil_code - header->code;
5768 bblock->cil_length = tblock->cil_length - l;
5769 tblock->cil_length = l;
5771 g_print ("recheck failed.\n");
5776 * we compute regions here, because the length of filter clauses is not known in advance.
5777 * It is computed in the CEE_ENDFILTER case in the above switch statement
5779 if (cfg->method == method) {
5781 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5782 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
5784 mono_create_spvar_for_region (cfg, bb->region);
5785 if (cfg->verbose_level > 2)
5786 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
5789 g_hash_table_destroy (bbhash);
5792 dont_inline = g_list_remove (dont_inline, method);
5793 return inline_costs;
5796 if (cfg->method != method)
5797 g_hash_table_destroy (bbhash);
5798 dont_inline = g_list_remove (dont_inline, method);
5802 if (cfg->method != method)
5803 g_hash_table_destroy (bbhash);
5804 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
5805 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
5806 dont_inline = g_list_remove (dont_inline, method);
5811 mono_print_tree (MonoInst *tree) {
5817 arity = mono_burg_arity [tree->opcode];
5819 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
5821 switch (tree->opcode) {
5823 printf ("[%d]", tree->inst_c0);
5826 printf ("[%lld]", tree->inst_l);
5829 printf ("[%f]", *(double*)tree->inst_p0);
5832 printf ("[%f]", *(float*)tree->inst_p0);
5836 printf ("[%d]", tree->inst_c0);
5839 if (tree->inst_offset < 0)
5840 printf ("[-0x%x(%s)]", -tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5842 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5845 printf ("[%s]", mono_arch_regname (tree->dreg));
5848 printf ("[%s]", tree->inst_newa_class->name);
5849 mono_print_tree (tree->inst_newa_len);
5860 case OP_VOIDCALLVIRT: {
5861 MonoCallInst *call = (MonoCallInst*)tree;
5863 printf ("[%s]", call->method->name);
5868 printf ("[%d (", tree->inst_c0);
5869 for (i = 0; i < tree->inst_phi_args [0]; i++) {
5872 printf ("%d", tree->inst_phi_args [i + 1]);
5883 case OP_LOAD_MEMBASE:
5884 case OP_LOADI4_MEMBASE:
5885 case OP_LOADU4_MEMBASE:
5886 case OP_LOADU1_MEMBASE:
5887 case OP_LOADI1_MEMBASE:
5888 case OP_LOADU2_MEMBASE:
5889 case OP_LOADI2_MEMBASE:
5890 printf ("[%s] <- [%s + 0x%x]", mono_arch_regname (tree->dreg), mono_arch_regname (tree->inst_basereg), tree->inst_offset);
5893 printf ("[B%d]", tree->inst_target_bb->block_num);
5903 case OP_VOIDCALL_REG:
5904 mono_print_tree (tree->inst_left);
5916 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
5917 mono_print_tree (tree->inst_left);
5920 if (!mono_arch_print_tree(tree, arity)) {
5922 mono_print_tree (tree->inst_left);
5924 mono_print_tree (tree->inst_right);
5935 mono_print_tree_nl (MonoInst *tree)
5937 mono_print_tree (tree);
5942 type_from_typename (char *typename)
5946 if (!strcmp (typename, "int"))
5947 klass = mono_defaults.int_class;
5948 else if (!strcmp (typename, "ptr"))
5949 klass = mono_defaults.int_class;
5950 else if (!strcmp (typename, "void"))
5951 klass = mono_defaults.void_class;
5952 else if (!strcmp (typename, "int32"))
5953 klass = mono_defaults.int32_class;
5954 else if (!strcmp (typename, "uint32"))
5955 klass = mono_defaults.uint32_class;
5956 else if (!strcmp (typename, "long"))
5957 klass = mono_defaults.int64_class;
5958 else if (!strcmp (typename, "ulong"))
5959 klass = mono_defaults.uint64_class;
5960 else if (!strcmp (typename, "float"))
5961 klass = mono_defaults.single_class;
5962 else if (!strcmp (typename, "double"))
5963 klass = mono_defaults.double_class;
5964 else if (!strcmp (typename, "object"))
5965 klass = mono_defaults.object_class;
5968 g_assert_not_reached ();
5970 return &klass->byval_arg;
5973 static MonoMethodSignature*
5974 make_icall_sig (const char *sigstr)
5979 MonoMethodSignature *res;
5981 parts = g_strsplit (sigstr, " ", 256);
5990 res = mono_metadata_signature_alloc (mono_defaults.corlib, len - 1);
5993 #ifdef PLATFORM_WIN32
5995 * Under windows, the default pinvoke calling convention is STDCALL but
5998 res->call_convention = MONO_CALL_C;
6001 res->ret = type_from_typename (parts [0]);
6002 for (i = 1; i < len; ++i) {
6003 res->params [i - 1] = type_from_typename (parts [i]);
6012 create_helper_signature (void)
6015 /* FIXME: set call conv */
6016 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
6017 helper_sig_newarr = make_icall_sig ("object ptr ptr int32");
6019 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
6020 helper_sig_newarr_specific = make_icall_sig ("object ptr int32");
6022 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
6023 helper_sig_object_new = make_icall_sig ("object ptr ptr");
6025 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
6026 helper_sig_object_new_specific = make_icall_sig ("object ptr");
6028 /* void* mono_method_compile (MonoMethod*) */
6029 helper_sig_compile = make_icall_sig ("ptr ptr");
6031 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
6032 helper_sig_compile_virt = make_icall_sig ("ptr object ptr");
6034 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
6035 helper_sig_ldstr = make_icall_sig ("object ptr ptr int32");
6037 /* MonoDomain *mono_domain_get (void) */
6038 helper_sig_domain_get = make_icall_sig ("ptr");
6040 /* void stelem_ref (MonoArray *, int index, MonoObject *) */
6041 helper_sig_stelem_ref = make_icall_sig ("void ptr int32 object");
6043 /* void stelem_ref_check (MonoArray *, MonoObject *) */
6044 helper_sig_stelem_ref_check = make_icall_sig ("void object object");
6046 /* long amethod (long, long) */
6047 helper_sig_long_long_long = make_icall_sig ("long long long");
6049 /* object amethod (intptr) */
6050 helper_sig_obj_ptr = make_icall_sig ("object ptr");
6052 helper_sig_obj_ptr_ptr = make_icall_sig ("object ptr ptr");
6054 /* void amethod (intptr) */
6055 helper_sig_void_ptr = make_icall_sig ("void ptr");
6057 /* void amethod (MonoObject *obj) */
6058 helper_sig_void_obj = make_icall_sig ("void object");
6060 /* void amethod (MonoObject *obj, void *ptr, int i) */
6061 helper_sig_void_obj_ptr_int = make_icall_sig ("void object ptr int");
6063 /* intptr amethod (void) */
6064 helper_sig_ptr_void = make_icall_sig ("ptr");
6066 /* object amethod (void) */
6067 helper_sig_obj_void = make_icall_sig ("object");
6069 /* void amethod (intptr, intptr) */
6070 helper_sig_void_ptr_ptr = make_icall_sig ("void ptr ptr");
6072 /* void amethod (intptr, intptr, intptr) */
6073 helper_sig_void_ptr_ptr_ptr = make_icall_sig ("void ptr ptr ptr");
6075 /* intptr amethod (intptr, intptr) */
6076 helper_sig_ptr_ptr_ptr = make_icall_sig ("ptr ptr ptr");
6078 /* IntPtr amethod (object) */
6079 helper_sig_ptr_obj = make_icall_sig ("ptr object");
6081 /* IntPtr amethod (object, int) */
6082 helper_sig_ptr_obj_int = make_icall_sig ("ptr object int");
6084 /* IntPtr amethod (int) */
6085 helper_sig_ptr_int = make_icall_sig ("ptr int32");
6087 /* long amethod (long, guint32) */
6088 helper_sig_long_long_int = make_icall_sig ("long long int32");
6090 /* ulong amethod (double) */
6091 helper_sig_ulong_double = make_icall_sig ("ulong double");
6093 /* long amethod (double) */
6094 helper_sig_long_double = make_icall_sig ("long double");
6096 /* double amethod (long) */
6097 helper_sig_double_long = make_icall_sig ("double long");
6099 /* double amethod (int) */
6100 helper_sig_double_int = make_icall_sig ("double int32");
6102 /* float amethod (long) */
6103 helper_sig_float_long = make_icall_sig ("float long");
6105 /* double amethod (double, double) */
6106 helper_sig_double_double_double = make_icall_sig ("double double double");
6108 /* uint amethod (double) */
6109 helper_sig_uint_double = make_icall_sig ("uint32 double");
6111 /* int amethod (double) */
6112 helper_sig_int_double = make_icall_sig ("int32 double");
6114 /* void initobj (intptr, int size) */
6115 helper_sig_initobj = make_icall_sig ("void ptr int32");
6117 /* void memcpy (intptr, intptr, int size) */
6118 helper_sig_memcpy = make_icall_sig ("void ptr ptr int32");
6120 /* void memset (intptr, int val, int size) */
6121 helper_sig_memset = make_icall_sig ("void ptr int32 int32");
6123 helper_sig_class_init_trampoline = make_icall_sig ("void");
6126 static GHashTable *jit_icall_hash_name = NULL;
6127 static GHashTable *jit_icall_hash_addr = NULL;
6130 mono_find_jit_icall_by_name (const char *name)
6132 MonoJitICallInfo *info;
6133 g_assert (jit_icall_hash_name);
6135 //printf ("lookup addr %s %p\n", name, g_hash_table_lookup (jit_icall_hash_name, name));
6136 EnterCriticalSection (&trampoline_hash_mutex);
6137 info = g_hash_table_lookup (jit_icall_hash_name, name);
6138 LeaveCriticalSection (&trampoline_hash_mutex);
6143 mono_find_jit_icall_by_addr (gconstpointer addr)
6145 MonoJitICallInfo *info;
6146 g_assert (jit_icall_hash_addr);
6148 EnterCriticalSection (&trampoline_hash_mutex);
6149 info = g_hash_table_lookup (jit_icall_hash_addr, (gpointer)addr);
6150 LeaveCriticalSection (&trampoline_hash_mutex);
6156 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
6159 MonoMethod *wrapper;
6162 if (callinfo->wrapper)
6163 return callinfo->wrapper;
6165 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
6166 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
6167 /* Must be domain neutral since there is only one copy */
6168 code = mono_jit_compile_method_with_opt (wrapper, default_opt | MONO_OPT_SHARED);
6170 EnterCriticalSection (&trampoline_hash_mutex);
6171 if (!callinfo->wrapper) {
6172 callinfo->wrapper = code;
6173 g_hash_table_insert (jit_icall_hash_addr, (gpointer)callinfo->wrapper, callinfo);
6175 LeaveCriticalSection (&trampoline_hash_mutex);
6178 return callinfo->wrapper;
6182 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save)
6184 MonoJitICallInfo *info;
6189 EnterCriticalSection (&trampoline_hash_mutex);
6191 if (!jit_icall_hash_name) {
6192 jit_icall_hash_name = g_hash_table_new (g_str_hash, g_str_equal);
6193 jit_icall_hash_addr = g_hash_table_new (NULL, NULL);
6196 if (g_hash_table_lookup (jit_icall_hash_name, name)) {
6197 g_warning ("jit icall already defined \"%s\"\n", name);
6198 g_assert_not_reached ();
6201 info = g_new (MonoJitICallInfo, 1);
6208 info->wrapper = func;
6210 info->wrapper = NULL;
6213 g_hash_table_insert (jit_icall_hash_name, (gpointer)info->name, info);
6214 g_hash_table_insert (jit_icall_hash_addr, (gpointer)func, info);
6216 LeaveCriticalSection (&trampoline_hash_mutex);
6221 mono_create_class_init_trampoline (MonoVTable *vtable)
6225 /* previously created trampoline code */
6226 mono_domain_lock (vtable->domain);
6228 mono_g_hash_table_lookup (vtable->domain->class_init_trampoline_hash,
6230 mono_domain_unlock (vtable->domain);
6234 code = mono_arch_create_class_init_trampoline (vtable);
6236 /* store trampoline address */
6237 mono_domain_lock (vtable->domain);
6238 mono_g_hash_table_insert (vtable->domain->class_init_trampoline_hash,
6240 mono_domain_unlock (vtable->domain);
6242 EnterCriticalSection (&trampoline_hash_mutex);
6243 if (!class_init_hash_addr)
6244 class_init_hash_addr = g_hash_table_new (NULL, NULL);
6245 g_hash_table_insert (class_init_hash_addr, code, vtable);
6246 LeaveCriticalSection (&trampoline_hash_mutex);
6252 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method,
6253 gboolean add_sync_wrapper)
6258 if (add_sync_wrapper && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
6259 return mono_create_jump_trampoline (domain, mono_marshal_get_synchronized_wrapper (method), FALSE);
6261 code = mono_jit_find_compiled_method (domain, method);
6265 EnterCriticalSection (&trampoline_hash_mutex);
6267 if (jump_trampoline_hash) {
6268 code = g_hash_table_lookup (jump_trampoline_hash, method);
6270 LeaveCriticalSection (&trampoline_hash_mutex);
6275 ji = mono_arch_create_jump_trampoline (method);
6278 * mono_delegate_ctor needs to find the method metadata from the
6279 * trampoline address, so we save it here.
6282 mono_jit_info_table_add (mono_get_root_domain (), ji);
6284 if (!jump_trampoline_hash)
6285 jump_trampoline_hash = g_hash_table_new (NULL, NULL);
6286 g_hash_table_insert (jump_trampoline_hash, method, ji->code_start);
6288 LeaveCriticalSection (&trampoline_hash_mutex);
6290 return ji->code_start;
6294 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
6298 EnterCriticalSection (&trampoline_hash_mutex);
6299 if (class_init_hash_addr)
6300 res = g_hash_table_lookup (class_init_hash_addr, addr);
6303 LeaveCriticalSection (&trampoline_hash_mutex);
6308 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func, gboolean no_throw)
6310 MonoJitICallInfo *info;
6312 if (!emul_opcode_map)
6313 emul_opcode_map = g_new0 (MonoJitICallInfo*, OP_LAST + 1);
6315 g_assert (!sig->hasthis);
6316 g_assert (sig->param_count < 3);
6318 info = mono_register_jit_icall (func, name, sig, no_throw);
6320 emul_opcode_map [opcode] = info;
6324 decompose_foreach (MonoInst *tree, gpointer data)
6326 static MonoJitICallInfo *newarr_info = NULL;
6327 static MonoJitICallInfo *newarr_specific_info = NULL;
6328 MonoJitICallInfo *info;
6331 switch (tree->opcode) {
6333 MonoCompile *cfg = data;
6334 MonoInst *iargs [3];
6337 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
6338 g_assert (newarr_info);
6339 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
6340 g_assert (newarr_specific_info);
6343 if (cfg->opt & MONO_OPT_SHARED) {
6344 NEW_DOMAINCONST (cfg, iargs [0]);
6345 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
6346 iargs [2] = tree->inst_newa_len;
6351 MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
6353 NEW_VTABLECONST (cfg, iargs [0], vtable);
6354 iargs [1] = tree->inst_newa_len;
6356 info = newarr_specific_info;
6359 mono_emulate_opcode (cfg, tree, iargs, info);
6361 /* Need to decompose arguments after the the opcode is decomposed */
6362 for (i = 0; i < info->sig->param_count; ++i)
6363 dec_foreach (iargs [i], cfg);
6373 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
6375 switch (mono_burg_arity [tree->opcode]) {
6378 mono_inst_foreach (tree->inst_left, func, data);
6381 mono_inst_foreach (tree->inst_left, func, data);
6382 mono_inst_foreach (tree->inst_right, func, data);
6385 g_assert_not_reached ();
6392 mono_print_bb_code (MonoBasicBlock *bb) {
6394 MonoInst *c = bb->code;
6396 mono_print_tree (c);
6404 print_dfn (MonoCompile *cfg) {
6409 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
6411 for (i = 0; i < cfg->num_bblocks; ++i) {
6412 bb = cfg->bblocks [i];
6414 char* code1, *code2;
6415 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
6416 if (bb->last_ins->cil_code)
6417 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
6419 code2 = g_strdup ("");
6421 code1 [strlen (code1) - 1] = 0;
6422 code = g_strdup_printf ("%s -> %s", code1, code2);
6426 code = g_strdup ("\n");
6427 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
6429 MonoInst *c = bb->code;
6431 mono_print_tree (c);
6439 g_print ("\tprev:");
6440 for (j = 0; j < bb->in_count; ++j) {
6441 g_print (" BB%d", bb->in_bb [j]->block_num);
6443 g_print ("\t\tsucc:");
6444 for (j = 0; j < bb->out_count; ++j) {
6445 g_print (" BB%d", bb->out_bb [j]->block_num);
6447 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
6450 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
6453 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
6455 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
6463 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
6467 g_assert (bb->code);
6468 bb->last_ins->next = inst;
6469 bb->last_ins = inst;
6471 bb->last_ins = bb->code = inst;
6476 mono_destroy_compile (MonoCompile *cfg)
6478 //mono_mempool_stats (cfg->mempool);
6479 g_hash_table_destroy (cfg->bb_hash);
6480 mono_free_loop_info (cfg);
6482 mono_regstate_free (cfg->rs);
6484 g_hash_table_destroy (cfg->spvars);
6485 mono_mempool_destroy (cfg->mempool);
6486 g_list_free (cfg->ldstr_list);
6488 g_free (cfg->varinfo);
6493 #ifdef HAVE_KW_THREAD
6494 static __thread gpointer mono_lmf_addr;
6498 mono_get_lmf_addr (void)
6500 #ifdef HAVE_KW_THREAD
6501 return mono_lmf_addr;
6503 MonoJitTlsData *jit_tls;
6505 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
6506 return &jit_tls->lmf;
6508 g_assert_not_reached ();
6514 * mono_thread_abort:
6515 * @obj: exception object
6517 * abort the thread, print exception information and stack trace
6520 mono_thread_abort (MonoObject *obj)
6522 /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
6524 /* handle_remove should be eventually called for this thread, too
6531 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
6533 MonoJitTlsData *jit_tls;
6536 jit_tls = TlsGetValue (mono_jit_tls_id);
6540 jit_tls = g_new0 (MonoJitTlsData, 1);
6542 TlsSetValue (mono_jit_tls_id, jit_tls);
6544 jit_tls->abort_func = abort_func;
6545 jit_tls->end_of_stack = stack_start;
6547 lmf = g_new0 (MonoLMF, 1);
6550 jit_tls->lmf = jit_tls->first_lmf = lmf;
6552 #ifdef HAVE_KW_THREAD
6553 mono_lmf_addr = &jit_tls->lmf;
6556 mono_arch_setup_jit_tls_data (jit_tls);
6562 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
6565 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
6566 thread = mono_thread_current ();
6568 thread->jit_data = jit_tls;
6571 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
6574 mono_thread_abort_dummy (MonoObject *obj)
6576 if (mono_thread_attach_aborted_cb)
6577 mono_thread_attach_aborted_cb (obj);
6579 mono_thread_abort (obj);
6583 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
6586 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
6587 thread = mono_thread_current ();
6589 thread->jit_data = jit_tls;
6593 mini_thread_cleanup (MonoThread *thread)
6595 MonoJitTlsData *jit_tls = thread->jit_data;
6598 mono_arch_free_jit_tls_data (jit_tls);
6599 g_free (jit_tls->first_lmf);
6601 thread->jit_data = NULL;
6606 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
6608 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
6612 ji->data.target = target;
6613 ji->next = cfg->patch_info;
6615 cfg->patch_info = ji;
6619 mono_remove_patch_info (MonoCompile *cfg, int ip)
6621 MonoJumpInfo **ji = &cfg->patch_info;
6624 if ((*ji)->ip.i == ip)
6627 ji = &((*ji)->next);
6632 mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors)
6634 unsigned char *ip = patch_info->ip.i + code;
6635 gconstpointer target = NULL;
6637 switch (patch_info->type) {
6638 case MONO_PATCH_INFO_BB:
6639 target = patch_info->data.bb->native_offset + code;
6641 case MONO_PATCH_INFO_ABS:
6642 target = patch_info->data.target;
6644 case MONO_PATCH_INFO_LABEL:
6645 target = patch_info->data.inst->inst_c0 + code;
6647 case MONO_PATCH_INFO_IP:
6650 case MONO_PATCH_INFO_METHOD_REL:
6651 target = code + patch_info->data.offset;
6653 case MONO_PATCH_INFO_INTERNAL_METHOD: {
6654 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
6656 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
6657 g_assert_not_reached ();
6659 target = mono_icall_get_wrapper (mi);
6662 case MONO_PATCH_INFO_METHOD_JUMP: {
6665 /* get the trampoline to the method from the domain */
6666 target = mono_create_jump_trampoline (domain, patch_info->data.method, TRUE);
6667 if (!domain->jump_target_hash)
6668 domain->jump_target_hash = g_hash_table_new (NULL, NULL);
6669 list = g_hash_table_lookup (domain->jump_target_hash, patch_info->data.method);
6670 list = g_slist_prepend (list, ip);
6671 g_hash_table_insert (domain->jump_target_hash, patch_info->data.method, list);
6674 case MONO_PATCH_INFO_METHOD:
6675 if (patch_info->data.method == method) {
6678 /* get the trampoline to the method from the domain */
6679 target = mono_arch_create_jit_trampoline (patch_info->data.method);
6681 case MONO_PATCH_INFO_SWITCH: {
6682 gpointer *jump_table;
6685 mono_domain_lock (domain);
6686 jump_table = mono_code_manager_reserve (domain->code_mp, sizeof (gpointer) * patch_info->table_size);
6687 mono_domain_unlock (domain);
6689 for (i = 0; i < patch_info->table_size; i++) {
6690 jump_table [i] = code + (int)patch_info->data.table [i];
6692 target = jump_table;
6695 case MONO_PATCH_INFO_METHODCONST:
6696 case MONO_PATCH_INFO_CLASS:
6697 case MONO_PATCH_INFO_IMAGE:
6698 case MONO_PATCH_INFO_FIELD:
6699 target = patch_info->data.target;
6701 case MONO_PATCH_INFO_IID:
6702 mono_class_init (patch_info->data.klass);
6703 target = (gpointer)patch_info->data.klass->interface_id;
6705 case MONO_PATCH_INFO_VTABLE:
6706 target = mono_class_vtable (domain, patch_info->data.klass);
6708 case MONO_PATCH_INFO_CLASS_INIT:
6709 target = mono_create_class_init_trampoline (mono_class_vtable (domain, patch_info->data.klass));
6711 case MONO_PATCH_INFO_SFLDA: {
6712 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
6713 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (vtable->klass, method))
6714 /* Done by the generated code */
6718 mono_runtime_class_init (vtable);
6720 target = (char*)vtable->data + patch_info->data.field->offset;
6723 case MONO_PATCH_INFO_R4:
6724 case MONO_PATCH_INFO_R8:
6725 target = patch_info->data.target;
6727 case MONO_PATCH_INFO_EXC_NAME:
6728 target = patch_info->data.name;
6730 case MONO_PATCH_INFO_LDSTR:
6732 mono_ldstr (domain, patch_info->data.token->image,
6733 mono_metadata_token_index (patch_info->data.token->token));
6735 case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
6737 MonoClass *handle_class;
6739 handle = mono_ldtoken (patch_info->data.token->image,
6740 patch_info->data.token->token, &handle_class, NULL);
6741 mono_class_init (handle_class);
6742 mono_class_init (mono_class_from_mono_type (handle));
6745 mono_type_get_object (domain, handle);
6748 case MONO_PATCH_INFO_LDTOKEN: {
6750 MonoClass *handle_class;
6752 handle = mono_ldtoken (patch_info->data.token->image,
6753 patch_info->data.token->token, &handle_class, NULL);
6754 mono_class_init (handle_class);
6759 case MONO_PATCH_INFO_BB_OVF:
6760 case MONO_PATCH_INFO_EXC_OVF:
6763 g_assert_not_reached ();
6766 return (gpointer)target;
6770 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
6771 MonoJitICallInfo *info;
6773 decompose_foreach (tree, cfg);
6775 switch (mono_burg_arity [tree->opcode]) {
6778 dec_foreach (tree->inst_left, cfg);
6780 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6781 MonoInst *iargs [2];
6783 iargs [0] = tree->inst_left;
6785 mono_emulate_opcode (cfg, tree, iargs, info);
6791 #ifdef MONO_ARCH_BIGMUL_INTRINS
6792 if (tree->opcode == OP_LMUL
6793 && (cfg->opt & MONO_OPT_INTRINS)
6794 && (tree->inst_left->opcode == CEE_CONV_I8
6795 || tree->inst_left->opcode == CEE_CONV_U8)
6796 && tree->inst_left->inst_left->type == STACK_I4
6797 && (tree->inst_right->opcode == CEE_CONV_I8
6798 || tree->inst_right->opcode == CEE_CONV_U8)
6799 && tree->inst_right->inst_left->type == STACK_I4
6800 && tree->inst_left->opcode == tree->inst_right->opcode) {
6801 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
6802 tree->inst_left = tree->inst_left->inst_left;
6803 tree->inst_right = tree->inst_right->inst_left;
6804 dec_foreach (tree, cfg);
6807 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6808 MonoInst *iargs [2];
6810 iargs [0] = tree->inst_i0;
6811 iargs [1] = tree->inst_i1;
6813 mono_emulate_opcode (cfg, tree, iargs, info);
6815 dec_foreach (iargs [0], cfg);
6816 dec_foreach (iargs [1], cfg);
6819 dec_foreach (tree->inst_left, cfg);
6820 dec_foreach (tree->inst_right, cfg);
6824 g_assert_not_reached ();
6829 decompose_pass (MonoCompile *cfg) {
6832 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6835 cfg->prev_ins = NULL;
6836 for (tree = cfg->cbb->code; tree; tree = tree->next) {
6837 dec_foreach (tree, cfg);
6838 cfg->prev_ins = tree;
6844 nullify_basic_block (MonoBasicBlock *bb)
6851 bb->code = bb->last_ins = NULL;
6852 bb->cil_code = NULL;
6856 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6860 for (i = 0; i < bb->out_count; i++) {
6861 MonoBasicBlock *ob = bb->out_bb [i];
6864 if (bb->out_count > 1) {
6865 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
6869 bb->out_bb [i] = repl;
6876 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6880 for (i = 0; i < bb->in_count; i++) {
6881 MonoBasicBlock *ib = bb->in_bb [i];
6884 if (bb->in_count > 1) {
6885 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
6889 bb->in_bb [i] = repl;
6896 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6900 for (i = 0; i < bb->out_count; i++) {
6901 MonoBasicBlock *ob = bb->out_bb [i];
6902 for (j = 0; j < ob->in_count; j++) {
6903 if (ob->in_bb [j] == orig) {
6904 ob->in_bb [j] = repl;
6913 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
6915 bb->out_count = bbn->out_count;
6916 bb->out_bb = bbn->out_bb;
6918 replace_basic_block (bb, bbn, bb);
6922 bb->last_ins->next = bbn->code;
6923 bb->last_ins = bbn->last_ins;
6926 bb->code = bbn->code;
6927 bb->last_ins = bbn->last_ins;
6929 bb->next_bb = bbn->next_bb;
6930 nullify_basic_block (bbn);
6934 * Optimizes the branches on the Control Flow Graph
6938 optimize_branches (MonoCompile *cfg)
6940 int i, changed = FALSE;
6941 MonoBasicBlock *bb, *bbn;
6942 guint32 niterations;
6945 * Some crazy loops could cause the code below to go into an infinite
6946 * loop, see bug #53003 for an example. To prevent this, we put an upper
6947 * bound on the number of iterations.
6954 /* we skip the entry block (exit is handled specially instead ) */
6955 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
6957 /* dont touch code inside exception clauses */
6958 if (bb->region != -1)
6961 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
6962 if (cfg->verbose_level > 2)
6963 g_print ("nullify block triggered %d\n", bbn->block_num);
6965 bb->next_bb = bbn->next_bb;
6967 for (i = 0; i < bbn->out_count; i++)
6968 replace_in_block (bbn->out_bb [i], bbn, NULL);
6970 nullify_basic_block (bbn);
6974 if (bb->out_count == 1) {
6975 bbn = bb->out_bb [0];
6977 /* conditional branches where true and false targets are the same can be also replaced with CEE_BR */
6978 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins)) {
6979 bb->last_ins->opcode = CEE_BR;
6980 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
6982 if (cfg->verbose_level > 2)
6983 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
6986 if (bb->region == bbn->region && bb->next_bb == bbn) {
6987 /* the block are in sequence anyway ... */
6989 /* branches to the following block can be removed */
6990 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
6991 bb->last_ins->opcode = CEE_NOP;
6993 if (cfg->verbose_level > 2)
6994 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
6997 if (bbn->in_count == 1) {
6999 if (bbn != cfg->bb_exit) {
7000 if (cfg->verbose_level > 2)
7001 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
7002 merge_basic_blocks (bb, bbn);
7006 //mono_print_bb_code (bb);
7011 } while (changed && (niterations > 0));
7018 /* we skip the entry block (exit is handled specially instead ) */
7019 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
7021 /* dont touch code inside exception clauses */
7022 if (bb->region != -1)
7025 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
7026 if (cfg->verbose_level > 2) {
7027 g_print ("nullify block triggered %d\n", bbn->block_num);
7029 bb->next_bb = bbn->next_bb;
7031 for (i = 0; i < bbn->out_count; i++)
7032 replace_in_block (bbn->out_bb [i], bbn, NULL);
7034 nullify_basic_block (bbn);
7040 if (bb->out_count == 1) {
7041 bbn = bb->out_bb [0];
7043 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
7044 bbn = bb->last_ins->inst_target_bb;
7045 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7046 bbn->code->inst_target_bb->region == bb->region) {
7048 if (cfg->verbose_level > 2)
7049 g_print ("in %s branch to branch triggered %d -> %d\n", cfg->method->name,
7050 bb->block_num, bbn->block_num);
7052 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
7053 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
7058 } else if (bb->out_count == 2) {
7059 if (bb->last_ins && MONO_IS_COND_BRANCH_NOFP (bb->last_ins)) {
7060 bbn = bb->last_ins->inst_true_bb;
7061 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7062 bbn->code->inst_target_bb->region == bb->region) {
7063 if (cfg->verbose_level > 2)
7064 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
7065 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
7068 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
7070 replace_in_block (bbn, bb, NULL);
7072 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
7073 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7075 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7081 bbn = bb->last_ins->inst_false_bb;
7082 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7083 bbn->code->inst_target_bb->region == bb->region) {
7084 if (cfg->verbose_level > 2)
7085 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
7086 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
7089 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
7091 replace_in_block (bbn, bb, NULL);
7093 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
7094 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7096 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7104 } while (changed && (niterations > 0));
7109 mono_compile_create_vars (MonoCompile *cfg)
7111 MonoMethodSignature *sig;
7112 MonoMethodHeader *header;
7115 header = ((MonoMethodNormal *)cfg->method)->header;
7117 sig = cfg->method->signature;
7119 if (!MONO_TYPE_IS_VOID (sig->ret)) {
7120 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
7121 cfg->ret->opcode = OP_RETARG;
7122 cfg->ret->inst_vtype = sig->ret;
7123 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
7125 if (cfg->verbose_level > 2)
7126 g_print ("creating vars\n");
7129 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
7131 for (i = 0; i < sig->param_count; ++i)
7132 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
7134 cfg->locals_start = cfg->num_varinfo;
7136 if (cfg->verbose_level > 2)
7137 g_print ("creating locals\n");
7138 for (i = 0; i < header->num_locals; ++i)
7139 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
7140 if (cfg->verbose_level > 2)
7141 g_print ("locals done\n");
7145 mono_print_code (MonoCompile *cfg)
7149 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7150 MonoInst *tree = bb->code;
7155 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
7157 for (; tree; tree = tree->next) {
7158 mono_print_tree (tree);
7163 bb->last_ins->next = NULL;
7167 extern const char * const mono_burg_rule_string [];
7170 emit_state (MonoCompile *cfg, MBState *state, int goal)
7173 int ern = mono_burg_rule (state, goal);
7174 const guint16 *nts = mono_burg_nts [ern];
7177 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
7181 // state->reg1 = state->reg2; /* chain rule */
7183 state->reg1 = mono_regstate_next_int (cfg->rs);
7184 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
7187 state->reg1 = mono_regstate_next_int (cfg->rs);
7188 state->reg2 = mono_regstate_next_int (cfg->rs);
7191 state->reg1 = mono_regstate_next_float (cfg->rs);
7198 mono_burg_kids (state, ern, kids);
7200 emit_state (cfg, kids [0], nts [0]);
7202 emit_state (cfg, kids [1], nts [1]);
7204 g_assert (!nts [3]);
7205 emit_state (cfg, kids [2], nts [2]);
7210 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
7211 if ((emit = mono_burg_func [ern]))
7212 emit (state, state->tree, cfg);
7215 #define DEBUG_SELECTION
7218 mini_select_instructions (MonoCompile *cfg)
7220 static const int reverse_map [] = {
7221 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
7222 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
7224 static const int reverse_fmap [] = {
7225 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
7226 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
7228 static const int reverse_lmap [] = {
7229 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
7230 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
7235 cfg->state_pool = mono_mempool_new ();
7236 cfg->rs = mono_regstate_new ();
7238 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7239 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins) &&
7240 bb->next_bb != bb->last_ins->inst_false_bb) {
7242 /* we are careful when inverting, since bugs like #59580
7243 * could show up when dealing with NaNs.
7245 if (MONO_IS_COND_BRANCH_NOFP(bb->last_ins) && bb->next_bb == bb->last_ins->inst_true_bb) {
7246 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
7247 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
7248 bb->last_ins->inst_false_bb = tmp;
7250 if (bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
7251 bb->last_ins->opcode = reverse_map [bb->last_ins->opcode - CEE_BEQ];
7252 } else if (bb->last_ins->opcode >= OP_FBEQ && bb->last_ins->opcode <= OP_FBLT_UN) {
7253 bb->last_ins->opcode = reverse_fmap [bb->last_ins->opcode - OP_FBEQ];
7254 } else if (bb->last_ins->opcode >= OP_LBEQ && bb->last_ins->opcode <= OP_LBLT_UN) {
7255 bb->last_ins->opcode = reverse_lmap [bb->last_ins->opcode - OP_LBEQ];
7258 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
7259 inst->opcode = CEE_BR;
7260 inst->inst_target_bb = bb->last_ins->inst_false_bb;
7261 mono_bblock_add_inst (bb, inst);
7266 #ifdef DEBUG_SELECTION
7267 if (cfg->verbose_level >= 4) {
7268 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7269 MonoInst *tree = bb->code;
7270 g_print ("DUMP BLOCK %d:\n", bb->block_num);
7273 for (; tree; tree = tree->next) {
7274 mono_print_tree (tree);
7281 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7282 MonoInst *tree = bb->code, *next;
7288 bb->last_ins = NULL;
7291 mono_regstate_reset (cfg->rs);
7293 #ifdef DEBUG_SELECTION
7294 if (cfg->verbose_level >= 3)
7295 g_print ("LABEL BLOCK %d:\n", bb->block_num);
7297 for (; tree; tree = next) {
7299 #ifdef DEBUG_SELECTION
7300 if (cfg->verbose_level >= 3) {
7301 mono_print_tree (tree);
7306 if (!(mbstate = mono_burg_label (tree, cfg))) {
7307 g_warning ("unable to label tree %p", tree);
7308 mono_print_tree (tree);
7310 g_assert_not_reached ();
7312 emit_state (cfg, mbstate, MB_NTERM_stmt);
7314 bb->max_ireg = cfg->rs->next_vireg;
7315 bb->max_freg = cfg->rs->next_vfreg;
7318 bb->last_ins->next = NULL;
7320 mono_mempool_empty (cfg->state_pool);
7322 mono_mempool_destroy (cfg->state_pool);
7326 mono_codegen (MonoCompile *cfg)
7328 MonoJumpInfo *patch_info;
7330 int i, max_epilog_size;
7333 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7334 cfg->spill_count = 0;
7335 /* we reuse dfn here */
7336 /* bb->dfn = bb_count++; */
7337 mono_arch_local_regalloc (cfg, bb);
7340 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
7341 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
7343 code = mono_arch_emit_prolog (cfg);
7345 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7346 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
7348 cfg->code_len = code - cfg->native_code;
7349 cfg->prolog_end = cfg->code_len;
7351 mono_debug_open_method (cfg);
7353 /* emit code all basic blocks */
7354 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7355 bb->native_offset = cfg->code_len;
7356 mono_arch_output_basic_block (cfg, bb);
7358 cfg->bb_exit->native_offset = cfg->code_len;
7360 code = cfg->native_code + cfg->code_len;
7362 max_epilog_size = mono_arch_max_epilog_size (cfg);
7364 /* we always allocate code in cfg->domain->code_mp to increase locality */
7365 cfg->code_size = cfg->code_len + max_epilog_size;
7366 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
7368 mono_domain_lock (cfg->domain);
7369 code = mono_code_manager_reserve (cfg->domain->code_mp, cfg->code_size);
7370 mono_domain_unlock (cfg->domain);
7372 memcpy (code, cfg->native_code, cfg->code_len);
7373 g_free (cfg->native_code);
7374 cfg->native_code = code;
7375 code = cfg->native_code + cfg->code_len;
7377 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
7379 cfg->epilog_begin = cfg->code_len;
7381 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7382 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
7384 cfg->code_len = code - cfg->native_code;
7386 mono_arch_emit_epilog (cfg);
7388 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7389 switch (patch_info->type) {
7390 case MONO_PATCH_INFO_ABS: {
7391 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
7393 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
7394 if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) &&
7395 strstr (cfg->method->name, info->name))
7397 * This is an icall wrapper, and this is a call to the
7402 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
7403 patch_info->data.name = info->name;
7407 MonoVTable *vtable = mono_find_class_init_trampoline_by_addr (patch_info->data.target);
7409 patch_info->type = MONO_PATCH_INFO_CLASS_INIT;
7410 patch_info->data.klass = vtable->klass;
7415 case MONO_PATCH_INFO_SWITCH: {
7417 mono_domain_lock (cfg->domain);
7418 table = mono_code_manager_reserve (cfg->domain->code_mp, sizeof (gpointer) * patch_info->table_size);
7419 mono_domain_unlock (cfg->domain);
7421 patch_info->ip.i = patch_info->ip.label->inst_c0;
7422 for (i = 0; i < patch_info->table_size; i++) {
7423 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
7425 patch_info->data.target = table;
7434 if (cfg->verbose_level > 0)
7435 g_print ("Method %s emitted at %p to %p [%s]\n",
7436 mono_method_full_name (cfg->method, TRUE),
7437 cfg->native_code, cfg->native_code + cfg->code_len, cfg->domain->friendly_name);
7439 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info, cfg->run_cctors);
7441 mono_domain_lock (cfg->domain);
7442 mono_code_manager_commit (cfg->domain->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
7443 mono_domain_unlock (cfg->domain);
7445 mono_arch_flush_icache (cfg->native_code, cfg->code_len);
7447 mono_debug_close_method (cfg);
7451 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
7456 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
7457 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
7459 if (cp->opcode == OP_ICONST) {
7460 if (cfg->opt & MONO_OPT_CONSPROP) {
7461 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
7465 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
7466 if (cfg->opt & MONO_OPT_COPYPROP) {
7467 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
7473 arity = mono_burg_arity [tree->opcode];
7476 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
7477 if (cfg->opt & MONO_OPT_CFOLD)
7478 mono_constant_fold_inst (tree, NULL);
7479 /* The opcode may have changed */
7480 if (mono_burg_arity [tree->opcode] > 1) {
7481 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
7482 if (cfg->opt & MONO_OPT_CFOLD)
7483 mono_constant_fold_inst (tree, NULL);
7485 mono_constant_fold_inst (tree, NULL);
7491 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
7495 switch (tree->opcode) {
7505 if (tree->ssa_op == MONO_SSA_NOP) {
7506 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7523 case OP_VOIDCALL_REG:
7524 case OP_VOIDCALLVIRT:
7526 MonoCallInst *call = (MonoCallInst *)tree;
7527 MonoMethodSignature *sig = call->signature;
7528 int i, byref = FALSE;
7530 for (i = 0; i < sig->param_count; i++) {
7531 if (sig->params [i]->byref) {
7538 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7546 arity = mono_burg_arity [tree->opcode];
7552 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7555 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7556 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
7559 g_assert_not_reached ();
7564 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
7566 MonoInst *tree = bb->code;
7572 for (; tree; tree = tree->next) {
7574 mono_cprop_copy_values (cfg, tree, acp);
7576 mono_cprop_invalidate_values (tree, acp, acp_size);
7578 if (tree->ssa_op == MONO_SSA_STORE &&
7579 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
7580 MonoInst *i1 = tree->inst_i1;
7582 acp [tree->inst_i0->inst_c0] = NULL;
7584 for (i = 0; i < acp_size; i++) {
7585 if (acp [i] && acp [i]->opcode != OP_ICONST &&
7586 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
7591 if (i1->opcode == OP_ICONST) {
7592 acp [tree->inst_i0->inst_c0] = i1;
7593 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
7595 if (i1->ssa_op == MONO_SSA_LOAD &&
7596 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
7597 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
7598 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
7599 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
7604 if (tree->opcode == CEE_BEQ) {
7605 g_assert (tree->inst_i0->opcode == OP_COMPARE);
7606 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
7607 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
7609 tree->opcode = CEE_BR;
7610 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
7611 tree->inst_target_bb = tree->inst_true_bb;
7613 tree->inst_target_bb = tree->inst_false_bb;
7622 mono_local_cprop (MonoCompile *cfg)
7627 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
7629 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7630 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
7631 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
7636 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, int parts)
7638 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
7639 guint8 *ip = (guint8 *)header->code;
7642 int dfn = 0, i, code_size_ratio;
7644 mono_jit_stats.methods_compiled++;
7645 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
7646 mono_profiler_method_jit (method);
7648 cfg = g_new0 (MonoCompile, 1);
7649 cfg->method = method;
7650 cfg->mempool = mono_mempool_new ();
7652 cfg->prof_options = mono_profiler_get_events ();
7653 cfg->run_cctors = run_cctors;
7654 cfg->bb_hash = g_hash_table_new (NULL, NULL);
7655 cfg->domain = domain;
7656 cfg->verbose_level = mini_verbose;
7657 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
7658 ((MonoMethodNormal *)method)->header->max_stack);
7660 if (cfg->verbose_level > 2)
7661 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
7664 * create MonoInst* which represents arguments and local variables
7666 mono_compile_create_vars (cfg);
7668 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
7669 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7670 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
7671 mono_destroy_compile (cfg);
7675 mono_jit_stats.basic_blocks += cfg->num_bblocks;
7676 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
7678 if (cfg->num_varinfo > 2000) {
7680 * we disable some optimizations if there are too many variables
7681 * because JIT time may become too expensive. The actual number needs
7682 * to be tweaked and eventually the non-linear algorithms should be fixed.
7684 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
7685 cfg->disable_ssa = TRUE;
7687 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
7689 /* Depth-first ordering on basic blocks */
7690 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
7692 if (cfg->opt & MONO_OPT_BRANCH)
7693 optimize_branches (cfg);
7695 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
7696 if (cfg->num_bblocks != dfn + 1) {
7699 cfg->num_bblocks = dfn + 1;
7701 if (!header->clauses) {
7702 /* remove unreachable code, because the code in them may be
7703 * inconsistent (access to dead variables for example) */
7704 for (bb = cfg->bb_entry; bb;) {
7705 MonoBasicBlock *bbn = bb->next_bb;
7707 if (bbn && bbn->region == -1 && !bbn->dfn) {
7708 if (cfg->verbose_level > 1)
7709 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
7710 bb->next_bb = bbn->next_bb;
7711 nullify_basic_block (bbn);
7719 if (cfg->opt & MONO_OPT_LOOP) {
7720 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
7721 mono_compute_natural_loops (cfg);
7724 /* after method_to_ir */
7728 //#define DEBUGSSA "logic_run"
7729 #define DEBUGSSA_CLASS "Tests"
7732 if (!header->num_clauses && !cfg->disable_ssa) {
7733 mono_local_cprop (cfg);
7734 mono_ssa_compute (cfg);
7738 /* fixme: add all optimizations which requires SSA */
7739 if (cfg->opt & (MONO_OPT_DEADCE | MONO_OPT_ABCREM)) {
7740 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
7741 mono_local_cprop (cfg);
7742 mono_ssa_compute (cfg);
7744 if (cfg->verbose_level >= 2) {
7751 /* after SSA translation */
7755 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
7756 if (cfg->comp_done & MONO_COMP_SSA) {
7757 mono_ssa_cprop (cfg);
7759 mono_local_cprop (cfg);
7763 if (cfg->comp_done & MONO_COMP_SSA) {
7764 mono_ssa_deadce (cfg);
7766 //mono_ssa_strength_reduction (cfg);
7768 if ((cfg->flags & MONO_CFG_HAS_LDELEMA) && (cfg->opt & MONO_OPT_ABCREM))
7769 mono_perform_abc_removal (cfg);
7771 mono_ssa_remove (cfg);
7773 if (cfg->opt & MONO_OPT_BRANCH)
7774 optimize_branches (cfg);
7777 /* after SSA removal */
7781 decompose_pass (cfg);
7783 if (cfg->opt & MONO_OPT_LINEARS) {
7786 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
7787 cfg->comp_done &= ~MONO_COMP_LIVENESS;
7788 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
7789 mono_analyze_liveness (cfg);
7791 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
7792 regs = mono_arch_get_global_int_regs (cfg);
7793 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
7797 //mono_print_code (cfg);
7801 /* variables are allocated after decompose, since decompose could create temps */
7802 mono_arch_allocate_vars (cfg);
7804 if (cfg->opt & MONO_OPT_CFOLD)
7805 mono_constant_fold (cfg);
7807 mini_select_instructions (cfg);
7810 if (cfg->verbose_level >= 2) {
7811 char *id = mono_method_full_name (cfg->method, FALSE);
7812 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
7816 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
7818 jinfo = g_new0 (MonoJitInfo, 1);
7819 jinfo->method = method;
7820 jinfo->code_start = cfg->native_code;
7821 jinfo->code_size = cfg->code_len;
7822 jinfo->used_regs = cfg->used_int_regs;
7823 jinfo->domain_neutral = (cfg->opt & MONO_OPT_SHARED) != 0;
7825 if (header->num_clauses) {
7828 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
7829 jinfo->num_clauses = header->num_clauses;
7830 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
7831 sizeof (MonoJitExceptionInfo) * header->num_clauses);
7833 for (i = 0; i < header->num_clauses; i++) {
7834 MonoExceptionClause *ec = &header->clauses [i];
7835 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
7836 MonoBasicBlock *tblock;
7838 ei->flags = ec->flags;
7840 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7841 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->token_or_filter);
7843 ei->data.filter = cfg->native_code + tblock->native_offset;
7845 ei->data.token = ec->token_or_filter;
7848 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
7850 ei->try_start = cfg->native_code + tblock->native_offset;
7851 g_assert (tblock->native_offset);
7852 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
7854 ei->try_end = cfg->native_code + tblock->native_offset;
7855 g_assert (tblock->native_offset);
7856 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
7858 ei->handler_start = cfg->native_code + tblock->native_offset;
7862 cfg->jit_info = jinfo;
7864 mono_jit_info_table_add (cfg->domain, jinfo);
7866 /* collect statistics */
7867 mono_jit_stats.allocated_code_size += cfg->code_len;
7868 code_size_ratio = cfg->code_len;
7869 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
7870 mono_jit_stats.biggest_method_size = code_size_ratio;
7871 mono_jit_stats.biggest_method = method;
7873 code_size_ratio = (code_size_ratio * 100) / ((MonoMethodNormal *)method)->header->code_size;
7874 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
7875 mono_jit_stats.max_code_size_ratio = code_size_ratio;
7876 mono_jit_stats.max_ratio_method = method;
7878 mono_jit_stats.native_code_size += cfg->code_len;
7880 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7881 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
7887 mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain)
7890 GHashTable *jit_code_hash;
7891 gpointer code = NULL;
7897 jit_code_hash = target_domain->jit_code_hash;
7899 #ifdef MONO_USE_AOT_COMPILER
7900 if (!mono_compile_aot && (opt & MONO_OPT_AOT)) {
7902 MonoDomain *domain = mono_domain_get ();
7904 mono_domain_lock (domain);
7906 mono_class_init (method->klass);
7907 if ((info = mono_aot_get_method (domain, method))) {
7908 g_hash_table_insert (domain->jit_code_hash, method, info);
7909 mono_domain_unlock (domain);
7910 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
7911 return info->code_start;
7914 mono_domain_unlock (domain);
7918 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
7919 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
7922 if (!method->addr) {
7923 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
7924 method->addr = mono_lookup_internal_call (method);
7926 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
7927 mono_lookup_pinvoke_call (method, NULL, NULL);
7929 nm = mono_marshal_get_native_wrapper (method);
7930 return mono_compile_method (nm);
7932 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
7933 //mono_debug_add_wrapper (method, nm);
7934 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
7935 const char *name = method->name;
7938 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
7939 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
7940 /* FIXME: uhm, we need a wrapper to handle exceptions? */
7941 return (gpointer)mono_delegate_ctor;
7942 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
7943 nm = mono_marshal_get_delegate_invoke (method);
7944 return mono_jit_compile_method (nm);
7945 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
7946 nm = mono_marshal_get_delegate_begin_invoke (method);
7947 return mono_jit_compile_method (nm);
7948 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
7949 nm = mono_marshal_get_delegate_end_invoke (method);
7950 return mono_jit_compile_method (nm);
7956 cfg = mini_method_compile (method, opt, target_domain, TRUE, 0);
7958 mono_domain_lock (target_domain);
7960 /* Check if some other thread already did the job. In this case, we can
7961 discard the code this thread generated. */
7963 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
7964 /* We can't use a domain specific method in another domain */
7965 if ((target_domain == mono_domain_get ()) || info->domain_neutral) {
7966 code = info->code_start;
7967 // printf("Discarding code for method %s\n", method->name);
7972 g_hash_table_insert (jit_code_hash, method, cfg->jit_info);
7973 code = cfg->native_code;
7976 mono_destroy_compile (cfg);
7978 if (target_domain->jump_target_hash) {
7979 MonoJumpInfo patch_info;
7981 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
7983 patch_info.next = NULL;
7984 patch_info.ip.i = 0;
7985 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
7986 patch_info.data.method = method;
7987 g_hash_table_remove (target_domain->jump_target_hash, method);
7989 for (tmp = list; tmp; tmp = tmp->next)
7990 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info, TRUE);
7991 g_slist_free (list);
7994 mono_domain_unlock (target_domain);
7996 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
8001 mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt)
8003 /* FIXME: later copy the code from mono */
8004 MonoDomain *target_domain, *domain = mono_domain_get ();
8008 if (opt & MONO_OPT_SHARED)
8009 target_domain = mono_get_root_domain ();
8011 target_domain = domain;
8013 mono_domain_lock (target_domain);
8015 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
8016 /* We can't use a domain specific method in another domain */
8017 if (! ((domain != target_domain) && !info->domain_neutral)) {
8018 mono_domain_unlock (target_domain);
8019 mono_jit_stats.methods_lookups++;
8020 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
8021 return info->code_start;
8025 mono_domain_unlock (target_domain);
8026 p = mono_jit_compile_method_inner (method, target_domain);
8031 mono_jit_compile_method (MonoMethod *method)
8033 return mono_jit_compile_method_with_opt (method, default_opt);
8037 mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method)
8039 MonoDomain *target_domain;
8042 if (default_opt & MONO_OPT_SHARED)
8043 target_domain = mono_get_root_domain ();
8045 target_domain = domain;
8047 mono_domain_lock (target_domain);
8049 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
8050 /* We can't use a domain specific method in another domain */
8051 if (! ((domain != target_domain) && !info->domain_neutral)) {
8052 mono_domain_unlock (target_domain);
8053 mono_jit_stats.methods_lookups++;
8054 return info->code_start;
8058 mono_domain_unlock (target_domain);
8064 * mono_jit_runtime_invoke:
8065 * @method: the method to invoke
8066 * @obj: this pointer
8067 * @params: array of parameter values.
8068 * @exc: used to catch exceptions objects
8071 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
8074 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc, void* compiled_method);
8075 void* compiled_method;
8077 if (obj == NULL && !(method->flags & METHOD_ATTRIBUTE_STATIC) && !method->string_ctor && (method->wrapper_type == 0)) {
8078 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
8082 invoke = mono_marshal_get_runtime_invoke (method);
8083 runtime_invoke = mono_jit_compile_method (invoke);
8085 /* We need this here becuase mono_marshal_get_runtime_invoke can be place
8086 * the helper method in System.Object and not the target class
8088 mono_runtime_class_init (mono_class_vtable (mono_domain_get (), method->klass));
8090 compiled_method = mono_jit_compile_method (method);
8091 return runtime_invoke (obj, params, exc, compiled_method);
8094 #ifdef PLATFORM_WIN32
8095 #define GET_CONTEXT \
8096 struct sigcontext *ctx = (struct sigcontext*)_dummy;
8099 #define GET_CONTEXT \
8100 void *ctx = context;
8101 #elif defined(sun) // Solaris x86
8102 #define GET_CONTEXT \
8103 ucontext_t *uctx = context; \
8104 struct sigcontext *ctx = (struct sigcontext *)&(uctx->uc_mcontext);
8105 #elif defined(__ppc__) || defined (__powerpc__) || defined (__s390__)
8106 #define GET_CONTEXT \
8107 void *ctx = context;
8109 #define GET_CONTEXT \
8110 void **_p = (void **)&_dummy; \
8111 struct sigcontext *ctx = (struct sigcontext *)++_p;
8115 #ifdef MONO_ARCH_USE_SIGACTION
8116 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, siginfo_t *info, void *context)
8118 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy)
8122 SIG_HANDLER_SIGNATURE (sigfpe_signal_handler)
8127 exc = mono_get_exception_divide_by_zero ();
8129 mono_arch_handle_exception (ctx, exc, FALSE);
8133 SIG_HANDLER_SIGNATURE (sigill_signal_handler)
8137 exc = mono_get_exception_execution_engine ("SIGILL");
8139 mono_arch_handle_exception (ctx, exc, FALSE);
8142 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8145 sigsegv_signal_handler (int _dummy, siginfo_t *info, void *context)
8148 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
8149 struct sigcontext *ctx = (struct sigcontext *)&(((ucontext_t*)context)->uc_mcontext);
8151 /* Can't allocate memory using Boehm GC on altstack */
8152 if (jit_tls->stack_size &&
8153 ((guint8*)info->si_addr >= (guint8*)jit_tls->end_of_stack - jit_tls->stack_size) &&
8154 ((guint8*)info->si_addr < (guint8*)jit_tls->end_of_stack))
8155 exc = mono_domain_get ()->stack_overflow_ex;
8157 exc = mono_domain_get ()->null_reference_ex;
8159 mono_arch_handle_exception (ctx, exc, FALSE);
8165 SIG_HANDLER_SIGNATURE (sigsegv_signal_handler)
8169 mono_arch_handle_exception (ctx, NULL, FALSE);
8175 SIG_HANDLER_SIGNATURE (sigusr1_signal_handler)
8177 gboolean running_managed;
8182 running_managed = (mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx)) != NULL);
8184 exc = mono_thread_request_interruption (running_managed);
8187 mono_arch_handle_exception (ctx, exc, FALSE);
8191 SIG_HANDLER_SIGNATURE (sigquit_signal_handler)
8196 exc = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
8198 mono_arch_handle_exception (ctx, exc, FALSE);
8202 SIG_HANDLER_SIGNATURE (sigint_signal_handler)
8207 exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
8209 mono_arch_handle_exception (ctx, exc, FALSE);
8212 #ifndef PLATFORM_WIN32
8214 add_signal_handler (int signo, gpointer handler)
8216 struct sigaction sa;
8218 #ifdef MONO_ARCH_USE_SIGACTION
8219 sa.sa_sigaction = handler;
8220 sigemptyset (&sa.sa_mask);
8221 sa.sa_flags = SA_SIGINFO;
8223 sa.sa_handler = handler;
8224 sigemptyset (&sa.sa_mask);
8227 g_assert (sigaction (signo, &sa, NULL) != -1);
8232 mono_runtime_install_handlers (void)
8234 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8235 struct sigaction sa;
8238 #ifdef PLATFORM_WIN32
8240 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
8241 win32_seh_set_handler(SIGILL, sigill_signal_handler);
8242 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
8243 if (getenv ("MONO_DEBUG"))
8244 win32_seh_set_handler(SIGINT, sigint_signal_handler);
8245 #else /* !PLATFORM_WIN32 */
8247 /* libpthreads has its own implementation of sigaction(),
8248 * but it seems to work well with our current exception
8249 * handlers. If not we must call syscall directly instead
8252 if (getenv ("MONO_DEBUG")) {
8253 add_signal_handler (SIGINT, sigint_signal_handler);
8256 add_signal_handler (SIGFPE, sigfpe_signal_handler);
8257 add_signal_handler (SIGQUIT, sigquit_signal_handler);
8258 add_signal_handler (SIGILL, sigill_signal_handler);
8259 add_signal_handler (SIGBUS, sigsegv_signal_handler);
8260 add_signal_handler (mono_thread_get_abort_signal (), sigusr1_signal_handler);
8263 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8264 sa.sa_sigaction = sigsegv_signal_handler;
8265 sigemptyset (&sa.sa_mask);
8266 sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
8267 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
8269 add_signal_handler (SIGSEGV, sigsegv_signal_handler);
8272 #endif /* PLATFORM_WIN32 */
8275 /* mono_jit_create_remoting_trampoline:
8276 * @method: pointer to the method info
8278 * Creates a trampoline which calls the remoting functions. This
8279 * is used in the vtable of transparent proxies.
8281 * Returns: a pointer to the newly created code
8284 mono_jit_create_remoting_trampoline (MonoMethod *method)
8287 guint8 *addr = NULL;
8289 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
8290 (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
8291 nm = mono_marshal_get_remoting_invoke (method);
8292 addr = mono_compile_method (nm);
8294 addr = mono_compile_method (method);
8300 mini_init (const char *filename)
8304 mono_arch_cpu_init ();
8306 if (!g_thread_supported ())
8307 g_thread_init (NULL);
8309 mono_jit_tls_id = TlsAlloc ();
8310 setup_jit_tls_data ((gpointer)-1, mono_thread_abort);
8312 InitializeCriticalSection (&trampoline_hash_mutex);
8316 if (default_opt & MONO_OPT_AOT)
8319 mono_runtime_install_handlers ();
8320 mono_threads_install_cleanup (mini_thread_cleanup);
8322 #define JIT_TRAMPOLINES_WORK
8323 #ifdef JIT_TRAMPOLINES_WORK
8324 mono_install_compile_method (mono_jit_compile_method);
8325 mono_install_trampoline (mono_arch_create_jit_trampoline);
8326 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
8328 #define JIT_INVOKE_WORKS
8329 #ifdef JIT_INVOKE_WORKS
8330 mono_install_runtime_invoke (mono_jit_runtime_invoke);
8331 mono_install_handler (mono_arch_get_throw_exception ());
8333 mono_install_stack_walk (mono_jit_walk_stack);
8335 domain = mono_init (filename);
8338 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
8339 ves_icall_get_frame_info);
8340 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
8341 ves_icall_get_trace);
8342 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
8343 mono_runtime_install_handlers);
8346 create_helper_signature ();
8348 #define JIT_CALLS_WORK
8349 #ifdef JIT_CALLS_WORK
8350 /* Needs to be called here since register_jit_icall depends on it */
8351 mono_marshal_init ();
8353 mono_arch_register_lowlevel_calls ();
8354 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
8355 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
8356 mono_register_jit_icall (mono_trace_enter_method, "mono_trace_enter_method", NULL, TRUE);
8357 mono_register_jit_icall (mono_trace_leave_method, "mono_trace_leave_method", NULL, TRUE);
8358 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
8359 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
8361 /* fixme: we cant handle vararg methods this way, because the signature is not constant */
8362 //mono_register_jit_icall (ves_array_element_address, "ves_array_element_address", NULL);
8363 //mono_register_jit_icall (mono_array_new_va, "mono_array_new_va", NULL);
8365 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
8366 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
8367 helper_sig_void_ptr, TRUE);
8368 mono_register_jit_icall (mono_thread_get_pending_exception, "mono_thread_get_pending_exception", helper_sig_obj_void, FALSE);
8371 * NOTE, NOTE, NOTE, NOTE:
8372 * when adding emulation for some opcodes, remember to also add a dummy
8373 * rule to the burg files, because we need the arity information to be correct.
8375 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult, TRUE);
8376 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un, FALSE);
8377 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf, FALSE);
8378 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv, FALSE);
8379 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un, FALSE);
8380 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem, FALSE);
8381 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un, FALSE);
8383 #ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
8384 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl, TRUE);
8385 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr, TRUE);
8386 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un, TRUE);
8389 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8, FALSE);
8390 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4, FALSE);
8391 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8, FALSE);
8392 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8, FALSE);
8394 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
8395 mono_register_opcode_emulation (OP_FCONV_TO_I8, "__emul_fconv_to_i8", helper_sig_long_double, mono_fconv_i8, FALSE);
8397 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
8398 mono_register_opcode_emulation (CEE_CONV_R_UN, "__emul_conv_r_un", helper_sig_double_int, mono_conv_to_r8_un, FALSE);
8400 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
8401 mono_register_opcode_emulation (OP_LCONV_TO_R8, "__emul_lconv_to_r8", helper_sig_double_long, mono_lconv_to_r8, FALSE);
8403 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
8404 mono_register_opcode_emulation (OP_LCONV_TO_R4, "__emul_lconv_to_r4", helper_sig_float_long, mono_lconv_to_r4, FALSE);
8406 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
8407 mono_register_opcode_emulation (OP_LCONV_TO_R_UN, "__emul_lconv_to_r8_un", helper_sig_double_long, mono_lconv_to_r8_un, FALSE);
8409 #ifdef MONO_ARCH_EMULATE_FREM
8410 mono_register_opcode_emulation (OP_FREM, "__emul_frem", helper_sig_double_double_double, fmod, FALSE);
8413 #if SIZEOF_VOID_P == 4
8414 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4, TRUE);
8417 #warning "fixme: add opcode emulation"
8421 /* other jit icalls */
8422 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
8423 helper_sig_ptr_ptr_ptr, FALSE);
8424 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
8425 mono_register_jit_icall (mono_get_special_static_data, "mono_get_special_static_data", helper_sig_ptr_int, FALSE);
8426 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
8427 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
8428 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
8429 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
8430 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
8431 mono_register_jit_icall (helper_stelem_ref_check, "helper_stelem_ref_check", helper_sig_stelem_ref_check, FALSE);
8432 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
8433 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
8434 mono_register_jit_icall (mono_object_new_fast, "mono_object_new_fast", helper_sig_object_new_specific, FALSE);
8435 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
8436 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
8437 mono_register_jit_icall (mono_string_to_utf16, "mono_string_to_utf16", helper_sig_ptr_obj, FALSE);
8438 mono_register_jit_icall (mono_string_from_utf16, "mono_string_from_utf16", helper_sig_obj_ptr, FALSE);
8439 mono_register_jit_icall (mono_string_new_wrapper, "mono_string_new_wrapper", helper_sig_obj_ptr, FALSE);
8440 mono_register_jit_icall (mono_string_to_utf8, "mono_string_to_utf8", helper_sig_ptr_obj, FALSE);
8441 mono_register_jit_icall (mono_string_to_bstr, "mono_string_to_bstr", helper_sig_ptr_obj, FALSE);
8442 mono_register_jit_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", helper_sig_ptr_obj, FALSE);
8443 mono_register_jit_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", helper_sig_ptr_obj, FALSE);
8444 mono_register_jit_icall (mono_string_builder_to_utf16, "mono_string_builder_to_utf16", helper_sig_ptr_obj, FALSE);
8445 mono_register_jit_icall (mono_array_to_savearray, "mono_array_to_savearray", helper_sig_ptr_obj, FALSE);
8446 mono_register_jit_icall (mono_array_to_lparray, "mono_array_to_lparray", helper_sig_ptr_obj, FALSE);
8447 mono_register_jit_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", helper_sig_ptr_obj, FALSE);
8448 mono_register_jit_icall (mono_ftnptr_to_delegate, "mono_ftnptr_to_delegate", helper_sig_obj_ptr_ptr, FALSE);
8449 mono_register_jit_icall (mono_marshal_string_array, "mono_marshal_string_array", helper_sig_ptr_obj, FALSE);
8450 mono_register_jit_icall (mono_marshal_string_array_to_unicode, "mono_marshal_string_array_to_unicode", helper_sig_ptr_obj, FALSE);
8451 mono_register_jit_icall (mono_marshal_asany, "mono_marshal_asany", helper_sig_ptr_obj_int, FALSE);
8452 mono_register_jit_icall (mono_marshal_free_asany, "mono_marshal_free_asany", helper_sig_void_obj_ptr_int, FALSE);
8453 mono_register_jit_icall (mono_string_utf8_to_builder, "mono_string_utf8_to_builder", helper_sig_void_ptr_ptr, FALSE);
8454 mono_register_jit_icall (mono_string_utf16_to_builder, "mono_string_utf16_to_builder", helper_sig_void_ptr_ptr, FALSE);
8455 mono_register_jit_icall (mono_marshal_free_array, "mono_marshal_free_array", helper_sig_void_ptr_ptr, FALSE);
8456 mono_register_jit_icall (mono_string_to_byvalstr, "mono_string_to_byvalstr", helper_sig_void_ptr_ptr_ptr, FALSE);
8457 mono_register_jit_icall (mono_string_to_byvalwstr, "mono_string_to_byvalwstr", helper_sig_void_ptr_ptr_ptr, FALSE);
8458 mono_register_jit_icall (g_free, "g_free", helper_sig_void_ptr, FALSE);
8459 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
8460 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
8461 mono_register_jit_icall (mono_ldftn_nosync, "mono_ldftn_nosync", helper_sig_compile, FALSE);
8462 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
8465 #define JIT_RUNTIME_WORKS
8466 #ifdef JIT_RUNTIME_WORKS
8467 mono_runtime_install_cleanup ((MonoDomainFunc)mini_cleanup);
8468 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
8471 mono_thread_attach (domain);
8475 MonoJitStats mono_jit_stats = {0};
8478 print_jit_stats (void)
8480 if (mono_jit_stats.enabled) {
8481 g_print ("Mono Jit statistics\n");
8482 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
8483 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
8484 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
8485 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
8486 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
8487 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
8488 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
8489 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
8490 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
8491 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
8492 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
8493 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
8494 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
8495 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
8496 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
8497 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
8498 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
8499 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
8501 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
8502 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
8503 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
8504 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
8505 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
8507 g_print ("\nGeneric instances: %ld\n", mono_stats.generic_instance_count);
8508 g_print ("Inflated methods: %ld\n", mono_stats.inflated_method_count);
8509 g_print ("Inflated types: %ld\n", mono_stats.inflated_type_count);
8510 g_print ("Generics metadata size: %ld\n", mono_stats.generics_metadata_size);
8515 mini_cleanup (MonoDomain *domain)
8518 * mono_runtime_cleanup() and mono_domain_finalize () need to
8519 * be called early since they need the execution engine still
8520 * fully working (mono_domain_finalize may invoke managed finalizers
8521 * and mono_runtime_cleanup will wait for other threads to finish).
8523 mono_domain_finalize (domain, 2000);
8525 mono_runtime_cleanup (domain);
8527 mono_profiler_shutdown ();
8529 mono_debug_cleanup ();
8531 #ifdef PLATFORM_WIN32
8532 win32_seh_cleanup();
8535 mono_domain_free (domain, TRUE);
8541 mono_set_defaults (int verbose_level, guint32 opts)
8543 mini_verbose = verbose_level;
8548 mono_precompile_assembly (MonoAssembly *ass, void *user_data)
8550 MonoImage *image = mono_assembly_get_image (ass);
8551 MonoMethod *method, *invoke;
8554 if (mini_verbose > 0)
8555 printf ("PRECOMPILE: %s.\n", mono_image_get_filename (image));
8557 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
8558 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
8559 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
8563 if (mini_verbose > 1) {
8564 char * desc = mono_method_full_name (method, TRUE);
8565 g_print ("Compiling %d %s\n", count, desc);
8568 mono_compile_method (method);
8569 if (strcmp (method->name, "Finalize") == 0) {
8570 invoke = mono_marshal_get_runtime_invoke (method);
8571 mono_compile_method (invoke);
8573 if (method->klass->marshalbyref && method->signature->hasthis) {
8574 invoke = mono_marshal_get_remoting_invoke_with_check (method);
8575 mono_compile_method (invoke);
8580 void mono_precompile_assemblies ()
8582 mono_assembly_foreach ((GFunc)mono_precompile_assembly, NULL);