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>
49 #include "jit-icalls.c"
51 #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))
53 #define MONO_CHECK_THIS(ins) (cfg->method->signature->hasthis && (ins)->ssa_op == MONO_SSA_LOAD && (ins)->inst_left->inst_c0 == 0)
55 gboolean mono_arch_handle_exception (struct sigcontext *ctx, gpointer obj, gboolean test_only);
56 static gpointer mono_jit_compile_method (MonoMethod *method);
58 static void handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src,
59 const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native);
61 static void dec_foreach (MonoInst *tree, MonoCompile *cfg);
63 static int mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
64 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
65 guint inline_offset, gboolean is_virtual_call);
67 extern guint8 mono_burg_arity [];
68 /* helper methods signature */
69 static MonoMethodSignature *helper_sig_long_long_long = NULL;
70 static MonoMethodSignature *helper_sig_long_long_int = NULL;
71 static MonoMethodSignature *helper_sig_newarr = NULL;
72 static MonoMethodSignature *helper_sig_newarr_specific = NULL;
73 static MonoMethodSignature *helper_sig_ldstr = NULL;
74 static MonoMethodSignature *helper_sig_domain_get = NULL;
75 static MonoMethodSignature *helper_sig_object_new = NULL;
76 static MonoMethodSignature *helper_sig_object_new_specific = NULL;
77 static MonoMethodSignature *helper_sig_compile = NULL;
78 static MonoMethodSignature *helper_sig_compile_virt = NULL;
79 static MonoMethodSignature *helper_sig_obj_ptr = NULL;
80 static MonoMethodSignature *helper_sig_obj_void = NULL;
81 static MonoMethodSignature *helper_sig_ptr_void = NULL;
82 static MonoMethodSignature *helper_sig_void_ptr = NULL;
83 static MonoMethodSignature *helper_sig_void_obj = NULL;
84 static MonoMethodSignature *helper_sig_void_ptr_ptr = NULL;
85 static MonoMethodSignature *helper_sig_void_ptr_ptr_ptr = NULL;
86 static MonoMethodSignature *helper_sig_ptr_ptr_ptr = NULL;
87 static MonoMethodSignature *helper_sig_ptr_obj = NULL;
88 static MonoMethodSignature *helper_sig_ptr_int = NULL;
89 static MonoMethodSignature *helper_sig_initobj = NULL;
90 static MonoMethodSignature *helper_sig_memcpy = NULL;
91 static MonoMethodSignature *helper_sig_memset = NULL;
92 static MonoMethodSignature *helper_sig_ulong_double = NULL;
93 static MonoMethodSignature *helper_sig_long_double = NULL;
94 static MonoMethodSignature *helper_sig_double_long = NULL;
95 static MonoMethodSignature *helper_sig_float_long = NULL;
96 static MonoMethodSignature *helper_sig_double_double_double = NULL;
97 static MonoMethodSignature *helper_sig_uint_double = NULL;
98 static MonoMethodSignature *helper_sig_int_double = NULL;
99 static MonoMethodSignature *helper_sig_stelem_ref = NULL;
100 static MonoMethodSignature *helper_sig_stelem_ref_check = NULL;
101 static MonoMethodSignature *helper_sig_class_init_trampoline = NULL;
102 static MonoMethodSignature *helper_sig_obj_obj_cls = NULL;
104 static guint32 default_opt = MONO_OPT_PEEPHOLE;
106 guint32 mono_jit_tls_id = -1;
107 MonoTraceSpec *mono_jit_trace_calls = NULL;
108 gboolean mono_break_on_exc = FALSE;
109 gboolean mono_compile_aot = FALSE;
111 static int mini_verbose = 0;
113 static CRITICAL_SECTION class_init_hash_mutex;
115 static GHashTable *class_init_hash_addr = NULL;
117 #ifdef MONO_USE_EXC_TABLES
119 mono_type_blittable (MonoType *type)
139 case MONO_TYPE_VALUETYPE:
140 case MONO_TYPE_CLASS:
141 return type->data.klass->blittable;
151 mono_method_blittable (MonoMethod *method)
153 MonoMethodSignature *sig;
159 if (!mono_arch_has_unwind_info (method->addr)) {
163 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
166 sig = method->signature;
168 if (!mono_type_blittable (sig->ret))
171 for (i = 0; i < sig->param_count; i++)
172 if (!mono_type_blittable (sig->params [i]))
175 if (mono_method_has_marshal_info (method))
178 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && ((MonoMethodPInvoke*)method)->piflags & PINVOKE_ATTRIBUTE_SUPPORTS_LAST_ERROR)
186 G_GNUC_UNUSED static void
187 print_method_from_ip (void *ip)
192 MonoDomain *domain = mono_domain_get ();
194 ji = mono_jit_info_table_find (domain, ip);
196 g_print ("No method at %p\n", ip);
199 method = mono_method_full_name (ji->method, TRUE);
200 source = mono_debug_source_location_from_address (ji->method, (int) ip, NULL, domain);
202 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);
205 g_print ("%s\n", source);
213 * mono_method_same_domain:
215 * Determine whenever two compiled methods are in the same domain, thus
216 * the address of the callee can be embedded in the caller.
218 gboolean mono_method_same_domain (MonoJitInfo *caller, MonoJitInfo *callee)
220 if (!caller || !callee)
224 * If the call was made from domain-neutral to domain-specific
225 * code, we can't patch the call site.
227 if (caller->domain_neutral && !callee->domain_neutral)
230 if ((caller->method->klass == mono_defaults.appdomain_class) &&
231 (strstr (caller->method->name, "InvokeInDomain"))) {
232 /* The InvokeInDomain methods change the current appdomain */
240 mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token)
242 MonoJumpInfoToken *res = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoToken));
249 #define MONO_INIT_VARINFO(vi,id) do { \
250 (vi)->range.first_use.pos.bid = 0xffff; \
256 * Basic blocks have two numeric identifiers:
257 * dfn: Depth First Number
258 * block_num: unique ID assigned at bblock creation
260 #define NEW_BBLOCK(cfg) (mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)))
261 #define ADD_BBLOCK(cfg,bbhash,b) do { \
262 g_hash_table_insert (bbhash, (b)->cil_code, (b)); \
263 (b)->block_num = cfg->num_bblocks++; \
264 (b)->real_offset = real_offset; \
267 #define GET_BBLOCK(cfg,bbhash,tblock,ip) do { \
268 (tblock) = g_hash_table_lookup (bbhash, (ip)); \
270 if ((ip) >= end || (ip) < header->code) goto unverified; \
271 (tblock) = NEW_BBLOCK (cfg); \
272 (tblock)->cil_code = (ip); \
273 ADD_BBLOCK (cfg, (bbhash), (tblock)); \
275 (tblock)->real_offset = real_offset; \
278 #define CHECK_BBLOCK(target,ip,tblock) do { \
279 if ((target) < (ip) && !(tblock)->code) { \
280 bb_recheck = g_list_prepend (bb_recheck, (tblock)); \
281 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); \
285 #define NEW_ICONST(cfg,dest,val) do { \
286 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
287 (dest)->opcode = OP_ICONST; \
288 (dest)->inst_c0 = (val); \
289 (dest)->type = STACK_I4; \
292 /* FIXME: have a different definition of NEW_PCONST for 64 bit systems */
293 #define NEW_PCONST(cfg,dest,val) do { \
294 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
295 (dest)->opcode = OP_ICONST; \
296 (dest)->inst_p0 = (val); \
297 (dest)->type = STACK_PTR; \
300 #define NEW_CLASSCONST(cfg,dest,val) do { \
301 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
302 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
303 (dest)->inst_p0 = (val); \
304 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_CLASS; \
305 (dest)->type = STACK_PTR; \
308 #define NEW_IMAGECONST(cfg,dest,val) do { \
309 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
310 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
311 (dest)->inst_p0 = (val); \
312 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_IMAGE; \
313 (dest)->type = STACK_PTR; \
316 #define NEW_FIELDCONST(cfg,dest,field) do { \
317 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
318 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
319 (dest)->inst_p0 = (field); \
320 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_FIELD; \
321 (dest)->type = STACK_PTR; \
324 #define NEW_METHODCONST(cfg,dest,val) do { \
325 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
326 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
327 (dest)->inst_p0 = (val); \
328 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_METHODCONST; \
329 (dest)->type = STACK_PTR; \
332 #define NEW_VTABLECONST(cfg,dest,vtable) do { \
333 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
334 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
335 (dest)->inst_p0 = mono_compile_aot ? (gpointer)((vtable)->klass) : (vtable); \
336 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_VTABLE; \
337 (dest)->type = STACK_PTR; \
340 #define NEW_SFLDACONST(cfg,dest,field) do { \
341 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
342 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
343 (dest)->inst_p0 = (field); \
344 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_SFLDA; \
345 (dest)->type = STACK_PTR; \
348 #define NEW_LDSTRCONST(cfg,dest,image,token) do { \
349 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
350 (dest)->opcode = OP_AOTCONST; \
351 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
352 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_LDSTR; \
353 (dest)->type = STACK_OBJ; \
356 #define NEW_TYPE_FROM_HANDLE_CONST(cfg,dest,image,token) do { \
357 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
358 (dest)->opcode = OP_AOTCONST; \
359 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
360 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_TYPE_FROM_HANDLE; \
361 (dest)->type = STACK_OBJ; \
364 #define NEW_LDTOKENCONST(cfg,dest,image,token) do { \
365 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
366 (dest)->opcode = OP_AOTCONST; \
367 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
368 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_LDTOKEN; \
369 (dest)->type = STACK_PTR; \
372 #define NEW_DOMAINCONST(cfg,dest) do { \
373 if (cfg->opt & MONO_OPT_SHARED) { \
374 NEW_TEMPLOAD (cfg, dest, mono_get_domainvar (cfg)->inst_c0); \
376 NEW_PCONST (cfg, dest, (cfg)->domain); \
380 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
382 #define NEW_ARGLOAD(cfg,dest,num) do { \
383 if (arg_array [(num)]->opcode == OP_ICONST) (dest) = arg_array [(num)]; else { \
384 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
385 (dest)->ssa_op = MONO_SSA_LOAD; \
386 (dest)->inst_i0 = arg_array [(num)]; \
387 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
388 type_to_eval_stack_type (param_types [(num)], (dest)); \
389 (dest)->klass = (dest)->inst_i0->klass; \
392 #define NEW_LOCLOAD(cfg,dest,num) do { \
393 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
394 (dest)->ssa_op = MONO_SSA_LOAD; \
395 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
396 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
397 type_to_eval_stack_type (header->locals [(num)], (dest)); \
398 (dest)->klass = (dest)->inst_i0->klass; \
401 #define NEW_LOCLOADA(cfg,dest,num) do { \
402 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
403 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
404 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
405 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
406 (dest)->opcode = OP_LDADDR; \
407 (dest)->type = STACK_MP; \
408 (dest)->klass = (dest)->inst_i0->klass; \
409 if (!MONO_TYPE_ISSTRUCT (header->locals [(num)])) \
410 (cfg)->disable_ssa = TRUE; \
413 #define NEW_RETLOADA(cfg,dest) do { \
414 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
415 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
416 (dest)->inst_i0 = (cfg)->ret; \
417 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
418 (dest)->opcode = CEE_LDIND_I; \
419 (dest)->type = STACK_MP; \
420 (dest)->klass = (dest)->inst_i0->klass; \
421 (cfg)->disable_ssa = TRUE; \
424 #define NEW_ARGLOADA(cfg,dest,num) do { \
425 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
426 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
427 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
428 (dest)->inst_i0 = arg_array [(num)]; \
429 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
430 (dest)->opcode = OP_LDADDR; \
431 (dest)->type = STACK_MP; \
432 (dest)->klass = (dest)->inst_i0->klass; \
433 (cfg)->disable_ssa = TRUE; \
436 #define NEW_TEMPLOAD(cfg,dest,num) do { \
437 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
438 (dest)->ssa_op = MONO_SSA_LOAD; \
439 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
440 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
441 type_to_eval_stack_type ((dest)->inst_i0->inst_vtype, (dest)); \
442 (dest)->klass = (dest)->inst_i0->klass; \
445 #define NEW_TEMPLOADA(cfg,dest,num) do { \
446 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
447 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
448 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
449 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
450 (dest)->opcode = OP_LDADDR; \
451 (dest)->type = STACK_MP; \
452 (dest)->klass = (dest)->inst_i0->klass; \
453 if (!MONO_TYPE_ISSTRUCT (cfg->varinfo [(num)]->inst_vtype)) \
454 (cfg)->disable_ssa = TRUE; \
458 #define NEW_INDLOAD(cfg,dest,addr,vtype) do { \
459 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
460 (dest)->inst_left = addr; \
461 (dest)->opcode = mono_type_to_ldind (vtype); \
462 type_to_eval_stack_type (vtype, (dest)); \
463 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
466 #define NEW_INDSTORE(cfg,dest,addr,value,vtype) do { \
467 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
468 (dest)->inst_i0 = addr; \
469 (dest)->opcode = mono_type_to_stind (vtype); \
470 (dest)->inst_i1 = (value); \
471 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
474 #define NEW_TEMPSTORE(cfg,dest,num,inst) do { \
475 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
476 (dest)->ssa_op = MONO_SSA_STORE; \
477 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
478 (dest)->opcode = mono_type_to_stind ((dest)->inst_i0->inst_vtype); \
479 (dest)->inst_i1 = (inst); \
480 (dest)->klass = (dest)->inst_i0->klass; \
483 #define NEW_LOCSTORE(cfg,dest,num,inst) do { \
484 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
485 (dest)->opcode = mono_type_to_stind (header->locals [(num)]); \
486 (dest)->ssa_op = MONO_SSA_STORE; \
487 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
488 (dest)->inst_i1 = (inst); \
489 (dest)->klass = (dest)->inst_i0->klass; \
492 #define NEW_ARGSTORE(cfg,dest,num,inst) do { \
493 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
494 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
495 (dest)->opcode = mono_type_to_stind (param_types [(num)]); \
496 (dest)->ssa_op = MONO_SSA_STORE; \
497 (dest)->inst_i0 = arg_array [(num)]; \
498 (dest)->inst_i1 = (inst); \
499 (dest)->klass = (dest)->inst_i0->klass; \
502 #define ADD_BINOP(op) do { \
503 MONO_INST_NEW (cfg, ins, (op)); \
504 ins->cil_code = ip; \
506 ins->inst_i0 = sp [0]; \
507 ins->inst_i1 = sp [1]; \
509 type_from_op (ins); \
513 #define ADD_UNOP(op) do { \
514 MONO_INST_NEW (cfg, ins, (op)); \
515 ins->cil_code = ip; \
517 ins->inst_i0 = sp [0]; \
519 type_from_op (ins); \
523 #define ADD_BINCOND(next_block) do { \
525 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
527 cmp->inst_i0 = sp [0]; \
528 cmp->inst_i1 = sp [1]; \
529 cmp->cil_code = ins->cil_code; \
530 type_from_op (cmp); \
532 ins->inst_i0 = cmp; \
533 MONO_ADD_INS (bblock, ins); \
534 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
535 GET_BBLOCK (cfg, bbhash, tblock, target); \
536 link_bblock (cfg, bblock, tblock); \
537 ins->inst_true_bb = tblock; \
538 CHECK_BBLOCK (target, ip, tblock); \
539 if ((next_block)) { \
540 link_bblock (cfg, bblock, (next_block)); \
541 ins->inst_false_bb = (next_block); \
542 start_new_bblock = 1; \
544 GET_BBLOCK (cfg, bbhash, tblock, ip); \
545 link_bblock (cfg, bblock, tblock); \
546 ins->inst_false_bb = tblock; \
547 start_new_bblock = 2; \
551 /* FIXME: handle float, long ... */
552 #define ADD_UNCOND(istrue) do { \
554 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
556 cmp->inst_i0 = sp [0]; \
557 switch (cmp->inst_i0->type) { \
559 cmp->inst_i1 = zero_int64; break; \
561 cmp->inst_i1 = zero_r8; break; \
564 cmp->inst_i1 = zero_ptr; break; \
566 cmp->inst_i1 = zero_obj; break; \
568 cmp->inst_i1 = zero_int32; \
570 cmp->cil_code = ins->cil_code; \
571 type_from_op (cmp); \
573 ins->inst_i0 = cmp; \
574 ins->opcode = (istrue)? CEE_BNE_UN: CEE_BEQ; \
575 MONO_ADD_INS (bblock, ins); \
576 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
577 GET_BBLOCK (cfg, bbhash, tblock, target); \
578 link_bblock (cfg, bblock, tblock); \
579 ins->inst_true_bb = tblock; \
580 CHECK_BBLOCK (target, ip, tblock); \
581 GET_BBLOCK (cfg, bbhash, tblock, ip); \
582 link_bblock (cfg, bblock, tblock); \
583 ins->inst_false_bb = tblock; \
584 start_new_bblock = 2; \
587 #define NEW_LDELEMA(cfg,dest,sp,k) do { \
588 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
589 (dest)->opcode = CEE_LDELEMA; \
590 (dest)->inst_left = (sp) [0]; \
591 (dest)->inst_right = (sp) [1]; \
592 (dest)->type = STACK_MP; \
593 (dest)->klass = (k); \
596 #define NEW_GROUP(cfg,dest,el1,el2) do { \
597 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
598 (dest)->opcode = OP_GROUP; \
599 (dest)->inst_left = (el1); \
600 (dest)->inst_right = (el2); \
605 compare_bblock (gconstpointer a, gconstpointer b)
607 const MonoBasicBlock *b1 = a;
608 const MonoBasicBlock *b2 = b;
610 return b2->cil_code - b1->cil_code;
615 * link_bblock: Links two basic blocks
617 * links two basic blocks in the control flow graph, the 'from'
618 * argument is the starting block and the 'to' argument is the block
619 * the control flow ends to after 'from'.
622 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
624 MonoBasicBlock **newa;
628 if (from->cil_code) {
630 g_print ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
632 g_print ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
635 g_print ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
637 g_print ("edge from entry to exit\n");
641 for (i = 0; i < from->out_count; ++i) {
642 if (to == from->out_bb [i]) {
648 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
649 for (i = 0; i < from->out_count; ++i) {
650 newa [i] = from->out_bb [i];
658 for (i = 0; i < to->in_count; ++i) {
659 if (from == to->in_bb [i]) {
665 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
666 for (i = 0; i < to->in_count; ++i) {
667 newa [i] = to->in_bb [i];
676 * We mark each basic block with a region ID. We use that to avoid BB
677 * optimizations when blocks are in different regions.
680 mono_find_block_region (MonoCompile *cfg, int offset, int *filter_lengths)
682 MonoMethod *method = cfg->method;
683 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
684 MonoExceptionClause *clause;
687 /* first search for handlers and filters */
688 for (i = 0; i < header->num_clauses; ++i) {
689 clause = &header->clauses [i];
690 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->token_or_filter) &&
691 (offset < (clause->token_or_filter + filter_lengths [i])))
692 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
694 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
695 if (clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)
696 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
698 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
702 /* search the try blocks */
703 for (i = 0; i < header->num_clauses; ++i) {
704 clause = &header->clauses [i];
705 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
706 return ((i + 1) << 8) | clause->flags;
713 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
715 MonoMethod *method = cfg->method;
716 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
717 MonoExceptionClause *clause;
718 MonoBasicBlock *handler;
722 for (i = 0; i < header->num_clauses; ++i) {
723 clause = &header->clauses [i];
724 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
725 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
726 if (clause->flags == type) {
727 handler = g_hash_table_lookup (cfg->bb_hash, header->code + clause->handler_offset);
729 res = g_list_append (res, handler);
737 mono_find_spvar_for_region (MonoCompile *cfg, int region)
739 return g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
743 mono_create_spvar_for_region (MonoCompile *cfg, int region)
747 var = g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
751 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
752 /* prevent it from being register allocated */
753 var->flags |= MONO_INST_INDIRECT;
755 g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
759 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
763 array [*dfn] = start;
764 /*g_print ("visit %d at %p\n", *dfn, start->cil_code);*/
765 for (i = 0; i < start->out_count; ++i) {
766 if (start->out_bb [i]->dfn)
769 start->out_bb [i]->dfn = *dfn;
770 start->out_bb [i]->df_parent = start;
771 array [*dfn] = start->out_bb [i];
772 df_visit (start->out_bb [i], dfn, array);
778 MonoBasicBlock *best;
782 previous_foreach (gconstpointer key, gpointer val, gpointer data)
784 PrevStruct *p = data;
785 MonoBasicBlock *bb = val;
786 //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,
787 //bb->method == p->best->method, bb->cil_code < p->code, bb->cil_code > p->best->cil_code);
789 if (bb->cil_code && bb->cil_code < p->code && bb->cil_code > p->best->cil_code)
793 static MonoBasicBlock*
794 find_previous (GHashTable *bb_hash, MonoBasicBlock *start, const guchar *code) {
800 g_hash_table_foreach (bb_hash, (GHFunc)previous_foreach, &p);
805 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
814 * FIXME: take into account all the details:
815 * second may have been the target of more than one bblock
817 second->out_count = first->out_count;
818 second->out_bb = first->out_bb;
820 for (i = 0; i < first->out_count; ++i) {
821 bb = first->out_bb [i];
822 for (j = 0; j < bb->in_count; ++j) {
823 if (bb->in_bb [j] == first)
824 bb->in_bb [j] = second;
828 first->out_count = 0;
829 first->out_bb = NULL;
830 link_bblock (cfg, first, second);
832 second->last_ins = first->last_ins;
834 /*g_print ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
835 for (inst = first->code; inst && inst->next; inst = inst->next) {
836 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
837 g_print ("found %p: %s", inst->next->cil_code, code);
839 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
840 second->code = inst->next;
842 first->last_ins = inst;
843 second->next_bb = first->next_bb;
844 first->next_bb = second;
849 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
855 mono_type_to_ldind (MonoType *type)
867 case MONO_TYPE_BOOLEAN:
881 case MONO_TYPE_FNPTR:
883 case MONO_TYPE_CLASS:
884 case MONO_TYPE_STRING:
885 case MONO_TYPE_OBJECT:
886 case MONO_TYPE_SZARRAY:
887 case MONO_TYPE_ARRAY:
888 return CEE_LDIND_REF;
896 case MONO_TYPE_VALUETYPE:
897 if (type->data.klass->enumtype) {
898 t = type->data.klass->enum_basetype->type;
902 case MONO_TYPE_TYPEDBYREF:
904 case MONO_TYPE_GENERICINST:
905 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE)
907 return CEE_LDIND_REF;
909 g_error ("unknown type 0x%02x in type_to_ldind", type->type);
915 mono_type_to_stind (MonoType *type)
926 case MONO_TYPE_BOOLEAN:
938 case MONO_TYPE_FNPTR:
940 case MONO_TYPE_CLASS:
941 case MONO_TYPE_STRING:
942 case MONO_TYPE_OBJECT:
943 case MONO_TYPE_SZARRAY:
944 case MONO_TYPE_ARRAY:
945 return CEE_STIND_REF;
953 case MONO_TYPE_VALUETYPE:
954 if (type->data.klass->enumtype) {
955 t = type->data.klass->enum_basetype->type;
959 case MONO_TYPE_TYPEDBYREF:
961 case MONO_TYPE_GENERICINST:
962 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE)
964 return CEE_STIND_REF;
966 g_error ("unknown type 0x%02x in type_to_stind", type->type);
972 * Returns the type used in the eval stack when @type is loaded.
973 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
976 type_to_eval_stack_type (MonoType *type, MonoInst *inst) {
980 inst->type = STACK_MP;
988 case MONO_TYPE_BOOLEAN:
994 inst->type = STACK_I4;
999 case MONO_TYPE_FNPTR:
1000 inst->type = STACK_PTR;
1002 case MONO_TYPE_CLASS:
1003 case MONO_TYPE_STRING:
1004 case MONO_TYPE_OBJECT:
1005 case MONO_TYPE_SZARRAY:
1006 case MONO_TYPE_ARRAY:
1007 inst->type = STACK_OBJ;
1011 inst->type = STACK_I8;
1015 inst->type = STACK_R8;
1017 case MONO_TYPE_VALUETYPE:
1018 if (type->data.klass->enumtype) {
1019 t = type->data.klass->enum_basetype->type;
1022 inst->klass = type->data.klass;
1023 inst->type = STACK_VTYPE;
1026 case MONO_TYPE_TYPEDBYREF:
1027 inst->klass = mono_defaults.typed_reference_class;
1028 inst->type = STACK_VTYPE;
1030 case MONO_TYPE_GENERICINST:
1031 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE) {
1032 inst->klass = mono_class_from_mono_type (type);
1033 inst->type = STACK_VTYPE;
1035 inst->type = STACK_OBJ;
1039 g_error ("unknown type 0x%02x in eval stack type", type->type);
1044 * The following tables are used to quickly validate the IL code in type_from_op ().
1047 bin_num_table [STACK_MAX] [STACK_MAX] = {
1048 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1049 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1050 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1051 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
1052 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
1053 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
1054 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1055 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1060 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
1063 /* reduce the size of this table */
1065 bin_int_table [STACK_MAX] [STACK_MAX] = {
1066 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1067 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1068 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1069 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1070 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1071 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1072 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1073 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1077 bin_comp_table [STACK_MAX] [STACK_MAX] = {
1079 {0, 1, 0, 1, 0, 0, 4, 0},
1080 {0, 0, 1, 0, 0, 0, 0, 0},
1081 {0, 1, 0, 1, 0, 2, 4, 0},
1082 {0, 0, 0, 0, 1, 0, 0, 0},
1083 {0, 0, 0, 2, 0, 1, 0, 0},
1084 {0, 4, 0, 4, 0, 0, 3, 0},
1085 {0, 0, 0, 0, 0, 0, 0, 0},
1088 /* reduce the size of this table */
1090 shift_table [STACK_MAX] [STACK_MAX] = {
1091 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1092 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1093 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1094 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1095 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1096 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1097 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, 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}
1102 * Tables to map from the non-specific opcode to the matching
1103 * type-specific opcode.
1105 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
1106 static const guint16
1107 binops_op_map [STACK_MAX] = {
1108 0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, 0
1111 /* handles from CEE_NEG to CEE_CONV_U8 */
1112 static const guint16
1113 unops_op_map [STACK_MAX] = {
1114 0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, 0
1117 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
1118 static const guint16
1119 ovfops_op_map [STACK_MAX] = {
1120 0, 0, OP_LCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_FCONV_TO_U2-CEE_CONV_U2, 0
1123 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
1124 static const guint16
1125 ovf2ops_op_map [STACK_MAX] = {
1126 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
1129 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
1130 static const guint16
1131 ovf3ops_op_map [STACK_MAX] = {
1132 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
1135 /* handles from CEE_CEQ to CEE_CLT_UN */
1136 static const guint16
1137 ceqops_op_map [STACK_MAX] = {
1138 0, 0, OP_LCEQ-CEE_CEQ, OP_PCEQ-CEE_CEQ, OP_FCEQ-CEE_CEQ, 0
1142 * Sets ins->type (the type on the eval stack) according to the
1143 * type of the opcode and the arguments to it.
1144 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1146 * FIXME: this function sets ins->type unconditionally in some cases, but
1147 * it should set it to invalid for some types (a conv.x on an object)
1150 type_from_op (MonoInst *ins) {
1151 switch (ins->opcode) {
1158 /* FIXME: check unverifiable args for STACK_MP */
1159 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1160 ins->opcode += binops_op_map [ins->type];
1167 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1168 ins->opcode += binops_op_map [ins->type];
1173 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1174 ins->opcode += binops_op_map [ins->type];
1177 /* FIXME: handle some specifics with ins->next->type */
1178 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1182 case 256+CEE_CGT_UN:
1184 case 256+CEE_CLT_UN:
1185 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1186 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1190 ins->type = neg_table [ins->inst_i0->type];
1191 ins->opcode += unops_op_map [ins->type];
1194 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1195 ins->type = ins->inst_i0->type;
1197 ins->type = STACK_INV;
1198 ins->opcode += unops_op_map [ins->type];
1204 ins->type = STACK_I4;
1205 ins->opcode += unops_op_map [ins->inst_i0->type];
1208 ins->type = STACK_R8;
1209 switch (ins->inst_i0->type) {
1214 ins->opcode = OP_LCONV_TO_R_UN;
1218 case CEE_CONV_OVF_I1:
1219 case CEE_CONV_OVF_U1:
1220 case CEE_CONV_OVF_I2:
1221 case CEE_CONV_OVF_U2:
1222 case CEE_CONV_OVF_I4:
1223 case CEE_CONV_OVF_U4:
1224 ins->type = STACK_I4;
1225 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1227 case CEE_CONV_OVF_I_UN:
1228 case CEE_CONV_OVF_U_UN:
1229 ins->type = STACK_PTR;
1230 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1232 case CEE_CONV_OVF_I1_UN:
1233 case CEE_CONV_OVF_I2_UN:
1234 case CEE_CONV_OVF_I4_UN:
1235 case CEE_CONV_OVF_U1_UN:
1236 case CEE_CONV_OVF_U2_UN:
1237 case CEE_CONV_OVF_U4_UN:
1238 ins->type = STACK_I4;
1239 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1242 ins->type = STACK_PTR;
1243 switch (ins->inst_i0->type) {
1249 ins->opcode = OP_LCONV_TO_U;
1252 ins->opcode = OP_FCONV_TO_U;
1258 ins->type = STACK_I8;
1259 ins->opcode += unops_op_map [ins->inst_i0->type];
1261 case CEE_CONV_OVF_I8:
1262 case CEE_CONV_OVF_U8:
1263 ins->type = STACK_I8;
1264 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1266 case CEE_CONV_OVF_U8_UN:
1267 case CEE_CONV_OVF_I8_UN:
1268 ins->type = STACK_I8;
1269 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1273 ins->type = STACK_R8;
1274 ins->opcode += unops_op_map [ins->inst_i0->type];
1277 ins->type = STACK_R8;
1281 ins->type = STACK_I4;
1282 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1285 case CEE_CONV_OVF_I:
1286 case CEE_CONV_OVF_U:
1287 ins->type = STACK_PTR;
1288 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1291 case CEE_ADD_OVF_UN:
1293 case CEE_MUL_OVF_UN:
1295 case CEE_SUB_OVF_UN:
1296 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1297 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1300 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1307 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_MP, STACK_R8, STACK_R8, STACK_OBJ
1310 /* map ldelem.x to the matching ldind.x opcode */
1312 ldelem_to_ldind [] = {
1326 /* map stelem.x to the matching stind.x opcode */
1328 stelem_to_stind [] = {
1342 param_table [STACK_MAX] [STACK_MAX] = {
1347 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1351 switch (args->type) {
1361 for (i = 0; i < sig->param_count; ++i) {
1362 switch (args [i].type) {
1366 if (!sig->params [i]->byref)
1370 if (sig->params [i]->byref)
1372 switch (sig->params [i]->type) {
1373 case MONO_TYPE_CLASS:
1374 case MONO_TYPE_STRING:
1375 case MONO_TYPE_OBJECT:
1376 case MONO_TYPE_SZARRAY:
1377 case MONO_TYPE_ARRAY:
1384 if (sig->params [i]->byref)
1386 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1395 /*if (!param_table [args [i].type] [sig->params [i]->type])
1403 * When we need a pointer to the current domain many times in a method, we
1404 * call mono_domain_get() once and we store the result in a local variable.
1405 * This function returns the variable that represents the MonoDomain*.
1407 inline static MonoInst *
1408 mono_get_domainvar (MonoCompile *cfg)
1410 if (!cfg->domainvar)
1411 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1412 return cfg->domainvar;
1416 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
1419 int num = cfg->num_varinfo;
1421 if ((num + 1) >= cfg->varinfo_count) {
1422 cfg->varinfo_count = (cfg->varinfo_count + 2) * 2;
1423 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
1424 cfg->vars = (MonoMethodVar **)g_realloc (cfg->vars, sizeof (MonoMethodVar*) * cfg->varinfo_count);
1427 /*g_print ("created temp %d of type 0x%x\n", num, type->type);*/
1428 mono_jit_stats.allocate_var++;
1430 MONO_INST_NEW (cfg, inst, opcode);
1431 inst->inst_c0 = num;
1432 inst->inst_vtype = type;
1433 inst->klass = mono_class_from_mono_type (type);
1434 /* if set to 1 the variable is native */
1437 cfg->varinfo [num] = inst;
1439 cfg->vars [num] = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMethodVar));
1440 MONO_INIT_VARINFO (cfg->vars [num], num);
1443 //g_print ("created temp %d of type %s\n", num, mono_type_get_name (type));
1448 type_from_stack_type (MonoInst *ins) {
1449 switch (ins->type) {
1450 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1451 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1452 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1453 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1454 case STACK_MP: return &mono_defaults.int_class->byval_arg;
1455 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1456 case STACK_VTYPE: return &ins->klass->byval_arg;
1458 g_error ("stack type %d to montype not handled\n", ins->type);
1464 array_access_to_klass (int opcode)
1468 return mono_defaults.byte_class;
1470 return mono_defaults.uint16_class;
1473 return mono_defaults.int_class;
1476 return mono_defaults.sbyte_class;
1479 return mono_defaults.int16_class;
1482 return mono_defaults.int32_class;
1484 return mono_defaults.uint32_class;
1487 return mono_defaults.int64_class;
1490 return mono_defaults.single_class;
1493 return mono_defaults.double_class;
1494 case CEE_LDELEM_REF:
1495 case CEE_STELEM_REF:
1496 return mono_defaults.object_class;
1498 g_assert_not_reached ();
1504 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
1508 MONO_ADD_INS (bb, inst);
1511 switch (bb->last_ins->opcode) {
1524 while (prev->next && prev->next != bb->last_ins)
1526 if (prev == bb->code) {
1527 if (bb->last_ins == bb->code) {
1528 inst->next = bb->code;
1531 inst->next = prev->next;
1535 inst->next = bb->last_ins;
1539 // g_warning ("handle conditional jump in add_ins_to_end ()\n");
1541 MONO_ADD_INS (bb, inst);
1547 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
1549 MonoInst *inst, *load;
1551 NEW_TEMPLOAD (cfg, load, src);
1553 NEW_TEMPSTORE (cfg, inst, dest, load);
1554 if (inst->opcode == CEE_STOBJ) {
1555 NEW_TEMPLOADA (cfg, inst, dest);
1556 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE);
1558 inst->cil_code = NULL;
1559 mono_add_ins_to_end (bb, inst);
1564 * We try to share variables when possible
1567 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1572 /* inlining can result in deeper stacks */
1573 if (slot >= ((MonoMethodNormal *)cfg->method)->header->max_stack)
1574 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1576 pos = ins->type - 1 + slot * STACK_MAX;
1578 switch (ins->type) {
1585 if ((vnum = cfg->intvars [pos]))
1586 return cfg->varinfo [vnum];
1587 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1588 cfg->intvars [pos] = res->inst_c0;
1591 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1597 * This function is called to handle items that are left on the evaluation stack
1598 * at basic block boundaries. What happens is that we save the values to local variables
1599 * and we reload them later when first entering the target basic block (with the
1600 * handle_loaded_temps () function).
1601 * A single joint point will use the same variables (stored in the array bb->out_stack or
1602 * bb->in_stack, if the basic block is before or after the joint point).
1605 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count) {
1607 MonoBasicBlock *outb;
1608 MonoInst *inst, **locals;
1612 if (cfg->verbose_level > 3)
1613 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
1614 if (!bb->out_scount) {
1616 bb->out_scount = count;
1617 //g_print ("bblock %d has out:", bb->block_num);
1618 for (i = 0; i < bb->out_count; ++i) {
1619 outb = bb->out_bb [i];
1620 //g_print (" %d", outb->block_num);
1621 if (outb->in_stack) {
1623 bb->out_stack = outb->in_stack;
1629 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1630 for (i = 0; i < count; ++i) {
1632 /* try to reuse temps already allocated for this purpouse, if they occupy the same
1633 * stack slot and if they are of the same type. */
1634 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1636 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1641 locals = bb->out_stack;
1642 for (i = 0; i < count; ++i) {
1643 /* add store ops at the end of the bb, before the branch */
1644 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1645 if (inst->opcode == CEE_STOBJ) {
1646 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
1647 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE);
1649 inst->cil_code = sp [i]->cil_code;
1650 mono_add_ins_to_end (bb, inst);
1652 if (cfg->verbose_level > 3)
1653 g_print ("storing %d to temp %d\n", i, locals [i]->inst_c0);
1656 for (i = 0; i < bb->out_count; ++i) {
1657 outb = bb->out_bb [i];
1658 if (outb->in_scount)
1659 continue; /* check they are the same locals */
1660 outb->in_scount = count;
1661 outb->in_stack = locals;
1667 ret_type_to_call_opcode (MonoType *type, int calli, int virt)
1672 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1676 case MONO_TYPE_VOID:
1677 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
1680 case MONO_TYPE_BOOLEAN:
1683 case MONO_TYPE_CHAR:
1686 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1690 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1691 case MONO_TYPE_CLASS:
1692 case MONO_TYPE_STRING:
1693 case MONO_TYPE_OBJECT:
1694 case MONO_TYPE_SZARRAY:
1695 case MONO_TYPE_ARRAY:
1696 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1699 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
1702 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
1703 case MONO_TYPE_VALUETYPE:
1704 if (type->data.klass->enumtype) {
1705 t = type->data.klass->enum_basetype->type;
1708 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1709 case MONO_TYPE_TYPEDBYREF:
1710 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1712 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
1718 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
1720 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
1722 ji->ip.label = label;
1723 ji->type = MONO_PATCH_INFO_SWITCH;
1724 ji->data.table = bbs;
1725 ji->next = cfg->patch_info;
1726 ji->table_size = num_blocks;
1727 cfg->patch_info = ji;
1731 * When we add a tree of instructions, we need to ensure the instructions currently
1732 * on the stack are executed before (like, if we load a value from a local).
1733 * We ensure this by saving the currently loaded values to temps and rewriting the
1734 * instructions to load the values.
1735 * This is not done for opcodes that terminate a basic block (because it's handled already
1736 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
1739 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
1741 MonoInst *load, *store, *temp, *ins;
1743 while (stack < sp) {
1745 /* handle also other constants */
1746 if (ins->opcode != OP_ICONST) {
1747 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1748 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1749 store->cil_code = ins->cil_code;
1750 if (store->opcode == CEE_STOBJ) {
1751 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
1752 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE);
1754 MONO_ADD_INS (bblock, store);
1755 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1756 load->cil_code = ins->cil_code;
1764 * Prepare arguments for passing to a function call.
1765 * Return a non-zero value if the arguments can't be passed to the given
1767 * The type checks are not yet complete and some conversions may need
1768 * casts on 32 or 64 bit architectures.
1771 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
1776 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
1780 for (i = 0; i < sig->param_count; ++i) {
1781 if (sig->params [i]->byref) {
1783 * check the result of ldelema is only passed as an argument if the byref
1784 * type matches exactly the array element type.
1785 * FIXME: if the argument as been saved on the stack as part of the
1786 * interface variable code (the value was on the stack at a basic block boundary)
1787 * we need to add the check in that case, too.
1789 if (args [i]->opcode == CEE_LDELEMA) {
1791 MonoClass *exact_class = mono_class_from_mono_type (sig->params [i]);
1792 if (!exact_class->valuetype) {
1793 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
1794 check->cil_code = args [i]->cil_code;
1795 check->klass = exact_class;
1796 check->inst_left = args [i]->inst_left;
1797 check->type = STACK_OBJ;
1798 args [i]->inst_left = check;
1801 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
1805 simple_type = sig->params [i]->type;
1807 switch (simple_type) {
1808 case MONO_TYPE_VOID:
1813 case MONO_TYPE_BOOLEAN:
1816 case MONO_TYPE_CHAR:
1819 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
1825 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
1828 case MONO_TYPE_CLASS:
1829 case MONO_TYPE_STRING:
1830 case MONO_TYPE_OBJECT:
1831 case MONO_TYPE_SZARRAY:
1832 case MONO_TYPE_ARRAY:
1833 if (args [i]->type != STACK_OBJ)
1838 if (args [i]->type != STACK_I8)
1843 if (args [i]->type != STACK_R8)
1846 case MONO_TYPE_VALUETYPE:
1847 if (sig->params [i]->data.klass->enumtype) {
1848 simple_type = sig->params [i]->data.klass->enum_basetype->type;
1851 if (args [i]->type != STACK_VTYPE)
1854 case MONO_TYPE_TYPEDBYREF:
1855 if (args [i]->type != STACK_VTYPE)
1858 case MONO_TYPE_GENERICINST:
1859 simple_type = sig->params [i]->data.generic_inst->generic_type->type;
1863 g_error ("unknown type 0x%02x in check_call_signature", simple_type);
1870 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
1871 const guint8 *ip, gboolean to_end)
1873 MonoInst *temp, *store, *ins = (MonoInst*)call;
1874 MonoType *ret = sig->ret;
1876 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
1878 call->inst.type = STACK_OBJ;
1879 call->inst.opcode = CEE_CALL;
1880 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
1882 type_to_eval_stack_type (ret, ins);
1883 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
1886 if (MONO_TYPE_ISSTRUCT (ret)) {
1889 /* we use this to allocate native sized structs */
1890 temp->unused = sig->pinvoke;
1892 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
1893 if (call->inst.opcode == OP_VCALL)
1894 ins->inst_left = loada;
1896 ins->inst_right = loada; /* a virtual or indirect call */
1899 mono_add_ins_to_end (bblock, ins);
1901 MONO_ADD_INS (bblock, ins);
1903 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1904 store->cil_code = ip;
1906 mono_add_ins_to_end (bblock, store);
1908 MONO_ADD_INS (bblock, store);
1910 return temp->inst_c0;
1913 mono_add_ins_to_end (bblock, ins);
1915 MONO_ADD_INS (bblock, ins);
1920 inline static MonoCallInst *
1921 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1922 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
1927 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual));
1929 call->inst.cil_code = ip;
1931 call->signature = sig;
1932 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
1934 for (arg = call->out_args; arg;) {
1935 MonoInst *narg = arg->next;
1940 mono_add_ins_to_end (bblock, arg);
1942 MONO_ADD_INS (bblock, arg);
1949 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1950 MonoInst **args, MonoInst *addr, const guint8 *ip)
1952 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
1954 call->inst.inst_i0 = addr;
1956 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
1959 static MonoCallInst*
1960 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
1961 MonoInst **args, const guint8 *ip, MonoInst *this)
1963 gboolean virtual = this != NULL;
1966 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, FALSE);
1968 if (this && sig->hasthis &&
1969 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
1970 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
1971 call->method = mono_marshal_get_remoting_invoke_with_check (method);
1973 call->method = method;
1975 call->inst.flags |= MONO_INST_HAS_METHOD;
1976 call->inst.inst_left = this;
1982 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
1983 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
1985 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, signature, args, ip, this);
1987 return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
1991 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
1992 MonoInst **args, const guint8 *ip, gboolean to_end)
1998 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
2000 return mono_spill_call (cfg, bblock, call, sig, func == mono_array_new_va, ip, to_end);
2004 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
2006 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
2009 g_warning ("unregistered JIT ICall");
2010 g_assert_not_reached ();
2013 return mono_emit_native_call (cfg, bblock, info->wrapper, info->sig, args, ip, FALSE);
2017 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
2019 MonoInst *ins, *temp = NULL, *store, *load, *begin;
2020 MonoInst *last_arg = NULL;
2024 //g_print ("emulating: ");
2025 //mono_print_tree_nl (tree);
2026 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE));
2027 ins = (MonoInst*)call;
2029 call->inst.cil_code = tree->cil_code;
2031 call->signature = info->sig;
2033 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
2035 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2036 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
2037 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2038 store->cil_code = tree->cil_code;
2043 nargs = info->sig->param_count + info->sig->hasthis;
2045 for (last_arg = call->out_args; last_arg && last_arg->next; last_arg = last_arg->next) ;
2048 last_arg->next = store;
2051 begin = call->out_args;
2055 if (cfg->prev_ins) {
2057 * This assumes that that in a tree, emulate_opcode is called for a
2058 * node before it is called for its children. dec_foreach needs to
2059 * take this into account.
2061 store->next = cfg->prev_ins->next;
2062 cfg->prev_ins->next = begin;
2064 store->next = cfg->cbb->code;
2065 cfg->cbb->code = begin;
2068 call->fptr = info->wrapper;
2070 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2071 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2076 static MonoMethodSignature *
2077 mono_get_element_address_signature (int arity)
2079 static GHashTable *sighash = NULL;
2080 MonoMethodSignature *res;
2084 sighash = g_hash_table_new (NULL, NULL);
2087 if ((res = g_hash_table_lookup (sighash, (gpointer)arity)))
2090 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
2092 res->params [0] = &mono_defaults.array_class->byval_arg;
2094 for (i = 1; i <= arity; i++)
2095 res->params [i] = &mono_defaults.int_class->byval_arg;
2097 res->ret = &mono_defaults.int_class->byval_arg;
2099 g_hash_table_insert (sighash, (gpointer)arity, res);
2105 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native) {
2106 MonoInst *iargs [3];
2112 * This check breaks with spilled vars... need to handle it during verification anyway.
2113 * g_assert (klass && klass == src->klass && klass == dest->klass);
2117 n = mono_class_native_size (klass, &align);
2119 n = mono_class_value_size (klass, &align);
2121 if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
2123 MONO_INST_NEW (cfg, inst, OP_MEMCPY);
2124 inst->inst_left = dest;
2125 inst->inst_right = src;
2126 inst->cil_code = ip;
2128 MONO_ADD_INS (bblock, inst);
2133 NEW_ICONST (cfg, iargs [2], n);
2135 mono_emit_native_call (cfg, bblock, helper_memcpy, helper_sig_memcpy, iargs, ip, to_end);
2139 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
2141 MonoInst *iargs [2];
2142 MonoInst *ins, *zero_int32;
2145 NEW_ICONST (cfg, zero_int32, 0);
2147 mono_class_init (klass);
2148 n = mono_class_value_size (klass, NULL);
2149 MONO_INST_NEW (cfg, ins, 0);
2151 ins->inst_left = dest;
2152 ins->inst_right = zero_int32;
2155 ins->opcode = CEE_STIND_I1;
2156 MONO_ADD_INS (bblock, ins);
2159 ins->opcode = CEE_STIND_I2;
2160 MONO_ADD_INS (bblock, ins);
2163 ins->opcode = CEE_STIND_I4;
2164 MONO_ADD_INS (bblock, ins);
2167 if (n <= sizeof (gpointer) * 5) {
2168 ins->opcode = OP_MEMSET;
2171 MONO_ADD_INS (bblock, ins);
2174 handle_loaded_temps (cfg, bblock, stack_start, sp);
2175 NEW_ICONST (cfg, ins, n);
2178 mono_emit_jit_icall (cfg, bblock, helper_initobj, iargs, ip);
2183 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
2186 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
2188 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
2189 MonoMethodSignature *signature = method->signature;
2193 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2194 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2195 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
2196 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
2197 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2198 (method->klass->marshalbyref) ||
2199 !header || header->num_clauses ||
2200 /* fixme: why cant we inline valuetype returns? */
2201 MONO_TYPE_ISSTRUCT (signature->ret))
2204 /* its not worth to inline methods with valuetype arguments?? */
2205 for (i = 0; i < signature->param_count; i++) {
2206 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
2212 * if we can initialize the class of the method right away, we do,
2213 * otherwise we don't allow inlining if the class needs initialization,
2214 * since it would mean inserting a call to mono_runtime_class_init()
2215 * inside the inlined code
2217 if (!(cfg->opt & MONO_OPT_SHARED)) {
2218 vtable = mono_class_vtable (cfg->domain, method->klass);
2219 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
2220 if (cfg->run_cctors)
2221 mono_runtime_class_init (vtable);
2223 else if (!vtable->initialized && mono_class_needs_cctor_run (method->klass, NULL))
2227 * If we're compiling for shared code
2228 * the cctor will need to be run at aot method load time, for example,
2229 * or at the end of the compilation of the inlining method.
2231 if (mono_class_needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
2234 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
2236 /* also consider num_locals? */
2237 if (getenv ("MONO_INLINELIMIT"))
2238 if (header->code_size < atoi (getenv ("MONO_INLINELIMIT"))) {
2242 if (header->code_size < 20)
2249 mini_get_ldelema_ins (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
2253 MonoMethodSignature *esig;
2255 rank = cmethod->signature->param_count - (is_set? 1: 0);
2257 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
2259 NEW_GROUP (cfg, indexes, sp [1], sp [2]);
2260 MONO_INST_NEW (cfg, addr, OP_LDELEMA2D);
2261 addr->inst_left = sp [0];
2262 addr->inst_right = indexes;
2263 addr->cil_code = ip;
2264 addr->type = STACK_MP;
2265 addr->klass = cmethod->klass;
2268 esig = mono_get_element_address_signature (rank);
2269 temp = mono_emit_native_call (cfg, bblock, ves_array_element_address, esig, sp, ip, FALSE);
2270 NEW_TEMPLOAD (cfg, addr, temp);
2274 static MonoJitICallInfo **emul_opcode_map = NULL;
2276 static inline MonoJitICallInfo *
2277 mono_find_jit_opcode_emulation (int opcode)
2279 if (emul_opcode_map)
2280 return emul_opcode_map [opcode];
2286 mini_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
2291 if (cmethod->klass == mono_defaults.string_class) {
2292 if (cmethod->name [0] != 'g')
2295 if (strcmp (cmethod->name, "get_Chars") == 0)
2297 else if (strcmp (cmethod->name, "get_Length") == 0)
2300 } else if (cmethod->klass == mono_defaults.object_class) {
2301 if (strcmp (cmethod->name, "GetType") == 0)
2305 } else if (cmethod->klass == mono_defaults.array_class) {
2306 if (strcmp (cmethod->name, "get_Rank") == 0)
2308 else if (strcmp (cmethod->name, "get_Length") == 0)
2313 op = mono_arch_get_opcode_for_method (cfg, cmethod, fsig, args);
2317 pc = fsig->param_count + fsig->hasthis;
2318 MONO_INST_NEW (cfg, ins, op);
2321 ins->inst_i0 = args [0];
2323 ins->inst_i1 = args [1];
2330 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
2332 MonoInst *store, *temp;
2335 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
2337 if (!sig->hasthis && sig->param_count == 0)
2341 if (sp [0]->opcode == OP_ICONST) {
2344 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
2346 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2347 store->cil_code = sp [0]->cil_code;
2348 MONO_ADD_INS (bblock, store);
2353 for (i = 0; i < sig->param_count; ++i) {
2354 if (sp [0]->opcode == OP_ICONST) {
2357 temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
2359 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2360 store->cil_code = sp [0]->cil_code;
2361 if (store->opcode == CEE_STOBJ) {
2362 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2363 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE);
2365 MONO_ADD_INS (bblock, store);
2373 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
2374 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b)
2376 MonoInst *ins, *rvar = NULL;
2377 MonoMethodHeader *cheader;
2378 MonoBasicBlock *ebblock, *sbblock;
2379 int i, costs, new_locals_offset;
2381 if (cfg->verbose_level > 2)
2382 g_print ("INLINE START %p %s -> %s\n", cmethod, mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2384 cheader = ((MonoMethodNormal *)cmethod)->header;
2386 if (!cmethod->inline_info) {
2387 mono_jit_stats.inlineable_methods++;
2388 cmethod->inline_info = 1;
2390 /* allocate space to store the return value */
2391 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2392 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2395 /* allocate local variables */
2396 new_locals_offset = cfg->num_varinfo;
2397 for (i = 0; i < cheader->num_locals; ++i)
2398 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
2400 /* allocate starte and end blocks */
2401 sbblock = NEW_BBLOCK (cfg);
2402 sbblock->block_num = cfg->num_bblocks++;
2403 sbblock->real_offset = real_offset;
2405 ebblock = NEW_BBLOCK (cfg);
2406 ebblock->block_num = cfg->num_bblocks++;
2407 ebblock->real_offset = real_offset;
2409 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
2411 if (costs >= 0 && costs < 60) {
2412 if (cfg->verbose_level > 2)
2413 g_print ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
2415 mono_jit_stats.inlined_methods++;
2417 /* always add some code to avoid block split failures */
2418 MONO_INST_NEW (cfg, ins, CEE_NOP);
2419 MONO_ADD_INS (bblock, ins);
2422 bblock->next_bb = sbblock;
2423 link_bblock (cfg, bblock, sbblock);
2426 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
2432 if (cfg->verbose_level > 2)
2433 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
2439 * Some of these comments may well be out-of-date.
2440 * Design decisions: we do a single pass over the IL code (and we do bblock
2441 * splitting/merging in the few cases when it's required: a back jump to an IL
2442 * address that was not already seen as bblock starting point).
2443 * Code is validated as we go (full verification is still better left to metadata/verify.c).
2444 * Complex operations are decomposed in simpler ones right away. We need to let the
2445 * arch-specific code peek and poke inside this process somehow (except when the
2446 * optimizations can take advantage of the full semantic info of coarse opcodes).
2447 * All the opcodes of the form opcode.s are 'normalized' to opcode.
2448 * MonoInst->opcode initially is the IL opcode or some simplification of that
2449 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
2450 * opcode with value bigger than OP_LAST.
2451 * At this point the IR can be handed over to an interpreter, a dumb code generator
2452 * or to the optimizing code generator that will translate it to SSA form.
2454 * Profiling directed optimizations.
2455 * We may compile by default with few or no optimizations and instrument the code
2456 * or the user may indicate what methods to optimize the most either in a config file
2457 * or through repeated runs where the compiler applies offline the optimizations to
2458 * each method and then decides if it was worth it.
2461 * * consider using an array instead of an hash table (bb_hash)
2464 #define CHECK_TYPE(ins) if (!(ins)->type) goto unverified
2465 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) goto unverified
2466 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) goto unverified
2467 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) goto unverified
2468 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) goto unverified
2469 #define CHECK_OPSIZE(size) if (ip + size > end) goto unverified
2471 #define TYPE_PARAM_TO_TYPE(num) (method->klass->generic_inst->data.generic_inst->type_argv [(num)])
2472 #define TYPE_PARAM_TO_CLASS(num) (mono_class_from_mono_type (TYPE_PARAM_TO_TYPE ((num))))
2474 #define MTYPE_PARAM_TO_TYPE(num) (((MonoMethodNormal *) method)->header->geninst->type_argv [(num)])
2475 #define MTYPE_PARAM_TO_CLASS(num) (mono_class_from_mono_type (MTYPE_PARAM_TO_TYPE ((num))))
2478 /* offset from br.s -> br like opcodes */
2479 #define BIG_BRANCH_OFFSET 13
2482 get_basic_blocks (MonoCompile *cfg, GHashTable *bbhash, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
2484 unsigned char *ip = start;
2485 unsigned char *target;
2488 MonoBasicBlock *bblock;
2489 const MonoOpcode *opcode;
2492 cli_addr = ip - start;
2493 i = mono_opcode_value ((const guint8 **)&ip);
2494 opcode = &mono_opcodes [i];
2495 switch (opcode->argument) {
2496 case MonoInlineNone:
2499 case MonoInlineString:
2500 case MonoInlineType:
2501 case MonoInlineField:
2502 case MonoInlineMethod:
2505 case MonoShortInlineR:
2512 case MonoShortInlineVar:
2513 case MonoShortInlineI:
2516 case MonoShortInlineBrTarget:
2517 target = start + cli_addr + 2 + (signed char)ip [1];
2518 GET_BBLOCK (cfg, bbhash, bblock, target);
2521 case MonoInlineBrTarget:
2522 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
2523 GET_BBLOCK (cfg, bbhash, bblock, target);
2526 case MonoInlineSwitch: {
2527 guint32 n = read32 (ip + 1);
2530 cli_addr += 5 + 4 * n;
2531 target = start + cli_addr;
2532 GET_BBLOCK (cfg, bbhash, bblock, target);
2534 for (j = 0; j < n; ++j) {
2535 target = start + cli_addr + (gint32)read32 (ip);
2536 GET_BBLOCK (cfg, bbhash, bblock, target);
2546 g_assert_not_reached ();
2555 static MonoClassField *
2556 get_generic_field_inst (MonoClassField *field, MonoClass *klass, MonoClass **retclass)
2559 for (i = 0; i < field->parent->field.count; ++i) {
2560 if (field == &field->parent->fields [i]) {
2562 return &klass->fields [i];
2568 static MonoClassField *
2569 inflate_generic_field (MonoClassField *field, MonoClass *klass, MonoClass **retclass)
2571 MonoGenericInst *ginst;
2572 MonoClassField *res;
2574 res = g_new0 (MonoClassField, 1);
2576 ginst = klass->generic_inst->data.generic_inst;
2577 res->type = mono_class_inflate_generic_type (field->type, ginst);
2582 * mono_method_to_ir: translates IL into basic blocks containing trees
2585 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
2586 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
2587 guint inline_offset, gboolean is_virtual_call)
2589 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
2590 MonoInst *ins, **sp, **stack_start;
2591 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
2593 MonoMethod *cmethod;
2594 MonoInst **arg_array;
2595 MonoMethodHeader *header;
2597 guint32 token, ins_flag;
2599 unsigned char *ip, *end, *target, *err_pos;
2600 static double r8_0 = 0.0;
2601 MonoMethodSignature *sig;
2602 MonoType **param_types;
2603 GList *bb_recheck = NULL, *tmp;
2604 int i, n, start_new_bblock, align;
2605 int num_calls = 0, inline_costs = 0;
2606 int *filter_lengths = NULL;
2607 int breakpoint_id = 0;
2608 guint real_offset, num_args;
2610 image = method->klass->image;
2611 header = ((MonoMethodNormal *)method)->header;
2612 sig = method->signature;
2613 num_args = sig->hasthis + sig->param_count;
2614 ip = (unsigned char*)header->code;
2615 end = ip + header->code_size;
2616 mono_jit_stats.cil_code_size += header->code_size;
2618 if (cfg->method == method) {
2620 bbhash = cfg->bb_hash;
2622 real_offset = inline_offset;
2623 bbhash = g_hash_table_new (g_direct_hash, NULL);
2626 if (cfg->verbose_level > 2)
2627 g_print ("method to IR %s\n", mono_method_full_name (method, TRUE));
2629 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
2630 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
2632 dont_inline = g_list_prepend (dont_inline, method);
2633 if (cfg->method == method) {
2636 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
2637 start_bblock->cil_code = NULL;
2638 start_bblock->cil_length = 0;
2639 start_bblock->block_num = cfg->num_bblocks++;
2642 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
2643 end_bblock->cil_code = NULL;
2644 end_bblock->cil_length = 0;
2645 end_bblock->block_num = cfg->num_bblocks++;
2646 g_assert (cfg->num_bblocks == 2);
2648 arg_array = alloca (sizeof (MonoInst *) * num_args);
2649 for (i = num_args - 1; i >= 0; i--)
2650 arg_array [i] = cfg->varinfo [i];
2652 if (header->num_clauses) {
2653 int size = sizeof (int) * header->num_clauses;
2654 filter_lengths = alloca (size);
2655 memset (filter_lengths, 0, size);
2657 cfg->spvars = g_hash_table_new (NULL, NULL);
2659 /* handle exception clauses */
2660 for (i = 0; i < header->num_clauses; ++i) {
2661 //unsigned char *p = ip;
2662 MonoExceptionClause *clause = &header->clauses [i];
2663 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->try_offset);
2664 tblock->real_offset = clause->try_offset;
2665 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
2666 tblock->real_offset = clause->handler_offset;
2668 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
2669 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2670 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2671 MONO_ADD_INS (tblock, ins);
2674 /*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);
2676 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
2678 /* catch and filter blocks get the exception object on the stack */
2679 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
2680 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2681 /* mostly like handle_stack_args (), but just sets the input args */
2682 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
2684 cfg->exvar = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
2685 /* prevent it from being register allocated */
2686 cfg->exvar->flags |= MONO_INST_INDIRECT;
2688 tblock->in_scount = 1;
2689 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2690 tblock->in_stack [0] = cfg->exvar;
2692 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2693 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->token_or_filter);
2694 tblock->real_offset = clause->token_or_filter;
2695 tblock->in_scount = 1;
2696 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2697 tblock->in_stack [0] = cfg->exvar;
2698 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2699 MONO_ADD_INS (tblock, ins);
2705 arg_array = alloca (sizeof (MonoInst *) * num_args);
2706 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
2709 /* FIRST CODE BLOCK */
2710 bblock = NEW_BBLOCK (cfg);
2711 bblock->cil_code = ip;
2713 ADD_BBLOCK (cfg, bbhash, bblock);
2715 if (cfg->method == method) {
2716 breakpoint_id = mono_debugger_method_has_breakpoint (method);
2717 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
2718 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2719 MONO_ADD_INS (bblock, ins);
2723 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED))) || mono_compile_aot) {
2724 /* we use a separate basic block for the initialization code */
2725 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
2726 init_localsbb->real_offset = real_offset;
2727 start_bblock->next_bb = init_localsbb;
2728 init_localsbb->next_bb = bblock;
2729 link_bblock (cfg, start_bblock, init_localsbb);
2730 link_bblock (cfg, init_localsbb, bblock);
2731 init_localsbb->block_num = cfg->num_bblocks++;
2733 start_bblock->next_bb = bblock;
2734 link_bblock (cfg, start_bblock, bblock);
2737 if (get_basic_blocks (cfg, bbhash, header, real_offset, ip, end, &err_pos)) {
2742 mono_debug_init_method (cfg, bblock, breakpoint_id);
2744 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
2746 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
2747 for (n = 0; n < sig->param_count; ++n)
2748 param_types [n + sig->hasthis] = sig->params [n];
2750 /* do this somewhere outside - not here */
2751 NEW_ICONST (cfg, zero_int32, 0);
2752 NEW_ICONST (cfg, zero_int64, 0);
2753 zero_int64->type = STACK_I8;
2754 NEW_PCONST (cfg, zero_ptr, 0);
2755 NEW_PCONST (cfg, zero_obj, 0);
2756 zero_obj->type = STACK_OBJ;
2758 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
2759 zero_r8->type = STACK_R8;
2760 zero_r8->inst_p0 = &r8_0;
2762 /* add a check for this != NULL to inlined methods */
2763 if (is_virtual_call) {
2764 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
2765 NEW_ARGLOAD (cfg, ins->inst_left, 0);
2767 MONO_ADD_INS (bblock, ins);
2770 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
2771 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
2774 start_new_bblock = 0;
2777 if (cfg->method == method)
2778 real_offset = ip - header->code;
2780 real_offset = inline_offset;
2782 if (start_new_bblock) {
2783 bblock->cil_length = ip - bblock->cil_code;
2784 if (start_new_bblock == 2) {
2785 g_assert (ip == tblock->cil_code);
2787 GET_BBLOCK (cfg, bbhash, tblock, ip);
2789 bblock->next_bb = tblock;
2791 start_new_bblock = 0;
2792 for (i = 0; i < bblock->in_scount; ++i) {
2793 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2797 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
2798 link_bblock (cfg, bblock, tblock);
2799 if (sp != stack_start) {
2800 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2803 bblock->next_bb = tblock;
2805 for (i = 0; i < bblock->in_scount; ++i) {
2806 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2812 if (cfg->coverage_info) {
2813 MonoInst *store, *one;
2814 guint32 cil_offset = ip - header->code;
2815 cfg->coverage_info->data [cil_offset].cil_code = ip;
2817 /* TODO: Use an increment here */
2818 NEW_ICONST (cfg, one, 1);
2821 NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
2824 MONO_INST_NEW (cfg, store, CEE_STIND_I);
2825 store->cil_code = ip;
2826 store->inst_left = ins;
2827 store->inst_right = one;
2829 MONO_ADD_INS (bblock, store);
2832 if (cfg->verbose_level > 3)
2833 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, sp-stack_start, mono_disasm_code_one (NULL, method, ip, NULL));
2835 /* Workaround for bug #51126 */
2836 bblock->real_offset = real_offset;
2843 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2844 ins->cil_code = ip++;
2845 MONO_ADD_INS (bblock, ins);
2851 CHECK_STACK_OVF (1);
2852 n = (*ip)-CEE_LDARG_0;
2854 NEW_ARGLOAD (cfg, ins, n);
2855 ins->cil_code = ip++;
2862 CHECK_STACK_OVF (1);
2863 n = (*ip)-CEE_LDLOC_0;
2865 NEW_LOCLOAD (cfg, ins, n);
2866 ins->cil_code = ip++;
2874 n = (*ip)-CEE_STLOC_0;
2877 handle_loaded_temps (cfg, bblock, stack_start, sp);
2878 NEW_LOCSTORE (cfg, ins, n, *sp);
2880 if (ins->opcode == CEE_STOBJ) {
2881 NEW_LOCLOADA (cfg, ins, n);
2882 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2884 MONO_ADD_INS (bblock, ins);
2890 CHECK_STACK_OVF (1);
2892 NEW_ARGLOAD (cfg, ins, ip [1]);
2899 CHECK_STACK_OVF (1);
2901 NEW_ARGLOADA (cfg, ins, ip [1]);
2911 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
2912 handle_loaded_temps (cfg, bblock, stack_start, sp);
2914 if (ins->opcode == CEE_STOBJ) {
2915 NEW_ARGLOADA (cfg, ins, ip [1]);
2916 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2918 MONO_ADD_INS (bblock, ins);
2923 CHECK_STACK_OVF (1);
2924 CHECK_LOCAL (ip [1]);
2925 NEW_LOCLOAD (cfg, ins, ip [1]);
2932 CHECK_STACK_OVF (1);
2933 CHECK_LOCAL (ip [1]);
2934 NEW_LOCLOADA (cfg, ins, ip [1]);
2943 handle_loaded_temps (cfg, bblock, stack_start, sp);
2944 CHECK_LOCAL (ip [1]);
2945 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
2947 if (ins->opcode == CEE_STOBJ) {
2948 NEW_LOCLOADA (cfg, ins, ip [1]);
2949 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2951 MONO_ADD_INS (bblock, ins);
2956 CHECK_STACK_OVF (1);
2957 NEW_PCONST (cfg, ins, NULL);
2959 ins->type = STACK_OBJ;
2964 CHECK_STACK_OVF (1);
2965 NEW_ICONST (cfg, ins, -1);
2979 CHECK_STACK_OVF (1);
2980 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
2987 CHECK_STACK_OVF (1);
2989 NEW_ICONST (cfg, ins, *((signed char*)ip));
2996 CHECK_STACK_OVF (1);
2997 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
3004 CHECK_STACK_OVF (1);
3005 MONO_INST_NEW (cfg, ins, OP_I8CONST);
3007 ins->type = STACK_I8;
3009 ins->inst_l = (gint64)read64 (ip);
3014 float *f = g_malloc (sizeof (float));
3016 CHECK_STACK_OVF (1);
3017 MONO_INST_NEW (cfg, ins, OP_R4CONST);
3018 ins->type = STACK_R8;
3027 double *d = g_malloc (sizeof (double));
3029 CHECK_STACK_OVF (1);
3030 MONO_INST_NEW (cfg, ins, OP_R8CONST);
3031 ins->type = STACK_R8;
3040 MonoInst *temp, *store;
3042 CHECK_STACK_OVF (1);
3047 * small optimization: if the loaded value was from a local already,
3048 * just load it twice.
3050 if (ins->ssa_op == MONO_SSA_LOAD &&
3051 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
3053 MONO_INST_NEW (cfg, temp, 0);
3055 temp->cil_code = ip;
3058 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3059 temp->cil_code = ip;
3060 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3061 store->cil_code = ip;
3062 MONO_ADD_INS (bblock, store);
3063 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3066 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
3076 MONO_INST_NEW (cfg, ins, CEE_POP);
3077 MONO_ADD_INS (bblock, ins);
3078 ins->cil_code = ip++;
3084 if (stack_start != sp)
3086 MONO_INST_NEW (cfg, ins, CEE_JMP);
3087 token = read32 (ip + 1);
3088 /* FIXME: check the signature matches */
3089 cmethod = mono_get_method (image, token, NULL);
3090 ins->inst_p0 = cmethod;
3091 MONO_ADD_INS (bblock, ins);
3093 start_new_bblock = 1;
3097 case CEE_CALLVIRT: {
3098 MonoInst *addr = NULL;
3099 MonoMethodSignature *fsig = NULL;
3100 int temp, array_rank = 0;
3101 int virtual = *ip == CEE_CALLVIRT;
3104 token = read32 (ip + 1);
3106 if (*ip == CEE_CALLI) {
3111 if (method->wrapper_type != MONO_WRAPPER_NONE)
3112 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
3114 fsig = mono_metadata_parse_signature (image, token);
3116 n = fsig->param_count + fsig->hasthis;
3118 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3119 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
3121 cmethod = mono_get_method (image, token, NULL);
3124 if (!cmethod->klass->inited)
3125 mono_class_init (cmethod->klass);
3127 if (cmethod->signature->pinvoke) {
3128 #ifdef MONO_USE_EXC_TABLES
3129 if (mono_method_blittable (cmethod)) {
3130 fsig = cmethod->signature;
3133 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
3134 fsig = wrapper->signature;
3135 #ifdef MONO_USE_EXC_TABLES
3139 fsig = mono_method_get_signature (cmethod, image, token);
3142 n = fsig->param_count + fsig->hasthis;
3144 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
3145 cmethod->klass->parent == mono_defaults.array_class) {
3146 array_rank = cmethod->klass->rank;
3149 if (cmethod->string_ctor)
3150 g_assert_not_reached ();
3156 //g_assert (!virtual || fsig->hasthis);
3160 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
3163 if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL) && (mono_metadata_signature_equal (method->signature, cmethod->signature))) {
3165 /* FIXME: This assumes the two methods has the same number and type of arguments */
3166 for (i = 0; i < n; ++i) {
3167 /* Check if argument is the same */
3168 NEW_ARGLOAD (cfg, ins, i);
3169 if ((ins->opcode == sp [i]->opcode) && (ins->inst_i0 == sp [i]->inst_i0))
3172 /* Prevent argument from being register allocated */
3173 arg_array [i]->flags |= MONO_INST_VOLATILE;
3174 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3176 if (ins->opcode == CEE_STOBJ) {
3177 NEW_ARGLOADA (cfg, ins, i);
3178 handle_stobj (cfg, bblock, ins, sp [i], sp [i]->cil_code, ins->klass, FALSE, FALSE);
3181 MONO_ADD_INS (bblock, ins);
3183 MONO_INST_NEW (cfg, ins, CEE_JMP);
3185 ins->inst_p0 = cmethod;
3186 ins->inst_p1 = arg_array [0];
3187 MONO_ADD_INS (bblock, ins);
3188 start_new_bblock = 1;
3189 /* skip CEE_RET as well */
3194 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_opcode_for_method (cfg, cmethod, fsig, sp))) {
3197 if (MONO_TYPE_IS_VOID (fsig->ret)) {
3198 MONO_ADD_INS (bblock, ins);
3200 type_to_eval_stack_type (fsig->ret, ins);
3209 handle_loaded_temps (cfg, bblock, stack_start, sp);
3211 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3212 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
3213 mono_method_check_inlining (cfg, cmethod) &&
3214 !g_list_find (dont_inline, cmethod)) {
3216 MonoBasicBlock *ebblock;
3218 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3222 GET_BBLOCK (cfg, bbhash, bblock, ip);
3223 ebblock->next_bb = bblock;
3224 link_bblock (cfg, ebblock, bblock);
3226 if (!MONO_TYPE_IS_VOID (fsig->ret))
3229 /* indicates start of a new block, and triggers a load of all
3230 stack arguments at bb boundarie */
3233 inline_costs += costs;
3238 inline_costs += 10 * num_calls++;
3240 /* tail recursion elimination */
3241 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == method && ip [5] == CEE_RET) {
3242 gboolean has_vtargs = FALSE;
3245 /* keep it simple */
3246 for (i = fsig->param_count - 1; i >= 0; i--) {
3247 if (MONO_TYPE_ISSTRUCT (cmethod->signature->params [i]))
3252 for (i = 0; i < n; ++i) {
3253 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3255 MONO_ADD_INS (bblock, ins);
3257 MONO_INST_NEW (cfg, ins, CEE_BR);
3259 MONO_ADD_INS (bblock, ins);
3260 tblock = start_bblock->out_bb [0];
3261 link_bblock (cfg, bblock, tblock);
3262 ins->inst_target_bb = tblock;
3263 start_new_bblock = 1;
3266 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3267 /* just create a dummy - the value is never used */
3268 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
3269 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3277 if (*ip == CEE_CALLI) {
3279 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
3280 NEW_TEMPLOAD (cfg, *sp, temp);
3284 } else if (array_rank) {
3287 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
3288 if (sp [fsig->param_count]->type == STACK_OBJ) {
3289 MonoInst *iargs [2];
3291 handle_loaded_temps (cfg, bblock, stack_start, sp);
3294 iargs [1] = sp [fsig->param_count];
3296 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref_check, iargs, ip);
3299 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
3300 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
3302 if (ins->opcode == CEE_STOBJ) {
3303 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
3305 MONO_ADD_INS (bblock, ins);
3308 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
3309 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3310 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
3314 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
3315 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3318 g_assert_not_reached ();
3322 if (0 && CODE_IS_STLOC (ip + 5) && (!MONO_TYPE_ISSTRUCT (fsig->ret)) && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)) {
3323 /* no need to spill */
3324 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
3327 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) {
3328 NEW_TEMPLOAD (cfg, *sp, temp);
3338 if (cfg->method != method) {
3339 /* return from inlined methode */
3344 //g_assert (returnvar != -1);
3345 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
3346 store->cil_code = sp [0]->cil_code;
3347 if (store->opcode == CEE_STOBJ) {
3348 g_assert_not_reached ();
3349 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
3350 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
3352 MONO_ADD_INS (bblock, store);
3356 g_assert (!return_var);
3359 MONO_INST_NEW (cfg, ins, CEE_NOP);
3360 ins->opcode = mono_type_to_stind (method->signature->ret);
3361 if (ins->opcode == CEE_STOBJ) {
3362 NEW_RETLOADA (cfg, ins);
3363 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3365 ins->opcode = OP_SETRET;
3367 ins->inst_i0 = *sp;;
3368 ins->inst_i1 = NULL;
3369 MONO_ADD_INS (bblock, ins);
3373 if (sp != stack_start)
3375 MONO_INST_NEW (cfg, ins, CEE_BR);
3376 ins->cil_code = ip++;
3377 ins->inst_target_bb = end_bblock;
3378 MONO_ADD_INS (bblock, ins);
3379 link_bblock (cfg, bblock, end_bblock);
3380 start_new_bblock = 1;
3384 MONO_INST_NEW (cfg, ins, CEE_BR);
3385 ins->cil_code = ip++;
3386 MONO_ADD_INS (bblock, ins);
3387 target = ip + 1 + (signed char)(*ip);
3389 GET_BBLOCK (cfg, bbhash, tblock, target);
3390 link_bblock (cfg, bblock, tblock);
3391 CHECK_BBLOCK (target, ip, tblock);
3392 ins->inst_target_bb = tblock;
3393 if (sp != stack_start) {
3394 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3397 start_new_bblock = 1;
3404 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3405 ins->cil_code = ip++;
3406 target = ip + 1 + *(signed char*)ip;
3408 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
3409 if (sp != stack_start) {
3410 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3427 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3428 ins->cil_code = ip++;
3429 target = ip + 1 + *(signed char*)ip;
3432 if (sp != stack_start) {
3433 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3440 MONO_INST_NEW (cfg, ins, CEE_BR);
3441 ins->cil_code = ip++;
3442 MONO_ADD_INS (bblock, ins);
3443 target = ip + 4 + (gint32)read32(ip);
3445 GET_BBLOCK (cfg, bbhash, tblock, target);
3446 link_bblock (cfg, bblock, tblock);
3447 CHECK_BBLOCK (target, ip, tblock);
3448 ins->inst_target_bb = tblock;
3449 if (sp != stack_start) {
3450 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3453 start_new_bblock = 1;
3460 MONO_INST_NEW (cfg, ins, *ip);
3461 ins->cil_code = ip++;
3462 target = ip + 4 + (gint32)read32(ip);
3464 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
3465 if (sp != stack_start) {
3466 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3483 MONO_INST_NEW (cfg, ins, *ip);
3484 ins->cil_code = ip++;
3485 target = ip + 4 + (gint32)read32(ip);
3488 if (sp != stack_start) {
3489 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3497 n = read32 (ip + 1);
3498 MONO_INST_NEW (cfg, ins, *ip);
3500 ins->inst_left = *sp;
3501 if (ins->inst_left->type != STACK_I4) goto unverified;
3504 CHECK_OPSIZE (n * sizeof (guint32));
3505 target = ip + n * sizeof (guint32);
3506 MONO_ADD_INS (bblock, ins);
3507 GET_BBLOCK (cfg, bbhash, tblock, target);
3508 link_bblock (cfg, bblock, tblock);
3509 ins->klass = GUINT_TO_POINTER (n);
3510 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
3511 ins->inst_many_bb [n] = tblock;
3513 for (i = 0; i < n; ++i) {
3514 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
3515 link_bblock (cfg, bblock, tblock);
3516 ins->inst_many_bb [i] = tblock;
3519 /* FIXME: handle stack args */
3534 MONO_INST_NEW (cfg, ins, *ip);
3539 ins->type = ldind_type [*ip - CEE_LDIND_I1];
3540 ins->flags |= ins_flag;
3552 MONO_INST_NEW (cfg, ins, *ip);
3553 ins->cil_code = ip++;
3555 handle_loaded_temps (cfg, bblock, stack_start, sp);
3556 MONO_ADD_INS (bblock, ins);
3557 ins->inst_i0 = sp [0];
3558 ins->inst_i1 = sp [1];
3559 ins->flags |= ins_flag;
3578 if (mono_find_jit_opcode_emulation (ins->opcode)) {
3579 MonoInst *store, *temp, *load;
3581 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3582 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3583 store->cil_code = ins->cil_code;
3584 MONO_ADD_INS (bblock, store);
3585 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
3586 load->cil_code = ins->cil_code;
3588 /*g_print ("found emulation for %d\n", ins->opcode);*/
3602 case CEE_CONV_OVF_I8:
3603 case CEE_CONV_OVF_U8:
3607 if (mono_find_jit_opcode_emulation (ins->opcode)) {
3608 MonoInst *store, *temp, *load;
3610 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3611 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3612 store->cil_code = ins->cil_code;
3613 MONO_ADD_INS (bblock, store);
3614 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
3615 load->cil_code = ins->cil_code;
3617 /*g_print ("found emulation for %d\n", ins->opcode);*/
3621 case CEE_CONV_OVF_I4:
3622 case CEE_CONV_OVF_I1:
3623 case CEE_CONV_OVF_I2:
3624 case CEE_CONV_OVF_I:
3625 case CEE_CONV_OVF_U:
3628 if (sp [-1]->type == STACK_R8) {
3629 ADD_UNOP (CEE_CONV_OVF_I8);
3637 case CEE_CONV_OVF_U1:
3638 case CEE_CONV_OVF_U2:
3639 case CEE_CONV_OVF_U4:
3642 if (sp [-1]->type == STACK_R8) {
3643 ADD_UNOP (CEE_CONV_OVF_U8);
3651 case CEE_CONV_OVF_I1_UN:
3652 case CEE_CONV_OVF_I2_UN:
3653 case CEE_CONV_OVF_I4_UN:
3654 case CEE_CONV_OVF_I8_UN:
3655 case CEE_CONV_OVF_U1_UN:
3656 case CEE_CONV_OVF_U2_UN:
3657 case CEE_CONV_OVF_U4_UN:
3658 case CEE_CONV_OVF_U8_UN:
3659 case CEE_CONV_OVF_I_UN:
3660 case CEE_CONV_OVF_U_UN:
3668 token = read32 (ip + 1);
3669 if (method->wrapper_type != MONO_WRAPPER_NONE)
3670 klass = mono_method_get_wrapper_data (method, token);
3672 klass = mono_class_get (image, token);
3674 mono_class_init (klass);
3675 if (klass->byval_arg.type == MONO_TYPE_VAR)
3676 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3677 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
3678 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3680 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3681 MonoInst *store, *load;
3682 MONO_INST_NEW (cfg, load, CEE_LDIND_REF);
3683 load->cil_code = ip;
3684 load->inst_i0 = sp [1];
3685 load->type = ldind_type [CEE_LDIND_REF];
3686 load->flags |= ins_flag;
3687 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
3688 store->cil_code = ip;
3689 handle_loaded_temps (cfg, bblock, stack_start, sp);
3690 MONO_ADD_INS (bblock, store);
3691 store->inst_i0 = sp [0];
3692 store->inst_i1 = load;
3693 store->flags |= ins_flag;
3695 n = mono_class_value_size (klass, NULL);
3696 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3698 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3699 copy->inst_left = sp [0];
3700 copy->inst_right = sp [1];
3701 copy->cil_code = ip;
3703 MONO_ADD_INS (bblock, copy);
3705 MonoInst *iargs [3];
3708 NEW_ICONST (cfg, iargs [2], n);
3709 iargs [2]->cil_code = ip;
3711 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3718 MonoInst *iargs [3];
3722 token = read32 (ip + 1);
3723 if (method->wrapper_type != MONO_WRAPPER_NONE)
3724 klass = mono_method_get_wrapper_data (method, token);
3726 klass = mono_class_get (image, token);
3728 mono_class_init (klass);
3729 if (klass->byval_arg.type == MONO_TYPE_VAR)
3730 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3731 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
3732 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3733 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3734 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3736 ins->inst_i0 = sp [0];
3737 ins->type = ldind_type [CEE_LDIND_REF];
3738 ins->flags |= ins_flag;
3744 n = mono_class_value_size (klass, NULL);
3745 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3746 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3747 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3749 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3750 copy->inst_left = iargs [0];
3751 copy->inst_right = *sp;
3752 copy->cil_code = ip;
3754 MONO_ADD_INS (bblock, copy);
3757 NEW_ICONST (cfg, iargs [2], n);
3758 iargs [2]->cil_code = ip;
3760 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3762 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3770 CHECK_STACK_OVF (1);
3772 n = read32 (ip + 1);
3774 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3776 MonoInst *iargs [1];
3778 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
3779 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
3780 NEW_TEMPLOAD (cfg, *sp, temp);
3784 if (cfg->opt & MONO_OPT_SHARED) {
3786 MonoInst *iargs [3];
3788 if (mono_compile_aot) {
3789 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n);
3792 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3793 NEW_IMAGECONST (cfg, iargs [1], image);
3794 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
3795 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
3796 NEW_TEMPLOAD (cfg, *sp, temp);
3797 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3799 if (mono_compile_aot)
3800 NEW_LDSTRCONST (cfg, ins, image, n);
3802 NEW_PCONST (cfg, ins, NULL);
3804 ins->type = STACK_OBJ;
3805 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3815 MonoInst *iargs [2];
3816 MonoMethodSignature *fsig;
3820 token = read32 (ip + 1);
3821 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3822 cmethod = mono_method_get_wrapper_data (method, token);
3824 cmethod = mono_get_method (image, token, NULL);
3825 fsig = mono_method_get_signature (cmethod, image, token);
3827 mono_class_init (cmethod->klass);
3829 n = fsig->param_count;
3832 /* move the args to allow room for 'this' in the first position */
3838 handle_loaded_temps (cfg, bblock, stack_start, sp);
3841 if (cmethod->klass->parent == mono_defaults.array_class) {
3842 NEW_METHODCONST (cfg, *sp, cmethod);
3843 temp = mono_emit_native_call (cfg, bblock, mono_array_new_va, fsig, sp, ip, FALSE);
3845 } else if (cmethod->string_ctor) {
3846 /* we simply pass a null pointer */
3847 NEW_PCONST (cfg, *sp, NULL);
3848 /* now call the string ctor */
3849 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
3851 if (cmethod->klass->valuetype) {
3852 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
3853 temp = iargs [0]->inst_c0;
3854 NEW_TEMPLOADA (cfg, *sp, temp);
3856 if (cfg->opt & MONO_OPT_SHARED) {
3857 NEW_DOMAINCONST (cfg, iargs [0]);
3858 NEW_CLASSCONST (cfg, iargs [1], cmethod->klass);
3860 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3862 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
3863 NEW_VTABLECONST (cfg, iargs [0], vtable);
3864 if (cmethod->klass->has_finalize || cmethod->klass->marshalbyref || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
3865 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
3867 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
3869 NEW_TEMPLOAD (cfg, *sp, temp);
3872 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3873 mono_method_check_inlining (cfg, cmethod) &&
3874 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
3875 !g_list_find (dont_inline, cmethod)) {
3877 MonoBasicBlock *ebblock;
3878 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3883 GET_BBLOCK (cfg, bbhash, bblock, ip);
3884 ebblock->next_bb = bblock;
3885 link_bblock (cfg, ebblock, bblock);
3887 NEW_TEMPLOAD (cfg, *sp, temp);
3890 /* indicates start of a new block, and triggers a load
3891 of all stack arguments at bb boundarie */
3894 inline_costs += costs;
3898 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3901 /* now call the actual ctor */
3902 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3906 NEW_TEMPLOAD (cfg, *sp, temp);
3917 klass = mono_class_get (image, read32 (ip + 1));
3918 mono_class_init (klass);
3920 if (klass->marshalbyref) {
3921 MonoInst *iargs [2];
3924 NEW_CLASSCONST (cfg, iargs [1], klass);
3925 temp = mono_emit_native_call (cfg, bblock, mono_object_isinst_mbyref, helper_sig_obj_obj_cls, iargs, ip, FALSE);
3926 NEW_TEMPLOAD (cfg, *sp, temp);
3930 MONO_INST_NEW (cfg, ins, *ip);
3931 ins->type = STACK_OBJ;
3932 ins->inst_left = *sp;
3933 ins->inst_newa_class = klass;
3939 case CEE_UNBOX_ANY: {
3940 MonoInst *add, *vtoffset;
3941 MonoInst *iargs [3];
3946 token = read32 (ip + 1);
3947 if (method->wrapper_type != MONO_WRAPPER_NONE)
3948 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3950 klass = mono_class_get (image, token);
3951 mono_class_init (klass);
3953 if (klass->byval_arg.type == MONO_TYPE_VAR)
3954 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3955 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
3956 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
3958 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3960 if (klass->marshalbyref) {
3961 MonoInst *iargs [2];
3964 NEW_CLASSCONST (cfg, iargs [1], klass);
3965 temp = mono_emit_jit_icall (cfg, bblock, mono_object_castclass_mbyref, iargs, ip);
3966 NEW_TEMPLOAD (cfg, *sp, temp);
3970 MONO_INST_NEW (cfg, ins, CEE_CASTCLASS);
3971 ins->type = STACK_OBJ;
3972 ins->inst_left = *sp;
3974 ins->inst_newa_class = klass;
3982 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
3983 ins->type = STACK_OBJ;
3984 ins->inst_left = *sp;
3986 ins->inst_newa_class = klass;
3989 MONO_INST_NEW (cfg, add, CEE_ADD);
3990 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3991 add->inst_left = ins;
3992 add->inst_right = vtoffset;
3993 add->type = STACK_MP;
3997 n = mono_class_value_size (klass, NULL);
3998 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3999 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
4000 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
4002 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
4003 copy->inst_left = iargs [0];
4004 copy->inst_right = *sp;
4005 copy->cil_code = ip;
4007 MONO_ADD_INS (bblock, copy);
4010 NEW_ICONST (cfg, iargs [2], n);
4011 iargs [2]->cil_code = ip;
4013 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
4015 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
4021 MonoInst *add, *vtoffset;
4026 token = read32 (ip + 1);
4027 if (method->wrapper_type != MONO_WRAPPER_NONE)
4028 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4030 klass = mono_class_get (image, token);
4031 mono_class_init (klass);
4033 if (klass->byval_arg.type == MONO_TYPE_VAR)
4034 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4035 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4036 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4038 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
4039 ins->type = STACK_OBJ;
4040 ins->inst_left = *sp;
4042 ins->inst_newa_class = klass;
4045 MONO_INST_NEW (cfg, add, CEE_ADD);
4046 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4047 add->inst_left = ins;
4048 add->inst_right = vtoffset;
4049 add->type = STACK_MP;
4059 klass = mono_class_get (image, read32 (ip + 1));
4060 mono_class_init (klass);
4061 if (klass->byval_arg.type == MONO_TYPE_VAR)
4062 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4063 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4064 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4066 if (klass->marshalbyref) {
4067 MonoInst *iargs [2];
4070 NEW_CLASSCONST (cfg, iargs [1], klass);
4071 temp = mono_emit_jit_icall (cfg, bblock, mono_object_castclass_mbyref, iargs, ip);
4072 NEW_TEMPLOAD (cfg, *sp, temp);
4076 MONO_INST_NEW (cfg, ins, *ip);
4077 ins->type = STACK_OBJ;
4078 ins->inst_left = *sp;
4080 ins->inst_newa_class = klass;
4088 MONO_INST_NEW (cfg, ins, *ip);
4090 ins->inst_left = *sp;
4091 ins->cil_code = ip++;
4092 MONO_ADD_INS (bblock, ins);
4094 start_new_bblock = 1;
4099 MonoInst *offset_ins;
4100 MonoClassField *field;
4101 MonoBasicBlock *ebblock;
4105 if (*ip == CEE_STFLD) {
4112 // FIXME: enable this test later.
4113 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
4116 token = read32 (ip + 1);
4117 field = mono_field_from_token (image, token, &klass);
4118 if (field->parent->gen_params)
4119 field = get_generic_field_inst (field, method->klass, &klass);
4120 else if (field->parent->generic_inst && method->klass->generic_inst)
4121 field = inflate_generic_field (field, method->klass, &klass);
4122 mono_class_init (klass);
4124 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
4125 /* FIXME: mark instructions for use in SSA */
4126 if (*ip == CEE_STFLD) {
4127 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
4128 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
4129 MonoInst *iargs [5];
4132 NEW_CLASSCONST (cfg, iargs [1], klass);
4133 NEW_FIELDCONST (cfg, iargs [2], field);
4134 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
4138 if (cfg->opt & MONO_OPT_INLINE) {
4139 costs = inline_method (cfg, stfld_wrapper, stfld_wrapper->signature, bblock,
4140 iargs, ip, real_offset, dont_inline, &ebblock);
4141 g_assert (costs > 0);
4146 GET_BBLOCK (cfg, bbhash, bblock, ip);
4147 ebblock->next_bb = bblock;
4148 link_bblock (cfg, ebblock, bblock);
4150 /* indicates start of a new block, and triggers a load
4151 of all stack arguments at bb boundarie */
4154 inline_costs += costs;
4157 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, stfld_wrapper->signature, iargs, ip, NULL);
4161 NEW_ICONST (cfg, offset_ins, foffset);
4162 MONO_INST_NEW (cfg, ins, CEE_ADD);
4164 ins->inst_left = *sp;
4165 ins->inst_right = offset_ins;
4166 ins->type = STACK_MP;
4168 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4169 store->cil_code = ip;
4170 store->inst_left = ins;
4171 store->inst_right = sp [1];
4172 handle_loaded_temps (cfg, bblock, stack_start, sp);
4173 store->flags |= ins_flag;
4175 if (store->opcode == CEE_STOBJ) {
4176 handle_stobj (cfg, bblock, ins, sp [1], ip,
4177 mono_class_from_mono_type (field->type), FALSE, FALSE);
4179 MONO_ADD_INS (bblock, store);
4182 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
4183 /* fixme: we need to inline that call somehow */
4184 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
4185 MonoInst *iargs [4];
4189 NEW_CLASSCONST (cfg, iargs [1], klass);
4190 NEW_FIELDCONST (cfg, iargs [2], field);
4191 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
4192 if (cfg->opt & MONO_OPT_INLINE) {
4193 costs = inline_method (cfg, ldfld_wrapper, ldfld_wrapper->signature, bblock,
4194 iargs, ip, real_offset, dont_inline, &ebblock);
4195 g_assert (costs > 0);
4200 GET_BBLOCK (cfg, bbhash, bblock, ip);
4201 ebblock->next_bb = bblock;
4202 link_bblock (cfg, ebblock, bblock);
4204 temp = iargs [0]->inst_i0->inst_c0;
4206 if (*ip == CEE_LDFLDA) {
4207 /* not sure howto handle this */
4208 NEW_TEMPLOADA (cfg, *sp, temp);
4210 NEW_TEMPLOAD (cfg, *sp, temp);
4214 /* indicates start of a new block, and triggers a load of
4215 all stack arguments at bb boundarie */
4218 inline_costs += costs;
4221 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, ldfld_wrapper->signature, iargs, ip, NULL);
4222 if (*ip == CEE_LDFLDA) {
4223 /* not sure howto handle this */
4224 NEW_TEMPLOADA (cfg, *sp, temp);
4226 NEW_TEMPLOAD (cfg, *sp, temp);
4231 NEW_ICONST (cfg, offset_ins, foffset);
4232 MONO_INST_NEW (cfg, ins, CEE_ADD);
4234 ins->inst_left = *sp;
4235 ins->inst_right = offset_ins;
4236 ins->type = STACK_MP;
4238 if (*ip == CEE_LDFLDA) {
4242 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4243 type_to_eval_stack_type (field->type, load);
4244 load->cil_code = ip;
4245 load->inst_left = ins;
4246 load->flags |= ins_flag;
4258 MonoClassField *field;
4259 gpointer addr = NULL;
4262 token = read32 (ip + 1);
4264 field = mono_field_from_token (image, token, &klass);
4265 if (field->parent->gen_params)
4266 field = get_generic_field_inst (field, method->klass, &klass);
4267 else if (field->parent->generic_inst && method->klass->generic_inst)
4268 field = inflate_generic_field (field, method->klass, &klass);
4269 mono_class_init (klass);
4271 handle_loaded_temps (cfg, bblock, stack_start, sp);
4273 if (cfg->domain->special_static_fields)
4274 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
4276 if ((cfg->opt & MONO_OPT_SHARED) || (mono_compile_aot && addr)) {
4278 MonoInst *iargs [2];
4279 g_assert (field->parent);
4280 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
4281 NEW_FIELDCONST (cfg, iargs [1], field);
4282 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
4283 NEW_TEMPLOAD (cfg, ins, temp);
4286 vtable = mono_class_vtable (cfg->domain, klass);
4288 if ((!vtable->initialized || mono_compile_aot) && !(klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (klass, method)) {
4289 guint8 *tramp = mono_create_class_init_trampoline (vtable);
4290 mono_emit_native_call (cfg, bblock, tramp,
4291 helper_sig_class_init_trampoline,
4293 if (cfg->verbose_level > 2)
4294 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
4296 if (cfg->run_cctors)
4297 mono_runtime_class_init (vtable);
4299 addr = (char*)vtable->data + field->offset;
4301 if (mono_compile_aot)
4302 NEW_SFLDACONST (cfg, ins, field);
4304 NEW_PCONST (cfg, ins, addr);
4308 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
4309 * This could be later optimized to do just a couple of
4310 * memory dereferences with constant offsets.
4313 MonoInst *iargs [1];
4314 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
4315 temp = mono_emit_jit_icall (cfg, bblock, mono_get_special_static_data, iargs, ip);
4316 NEW_TEMPLOAD (cfg, ins, temp);
4320 /* FIXME: mark instructions for use in SSA */
4321 if (*ip == CEE_LDSFLDA) {
4323 } else if (*ip == CEE_STSFLD) {
4327 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4328 store->cil_code = ip;
4329 store->inst_left = ins;
4330 store->inst_right = sp [0];
4331 store->flags |= ins_flag;
4334 if (store->opcode == CEE_STOBJ) {
4335 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
4337 MONO_ADD_INS (bblock, store);
4339 gboolean is_const = FALSE;
4340 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4341 if (!((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) &&
4342 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
4343 gpointer addr = (char*)vtable->data + field->offset;
4344 /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
4346 switch (field->type->type) {
4347 case MONO_TYPE_BOOLEAN:
4349 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
4353 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
4356 case MONO_TYPE_CHAR:
4358 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
4362 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
4367 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
4371 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
4376 case MONO_TYPE_STRING:
4377 case MONO_TYPE_OBJECT:
4378 case MONO_TYPE_CLASS:
4379 case MONO_TYPE_SZARRAY:
4381 case MONO_TYPE_FNPTR:
4382 case MONO_TYPE_ARRAY:
4383 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
4384 type_to_eval_stack_type (field->type, *sp);
4389 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
4390 sp [0]->type = STACK_I8;
4391 sp [0]->inst_l = *((gint64 *)addr);
4396 case MONO_TYPE_VALUETYPE:
4405 CHECK_STACK_OVF (1);
4406 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4407 type_to_eval_stack_type (field->type, load);
4408 load->cil_code = ip;
4409 load->inst_left = ins;
4411 load->flags |= ins_flag;
4413 /* fixme: dont see the problem why this does not work */
4414 //cfg->disable_aot = TRUE;
4424 token = read32 (ip + 1);
4425 if (method->wrapper_type != MONO_WRAPPER_NONE)
4426 klass = mono_method_get_wrapper_data (method, token);
4428 klass = mono_class_get (image, token);
4429 mono_class_init (klass);
4430 if (klass->byval_arg.type == MONO_TYPE_VAR)
4431 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4432 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4433 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4434 n = mono_type_to_stind (&klass->byval_arg);
4435 if (n == CEE_STOBJ) {
4436 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
4438 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
4440 MONO_INST_NEW (cfg, store, n);
4441 store->cil_code = ip;
4442 store->inst_left = sp [0];
4443 store->inst_right = sp [1];
4444 store->flags |= ins_flag;
4445 MONO_ADD_INS (bblock, store);
4452 MonoInst *iargs [2];
4453 MonoInst *load, *vtoffset, *add, *val, *vstore;
4459 token = read32 (ip + 1);
4460 if (method->wrapper_type != MONO_WRAPPER_NONE)
4461 klass = mono_method_get_wrapper_data (method, token);
4463 klass = mono_class_get (image, token);
4464 mono_class_init (klass);
4465 if (klass->byval_arg.type == MONO_TYPE_VAR)
4466 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4467 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4468 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4470 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4475 /* much like NEWOBJ */
4476 if (cfg->opt & MONO_OPT_SHARED) {
4477 NEW_DOMAINCONST (cfg, iargs [0]);
4478 NEW_CLASSCONST (cfg, iargs [1], klass);
4480 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
4482 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4483 NEW_VTABLECONST (cfg, iargs [0], vtable);
4484 if (1 || klass->has_finalize || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
4485 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
4487 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
4489 NEW_TEMPLOAD (cfg, load, temp);
4490 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4491 MONO_INST_NEW (cfg, add, CEE_ADD);
4492 add->inst_left = load;
4493 add->inst_right = vtoffset;
4496 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
4497 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
4498 vstore->cil_code = ip;
4499 vstore->inst_left = add;
4500 vstore->inst_right = val;
4502 if (vstore->opcode == CEE_STOBJ) {
4503 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
4505 MONO_ADD_INS (bblock, vstore);
4507 NEW_TEMPLOAD (cfg, load, temp);
4515 MONO_INST_NEW (cfg, ins, *ip);
4520 token = read32 (ip + 1);
4522 /* allocate the domainvar - becaus this is used in decompose_foreach */
4523 if (cfg->opt & MONO_OPT_SHARED)
4524 mono_get_domainvar (cfg);
4526 if (method->wrapper_type != MONO_WRAPPER_NONE)
4527 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4529 klass = mono_class_get (image, token);
4531 mono_class_init (klass);
4532 if (klass->byval_arg.type == MONO_TYPE_VAR)
4533 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4534 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4535 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4536 ins->inst_newa_class = klass;
4537 ins->inst_newa_len = *sp;
4538 ins->type = STACK_OBJ;
4542 * we store the object so calls to create the array are not interleaved
4543 * with the arguments of other calls.
4546 MonoInst *store, *temp, *load;
4548 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
4549 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4550 store->cil_code = ins->cil_code;
4551 MONO_ADD_INS (bblock, store);
4552 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
4553 load->cil_code = ins->cil_code;
4560 MONO_INST_NEW (cfg, ins, *ip);
4561 ins->cil_code = ip++;
4563 ins->inst_left = *sp;
4564 ins->type = STACK_PTR;
4572 if (method->wrapper_type != MONO_WRAPPER_NONE)
4573 klass = (MonoClass*)mono_method_get_wrapper_data (method, read32 (ip + 1));
4575 klass = mono_class_get (image, read32 (ip + 1));
4576 mono_class_init (klass);
4577 if (klass->byval_arg.type == MONO_TYPE_VAR)
4578 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4579 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4580 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4581 NEW_LDELEMA (cfg, ins, sp, klass);
4591 token = read32 (ip + 1);
4592 klass = mono_class_get (image, token);
4593 mono_class_init (klass);
4594 if (klass->byval_arg.type == MONO_TYPE_VAR)
4595 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4596 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4597 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4598 NEW_LDELEMA (cfg, load, sp, klass);
4599 load->cil_code = ip;
4600 MONO_INST_NEW (cfg, ins, mono_type_to_ldind (&klass->byval_arg));
4602 ins->inst_left = load;
4604 type_to_eval_stack_type (&klass->byval_arg, ins);
4618 case CEE_LDELEM_REF: {
4622 * ldind.x (ldelema (array, index))
4623 * ldelema does the bounds check
4627 klass = array_access_to_klass (*ip);
4628 NEW_LDELEMA (cfg, load, sp, klass);
4629 load->cil_code = ip;
4630 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
4632 ins->inst_left = load;
4634 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
4644 case CEE_STELEM_R8: {
4648 * stind.x (ldelema (array, index), val)
4649 * ldelema does the bounds check
4653 klass = array_access_to_klass (*ip);
4654 NEW_LDELEMA (cfg, load, sp, klass);
4655 load->cil_code = ip;
4656 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
4658 ins->inst_left = load;
4659 ins->inst_right = sp [2];
4661 handle_loaded_temps (cfg, bblock, stack_start, sp);
4662 MONO_ADD_INS (bblock, ins);
4670 * stind.x (ldelema (array, index), val)
4671 * ldelema does the bounds check
4676 token = read32 (ip + 1);
4677 klass = mono_class_get (image, token);
4678 mono_class_init (klass);
4679 if (klass->byval_arg.type == MONO_TYPE_VAR)
4680 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4681 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4682 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4683 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4684 MonoInst *iargs [3];
4685 handle_loaded_temps (cfg, bblock, stack_start, sp);
4691 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4693 NEW_LDELEMA (cfg, load, sp, klass);
4694 load->cil_code = ip;
4695 MONO_INST_NEW (cfg, ins, mono_type_to_stind (&klass->byval_arg));
4697 ins->inst_left = load;
4698 ins->inst_right = sp [2];
4699 handle_loaded_temps (cfg, bblock, stack_start, sp);
4700 MONO_ADD_INS (bblock, ins);
4706 case CEE_STELEM_REF: {
4707 MonoInst *iargs [3];
4712 handle_loaded_temps (cfg, bblock, stack_start, sp);
4718 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4722 NEW_GROUP (cfg, group, sp [0], sp [1]);
4723 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
4725 ins->inst_left = group;
4726 ins->inst_right = sp [2];
4727 MONO_ADD_INS (bblock, ins);
4734 case CEE_CKFINITE: {
4735 MonoInst *store, *temp;
4738 /* this instr. can throw exceptions as side effect,
4739 * so we cant eliminate dead code which contains CKFINITE opdodes.
4740 * Spilling to memory makes sure that we always perform
4744 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
4746 ins->inst_left = sp [-1];
4747 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
4749 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4750 store->cil_code = ip;
4751 MONO_ADD_INS (bblock, store);
4753 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
4760 MONO_INST_NEW (cfg, ins, *ip);
4763 klass = mono_class_get (image, read32 (ip + 1));
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 ins->type = STACK_MP;
4770 ins->inst_left = *sp;
4772 ins->inst_newa_class = klass;
4777 case CEE_MKREFANY: {
4778 MonoInst *loc, *klassconst;
4781 MONO_INST_NEW (cfg, ins, *ip);
4784 klass = mono_class_get (image, read32 (ip + 1));
4785 mono_class_init (klass);
4786 if (klass->byval_arg.type == MONO_TYPE_VAR)
4787 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4788 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
4789 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
4792 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
4793 NEW_TEMPLOADA (cfg, ins->inst_right, loc->inst_c0);
4795 NEW_PCONST (cfg, klassconst, klass);
4796 NEW_GROUP (cfg, ins->inst_left, *sp, klassconst);
4798 MONO_ADD_INS (bblock, ins);
4800 NEW_TEMPLOAD (cfg, *sp, loc->inst_c0);
4807 MonoClass *handle_class;
4809 CHECK_STACK_OVF (1);
4812 n = read32 (ip + 1);
4814 handle = mono_ldtoken (image, n, &handle_class);
4815 mono_class_init (handle_class);
4817 if (cfg->opt & MONO_OPT_SHARED) {
4819 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
4821 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
4823 NEW_IMAGECONST (cfg, iargs [0], image);
4824 NEW_ICONST (cfg, iargs [1], n);
4825 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
4826 NEW_TEMPLOAD (cfg, res, temp);
4827 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4828 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
4829 MONO_ADD_INS (bblock, store);
4830 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4832 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method (image, read32 (ip + 6), NULL)) &&
4833 (cmethod->klass == mono_defaults.monotype_class->parent) &&
4834 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
4835 MonoClass *tclass = mono_class_from_mono_type (handle);
4836 mono_class_init (tclass);
4837 if (mono_compile_aot)
4838 NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n);
4840 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
4841 ins->type = STACK_OBJ;
4842 ins->klass = cmethod->klass;
4845 if (mono_compile_aot)
4846 NEW_LDTOKENCONST (cfg, ins, image, n);
4848 NEW_PCONST (cfg, ins, handle);
4849 ins->type = STACK_VTYPE;
4850 ins->klass = handle_class;
4866 case CEE_ADD_OVF_UN:
4868 case CEE_MUL_OVF_UN:
4870 case CEE_SUB_OVF_UN:
4875 case CEE_ENDFINALLY:
4876 /* FIXME: check stack state */
4877 MONO_INST_NEW (cfg, ins, *ip);
4878 MONO_ADD_INS (bblock, ins);
4879 ins->cil_code = ip++;
4880 start_new_bblock = 1;
4885 if (*ip == CEE_LEAVE) {
4887 target = ip + 5 + (gint32)read32(ip + 1);
4890 target = ip + 2 + (signed char)(ip [1]);
4893 /* empty the stack */
4894 while (sp != stack_start) {
4895 MONO_INST_NEW (cfg, ins, CEE_POP);
4899 MONO_ADD_INS (bblock, ins);
4903 * If this leave statement is in a catch block, check for a
4904 * pending exception, and rethrow it if necessary.
4906 for (i = 0; i < header->num_clauses; ++i) {
4907 MonoExceptionClause *clause = &header->clauses [i];
4908 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code)) {
4911 temp = mono_emit_jit_icall (cfg, bblock, mono_thread_get_pending_exception, NULL, ip);
4912 NEW_TEMPLOAD (cfg, *sp, temp);
4914 MONO_INST_NEW (cfg, ins, OP_THROW_OR_NULL);
4915 ins->inst_left = *sp;
4917 MONO_ADD_INS (bblock, ins);
4921 /* fixme: call fault handler ? */
4923 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
4925 for (tmp = handlers; tmp; tmp = tmp->next) {
4927 link_bblock (cfg, bblock, tblock);
4928 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
4930 ins->inst_target_bb = tblock;
4931 MONO_ADD_INS (bblock, ins);
4933 g_list_free (handlers);
4936 MONO_INST_NEW (cfg, ins, CEE_BR);
4938 MONO_ADD_INS (bblock, ins);
4939 GET_BBLOCK (cfg, bbhash, tblock, target);
4940 link_bblock (cfg, bblock, tblock);
4941 CHECK_BBLOCK (target, ip, tblock);
4942 ins->inst_target_bb = tblock;
4943 start_new_bblock = 1;
4945 if (*ip == CEE_LEAVE)
4954 MONO_INST_NEW (cfg, ins, *ip);
4956 handle_loaded_temps (cfg, bblock, stack_start, sp);
4957 MONO_ADD_INS (bblock, ins);
4958 ins->cil_code = ip++;
4959 ins->inst_i0 = sp [0];
4960 ins->inst_i1 = sp [1];
4968 /* trampoline mono specific opcodes */
4969 case MONO_CUSTOM_PREFIX: {
4971 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
4976 case CEE_MONO_FUNC1: {
4978 gpointer func = NULL;
4984 case MONO_MARSHAL_CONV_STR_LPWSTR:
4985 func = mono_string_to_utf16;
4987 case MONO_MARSHAL_CONV_LPWSTR_STR:
4988 func = mono_string_from_utf16;
4990 case MONO_MARSHAL_CONV_LPSTR_STR:
4991 func = mono_string_new_wrapper;
4993 case MONO_MARSHAL_CONV_STR_LPTSTR:
4994 case MONO_MARSHAL_CONV_STR_LPSTR:
4995 func = mono_string_to_utf8;
4997 case MONO_MARSHAL_CONV_STR_BSTR:
4998 func = mono_string_to_bstr;
5000 case MONO_MARSHAL_CONV_STR_TBSTR:
5001 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
5002 func = mono_string_to_ansibstr;
5004 case MONO_MARSHAL_CONV_SB_LPSTR:
5005 func = mono_string_builder_to_utf8;
5007 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
5008 func = mono_array_to_savearray;
5010 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
5011 func = mono_array_to_lparray;
5013 case MONO_MARSHAL_CONV_DEL_FTN:
5014 func = mono_delegate_to_ftnptr;
5016 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
5017 func = mono_marshal_string_array;
5020 g_warning ("unknown conversion %d\n", ip [2]);
5021 g_assert_not_reached ();
5024 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5025 NEW_TEMPLOAD (cfg, *sp, temp);
5029 inline_costs += 10 * num_calls++;
5032 case CEE_MONO_PROC2: {
5033 gpointer func = NULL;
5039 case MONO_MARSHAL_CONV_LPSTR_SB:
5040 func = mono_string_utf8_to_builder;
5042 case MONO_MARSHAL_FREE_ARRAY:
5043 func = mono_marshal_free_array;
5046 g_assert_not_reached ();
5049 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5051 inline_costs += 10 * num_calls++;
5054 case CEE_MONO_PROC3: {
5055 gpointer func = NULL;
5061 case MONO_MARSHAL_CONV_STR_BYVALSTR:
5062 func = mono_string_to_byvalstr;
5064 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
5065 func = mono_string_to_byvalwstr;
5068 g_assert_not_reached ();
5071 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
5073 inline_costs += 10 * num_calls++;
5079 mono_emit_jit_icall (cfg, bblock, g_free, sp, ip);
5081 inline_costs += 10 * num_calls++;
5083 case CEE_MONO_LDPTR:
5084 CHECK_STACK_OVF (1);
5086 token = read32 (ip + 2);
5087 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
5091 inline_costs += 10 * num_calls++;
5093 case CEE_MONO_VTADDR:
5096 MONO_INST_NEW (cfg, ins, OP_VTADDR);
5098 ins->type = STACK_MP;
5099 ins->inst_left = *sp;
5103 case CEE_MONO_NEWOBJ: {
5104 MonoInst *iargs [2];
5106 CHECK_STACK_OVF (1);
5108 token = read32 (ip + 2);
5109 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5110 mono_class_init (klass);
5111 NEW_DOMAINCONST (cfg, iargs [0]);
5112 NEW_CLASSCONST (cfg, iargs [1], klass);
5113 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
5114 NEW_TEMPLOAD (cfg, *sp, temp);
5117 inline_costs += 10 * num_calls++;
5120 case CEE_MONO_OBJADDR:
5123 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
5125 ins->type = STACK_MP;
5126 ins->inst_left = *sp;
5130 case CEE_MONO_LDNATIVEOBJ:
5133 token = read32 (ip + 2);
5134 klass = mono_method_get_wrapper_data (method, token);
5135 g_assert (klass->valuetype);
5136 mono_class_init (klass);
5137 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
5141 case CEE_MONO_RETOBJ:
5142 g_assert (cfg->ret);
5143 g_assert (method->signature->pinvoke);
5148 token = read32 (ip + 2);
5149 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
5151 NEW_RETLOADA (cfg, ins);
5152 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
5154 if (sp != stack_start)
5157 MONO_INST_NEW (cfg, ins, CEE_BR);
5159 ins->inst_target_bb = end_bblock;
5160 MONO_ADD_INS (bblock, ins);
5161 link_bblock (cfg, bblock, end_bblock);
5162 start_new_bblock = 1;
5166 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
5175 /* somewhat similar to LDTOKEN */
5176 MonoInst *addr, *vtvar;
5177 CHECK_STACK_OVF (1);
5178 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
5180 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
5181 addr->cil_code = ip;
5182 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
5184 ins->inst_left = addr;
5185 MONO_ADD_INS (bblock, ins);
5186 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
5199 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
5200 MONO_INST_NEW (cfg, ins, cmp->opcode);
5202 cmp->inst_i0 = sp [0];
5203 cmp->inst_i1 = sp [1];
5207 cmp->opcode = OP_COMPARE;
5209 ins->type = STACK_I4;
5219 CHECK_STACK_OVF (1);
5221 n = read32 (ip + 2);
5222 if (method->wrapper_type != MONO_WRAPPER_NONE)
5223 cmethod = mono_method_get_wrapper_data (method, n);
5225 cmethod = mono_get_method (image, n, NULL);
5228 * We can't do this in mono_ldftn, since it is used in
5229 * the synchronized wrapper, leading to an infinite loop.
5231 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
5232 cmethod = mono_marshal_get_synchronized_wrapper (cmethod);
5235 mono_class_init (cmethod->klass);
5236 handle_loaded_temps (cfg, bblock, stack_start, sp);
5238 NEW_METHODCONST (cfg, argconst, cmethod);
5239 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
5240 NEW_TEMPLOAD (cfg, *sp, temp);
5244 inline_costs += 10 * num_calls++;
5247 case CEE_LDVIRTFTN: {
5253 n = read32 (ip + 2);
5254 if (method->wrapper_type != MONO_WRAPPER_NONE)
5255 cmethod = mono_method_get_wrapper_data (method, n);
5257 cmethod = mono_get_method (image, n, NULL);
5259 mono_class_init (cmethod->klass);
5260 handle_loaded_temps (cfg, bblock, stack_start, sp);
5264 NEW_METHODCONST (cfg, args [1], cmethod);
5265 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
5266 NEW_TEMPLOAD (cfg, *sp, temp);
5270 inline_costs += 10 * num_calls++;
5274 CHECK_STACK_OVF (1);
5276 n = read16 (ip + 2);
5278 NEW_ARGLOAD (cfg, ins, n);
5284 CHECK_STACK_OVF (1);
5286 n = read16 (ip + 2);
5288 NEW_ARGLOADA (cfg, ins, n);
5296 handle_loaded_temps (cfg, bblock, stack_start, sp);
5298 n = read16 (ip + 2);
5300 NEW_ARGSTORE (cfg, ins, n, *sp);
5302 if (ins->opcode == CEE_STOBJ) {
5303 NEW_ARGLOADA (cfg, ins, n);
5304 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5306 MONO_ADD_INS (bblock, ins);
5310 CHECK_STACK_OVF (1);
5312 n = read16 (ip + 2);
5314 NEW_LOCLOAD (cfg, ins, n);
5320 CHECK_STACK_OVF (1);
5322 n = read16 (ip + 2);
5324 NEW_LOCLOADA (cfg, ins, n);
5333 n = read16 (ip + 2);
5335 handle_loaded_temps (cfg, bblock, stack_start, sp);
5336 NEW_LOCSTORE (cfg, ins, n, *sp);
5338 if (ins->opcode == CEE_STOBJ) {
5339 NEW_LOCLOADA (cfg, ins, n);
5340 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
5342 MONO_ADD_INS (bblock, ins);
5349 if (sp != stack_start)
5351 MONO_INST_NEW (cfg, ins, 256 + ip [1]);
5352 ins->inst_left = *sp;
5355 cfg->flags |= MONO_CFG_HAS_ALLOCA;
5356 if (header->init_locals)
5357 ins->flags |= MONO_INST_INIT;
5361 /* FIXME: set init flag if locals init is set in this method */
5363 case CEE_ENDFILTER: {
5364 MonoExceptionClause *clause, *nearest;
5365 int cc, nearest_num;
5369 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
5371 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
5372 ins->inst_left = *sp;
5374 MONO_ADD_INS (bblock, ins);
5375 start_new_bblock = 1;
5380 for (cc = 0; cc < header->num_clauses; ++cc) {
5381 clause = &header->clauses [cc];
5382 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
5383 (!nearest || (clause->token_or_filter > nearest->token_or_filter))) {
5389 filter_lengths [nearest_num] = (ip - header->code) - nearest->token_or_filter;
5393 case CEE_UNALIGNED_:
5394 ins_flag |= MONO_INST_UNALIGNED;
5395 /* FIXME: record alignment? we can assume 1 for now */
5400 ins_flag |= MONO_INST_VOLATILE;
5404 ins_flag |= MONO_INST_TAILCALL;
5405 /* Can't inline tail calls at this time */
5406 inline_costs += 100000;
5413 token = read32 (ip + 2);
5414 if (method->wrapper_type != MONO_WRAPPER_NONE)
5415 klass = mono_method_get_wrapper_data (method, token);
5417 klass = mono_class_get (image, token);
5418 if (klass->byval_arg.type == MONO_TYPE_VAR)
5419 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
5420 else if (klass->byval_arg.type == MONO_TYPE_MVAR)
5421 klass = MTYPE_PARAM_TO_CLASS (klass->byval_arg.data.generic_param->num);
5423 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5424 MonoInst *store, *load;
5425 NEW_PCONST (cfg, load, NULL);
5426 load->cil_code = ip;
5427 load->type = STACK_OBJ;
5428 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
5429 store->cil_code = ip;
5430 handle_loaded_temps (cfg, bblock, stack_start, sp);
5431 MONO_ADD_INS (bblock, store);
5432 store->inst_i0 = sp [0];
5433 store->inst_i1 = load;
5436 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
5441 case CEE_CONSTRAINED_:
5442 /* FIXME: implement */
5444 token = read32 (ip + 2);
5449 MonoInst *iargs [3];
5455 handle_loaded_temps (cfg, bblock, stack_start, sp);
5456 if (ip [1] == CEE_CPBLK) {
5457 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
5459 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
5468 ins_flag |= MONO_INST_NOTYPECHECK;
5470 ins_flag |= MONO_INST_NORANGECHECK;
5471 /* we ignore the no-nullcheck for now since we
5472 * really do it explicitly only when doing callvirt->call
5478 /* FIXME: check we are in a catch handler */
5479 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
5480 load->cil_code = ip;
5481 MONO_INST_NEW (cfg, ins, CEE_THROW);
5482 ins->inst_left = load;
5484 MONO_ADD_INS (bblock, ins);
5486 start_new_bblock = 1;
5491 CHECK_STACK_OVF (1);
5493 token = read32 (ip + 2);
5494 /* FIXXME: handle generics. */
5495 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
5496 MonoType *type = mono_type_create_from_typespec (image, token);
5497 token = mono_type_size (type, &align);
5499 MonoClass *szclass = mono_class_get (image, token);
5500 mono_class_init (szclass);
5501 token = mono_class_value_size (szclass, &align);
5503 NEW_ICONST (cfg, ins, token);
5508 case CEE_REFANYTYPE:
5510 MONO_INST_NEW (cfg, ins, ip [1]);
5512 ins->type = STACK_MP;
5513 ins->inst_left = *sp;
5514 ins->type = STACK_VTYPE;
5515 ins->klass = mono_defaults.typehandle_class;
5524 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
5529 g_error ("opcode 0x%02x not handled", *ip);
5532 if (start_new_bblock != 1)
5535 bblock->cil_length = ip - bblock->cil_code;
5536 bblock->next_bb = end_bblock;
5537 link_bblock (cfg, bblock, end_bblock);
5539 if (cfg->method == method && cfg->domainvar) {
5543 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
5544 call->signature = helper_sig_domain_get;
5545 call->inst.type = STACK_PTR;
5546 call->fptr = mono_domain_get;
5547 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, (MonoInst*)call);
5549 MONO_ADD_INS (init_localsbb, store);
5552 if (header->init_locals) {
5554 for (i = 0; i < header->num_locals; ++i) {
5555 int t = header->locals [i]->type;
5556 if (t == MONO_TYPE_VALUETYPE && header->locals [i]->data.klass->enumtype)
5557 t = header->locals [i]->data.klass->enum_basetype->type;
5558 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
5559 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
5560 NEW_ICONST (cfg, ins, 0);
5561 NEW_LOCSTORE (cfg, store, i, ins);
5562 MONO_ADD_INS (init_localsbb, store);
5563 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
5564 MONO_INST_NEW (cfg, ins, OP_I8CONST);
5565 ins->type = STACK_I8;
5567 NEW_LOCSTORE (cfg, store, i, ins);
5568 MONO_ADD_INS (init_localsbb, store);
5569 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
5570 MONO_INST_NEW (cfg, ins, OP_R8CONST);
5571 ins->type = STACK_R8;
5572 ins->inst_p0 = (void*)&r8_0;
5573 NEW_LOCSTORE (cfg, store, i, ins);
5574 MONO_ADD_INS (init_localsbb, store);
5575 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF)) {
5576 NEW_LOCLOADA (cfg, ins, i);
5577 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (header->locals [i]), NULL, NULL);
5580 NEW_PCONST (cfg, ins, NULL);
5581 NEW_LOCSTORE (cfg, store, i, ins);
5582 MONO_ADD_INS (init_localsbb, store);
5588 /* resolve backward branches in the middle of an existing basic block */
5589 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
5591 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
5592 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
5593 if (tblock != start_bblock) {
5595 split_bblock (cfg, tblock, bblock);
5596 l = bblock->cil_code - header->code;
5597 bblock->cil_length = tblock->cil_length - l;
5598 tblock->cil_length = l;
5600 g_print ("recheck failed.\n");
5604 /* we compute regions here, because the length of filter clauses is not known in advance.
5605 * It is computed in the CEE_ENDFILTER case in the above switch statement*/
5606 if (cfg->method == method) {
5608 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5609 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
5611 mono_create_spvar_for_region (cfg, bb->region);
5612 if (cfg->verbose_level > 2)
5613 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
5616 g_hash_table_destroy (bbhash);
5619 dont_inline = g_list_remove (dont_inline, method);
5620 return inline_costs;
5623 if (cfg->method != method)
5624 g_hash_table_destroy (bbhash);
5625 dont_inline = g_list_remove (dont_inline, method);
5629 if (cfg->method != method)
5630 g_hash_table_destroy (bbhash);
5631 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
5632 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
5633 dont_inline = g_list_remove (dont_inline, method);
5638 mono_print_tree (MonoInst *tree) {
5644 arity = mono_burg_arity [tree->opcode];
5646 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
5648 switch (tree->opcode) {
5650 printf ("[%d]", tree->inst_c0);
5653 printf ("[%lld]", tree->inst_l);
5656 printf ("[%f]", *(double*)tree->inst_p0);
5659 printf ("[%f]", *(float*)tree->inst_p0);
5663 printf ("[%d]", tree->inst_c0);
5666 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5669 printf ("[%s]", mono_arch_regname (tree->dreg));
5672 printf ("[%s]", tree->inst_newa_class->name);
5673 mono_print_tree (tree->inst_newa_len);
5684 case OP_VOIDCALLVIRT: {
5685 MonoCallInst *call = (MonoCallInst*)tree;
5687 printf ("[%s]", call->method->name);
5692 printf ("[%d (", tree->inst_c0);
5693 for (i = 0; i < tree->inst_phi_args [0]; i++) {
5696 printf ("%d", tree->inst_phi_args [i + 1]);
5708 printf ("[B%d]", tree->inst_target_bb->block_num);
5718 case OP_VOIDCALL_REG:
5719 mono_print_tree (tree->inst_left);
5731 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
5732 mono_print_tree (tree->inst_left);
5736 mono_print_tree (tree->inst_left);
5738 mono_print_tree (tree->inst_right);
5748 mono_print_tree_nl (MonoInst *tree)
5750 mono_print_tree (tree);
5755 create_helper_signature (void)
5757 /* FIXME: set call conv */
5758 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
5759 helper_sig_newarr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5760 helper_sig_newarr->params [0] = helper_sig_newarr->params [1] = &mono_defaults.int_class->byval_arg;
5761 helper_sig_newarr->ret = &mono_defaults.object_class->byval_arg;
5762 helper_sig_newarr->params [2] = &mono_defaults.int32_class->byval_arg;
5763 helper_sig_newarr->pinvoke = 1;
5765 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
5766 helper_sig_newarr_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5767 helper_sig_newarr_specific->params [0] = &mono_defaults.int_class->byval_arg;
5768 helper_sig_newarr_specific->params [1] = &mono_defaults.int32_class->byval_arg;
5769 helper_sig_newarr_specific->ret = &mono_defaults.object_class->byval_arg;
5770 helper_sig_newarr_specific->pinvoke = 1;
5772 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
5773 helper_sig_object_new = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5774 helper_sig_object_new->params [0] = helper_sig_object_new->params [1] = &mono_defaults.int_class->byval_arg;
5775 helper_sig_object_new->ret = &mono_defaults.object_class->byval_arg;
5776 helper_sig_object_new->pinvoke = 1;
5778 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
5779 helper_sig_object_new_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5780 helper_sig_object_new_specific->params [0] = &mono_defaults.int_class->byval_arg;
5781 helper_sig_object_new_specific->ret = &mono_defaults.object_class->byval_arg;
5782 helper_sig_object_new_specific->pinvoke = 1;
5784 /* void* mono_method_compile (MonoMethod*) */
5785 helper_sig_compile = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5786 helper_sig_compile->params [0] = helper_sig_compile->ret = &mono_defaults.int_class->byval_arg;
5787 helper_sig_compile->pinvoke = 1;
5789 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
5790 helper_sig_compile_virt = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5791 helper_sig_compile_virt->params [0] = &mono_defaults.object_class->byval_arg;
5792 helper_sig_compile_virt->params [1] = helper_sig_compile_virt->ret = &mono_defaults.int_class->byval_arg;
5793 helper_sig_compile_virt->pinvoke = 1;
5795 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
5796 helper_sig_ldstr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5797 helper_sig_ldstr->params [0] = helper_sig_ldstr->params [1] = &mono_defaults.int_class->byval_arg;
5798 helper_sig_ldstr->params [2] = &mono_defaults.int32_class->byval_arg;
5799 helper_sig_ldstr->ret = &mono_defaults.object_class->byval_arg;
5800 helper_sig_ldstr->pinvoke = 1;
5802 /* MonoDomain *mono_domain_get (void) */
5803 helper_sig_domain_get = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5804 helper_sig_domain_get->ret = &mono_defaults.int_class->byval_arg;
5805 helper_sig_domain_get->pinvoke = 1;
5807 /* void* stelem_ref (MonoArray *, int index, MonoObject *) */
5808 helper_sig_stelem_ref = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5809 helper_sig_stelem_ref->params [0] = &mono_defaults.array_class->byval_arg;
5810 helper_sig_stelem_ref->params [1] = &mono_defaults.int32_class->byval_arg;
5811 helper_sig_stelem_ref->params [2] = &mono_defaults.object_class->byval_arg;
5812 helper_sig_stelem_ref->ret = &mono_defaults.void_class->byval_arg;
5813 helper_sig_stelem_ref->pinvoke = 1;
5815 /* void* stelem_ref_check (MonoArray *, MonoObject *) */
5816 helper_sig_stelem_ref_check = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5817 helper_sig_stelem_ref_check->params [0] = &mono_defaults.array_class->byval_arg;
5818 helper_sig_stelem_ref_check->params [1] = &mono_defaults.object_class->byval_arg;
5819 helper_sig_stelem_ref_check->ret = &mono_defaults.void_class->byval_arg;
5820 helper_sig_stelem_ref_check->pinvoke = 1;
5822 /* long amethod (long, long) */
5823 helper_sig_long_long_long = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5824 helper_sig_long_long_long->params [0] = helper_sig_long_long_long->params [1] =
5825 &mono_defaults.int64_class->byval_arg;
5826 helper_sig_long_long_long->ret = &mono_defaults.int64_class->byval_arg;
5827 helper_sig_long_long_long->pinvoke = 1;
5829 /* object amethod (intptr) */
5830 helper_sig_obj_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5831 helper_sig_obj_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5832 helper_sig_obj_ptr->ret = &mono_defaults.object_class->byval_arg;
5833 helper_sig_obj_ptr->pinvoke = 1;
5835 /* void amethod (intptr) */
5836 helper_sig_void_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5837 helper_sig_void_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5838 helper_sig_void_ptr->ret = &mono_defaults.void_class->byval_arg;
5839 helper_sig_void_ptr->pinvoke = 1;
5841 /* void amethod (MonoObject *obj) */
5842 helper_sig_void_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5843 helper_sig_void_obj->params [0] = &mono_defaults.object_class->byval_arg;
5844 helper_sig_void_obj->ret = &mono_defaults.void_class->byval_arg;
5845 helper_sig_void_obj->pinvoke = 1;
5847 /* intptr amethod (void) */
5848 helper_sig_ptr_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5849 helper_sig_ptr_void->ret = &mono_defaults.int_class->byval_arg;
5850 helper_sig_ptr_void->pinvoke = 1;
5852 /* object amethod (void) */
5853 helper_sig_obj_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5854 helper_sig_obj_void->ret = &mono_defaults.object_class->byval_arg;
5855 helper_sig_obj_void->pinvoke = 1;
5857 /* void amethod (intptr, intptr) */
5858 helper_sig_void_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5859 helper_sig_void_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5860 helper_sig_void_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5861 helper_sig_void_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
5862 helper_sig_void_ptr_ptr->pinvoke = 1;
5864 /* void amethod (intptr, intptr, intptr) */
5865 helper_sig_void_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5866 helper_sig_void_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5867 helper_sig_void_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5868 helper_sig_void_ptr_ptr_ptr->params [2] = &mono_defaults.int_class->byval_arg;
5869 helper_sig_void_ptr_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
5870 helper_sig_void_ptr_ptr_ptr->pinvoke = 1;
5872 /* intptr amethod (intptr, intptr) */
5873 helper_sig_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5874 helper_sig_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5875 helper_sig_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5876 helper_sig_ptr_ptr_ptr->ret = &mono_defaults.int_class->byval_arg;
5877 helper_sig_ptr_ptr_ptr->pinvoke = 1;
5879 /* IntPtr amethod (object) */
5880 helper_sig_ptr_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5881 helper_sig_ptr_obj->params [0] = &mono_defaults.object_class->byval_arg;
5882 helper_sig_ptr_obj->ret = &mono_defaults.int_class->byval_arg;
5883 helper_sig_ptr_obj->pinvoke = 1;
5885 /* IntPtr amethod (int) */
5886 helper_sig_ptr_int = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5887 helper_sig_ptr_int->params [0] = &mono_defaults.int32_class->byval_arg;
5888 helper_sig_ptr_int->ret = &mono_defaults.int_class->byval_arg;
5889 helper_sig_ptr_int->pinvoke = 1;
5891 /* long amethod (long, guint32) */
5892 helper_sig_long_long_int = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5893 helper_sig_long_long_int->params [0] = &mono_defaults.int64_class->byval_arg;
5894 helper_sig_long_long_int->params [1] = &mono_defaults.int32_class->byval_arg;
5895 helper_sig_long_long_int->ret = &mono_defaults.int64_class->byval_arg;
5896 helper_sig_long_long_int->pinvoke = 1;
5898 /* ulong amethod (double) */
5899 helper_sig_ulong_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5900 helper_sig_ulong_double->params [0] = &mono_defaults.double_class->byval_arg;
5901 helper_sig_ulong_double->ret = &mono_defaults.uint64_class->byval_arg;
5902 helper_sig_ulong_double->pinvoke = 1;
5904 /* long amethod (double) */
5905 helper_sig_long_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5906 helper_sig_long_double->params [0] = &mono_defaults.double_class->byval_arg;
5907 helper_sig_long_double->ret = &mono_defaults.int64_class->byval_arg;
5908 helper_sig_long_double->pinvoke = 1;
5910 /* double amethod (long) */
5911 helper_sig_double_long = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5912 helper_sig_double_long->params [0] = &mono_defaults.int64_class->byval_arg;
5913 helper_sig_double_long->ret = &mono_defaults.double_class->byval_arg;
5914 helper_sig_double_long->pinvoke = 1;
5916 /* float amethod (long) */
5917 helper_sig_float_long = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5918 helper_sig_float_long->params [0] = &mono_defaults.int64_class->byval_arg;
5919 helper_sig_float_long->ret = &mono_defaults.single_class->byval_arg;
5920 helper_sig_float_long->pinvoke = 1;
5922 /* double amethod (double, double) */
5923 helper_sig_double_double_double = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5924 helper_sig_double_double_double->params [0] = &mono_defaults.double_class->byval_arg;
5925 helper_sig_double_double_double->params [1] = &mono_defaults.double_class->byval_arg;
5926 helper_sig_double_double_double->ret = &mono_defaults.double_class->byval_arg;
5927 helper_sig_double_double_double->pinvoke = 1;
5929 /* uint amethod (double) */
5930 helper_sig_uint_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5931 helper_sig_uint_double->params [0] = &mono_defaults.double_class->byval_arg;
5932 helper_sig_uint_double->ret = &mono_defaults.uint32_class->byval_arg;
5933 helper_sig_uint_double->pinvoke = 1;
5935 /* int amethod (double) */
5936 helper_sig_int_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5937 helper_sig_int_double->params [0] = &mono_defaults.double_class->byval_arg;
5938 helper_sig_int_double->ret = &mono_defaults.int32_class->byval_arg;
5939 helper_sig_int_double->pinvoke = 1;
5941 /* void initobj (intptr, int size) */
5942 helper_sig_initobj = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5943 helper_sig_initobj->params [0] = &mono_defaults.int_class->byval_arg;
5944 helper_sig_initobj->params [1] = &mono_defaults.int32_class->byval_arg;
5945 helper_sig_initobj->ret = &mono_defaults.void_class->byval_arg;
5946 helper_sig_initobj->pinvoke = 1;
5948 /* void memcpy (intptr, intptr, int size) */
5949 helper_sig_memcpy = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5950 helper_sig_memcpy->params [0] = &mono_defaults.int_class->byval_arg;
5951 helper_sig_memcpy->params [1] = &mono_defaults.int_class->byval_arg;
5952 helper_sig_memcpy->params [2] = &mono_defaults.int32_class->byval_arg;
5953 helper_sig_memcpy->ret = &mono_defaults.void_class->byval_arg;
5954 helper_sig_memcpy->pinvoke = 1;
5956 /* void memset (intptr, int val, int size) */
5957 helper_sig_memset = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5958 helper_sig_memset->params [0] = &mono_defaults.int_class->byval_arg;
5959 helper_sig_memset->params [1] = &mono_defaults.int32_class->byval_arg;
5960 helper_sig_memset->params [2] = &mono_defaults.int32_class->byval_arg;
5961 helper_sig_memset->ret = &mono_defaults.void_class->byval_arg;
5962 helper_sig_memset->pinvoke = 1;
5964 helper_sig_class_init_trampoline = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5965 helper_sig_class_init_trampoline->ret = &mono_defaults.void_class->byval_arg;
5966 helper_sig_class_init_trampoline->pinvoke = 1;
5968 /* MonoObject * amethod (MonoObject *obj, MonoClass *klass) */
5969 helper_sig_obj_obj_cls = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5970 helper_sig_obj_obj_cls->params [0] = &mono_defaults.object_class->byval_arg;
5971 helper_sig_obj_obj_cls->params [1] = &mono_defaults.int_class->byval_arg;
5972 helper_sig_obj_obj_cls->ret = &mono_defaults.object_class->byval_arg;
5973 helper_sig_obj_obj_cls->pinvoke = 1;
5976 static GHashTable *jit_icall_hash_name = NULL;
5977 static GHashTable *jit_icall_hash_addr = NULL;
5980 mono_find_jit_icall_by_name (const char *name)
5982 g_assert (jit_icall_hash_name);
5984 //printf ("lookup addr %s %p\n", name, g_hash_table_lookup (jit_icall_hash_name, name));
5985 return g_hash_table_lookup (jit_icall_hash_name, name);
5989 mono_find_jit_icall_by_addr (gconstpointer addr)
5991 g_assert (jit_icall_hash_addr);
5993 return g_hash_table_lookup (jit_icall_hash_addr, (gpointer)addr);
5997 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
6000 MonoMethod *wrapper;
6002 if (callinfo->wrapper)
6003 return callinfo->wrapper;
6004 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
6005 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
6006 callinfo->wrapper = mono_jit_compile_method (wrapper);
6008 return callinfo->wrapper;
6012 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save)
6014 MonoJitICallInfo *info;
6019 if (!jit_icall_hash_name) {
6020 jit_icall_hash_name = g_hash_table_new (g_str_hash, g_str_equal);
6021 jit_icall_hash_addr = g_hash_table_new (NULL, NULL);
6024 if (g_hash_table_lookup (jit_icall_hash_name, name)) {
6025 g_warning ("jit icall already defined \"%s\"\n", name);
6026 g_assert_not_reached ();
6029 info = g_new (MonoJitICallInfo, 1);
6036 #ifdef MONO_USE_EXC_TABLES
6037 || mono_arch_has_unwind_info (func)
6040 info->wrapper = func;
6042 info->wrapper = NULL;
6043 mono_icall_get_wrapper (info);
6046 g_hash_table_insert (jit_icall_hash_name, (gpointer)info->name, info);
6047 g_hash_table_insert (jit_icall_hash_addr, (gpointer)func, info);
6048 if (func != info->wrapper)
6049 g_hash_table_insert (jit_icall_hash_addr, (gpointer)info->wrapper, info);
6055 mono_create_class_init_trampoline (MonoVTable *vtable)
6059 /* previously created trampoline code */
6060 mono_domain_lock (vtable->domain);
6062 mono_g_hash_table_lookup (vtable->domain->class_init_trampoline_hash,
6064 mono_domain_unlock (vtable->domain);
6068 code = mono_arch_create_class_init_trampoline (vtable);
6070 /* store trampoline address */
6071 mono_domain_lock (vtable->domain);
6072 mono_g_hash_table_insert (vtable->domain->class_init_trampoline_hash,
6074 mono_domain_unlock (vtable->domain);
6076 EnterCriticalSection (&class_init_hash_mutex);
6077 if (!class_init_hash_addr)
6078 class_init_hash_addr = g_hash_table_new (NULL, NULL);
6079 g_hash_table_insert (class_init_hash_addr, code, vtable);
6080 LeaveCriticalSection (&class_init_hash_mutex);
6086 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
6090 EnterCriticalSection (&class_init_hash_mutex);
6091 if (class_init_hash_addr)
6092 res = g_hash_table_lookup (class_init_hash_addr, addr);
6095 LeaveCriticalSection (&class_init_hash_mutex);
6100 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func, gboolean no_throw)
6102 MonoJitICallInfo *info;
6104 if (!emul_opcode_map)
6105 emul_opcode_map = g_new0 (MonoJitICallInfo*, OP_LAST + 1);
6107 g_assert (!sig->hasthis);
6108 g_assert (sig->param_count < 3);
6110 info = mono_register_jit_icall (func, name, sig, no_throw);
6112 emul_opcode_map [opcode] = info;
6116 decompose_foreach (MonoInst *tree, gpointer data)
6118 static MonoJitICallInfo *newarr_info = NULL;
6119 static MonoJitICallInfo *newarr_specific_info = NULL;
6120 MonoJitICallInfo *info;
6123 switch (tree->opcode) {
6125 MonoCompile *cfg = data;
6126 MonoInst *iargs [3];
6129 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
6130 g_assert (newarr_info);
6131 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
6132 g_assert (newarr_specific_info);
6135 if (cfg->opt & MONO_OPT_SHARED) {
6136 NEW_DOMAINCONST (cfg, iargs [0]);
6137 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
6138 iargs [2] = tree->inst_newa_len;
6143 MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
6145 NEW_VTABLECONST (cfg, iargs [0], vtable);
6146 iargs [1] = tree->inst_newa_len;
6148 info = newarr_specific_info;
6151 mono_emulate_opcode (cfg, tree, iargs, info);
6153 /* Need to decompose arguments after the the opcode is decomposed */
6154 for (i = 0; i < info->sig->param_count; ++i)
6155 dec_foreach (iargs [i], cfg);
6165 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
6167 switch (mono_burg_arity [tree->opcode]) {
6170 mono_inst_foreach (tree->inst_left, func, data);
6173 mono_inst_foreach (tree->inst_left, func, data);
6174 mono_inst_foreach (tree->inst_right, func, data);
6177 g_assert_not_reached ();
6184 mono_print_bb_code (MonoBasicBlock *bb) {
6186 MonoInst *c = bb->code;
6188 mono_print_tree (c);
6196 print_dfn (MonoCompile *cfg) {
6201 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
6203 for (i = 0; i < cfg->num_bblocks; ++i) {
6204 bb = cfg->bblocks [i];
6206 char* code1, *code2;
6207 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
6208 if (bb->last_ins->cil_code)
6209 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
6211 code2 = g_strdup ("");
6213 code1 [strlen (code1) - 1] = 0;
6214 code = g_strdup_printf ("%s -> %s", code1, code2);
6218 code = g_strdup ("\n");
6219 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
6221 MonoInst *c = bb->code;
6223 mono_print_tree (c);
6231 g_print ("\tprev:");
6232 for (j = 0; j < bb->in_count; ++j) {
6233 g_print (" BB%d", bb->in_bb [j]->block_num);
6235 g_print ("\t\tsucc:");
6236 for (j = 0; j < bb->out_count; ++j) {
6237 g_print (" BB%d", bb->out_bb [j]->block_num);
6239 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
6242 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
6245 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
6247 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
6255 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
6259 g_assert (bb->code);
6260 bb->last_ins->next = inst;
6261 bb->last_ins = inst;
6263 bb->last_ins = bb->code = inst;
6268 mono_destroy_compile (MonoCompile *cfg)
6270 //mono_mempool_stats (cfg->mempool);
6271 g_hash_table_destroy (cfg->bb_hash);
6273 mono_regstate_free (cfg->rs);
6275 g_hash_table_destroy (cfg->spvars);
6276 mono_mempool_destroy (cfg->mempool);
6277 g_list_free (cfg->ldstr_list);
6279 g_free (cfg->varinfo);
6285 mono_get_lmf_addr (void)
6287 MonoJitTlsData *jit_tls;
6289 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
6290 return &jit_tls->lmf;
6292 g_assert_not_reached ();
6297 * mono_thread_abort:
6298 * @obj: exception object
6300 * abort the thread, print exception information and stack trace
6303 mono_thread_abort (MonoObject *obj)
6305 /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
6307 /* handle_remove should be eventually called for this thread, too
6314 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
6316 MonoJitTlsData *jit_tls;
6319 jit_tls = g_new0 (MonoJitTlsData, 1);
6321 TlsSetValue (mono_jit_tls_id, jit_tls);
6323 jit_tls->abort_func = abort_func;
6324 jit_tls->end_of_stack = stack_start;
6326 lmf = g_new0 (MonoLMF, 1);
6329 jit_tls->lmf = jit_tls->first_lmf = lmf;
6331 mono_arch_setup_jit_tls_data (jit_tls);
6337 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
6340 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
6341 thread = mono_thread_current ();
6343 thread->jit_data = jit_tls;
6346 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
6349 mono_thread_abort_dummy (MonoObject *obj)
6351 if (mono_thread_attach_aborted_cb)
6352 mono_thread_attach_aborted_cb (obj);
6354 mono_thread_abort (obj);
6358 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
6361 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
6362 thread = mono_thread_current ();
6364 thread->jit_data = jit_tls;
6368 mini_thread_cleanup (MonoThread *thread)
6370 MonoJitTlsData *jit_tls = thread->jit_data;
6373 g_free (jit_tls->first_lmf);
6375 thread->jit_data = NULL;
6380 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
6382 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
6386 ji->data.target = target;
6387 ji->next = cfg->patch_info;
6389 cfg->patch_info = ji;
6393 mono_remove_patch_info (MonoCompile *cfg, int ip)
6395 MonoJumpInfo **ji = &cfg->patch_info;
6398 if ((*ji)->ip.i == ip)
6401 ji = &((*ji)->next);
6406 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
6407 MonoJitICallInfo *info;
6409 decompose_foreach (tree, cfg);
6411 switch (mono_burg_arity [tree->opcode]) {
6414 dec_foreach (tree->inst_left, cfg);
6416 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6417 MonoInst *iargs [2];
6419 iargs [0] = tree->inst_left;
6421 mono_emulate_opcode (cfg, tree, iargs, info);
6427 #ifdef MONO_ARCH_BIGMUL_INTRINS
6428 if (tree->opcode == OP_LMUL
6429 && (cfg->opt & MONO_OPT_INTRINS)
6430 && (tree->inst_left->opcode == CEE_CONV_I8
6431 || tree->inst_left->opcode == CEE_CONV_U8)
6432 && tree->inst_left->inst_left->type == STACK_I4
6433 && (tree->inst_right->opcode == CEE_CONV_I8
6434 || tree->inst_right->opcode == CEE_CONV_U8)
6435 && tree->inst_right->inst_left->type == STACK_I4) {
6436 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
6437 tree->inst_left = tree->inst_left->inst_left;
6438 tree->inst_right = tree->inst_right->inst_left;
6439 dec_foreach (tree, cfg);
6442 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
6443 MonoInst *iargs [2];
6445 iargs [0] = tree->inst_i0;
6446 iargs [1] = tree->inst_i1;
6448 mono_emulate_opcode (cfg, tree, iargs, info);
6450 dec_foreach (iargs [0], cfg);
6451 dec_foreach (iargs [1], cfg);
6454 dec_foreach (tree->inst_left, cfg);
6455 dec_foreach (tree->inst_right, cfg);
6459 g_assert_not_reached ();
6464 decompose_pass (MonoCompile *cfg) {
6467 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6470 cfg->prev_ins = NULL;
6471 for (tree = cfg->cbb->code; tree; tree = tree->next) {
6472 dec_foreach (tree, cfg);
6473 cfg->prev_ins = tree;
6479 nullify_basic_block (MonoBasicBlock *bb)
6486 bb->code = bb->last_ins = NULL;
6487 bb->cil_code = NULL;
6491 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6495 for (i = 0; i < bb->out_count; i++) {
6496 MonoBasicBlock *ob = bb->out_bb [i];
6499 if (bb->out_count > 1) {
6500 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
6504 bb->out_bb [i] = repl;
6511 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6515 for (i = 0; i < bb->in_count; i++) {
6516 MonoBasicBlock *ib = bb->in_bb [i];
6519 if (bb->in_count > 1) {
6520 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
6524 bb->in_bb [i] = repl;
6531 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6535 for (i = 0; i < bb->out_count; i++) {
6536 MonoBasicBlock *ob = bb->out_bb [i];
6537 for (j = 0; j < ob->in_count; j++) {
6538 if (ob->in_bb [j] == orig) {
6539 ob->in_bb [j] = repl;
6548 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
6550 bb->out_count = bbn->out_count;
6551 bb->out_bb = bbn->out_bb;
6553 replace_basic_block (bb, bbn, bb);
6557 bb->last_ins->next = bbn->code;
6558 bb->last_ins = bbn->last_ins;
6561 bb->code = bbn->code;
6562 bb->last_ins = bbn->last_ins;
6564 bb->next_bb = bbn->next_bb;
6565 nullify_basic_block (bbn);
6569 optimize_branches (MonoCompile *cfg) {
6570 int i, changed = FALSE;
6571 MonoBasicBlock *bb, *bbn;
6576 /* we skip the entry block (exit is handled specially instead ) */
6577 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
6579 /* dont touch code inside exception clauses */
6580 if (bb->region != -1)
6583 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
6584 if (cfg->verbose_level > 2)
6585 g_print ("nullify block triggered %d\n", bbn->block_num);
6587 bb->next_bb = bbn->next_bb;
6589 for (i = 0; i < bbn->out_count; i++)
6590 replace_in_block (bbn->out_bb [i], bbn, NULL);
6592 nullify_basic_block (bbn);
6596 if (bb->out_count == 1) {
6597 bbn = bb->out_bb [0];
6599 /* conditional branches where true and false targets are the same can be also replaced with CEE_BR */
6600 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
6601 bb->last_ins->opcode = CEE_BR;
6602 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
6604 if (cfg->verbose_level > 2)
6605 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
6608 if (bb->region == bbn->region && bb->next_bb == bbn) {
6609 /* the block are in sequence anyway ... */
6611 /* branches to the following block can be removed */
6612 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
6613 bb->last_ins->opcode = CEE_NOP;
6615 if (cfg->verbose_level > 2)
6616 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
6619 if (bbn->in_count == 1) {
6621 if (bbn != cfg->bb_exit) {
6622 if (cfg->verbose_level > 2)
6623 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
6624 merge_basic_blocks (bb, bbn);
6628 //mono_print_bb_code (bb);
6638 /* we skip the entry block (exit is handled specially instead ) */
6639 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
6641 /* dont touch code inside exception clauses */
6642 if (bb->region != -1)
6645 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
6646 if (cfg->verbose_level > 2) {
6647 g_print ("nullify block triggered %d\n", bbn->block_num);
6649 bb->next_bb = bbn->next_bb;
6651 for (i = 0; i < bbn->out_count; i++)
6652 replace_in_block (bbn->out_bb [i], bbn, NULL);
6654 nullify_basic_block (bbn);
6660 if (bb->out_count == 1) {
6661 bbn = bb->out_bb [0];
6663 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
6664 bbn = bb->last_ins->inst_target_bb;
6665 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
6666 bbn->code->inst_target_bb->region == bb->region) {
6668 if (cfg->verbose_level > 2)
6669 g_print ("in %s branch to branch triggered %d -> %d\n", cfg->method->name,
6670 bb->block_num, bbn->block_num);
6672 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
6673 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
6678 } else if (bb->out_count == 2) {
6679 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
6680 bbn = bb->last_ins->inst_true_bb;
6681 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
6682 bbn->code->inst_target_bb->region == bb->region) {
6683 if (cfg->verbose_level > 2)
6684 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
6685 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
6688 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
6690 replace_in_block (bbn, bb, NULL);
6692 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
6693 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
6695 link_bblock (cfg, bb, bbn->code->inst_target_bb);
6701 bbn = bb->last_ins->inst_false_bb;
6702 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
6703 bbn->code->inst_target_bb->region == bb->region) {
6704 if (cfg->verbose_level > 2)
6705 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
6706 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
6709 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
6711 replace_in_block (bbn, bb, NULL);
6713 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
6714 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
6716 link_bblock (cfg, bb, bbn->code->inst_target_bb);
6729 mono_compile_create_vars (MonoCompile *cfg)
6731 MonoMethodSignature *sig;
6732 MonoMethodHeader *header;
6735 header = ((MonoMethodNormal *)cfg->method)->header;
6737 sig = cfg->method->signature;
6739 if (!MONO_TYPE_IS_VOID (sig->ret)) {
6740 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
6741 cfg->ret->opcode = OP_RETARG;
6742 cfg->ret->inst_vtype = sig->ret;
6743 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
6745 if (cfg->verbose_level > 2)
6746 g_print ("creating vars\n");
6749 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
6751 for (i = 0; i < sig->param_count; ++i)
6752 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
6754 cfg->locals_start = cfg->num_varinfo;
6756 if (cfg->verbose_level > 2)
6757 g_print ("creating locals\n");
6758 for (i = 0; i < header->num_locals; ++i)
6759 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
6760 if (cfg->verbose_level > 2)
6761 g_print ("locals done\n");
6765 mono_print_code (MonoCompile *cfg)
6769 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6770 MonoInst *tree = bb->code;
6775 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
6777 for (; tree; tree = tree->next) {
6778 mono_print_tree (tree);
6783 bb->last_ins->next = NULL;
6787 extern const char * const mono_burg_rule_string [];
6790 emit_state (MonoCompile *cfg, MBState *state, int goal)
6793 int ern = mono_burg_rule (state, goal);
6794 const guint16 *nts = mono_burg_nts [ern];
6797 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
6801 // state->reg1 = state->reg2; /* chain rule */
6803 state->reg1 = mono_regstate_next_int (cfg->rs);
6804 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
6807 state->reg1 = mono_regstate_next_int (cfg->rs);
6808 state->reg2 = mono_regstate_next_int (cfg->rs);
6811 state->reg1 = mono_regstate_next_float (cfg->rs);
6818 mono_burg_kids (state, ern, kids);
6820 emit_state (cfg, kids [0], nts [0]);
6822 emit_state (cfg, kids [1], nts [1]);
6824 g_assert (!nts [3]);
6825 emit_state (cfg, kids [2], nts [2]);
6830 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
6831 if ((emit = mono_burg_func [ern]))
6832 emit (state, state->tree, cfg);
6835 #define DEBUG_SELECTION
6838 mini_select_instructions (MonoCompile *cfg)
6840 static int reverse_map [] = {
6841 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
6842 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
6844 static int reverse_fmap [] = {
6845 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
6846 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
6848 static int reverse_lmap [] = {
6849 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
6850 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
6855 cfg->state_pool = mono_mempool_new ();
6856 cfg->rs = mono_regstate_new ();
6858 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6859 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode) &&
6860 bb->next_bb != bb->last_ins->inst_false_bb) {
6862 if (bb->next_bb == bb->last_ins->inst_true_bb) {
6863 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
6864 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
6865 bb->last_ins->inst_false_bb = tmp;
6867 if (bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
6868 bb->last_ins->opcode = reverse_map [bb->last_ins->opcode - CEE_BEQ];
6869 } else if (bb->last_ins->opcode >= OP_FBEQ && bb->last_ins->opcode <= OP_FBLT_UN) {
6870 bb->last_ins->opcode = reverse_fmap [bb->last_ins->opcode - OP_FBEQ];
6871 } else if (bb->last_ins->opcode >= OP_LBEQ && bb->last_ins->opcode <= OP_LBLT_UN) {
6872 bb->last_ins->opcode = reverse_lmap [bb->last_ins->opcode - OP_LBEQ];
6875 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
6876 inst->opcode = CEE_BR;
6877 inst->inst_target_bb = bb->last_ins->inst_false_bb;
6878 mono_bblock_add_inst (bb, inst);
6883 #ifdef DEBUG_SELECTION
6884 if (cfg->verbose_level >= 4) {
6885 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6886 MonoInst *tree = bb->code;
6887 g_print ("DUMP BLOCK %d:\n", bb->block_num);
6890 for (; tree; tree = tree->next) {
6891 mono_print_tree (tree);
6898 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6899 MonoInst *tree = bb->code, *next;
6905 bb->last_ins = NULL;
6908 mono_regstate_reset (cfg->rs);
6910 #ifdef DEBUG_SELECTION
6911 if (cfg->verbose_level >= 3)
6912 g_print ("LABEL BLOCK %d:\n", bb->block_num);
6914 for (; tree; tree = next) {
6916 #ifdef DEBUG_SELECTION
6917 if (cfg->verbose_level >= 3) {
6918 mono_print_tree (tree);
6923 if (!(mbstate = mono_burg_label (tree, cfg))) {
6924 g_warning ("unable to label tree %p", tree);
6925 mono_print_tree (tree);
6927 g_assert_not_reached ();
6929 emit_state (cfg, mbstate, MB_NTERM_stmt);
6931 bb->max_ireg = cfg->rs->next_vireg;
6932 bb->max_freg = cfg->rs->next_vfreg;
6935 bb->last_ins->next = NULL;
6937 mono_mempool_empty (cfg->state_pool);
6939 mono_mempool_destroy (cfg->state_pool);
6943 mono_codegen (MonoCompile *cfg)
6945 MonoJumpInfo *patch_info;
6947 int i, max_epilog_size;
6950 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6951 cfg->spill_count = 0;
6952 /* we reuse dfn here */
6953 /* bb->dfn = bb_count++; */
6954 mono_arch_local_regalloc (cfg, bb);
6957 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
6958 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
6960 code = mono_arch_emit_prolog (cfg);
6962 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6963 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
6965 cfg->code_len = code - cfg->native_code;
6966 cfg->prolog_end = cfg->code_len;
6968 mono_debug_open_method (cfg);
6970 /* emit code all basic blocks */
6971 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6972 bb->native_offset = cfg->code_len;
6973 mono_arch_output_basic_block (cfg, bb);
6975 cfg->bb_exit->native_offset = cfg->code_len;
6977 code = cfg->native_code + cfg->code_len;
6979 max_epilog_size = mono_arch_max_epilog_size (cfg);
6981 /* we always allocate code in cfg->domain->code_mp to increase locality */
6982 cfg->code_size = cfg->code_len + max_epilog_size;
6983 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
6984 code = mono_mempool_alloc (cfg->domain->code_mp, cfg->code_size);
6985 memcpy (code, cfg->native_code, cfg->code_len);
6986 g_free (cfg->native_code);
6987 cfg->native_code = code;
6988 code = cfg->native_code + cfg->code_len;
6990 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
6992 cfg->epilog_begin = cfg->code_len;
6994 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6995 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
6997 cfg->code_len = code - cfg->native_code;
6999 mono_arch_emit_epilog (cfg);
7001 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7002 switch (patch_info->type) {
7003 case MONO_PATCH_INFO_ABS: {
7004 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
7006 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
7007 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
7008 patch_info->data.name = info->name;
7011 MonoVTable *vtable = mono_find_class_init_trampoline_by_addr (patch_info->data.target);
7013 patch_info->type = MONO_PATCH_INFO_CLASS_INIT;
7014 patch_info->data.klass = vtable->klass;
7019 case MONO_PATCH_INFO_SWITCH: {
7020 gpointer *table = mono_mempool_alloc (cfg->domain->code_mp, sizeof (gpointer) * patch_info->table_size);
7021 patch_info->ip.i = patch_info->ip.label->inst_c0;
7022 for (i = 0; i < patch_info->table_size; i++) {
7023 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
7025 patch_info->data.target = table;
7034 if (cfg->verbose_level > 0)
7035 g_print ("Method %s emitted at %p to %p [%s]\n",
7036 mono_method_full_name (cfg->method, TRUE),
7037 cfg->native_code, cfg->native_code + cfg->code_len, cfg->domain->friendly_name);
7039 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info, cfg->run_cctors);
7041 mono_arch_flush_icache (cfg->native_code, cfg->code_len);
7043 mono_debug_close_method (cfg);
7047 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
7052 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
7053 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
7055 if (cp->opcode == OP_ICONST) {
7056 if (cfg->opt & MONO_OPT_CONSPROP) {
7057 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
7061 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
7062 if (cfg->opt & MONO_OPT_COPYPROP) {
7063 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
7069 arity = mono_burg_arity [tree->opcode];
7072 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
7073 if (cfg->opt & MONO_OPT_CFOLD)
7074 mono_constant_fold_inst (tree, NULL);
7075 /* The opcode may have changed */
7076 if (mono_burg_arity [tree->opcode] > 1) {
7077 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
7078 if (cfg->opt & MONO_OPT_CFOLD)
7079 mono_constant_fold_inst (tree, NULL);
7081 mono_constant_fold_inst (tree, NULL);
7087 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
7091 switch (tree->opcode) {
7101 if (tree->ssa_op == MONO_SSA_NOP) {
7102 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7119 case OP_VOIDCALL_REG:
7120 case OP_VOIDCALLVIRT:
7122 MonoCallInst *call = (MonoCallInst *)tree;
7123 MonoMethodSignature *sig = call->signature;
7124 int i, byref = FALSE;
7126 for (i = 0; i < sig->param_count; i++) {
7127 if (sig->params [i]->byref) {
7134 memset (acp, 0, sizeof (MonoInst *) * acp_size);
7142 arity = mono_burg_arity [tree->opcode];
7148 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7151 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
7152 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
7155 g_assert_not_reached ();
7160 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
7162 MonoInst *tree = bb->code;
7168 for (; tree; tree = tree->next) {
7170 mono_cprop_copy_values (cfg, tree, acp);
7172 mono_cprop_invalidate_values (tree, acp, acp_size);
7174 if (tree->ssa_op == MONO_SSA_STORE &&
7175 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
7176 MonoInst *i1 = tree->inst_i1;
7178 acp [tree->inst_i0->inst_c0] = NULL;
7180 for (i = 0; i < acp_size; i++) {
7181 if (acp [i] && acp [i]->opcode != OP_ICONST &&
7182 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
7187 if (i1->opcode == OP_ICONST) {
7188 acp [tree->inst_i0->inst_c0] = i1;
7189 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
7191 if (i1->ssa_op == MONO_SSA_LOAD &&
7192 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
7193 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
7194 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
7195 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
7200 if (tree->opcode == CEE_BEQ) {
7201 g_assert (tree->inst_i0->opcode == OP_COMPARE);
7202 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
7203 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
7205 tree->opcode = CEE_BR;
7206 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
7207 tree->inst_target_bb = tree->inst_true_bb;
7209 tree->inst_target_bb = tree->inst_false_bb;
7218 mono_local_cprop (MonoCompile *cfg)
7223 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
7225 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7226 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
7227 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
7232 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, int parts)
7234 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
7235 guint8 *ip = (guint8 *)header->code;
7238 int dfn = 0, i, code_size_ratio;
7240 mono_jit_stats.methods_compiled++;
7241 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
7242 mono_profiler_method_jit (method);
7244 cfg = g_new0 (MonoCompile, 1);
7245 cfg->method = method;
7246 cfg->mempool = mono_mempool_new ();
7248 cfg->prof_options = mono_profiler_get_events ();
7249 cfg->run_cctors = run_cctors;
7250 cfg->bb_hash = g_hash_table_new (NULL, NULL);
7251 cfg->domain = domain;
7252 cfg->verbose_level = mini_verbose;
7253 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
7254 ((MonoMethodNormal *)method)->header->max_stack);
7256 if (cfg->verbose_level > 2)
7257 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
7260 * create MonoInst* which represents arguments and local variables
7262 mono_compile_create_vars (cfg);
7264 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
7265 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7266 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
7267 mono_destroy_compile (cfg);
7271 mono_jit_stats.basic_blocks += cfg->num_bblocks;
7272 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
7274 if (cfg->num_varinfo > 2000) {
7276 * we disable some optimizations if there are too many variables
7277 * because JIT time may become too expensive. The actual number needs
7278 * to be tweaked and eventually the non-linear algorithms should be fixed.
7280 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
7281 cfg->disable_ssa = TRUE;
7283 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
7285 /* Depth-first ordering on basic blocks */
7286 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
7288 if (cfg->opt & MONO_OPT_BRANCH)
7289 optimize_branches (cfg);
7291 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
7292 if (cfg->num_bblocks != dfn + 1) {
7295 cfg->num_bblocks = dfn + 1;
7297 if (!header->clauses) {
7298 /* remove unreachable code, because the code in them may be
7299 * inconsistent (access to dead variables for example) */
7300 for (bb = cfg->bb_entry; bb;) {
7301 MonoBasicBlock *bbn = bb->next_bb;
7303 if (bbn && bbn->region == -1 && !bbn->dfn) {
7304 if (cfg->verbose_level > 1)
7305 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
7306 bb->next_bb = bbn->next_bb;
7307 nullify_basic_block (bbn);
7315 if (cfg->opt & MONO_OPT_LOOP) {
7316 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
7317 mono_compute_natural_loops (cfg);
7320 /* after method_to_ir */
7324 //#define DEBUGSSA "logic_run"
7325 #define DEBUGSSA_CLASS "Tests"
7328 if (!header->num_clauses && !cfg->disable_ssa) {
7329 mono_local_cprop (cfg);
7330 mono_ssa_compute (cfg);
7334 /* fixme: add all optimizations which requires SSA */
7335 if (cfg->opt & (MONO_OPT_DEADCE)) {
7336 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
7337 mono_local_cprop (cfg);
7338 mono_ssa_compute (cfg);
7340 if (cfg->verbose_level >= 2) {
7347 /* after SSA translation */
7351 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
7352 if (cfg->comp_done & MONO_COMP_SSA) {
7353 mono_ssa_cprop (cfg);
7355 mono_local_cprop (cfg);
7359 if (cfg->comp_done & MONO_COMP_SSA) {
7360 mono_ssa_deadce (cfg);
7362 //mono_ssa_strength_reduction (cfg);
7364 mono_ssa_remove (cfg);
7366 if (cfg->opt & MONO_OPT_BRANCH)
7367 optimize_branches (cfg);
7370 /* after SSA removal */
7374 decompose_pass (cfg);
7376 /* FIXME: disabled with exception clauses: bug #42136 */
7377 if ((!header->num_clauses) && (cfg->opt & MONO_OPT_LINEARS)) {
7380 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
7381 cfg->comp_done &= ~MONO_COMP_LIVENESS;
7382 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
7383 mono_analyze_liveness (cfg);
7385 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
7386 regs = mono_arch_get_global_int_regs (cfg);
7387 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
7391 //mono_print_code (cfg);
7395 /* variables are allocated after decompose, since decompose could create temps */
7396 mono_arch_allocate_vars (cfg);
7398 if (cfg->opt & MONO_OPT_CFOLD)
7399 mono_constant_fold (cfg);
7401 mini_select_instructions (cfg);
7404 if (cfg->verbose_level >= 2) {
7405 char *id = mono_method_full_name (cfg->method, FALSE);
7406 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
7410 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
7412 jinfo = g_new0 (MonoJitInfo, 1);
7413 jinfo->method = method;
7414 jinfo->code_start = cfg->native_code;
7415 jinfo->code_size = cfg->code_len;
7416 jinfo->used_regs = cfg->used_int_regs;
7417 jinfo->domain_neutral = (cfg->opt & MONO_OPT_SHARED) != 0;
7419 if (header->num_clauses) {
7422 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
7423 jinfo->num_clauses = header->num_clauses;
7424 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
7425 sizeof (MonoJitExceptionInfo) * header->num_clauses);
7427 for (i = 0; i < header->num_clauses; i++) {
7428 MonoExceptionClause *ec = &header->clauses [i];
7429 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
7430 MonoBasicBlock *tblock;
7432 ei->flags = ec->flags;
7434 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
7435 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->token_or_filter);
7437 ei->data.filter = cfg->native_code + tblock->native_offset;
7439 ei->data.token = ec->token_or_filter;
7442 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
7444 ei->try_start = cfg->native_code + tblock->native_offset;
7445 g_assert (tblock->native_offset);
7446 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
7448 ei->try_end = cfg->native_code + tblock->native_offset;
7449 g_assert (tblock->native_offset);
7450 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
7452 ei->handler_start = cfg->native_code + tblock->native_offset;
7456 cfg->jit_info = jinfo;
7458 mono_jit_info_table_add (cfg->domain, jinfo);
7460 /* collect statistics */
7461 mono_jit_stats.allocated_code_size += cfg->code_len;
7462 code_size_ratio = cfg->code_len;
7463 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
7464 mono_jit_stats.biggest_method_size = code_size_ratio;
7465 mono_jit_stats.biggest_method = method;
7467 code_size_ratio = (code_size_ratio * 100) / ((MonoMethodNormal *)method)->header->code_size;
7468 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
7469 mono_jit_stats.max_code_size_ratio = code_size_ratio;
7470 mono_jit_stats.max_ratio_method = method;
7472 mono_jit_stats.native_code_size += cfg->code_len;
7474 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7475 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
7481 mono_jit_compile_method_inner (MonoMethod *method)
7483 /* FIXME: later copy the code from mono */
7484 MonoDomain *target_domain, *domain = mono_domain_get ();
7486 GHashTable *jit_code_hash;
7492 if (opt & MONO_OPT_SHARED)
7493 target_domain = mono_root_domain;
7495 target_domain = domain;
7497 jit_code_hash = target_domain->jit_code_hash;
7499 #ifdef MONO_USE_AOT_COMPILER
7500 if (!mono_compile_aot && (opt & MONO_OPT_AOT)) {
7503 mono_domain_lock (domain);
7505 mono_class_init (method->klass);
7506 if ((info = mono_aot_get_method (domain, method))) {
7508 g_hash_table_insert (domain->jit_code_hash, method, info);
7510 mono_domain_unlock (domain);
7512 /* make sure runtime_init is called */
7513 mono_runtime_class_init (mono_class_vtable (domain, method->klass));
7515 return info->code_start;
7518 mono_domain_unlock (domain);
7522 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
7523 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
7526 if (!method->addr) {
7527 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
7528 method->addr = mono_lookup_internal_call (method);
7530 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
7531 mono_lookup_pinvoke_call (method);
7533 #ifdef MONO_USE_EXC_TABLES
7534 if (mono_method_blittable (method)) {
7535 return method->addr;
7538 nm = mono_marshal_get_native_wrapper (method);
7539 return mono_compile_method (nm);
7541 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
7542 //mono_debug_add_wrapper (method, nm);
7543 #ifdef MONO_USE_EXC_TABLES
7546 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
7547 const char *name = method->name;
7550 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
7551 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
7552 /* FIXME: uhm, we need a wrapper to handle exceptions? */
7553 return (gpointer)mono_delegate_ctor;
7554 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
7555 nm = mono_marshal_get_delegate_invoke (method);
7556 return mono_jit_compile_method (nm);
7557 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
7558 nm = mono_marshal_get_delegate_begin_invoke (method);
7559 return mono_jit_compile_method (nm);
7560 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
7561 nm = mono_marshal_get_delegate_end_invoke (method);
7562 return mono_jit_compile_method (nm);
7568 cfg = mini_method_compile (method, opt, target_domain, TRUE, 0);
7569 code = cfg->native_code;
7571 g_hash_table_insert (jit_code_hash, method, cfg->jit_info);
7573 mono_destroy_compile (cfg);
7575 if (target_domain->jump_target_hash) {
7576 MonoJumpInfo patch_info;
7578 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
7580 patch_info.next = NULL;
7581 patch_info.ip.i = 0;
7582 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
7583 patch_info.data.method = method;
7584 g_hash_table_remove (target_domain->jump_target_hash, method);
7586 for (tmp = list; tmp; tmp = tmp->next)
7587 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info, TRUE);
7588 g_slist_free (list);
7590 /* make sure runtime_init is called */
7591 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
7597 mono_jit_compile_method (MonoMethod *method)
7599 /* FIXME: later copy the code from mono */
7600 MonoDomain *target_domain, *domain = mono_domain_get ();
7604 if (default_opt & MONO_OPT_SHARED)
7605 target_domain = mono_root_domain;
7607 target_domain = domain;
7609 mono_domain_lock (target_domain);
7611 if ((info = g_hash_table_lookup (target_domain->jit_code_hash, method))) {
7612 /* We can't use a domain specific method in another domain */
7613 if (! ((domain != target_domain) && !info->domain_neutral)) {
7614 mono_domain_unlock (target_domain);
7615 mono_jit_stats.methods_lookups++;
7616 return info->code_start;
7620 code = mono_jit_compile_method_inner (method);
7622 mono_domain_unlock (target_domain);
7628 * mono_jit_runtime_invoke:
7629 * @method: the method to invoke
7630 * @obj: this pointer
7631 * @params: array of parameter values.
7632 * @exc: used to catch exceptions objects
7635 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
7638 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc);
7640 if (obj == NULL && !(method->flags & METHOD_ATTRIBUTE_STATIC) && !method->string_ctor && (method->wrapper_type == 0)) {
7641 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
7645 invoke = mono_marshal_get_runtime_invoke (method);
7646 runtime_invoke = mono_jit_compile_method (invoke);
7647 return runtime_invoke (obj, params, exc);
7650 #ifdef PLATFORM_WIN32
7651 #define GET_CONTEXT \
7652 struct sigcontext *ctx = (struct sigcontext*)_dummy;
7654 #define GET_CONTEXT \
7655 void **_p = (void **)&_dummy; \
7656 struct sigcontext *ctx = (struct sigcontext *)++_p;
7660 sigfpe_signal_handler (int _dummy)
7665 exc = mono_get_exception_divide_by_zero ();
7667 mono_arch_handle_exception (ctx, exc, FALSE);
7671 sigill_signal_handler (int _dummy)
7675 exc = mono_get_exception_execution_engine ("SIGILL");
7677 mono_arch_handle_exception (ctx, exc, FALSE);
7681 sigsegv_signal_handler (int _dummy)
7686 exc = mono_get_exception_null_reference ();
7688 mono_arch_handle_exception (ctx, exc, FALSE);
7692 sigusr1_signal_handler (int _dummy)
7697 thread = mono_thread_current ();
7699 thread->abort_exc = mono_get_exception_thread_abort ();
7701 mono_arch_handle_exception (ctx, thread->abort_exc, FALSE);
7705 sigquit_signal_handler (int _dummy)
7710 exc = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
7712 mono_arch_handle_exception (ctx, exc, FALSE);
7716 sigint_signal_handler (int _dummy)
7721 exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
7723 mono_arch_handle_exception (ctx, exc, FALSE);
7727 mono_runtime_install_handlers (void)
7729 gboolean skip_sigabort = FALSE;
7730 #ifndef PLATFORM_WIN32
7731 struct sigaction sa;
7734 #ifdef PLATFORM_WIN32
7736 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
7737 win32_seh_set_handler(SIGILL, sigill_signal_handler);
7738 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
7739 if (getenv ("MONO_DEBUG"))
7740 win32_seh_set_handler(SIGINT, sigint_signal_handler);
7741 #else /* !PLATFORM_WIN32 */
7743 /* libpthreads has its own implementation of sigaction(),
7744 * but it seems to work well with our current exception
7745 * handlers. If not we must call syscall directly instead
7748 if (getenv ("MONO_DEBUG")) {
7750 sa.sa_handler = sigint_signal_handler;
7751 sigemptyset (&sa.sa_mask);
7753 //g_assert (syscall (SYS_sigaction, SIGINT, &sa, NULL) != -1);
7754 g_assert (sigaction (SIGINT, &sa, NULL) != -1);
7758 sa.sa_handler = sigfpe_signal_handler;
7759 sigemptyset (&sa.sa_mask);
7761 //g_assert (syscall (SYS_sigaction, SIGFPE, &sa, NULL) != -1);
7762 g_assert (sigaction (SIGFPE, &sa, NULL) != -1);
7765 sa.sa_handler = sigquit_signal_handler;
7766 sigemptyset (&sa.sa_mask);
7768 g_assert (sigaction (SIGQUIT, &sa, NULL) != -1);
7771 sa.sa_handler = sigill_signal_handler;
7772 sigemptyset (&sa.sa_mask);
7774 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
7775 g_assert (sigaction (SIGILL, &sa, NULL) != -1);
7777 /* catch thread abort signal */
7778 sa.sa_handler = sigusr1_signal_handler;
7779 sigemptyset (&sa.sa_mask);
7781 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
7783 #ifdef HAVE_VALGRIND_MEMCHECK_H
7784 if (RUNNING_ON_VALGRIND)
7785 /* valgrind 20030725 and earlier aborts on this call so we skip it */
7786 skip_sigabort = TRUE;
7790 g_assert (sigaction (mono_thread_get_abort_signal (), &sa, NULL) != -1);
7794 sa.sa_handler = sigsegv_signal_handler;
7795 sigemptyset (&sa.sa_mask);
7797 //g_assert (syscall (SYS_sigaction, SIGSEGV, &sa, NULL) != -1);
7798 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
7800 #endif /* PLATFORM_WIN32 */
7803 /* mono_jit_create_remoting_trampoline:
7804 * @method: pointer to the method info
7806 * Creates a trampoline which calls the remoting functions. This
7807 * is used in the vtable of transparent proxies.
7809 * Returns: a pointer to the newly created code
7812 mono_jit_create_remoting_trampoline (MonoMethod *method)
7815 guint8 *addr = NULL;
7817 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
7818 (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
7819 nm = mono_marshal_get_remoting_invoke (method);
7820 addr = mono_compile_method (nm);
7822 addr = mono_compile_method (method);
7828 mini_init (const char *filename)
7832 mono_arch_cpu_init ();
7834 if (!g_thread_supported ())
7835 g_thread_init (NULL);
7837 mono_jit_tls_id = TlsAlloc ();
7838 setup_jit_tls_data ((gpointer)-1, mono_thread_abort);
7840 InitializeCriticalSection (&class_init_hash_mutex);
7844 if (default_opt & MONO_OPT_AOT)
7847 mono_runtime_install_handlers ();
7848 mono_threads_install_cleanup (mini_thread_cleanup);
7850 #define JIT_TRAMPOLINES_WORK
7851 #ifdef JIT_TRAMPOLINES_WORK
7852 mono_install_compile_method (mono_jit_compile_method);
7853 mono_install_trampoline (mono_arch_create_jit_trampoline);
7854 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
7856 #define JIT_INVOKE_WORKS
7857 #ifdef JIT_INVOKE_WORKS
7858 mono_install_runtime_invoke (mono_jit_runtime_invoke);
7859 mono_install_handler (mono_arch_get_throw_exception ());
7861 mono_install_stack_walk (mono_jit_walk_stack);
7862 mono_install_get_config_dir ();
7864 domain = mono_init (filename);
7867 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
7868 ves_icall_get_frame_info);
7869 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
7870 ves_icall_get_trace);
7871 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
7872 mono_runtime_install_handlers);
7875 create_helper_signature ();
7877 #define JIT_CALLS_WORK
7878 #ifdef JIT_CALLS_WORK
7879 /* Needs to be called here since register_jit_icall depends on it */
7880 mono_marshal_init ();
7882 mono_arch_register_lowlevel_calls ();
7883 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
7884 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
7886 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
7887 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
7889 /* fixme: we cant hanlde vararg methods this way, because the signature is not constant */
7890 //mono_register_jit_icall (ves_array_element_address, "ves_array_element_address", NULL);
7891 //mono_register_jit_icall (mono_array_new_va, "mono_array_new_va", NULL);
7893 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
7894 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
7895 helper_sig_void_ptr, TRUE);
7896 mono_register_jit_icall (mono_thread_get_pending_exception, "mono_thread_get_pending_exception", helper_sig_obj_void, FALSE);
7899 * NOTE, NOTE, NOTE, NOTE:
7900 * when adding emulation for some opcodes, remember to also add a dummy
7901 * rule to the burg files, because we need the arity information to be correct.
7903 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult, TRUE);
7904 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un, FALSE);
7905 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf, FALSE);
7906 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv, FALSE);
7907 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un, FALSE);
7908 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem, FALSE);
7909 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un, FALSE);
7911 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl, TRUE);
7912 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr, TRUE);
7913 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un, TRUE);
7915 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8, FALSE);
7916 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4, FALSE);
7917 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8, TRUE);
7918 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8, TRUE);
7920 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
7921 mono_register_opcode_emulation (OP_FCONV_TO_I8, "__emul_fconv_to_i8", helper_sig_long_double, mono_fconv_i8, FALSE);
7923 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
7924 mono_register_opcode_emulation (OP_LCONV_TO_R8, "__emul_lconv_to_r8", helper_sig_double_long, mono_lconv_to_r8, FALSE);
7926 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
7927 mono_register_opcode_emulation (OP_LCONV_TO_R4, "__emul_lconv_to_r4", helper_sig_float_long, mono_lconv_to_r4, FALSE);
7929 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
7930 mono_register_opcode_emulation (OP_LCONV_TO_R_UN, "__emul_lconv_to_r8_un", helper_sig_double_long, mono_lconv_to_r8_un, FALSE);
7932 #ifdef MONO_ARCH_EMULATE_FREM
7933 mono_register_opcode_emulation (OP_FREM, "__emul_frem", helper_sig_double_double_double, fmod, FALSE);
7936 #if SIZEOF_VOID_P == 4
7937 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4, TRUE);
7939 #warning "fixme: add opcode emulation"
7942 /* other jit icalls */
7943 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
7944 helper_sig_ptr_ptr_ptr, FALSE);
7945 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
7946 mono_register_jit_icall (mono_get_special_static_data, "mono_get_special_static_data", helper_sig_ptr_int, FALSE);
7947 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
7948 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
7949 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
7950 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
7951 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
7952 mono_register_jit_icall (helper_stelem_ref_check, "helper_stelem_ref_check", helper_sig_stelem_ref_check, FALSE);
7953 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
7954 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
7955 mono_register_jit_icall (mono_object_new_fast, "mono_object_new_fast", helper_sig_object_new_specific, FALSE);
7956 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
7957 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
7958 mono_register_jit_icall (mono_string_to_utf16, "mono_string_to_utf16", helper_sig_ptr_obj, FALSE);
7959 mono_register_jit_icall (mono_string_from_utf16, "mono_string_from_utf16", helper_sig_obj_ptr, FALSE);
7960 mono_register_jit_icall (mono_string_new_wrapper, "mono_string_new_wrapper", helper_sig_obj_ptr, FALSE);
7961 mono_register_jit_icall (mono_string_to_utf8, "mono_string_to_utf8", helper_sig_ptr_obj, FALSE);
7962 mono_register_jit_icall (mono_string_to_bstr, "mono_string_to_bstr", helper_sig_ptr_obj, FALSE);
7963 mono_register_jit_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", helper_sig_ptr_obj, FALSE);
7964 mono_register_jit_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", helper_sig_ptr_obj, FALSE);
7965 mono_register_jit_icall (mono_array_to_savearray, "mono_array_to_savearray", helper_sig_ptr_obj, FALSE);
7966 mono_register_jit_icall (mono_array_to_lparray, "mono_array_to_lparray", helper_sig_ptr_obj, FALSE);
7967 mono_register_jit_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", helper_sig_ptr_obj, FALSE);
7968 mono_register_jit_icall (mono_marshal_string_array, "mono_marshal_string_array", helper_sig_ptr_obj, FALSE);
7969 mono_register_jit_icall (mono_string_utf8_to_builder, "mono_string_utf8_to_builder", helper_sig_void_ptr_ptr, FALSE);
7970 mono_register_jit_icall (mono_marshal_free_array, "mono_marshal_free_array", helper_sig_void_ptr_ptr, FALSE);
7971 mono_register_jit_icall (mono_string_to_byvalstr, "mono_string_to_byvalstr", helper_sig_void_ptr_ptr_ptr, FALSE);
7972 mono_register_jit_icall (mono_string_to_byvalwstr, "mono_string_to_byvalwstr", helper_sig_void_ptr_ptr_ptr, FALSE);
7973 mono_register_jit_icall (g_free, "g_free", helper_sig_void_ptr, FALSE);
7974 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
7975 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
7976 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
7977 mono_register_jit_icall (mono_object_castclass_mbyref, "mono_object_castclass", helper_sig_obj_obj_cls, FALSE);
7980 #define JIT_RUNTIME_WORKS
7981 #ifdef JIT_RUNTIME_WORKS
7982 mono_runtime_install_cleanup ((MonoDomainFunc)mini_cleanup);
7983 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
7986 //mono_thread_attach (domain);
7990 MonoJitStats mono_jit_stats = {0};
7993 print_jit_stats (void)
7995 if (mono_jit_stats.enabled) {
7996 g_print ("Mono Jit statistics\n");
7997 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
7998 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
7999 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
8000 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
8001 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
8002 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
8003 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
8004 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
8005 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
8006 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
8007 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
8008 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
8009 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
8010 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
8011 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
8012 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
8013 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
8014 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
8016 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
8017 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
8018 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
8019 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
8020 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
8025 mini_cleanup (MonoDomain *domain)
8028 * mono_runtime_cleanup() and mono_domain_finalize () need to
8029 * be called early since they need the execution engine still
8030 * fully working (mono_domain_finalize may invoke managed finalizers
8031 * and mono_runtime_cleanup will wait for other threads to finish).
8033 mono_domain_finalize (domain, -1);
8035 mono_runtime_cleanup (domain);
8037 mono_profiler_shutdown ();
8039 mono_debug_cleanup ();
8041 #ifdef PLATFORM_WIN32
8042 win32_seh_cleanup();
8045 mono_domain_free (domain, TRUE);
8051 mono_set_defaults (int verbose_level, guint32 opts)
8053 mini_verbose = verbose_level;
8058 mono_precompile_assembly (MonoAssembly *ass, void *user_data)
8060 MonoImage *image = ass->image;
8064 if (mini_verbose > 0)
8065 printf ("PRECOMPILE: %s.\n", ass->image->name);
8067 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
8068 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
8069 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
8071 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
8075 if (mini_verbose > 1) {
8076 char * desc = mono_method_full_name (method, TRUE);
8077 g_print ("Compiling %d %s\n", count, desc);
8080 mono_compile_method (method);
8084 void mono_precompile_assemblies ()
8086 mono_assembly_foreach ((GFunc)mono_precompile_assembly, NULL);