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 HAVE_VALGRIND_MEMCHECK_H
17 #include <valgrind/memcheck.h>
20 #include <mono/metadata/assembly.h>
21 #include <mono/metadata/loader.h>
22 #include <mono/metadata/cil-coff.h>
23 #include <mono/metadata/tabledefs.h>
24 #include <mono/metadata/class.h>
25 #include <mono/metadata/object.h>
26 #include <mono/metadata/exception.h>
27 #include <mono/metadata/opcodes.h>
28 #include <mono/metadata/mono-endian.h>
29 #include <mono/metadata/tokentype.h>
30 #include <mono/metadata/tabledefs.h>
31 #include <mono/metadata/threads.h>
32 #include <mono/metadata/marshal.h>
33 #include <mono/metadata/socket-io.h>
34 #include <mono/metadata/appdomain.h>
35 #include <mono/metadata/debug-helpers.h>
36 #include <mono/io-layer/io-layer.h>
37 #include "mono/metadata/profiler.h"
38 #include <mono/metadata/profiler-private.h>
39 #include <mono/metadata/mono-config.h>
40 #include <mono/metadata/environment.h>
41 #include <mono/metadata/mono-debug.h>
42 #include <mono/metadata/mono-debug-debugger.h>
43 #include <mono/metadata/monitor.h>
51 #include "jit-icalls.c"
54 * this is used to determine when some branch optimizations are possible: we exclude FP compares
55 * because they have weird semantics with NaNs.
57 #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))
58 #define MONO_IS_COND_BRANCH_NOFP(ins) (MONO_IS_COND_BRANCH_OP(ins) && (ins)->inst_left->inst_left->type != STACK_R8)
60 #define MONO_CHECK_THIS(ins) (cfg->method->signature->hasthis && (ins)->ssa_op == MONO_SSA_LOAD && (ins)->inst_left->inst_c0 == 0)
62 gboolean mono_arch_print_tree(MonoInst *tree, int arity);
63 static gpointer mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt);
64 static gpointer mono_jit_compile_method (MonoMethod *method);
65 static gpointer mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method);
67 static void handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src,
68 const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native);
70 static void dec_foreach (MonoInst *tree, MonoCompile *cfg);
72 static int mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
73 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
74 guint inline_offset, gboolean is_virtual_call);
76 extern guint8 mono_burg_arity [];
77 /* helper methods signature */
78 static MonoMethodSignature *helper_sig_long_long_long = NULL;
79 static MonoMethodSignature *helper_sig_long_long_int = NULL;
80 static MonoMethodSignature *helper_sig_newarr = NULL;
81 static MonoMethodSignature *helper_sig_newarr_specific = NULL;
82 static MonoMethodSignature *helper_sig_ldstr = NULL;
83 static MonoMethodSignature *helper_sig_domain_get = NULL;
84 static MonoMethodSignature *helper_sig_object_new = NULL;
85 static MonoMethodSignature *helper_sig_object_new_specific = NULL;
86 static MonoMethodSignature *helper_sig_compile = NULL;
87 static MonoMethodSignature *helper_sig_compile_virt = NULL;
88 static MonoMethodSignature *helper_sig_obj_ptr = NULL;
89 static MonoMethodSignature *helper_sig_obj_ptr_ptr = NULL;
90 static MonoMethodSignature *helper_sig_obj_void = NULL;
91 static MonoMethodSignature *helper_sig_ptr_void = NULL;
92 static MonoMethodSignature *helper_sig_void_ptr = NULL;
93 static MonoMethodSignature *helper_sig_void_obj = NULL;
94 static MonoMethodSignature *helper_sig_void_obj_ptr_int = NULL;
95 static MonoMethodSignature *helper_sig_void_ptr_ptr = NULL;
96 static MonoMethodSignature *helper_sig_void_ptr_ptr_ptr = NULL;
97 static MonoMethodSignature *helper_sig_ptr_ptr_ptr = NULL;
98 static MonoMethodSignature *helper_sig_ptr_obj = NULL;
99 static MonoMethodSignature *helper_sig_ptr_obj_int = NULL;
100 static MonoMethodSignature *helper_sig_ptr_int = NULL;
101 static MonoMethodSignature *helper_sig_initobj = NULL;
102 static MonoMethodSignature *helper_sig_memcpy = NULL;
103 static MonoMethodSignature *helper_sig_memset = NULL;
104 static MonoMethodSignature *helper_sig_ulong_double = NULL;
105 static MonoMethodSignature *helper_sig_long_double = NULL;
106 static MonoMethodSignature *helper_sig_double_long = NULL;
107 static MonoMethodSignature *helper_sig_double_int = NULL;
108 static MonoMethodSignature *helper_sig_float_long = NULL;
109 static MonoMethodSignature *helper_sig_double_double_double = NULL;
110 static MonoMethodSignature *helper_sig_uint_double = NULL;
111 static MonoMethodSignature *helper_sig_int_double = NULL;
112 static MonoMethodSignature *helper_sig_stelem_ref = NULL;
113 static MonoMethodSignature *helper_sig_stelem_ref_check = NULL;
114 static MonoMethodSignature *helper_sig_class_init_trampoline = NULL;
116 static guint32 default_opt = MONO_OPT_PEEPHOLE;
118 guint32 mono_jit_tls_id = -1;
119 MonoTraceSpec *mono_jit_trace_calls = NULL;
120 gboolean mono_break_on_exc = FALSE;
121 gboolean mono_compile_aot = FALSE;
123 static int mini_verbose = 0;
125 static CRITICAL_SECTION trampoline_hash_mutex;
127 static GHashTable *class_init_hash_addr = NULL;
129 static GHashTable *jump_trampoline_hash = NULL;
132 mono_running_on_valgrind (void)
134 #ifdef HAVE_VALGRIND_MEMCHECK_H
135 if (RUNNING_ON_VALGRIND)
145 G_GNUC_UNUSED static void
146 print_method_from_ip (void *ip)
151 MonoDomain *domain = mono_domain_get ();
153 ji = mono_jit_info_table_find (domain, ip);
155 g_print ("No method at %p\n", ip);
158 method = mono_method_full_name (ji->method, TRUE);
159 source = mono_debug_source_location_from_address (ji->method, (int) ip, NULL, domain);
161 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);
164 g_print ("%s\n", source);
172 * mono_method_same_domain:
174 * Determine whenever two compiled methods are in the same domain, thus
175 * the address of the callee can be embedded in the caller.
177 gboolean mono_method_same_domain (MonoJitInfo *caller, MonoJitInfo *callee)
179 if (!caller || !callee)
183 * If the call was made from domain-neutral to domain-specific
184 * code, we can't patch the call site.
186 if (caller->domain_neutral && !callee->domain_neutral)
189 if ((caller->method->klass == mono_defaults.appdomain_class) &&
190 (strstr (caller->method->name, "InvokeInDomain"))) {
191 /* The InvokeInDomain methods change the current appdomain */
199 mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token)
201 MonoJumpInfoToken *res = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoToken));
208 #define MONO_INIT_VARINFO(vi,id) do { \
209 (vi)->range.first_use.pos.bid = 0xffff; \
215 * Basic blocks have two numeric identifiers:
216 * dfn: Depth First Number
217 * block_num: unique ID assigned at bblock creation
219 #define NEW_BBLOCK(cfg) (mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)))
220 #define ADD_BBLOCK(cfg,bbhash,b) do { \
221 g_hash_table_insert (bbhash, (b)->cil_code, (b)); \
222 (b)->block_num = cfg->num_bblocks++; \
223 (b)->real_offset = real_offset; \
226 #define GET_BBLOCK(cfg,bbhash,tblock,ip) do { \
227 (tblock) = g_hash_table_lookup (bbhash, (ip)); \
229 if ((ip) >= end || (ip) < header->code) goto unverified; \
230 (tblock) = NEW_BBLOCK (cfg); \
231 (tblock)->cil_code = (ip); \
232 ADD_BBLOCK (cfg, (bbhash), (tblock)); \
236 #define CHECK_BBLOCK(target,ip,tblock) do { \
237 if ((target) < (ip) && !(tblock)->code) { \
238 bb_recheck = g_list_prepend (bb_recheck, (tblock)); \
239 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); \
243 #define NEW_ICONST(cfg,dest,val) do { \
244 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
245 (dest)->opcode = OP_ICONST; \
246 (dest)->inst_c0 = (val); \
247 (dest)->type = STACK_I4; \
250 /* FIXME: have a different definition of NEW_PCONST for 64 bit systems */
251 #define NEW_PCONST(cfg,dest,val) do { \
252 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
253 (dest)->opcode = OP_ICONST; \
254 (dest)->inst_p0 = (val); \
255 (dest)->type = STACK_PTR; \
258 #define NEW_CLASSCONST(cfg,dest,val) do { \
259 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
260 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
261 (dest)->inst_p0 = (val); \
262 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_CLASS; \
263 (dest)->type = STACK_PTR; \
266 #define NEW_IMAGECONST(cfg,dest,val) do { \
267 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
268 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
269 (dest)->inst_p0 = (val); \
270 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_IMAGE; \
271 (dest)->type = STACK_PTR; \
274 #define NEW_FIELDCONST(cfg,dest,field) do { \
275 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
276 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
277 (dest)->inst_p0 = (field); \
278 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_FIELD; \
279 (dest)->type = STACK_PTR; \
282 #define NEW_METHODCONST(cfg,dest,val) do { \
283 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
284 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
285 (dest)->inst_p0 = (val); \
286 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_METHODCONST; \
287 (dest)->type = STACK_PTR; \
290 #define NEW_VTABLECONST(cfg,dest,vtable) do { \
291 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
292 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
293 (dest)->inst_p0 = mono_compile_aot ? (gpointer)((vtable)->klass) : (vtable); \
294 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_VTABLE; \
295 (dest)->type = STACK_PTR; \
298 #define NEW_SFLDACONST(cfg,dest,field) do { \
299 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
300 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
301 (dest)->inst_p0 = (field); \
302 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_SFLDA; \
303 (dest)->type = STACK_PTR; \
306 #define NEW_LDSTRCONST(cfg,dest,image,token) do { \
307 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
308 (dest)->opcode = OP_AOTCONST; \
309 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
310 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_LDSTR; \
311 (dest)->type = STACK_OBJ; \
314 #define NEW_TYPE_FROM_HANDLE_CONST(cfg,dest,image,token) do { \
315 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
316 (dest)->opcode = OP_AOTCONST; \
317 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
318 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_TYPE_FROM_HANDLE; \
319 (dest)->type = STACK_OBJ; \
322 #define NEW_LDTOKENCONST(cfg,dest,image,token) do { \
323 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
324 (dest)->opcode = OP_AOTCONST; \
325 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
326 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_LDTOKEN; \
327 (dest)->type = STACK_PTR; \
330 #define NEW_DOMAINCONST(cfg,dest) do { \
331 if (cfg->opt & MONO_OPT_SHARED) { \
332 NEW_TEMPLOAD (cfg, dest, mono_get_domainvar (cfg)->inst_c0); \
334 NEW_PCONST (cfg, dest, (cfg)->domain); \
338 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
340 #define NEW_ARGLOAD(cfg,dest,num) do { \
341 if (arg_array [(num)]->opcode == OP_ICONST) (dest) = arg_array [(num)]; else { \
342 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
343 (dest)->ssa_op = MONO_SSA_LOAD; \
344 (dest)->inst_i0 = arg_array [(num)]; \
345 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
346 type_to_eval_stack_type (param_types [(num)], (dest)); \
347 (dest)->klass = (dest)->inst_i0->klass; \
350 #define NEW_LOCLOAD(cfg,dest,num) do { \
351 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
352 (dest)->ssa_op = MONO_SSA_LOAD; \
353 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
354 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
355 type_to_eval_stack_type (header->locals [(num)], (dest)); \
356 (dest)->klass = (dest)->inst_i0->klass; \
359 #define NEW_LOCLOADA(cfg,dest,num) do { \
360 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
361 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
362 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
363 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
364 (dest)->opcode = OP_LDADDR; \
365 (dest)->type = STACK_MP; \
366 (dest)->klass = (dest)->inst_i0->klass; \
367 if (!MONO_TYPE_ISSTRUCT (header->locals [(num)])) \
368 (cfg)->disable_ssa = TRUE; \
371 #define NEW_RETLOADA(cfg,dest) do { \
372 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
373 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
374 (dest)->inst_i0 = (cfg)->ret; \
375 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
376 (dest)->opcode = CEE_LDIND_I; \
377 (dest)->type = STACK_MP; \
378 (dest)->klass = (dest)->inst_i0->klass; \
379 (cfg)->disable_ssa = TRUE; \
382 #define NEW_ARGLOADA(cfg,dest,num) do { \
383 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
384 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
385 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
386 (dest)->inst_i0 = arg_array [(num)]; \
387 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
388 (dest)->opcode = OP_LDADDR; \
389 (dest)->type = STACK_MP; \
390 (dest)->klass = (dest)->inst_i0->klass; \
391 (cfg)->disable_ssa = TRUE; \
394 #define NEW_TEMPLOAD(cfg,dest,num) do { \
395 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
396 (dest)->ssa_op = MONO_SSA_LOAD; \
397 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
398 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
399 type_to_eval_stack_type ((dest)->inst_i0->inst_vtype, (dest)); \
400 (dest)->klass = (dest)->inst_i0->klass; \
403 #define NEW_TEMPLOADA(cfg,dest,num) do { \
404 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
405 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
406 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
407 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
408 (dest)->opcode = OP_LDADDR; \
409 (dest)->type = STACK_MP; \
410 (dest)->klass = (dest)->inst_i0->klass; \
411 if (!MONO_TYPE_ISSTRUCT (cfg->varinfo [(num)]->inst_vtype)) \
412 (cfg)->disable_ssa = TRUE; \
416 #define NEW_INDLOAD(cfg,dest,addr,vtype) do { \
417 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
418 (dest)->inst_left = addr; \
419 (dest)->opcode = mono_type_to_ldind (vtype); \
420 type_to_eval_stack_type (vtype, (dest)); \
421 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
424 #define NEW_INDSTORE(cfg,dest,addr,value,vtype) do { \
425 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
426 (dest)->inst_i0 = addr; \
427 (dest)->opcode = mono_type_to_stind (vtype); \
428 (dest)->inst_i1 = (value); \
429 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
432 #define NEW_TEMPSTORE(cfg,dest,num,inst) do { \
433 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
434 (dest)->ssa_op = MONO_SSA_STORE; \
435 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
436 (dest)->opcode = mono_type_to_stind ((dest)->inst_i0->inst_vtype); \
437 (dest)->inst_i1 = (inst); \
438 (dest)->klass = (dest)->inst_i0->klass; \
441 #define NEW_LOCSTORE(cfg,dest,num,inst) do { \
442 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
443 (dest)->opcode = mono_type_to_stind (header->locals [(num)]); \
444 (dest)->ssa_op = MONO_SSA_STORE; \
445 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
446 (dest)->inst_i1 = (inst); \
447 (dest)->klass = (dest)->inst_i0->klass; \
450 #define NEW_ARGSTORE(cfg,dest,num,inst) do { \
451 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
452 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
453 (dest)->opcode = mono_type_to_stind (param_types [(num)]); \
454 (dest)->ssa_op = MONO_SSA_STORE; \
455 (dest)->inst_i0 = arg_array [(num)]; \
456 (dest)->inst_i1 = (inst); \
457 (dest)->klass = (dest)->inst_i0->klass; \
460 #define ADD_BINOP(op) do { \
461 MONO_INST_NEW (cfg, ins, (op)); \
462 ins->cil_code = ip; \
464 ins->inst_i0 = sp [0]; \
465 ins->inst_i1 = sp [1]; \
467 type_from_op (ins); \
471 #define ADD_UNOP(op) do { \
472 MONO_INST_NEW (cfg, ins, (op)); \
473 ins->cil_code = ip; \
475 ins->inst_i0 = sp [0]; \
477 type_from_op (ins); \
481 #define ADD_BINCOND(next_block) do { \
483 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
485 cmp->inst_i0 = sp [0]; \
486 cmp->inst_i1 = sp [1]; \
487 cmp->cil_code = ins->cil_code; \
488 type_from_op (cmp); \
490 ins->inst_i0 = cmp; \
491 MONO_ADD_INS (bblock, ins); \
492 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
493 GET_BBLOCK (cfg, bbhash, tblock, target); \
494 link_bblock (cfg, bblock, tblock); \
495 ins->inst_true_bb = tblock; \
496 CHECK_BBLOCK (target, ip, tblock); \
497 if ((next_block)) { \
498 link_bblock (cfg, bblock, (next_block)); \
499 ins->inst_false_bb = (next_block); \
500 start_new_bblock = 1; \
502 GET_BBLOCK (cfg, bbhash, tblock, ip); \
503 link_bblock (cfg, bblock, tblock); \
504 ins->inst_false_bb = tblock; \
505 start_new_bblock = 2; \
509 /* FIXME: handle float, long ... */
510 #define ADD_UNCOND(istrue) do { \
512 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
514 cmp->inst_i0 = sp [0]; \
515 switch (cmp->inst_i0->type) { \
517 cmp->inst_i1 = zero_int64; break; \
519 cmp->inst_i1 = zero_r8; break; \
522 cmp->inst_i1 = zero_ptr; break; \
524 cmp->inst_i1 = zero_obj; break; \
526 cmp->inst_i1 = zero_int32; \
528 cmp->cil_code = ins->cil_code; \
529 type_from_op (cmp); \
531 ins->inst_i0 = cmp; \
532 ins->opcode = (istrue)? CEE_BNE_UN: CEE_BEQ; \
533 MONO_ADD_INS (bblock, ins); \
534 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
535 GET_BBLOCK (cfg, bbhash, tblock, target); \
536 link_bblock (cfg, bblock, tblock); \
537 ins->inst_true_bb = tblock; \
538 CHECK_BBLOCK (target, ip, tblock); \
539 GET_BBLOCK (cfg, bbhash, tblock, ip); \
540 link_bblock (cfg, bblock, tblock); \
541 ins->inst_false_bb = tblock; \
542 start_new_bblock = 2; \
545 #define NEW_LDELEMA(cfg,dest,sp,k) do { \
546 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
547 (dest)->opcode = CEE_LDELEMA; \
548 (dest)->inst_left = (sp) [0]; \
549 (dest)->inst_right = (sp) [1]; \
550 (dest)->type = STACK_MP; \
551 (dest)->klass = (k); \
552 (cfg)->flags |= MONO_CFG_HAS_LDELEMA; \
555 #define NEW_GROUP(cfg,dest,el1,el2) do { \
556 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
557 (dest)->opcode = OP_GROUP; \
558 (dest)->inst_left = (el1); \
559 (dest)->inst_right = (el2); \
564 compare_bblock (gconstpointer a, gconstpointer b)
566 const MonoBasicBlock *b1 = a;
567 const MonoBasicBlock *b2 = b;
569 return b2->cil_code - b1->cil_code;
574 * link_bblock: Links two basic blocks
576 * links two basic blocks in the control flow graph, the 'from'
577 * argument is the starting block and the 'to' argument is the block
578 * the control flow ends to after 'from'.
581 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
583 MonoBasicBlock **newa;
587 if (from->cil_code) {
589 g_print ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
591 g_print ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
594 g_print ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
596 g_print ("edge from entry to exit\n");
600 for (i = 0; i < from->out_count; ++i) {
601 if (to == from->out_bb [i]) {
607 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
608 for (i = 0; i < from->out_count; ++i) {
609 newa [i] = from->out_bb [i];
617 for (i = 0; i < to->in_count; ++i) {
618 if (from == to->in_bb [i]) {
624 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
625 for (i = 0; i < to->in_count; ++i) {
626 newa [i] = to->in_bb [i];
635 * mono_find_block_region:
637 * We mark each basic block with a region ID. We use that to avoid BB
638 * optimizations when blocks are in different regions.
641 * A region token that encodes where this region is, and information
642 * about the clause owner for this block.
644 * The region encodes the try/catch/filter clause that owns this block
645 * as well as the type. -1 is a special value that represents a block
646 * that is in none of try/catch/filter.
649 mono_find_block_region (MonoCompile *cfg, int offset, int *filter_lengths)
651 MonoMethod *method = cfg->method;
652 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
653 MonoExceptionClause *clause;
656 /* first search for handlers and filters */
657 for (i = 0; i < header->num_clauses; ++i) {
658 clause = &header->clauses [i];
659 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->token_or_filter) &&
660 (offset < (clause->token_or_filter + filter_lengths [i])))
661 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
663 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
664 if (clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)
665 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
667 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
671 /* search the try blocks */
672 for (i = 0; i < header->num_clauses; ++i) {
673 clause = &header->clauses [i];
674 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
675 return ((i + 1) << 8) | clause->flags;
682 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
684 MonoMethod *method = cfg->method;
685 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
686 MonoExceptionClause *clause;
687 MonoBasicBlock *handler;
691 for (i = 0; i < header->num_clauses; ++i) {
692 clause = &header->clauses [i];
693 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
694 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
695 if (clause->flags == type) {
696 handler = g_hash_table_lookup (cfg->bb_hash, header->code + clause->handler_offset);
698 res = g_list_append (res, handler);
706 mono_find_spvar_for_region (MonoCompile *cfg, int region)
708 return g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
712 mono_create_spvar_for_region (MonoCompile *cfg, int region)
716 var = g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
720 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
721 /* prevent it from being register allocated */
722 var->flags |= MONO_INST_INDIRECT;
724 g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
728 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
732 array [*dfn] = start;
733 /*g_print ("visit %d at %p\n", *dfn, start->cil_code);*/
734 for (i = 0; i < start->out_count; ++i) {
735 if (start->out_bb [i]->dfn)
738 start->out_bb [i]->dfn = *dfn;
739 start->out_bb [i]->df_parent = start;
740 array [*dfn] = start->out_bb [i];
741 df_visit (start->out_bb [i], dfn, array);
747 MonoBasicBlock *best;
751 previous_foreach (gconstpointer key, gpointer val, gpointer data)
753 PrevStruct *p = data;
754 MonoBasicBlock *bb = val;
755 //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,
756 //bb->method == p->best->method, bb->cil_code < p->code, bb->cil_code > p->best->cil_code);
758 if (bb->cil_code && bb->cil_code < p->code && bb->cil_code > p->best->cil_code)
762 static MonoBasicBlock*
763 find_previous (GHashTable *bb_hash, MonoBasicBlock *start, const guchar *code) {
769 g_hash_table_foreach (bb_hash, (GHFunc)previous_foreach, &p);
774 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
783 * FIXME: take into account all the details:
784 * second may have been the target of more than one bblock
786 second->out_count = first->out_count;
787 second->out_bb = first->out_bb;
789 for (i = 0; i < first->out_count; ++i) {
790 bb = first->out_bb [i];
791 for (j = 0; j < bb->in_count; ++j) {
792 if (bb->in_bb [j] == first)
793 bb->in_bb [j] = second;
797 first->out_count = 0;
798 first->out_bb = NULL;
799 link_bblock (cfg, first, second);
801 second->last_ins = first->last_ins;
803 /*g_print ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
804 for (inst = first->code; inst && inst->next; inst = inst->next) {
805 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
806 g_print ("found %p: %s", inst->next->cil_code, code);
808 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
809 second->code = inst->next;
811 first->last_ins = inst;
812 second->next_bb = first->next_bb;
813 first->next_bb = second;
818 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
824 mono_type_to_ldind (MonoType *type)
836 case MONO_TYPE_BOOLEAN:
850 case MONO_TYPE_FNPTR:
852 case MONO_TYPE_CLASS:
853 case MONO_TYPE_STRING:
854 case MONO_TYPE_OBJECT:
855 case MONO_TYPE_SZARRAY:
856 case MONO_TYPE_ARRAY:
857 return CEE_LDIND_REF;
865 case MONO_TYPE_VALUETYPE:
866 if (type->data.klass->enumtype) {
867 t = type->data.klass->enum_basetype->type;
871 case MONO_TYPE_TYPEDBYREF:
873 case MONO_TYPE_GENERICINST:
874 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE)
876 return CEE_LDIND_REF;
878 g_error ("unknown type 0x%02x in type_to_ldind", type->type);
884 mono_type_to_stind (MonoType *type)
895 case MONO_TYPE_BOOLEAN:
907 case MONO_TYPE_FNPTR:
909 case MONO_TYPE_CLASS:
910 case MONO_TYPE_STRING:
911 case MONO_TYPE_OBJECT:
912 case MONO_TYPE_SZARRAY:
913 case MONO_TYPE_ARRAY:
914 return CEE_STIND_REF;
922 case MONO_TYPE_VALUETYPE:
923 if (type->data.klass->enumtype) {
924 t = type->data.klass->enum_basetype->type;
928 case MONO_TYPE_TYPEDBYREF:
930 case MONO_TYPE_GENERICINST:
931 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE)
933 return CEE_STIND_REF;
935 g_error ("unknown type 0x%02x in type_to_stind", type->type);
941 * Returns the type used in the eval stack when @type is loaded.
942 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
945 type_to_eval_stack_type (MonoType *type, MonoInst *inst) {
949 inst->type = STACK_MP;
957 case MONO_TYPE_BOOLEAN:
963 inst->type = STACK_I4;
968 case MONO_TYPE_FNPTR:
969 inst->type = STACK_PTR;
971 case MONO_TYPE_CLASS:
972 case MONO_TYPE_STRING:
973 case MONO_TYPE_OBJECT:
974 case MONO_TYPE_SZARRAY:
975 case MONO_TYPE_ARRAY:
976 inst->type = STACK_OBJ;
980 inst->type = STACK_I8;
984 inst->type = STACK_R8;
986 case MONO_TYPE_VALUETYPE:
987 if (type->data.klass->enumtype) {
988 t = type->data.klass->enum_basetype->type;
991 inst->klass = type->data.klass;
992 inst->type = STACK_VTYPE;
995 case MONO_TYPE_TYPEDBYREF:
996 inst->klass = mono_defaults.typed_reference_class;
997 inst->type = STACK_VTYPE;
999 case MONO_TYPE_GENERICINST:
1000 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE) {
1001 inst->klass = mono_class_from_mono_type (type);
1002 inst->type = STACK_VTYPE;
1004 inst->type = STACK_OBJ;
1008 g_error ("unknown type 0x%02x in eval stack type", type->type);
1013 * The following tables are used to quickly validate the IL code in type_from_op ().
1016 bin_num_table [STACK_MAX] [STACK_MAX] = {
1017 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1018 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1019 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1020 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1021 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
1022 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
1023 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1024 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1029 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
1032 /* reduce the size of this table */
1034 bin_int_table [STACK_MAX] [STACK_MAX] = {
1035 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1036 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1037 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1038 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1039 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1040 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1041 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1042 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1046 bin_comp_table [STACK_MAX] [STACK_MAX] = {
1048 {0, 1, 0, 1, 0, 0, 4, 0},
1049 {0, 0, 1, 0, 0, 0, 0, 0},
1050 {0, 1, 0, 1, 0, 2, 4, 0},
1051 {0, 0, 0, 0, 1, 0, 0, 0},
1052 {0, 0, 0, 2, 0, 1, 0, 0},
1053 {0, 4, 0, 4, 0, 0, 3, 0},
1054 {0, 0, 0, 0, 0, 0, 0, 0},
1057 /* reduce the size of this table */
1059 shift_table [STACK_MAX] [STACK_MAX] = {
1060 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1061 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1062 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1063 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1064 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1065 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1066 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1067 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1071 * Tables to map from the non-specific opcode to the matching
1072 * type-specific opcode.
1074 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
1075 static const guint16
1076 binops_op_map [STACK_MAX] = {
1077 0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, 0
1080 /* handles from CEE_NEG to CEE_CONV_U8 */
1081 static const guint16
1082 unops_op_map [STACK_MAX] = {
1083 0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, 0
1086 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
1087 static const guint16
1088 ovfops_op_map [STACK_MAX] = {
1089 0, 0, OP_LCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_FCONV_TO_U2-CEE_CONV_U2, 0
1092 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
1093 static const guint16
1094 ovf2ops_op_map [STACK_MAX] = {
1095 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
1098 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
1099 static const guint16
1100 ovf3ops_op_map [STACK_MAX] = {
1101 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
1104 /* handles from CEE_CEQ to CEE_CLT_UN */
1105 static const guint16
1106 ceqops_op_map [STACK_MAX] = {
1107 0, 0, OP_LCEQ-CEE_CEQ, OP_PCEQ-CEE_CEQ, OP_FCEQ-CEE_CEQ, 0
1111 * Sets ins->type (the type on the eval stack) according to the
1112 * type of the opcode and the arguments to it.
1113 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1115 * FIXME: this function sets ins->type unconditionally in some cases, but
1116 * it should set it to invalid for some types (a conv.x on an object)
1119 type_from_op (MonoInst *ins) {
1120 switch (ins->opcode) {
1127 /* FIXME: check unverifiable args for STACK_MP */
1128 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1129 ins->opcode += binops_op_map [ins->type];
1136 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1137 ins->opcode += binops_op_map [ins->type];
1142 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1143 ins->opcode += binops_op_map [ins->type];
1146 /* FIXME: handle some specifics with ins->next->type */
1147 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1154 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1155 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1159 ins->type = neg_table [ins->inst_i0->type];
1160 ins->opcode += unops_op_map [ins->type];
1163 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1164 ins->type = ins->inst_i0->type;
1166 ins->type = STACK_INV;
1167 ins->opcode += unops_op_map [ins->type];
1173 ins->type = STACK_I4;
1174 ins->opcode += unops_op_map [ins->inst_i0->type];
1177 ins->type = STACK_R8;
1178 switch (ins->inst_i0->type) {
1183 ins->opcode = OP_LCONV_TO_R_UN;
1187 case CEE_CONV_OVF_I1:
1188 case CEE_CONV_OVF_U1:
1189 case CEE_CONV_OVF_I2:
1190 case CEE_CONV_OVF_U2:
1191 case CEE_CONV_OVF_I4:
1192 case CEE_CONV_OVF_U4:
1193 ins->type = STACK_I4;
1194 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1196 case CEE_CONV_OVF_I_UN:
1197 case CEE_CONV_OVF_U_UN:
1198 ins->type = STACK_PTR;
1199 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1201 case CEE_CONV_OVF_I1_UN:
1202 case CEE_CONV_OVF_I2_UN:
1203 case CEE_CONV_OVF_I4_UN:
1204 case CEE_CONV_OVF_U1_UN:
1205 case CEE_CONV_OVF_U2_UN:
1206 case CEE_CONV_OVF_U4_UN:
1207 ins->type = STACK_I4;
1208 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1211 ins->type = STACK_PTR;
1212 switch (ins->inst_i0->type) {
1218 ins->opcode = OP_LCONV_TO_U;
1221 ins->opcode = OP_FCONV_TO_U;
1227 ins->type = STACK_I8;
1228 ins->opcode += unops_op_map [ins->inst_i0->type];
1230 case CEE_CONV_OVF_I8:
1231 case CEE_CONV_OVF_U8:
1232 ins->type = STACK_I8;
1233 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1235 case CEE_CONV_OVF_U8_UN:
1236 case CEE_CONV_OVF_I8_UN:
1237 ins->type = STACK_I8;
1238 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1242 ins->type = STACK_R8;
1243 ins->opcode += unops_op_map [ins->inst_i0->type];
1246 ins->type = STACK_R8;
1250 ins->type = STACK_I4;
1251 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1254 case CEE_CONV_OVF_I:
1255 case CEE_CONV_OVF_U:
1256 ins->type = STACK_PTR;
1257 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1260 case CEE_ADD_OVF_UN:
1262 case CEE_MUL_OVF_UN:
1264 case CEE_SUB_OVF_UN:
1265 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1266 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1269 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1276 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_MP, STACK_R8, STACK_R8, STACK_OBJ
1279 /* map ldelem.x to the matching ldind.x opcode */
1281 ldelem_to_ldind [] = {
1295 /* map stelem.x to the matching stind.x opcode */
1297 stelem_to_stind [] = {
1311 param_table [STACK_MAX] [STACK_MAX] = {
1316 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1320 switch (args->type) {
1330 for (i = 0; i < sig->param_count; ++i) {
1331 switch (args [i].type) {
1335 if (!sig->params [i]->byref)
1339 if (sig->params [i]->byref)
1341 switch (sig->params [i]->type) {
1342 case MONO_TYPE_CLASS:
1343 case MONO_TYPE_STRING:
1344 case MONO_TYPE_OBJECT:
1345 case MONO_TYPE_SZARRAY:
1346 case MONO_TYPE_ARRAY:
1353 if (sig->params [i]->byref)
1355 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1364 /*if (!param_table [args [i].type] [sig->params [i]->type])
1372 * When we need a pointer to the current domain many times in a method, we
1373 * call mono_domain_get() once and we store the result in a local variable.
1374 * This function returns the variable that represents the MonoDomain*.
1376 inline static MonoInst *
1377 mono_get_domainvar (MonoCompile *cfg)
1379 if (!cfg->domainvar)
1380 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1381 return cfg->domainvar;
1385 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
1388 int num = cfg->num_varinfo;
1390 if ((num + 1) >= cfg->varinfo_count) {
1391 cfg->varinfo_count = (cfg->varinfo_count + 2) * 2;
1392 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
1393 cfg->vars = (MonoMethodVar **)g_realloc (cfg->vars, sizeof (MonoMethodVar*) * cfg->varinfo_count);
1396 /*g_print ("created temp %d of type 0x%x\n", num, type->type);*/
1397 mono_jit_stats.allocate_var++;
1399 MONO_INST_NEW (cfg, inst, opcode);
1400 inst->inst_c0 = num;
1401 inst->inst_vtype = type;
1402 inst->klass = mono_class_from_mono_type (type);
1403 /* if set to 1 the variable is native */
1406 cfg->varinfo [num] = inst;
1408 cfg->vars [num] = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMethodVar));
1409 MONO_INIT_VARINFO (cfg->vars [num], num);
1412 //g_print ("created temp %d of type %s\n", num, mono_type_get_name (type));
1417 type_from_stack_type (MonoInst *ins) {
1418 switch (ins->type) {
1419 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1420 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1421 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1422 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1423 case STACK_MP: return &mono_defaults.int_class->byval_arg;
1424 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1425 case STACK_VTYPE: return &ins->klass->byval_arg;
1427 g_error ("stack type %d to montype not handled\n", ins->type);
1433 array_access_to_klass (int opcode)
1437 return mono_defaults.byte_class;
1439 return mono_defaults.uint16_class;
1442 return mono_defaults.int_class;
1445 return mono_defaults.sbyte_class;
1448 return mono_defaults.int16_class;
1451 return mono_defaults.int32_class;
1453 return mono_defaults.uint32_class;
1456 return mono_defaults.int64_class;
1459 return mono_defaults.single_class;
1462 return mono_defaults.double_class;
1463 case CEE_LDELEM_REF:
1464 case CEE_STELEM_REF:
1465 return mono_defaults.object_class;
1467 g_assert_not_reached ();
1473 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
1477 MONO_ADD_INS (bb, inst);
1480 switch (bb->last_ins->opcode) {
1494 while (prev->next && prev->next != bb->last_ins)
1496 if (prev == bb->code) {
1497 if (bb->last_ins == bb->code) {
1498 inst->next = bb->code;
1501 inst->next = prev->next;
1505 inst->next = bb->last_ins;
1509 // g_warning ("handle conditional jump in add_ins_to_end ()\n");
1511 MONO_ADD_INS (bb, inst);
1517 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
1519 MonoInst *inst, *load;
1521 NEW_TEMPLOAD (cfg, load, src);
1523 NEW_TEMPSTORE (cfg, inst, dest, load);
1524 if (inst->opcode == CEE_STOBJ) {
1525 NEW_TEMPLOADA (cfg, inst, dest);
1526 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE);
1528 inst->cil_code = NULL;
1529 mono_add_ins_to_end (bb, inst);
1534 * We try to share variables when possible
1537 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1542 /* inlining can result in deeper stacks */
1543 if (slot >= ((MonoMethodNormal *)cfg->method)->header->max_stack)
1544 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1546 pos = ins->type - 1 + slot * STACK_MAX;
1548 switch (ins->type) {
1555 if ((vnum = cfg->intvars [pos]))
1556 return cfg->varinfo [vnum];
1557 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1558 cfg->intvars [pos] = res->inst_c0;
1561 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1567 * This function is called to handle items that are left on the evaluation stack
1568 * at basic block boundaries. What happens is that we save the values to local variables
1569 * and we reload them later when first entering the target basic block (with the
1570 * handle_loaded_temps () function).
1571 * It is also used to handle items on the stack in store opcodes, since it is
1572 * possible that the variable to be stored into is already on the stack, in
1573 * which case its old value should be used.
1574 * A single joint point will use the same variables (stored in the array bb->out_stack or
1575 * bb->in_stack, if the basic block is before or after the joint point).
1578 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count) {
1580 MonoBasicBlock *outb;
1581 MonoInst *inst, **locals;
1585 if (cfg->verbose_level > 3)
1586 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
1587 if (!bb->out_scount) {
1589 bb->out_scount = count;
1590 //g_print ("bblock %d has out:", bb->block_num);
1591 for (i = 0; i < bb->out_count; ++i) {
1592 outb = bb->out_bb [i];
1593 //g_print (" %d", outb->block_num);
1594 if (outb->in_stack) {
1596 bb->out_stack = outb->in_stack;
1602 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1603 for (i = 0; i < count; ++i) {
1604 /* see bug#58863, but removing this code causes regressions in gtk-sharp build
1605 * (SEGV running Method::Initialize() in gapi_codegen.exe)
1607 #if defined(__ppc__) || defined(__powerpc__)
1608 /* try to reuse temps already allocated for this purpouse, if they occupy the same
1609 * stack slot and if they are of the same type. */
1610 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1612 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1617 locals = bb->out_stack;
1618 for (i = 0; i < count; ++i) {
1619 /* add store ops at the end of the bb, before the branch */
1620 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1621 if (inst->opcode == CEE_STOBJ) {
1622 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
1623 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE);
1625 inst->cil_code = sp [i]->cil_code;
1626 mono_add_ins_to_end (bb, inst);
1628 if (cfg->verbose_level > 3)
1629 g_print ("storing %d to temp %d\n", i, locals [i]->inst_c0);
1632 for (i = 0; i < bb->out_count; ++i) {
1633 outb = bb->out_bb [i];
1634 if (outb->in_scount)
1635 continue; /* check they are the same locals */
1636 outb->in_scount = count;
1637 outb->in_stack = locals;
1643 ret_type_to_call_opcode (MonoType *type, int calli, int virt)
1648 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1652 case MONO_TYPE_VOID:
1653 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
1656 case MONO_TYPE_BOOLEAN:
1659 case MONO_TYPE_CHAR:
1662 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1666 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1667 case MONO_TYPE_CLASS:
1668 case MONO_TYPE_STRING:
1669 case MONO_TYPE_OBJECT:
1670 case MONO_TYPE_SZARRAY:
1671 case MONO_TYPE_ARRAY:
1672 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1675 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
1678 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
1679 case MONO_TYPE_VALUETYPE:
1680 if (type->data.klass->enumtype) {
1681 t = type->data.klass->enum_basetype->type;
1684 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1685 case MONO_TYPE_TYPEDBYREF:
1686 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1687 case MONO_TYPE_GENERICINST:
1688 t = type->data.generic_inst->generic_type->type;
1691 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
1697 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
1699 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
1701 ji->ip.label = label;
1702 ji->type = MONO_PATCH_INFO_SWITCH;
1703 ji->data.table = bbs;
1704 ji->next = cfg->patch_info;
1705 ji->table_size = num_blocks;
1706 cfg->patch_info = ji;
1710 * When we add a tree of instructions, we need to ensure the instructions currently
1711 * on the stack are executed before (like, if we load a value from a local).
1712 * We ensure this by saving the currently loaded values to temps and rewriting the
1713 * instructions to load the values.
1714 * This is not done for opcodes that terminate a basic block (because it's handled already
1715 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
1718 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
1720 MonoInst *load, *store, *temp, *ins;
1722 while (stack < sp) {
1724 /* handle also other constants */
1725 if (ins->opcode != OP_ICONST) {
1726 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1727 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1728 store->cil_code = ins->cil_code;
1729 if (store->opcode == CEE_STOBJ) {
1730 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
1731 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE);
1733 MONO_ADD_INS (bblock, store);
1734 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1735 load->cil_code = ins->cil_code;
1743 * Prepare arguments for passing to a function call.
1744 * Return a non-zero value if the arguments can't be passed to the given
1746 * The type checks are not yet complete and some conversions may need
1747 * casts on 32 or 64 bit architectures.
1750 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
1755 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
1759 for (i = 0; i < sig->param_count; ++i) {
1760 if (sig->params [i]->byref) {
1762 * check the result of ldelema is only passed as an argument if the byref
1763 * type matches exactly the array element type.
1764 * FIXME: if the argument as been saved on the stack as part of the
1765 * interface variable code (the value was on the stack at a basic block boundary)
1766 * we need to add the check in that case, too.
1768 if (args [i]->opcode == CEE_LDELEMA) {
1770 MonoClass *exact_class = mono_class_from_mono_type (sig->params [i]);
1771 if (!exact_class->valuetype) {
1772 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
1773 check->cil_code = args [i]->cil_code;
1774 check->klass = exact_class;
1775 check->inst_left = args [i]->inst_left;
1776 check->type = STACK_OBJ;
1777 args [i]->inst_left = check;
1780 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
1784 simple_type = sig->params [i]->type;
1786 switch (simple_type) {
1787 case MONO_TYPE_VOID:
1792 case MONO_TYPE_BOOLEAN:
1795 case MONO_TYPE_CHAR:
1798 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
1804 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
1807 case MONO_TYPE_CLASS:
1808 case MONO_TYPE_STRING:
1809 case MONO_TYPE_OBJECT:
1810 case MONO_TYPE_SZARRAY:
1811 case MONO_TYPE_ARRAY:
1812 if (args [i]->type != STACK_OBJ)
1817 if (args [i]->type != STACK_I8)
1822 if (args [i]->type != STACK_R8)
1825 case MONO_TYPE_VALUETYPE:
1826 if (sig->params [i]->data.klass->enumtype) {
1827 simple_type = sig->params [i]->data.klass->enum_basetype->type;
1830 if (args [i]->type != STACK_VTYPE)
1833 case MONO_TYPE_TYPEDBYREF:
1834 if (args [i]->type != STACK_VTYPE)
1837 case MONO_TYPE_GENERICINST:
1838 simple_type = sig->params [i]->data.generic_inst->generic_type->type;
1842 g_error ("unknown type 0x%02x in check_call_signature", simple_type);
1849 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
1850 const guint8 *ip, gboolean to_end)
1852 MonoInst *temp, *store, *ins = (MonoInst*)call;
1853 MonoType *ret = sig->ret;
1855 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
1857 call->inst.type = STACK_OBJ;
1858 call->inst.opcode = CEE_CALL;
1859 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
1861 type_to_eval_stack_type (ret, ins);
1862 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
1865 if (MONO_TYPE_ISSTRUCT (ret)) {
1868 /* we use this to allocate native sized structs */
1869 temp->unused = sig->pinvoke;
1871 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
1872 if (call->inst.opcode == OP_VCALL)
1873 ins->inst_left = loada;
1875 ins->inst_right = loada; /* a virtual or indirect call */
1878 mono_add_ins_to_end (bblock, ins);
1880 MONO_ADD_INS (bblock, ins);
1882 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1883 store->cil_code = ip;
1885 mono_add_ins_to_end (bblock, store);
1887 MONO_ADD_INS (bblock, store);
1889 return temp->inst_c0;
1892 mono_add_ins_to_end (bblock, ins);
1894 MONO_ADD_INS (bblock, ins);
1899 inline static MonoCallInst *
1900 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1901 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
1906 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual));
1908 call->inst.cil_code = ip;
1910 call->signature = sig;
1911 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
1913 for (arg = call->out_args; arg;) {
1914 MonoInst *narg = arg->next;
1919 mono_add_ins_to_end (bblock, arg);
1921 MONO_ADD_INS (bblock, arg);
1928 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1929 MonoInst **args, MonoInst *addr, const guint8 *ip)
1931 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
1933 call->inst.inst_i0 = addr;
1935 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
1938 static MonoCallInst*
1939 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
1940 MonoInst **args, const guint8 *ip, MonoInst *this)
1942 gboolean virtual = this != NULL;
1945 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, FALSE);
1947 if (this && sig->hasthis &&
1948 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
1949 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
1950 call->method = mono_marshal_get_remoting_invoke_with_check (method);
1952 call->method = method;
1954 call->inst.flags |= MONO_INST_HAS_METHOD;
1955 call->inst.inst_left = this;
1961 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
1962 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
1964 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, signature, args, ip, this);
1966 return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
1970 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
1971 MonoInst **args, const guint8 *ip, gboolean to_end)
1977 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
1979 return mono_spill_call (cfg, bblock, call, sig, func == mono_array_new_va, ip, to_end);
1983 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
1985 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
1988 g_warning ("unregistered JIT ICall");
1989 g_assert_not_reached ();
1992 return mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, args, ip, FALSE);
1996 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
1998 MonoInst *ins, *temp = NULL, *store, *load, *begin;
1999 MonoInst *last_arg = NULL;
2003 //g_print ("emulating: ");
2004 //mono_print_tree_nl (tree);
2005 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE));
2006 ins = (MonoInst*)call;
2008 call->inst.cil_code = tree->cil_code;
2010 call->signature = info->sig;
2012 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
2014 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2015 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
2016 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2017 store->cil_code = tree->cil_code;
2022 nargs = info->sig->param_count + info->sig->hasthis;
2024 for (last_arg = call->out_args; last_arg && last_arg->next; last_arg = last_arg->next) ;
2027 last_arg->next = store;
2030 begin = call->out_args;
2034 if (cfg->prev_ins) {
2036 * This assumes that that in a tree, emulate_opcode is called for a
2037 * node before it is called for its children. dec_foreach needs to
2038 * take this into account.
2040 store->next = cfg->prev_ins->next;
2041 cfg->prev_ins->next = begin;
2043 store->next = cfg->cbb->code;
2044 cfg->cbb->code = begin;
2047 call->fptr = mono_icall_get_wrapper (info);
2049 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2050 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2055 static MonoMethodSignature *
2056 mono_get_element_address_signature (int arity)
2058 static GHashTable *sighash = NULL;
2059 MonoMethodSignature *res;
2062 EnterCriticalSection (&trampoline_hash_mutex);
2064 sighash = g_hash_table_new (NULL, NULL);
2066 else if ((res = g_hash_table_lookup (sighash, (gpointer)arity))) {
2067 LeaveCriticalSection (&trampoline_hash_mutex);
2071 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
2074 res->params [0] = &mono_defaults.array_class->byval_arg;
2076 for (i = 1; i <= arity; i++)
2077 res->params [i] = &mono_defaults.int_class->byval_arg;
2079 res->ret = &mono_defaults.int_class->byval_arg;
2081 g_hash_table_insert (sighash, (gpointer)arity, res);
2082 LeaveCriticalSection (&trampoline_hash_mutex);
2088 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native) {
2089 MonoInst *iargs [3];
2095 * This check breaks with spilled vars... need to handle it during verification anyway.
2096 * g_assert (klass && klass == src->klass && klass == dest->klass);
2100 n = mono_class_native_size (klass, &align);
2102 n = mono_class_value_size (klass, &align);
2104 if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
2106 MONO_INST_NEW (cfg, inst, OP_MEMCPY);
2107 inst->inst_left = dest;
2108 inst->inst_right = src;
2109 inst->cil_code = ip;
2111 MONO_ADD_INS (bblock, inst);
2116 NEW_ICONST (cfg, iargs [2], n);
2118 mono_emit_native_call (cfg, bblock, helper_memcpy, helper_sig_memcpy, iargs, ip, to_end);
2122 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
2124 MonoInst *iargs [2];
2125 MonoInst *ins, *zero_int32;
2128 NEW_ICONST (cfg, zero_int32, 0);
2130 mono_class_init (klass);
2131 n = mono_class_value_size (klass, NULL);
2132 MONO_INST_NEW (cfg, ins, 0);
2134 ins->inst_left = dest;
2135 ins->inst_right = zero_int32;
2138 ins->opcode = CEE_STIND_I1;
2139 MONO_ADD_INS (bblock, ins);
2142 ins->opcode = CEE_STIND_I2;
2143 MONO_ADD_INS (bblock, ins);
2146 ins->opcode = CEE_STIND_I4;
2147 MONO_ADD_INS (bblock, ins);
2150 if (n <= sizeof (gpointer) * 5) {
2151 ins->opcode = OP_MEMSET;
2154 MONO_ADD_INS (bblock, ins);
2157 handle_loaded_temps (cfg, bblock, stack_start, sp);
2158 NEW_ICONST (cfg, ins, n);
2161 mono_emit_jit_icall (cfg, bblock, helper_initobj, iargs, ip);
2167 handle_box (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *val, const guchar *ip, MonoClass *klass)
2169 MonoInst *iargs [2];
2170 MonoInst *dest, *vtoffset, *add, *vstore;
2173 /* much like NEWOBJ */
2174 if (cfg->opt & MONO_OPT_SHARED) {
2175 NEW_DOMAINCONST (cfg, iargs [0]);
2176 NEW_CLASSCONST (cfg, iargs [1], klass);
2178 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
2180 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
2181 NEW_VTABLECONST (cfg, iargs [0], vtable);
2182 if (klass->has_finalize || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
2183 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
2185 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
2187 NEW_TEMPLOAD (cfg, dest, temp);
2188 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
2189 MONO_INST_NEW (cfg, add, CEE_ADD);
2190 add->inst_left = dest;
2191 add->inst_right = vtoffset;
2194 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
2195 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
2196 vstore->cil_code = ip;
2197 vstore->inst_left = add;
2198 vstore->inst_right = val;
2200 if (vstore->opcode == CEE_STOBJ) {
2201 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
2203 MONO_ADD_INS (bblock, vstore);
2205 NEW_TEMPLOAD (cfg, dest, temp);
2209 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
2212 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
2214 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
2215 MonoMethodSignature *signature = method->signature;
2219 #ifdef MONO_ARCH_HAVE_LMF_OPS
2220 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
2221 !MONO_TYPE_ISSTRUCT (signature->ret) && (method->klass->parent != mono_defaults.array_class))
2225 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2226 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2227 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
2228 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
2229 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2230 (method->klass->marshalbyref) ||
2231 !header || header->num_clauses ||
2232 /* fixme: why cant we inline valuetype returns? */
2233 MONO_TYPE_ISSTRUCT (signature->ret))
2236 /* its not worth to inline methods with valuetype arguments?? */
2237 for (i = 0; i < signature->param_count; i++) {
2238 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
2244 * if we can initialize the class of the method right away, we do,
2245 * otherwise we don't allow inlining if the class needs initialization,
2246 * since it would mean inserting a call to mono_runtime_class_init()
2247 * inside the inlined code
2249 if (!(cfg->opt & MONO_OPT_SHARED)) {
2250 vtable = mono_class_vtable (cfg->domain, method->klass);
2251 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
2252 if (cfg->run_cctors)
2253 mono_runtime_class_init (vtable);
2255 else if (!vtable->initialized && mono_class_needs_cctor_run (method->klass, NULL))
2259 * If we're compiling for shared code
2260 * the cctor will need to be run at aot method load time, for example,
2261 * or at the end of the compilation of the inlining method.
2263 if (mono_class_needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
2266 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
2268 /* also consider num_locals? */
2269 if (getenv ("MONO_INLINELIMIT"))
2270 if (header->code_size < atoi (getenv ("MONO_INLINELIMIT"))) {
2274 if (header->code_size < 20)
2281 mini_get_ldelema_ins (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
2285 MonoMethodSignature *esig;
2286 char icall_name [256];
2287 MonoJitICallInfo *info;
2289 rank = cmethod->signature->param_count - (is_set? 1: 0);
2291 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
2293 NEW_GROUP (cfg, indexes, sp [1], sp [2]);
2294 MONO_INST_NEW (cfg, addr, OP_LDELEMA2D);
2295 addr->inst_left = sp [0];
2296 addr->inst_right = indexes;
2297 addr->cil_code = ip;
2298 addr->type = STACK_MP;
2299 addr->klass = cmethod->klass;
2303 /* Need to register the icall so it gets an icall wrapper */
2304 sprintf (icall_name, "ves_array_element_address_%d", rank);
2306 info = mono_find_jit_icall_by_name (icall_name);
2308 esig = mono_get_element_address_signature (rank);
2309 info = mono_register_jit_icall (ves_array_element_address, g_strdup (icall_name), esig, FALSE);
2312 temp = mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, sp, ip, FALSE);
2314 NEW_TEMPLOAD (cfg, addr, temp);
2318 static MonoJitICallInfo **emul_opcode_map = NULL;
2320 static inline MonoJitICallInfo *
2321 mono_find_jit_opcode_emulation (int opcode)
2323 if (emul_opcode_map)
2324 return emul_opcode_map [opcode];
2330 mini_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
2335 if (cmethod->klass == mono_defaults.string_class) {
2336 if (cmethod->name [0] != 'g')
2339 if (strcmp (cmethod->name, "get_Chars") == 0)
2341 else if (strcmp (cmethod->name, "get_Length") == 0)
2344 } else if (cmethod->klass == mono_defaults.object_class) {
2345 if (strcmp (cmethod->name, "GetType") == 0)
2349 } else if (cmethod->klass == mono_defaults.array_class) {
2350 if (strcmp (cmethod->name, "get_Rank") == 0)
2352 else if (strcmp (cmethod->name, "get_Length") == 0)
2357 op = mono_arch_get_opcode_for_method (cfg, cmethod, fsig, args);
2361 pc = fsig->param_count + fsig->hasthis;
2362 MONO_INST_NEW (cfg, ins, op);
2365 ins->inst_i0 = args [0];
2367 ins->inst_i1 = args [1];
2374 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
2376 MonoInst *store, *temp;
2379 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
2381 if (!sig->hasthis && sig->param_count == 0)
2385 if (sp [0]->opcode == OP_ICONST) {
2388 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
2390 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2391 store->cil_code = sp [0]->cil_code;
2392 MONO_ADD_INS (bblock, store);
2397 for (i = 0; i < sig->param_count; ++i) {
2398 if (sp [0]->opcode == OP_ICONST) {
2401 temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
2403 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2404 store->cil_code = sp [0]->cil_code;
2405 if (store->opcode == CEE_STOBJ) {
2406 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2407 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE);
2409 MONO_ADD_INS (bblock, store);
2417 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
2418 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b, gboolean inline_allways)
2420 MonoInst *ins, *rvar = NULL;
2421 MonoMethodHeader *cheader;
2422 MonoBasicBlock *ebblock, *sbblock;
2423 int i, costs, new_locals_offset;
2425 if (cfg->verbose_level > 2)
2426 g_print ("INLINE START %p %s -> %s\n", cmethod, mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2428 if (!cmethod->inline_info) {
2429 mono_jit_stats.inlineable_methods++;
2430 cmethod->inline_info = 1;
2432 /* allocate space to store the return value */
2433 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2434 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2437 /* allocate local variables */
2438 cheader = ((MonoMethodNormal *)cmethod)->header;
2439 new_locals_offset = cfg->num_varinfo;
2440 for (i = 0; i < cheader->num_locals; ++i)
2441 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
2443 /* allocate starte and end blocks */
2444 sbblock = NEW_BBLOCK (cfg);
2445 sbblock->block_num = cfg->num_bblocks++;
2446 sbblock->real_offset = real_offset;
2448 ebblock = NEW_BBLOCK (cfg);
2449 ebblock->block_num = cfg->num_bblocks++;
2450 ebblock->real_offset = real_offset;
2452 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
2454 if ((costs >= 0 && costs < 60) || inline_allways) {
2455 if (cfg->verbose_level > 2)
2456 g_print ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2458 mono_jit_stats.inlined_methods++;
2460 /* always add some code to avoid block split failures */
2461 MONO_INST_NEW (cfg, ins, CEE_NOP);
2462 MONO_ADD_INS (bblock, ins);
2465 bblock->next_bb = sbblock;
2466 link_bblock (cfg, bblock, sbblock);
2469 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
2475 if (cfg->verbose_level > 2)
2476 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
2482 * Some of these comments may well be out-of-date.
2483 * Design decisions: we do a single pass over the IL code (and we do bblock
2484 * splitting/merging in the few cases when it's required: a back jump to an IL
2485 * address that was not already seen as bblock starting point).
2486 * Code is validated as we go (full verification is still better left to metadata/verify.c).
2487 * Complex operations are decomposed in simpler ones right away. We need to let the
2488 * arch-specific code peek and poke inside this process somehow (except when the
2489 * optimizations can take advantage of the full semantic info of coarse opcodes).
2490 * All the opcodes of the form opcode.s are 'normalized' to opcode.
2491 * MonoInst->opcode initially is the IL opcode or some simplification of that
2492 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
2493 * opcode with value bigger than OP_LAST.
2494 * At this point the IR can be handed over to an interpreter, a dumb code generator
2495 * or to the optimizing code generator that will translate it to SSA form.
2497 * Profiling directed optimizations.
2498 * We may compile by default with few or no optimizations and instrument the code
2499 * or the user may indicate what methods to optimize the most either in a config file
2500 * or through repeated runs where the compiler applies offline the optimizations to
2501 * each method and then decides if it was worth it.
2504 * * consider using an array instead of an hash table (bb_hash)
2507 #define CHECK_TYPE(ins) if (!(ins)->type) goto unverified
2508 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) goto unverified
2509 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) goto unverified
2510 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) goto unverified
2511 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) goto unverified
2512 #define CHECK_OPSIZE(size) if (ip + size > end) goto unverified
2515 /* offset from br.s -> br like opcodes */
2516 #define BIG_BRANCH_OFFSET 13
2519 get_basic_blocks (MonoCompile *cfg, GHashTable *bbhash, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
2521 unsigned char *ip = start;
2522 unsigned char *target;
2525 MonoBasicBlock *bblock;
2526 const MonoOpcode *opcode;
2529 cli_addr = ip - start;
2530 i = mono_opcode_value ((const guint8 **)&ip);
2531 opcode = &mono_opcodes [i];
2532 switch (opcode->argument) {
2533 case MonoInlineNone:
2536 case MonoInlineString:
2537 case MonoInlineType:
2538 case MonoInlineField:
2539 case MonoInlineMethod:
2542 case MonoShortInlineR:
2549 case MonoShortInlineVar:
2550 case MonoShortInlineI:
2553 case MonoShortInlineBrTarget:
2554 target = start + cli_addr + 2 + (signed char)ip [1];
2555 GET_BBLOCK (cfg, bbhash, bblock, target);
2558 case MonoInlineBrTarget:
2559 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
2560 GET_BBLOCK (cfg, bbhash, bblock, target);
2563 case MonoInlineSwitch: {
2564 guint32 n = read32 (ip + 1);
2567 cli_addr += 5 + 4 * n;
2568 target = start + cli_addr;
2569 GET_BBLOCK (cfg, bbhash, bblock, target);
2571 for (j = 0; j < n; ++j) {
2572 target = start + cli_addr + (gint32)read32 (ip);
2573 GET_BBLOCK (cfg, bbhash, bblock, target);
2583 g_assert_not_reached ();
2593 emit_tree (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *ins)
2595 MonoInst *store, *temp, *load;
2596 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
2597 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2598 store->cil_code = ins->cil_code;
2599 MONO_ADD_INS (bblock, store);
2600 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2601 load->cil_code = ins->cil_code;
2606 * mono_method_to_ir: translates IL into basic blocks containing trees
2609 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
2610 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
2611 guint inline_offset, gboolean is_virtual_call)
2613 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
2614 MonoInst *ins, **sp, **stack_start;
2615 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
2617 MonoMethod *cmethod;
2618 MonoInst **arg_array;
2619 MonoMethodHeader *header;
2621 guint32 token, ins_flag;
2623 MonoClass *constrained_call = NULL;
2624 unsigned char *ip, *end, *target, *err_pos;
2625 static double r8_0 = 0.0;
2626 MonoMethodSignature *sig;
2627 MonoGenericContext *generic_context = NULL;
2628 MonoType **param_types;
2629 GList *bb_recheck = NULL, *tmp;
2630 int i, n, start_new_bblock, align;
2631 int num_calls = 0, inline_costs = 0;
2632 int *filter_lengths = NULL;
2633 int breakpoint_id = 0;
2634 guint real_offset, num_args;
2636 image = method->klass->image;
2637 header = ((MonoMethodNormal *)method)->header;
2638 sig = method->signature;
2639 num_args = sig->hasthis + sig->param_count;
2640 ip = (unsigned char*)header->code;
2641 end = ip + header->code_size;
2642 mono_jit_stats.cil_code_size += header->code_size;
2644 if (method->signature->is_inflated)
2645 generic_context = ((MonoMethodInflated *) method)->context;
2647 if (cfg->method == method) {
2649 bbhash = cfg->bb_hash;
2651 real_offset = inline_offset;
2652 bbhash = g_hash_table_new (g_direct_hash, NULL);
2655 if (cfg->verbose_level > 2)
2656 g_print ("method to IR %s\n", mono_method_full_name (method, TRUE));
2658 dont_inline = g_list_prepend (dont_inline, method);
2659 if (cfg->method == method) {
2661 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
2662 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
2665 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
2666 start_bblock->cil_code = NULL;
2667 start_bblock->cil_length = 0;
2668 start_bblock->block_num = cfg->num_bblocks++;
2671 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
2672 end_bblock->cil_code = NULL;
2673 end_bblock->cil_length = 0;
2674 end_bblock->block_num = cfg->num_bblocks++;
2675 g_assert (cfg->num_bblocks == 2);
2677 arg_array = alloca (sizeof (MonoInst *) * num_args);
2678 for (i = num_args - 1; i >= 0; i--)
2679 arg_array [i] = cfg->varinfo [i];
2681 if (header->num_clauses) {
2682 int size = sizeof (int) * header->num_clauses;
2683 filter_lengths = alloca (size);
2684 memset (filter_lengths, 0, size);
2686 cfg->spvars = g_hash_table_new (NULL, NULL);
2688 /* handle exception clauses */
2689 for (i = 0; i < header->num_clauses; ++i) {
2690 //unsigned char *p = ip;
2691 MonoExceptionClause *clause = &header->clauses [i];
2692 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->try_offset);
2693 tblock->real_offset = clause->try_offset;
2694 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
2695 tblock->real_offset = clause->handler_offset;
2697 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
2698 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2699 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2700 MONO_ADD_INS (tblock, ins);
2703 /*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);
2705 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
2707 /* catch and filter blocks get the exception object on the stack */
2708 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
2709 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2710 /* mostly like handle_stack_args (), but just sets the input args */
2711 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
2713 cfg->exvar = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
2714 /* prevent it from being register allocated */
2715 cfg->exvar->flags |= MONO_INST_INDIRECT;
2717 tblock->in_scount = 1;
2718 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2719 tblock->in_stack [0] = cfg->exvar;
2721 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2722 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->token_or_filter);
2723 tblock->real_offset = clause->token_or_filter;
2724 tblock->in_scount = 1;
2725 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2726 tblock->in_stack [0] = cfg->exvar;
2727 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2728 MONO_ADD_INS (tblock, ins);
2734 arg_array = alloca (sizeof (MonoInst *) * num_args);
2735 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
2738 /* FIRST CODE BLOCK */
2739 bblock = NEW_BBLOCK (cfg);
2740 bblock->cil_code = ip;
2742 ADD_BBLOCK (cfg, bbhash, bblock);
2744 if (cfg->method == method) {
2745 breakpoint_id = mono_debugger_method_has_breakpoint (method);
2746 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
2747 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2748 MONO_ADD_INS (bblock, ins);
2752 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED))) || mono_compile_aot) {
2753 /* we use a separate basic block for the initialization code */
2754 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
2755 init_localsbb->real_offset = real_offset;
2756 start_bblock->next_bb = init_localsbb;
2757 init_localsbb->next_bb = bblock;
2758 link_bblock (cfg, start_bblock, init_localsbb);
2759 link_bblock (cfg, init_localsbb, bblock);
2760 init_localsbb->block_num = cfg->num_bblocks++;
2762 start_bblock->next_bb = bblock;
2763 link_bblock (cfg, start_bblock, bblock);
2766 if (get_basic_blocks (cfg, bbhash, header, real_offset, ip, end, &err_pos)) {
2771 mono_debug_init_method (cfg, bblock, breakpoint_id);
2773 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
2775 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
2776 for (n = 0; n < sig->param_count; ++n)
2777 param_types [n + sig->hasthis] = sig->params [n];
2779 /* do this somewhere outside - not here */
2780 NEW_ICONST (cfg, zero_int32, 0);
2781 NEW_ICONST (cfg, zero_int64, 0);
2782 zero_int64->type = STACK_I8;
2783 NEW_PCONST (cfg, zero_ptr, 0);
2784 NEW_PCONST (cfg, zero_obj, 0);
2785 zero_obj->type = STACK_OBJ;
2787 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
2788 zero_r8->type = STACK_R8;
2789 zero_r8->inst_p0 = &r8_0;
2791 /* add a check for this != NULL to inlined methods */
2792 if (is_virtual_call) {
2793 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
2794 NEW_ARGLOAD (cfg, ins->inst_left, 0);
2796 MONO_ADD_INS (bblock, ins);
2799 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
2800 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
2803 start_new_bblock = 0;
2806 if (cfg->method == method)
2807 real_offset = ip - header->code;
2809 real_offset = inline_offset;
2811 if (start_new_bblock) {
2812 bblock->cil_length = ip - bblock->cil_code;
2813 if (start_new_bblock == 2) {
2814 g_assert (ip == tblock->cil_code);
2816 GET_BBLOCK (cfg, bbhash, tblock, ip);
2818 bblock->next_bb = tblock;
2820 start_new_bblock = 0;
2821 for (i = 0; i < bblock->in_scount; ++i) {
2822 if (cfg->verbose_level > 3)
2823 g_print ("loading %d from temp %d\n", i, bblock->in_stack [i]->inst_c0);
2824 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2828 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
2829 link_bblock (cfg, bblock, tblock);
2830 if (sp != stack_start) {
2831 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2834 bblock->next_bb = tblock;
2836 for (i = 0; i < bblock->in_scount; ++i) {
2837 if (cfg->verbose_level > 3)
2838 g_print ("loading %d from temp %d\n", i, bblock->in_stack [i]->inst_c0);
2839 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2845 bblock->real_offset = real_offset;
2847 if ((cfg->method == method) && cfg->coverage_info) {
2848 MonoInst *store, *one;
2849 guint32 cil_offset = ip - header->code;
2850 cfg->coverage_info->data [cil_offset].cil_code = ip;
2852 /* TODO: Use an increment here */
2853 NEW_ICONST (cfg, one, 1);
2856 NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
2859 MONO_INST_NEW (cfg, store, CEE_STIND_I);
2860 store->cil_code = ip;
2861 store->inst_left = ins;
2862 store->inst_right = one;
2864 MONO_ADD_INS (bblock, store);
2867 if (cfg->verbose_level > 3)
2868 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, sp-stack_start, mono_disasm_code_one (NULL, method, ip, NULL));
2875 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2876 ins->cil_code = ip++;
2877 MONO_ADD_INS (bblock, ins);
2883 CHECK_STACK_OVF (1);
2884 n = (*ip)-CEE_LDARG_0;
2886 NEW_ARGLOAD (cfg, ins, n);
2887 ins->cil_code = ip++;
2894 CHECK_STACK_OVF (1);
2895 n = (*ip)-CEE_LDLOC_0;
2897 NEW_LOCLOAD (cfg, ins, n);
2898 ins->cil_code = ip++;
2906 n = (*ip)-CEE_STLOC_0;
2909 handle_loaded_temps (cfg, bblock, stack_start, sp);
2910 NEW_LOCSTORE (cfg, ins, n, *sp);
2912 if (ins->opcode == CEE_STOBJ) {
2913 NEW_LOCLOADA (cfg, ins, n);
2914 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2916 MONO_ADD_INS (bblock, ins);
2922 CHECK_STACK_OVF (1);
2924 NEW_ARGLOAD (cfg, ins, ip [1]);
2931 CHECK_STACK_OVF (1);
2933 NEW_ARGLOADA (cfg, ins, ip [1]);
2943 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
2944 handle_loaded_temps (cfg, bblock, stack_start, sp);
2946 if (ins->opcode == CEE_STOBJ) {
2947 NEW_ARGLOADA (cfg, ins, ip [1]);
2948 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2950 MONO_ADD_INS (bblock, ins);
2955 CHECK_STACK_OVF (1);
2956 CHECK_LOCAL (ip [1]);
2957 NEW_LOCLOAD (cfg, ins, ip [1]);
2964 CHECK_STACK_OVF (1);
2965 CHECK_LOCAL (ip [1]);
2966 NEW_LOCLOADA (cfg, ins, ip [1]);
2975 handle_loaded_temps (cfg, bblock, stack_start, sp);
2976 CHECK_LOCAL (ip [1]);
2977 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
2979 if (ins->opcode == CEE_STOBJ) {
2980 NEW_LOCLOADA (cfg, ins, ip [1]);
2981 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2983 MONO_ADD_INS (bblock, ins);
2988 CHECK_STACK_OVF (1);
2989 NEW_PCONST (cfg, ins, NULL);
2991 ins->type = STACK_OBJ;
2996 CHECK_STACK_OVF (1);
2997 NEW_ICONST (cfg, ins, -1);
3011 CHECK_STACK_OVF (1);
3012 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
3019 CHECK_STACK_OVF (1);
3021 NEW_ICONST (cfg, ins, *((signed char*)ip));
3028 CHECK_STACK_OVF (1);
3029 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
3036 CHECK_STACK_OVF (1);
3037 MONO_INST_NEW (cfg, ins, OP_I8CONST);
3039 ins->type = STACK_I8;
3041 ins->inst_l = (gint64)read64 (ip);
3046 float *f = g_malloc (sizeof (float));
3048 CHECK_STACK_OVF (1);
3049 MONO_INST_NEW (cfg, ins, OP_R4CONST);
3050 ins->type = STACK_R8;
3059 double *d = g_malloc (sizeof (double));
3061 CHECK_STACK_OVF (1);
3062 MONO_INST_NEW (cfg, ins, OP_R8CONST);
3063 ins->type = STACK_R8;
3072 MonoInst *temp, *store;
3074 CHECK_STACK_OVF (1);
3079 * small optimization: if the loaded value was from a local already,
3080 * just load it twice.
3082 if (ins->ssa_op == MONO_SSA_LOAD &&
3083 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
3085 MONO_INST_NEW (cfg, temp, 0);
3087 temp->cil_code = ip;
3090 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3091 temp->cil_code = ip;
3092 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3093 store->cil_code = ip;
3094 MONO_ADD_INS (bblock, store);
3095 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3098 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3108 MONO_INST_NEW (cfg, ins, CEE_POP);
3109 MONO_ADD_INS (bblock, ins);
3110 ins->cil_code = ip++;
3116 if (stack_start != sp)
3118 MONO_INST_NEW (cfg, ins, CEE_JMP);
3119 token = read32 (ip + 1);
3120 /* FIXME: check the signature matches */
3121 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3122 ins->inst_p0 = cmethod;
3123 MONO_ADD_INS (bblock, ins);
3125 start_new_bblock = 1;
3129 case CEE_CALLVIRT: {
3130 MonoInst *addr = NULL;
3131 MonoMethodSignature *fsig = NULL;
3132 int temp, array_rank = 0;
3133 int virtual = *ip == CEE_CALLVIRT;
3136 token = read32 (ip + 1);
3138 if (*ip == CEE_CALLI) {
3143 if (method->wrapper_type != MONO_WRAPPER_NONE)
3144 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
3146 fsig = mono_metadata_parse_signature (image, token);
3148 n = fsig->param_count + fsig->hasthis;
3150 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3151 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
3152 } else if (constrained_call) {
3153 cmethod = mono_get_method_constrained (image, token, constrained_call, generic_context);
3155 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3160 if (!cmethod->klass->inited)
3161 mono_class_init (cmethod->klass);
3163 if (cmethod->signature->pinvoke) {
3164 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
3165 fsig = wrapper->signature;
3167 fsig = mono_method_get_signature (cmethod, image, token);
3170 n = fsig->param_count + fsig->hasthis;
3172 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
3173 cmethod->klass->parent == mono_defaults.array_class) {
3174 array_rank = cmethod->klass->rank;
3177 if (cmethod->string_ctor)
3178 g_assert_not_reached ();
3184 //g_assert (!virtual || fsig->hasthis);
3188 if (constrained_call) {
3190 * We have the `constrained.' prefix opcode.
3192 if (constrained_call->valuetype && !cmethod->klass->valuetype) {
3194 * The type parameter is instantiated as a valuetype,
3195 * but that type doesn't override the method we're
3196 * calling, so we need to box `this'.
3198 sp [0] = handle_box (cfg, bblock, sp [0], ip, constrained_call);
3199 } else if (!constrained_call->valuetype) {
3203 * The type parameter is instantiated as a reference
3204 * type. We have a managed pointer on the stack, so
3205 * we need to dereference it here.
3208 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3210 ins->inst_i0 = sp [0];
3211 ins->type = STACK_OBJ;
3213 } else if (cmethod->klass->valuetype)
3215 constrained_call = NULL;
3218 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
3221 if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL) && (mono_metadata_signature_equal (method->signature, cmethod->signature))) {
3223 /* FIXME: This assumes the two methods has the same number and type of arguments */
3224 for (i = 0; i < n; ++i) {
3225 /* Check if argument is the same */
3226 NEW_ARGLOAD (cfg, ins, i);
3227 if ((ins->opcode == sp [i]->opcode) && (ins->inst_i0 == sp [i]->inst_i0))
3230 /* Prevent argument from being register allocated */
3231 arg_array [i]->flags |= MONO_INST_VOLATILE;
3232 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3234 if (ins->opcode == CEE_STOBJ) {
3235 NEW_ARGLOADA (cfg, ins, i);
3236 handle_stobj (cfg, bblock, ins, sp [i], sp [i]->cil_code, ins->klass, FALSE, FALSE);
3239 MONO_ADD_INS (bblock, ins);
3241 MONO_INST_NEW (cfg, ins, CEE_JMP);
3243 ins->inst_p0 = cmethod;
3244 ins->inst_p1 = arg_array [0];
3245 MONO_ADD_INS (bblock, ins);
3246 start_new_bblock = 1;
3247 /* skip CEE_RET as well */
3252 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_opcode_for_method (cfg, cmethod, fsig, sp))) {
3255 if (MONO_TYPE_IS_VOID (fsig->ret)) {
3256 MONO_ADD_INS (bblock, ins);
3258 type_to_eval_stack_type (fsig->ret, ins);
3267 handle_loaded_temps (cfg, bblock, stack_start, sp);
3269 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3270 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
3271 mono_method_check_inlining (cfg, cmethod) &&
3272 !g_list_find (dont_inline, cmethod)) {
3274 MonoBasicBlock *ebblock;
3276 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
3277 cmethod = mono_marshal_get_native_wrapper (cmethod);
3279 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, FALSE))) {
3283 GET_BBLOCK (cfg, bbhash, bblock, ip);
3284 ebblock->next_bb = bblock;
3285 link_bblock (cfg, ebblock, bblock);
3287 if (!MONO_TYPE_IS_VOID (fsig->ret))
3290 /* indicates start of a new block, and triggers a load of all
3291 stack arguments at bb boundarie */
3294 inline_costs += costs;
3299 inline_costs += 10 * num_calls++;
3301 /* tail recursion elimination */
3302 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == method && ip [5] == CEE_RET) {
3303 gboolean has_vtargs = FALSE;
3306 /* keep it simple */
3307 for (i = fsig->param_count - 1; i >= 0; i--) {
3308 if (MONO_TYPE_ISSTRUCT (cmethod->signature->params [i]))
3313 for (i = 0; i < n; ++i) {
3314 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3316 MONO_ADD_INS (bblock, ins);
3318 MONO_INST_NEW (cfg, ins, CEE_BR);
3320 MONO_ADD_INS (bblock, ins);
3321 tblock = start_bblock->out_bb [0];
3322 link_bblock (cfg, bblock, tblock);
3323 ins->inst_target_bb = tblock;
3324 start_new_bblock = 1;
3327 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3328 /* just create a dummy - the value is never used */
3329 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
3330 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3338 if (*ip == CEE_CALLI) {
3340 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
3341 NEW_TEMPLOAD (cfg, *sp, temp);
3345 } else if (array_rank) {
3348 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
3349 if (sp [fsig->param_count]->type == STACK_OBJ) {
3350 MonoInst *iargs [2];
3351 MonoInst *array, *to_store, *store;
3353 handle_loaded_temps (cfg, bblock, stack_start, sp);
3355 array = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
3356 NEW_TEMPSTORE (cfg, store, array->inst_c0, sp [0]);
3357 store->cil_code = ip;
3358 MONO_ADD_INS (bblock, store);
3359 NEW_TEMPLOAD (cfg, iargs [0], array->inst_c0);
3361 to_store = mono_compile_create_var (cfg, type_from_stack_type (sp [fsig->param_count]), OP_LOCAL);
3362 NEW_TEMPSTORE (cfg, store, to_store->inst_c0, sp [fsig->param_count]);
3363 store->cil_code = ip;
3364 MONO_ADD_INS (bblock, store);
3365 NEW_TEMPLOAD (cfg, iargs [1], to_store->inst_c0);
3368 * We first save the args for the call so that the args are copied to the stack
3369 * and a new instruction tree for them is created. If we don't do this,
3370 * the same MonoInst is added to two different trees and this is not
3373 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref_check, iargs, ip);
3375 NEW_TEMPLOAD (cfg, sp [0], array->inst_c0);
3376 NEW_TEMPLOAD (cfg, sp [fsig->param_count], to_store->inst_c0);
3379 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
3380 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
3382 if (ins->opcode == CEE_STOBJ) {
3383 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
3385 MONO_ADD_INS (bblock, ins);
3388 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
3389 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3390 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
3394 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
3395 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3398 g_assert_not_reached ();
3402 if (0 && CODE_IS_STLOC (ip + 5) && (!MONO_TYPE_ISSTRUCT (fsig->ret)) && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)) {
3403 /* no need to spill */
3404 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
3407 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) {
3408 NEW_TEMPLOAD (cfg, *sp, temp);
3418 if (cfg->method != method) {
3419 /* return from inlined methode */
3424 //g_assert (returnvar != -1);
3425 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
3426 store->cil_code = sp [0]->cil_code;
3427 if (store->opcode == CEE_STOBJ) {
3428 g_assert_not_reached ();
3429 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
3430 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
3432 MONO_ADD_INS (bblock, store);
3436 g_assert (!return_var);
3439 MONO_INST_NEW (cfg, ins, CEE_NOP);
3440 ins->opcode = mono_type_to_stind (method->signature->ret);
3441 if (ins->opcode == CEE_STOBJ) {
3442 NEW_RETLOADA (cfg, ins);
3443 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3445 ins->opcode = OP_SETRET;
3447 ins->inst_i0 = *sp;;
3448 ins->inst_i1 = NULL;
3449 MONO_ADD_INS (bblock, ins);
3453 if (sp != stack_start)
3455 MONO_INST_NEW (cfg, ins, CEE_BR);
3456 ins->cil_code = ip++;
3457 ins->inst_target_bb = end_bblock;
3458 MONO_ADD_INS (bblock, ins);
3459 link_bblock (cfg, bblock, end_bblock);
3460 start_new_bblock = 1;
3464 MONO_INST_NEW (cfg, ins, CEE_BR);
3465 ins->cil_code = ip++;
3466 MONO_ADD_INS (bblock, ins);
3467 target = ip + 1 + (signed char)(*ip);
3469 GET_BBLOCK (cfg, bbhash, tblock, target);
3470 link_bblock (cfg, bblock, tblock);
3471 CHECK_BBLOCK (target, ip, tblock);
3472 ins->inst_target_bb = tblock;
3473 if (sp != stack_start) {
3474 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3477 start_new_bblock = 1;
3484 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3485 ins->cil_code = ip++;
3486 target = ip + 1 + *(signed char*)ip;
3488 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
3489 if (sp != stack_start) {
3490 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3507 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3508 ins->cil_code = ip++;
3509 target = ip + 1 + *(signed char*)ip;
3512 if (sp != stack_start) {
3513 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3520 MONO_INST_NEW (cfg, ins, CEE_BR);
3521 ins->cil_code = ip++;
3522 MONO_ADD_INS (bblock, ins);
3523 target = ip + 4 + (gint32)read32(ip);
3525 GET_BBLOCK (cfg, bbhash, tblock, target);
3526 link_bblock (cfg, bblock, tblock);
3527 CHECK_BBLOCK (target, ip, tblock);
3528 ins->inst_target_bb = tblock;
3529 if (sp != stack_start) {
3530 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3533 start_new_bblock = 1;
3540 MONO_INST_NEW (cfg, ins, *ip);
3541 ins->cil_code = ip++;
3542 target = ip + 4 + (gint32)read32(ip);
3544 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
3545 if (sp != stack_start) {
3546 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3563 MONO_INST_NEW (cfg, ins, *ip);
3564 ins->cil_code = ip++;
3565 target = ip + 4 + (gint32)read32(ip);
3568 if (sp != stack_start) {
3569 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3577 n = read32 (ip + 1);
3578 MONO_INST_NEW (cfg, ins, *ip);
3580 ins->inst_left = *sp;
3581 if (ins->inst_left->type != STACK_I4) goto unverified;
3584 CHECK_OPSIZE (n * sizeof (guint32));
3585 target = ip + n * sizeof (guint32);
3586 MONO_ADD_INS (bblock, ins);
3587 GET_BBLOCK (cfg, bbhash, tblock, target);
3588 link_bblock (cfg, bblock, tblock);
3589 ins->klass = GUINT_TO_POINTER (n);
3590 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
3591 ins->inst_many_bb [n] = tblock;
3593 for (i = 0; i < n; ++i) {
3594 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
3595 link_bblock (cfg, bblock, tblock);
3596 ins->inst_many_bb [i] = tblock;
3599 if (sp != stack_start) {
3600 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3617 MONO_INST_NEW (cfg, ins, *ip);
3622 ins->type = ldind_type [*ip - CEE_LDIND_I1];
3623 ins->flags |= ins_flag;
3635 MONO_INST_NEW (cfg, ins, *ip);
3636 ins->cil_code = ip++;
3638 handle_loaded_temps (cfg, bblock, stack_start, sp);
3639 MONO_ADD_INS (bblock, ins);
3640 ins->inst_i0 = sp [0];
3641 ins->inst_i1 = sp [1];
3642 ins->flags |= ins_flag;
3661 /* special case that gives a nice speedup and happens to workaorund a ppc jit but (for the release)
3662 * later apply the speedup to the left shift as well
3665 if ((ins->opcode == OP_LSHR_UN) && (ins->type == STACK_I8)
3666 && (ins->inst_right->opcode == OP_ICONST) && (ins->inst_right->inst_c0 == 32)) {
3667 ins->opcode = OP_LONG_SHRUN_32;
3668 /*g_print ("applied long shr speedup to %s\n", cfg->method->name);*/
3672 if (mono_find_jit_opcode_emulation (ins->opcode)) {
3674 *sp++ = emit_tree (cfg, bblock, ins);
3688 case CEE_CONV_OVF_I8:
3689 case CEE_CONV_OVF_U8:
3693 if (mono_find_jit_opcode_emulation (ins->opcode)) {
3695 *sp++ = emit_tree (cfg, bblock, ins);
3699 case CEE_CONV_OVF_I4:
3700 case CEE_CONV_OVF_I1:
3701 case CEE_CONV_OVF_I2:
3702 case CEE_CONV_OVF_I:
3703 case CEE_CONV_OVF_U:
3706 if (sp [-1]->type == STACK_R8) {
3707 ADD_UNOP (CEE_CONV_OVF_I8);
3715 case CEE_CONV_OVF_U1:
3716 case CEE_CONV_OVF_U2:
3717 case CEE_CONV_OVF_U4:
3720 if (sp [-1]->type == STACK_R8) {
3721 ADD_UNOP (CEE_CONV_OVF_U8);
3729 case CEE_CONV_OVF_I1_UN:
3730 case CEE_CONV_OVF_I2_UN:
3731 case CEE_CONV_OVF_I4_UN:
3732 case CEE_CONV_OVF_I8_UN:
3733 case CEE_CONV_OVF_U1_UN:
3734 case CEE_CONV_OVF_U2_UN:
3735 case CEE_CONV_OVF_U4_UN:
3736 case CEE_CONV_OVF_U8_UN:
3737 case CEE_CONV_OVF_I_UN:
3738 case CEE_CONV_OVF_U_UN:
3746 token = read32 (ip + 1);
3747 if (method->wrapper_type != MONO_WRAPPER_NONE)
3748 klass = mono_method_get_wrapper_data (method, token);
3750 klass = mono_class_get_full (image, token, generic_context);
3752 mono_class_init (klass);
3754 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3755 MonoInst *store, *load;
3756 MONO_INST_NEW (cfg, load, CEE_LDIND_REF);
3757 load->cil_code = ip;
3758 load->inst_i0 = sp [1];
3759 load->type = ldind_type [CEE_LDIND_REF];
3760 load->flags |= ins_flag;
3761 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
3762 store->cil_code = ip;
3763 handle_loaded_temps (cfg, bblock, stack_start, sp);
3764 MONO_ADD_INS (bblock, store);
3765 store->inst_i0 = sp [0];
3766 store->inst_i1 = load;
3767 store->flags |= ins_flag;
3769 n = mono_class_value_size (klass, NULL);
3770 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3772 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3773 copy->inst_left = sp [0];
3774 copy->inst_right = sp [1];
3775 copy->cil_code = ip;
3777 MONO_ADD_INS (bblock, copy);
3779 MonoInst *iargs [3];
3782 NEW_ICONST (cfg, iargs [2], n);
3783 iargs [2]->cil_code = ip;
3785 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3792 MonoInst *iargs [3];
3796 token = read32 (ip + 1);
3797 if (method->wrapper_type != MONO_WRAPPER_NONE)
3798 klass = mono_method_get_wrapper_data (method, token);
3800 klass = mono_class_get_full (image, token, generic_context);
3802 mono_class_init (klass);
3803 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3804 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3806 ins->inst_i0 = sp [0];
3807 ins->type = ldind_type [CEE_LDIND_REF];
3808 ins->flags |= ins_flag;
3814 n = mono_class_value_size (klass, NULL);
3815 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3816 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3817 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3819 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3820 copy->inst_left = iargs [0];
3821 copy->inst_right = *sp;
3822 copy->cil_code = ip;
3824 MONO_ADD_INS (bblock, copy);
3827 NEW_ICONST (cfg, iargs [2], n);
3828 iargs [2]->cil_code = ip;
3830 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3832 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3840 CHECK_STACK_OVF (1);
3842 n = read32 (ip + 1);
3844 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3846 MonoInst *iargs [1];
3848 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
3849 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
3850 NEW_TEMPLOAD (cfg, *sp, temp);
3854 if (cfg->opt & MONO_OPT_SHARED) {
3856 MonoInst *iargs [3];
3858 if (mono_compile_aot) {
3859 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n);
3862 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3863 NEW_IMAGECONST (cfg, iargs [1], image);
3864 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
3865 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
3866 NEW_TEMPLOAD (cfg, *sp, temp);
3867 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3869 if (mono_compile_aot)
3870 NEW_LDSTRCONST (cfg, ins, image, n);
3872 NEW_PCONST (cfg, ins, NULL);
3874 ins->type = STACK_OBJ;
3875 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3885 MonoInst *iargs [2];
3886 MonoMethodSignature *fsig;
3890 token = read32 (ip + 1);
3891 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3892 cmethod = mono_method_get_wrapper_data (method, token);
3894 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3895 fsig = mono_method_get_signature (cmethod, image, token);
3897 mono_class_init (cmethod->klass);
3899 n = fsig->param_count;
3902 /* move the args to allow room for 'this' in the first position */
3908 handle_loaded_temps (cfg, bblock, stack_start, sp);
3911 if (cmethod->klass->parent == mono_defaults.array_class) {
3912 NEW_METHODCONST (cfg, *sp, cmethod);
3913 temp = mono_emit_native_call (cfg, bblock, mono_array_new_va, fsig, sp, ip, FALSE);
3915 } else if (cmethod->string_ctor) {
3916 /* we simply pass a null pointer */
3917 NEW_PCONST (cfg, *sp, NULL);
3918 /* now call the string ctor */
3919 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
3921 if (cmethod->klass->valuetype) {
3922 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
3923 temp = iargs [0]->inst_c0;
3924 NEW_TEMPLOADA (cfg, *sp, temp);
3926 if (cfg->opt & MONO_OPT_SHARED) {
3927 NEW_DOMAINCONST (cfg, iargs [0]);
3928 NEW_CLASSCONST (cfg, iargs [1], cmethod->klass);
3930 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3932 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
3933 NEW_VTABLECONST (cfg, iargs [0], vtable);
3934 if (cmethod->klass->has_finalize || cmethod->klass->marshalbyref || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
3935 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
3937 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
3939 NEW_TEMPLOAD (cfg, *sp, temp);
3942 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3943 mono_method_check_inlining (cfg, cmethod) &&
3944 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
3945 !g_list_find (dont_inline, cmethod)) {
3947 MonoBasicBlock *ebblock;
3948 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, FALSE))) {
3953 GET_BBLOCK (cfg, bbhash, bblock, ip);
3954 ebblock->next_bb = bblock;
3955 link_bblock (cfg, ebblock, bblock);
3957 NEW_TEMPLOAD (cfg, *sp, temp);
3960 /* indicates start of a new block, and triggers a load
3961 of all stack arguments at bb boundarie */
3964 inline_costs += costs;
3968 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3971 /* now call the actual ctor */
3972 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3976 NEW_TEMPLOAD (cfg, *sp, temp);
3987 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
3988 mono_class_init (klass);
3990 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
3992 MonoMethod *mono_isinst;
3993 MonoInst *iargs [1];
3994 MonoBasicBlock *ebblock;
3998 mono_isinst = mono_marshal_get_isinst (klass);
4001 costs = inline_method (cfg, mono_isinst, mono_isinst->signature, bblock,
4002 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4004 g_assert (costs > 0);
4009 GET_BBLOCK (cfg, bbhash, bblock, ip);
4010 ebblock->next_bb = bblock;
4011 link_bblock (cfg, ebblock, bblock);
4013 temp = iargs [0]->inst_i0->inst_c0;
4014 NEW_TEMPLOAD (cfg, *sp, temp);
4018 inline_costs += costs;
4022 MONO_INST_NEW (cfg, ins, *ip);
4023 ins->type = STACK_OBJ;
4024 ins->inst_left = *sp;
4025 ins->inst_newa_class = klass;
4027 *sp++ = emit_tree (cfg, bblock, ins);
4031 case CEE_UNBOX_ANY: {
4032 MonoInst *add, *vtoffset;
4033 MonoInst *iargs [3];
4038 token = read32 (ip + 1);
4039 if (method->wrapper_type != MONO_WRAPPER_NONE)
4040 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4042 klass = mono_class_get_full (image, token, generic_context);
4043 mono_class_init (klass);
4045 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4047 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4048 MonoMethod *mono_castclass;
4049 MonoInst *iargs [1];
4050 MonoBasicBlock *ebblock;
4054 mono_castclass = mono_marshal_get_castclass (klass);
4057 costs = inline_method (cfg, mono_castclass, mono_castclass->signature, bblock,
4058 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4060 g_assert (costs > 0);
4065 GET_BBLOCK (cfg, bbhash, bblock, ip);
4066 ebblock->next_bb = bblock;
4067 link_bblock (cfg, ebblock, bblock);
4069 temp = iargs [0]->inst_i0->inst_c0;
4070 NEW_TEMPLOAD (cfg, *sp, temp);
4074 inline_costs += costs;
4077 MONO_INST_NEW (cfg, ins, CEE_CASTCLASS);
4078 ins->type = STACK_OBJ;
4079 ins->inst_left = *sp;
4081 ins->inst_newa_class = klass;
4089 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
4090 ins->type = STACK_OBJ;
4091 ins->inst_left = *sp;
4093 ins->inst_newa_class = klass;
4096 MONO_INST_NEW (cfg, add, CEE_ADD);
4097 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4098 add->inst_left = ins;
4099 add->inst_right = vtoffset;
4100 add->type = STACK_MP;
4104 n = mono_class_value_size (klass, NULL);
4105 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
4106 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
4107 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
4109 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
4110 copy->inst_left = iargs [0];
4111 copy->inst_right = *sp;
4112 copy->cil_code = ip;
4114 MONO_ADD_INS (bblock, copy);
4117 NEW_ICONST (cfg, iargs [2], n);
4118 iargs [2]->cil_code = ip;
4120 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
4122 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
4128 MonoInst *add, *vtoffset;
4133 token = read32 (ip + 1);
4134 if (method->wrapper_type != MONO_WRAPPER_NONE)
4135 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4137 klass = mono_class_get_full (image, token, generic_context);
4138 mono_class_init (klass);
4140 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
4141 ins->type = STACK_OBJ;
4142 ins->inst_left = *sp;
4144 ins->inst_newa_class = klass;
4147 MONO_INST_NEW (cfg, add, CEE_ADD);
4148 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4149 add->inst_left = ins;
4150 add->inst_right = vtoffset;
4151 add->type = STACK_MP;
4161 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4162 mono_class_init (klass);
4164 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4166 MonoMethod *mono_castclass;
4167 MonoInst *iargs [1];
4168 MonoBasicBlock *ebblock;
4172 mono_castclass = mono_marshal_get_castclass (klass);
4175 costs = inline_method (cfg, mono_castclass, mono_castclass->signature, bblock,
4176 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4178 g_assert (costs > 0);
4183 GET_BBLOCK (cfg, bbhash, bblock, ip);
4184 ebblock->next_bb = bblock;
4185 link_bblock (cfg, ebblock, bblock);
4187 temp = iargs [0]->inst_i0->inst_c0;
4188 NEW_TEMPLOAD (cfg, *sp, temp);
4192 inline_costs += costs;
4195 MONO_INST_NEW (cfg, ins, *ip);
4196 ins->type = STACK_OBJ;
4197 ins->inst_left = *sp;
4199 ins->inst_newa_class = klass;
4201 *sp++ = emit_tree (cfg, bblock, ins);
4207 MONO_INST_NEW (cfg, ins, *ip);
4209 ins->inst_left = *sp;
4210 ins->cil_code = ip++;
4211 MONO_ADD_INS (bblock, ins);
4213 start_new_bblock = 1;
4218 MonoInst *offset_ins;
4219 MonoClassField *field;
4220 MonoBasicBlock *ebblock;
4224 if (*ip == CEE_STFLD) {
4231 // FIXME: enable this test later.
4232 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
4235 token = read32 (ip + 1);
4236 field = mono_field_from_token (image, token, &klass, generic_context);
4237 mono_class_init (klass);
4239 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
4240 /* FIXME: mark instructions for use in SSA */
4241 if (*ip == CEE_STFLD) {
4242 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound) {
4243 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
4244 MonoInst *iargs [5];
4247 NEW_CLASSCONST (cfg, iargs [1], klass);
4248 NEW_FIELDCONST (cfg, iargs [2], field);
4249 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
4253 if (cfg->opt & MONO_OPT_INLINE) {
4254 costs = inline_method (cfg, stfld_wrapper, stfld_wrapper->signature, bblock,
4255 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4256 g_assert (costs > 0);
4261 GET_BBLOCK (cfg, bbhash, bblock, ip);
4262 ebblock->next_bb = bblock;
4263 link_bblock (cfg, ebblock, bblock);
4265 /* indicates start of a new block, and triggers a load
4266 of all stack arguments at bb boundarie */
4269 inline_costs += costs;
4272 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, stfld_wrapper->signature, iargs, ip, NULL);
4276 NEW_ICONST (cfg, offset_ins, foffset);
4277 MONO_INST_NEW (cfg, ins, CEE_ADD);
4279 ins->inst_left = *sp;
4280 ins->inst_right = offset_ins;
4281 ins->type = STACK_MP;
4283 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4284 store->cil_code = ip;
4285 store->inst_left = ins;
4286 store->inst_right = sp [1];
4287 handle_loaded_temps (cfg, bblock, stack_start, sp);
4288 store->flags |= ins_flag;
4290 if (store->opcode == CEE_STOBJ) {
4291 handle_stobj (cfg, bblock, ins, sp [1], ip,
4292 mono_class_from_mono_type (field->type), FALSE, FALSE);
4294 MONO_ADD_INS (bblock, store);
4297 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound) {
4298 /* fixme: we need to inline that call somehow */
4299 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
4300 MonoInst *iargs [4];
4304 NEW_CLASSCONST (cfg, iargs [1], klass);
4305 NEW_FIELDCONST (cfg, iargs [2], field);
4306 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
4307 if (cfg->opt & MONO_OPT_INLINE) {
4308 costs = inline_method (cfg, ldfld_wrapper, ldfld_wrapper->signature, bblock,
4309 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
4310 g_assert (costs > 0);
4315 GET_BBLOCK (cfg, bbhash, bblock, ip);
4316 ebblock->next_bb = bblock;
4317 link_bblock (cfg, ebblock, bblock);
4319 temp = iargs [0]->inst_i0->inst_c0;
4321 if (*ip == CEE_LDFLDA) {
4322 /* not sure howto handle this */
4323 NEW_TEMPLOADA (cfg, *sp, temp);
4325 NEW_TEMPLOAD (cfg, *sp, temp);
4329 /* indicates start of a new block, and triggers a load of
4330 all stack arguments at bb boundarie */
4333 inline_costs += costs;
4336 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, ldfld_wrapper->signature, iargs, ip, NULL);
4337 if (*ip == CEE_LDFLDA) {
4338 /* not sure howto handle this */
4339 NEW_TEMPLOADA (cfg, *sp, temp);
4341 NEW_TEMPLOAD (cfg, *sp, temp);
4346 NEW_ICONST (cfg, offset_ins, foffset);
4347 MONO_INST_NEW (cfg, ins, CEE_ADD);
4349 ins->inst_left = *sp;
4350 ins->inst_right = offset_ins;
4351 ins->type = STACK_MP;
4353 if (*ip == CEE_LDFLDA) {
4357 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4358 type_to_eval_stack_type (field->type, load);
4359 load->cil_code = ip;
4360 load->inst_left = ins;
4361 load->flags |= ins_flag;
4373 MonoClassField *field;
4374 gpointer addr = NULL;
4377 token = read32 (ip + 1);
4379 field = mono_field_from_token (image, token, &klass, generic_context);
4380 mono_class_init (klass);
4382 if ((*ip) == CEE_STSFLD)
4383 handle_loaded_temps (cfg, bblock, stack_start, sp);
4385 if (cfg->domain->special_static_fields)
4386 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
4388 if ((cfg->opt & MONO_OPT_SHARED) || (mono_compile_aot && addr)) {
4390 MonoInst *iargs [2];
4391 g_assert (field->parent);
4392 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
4393 NEW_FIELDCONST (cfg, iargs [1], field);
4394 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
4395 NEW_TEMPLOAD (cfg, ins, temp);
4398 vtable = mono_class_vtable (cfg->domain, klass);
4400 if ((!vtable->initialized || mono_compile_aot) && !(klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (klass, method)) {
4401 guint8 *tramp = mono_create_class_init_trampoline (vtable);
4402 mono_emit_native_call (cfg, bblock, tramp,
4403 helper_sig_class_init_trampoline,
4405 if (cfg->verbose_level > 2)
4406 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
4408 if (cfg->run_cctors)
4409 mono_runtime_class_init (vtable);
4411 addr = (char*)vtable->data + field->offset;
4413 if (mono_compile_aot)
4414 NEW_SFLDACONST (cfg, ins, field);
4416 NEW_PCONST (cfg, ins, addr);
4420 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
4421 * This could be later optimized to do just a couple of
4422 * memory dereferences with constant offsets.
4425 MonoInst *iargs [1];
4426 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
4427 temp = mono_emit_jit_icall (cfg, bblock, mono_get_special_static_data, iargs, ip);
4428 NEW_TEMPLOAD (cfg, ins, temp);
4432 /* FIXME: mark instructions for use in SSA */
4433 if (*ip == CEE_LDSFLDA) {
4435 } else if (*ip == CEE_STSFLD) {
4439 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4440 store->cil_code = ip;
4441 store->inst_left = ins;
4442 store->inst_right = sp [0];
4443 store->flags |= ins_flag;
4446 if (store->opcode == CEE_STOBJ) {
4447 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
4449 MONO_ADD_INS (bblock, store);
4451 gboolean is_const = FALSE;
4452 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4453 if (!((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) &&
4454 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
4455 gpointer addr = (char*)vtable->data + field->offset;
4456 /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
4458 switch (field->type->type) {
4459 case MONO_TYPE_BOOLEAN:
4461 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
4465 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
4468 case MONO_TYPE_CHAR:
4470 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
4474 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
4479 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
4483 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
4488 case MONO_TYPE_STRING:
4489 case MONO_TYPE_OBJECT:
4490 case MONO_TYPE_CLASS:
4491 case MONO_TYPE_SZARRAY:
4493 case MONO_TYPE_FNPTR:
4494 case MONO_TYPE_ARRAY:
4495 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
4496 type_to_eval_stack_type (field->type, *sp);
4501 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
4502 sp [0]->type = STACK_I8;
4503 sp [0]->inst_l = *((gint64 *)addr);
4508 case MONO_TYPE_VALUETYPE:
4517 CHECK_STACK_OVF (1);
4518 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4519 type_to_eval_stack_type (field->type, load);
4520 load->cil_code = ip;
4521 load->inst_left = ins;
4523 load->flags |= ins_flag;
4525 /* fixme: dont see the problem why this does not work */
4526 //cfg->disable_aot = TRUE;
4536 token = read32 (ip + 1);
4537 if (method->wrapper_type != MONO_WRAPPER_NONE)
4538 klass = mono_method_get_wrapper_data (method, token);
4540 klass = mono_class_get_full (image, token, generic_context);
4541 mono_class_init (klass);
4542 n = mono_type_to_stind (&klass->byval_arg);
4543 if (n == CEE_STOBJ) {
4544 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
4546 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
4548 MONO_INST_NEW (cfg, store, n);
4549 store->cil_code = ip;
4550 store->inst_left = sp [0];
4551 store->inst_right = sp [1];
4552 store->flags |= ins_flag;
4553 MONO_ADD_INS (bblock, store);
4565 token = read32 (ip + 1);
4566 if (method->wrapper_type != MONO_WRAPPER_NONE)
4567 klass = mono_method_get_wrapper_data (method, token);
4569 klass = mono_class_get_full (image, token, generic_context);
4570 mono_class_init (klass);
4572 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4577 *sp++ = handle_box (cfg, bblock, val, ip, klass);
4584 MONO_INST_NEW (cfg, ins, *ip);
4589 token = read32 (ip + 1);
4591 /* allocate the domainvar - becaus this is used in decompose_foreach */
4592 if (cfg->opt & MONO_OPT_SHARED)
4593 mono_get_domainvar (cfg);
4595 if (method->wrapper_type != MONO_WRAPPER_NONE)
4596 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4598 klass = mono_class_get_full (image, token, generic_context);
4600 mono_class_init (klass);
4601 ins->inst_newa_class = klass;
4602 ins->inst_newa_len = *sp;
4603 ins->type = STACK_OBJ;
4607 * we store the object so calls to create the array are not interleaved
4608 * with the arguments of other calls.
4611 MonoInst *store, *temp, *load;
4613 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
4614 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4615 store->cil_code = ins->cil_code;
4616 MONO_ADD_INS (bblock, store);
4617 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
4618 load->cil_code = ins->cil_code;
4625 MONO_INST_NEW (cfg, ins, *ip);
4626 ins->cil_code = ip++;
4628 ins->inst_left = *sp;
4629 ins->type = STACK_PTR;
4637 if (method->wrapper_type != MONO_WRAPPER_NONE)
4638 klass = (MonoClass*)mono_method_get_wrapper_data (method, read32 (ip + 1));
4640 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4641 mono_class_init (klass);
4642 NEW_LDELEMA (cfg, ins, sp, klass);
4652 token = read32 (ip + 1);
4653 klass = mono_class_get_full (image, token, generic_context);
4654 mono_class_init (klass);
4655 NEW_LDELEMA (cfg, load, sp, klass);
4656 load->cil_code = ip;
4657 MONO_INST_NEW (cfg, ins, mono_type_to_ldind (&klass->byval_arg));
4659 ins->inst_left = load;
4661 type_to_eval_stack_type (&klass->byval_arg, ins);
4675 case CEE_LDELEM_REF: {
4679 * ldind.x (ldelema (array, index))
4680 * ldelema does the bounds check
4684 klass = array_access_to_klass (*ip);
4685 NEW_LDELEMA (cfg, load, sp, klass);
4686 load->cil_code = ip;
4687 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
4689 ins->inst_left = load;
4691 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
4701 case CEE_STELEM_R8: {
4705 * stind.x (ldelema (array, index), val)
4706 * ldelema does the bounds check
4710 klass = array_access_to_klass (*ip);
4711 NEW_LDELEMA (cfg, load, sp, klass);
4712 load->cil_code = ip;
4713 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
4715 ins->inst_left = load;
4716 ins->inst_right = sp [2];
4718 handle_loaded_temps (cfg, bblock, stack_start, sp);
4719 MONO_ADD_INS (bblock, ins);
4727 * stind.x (ldelema (array, index), val)
4728 * ldelema does the bounds check
4733 token = read32 (ip + 1);
4734 klass = mono_class_get_full (image, token, generic_context);
4735 mono_class_init (klass);
4736 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4737 MonoInst *iargs [3];
4738 handle_loaded_temps (cfg, bblock, stack_start, sp);
4744 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4746 NEW_LDELEMA (cfg, load, sp, klass);
4747 load->cil_code = ip;
4749 n = mono_type_to_stind (&klass->byval_arg);
4751 handle_stobj (cfg, bblock, load, sp [2], ip, klass, FALSE, FALSE);
4753 MONO_INST_NEW (cfg, ins, n);
4755 ins->inst_left = load;
4756 ins->inst_right = sp [2];
4757 handle_loaded_temps (cfg, bblock, stack_start, sp);
4758 MONO_ADD_INS (bblock, ins);
4765 case CEE_STELEM_REF: {
4766 MonoInst *iargs [3];
4771 handle_loaded_temps (cfg, bblock, stack_start, sp);
4777 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4781 NEW_GROUP (cfg, group, sp [0], sp [1]);
4782 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
4784 ins->inst_left = group;
4785 ins->inst_right = sp [2];
4786 MONO_ADD_INS (bblock, ins);
4793 case CEE_CKFINITE: {
4794 MonoInst *store, *temp;
4797 /* this instr. can throw exceptions as side effect,
4798 * so we cant eliminate dead code which contains CKFINITE opdodes.
4799 * Spilling to memory makes sure that we always perform
4803 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
4805 ins->inst_left = sp [-1];
4806 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
4808 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4809 store->cil_code = ip;
4810 MONO_ADD_INS (bblock, store);
4812 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
4819 MONO_INST_NEW (cfg, ins, *ip);
4822 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4823 mono_class_init (klass);
4824 ins->type = STACK_MP;
4825 ins->inst_left = *sp;
4827 ins->inst_newa_class = klass;
4832 case CEE_MKREFANY: {
4833 MonoInst *loc, *klassconst;
4836 MONO_INST_NEW (cfg, ins, *ip);
4839 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4840 mono_class_init (klass);
4843 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
4844 NEW_TEMPLOADA (cfg, ins->inst_right, loc->inst_c0);
4846 NEW_PCONST (cfg, klassconst, klass);
4847 NEW_GROUP (cfg, ins->inst_left, *sp, klassconst);
4849 MONO_ADD_INS (bblock, ins);
4851 NEW_TEMPLOAD (cfg, *sp, loc->inst_c0);
4858 MonoClass *handle_class;
4860 CHECK_STACK_OVF (1);
4863 n = read32 (ip + 1);
4865 handle = mono_ldtoken (image, n, &handle_class, generic_context);
4866 mono_class_init (handle_class);
4868 if (cfg->opt & MONO_OPT_SHARED) {
4870 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
4872 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
4874 NEW_IMAGECONST (cfg, iargs [0], image);
4875 NEW_ICONST (cfg, iargs [1], n);
4876 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
4877 NEW_TEMPLOAD (cfg, res, temp);
4878 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4879 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
4880 MONO_ADD_INS (bblock, store);
4881 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4883 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method_full (image, read32 (ip + 6), NULL, generic_context)) &&
4884 (cmethod->klass == mono_defaults.monotype_class->parent) &&
4885 (strcmp (cmethod->name, "GetTypeFromHandle") == 0) &&
4886 ((g_hash_table_lookup (bbhash, ip + 5) == NULL) ||
4887 (g_hash_table_lookup (bbhash, ip + 5) == bblock))) {
4888 MonoClass *tclass = mono_class_from_mono_type (handle);
4889 mono_class_init (tclass);
4890 if (mono_compile_aot)
4891 NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n);
4893 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
4894 ins->type = STACK_OBJ;
4895 ins->klass = cmethod->klass;
4898 MonoInst *store, *addr, *vtvar;
4900 if (mono_compile_aot)
4901 NEW_LDTOKENCONST (cfg, ins, image, n);
4903 NEW_PCONST (cfg, ins, handle);
4904 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
4905 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4906 NEW_INDSTORE (cfg, store, addr, ins, &mono_defaults.int_class->byval_arg);
4907 MONO_ADD_INS (bblock, store);
4908 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4924 case CEE_ADD_OVF_UN:
4926 case CEE_MUL_OVF_UN:
4928 case CEE_SUB_OVF_UN:
4931 if (mono_find_jit_opcode_emulation (ins->opcode)) {
4933 *sp++ = emit_tree (cfg, bblock, ins);
4937 case CEE_ENDFINALLY:
4938 MONO_INST_NEW (cfg, ins, *ip);
4939 MONO_ADD_INS (bblock, ins);
4940 ins->cil_code = ip++;
4941 start_new_bblock = 1;
4944 * Control will leave the method so empty the stack, otherwise
4945 * the next basic block will start with a nonempty stack.
4947 while (sp != stack_start) {
4948 MONO_INST_NEW (cfg, ins, CEE_POP);
4952 MONO_ADD_INS (bblock, ins);
4958 if (*ip == CEE_LEAVE) {
4960 target = ip + 5 + (gint32)read32(ip + 1);
4963 target = ip + 2 + (signed char)(ip [1]);
4966 /* empty the stack */
4967 while (sp != stack_start) {
4968 MONO_INST_NEW (cfg, ins, CEE_POP);
4972 MONO_ADD_INS (bblock, ins);
4976 * If this leave statement is in a catch block, check for a
4977 * pending exception, and rethrow it if necessary.
4979 for (i = 0; i < header->num_clauses; ++i) {
4980 MonoExceptionClause *clause = &header->clauses [i];
4981 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code)) {
4984 temp = mono_emit_jit_icall (cfg, bblock, mono_thread_get_pending_exception, NULL, ip);
4985 NEW_TEMPLOAD (cfg, *sp, temp);
4987 MONO_INST_NEW (cfg, ins, OP_THROW_OR_NULL);
4988 ins->inst_left = *sp;
4990 MONO_ADD_INS (bblock, ins);
4994 /* fixme: call fault handler ? */
4996 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
4998 for (tmp = handlers; tmp; tmp = tmp->next) {
5000 link_bblock (cfg, bblock, tblock);
5001 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
5003 ins->inst_target_bb = tblock;
5004 MONO_ADD_INS (bblock, ins);
5006 g_list_free (handlers);
5009 MONO_INST_NEW (cfg, ins, CEE_BR);
5011 MONO_ADD_INS (bblock, ins);
5012 GET_BBLOCK (cfg, bbhash, tblock, target);
5013 link_bblock (cfg, bblock, tblock);
5014 CHECK_BBLOCK (target, ip, tblock);
5015 ins->inst_target_bb = tblock;
5016 start_new_bblock = 1;
5018 if (*ip == CEE_LEAVE)
5027 MONO_INST_NEW (cfg, ins, *ip);
5029 handle_loaded_temps (cfg, bblock, stack_start, sp);
5030 MONO_ADD_INS (bblock, ins);
5031 ins->cil_code = ip++;
5032 ins->inst_i0 = sp [0];
5033 ins->inst_i1 = sp [1];
5041 /* trampoline mono specific opcodes */
5042 case MONO_CUSTOM_PREFIX: {
5044 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
5049 case CEE_MONO_FUNC1: {
5051 gpointer func = NULL;
5057 case MONO_MARSHAL_CONV_STR_LPWSTR:
5058 func = mono_string_to_utf16;
5060 case MONO_MARSHAL_CONV_LPWSTR_STR:
5061 func = mono_string_from_utf16;
5063 case MONO_MARSHAL_CONV_LPSTR_STR:
5064 func = mono_string_new_wrapper;
5066 case MONO_MARSHAL_CONV_STR_LPTSTR:
5067 case MONO_MARSHAL_CONV_STR_LPSTR:
5068 func = mono_string_to_utf8;
5070 case MONO_MARSHAL_CONV_STR_BSTR:
5071 func = mono_string_to_bstr;
5073 case MONO_MARSHAL_CONV_STR_TBSTR:
5074 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
5075 func = mono_string_to_ansibstr;
5077 case MONO_MARSHAL_CONV_SB_LPSTR:
5078 case MONO_MARSHAL_CONV_SB_LPTSTR:
5079 func = mono_string_builder_to_utf8;
5081 case MONO_MARSHAL_CONV_SB_LPWSTR:
5082 func = mono_string_builder_to_utf16;
5084 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
5085 func = mono_array_to_savearray;
5087 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
5088 func = mono_array_to_lparray;
5090 case MONO_MARSHAL_CONV_DEL_FTN:
5091 func = mono_delegate_to_ftnptr;
5093 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
5094 func = mono_marshal_string_array;
5096 case MONO_MARSHAL_CONV_STRARRAY_STRWLPARRAY:
5097 func = mono_marshal_string_array_to_unicode;
5100 g_warning ("unknown conversion %d\n", ip [2]);
5101 g_assert_not_reached ();
5104 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5105 NEW_TEMPLOAD (cfg, *sp, temp);
5109 inline_costs += 10 * num_calls++;
5113 case CEE_MONO_FUNC2: {
5115 gpointer func = NULL;
5121 case MONO_MARSHAL_CONV_ASANY:
5122 func = mono_marshal_asany;
5124 case MONO_MARSHAL_CONV_FTN_DEL:
5125 func = mono_ftnptr_to_delegate;
5128 g_warning ("unknown conversion %d\n", ip [2]);
5129 g_assert_not_reached ();
5131 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5132 NEW_TEMPLOAD (cfg, *sp, temp);
5136 inline_costs += 10 * num_calls++;
5140 case CEE_MONO_PROC2: {
5141 gpointer func = NULL;
5147 case MONO_MARSHAL_CONV_LPSTR_SB:
5148 case MONO_MARSHAL_CONV_LPTSTR_SB:
5149 func = mono_string_utf8_to_builder;
5151 case MONO_MARSHAL_CONV_LPWSTR_SB:
5152 func = mono_string_utf16_to_builder;
5154 case MONO_MARSHAL_FREE_ARRAY:
5155 func = mono_marshal_free_array;
5157 case MONO_MARSHAL_CONV_ASANY:
5158 func = mono_marshal_asany;
5161 g_assert_not_reached ();
5164 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5166 inline_costs += 10 * num_calls++;
5169 case CEE_MONO_PROC3: {
5170 gpointer func = NULL;
5176 case MONO_MARSHAL_CONV_STR_BYVALSTR:
5177 func = mono_string_to_byvalstr;
5179 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
5180 func = mono_string_to_byvalwstr;
5182 case MONO_MARSHAL_FREE_ASANY:
5183 func = mono_marshal_free_asany;
5186 g_assert_not_reached ();
5189 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5191 inline_costs += 10 * num_calls++;
5197 mono_emit_jit_icall (cfg, bblock, g_free, sp, ip);
5199 inline_costs += 10 * num_calls++;
5201 case CEE_MONO_LDPTR:
5202 CHECK_STACK_OVF (1);
5204 token = read32 (ip + 2);
5205 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
5209 inline_costs += 10 * num_calls++;
5211 case CEE_MONO_VTADDR:
5214 MONO_INST_NEW (cfg, ins, OP_VTADDR);
5216 ins->type = STACK_MP;
5217 ins->inst_left = *sp;
5221 case CEE_MONO_NEWOBJ: {
5222 MonoInst *iargs [2];
5224 CHECK_STACK_OVF (1);
5226 token = read32 (ip + 2);
5227 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5228 mono_class_init (klass);
5229 NEW_DOMAINCONST (cfg, iargs [0]);
5230 NEW_CLASSCONST (cfg, iargs [1], klass);
5231 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
5232 NEW_TEMPLOAD (cfg, *sp, temp);
5235 inline_costs += 10 * num_calls++;
5238 case CEE_MONO_OBJADDR:
5241 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
5243 ins->type = STACK_MP;
5244 ins->inst_left = *sp;
5248 case CEE_MONO_LDNATIVEOBJ:
5251 token = read32 (ip + 2);
5252 klass = mono_method_get_wrapper_data (method, token);
5253 g_assert (klass->valuetype);
5254 mono_class_init (klass);
5255 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
5259 case CEE_MONO_RETOBJ:
5260 g_assert (cfg->ret);
5261 g_assert (method->signature->pinvoke);
5266 token = read32 (ip + 2);
5267 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5269 NEW_RETLOADA (cfg, ins);
5270 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
5272 if (sp != stack_start)
5275 MONO_INST_NEW (cfg, ins, CEE_BR);
5277 ins->inst_target_bb = end_bblock;
5278 MONO_ADD_INS (bblock, ins);
5279 link_bblock (cfg, bblock, end_bblock);
5280 start_new_bblock = 1;
5283 case CEE_MONO_CISINST:
5284 case CEE_MONO_CCASTCLASS: {
5289 token = read32 (ip + 2);
5290 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5291 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_CISINST) ? OP_CISINST : OP_CCASTCLASS);
5292 ins->type = STACK_I4;
5293 ins->inst_left = *sp;
5294 ins->inst_newa_class = klass;
5296 *sp++ = emit_tree (cfg, bblock, ins);
5300 case CEE_MONO_SAVE_LMF:
5301 case CEE_MONO_RESTORE_LMF:
5302 #ifdef MONO_ARCH_HAVE_LMF_OPS
5303 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_SAVE_LMF) ? OP_SAVE_LMF : OP_RESTORE_LMF);
5304 MONO_ADD_INS (bblock, ins);
5305 cfg->need_lmf_area = TRUE;
5310 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
5319 /* somewhat similar to LDTOKEN */
5320 MonoInst *addr, *vtvar;
5321 CHECK_STACK_OVF (1);
5322 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
5324 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5325 addr->cil_code = ip;
5326 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
5328 ins->inst_left = addr;
5329 MONO_ADD_INS (bblock, ins);
5330 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5344 * The following transforms:
5345 * CEE_CEQ into OP_CEQ
5346 * CEE_CGT into OP_CGT
5347 * CEE_CGT_UN into OP_CGT_UN
5348 * CEE_CLT into OP_CLT
5349 * CEE_CLT_UN into OP_CLT_UN
5351 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
5353 MONO_INST_NEW (cfg, ins, cmp->opcode);
5355 cmp->inst_i0 = sp [0];
5356 cmp->inst_i1 = sp [1];
5360 cmp->opcode = OP_COMPARE;
5362 ins->type = STACK_I4;
5365 /* spill it to reduce the expression complexity
5366 * and workaround bug 54209
5368 if (cmp->inst_left->type == STACK_I8) {
5370 *sp++ = emit_tree (cfg, bblock, ins);
5379 CHECK_STACK_OVF (1);
5381 n = read32 (ip + 2);
5382 if (method->wrapper_type != MONO_WRAPPER_NONE)
5383 cmethod = mono_method_get_wrapper_data (method, n);
5385 cmethod = mono_get_method_full (image, n, NULL, generic_context);
5388 mono_class_init (cmethod->klass);
5389 handle_loaded_temps (cfg, bblock, stack_start, sp);
5391 NEW_METHODCONST (cfg, argconst, cmethod);
5392 if (method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED)
5393 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
5395 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn_nosync, &argconst, ip);
5396 NEW_TEMPLOAD (cfg, *sp, temp);
5400 inline_costs += 10 * num_calls++;
5403 case CEE_LDVIRTFTN: {
5409 n = read32 (ip + 2);
5410 if (method->wrapper_type != MONO_WRAPPER_NONE)
5411 cmethod = mono_method_get_wrapper_data (method, n);
5413 cmethod = mono_get_method_full (image, n, NULL, generic_context);
5415 mono_class_init (cmethod->klass);
5416 handle_loaded_temps (cfg, bblock, stack_start, sp);
5420 NEW_METHODCONST (cfg, args [1], cmethod);
5421 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
5422 NEW_TEMPLOAD (cfg, *sp, temp);
5426 inline_costs += 10 * num_calls++;
5430 CHECK_STACK_OVF (1);
5432 n = read16 (ip + 2);
5434 NEW_ARGLOAD (cfg, ins, n);
5440 CHECK_STACK_OVF (1);
5442 n = read16 (ip + 2);
5444 NEW_ARGLOADA (cfg, ins, n);
5452 handle_loaded_temps (cfg, bblock, stack_start, sp);
5454 n = read16 (ip + 2);
5456 NEW_ARGSTORE (cfg, ins, n, *sp);
5458 if (ins->opcode == CEE_STOBJ) {
5459 NEW_ARGLOADA (cfg, ins, n);
5460 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5462 MONO_ADD_INS (bblock, ins);
5466 CHECK_STACK_OVF (1);
5468 n = read16 (ip + 2);
5470 NEW_LOCLOAD (cfg, ins, n);
5476 CHECK_STACK_OVF (1);
5478 n = read16 (ip + 2);
5480 NEW_LOCLOADA (cfg, ins, n);
5489 n = read16 (ip + 2);
5491 handle_loaded_temps (cfg, bblock, stack_start, sp);
5492 NEW_LOCSTORE (cfg, ins, n, *sp);
5494 if (ins->opcode == CEE_STOBJ) {
5495 NEW_LOCLOADA (cfg, ins, n);
5496 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5498 MONO_ADD_INS (bblock, ins);
5505 if (sp != stack_start)
5507 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
5508 ins->inst_left = *sp;
5511 cfg->flags |= MONO_CFG_HAS_ALLOCA;
5512 if (header->init_locals)
5513 ins->flags |= MONO_INST_INIT;
5517 /* FIXME: set init flag if locals init is set in this method */
5519 case CEE_ENDFILTER: {
5520 MonoExceptionClause *clause, *nearest;
5521 int cc, nearest_num;
5525 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
5527 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
5528 ins->inst_left = *sp;
5530 MONO_ADD_INS (bblock, ins);
5531 start_new_bblock = 1;
5536 for (cc = 0; cc < header->num_clauses; ++cc) {
5537 clause = &header->clauses [cc];
5538 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
5539 (!nearest || (clause->token_or_filter > nearest->token_or_filter))) {
5545 filter_lengths [nearest_num] = (ip - header->code) - nearest->token_or_filter;
5549 case CEE_UNALIGNED_:
5550 ins_flag |= MONO_INST_UNALIGNED;
5551 /* FIXME: record alignment? we can assume 1 for now */
5556 ins_flag |= MONO_INST_VOLATILE;
5560 ins_flag |= MONO_INST_TAILCALL;
5561 /* Can't inline tail calls at this time */
5562 inline_costs += 100000;
5569 token = read32 (ip + 2);
5570 if (method->wrapper_type != MONO_WRAPPER_NONE)
5571 klass = mono_method_get_wrapper_data (method, token);
5573 klass = mono_class_get_full (image, token, generic_context);
5574 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5575 MonoInst *store, *load;
5576 NEW_PCONST (cfg, load, NULL);
5577 load->cil_code = ip;
5578 load->type = STACK_OBJ;
5579 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
5580 store->cil_code = ip;
5581 handle_loaded_temps (cfg, bblock, stack_start, sp);
5582 MONO_ADD_INS (bblock, store);
5583 store->inst_i0 = sp [0];
5584 store->inst_i1 = load;
5586 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
5591 case CEE_CONSTRAINED_:
5592 /* FIXME: implement */
5594 token = read32 (ip + 2);
5595 constrained_call = mono_class_get_full (image, token, generic_context);
5600 MonoInst *iargs [3];
5603 if ((cfg->opt & MONO_OPT_INTRINS) && (ip [1] == CEE_CPBLK) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5)) {
5605 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
5606 copy->inst_left = sp [0];
5607 copy->inst_right = sp [1];
5608 copy->cil_code = ip;
5610 MONO_ADD_INS (bblock, copy);
5617 handle_loaded_temps (cfg, bblock, stack_start, sp);
5618 if (ip [1] == CEE_CPBLK) {
5619 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
5621 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
5630 ins_flag |= MONO_INST_NOTYPECHECK;
5632 ins_flag |= MONO_INST_NORANGECHECK;
5633 /* we ignore the no-nullcheck for now since we
5634 * really do it explicitly only when doing callvirt->call
5640 /* FIXME: check we are in a catch handler */
5641 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
5642 load->cil_code = ip;
5643 MONO_INST_NEW (cfg, ins, CEE_THROW);
5644 ins->inst_left = load;
5646 MONO_ADD_INS (bblock, ins);
5648 start_new_bblock = 1;
5653 CHECK_STACK_OVF (1);
5655 token = read32 (ip + 2);
5656 /* FIXXME: handle generics. */
5657 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
5658 MonoType *type = mono_type_create_from_typespec (image, token);
5659 token = mono_type_size (type, &align);
5661 MonoClass *szclass = mono_class_get_full (image, token, generic_context);
5662 mono_class_init (szclass);
5663 token = mono_class_value_size (szclass, &align);
5665 NEW_ICONST (cfg, ins, token);
5670 case CEE_REFANYTYPE:
5672 MONO_INST_NEW (cfg, ins, OP_REFANYTYPE);
5674 ins->type = STACK_MP;
5675 ins->inst_left = *sp;
5676 ins->type = STACK_VTYPE;
5677 ins->klass = mono_defaults.typehandle_class;
5686 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
5691 g_error ("opcode 0x%02x not handled", *ip);
5694 if (start_new_bblock != 1)
5697 bblock->cil_length = ip - bblock->cil_code;
5698 bblock->next_bb = end_bblock;
5699 link_bblock (cfg, bblock, end_bblock);
5701 if (cfg->method == method && cfg->domainvar) {
5705 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
5706 call->signature = helper_sig_domain_get;
5707 call->inst.type = STACK_PTR;
5708 call->fptr = mono_domain_get;
5709 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, (MonoInst*)call);
5711 MONO_ADD_INS (init_localsbb, store);
5714 if (header->init_locals) {
5716 for (i = 0; i < header->num_locals; ++i) {
5717 int t = header->locals [i]->type;
5718 if (t == MONO_TYPE_VALUETYPE && header->locals [i]->data.klass->enumtype)
5719 t = header->locals [i]->data.klass->enum_basetype->type;
5720 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
5721 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
5722 NEW_ICONST (cfg, ins, 0);
5723 NEW_LOCSTORE (cfg, store, i, ins);
5724 MONO_ADD_INS (init_localsbb, store);
5725 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
5726 MONO_INST_NEW (cfg, ins, OP_I8CONST);
5727 ins->type = STACK_I8;
5729 NEW_LOCSTORE (cfg, store, i, ins);
5730 MONO_ADD_INS (init_localsbb, store);
5731 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
5732 MONO_INST_NEW (cfg, ins, OP_R8CONST);
5733 ins->type = STACK_R8;
5734 ins->inst_p0 = (void*)&r8_0;
5735 NEW_LOCSTORE (cfg, store, i, ins);
5736 MONO_ADD_INS (init_localsbb, store);
5737 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF)) {
5738 NEW_LOCLOADA (cfg, ins, i);
5739 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (header->locals [i]), NULL, NULL);
5742 NEW_PCONST (cfg, ins, NULL);
5743 NEW_LOCSTORE (cfg, store, i, ins);
5744 MONO_ADD_INS (init_localsbb, store);
5750 /* resolve backward branches in the middle of an existing basic block */
5751 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
5753 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
5754 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
5755 if (tblock != start_bblock) {
5757 split_bblock (cfg, tblock, bblock);
5758 l = bblock->cil_code - header->code;
5759 bblock->cil_length = tblock->cil_length - l;
5760 tblock->cil_length = l;
5762 g_print ("recheck failed.\n");
5767 * we compute regions here, because the length of filter clauses is not known in advance.
5768 * It is computed in the CEE_ENDFILTER case in the above switch statement
5770 if (cfg->method == method) {
5772 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5773 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
5775 mono_create_spvar_for_region (cfg, bb->region);
5776 if (cfg->verbose_level > 2)
5777 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
5780 g_hash_table_destroy (bbhash);
5783 dont_inline = g_list_remove (dont_inline, method);
5784 return inline_costs;
5787 if (cfg->method != method)
5788 g_hash_table_destroy (bbhash);
5789 dont_inline = g_list_remove (dont_inline, method);
5793 if (cfg->method != method)
5794 g_hash_table_destroy (bbhash);
5795 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
5796 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
5797 dont_inline = g_list_remove (dont_inline, method);
5802 mono_print_tree (MonoInst *tree) {
5808 arity = mono_burg_arity [tree->opcode];
5810 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
5812 switch (tree->opcode) {
5814 printf ("[%d]", tree->inst_c0);
5817 printf ("[%lld]", tree->inst_l);
5820 printf ("[%f]", *(double*)tree->inst_p0);
5823 printf ("[%f]", *(float*)tree->inst_p0);
5827 printf ("[%d]", tree->inst_c0);
5830 if (tree->inst_offset < 0)
5831 printf ("[-0x%x(%s)]", -tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5833 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5836 printf ("[%s]", mono_arch_regname (tree->dreg));
5839 printf ("[%s]", tree->inst_newa_class->name);
5840 mono_print_tree (tree->inst_newa_len);
5851 case OP_VOIDCALLVIRT: {
5852 MonoCallInst *call = (MonoCallInst*)tree;
5854 printf ("[%s]", call->method->name);
5859 printf ("[%d (", tree->inst_c0);
5860 for (i = 0; i < tree->inst_phi_args [0]; i++) {
5863 printf ("%d", tree->inst_phi_args [i + 1]);
5874 case OP_LOAD_MEMBASE:
5875 case OP_LOADI4_MEMBASE:
5876 case OP_LOADU4_MEMBASE:
5877 case OP_LOADU1_MEMBASE:
5878 case OP_LOADI1_MEMBASE:
5879 case OP_LOADU2_MEMBASE:
5880 case OP_LOADI2_MEMBASE:
5881 printf ("[%s] <- [%s + 0x%x]", mono_arch_regname (tree->dreg), mono_arch_regname (tree->inst_basereg), tree->inst_offset);
5884 printf ("[B%d]", tree->inst_target_bb->block_num);
5894 case OP_VOIDCALL_REG:
5895 mono_print_tree (tree->inst_left);
5907 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
5908 mono_print_tree (tree->inst_left);
5911 if (!mono_arch_print_tree(tree, arity)) {
5913 mono_print_tree (tree->inst_left);
5915 mono_print_tree (tree->inst_right);
5926 mono_print_tree_nl (MonoInst *tree)
5928 mono_print_tree (tree);
5933 type_from_typename (char *typename)
5937 if (!strcmp (typename, "int"))
5938 klass = mono_defaults.int_class;
5939 else if (!strcmp (typename, "ptr"))
5940 klass = mono_defaults.int_class;
5941 else if (!strcmp (typename, "void"))
5942 klass = mono_defaults.void_class;
5943 else if (!strcmp (typename, "int32"))
5944 klass = mono_defaults.int32_class;
5945 else if (!strcmp (typename, "uint32"))
5946 klass = mono_defaults.uint32_class;
5947 else if (!strcmp (typename, "long"))
5948 klass = mono_defaults.int64_class;
5949 else if (!strcmp (typename, "ulong"))
5950 klass = mono_defaults.uint64_class;
5951 else if (!strcmp (typename, "float"))
5952 klass = mono_defaults.single_class;
5953 else if (!strcmp (typename, "double"))
5954 klass = mono_defaults.double_class;
5955 else if (!strcmp (typename, "object"))
5956 klass = mono_defaults.object_class;
5959 g_assert_not_reached ();
5961 return &klass->byval_arg;
5964 static MonoMethodSignature*
5965 make_icall_sig (const char *sigstr)
5970 MonoMethodSignature *res;
5972 parts = g_strsplit (sigstr, " ", 256);
5981 res = mono_metadata_signature_alloc (mono_defaults.corlib, len - 1);
5984 #ifdef PLATFORM_WIN32
5986 * Under windows, the default pinvoke calling convention is STDCALL but
5989 res->call_convention = MONO_CALL_C;
5992 res->ret = type_from_typename (parts [0]);
5993 for (i = 1; i < len; ++i) {
5994 res->params [i - 1] = type_from_typename (parts [i]);
6003 create_helper_signature (void)
6006 /* FIXME: set call conv */
6007 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
6008 helper_sig_newarr = make_icall_sig ("object ptr ptr int32");
6010 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
6011 helper_sig_newarr_specific = make_icall_sig ("object ptr int32");
6013 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
6014 helper_sig_object_new = make_icall_sig ("object ptr ptr");
6016 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
6017 helper_sig_object_new_specific = make_icall_sig ("object ptr");
6019 /* void* mono_method_compile (MonoMethod*) */
6020 helper_sig_compile = make_icall_sig ("ptr ptr");
6022 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
6023 helper_sig_compile_virt = make_icall_sig ("ptr object ptr");
6025 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
6026 helper_sig_ldstr = make_icall_sig ("object ptr ptr int32");
6028 /* MonoDomain *mono_domain_get (void) */
6029 helper_sig_domain_get = make_icall_sig ("ptr");
6031 /* void stelem_ref (MonoArray *, int index, MonoObject *) */
6032 helper_sig_stelem_ref = make_icall_sig ("void ptr int32 object");
6034 /* void stelem_ref_check (MonoArray *, MonoObject *) */
6035 helper_sig_stelem_ref_check = make_icall_sig ("void object object");
6037 /* long amethod (long, long) */
6038 helper_sig_long_long_long = make_icall_sig ("long long long");
6040 /* object amethod (intptr) */
6041 helper_sig_obj_ptr = make_icall_sig ("object ptr");
6043 helper_sig_obj_ptr_ptr = make_icall_sig ("object ptr ptr");
6045 /* void amethod (intptr) */
6046 helper_sig_void_ptr = make_icall_sig ("void ptr");
6048 /* void amethod (MonoObject *obj) */
6049 helper_sig_void_obj = make_icall_sig ("void object");
6051 /* void amethod (MonoObject *obj, void *ptr, int i) */
6052 helper_sig_void_obj_ptr_int = make_icall_sig ("void object ptr int");
6054 /* intptr amethod (void) */
6055 helper_sig_ptr_void = make_icall_sig ("ptr");
6057 /* object amethod (void) */
6058 helper_sig_obj_void = make_icall_sig ("object");
6060 /* void amethod (intptr, intptr) */
6061 helper_sig_void_ptr_ptr = make_icall_sig ("void ptr ptr");
6063 /* void amethod (intptr, intptr, intptr) */
6064 helper_sig_void_ptr_ptr_ptr = make_icall_sig ("void ptr ptr ptr");
6066 /* intptr amethod (intptr, intptr) */
6067 helper_sig_ptr_ptr_ptr = make_icall_sig ("ptr ptr ptr");
6069 /* IntPtr amethod (object) */
6070 helper_sig_ptr_obj = make_icall_sig ("ptr object");
6072 /* IntPtr amethod (object, int) */
6073 helper_sig_ptr_obj_int = make_icall_sig ("ptr object int");
6075 /* IntPtr amethod (int) */
6076 helper_sig_ptr_int = make_icall_sig ("ptr int32");
6078 /* long amethod (long, guint32) */
6079 helper_sig_long_long_int = make_icall_sig ("long long int32");
6081 /* ulong amethod (double) */
6082 helper_sig_ulong_double = make_icall_sig ("ulong double");
6084 /* long amethod (double) */
6085 helper_sig_long_double = make_icall_sig ("long double");
6087 /* double amethod (long) */
6088 helper_sig_double_long = make_icall_sig ("double long");
6090 /* double amethod (int) */
6091 helper_sig_double_int = make_icall_sig ("double int32");
6093 /* float amethod (long) */
6094 helper_sig_float_long = make_icall_sig ("float long");
6096 /* double amethod (double, double) */
6097 helper_sig_double_double_double = make_icall_sig ("double double double");
6099 /* uint amethod (double) */
6100 helper_sig_uint_double = make_icall_sig ("uint32 double");
6102 /* int amethod (double) */
6103 helper_sig_int_double = make_icall_sig ("int32 double");
6105 /* void initobj (intptr, int size) */
6106 helper_sig_initobj = make_icall_sig ("void ptr int32");
6108 /* void memcpy (intptr, intptr, int size) */
6109 helper_sig_memcpy = make_icall_sig ("void ptr ptr int32");
6111 /* void memset (intptr, int val, int size) */
6112 helper_sig_memset = make_icall_sig ("void ptr int32 int32");
6114 helper_sig_class_init_trampoline = make_icall_sig ("void");
6117 static GHashTable *jit_icall_hash_name = NULL;
6118 static GHashTable *jit_icall_hash_addr = NULL;
6121 mono_find_jit_icall_by_name (const char *name)
6123 MonoJitICallInfo *info;
6124 g_assert (jit_icall_hash_name);
6126 //printf ("lookup addr %s %p\n", name, g_hash_table_lookup (jit_icall_hash_name, name));
6127 EnterCriticalSection (&trampoline_hash_mutex);
6128 info = g_hash_table_lookup (jit_icall_hash_name, name);
6129 LeaveCriticalSection (&trampoline_hash_mutex);
6134 mono_find_jit_icall_by_addr (gconstpointer addr)
6136 MonoJitICallInfo *info;
6137 g_assert (jit_icall_hash_addr);
6139 EnterCriticalSection (&trampoline_hash_mutex);
6140 info = g_hash_table_lookup (jit_icall_hash_addr, (gpointer)addr);
6141 LeaveCriticalSection (&trampoline_hash_mutex);
6147 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
6150 MonoMethod *wrapper;
6153 if (callinfo->wrapper)
6154 return callinfo->wrapper;
6156 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
6157 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
6158 /* Must be domain neutral since there is only one copy */
6159 code = mono_jit_compile_method_with_opt (wrapper, default_opt | MONO_OPT_SHARED);
6161 EnterCriticalSection (&trampoline_hash_mutex);
6162 if (!callinfo->wrapper) {
6163 callinfo->wrapper = code;
6164 g_hash_table_insert (jit_icall_hash_addr, (gpointer)callinfo->wrapper, callinfo);
6166 LeaveCriticalSection (&trampoline_hash_mutex);
6169 return callinfo->wrapper;
6173 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save)
6175 MonoJitICallInfo *info;
6180 EnterCriticalSection (&trampoline_hash_mutex);
6182 if (!jit_icall_hash_name) {
6183 jit_icall_hash_name = g_hash_table_new (g_str_hash, g_str_equal);
6184 jit_icall_hash_addr = g_hash_table_new (NULL, NULL);
6187 if (g_hash_table_lookup (jit_icall_hash_name, name)) {
6188 g_warning ("jit icall already defined \"%s\"\n", name);
6189 g_assert_not_reached ();
6192 info = g_new (MonoJitICallInfo, 1);
6199 info->wrapper = func;
6201 info->wrapper = NULL;
6204 g_hash_table_insert (jit_icall_hash_name, (gpointer)info->name, info);
6205 g_hash_table_insert (jit_icall_hash_addr, (gpointer)func, info);
6207 LeaveCriticalSection (&trampoline_hash_mutex);
6212 mono_create_class_init_trampoline (MonoVTable *vtable)
6216 /* previously created trampoline code */
6217 mono_domain_lock (vtable->domain);
6219 mono_g_hash_table_lookup (vtable->domain->class_init_trampoline_hash,
6221 mono_domain_unlock (vtable->domain);
6225 code = mono_arch_create_class_init_trampoline (vtable);
6227 /* store trampoline address */
6228 mono_domain_lock (vtable->domain);
6229 mono_g_hash_table_insert (vtable->domain->class_init_trampoline_hash,
6231 mono_domain_unlock (vtable->domain);
6233 EnterCriticalSection (&trampoline_hash_mutex);
6234 if (!class_init_hash_addr)
6235 class_init_hash_addr = g_hash_table_new (NULL, NULL);
6236 g_hash_table_insert (class_init_hash_addr, code, vtable);
6237 LeaveCriticalSection (&trampoline_hash_mutex);
6243 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method,
6244 gboolean add_sync_wrapper)
6249 if (add_sync_wrapper && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
6250 return mono_create_jump_trampoline (domain, mono_marshal_get_synchronized_wrapper (method), FALSE);
6252 code = mono_jit_find_compiled_method (domain, method);
6256 EnterCriticalSection (&trampoline_hash_mutex);
6258 if (jump_trampoline_hash) {
6259 code = g_hash_table_lookup (jump_trampoline_hash, method);
6261 LeaveCriticalSection (&trampoline_hash_mutex);
6266 ji = mono_arch_create_jump_trampoline (method);
6269 * mono_delegate_ctor needs to find the method metadata from the
6270 * trampoline address, so we save it here.
6273 mono_jit_info_table_add (mono_root_domain, ji);
6275 if (!jump_trampoline_hash)
6276 jump_trampoline_hash = g_hash_table_new (NULL, NULL);
6277 g_hash_table_insert (jump_trampoline_hash, method, ji->code_start);
6279 LeaveCriticalSection (&trampoline_hash_mutex);
6281 return ji->code_start;
6285 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
6289 EnterCriticalSection (&trampoline_hash_mutex);
6290 if (class_init_hash_addr)
6291 res = g_hash_table_lookup (class_init_hash_addr, addr);
6294 LeaveCriticalSection (&trampoline_hash_mutex);
6299 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func, gboolean no_throw)
6301 MonoJitICallInfo *info;
6303 if (!emul_opcode_map)
6304 emul_opcode_map = g_new0 (MonoJitICallInfo*, OP_LAST + 1);
6306 g_assert (!sig->hasthis);
6307 g_assert (sig->param_count < 3);
6309 info = mono_register_jit_icall (func, name, sig, no_throw);
6311 emul_opcode_map [opcode] = info;
6315 decompose_foreach (MonoInst *tree, gpointer data)
6317 static MonoJitICallInfo *newarr_info = NULL;
6318 static MonoJitICallInfo *newarr_specific_info = NULL;
6319 MonoJitICallInfo *info;
6322 switch (tree->opcode) {
6324 MonoCompile *cfg = data;
6325 MonoInst *iargs [3];
6328 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
6329 g_assert (newarr_info);
6330 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
6331 g_assert (newarr_specific_info);
6334 if (cfg->opt & MONO_OPT_SHARED) {
6335 NEW_DOMAINCONST (cfg, iargs [0]);
6336 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
6337 iargs [2] = tree->inst_newa_len;
6342 MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
6344 NEW_VTABLECONST (cfg, iargs [0], vtable);
6345 iargs [1] = tree->inst_newa_len;
6347 info = newarr_specific_info;
6350 mono_emulate_opcode (cfg, tree, iargs, info);
6352 /* Need to decompose arguments after the the opcode is decomposed */
6353 for (i = 0; i < info->sig->param_count; ++i)
6354 dec_foreach (iargs [i], cfg);
6364 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
6366 switch (mono_burg_arity [tree->opcode]) {
6369 mono_inst_foreach (tree->inst_left, func, data);
6372 mono_inst_foreach (tree->inst_left, func, data);
6373 mono_inst_foreach (tree->inst_right, func, data);
6376 g_assert_not_reached ();
6383 mono_print_bb_code (MonoBasicBlock *bb) {
6385 MonoInst *c = bb->code;
6387 mono_print_tree (c);
6395 print_dfn (MonoCompile *cfg) {
6400 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
6402 for (i = 0; i < cfg->num_bblocks; ++i) {
6403 bb = cfg->bblocks [i];
6405 char* code1, *code2;
6406 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
6407 if (bb->last_ins->cil_code)
6408 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
6410 code2 = g_strdup ("");
6412 code1 [strlen (code1) - 1] = 0;
6413 code = g_strdup_printf ("%s -> %s", code1, code2);
6417 code = g_strdup ("\n");
6418 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
6420 MonoInst *c = bb->code;
6422 mono_print_tree (c);
6430 g_print ("\tprev:");
6431 for (j = 0; j < bb->in_count; ++j) {
6432 g_print (" BB%d", bb->in_bb [j]->block_num);
6434 g_print ("\t\tsucc:");
6435 for (j = 0; j < bb->out_count; ++j) {
6436 g_print (" BB%d", bb->out_bb [j]->block_num);
6438 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
6441 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
6444 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
6446 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
6454 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
6458 g_assert (bb->code);
6459 bb->last_ins->next = inst;
6460 bb->last_ins = inst;
6462 bb->last_ins = bb->code = inst;
6467 mono_destroy_compile (MonoCompile *cfg)
6469 //mono_mempool_stats (cfg->mempool);
6470 g_hash_table_destroy (cfg->bb_hash);
6471 mono_free_loop_info (cfg);
6473 mono_regstate_free (cfg->rs);
6475 g_hash_table_destroy (cfg->spvars);
6476 mono_mempool_destroy (cfg->mempool);
6477 g_list_free (cfg->ldstr_list);
6479 g_free (cfg->varinfo);
6484 #ifdef HAVE_KW_THREAD
6485 static __thread gpointer mono_lmf_addr;
6489 mono_get_lmf_addr (void)
6491 #ifdef HAVE_KW_THREAD
6492 return mono_lmf_addr;
6494 MonoJitTlsData *jit_tls;
6496 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
6497 return &jit_tls->lmf;
6499 g_assert_not_reached ();
6505 * mono_thread_abort:
6506 * @obj: exception object
6508 * abort the thread, print exception information and stack trace
6511 mono_thread_abort (MonoObject *obj)
6513 /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
6515 /* handle_remove should be eventually called for this thread, too
6522 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
6524 MonoJitTlsData *jit_tls;
6527 jit_tls = TlsGetValue (mono_jit_tls_id);
6531 jit_tls = g_new0 (MonoJitTlsData, 1);
6533 TlsSetValue (mono_jit_tls_id, jit_tls);
6535 jit_tls->abort_func = abort_func;
6536 jit_tls->end_of_stack = stack_start;
6538 lmf = g_new0 (MonoLMF, 1);
6541 jit_tls->lmf = jit_tls->first_lmf = lmf;
6543 #ifdef HAVE_KW_THREAD
6544 mono_lmf_addr = &jit_tls->lmf;
6547 mono_arch_setup_jit_tls_data (jit_tls);
6553 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
6556 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
6557 thread = mono_thread_current ();
6559 thread->jit_data = jit_tls;
6562 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
6565 mono_thread_abort_dummy (MonoObject *obj)
6567 if (mono_thread_attach_aborted_cb)
6568 mono_thread_attach_aborted_cb (obj);
6570 mono_thread_abort (obj);
6574 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
6577 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
6578 thread = mono_thread_current ();
6580 thread->jit_data = jit_tls;
6584 mini_thread_cleanup (MonoThread *thread)
6586 MonoJitTlsData *jit_tls = thread->jit_data;
6589 mono_arch_free_jit_tls_data (jit_tls);
6590 g_free (jit_tls->first_lmf);
6592 thread->jit_data = NULL;
6597 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
6599 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
6603 ji->data.target = target;
6604 ji->next = cfg->patch_info;
6606 cfg->patch_info = ji;
6610 mono_remove_patch_info (MonoCompile *cfg, int ip)
6612 MonoJumpInfo **ji = &cfg->patch_info;
6615 if ((*ji)->ip.i == ip)
6618 ji = &((*ji)->next);
6623 mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors)
6625 unsigned char *ip = patch_info->ip.i + code;
6626 gconstpointer target = NULL;
6628 switch (patch_info->type) {
6629 case MONO_PATCH_INFO_BB:
6630 target = patch_info->data.bb->native_offset + code;
6632 case MONO_PATCH_INFO_ABS:
6633 target = patch_info->data.target;
6635 case MONO_PATCH_INFO_LABEL:
6636 target = patch_info->data.inst->inst_c0 + code;
6638 case MONO_PATCH_INFO_IP:
6641 case MONO_PATCH_INFO_METHOD_REL:
6642 target = code + patch_info->data.offset;
6644 case MONO_PATCH_INFO_INTERNAL_METHOD: {
6645 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
6647 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
6648 g_assert_not_reached ();
6650 target = mono_icall_get_wrapper (mi);
6653 case MONO_PATCH_INFO_METHOD_JUMP: {
6656 /* get the trampoline to the method from the domain */
6657 target = mono_create_jump_trampoline (domain, patch_info->data.method, TRUE);
6658 if (!domain->jump_target_hash)
6659 domain->jump_target_hash = g_hash_table_new (NULL, NULL);
6660 list = g_hash_table_lookup (domain->jump_target_hash, patch_info->data.method);
6661 list = g_slist_prepend (list, ip);
6662 g_hash_table_insert (domain->jump_target_hash, patch_info->data.method, list);
6665 case MONO_PATCH_INFO_METHOD:
6666 if (patch_info->data.method == method) {
6669 /* get the trampoline to the method from the domain */
6670 target = mono_arch_create_jit_trampoline (patch_info->data.method);
6672 case MONO_PATCH_INFO_SWITCH: {
6673 gpointer *jump_table;
6676 mono_domain_lock (domain);
6677 jump_table = mono_code_manager_reserve (domain->code_mp, sizeof (gpointer) * patch_info->table_size);
6678 mono_domain_unlock (domain);
6680 for (i = 0; i < patch_info->table_size; i++) {
6681 jump_table [i] = code + (int)patch_info->data.table [i];
6683 target = jump_table;
6686 case MONO_PATCH_INFO_METHODCONST:
6687 case MONO_PATCH_INFO_CLASS:
6688 case MONO_PATCH_INFO_IMAGE:
6689 case MONO_PATCH_INFO_FIELD:
6690 target = patch_info->data.target;
6692 case MONO_PATCH_INFO_IID:
6693 mono_class_init (patch_info->data.klass);
6694 target = (gpointer)patch_info->data.klass->interface_id;
6696 case MONO_PATCH_INFO_VTABLE:
6697 target = mono_class_vtable (domain, patch_info->data.klass);
6699 case MONO_PATCH_INFO_CLASS_INIT:
6700 target = mono_create_class_init_trampoline (mono_class_vtable (domain, patch_info->data.klass));
6702 case MONO_PATCH_INFO_SFLDA: {
6703 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
6704 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (vtable->klass, method))
6705 /* Done by the generated code */
6709 mono_runtime_class_init (vtable);
6711 target = (char*)vtable->data + patch_info->data.field->offset;
6714 case MONO_PATCH_INFO_R4:
6715 case MONO_PATCH_INFO_R8:
6716 target = patch_info->data.target;
6718 case MONO_PATCH_INFO_EXC_NAME:
6719 target = patch_info->data.name;
6721 case MONO_PATCH_INFO_LDSTR:
6723 mono_ldstr (domain, patch_info->data.token->image,
6724 mono_metadata_token_index (patch_info->data.token->token));
6726 case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
6728 MonoClass *handle_class;
6730 handle = mono_ldtoken (patch_info->data.token->image,
6731 patch_info->data.token->token, &handle_class, NULL);
6732 mono_class_init (handle_class);
6733 mono_class_init (mono_class_from_mono_type (handle));
6736 mono_type_get_object (domain, handle);
6739 case MONO_PATCH_INFO_LDTOKEN: {
6741 MonoClass *handle_class;
6743 handle = mono_ldtoken (patch_info->data.token->image,
6744 patch_info->data.token->token, &handle_class, NULL);
6745 mono_class_init (handle_class);
6750 case MONO_PATCH_INFO_BB_OVF:
6751 case MONO_PATCH_INFO_EXC_OVF:
6754 g_assert_not_reached ();
6757 return (gpointer)target;
6761 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
6762 MonoJitICallInfo *info;
6764 decompose_foreach (tree, cfg);
6766 switch (mono_burg_arity [tree->opcode]) {
6769 dec_foreach (tree->inst_left, cfg);
6771 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6772 MonoInst *iargs [2];
6774 iargs [0] = tree->inst_left;
6776 mono_emulate_opcode (cfg, tree, iargs, info);
6782 #ifdef MONO_ARCH_BIGMUL_INTRINS
6783 if (tree->opcode == OP_LMUL
6784 && (cfg->opt & MONO_OPT_INTRINS)
6785 && (tree->inst_left->opcode == CEE_CONV_I8
6786 || tree->inst_left->opcode == CEE_CONV_U8)
6787 && tree->inst_left->inst_left->type == STACK_I4
6788 && (tree->inst_right->opcode == CEE_CONV_I8
6789 || tree->inst_right->opcode == CEE_CONV_U8)
6790 && tree->inst_right->inst_left->type == STACK_I4) {
6791 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
6792 tree->inst_left = tree->inst_left->inst_left;
6793 tree->inst_right = tree->inst_right->inst_left;
6794 dec_foreach (tree, cfg);
6797 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6798 MonoInst *iargs [2];
6800 iargs [0] = tree->inst_i0;
6801 iargs [1] = tree->inst_i1;
6803 mono_emulate_opcode (cfg, tree, iargs, info);
6805 dec_foreach (iargs [0], cfg);
6806 dec_foreach (iargs [1], cfg);
6809 dec_foreach (tree->inst_left, cfg);
6810 dec_foreach (tree->inst_right, cfg);
6814 g_assert_not_reached ();
6819 decompose_pass (MonoCompile *cfg) {
6822 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6825 cfg->prev_ins = NULL;
6826 for (tree = cfg->cbb->code; tree; tree = tree->next) {
6827 dec_foreach (tree, cfg);
6828 cfg->prev_ins = tree;
6834 nullify_basic_block (MonoBasicBlock *bb)
6841 bb->code = bb->last_ins = NULL;
6842 bb->cil_code = NULL;
6846 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6850 for (i = 0; i < bb->out_count; i++) {
6851 MonoBasicBlock *ob = bb->out_bb [i];
6854 if (bb->out_count > 1) {
6855 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
6859 bb->out_bb [i] = repl;
6866 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6870 for (i = 0; i < bb->in_count; i++) {
6871 MonoBasicBlock *ib = bb->in_bb [i];
6874 if (bb->in_count > 1) {
6875 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
6879 bb->in_bb [i] = repl;
6886 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6890 for (i = 0; i < bb->out_count; i++) {
6891 MonoBasicBlock *ob = bb->out_bb [i];
6892 for (j = 0; j < ob->in_count; j++) {
6893 if (ob->in_bb [j] == orig) {
6894 ob->in_bb [j] = repl;
6903 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
6905 bb->out_count = bbn->out_count;
6906 bb->out_bb = bbn->out_bb;
6908 replace_basic_block (bb, bbn, bb);
6912 bb->last_ins->next = bbn->code;
6913 bb->last_ins = bbn->last_ins;
6916 bb->code = bbn->code;
6917 bb->last_ins = bbn->last_ins;
6919 bb->next_bb = bbn->next_bb;
6920 nullify_basic_block (bbn);
6924 * Optimizes the branches on the Control Flow Graph
6928 optimize_branches (MonoCompile *cfg)
6930 int i, changed = FALSE;
6931 MonoBasicBlock *bb, *bbn;
6932 guint32 niterations;
6935 * Some crazy loops could cause the code below to go into an infinite
6936 * loop, see bug #53003 for an example. To prevent this, we put an upper
6937 * bound on the number of iterations.
6944 /* we skip the entry block (exit is handled specially instead ) */
6945 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
6947 /* dont touch code inside exception clauses */
6948 if (bb->region != -1)
6951 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
6952 if (cfg->verbose_level > 2)
6953 g_print ("nullify block triggered %d\n", bbn->block_num);
6955 bb->next_bb = bbn->next_bb;
6957 for (i = 0; i < bbn->out_count; i++)
6958 replace_in_block (bbn->out_bb [i], bbn, NULL);
6960 nullify_basic_block (bbn);
6964 if (bb->out_count == 1) {
6965 bbn = bb->out_bb [0];
6967 /* conditional branches where true and false targets are the same can be also replaced with CEE_BR */
6968 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins)) {
6969 bb->last_ins->opcode = CEE_BR;
6970 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
6972 if (cfg->verbose_level > 2)
6973 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
6976 if (bb->region == bbn->region && bb->next_bb == bbn) {
6977 /* the block are in sequence anyway ... */
6979 /* branches to the following block can be removed */
6980 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
6981 bb->last_ins->opcode = CEE_NOP;
6983 if (cfg->verbose_level > 2)
6984 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
6987 if (bbn->in_count == 1) {
6989 if (bbn != cfg->bb_exit) {
6990 if (cfg->verbose_level > 2)
6991 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
6992 merge_basic_blocks (bb, bbn);
6996 //mono_print_bb_code (bb);
7001 } while (changed && (niterations > 0));
7008 /* we skip the entry block (exit is handled specially instead ) */
7009 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
7011 /* dont touch code inside exception clauses */
7012 if (bb->region != -1)
7015 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
7016 if (cfg->verbose_level > 2) {
7017 g_print ("nullify block triggered %d\n", bbn->block_num);
7019 bb->next_bb = bbn->next_bb;
7021 for (i = 0; i < bbn->out_count; i++)
7022 replace_in_block (bbn->out_bb [i], bbn, NULL);
7024 nullify_basic_block (bbn);
7030 if (bb->out_count == 1) {
7031 bbn = bb->out_bb [0];
7033 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
7034 bbn = bb->last_ins->inst_target_bb;
7035 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7036 bbn->code->inst_target_bb->region == bb->region) {
7038 if (cfg->verbose_level > 2)
7039 g_print ("in %s branch to branch triggered %d -> %d\n", cfg->method->name,
7040 bb->block_num, bbn->block_num);
7042 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
7043 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
7048 } else if (bb->out_count == 2) {
7049 if (bb->last_ins && MONO_IS_COND_BRANCH_NOFP (bb->last_ins)) {
7050 bbn = bb->last_ins->inst_true_bb;
7051 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7052 bbn->code->inst_target_bb->region == bb->region) {
7053 if (cfg->verbose_level > 2)
7054 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
7055 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
7058 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
7060 replace_in_block (bbn, bb, NULL);
7062 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
7063 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7065 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7071 bbn = bb->last_ins->inst_false_bb;
7072 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7073 bbn->code->inst_target_bb->region == bb->region) {
7074 if (cfg->verbose_level > 2)
7075 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
7076 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
7079 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
7081 replace_in_block (bbn, bb, NULL);
7083 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
7084 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7086 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7094 } while (changed && (niterations > 0));
7099 mono_compile_create_vars (MonoCompile *cfg)
7101 MonoMethodSignature *sig;
7102 MonoMethodHeader *header;
7105 header = ((MonoMethodNormal *)cfg->method)->header;
7107 sig = cfg->method->signature;
7109 if (!MONO_TYPE_IS_VOID (sig->ret)) {
7110 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
7111 cfg->ret->opcode = OP_RETARG;
7112 cfg->ret->inst_vtype = sig->ret;
7113 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
7115 if (cfg->verbose_level > 2)
7116 g_print ("creating vars\n");
7119 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
7121 for (i = 0; i < sig->param_count; ++i)
7122 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
7124 cfg->locals_start = cfg->num_varinfo;
7126 if (cfg->verbose_level > 2)
7127 g_print ("creating locals\n");
7128 for (i = 0; i < header->num_locals; ++i)
7129 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
7130 if (cfg->verbose_level > 2)
7131 g_print ("locals done\n");
7135 mono_print_code (MonoCompile *cfg)
7139 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7140 MonoInst *tree = bb->code;
7145 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
7147 for (; tree; tree = tree->next) {
7148 mono_print_tree (tree);
7153 bb->last_ins->next = NULL;
7157 extern const char * const mono_burg_rule_string [];
7160 emit_state (MonoCompile *cfg, MBState *state, int goal)
7163 int ern = mono_burg_rule (state, goal);
7164 const guint16 *nts = mono_burg_nts [ern];
7167 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
7171 // state->reg1 = state->reg2; /* chain rule */
7173 state->reg1 = mono_regstate_next_int (cfg->rs);
7174 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
7177 state->reg1 = mono_regstate_next_int (cfg->rs);
7178 state->reg2 = mono_regstate_next_int (cfg->rs);
7181 state->reg1 = mono_regstate_next_float (cfg->rs);
7188 mono_burg_kids (state, ern, kids);
7190 emit_state (cfg, kids [0], nts [0]);
7192 emit_state (cfg, kids [1], nts [1]);
7194 g_assert (!nts [3]);
7195 emit_state (cfg, kids [2], nts [2]);
7200 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
7201 if ((emit = mono_burg_func [ern]))
7202 emit (state, state->tree, cfg);
7205 #define DEBUG_SELECTION
7208 mini_select_instructions (MonoCompile *cfg)
7210 static const int reverse_map [] = {
7211 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
7212 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
7214 static const int reverse_fmap [] = {
7215 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
7216 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
7218 static const int reverse_lmap [] = {
7219 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
7220 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
7225 cfg->state_pool = mono_mempool_new ();
7226 cfg->rs = mono_regstate_new ();
7228 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7229 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins) &&
7230 bb->next_bb != bb->last_ins->inst_false_bb) {
7232 /* we are careful when inverting, since bugs like #59580
7233 * could show up when dealing with NaNs.
7235 if (MONO_IS_COND_BRANCH_NOFP(bb->last_ins) && bb->next_bb == bb->last_ins->inst_true_bb) {
7236 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
7237 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
7238 bb->last_ins->inst_false_bb = tmp;
7240 if (bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
7241 bb->last_ins->opcode = reverse_map [bb->last_ins->opcode - CEE_BEQ];
7242 } else if (bb->last_ins->opcode >= OP_FBEQ && bb->last_ins->opcode <= OP_FBLT_UN) {
7243 bb->last_ins->opcode = reverse_fmap [bb->last_ins->opcode - OP_FBEQ];
7244 } else if (bb->last_ins->opcode >= OP_LBEQ && bb->last_ins->opcode <= OP_LBLT_UN) {
7245 bb->last_ins->opcode = reverse_lmap [bb->last_ins->opcode - OP_LBEQ];
7248 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
7249 inst->opcode = CEE_BR;
7250 inst->inst_target_bb = bb->last_ins->inst_false_bb;
7251 mono_bblock_add_inst (bb, inst);
7256 #ifdef DEBUG_SELECTION
7257 if (cfg->verbose_level >= 4) {
7258 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7259 MonoInst *tree = bb->code;
7260 g_print ("DUMP BLOCK %d:\n", bb->block_num);
7263 for (; tree; tree = tree->next) {
7264 mono_print_tree (tree);
7271 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7272 MonoInst *tree = bb->code, *next;
7278 bb->last_ins = NULL;
7281 mono_regstate_reset (cfg->rs);
7283 #ifdef DEBUG_SELECTION
7284 if (cfg->verbose_level >= 3)
7285 g_print ("LABEL BLOCK %d:\n", bb->block_num);
7287 for (; tree; tree = next) {
7289 #ifdef DEBUG_SELECTION
7290 if (cfg->verbose_level >= 3) {
7291 mono_print_tree (tree);
7296 if (!(mbstate = mono_burg_label (tree, cfg))) {
7297 g_warning ("unable to label tree %p", tree);
7298 mono_print_tree (tree);
7300 g_assert_not_reached ();
7302 emit_state (cfg, mbstate, MB_NTERM_stmt);
7304 bb->max_ireg = cfg->rs->next_vireg;
7305 bb->max_freg = cfg->rs->next_vfreg;
7308 bb->last_ins->next = NULL;
7310 mono_mempool_empty (cfg->state_pool);
7312 mono_mempool_destroy (cfg->state_pool);
7316 mono_codegen (MonoCompile *cfg)
7318 MonoJumpInfo *patch_info;
7320 int i, max_epilog_size;
7323 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7324 cfg->spill_count = 0;
7325 /* we reuse dfn here */
7326 /* bb->dfn = bb_count++; */
7327 mono_arch_local_regalloc (cfg, bb);
7330 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
7331 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
7333 code = mono_arch_emit_prolog (cfg);
7335 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7336 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
7338 cfg->code_len = code - cfg->native_code;
7339 cfg->prolog_end = cfg->code_len;
7341 mono_debug_open_method (cfg);
7343 /* emit code all basic blocks */
7344 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7345 bb->native_offset = cfg->code_len;
7346 mono_arch_output_basic_block (cfg, bb);
7348 cfg->bb_exit->native_offset = cfg->code_len;
7350 code = cfg->native_code + cfg->code_len;
7352 max_epilog_size = mono_arch_max_epilog_size (cfg);
7354 /* we always allocate code in cfg->domain->code_mp to increase locality */
7355 cfg->code_size = cfg->code_len + max_epilog_size;
7356 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
7358 mono_domain_lock (cfg->domain);
7359 code = mono_code_manager_reserve (cfg->domain->code_mp, cfg->code_size);
7360 mono_domain_unlock (cfg->domain);
7362 memcpy (code, cfg->native_code, cfg->code_len);
7363 g_free (cfg->native_code);
7364 cfg->native_code = code;
7365 code = cfg->native_code + cfg->code_len;
7367 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
7369 cfg->epilog_begin = cfg->code_len;
7371 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7372 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
7374 cfg->code_len = code - cfg->native_code;
7376 mono_arch_emit_epilog (cfg);
7378 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7379 switch (patch_info->type) {
7380 case MONO_PATCH_INFO_ABS: {
7381 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
7383 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
7384 if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) &&
7385 strstr (cfg->method->name, info->name))
7387 * This is an icall wrapper, and this is a call to the
7392 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
7393 patch_info->data.name = info->name;
7397 MonoVTable *vtable = mono_find_class_init_trampoline_by_addr (patch_info->data.target);
7399 patch_info->type = MONO_PATCH_INFO_CLASS_INIT;
7400 patch_info->data.klass = vtable->klass;
7405 case MONO_PATCH_INFO_SWITCH: {
7407 mono_domain_lock (cfg->domain);
7408 table = mono_code_manager_reserve (cfg->domain->code_mp, sizeof (gpointer) * patch_info->table_size);
7409 mono_domain_unlock (cfg->domain);
7411 patch_info->ip.i = patch_info->ip.label->inst_c0;
7412 for (i = 0; i < patch_info->table_size; i++) {
7413 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
7415 patch_info->data.target = table;
7424 if (cfg->verbose_level > 0)
7425 g_print ("Method %s emitted at %p to %p [%s]\n",
7426 mono_method_full_name (cfg->method, TRUE),
7427 cfg->native_code, cfg->native_code + cfg->code_len, cfg->domain->friendly_name);
7429 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info, cfg->run_cctors);
7431 mono_domain_lock (cfg->domain);
7432 mono_code_manager_commit (cfg->domain->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
7433 mono_domain_unlock (cfg->domain);
7435 mono_arch_flush_icache (cfg->native_code, cfg->code_len);
7437 mono_debug_close_method (cfg);
7441 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
7446 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
7447 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
7449 if (cp->opcode == OP_ICONST) {
7450 if (cfg->opt & MONO_OPT_CONSPROP) {
7451 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
7455 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
7456 if (cfg->opt & MONO_OPT_COPYPROP) {
7457 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
7463 arity = mono_burg_arity [tree->opcode];
7466 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
7467 if (cfg->opt & MONO_OPT_CFOLD)
7468 mono_constant_fold_inst (tree, NULL);
7469 /* The opcode may have changed */
7470 if (mono_burg_arity [tree->opcode] > 1) {
7471 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
7472 if (cfg->opt & MONO_OPT_CFOLD)
7473 mono_constant_fold_inst (tree, NULL);
7475 mono_constant_fold_inst (tree, NULL);
7481 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
7485 switch (tree->opcode) {
7495 if (tree->ssa_op == MONO_SSA_NOP) {
7496 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7513 case OP_VOIDCALL_REG:
7514 case OP_VOIDCALLVIRT:
7516 MonoCallInst *call = (MonoCallInst *)tree;
7517 MonoMethodSignature *sig = call->signature;
7518 int i, byref = FALSE;
7520 for (i = 0; i < sig->param_count; i++) {
7521 if (sig->params [i]->byref) {
7528 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7536 arity = mono_burg_arity [tree->opcode];
7542 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7545 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7546 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
7549 g_assert_not_reached ();
7554 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
7556 MonoInst *tree = bb->code;
7562 for (; tree; tree = tree->next) {
7564 mono_cprop_copy_values (cfg, tree, acp);
7566 mono_cprop_invalidate_values (tree, acp, acp_size);
7568 if (tree->ssa_op == MONO_SSA_STORE &&
7569 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
7570 MonoInst *i1 = tree->inst_i1;
7572 acp [tree->inst_i0->inst_c0] = NULL;
7574 for (i = 0; i < acp_size; i++) {
7575 if (acp [i] && acp [i]->opcode != OP_ICONST &&
7576 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
7581 if (i1->opcode == OP_ICONST) {
7582 acp [tree->inst_i0->inst_c0] = i1;
7583 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
7585 if (i1->ssa_op == MONO_SSA_LOAD &&
7586 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
7587 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
7588 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
7589 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
7594 if (tree->opcode == CEE_BEQ) {
7595 g_assert (tree->inst_i0->opcode == OP_COMPARE);
7596 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
7597 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
7599 tree->opcode = CEE_BR;
7600 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
7601 tree->inst_target_bb = tree->inst_true_bb;
7603 tree->inst_target_bb = tree->inst_false_bb;
7612 mono_local_cprop (MonoCompile *cfg)
7617 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
7619 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7620 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
7621 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
7626 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, int parts)
7628 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
7629 guint8 *ip = (guint8 *)header->code;
7632 int dfn = 0, i, code_size_ratio;
7634 mono_jit_stats.methods_compiled++;
7635 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
7636 mono_profiler_method_jit (method);
7638 cfg = g_new0 (MonoCompile, 1);
7639 cfg->method = method;
7640 cfg->mempool = mono_mempool_new ();
7642 cfg->prof_options = mono_profiler_get_events ();
7643 cfg->run_cctors = run_cctors;
7644 cfg->bb_hash = g_hash_table_new (NULL, NULL);
7645 cfg->domain = domain;
7646 cfg->verbose_level = mini_verbose;
7647 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
7648 ((MonoMethodNormal *)method)->header->max_stack);
7650 if (cfg->verbose_level > 2)
7651 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
7654 * create MonoInst* which represents arguments and local variables
7656 mono_compile_create_vars (cfg);
7658 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
7659 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7660 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
7661 mono_destroy_compile (cfg);
7665 mono_jit_stats.basic_blocks += cfg->num_bblocks;
7666 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
7668 if (cfg->num_varinfo > 2000) {
7670 * we disable some optimizations if there are too many variables
7671 * because JIT time may become too expensive. The actual number needs
7672 * to be tweaked and eventually the non-linear algorithms should be fixed.
7674 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
7675 cfg->disable_ssa = TRUE;
7677 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
7679 /* Depth-first ordering on basic blocks */
7680 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
7682 if (cfg->opt & MONO_OPT_BRANCH)
7683 optimize_branches (cfg);
7685 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
7686 if (cfg->num_bblocks != dfn + 1) {
7689 cfg->num_bblocks = dfn + 1;
7691 if (!header->clauses) {
7692 /* remove unreachable code, because the code in them may be
7693 * inconsistent (access to dead variables for example) */
7694 for (bb = cfg->bb_entry; bb;) {
7695 MonoBasicBlock *bbn = bb->next_bb;
7697 if (bbn && bbn->region == -1 && !bbn->dfn) {
7698 if (cfg->verbose_level > 1)
7699 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
7700 bb->next_bb = bbn->next_bb;
7701 nullify_basic_block (bbn);
7709 if (cfg->opt & MONO_OPT_LOOP) {
7710 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
7711 mono_compute_natural_loops (cfg);
7714 /* after method_to_ir */
7718 //#define DEBUGSSA "logic_run"
7719 #define DEBUGSSA_CLASS "Tests"
7722 if (!header->num_clauses && !cfg->disable_ssa) {
7723 mono_local_cprop (cfg);
7724 mono_ssa_compute (cfg);
7728 /* fixme: add all optimizations which requires SSA */
7729 if (cfg->opt & (MONO_OPT_DEADCE | MONO_OPT_ABCREM)) {
7730 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
7731 mono_local_cprop (cfg);
7732 mono_ssa_compute (cfg);
7734 if (cfg->verbose_level >= 2) {
7741 /* after SSA translation */
7745 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
7746 if (cfg->comp_done & MONO_COMP_SSA) {
7747 mono_ssa_cprop (cfg);
7749 mono_local_cprop (cfg);
7753 if (cfg->comp_done & MONO_COMP_SSA) {
7754 mono_ssa_deadce (cfg);
7756 //mono_ssa_strength_reduction (cfg);
7758 if ((cfg->flags & MONO_CFG_HAS_LDELEMA) && (cfg->opt & MONO_OPT_ABCREM))
7759 mono_perform_abc_removal (cfg);
7761 mono_ssa_remove (cfg);
7763 if (cfg->opt & MONO_OPT_BRANCH)
7764 optimize_branches (cfg);
7767 /* after SSA removal */
7771 decompose_pass (cfg);
7773 if (cfg->opt & MONO_OPT_LINEARS) {
7776 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
7777 cfg->comp_done &= ~MONO_COMP_LIVENESS;
7778 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
7779 mono_analyze_liveness (cfg);
7781 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
7782 regs = mono_arch_get_global_int_regs (cfg);
7783 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
7787 //mono_print_code (cfg);
7791 /* variables are allocated after decompose, since decompose could create temps */
7792 mono_arch_allocate_vars (cfg);
7794 if (cfg->opt & MONO_OPT_CFOLD)
7795 mono_constant_fold (cfg);
7797 mini_select_instructions (cfg);
7800 if (cfg->verbose_level >= 2) {
7801 char *id = mono_method_full_name (cfg->method, FALSE);
7802 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
7806 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
7808 jinfo = g_new0 (MonoJitInfo, 1);
7809 jinfo->method = method;
7810 jinfo->code_start = cfg->native_code;
7811 jinfo->code_size = cfg->code_len;
7812 jinfo->used_regs = cfg->used_int_regs;
7813 jinfo->domain_neutral = (cfg->opt & MONO_OPT_SHARED) != 0;
7815 if (header->num_clauses) {
7818 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
7819 jinfo->num_clauses = header->num_clauses;
7820 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
7821 sizeof (MonoJitExceptionInfo) * header->num_clauses);
7823 for (i = 0; i < header->num_clauses; i++) {
7824 MonoExceptionClause *ec = &header->clauses [i];
7825 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
7826 MonoBasicBlock *tblock;
7828 ei->flags = ec->flags;
7830 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7831 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->token_or_filter);
7833 ei->data.filter = cfg->native_code + tblock->native_offset;
7835 ei->data.token = ec->token_or_filter;
7838 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
7840 ei->try_start = cfg->native_code + tblock->native_offset;
7841 g_assert (tblock->native_offset);
7842 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
7844 ei->try_end = cfg->native_code + tblock->native_offset;
7845 g_assert (tblock->native_offset);
7846 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
7848 ei->handler_start = cfg->native_code + tblock->native_offset;
7852 cfg->jit_info = jinfo;
7854 mono_jit_info_table_add (cfg->domain, jinfo);
7856 /* collect statistics */
7857 mono_jit_stats.allocated_code_size += cfg->code_len;
7858 code_size_ratio = cfg->code_len;
7859 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
7860 mono_jit_stats.biggest_method_size = code_size_ratio;
7861 mono_jit_stats.biggest_method = method;
7863 code_size_ratio = (code_size_ratio * 100) / ((MonoMethodNormal *)method)->header->code_size;
7864 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
7865 mono_jit_stats.max_code_size_ratio = code_size_ratio;
7866 mono_jit_stats.max_ratio_method = method;
7868 mono_jit_stats.native_code_size += cfg->code_len;
7870 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7871 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
7877 mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain)
7880 GHashTable *jit_code_hash;
7881 gpointer code = NULL;
7887 jit_code_hash = target_domain->jit_code_hash;
7889 #ifdef MONO_USE_AOT_COMPILER
7890 if (!mono_compile_aot && (opt & MONO_OPT_AOT)) {
7892 MonoDomain *domain = mono_domain_get ();
7894 mono_domain_lock (domain);
7896 mono_class_init (method->klass);
7897 if ((info = mono_aot_get_method (domain, method))) {
7898 g_hash_table_insert (domain->jit_code_hash, method, info);
7899 mono_domain_unlock (domain);
7900 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
7901 return info->code_start;
7904 mono_domain_unlock (domain);
7908 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
7909 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
7912 if (!method->addr) {
7913 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
7914 method->addr = mono_lookup_internal_call (method);
7916 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
7917 mono_lookup_pinvoke_call (method, NULL, NULL);
7919 nm = mono_marshal_get_native_wrapper (method);
7920 return mono_compile_method (nm);
7922 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
7923 //mono_debug_add_wrapper (method, nm);
7924 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
7925 const char *name = method->name;
7928 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
7929 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
7930 /* FIXME: uhm, we need a wrapper to handle exceptions? */
7931 return (gpointer)mono_delegate_ctor;
7932 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
7933 nm = mono_marshal_get_delegate_invoke (method);
7934 return mono_jit_compile_method (nm);
7935 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
7936 nm = mono_marshal_get_delegate_begin_invoke (method);
7937 return mono_jit_compile_method (nm);
7938 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
7939 nm = mono_marshal_get_delegate_end_invoke (method);
7940 return mono_jit_compile_method (nm);
7946 cfg = mini_method_compile (method, opt, target_domain, TRUE, 0);
7948 mono_domain_lock (target_domain);
7950 /* Check if some other thread already did the job. In this case, we can
7951 discard the code this thread generated. */
7953 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
7954 /* We can't use a domain specific method in another domain */
7955 if ((target_domain == mono_domain_get ()) || info->domain_neutral) {
7956 code = info->code_start;
7957 // printf("Discarding code for method %s\n", method->name);
7962 g_hash_table_insert (jit_code_hash, method, cfg->jit_info);
7963 code = cfg->native_code;
7966 mono_destroy_compile (cfg);
7968 if (target_domain->jump_target_hash) {
7969 MonoJumpInfo patch_info;
7971 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
7973 patch_info.next = NULL;
7974 patch_info.ip.i = 0;
7975 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
7976 patch_info.data.method = method;
7977 g_hash_table_remove (target_domain->jump_target_hash, method);
7979 for (tmp = list; tmp; tmp = tmp->next)
7980 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info, TRUE);
7981 g_slist_free (list);
7984 mono_domain_unlock (target_domain);
7986 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
7991 mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt)
7993 /* FIXME: later copy the code from mono */
7994 MonoDomain *target_domain, *domain = mono_domain_get ();
7998 if (opt & MONO_OPT_SHARED)
7999 target_domain = mono_root_domain;
8001 target_domain = domain;
8003 mono_domain_lock (target_domain);
8005 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
8006 /* We can't use a domain specific method in another domain */
8007 if (! ((domain != target_domain) && !info->domain_neutral)) {
8008 mono_domain_unlock (target_domain);
8009 mono_jit_stats.methods_lookups++;
8010 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
8011 return info->code_start;
8015 mono_domain_unlock (target_domain);
8016 p = mono_jit_compile_method_inner (method, target_domain);
8021 mono_jit_compile_method (MonoMethod *method)
8023 return mono_jit_compile_method_with_opt (method, default_opt);
8027 mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method)
8029 MonoDomain *target_domain;
8032 if (default_opt & MONO_OPT_SHARED)
8033 target_domain = mono_root_domain;
8035 target_domain = domain;
8037 mono_domain_lock (target_domain);
8039 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
8040 /* We can't use a domain specific method in another domain */
8041 if (! ((domain != target_domain) && !info->domain_neutral)) {
8042 mono_domain_unlock (target_domain);
8043 mono_jit_stats.methods_lookups++;
8044 return info->code_start;
8048 mono_domain_unlock (target_domain);
8054 * mono_jit_runtime_invoke:
8055 * @method: the method to invoke
8056 * @obj: this pointer
8057 * @params: array of parameter values.
8058 * @exc: used to catch exceptions objects
8061 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
8064 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc, void* compiled_method);
8065 void* compiled_method;
8067 if (obj == NULL && !(method->flags & METHOD_ATTRIBUTE_STATIC) && !method->string_ctor && (method->wrapper_type == 0)) {
8068 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
8072 invoke = mono_marshal_get_runtime_invoke (method);
8073 runtime_invoke = mono_jit_compile_method (invoke);
8075 /* We need this here becuase mono_marshal_get_runtime_invoke can be place
8076 * the helper method in System.Object and not the target class
8078 mono_runtime_class_init (mono_class_vtable (mono_domain_get (), method->klass));
8080 compiled_method = mono_jit_compile_method (method);
8081 return runtime_invoke (obj, params, exc, compiled_method);
8084 #ifdef PLATFORM_WIN32
8085 #define GET_CONTEXT \
8086 struct sigcontext *ctx = (struct sigcontext*)_dummy;
8089 #define GET_CONTEXT \
8090 void *ctx = context;
8091 #elif defined(__ppc__) || defined (__powerpc__) || defined (__s390__)
8092 #define GET_CONTEXT \
8093 void *ctx = context;
8095 #define GET_CONTEXT \
8096 void **_p = (void **)&_dummy; \
8097 struct sigcontext *ctx = (struct sigcontext *)++_p;
8101 #ifdef MONO_ARCH_USE_SIGACTION
8102 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, siginfo_t *info, void *context)
8104 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy)
8108 SIG_HANDLER_SIGNATURE (sigfpe_signal_handler)
8113 exc = mono_get_exception_divide_by_zero ();
8115 mono_arch_handle_exception (ctx, exc, FALSE);
8119 SIG_HANDLER_SIGNATURE (sigill_signal_handler)
8123 exc = mono_get_exception_execution_engine ("SIGILL");
8125 mono_arch_handle_exception (ctx, exc, FALSE);
8128 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8131 sigsegv_signal_handler (int _dummy, siginfo_t *info, void *context)
8134 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
8135 struct sigcontext *ctx = (struct sigcontext *)&(((ucontext_t*)context)->uc_mcontext);
8137 /* Can't allocate memory using Boehm GC on altstack */
8138 if (jit_tls->stack_size &&
8139 ((guint8*)info->si_addr >= (guint8*)jit_tls->end_of_stack - jit_tls->stack_size) &&
8140 ((guint8*)info->si_addr < (guint8*)jit_tls->end_of_stack))
8141 exc = mono_domain_get ()->stack_overflow_ex;
8143 exc = mono_domain_get ()->null_reference_ex;
8145 mono_arch_handle_exception (ctx, exc, FALSE);
8151 SIG_HANDLER_SIGNATURE (sigsegv_signal_handler)
8155 mono_arch_handle_exception (ctx, NULL, FALSE);
8161 SIG_HANDLER_SIGNATURE (sigusr1_signal_handler)
8163 gboolean running_managed;
8168 running_managed = (mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx)) != NULL);
8170 exc = mono_thread_request_interruption (running_managed);
8173 mono_arch_handle_exception (ctx, exc, FALSE);
8177 SIG_HANDLER_SIGNATURE (sigquit_signal_handler)
8182 exc = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
8184 mono_arch_handle_exception (ctx, exc, FALSE);
8188 SIG_HANDLER_SIGNATURE (sigint_signal_handler)
8193 exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
8195 mono_arch_handle_exception (ctx, exc, FALSE);
8198 #ifndef PLATFORM_WIN32
8200 add_signal_handler (int signo, gpointer handler)
8202 struct sigaction sa;
8204 #ifdef MONO_ARCH_USE_SIGACTION
8205 sa.sa_sigaction = handler;
8206 sigemptyset (&sa.sa_mask);
8207 sa.sa_flags = SA_SIGINFO;
8209 sa.sa_handler = handler;
8210 sigemptyset (&sa.sa_mask);
8213 g_assert (sigaction (signo, &sa, NULL) != -1);
8218 mono_runtime_install_handlers (void)
8220 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8221 struct sigaction sa;
8224 #ifdef PLATFORM_WIN32
8226 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
8227 win32_seh_set_handler(SIGILL, sigill_signal_handler);
8228 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
8229 if (getenv ("MONO_DEBUG"))
8230 win32_seh_set_handler(SIGINT, sigint_signal_handler);
8231 #else /* !PLATFORM_WIN32 */
8233 /* libpthreads has its own implementation of sigaction(),
8234 * but it seems to work well with our current exception
8235 * handlers. If not we must call syscall directly instead
8238 if (getenv ("MONO_DEBUG")) {
8239 add_signal_handler (SIGINT, sigint_signal_handler);
8242 add_signal_handler (SIGFPE, sigfpe_signal_handler);
8243 add_signal_handler (SIGQUIT, sigquit_signal_handler);
8244 add_signal_handler (SIGILL, sigill_signal_handler);
8245 add_signal_handler (SIGBUS, sigsegv_signal_handler);
8246 add_signal_handler (mono_thread_get_abort_signal (), sigusr1_signal_handler);
8249 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8250 sa.sa_sigaction = sigsegv_signal_handler;
8251 sigemptyset (&sa.sa_mask);
8252 sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
8253 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
8255 add_signal_handler (SIGSEGV, sigsegv_signal_handler);
8258 #endif /* PLATFORM_WIN32 */
8261 /* mono_jit_create_remoting_trampoline:
8262 * @method: pointer to the method info
8264 * Creates a trampoline which calls the remoting functions. This
8265 * is used in the vtable of transparent proxies.
8267 * Returns: a pointer to the newly created code
8270 mono_jit_create_remoting_trampoline (MonoMethod *method)
8273 guint8 *addr = NULL;
8275 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
8276 (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
8277 nm = mono_marshal_get_remoting_invoke (method);
8278 addr = mono_compile_method (nm);
8280 addr = mono_compile_method (method);
8286 mini_init (const char *filename)
8290 mono_arch_cpu_init ();
8292 if (!g_thread_supported ())
8293 g_thread_init (NULL);
8295 mono_jit_tls_id = TlsAlloc ();
8296 setup_jit_tls_data ((gpointer)-1, mono_thread_abort);
8298 InitializeCriticalSection (&trampoline_hash_mutex);
8302 if (default_opt & MONO_OPT_AOT)
8305 mono_runtime_install_handlers ();
8306 mono_threads_install_cleanup (mini_thread_cleanup);
8308 #define JIT_TRAMPOLINES_WORK
8309 #ifdef JIT_TRAMPOLINES_WORK
8310 mono_install_compile_method (mono_jit_compile_method);
8311 mono_install_trampoline (mono_arch_create_jit_trampoline);
8312 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
8314 #define JIT_INVOKE_WORKS
8315 #ifdef JIT_INVOKE_WORKS
8316 mono_install_runtime_invoke (mono_jit_runtime_invoke);
8317 mono_install_handler (mono_arch_get_throw_exception ());
8319 mono_install_stack_walk (mono_jit_walk_stack);
8321 domain = mono_init (filename);
8324 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
8325 ves_icall_get_frame_info);
8326 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
8327 ves_icall_get_trace);
8328 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
8329 mono_runtime_install_handlers);
8332 create_helper_signature ();
8334 #define JIT_CALLS_WORK
8335 #ifdef JIT_CALLS_WORK
8336 /* Needs to be called here since register_jit_icall depends on it */
8337 mono_marshal_init ();
8339 mono_arch_register_lowlevel_calls ();
8340 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
8341 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
8342 mono_register_jit_icall (mono_trace_enter_method, "mono_trace_enter_method", NULL, TRUE);
8343 mono_register_jit_icall (mono_trace_leave_method, "mono_trace_leave_method", NULL, TRUE);
8344 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
8345 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
8347 /* fixme: we cant handle vararg methods this way, because the signature is not constant */
8348 //mono_register_jit_icall (ves_array_element_address, "ves_array_element_address", NULL);
8349 //mono_register_jit_icall (mono_array_new_va, "mono_array_new_va", NULL);
8351 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
8352 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
8353 helper_sig_void_ptr, TRUE);
8354 mono_register_jit_icall (mono_thread_get_pending_exception, "mono_thread_get_pending_exception", helper_sig_obj_void, FALSE);
8357 * NOTE, NOTE, NOTE, NOTE:
8358 * when adding emulation for some opcodes, remember to also add a dummy
8359 * rule to the burg files, because we need the arity information to be correct.
8361 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult, TRUE);
8362 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un, FALSE);
8363 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf, FALSE);
8364 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv, FALSE);
8365 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un, FALSE);
8366 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem, FALSE);
8367 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un, FALSE);
8369 #ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
8370 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl, TRUE);
8371 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr, TRUE);
8372 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un, TRUE);
8375 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8, FALSE);
8376 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4, FALSE);
8377 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8, FALSE);
8378 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8, FALSE);
8380 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
8381 mono_register_opcode_emulation (OP_FCONV_TO_I8, "__emul_fconv_to_i8", helper_sig_long_double, mono_fconv_i8, FALSE);
8383 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
8384 mono_register_opcode_emulation (CEE_CONV_R_UN, "__emul_conv_r_un", helper_sig_double_int, mono_conv_to_r8_un, FALSE);
8386 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
8387 mono_register_opcode_emulation (OP_LCONV_TO_R8, "__emul_lconv_to_r8", helper_sig_double_long, mono_lconv_to_r8, FALSE);
8389 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
8390 mono_register_opcode_emulation (OP_LCONV_TO_R4, "__emul_lconv_to_r4", helper_sig_float_long, mono_lconv_to_r4, FALSE);
8392 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
8393 mono_register_opcode_emulation (OP_LCONV_TO_R_UN, "__emul_lconv_to_r8_un", helper_sig_double_long, mono_lconv_to_r8_un, FALSE);
8395 #ifdef MONO_ARCH_EMULATE_FREM
8396 mono_register_opcode_emulation (OP_FREM, "__emul_frem", helper_sig_double_double_double, fmod, FALSE);
8399 #if SIZEOF_VOID_P == 4
8400 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4, TRUE);
8403 #warning "fixme: add opcode emulation"
8407 /* other jit icalls */
8408 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
8409 helper_sig_ptr_ptr_ptr, FALSE);
8410 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
8411 mono_register_jit_icall (mono_get_special_static_data, "mono_get_special_static_data", helper_sig_ptr_int, FALSE);
8412 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
8413 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
8414 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
8415 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
8416 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
8417 mono_register_jit_icall (helper_stelem_ref_check, "helper_stelem_ref_check", helper_sig_stelem_ref_check, FALSE);
8418 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
8419 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
8420 mono_register_jit_icall (mono_object_new_fast, "mono_object_new_fast", helper_sig_object_new_specific, FALSE);
8421 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
8422 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
8423 mono_register_jit_icall (mono_string_to_utf16, "mono_string_to_utf16", helper_sig_ptr_obj, FALSE);
8424 mono_register_jit_icall (mono_string_from_utf16, "mono_string_from_utf16", helper_sig_obj_ptr, FALSE);
8425 mono_register_jit_icall (mono_string_new_wrapper, "mono_string_new_wrapper", helper_sig_obj_ptr, FALSE);
8426 mono_register_jit_icall (mono_string_to_utf8, "mono_string_to_utf8", helper_sig_ptr_obj, FALSE);
8427 mono_register_jit_icall (mono_string_to_bstr, "mono_string_to_bstr", helper_sig_ptr_obj, FALSE);
8428 mono_register_jit_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", helper_sig_ptr_obj, FALSE);
8429 mono_register_jit_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", helper_sig_ptr_obj, FALSE);
8430 mono_register_jit_icall (mono_string_builder_to_utf16, "mono_string_builder_to_utf16", helper_sig_ptr_obj, FALSE);
8431 mono_register_jit_icall (mono_array_to_savearray, "mono_array_to_savearray", helper_sig_ptr_obj, FALSE);
8432 mono_register_jit_icall (mono_array_to_lparray, "mono_array_to_lparray", helper_sig_ptr_obj, FALSE);
8433 mono_register_jit_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", helper_sig_ptr_obj, FALSE);
8434 mono_register_jit_icall (mono_ftnptr_to_delegate, "mono_ftnptr_to_delegate", helper_sig_obj_ptr_ptr, FALSE);
8435 mono_register_jit_icall (mono_marshal_string_array, "mono_marshal_string_array", helper_sig_ptr_obj, FALSE);
8436 mono_register_jit_icall (mono_marshal_string_array_to_unicode, "mono_marshal_string_array_to_unicode", helper_sig_ptr_obj, FALSE);
8437 mono_register_jit_icall (mono_marshal_asany, "mono_marshal_asany", helper_sig_ptr_obj_int, FALSE);
8438 mono_register_jit_icall (mono_marshal_free_asany, "mono_marshal_free_asany", helper_sig_void_obj_ptr_int, FALSE);
8439 mono_register_jit_icall (mono_string_utf8_to_builder, "mono_string_utf8_to_builder", helper_sig_void_ptr_ptr, FALSE);
8440 mono_register_jit_icall (mono_string_utf16_to_builder, "mono_string_utf16_to_builder", helper_sig_void_ptr_ptr, FALSE);
8441 mono_register_jit_icall (mono_marshal_free_array, "mono_marshal_free_array", helper_sig_void_ptr_ptr, FALSE);
8442 mono_register_jit_icall (mono_string_to_byvalstr, "mono_string_to_byvalstr", helper_sig_void_ptr_ptr_ptr, FALSE);
8443 mono_register_jit_icall (mono_string_to_byvalwstr, "mono_string_to_byvalwstr", helper_sig_void_ptr_ptr_ptr, FALSE);
8444 mono_register_jit_icall (g_free, "g_free", helper_sig_void_ptr, FALSE);
8445 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
8446 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
8447 mono_register_jit_icall (mono_ldftn_nosync, "mono_ldftn_nosync", helper_sig_compile, FALSE);
8448 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
8451 #define JIT_RUNTIME_WORKS
8452 #ifdef JIT_RUNTIME_WORKS
8453 mono_runtime_install_cleanup ((MonoDomainFunc)mini_cleanup);
8454 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
8457 mono_thread_attach (domain);
8461 MonoJitStats mono_jit_stats = {0};
8464 print_jit_stats (void)
8466 if (mono_jit_stats.enabled) {
8467 g_print ("Mono Jit statistics\n");
8468 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
8469 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
8470 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
8471 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
8472 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
8473 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
8474 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
8475 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
8476 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
8477 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
8478 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
8479 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
8480 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
8481 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
8482 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
8483 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
8484 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
8485 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
8487 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
8488 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
8489 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
8490 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
8491 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
8493 g_print ("\nGeneric instances: %ld\n", mono_stats.generic_instance_count);
8494 g_print ("Inflated methods: %ld\n", mono_stats.inflated_method_count);
8495 g_print ("Inflated types: %ld\n", mono_stats.inflated_type_count);
8496 g_print ("Generics metadata size: %ld\n", mono_stats.generics_metadata_size);
8501 mini_cleanup (MonoDomain *domain)
8504 * mono_runtime_cleanup() and mono_domain_finalize () need to
8505 * be called early since they need the execution engine still
8506 * fully working (mono_domain_finalize may invoke managed finalizers
8507 * and mono_runtime_cleanup will wait for other threads to finish).
8509 mono_domain_finalize (domain, 2000);
8511 mono_runtime_cleanup (domain);
8513 mono_profiler_shutdown ();
8515 mono_debug_cleanup ();
8517 #ifdef PLATFORM_WIN32
8518 win32_seh_cleanup();
8521 mono_domain_free (domain, TRUE);
8527 mono_set_defaults (int verbose_level, guint32 opts)
8529 mini_verbose = verbose_level;
8534 mono_precompile_assembly (MonoAssembly *ass, void *user_data)
8536 MonoImage *image = ass->image;
8540 if (mini_verbose > 0)
8541 printf ("PRECOMPILE: %s.\n", ass->image->name);
8543 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
8544 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
8545 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
8547 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
8551 if (mini_verbose > 1) {
8552 char * desc = mono_method_full_name (method, TRUE);
8553 g_print ("Compiling %d %s\n", count, desc);
8556 mono_compile_method (method);
8560 void mono_precompile_assemblies ()
8562 mono_assembly_foreach ((GFunc)mono_precompile_assembly, NULL);