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>
50 #include "jit-icalls.c"
52 #define MONO_IS_COND_BRANCH(op) ((op >= CEE_BEQ && op <= CEE_BLT_UN) || (op >= OP_LBEQ && op <= OP_LBLT_UN) || (op >= OP_FBEQ && op <= OP_FBLT_UN))
54 #define MONO_CHECK_THIS(ins) (cfg->method->signature->hasthis && (ins)->ssa_op == MONO_SSA_LOAD && (ins)->inst_left->inst_c0 == 0)
56 gboolean mono_arch_handle_exception (struct sigcontext *ctx, gpointer obj, gboolean test_only);
57 static gpointer mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt);
58 static gpointer mono_jit_compile_method (MonoMethod *method);
59 static gpointer mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method);
61 static void handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src,
62 const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native);
64 static void dec_foreach (MonoInst *tree, MonoCompile *cfg);
66 static int mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
67 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
68 guint inline_offset, gboolean is_virtual_call);
70 extern guint8 mono_burg_arity [];
71 /* helper methods signature */
72 static MonoMethodSignature *helper_sig_long_long_long = NULL;
73 static MonoMethodSignature *helper_sig_long_long_int = NULL;
74 static MonoMethodSignature *helper_sig_newarr = NULL;
75 static MonoMethodSignature *helper_sig_newarr_specific = NULL;
76 static MonoMethodSignature *helper_sig_ldstr = NULL;
77 static MonoMethodSignature *helper_sig_domain_get = NULL;
78 static MonoMethodSignature *helper_sig_object_new = NULL;
79 static MonoMethodSignature *helper_sig_object_new_specific = NULL;
80 static MonoMethodSignature *helper_sig_compile = NULL;
81 static MonoMethodSignature *helper_sig_compile_virt = NULL;
82 static MonoMethodSignature *helper_sig_obj_ptr = NULL;
83 static MonoMethodSignature *helper_sig_obj_void = NULL;
84 static MonoMethodSignature *helper_sig_ptr_void = NULL;
85 static MonoMethodSignature *helper_sig_void_ptr = NULL;
86 static MonoMethodSignature *helper_sig_void_obj = NULL;
87 static MonoMethodSignature *helper_sig_void_ptr_ptr = NULL;
88 static MonoMethodSignature *helper_sig_void_ptr_ptr_ptr = NULL;
89 static MonoMethodSignature *helper_sig_ptr_ptr_ptr = NULL;
90 static MonoMethodSignature *helper_sig_ptr_obj = NULL;
91 static MonoMethodSignature *helper_sig_ptr_int = NULL;
92 static MonoMethodSignature *helper_sig_initobj = NULL;
93 static MonoMethodSignature *helper_sig_memcpy = NULL;
94 static MonoMethodSignature *helper_sig_memset = NULL;
95 static MonoMethodSignature *helper_sig_ulong_double = NULL;
96 static MonoMethodSignature *helper_sig_long_double = NULL;
97 static MonoMethodSignature *helper_sig_double_long = NULL;
98 static MonoMethodSignature *helper_sig_double_int = NULL;
99 static MonoMethodSignature *helper_sig_float_long = NULL;
100 static MonoMethodSignature *helper_sig_double_double_double = NULL;
101 static MonoMethodSignature *helper_sig_uint_double = NULL;
102 static MonoMethodSignature *helper_sig_int_double = NULL;
103 static MonoMethodSignature *helper_sig_stelem_ref = NULL;
104 static MonoMethodSignature *helper_sig_stelem_ref_check = NULL;
105 static MonoMethodSignature *helper_sig_class_init_trampoline = NULL;
107 static guint32 default_opt = MONO_OPT_PEEPHOLE;
109 guint32 mono_jit_tls_id = -1;
110 MonoTraceSpec *mono_jit_trace_calls = NULL;
111 gboolean mono_break_on_exc = FALSE;
112 gboolean mono_compile_aot = FALSE;
114 static int mini_verbose = 0;
116 static CRITICAL_SECTION trampoline_hash_mutex;
118 static GHashTable *class_init_hash_addr = NULL;
120 static GHashTable *jump_trampoline_hash = NULL;
123 mono_running_on_valgrind (void)
125 #ifdef HAVE_VALGRIND_MEMCHECK_H
126 if (RUNNING_ON_VALGRIND)
135 #ifdef MONO_USE_EXC_TABLES
137 mono_type_blittable (MonoType *type)
157 case MONO_TYPE_VALUETYPE:
158 case MONO_TYPE_CLASS:
159 return type->data.klass->blittable;
169 mono_method_blittable (MonoMethod *method)
171 MonoMethodSignature *sig;
177 if (!mono_arch_has_unwind_info (method->addr)) {
181 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
184 sig = method->signature;
186 if (!mono_type_blittable (sig->ret))
189 for (i = 0; i < sig->param_count; i++)
190 if (!mono_type_blittable (sig->params [i]))
193 if (mono_method_has_marshal_info (method))
196 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && ((MonoMethodPInvoke*)method)->piflags & PINVOKE_ATTRIBUTE_SUPPORTS_LAST_ERROR)
204 G_GNUC_UNUSED static void
205 print_method_from_ip (void *ip)
210 MonoDomain *domain = mono_domain_get ();
212 ji = mono_jit_info_table_find (domain, ip);
214 g_print ("No method at %p\n", ip);
217 method = mono_method_full_name (ji->method, TRUE);
218 source = mono_debug_source_location_from_address (ji->method, (int) ip, NULL, domain);
220 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);
223 g_print ("%s\n", source);
231 * mono_method_same_domain:
233 * Determine whenever two compiled methods are in the same domain, thus
234 * the address of the callee can be embedded in the caller.
236 gboolean mono_method_same_domain (MonoJitInfo *caller, MonoJitInfo *callee)
238 if (!caller || !callee)
242 * If the call was made from domain-neutral to domain-specific
243 * code, we can't patch the call site.
245 if (caller->domain_neutral && !callee->domain_neutral)
248 if ((caller->method->klass == mono_defaults.appdomain_class) &&
249 (strstr (caller->method->name, "InvokeInDomain"))) {
250 /* The InvokeInDomain methods change the current appdomain */
258 mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token)
260 MonoJumpInfoToken *res = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoToken));
267 #define MONO_INIT_VARINFO(vi,id) do { \
268 (vi)->range.first_use.pos.bid = 0xffff; \
274 * Basic blocks have two numeric identifiers:
275 * dfn: Depth First Number
276 * block_num: unique ID assigned at bblock creation
278 #define NEW_BBLOCK(cfg) (mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)))
279 #define ADD_BBLOCK(cfg,bbhash,b) do { \
280 g_hash_table_insert (bbhash, (b)->cil_code, (b)); \
281 (b)->block_num = cfg->num_bblocks++; \
282 (b)->real_offset = real_offset; \
285 #define GET_BBLOCK(cfg,bbhash,tblock,ip) do { \
286 (tblock) = g_hash_table_lookup (bbhash, (ip)); \
288 if ((ip) >= end || (ip) < header->code) goto unverified; \
289 (tblock) = NEW_BBLOCK (cfg); \
290 (tblock)->cil_code = (ip); \
291 ADD_BBLOCK (cfg, (bbhash), (tblock)); \
293 (tblock)->real_offset = real_offset; \
296 #define CHECK_BBLOCK(target,ip,tblock) do { \
297 if ((target) < (ip) && !(tblock)->code) { \
298 bb_recheck = g_list_prepend (bb_recheck, (tblock)); \
299 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); \
303 #define NEW_ICONST(cfg,dest,val) do { \
304 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
305 (dest)->opcode = OP_ICONST; \
306 (dest)->inst_c0 = (val); \
307 (dest)->type = STACK_I4; \
310 /* FIXME: have a different definition of NEW_PCONST for 64 bit systems */
311 #define NEW_PCONST(cfg,dest,val) do { \
312 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
313 (dest)->opcode = OP_ICONST; \
314 (dest)->inst_p0 = (val); \
315 (dest)->type = STACK_PTR; \
318 #define NEW_CLASSCONST(cfg,dest,val) do { \
319 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
320 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
321 (dest)->inst_p0 = (val); \
322 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_CLASS; \
323 (dest)->type = STACK_PTR; \
326 #define NEW_IMAGECONST(cfg,dest,val) do { \
327 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
328 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
329 (dest)->inst_p0 = (val); \
330 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_IMAGE; \
331 (dest)->type = STACK_PTR; \
334 #define NEW_FIELDCONST(cfg,dest,field) do { \
335 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
336 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
337 (dest)->inst_p0 = (field); \
338 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_FIELD; \
339 (dest)->type = STACK_PTR; \
342 #define NEW_METHODCONST(cfg,dest,val) do { \
343 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
344 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
345 (dest)->inst_p0 = (val); \
346 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_METHODCONST; \
347 (dest)->type = STACK_PTR; \
350 #define NEW_VTABLECONST(cfg,dest,vtable) do { \
351 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
352 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
353 (dest)->inst_p0 = mono_compile_aot ? (gpointer)((vtable)->klass) : (vtable); \
354 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_VTABLE; \
355 (dest)->type = STACK_PTR; \
358 #define NEW_SFLDACONST(cfg,dest,field) do { \
359 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
360 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
361 (dest)->inst_p0 = (field); \
362 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_SFLDA; \
363 (dest)->type = STACK_PTR; \
366 #define NEW_LDSTRCONST(cfg,dest,image,token) do { \
367 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
368 (dest)->opcode = OP_AOTCONST; \
369 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
370 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_LDSTR; \
371 (dest)->type = STACK_OBJ; \
374 #define NEW_TYPE_FROM_HANDLE_CONST(cfg,dest,image,token) do { \
375 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
376 (dest)->opcode = OP_AOTCONST; \
377 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
378 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_TYPE_FROM_HANDLE; \
379 (dest)->type = STACK_OBJ; \
382 #define NEW_LDTOKENCONST(cfg,dest,image,token) do { \
383 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
384 (dest)->opcode = OP_AOTCONST; \
385 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
386 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_LDTOKEN; \
387 (dest)->type = STACK_PTR; \
390 #define NEW_DOMAINCONST(cfg,dest) do { \
391 if (cfg->opt & MONO_OPT_SHARED) { \
392 NEW_TEMPLOAD (cfg, dest, mono_get_domainvar (cfg)->inst_c0); \
394 NEW_PCONST (cfg, dest, (cfg)->domain); \
398 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
400 #define NEW_ARGLOAD(cfg,dest,num) do { \
401 if (arg_array [(num)]->opcode == OP_ICONST) (dest) = arg_array [(num)]; else { \
402 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
403 (dest)->ssa_op = MONO_SSA_LOAD; \
404 (dest)->inst_i0 = arg_array [(num)]; \
405 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
406 type_to_eval_stack_type (param_types [(num)], (dest)); \
407 (dest)->klass = (dest)->inst_i0->klass; \
410 #define NEW_LOCLOAD(cfg,dest,num) do { \
411 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
412 (dest)->ssa_op = MONO_SSA_LOAD; \
413 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
414 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
415 type_to_eval_stack_type (header->locals [(num)], (dest)); \
416 (dest)->klass = (dest)->inst_i0->klass; \
419 #define NEW_LOCLOADA(cfg,dest,num) do { \
420 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
421 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
422 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
423 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
424 (dest)->opcode = OP_LDADDR; \
425 (dest)->type = STACK_MP; \
426 (dest)->klass = (dest)->inst_i0->klass; \
427 if (!MONO_TYPE_ISSTRUCT (header->locals [(num)])) \
428 (cfg)->disable_ssa = TRUE; \
431 #define NEW_RETLOADA(cfg,dest) do { \
432 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
433 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
434 (dest)->inst_i0 = (cfg)->ret; \
435 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
436 (dest)->opcode = CEE_LDIND_I; \
437 (dest)->type = STACK_MP; \
438 (dest)->klass = (dest)->inst_i0->klass; \
439 (cfg)->disable_ssa = TRUE; \
442 #define NEW_ARGLOADA(cfg,dest,num) do { \
443 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
444 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
445 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
446 (dest)->inst_i0 = arg_array [(num)]; \
447 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
448 (dest)->opcode = OP_LDADDR; \
449 (dest)->type = STACK_MP; \
450 (dest)->klass = (dest)->inst_i0->klass; \
451 (cfg)->disable_ssa = TRUE; \
454 #define NEW_TEMPLOAD(cfg,dest,num) do { \
455 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
456 (dest)->ssa_op = MONO_SSA_LOAD; \
457 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
458 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
459 type_to_eval_stack_type ((dest)->inst_i0->inst_vtype, (dest)); \
460 (dest)->klass = (dest)->inst_i0->klass; \
463 #define NEW_TEMPLOADA(cfg,dest,num) do { \
464 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
465 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
466 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
467 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
468 (dest)->opcode = OP_LDADDR; \
469 (dest)->type = STACK_MP; \
470 (dest)->klass = (dest)->inst_i0->klass; \
471 if (!MONO_TYPE_ISSTRUCT (cfg->varinfo [(num)]->inst_vtype)) \
472 (cfg)->disable_ssa = TRUE; \
476 #define NEW_INDLOAD(cfg,dest,addr,vtype) do { \
477 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
478 (dest)->inst_left = addr; \
479 (dest)->opcode = mono_type_to_ldind (vtype); \
480 type_to_eval_stack_type (vtype, (dest)); \
481 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
484 #define NEW_INDSTORE(cfg,dest,addr,value,vtype) do { \
485 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
486 (dest)->inst_i0 = addr; \
487 (dest)->opcode = mono_type_to_stind (vtype); \
488 (dest)->inst_i1 = (value); \
489 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
492 #define NEW_TEMPSTORE(cfg,dest,num,inst) do { \
493 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
494 (dest)->ssa_op = MONO_SSA_STORE; \
495 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
496 (dest)->opcode = mono_type_to_stind ((dest)->inst_i0->inst_vtype); \
497 (dest)->inst_i1 = (inst); \
498 (dest)->klass = (dest)->inst_i0->klass; \
501 #define NEW_LOCSTORE(cfg,dest,num,inst) do { \
502 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
503 (dest)->opcode = mono_type_to_stind (header->locals [(num)]); \
504 (dest)->ssa_op = MONO_SSA_STORE; \
505 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
506 (dest)->inst_i1 = (inst); \
507 (dest)->klass = (dest)->inst_i0->klass; \
510 #define NEW_ARGSTORE(cfg,dest,num,inst) do { \
511 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
512 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
513 (dest)->opcode = mono_type_to_stind (param_types [(num)]); \
514 (dest)->ssa_op = MONO_SSA_STORE; \
515 (dest)->inst_i0 = arg_array [(num)]; \
516 (dest)->inst_i1 = (inst); \
517 (dest)->klass = (dest)->inst_i0->klass; \
520 #define ADD_BINOP(op) do { \
521 MONO_INST_NEW (cfg, ins, (op)); \
522 ins->cil_code = ip; \
524 ins->inst_i0 = sp [0]; \
525 ins->inst_i1 = sp [1]; \
527 type_from_op (ins); \
531 #define ADD_UNOP(op) do { \
532 MONO_INST_NEW (cfg, ins, (op)); \
533 ins->cil_code = ip; \
535 ins->inst_i0 = sp [0]; \
537 type_from_op (ins); \
541 #define ADD_BINCOND(next_block) do { \
543 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
545 cmp->inst_i0 = sp [0]; \
546 cmp->inst_i1 = sp [1]; \
547 cmp->cil_code = ins->cil_code; \
548 type_from_op (cmp); \
550 ins->inst_i0 = cmp; \
551 MONO_ADD_INS (bblock, ins); \
552 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
553 GET_BBLOCK (cfg, bbhash, tblock, target); \
554 link_bblock (cfg, bblock, tblock); \
555 ins->inst_true_bb = tblock; \
556 CHECK_BBLOCK (target, ip, tblock); \
557 if ((next_block)) { \
558 link_bblock (cfg, bblock, (next_block)); \
559 ins->inst_false_bb = (next_block); \
560 start_new_bblock = 1; \
562 GET_BBLOCK (cfg, bbhash, tblock, ip); \
563 link_bblock (cfg, bblock, tblock); \
564 ins->inst_false_bb = tblock; \
565 start_new_bblock = 2; \
569 /* FIXME: handle float, long ... */
570 #define ADD_UNCOND(istrue) do { \
572 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
574 cmp->inst_i0 = sp [0]; \
575 switch (cmp->inst_i0->type) { \
577 cmp->inst_i1 = zero_int64; break; \
579 cmp->inst_i1 = zero_r8; break; \
582 cmp->inst_i1 = zero_ptr; break; \
584 cmp->inst_i1 = zero_obj; break; \
586 cmp->inst_i1 = zero_int32; \
588 cmp->cil_code = ins->cil_code; \
589 type_from_op (cmp); \
591 ins->inst_i0 = cmp; \
592 ins->opcode = (istrue)? CEE_BNE_UN: CEE_BEQ; \
593 MONO_ADD_INS (bblock, ins); \
594 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
595 GET_BBLOCK (cfg, bbhash, tblock, target); \
596 link_bblock (cfg, bblock, tblock); \
597 ins->inst_true_bb = tblock; \
598 CHECK_BBLOCK (target, ip, tblock); \
599 GET_BBLOCK (cfg, bbhash, tblock, ip); \
600 link_bblock (cfg, bblock, tblock); \
601 ins->inst_false_bb = tblock; \
602 start_new_bblock = 2; \
605 #define NEW_LDELEMA(cfg,dest,sp,k) do { \
606 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
607 (dest)->opcode = CEE_LDELEMA; \
608 (dest)->inst_left = (sp) [0]; \
609 (dest)->inst_right = (sp) [1]; \
610 (dest)->type = STACK_MP; \
611 (dest)->klass = (k); \
614 #define NEW_GROUP(cfg,dest,el1,el2) do { \
615 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
616 (dest)->opcode = OP_GROUP; \
617 (dest)->inst_left = (el1); \
618 (dest)->inst_right = (el2); \
623 compare_bblock (gconstpointer a, gconstpointer b)
625 const MonoBasicBlock *b1 = a;
626 const MonoBasicBlock *b2 = b;
628 return b2->cil_code - b1->cil_code;
633 * link_bblock: Links two basic blocks
635 * links two basic blocks in the control flow graph, the 'from'
636 * argument is the starting block and the 'to' argument is the block
637 * the control flow ends to after 'from'.
640 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
642 MonoBasicBlock **newa;
646 if (from->cil_code) {
648 g_print ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
650 g_print ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
653 g_print ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
655 g_print ("edge from entry to exit\n");
659 for (i = 0; i < from->out_count; ++i) {
660 if (to == from->out_bb [i]) {
666 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
667 for (i = 0; i < from->out_count; ++i) {
668 newa [i] = from->out_bb [i];
676 for (i = 0; i < to->in_count; ++i) {
677 if (from == to->in_bb [i]) {
683 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
684 for (i = 0; i < to->in_count; ++i) {
685 newa [i] = to->in_bb [i];
694 * mono_find_block_region:
696 * We mark each basic block with a region ID. We use that to avoid BB
697 * optimizations when blocks are in different regions.
700 * A region token that encodes where this region is, and information
701 * about the clause owner for this block.
703 * The region encodes the try/catch/filter clause that owns this block
704 * as well as the type. -1 is a special value that represents a block
705 * that is in none of try/catch/filter.
708 mono_find_block_region (MonoCompile *cfg, int offset, int *filter_lengths)
710 MonoMethod *method = cfg->method;
711 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
712 MonoExceptionClause *clause;
715 /* first search for handlers and filters */
716 for (i = 0; i < header->num_clauses; ++i) {
717 clause = &header->clauses [i];
718 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->token_or_filter) &&
719 (offset < (clause->token_or_filter + filter_lengths [i])))
720 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
722 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
723 if (clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)
724 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
726 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
730 /* search the try blocks */
731 for (i = 0; i < header->num_clauses; ++i) {
732 clause = &header->clauses [i];
733 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
734 return ((i + 1) << 8) | clause->flags;
741 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
743 MonoMethod *method = cfg->method;
744 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
745 MonoExceptionClause *clause;
746 MonoBasicBlock *handler;
750 for (i = 0; i < header->num_clauses; ++i) {
751 clause = &header->clauses [i];
752 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
753 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
754 if (clause->flags == type) {
755 handler = g_hash_table_lookup (cfg->bb_hash, header->code + clause->handler_offset);
757 res = g_list_append (res, handler);
765 mono_find_spvar_for_region (MonoCompile *cfg, int region)
767 return g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
771 mono_create_spvar_for_region (MonoCompile *cfg, int region)
775 var = g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
779 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
780 /* prevent it from being register allocated */
781 var->flags |= MONO_INST_INDIRECT;
783 g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
787 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
791 array [*dfn] = start;
792 /*g_print ("visit %d at %p\n", *dfn, start->cil_code);*/
793 for (i = 0; i < start->out_count; ++i) {
794 if (start->out_bb [i]->dfn)
797 start->out_bb [i]->dfn = *dfn;
798 start->out_bb [i]->df_parent = start;
799 array [*dfn] = start->out_bb [i];
800 df_visit (start->out_bb [i], dfn, array);
806 MonoBasicBlock *best;
810 previous_foreach (gconstpointer key, gpointer val, gpointer data)
812 PrevStruct *p = data;
813 MonoBasicBlock *bb = val;
814 //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,
815 //bb->method == p->best->method, bb->cil_code < p->code, bb->cil_code > p->best->cil_code);
817 if (bb->cil_code && bb->cil_code < p->code && bb->cil_code > p->best->cil_code)
821 static MonoBasicBlock*
822 find_previous (GHashTable *bb_hash, MonoBasicBlock *start, const guchar *code) {
828 g_hash_table_foreach (bb_hash, (GHFunc)previous_foreach, &p);
833 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
842 * FIXME: take into account all the details:
843 * second may have been the target of more than one bblock
845 second->out_count = first->out_count;
846 second->out_bb = first->out_bb;
848 for (i = 0; i < first->out_count; ++i) {
849 bb = first->out_bb [i];
850 for (j = 0; j < bb->in_count; ++j) {
851 if (bb->in_bb [j] == first)
852 bb->in_bb [j] = second;
856 first->out_count = 0;
857 first->out_bb = NULL;
858 link_bblock (cfg, first, second);
860 second->last_ins = first->last_ins;
862 /*g_print ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
863 for (inst = first->code; inst && inst->next; inst = inst->next) {
864 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
865 g_print ("found %p: %s", inst->next->cil_code, code);
867 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
868 second->code = inst->next;
870 first->last_ins = inst;
871 second->next_bb = first->next_bb;
872 first->next_bb = second;
877 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
883 mono_type_to_ldind (MonoType *type)
895 case MONO_TYPE_BOOLEAN:
909 case MONO_TYPE_FNPTR:
911 case MONO_TYPE_CLASS:
912 case MONO_TYPE_STRING:
913 case MONO_TYPE_OBJECT:
914 case MONO_TYPE_SZARRAY:
915 case MONO_TYPE_ARRAY:
916 return CEE_LDIND_REF;
924 case MONO_TYPE_VALUETYPE:
925 if (type->data.klass->enumtype) {
926 t = type->data.klass->enum_basetype->type;
930 case MONO_TYPE_TYPEDBYREF:
932 case MONO_TYPE_GENERICINST:
933 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE)
935 return CEE_LDIND_REF;
937 g_error ("unknown type 0x%02x in type_to_ldind", type->type);
943 mono_type_to_stind (MonoType *type)
954 case MONO_TYPE_BOOLEAN:
966 case MONO_TYPE_FNPTR:
968 case MONO_TYPE_CLASS:
969 case MONO_TYPE_STRING:
970 case MONO_TYPE_OBJECT:
971 case MONO_TYPE_SZARRAY:
972 case MONO_TYPE_ARRAY:
973 return CEE_STIND_REF;
981 case MONO_TYPE_VALUETYPE:
982 if (type->data.klass->enumtype) {
983 t = type->data.klass->enum_basetype->type;
987 case MONO_TYPE_TYPEDBYREF:
989 case MONO_TYPE_GENERICINST:
990 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE)
992 return CEE_STIND_REF;
994 g_error ("unknown type 0x%02x in type_to_stind", type->type);
1000 * Returns the type used in the eval stack when @type is loaded.
1001 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
1004 type_to_eval_stack_type (MonoType *type, MonoInst *inst) {
1008 inst->type = STACK_MP;
1016 case MONO_TYPE_BOOLEAN:
1019 case MONO_TYPE_CHAR:
1022 inst->type = STACK_I4;
1027 case MONO_TYPE_FNPTR:
1028 inst->type = STACK_PTR;
1030 case MONO_TYPE_CLASS:
1031 case MONO_TYPE_STRING:
1032 case MONO_TYPE_OBJECT:
1033 case MONO_TYPE_SZARRAY:
1034 case MONO_TYPE_ARRAY:
1035 inst->type = STACK_OBJ;
1039 inst->type = STACK_I8;
1043 inst->type = STACK_R8;
1045 case MONO_TYPE_VALUETYPE:
1046 if (type->data.klass->enumtype) {
1047 t = type->data.klass->enum_basetype->type;
1050 inst->klass = type->data.klass;
1051 inst->type = STACK_VTYPE;
1054 case MONO_TYPE_TYPEDBYREF:
1055 inst->klass = mono_defaults.typed_reference_class;
1056 inst->type = STACK_VTYPE;
1058 case MONO_TYPE_GENERICINST:
1059 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE) {
1060 inst->klass = mono_class_from_mono_type (type);
1061 inst->type = STACK_VTYPE;
1063 inst->type = STACK_OBJ;
1067 g_error ("unknown type 0x%02x in eval stack type", type->type);
1072 * The following tables are used to quickly validate the IL code in type_from_op ().
1075 bin_num_table [STACK_MAX] [STACK_MAX] = {
1076 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1077 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1078 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1079 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1080 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
1081 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
1082 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1083 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1088 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
1091 /* reduce the size of this table */
1093 bin_int_table [STACK_MAX] [STACK_MAX] = {
1094 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1095 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1096 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1097 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1098 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1099 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1100 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1101 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1105 bin_comp_table [STACK_MAX] [STACK_MAX] = {
1107 {0, 1, 0, 1, 0, 0, 4, 0},
1108 {0, 0, 1, 0, 0, 0, 0, 0},
1109 {0, 1, 0, 1, 0, 2, 4, 0},
1110 {0, 0, 0, 0, 1, 0, 0, 0},
1111 {0, 0, 0, 2, 0, 1, 0, 0},
1112 {0, 4, 0, 4, 0, 0, 3, 0},
1113 {0, 0, 0, 0, 0, 0, 0, 0},
1116 /* reduce the size of this table */
1118 shift_table [STACK_MAX] [STACK_MAX] = {
1119 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1120 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1121 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1122 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1123 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1124 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1125 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1126 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1130 * Tables to map from the non-specific opcode to the matching
1131 * type-specific opcode.
1133 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
1134 static const guint16
1135 binops_op_map [STACK_MAX] = {
1136 0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, 0
1139 /* handles from CEE_NEG to CEE_CONV_U8 */
1140 static const guint16
1141 unops_op_map [STACK_MAX] = {
1142 0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, 0
1145 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
1146 static const guint16
1147 ovfops_op_map [STACK_MAX] = {
1148 0, 0, OP_LCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_FCONV_TO_U2-CEE_CONV_U2, 0
1151 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
1152 static const guint16
1153 ovf2ops_op_map [STACK_MAX] = {
1154 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
1157 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
1158 static const guint16
1159 ovf3ops_op_map [STACK_MAX] = {
1160 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
1163 /* handles from CEE_CEQ to CEE_CLT_UN */
1164 static const guint16
1165 ceqops_op_map [STACK_MAX] = {
1166 0, 0, OP_LCEQ-CEE_CEQ, OP_PCEQ-CEE_CEQ, OP_FCEQ-CEE_CEQ, 0
1170 * Sets ins->type (the type on the eval stack) according to the
1171 * type of the opcode and the arguments to it.
1172 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1174 * FIXME: this function sets ins->type unconditionally in some cases, but
1175 * it should set it to invalid for some types (a conv.x on an object)
1178 type_from_op (MonoInst *ins) {
1179 switch (ins->opcode) {
1186 /* FIXME: check unverifiable args for STACK_MP */
1187 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1188 ins->opcode += binops_op_map [ins->type];
1195 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1196 ins->opcode += binops_op_map [ins->type];
1201 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1202 ins->opcode += binops_op_map [ins->type];
1205 /* FIXME: handle some specifics with ins->next->type */
1206 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1213 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1214 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1218 ins->type = neg_table [ins->inst_i0->type];
1219 ins->opcode += unops_op_map [ins->type];
1222 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1223 ins->type = ins->inst_i0->type;
1225 ins->type = STACK_INV;
1226 ins->opcode += unops_op_map [ins->type];
1232 ins->type = STACK_I4;
1233 ins->opcode += unops_op_map [ins->inst_i0->type];
1236 ins->type = STACK_R8;
1237 switch (ins->inst_i0->type) {
1242 ins->opcode = OP_LCONV_TO_R_UN;
1246 case CEE_CONV_OVF_I1:
1247 case CEE_CONV_OVF_U1:
1248 case CEE_CONV_OVF_I2:
1249 case CEE_CONV_OVF_U2:
1250 case CEE_CONV_OVF_I4:
1251 case CEE_CONV_OVF_U4:
1252 ins->type = STACK_I4;
1253 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1255 case CEE_CONV_OVF_I_UN:
1256 case CEE_CONV_OVF_U_UN:
1257 ins->type = STACK_PTR;
1258 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1260 case CEE_CONV_OVF_I1_UN:
1261 case CEE_CONV_OVF_I2_UN:
1262 case CEE_CONV_OVF_I4_UN:
1263 case CEE_CONV_OVF_U1_UN:
1264 case CEE_CONV_OVF_U2_UN:
1265 case CEE_CONV_OVF_U4_UN:
1266 ins->type = STACK_I4;
1267 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1270 ins->type = STACK_PTR;
1271 switch (ins->inst_i0->type) {
1277 ins->opcode = OP_LCONV_TO_U;
1280 ins->opcode = OP_FCONV_TO_U;
1286 ins->type = STACK_I8;
1287 ins->opcode += unops_op_map [ins->inst_i0->type];
1289 case CEE_CONV_OVF_I8:
1290 case CEE_CONV_OVF_U8:
1291 ins->type = STACK_I8;
1292 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1294 case CEE_CONV_OVF_U8_UN:
1295 case CEE_CONV_OVF_I8_UN:
1296 ins->type = STACK_I8;
1297 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1301 ins->type = STACK_R8;
1302 ins->opcode += unops_op_map [ins->inst_i0->type];
1305 ins->type = STACK_R8;
1309 ins->type = STACK_I4;
1310 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1313 case CEE_CONV_OVF_I:
1314 case CEE_CONV_OVF_U:
1315 ins->type = STACK_PTR;
1316 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1319 case CEE_ADD_OVF_UN:
1321 case CEE_MUL_OVF_UN:
1323 case CEE_SUB_OVF_UN:
1324 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1325 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1328 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1335 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_MP, STACK_R8, STACK_R8, STACK_OBJ
1338 /* map ldelem.x to the matching ldind.x opcode */
1340 ldelem_to_ldind [] = {
1354 /* map stelem.x to the matching stind.x opcode */
1356 stelem_to_stind [] = {
1370 param_table [STACK_MAX] [STACK_MAX] = {
1375 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1379 switch (args->type) {
1389 for (i = 0; i < sig->param_count; ++i) {
1390 switch (args [i].type) {
1394 if (!sig->params [i]->byref)
1398 if (sig->params [i]->byref)
1400 switch (sig->params [i]->type) {
1401 case MONO_TYPE_CLASS:
1402 case MONO_TYPE_STRING:
1403 case MONO_TYPE_OBJECT:
1404 case MONO_TYPE_SZARRAY:
1405 case MONO_TYPE_ARRAY:
1412 if (sig->params [i]->byref)
1414 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1423 /*if (!param_table [args [i].type] [sig->params [i]->type])
1431 * When we need a pointer to the current domain many times in a method, we
1432 * call mono_domain_get() once and we store the result in a local variable.
1433 * This function returns the variable that represents the MonoDomain*.
1435 inline static MonoInst *
1436 mono_get_domainvar (MonoCompile *cfg)
1438 if (!cfg->domainvar)
1439 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1440 return cfg->domainvar;
1444 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
1447 int num = cfg->num_varinfo;
1449 if ((num + 1) >= cfg->varinfo_count) {
1450 cfg->varinfo_count = (cfg->varinfo_count + 2) * 2;
1451 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
1452 cfg->vars = (MonoMethodVar **)g_realloc (cfg->vars, sizeof (MonoMethodVar*) * cfg->varinfo_count);
1455 /*g_print ("created temp %d of type 0x%x\n", num, type->type);*/
1456 mono_jit_stats.allocate_var++;
1458 MONO_INST_NEW (cfg, inst, opcode);
1459 inst->inst_c0 = num;
1460 inst->inst_vtype = type;
1461 inst->klass = mono_class_from_mono_type (type);
1462 /* if set to 1 the variable is native */
1465 cfg->varinfo [num] = inst;
1467 cfg->vars [num] = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMethodVar));
1468 MONO_INIT_VARINFO (cfg->vars [num], num);
1471 //g_print ("created temp %d of type %s\n", num, mono_type_get_name (type));
1476 type_from_stack_type (MonoInst *ins) {
1477 switch (ins->type) {
1478 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1479 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1480 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1481 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1482 case STACK_MP: return &mono_defaults.int_class->byval_arg;
1483 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1484 case STACK_VTYPE: return &ins->klass->byval_arg;
1486 g_error ("stack type %d to montype not handled\n", ins->type);
1492 array_access_to_klass (int opcode)
1496 return mono_defaults.byte_class;
1498 return mono_defaults.uint16_class;
1501 return mono_defaults.int_class;
1504 return mono_defaults.sbyte_class;
1507 return mono_defaults.int16_class;
1510 return mono_defaults.int32_class;
1512 return mono_defaults.uint32_class;
1515 return mono_defaults.int64_class;
1518 return mono_defaults.single_class;
1521 return mono_defaults.double_class;
1522 case CEE_LDELEM_REF:
1523 case CEE_STELEM_REF:
1524 return mono_defaults.object_class;
1526 g_assert_not_reached ();
1532 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
1536 MONO_ADD_INS (bb, inst);
1539 switch (bb->last_ins->opcode) {
1553 while (prev->next && prev->next != bb->last_ins)
1555 if (prev == bb->code) {
1556 if (bb->last_ins == bb->code) {
1557 inst->next = bb->code;
1560 inst->next = prev->next;
1564 inst->next = bb->last_ins;
1568 // g_warning ("handle conditional jump in add_ins_to_end ()\n");
1570 MONO_ADD_INS (bb, inst);
1576 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
1578 MonoInst *inst, *load;
1580 NEW_TEMPLOAD (cfg, load, src);
1582 NEW_TEMPSTORE (cfg, inst, dest, load);
1583 if (inst->opcode == CEE_STOBJ) {
1584 NEW_TEMPLOADA (cfg, inst, dest);
1585 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE);
1587 inst->cil_code = NULL;
1588 mono_add_ins_to_end (bb, inst);
1593 * We try to share variables when possible
1596 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1601 /* inlining can result in deeper stacks */
1602 if (slot >= ((MonoMethodNormal *)cfg->method)->header->max_stack)
1603 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1605 pos = ins->type - 1 + slot * STACK_MAX;
1607 switch (ins->type) {
1614 if ((vnum = cfg->intvars [pos]))
1615 return cfg->varinfo [vnum];
1616 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1617 cfg->intvars [pos] = res->inst_c0;
1620 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1626 * This function is called to handle items that are left on the evaluation stack
1627 * at basic block boundaries. What happens is that we save the values to local variables
1628 * and we reload them later when first entering the target basic block (with the
1629 * handle_loaded_temps () function).
1630 * A single joint point will use the same variables (stored in the array bb->out_stack or
1631 * bb->in_stack, if the basic block is before or after the joint point).
1634 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count) {
1636 MonoBasicBlock *outb;
1637 MonoInst *inst, **locals;
1641 if (cfg->verbose_level > 3)
1642 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
1643 if (!bb->out_scount) {
1645 bb->out_scount = count;
1646 //g_print ("bblock %d has out:", bb->block_num);
1647 for (i = 0; i < bb->out_count; ++i) {
1648 outb = bb->out_bb [i];
1649 //g_print (" %d", outb->block_num);
1650 if (outb->in_stack) {
1652 bb->out_stack = outb->in_stack;
1658 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1659 for (i = 0; i < count; ++i) {
1661 /* try to reuse temps already allocated for this purpouse, if they occupy the same
1662 * stack slot and if they are of the same type. */
1663 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1665 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1670 locals = bb->out_stack;
1671 for (i = 0; i < count; ++i) {
1672 /* add store ops at the end of the bb, before the branch */
1673 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1674 if (inst->opcode == CEE_STOBJ) {
1675 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
1676 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE);
1678 inst->cil_code = sp [i]->cil_code;
1679 mono_add_ins_to_end (bb, inst);
1681 if (cfg->verbose_level > 3)
1682 g_print ("storing %d to temp %d\n", i, locals [i]->inst_c0);
1685 for (i = 0; i < bb->out_count; ++i) {
1686 outb = bb->out_bb [i];
1687 if (outb->in_scount)
1688 continue; /* check they are the same locals */
1689 outb->in_scount = count;
1690 outb->in_stack = locals;
1696 ret_type_to_call_opcode (MonoType *type, int calli, int virt)
1701 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1705 case MONO_TYPE_VOID:
1706 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
1709 case MONO_TYPE_BOOLEAN:
1712 case MONO_TYPE_CHAR:
1715 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1719 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1720 case MONO_TYPE_CLASS:
1721 case MONO_TYPE_STRING:
1722 case MONO_TYPE_OBJECT:
1723 case MONO_TYPE_SZARRAY:
1724 case MONO_TYPE_ARRAY:
1725 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1728 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
1731 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
1732 case MONO_TYPE_VALUETYPE:
1733 if (type->data.klass->enumtype) {
1734 t = type->data.klass->enum_basetype->type;
1737 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1738 case MONO_TYPE_TYPEDBYREF:
1739 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1740 case MONO_TYPE_GENERICINST:
1741 t = type->data.generic_inst->generic_type->type;
1744 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
1750 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
1752 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
1754 ji->ip.label = label;
1755 ji->type = MONO_PATCH_INFO_SWITCH;
1756 ji->data.table = bbs;
1757 ji->next = cfg->patch_info;
1758 ji->table_size = num_blocks;
1759 cfg->patch_info = ji;
1763 * When we add a tree of instructions, we need to ensure the instructions currently
1764 * on the stack are executed before (like, if we load a value from a local).
1765 * We ensure this by saving the currently loaded values to temps and rewriting the
1766 * instructions to load the values.
1767 * This is not done for opcodes that terminate a basic block (because it's handled already
1768 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
1771 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
1773 MonoInst *load, *store, *temp, *ins;
1775 while (stack < sp) {
1777 /* handle also other constants */
1778 if (ins->opcode != OP_ICONST) {
1779 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1780 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1781 store->cil_code = ins->cil_code;
1782 if (store->opcode == CEE_STOBJ) {
1783 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
1784 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE);
1786 MONO_ADD_INS (bblock, store);
1787 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1788 load->cil_code = ins->cil_code;
1796 * Prepare arguments for passing to a function call.
1797 * Return a non-zero value if the arguments can't be passed to the given
1799 * The type checks are not yet complete and some conversions may need
1800 * casts on 32 or 64 bit architectures.
1803 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
1808 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
1812 for (i = 0; i < sig->param_count; ++i) {
1813 if (sig->params [i]->byref) {
1815 * check the result of ldelema is only passed as an argument if the byref
1816 * type matches exactly the array element type.
1817 * FIXME: if the argument as been saved on the stack as part of the
1818 * interface variable code (the value was on the stack at a basic block boundary)
1819 * we need to add the check in that case, too.
1821 if (args [i]->opcode == CEE_LDELEMA) {
1823 MonoClass *exact_class = mono_class_from_mono_type (sig->params [i]);
1824 if (!exact_class->valuetype) {
1825 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
1826 check->cil_code = args [i]->cil_code;
1827 check->klass = exact_class;
1828 check->inst_left = args [i]->inst_left;
1829 check->type = STACK_OBJ;
1830 args [i]->inst_left = check;
1833 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
1837 simple_type = sig->params [i]->type;
1839 switch (simple_type) {
1840 case MONO_TYPE_VOID:
1845 case MONO_TYPE_BOOLEAN:
1848 case MONO_TYPE_CHAR:
1851 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
1857 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
1860 case MONO_TYPE_CLASS:
1861 case MONO_TYPE_STRING:
1862 case MONO_TYPE_OBJECT:
1863 case MONO_TYPE_SZARRAY:
1864 case MONO_TYPE_ARRAY:
1865 if (args [i]->type != STACK_OBJ)
1870 if (args [i]->type != STACK_I8)
1875 if (args [i]->type != STACK_R8)
1878 case MONO_TYPE_VALUETYPE:
1879 if (sig->params [i]->data.klass->enumtype) {
1880 simple_type = sig->params [i]->data.klass->enum_basetype->type;
1883 if (args [i]->type != STACK_VTYPE)
1886 case MONO_TYPE_TYPEDBYREF:
1887 if (args [i]->type != STACK_VTYPE)
1890 case MONO_TYPE_GENERICINST:
1891 simple_type = sig->params [i]->data.generic_inst->generic_type->type;
1895 g_error ("unknown type 0x%02x in check_call_signature", simple_type);
1902 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
1903 const guint8 *ip, gboolean to_end)
1905 MonoInst *temp, *store, *ins = (MonoInst*)call;
1906 MonoType *ret = sig->ret;
1908 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
1910 call->inst.type = STACK_OBJ;
1911 call->inst.opcode = CEE_CALL;
1912 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
1914 type_to_eval_stack_type (ret, ins);
1915 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
1918 if (MONO_TYPE_ISSTRUCT (ret)) {
1921 /* we use this to allocate native sized structs */
1922 temp->unused = sig->pinvoke;
1924 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
1925 if (call->inst.opcode == OP_VCALL)
1926 ins->inst_left = loada;
1928 ins->inst_right = loada; /* a virtual or indirect call */
1931 mono_add_ins_to_end (bblock, ins);
1933 MONO_ADD_INS (bblock, ins);
1935 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1936 store->cil_code = ip;
1938 mono_add_ins_to_end (bblock, store);
1940 MONO_ADD_INS (bblock, store);
1942 return temp->inst_c0;
1945 mono_add_ins_to_end (bblock, ins);
1947 MONO_ADD_INS (bblock, ins);
1952 inline static MonoCallInst *
1953 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1954 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
1959 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual));
1961 call->inst.cil_code = ip;
1963 call->signature = sig;
1964 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
1966 for (arg = call->out_args; arg;) {
1967 MonoInst *narg = arg->next;
1972 mono_add_ins_to_end (bblock, arg);
1974 MONO_ADD_INS (bblock, arg);
1981 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1982 MonoInst **args, MonoInst *addr, const guint8 *ip)
1984 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
1986 call->inst.inst_i0 = addr;
1988 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
1991 static MonoCallInst*
1992 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
1993 MonoInst **args, const guint8 *ip, MonoInst *this)
1995 gboolean virtual = this != NULL;
1998 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, FALSE);
2000 if (this && sig->hasthis &&
2001 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
2002 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
2003 call->method = mono_marshal_get_remoting_invoke_with_check (method);
2005 call->method = method;
2007 call->inst.flags |= MONO_INST_HAS_METHOD;
2008 call->inst.inst_left = this;
2014 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
2015 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
2017 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, signature, args, ip, this);
2019 return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
2023 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
2024 MonoInst **args, const guint8 *ip, gboolean to_end)
2030 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
2032 return mono_spill_call (cfg, bblock, call, sig, func == mono_array_new_va, ip, to_end);
2036 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
2038 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
2041 g_warning ("unregistered JIT ICall");
2042 g_assert_not_reached ();
2045 return mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, args, ip, FALSE);
2049 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
2051 MonoInst *ins, *temp = NULL, *store, *load, *begin;
2052 MonoInst *last_arg = NULL;
2056 //g_print ("emulating: ");
2057 //mono_print_tree_nl (tree);
2058 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE));
2059 ins = (MonoInst*)call;
2061 call->inst.cil_code = tree->cil_code;
2063 call->signature = info->sig;
2065 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
2067 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2068 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
2069 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2070 store->cil_code = tree->cil_code;
2075 nargs = info->sig->param_count + info->sig->hasthis;
2077 for (last_arg = call->out_args; last_arg && last_arg->next; last_arg = last_arg->next) ;
2080 last_arg->next = store;
2083 begin = call->out_args;
2087 if (cfg->prev_ins) {
2089 * This assumes that that in a tree, emulate_opcode is called for a
2090 * node before it is called for its children. dec_foreach needs to
2091 * take this into account.
2093 store->next = cfg->prev_ins->next;
2094 cfg->prev_ins->next = begin;
2096 store->next = cfg->cbb->code;
2097 cfg->cbb->code = begin;
2100 call->fptr = mono_icall_get_wrapper (info);
2102 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2103 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2108 static MonoMethodSignature *
2109 mono_get_element_address_signature (int arity)
2111 static GHashTable *sighash = NULL;
2112 MonoMethodSignature *res;
2115 EnterCriticalSection (&trampoline_hash_mutex);
2117 sighash = g_hash_table_new (NULL, NULL);
2119 else if ((res = g_hash_table_lookup (sighash, (gpointer)arity))) {
2120 LeaveCriticalSection (&trampoline_hash_mutex);
2124 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
2127 res->params [0] = &mono_defaults.array_class->byval_arg;
2129 for (i = 1; i <= arity; i++)
2130 res->params [i] = &mono_defaults.int_class->byval_arg;
2132 res->ret = &mono_defaults.int_class->byval_arg;
2134 g_hash_table_insert (sighash, (gpointer)arity, res);
2135 LeaveCriticalSection (&trampoline_hash_mutex);
2141 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native) {
2142 MonoInst *iargs [3];
2148 * This check breaks with spilled vars... need to handle it during verification anyway.
2149 * g_assert (klass && klass == src->klass && klass == dest->klass);
2153 n = mono_class_native_size (klass, &align);
2155 n = mono_class_value_size (klass, &align);
2157 if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
2159 MONO_INST_NEW (cfg, inst, OP_MEMCPY);
2160 inst->inst_left = dest;
2161 inst->inst_right = src;
2162 inst->cil_code = ip;
2164 MONO_ADD_INS (bblock, inst);
2169 NEW_ICONST (cfg, iargs [2], n);
2171 mono_emit_native_call (cfg, bblock, helper_memcpy, helper_sig_memcpy, iargs, ip, to_end);
2175 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
2177 MonoInst *iargs [2];
2178 MonoInst *ins, *zero_int32;
2181 NEW_ICONST (cfg, zero_int32, 0);
2183 mono_class_init (klass);
2184 n = mono_class_value_size (klass, NULL);
2185 MONO_INST_NEW (cfg, ins, 0);
2187 ins->inst_left = dest;
2188 ins->inst_right = zero_int32;
2191 ins->opcode = CEE_STIND_I1;
2192 MONO_ADD_INS (bblock, ins);
2195 ins->opcode = CEE_STIND_I2;
2196 MONO_ADD_INS (bblock, ins);
2199 ins->opcode = CEE_STIND_I4;
2200 MONO_ADD_INS (bblock, ins);
2203 if (n <= sizeof (gpointer) * 5) {
2204 ins->opcode = OP_MEMSET;
2207 MONO_ADD_INS (bblock, ins);
2210 handle_loaded_temps (cfg, bblock, stack_start, sp);
2211 NEW_ICONST (cfg, ins, n);
2214 mono_emit_jit_icall (cfg, bblock, helper_initobj, iargs, ip);
2219 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
2222 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
2224 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
2225 MonoMethodSignature *signature = method->signature;
2229 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2230 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2231 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
2232 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
2233 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2234 (method->klass->marshalbyref) ||
2235 !header || header->num_clauses ||
2236 /* fixme: why cant we inline valuetype returns? */
2237 MONO_TYPE_ISSTRUCT (signature->ret))
2240 /* its not worth to inline methods with valuetype arguments?? */
2241 for (i = 0; i < signature->param_count; i++) {
2242 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
2248 * if we can initialize the class of the method right away, we do,
2249 * otherwise we don't allow inlining if the class needs initialization,
2250 * since it would mean inserting a call to mono_runtime_class_init()
2251 * inside the inlined code
2253 if (!(cfg->opt & MONO_OPT_SHARED)) {
2254 vtable = mono_class_vtable (cfg->domain, method->klass);
2255 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
2256 if (cfg->run_cctors)
2257 mono_runtime_class_init (vtable);
2259 else if (!vtable->initialized && mono_class_needs_cctor_run (method->klass, NULL))
2263 * If we're compiling for shared code
2264 * the cctor will need to be run at aot method load time, for example,
2265 * or at the end of the compilation of the inlining method.
2267 if (mono_class_needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
2270 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
2272 /* also consider num_locals? */
2273 if (getenv ("MONO_INLINELIMIT"))
2274 if (header->code_size < atoi (getenv ("MONO_INLINELIMIT"))) {
2278 if (header->code_size < 20)
2285 mini_get_ldelema_ins (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
2289 MonoMethodSignature *esig;
2290 char icall_name [256];
2291 MonoJitICallInfo *info;
2293 rank = cmethod->signature->param_count - (is_set? 1: 0);
2295 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
2297 NEW_GROUP (cfg, indexes, sp [1], sp [2]);
2298 MONO_INST_NEW (cfg, addr, OP_LDELEMA2D);
2299 addr->inst_left = sp [0];
2300 addr->inst_right = indexes;
2301 addr->cil_code = ip;
2302 addr->type = STACK_MP;
2303 addr->klass = cmethod->klass;
2307 /* Need to register the icall so it gets an icall wrapper */
2308 sprintf (icall_name, "ves_array_element_address_%d", rank);
2310 info = mono_find_jit_icall_by_name (icall_name);
2312 esig = mono_get_element_address_signature (rank);
2313 info = mono_register_jit_icall (ves_array_element_address, g_strdup (icall_name), esig, FALSE);
2316 temp = mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, sp, ip, FALSE);
2318 NEW_TEMPLOAD (cfg, addr, temp);
2322 static MonoJitICallInfo **emul_opcode_map = NULL;
2324 static inline MonoJitICallInfo *
2325 mono_find_jit_opcode_emulation (int opcode)
2327 if (emul_opcode_map)
2328 return emul_opcode_map [opcode];
2334 mini_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
2339 if (cmethod->klass == mono_defaults.string_class) {
2340 if (cmethod->name [0] != 'g')
2343 if (strcmp (cmethod->name, "get_Chars") == 0)
2345 else if (strcmp (cmethod->name, "get_Length") == 0)
2348 } else if (cmethod->klass == mono_defaults.object_class) {
2349 if (strcmp (cmethod->name, "GetType") == 0)
2353 } else if (cmethod->klass == mono_defaults.array_class) {
2354 if (strcmp (cmethod->name, "get_Rank") == 0)
2356 else if (strcmp (cmethod->name, "get_Length") == 0)
2361 op = mono_arch_get_opcode_for_method (cfg, cmethod, fsig, args);
2365 pc = fsig->param_count + fsig->hasthis;
2366 MONO_INST_NEW (cfg, ins, op);
2369 ins->inst_i0 = args [0];
2371 ins->inst_i1 = args [1];
2378 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
2380 MonoInst *store, *temp;
2383 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
2385 if (!sig->hasthis && sig->param_count == 0)
2389 if (sp [0]->opcode == OP_ICONST) {
2392 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
2394 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2395 store->cil_code = sp [0]->cil_code;
2396 MONO_ADD_INS (bblock, store);
2401 for (i = 0; i < sig->param_count; ++i) {
2402 if (sp [0]->opcode == OP_ICONST) {
2405 temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
2407 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2408 store->cil_code = sp [0]->cil_code;
2409 if (store->opcode == CEE_STOBJ) {
2410 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2411 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE);
2413 MONO_ADD_INS (bblock, store);
2421 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
2422 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b)
2424 MonoInst *ins, *rvar = NULL;
2425 MonoMethodHeader *cheader;
2426 MonoBasicBlock *ebblock, *sbblock;
2427 int i, costs, new_locals_offset;
2429 if (cfg->verbose_level > 2)
2430 g_print ("INLINE START %p %s -> %s\n", cmethod, mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2432 cheader = ((MonoMethodNormal *)cmethod)->header;
2434 if (!cmethod->inline_info) {
2435 mono_jit_stats.inlineable_methods++;
2436 cmethod->inline_info = 1;
2438 /* allocate space to store the return value */
2439 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2440 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2443 /* allocate local variables */
2444 new_locals_offset = cfg->num_varinfo;
2445 for (i = 0; i < cheader->num_locals; ++i)
2446 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
2448 /* allocate starte and end blocks */
2449 sbblock = NEW_BBLOCK (cfg);
2450 sbblock->block_num = cfg->num_bblocks++;
2451 sbblock->real_offset = real_offset;
2453 ebblock = NEW_BBLOCK (cfg);
2454 ebblock->block_num = cfg->num_bblocks++;
2455 ebblock->real_offset = real_offset;
2457 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
2459 if (costs >= 0 && costs < 60) {
2460 if (cfg->verbose_level > 2)
2461 g_print ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2463 mono_jit_stats.inlined_methods++;
2465 /* always add some code to avoid block split failures */
2466 MONO_INST_NEW (cfg, ins, CEE_NOP);
2467 MONO_ADD_INS (bblock, ins);
2470 bblock->next_bb = sbblock;
2471 link_bblock (cfg, bblock, sbblock);
2474 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
2480 if (cfg->verbose_level > 2)
2481 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
2487 * Some of these comments may well be out-of-date.
2488 * Design decisions: we do a single pass over the IL code (and we do bblock
2489 * splitting/merging in the few cases when it's required: a back jump to an IL
2490 * address that was not already seen as bblock starting point).
2491 * Code is validated as we go (full verification is still better left to metadata/verify.c).
2492 * Complex operations are decomposed in simpler ones right away. We need to let the
2493 * arch-specific code peek and poke inside this process somehow (except when the
2494 * optimizations can take advantage of the full semantic info of coarse opcodes).
2495 * All the opcodes of the form opcode.s are 'normalized' to opcode.
2496 * MonoInst->opcode initially is the IL opcode or some simplification of that
2497 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
2498 * opcode with value bigger than OP_LAST.
2499 * At this point the IR can be handed over to an interpreter, a dumb code generator
2500 * or to the optimizing code generator that will translate it to SSA form.
2502 * Profiling directed optimizations.
2503 * We may compile by default with few or no optimizations and instrument the code
2504 * or the user may indicate what methods to optimize the most either in a config file
2505 * or through repeated runs where the compiler applies offline the optimizations to
2506 * each method and then decides if it was worth it.
2509 * * consider using an array instead of an hash table (bb_hash)
2512 #define CHECK_TYPE(ins) if (!(ins)->type) goto unverified
2513 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) goto unverified
2514 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) goto unverified
2515 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) goto unverified
2516 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) goto unverified
2517 #define CHECK_OPSIZE(size) if (ip + size > end) goto unverified
2519 #define TYPE_PARAM_TO_TYPE(num) (generic_context->ginst->type_argv [(num)])
2520 #define TYPE_PARAM_TO_CLASS(num) (mono_class_from_mono_type (TYPE_PARAM_TO_TYPE ((num))))
2522 #define MTYPE_PARAM_TO_TYPE(num) (generic_context->gmethod->mtype_argv [(num)])
2523 #define MTYPE_PARAM_TO_CLASS(num) (mono_class_from_mono_type (MTYPE_PARAM_TO_TYPE ((num))))
2526 /* offset from br.s -> br like opcodes */
2527 #define BIG_BRANCH_OFFSET 13
2530 get_basic_blocks (MonoCompile *cfg, GHashTable *bbhash, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
2532 unsigned char *ip = start;
2533 unsigned char *target;
2536 MonoBasicBlock *bblock;
2537 const MonoOpcode *opcode;
2540 cli_addr = ip - start;
2541 i = mono_opcode_value ((const guint8 **)&ip);
2542 opcode = &mono_opcodes [i];
2543 switch (opcode->argument) {
2544 case MonoInlineNone:
2547 case MonoInlineString:
2548 case MonoInlineType:
2549 case MonoInlineField:
2550 case MonoInlineMethod:
2553 case MonoShortInlineR:
2560 case MonoShortInlineVar:
2561 case MonoShortInlineI:
2564 case MonoShortInlineBrTarget:
2565 target = start + cli_addr + 2 + (signed char)ip [1];
2566 GET_BBLOCK (cfg, bbhash, bblock, target);
2569 case MonoInlineBrTarget:
2570 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
2571 GET_BBLOCK (cfg, bbhash, bblock, target);
2574 case MonoInlineSwitch: {
2575 guint32 n = read32 (ip + 1);
2578 cli_addr += 5 + 4 * n;
2579 target = start + cli_addr;
2580 GET_BBLOCK (cfg, bbhash, bblock, target);
2582 for (j = 0; j < n; ++j) {
2583 target = start + cli_addr + (gint32)read32 (ip);
2584 GET_BBLOCK (cfg, bbhash, bblock, target);
2594 g_assert_not_reached ();
2604 * mono_method_to_ir: translates IL into basic blocks containing trees
2607 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
2608 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
2609 guint inline_offset, gboolean is_virtual_call)
2611 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
2612 MonoInst *ins, **sp, **stack_start;
2613 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
2615 MonoMethod *cmethod;
2616 MonoInst **arg_array;
2617 MonoMethodHeader *header;
2619 guint32 token, ins_flag;
2621 unsigned char *ip, *end, *target, *err_pos;
2622 static double r8_0 = 0.0;
2623 MonoMethodSignature *sig;
2624 MonoGenericContext *generic_context = NULL;
2625 MonoType **param_types;
2626 GList *bb_recheck = NULL, *tmp;
2627 int i, n, start_new_bblock, align;
2628 int num_calls = 0, inline_costs = 0;
2629 int *filter_lengths = NULL;
2630 int breakpoint_id = 0;
2631 guint real_offset, num_args;
2633 image = method->klass->image;
2634 header = ((MonoMethodNormal *)method)->header;
2635 sig = method->signature;
2636 num_args = sig->hasthis + sig->param_count;
2637 ip = (unsigned char*)header->code;
2638 end = ip + header->code_size;
2639 mono_jit_stats.cil_code_size += header->code_size;
2641 if (method->signature->is_inflated)
2642 generic_context = ((MonoMethodInflated *) method)->context;
2644 if (cfg->method == method) {
2646 bbhash = cfg->bb_hash;
2648 real_offset = inline_offset;
2649 bbhash = g_hash_table_new (g_direct_hash, NULL);
2652 if (cfg->verbose_level > 2)
2653 g_print ("method to IR %s\n", mono_method_full_name (method, TRUE));
2655 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
2656 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
2658 dont_inline = g_list_prepend (dont_inline, method);
2659 if (cfg->method == method) {
2662 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
2663 start_bblock->cil_code = NULL;
2664 start_bblock->cil_length = 0;
2665 start_bblock->block_num = cfg->num_bblocks++;
2668 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
2669 end_bblock->cil_code = NULL;
2670 end_bblock->cil_length = 0;
2671 end_bblock->block_num = cfg->num_bblocks++;
2672 g_assert (cfg->num_bblocks == 2);
2674 arg_array = alloca (sizeof (MonoInst *) * num_args);
2675 for (i = num_args - 1; i >= 0; i--)
2676 arg_array [i] = cfg->varinfo [i];
2678 if (header->num_clauses) {
2679 int size = sizeof (int) * header->num_clauses;
2680 filter_lengths = alloca (size);
2681 memset (filter_lengths, 0, size);
2683 cfg->spvars = g_hash_table_new (NULL, NULL);
2685 /* handle exception clauses */
2686 for (i = 0; i < header->num_clauses; ++i) {
2687 //unsigned char *p = ip;
2688 MonoExceptionClause *clause = &header->clauses [i];
2689 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->try_offset);
2690 tblock->real_offset = clause->try_offset;
2691 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
2692 tblock->real_offset = clause->handler_offset;
2694 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
2695 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2696 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2697 MONO_ADD_INS (tblock, ins);
2700 /*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);
2702 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
2704 /* catch and filter blocks get the exception object on the stack */
2705 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
2706 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2707 /* mostly like handle_stack_args (), but just sets the input args */
2708 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
2710 cfg->exvar = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
2711 /* prevent it from being register allocated */
2712 cfg->exvar->flags |= MONO_INST_INDIRECT;
2714 tblock->in_scount = 1;
2715 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2716 tblock->in_stack [0] = cfg->exvar;
2718 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2719 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->token_or_filter);
2720 tblock->real_offset = clause->token_or_filter;
2721 tblock->in_scount = 1;
2722 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2723 tblock->in_stack [0] = cfg->exvar;
2724 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2725 MONO_ADD_INS (tblock, ins);
2731 arg_array = alloca (sizeof (MonoInst *) * num_args);
2732 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
2735 /* FIRST CODE BLOCK */
2736 bblock = NEW_BBLOCK (cfg);
2737 bblock->cil_code = ip;
2739 ADD_BBLOCK (cfg, bbhash, bblock);
2741 if (cfg->method == method) {
2742 breakpoint_id = mono_debugger_method_has_breakpoint (method);
2743 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
2744 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2745 MONO_ADD_INS (bblock, ins);
2749 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED))) || mono_compile_aot) {
2750 /* we use a separate basic block for the initialization code */
2751 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
2752 init_localsbb->real_offset = real_offset;
2753 start_bblock->next_bb = init_localsbb;
2754 init_localsbb->next_bb = bblock;
2755 link_bblock (cfg, start_bblock, init_localsbb);
2756 link_bblock (cfg, init_localsbb, bblock);
2757 init_localsbb->block_num = cfg->num_bblocks++;
2759 start_bblock->next_bb = bblock;
2760 link_bblock (cfg, start_bblock, bblock);
2763 if (get_basic_blocks (cfg, bbhash, header, real_offset, ip, end, &err_pos)) {
2768 mono_debug_init_method (cfg, bblock, breakpoint_id);
2770 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
2772 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
2773 for (n = 0; n < sig->param_count; ++n)
2774 param_types [n + sig->hasthis] = sig->params [n];
2776 /* do this somewhere outside - not here */
2777 NEW_ICONST (cfg, zero_int32, 0);
2778 NEW_ICONST (cfg, zero_int64, 0);
2779 zero_int64->type = STACK_I8;
2780 NEW_PCONST (cfg, zero_ptr, 0);
2781 NEW_PCONST (cfg, zero_obj, 0);
2782 zero_obj->type = STACK_OBJ;
2784 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
2785 zero_r8->type = STACK_R8;
2786 zero_r8->inst_p0 = &r8_0;
2788 /* add a check for this != NULL to inlined methods */
2789 if (is_virtual_call) {
2790 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
2791 NEW_ARGLOAD (cfg, ins->inst_left, 0);
2793 MONO_ADD_INS (bblock, ins);
2796 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
2797 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
2800 start_new_bblock = 0;
2803 if (cfg->method == method)
2804 real_offset = ip - header->code;
2806 real_offset = inline_offset;
2808 if (start_new_bblock) {
2809 bblock->cil_length = ip - bblock->cil_code;
2810 if (start_new_bblock == 2) {
2811 g_assert (ip == tblock->cil_code);
2813 GET_BBLOCK (cfg, bbhash, tblock, ip);
2815 bblock->next_bb = tblock;
2817 start_new_bblock = 0;
2818 for (i = 0; i < bblock->in_scount; ++i) {
2819 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2823 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
2824 link_bblock (cfg, bblock, tblock);
2825 if (sp != stack_start) {
2826 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2829 bblock->next_bb = tblock;
2831 for (i = 0; i < bblock->in_scount; ++i) {
2832 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2838 if (cfg->coverage_info) {
2839 MonoInst *store, *one;
2840 guint32 cil_offset = ip - header->code;
2841 cfg->coverage_info->data [cil_offset].cil_code = ip;
2843 /* TODO: Use an increment here */
2844 NEW_ICONST (cfg, one, 1);
2847 NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
2850 MONO_INST_NEW (cfg, store, CEE_STIND_I);
2851 store->cil_code = ip;
2852 store->inst_left = ins;
2853 store->inst_right = one;
2855 MONO_ADD_INS (bblock, store);
2858 if (cfg->verbose_level > 3)
2859 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, sp-stack_start, mono_disasm_code_one (NULL, method, ip, NULL));
2861 /* Workaround for bug #51126 */
2862 bblock->real_offset = real_offset;
2869 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2870 ins->cil_code = ip++;
2871 MONO_ADD_INS (bblock, ins);
2877 CHECK_STACK_OVF (1);
2878 n = (*ip)-CEE_LDARG_0;
2880 NEW_ARGLOAD (cfg, ins, n);
2881 ins->cil_code = ip++;
2888 CHECK_STACK_OVF (1);
2889 n = (*ip)-CEE_LDLOC_0;
2891 NEW_LOCLOAD (cfg, ins, n);
2892 ins->cil_code = ip++;
2900 n = (*ip)-CEE_STLOC_0;
2903 handle_loaded_temps (cfg, bblock, stack_start, sp);
2904 NEW_LOCSTORE (cfg, ins, n, *sp);
2906 if (ins->opcode == CEE_STOBJ) {
2907 NEW_LOCLOADA (cfg, ins, n);
2908 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2910 MONO_ADD_INS (bblock, ins);
2916 CHECK_STACK_OVF (1);
2918 NEW_ARGLOAD (cfg, ins, ip [1]);
2925 CHECK_STACK_OVF (1);
2927 NEW_ARGLOADA (cfg, ins, ip [1]);
2937 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
2938 handle_loaded_temps (cfg, bblock, stack_start, sp);
2940 if (ins->opcode == CEE_STOBJ) {
2941 NEW_ARGLOADA (cfg, ins, ip [1]);
2942 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2944 MONO_ADD_INS (bblock, ins);
2949 CHECK_STACK_OVF (1);
2950 CHECK_LOCAL (ip [1]);
2951 NEW_LOCLOAD (cfg, ins, ip [1]);
2958 CHECK_STACK_OVF (1);
2959 CHECK_LOCAL (ip [1]);
2960 NEW_LOCLOADA (cfg, ins, ip [1]);
2969 handle_loaded_temps (cfg, bblock, stack_start, sp);
2970 CHECK_LOCAL (ip [1]);
2971 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
2973 if (ins->opcode == CEE_STOBJ) {
2974 NEW_LOCLOADA (cfg, ins, ip [1]);
2975 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2977 MONO_ADD_INS (bblock, ins);
2982 CHECK_STACK_OVF (1);
2983 NEW_PCONST (cfg, ins, NULL);
2985 ins->type = STACK_OBJ;
2990 CHECK_STACK_OVF (1);
2991 NEW_ICONST (cfg, ins, -1);
3005 CHECK_STACK_OVF (1);
3006 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
3013 CHECK_STACK_OVF (1);
3015 NEW_ICONST (cfg, ins, *((signed char*)ip));
3022 CHECK_STACK_OVF (1);
3023 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
3030 CHECK_STACK_OVF (1);
3031 MONO_INST_NEW (cfg, ins, OP_I8CONST);
3033 ins->type = STACK_I8;
3035 ins->inst_l = (gint64)read64 (ip);
3040 float *f = g_malloc (sizeof (float));
3042 CHECK_STACK_OVF (1);
3043 MONO_INST_NEW (cfg, ins, OP_R4CONST);
3044 ins->type = STACK_R8;
3053 double *d = g_malloc (sizeof (double));
3055 CHECK_STACK_OVF (1);
3056 MONO_INST_NEW (cfg, ins, OP_R8CONST);
3057 ins->type = STACK_R8;
3066 MonoInst *temp, *store;
3068 CHECK_STACK_OVF (1);
3073 * small optimization: if the loaded value was from a local already,
3074 * just load it twice.
3076 if (ins->ssa_op == MONO_SSA_LOAD &&
3077 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
3079 MONO_INST_NEW (cfg, temp, 0);
3081 temp->cil_code = ip;
3084 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3085 temp->cil_code = ip;
3086 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3087 store->cil_code = ip;
3088 MONO_ADD_INS (bblock, store);
3089 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3092 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3102 MONO_INST_NEW (cfg, ins, CEE_POP);
3103 MONO_ADD_INS (bblock, ins);
3104 ins->cil_code = ip++;
3110 if (stack_start != sp)
3112 MONO_INST_NEW (cfg, ins, CEE_JMP);
3113 token = read32 (ip + 1);
3114 /* FIXME: check the signature matches */
3115 cmethod = mono_get_method_full (image, token, generic_context);
3116 ins->inst_p0 = cmethod;
3117 MONO_ADD_INS (bblock, ins);
3119 start_new_bblock = 1;
3123 case CEE_CALLVIRT: {
3124 MonoInst *addr = NULL;
3125 MonoMethodSignature *fsig = NULL;
3126 int temp, array_rank = 0;
3127 int virtual = *ip == CEE_CALLVIRT;
3130 token = read32 (ip + 1);
3132 if (*ip == CEE_CALLI) {
3137 if (method->wrapper_type != MONO_WRAPPER_NONE)
3138 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
3140 fsig = mono_metadata_parse_signature (image, token);
3142 n = fsig->param_count + fsig->hasthis;
3144 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3145 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
3147 cmethod = mono_get_method_full (image, token, generic_context);
3152 if (!cmethod->klass->inited)
3153 mono_class_init (cmethod->klass);
3155 if (cmethod->signature->pinvoke) {
3156 #ifdef MONO_USE_EXC_TABLES
3157 if (mono_method_blittable (cmethod)) {
3158 fsig = cmethod->signature;
3161 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
3162 fsig = wrapper->signature;
3163 #ifdef MONO_USE_EXC_TABLES
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 (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
3191 if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL) && (mono_metadata_signature_equal (method->signature, cmethod->signature))) {
3193 /* FIXME: This assumes the two methods has the same number and type of arguments */
3194 for (i = 0; i < n; ++i) {
3195 /* Check if argument is the same */
3196 NEW_ARGLOAD (cfg, ins, i);
3197 if ((ins->opcode == sp [i]->opcode) && (ins->inst_i0 == sp [i]->inst_i0))
3200 /* Prevent argument from being register allocated */
3201 arg_array [i]->flags |= MONO_INST_VOLATILE;
3202 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3204 if (ins->opcode == CEE_STOBJ) {
3205 NEW_ARGLOADA (cfg, ins, i);
3206 handle_stobj (cfg, bblock, ins, sp [i], sp [i]->cil_code, ins->klass, FALSE, FALSE);
3209 MONO_ADD_INS (bblock, ins);
3211 MONO_INST_NEW (cfg, ins, CEE_JMP);
3213 ins->inst_p0 = cmethod;
3214 ins->inst_p1 = arg_array [0];
3215 MONO_ADD_INS (bblock, ins);
3216 start_new_bblock = 1;
3217 /* skip CEE_RET as well */
3222 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_opcode_for_method (cfg, cmethod, fsig, sp))) {
3225 if (MONO_TYPE_IS_VOID (fsig->ret)) {
3226 MONO_ADD_INS (bblock, ins);
3228 type_to_eval_stack_type (fsig->ret, ins);
3237 handle_loaded_temps (cfg, bblock, stack_start, sp);
3239 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3240 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
3241 mono_method_check_inlining (cfg, cmethod) &&
3242 !g_list_find (dont_inline, cmethod)) {
3244 MonoBasicBlock *ebblock;
3246 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3250 GET_BBLOCK (cfg, bbhash, bblock, ip);
3251 ebblock->next_bb = bblock;
3252 link_bblock (cfg, ebblock, bblock);
3254 if (!MONO_TYPE_IS_VOID (fsig->ret))
3257 /* indicates start of a new block, and triggers a load of all
3258 stack arguments at bb boundarie */
3261 inline_costs += costs;
3266 inline_costs += 10 * num_calls++;
3268 /* tail recursion elimination */
3269 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == method && ip [5] == CEE_RET) {
3270 gboolean has_vtargs = FALSE;
3273 /* keep it simple */
3274 for (i = fsig->param_count - 1; i >= 0; i--) {
3275 if (MONO_TYPE_ISSTRUCT (cmethod->signature->params [i]))
3280 for (i = 0; i < n; ++i) {
3281 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3283 MONO_ADD_INS (bblock, ins);
3285 MONO_INST_NEW (cfg, ins, CEE_BR);
3287 MONO_ADD_INS (bblock, ins);
3288 tblock = start_bblock->out_bb [0];
3289 link_bblock (cfg, bblock, tblock);
3290 ins->inst_target_bb = tblock;
3291 start_new_bblock = 1;
3294 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3295 /* just create a dummy - the value is never used */
3296 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
3297 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3305 if (*ip == CEE_CALLI) {
3307 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
3308 NEW_TEMPLOAD (cfg, *sp, temp);
3312 } else if (array_rank) {
3315 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
3316 if (sp [fsig->param_count]->type == STACK_OBJ) {
3317 MonoInst *iargs [2];
3319 handle_loaded_temps (cfg, bblock, stack_start, sp);
3322 iargs [1] = sp [fsig->param_count];
3324 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref_check, iargs, ip);
3327 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
3328 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
3330 if (ins->opcode == CEE_STOBJ) {
3331 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
3333 MONO_ADD_INS (bblock, ins);
3336 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
3337 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3338 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
3342 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
3343 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3346 g_assert_not_reached ();
3350 if (0 && CODE_IS_STLOC (ip + 5) && (!MONO_TYPE_ISSTRUCT (fsig->ret)) && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)) {
3351 /* no need to spill */
3352 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
3355 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) {
3356 NEW_TEMPLOAD (cfg, *sp, temp);
3366 if (cfg->method != method) {
3367 /* return from inlined methode */
3372 //g_assert (returnvar != -1);
3373 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
3374 store->cil_code = sp [0]->cil_code;
3375 if (store->opcode == CEE_STOBJ) {
3376 g_assert_not_reached ();
3377 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
3378 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
3380 MONO_ADD_INS (bblock, store);
3384 g_assert (!return_var);
3387 MONO_INST_NEW (cfg, ins, CEE_NOP);
3388 ins->opcode = mono_type_to_stind (method->signature->ret);
3389 if (ins->opcode == CEE_STOBJ) {
3390 NEW_RETLOADA (cfg, ins);
3391 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3393 ins->opcode = OP_SETRET;
3395 ins->inst_i0 = *sp;;
3396 ins->inst_i1 = NULL;
3397 MONO_ADD_INS (bblock, ins);
3401 if (sp != stack_start)
3403 MONO_INST_NEW (cfg, ins, CEE_BR);
3404 ins->cil_code = ip++;
3405 ins->inst_target_bb = end_bblock;
3406 MONO_ADD_INS (bblock, ins);
3407 link_bblock (cfg, bblock, end_bblock);
3408 start_new_bblock = 1;
3412 MONO_INST_NEW (cfg, ins, CEE_BR);
3413 ins->cil_code = ip++;
3414 MONO_ADD_INS (bblock, ins);
3415 target = ip + 1 + (signed char)(*ip);
3417 GET_BBLOCK (cfg, bbhash, tblock, target);
3418 link_bblock (cfg, bblock, tblock);
3419 CHECK_BBLOCK (target, ip, tblock);
3420 ins->inst_target_bb = tblock;
3421 if (sp != stack_start) {
3422 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3425 start_new_bblock = 1;
3432 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3433 ins->cil_code = ip++;
3434 target = ip + 1 + *(signed char*)ip;
3436 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
3437 if (sp != stack_start) {
3438 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3455 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3456 ins->cil_code = ip++;
3457 target = ip + 1 + *(signed char*)ip;
3460 if (sp != stack_start) {
3461 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3468 MONO_INST_NEW (cfg, ins, CEE_BR);
3469 ins->cil_code = ip++;
3470 MONO_ADD_INS (bblock, ins);
3471 target = ip + 4 + (gint32)read32(ip);
3473 GET_BBLOCK (cfg, bbhash, tblock, target);
3474 link_bblock (cfg, bblock, tblock);
3475 CHECK_BBLOCK (target, ip, tblock);
3476 ins->inst_target_bb = tblock;
3477 if (sp != stack_start) {
3478 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3481 start_new_bblock = 1;
3488 MONO_INST_NEW (cfg, ins, *ip);
3489 ins->cil_code = ip++;
3490 target = ip + 4 + (gint32)read32(ip);
3492 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
3493 if (sp != stack_start) {
3494 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3511 MONO_INST_NEW (cfg, ins, *ip);
3512 ins->cil_code = ip++;
3513 target = ip + 4 + (gint32)read32(ip);
3516 if (sp != stack_start) {
3517 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3525 n = read32 (ip + 1);
3526 MONO_INST_NEW (cfg, ins, *ip);
3528 ins->inst_left = *sp;
3529 if (ins->inst_left->type != STACK_I4) goto unverified;
3532 CHECK_OPSIZE (n * sizeof (guint32));
3533 target = ip + n * sizeof (guint32);
3534 MONO_ADD_INS (bblock, ins);
3535 GET_BBLOCK (cfg, bbhash, tblock, target);
3536 link_bblock (cfg, bblock, tblock);
3537 ins->klass = GUINT_TO_POINTER (n);
3538 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
3539 ins->inst_many_bb [n] = tblock;
3541 for (i = 0; i < n; ++i) {
3542 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
3543 link_bblock (cfg, bblock, tblock);
3544 ins->inst_many_bb [i] = tblock;
3547 if (sp != stack_start) {
3548 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3565 MONO_INST_NEW (cfg, ins, *ip);
3570 ins->type = ldind_type [*ip - CEE_LDIND_I1];
3571 ins->flags |= ins_flag;
3583 MONO_INST_NEW (cfg, ins, *ip);
3584 ins->cil_code = ip++;
3586 handle_loaded_temps (cfg, bblock, stack_start, sp);
3587 MONO_ADD_INS (bblock, ins);
3588 ins->inst_i0 = sp [0];
3589 ins->inst_i1 = sp [1];
3590 ins->flags |= ins_flag;
3609 if (mono_find_jit_opcode_emulation (ins->opcode)) {
3610 MonoInst *store, *temp, *load;
3612 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3613 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3614 store->cil_code = ins->cil_code;
3615 MONO_ADD_INS (bblock, store);
3616 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
3617 load->cil_code = ins->cil_code;
3619 /*g_print ("found emulation for %d\n", ins->opcode);*/
3633 case CEE_CONV_OVF_I8:
3634 case CEE_CONV_OVF_U8:
3638 if (mono_find_jit_opcode_emulation (ins->opcode)) {
3639 MonoInst *store, *temp, *load;
3641 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3642 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3643 store->cil_code = ins->cil_code;
3644 MONO_ADD_INS (bblock, store);
3645 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
3646 load->cil_code = ins->cil_code;
3648 /*g_print ("found emulation for %d\n", ins->opcode);*/
3652 case CEE_CONV_OVF_I4:
3653 case CEE_CONV_OVF_I1:
3654 case CEE_CONV_OVF_I2:
3655 case CEE_CONV_OVF_I:
3656 case CEE_CONV_OVF_U:
3659 if (sp [-1]->type == STACK_R8) {
3660 ADD_UNOP (CEE_CONV_OVF_I8);
3668 case CEE_CONV_OVF_U1:
3669 case CEE_CONV_OVF_U2:
3670 case CEE_CONV_OVF_U4:
3673 if (sp [-1]->type == STACK_R8) {
3674 ADD_UNOP (CEE_CONV_OVF_U8);
3682 case CEE_CONV_OVF_I1_UN:
3683 case CEE_CONV_OVF_I2_UN:
3684 case CEE_CONV_OVF_I4_UN:
3685 case CEE_CONV_OVF_I8_UN:
3686 case CEE_CONV_OVF_U1_UN:
3687 case CEE_CONV_OVF_U2_UN:
3688 case CEE_CONV_OVF_U4_UN:
3689 case CEE_CONV_OVF_U8_UN:
3690 case CEE_CONV_OVF_I_UN:
3691 case CEE_CONV_OVF_U_UN:
3699 token = read32 (ip + 1);
3700 if (method->wrapper_type != MONO_WRAPPER_NONE)
3701 klass = mono_method_get_wrapper_data (method, token);
3703 klass = mono_class_get_full (image, token, generic_context);
3705 mono_class_init (klass);
3706 if (klass->byval_arg.type == MONO_TYPE_VAR)
3707 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3708 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
3709 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3711 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3712 MonoInst *store, *load;
3713 MONO_INST_NEW (cfg, load, CEE_LDIND_REF);
3714 load->cil_code = ip;
3715 load->inst_i0 = sp [1];
3716 load->type = ldind_type [CEE_LDIND_REF];
3717 load->flags |= ins_flag;
3718 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
3719 store->cil_code = ip;
3720 handle_loaded_temps (cfg, bblock, stack_start, sp);
3721 MONO_ADD_INS (bblock, store);
3722 store->inst_i0 = sp [0];
3723 store->inst_i1 = load;
3724 store->flags |= ins_flag;
3726 n = mono_class_value_size (klass, NULL);
3727 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3729 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3730 copy->inst_left = sp [0];
3731 copy->inst_right = sp [1];
3732 copy->cil_code = ip;
3734 MONO_ADD_INS (bblock, copy);
3736 MonoInst *iargs [3];
3739 NEW_ICONST (cfg, iargs [2], n);
3740 iargs [2]->cil_code = ip;
3742 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3749 MonoInst *iargs [3];
3753 token = read32 (ip + 1);
3754 if (method->wrapper_type != MONO_WRAPPER_NONE)
3755 klass = mono_method_get_wrapper_data (method, token);
3757 klass = mono_class_get_full (image, token, generic_context);
3759 mono_class_init (klass);
3760 if (klass->byval_arg.type == MONO_TYPE_VAR)
3761 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3762 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
3763 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3764 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3765 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3767 ins->inst_i0 = sp [0];
3768 ins->type = ldind_type [CEE_LDIND_REF];
3769 ins->flags |= ins_flag;
3775 n = mono_class_value_size (klass, NULL);
3776 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3777 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3778 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3780 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3781 copy->inst_left = iargs [0];
3782 copy->inst_right = *sp;
3783 copy->cil_code = ip;
3785 MONO_ADD_INS (bblock, copy);
3788 NEW_ICONST (cfg, iargs [2], n);
3789 iargs [2]->cil_code = ip;
3791 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3793 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3801 CHECK_STACK_OVF (1);
3803 n = read32 (ip + 1);
3805 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3807 MonoInst *iargs [1];
3809 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
3810 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
3811 NEW_TEMPLOAD (cfg, *sp, temp);
3815 if (cfg->opt & MONO_OPT_SHARED) {
3817 MonoInst *iargs [3];
3819 if (mono_compile_aot) {
3820 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n);
3823 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3824 NEW_IMAGECONST (cfg, iargs [1], image);
3825 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
3826 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
3827 NEW_TEMPLOAD (cfg, *sp, temp);
3828 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3830 if (mono_compile_aot)
3831 NEW_LDSTRCONST (cfg, ins, image, n);
3833 NEW_PCONST (cfg, ins, NULL);
3835 ins->type = STACK_OBJ;
3836 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3846 MonoInst *iargs [2];
3847 MonoMethodSignature *fsig;
3851 token = read32 (ip + 1);
3852 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3853 cmethod = mono_method_get_wrapper_data (method, token);
3855 cmethod = mono_get_method_full (image, token, generic_context);
3856 fsig = mono_method_get_signature (cmethod, image, token);
3858 mono_class_init (cmethod->klass);
3860 n = fsig->param_count;
3863 /* move the args to allow room for 'this' in the first position */
3869 handle_loaded_temps (cfg, bblock, stack_start, sp);
3872 if (cmethod->klass->parent == mono_defaults.array_class) {
3873 NEW_METHODCONST (cfg, *sp, cmethod);
3874 temp = mono_emit_native_call (cfg, bblock, mono_array_new_va, fsig, sp, ip, FALSE);
3876 } else if (cmethod->string_ctor) {
3877 /* we simply pass a null pointer */
3878 NEW_PCONST (cfg, *sp, NULL);
3879 /* now call the string ctor */
3880 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
3882 if (cmethod->klass->valuetype) {
3883 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
3884 temp = iargs [0]->inst_c0;
3885 NEW_TEMPLOADA (cfg, *sp, temp);
3887 if (cfg->opt & MONO_OPT_SHARED) {
3888 NEW_DOMAINCONST (cfg, iargs [0]);
3889 NEW_CLASSCONST (cfg, iargs [1], cmethod->klass);
3891 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3893 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
3894 NEW_VTABLECONST (cfg, iargs [0], vtable);
3895 if (cmethod->klass->has_finalize || cmethod->klass->marshalbyref || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
3896 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
3898 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
3900 NEW_TEMPLOAD (cfg, *sp, temp);
3903 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3904 mono_method_check_inlining (cfg, cmethod) &&
3905 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
3906 !g_list_find (dont_inline, cmethod)) {
3908 MonoBasicBlock *ebblock;
3909 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3914 GET_BBLOCK (cfg, bbhash, bblock, ip);
3915 ebblock->next_bb = bblock;
3916 link_bblock (cfg, ebblock, bblock);
3918 NEW_TEMPLOAD (cfg, *sp, temp);
3921 /* indicates start of a new block, and triggers a load
3922 of all stack arguments at bb boundarie */
3925 inline_costs += costs;
3929 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3932 /* now call the actual ctor */
3933 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3937 NEW_TEMPLOAD (cfg, *sp, temp);
3948 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
3949 mono_class_init (klass);
3951 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
3953 MonoMethod *mono_isinst;
3954 MonoInst *iargs [1];
3955 MonoBasicBlock *ebblock;
3959 mono_isinst = mono_marshal_get_isinst (klass);
3962 costs = inline_method (cfg, mono_isinst, mono_isinst->signature, bblock,
3963 iargs, ip, real_offset, dont_inline, &ebblock);
3965 g_assert (costs > 0);
3970 GET_BBLOCK (cfg, bbhash, bblock, ip);
3971 ebblock->next_bb = bblock;
3972 link_bblock (cfg, ebblock, bblock);
3974 temp = iargs [0]->inst_i0->inst_c0;
3975 NEW_TEMPLOAD (cfg, *sp, temp);
3979 inline_costs += costs;
3983 MONO_INST_NEW (cfg, ins, *ip);
3984 ins->type = STACK_OBJ;
3985 ins->inst_left = *sp;
3986 ins->inst_newa_class = klass;
3992 case CEE_UNBOX_ANY: {
3993 MonoInst *add, *vtoffset;
3994 MonoInst *iargs [3];
3999 token = read32 (ip + 1);
4000 if (method->wrapper_type != MONO_WRAPPER_NONE)
4001 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4003 klass = mono_class_get_full (image, token, generic_context);
4004 mono_class_init (klass);
4006 if (klass->byval_arg.type == MONO_TYPE_VAR)
4007 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4008 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4009 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4011 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4013 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4014 MonoMethod *mono_castclass;
4015 MonoInst *iargs [1];
4016 MonoBasicBlock *ebblock;
4020 mono_castclass = mono_marshal_get_castclass (klass);
4023 costs = inline_method (cfg, mono_castclass, mono_castclass->signature, bblock,
4024 iargs, ip, real_offset, dont_inline, &ebblock);
4026 g_assert (costs > 0);
4031 GET_BBLOCK (cfg, bbhash, bblock, ip);
4032 ebblock->next_bb = bblock;
4033 link_bblock (cfg, ebblock, bblock);
4035 temp = iargs [0]->inst_i0->inst_c0;
4036 NEW_TEMPLOAD (cfg, *sp, temp);
4040 inline_costs += costs;
4043 MONO_INST_NEW (cfg, ins, CEE_CASTCLASS);
4044 ins->type = STACK_OBJ;
4045 ins->inst_left = *sp;
4047 ins->inst_newa_class = klass;
4055 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
4056 ins->type = STACK_OBJ;
4057 ins->inst_left = *sp;
4059 ins->inst_newa_class = klass;
4062 MONO_INST_NEW (cfg, add, CEE_ADD);
4063 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4064 add->inst_left = ins;
4065 add->inst_right = vtoffset;
4066 add->type = STACK_MP;
4070 n = mono_class_value_size (klass, NULL);
4071 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
4072 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
4073 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
4075 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
4076 copy->inst_left = iargs [0];
4077 copy->inst_right = *sp;
4078 copy->cil_code = ip;
4080 MONO_ADD_INS (bblock, copy);
4083 NEW_ICONST (cfg, iargs [2], n);
4084 iargs [2]->cil_code = ip;
4086 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
4088 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
4094 MonoInst *add, *vtoffset;
4099 token = read32 (ip + 1);
4100 if (method->wrapper_type != MONO_WRAPPER_NONE)
4101 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4103 klass = mono_class_get_full (image, token, generic_context);
4104 mono_class_init (klass);
4106 if (klass->byval_arg.type == MONO_TYPE_VAR)
4107 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4108 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4109 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4111 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
4112 ins->type = STACK_OBJ;
4113 ins->inst_left = *sp;
4115 ins->inst_newa_class = klass;
4118 MONO_INST_NEW (cfg, add, CEE_ADD);
4119 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4120 add->inst_left = ins;
4121 add->inst_right = vtoffset;
4122 add->type = STACK_MP;
4132 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4133 mono_class_init (klass);
4134 if (klass->byval_arg.type == MONO_TYPE_VAR)
4135 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4136 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4137 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4139 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4141 MonoMethod *mono_castclass;
4142 MonoInst *iargs [1];
4143 MonoBasicBlock *ebblock;
4147 mono_castclass = mono_marshal_get_castclass (klass);
4150 costs = inline_method (cfg, mono_castclass, mono_castclass->signature, bblock,
4151 iargs, ip, real_offset, dont_inline, &ebblock);
4153 g_assert (costs > 0);
4158 GET_BBLOCK (cfg, bbhash, bblock, ip);
4159 ebblock->next_bb = bblock;
4160 link_bblock (cfg, ebblock, bblock);
4162 temp = iargs [0]->inst_i0->inst_c0;
4163 NEW_TEMPLOAD (cfg, *sp, temp);
4167 inline_costs += costs;
4170 MONO_INST_NEW (cfg, ins, *ip);
4171 ins->type = STACK_OBJ;
4172 ins->inst_left = *sp;
4174 ins->inst_newa_class = klass;
4182 MONO_INST_NEW (cfg, ins, *ip);
4184 ins->inst_left = *sp;
4185 ins->cil_code = ip++;
4186 MONO_ADD_INS (bblock, ins);
4188 start_new_bblock = 1;
4193 MonoInst *offset_ins;
4194 MonoClassField *field;
4195 MonoBasicBlock *ebblock;
4199 if (*ip == CEE_STFLD) {
4206 // FIXME: enable this test later.
4207 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
4210 token = read32 (ip + 1);
4211 field = mono_field_from_token (image, token, &klass, generic_context);
4212 mono_class_init (klass);
4214 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
4215 /* FIXME: mark instructions for use in SSA */
4216 if (*ip == CEE_STFLD) {
4217 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound) {
4218 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
4219 MonoInst *iargs [5];
4222 NEW_CLASSCONST (cfg, iargs [1], klass);
4223 NEW_FIELDCONST (cfg, iargs [2], field);
4224 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
4228 if (cfg->opt & MONO_OPT_INLINE) {
4229 costs = inline_method (cfg, stfld_wrapper, stfld_wrapper->signature, bblock,
4230 iargs, ip, real_offset, dont_inline, &ebblock);
4231 g_assert (costs > 0);
4236 GET_BBLOCK (cfg, bbhash, bblock, ip);
4237 ebblock->next_bb = bblock;
4238 link_bblock (cfg, ebblock, bblock);
4240 /* indicates start of a new block, and triggers a load
4241 of all stack arguments at bb boundarie */
4244 inline_costs += costs;
4247 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, stfld_wrapper->signature, iargs, ip, NULL);
4251 NEW_ICONST (cfg, offset_ins, foffset);
4252 MONO_INST_NEW (cfg, ins, CEE_ADD);
4254 ins->inst_left = *sp;
4255 ins->inst_right = offset_ins;
4256 ins->type = STACK_MP;
4258 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4259 store->cil_code = ip;
4260 store->inst_left = ins;
4261 store->inst_right = sp [1];
4262 handle_loaded_temps (cfg, bblock, stack_start, sp);
4263 store->flags |= ins_flag;
4265 if (store->opcode == CEE_STOBJ) {
4266 handle_stobj (cfg, bblock, ins, sp [1], ip,
4267 mono_class_from_mono_type (field->type), FALSE, FALSE);
4269 MONO_ADD_INS (bblock, store);
4272 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound) {
4273 /* fixme: we need to inline that call somehow */
4274 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
4275 MonoInst *iargs [4];
4279 NEW_CLASSCONST (cfg, iargs [1], klass);
4280 NEW_FIELDCONST (cfg, iargs [2], field);
4281 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
4282 if (cfg->opt & MONO_OPT_INLINE) {
4283 costs = inline_method (cfg, ldfld_wrapper, ldfld_wrapper->signature, bblock,
4284 iargs, ip, real_offset, dont_inline, &ebblock);
4285 g_assert (costs > 0);
4290 GET_BBLOCK (cfg, bbhash, bblock, ip);
4291 ebblock->next_bb = bblock;
4292 link_bblock (cfg, ebblock, bblock);
4294 temp = iargs [0]->inst_i0->inst_c0;
4296 if (*ip == CEE_LDFLDA) {
4297 /* not sure howto handle this */
4298 NEW_TEMPLOADA (cfg, *sp, temp);
4300 NEW_TEMPLOAD (cfg, *sp, temp);
4304 /* indicates start of a new block, and triggers a load of
4305 all stack arguments at bb boundarie */
4308 inline_costs += costs;
4311 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, ldfld_wrapper->signature, iargs, ip, NULL);
4312 if (*ip == CEE_LDFLDA) {
4313 /* not sure howto handle this */
4314 NEW_TEMPLOADA (cfg, *sp, temp);
4316 NEW_TEMPLOAD (cfg, *sp, temp);
4321 NEW_ICONST (cfg, offset_ins, foffset);
4322 MONO_INST_NEW (cfg, ins, CEE_ADD);
4324 ins->inst_left = *sp;
4325 ins->inst_right = offset_ins;
4326 ins->type = STACK_MP;
4328 if (*ip == CEE_LDFLDA) {
4332 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4333 type_to_eval_stack_type (field->type, load);
4334 load->cil_code = ip;
4335 load->inst_left = ins;
4336 load->flags |= ins_flag;
4348 MonoClassField *field;
4349 gpointer addr = NULL;
4352 token = read32 (ip + 1);
4354 field = mono_field_from_token (image, token, &klass, generic_context);
4355 mono_class_init (klass);
4357 handle_loaded_temps (cfg, bblock, stack_start, sp);
4359 if (cfg->domain->special_static_fields)
4360 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
4362 if ((cfg->opt & MONO_OPT_SHARED) || (mono_compile_aot && addr)) {
4364 MonoInst *iargs [2];
4365 g_assert (field->parent);
4366 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
4367 NEW_FIELDCONST (cfg, iargs [1], field);
4368 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
4369 NEW_TEMPLOAD (cfg, ins, temp);
4372 vtable = mono_class_vtable (cfg->domain, klass);
4374 if ((!vtable->initialized || mono_compile_aot) && !(klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (klass, method)) {
4375 guint8 *tramp = mono_create_class_init_trampoline (vtable);
4376 mono_emit_native_call (cfg, bblock, tramp,
4377 helper_sig_class_init_trampoline,
4379 if (cfg->verbose_level > 2)
4380 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
4382 if (cfg->run_cctors)
4383 mono_runtime_class_init (vtable);
4385 addr = (char*)vtable->data + field->offset;
4387 if (mono_compile_aot)
4388 NEW_SFLDACONST (cfg, ins, field);
4390 NEW_PCONST (cfg, ins, addr);
4394 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
4395 * This could be later optimized to do just a couple of
4396 * memory dereferences with constant offsets.
4399 MonoInst *iargs [1];
4400 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
4401 temp = mono_emit_jit_icall (cfg, bblock, mono_get_special_static_data, iargs, ip);
4402 NEW_TEMPLOAD (cfg, ins, temp);
4406 /* FIXME: mark instructions for use in SSA */
4407 if (*ip == CEE_LDSFLDA) {
4409 } else if (*ip == CEE_STSFLD) {
4413 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4414 store->cil_code = ip;
4415 store->inst_left = ins;
4416 store->inst_right = sp [0];
4417 store->flags |= ins_flag;
4420 if (store->opcode == CEE_STOBJ) {
4421 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
4423 MONO_ADD_INS (bblock, store);
4425 gboolean is_const = FALSE;
4426 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4427 if (!((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) &&
4428 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
4429 gpointer addr = (char*)vtable->data + field->offset;
4430 /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
4432 switch (field->type->type) {
4433 case MONO_TYPE_BOOLEAN:
4435 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
4439 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
4442 case MONO_TYPE_CHAR:
4444 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
4448 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
4453 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
4457 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
4462 case MONO_TYPE_STRING:
4463 case MONO_TYPE_OBJECT:
4464 case MONO_TYPE_CLASS:
4465 case MONO_TYPE_SZARRAY:
4467 case MONO_TYPE_FNPTR:
4468 case MONO_TYPE_ARRAY:
4469 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
4470 type_to_eval_stack_type (field->type, *sp);
4475 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
4476 sp [0]->type = STACK_I8;
4477 sp [0]->inst_l = *((gint64 *)addr);
4482 case MONO_TYPE_VALUETYPE:
4491 CHECK_STACK_OVF (1);
4492 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4493 type_to_eval_stack_type (field->type, load);
4494 load->cil_code = ip;
4495 load->inst_left = ins;
4497 load->flags |= ins_flag;
4499 /* fixme: dont see the problem why this does not work */
4500 //cfg->disable_aot = TRUE;
4510 token = read32 (ip + 1);
4511 if (method->wrapper_type != MONO_WRAPPER_NONE)
4512 klass = mono_method_get_wrapper_data (method, token);
4514 klass = mono_class_get_full (image, token, generic_context);
4515 mono_class_init (klass);
4516 if (klass->byval_arg.type == MONO_TYPE_VAR)
4517 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4518 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4519 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4520 n = mono_type_to_stind (&klass->byval_arg);
4521 if (n == CEE_STOBJ) {
4522 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
4524 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
4526 MONO_INST_NEW (cfg, store, n);
4527 store->cil_code = ip;
4528 store->inst_left = sp [0];
4529 store->inst_right = sp [1];
4530 store->flags |= ins_flag;
4531 MONO_ADD_INS (bblock, store);
4538 MonoInst *iargs [2];
4539 MonoInst *load, *vtoffset, *add, *val, *vstore;
4545 token = read32 (ip + 1);
4546 if (method->wrapper_type != MONO_WRAPPER_NONE)
4547 klass = mono_method_get_wrapper_data (method, token);
4549 klass = mono_class_get_full (image, token, generic_context);
4550 mono_class_init (klass);
4551 if (klass->byval_arg.type == MONO_TYPE_VAR)
4552 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4553 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4554 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4556 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4561 /* much like NEWOBJ */
4562 if (cfg->opt & MONO_OPT_SHARED) {
4563 NEW_DOMAINCONST (cfg, iargs [0]);
4564 NEW_CLASSCONST (cfg, iargs [1], klass);
4566 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
4568 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4569 NEW_VTABLECONST (cfg, iargs [0], vtable);
4570 if (klass->has_finalize || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
4571 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
4573 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
4575 NEW_TEMPLOAD (cfg, load, temp);
4576 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4577 MONO_INST_NEW (cfg, add, CEE_ADD);
4578 add->inst_left = load;
4579 add->inst_right = vtoffset;
4582 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
4583 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
4584 vstore->cil_code = ip;
4585 vstore->inst_left = add;
4586 vstore->inst_right = val;
4588 if (vstore->opcode == CEE_STOBJ) {
4589 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
4591 MONO_ADD_INS (bblock, vstore);
4593 NEW_TEMPLOAD (cfg, load, temp);
4601 MONO_INST_NEW (cfg, ins, *ip);
4606 token = read32 (ip + 1);
4608 /* allocate the domainvar - becaus this is used in decompose_foreach */
4609 if (cfg->opt & MONO_OPT_SHARED)
4610 mono_get_domainvar (cfg);
4612 if (method->wrapper_type != MONO_WRAPPER_NONE)
4613 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4615 klass = mono_class_get_full (image, token, generic_context);
4617 mono_class_init (klass);
4618 if (klass->byval_arg.type == MONO_TYPE_VAR)
4619 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4620 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4621 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4622 ins->inst_newa_class = klass;
4623 ins->inst_newa_len = *sp;
4624 ins->type = STACK_OBJ;
4628 * we store the object so calls to create the array are not interleaved
4629 * with the arguments of other calls.
4632 MonoInst *store, *temp, *load;
4634 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
4635 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4636 store->cil_code = ins->cil_code;
4637 MONO_ADD_INS (bblock, store);
4638 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
4639 load->cil_code = ins->cil_code;
4646 MONO_INST_NEW (cfg, ins, *ip);
4647 ins->cil_code = ip++;
4649 ins->inst_left = *sp;
4650 ins->type = STACK_PTR;
4658 if (method->wrapper_type != MONO_WRAPPER_NONE)
4659 klass = (MonoClass*)mono_method_get_wrapper_data (method, read32 (ip + 1));
4661 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4662 mono_class_init (klass);
4663 if (klass->byval_arg.type == MONO_TYPE_VAR)
4664 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4665 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4666 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4667 NEW_LDELEMA (cfg, ins, sp, klass);
4677 token = read32 (ip + 1);
4678 klass = mono_class_get_full (image, token, generic_context);
4679 mono_class_init (klass);
4680 if (klass->byval_arg.type == MONO_TYPE_VAR)
4681 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4682 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4683 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4684 NEW_LDELEMA (cfg, load, sp, klass);
4685 load->cil_code = ip;
4686 MONO_INST_NEW (cfg, ins, mono_type_to_ldind (&klass->byval_arg));
4688 ins->inst_left = load;
4690 type_to_eval_stack_type (&klass->byval_arg, ins);
4704 case CEE_LDELEM_REF: {
4708 * ldind.x (ldelema (array, index))
4709 * ldelema does the bounds check
4713 klass = array_access_to_klass (*ip);
4714 NEW_LDELEMA (cfg, load, sp, klass);
4715 load->cil_code = ip;
4716 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
4718 ins->inst_left = load;
4720 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
4730 case CEE_STELEM_R8: {
4734 * stind.x (ldelema (array, index), val)
4735 * ldelema does the bounds check
4739 klass = array_access_to_klass (*ip);
4740 NEW_LDELEMA (cfg, load, sp, klass);
4741 load->cil_code = ip;
4742 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
4744 ins->inst_left = load;
4745 ins->inst_right = sp [2];
4747 handle_loaded_temps (cfg, bblock, stack_start, sp);
4748 MONO_ADD_INS (bblock, ins);
4756 * stind.x (ldelema (array, index), val)
4757 * ldelema does the bounds check
4762 token = read32 (ip + 1);
4763 klass = mono_class_get_full (image, token, generic_context);
4764 mono_class_init (klass);
4765 if (klass->byval_arg.type == MONO_TYPE_VAR)
4766 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4767 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4768 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4769 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4770 MonoInst *iargs [3];
4771 handle_loaded_temps (cfg, bblock, stack_start, sp);
4777 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4779 NEW_LDELEMA (cfg, load, sp, klass);
4780 load->cil_code = ip;
4781 MONO_INST_NEW (cfg, ins, mono_type_to_stind (&klass->byval_arg));
4783 ins->inst_left = load;
4784 ins->inst_right = sp [2];
4785 handle_loaded_temps (cfg, bblock, stack_start, sp);
4786 MONO_ADD_INS (bblock, ins);
4792 case CEE_STELEM_REF: {
4793 MonoInst *iargs [3];
4798 handle_loaded_temps (cfg, bblock, stack_start, sp);
4804 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4808 NEW_GROUP (cfg, group, sp [0], sp [1]);
4809 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
4811 ins->inst_left = group;
4812 ins->inst_right = sp [2];
4813 MONO_ADD_INS (bblock, ins);
4820 case CEE_CKFINITE: {
4821 MonoInst *store, *temp;
4824 /* this instr. can throw exceptions as side effect,
4825 * so we cant eliminate dead code which contains CKFINITE opdodes.
4826 * Spilling to memory makes sure that we always perform
4830 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
4832 ins->inst_left = sp [-1];
4833 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
4835 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4836 store->cil_code = ip;
4837 MONO_ADD_INS (bblock, store);
4839 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
4846 MONO_INST_NEW (cfg, ins, *ip);
4849 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4850 mono_class_init (klass);
4851 if (klass->byval_arg.type == MONO_TYPE_VAR)
4852 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4853 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4854 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4855 ins->type = STACK_MP;
4856 ins->inst_left = *sp;
4858 ins->inst_newa_class = klass;
4863 case CEE_MKREFANY: {
4864 MonoInst *loc, *klassconst;
4867 MONO_INST_NEW (cfg, ins, *ip);
4870 klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
4871 mono_class_init (klass);
4872 if (klass->byval_arg.type == MONO_TYPE_VAR)
4873 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4874 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4875 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4878 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
4879 NEW_TEMPLOADA (cfg, ins->inst_right, loc->inst_c0);
4881 NEW_PCONST (cfg, klassconst, klass);
4882 NEW_GROUP (cfg, ins->inst_left, *sp, klassconst);
4884 MONO_ADD_INS (bblock, ins);
4886 NEW_TEMPLOAD (cfg, *sp, loc->inst_c0);
4893 MonoClass *handle_class;
4895 CHECK_STACK_OVF (1);
4898 n = read32 (ip + 1);
4900 handle = mono_ldtoken (image, n, &handle_class);
4901 mono_class_init (handle_class);
4903 if (cfg->opt & MONO_OPT_SHARED) {
4905 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
4907 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
4909 NEW_IMAGECONST (cfg, iargs [0], image);
4910 NEW_ICONST (cfg, iargs [1], n);
4911 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
4912 NEW_TEMPLOAD (cfg, res, temp);
4913 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4914 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
4915 MONO_ADD_INS (bblock, store);
4916 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4918 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method_full (image, read32 (ip + 6), generic_context)) &&
4919 (cmethod->klass == mono_defaults.monotype_class->parent) &&
4920 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
4921 MonoClass *tclass = mono_class_from_mono_type (handle);
4922 mono_class_init (tclass);
4923 if (mono_compile_aot)
4924 NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n);
4926 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
4927 ins->type = STACK_OBJ;
4928 ins->klass = cmethod->klass;
4931 if (mono_compile_aot)
4932 NEW_LDTOKENCONST (cfg, ins, image, n);
4934 NEW_PCONST (cfg, ins, handle);
4935 ins->type = STACK_VTYPE;
4936 ins->klass = handle_class;
4952 case CEE_ADD_OVF_UN:
4954 case CEE_MUL_OVF_UN:
4956 case CEE_SUB_OVF_UN:
4961 case CEE_ENDFINALLY:
4962 MONO_INST_NEW (cfg, ins, *ip);
4963 MONO_ADD_INS (bblock, ins);
4964 ins->cil_code = ip++;
4965 start_new_bblock = 1;
4968 * Control will leave the method so empty the stack, otherwise
4969 * the next basic block will start with a nonempty stack.
4971 while (sp != stack_start) {
4972 MONO_INST_NEW (cfg, ins, CEE_POP);
4976 MONO_ADD_INS (bblock, ins);
4982 if (*ip == CEE_LEAVE) {
4984 target = ip + 5 + (gint32)read32(ip + 1);
4987 target = ip + 2 + (signed char)(ip [1]);
4990 /* empty the stack */
4991 while (sp != stack_start) {
4992 MONO_INST_NEW (cfg, ins, CEE_POP);
4996 MONO_ADD_INS (bblock, ins);
5000 * If this leave statement is in a catch block, check for a
5001 * pending exception, and rethrow it if necessary.
5003 for (i = 0; i < header->num_clauses; ++i) {
5004 MonoExceptionClause *clause = &header->clauses [i];
5005 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code)) {
5008 temp = mono_emit_jit_icall (cfg, bblock, mono_thread_get_pending_exception, NULL, ip);
5009 NEW_TEMPLOAD (cfg, *sp, temp);
5011 MONO_INST_NEW (cfg, ins, OP_THROW_OR_NULL);
5012 ins->inst_left = *sp;
5014 MONO_ADD_INS (bblock, ins);
5018 /* fixme: call fault handler ? */
5020 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
5022 for (tmp = handlers; tmp; tmp = tmp->next) {
5024 link_bblock (cfg, bblock, tblock);
5025 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
5027 ins->inst_target_bb = tblock;
5028 MONO_ADD_INS (bblock, ins);
5030 g_list_free (handlers);
5033 MONO_INST_NEW (cfg, ins, CEE_BR);
5035 MONO_ADD_INS (bblock, ins);
5036 GET_BBLOCK (cfg, bbhash, tblock, target);
5037 link_bblock (cfg, bblock, tblock);
5038 CHECK_BBLOCK (target, ip, tblock);
5039 ins->inst_target_bb = tblock;
5040 start_new_bblock = 1;
5042 if (*ip == CEE_LEAVE)
5051 MONO_INST_NEW (cfg, ins, *ip);
5053 handle_loaded_temps (cfg, bblock, stack_start, sp);
5054 MONO_ADD_INS (bblock, ins);
5055 ins->cil_code = ip++;
5056 ins->inst_i0 = sp [0];
5057 ins->inst_i1 = sp [1];
5065 /* trampoline mono specific opcodes */
5066 case MONO_CUSTOM_PREFIX: {
5068 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
5073 case CEE_MONO_FUNC1: {
5075 gpointer func = NULL;
5081 case MONO_MARSHAL_CONV_STR_LPWSTR:
5082 func = mono_string_to_utf16;
5084 case MONO_MARSHAL_CONV_LPWSTR_STR:
5085 func = mono_string_from_utf16;
5087 case MONO_MARSHAL_CONV_LPSTR_STR:
5088 func = mono_string_new_wrapper;
5090 case MONO_MARSHAL_CONV_STR_LPTSTR:
5091 case MONO_MARSHAL_CONV_STR_LPSTR:
5092 func = mono_string_to_utf8;
5094 case MONO_MARSHAL_CONV_STR_BSTR:
5095 func = mono_string_to_bstr;
5097 case MONO_MARSHAL_CONV_STR_TBSTR:
5098 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
5099 func = mono_string_to_ansibstr;
5101 case MONO_MARSHAL_CONV_SB_LPSTR:
5102 case MONO_MARSHAL_CONV_SB_LPTSTR:
5103 func = mono_string_builder_to_utf8;
5105 case MONO_MARSHAL_CONV_SB_LPWSTR:
5106 func = mono_string_builder_to_utf16;
5108 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
5109 func = mono_array_to_savearray;
5111 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
5112 func = mono_array_to_lparray;
5114 case MONO_MARSHAL_CONV_DEL_FTN:
5115 func = mono_delegate_to_ftnptr;
5117 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
5118 func = mono_marshal_string_array;
5121 g_warning ("unknown conversion %d\n", ip [2]);
5122 g_assert_not_reached ();
5125 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5126 NEW_TEMPLOAD (cfg, *sp, temp);
5130 inline_costs += 10 * num_calls++;
5133 case CEE_MONO_PROC2: {
5134 gpointer func = NULL;
5140 case MONO_MARSHAL_CONV_LPSTR_SB:
5141 case MONO_MARSHAL_CONV_LPTSTR_SB:
5142 func = mono_string_utf8_to_builder;
5144 case MONO_MARSHAL_CONV_LPWSTR_SB:
5145 func = mono_string_utf16_to_builder;
5147 case MONO_MARSHAL_FREE_ARRAY:
5148 func = mono_marshal_free_array;
5151 g_assert_not_reached ();
5154 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5156 inline_costs += 10 * num_calls++;
5159 case CEE_MONO_PROC3: {
5160 gpointer func = NULL;
5166 case MONO_MARSHAL_CONV_STR_BYVALSTR:
5167 func = mono_string_to_byvalstr;
5169 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
5170 func = mono_string_to_byvalwstr;
5173 g_assert_not_reached ();
5176 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5178 inline_costs += 10 * num_calls++;
5184 mono_emit_jit_icall (cfg, bblock, g_free, sp, ip);
5186 inline_costs += 10 * num_calls++;
5188 case CEE_MONO_LDPTR:
5189 CHECK_STACK_OVF (1);
5191 token = read32 (ip + 2);
5192 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
5196 inline_costs += 10 * num_calls++;
5198 case CEE_MONO_VTADDR:
5201 MONO_INST_NEW (cfg, ins, OP_VTADDR);
5203 ins->type = STACK_MP;
5204 ins->inst_left = *sp;
5208 case CEE_MONO_NEWOBJ: {
5209 MonoInst *iargs [2];
5211 CHECK_STACK_OVF (1);
5213 token = read32 (ip + 2);
5214 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5215 mono_class_init (klass);
5216 NEW_DOMAINCONST (cfg, iargs [0]);
5217 NEW_CLASSCONST (cfg, iargs [1], klass);
5218 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
5219 NEW_TEMPLOAD (cfg, *sp, temp);
5222 inline_costs += 10 * num_calls++;
5225 case CEE_MONO_OBJADDR:
5228 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
5230 ins->type = STACK_MP;
5231 ins->inst_left = *sp;
5235 case CEE_MONO_LDNATIVEOBJ:
5238 token = read32 (ip + 2);
5239 klass = mono_method_get_wrapper_data (method, token);
5240 g_assert (klass->valuetype);
5241 mono_class_init (klass);
5242 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
5246 case CEE_MONO_RETOBJ:
5247 g_assert (cfg->ret);
5248 g_assert (method->signature->pinvoke);
5253 token = read32 (ip + 2);
5254 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5256 NEW_RETLOADA (cfg, ins);
5257 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
5259 if (sp != stack_start)
5262 MONO_INST_NEW (cfg, ins, CEE_BR);
5264 ins->inst_target_bb = end_bblock;
5265 MONO_ADD_INS (bblock, ins);
5266 link_bblock (cfg, bblock, end_bblock);
5267 start_new_bblock = 1;
5270 case CEE_MONO_CISINST:
5271 case CEE_MONO_CCASTCLASS: {
5276 token = read32 (ip + 2);
5277 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5278 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_CISINST) ? OP_CISINST : OP_CCASTCLASS);
5279 ins->type = STACK_I4;
5280 ins->inst_left = *sp;
5281 ins->inst_newa_class = klass;
5288 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
5297 /* somewhat similar to LDTOKEN */
5298 MonoInst *addr, *vtvar;
5299 CHECK_STACK_OVF (1);
5300 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
5302 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5303 addr->cil_code = ip;
5304 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
5306 ins->inst_left = addr;
5307 MONO_ADD_INS (bblock, ins);
5308 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5322 * The following transforms:
5323 * CEE_CEQ into OP_CEQ
5324 * CEE_CGT into OP_CGT
5325 * CEE_CGT_UN into OP_CGT_UN
5326 * CEE_CLT into OP_CLT
5327 * CEE_CLT_UN into OP_CLT_UN
5329 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
5331 MONO_INST_NEW (cfg, ins, cmp->opcode);
5333 cmp->inst_i0 = sp [0];
5334 cmp->inst_i1 = sp [1];
5338 cmp->opcode = OP_COMPARE;
5340 ins->type = STACK_I4;
5350 CHECK_STACK_OVF (1);
5352 n = read32 (ip + 2);
5353 if (method->wrapper_type != MONO_WRAPPER_NONE)
5354 cmethod = mono_method_get_wrapper_data (method, n);
5356 cmethod = mono_get_method_full (image, n, generic_context);
5359 mono_class_init (cmethod->klass);
5360 handle_loaded_temps (cfg, bblock, stack_start, sp);
5362 NEW_METHODCONST (cfg, argconst, cmethod);
5363 if (method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED)
5364 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
5366 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn_nosync, &argconst, ip);
5367 NEW_TEMPLOAD (cfg, *sp, temp);
5371 inline_costs += 10 * num_calls++;
5374 case CEE_LDVIRTFTN: {
5380 n = read32 (ip + 2);
5381 if (method->wrapper_type != MONO_WRAPPER_NONE)
5382 cmethod = mono_method_get_wrapper_data (method, n);
5384 cmethod = mono_get_method_full (image, n, generic_context);
5386 mono_class_init (cmethod->klass);
5387 handle_loaded_temps (cfg, bblock, stack_start, sp);
5391 NEW_METHODCONST (cfg, args [1], cmethod);
5392 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
5393 NEW_TEMPLOAD (cfg, *sp, temp);
5397 inline_costs += 10 * num_calls++;
5401 CHECK_STACK_OVF (1);
5403 n = read16 (ip + 2);
5405 NEW_ARGLOAD (cfg, ins, n);
5411 CHECK_STACK_OVF (1);
5413 n = read16 (ip + 2);
5415 NEW_ARGLOADA (cfg, ins, n);
5423 handle_loaded_temps (cfg, bblock, stack_start, sp);
5425 n = read16 (ip + 2);
5427 NEW_ARGSTORE (cfg, ins, n, *sp);
5429 if (ins->opcode == CEE_STOBJ) {
5430 NEW_ARGLOADA (cfg, ins, n);
5431 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5433 MONO_ADD_INS (bblock, ins);
5437 CHECK_STACK_OVF (1);
5439 n = read16 (ip + 2);
5441 NEW_LOCLOAD (cfg, ins, n);
5447 CHECK_STACK_OVF (1);
5449 n = read16 (ip + 2);
5451 NEW_LOCLOADA (cfg, ins, n);
5460 n = read16 (ip + 2);
5462 handle_loaded_temps (cfg, bblock, stack_start, sp);
5463 NEW_LOCSTORE (cfg, ins, n, *sp);
5465 if (ins->opcode == CEE_STOBJ) {
5466 NEW_LOCLOADA (cfg, ins, n);
5467 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5469 MONO_ADD_INS (bblock, ins);
5476 if (sp != stack_start)
5478 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
5479 ins->inst_left = *sp;
5482 cfg->flags |= MONO_CFG_HAS_ALLOCA;
5483 if (header->init_locals)
5484 ins->flags |= MONO_INST_INIT;
5488 /* FIXME: set init flag if locals init is set in this method */
5490 case CEE_ENDFILTER: {
5491 MonoExceptionClause *clause, *nearest;
5492 int cc, nearest_num;
5496 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
5498 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
5499 ins->inst_left = *sp;
5501 MONO_ADD_INS (bblock, ins);
5502 start_new_bblock = 1;
5507 for (cc = 0; cc < header->num_clauses; ++cc) {
5508 clause = &header->clauses [cc];
5509 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
5510 (!nearest || (clause->token_or_filter > nearest->token_or_filter))) {
5516 filter_lengths [nearest_num] = (ip - header->code) - nearest->token_or_filter;
5520 case CEE_UNALIGNED_:
5521 ins_flag |= MONO_INST_UNALIGNED;
5522 /* FIXME: record alignment? we can assume 1 for now */
5527 ins_flag |= MONO_INST_VOLATILE;
5531 ins_flag |= MONO_INST_TAILCALL;
5532 /* Can't inline tail calls at this time */
5533 inline_costs += 100000;
5540 token = read32 (ip + 2);
5541 if (method->wrapper_type != MONO_WRAPPER_NONE)
5542 klass = mono_method_get_wrapper_data (method, token);
5544 klass = mono_class_get_full (image, token, generic_context);
5545 if (klass->byval_arg.type == MONO_TYPE_VAR)
5546 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
5547 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
5548 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
5550 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5551 MonoInst *store, *load;
5552 NEW_PCONST (cfg, load, NULL);
5553 load->cil_code = ip;
5554 load->type = STACK_OBJ;
5555 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
5556 store->cil_code = ip;
5557 handle_loaded_temps (cfg, bblock, stack_start, sp);
5558 MONO_ADD_INS (bblock, store);
5559 store->inst_i0 = sp [0];
5560 store->inst_i1 = load;
5562 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
5567 case CEE_CONSTRAINED_:
5568 /* FIXME: implement */
5570 token = read32 (ip + 2);
5575 MonoInst *iargs [3];
5578 if ((cfg->opt & MONO_OPT_INTRINS) && (ip [1] == CEE_CPBLK) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5)) {
5580 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
5581 copy->inst_left = sp [0];
5582 copy->inst_right = sp [1];
5583 copy->cil_code = ip;
5585 MONO_ADD_INS (bblock, copy);
5592 handle_loaded_temps (cfg, bblock, stack_start, sp);
5593 if (ip [1] == CEE_CPBLK) {
5594 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
5596 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
5605 ins_flag |= MONO_INST_NOTYPECHECK;
5607 ins_flag |= MONO_INST_NORANGECHECK;
5608 /* we ignore the no-nullcheck for now since we
5609 * really do it explicitly only when doing callvirt->call
5615 /* FIXME: check we are in a catch handler */
5616 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
5617 load->cil_code = ip;
5618 MONO_INST_NEW (cfg, ins, CEE_THROW);
5619 ins->inst_left = load;
5621 MONO_ADD_INS (bblock, ins);
5623 start_new_bblock = 1;
5628 CHECK_STACK_OVF (1);
5630 token = read32 (ip + 2);
5631 /* FIXXME: handle generics. */
5632 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
5633 MonoType *type = mono_type_create_from_typespec (image, token);
5634 token = mono_type_size (type, &align);
5636 MonoClass *szclass = mono_class_get_full (image, token, generic_context);
5637 mono_class_init (szclass);
5638 token = mono_class_value_size (szclass, &align);
5640 NEW_ICONST (cfg, ins, token);
5645 case CEE_REFANYTYPE:
5647 MONO_INST_NEW (cfg, ins, ip [1]);
5649 ins->type = STACK_MP;
5650 ins->inst_left = *sp;
5651 ins->type = STACK_VTYPE;
5652 ins->klass = mono_defaults.typehandle_class;
5661 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
5666 g_error ("opcode 0x%02x not handled", *ip);
5669 if (start_new_bblock != 1)
5672 bblock->cil_length = ip - bblock->cil_code;
5673 bblock->next_bb = end_bblock;
5674 link_bblock (cfg, bblock, end_bblock);
5676 if (cfg->method == method && cfg->domainvar) {
5680 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
5681 call->signature = helper_sig_domain_get;
5682 call->inst.type = STACK_PTR;
5683 call->fptr = mono_domain_get;
5684 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, (MonoInst*)call);
5686 MONO_ADD_INS (init_localsbb, store);
5689 if (header->init_locals) {
5691 for (i = 0; i < header->num_locals; ++i) {
5692 int t = header->locals [i]->type;
5693 if (t == MONO_TYPE_VALUETYPE && header->locals [i]->data.klass->enumtype)
5694 t = header->locals [i]->data.klass->enum_basetype->type;
5695 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
5696 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
5697 NEW_ICONST (cfg, ins, 0);
5698 NEW_LOCSTORE (cfg, store, i, ins);
5699 MONO_ADD_INS (init_localsbb, store);
5700 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
5701 MONO_INST_NEW (cfg, ins, OP_I8CONST);
5702 ins->type = STACK_I8;
5704 NEW_LOCSTORE (cfg, store, i, ins);
5705 MONO_ADD_INS (init_localsbb, store);
5706 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
5707 MONO_INST_NEW (cfg, ins, OP_R8CONST);
5708 ins->type = STACK_R8;
5709 ins->inst_p0 = (void*)&r8_0;
5710 NEW_LOCSTORE (cfg, store, i, ins);
5711 MONO_ADD_INS (init_localsbb, store);
5712 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF)) {
5713 NEW_LOCLOADA (cfg, ins, i);
5714 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (header->locals [i]), NULL, NULL);
5717 NEW_PCONST (cfg, ins, NULL);
5718 NEW_LOCSTORE (cfg, store, i, ins);
5719 MONO_ADD_INS (init_localsbb, store);
5725 /* resolve backward branches in the middle of an existing basic block */
5726 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
5728 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
5729 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
5730 if (tblock != start_bblock) {
5732 split_bblock (cfg, tblock, bblock);
5733 l = bblock->cil_code - header->code;
5734 bblock->cil_length = tblock->cil_length - l;
5735 tblock->cil_length = l;
5737 g_print ("recheck failed.\n");
5742 * we compute regions here, because the length of filter clauses is not known in advance.
5743 * It is computed in the CEE_ENDFILTER case in the above switch statement
5745 if (cfg->method == method) {
5747 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5748 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
5750 mono_create_spvar_for_region (cfg, bb->region);
5751 if (cfg->verbose_level > 2)
5752 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
5755 g_hash_table_destroy (bbhash);
5758 dont_inline = g_list_remove (dont_inline, method);
5759 return inline_costs;
5762 if (cfg->method != method)
5763 g_hash_table_destroy (bbhash);
5764 dont_inline = g_list_remove (dont_inline, method);
5768 if (cfg->method != method)
5769 g_hash_table_destroy (bbhash);
5770 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
5771 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
5772 dont_inline = g_list_remove (dont_inline, method);
5777 mono_print_tree (MonoInst *tree) {
5783 arity = mono_burg_arity [tree->opcode];
5785 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
5787 switch (tree->opcode) {
5789 printf ("[%d]", tree->inst_c0);
5792 printf ("[%lld]", tree->inst_l);
5795 printf ("[%f]", *(double*)tree->inst_p0);
5798 printf ("[%f]", *(float*)tree->inst_p0);
5802 printf ("[%d]", tree->inst_c0);
5805 if (tree->inst_offset < 0)
5806 printf ("[-0x%x(%s)]", -tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5808 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5811 printf ("[%s]", mono_arch_regname (tree->dreg));
5814 printf ("[%s]", tree->inst_newa_class->name);
5815 mono_print_tree (tree->inst_newa_len);
5826 case OP_VOIDCALLVIRT: {
5827 MonoCallInst *call = (MonoCallInst*)tree;
5829 printf ("[%s]", call->method->name);
5834 printf ("[%d (", tree->inst_c0);
5835 for (i = 0; i < tree->inst_phi_args [0]; i++) {
5838 printf ("%d", tree->inst_phi_args [i + 1]);
5849 case OP_LOAD_MEMBASE:
5850 case OP_LOADI4_MEMBASE:
5851 case OP_LOADU4_MEMBASE:
5852 case OP_LOADU1_MEMBASE:
5853 case OP_LOADI1_MEMBASE:
5854 case OP_LOADU2_MEMBASE:
5855 case OP_LOADI2_MEMBASE:
5856 printf ("[%s] <- [%s + 0x%x]", mono_arch_regname (tree->dreg), mono_arch_regname (tree->inst_basereg), tree->inst_offset);
5859 printf ("[B%d]", tree->inst_target_bb->block_num);
5869 case OP_VOIDCALL_REG:
5870 mono_print_tree (tree->inst_left);
5882 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
5883 mono_print_tree (tree->inst_left);
5887 mono_print_tree (tree->inst_left);
5889 mono_print_tree (tree->inst_right);
5899 mono_print_tree_nl (MonoInst *tree)
5901 mono_print_tree (tree);
5906 create_helper_signature (void)
5908 /* FIXME: set call conv */
5909 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
5910 helper_sig_newarr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5911 helper_sig_newarr->params [0] = helper_sig_newarr->params [1] = &mono_defaults.int_class->byval_arg;
5912 helper_sig_newarr->ret = &mono_defaults.object_class->byval_arg;
5913 helper_sig_newarr->params [2] = &mono_defaults.int32_class->byval_arg;
5914 helper_sig_newarr->pinvoke = 1;
5916 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
5917 helper_sig_newarr_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5918 helper_sig_newarr_specific->params [0] = &mono_defaults.int_class->byval_arg;
5919 helper_sig_newarr_specific->params [1] = &mono_defaults.int32_class->byval_arg;
5920 helper_sig_newarr_specific->ret = &mono_defaults.object_class->byval_arg;
5921 helper_sig_newarr_specific->pinvoke = 1;
5923 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
5924 helper_sig_object_new = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5925 helper_sig_object_new->params [0] = helper_sig_object_new->params [1] = &mono_defaults.int_class->byval_arg;
5926 helper_sig_object_new->ret = &mono_defaults.object_class->byval_arg;
5927 helper_sig_object_new->pinvoke = 1;
5929 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
5930 helper_sig_object_new_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5931 helper_sig_object_new_specific->params [0] = &mono_defaults.int_class->byval_arg;
5932 helper_sig_object_new_specific->ret = &mono_defaults.object_class->byval_arg;
5933 helper_sig_object_new_specific->pinvoke = 1;
5935 /* void* mono_method_compile (MonoMethod*) */
5936 helper_sig_compile = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5937 helper_sig_compile->params [0] = helper_sig_compile->ret = &mono_defaults.int_class->byval_arg;
5938 helper_sig_compile->pinvoke = 1;
5940 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
5941 helper_sig_compile_virt = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5942 helper_sig_compile_virt->params [0] = &mono_defaults.object_class->byval_arg;
5943 helper_sig_compile_virt->params [1] = helper_sig_compile_virt->ret = &mono_defaults.int_class->byval_arg;
5944 helper_sig_compile_virt->pinvoke = 1;
5946 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
5947 helper_sig_ldstr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5948 helper_sig_ldstr->params [0] = helper_sig_ldstr->params [1] = &mono_defaults.int_class->byval_arg;
5949 helper_sig_ldstr->params [2] = &mono_defaults.int32_class->byval_arg;
5950 helper_sig_ldstr->ret = &mono_defaults.object_class->byval_arg;
5951 helper_sig_ldstr->pinvoke = 1;
5953 /* MonoDomain *mono_domain_get (void) */
5954 helper_sig_domain_get = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5955 helper_sig_domain_get->ret = &mono_defaults.int_class->byval_arg;
5956 helper_sig_domain_get->pinvoke = 1;
5958 /* void* stelem_ref (MonoArray *, int index, MonoObject *) */
5959 helper_sig_stelem_ref = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5960 helper_sig_stelem_ref->params [0] = &mono_defaults.array_class->byval_arg;
5961 helper_sig_stelem_ref->params [1] = &mono_defaults.int32_class->byval_arg;
5962 helper_sig_stelem_ref->params [2] = &mono_defaults.object_class->byval_arg;
5963 helper_sig_stelem_ref->ret = &mono_defaults.void_class->byval_arg;
5964 helper_sig_stelem_ref->pinvoke = 1;
5966 /* void* stelem_ref_check (MonoArray *, MonoObject *) */
5967 helper_sig_stelem_ref_check = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5968 helper_sig_stelem_ref_check->params [0] = &mono_defaults.array_class->byval_arg;
5969 helper_sig_stelem_ref_check->params [1] = &mono_defaults.object_class->byval_arg;
5970 helper_sig_stelem_ref_check->ret = &mono_defaults.void_class->byval_arg;
5971 helper_sig_stelem_ref_check->pinvoke = 1;
5973 /* long amethod (long, long) */
5974 helper_sig_long_long_long = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5975 helper_sig_long_long_long->params [0] = helper_sig_long_long_long->params [1] =
5976 &mono_defaults.int64_class->byval_arg;
5977 helper_sig_long_long_long->ret = &mono_defaults.int64_class->byval_arg;
5978 helper_sig_long_long_long->pinvoke = 1;
5980 /* object amethod (intptr) */
5981 helper_sig_obj_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5982 helper_sig_obj_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5983 helper_sig_obj_ptr->ret = &mono_defaults.object_class->byval_arg;
5984 helper_sig_obj_ptr->pinvoke = 1;
5986 /* void amethod (intptr) */
5987 helper_sig_void_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5988 helper_sig_void_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5989 helper_sig_void_ptr->ret = &mono_defaults.void_class->byval_arg;
5990 helper_sig_void_ptr->pinvoke = 1;
5992 /* void amethod (MonoObject *obj) */
5993 helper_sig_void_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5994 helper_sig_void_obj->params [0] = &mono_defaults.object_class->byval_arg;
5995 helper_sig_void_obj->ret = &mono_defaults.void_class->byval_arg;
5996 helper_sig_void_obj->pinvoke = 1;
5998 /* intptr amethod (void) */
5999 helper_sig_ptr_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
6000 helper_sig_ptr_void->ret = &mono_defaults.int_class->byval_arg;
6001 helper_sig_ptr_void->pinvoke = 1;
6003 /* object amethod (void) */
6004 helper_sig_obj_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
6005 helper_sig_obj_void->ret = &mono_defaults.object_class->byval_arg;
6006 helper_sig_obj_void->pinvoke = 1;
6008 /* void amethod (intptr, intptr) */
6009 helper_sig_void_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6010 helper_sig_void_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
6011 helper_sig_void_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
6012 helper_sig_void_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
6013 helper_sig_void_ptr_ptr->pinvoke = 1;
6015 /* void amethod (intptr, intptr, intptr) */
6016 helper_sig_void_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
6017 helper_sig_void_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
6018 helper_sig_void_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
6019 helper_sig_void_ptr_ptr_ptr->params [2] = &mono_defaults.int_class->byval_arg;
6020 helper_sig_void_ptr_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
6021 helper_sig_void_ptr_ptr_ptr->pinvoke = 1;
6023 /* intptr amethod (intptr, intptr) */
6024 helper_sig_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6025 helper_sig_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
6026 helper_sig_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
6027 helper_sig_ptr_ptr_ptr->ret = &mono_defaults.int_class->byval_arg;
6028 helper_sig_ptr_ptr_ptr->pinvoke = 1;
6030 /* IntPtr amethod (object) */
6031 helper_sig_ptr_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6032 helper_sig_ptr_obj->params [0] = &mono_defaults.object_class->byval_arg;
6033 helper_sig_ptr_obj->ret = &mono_defaults.int_class->byval_arg;
6034 helper_sig_ptr_obj->pinvoke = 1;
6036 /* IntPtr amethod (int) */
6037 helper_sig_ptr_int = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6038 helper_sig_ptr_int->params [0] = &mono_defaults.int32_class->byval_arg;
6039 helper_sig_ptr_int->ret = &mono_defaults.int_class->byval_arg;
6040 helper_sig_ptr_int->pinvoke = 1;
6042 /* long amethod (long, guint32) */
6043 helper_sig_long_long_int = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6044 helper_sig_long_long_int->params [0] = &mono_defaults.int64_class->byval_arg;
6045 helper_sig_long_long_int->params [1] = &mono_defaults.int32_class->byval_arg;
6046 helper_sig_long_long_int->ret = &mono_defaults.int64_class->byval_arg;
6047 helper_sig_long_long_int->pinvoke = 1;
6049 /* ulong amethod (double) */
6050 helper_sig_ulong_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6051 helper_sig_ulong_double->params [0] = &mono_defaults.double_class->byval_arg;
6052 helper_sig_ulong_double->ret = &mono_defaults.uint64_class->byval_arg;
6053 helper_sig_ulong_double->pinvoke = 1;
6055 /* long amethod (double) */
6056 helper_sig_long_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6057 helper_sig_long_double->params [0] = &mono_defaults.double_class->byval_arg;
6058 helper_sig_long_double->ret = &mono_defaults.int64_class->byval_arg;
6059 helper_sig_long_double->pinvoke = 1;
6061 /* double amethod (long) */
6062 helper_sig_double_long = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6063 helper_sig_double_long->params [0] = &mono_defaults.int64_class->byval_arg;
6064 helper_sig_double_long->ret = &mono_defaults.double_class->byval_arg;
6065 helper_sig_double_long->pinvoke = 1;
6067 /* double amethod (int) */
6068 helper_sig_double_int = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6069 helper_sig_double_int->params [0] = &mono_defaults.int32_class->byval_arg;
6070 helper_sig_double_int->ret = &mono_defaults.double_class->byval_arg;
6071 helper_sig_double_int->pinvoke = 1;
6073 /* float amethod (long) */
6074 helper_sig_float_long = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6075 helper_sig_float_long->params [0] = &mono_defaults.int64_class->byval_arg;
6076 helper_sig_float_long->ret = &mono_defaults.single_class->byval_arg;
6077 helper_sig_float_long->pinvoke = 1;
6079 /* double amethod (double, double) */
6080 helper_sig_double_double_double = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6081 helper_sig_double_double_double->params [0] = &mono_defaults.double_class->byval_arg;
6082 helper_sig_double_double_double->params [1] = &mono_defaults.double_class->byval_arg;
6083 helper_sig_double_double_double->ret = &mono_defaults.double_class->byval_arg;
6084 helper_sig_double_double_double->pinvoke = 1;
6086 /* uint amethod (double) */
6087 helper_sig_uint_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6088 helper_sig_uint_double->params [0] = &mono_defaults.double_class->byval_arg;
6089 helper_sig_uint_double->ret = &mono_defaults.uint32_class->byval_arg;
6090 helper_sig_uint_double->pinvoke = 1;
6092 /* int amethod (double) */
6093 helper_sig_int_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
6094 helper_sig_int_double->params [0] = &mono_defaults.double_class->byval_arg;
6095 helper_sig_int_double->ret = &mono_defaults.int32_class->byval_arg;
6096 helper_sig_int_double->pinvoke = 1;
6098 /* void initobj (intptr, int size) */
6099 helper_sig_initobj = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
6100 helper_sig_initobj->params [0] = &mono_defaults.int_class->byval_arg;
6101 helper_sig_initobj->params [1] = &mono_defaults.int32_class->byval_arg;
6102 helper_sig_initobj->ret = &mono_defaults.void_class->byval_arg;
6103 helper_sig_initobj->pinvoke = 1;
6105 /* void memcpy (intptr, intptr, int size) */
6106 helper_sig_memcpy = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
6107 helper_sig_memcpy->params [0] = &mono_defaults.int_class->byval_arg;
6108 helper_sig_memcpy->params [1] = &mono_defaults.int_class->byval_arg;
6109 helper_sig_memcpy->params [2] = &mono_defaults.int32_class->byval_arg;
6110 helper_sig_memcpy->ret = &mono_defaults.void_class->byval_arg;
6111 helper_sig_memcpy->pinvoke = 1;
6113 /* void memset (intptr, int val, int size) */
6114 helper_sig_memset = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
6115 helper_sig_memset->params [0] = &mono_defaults.int_class->byval_arg;
6116 helper_sig_memset->params [1] = &mono_defaults.int32_class->byval_arg;
6117 helper_sig_memset->params [2] = &mono_defaults.int32_class->byval_arg;
6118 helper_sig_memset->ret = &mono_defaults.void_class->byval_arg;
6119 helper_sig_memset->pinvoke = 1;
6121 helper_sig_class_init_trampoline = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
6122 helper_sig_class_init_trampoline->ret = &mono_defaults.void_class->byval_arg;
6123 helper_sig_class_init_trampoline->pinvoke = 1;
6126 static GHashTable *jit_icall_hash_name = NULL;
6127 static GHashTable *jit_icall_hash_addr = NULL;
6130 mono_find_jit_icall_by_name (const char *name)
6132 MonoJitICallInfo *info;
6133 g_assert (jit_icall_hash_name);
6135 //printf ("lookup addr %s %p\n", name, g_hash_table_lookup (jit_icall_hash_name, name));
6136 EnterCriticalSection (&trampoline_hash_mutex);
6137 info = g_hash_table_lookup (jit_icall_hash_name, name);
6138 LeaveCriticalSection (&trampoline_hash_mutex);
6143 mono_find_jit_icall_by_addr (gconstpointer addr)
6145 MonoJitICallInfo *info;
6146 g_assert (jit_icall_hash_addr);
6148 EnterCriticalSection (&trampoline_hash_mutex);
6149 info = g_hash_table_lookup (jit_icall_hash_addr, (gpointer)addr);
6150 LeaveCriticalSection (&trampoline_hash_mutex);
6156 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
6159 MonoMethod *wrapper;
6162 if (callinfo->wrapper)
6163 return callinfo->wrapper;
6165 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
6166 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
6167 /* Must be domain neutral since there is only one copy */
6168 code = mono_jit_compile_method_with_opt (wrapper, default_opt | MONO_OPT_SHARED);
6170 EnterCriticalSection (&trampoline_hash_mutex);
6171 if (!callinfo->wrapper) {
6172 callinfo->wrapper = code;
6173 g_hash_table_insert (jit_icall_hash_addr, (gpointer)callinfo->wrapper, callinfo);
6175 LeaveCriticalSection (&trampoline_hash_mutex);
6178 return callinfo->wrapper;
6182 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save)
6184 MonoJitICallInfo *info;
6189 EnterCriticalSection (&trampoline_hash_mutex);
6191 if (!jit_icall_hash_name) {
6192 jit_icall_hash_name = g_hash_table_new (g_str_hash, g_str_equal);
6193 jit_icall_hash_addr = g_hash_table_new (NULL, NULL);
6196 if (g_hash_table_lookup (jit_icall_hash_name, name)) {
6197 g_warning ("jit icall already defined \"%s\"\n", name);
6198 g_assert_not_reached ();
6201 info = g_new (MonoJitICallInfo, 1);
6208 #ifdef MONO_USE_EXC_TABLES
6209 || mono_arch_has_unwind_info (func)
6212 info->wrapper = func;
6214 info->wrapper = NULL;
6217 g_hash_table_insert (jit_icall_hash_name, (gpointer)info->name, info);
6218 g_hash_table_insert (jit_icall_hash_addr, (gpointer)func, info);
6220 LeaveCriticalSection (&trampoline_hash_mutex);
6225 mono_create_class_init_trampoline (MonoVTable *vtable)
6229 /* previously created trampoline code */
6230 mono_domain_lock (vtable->domain);
6232 mono_g_hash_table_lookup (vtable->domain->class_init_trampoline_hash,
6234 mono_domain_unlock (vtable->domain);
6238 code = mono_arch_create_class_init_trampoline (vtable);
6240 /* store trampoline address */
6241 mono_domain_lock (vtable->domain);
6242 mono_g_hash_table_insert (vtable->domain->class_init_trampoline_hash,
6244 mono_domain_unlock (vtable->domain);
6246 EnterCriticalSection (&trampoline_hash_mutex);
6247 if (!class_init_hash_addr)
6248 class_init_hash_addr = g_hash_table_new (NULL, NULL);
6249 g_hash_table_insert (class_init_hash_addr, code, vtable);
6250 LeaveCriticalSection (&trampoline_hash_mutex);
6256 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method,
6257 gboolean add_sync_wrapper)
6262 if (add_sync_wrapper && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
6263 return mono_create_jump_trampoline (domain, mono_marshal_get_synchronized_wrapper (method), FALSE);
6265 code = mono_jit_find_compiled_method (domain, method);
6269 EnterCriticalSection (&trampoline_hash_mutex);
6271 if (jump_trampoline_hash) {
6272 code = g_hash_table_lookup (jump_trampoline_hash, method);
6274 LeaveCriticalSection (&trampoline_hash_mutex);
6279 ji = mono_arch_create_jump_trampoline (method);
6282 * mono_delegate_ctor needs to find the method metadata from the
6283 * trampoline address, so we save it here.
6286 mono_jit_info_table_add (mono_root_domain, ji);
6288 if (!jump_trampoline_hash)
6289 jump_trampoline_hash = g_hash_table_new (NULL, NULL);
6290 g_hash_table_insert (jump_trampoline_hash, method, ji->code_start);
6292 LeaveCriticalSection (&trampoline_hash_mutex);
6294 return ji->code_start;
6298 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
6302 EnterCriticalSection (&trampoline_hash_mutex);
6303 if (class_init_hash_addr)
6304 res = g_hash_table_lookup (class_init_hash_addr, addr);
6307 LeaveCriticalSection (&trampoline_hash_mutex);
6312 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func, gboolean no_throw)
6314 MonoJitICallInfo *info;
6316 if (!emul_opcode_map)
6317 emul_opcode_map = g_new0 (MonoJitICallInfo*, OP_LAST + 1);
6319 g_assert (!sig->hasthis);
6320 g_assert (sig->param_count < 3);
6322 info = mono_register_jit_icall (func, name, sig, no_throw);
6324 emul_opcode_map [opcode] = info;
6328 decompose_foreach (MonoInst *tree, gpointer data)
6330 static MonoJitICallInfo *newarr_info = NULL;
6331 static MonoJitICallInfo *newarr_specific_info = NULL;
6332 MonoJitICallInfo *info;
6335 switch (tree->opcode) {
6337 MonoCompile *cfg = data;
6338 MonoInst *iargs [3];
6341 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
6342 g_assert (newarr_info);
6343 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
6344 g_assert (newarr_specific_info);
6347 if (cfg->opt & MONO_OPT_SHARED) {
6348 NEW_DOMAINCONST (cfg, iargs [0]);
6349 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
6350 iargs [2] = tree->inst_newa_len;
6355 MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
6357 NEW_VTABLECONST (cfg, iargs [0], vtable);
6358 iargs [1] = tree->inst_newa_len;
6360 info = newarr_specific_info;
6363 mono_emulate_opcode (cfg, tree, iargs, info);
6365 /* Need to decompose arguments after the the opcode is decomposed */
6366 for (i = 0; i < info->sig->param_count; ++i)
6367 dec_foreach (iargs [i], cfg);
6377 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
6379 switch (mono_burg_arity [tree->opcode]) {
6382 mono_inst_foreach (tree->inst_left, func, data);
6385 mono_inst_foreach (tree->inst_left, func, data);
6386 mono_inst_foreach (tree->inst_right, func, data);
6389 g_assert_not_reached ();
6396 mono_print_bb_code (MonoBasicBlock *bb) {
6398 MonoInst *c = bb->code;
6400 mono_print_tree (c);
6408 print_dfn (MonoCompile *cfg) {
6413 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
6415 for (i = 0; i < cfg->num_bblocks; ++i) {
6416 bb = cfg->bblocks [i];
6418 char* code1, *code2;
6419 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
6420 if (bb->last_ins->cil_code)
6421 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
6423 code2 = g_strdup ("");
6425 code1 [strlen (code1) - 1] = 0;
6426 code = g_strdup_printf ("%s -> %s", code1, code2);
6430 code = g_strdup ("\n");
6431 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
6433 MonoInst *c = bb->code;
6435 mono_print_tree (c);
6443 g_print ("\tprev:");
6444 for (j = 0; j < bb->in_count; ++j) {
6445 g_print (" BB%d", bb->in_bb [j]->block_num);
6447 g_print ("\t\tsucc:");
6448 for (j = 0; j < bb->out_count; ++j) {
6449 g_print (" BB%d", bb->out_bb [j]->block_num);
6451 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
6454 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
6457 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
6459 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
6467 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
6471 g_assert (bb->code);
6472 bb->last_ins->next = inst;
6473 bb->last_ins = inst;
6475 bb->last_ins = bb->code = inst;
6480 mono_destroy_compile (MonoCompile *cfg)
6482 //mono_mempool_stats (cfg->mempool);
6483 g_hash_table_destroy (cfg->bb_hash);
6485 mono_regstate_free (cfg->rs);
6487 g_hash_table_destroy (cfg->spvars);
6488 mono_mempool_destroy (cfg->mempool);
6489 g_list_free (cfg->ldstr_list);
6491 g_free (cfg->varinfo);
6497 mono_get_lmf_addr (void)
6499 MonoJitTlsData *jit_tls;
6501 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
6502 return &jit_tls->lmf;
6504 g_assert_not_reached ();
6509 * mono_thread_abort:
6510 * @obj: exception object
6512 * abort the thread, print exception information and stack trace
6515 mono_thread_abort (MonoObject *obj)
6517 /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
6519 /* handle_remove should be eventually called for this thread, too
6526 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
6528 MonoJitTlsData *jit_tls;
6531 jit_tls = TlsGetValue (mono_jit_tls_id);
6535 jit_tls = g_new0 (MonoJitTlsData, 1);
6537 TlsSetValue (mono_jit_tls_id, jit_tls);
6539 jit_tls->abort_func = abort_func;
6540 jit_tls->end_of_stack = stack_start;
6542 lmf = g_new0 (MonoLMF, 1);
6545 jit_tls->lmf = jit_tls->first_lmf = 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);
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);
6745 mono_class_init (handle_class);
6751 g_assert_not_reached ();
6754 return (gpointer)target;
6758 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
6759 MonoJitICallInfo *info;
6761 decompose_foreach (tree, cfg);
6763 switch (mono_burg_arity [tree->opcode]) {
6766 dec_foreach (tree->inst_left, cfg);
6768 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6769 MonoInst *iargs [2];
6771 iargs [0] = tree->inst_left;
6773 mono_emulate_opcode (cfg, tree, iargs, info);
6779 #ifdef MONO_ARCH_BIGMUL_INTRINS
6780 if (tree->opcode == OP_LMUL
6781 && (cfg->opt & MONO_OPT_INTRINS)
6782 && (tree->inst_left->opcode == CEE_CONV_I8
6783 || tree->inst_left->opcode == CEE_CONV_U8)
6784 && tree->inst_left->inst_left->type == STACK_I4
6785 && (tree->inst_right->opcode == CEE_CONV_I8
6786 || tree->inst_right->opcode == CEE_CONV_U8)
6787 && tree->inst_right->inst_left->type == STACK_I4) {
6788 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
6789 tree->inst_left = tree->inst_left->inst_left;
6790 tree->inst_right = tree->inst_right->inst_left;
6791 dec_foreach (tree, cfg);
6794 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6795 MonoInst *iargs [2];
6797 iargs [0] = tree->inst_i0;
6798 iargs [1] = tree->inst_i1;
6800 mono_emulate_opcode (cfg, tree, iargs, info);
6802 dec_foreach (iargs [0], cfg);
6803 dec_foreach (iargs [1], cfg);
6806 dec_foreach (tree->inst_left, cfg);
6807 dec_foreach (tree->inst_right, cfg);
6811 g_assert_not_reached ();
6816 decompose_pass (MonoCompile *cfg) {
6819 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6822 cfg->prev_ins = NULL;
6823 for (tree = cfg->cbb->code; tree; tree = tree->next) {
6824 dec_foreach (tree, cfg);
6825 cfg->prev_ins = tree;
6831 nullify_basic_block (MonoBasicBlock *bb)
6838 bb->code = bb->last_ins = NULL;
6839 bb->cil_code = NULL;
6843 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6847 for (i = 0; i < bb->out_count; i++) {
6848 MonoBasicBlock *ob = bb->out_bb [i];
6851 if (bb->out_count > 1) {
6852 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
6856 bb->out_bb [i] = repl;
6863 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6867 for (i = 0; i < bb->in_count; i++) {
6868 MonoBasicBlock *ib = bb->in_bb [i];
6871 if (bb->in_count > 1) {
6872 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
6876 bb->in_bb [i] = repl;
6883 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6887 for (i = 0; i < bb->out_count; i++) {
6888 MonoBasicBlock *ob = bb->out_bb [i];
6889 for (j = 0; j < ob->in_count; j++) {
6890 if (ob->in_bb [j] == orig) {
6891 ob->in_bb [j] = repl;
6900 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
6902 bb->out_count = bbn->out_count;
6903 bb->out_bb = bbn->out_bb;
6905 replace_basic_block (bb, bbn, bb);
6909 bb->last_ins->next = bbn->code;
6910 bb->last_ins = bbn->last_ins;
6913 bb->code = bbn->code;
6914 bb->last_ins = bbn->last_ins;
6916 bb->next_bb = bbn->next_bb;
6917 nullify_basic_block (bbn);
6921 * Optimizes the branches on the Control Flow Graph
6925 optimize_branches (MonoCompile *cfg)
6927 int i, changed = FALSE;
6928 MonoBasicBlock *bb, *bbn;
6929 guint32 niterations;
6932 * Some crazy loops could cause the code below to go into an infinite
6933 * loop, see bug #53003 for an example. To prevent this, we put an upper
6934 * bound on the number of iterations.
6941 /* we skip the entry block (exit is handled specially instead ) */
6942 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
6944 /* dont touch code inside exception clauses */
6945 if (bb->region != -1)
6948 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
6949 if (cfg->verbose_level > 2)
6950 g_print ("nullify block triggered %d\n", bbn->block_num);
6952 bb->next_bb = bbn->next_bb;
6954 for (i = 0; i < bbn->out_count; i++)
6955 replace_in_block (bbn->out_bb [i], bbn, NULL);
6957 nullify_basic_block (bbn);
6961 if (bb->out_count == 1) {
6962 bbn = bb->out_bb [0];
6964 /* conditional branches where true and false targets are the same can be also replaced with CEE_BR */
6965 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
6966 bb->last_ins->opcode = CEE_BR;
6967 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
6969 if (cfg->verbose_level > 2)
6970 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
6973 if (bb->region == bbn->region && bb->next_bb == bbn) {
6974 /* the block are in sequence anyway ... */
6976 /* branches to the following block can be removed */
6977 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
6978 bb->last_ins->opcode = CEE_NOP;
6980 if (cfg->verbose_level > 2)
6981 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
6984 if (bbn->in_count == 1) {
6986 if (bbn != cfg->bb_exit) {
6987 if (cfg->verbose_level > 2)
6988 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
6989 merge_basic_blocks (bb, bbn);
6993 //mono_print_bb_code (bb);
6998 } while (changed && (niterations > 0));
7005 /* we skip the entry block (exit is handled specially instead ) */
7006 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
7008 /* dont touch code inside exception clauses */
7009 if (bb->region != -1)
7012 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
7013 if (cfg->verbose_level > 2) {
7014 g_print ("nullify block triggered %d\n", bbn->block_num);
7016 bb->next_bb = bbn->next_bb;
7018 for (i = 0; i < bbn->out_count; i++)
7019 replace_in_block (bbn->out_bb [i], bbn, NULL);
7021 nullify_basic_block (bbn);
7027 if (bb->out_count == 1) {
7028 bbn = bb->out_bb [0];
7030 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
7031 bbn = bb->last_ins->inst_target_bb;
7032 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7033 bbn->code->inst_target_bb->region == bb->region) {
7035 if (cfg->verbose_level > 2)
7036 g_print ("in %s branch to branch triggered %d -> %d\n", cfg->method->name,
7037 bb->block_num, bbn->block_num);
7039 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
7040 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
7045 } else if (bb->out_count == 2) {
7046 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
7047 bbn = bb->last_ins->inst_true_bb;
7048 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7049 bbn->code->inst_target_bb->region == bb->region) {
7050 if (cfg->verbose_level > 2)
7051 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
7052 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
7055 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
7057 replace_in_block (bbn, bb, NULL);
7059 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
7060 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7062 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7068 bbn = bb->last_ins->inst_false_bb;
7069 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
7070 bbn->code->inst_target_bb->region == bb->region) {
7071 if (cfg->verbose_level > 2)
7072 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
7073 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
7076 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
7078 replace_in_block (bbn, bb, NULL);
7080 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
7081 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
7083 link_bblock (cfg, bb, bbn->code->inst_target_bb);
7091 } while (changed && (niterations > 0));
7096 mono_compile_create_vars (MonoCompile *cfg)
7098 MonoMethodSignature *sig;
7099 MonoMethodHeader *header;
7102 header = ((MonoMethodNormal *)cfg->method)->header;
7104 sig = cfg->method->signature;
7106 if (!MONO_TYPE_IS_VOID (sig->ret)) {
7107 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
7108 cfg->ret->opcode = OP_RETARG;
7109 cfg->ret->inst_vtype = sig->ret;
7110 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
7112 if (cfg->verbose_level > 2)
7113 g_print ("creating vars\n");
7116 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
7118 for (i = 0; i < sig->param_count; ++i)
7119 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
7121 cfg->locals_start = cfg->num_varinfo;
7123 if (cfg->verbose_level > 2)
7124 g_print ("creating locals\n");
7125 for (i = 0; i < header->num_locals; ++i)
7126 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
7127 if (cfg->verbose_level > 2)
7128 g_print ("locals done\n");
7132 mono_print_code (MonoCompile *cfg)
7136 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7137 MonoInst *tree = bb->code;
7142 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
7144 for (; tree; tree = tree->next) {
7145 mono_print_tree (tree);
7150 bb->last_ins->next = NULL;
7154 extern const char * const mono_burg_rule_string [];
7157 emit_state (MonoCompile *cfg, MBState *state, int goal)
7160 int ern = mono_burg_rule (state, goal);
7161 const guint16 *nts = mono_burg_nts [ern];
7164 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
7168 // state->reg1 = state->reg2; /* chain rule */
7170 state->reg1 = mono_regstate_next_int (cfg->rs);
7171 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
7174 state->reg1 = mono_regstate_next_int (cfg->rs);
7175 state->reg2 = mono_regstate_next_int (cfg->rs);
7178 state->reg1 = mono_regstate_next_float (cfg->rs);
7185 mono_burg_kids (state, ern, kids);
7187 emit_state (cfg, kids [0], nts [0]);
7189 emit_state (cfg, kids [1], nts [1]);
7191 g_assert (!nts [3]);
7192 emit_state (cfg, kids [2], nts [2]);
7197 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
7198 if ((emit = mono_burg_func [ern]))
7199 emit (state, state->tree, cfg);
7202 #define DEBUG_SELECTION
7205 mini_select_instructions (MonoCompile *cfg)
7207 static int reverse_map [] = {
7208 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
7209 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
7211 static int reverse_fmap [] = {
7212 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
7213 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
7215 static int reverse_lmap [] = {
7216 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
7217 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
7222 cfg->state_pool = mono_mempool_new ();
7223 cfg->rs = mono_regstate_new ();
7225 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7226 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode) &&
7227 bb->next_bb != bb->last_ins->inst_false_bb) {
7229 if (bb->next_bb == bb->last_ins->inst_true_bb) {
7230 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
7231 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
7232 bb->last_ins->inst_false_bb = tmp;
7234 if (bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
7235 bb->last_ins->opcode = reverse_map [bb->last_ins->opcode - CEE_BEQ];
7236 } else if (bb->last_ins->opcode >= OP_FBEQ && bb->last_ins->opcode <= OP_FBLT_UN) {
7237 bb->last_ins->opcode = reverse_fmap [bb->last_ins->opcode - OP_FBEQ];
7238 } else if (bb->last_ins->opcode >= OP_LBEQ && bb->last_ins->opcode <= OP_LBLT_UN) {
7239 bb->last_ins->opcode = reverse_lmap [bb->last_ins->opcode - OP_LBEQ];
7242 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
7243 inst->opcode = CEE_BR;
7244 inst->inst_target_bb = bb->last_ins->inst_false_bb;
7245 mono_bblock_add_inst (bb, inst);
7250 #ifdef DEBUG_SELECTION
7251 if (cfg->verbose_level >= 4) {
7252 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7253 MonoInst *tree = bb->code;
7254 g_print ("DUMP BLOCK %d:\n", bb->block_num);
7257 for (; tree; tree = tree->next) {
7258 mono_print_tree (tree);
7265 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7266 MonoInst *tree = bb->code, *next;
7272 bb->last_ins = NULL;
7275 mono_regstate_reset (cfg->rs);
7277 #ifdef DEBUG_SELECTION
7278 if (cfg->verbose_level >= 3)
7279 g_print ("LABEL BLOCK %d:\n", bb->block_num);
7281 for (; tree; tree = next) {
7283 #ifdef DEBUG_SELECTION
7284 if (cfg->verbose_level >= 3) {
7285 mono_print_tree (tree);
7290 if (!(mbstate = mono_burg_label (tree, cfg))) {
7291 g_warning ("unable to label tree %p", tree);
7292 mono_print_tree (tree);
7294 g_assert_not_reached ();
7296 emit_state (cfg, mbstate, MB_NTERM_stmt);
7298 bb->max_ireg = cfg->rs->next_vireg;
7299 bb->max_freg = cfg->rs->next_vfreg;
7302 bb->last_ins->next = NULL;
7304 mono_mempool_empty (cfg->state_pool);
7306 mono_mempool_destroy (cfg->state_pool);
7310 mono_codegen (MonoCompile *cfg)
7312 MonoJumpInfo *patch_info;
7314 int i, max_epilog_size;
7317 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7318 cfg->spill_count = 0;
7319 /* we reuse dfn here */
7320 /* bb->dfn = bb_count++; */
7321 mono_arch_local_regalloc (cfg, bb);
7324 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
7325 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
7327 code = mono_arch_emit_prolog (cfg);
7329 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7330 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
7332 cfg->code_len = code - cfg->native_code;
7333 cfg->prolog_end = cfg->code_len;
7335 mono_debug_open_method (cfg);
7337 /* emit code all basic blocks */
7338 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7339 bb->native_offset = cfg->code_len;
7340 mono_arch_output_basic_block (cfg, bb);
7342 cfg->bb_exit->native_offset = cfg->code_len;
7344 code = cfg->native_code + cfg->code_len;
7346 max_epilog_size = mono_arch_max_epilog_size (cfg);
7348 /* we always allocate code in cfg->domain->code_mp to increase locality */
7349 cfg->code_size = cfg->code_len + max_epilog_size;
7350 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
7352 mono_domain_lock (cfg->domain);
7353 code = mono_code_manager_reserve (cfg->domain->code_mp, cfg->code_size);
7354 mono_domain_unlock (cfg->domain);
7356 memcpy (code, cfg->native_code, cfg->code_len);
7357 g_free (cfg->native_code);
7358 cfg->native_code = code;
7359 code = cfg->native_code + cfg->code_len;
7361 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
7363 cfg->epilog_begin = cfg->code_len;
7365 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7366 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
7368 cfg->code_len = code - cfg->native_code;
7370 mono_arch_emit_epilog (cfg);
7372 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7373 switch (patch_info->type) {
7374 case MONO_PATCH_INFO_ABS: {
7375 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
7377 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
7378 if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) &&
7379 strstr (cfg->method->name, info->name))
7381 * This is an icall wrapper, and this is a call to the
7386 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
7387 patch_info->data.name = info->name;
7391 MonoVTable *vtable = mono_find_class_init_trampoline_by_addr (patch_info->data.target);
7393 patch_info->type = MONO_PATCH_INFO_CLASS_INIT;
7394 patch_info->data.klass = vtable->klass;
7399 case MONO_PATCH_INFO_SWITCH: {
7401 mono_domain_lock (cfg->domain);
7402 table = mono_code_manager_reserve (cfg->domain->code_mp, sizeof (gpointer) * patch_info->table_size);
7403 mono_domain_unlock (cfg->domain);
7405 patch_info->ip.i = patch_info->ip.label->inst_c0;
7406 for (i = 0; i < patch_info->table_size; i++) {
7407 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
7409 patch_info->data.target = table;
7418 if (cfg->verbose_level > 0)
7419 g_print ("Method %s emitted at %p to %p [%s]\n",
7420 mono_method_full_name (cfg->method, TRUE),
7421 cfg->native_code, cfg->native_code + cfg->code_len, cfg->domain->friendly_name);
7423 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info, cfg->run_cctors);
7425 mono_domain_lock (cfg->domain);
7426 mono_code_manager_commit (cfg->domain->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
7427 mono_domain_unlock (cfg->domain);
7429 mono_arch_flush_icache (cfg->native_code, cfg->code_len);
7431 mono_debug_close_method (cfg);
7435 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
7440 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
7441 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
7443 if (cp->opcode == OP_ICONST) {
7444 if (cfg->opt & MONO_OPT_CONSPROP) {
7445 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
7449 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
7450 if (cfg->opt & MONO_OPT_COPYPROP) {
7451 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
7457 arity = mono_burg_arity [tree->opcode];
7460 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
7461 if (cfg->opt & MONO_OPT_CFOLD)
7462 mono_constant_fold_inst (tree, NULL);
7463 /* The opcode may have changed */
7464 if (mono_burg_arity [tree->opcode] > 1) {
7465 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
7466 if (cfg->opt & MONO_OPT_CFOLD)
7467 mono_constant_fold_inst (tree, NULL);
7469 mono_constant_fold_inst (tree, NULL);
7475 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
7479 switch (tree->opcode) {
7489 if (tree->ssa_op == MONO_SSA_NOP) {
7490 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7507 case OP_VOIDCALL_REG:
7508 case OP_VOIDCALLVIRT:
7510 MonoCallInst *call = (MonoCallInst *)tree;
7511 MonoMethodSignature *sig = call->signature;
7512 int i, byref = FALSE;
7514 for (i = 0; i < sig->param_count; i++) {
7515 if (sig->params [i]->byref) {
7522 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7530 arity = mono_burg_arity [tree->opcode];
7536 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7539 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7540 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
7543 g_assert_not_reached ();
7548 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
7550 MonoInst *tree = bb->code;
7556 for (; tree; tree = tree->next) {
7558 mono_cprop_copy_values (cfg, tree, acp);
7560 mono_cprop_invalidate_values (tree, acp, acp_size);
7562 if (tree->ssa_op == MONO_SSA_STORE &&
7563 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
7564 MonoInst *i1 = tree->inst_i1;
7566 acp [tree->inst_i0->inst_c0] = NULL;
7568 for (i = 0; i < acp_size; i++) {
7569 if (acp [i] && acp [i]->opcode != OP_ICONST &&
7570 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
7575 if (i1->opcode == OP_ICONST) {
7576 acp [tree->inst_i0->inst_c0] = i1;
7577 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
7579 if (i1->ssa_op == MONO_SSA_LOAD &&
7580 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
7581 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
7582 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
7583 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
7588 if (tree->opcode == CEE_BEQ) {
7589 g_assert (tree->inst_i0->opcode == OP_COMPARE);
7590 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
7591 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
7593 tree->opcode = CEE_BR;
7594 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
7595 tree->inst_target_bb = tree->inst_true_bb;
7597 tree->inst_target_bb = tree->inst_false_bb;
7606 mono_local_cprop (MonoCompile *cfg)
7611 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
7613 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7614 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
7615 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
7620 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, int parts)
7622 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
7623 guint8 *ip = (guint8 *)header->code;
7626 int dfn = 0, i, code_size_ratio;
7628 mono_jit_stats.methods_compiled++;
7629 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
7630 mono_profiler_method_jit (method);
7632 cfg = g_new0 (MonoCompile, 1);
7633 cfg->method = method;
7634 cfg->mempool = mono_mempool_new ();
7636 cfg->prof_options = mono_profiler_get_events ();
7637 cfg->run_cctors = run_cctors;
7638 cfg->bb_hash = g_hash_table_new (NULL, NULL);
7639 cfg->domain = domain;
7640 cfg->verbose_level = mini_verbose;
7641 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
7642 ((MonoMethodNormal *)method)->header->max_stack);
7644 if (cfg->verbose_level > 2)
7645 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
7648 * create MonoInst* which represents arguments and local variables
7650 mono_compile_create_vars (cfg);
7652 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
7653 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7654 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
7655 mono_destroy_compile (cfg);
7659 mono_jit_stats.basic_blocks += cfg->num_bblocks;
7660 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
7662 if (cfg->num_varinfo > 2000) {
7664 * we disable some optimizations if there are too many variables
7665 * because JIT time may become too expensive. The actual number needs
7666 * to be tweaked and eventually the non-linear algorithms should be fixed.
7668 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
7669 cfg->disable_ssa = TRUE;
7671 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
7673 /* Depth-first ordering on basic blocks */
7674 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
7676 if (cfg->opt & MONO_OPT_BRANCH)
7677 optimize_branches (cfg);
7679 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
7680 if (cfg->num_bblocks != dfn + 1) {
7683 cfg->num_bblocks = dfn + 1;
7685 if (!header->clauses) {
7686 /* remove unreachable code, because the code in them may be
7687 * inconsistent (access to dead variables for example) */
7688 for (bb = cfg->bb_entry; bb;) {
7689 MonoBasicBlock *bbn = bb->next_bb;
7691 if (bbn && bbn->region == -1 && !bbn->dfn) {
7692 if (cfg->verbose_level > 1)
7693 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
7694 bb->next_bb = bbn->next_bb;
7695 nullify_basic_block (bbn);
7703 if (cfg->opt & MONO_OPT_LOOP) {
7704 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
7705 mono_compute_natural_loops (cfg);
7708 /* after method_to_ir */
7712 //#define DEBUGSSA "logic_run"
7713 #define DEBUGSSA_CLASS "Tests"
7716 if (!header->num_clauses && !cfg->disable_ssa) {
7717 mono_local_cprop (cfg);
7718 mono_ssa_compute (cfg);
7722 /* fixme: add all optimizations which requires SSA */
7723 if (cfg->opt & (MONO_OPT_DEADCE)) {
7724 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
7725 mono_local_cprop (cfg);
7726 mono_ssa_compute (cfg);
7728 if (cfg->verbose_level >= 2) {
7735 /* after SSA translation */
7739 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
7740 if (cfg->comp_done & MONO_COMP_SSA) {
7741 mono_ssa_cprop (cfg);
7743 mono_local_cprop (cfg);
7747 if (cfg->comp_done & MONO_COMP_SSA) {
7748 mono_ssa_deadce (cfg);
7750 //mono_ssa_strength_reduction (cfg);
7752 mono_ssa_remove (cfg);
7754 if (cfg->opt & MONO_OPT_BRANCH)
7755 optimize_branches (cfg);
7758 /* after SSA removal */
7762 decompose_pass (cfg);
7764 if (cfg->opt & MONO_OPT_LINEARS) {
7767 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
7768 cfg->comp_done &= ~MONO_COMP_LIVENESS;
7769 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
7770 mono_analyze_liveness (cfg);
7772 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
7773 regs = mono_arch_get_global_int_regs (cfg);
7774 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
7778 //mono_print_code (cfg);
7782 /* variables are allocated after decompose, since decompose could create temps */
7783 mono_arch_allocate_vars (cfg);
7785 if (cfg->opt & MONO_OPT_CFOLD)
7786 mono_constant_fold (cfg);
7788 mini_select_instructions (cfg);
7791 if (cfg->verbose_level >= 2) {
7792 char *id = mono_method_full_name (cfg->method, FALSE);
7793 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
7797 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
7799 jinfo = g_new0 (MonoJitInfo, 1);
7800 jinfo->method = method;
7801 jinfo->code_start = cfg->native_code;
7802 jinfo->code_size = cfg->code_len;
7803 jinfo->used_regs = cfg->used_int_regs;
7804 jinfo->domain_neutral = (cfg->opt & MONO_OPT_SHARED) != 0;
7806 if (header->num_clauses) {
7809 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
7810 jinfo->num_clauses = header->num_clauses;
7811 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
7812 sizeof (MonoJitExceptionInfo) * header->num_clauses);
7814 for (i = 0; i < header->num_clauses; i++) {
7815 MonoExceptionClause *ec = &header->clauses [i];
7816 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
7817 MonoBasicBlock *tblock;
7819 ei->flags = ec->flags;
7821 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7822 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->token_or_filter);
7824 ei->data.filter = cfg->native_code + tblock->native_offset;
7826 ei->data.token = ec->token_or_filter;
7829 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
7831 ei->try_start = cfg->native_code + tblock->native_offset;
7832 g_assert (tblock->native_offset);
7833 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
7835 ei->try_end = cfg->native_code + tblock->native_offset;
7836 g_assert (tblock->native_offset);
7837 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
7839 ei->handler_start = cfg->native_code + tblock->native_offset;
7843 cfg->jit_info = jinfo;
7845 mono_jit_info_table_add (cfg->domain, jinfo);
7847 /* collect statistics */
7848 mono_jit_stats.allocated_code_size += cfg->code_len;
7849 code_size_ratio = cfg->code_len;
7850 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
7851 mono_jit_stats.biggest_method_size = code_size_ratio;
7852 mono_jit_stats.biggest_method = method;
7854 code_size_ratio = (code_size_ratio * 100) / ((MonoMethodNormal *)method)->header->code_size;
7855 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
7856 mono_jit_stats.max_code_size_ratio = code_size_ratio;
7857 mono_jit_stats.max_ratio_method = method;
7859 mono_jit_stats.native_code_size += cfg->code_len;
7861 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7862 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
7868 mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain)
7871 GHashTable *jit_code_hash;
7872 gpointer code = NULL;
7878 jit_code_hash = target_domain->jit_code_hash;
7880 #ifdef MONO_USE_AOT_COMPILER
7881 if (!mono_compile_aot && (opt & MONO_OPT_AOT)) {
7883 MonoDomain *domain = mono_domain_get ();
7885 mono_domain_lock (domain);
7887 mono_class_init (method->klass);
7888 if ((info = mono_aot_get_method (domain, method))) {
7889 g_hash_table_insert (domain->jit_code_hash, method, info);
7890 mono_domain_unlock (domain);
7891 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
7892 return info->code_start;
7895 mono_domain_unlock (domain);
7899 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
7900 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
7903 if (!method->addr) {
7904 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
7905 method->addr = mono_lookup_internal_call (method);
7907 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
7908 mono_lookup_pinvoke_call (method, NULL, NULL);
7910 #ifdef MONO_USE_EXC_TABLES
7911 if (mono_method_blittable (method)) {
7912 return method->addr;
7915 nm = mono_marshal_get_native_wrapper (method);
7916 return mono_compile_method (nm);
7918 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
7919 //mono_debug_add_wrapper (method, nm);
7920 #ifdef MONO_USE_EXC_TABLES
7923 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
7924 const char *name = method->name;
7927 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
7928 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
7929 /* FIXME: uhm, we need a wrapper to handle exceptions? */
7930 return (gpointer)mono_delegate_ctor;
7931 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
7932 nm = mono_marshal_get_delegate_invoke (method);
7933 return mono_jit_compile_method (nm);
7934 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
7935 nm = mono_marshal_get_delegate_begin_invoke (method);
7936 return mono_jit_compile_method (nm);
7937 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
7938 nm = mono_marshal_get_delegate_end_invoke (method);
7939 return mono_jit_compile_method (nm);
7945 cfg = mini_method_compile (method, opt, target_domain, TRUE, 0);
7947 mono_domain_lock (target_domain);
7949 /* Check if some other thread already did the job. In this case, we can
7950 discard the code this thread generated. */
7952 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
7953 /* We can't use a domain specific method in another domain */
7954 if ((target_domain == mono_domain_get ()) || info->domain_neutral) {
7955 code = info->code_start;
7956 // printf("Discarding code for method %s\n", method->name);
7961 g_hash_table_insert (jit_code_hash, method, cfg->jit_info);
7962 code = cfg->native_code;
7965 mono_destroy_compile (cfg);
7967 if (target_domain->jump_target_hash) {
7968 MonoJumpInfo patch_info;
7970 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
7972 patch_info.next = NULL;
7973 patch_info.ip.i = 0;
7974 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
7975 patch_info.data.method = method;
7976 g_hash_table_remove (target_domain->jump_target_hash, method);
7978 for (tmp = list; tmp; tmp = tmp->next)
7979 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info, TRUE);
7980 g_slist_free (list);
7983 mono_domain_unlock (target_domain);
7985 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
7990 mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt)
7992 /* FIXME: later copy the code from mono */
7993 MonoDomain *target_domain, *domain = mono_domain_get ();
7997 if (default_opt & MONO_OPT_SHARED)
7998 target_domain = mono_root_domain;
8000 target_domain = domain;
8002 mono_domain_lock (target_domain);
8004 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
8005 /* We can't use a domain specific method in another domain */
8006 if (! ((domain != target_domain) && !info->domain_neutral)) {
8007 mono_domain_unlock (target_domain);
8008 mono_jit_stats.methods_lookups++;
8009 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
8010 return info->code_start;
8014 mono_domain_unlock (target_domain);
8015 p = mono_jit_compile_method_inner (method, target_domain);
8020 mono_jit_compile_method (MonoMethod *method)
8022 return mono_jit_compile_method_with_opt (method, default_opt);
8026 mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method)
8028 MonoDomain *target_domain;
8031 if (default_opt & MONO_OPT_SHARED)
8032 target_domain = mono_root_domain;
8034 target_domain = domain;
8036 mono_domain_lock (target_domain);
8038 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
8039 /* We can't use a domain specific method in another domain */
8040 if (! ((domain != target_domain) && !info->domain_neutral)) {
8041 mono_domain_unlock (target_domain);
8042 mono_jit_stats.methods_lookups++;
8043 return info->code_start;
8047 mono_domain_unlock (target_domain);
8053 * mono_jit_runtime_invoke:
8054 * @method: the method to invoke
8055 * @obj: this pointer
8056 * @params: array of parameter values.
8057 * @exc: used to catch exceptions objects
8060 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
8063 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc);
8065 if (obj == NULL && !(method->flags & METHOD_ATTRIBUTE_STATIC) && !method->string_ctor && (method->wrapper_type == 0)) {
8066 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
8070 invoke = mono_marshal_get_runtime_invoke (method);
8071 runtime_invoke = mono_jit_compile_method (invoke);
8072 return runtime_invoke (obj, params, exc);
8075 #ifdef PLATFORM_WIN32
8076 #define GET_CONTEXT \
8077 struct sigcontext *ctx = (struct sigcontext*)_dummy;
8080 #define GET_CONTEXT \
8081 void *ctx = context;
8083 #define GET_CONTEXT \
8084 void **_p = (void **)&_dummy; \
8085 struct sigcontext *ctx = (struct sigcontext *)++_p;
8089 #ifdef MONO_ARCH_USE_SIGACTION
8090 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, siginfo_t *info, void *context)
8092 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy)
8096 SIG_HANDLER_SIGNATURE (sigfpe_signal_handler)
8101 exc = mono_get_exception_divide_by_zero ();
8103 mono_arch_handle_exception (ctx, exc, FALSE);
8107 SIG_HANDLER_SIGNATURE (sigill_signal_handler)
8111 exc = mono_get_exception_execution_engine ("SIGILL");
8113 mono_arch_handle_exception (ctx, exc, FALSE);
8116 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8119 sigsegv_signal_handler (int _dummy, siginfo_t *info, void *context)
8122 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
8123 struct sigcontext *ctx = (struct sigcontext *)&(((ucontext_t*)context)->uc_mcontext);
8125 /* Can't allocate memory using Boehm GC on altstack */
8126 if (jit_tls->stack_size &&
8127 ((guint8*)info->si_addr >= (guint8*)jit_tls->end_of_stack - jit_tls->stack_size) &&
8128 ((guint8*)info->si_addr < (guint8*)jit_tls->end_of_stack))
8129 exc = mono_domain_get ()->stack_overflow_ex;
8131 exc = mono_domain_get ()->null_reference_ex;
8133 mono_arch_handle_exception (ctx, exc, FALSE);
8139 SIG_HANDLER_SIGNATURE (sigsegv_signal_handler)
8143 mono_arch_handle_exception (ctx, NULL, FALSE);
8149 SIG_HANDLER_SIGNATURE (sigusr1_signal_handler)
8154 thread = mono_thread_current ();
8156 thread->abort_exc = mono_get_exception_thread_abort ();
8158 mono_arch_handle_exception (ctx, thread->abort_exc, FALSE);
8162 SIG_HANDLER_SIGNATURE (sigquit_signal_handler)
8167 exc = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
8169 mono_arch_handle_exception (ctx, exc, FALSE);
8173 SIG_HANDLER_SIGNATURE (sigint_signal_handler)
8178 exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
8180 mono_arch_handle_exception (ctx, exc, FALSE);
8183 #ifndef PLATFORM_WIN32
8185 add_signal_handler (int signo, gpointer handler)
8187 struct sigaction sa;
8189 #ifdef MONO_ARCH_USE_SIGACTION
8190 sa.sa_sigaction = handler;
8191 sigemptyset (&sa.sa_mask);
8192 sa.sa_flags = SA_SIGINFO;
8194 sa.sa_handler = handler;
8195 sigemptyset (&sa.sa_mask);
8198 g_assert (sigaction (signo, &sa, NULL) != -1);
8203 mono_runtime_install_handlers (void)
8205 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8206 struct sigaction sa;
8209 #ifdef PLATFORM_WIN32
8211 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
8212 win32_seh_set_handler(SIGILL, sigill_signal_handler);
8213 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
8214 if (getenv ("MONO_DEBUG"))
8215 win32_seh_set_handler(SIGINT, sigint_signal_handler);
8216 #else /* !PLATFORM_WIN32 */
8218 /* libpthreads has its own implementation of sigaction(),
8219 * but it seems to work well with our current exception
8220 * handlers. If not we must call syscall directly instead
8223 if (getenv ("MONO_DEBUG")) {
8224 add_signal_handler (SIGINT, sigint_signal_handler);
8227 add_signal_handler (SIGFPE, sigfpe_signal_handler);
8228 add_signal_handler (SIGQUIT, sigquit_signal_handler);
8229 add_signal_handler (SIGILL, sigill_signal_handler);
8230 add_signal_handler (mono_thread_get_abort_signal (), sigusr1_signal_handler);
8233 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8234 sa.sa_sigaction = sigsegv_signal_handler;
8235 sigemptyset (&sa.sa_mask);
8236 sa.sa_flags = SA_SIGINFO | SA_STACK;
8237 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
8239 add_signal_handler (SIGSEGV, sigsegv_signal_handler);
8242 #endif /* PLATFORM_WIN32 */
8245 /* mono_jit_create_remoting_trampoline:
8246 * @method: pointer to the method info
8248 * Creates a trampoline which calls the remoting functions. This
8249 * is used in the vtable of transparent proxies.
8251 * Returns: a pointer to the newly created code
8254 mono_jit_create_remoting_trampoline (MonoMethod *method)
8257 guint8 *addr = NULL;
8259 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
8260 (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
8261 nm = mono_marshal_get_remoting_invoke (method);
8262 addr = mono_compile_method (nm);
8264 addr = mono_compile_method (method);
8270 mini_init (const char *filename)
8274 mono_arch_cpu_init ();
8276 if (!g_thread_supported ())
8277 g_thread_init (NULL);
8279 mono_jit_tls_id = TlsAlloc ();
8280 setup_jit_tls_data ((gpointer)-1, mono_thread_abort);
8282 InitializeCriticalSection (&trampoline_hash_mutex);
8286 if (default_opt & MONO_OPT_AOT)
8289 mono_runtime_install_handlers ();
8290 mono_threads_install_cleanup (mini_thread_cleanup);
8292 #define JIT_TRAMPOLINES_WORK
8293 #ifdef JIT_TRAMPOLINES_WORK
8294 mono_install_compile_method (mono_jit_compile_method);
8295 mono_install_trampoline (mono_arch_create_jit_trampoline);
8296 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
8298 #define JIT_INVOKE_WORKS
8299 #ifdef JIT_INVOKE_WORKS
8300 mono_install_runtime_invoke (mono_jit_runtime_invoke);
8301 mono_install_handler (mono_arch_get_throw_exception ());
8303 mono_install_stack_walk (mono_jit_walk_stack);
8304 mono_install_get_config_dir ();
8306 domain = mono_init (filename);
8309 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
8310 ves_icall_get_frame_info);
8311 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
8312 ves_icall_get_trace);
8313 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
8314 mono_runtime_install_handlers);
8317 create_helper_signature ();
8319 #define JIT_CALLS_WORK
8320 #ifdef JIT_CALLS_WORK
8321 /* Needs to be called here since register_jit_icall depends on it */
8322 mono_marshal_init ();
8324 mono_arch_register_lowlevel_calls ();
8325 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
8326 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
8327 mono_register_jit_icall (mono_trace_enter_method, "mono_trace_enter_method", NULL, TRUE);
8328 mono_register_jit_icall (mono_trace_leave_method, "mono_trace_leave_method", NULL, TRUE);
8330 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
8331 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
8333 /* fixme: we cant hanlde vararg methods this way, because the signature is not constant */
8334 //mono_register_jit_icall (ves_array_element_address, "ves_array_element_address", NULL);
8335 //mono_register_jit_icall (mono_array_new_va, "mono_array_new_va", NULL);
8337 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
8338 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
8339 helper_sig_void_ptr, TRUE);
8340 mono_register_jit_icall (mono_thread_get_pending_exception, "mono_thread_get_pending_exception", helper_sig_obj_void, FALSE);
8343 * NOTE, NOTE, NOTE, NOTE:
8344 * when adding emulation for some opcodes, remember to also add a dummy
8345 * rule to the burg files, because we need the arity information to be correct.
8347 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult, TRUE);
8348 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un, FALSE);
8349 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf, FALSE);
8350 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv, FALSE);
8351 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un, FALSE);
8352 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem, FALSE);
8353 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un, FALSE);
8355 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl, TRUE);
8356 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr, TRUE);
8357 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un, TRUE);
8359 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8, FALSE);
8360 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4, FALSE);
8361 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8, FALSE);
8362 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8, FALSE);
8364 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
8365 mono_register_opcode_emulation (OP_FCONV_TO_I8, "__emul_fconv_to_i8", helper_sig_long_double, mono_fconv_i8, FALSE);
8367 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
8368 mono_register_opcode_emulation (CEE_CONV_R_UN, "__emul_conv_r_un", helper_sig_double_int, mono_conv_to_r8_un, FALSE);
8370 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
8371 mono_register_opcode_emulation (OP_LCONV_TO_R8, "__emul_lconv_to_r8", helper_sig_double_long, mono_lconv_to_r8, FALSE);
8373 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
8374 mono_register_opcode_emulation (OP_LCONV_TO_R4, "__emul_lconv_to_r4", helper_sig_float_long, mono_lconv_to_r4, FALSE);
8376 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
8377 mono_register_opcode_emulation (OP_LCONV_TO_R_UN, "__emul_lconv_to_r8_un", helper_sig_double_long, mono_lconv_to_r8_un, FALSE);
8379 #ifdef MONO_ARCH_EMULATE_FREM
8380 mono_register_opcode_emulation (OP_FREM, "__emul_frem", helper_sig_double_double_double, fmod, FALSE);
8383 #if SIZEOF_VOID_P == 4
8384 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4, TRUE);
8387 #warning "fixme: add opcode emulation"
8391 /* other jit icalls */
8392 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
8393 helper_sig_ptr_ptr_ptr, FALSE);
8394 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
8395 mono_register_jit_icall (mono_get_special_static_data, "mono_get_special_static_data", helper_sig_ptr_int, FALSE);
8396 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
8397 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
8398 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
8399 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
8400 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
8401 mono_register_jit_icall (helper_stelem_ref_check, "helper_stelem_ref_check", helper_sig_stelem_ref_check, FALSE);
8402 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
8403 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
8404 mono_register_jit_icall (mono_object_new_fast, "mono_object_new_fast", helper_sig_object_new_specific, FALSE);
8405 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
8406 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
8407 mono_register_jit_icall (mono_string_to_utf16, "mono_string_to_utf16", helper_sig_ptr_obj, FALSE);
8408 mono_register_jit_icall (mono_string_from_utf16, "mono_string_from_utf16", helper_sig_obj_ptr, FALSE);
8409 mono_register_jit_icall (mono_string_new_wrapper, "mono_string_new_wrapper", helper_sig_obj_ptr, FALSE);
8410 mono_register_jit_icall (mono_string_to_utf8, "mono_string_to_utf8", helper_sig_ptr_obj, FALSE);
8411 mono_register_jit_icall (mono_string_to_bstr, "mono_string_to_bstr", helper_sig_ptr_obj, FALSE);
8412 mono_register_jit_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", helper_sig_ptr_obj, FALSE);
8413 mono_register_jit_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", helper_sig_ptr_obj, FALSE);
8414 mono_register_jit_icall (mono_string_builder_to_utf16, "mono_string_builder_to_utf16", helper_sig_ptr_obj, FALSE);
8415 mono_register_jit_icall (mono_array_to_savearray, "mono_array_to_savearray", helper_sig_ptr_obj, FALSE);
8416 mono_register_jit_icall (mono_array_to_lparray, "mono_array_to_lparray", helper_sig_ptr_obj, FALSE);
8417 mono_register_jit_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", helper_sig_ptr_obj, FALSE);
8418 mono_register_jit_icall (mono_marshal_string_array, "mono_marshal_string_array", helper_sig_ptr_obj, FALSE);
8419 mono_register_jit_icall (mono_string_utf8_to_builder, "mono_string_utf8_to_builder", helper_sig_void_ptr_ptr, FALSE);
8420 mono_register_jit_icall (mono_string_utf16_to_builder, "mono_string_utf16_to_builder", helper_sig_void_ptr_ptr, FALSE);
8421 mono_register_jit_icall (mono_marshal_free_array, "mono_marshal_free_array", helper_sig_void_ptr_ptr, FALSE);
8422 mono_register_jit_icall (mono_string_to_byvalstr, "mono_string_to_byvalstr", helper_sig_void_ptr_ptr_ptr, FALSE);
8423 mono_register_jit_icall (mono_string_to_byvalwstr, "mono_string_to_byvalwstr", helper_sig_void_ptr_ptr_ptr, FALSE);
8424 mono_register_jit_icall (g_free, "g_free", helper_sig_void_ptr, FALSE);
8425 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
8426 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
8427 mono_register_jit_icall (mono_ldftn_nosync, "mono_ldftn_nosync", helper_sig_compile, FALSE);
8428 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
8431 #define JIT_RUNTIME_WORKS
8432 #ifdef JIT_RUNTIME_WORKS
8433 mono_runtime_install_cleanup ((MonoDomainFunc)mini_cleanup);
8434 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
8437 mono_thread_attach (domain);
8441 MonoJitStats mono_jit_stats = {0};
8444 print_jit_stats (void)
8446 if (mono_jit_stats.enabled) {
8447 g_print ("Mono Jit statistics\n");
8448 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
8449 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
8450 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
8451 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
8452 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
8453 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
8454 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
8455 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
8456 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
8457 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
8458 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
8459 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
8460 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
8461 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
8462 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
8463 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
8464 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
8465 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
8467 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
8468 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
8469 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
8470 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
8471 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
8476 mini_cleanup (MonoDomain *domain)
8479 * mono_runtime_cleanup() and mono_domain_finalize () need to
8480 * be called early since they need the execution engine still
8481 * fully working (mono_domain_finalize may invoke managed finalizers
8482 * and mono_runtime_cleanup will wait for other threads to finish).
8484 mono_domain_finalize (domain, -1);
8486 mono_runtime_cleanup (domain);
8488 mono_profiler_shutdown ();
8490 mono_debug_cleanup ();
8492 #ifdef PLATFORM_WIN32
8493 win32_seh_cleanup();
8496 mono_domain_free (domain, TRUE);
8502 mono_set_defaults (int verbose_level, guint32 opts)
8504 mini_verbose = verbose_level;
8509 mono_precompile_assembly (MonoAssembly *ass, void *user_data)
8511 MonoImage *image = ass->image;
8515 if (mini_verbose > 0)
8516 printf ("PRECOMPILE: %s.\n", ass->image->name);
8518 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
8519 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
8520 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
8522 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
8526 if (mini_verbose > 1) {
8527 char * desc = mono_method_full_name (method, TRUE);
8528 g_print ("Compiling %d %s\n", count, desc);
8531 mono_compile_method (method);
8535 void mono_precompile_assemblies ()
8537 mono_assembly_foreach ((GFunc)mono_precompile_assembly, NULL);