[mini] Propagate MonoError in mini_get_signature
[mono-project.git] / mono / mini / method-to-ir.c
blob231270a865c5bfb07b0e0699c658474a6f7e462a
1 /*
2 * method-to-ir.c: Convert CIL to the JIT internal representation
4 * Author:
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2002 Ximian, Inc.
9 * Copyright 2003-2010 Novell, Inc (http://www.novell.com)
10 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
11 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
14 #include <config.h>
16 #ifndef DISABLE_JIT
18 #include <signal.h>
20 #ifdef HAVE_UNISTD_H
21 #include <unistd.h>
22 #endif
24 #include <math.h>
25 #include <string.h>
26 #include <ctype.h>
28 #ifdef HAVE_SYS_TIME_H
29 #include <sys/time.h>
30 #endif
32 #ifdef HAVE_ALLOCA_H
33 #include <alloca.h>
34 #endif
36 #include <mono/utils/memcheck.h>
37 #include "mini.h"
38 #include <mono/metadata/abi-details.h>
39 #include <mono/metadata/assembly.h>
40 #include <mono/metadata/attrdefs.h>
41 #include <mono/metadata/loader.h>
42 #include <mono/metadata/tabledefs.h>
43 #include <mono/metadata/class.h>
44 #include <mono/metadata/object.h>
45 #include <mono/metadata/exception.h>
46 #include <mono/metadata/opcodes.h>
47 #include <mono/metadata/mono-endian.h>
48 #include <mono/metadata/tokentype.h>
49 #include <mono/metadata/tabledefs.h>
50 #include <mono/metadata/marshal.h>
51 #include <mono/metadata/debug-helpers.h>
52 #include <mono/metadata/mono-debug.h>
53 #include <mono/metadata/mono-debug-debugger.h>
54 #include <mono/metadata/gc-internals.h>
55 #include <mono/metadata/security-manager.h>
56 #include <mono/metadata/threads-types.h>
57 #include <mono/metadata/security-core-clr.h>
58 #include <mono/metadata/profiler-private.h>
59 #include <mono/metadata/profiler.h>
60 #include <mono/metadata/monitor.h>
61 #include <mono/metadata/debug-mono-symfile.h>
62 #include <mono/utils/mono-compiler.h>
63 #include <mono/utils/mono-memory-model.h>
64 #include <mono/utils/mono-error-internals.h>
65 #include <mono/metadata/mono-basic-block.h>
66 #include <mono/metadata/reflection-internals.h>
68 #include "trace.h"
70 #include "ir-emit.h"
72 #include "jit-icalls.h"
73 #include "jit.h"
74 #include "debugger-agent.h"
75 #include "seq-points.h"
76 #include "aot-compiler.h"
77 #include "mini-llvm.h"
79 #define BRANCH_COST 10
80 #define INLINE_LENGTH_LIMIT 20
82 /* These have 'cfg' as an implicit argument */
83 #define INLINE_FAILURE(msg) do { \
84 if ((cfg->method != cfg->current_method) && (cfg->current_method->wrapper_type == MONO_WRAPPER_NONE)) { \
85 inline_failure (cfg, msg); \
86 goto exception_exit; \
87 } \
88 } while (0)
89 #define CHECK_CFG_EXCEPTION do {\
90 if (cfg->exception_type != MONO_EXCEPTION_NONE) \
91 goto exception_exit; \
92 } while (0)
93 #define METHOD_ACCESS_FAILURE(method, cmethod) do { \
94 method_access_failure ((cfg), (method), (cmethod)); \
95 goto exception_exit; \
96 } while (0)
97 #define FIELD_ACCESS_FAILURE(method, field) do { \
98 field_access_failure ((cfg), (method), (field)); \
99 goto exception_exit; \
100 } while (0)
101 #define GENERIC_SHARING_FAILURE(opcode) do { \
102 if (cfg->gshared) { \
103 gshared_failure (cfg, opcode, __FILE__, __LINE__); \
104 goto exception_exit; \
106 } while (0)
107 #define GSHAREDVT_FAILURE(opcode) do { \
108 if (cfg->gsharedvt) { \
109 gsharedvt_failure (cfg, opcode, __FILE__, __LINE__); \
110 goto exception_exit; \
112 } while (0)
113 #define OUT_OF_MEMORY_FAILURE do { \
114 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR); \
115 mono_error_set_out_of_memory (&cfg->error, ""); \
116 goto exception_exit; \
117 } while (0)
118 #define DISABLE_AOT(cfg) do { \
119 if ((cfg)->verbose_level >= 2) \
120 printf ("AOT disabled: %s:%d\n", __FILE__, __LINE__); \
121 (cfg)->disable_aot = TRUE; \
122 } while (0)
123 #define LOAD_ERROR do { \
124 break_on_unverified (); \
125 mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD); \
126 goto exception_exit; \
127 } while (0)
129 #define TYPE_LOAD_ERROR(klass) do { \
130 cfg->exception_ptr = klass; \
131 LOAD_ERROR; \
132 } while (0)
134 #define CHECK_CFG_ERROR do {\
135 if (!mono_error_ok (&cfg->error)) { \
136 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR); \
137 goto mono_error_exit; \
139 } while (0)
141 /* Determine whenever 'ins' represents a load of the 'this' argument */
142 #define MONO_CHECK_THIS(ins) (mono_method_signature (cfg->method)->hasthis && ((ins)->opcode == OP_MOVE) && ((ins)->sreg1 == cfg->args [0]->dreg))
144 static int ldind_to_load_membase (int opcode);
145 static int stind_to_store_membase (int opcode);
147 int mono_op_to_op_imm (int opcode);
148 int mono_op_to_op_imm_noemul (int opcode);
150 MONO_API MonoInst* mono_emit_native_call (MonoCompile *cfg, gconstpointer func, MonoMethodSignature *sig, MonoInst **args);
152 static int inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp,
153 guchar *ip, guint real_offset, gboolean inline_always);
154 static MonoInst*
155 emit_llvmonly_virtual_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, int context_used, MonoInst **sp);
157 /* helper methods signatures */
158 static MonoMethodSignature *helper_sig_domain_get;
159 static MonoMethodSignature *helper_sig_rgctx_lazy_fetch_trampoline;
160 static MonoMethodSignature *helper_sig_llvmonly_imt_thunk;
163 /* type loading helpers */
164 static GENERATE_GET_CLASS_WITH_CACHE (runtime_helpers, System.Runtime.CompilerServices, RuntimeHelpers)
165 static GENERATE_TRY_GET_CLASS_WITH_CACHE (debuggable_attribute, System.Diagnostics, DebuggableAttribute)
168 * Instruction metadata
170 #ifdef MINI_OP
171 #undef MINI_OP
172 #endif
173 #ifdef MINI_OP3
174 #undef MINI_OP3
175 #endif
176 #define MINI_OP(a,b,dest,src1,src2) dest, src1, src2, ' ',
177 #define MINI_OP3(a,b,dest,src1,src2,src3) dest, src1, src2, src3,
178 #define NONE ' '
179 #define IREG 'i'
180 #define FREG 'f'
181 #define VREG 'v'
182 #define XREG 'x'
183 #if SIZEOF_REGISTER == 8 && SIZEOF_REGISTER == SIZEOF_VOID_P
184 #define LREG IREG
185 #else
186 #define LREG 'l'
187 #endif
188 /* keep in sync with the enum in mini.h */
189 const char
190 ins_info[] = {
191 #include "mini-ops.h"
193 #undef MINI_OP
194 #undef MINI_OP3
196 #define MINI_OP(a,b,dest,src1,src2) ((src2) != NONE ? 2 : ((src1) != NONE ? 1 : 0)),
197 #define MINI_OP3(a,b,dest,src1,src2,src3) ((src3) != NONE ? 3 : ((src2) != NONE ? 2 : ((src1) != NONE ? 1 : 0))),
199 * This should contain the index of the last sreg + 1. This is not the same
200 * as the number of sregs for opcodes like IA64_CMP_EQ_IMM.
202 const gint8 ins_sreg_counts[] = {
203 #include "mini-ops.h"
205 #undef MINI_OP
206 #undef MINI_OP3
208 #define MONO_INIT_VARINFO(vi,id) do { \
209 (vi)->range.first_use.pos.bid = 0xffff; \
210 (vi)->reg = -1; \
211 (vi)->idx = (id); \
212 } while (0)
214 guint32
215 mono_alloc_ireg (MonoCompile *cfg)
217 return alloc_ireg (cfg);
220 guint32
221 mono_alloc_lreg (MonoCompile *cfg)
223 return alloc_lreg (cfg);
226 guint32
227 mono_alloc_freg (MonoCompile *cfg)
229 return alloc_freg (cfg);
232 guint32
233 mono_alloc_preg (MonoCompile *cfg)
235 return alloc_preg (cfg);
238 guint32
239 mono_alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
241 return alloc_dreg (cfg, stack_type);
245 * mono_alloc_ireg_ref:
247 * Allocate an IREG, and mark it as holding a GC ref.
249 guint32
250 mono_alloc_ireg_ref (MonoCompile *cfg)
252 return alloc_ireg_ref (cfg);
256 * mono_alloc_ireg_mp:
258 * Allocate an IREG, and mark it as holding a managed pointer.
260 guint32
261 mono_alloc_ireg_mp (MonoCompile *cfg)
263 return alloc_ireg_mp (cfg);
267 * mono_alloc_ireg_copy:
269 * Allocate an IREG with the same GC type as VREG.
271 guint32
272 mono_alloc_ireg_copy (MonoCompile *cfg, guint32 vreg)
274 if (vreg_is_ref (cfg, vreg))
275 return alloc_ireg_ref (cfg);
276 else if (vreg_is_mp (cfg, vreg))
277 return alloc_ireg_mp (cfg);
278 else
279 return alloc_ireg (cfg);
282 guint
283 mono_type_to_regmove (MonoCompile *cfg, MonoType *type)
285 if (type->byref)
286 return OP_MOVE;
288 type = mini_get_underlying_type (type);
289 handle_enum:
290 switch (type->type) {
291 case MONO_TYPE_I1:
292 case MONO_TYPE_U1:
293 return OP_MOVE;
294 case MONO_TYPE_I2:
295 case MONO_TYPE_U2:
296 return OP_MOVE;
297 case MONO_TYPE_I4:
298 case MONO_TYPE_U4:
299 return OP_MOVE;
300 case MONO_TYPE_I:
301 case MONO_TYPE_U:
302 case MONO_TYPE_PTR:
303 case MONO_TYPE_FNPTR:
304 return OP_MOVE;
305 case MONO_TYPE_CLASS:
306 case MONO_TYPE_STRING:
307 case MONO_TYPE_OBJECT:
308 case MONO_TYPE_SZARRAY:
309 case MONO_TYPE_ARRAY:
310 return OP_MOVE;
311 case MONO_TYPE_I8:
312 case MONO_TYPE_U8:
313 #if SIZEOF_REGISTER == 8
314 return OP_MOVE;
315 #else
316 return OP_LMOVE;
317 #endif
318 case MONO_TYPE_R4:
319 return cfg->r4fp ? OP_RMOVE : OP_FMOVE;
320 case MONO_TYPE_R8:
321 return OP_FMOVE;
322 case MONO_TYPE_VALUETYPE:
323 if (type->data.klass->enumtype) {
324 type = mono_class_enum_basetype (type->data.klass);
325 goto handle_enum;
327 if (MONO_CLASS_IS_SIMD (cfg, mono_class_from_mono_type (type)))
328 return OP_XMOVE;
329 return OP_VMOVE;
330 case MONO_TYPE_TYPEDBYREF:
331 return OP_VMOVE;
332 case MONO_TYPE_GENERICINST:
333 type = &type->data.generic_class->container_class->byval_arg;
334 goto handle_enum;
335 case MONO_TYPE_VAR:
336 case MONO_TYPE_MVAR:
337 g_assert (cfg->gshared);
338 if (mini_type_var_is_vt (type))
339 return OP_VMOVE;
340 else
341 return mono_type_to_regmove (cfg, mini_get_underlying_type (type));
342 default:
343 g_error ("unknown type 0x%02x in type_to_regstore", type->type);
345 return -1;
348 void
349 mono_print_bb (MonoBasicBlock *bb, const char *msg)
351 int i;
352 MonoInst *tree;
354 printf ("\n%s %d: [IN: ", msg, bb->block_num);
355 for (i = 0; i < bb->in_count; ++i)
356 printf (" BB%d(%d)", bb->in_bb [i]->block_num, bb->in_bb [i]->dfn);
357 printf (", OUT: ");
358 for (i = 0; i < bb->out_count; ++i)
359 printf (" BB%d(%d)", bb->out_bb [i]->block_num, bb->out_bb [i]->dfn);
360 printf (" ]\n");
361 for (tree = bb->code; tree; tree = tree->next)
362 mono_print_ins_index (-1, tree);
365 void
366 mono_create_helper_signatures (void)
368 helper_sig_domain_get = mono_create_icall_signature ("ptr");
369 helper_sig_rgctx_lazy_fetch_trampoline = mono_create_icall_signature ("ptr ptr");
370 helper_sig_llvmonly_imt_thunk = mono_create_icall_signature ("ptr ptr ptr");
373 static MONO_NEVER_INLINE void
374 break_on_unverified (void)
376 if (mini_get_debug_options ()->break_on_unverified)
377 G_BREAKPOINT ();
380 static MONO_NEVER_INLINE void
381 method_access_failure (MonoCompile *cfg, MonoMethod *method, MonoMethod *cil_method)
383 char *method_fname = mono_method_full_name (method, TRUE);
384 char *cil_method_fname = mono_method_full_name (cil_method, TRUE);
385 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
386 mono_error_set_generic_error (&cfg->error, "System", "MethodAccessException", "Method `%s' is inaccessible from method `%s'\n", cil_method_fname, method_fname);
387 g_free (method_fname);
388 g_free (cil_method_fname);
391 static MONO_NEVER_INLINE void
392 field_access_failure (MonoCompile *cfg, MonoMethod *method, MonoClassField *field)
394 char *method_fname = mono_method_full_name (method, TRUE);
395 char *field_fname = mono_field_full_name (field);
396 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
397 mono_error_set_generic_error (&cfg->error, "System", "FieldAccessException", "Field `%s' is inaccessible from method `%s'\n", field_fname, method_fname);
398 g_free (method_fname);
399 g_free (field_fname);
402 static MONO_NEVER_INLINE void
403 inline_failure (MonoCompile *cfg, const char *msg)
405 if (cfg->verbose_level >= 2)
406 printf ("inline failed: %s\n", msg);
407 mono_cfg_set_exception (cfg, MONO_EXCEPTION_INLINE_FAILED);
410 static MONO_NEVER_INLINE void
411 gshared_failure (MonoCompile *cfg, int opcode, const char *file, int line)
413 if (cfg->verbose_level > 2) \
414 printf ("sharing failed for method %s.%s.%s/%d opcode %s line %d\n", cfg->current_method->klass->name_space, cfg->current_method->klass->name, cfg->current_method->name, cfg->current_method->signature->param_count, mono_opcode_name ((opcode)), line);
415 mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED);
418 static MONO_NEVER_INLINE void
419 gsharedvt_failure (MonoCompile *cfg, int opcode, const char *file, int line)
421 cfg->exception_message = g_strdup_printf ("gsharedvt failed for method %s.%s.%s/%d opcode %s %s:%d", cfg->current_method->klass->name_space, cfg->current_method->klass->name, cfg->current_method->name, cfg->current_method->signature->param_count, mono_opcode_name ((opcode)), file, line);
422 if (cfg->verbose_level >= 2)
423 printf ("%s\n", cfg->exception_message);
424 mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED);
428 * When using gsharedvt, some instatiations might be verifiable, and some might be not. i.e.
429 * foo<T> (int i) { ldarg.0; box T; }
431 #define UNVERIFIED do { \
432 if (cfg->gsharedvt) { \
433 if (cfg->verbose_level > 2) \
434 printf ("gsharedvt method failed to verify, falling back to instantiation.\n"); \
435 mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED); \
436 goto exception_exit; \
438 break_on_unverified (); \
439 goto unverified; \
440 } while (0)
442 #define GET_BBLOCK(cfg,tblock,ip) do { \
443 (tblock) = cfg->cil_offset_to_bb [(ip) - cfg->cil_start]; \
444 if (!(tblock)) { \
445 if ((ip) >= end || (ip) < header->code) UNVERIFIED; \
446 NEW_BBLOCK (cfg, (tblock)); \
447 (tblock)->cil_code = (ip); \
448 ADD_BBLOCK (cfg, (tblock)); \
450 } while (0)
452 #if defined(TARGET_X86) || defined(TARGET_AMD64)
453 #define EMIT_NEW_X86_LEA(cfg,dest,sr1,sr2,shift,imm) do { \
454 MONO_INST_NEW (cfg, dest, OP_X86_LEA); \
455 (dest)->dreg = alloc_ireg_mp ((cfg)); \
456 (dest)->sreg1 = (sr1); \
457 (dest)->sreg2 = (sr2); \
458 (dest)->inst_imm = (imm); \
459 (dest)->backend.shift_amount = (shift); \
460 MONO_ADD_INS ((cfg)->cbb, (dest)); \
461 } while (0)
462 #endif
464 /* Emit conversions so both operands of a binary opcode are of the same type */
465 static void
466 add_widen_op (MonoCompile *cfg, MonoInst *ins, MonoInst **arg1_ref, MonoInst **arg2_ref)
468 MonoInst *arg1 = *arg1_ref;
469 MonoInst *arg2 = *arg2_ref;
471 if (cfg->r4fp &&
472 ((arg1->type == STACK_R4 && arg2->type == STACK_R8) ||
473 (arg1->type == STACK_R8 && arg2->type == STACK_R4))) {
474 MonoInst *conv;
476 /* Mixing r4/r8 is allowed by the spec */
477 if (arg1->type == STACK_R4) {
478 int dreg = alloc_freg (cfg);
480 EMIT_NEW_UNALU (cfg, conv, OP_RCONV_TO_R8, dreg, arg1->dreg);
481 conv->type = STACK_R8;
482 ins->sreg1 = dreg;
483 *arg1_ref = conv;
485 if (arg2->type == STACK_R4) {
486 int dreg = alloc_freg (cfg);
488 EMIT_NEW_UNALU (cfg, conv, OP_RCONV_TO_R8, dreg, arg2->dreg);
489 conv->type = STACK_R8;
490 ins->sreg2 = dreg;
491 *arg2_ref = conv;
495 #if SIZEOF_REGISTER == 8
496 /* FIXME: Need to add many more cases */
497 if ((arg1)->type == STACK_PTR && (arg2)->type == STACK_I4) {
498 MonoInst *widen;
500 int dr = alloc_preg (cfg);
501 EMIT_NEW_UNALU (cfg, widen, OP_SEXT_I4, dr, (arg2)->dreg);
502 (ins)->sreg2 = widen->dreg;
504 #endif
507 #define ADD_BINOP(op) do { \
508 MONO_INST_NEW (cfg, ins, (op)); \
509 sp -= 2; \
510 ins->sreg1 = sp [0]->dreg; \
511 ins->sreg2 = sp [1]->dreg; \
512 type_from_op (cfg, ins, sp [0], sp [1]); \
513 CHECK_TYPE (ins); \
514 /* Have to insert a widening op */ \
515 add_widen_op (cfg, ins, &sp [0], &sp [1]); \
516 ins->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type); \
517 MONO_ADD_INS ((cfg)->cbb, (ins)); \
518 *sp++ = mono_decompose_opcode ((cfg), (ins)); \
519 } while (0)
521 #define ADD_UNOP(op) do { \
522 MONO_INST_NEW (cfg, ins, (op)); \
523 sp--; \
524 ins->sreg1 = sp [0]->dreg; \
525 type_from_op (cfg, ins, sp [0], NULL); \
526 CHECK_TYPE (ins); \
527 (ins)->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type); \
528 MONO_ADD_INS ((cfg)->cbb, (ins)); \
529 *sp++ = mono_decompose_opcode (cfg, ins); \
530 } while (0)
532 #define ADD_BINCOND(next_block) do { \
533 MonoInst *cmp; \
534 sp -= 2; \
535 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
536 cmp->sreg1 = sp [0]->dreg; \
537 cmp->sreg2 = sp [1]->dreg; \
538 type_from_op (cfg, cmp, sp [0], sp [1]); \
539 CHECK_TYPE (cmp); \
540 add_widen_op (cfg, cmp, &sp [0], &sp [1]); \
541 type_from_op (cfg, ins, sp [0], sp [1]); \
542 ins->inst_many_bb = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
543 GET_BBLOCK (cfg, tblock, target); \
544 link_bblock (cfg, cfg->cbb, tblock); \
545 ins->inst_true_bb = tblock; \
546 if ((next_block)) { \
547 link_bblock (cfg, cfg->cbb, (next_block)); \
548 ins->inst_false_bb = (next_block); \
549 start_new_bblock = 1; \
550 } else { \
551 GET_BBLOCK (cfg, tblock, ip); \
552 link_bblock (cfg, cfg->cbb, tblock); \
553 ins->inst_false_bb = tblock; \
554 start_new_bblock = 2; \
556 if (sp != stack_start) { \
557 handle_stack_args (cfg, stack_start, sp - stack_start); \
558 CHECK_UNVERIFIABLE (cfg); \
560 MONO_ADD_INS (cfg->cbb, cmp); \
561 MONO_ADD_INS (cfg->cbb, ins); \
562 } while (0)
564 /* *
565 * link_bblock: Links two basic blocks
567 * links two basic blocks in the control flow graph, the 'from'
568 * argument is the starting block and the 'to' argument is the block
569 * the control flow ends to after 'from'.
571 static void
572 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
574 MonoBasicBlock **newa;
575 int i, found;
577 #if 0
578 if (from->cil_code) {
579 if (to->cil_code)
580 printf ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
581 else
582 printf ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
583 } else {
584 if (to->cil_code)
585 printf ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
586 else
587 printf ("edge from entry to exit\n");
589 #endif
591 found = FALSE;
592 for (i = 0; i < from->out_count; ++i) {
593 if (to == from->out_bb [i]) {
594 found = TRUE;
595 break;
598 if (!found) {
599 newa = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
600 for (i = 0; i < from->out_count; ++i) {
601 newa [i] = from->out_bb [i];
603 newa [i] = to;
604 from->out_count++;
605 from->out_bb = newa;
608 found = FALSE;
609 for (i = 0; i < to->in_count; ++i) {
610 if (from == to->in_bb [i]) {
611 found = TRUE;
612 break;
615 if (!found) {
616 newa = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
617 for (i = 0; i < to->in_count; ++i) {
618 newa [i] = to->in_bb [i];
620 newa [i] = from;
621 to->in_count++;
622 to->in_bb = newa;
626 void
627 mono_link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
629 link_bblock (cfg, from, to);
633 * mono_find_block_region:
635 * We mark each basic block with a region ID. We use that to avoid BB
636 * optimizations when blocks are in different regions.
638 * Returns:
639 * A region token that encodes where this region is, and information
640 * about the clause owner for this block.
642 * The region encodes the try/catch/filter clause that owns this block
643 * as well as the type. -1 is a special value that represents a block
644 * that is in none of try/catch/filter.
646 static int
647 mono_find_block_region (MonoCompile *cfg, int offset)
649 MonoMethodHeader *header = cfg->header;
650 MonoExceptionClause *clause;
651 int i;
653 for (i = 0; i < header->num_clauses; ++i) {
654 clause = &header->clauses [i];
655 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->data.filter_offset) &&
656 (offset < (clause->handler_offset)))
657 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
659 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
660 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
661 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
662 else if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
663 return ((i + 1) << 8) | MONO_REGION_FAULT | clause->flags;
664 else
665 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
668 for (i = 0; i < header->num_clauses; ++i) {
669 clause = &header->clauses [i];
671 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
672 return ((i + 1) << 8) | clause->flags;
675 return -1;
678 static GList*
679 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
681 MonoMethodHeader *header = cfg->header;
682 MonoExceptionClause *clause;
683 int i;
684 GList *res = NULL;
686 for (i = 0; i < header->num_clauses; ++i) {
687 clause = &header->clauses [i];
688 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
689 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
690 if (clause->flags == type)
691 res = g_list_append (res, clause);
694 return res;
697 static void
698 mono_create_spvar_for_region (MonoCompile *cfg, int region)
700 MonoInst *var;
702 var = (MonoInst *)g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
703 if (var)
704 return;
706 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
707 /* prevent it from being register allocated */
708 var->flags |= MONO_INST_VOLATILE;
710 g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
713 MonoInst *
714 mono_find_exvar_for_offset (MonoCompile *cfg, int offset)
716 return (MonoInst *)g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
719 static MonoInst*
720 mono_create_exvar_for_offset (MonoCompile *cfg, int offset)
722 MonoInst *var;
724 var = (MonoInst *)g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
725 if (var)
726 return var;
728 var = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
729 /* prevent it from being register allocated */
730 var->flags |= MONO_INST_VOLATILE;
732 g_hash_table_insert (cfg->exvars, GINT_TO_POINTER (offset), var);
734 return var;
738 * Returns the type used in the eval stack when @type is loaded.
739 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
741 void
742 type_to_eval_stack_type (MonoCompile *cfg, MonoType *type, MonoInst *inst)
744 MonoClass *klass;
746 type = mini_get_underlying_type (type);
747 inst->klass = klass = mono_class_from_mono_type (type);
748 if (type->byref) {
749 inst->type = STACK_MP;
750 return;
753 handle_enum:
754 switch (type->type) {
755 case MONO_TYPE_VOID:
756 inst->type = STACK_INV;
757 return;
758 case MONO_TYPE_I1:
759 case MONO_TYPE_U1:
760 case MONO_TYPE_I2:
761 case MONO_TYPE_U2:
762 case MONO_TYPE_I4:
763 case MONO_TYPE_U4:
764 inst->type = STACK_I4;
765 return;
766 case MONO_TYPE_I:
767 case MONO_TYPE_U:
768 case MONO_TYPE_PTR:
769 case MONO_TYPE_FNPTR:
770 inst->type = STACK_PTR;
771 return;
772 case MONO_TYPE_CLASS:
773 case MONO_TYPE_STRING:
774 case MONO_TYPE_OBJECT:
775 case MONO_TYPE_SZARRAY:
776 case MONO_TYPE_ARRAY:
777 inst->type = STACK_OBJ;
778 return;
779 case MONO_TYPE_I8:
780 case MONO_TYPE_U8:
781 inst->type = STACK_I8;
782 return;
783 case MONO_TYPE_R4:
784 inst->type = cfg->r4_stack_type;
785 break;
786 case MONO_TYPE_R8:
787 inst->type = STACK_R8;
788 return;
789 case MONO_TYPE_VALUETYPE:
790 if (type->data.klass->enumtype) {
791 type = mono_class_enum_basetype (type->data.klass);
792 goto handle_enum;
793 } else {
794 inst->klass = klass;
795 inst->type = STACK_VTYPE;
796 return;
798 case MONO_TYPE_TYPEDBYREF:
799 inst->klass = mono_defaults.typed_reference_class;
800 inst->type = STACK_VTYPE;
801 return;
802 case MONO_TYPE_GENERICINST:
803 type = &type->data.generic_class->container_class->byval_arg;
804 goto handle_enum;
805 case MONO_TYPE_VAR:
806 case MONO_TYPE_MVAR:
807 g_assert (cfg->gshared);
808 if (mini_is_gsharedvt_type (type)) {
809 g_assert (cfg->gsharedvt);
810 inst->type = STACK_VTYPE;
811 } else {
812 type_to_eval_stack_type (cfg, mini_get_underlying_type (type), inst);
814 return;
815 default:
816 g_error ("unknown type 0x%02x in eval stack type", type->type);
821 * The following tables are used to quickly validate the IL code in type_from_op ().
823 static const char
824 bin_num_table [STACK_MAX] [STACK_MAX] = {
825 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
826 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
827 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
828 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
829 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV, STACK_R8},
830 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
831 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
832 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
833 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV, STACK_R4}
836 static const char
837 neg_table [] = {
838 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV, STACK_R4
841 /* reduce the size of this table */
842 static const char
843 bin_int_table [STACK_MAX] [STACK_MAX] = {
844 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
845 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
846 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
847 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
848 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
849 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
850 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
851 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
854 static const char
855 bin_comp_table [STACK_MAX] [STACK_MAX] = {
856 /* Inv i L p F & O vt r4 */
857 {0},
858 {0, 1, 0, 1, 0, 0, 0, 0}, /* i, int32 */
859 {0, 0, 1, 0, 0, 0, 0, 0}, /* L, int64 */
860 {0, 1, 0, 1, 0, 2, 4, 0}, /* p, ptr */
861 {0, 0, 0, 0, 1, 0, 0, 0, 1}, /* F, R8 */
862 {0, 0, 0, 2, 0, 1, 0, 0}, /* &, managed pointer */
863 {0, 0, 0, 4, 0, 0, 3, 0}, /* O, reference */
864 {0, 0, 0, 0, 0, 0, 0, 0}, /* vt value type */
865 {0, 0, 0, 0, 1, 0, 0, 0, 1}, /* r, r4 */
868 /* reduce the size of this table */
869 static const char
870 shift_table [STACK_MAX] [STACK_MAX] = {
871 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
872 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
873 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
874 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
875 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
876 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
877 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
878 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
882 * Tables to map from the non-specific opcode to the matching
883 * type-specific opcode.
885 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
886 static const guint16
887 binops_op_map [STACK_MAX] = {
888 0, OP_IADD-CEE_ADD, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, OP_PADD-CEE_ADD, 0, 0, OP_RADD-CEE_ADD
891 /* handles from CEE_NEG to CEE_CONV_U8 */
892 static const guint16
893 unops_op_map [STACK_MAX] = {
894 0, OP_INEG-CEE_NEG, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, OP_PNEG-CEE_NEG, 0, 0, OP_RNEG-CEE_NEG
897 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
898 static const guint16
899 ovfops_op_map [STACK_MAX] = {
900 0, OP_ICONV_TO_U2-CEE_CONV_U2, OP_LCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_FCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, 0, OP_RCONV_TO_U2-CEE_CONV_U2
903 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
904 static const guint16
905 ovf2ops_op_map [STACK_MAX] = {
906 0, OP_ICONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN, 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, OP_PCONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN, 0, 0, OP_RCONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN
909 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
910 static const guint16
911 ovf3ops_op_map [STACK_MAX] = {
912 0, OP_ICONV_TO_OVF_I1-CEE_CONV_OVF_I1, 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, OP_PCONV_TO_OVF_I1-CEE_CONV_OVF_I1, 0, 0, OP_RCONV_TO_OVF_I1-CEE_CONV_OVF_I1
915 /* handles from CEE_BEQ to CEE_BLT_UN */
916 static const guint16
917 beqops_op_map [STACK_MAX] = {
918 0, OP_IBEQ-CEE_BEQ, OP_LBEQ-CEE_BEQ, OP_PBEQ-CEE_BEQ, OP_FBEQ-CEE_BEQ, OP_PBEQ-CEE_BEQ, OP_PBEQ-CEE_BEQ, 0, OP_FBEQ-CEE_BEQ
921 /* handles from CEE_CEQ to CEE_CLT_UN */
922 static const guint16
923 ceqops_op_map [STACK_MAX] = {
924 0, OP_ICEQ-OP_CEQ, OP_LCEQ-OP_CEQ, OP_PCEQ-OP_CEQ, OP_FCEQ-OP_CEQ, OP_PCEQ-OP_CEQ, OP_PCEQ-OP_CEQ, 0, OP_RCEQ-OP_CEQ
928 * Sets ins->type (the type on the eval stack) according to the
929 * type of the opcode and the arguments to it.
930 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
932 * FIXME: this function sets ins->type unconditionally in some cases, but
933 * it should set it to invalid for some types (a conv.x on an object)
935 static void
936 type_from_op (MonoCompile *cfg, MonoInst *ins, MonoInst *src1, MonoInst *src2)
938 switch (ins->opcode) {
939 /* binops */
940 case CEE_ADD:
941 case CEE_SUB:
942 case CEE_MUL:
943 case CEE_DIV:
944 case CEE_REM:
945 /* FIXME: check unverifiable args for STACK_MP */
946 ins->type = bin_num_table [src1->type] [src2->type];
947 ins->opcode += binops_op_map [ins->type];
948 break;
949 case CEE_DIV_UN:
950 case CEE_REM_UN:
951 case CEE_AND:
952 case CEE_OR:
953 case CEE_XOR:
954 ins->type = bin_int_table [src1->type] [src2->type];
955 ins->opcode += binops_op_map [ins->type];
956 break;
957 case CEE_SHL:
958 case CEE_SHR:
959 case CEE_SHR_UN:
960 ins->type = shift_table [src1->type] [src2->type];
961 ins->opcode += binops_op_map [ins->type];
962 break;
963 case OP_COMPARE:
964 case OP_LCOMPARE:
965 case OP_ICOMPARE:
966 ins->type = bin_comp_table [src1->type] [src2->type] ? STACK_I4: STACK_INV;
967 if ((src1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((src1->type == STACK_PTR) || (src1->type == STACK_OBJ) || (src1->type == STACK_MP))))
968 ins->opcode = OP_LCOMPARE;
969 else if (src1->type == STACK_R4)
970 ins->opcode = OP_RCOMPARE;
971 else if (src1->type == STACK_R8)
972 ins->opcode = OP_FCOMPARE;
973 else
974 ins->opcode = OP_ICOMPARE;
975 break;
976 case OP_ICOMPARE_IMM:
977 ins->type = bin_comp_table [src1->type] [src1->type] ? STACK_I4 : STACK_INV;
978 if ((src1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((src1->type == STACK_PTR) || (src1->type == STACK_OBJ) || (src1->type == STACK_MP))))
979 ins->opcode = OP_LCOMPARE_IMM;
980 break;
981 case CEE_BEQ:
982 case CEE_BGE:
983 case CEE_BGT:
984 case CEE_BLE:
985 case CEE_BLT:
986 case CEE_BNE_UN:
987 case CEE_BGE_UN:
988 case CEE_BGT_UN:
989 case CEE_BLE_UN:
990 case CEE_BLT_UN:
991 ins->opcode += beqops_op_map [src1->type];
992 break;
993 case OP_CEQ:
994 ins->type = bin_comp_table [src1->type] [src2->type] ? STACK_I4: STACK_INV;
995 ins->opcode += ceqops_op_map [src1->type];
996 break;
997 case OP_CGT:
998 case OP_CGT_UN:
999 case OP_CLT:
1000 case OP_CLT_UN:
1001 ins->type = (bin_comp_table [src1->type] [src2->type] & 1) ? STACK_I4: STACK_INV;
1002 ins->opcode += ceqops_op_map [src1->type];
1003 break;
1004 /* unops */
1005 case CEE_NEG:
1006 ins->type = neg_table [src1->type];
1007 ins->opcode += unops_op_map [ins->type];
1008 break;
1009 case CEE_NOT:
1010 if (src1->type >= STACK_I4 && src1->type <= STACK_PTR)
1011 ins->type = src1->type;
1012 else
1013 ins->type = STACK_INV;
1014 ins->opcode += unops_op_map [ins->type];
1015 break;
1016 case CEE_CONV_I1:
1017 case CEE_CONV_I2:
1018 case CEE_CONV_I4:
1019 case CEE_CONV_U4:
1020 ins->type = STACK_I4;
1021 ins->opcode += unops_op_map [src1->type];
1022 break;
1023 case CEE_CONV_R_UN:
1024 ins->type = STACK_R8;
1025 switch (src1->type) {
1026 case STACK_I4:
1027 case STACK_PTR:
1028 ins->opcode = OP_ICONV_TO_R_UN;
1029 break;
1030 case STACK_I8:
1031 ins->opcode = OP_LCONV_TO_R_UN;
1032 break;
1034 break;
1035 case CEE_CONV_OVF_I1:
1036 case CEE_CONV_OVF_U1:
1037 case CEE_CONV_OVF_I2:
1038 case CEE_CONV_OVF_U2:
1039 case CEE_CONV_OVF_I4:
1040 case CEE_CONV_OVF_U4:
1041 ins->type = STACK_I4;
1042 ins->opcode += ovf3ops_op_map [src1->type];
1043 break;
1044 case CEE_CONV_OVF_I_UN:
1045 case CEE_CONV_OVF_U_UN:
1046 ins->type = STACK_PTR;
1047 ins->opcode += ovf2ops_op_map [src1->type];
1048 break;
1049 case CEE_CONV_OVF_I1_UN:
1050 case CEE_CONV_OVF_I2_UN:
1051 case CEE_CONV_OVF_I4_UN:
1052 case CEE_CONV_OVF_U1_UN:
1053 case CEE_CONV_OVF_U2_UN:
1054 case CEE_CONV_OVF_U4_UN:
1055 ins->type = STACK_I4;
1056 ins->opcode += ovf2ops_op_map [src1->type];
1057 break;
1058 case CEE_CONV_U:
1059 ins->type = STACK_PTR;
1060 switch (src1->type) {
1061 case STACK_I4:
1062 ins->opcode = OP_ICONV_TO_U;
1063 break;
1064 case STACK_PTR:
1065 case STACK_MP:
1066 #if SIZEOF_VOID_P == 8
1067 ins->opcode = OP_LCONV_TO_U;
1068 #else
1069 ins->opcode = OP_MOVE;
1070 #endif
1071 break;
1072 case STACK_I8:
1073 ins->opcode = OP_LCONV_TO_U;
1074 break;
1075 case STACK_R8:
1076 ins->opcode = OP_FCONV_TO_U;
1077 break;
1079 break;
1080 case CEE_CONV_I8:
1081 case CEE_CONV_U8:
1082 ins->type = STACK_I8;
1083 ins->opcode += unops_op_map [src1->type];
1084 break;
1085 case CEE_CONV_OVF_I8:
1086 case CEE_CONV_OVF_U8:
1087 ins->type = STACK_I8;
1088 ins->opcode += ovf3ops_op_map [src1->type];
1089 break;
1090 case CEE_CONV_OVF_U8_UN:
1091 case CEE_CONV_OVF_I8_UN:
1092 ins->type = STACK_I8;
1093 ins->opcode += ovf2ops_op_map [src1->type];
1094 break;
1095 case CEE_CONV_R4:
1096 ins->type = cfg->r4_stack_type;
1097 ins->opcode += unops_op_map [src1->type];
1098 break;
1099 case CEE_CONV_R8:
1100 ins->type = STACK_R8;
1101 ins->opcode += unops_op_map [src1->type];
1102 break;
1103 case OP_CKFINITE:
1104 ins->type = STACK_R8;
1105 break;
1106 case CEE_CONV_U2:
1107 case CEE_CONV_U1:
1108 ins->type = STACK_I4;
1109 ins->opcode += ovfops_op_map [src1->type];
1110 break;
1111 case CEE_CONV_I:
1112 case CEE_CONV_OVF_I:
1113 case CEE_CONV_OVF_U:
1114 ins->type = STACK_PTR;
1115 ins->opcode += ovfops_op_map [src1->type];
1116 break;
1117 case CEE_ADD_OVF:
1118 case CEE_ADD_OVF_UN:
1119 case CEE_MUL_OVF:
1120 case CEE_MUL_OVF_UN:
1121 case CEE_SUB_OVF:
1122 case CEE_SUB_OVF_UN:
1123 ins->type = bin_num_table [src1->type] [src2->type];
1124 ins->opcode += ovfops_op_map [src1->type];
1125 if (ins->type == STACK_R8)
1126 ins->type = STACK_INV;
1127 break;
1128 case OP_LOAD_MEMBASE:
1129 ins->type = STACK_PTR;
1130 break;
1131 case OP_LOADI1_MEMBASE:
1132 case OP_LOADU1_MEMBASE:
1133 case OP_LOADI2_MEMBASE:
1134 case OP_LOADU2_MEMBASE:
1135 case OP_LOADI4_MEMBASE:
1136 case OP_LOADU4_MEMBASE:
1137 ins->type = STACK_PTR;
1138 break;
1139 case OP_LOADI8_MEMBASE:
1140 ins->type = STACK_I8;
1141 break;
1142 case OP_LOADR4_MEMBASE:
1143 ins->type = cfg->r4_stack_type;
1144 break;
1145 case OP_LOADR8_MEMBASE:
1146 ins->type = STACK_R8;
1147 break;
1148 default:
1149 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1150 break;
1153 if (ins->type == STACK_MP)
1154 ins->klass = mono_defaults.object_class;
1157 static const char
1158 ldind_type [] = {
1159 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_R8, STACK_OBJ
1162 #if 0
1164 static const char
1165 param_table [STACK_MAX] [STACK_MAX] = {
1166 {0},
1169 static int
1170 check_values_to_signature (MonoInst *args, MonoType *this_ins, MonoMethodSignature *sig)
1172 int i;
1174 if (sig->hasthis) {
1175 switch (args->type) {
1176 case STACK_I4:
1177 case STACK_I8:
1178 case STACK_R8:
1179 case STACK_VTYPE:
1180 case STACK_INV:
1181 return 0;
1183 args++;
1185 for (i = 0; i < sig->param_count; ++i) {
1186 switch (args [i].type) {
1187 case STACK_INV:
1188 return 0;
1189 case STACK_MP:
1190 if (!sig->params [i]->byref)
1191 return 0;
1192 continue;
1193 case STACK_OBJ:
1194 if (sig->params [i]->byref)
1195 return 0;
1196 switch (sig->params [i]->type) {
1197 case MONO_TYPE_CLASS:
1198 case MONO_TYPE_STRING:
1199 case MONO_TYPE_OBJECT:
1200 case MONO_TYPE_SZARRAY:
1201 case MONO_TYPE_ARRAY:
1202 break;
1203 default:
1204 return 0;
1206 continue;
1207 case STACK_R8:
1208 if (sig->params [i]->byref)
1209 return 0;
1210 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1211 return 0;
1212 continue;
1213 case STACK_PTR:
1214 case STACK_I4:
1215 case STACK_I8:
1216 case STACK_VTYPE:
1217 break;
1219 /*if (!param_table [args [i].type] [sig->params [i]->type])
1220 return 0;*/
1222 return 1;
1224 #endif
1227 * When we need a pointer to the current domain many times in a method, we
1228 * call mono_domain_get() once and we store the result in a local variable.
1229 * This function returns the variable that represents the MonoDomain*.
1231 inline static MonoInst *
1232 mono_get_domainvar (MonoCompile *cfg)
1234 if (!cfg->domainvar)
1235 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1236 return cfg->domainvar;
1240 * The got_var contains the address of the Global Offset Table when AOT
1241 * compiling.
1243 MonoInst *
1244 mono_get_got_var (MonoCompile *cfg)
1246 if (!cfg->compile_aot || !cfg->backend->need_got_var)
1247 return NULL;
1248 if (!cfg->got_var) {
1249 cfg->got_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1251 return cfg->got_var;
1254 static MonoInst *
1255 mono_get_vtable_var (MonoCompile *cfg)
1257 g_assert (cfg->gshared);
1259 if (!cfg->rgctx_var) {
1260 cfg->rgctx_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1261 /* force the var to be stack allocated */
1262 cfg->rgctx_var->flags |= MONO_INST_VOLATILE;
1265 return cfg->rgctx_var;
1268 static MonoType*
1269 type_from_stack_type (MonoInst *ins) {
1270 switch (ins->type) {
1271 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1272 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1273 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1274 case STACK_R4: return &mono_defaults.single_class->byval_arg;
1275 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1276 case STACK_MP:
1277 return &ins->klass->this_arg;
1278 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1279 case STACK_VTYPE: return &ins->klass->byval_arg;
1280 default:
1281 g_error ("stack type %d to monotype not handled\n", ins->type);
1283 return NULL;
1286 static G_GNUC_UNUSED int
1287 type_to_stack_type (MonoCompile *cfg, MonoType *t)
1289 t = mono_type_get_underlying_type (t);
1290 switch (t->type) {
1291 case MONO_TYPE_I1:
1292 case MONO_TYPE_U1:
1293 case MONO_TYPE_I2:
1294 case MONO_TYPE_U2:
1295 case MONO_TYPE_I4:
1296 case MONO_TYPE_U4:
1297 return STACK_I4;
1298 case MONO_TYPE_I:
1299 case MONO_TYPE_U:
1300 case MONO_TYPE_PTR:
1301 case MONO_TYPE_FNPTR:
1302 return STACK_PTR;
1303 case MONO_TYPE_CLASS:
1304 case MONO_TYPE_STRING:
1305 case MONO_TYPE_OBJECT:
1306 case MONO_TYPE_SZARRAY:
1307 case MONO_TYPE_ARRAY:
1308 return STACK_OBJ;
1309 case MONO_TYPE_I8:
1310 case MONO_TYPE_U8:
1311 return STACK_I8;
1312 case MONO_TYPE_R4:
1313 return cfg->r4_stack_type;
1314 case MONO_TYPE_R8:
1315 return STACK_R8;
1316 case MONO_TYPE_VALUETYPE:
1317 case MONO_TYPE_TYPEDBYREF:
1318 return STACK_VTYPE;
1319 case MONO_TYPE_GENERICINST:
1320 if (mono_type_generic_inst_is_valuetype (t))
1321 return STACK_VTYPE;
1322 else
1323 return STACK_OBJ;
1324 break;
1325 default:
1326 g_assert_not_reached ();
1329 return -1;
1332 static MonoClass*
1333 array_access_to_klass (int opcode)
1335 switch (opcode) {
1336 case CEE_LDELEM_U1:
1337 return mono_defaults.byte_class;
1338 case CEE_LDELEM_U2:
1339 return mono_defaults.uint16_class;
1340 case CEE_LDELEM_I:
1341 case CEE_STELEM_I:
1342 return mono_defaults.int_class;
1343 case CEE_LDELEM_I1:
1344 case CEE_STELEM_I1:
1345 return mono_defaults.sbyte_class;
1346 case CEE_LDELEM_I2:
1347 case CEE_STELEM_I2:
1348 return mono_defaults.int16_class;
1349 case CEE_LDELEM_I4:
1350 case CEE_STELEM_I4:
1351 return mono_defaults.int32_class;
1352 case CEE_LDELEM_U4:
1353 return mono_defaults.uint32_class;
1354 case CEE_LDELEM_I8:
1355 case CEE_STELEM_I8:
1356 return mono_defaults.int64_class;
1357 case CEE_LDELEM_R4:
1358 case CEE_STELEM_R4:
1359 return mono_defaults.single_class;
1360 case CEE_LDELEM_R8:
1361 case CEE_STELEM_R8:
1362 return mono_defaults.double_class;
1363 case CEE_LDELEM_REF:
1364 case CEE_STELEM_REF:
1365 return mono_defaults.object_class;
1366 default:
1367 g_assert_not_reached ();
1369 return NULL;
1373 * We try to share variables when possible
1375 static MonoInst *
1376 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1378 MonoInst *res;
1379 int pos, vnum;
1381 /* inlining can result in deeper stacks */
1382 if (slot >= cfg->header->max_stack)
1383 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1385 pos = ins->type - 1 + slot * STACK_MAX;
1387 switch (ins->type) {
1388 case STACK_I4:
1389 case STACK_I8:
1390 case STACK_R8:
1391 case STACK_PTR:
1392 case STACK_MP:
1393 case STACK_OBJ:
1394 if ((vnum = cfg->intvars [pos]))
1395 return cfg->varinfo [vnum];
1396 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1397 cfg->intvars [pos] = res->inst_c0;
1398 break;
1399 default:
1400 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1402 return res;
1405 static void
1406 mono_save_token_info (MonoCompile *cfg, MonoImage *image, guint32 token, gpointer key)
1409 * Don't use this if a generic_context is set, since that means AOT can't
1410 * look up the method using just the image+token.
1411 * table == 0 means this is a reference made from a wrapper.
1413 if (cfg->compile_aot && !cfg->generic_context && (mono_metadata_token_table (token) > 0)) {
1414 MonoJumpInfoToken *jump_info_token = (MonoJumpInfoToken *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoToken));
1415 jump_info_token->image = image;
1416 jump_info_token->token = token;
1417 g_hash_table_insert (cfg->token_info_hash, key, jump_info_token);
1422 * This function is called to handle items that are left on the evaluation stack
1423 * at basic block boundaries. What happens is that we save the values to local variables
1424 * and we reload them later when first entering the target basic block (with the
1425 * handle_loaded_temps () function).
1426 * A single joint point will use the same variables (stored in the array bb->out_stack or
1427 * bb->in_stack, if the basic block is before or after the joint point).
1429 * This function needs to be called _before_ emitting the last instruction of
1430 * the bb (i.e. before emitting a branch).
1431 * If the stack merge fails at a join point, cfg->unverifiable is set.
1433 static void
1434 handle_stack_args (MonoCompile *cfg, MonoInst **sp, int count)
1436 int i, bindex;
1437 MonoBasicBlock *bb = cfg->cbb;
1438 MonoBasicBlock *outb;
1439 MonoInst *inst, **locals;
1440 gboolean found;
1442 if (!count)
1443 return;
1444 if (cfg->verbose_level > 3)
1445 printf ("%d item(s) on exit from B%d\n", count, bb->block_num);
1446 if (!bb->out_scount) {
1447 bb->out_scount = count;
1448 //printf ("bblock %d has out:", bb->block_num);
1449 found = FALSE;
1450 for (i = 0; i < bb->out_count; ++i) {
1451 outb = bb->out_bb [i];
1452 /* exception handlers are linked, but they should not be considered for stack args */
1453 if (outb->flags & BB_EXCEPTION_HANDLER)
1454 continue;
1455 //printf (" %d", outb->block_num);
1456 if (outb->in_stack) {
1457 found = TRUE;
1458 bb->out_stack = outb->in_stack;
1459 break;
1462 //printf ("\n");
1463 if (!found) {
1464 bb->out_stack = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1465 for (i = 0; i < count; ++i) {
1467 * try to reuse temps already allocated for this purpouse, if they occupy the same
1468 * stack slot and if they are of the same type.
1469 * This won't cause conflicts since if 'local' is used to
1470 * store one of the values in the in_stack of a bblock, then
1471 * the same variable will be used for the same outgoing stack
1472 * slot as well.
1473 * This doesn't work when inlining methods, since the bblocks
1474 * in the inlined methods do not inherit their in_stack from
1475 * the bblock they are inlined to. See bug #58863 for an
1476 * example.
1478 if (cfg->inlined_method)
1479 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1480 else
1481 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1486 for (i = 0; i < bb->out_count; ++i) {
1487 outb = bb->out_bb [i];
1488 /* exception handlers are linked, but they should not be considered for stack args */
1489 if (outb->flags & BB_EXCEPTION_HANDLER)
1490 continue;
1491 if (outb->in_scount) {
1492 if (outb->in_scount != bb->out_scount) {
1493 cfg->unverifiable = TRUE;
1494 return;
1496 continue; /* check they are the same locals */
1498 outb->in_scount = count;
1499 outb->in_stack = bb->out_stack;
1502 locals = bb->out_stack;
1503 cfg->cbb = bb;
1504 for (i = 0; i < count; ++i) {
1505 EMIT_NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1506 inst->cil_code = sp [i]->cil_code;
1507 sp [i] = locals [i];
1508 if (cfg->verbose_level > 3)
1509 printf ("storing %d to temp %d\n", i, (int)locals [i]->inst_c0);
1513 * It is possible that the out bblocks already have in_stack assigned, and
1514 * the in_stacks differ. In this case, we will store to all the different
1515 * in_stacks.
1518 found = TRUE;
1519 bindex = 0;
1520 while (found) {
1521 /* Find a bblock which has a different in_stack */
1522 found = FALSE;
1523 while (bindex < bb->out_count) {
1524 outb = bb->out_bb [bindex];
1525 /* exception handlers are linked, but they should not be considered for stack args */
1526 if (outb->flags & BB_EXCEPTION_HANDLER) {
1527 bindex++;
1528 continue;
1530 if (outb->in_stack != locals) {
1531 for (i = 0; i < count; ++i) {
1532 EMIT_NEW_TEMPSTORE (cfg, inst, outb->in_stack [i]->inst_c0, sp [i]);
1533 inst->cil_code = sp [i]->cil_code;
1534 sp [i] = locals [i];
1535 if (cfg->verbose_level > 3)
1536 printf ("storing %d to temp %d\n", i, (int)outb->in_stack [i]->inst_c0);
1538 locals = outb->in_stack;
1539 found = TRUE;
1540 break;
1542 bindex ++;
1547 static MonoInst*
1548 emit_runtime_constant (MonoCompile *cfg, MonoJumpInfoType patch_type, gpointer data)
1550 MonoInst *ins;
1552 if (cfg->compile_aot) {
1553 EMIT_NEW_AOTCONST (cfg, ins, patch_type, data);
1554 } else {
1555 MonoJumpInfo ji;
1556 gpointer target;
1557 MonoError error;
1559 ji.type = patch_type;
1560 ji.data.target = data;
1561 target = mono_resolve_patch_target (NULL, cfg->domain, NULL, &ji, FALSE, &error);
1562 mono_error_assert_ok (&error);
1564 EMIT_NEW_PCONST (cfg, ins, target);
1566 return ins;
1569 static void
1570 mini_emit_interface_bitmap_check (MonoCompile *cfg, int intf_bit_reg, int base_reg, int offset, MonoClass *klass)
1572 int ibitmap_reg = alloc_preg (cfg);
1573 #ifdef COMPRESSED_INTERFACE_BITMAP
1574 MonoInst *args [2];
1575 MonoInst *res, *ins;
1576 NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, ibitmap_reg, base_reg, offset);
1577 MONO_ADD_INS (cfg->cbb, ins);
1578 args [0] = ins;
1579 args [1] = emit_runtime_constant (cfg, MONO_PATCH_INFO_IID, klass);
1580 res = mono_emit_jit_icall (cfg, mono_class_interface_match, args);
1581 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, intf_bit_reg, res->dreg);
1582 #else
1583 int ibitmap_byte_reg = alloc_preg (cfg);
1585 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, ibitmap_reg, base_reg, offset);
1587 if (cfg->compile_aot) {
1588 int iid_reg = alloc_preg (cfg);
1589 int shifted_iid_reg = alloc_preg (cfg);
1590 int ibitmap_byte_address_reg = alloc_preg (cfg);
1591 int masked_iid_reg = alloc_preg (cfg);
1592 int iid_one_bit_reg = alloc_preg (cfg);
1593 int iid_bit_reg = alloc_preg (cfg);
1594 MONO_EMIT_NEW_AOTCONST (cfg, iid_reg, klass, MONO_PATCH_INFO_IID);
1595 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHR_IMM, shifted_iid_reg, iid_reg, 3);
1596 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, ibitmap_byte_address_reg, ibitmap_reg, shifted_iid_reg);
1597 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, ibitmap_byte_reg, ibitmap_byte_address_reg, 0);
1598 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, masked_iid_reg, iid_reg, 7);
1599 MONO_EMIT_NEW_ICONST (cfg, iid_one_bit_reg, 1);
1600 MONO_EMIT_NEW_BIALU (cfg, OP_ISHL, iid_bit_reg, iid_one_bit_reg, masked_iid_reg);
1601 MONO_EMIT_NEW_BIALU (cfg, OP_IAND, intf_bit_reg, ibitmap_byte_reg, iid_bit_reg);
1602 } else {
1603 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, ibitmap_byte_reg, ibitmap_reg, klass->interface_id >> 3);
1604 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_AND_IMM, intf_bit_reg, ibitmap_byte_reg, 1 << (klass->interface_id & 7));
1606 #endif
1610 * Emit code which loads into "intf_bit_reg" a nonzero value if the MonoClass
1611 * stored in "klass_reg" implements the interface "klass".
1613 static void
1614 mini_emit_load_intf_bit_reg_class (MonoCompile *cfg, int intf_bit_reg, int klass_reg, MonoClass *klass)
1616 mini_emit_interface_bitmap_check (cfg, intf_bit_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, interface_bitmap), klass);
1620 * Emit code which loads into "intf_bit_reg" a nonzero value if the MonoVTable
1621 * stored in "vtable_reg" implements the interface "klass".
1623 static void
1624 mini_emit_load_intf_bit_reg_vtable (MonoCompile *cfg, int intf_bit_reg, int vtable_reg, MonoClass *klass)
1626 mini_emit_interface_bitmap_check (cfg, intf_bit_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, interface_bitmap), klass);
1630 * Emit code which checks whenever the interface id of @klass is smaller than
1631 * than the value given by max_iid_reg.
1633 static void
1634 mini_emit_max_iid_check (MonoCompile *cfg, int max_iid_reg, MonoClass *klass,
1635 MonoBasicBlock *false_target)
1637 if (cfg->compile_aot) {
1638 int iid_reg = alloc_preg (cfg);
1639 MONO_EMIT_NEW_AOTCONST (cfg, iid_reg, klass, MONO_PATCH_INFO_IID);
1640 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, max_iid_reg, iid_reg);
1642 else
1643 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, max_iid_reg, klass->interface_id);
1644 if (false_target)
1645 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBLT_UN, false_target);
1646 else
1647 MONO_EMIT_NEW_COND_EXC (cfg, LT_UN, "InvalidCastException");
1650 /* Same as above, but obtains max_iid from a vtable */
1651 static void
1652 mini_emit_max_iid_check_vtable (MonoCompile *cfg, int vtable_reg, MonoClass *klass,
1653 MonoBasicBlock *false_target)
1655 int max_iid_reg = alloc_preg (cfg);
1657 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, max_iid_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, max_interface_id));
1658 mini_emit_max_iid_check (cfg, max_iid_reg, klass, false_target);
1661 /* Same as above, but obtains max_iid from a klass */
1662 static void
1663 mini_emit_max_iid_check_class (MonoCompile *cfg, int klass_reg, MonoClass *klass,
1664 MonoBasicBlock *false_target)
1666 int max_iid_reg = alloc_preg (cfg);
1668 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, max_iid_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, max_interface_id));
1669 mini_emit_max_iid_check (cfg, max_iid_reg, klass, false_target);
1672 static void
1673 mini_emit_isninst_cast_inst (MonoCompile *cfg, int klass_reg, MonoClass *klass, MonoInst *klass_ins, MonoBasicBlock *false_target, MonoBasicBlock *true_target)
1675 int idepth_reg = alloc_preg (cfg);
1676 int stypes_reg = alloc_preg (cfg);
1677 int stype = alloc_preg (cfg);
1679 mono_class_setup_supertypes (klass);
1681 if (klass->idepth > MONO_DEFAULT_SUPERTABLE_SIZE) {
1682 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, idepth_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, idepth));
1683 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, idepth_reg, klass->idepth);
1684 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBLT_UN, false_target);
1686 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, stypes_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, supertypes));
1687 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, stype, stypes_reg, ((klass->idepth - 1) * SIZEOF_VOID_P));
1688 if (klass_ins) {
1689 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, stype, klass_ins->dreg);
1690 } else if (cfg->compile_aot) {
1691 int const_reg = alloc_preg (cfg);
1692 MONO_EMIT_NEW_CLASSCONST (cfg, const_reg, klass);
1693 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, stype, const_reg);
1694 } else {
1695 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, stype, klass);
1697 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, true_target);
1700 static void
1701 mini_emit_isninst_cast (MonoCompile *cfg, int klass_reg, MonoClass *klass, MonoBasicBlock *false_target, MonoBasicBlock *true_target)
1703 mini_emit_isninst_cast_inst (cfg, klass_reg, klass, NULL, false_target, true_target);
1706 static void
1707 mini_emit_iface_cast (MonoCompile *cfg, int vtable_reg, MonoClass *klass, MonoBasicBlock *false_target, MonoBasicBlock *true_target)
1709 int intf_reg = alloc_preg (cfg);
1711 mini_emit_max_iid_check_vtable (cfg, vtable_reg, klass, false_target);
1712 mini_emit_load_intf_bit_reg_vtable (cfg, intf_reg, vtable_reg, klass);
1713 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, intf_reg, 0);
1714 if (true_target)
1715 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, true_target);
1716 else
1717 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "InvalidCastException");
1721 * Variant of the above that takes a register to the class, not the vtable.
1723 static void
1724 mini_emit_iface_class_cast (MonoCompile *cfg, int klass_reg, MonoClass *klass, MonoBasicBlock *false_target, MonoBasicBlock *true_target)
1726 int intf_bit_reg = alloc_preg (cfg);
1728 mini_emit_max_iid_check_class (cfg, klass_reg, klass, false_target);
1729 mini_emit_load_intf_bit_reg_class (cfg, intf_bit_reg, klass_reg, klass);
1730 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, intf_bit_reg, 0);
1731 if (true_target)
1732 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, true_target);
1733 else
1734 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "InvalidCastException");
1737 static inline void
1738 mini_emit_class_check_inst (MonoCompile *cfg, int klass_reg, MonoClass *klass, MonoInst *klass_inst)
1740 if (klass_inst) {
1741 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, klass_inst->dreg);
1742 } else {
1743 MonoInst *ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_CLASS, klass);
1744 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, ins->dreg);
1746 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
1749 static inline void
1750 mini_emit_class_check (MonoCompile *cfg, int klass_reg, MonoClass *klass)
1752 mini_emit_class_check_inst (cfg, klass_reg, klass, NULL);
1755 static inline void
1756 mini_emit_class_check_branch (MonoCompile *cfg, int klass_reg, MonoClass *klass, int branch_op, MonoBasicBlock *target)
1758 if (cfg->compile_aot) {
1759 int const_reg = alloc_preg (cfg);
1760 MONO_EMIT_NEW_CLASSCONST (cfg, const_reg, klass);
1761 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, const_reg);
1762 } else {
1763 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, klass_reg, klass);
1765 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, branch_op, target);
1768 static void
1769 mini_emit_castclass (MonoCompile *cfg, int obj_reg, int klass_reg, MonoClass *klass, MonoBasicBlock *object_is_null);
1771 static void
1772 mini_emit_castclass_inst (MonoCompile *cfg, int obj_reg, int klass_reg, MonoClass *klass, MonoInst *klass_inst, MonoBasicBlock *object_is_null)
1774 if (klass->rank) {
1775 int rank_reg = alloc_preg (cfg);
1776 int eclass_reg = alloc_preg (cfg);
1778 g_assert (!klass_inst);
1779 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, rank_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, rank));
1780 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rank_reg, klass->rank);
1781 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
1782 // MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
1783 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, eclass_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, cast_class));
1784 if (klass->cast_class == mono_defaults.object_class) {
1785 int parent_reg = alloc_preg (cfg);
1786 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, parent_reg, eclass_reg, MONO_STRUCT_OFFSET (MonoClass, parent));
1787 mini_emit_class_check_branch (cfg, parent_reg, mono_defaults.enum_class->parent, OP_PBNE_UN, object_is_null);
1788 mini_emit_class_check (cfg, eclass_reg, mono_defaults.enum_class);
1789 } else if (klass->cast_class == mono_defaults.enum_class->parent) {
1790 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class->parent, OP_PBEQ, object_is_null);
1791 mini_emit_class_check (cfg, eclass_reg, mono_defaults.enum_class);
1792 } else if (klass->cast_class == mono_defaults.enum_class) {
1793 mini_emit_class_check (cfg, eclass_reg, mono_defaults.enum_class);
1794 } else if (klass->cast_class->flags & TYPE_ATTRIBUTE_INTERFACE) {
1795 mini_emit_iface_class_cast (cfg, eclass_reg, klass->cast_class, NULL, NULL);
1796 } else {
1797 // Pass -1 as obj_reg to skip the check below for arrays of arrays
1798 mini_emit_castclass (cfg, -1, eclass_reg, klass->cast_class, object_is_null);
1801 if ((klass->rank == 1) && (klass->byval_arg.type == MONO_TYPE_SZARRAY) && (obj_reg != -1)) {
1802 /* Check that the object is a vector too */
1803 int bounds_reg = alloc_preg (cfg);
1804 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg, obj_reg, MONO_STRUCT_OFFSET (MonoArray, bounds));
1805 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, bounds_reg, 0);
1806 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
1808 } else {
1809 int idepth_reg = alloc_preg (cfg);
1810 int stypes_reg = alloc_preg (cfg);
1811 int stype = alloc_preg (cfg);
1813 mono_class_setup_supertypes (klass);
1815 if (klass->idepth > MONO_DEFAULT_SUPERTABLE_SIZE) {
1816 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, idepth_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, idepth));
1817 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, idepth_reg, klass->idepth);
1818 MONO_EMIT_NEW_COND_EXC (cfg, LT_UN, "InvalidCastException");
1820 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, stypes_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, supertypes));
1821 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, stype, stypes_reg, ((klass->idepth - 1) * SIZEOF_VOID_P));
1822 mini_emit_class_check_inst (cfg, stype, klass, klass_inst);
1826 static void
1827 mini_emit_castclass (MonoCompile *cfg, int obj_reg, int klass_reg, MonoClass *klass, MonoBasicBlock *object_is_null)
1829 mini_emit_castclass_inst (cfg, obj_reg, klass_reg, klass, NULL, object_is_null);
1832 static void
1833 mini_emit_memset (MonoCompile *cfg, int destreg, int offset, int size, int val, int align)
1835 int val_reg;
1837 g_assert (val == 0);
1839 if (align == 0)
1840 align = 4;
1842 if ((size <= SIZEOF_REGISTER) && (size <= align)) {
1843 switch (size) {
1844 case 1:
1845 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI1_MEMBASE_IMM, destreg, offset, val);
1846 return;
1847 case 2:
1848 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI2_MEMBASE_IMM, destreg, offset, val);
1849 return;
1850 case 4:
1851 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI4_MEMBASE_IMM, destreg, offset, val);
1852 return;
1853 #if SIZEOF_REGISTER == 8
1854 case 8:
1855 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI8_MEMBASE_IMM, destreg, offset, val);
1856 return;
1857 #endif
1861 val_reg = alloc_preg (cfg);
1863 if (SIZEOF_REGISTER == 8)
1864 MONO_EMIT_NEW_I8CONST (cfg, val_reg, val);
1865 else
1866 MONO_EMIT_NEW_ICONST (cfg, val_reg, val);
1868 if (align < 4) {
1869 /* This could be optimized further if neccesary */
1870 while (size >= 1) {
1871 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, offset, val_reg);
1872 offset += 1;
1873 size -= 1;
1875 return;
1878 if (!cfg->backend->no_unaligned_access && SIZEOF_REGISTER == 8) {
1879 if (offset % 8) {
1880 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, offset, val_reg);
1881 offset += 4;
1882 size -= 4;
1884 while (size >= 8) {
1885 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI8_MEMBASE_REG, destreg, offset, val_reg);
1886 offset += 8;
1887 size -= 8;
1891 while (size >= 4) {
1892 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, offset, val_reg);
1893 offset += 4;
1894 size -= 4;
1896 while (size >= 2) {
1897 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI2_MEMBASE_REG, destreg, offset, val_reg);
1898 offset += 2;
1899 size -= 2;
1901 while (size >= 1) {
1902 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, offset, val_reg);
1903 offset += 1;
1904 size -= 1;
1908 void
1909 mini_emit_memcpy (MonoCompile *cfg, int destreg, int doffset, int srcreg, int soffset, int size, int align)
1911 int cur_reg;
1913 if (align == 0)
1914 align = 4;
1916 /*FIXME arbitrary hack to avoid unbound code expansion.*/
1917 g_assert (size < 10000);
1919 if (align < 4) {
1920 /* This could be optimized further if neccesary */
1921 while (size >= 1) {
1922 cur_reg = alloc_preg (cfg);
1923 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, cur_reg, srcreg, soffset);
1924 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, doffset, cur_reg);
1925 doffset += 1;
1926 soffset += 1;
1927 size -= 1;
1931 if (!cfg->backend->no_unaligned_access && SIZEOF_REGISTER == 8) {
1932 while (size >= 8) {
1933 cur_reg = alloc_preg (cfg);
1934 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI8_MEMBASE, cur_reg, srcreg, soffset);
1935 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI8_MEMBASE_REG, destreg, doffset, cur_reg);
1936 doffset += 8;
1937 soffset += 8;
1938 size -= 8;
1942 while (size >= 4) {
1943 cur_reg = alloc_preg (cfg);
1944 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, cur_reg, srcreg, soffset);
1945 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, doffset, cur_reg);
1946 doffset += 4;
1947 soffset += 4;
1948 size -= 4;
1950 while (size >= 2) {
1951 cur_reg = alloc_preg (cfg);
1952 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI2_MEMBASE, cur_reg, srcreg, soffset);
1953 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI2_MEMBASE_REG, destreg, doffset, cur_reg);
1954 doffset += 2;
1955 soffset += 2;
1956 size -= 2;
1958 while (size >= 1) {
1959 cur_reg = alloc_preg (cfg);
1960 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, cur_reg, srcreg, soffset);
1961 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, doffset, cur_reg);
1962 doffset += 1;
1963 soffset += 1;
1964 size -= 1;
1968 static void
1969 emit_tls_set (MonoCompile *cfg, int sreg1, MonoTlsKey tls_key)
1971 MonoInst *ins, *c;
1973 if (cfg->compile_aot) {
1974 EMIT_NEW_TLS_OFFSETCONST (cfg, c, tls_key);
1975 MONO_INST_NEW (cfg, ins, OP_TLS_SET_REG);
1976 ins->sreg1 = sreg1;
1977 ins->sreg2 = c->dreg;
1978 MONO_ADD_INS (cfg->cbb, ins);
1979 } else {
1980 MONO_INST_NEW (cfg, ins, OP_TLS_SET);
1981 ins->sreg1 = sreg1;
1982 ins->inst_offset = mini_get_tls_offset (tls_key);
1983 MONO_ADD_INS (cfg->cbb, ins);
1988 * emit_push_lmf:
1990 * Emit IR to push the current LMF onto the LMF stack.
1992 static void
1993 emit_push_lmf (MonoCompile *cfg)
1996 * Emit IR to push the LMF:
1997 * lmf_addr = <lmf_addr from tls>
1998 * lmf->lmf_addr = lmf_addr
1999 * lmf->prev_lmf = *lmf_addr
2000 * *lmf_addr = lmf
2002 int lmf_reg, prev_lmf_reg;
2003 MonoInst *ins, *lmf_ins;
2005 if (!cfg->lmf_ir)
2006 return;
2008 if (cfg->lmf_ir_mono_lmf && mini_tls_get_supported (cfg, TLS_KEY_LMF)) {
2009 /* Load current lmf */
2010 lmf_ins = mono_get_lmf_intrinsic (cfg);
2011 g_assert (lmf_ins);
2012 MONO_ADD_INS (cfg->cbb, lmf_ins);
2013 EMIT_NEW_VARLOADA (cfg, ins, cfg->lmf_var, NULL);
2014 lmf_reg = ins->dreg;
2015 /* Save previous_lmf */
2016 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), lmf_ins->dreg);
2017 /* Set new LMF */
2018 emit_tls_set (cfg, lmf_reg, TLS_KEY_LMF);
2019 } else {
2021 * Store lmf_addr in a variable, so it can be allocated to a global register.
2023 if (!cfg->lmf_addr_var)
2024 cfg->lmf_addr_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2026 #ifdef HOST_WIN32
2027 ins = mono_get_jit_tls_intrinsic (cfg);
2028 if (ins) {
2029 int jit_tls_dreg = ins->dreg;
2031 MONO_ADD_INS (cfg->cbb, ins);
2032 lmf_reg = alloc_preg (cfg);
2033 EMIT_NEW_BIALU_IMM (cfg, lmf_ins, OP_PADD_IMM, lmf_reg, jit_tls_dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, lmf));
2034 } else {
2035 lmf_ins = mono_emit_jit_icall (cfg, mono_get_lmf_addr, NULL);
2037 #else
2038 lmf_ins = mono_get_lmf_addr_intrinsic (cfg);
2039 if (lmf_ins) {
2040 MONO_ADD_INS (cfg->cbb, lmf_ins);
2041 } else {
2042 #ifdef TARGET_IOS
2043 MonoInst *args [16], *jit_tls_ins, *ins;
2045 /* Inline mono_get_lmf_addr () */
2046 /* jit_tls = pthread_getspecific (mono_jit_tls_id); lmf_addr = &jit_tls->lmf; */
2048 /* Load mono_jit_tls_id */
2049 if (cfg->compile_aot)
2050 EMIT_NEW_AOTCONST (cfg, args [0], MONO_PATCH_INFO_JIT_TLS_ID, NULL);
2051 else
2052 EMIT_NEW_ICONST (cfg, args [0], mono_jit_tls_id);
2053 /* call pthread_getspecific () */
2054 jit_tls_ins = mono_emit_jit_icall (cfg, pthread_getspecific, args);
2055 /* lmf_addr = &jit_tls->lmf */
2056 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, cfg->lmf_addr_var->dreg, jit_tls_ins->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, lmf));
2057 lmf_ins = ins;
2058 #else
2059 lmf_ins = mono_emit_jit_icall (cfg, mono_get_lmf_addr, NULL);
2060 #endif
2062 #endif
2063 lmf_ins->dreg = cfg->lmf_addr_var->dreg;
2065 EMIT_NEW_VARLOADA (cfg, ins, cfg->lmf_var, NULL);
2066 lmf_reg = ins->dreg;
2068 prev_lmf_reg = alloc_preg (cfg);
2069 /* Save previous_lmf */
2070 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, prev_lmf_reg, cfg->lmf_addr_var->dreg, 0);
2071 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), prev_lmf_reg);
2072 /* Set new lmf */
2073 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, cfg->lmf_addr_var->dreg, 0, lmf_reg);
2078 * emit_pop_lmf:
2080 * Emit IR to pop the current LMF from the LMF stack.
2082 static void
2083 emit_pop_lmf (MonoCompile *cfg)
2085 int lmf_reg, lmf_addr_reg, prev_lmf_reg;
2086 MonoInst *ins;
2088 if (!cfg->lmf_ir)
2089 return;
2091 EMIT_NEW_VARLOADA (cfg, ins, cfg->lmf_var, NULL);
2092 lmf_reg = ins->dreg;
2094 if (cfg->lmf_ir_mono_lmf && mini_tls_get_supported (cfg, TLS_KEY_LMF)) {
2095 /* Load previous_lmf */
2096 prev_lmf_reg = alloc_preg (cfg);
2097 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, prev_lmf_reg, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
2098 /* Set new LMF */
2099 emit_tls_set (cfg, prev_lmf_reg, TLS_KEY_LMF);
2100 } else {
2102 * Emit IR to pop the LMF:
2103 * *(lmf->lmf_addr) = lmf->prev_lmf
2105 /* This could be called before emit_push_lmf () */
2106 if (!cfg->lmf_addr_var)
2107 cfg->lmf_addr_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2108 lmf_addr_reg = cfg->lmf_addr_var->dreg;
2110 prev_lmf_reg = alloc_preg (cfg);
2111 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, prev_lmf_reg, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
2112 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, lmf_addr_reg, 0, prev_lmf_reg);
2116 static void
2117 emit_instrumentation_call (MonoCompile *cfg, void *func)
2119 MonoInst *iargs [1];
2122 * Avoid instrumenting inlined methods since it can
2123 * distort profiling results.
2125 if (cfg->method != cfg->current_method)
2126 return;
2128 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE) {
2129 EMIT_NEW_METHODCONST (cfg, iargs [0], cfg->method);
2130 mono_emit_jit_icall (cfg, func, iargs);
2134 static int
2135 ret_type_to_call_opcode (MonoCompile *cfg, MonoType *type, int calli, int virt)
2137 handle_enum:
2138 type = mini_get_underlying_type (type);
2139 switch (type->type) {
2140 case MONO_TYPE_VOID:
2141 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALL_MEMBASE: OP_VOIDCALL;
2142 case MONO_TYPE_I1:
2143 case MONO_TYPE_U1:
2144 case MONO_TYPE_I2:
2145 case MONO_TYPE_U2:
2146 case MONO_TYPE_I4:
2147 case MONO_TYPE_U4:
2148 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
2149 case MONO_TYPE_I:
2150 case MONO_TYPE_U:
2151 case MONO_TYPE_PTR:
2152 case MONO_TYPE_FNPTR:
2153 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
2154 case MONO_TYPE_CLASS:
2155 case MONO_TYPE_STRING:
2156 case MONO_TYPE_OBJECT:
2157 case MONO_TYPE_SZARRAY:
2158 case MONO_TYPE_ARRAY:
2159 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
2160 case MONO_TYPE_I8:
2161 case MONO_TYPE_U8:
2162 return calli? OP_LCALL_REG: virt? OP_LCALL_MEMBASE: OP_LCALL;
2163 case MONO_TYPE_R4:
2164 if (cfg->r4fp)
2165 return calli? OP_RCALL_REG: virt? OP_RCALL_MEMBASE: OP_RCALL;
2166 else
2167 return calli? OP_FCALL_REG: virt? OP_FCALL_MEMBASE: OP_FCALL;
2168 case MONO_TYPE_R8:
2169 return calli? OP_FCALL_REG: virt? OP_FCALL_MEMBASE: OP_FCALL;
2170 case MONO_TYPE_VALUETYPE:
2171 if (type->data.klass->enumtype) {
2172 type = mono_class_enum_basetype (type->data.klass);
2173 goto handle_enum;
2174 } else
2175 return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
2176 case MONO_TYPE_TYPEDBYREF:
2177 return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
2178 case MONO_TYPE_GENERICINST:
2179 type = &type->data.generic_class->container_class->byval_arg;
2180 goto handle_enum;
2181 case MONO_TYPE_VAR:
2182 case MONO_TYPE_MVAR:
2183 /* gsharedvt */
2184 return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
2185 default:
2186 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
2188 return -1;
2191 //XXX this ignores if t is byref
2192 #define MONO_TYPE_IS_PRIMITIVE_SCALAR(t) ((((((t)->type >= MONO_TYPE_BOOLEAN && (t)->type <= MONO_TYPE_U8) || ((t)->type >= MONO_TYPE_I && (t)->type <= MONO_TYPE_U)))))
2195 * target_type_is_incompatible:
2196 * @cfg: MonoCompile context
2198 * Check that the item @arg on the evaluation stack can be stored
2199 * in the target type (can be a local, or field, etc).
2200 * The cfg arg can be used to check if we need verification or just
2201 * validity checks.
2203 * Returns: non-0 value if arg can't be stored on a target.
2205 static int
2206 target_type_is_incompatible (MonoCompile *cfg, MonoType *target, MonoInst *arg)
2208 MonoType *simple_type;
2209 MonoClass *klass;
2211 if (target->byref) {
2212 /* FIXME: check that the pointed to types match */
2213 if (arg->type == STACK_MP) {
2214 if (cfg->verbose_level) printf ("ok\n");
2215 /* This is needed to handle gshared types + ldaddr. We lower the types so we can handle enums and other typedef-like types. */
2216 MonoClass *target_class_lowered = mono_class_from_mono_type (mini_get_underlying_type (&mono_class_from_mono_type (target)->byval_arg));
2217 MonoClass *source_class_lowered = mono_class_from_mono_type (mini_get_underlying_type (&arg->klass->byval_arg));
2219 /* if the target is native int& or same type */
2220 if (target->type == MONO_TYPE_I || target_class_lowered == source_class_lowered)
2221 return 0;
2223 /* Both are primitive type byrefs and the source points to a larger type that the destination */
2224 if (MONO_TYPE_IS_PRIMITIVE_SCALAR (&target_class_lowered->byval_arg) && MONO_TYPE_IS_PRIMITIVE_SCALAR (&source_class_lowered->byval_arg) &&
2225 mono_class_instance_size (target_class_lowered) <= mono_class_instance_size (source_class_lowered))
2226 return 0;
2227 return 1;
2229 if (arg->type == STACK_PTR)
2230 return 0;
2231 return 1;
2234 simple_type = mini_get_underlying_type (target);
2235 switch (simple_type->type) {
2236 case MONO_TYPE_VOID:
2237 return 1;
2238 case MONO_TYPE_I1:
2239 case MONO_TYPE_U1:
2240 case MONO_TYPE_I2:
2241 case MONO_TYPE_U2:
2242 case MONO_TYPE_I4:
2243 case MONO_TYPE_U4:
2244 if (arg->type != STACK_I4 && arg->type != STACK_PTR)
2245 return 1;
2246 return 0;
2247 case MONO_TYPE_PTR:
2248 /* STACK_MP is needed when setting pinned locals */
2249 if (arg->type != STACK_I4 && arg->type != STACK_PTR && arg->type != STACK_MP)
2250 return 1;
2251 return 0;
2252 case MONO_TYPE_I:
2253 case MONO_TYPE_U:
2254 case MONO_TYPE_FNPTR:
2256 * Some opcodes like ldloca returns 'transient pointers' which can be stored in
2257 * in native int. (#688008).
2259 if (arg->type != STACK_I4 && arg->type != STACK_PTR && arg->type != STACK_MP)
2260 return 1;
2261 return 0;
2262 case MONO_TYPE_CLASS:
2263 case MONO_TYPE_STRING:
2264 case MONO_TYPE_OBJECT:
2265 case MONO_TYPE_SZARRAY:
2266 case MONO_TYPE_ARRAY:
2267 if (arg->type != STACK_OBJ)
2268 return 1;
2269 /* FIXME: check type compatibility */
2270 return 0;
2271 case MONO_TYPE_I8:
2272 case MONO_TYPE_U8:
2273 if (arg->type != STACK_I8)
2274 return 1;
2275 return 0;
2276 case MONO_TYPE_R4:
2277 if (arg->type != cfg->r4_stack_type)
2278 return 1;
2279 return 0;
2280 case MONO_TYPE_R8:
2281 if (arg->type != STACK_R8)
2282 return 1;
2283 return 0;
2284 case MONO_TYPE_VALUETYPE:
2285 if (arg->type != STACK_VTYPE)
2286 return 1;
2287 klass = mono_class_from_mono_type (simple_type);
2288 if (klass != arg->klass)
2289 return 1;
2290 return 0;
2291 case MONO_TYPE_TYPEDBYREF:
2292 if (arg->type != STACK_VTYPE)
2293 return 1;
2294 klass = mono_class_from_mono_type (simple_type);
2295 if (klass != arg->klass)
2296 return 1;
2297 return 0;
2298 case MONO_TYPE_GENERICINST:
2299 if (mono_type_generic_inst_is_valuetype (simple_type)) {
2300 MonoClass *target_class;
2301 if (arg->type != STACK_VTYPE)
2302 return 1;
2303 klass = mono_class_from_mono_type (simple_type);
2304 target_class = mono_class_from_mono_type (target);
2305 /* The second cases is needed when doing partial sharing */
2306 if (klass != arg->klass && target_class != arg->klass && target_class != mono_class_from_mono_type (mini_get_underlying_type (&arg->klass->byval_arg)))
2307 return 1;
2308 return 0;
2309 } else {
2310 if (arg->type != STACK_OBJ)
2311 return 1;
2312 /* FIXME: check type compatibility */
2313 return 0;
2315 case MONO_TYPE_VAR:
2316 case MONO_TYPE_MVAR:
2317 g_assert (cfg->gshared);
2318 if (mini_type_var_is_vt (simple_type)) {
2319 if (arg->type != STACK_VTYPE)
2320 return 1;
2321 } else {
2322 if (arg->type != STACK_OBJ)
2323 return 1;
2325 return 0;
2326 default:
2327 g_error ("unknown type 0x%02x in target_type_is_incompatible", simple_type->type);
2329 return 1;
2333 * Prepare arguments for passing to a function call.
2334 * Return a non-zero value if the arguments can't be passed to the given
2335 * signature.
2336 * The type checks are not yet complete and some conversions may need
2337 * casts on 32 or 64 bit architectures.
2339 * FIXME: implement this using target_type_is_incompatible ()
2341 static int
2342 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
2344 MonoType *simple_type;
2345 int i;
2347 if (sig->hasthis) {
2348 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
2349 return 1;
2350 args++;
2352 for (i = 0; i < sig->param_count; ++i) {
2353 if (sig->params [i]->byref) {
2354 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
2355 return 1;
2356 continue;
2358 simple_type = mini_get_underlying_type (sig->params [i]);
2359 handle_enum:
2360 switch (simple_type->type) {
2361 case MONO_TYPE_VOID:
2362 return 1;
2363 continue;
2364 case MONO_TYPE_I1:
2365 case MONO_TYPE_U1:
2366 case MONO_TYPE_I2:
2367 case MONO_TYPE_U2:
2368 case MONO_TYPE_I4:
2369 case MONO_TYPE_U4:
2370 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
2371 return 1;
2372 continue;
2373 case MONO_TYPE_I:
2374 case MONO_TYPE_U:
2375 case MONO_TYPE_PTR:
2376 case MONO_TYPE_FNPTR:
2377 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
2378 return 1;
2379 continue;
2380 case MONO_TYPE_CLASS:
2381 case MONO_TYPE_STRING:
2382 case MONO_TYPE_OBJECT:
2383 case MONO_TYPE_SZARRAY:
2384 case MONO_TYPE_ARRAY:
2385 if (args [i]->type != STACK_OBJ)
2386 return 1;
2387 continue;
2388 case MONO_TYPE_I8:
2389 case MONO_TYPE_U8:
2390 if (args [i]->type != STACK_I8)
2391 return 1;
2392 continue;
2393 case MONO_TYPE_R4:
2394 if (args [i]->type != cfg->r4_stack_type)
2395 return 1;
2396 continue;
2397 case MONO_TYPE_R8:
2398 if (args [i]->type != STACK_R8)
2399 return 1;
2400 continue;
2401 case MONO_TYPE_VALUETYPE:
2402 if (simple_type->data.klass->enumtype) {
2403 simple_type = mono_class_enum_basetype (simple_type->data.klass);
2404 goto handle_enum;
2406 if (args [i]->type != STACK_VTYPE)
2407 return 1;
2408 continue;
2409 case MONO_TYPE_TYPEDBYREF:
2410 if (args [i]->type != STACK_VTYPE)
2411 return 1;
2412 continue;
2413 case MONO_TYPE_GENERICINST:
2414 simple_type = &simple_type->data.generic_class->container_class->byval_arg;
2415 goto handle_enum;
2416 case MONO_TYPE_VAR:
2417 case MONO_TYPE_MVAR:
2418 /* gsharedvt */
2419 if (args [i]->type != STACK_VTYPE)
2420 return 1;
2421 continue;
2422 default:
2423 g_error ("unknown type 0x%02x in check_call_signature",
2424 simple_type->type);
2427 return 0;
2430 static int
2431 callvirt_to_call (int opcode)
2433 switch (opcode) {
2434 case OP_CALL_MEMBASE:
2435 return OP_CALL;
2436 case OP_VOIDCALL_MEMBASE:
2437 return OP_VOIDCALL;
2438 case OP_FCALL_MEMBASE:
2439 return OP_FCALL;
2440 case OP_RCALL_MEMBASE:
2441 return OP_RCALL;
2442 case OP_VCALL_MEMBASE:
2443 return OP_VCALL;
2444 case OP_LCALL_MEMBASE:
2445 return OP_LCALL;
2446 default:
2447 g_assert_not_reached ();
2450 return -1;
2453 static int
2454 callvirt_to_call_reg (int opcode)
2456 switch (opcode) {
2457 case OP_CALL_MEMBASE:
2458 return OP_CALL_REG;
2459 case OP_VOIDCALL_MEMBASE:
2460 return OP_VOIDCALL_REG;
2461 case OP_FCALL_MEMBASE:
2462 return OP_FCALL_REG;
2463 case OP_RCALL_MEMBASE:
2464 return OP_RCALL_REG;
2465 case OP_VCALL_MEMBASE:
2466 return OP_VCALL_REG;
2467 case OP_LCALL_MEMBASE:
2468 return OP_LCALL_REG;
2469 default:
2470 g_assert_not_reached ();
2473 return -1;
2476 /* Either METHOD or IMT_ARG needs to be set */
2477 static void
2478 emit_imt_argument (MonoCompile *cfg, MonoCallInst *call, MonoMethod *method, MonoInst *imt_arg)
2480 int method_reg;
2482 if (COMPILE_LLVM (cfg)) {
2483 if (imt_arg) {
2484 method_reg = alloc_preg (cfg);
2485 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, method_reg, imt_arg->dreg);
2486 } else {
2487 MonoInst *ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_METHODCONST, method);
2488 method_reg = ins->dreg;
2491 #ifdef ENABLE_LLVM
2492 call->imt_arg_reg = method_reg;
2493 #endif
2494 mono_call_inst_add_outarg_reg (cfg, call, method_reg, MONO_ARCH_IMT_REG, FALSE);
2495 return;
2498 if (imt_arg) {
2499 method_reg = alloc_preg (cfg);
2500 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, method_reg, imt_arg->dreg);
2501 } else {
2502 MonoInst *ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_METHODCONST, method);
2503 method_reg = ins->dreg;
2506 mono_call_inst_add_outarg_reg (cfg, call, method_reg, MONO_ARCH_IMT_REG, FALSE);
2509 static MonoJumpInfo *
2510 mono_patch_info_new (MonoMemPool *mp, int ip, MonoJumpInfoType type, gconstpointer target)
2512 MonoJumpInfo *ji = (MonoJumpInfo *)mono_mempool_alloc (mp, sizeof (MonoJumpInfo));
2514 ji->ip.i = ip;
2515 ji->type = type;
2516 ji->data.target = target;
2518 return ji;
2521 static int
2522 mini_class_check_context_used (MonoCompile *cfg, MonoClass *klass)
2524 if (cfg->gshared)
2525 return mono_class_check_context_used (klass);
2526 else
2527 return 0;
2530 static int
2531 mini_method_check_context_used (MonoCompile *cfg, MonoMethod *method)
2533 if (cfg->gshared)
2534 return mono_method_check_context_used (method);
2535 else
2536 return 0;
2540 * check_method_sharing:
2542 * Check whenever the vtable or an mrgctx needs to be passed when calling CMETHOD.
2544 static void
2545 check_method_sharing (MonoCompile *cfg, MonoMethod *cmethod, gboolean *out_pass_vtable, gboolean *out_pass_mrgctx)
2547 gboolean pass_vtable = FALSE;
2548 gboolean pass_mrgctx = FALSE;
2550 if (((cmethod->flags & METHOD_ATTRIBUTE_STATIC) || cmethod->klass->valuetype) &&
2551 (cmethod->klass->generic_class || cmethod->klass->generic_container)) {
2552 gboolean sharable = FALSE;
2554 if (mono_method_is_generic_sharable_full (cmethod, TRUE, TRUE, TRUE))
2555 sharable = TRUE;
2558 * Pass vtable iff target method might
2559 * be shared, which means that sharing
2560 * is enabled for its class and its
2561 * context is sharable (and it's not a
2562 * generic method).
2564 if (sharable && !(mini_method_get_context (cmethod) && mini_method_get_context (cmethod)->method_inst))
2565 pass_vtable = TRUE;
2568 if (mini_method_get_context (cmethod) &&
2569 mini_method_get_context (cmethod)->method_inst) {
2570 g_assert (!pass_vtable);
2572 if (mono_method_is_generic_sharable_full (cmethod, TRUE, TRUE, TRUE)) {
2573 pass_mrgctx = TRUE;
2574 } else {
2575 if (cfg->gsharedvt && mini_is_gsharedvt_signature (mono_method_signature (cmethod)))
2576 pass_mrgctx = TRUE;
2580 if (out_pass_vtable)
2581 *out_pass_vtable = pass_vtable;
2582 if (out_pass_mrgctx)
2583 *out_pass_mrgctx = pass_mrgctx;
2586 inline static MonoCallInst *
2587 mono_emit_call_args (MonoCompile *cfg, MonoMethodSignature *sig,
2588 MonoInst **args, int calli, int virtual_, int tail, int rgctx, int unbox_trampoline)
2590 MonoType *sig_ret;
2591 MonoCallInst *call;
2592 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
2593 int i;
2594 #endif
2596 if (cfg->llvm_only)
2597 tail = FALSE;
2599 if (tail) {
2600 emit_instrumentation_call (cfg, mono_profiler_method_leave);
2602 MONO_INST_NEW_CALL (cfg, call, OP_TAILCALL);
2603 } else
2604 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (cfg, sig->ret, calli, virtual_));
2606 call->args = args;
2607 call->signature = sig;
2608 call->rgctx_reg = rgctx;
2609 sig_ret = mini_get_underlying_type (sig->ret);
2611 type_to_eval_stack_type ((cfg), sig_ret, &call->inst);
2613 if (tail) {
2614 if (mini_type_is_vtype (sig_ret)) {
2615 call->vret_var = cfg->vret_addr;
2616 //g_assert_not_reached ();
2618 } else if (mini_type_is_vtype (sig_ret)) {
2619 MonoInst *temp = mono_compile_create_var (cfg, sig_ret, OP_LOCAL);
2620 MonoInst *loada;
2622 temp->backend.is_pinvoke = sig->pinvoke;
2625 * We use a new opcode OP_OUTARG_VTRETADDR instead of LDADDR for emitting the
2626 * address of return value to increase optimization opportunities.
2627 * Before vtype decomposition, the dreg of the call ins itself represents the
2628 * fact the call modifies the return value. After decomposition, the call will
2629 * be transformed into one of the OP_VOIDCALL opcodes, and the VTRETADDR opcode
2630 * will be transformed into an LDADDR.
2632 MONO_INST_NEW (cfg, loada, OP_OUTARG_VTRETADDR);
2633 loada->dreg = alloc_preg (cfg);
2634 loada->inst_p0 = temp;
2635 /* We reference the call too since call->dreg could change during optimization */
2636 loada->inst_p1 = call;
2637 MONO_ADD_INS (cfg->cbb, loada);
2639 call->inst.dreg = temp->dreg;
2641 call->vret_var = loada;
2642 } else if (!MONO_TYPE_IS_VOID (sig_ret))
2643 call->inst.dreg = alloc_dreg (cfg, (MonoStackType)call->inst.type);
2645 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
2646 if (COMPILE_SOFT_FLOAT (cfg)) {
2648 * If the call has a float argument, we would need to do an r8->r4 conversion using
2649 * an icall, but that cannot be done during the call sequence since it would clobber
2650 * the call registers + the stack. So we do it before emitting the call.
2652 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2653 MonoType *t;
2654 MonoInst *in = call->args [i];
2656 if (i >= sig->hasthis)
2657 t = sig->params [i - sig->hasthis];
2658 else
2659 t = &mono_defaults.int_class->byval_arg;
2660 t = mono_type_get_underlying_type (t);
2662 if (!t->byref && t->type == MONO_TYPE_R4) {
2663 MonoInst *iargs [1];
2664 MonoInst *conv;
2666 iargs [0] = in;
2667 conv = mono_emit_jit_icall (cfg, mono_fload_r4_arg, iargs);
2669 /* The result will be in an int vreg */
2670 call->args [i] = conv;
2674 #endif
2676 call->need_unbox_trampoline = unbox_trampoline;
2678 #ifdef ENABLE_LLVM
2679 if (COMPILE_LLVM (cfg))
2680 mono_llvm_emit_call (cfg, call);
2681 else
2682 mono_arch_emit_call (cfg, call);
2683 #else
2684 mono_arch_emit_call (cfg, call);
2685 #endif
2687 cfg->param_area = MAX (cfg->param_area, call->stack_usage);
2688 cfg->flags |= MONO_CFG_HAS_CALLS;
2690 return call;
2693 static void
2694 set_rgctx_arg (MonoCompile *cfg, MonoCallInst *call, int rgctx_reg, MonoInst *rgctx_arg)
2696 mono_call_inst_add_outarg_reg (cfg, call, rgctx_reg, MONO_ARCH_RGCTX_REG, FALSE);
2697 cfg->uses_rgctx_reg = TRUE;
2698 call->rgctx_reg = TRUE;
2699 #ifdef ENABLE_LLVM
2700 call->rgctx_arg_reg = rgctx_reg;
2701 #endif
2704 inline static MonoInst*
2705 mono_emit_calli (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args, MonoInst *addr, MonoInst *imt_arg, MonoInst *rgctx_arg)
2707 MonoCallInst *call;
2708 MonoInst *ins;
2709 int rgctx_reg = -1;
2710 gboolean check_sp = FALSE;
2712 if (cfg->check_pinvoke_callconv && cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
2713 WrapperInfo *info = mono_marshal_get_wrapper_info (cfg->method);
2715 if (info && info->subtype == WRAPPER_SUBTYPE_PINVOKE)
2716 check_sp = TRUE;
2719 if (rgctx_arg) {
2720 rgctx_reg = mono_alloc_preg (cfg);
2721 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, rgctx_reg, rgctx_arg->dreg);
2724 if (check_sp) {
2725 if (!cfg->stack_inbalance_var)
2726 cfg->stack_inbalance_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2728 MONO_INST_NEW (cfg, ins, OP_GET_SP);
2729 ins->dreg = cfg->stack_inbalance_var->dreg;
2730 MONO_ADD_INS (cfg->cbb, ins);
2733 call = mono_emit_call_args (cfg, sig, args, TRUE, FALSE, FALSE, rgctx_arg ? TRUE : FALSE, FALSE);
2735 call->inst.sreg1 = addr->dreg;
2737 if (imt_arg)
2738 emit_imt_argument (cfg, call, NULL, imt_arg);
2740 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2742 if (check_sp) {
2743 int sp_reg;
2745 sp_reg = mono_alloc_preg (cfg);
2747 MONO_INST_NEW (cfg, ins, OP_GET_SP);
2748 ins->dreg = sp_reg;
2749 MONO_ADD_INS (cfg->cbb, ins);
2751 /* Restore the stack so we don't crash when throwing the exception */
2752 MONO_INST_NEW (cfg, ins, OP_SET_SP);
2753 ins->sreg1 = cfg->stack_inbalance_var->dreg;
2754 MONO_ADD_INS (cfg->cbb, ins);
2756 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, cfg->stack_inbalance_var->dreg, sp_reg);
2757 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "ExecutionEngineException");
2760 if (rgctx_arg)
2761 set_rgctx_arg (cfg, call, rgctx_reg, rgctx_arg);
2763 return (MonoInst*)call;
2766 static MonoInst*
2767 emit_get_gsharedvt_info_klass (MonoCompile *cfg, MonoClass *klass, MonoRgctxInfoType rgctx_type);
2769 static MonoInst*
2770 emit_get_rgctx_method (MonoCompile *cfg, int context_used, MonoMethod *cmethod, MonoRgctxInfoType rgctx_type);
2771 static MonoInst*
2772 emit_get_rgctx_klass (MonoCompile *cfg, int context_used, MonoClass *klass, MonoRgctxInfoType rgctx_type);
2774 static MonoInst*
2775 mono_emit_method_call_full (MonoCompile *cfg, MonoMethod *method, MonoMethodSignature *sig, gboolean tail,
2776 MonoInst **args, MonoInst *this_ins, MonoInst *imt_arg, MonoInst *rgctx_arg)
2778 #ifndef DISABLE_REMOTING
2779 gboolean might_be_remote = FALSE;
2780 #endif
2781 gboolean virtual_ = this_ins != NULL;
2782 gboolean enable_for_aot = TRUE;
2783 int context_used;
2784 MonoCallInst *call;
2785 MonoInst *call_target = NULL;
2786 int rgctx_reg = 0;
2787 gboolean need_unbox_trampoline;
2789 if (!sig)
2790 sig = mono_method_signature (method);
2792 if (cfg->llvm_only && (method->klass->flags & TYPE_ATTRIBUTE_INTERFACE))
2793 g_assert_not_reached ();
2795 if (rgctx_arg) {
2796 rgctx_reg = mono_alloc_preg (cfg);
2797 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, rgctx_reg, rgctx_arg->dreg);
2800 if (method->string_ctor) {
2801 /* Create the real signature */
2802 /* FIXME: Cache these */
2803 MonoMethodSignature *ctor_sig = mono_metadata_signature_dup_mempool (cfg->mempool, sig);
2804 ctor_sig->ret = &mono_defaults.string_class->byval_arg;
2806 sig = ctor_sig;
2809 context_used = mini_method_check_context_used (cfg, method);
2811 #ifndef DISABLE_REMOTING
2812 might_be_remote = this_ins && sig->hasthis &&
2813 (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class) &&
2814 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && (!MONO_CHECK_THIS (this_ins) || context_used);
2816 if (might_be_remote && context_used) {
2817 MonoInst *addr;
2819 g_assert (cfg->gshared);
2821 addr = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_REMOTING_INVOKE_WITH_CHECK);
2823 return mono_emit_calli (cfg, sig, args, addr, NULL, NULL);
2825 #endif
2827 if (cfg->llvm_only && !call_target && virtual_ && (method->flags & METHOD_ATTRIBUTE_VIRTUAL))
2828 return emit_llvmonly_virtual_call (cfg, method, sig, 0, args);
2830 need_unbox_trampoline = method->klass == mono_defaults.object_class || (method->klass->flags & TYPE_ATTRIBUTE_INTERFACE);
2832 call = mono_emit_call_args (cfg, sig, args, FALSE, virtual_, tail, rgctx_arg ? TRUE : FALSE, need_unbox_trampoline);
2834 #ifndef DISABLE_REMOTING
2835 if (might_be_remote)
2836 call->method = mono_marshal_get_remoting_invoke_with_check (method);
2837 else
2838 #endif
2839 call->method = method;
2840 call->inst.flags |= MONO_INST_HAS_METHOD;
2841 call->inst.inst_left = this_ins;
2842 call->tail_call = tail;
2844 if (virtual_) {
2845 int vtable_reg, slot_reg, this_reg;
2846 int offset;
2848 this_reg = this_ins->dreg;
2850 if (!cfg->llvm_only && (method->klass->parent == mono_defaults.multicastdelegate_class) && !strcmp (method->name, "Invoke")) {
2851 MonoInst *dummy_use;
2853 MONO_EMIT_NULL_CHECK (cfg, this_reg);
2855 /* Make a call to delegate->invoke_impl */
2856 call->inst.inst_basereg = this_reg;
2857 call->inst.inst_offset = MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl);
2858 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2860 /* We must emit a dummy use here because the delegate trampoline will
2861 replace the 'this' argument with the delegate target making this activation
2862 no longer a root for the delegate.
2863 This is an issue for delegates that target collectible code such as dynamic
2864 methods of GC'able assemblies.
2866 For a test case look into #667921.
2868 FIXME: a dummy use is not the best way to do it as the local register allocator
2869 will put it on a caller save register and spil it around the call.
2870 Ideally, we would either put it on a callee save register or only do the store part.
2872 EMIT_NEW_DUMMY_USE (cfg, dummy_use, args [0]);
2874 return (MonoInst*)call;
2877 if ((!cfg->compile_aot || enable_for_aot) &&
2878 (!(method->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
2879 (MONO_METHOD_IS_FINAL (method) &&
2880 method->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)) &&
2881 !(mono_class_is_marshalbyref (method->klass) && context_used)) {
2883 * the method is not virtual, we just need to ensure this is not null
2884 * and then we can call the method directly.
2886 #ifndef DISABLE_REMOTING
2887 if (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class) {
2889 * The check above ensures method is not gshared, this is needed since
2890 * gshared methods can't have wrappers.
2892 method = call->method = mono_marshal_get_remoting_invoke_with_check (method);
2894 #endif
2896 if (!method->string_ctor)
2897 MONO_EMIT_NEW_CHECK_THIS (cfg, this_reg);
2899 call->inst.opcode = callvirt_to_call (call->inst.opcode);
2900 } else if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && MONO_METHOD_IS_FINAL (method)) {
2902 * the method is virtual, but we can statically dispatch since either
2903 * it's class or the method itself are sealed.
2904 * But first we need to ensure it's not a null reference.
2906 MONO_EMIT_NEW_CHECK_THIS (cfg, this_reg);
2908 call->inst.opcode = callvirt_to_call (call->inst.opcode);
2909 } else if (call_target) {
2910 vtable_reg = alloc_preg (cfg);
2911 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
2913 call->inst.opcode = callvirt_to_call_reg (call->inst.opcode);
2914 call->inst.sreg1 = call_target->dreg;
2915 call->inst.flags &= !MONO_INST_HAS_METHOD;
2916 } else {
2917 vtable_reg = alloc_preg (cfg);
2918 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
2919 if (method->klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
2920 guint32 imt_slot = mono_method_get_imt_slot (method);
2921 emit_imt_argument (cfg, call, call->method, imt_arg);
2922 slot_reg = vtable_reg;
2923 offset = ((gint32)imt_slot - MONO_IMT_SIZE) * SIZEOF_VOID_P;
2924 } else {
2925 slot_reg = vtable_reg;
2926 offset = MONO_STRUCT_OFFSET (MonoVTable, vtable) +
2927 ((mono_method_get_vtable_index (method)) * (SIZEOF_VOID_P));
2928 if (imt_arg) {
2929 g_assert (mono_method_signature (method)->generic_param_count);
2930 emit_imt_argument (cfg, call, call->method, imt_arg);
2934 call->inst.sreg1 = slot_reg;
2935 call->inst.inst_offset = offset;
2936 call->is_virtual = TRUE;
2940 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2942 if (rgctx_arg)
2943 set_rgctx_arg (cfg, call, rgctx_reg, rgctx_arg);
2945 return (MonoInst*)call;
2948 MonoInst*
2949 mono_emit_method_call (MonoCompile *cfg, MonoMethod *method, MonoInst **args, MonoInst *this_ins)
2951 return mono_emit_method_call_full (cfg, method, mono_method_signature (method), FALSE, args, this_ins, NULL, NULL);
2954 MonoInst*
2955 mono_emit_native_call (MonoCompile *cfg, gconstpointer func, MonoMethodSignature *sig,
2956 MonoInst **args)
2958 MonoCallInst *call;
2960 g_assert (sig);
2962 call = mono_emit_call_args (cfg, sig, args, FALSE, FALSE, FALSE, FALSE, FALSE);
2963 call->fptr = func;
2965 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2967 return (MonoInst*)call;
2970 MonoInst*
2971 mono_emit_jit_icall (MonoCompile *cfg, gconstpointer func, MonoInst **args)
2973 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
2975 g_assert (info);
2977 return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, args);
2981 * mono_emit_abs_call:
2983 * Emit a call to the runtime function described by PATCH_TYPE and DATA.
2985 inline static MonoInst*
2986 mono_emit_abs_call (MonoCompile *cfg, MonoJumpInfoType patch_type, gconstpointer data,
2987 MonoMethodSignature *sig, MonoInst **args)
2989 MonoJumpInfo *ji = mono_patch_info_new (cfg->mempool, 0, patch_type, data);
2990 MonoInst *ins;
2993 * We pass ji as the call address, the PATCH_INFO_ABS resolving code will
2994 * handle it.
2996 if (cfg->abs_patches == NULL)
2997 cfg->abs_patches = g_hash_table_new (NULL, NULL);
2998 g_hash_table_insert (cfg->abs_patches, ji, ji);
2999 ins = mono_emit_native_call (cfg, ji, sig, args);
3000 ((MonoCallInst*)ins)->fptr_is_patch = TRUE;
3001 return ins;
3004 static MonoMethodSignature*
3005 sig_to_rgctx_sig (MonoMethodSignature *sig)
3007 // FIXME: memory allocation
3008 MonoMethodSignature *res;
3009 int i;
3011 res = (MonoMethodSignature *)g_malloc (MONO_SIZEOF_METHOD_SIGNATURE + (sig->param_count + 1) * sizeof (MonoType*));
3012 memcpy (res, sig, MONO_SIZEOF_METHOD_SIGNATURE);
3013 res->param_count = sig->param_count + 1;
3014 for (i = 0; i < sig->param_count; ++i)
3015 res->params [i] = sig->params [i];
3016 res->params [sig->param_count] = &mono_defaults.int_class->this_arg;
3017 return res;
3020 /* Make an indirect call to FSIG passing an additional argument */
3021 static MonoInst*
3022 emit_extra_arg_calli (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **orig_args, int arg_reg, MonoInst *call_target)
3024 MonoMethodSignature *csig;
3025 MonoInst *args_buf [16];
3026 MonoInst **args;
3027 int i, pindex, tmp_reg;
3029 /* Make a call with an rgctx/extra arg */
3030 if (fsig->param_count + 2 < 16)
3031 args = args_buf;
3032 else
3033 args = (MonoInst **)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (fsig->param_count + 2));
3034 pindex = 0;
3035 if (fsig->hasthis)
3036 args [pindex ++] = orig_args [0];
3037 for (i = 0; i < fsig->param_count; ++i)
3038 args [pindex ++] = orig_args [fsig->hasthis + i];
3039 tmp_reg = alloc_preg (cfg);
3040 EMIT_NEW_UNALU (cfg, args [pindex], OP_MOVE, tmp_reg, arg_reg);
3041 csig = sig_to_rgctx_sig (fsig);
3042 return mono_emit_calli (cfg, csig, args, call_target, NULL, NULL);
3045 /* Emit an indirect call to the function descriptor ADDR */
3046 static MonoInst*
3047 emit_llvmonly_calli (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, MonoInst *addr)
3049 int addr_reg, arg_reg;
3050 MonoInst *call_target;
3052 g_assert (cfg->llvm_only);
3055 * addr points to a <addr, arg> pair, load both of them, and
3056 * make a call to addr, passing arg as an extra arg.
3058 addr_reg = alloc_preg (cfg);
3059 EMIT_NEW_LOAD_MEMBASE (cfg, call_target, OP_LOAD_MEMBASE, addr_reg, addr->dreg, 0);
3060 arg_reg = alloc_preg (cfg);
3061 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, arg_reg, addr->dreg, sizeof (gpointer));
3063 return emit_extra_arg_calli (cfg, fsig, args, arg_reg, call_target);
3066 static gboolean
3067 direct_icalls_enabled (MonoCompile *cfg)
3069 /* LLVM on amd64 can't handle calls to non-32 bit addresses */
3070 #ifdef TARGET_AMD64
3071 if (cfg->compile_llvm && !cfg->llvm_only)
3072 return FALSE;
3073 #endif
3074 if (cfg->gen_sdb_seq_points || cfg->disable_direct_icalls)
3075 return FALSE;
3076 return TRUE;
3079 MonoInst*
3080 mono_emit_jit_icall_by_info (MonoCompile *cfg, int il_offset, MonoJitICallInfo *info, MonoInst **args)
3083 * Call the jit icall without a wrapper if possible.
3084 * The wrapper is needed for the following reasons:
3085 * - to handle exceptions thrown using mono_raise_exceptions () from the
3086 * icall function. The EH code needs the lmf frame pushed by the
3087 * wrapper to be able to unwind back to managed code.
3088 * - to be able to do stack walks for asynchronously suspended
3089 * threads when debugging.
3091 if (info->no_raise && direct_icalls_enabled (cfg)) {
3092 char *name;
3093 int costs;
3095 if (!info->wrapper_method) {
3096 name = g_strdup_printf ("__icall_wrapper_%s", info->name);
3097 info->wrapper_method = mono_marshal_get_icall_wrapper (info->sig, name, info->func, TRUE);
3098 g_free (name);
3099 mono_memory_barrier ();
3103 * Inline the wrapper method, which is basically a call to the C icall, and
3104 * an exception check.
3106 costs = inline_method (cfg, info->wrapper_method, NULL,
3107 args, NULL, il_offset, TRUE);
3108 g_assert (costs > 0);
3109 g_assert (!MONO_TYPE_IS_VOID (info->sig->ret));
3111 return args [0];
3112 } else {
3113 return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, args);
3117 static MonoInst*
3118 mono_emit_widen_call_res (MonoCompile *cfg, MonoInst *ins, MonoMethodSignature *fsig)
3120 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3121 if ((fsig->pinvoke || LLVM_ENABLED) && !fsig->ret->byref) {
3122 int widen_op = -1;
3125 * Native code might return non register sized integers
3126 * without initializing the upper bits.
3128 switch (mono_type_to_load_membase (cfg, fsig->ret)) {
3129 case OP_LOADI1_MEMBASE:
3130 widen_op = OP_ICONV_TO_I1;
3131 break;
3132 case OP_LOADU1_MEMBASE:
3133 widen_op = OP_ICONV_TO_U1;
3134 break;
3135 case OP_LOADI2_MEMBASE:
3136 widen_op = OP_ICONV_TO_I2;
3137 break;
3138 case OP_LOADU2_MEMBASE:
3139 widen_op = OP_ICONV_TO_U2;
3140 break;
3141 default:
3142 break;
3145 if (widen_op != -1) {
3146 int dreg = alloc_preg (cfg);
3147 MonoInst *widen;
3149 EMIT_NEW_UNALU (cfg, widen, widen_op, dreg, ins->dreg);
3150 widen->type = ins->type;
3151 ins = widen;
3156 return ins;
3159 static MonoMethod*
3160 get_memcpy_method (void)
3162 static MonoMethod *memcpy_method = NULL;
3163 if (!memcpy_method) {
3164 memcpy_method = mono_class_get_method_from_name (mono_defaults.string_class, "memcpy", 3);
3165 if (!memcpy_method)
3166 g_error ("Old corlib found. Install a new one");
3168 return memcpy_method;
3171 static void
3172 create_write_barrier_bitmap (MonoCompile *cfg, MonoClass *klass, unsigned *wb_bitmap, int offset)
3174 MonoClassField *field;
3175 gpointer iter = NULL;
3177 while ((field = mono_class_get_fields (klass, &iter))) {
3178 int foffset;
3180 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
3181 continue;
3182 foffset = klass->valuetype ? field->offset - sizeof (MonoObject): field->offset;
3183 if (mini_type_is_reference (mono_field_get_type (field))) {
3184 g_assert ((foffset % SIZEOF_VOID_P) == 0);
3185 *wb_bitmap |= 1 << ((offset + foffset) / SIZEOF_VOID_P);
3186 } else {
3187 MonoClass *field_class = mono_class_from_mono_type (field->type);
3188 if (field_class->has_references)
3189 create_write_barrier_bitmap (cfg, field_class, wb_bitmap, offset + foffset);
3194 static void
3195 emit_write_barrier (MonoCompile *cfg, MonoInst *ptr, MonoInst *value)
3197 int card_table_shift_bits;
3198 gpointer card_table_mask;
3199 guint8 *card_table;
3200 MonoInst *dummy_use;
3201 int nursery_shift_bits;
3202 size_t nursery_size;
3204 if (!cfg->gen_write_barriers)
3205 return;
3207 card_table = mono_gc_get_card_table (&card_table_shift_bits, &card_table_mask);
3209 mono_gc_get_nursery (&nursery_shift_bits, &nursery_size);
3211 if (cfg->backend->have_card_table_wb && !cfg->compile_aot && card_table && nursery_shift_bits > 0 && !COMPILE_LLVM (cfg)) {
3212 MonoInst *wbarrier;
3214 MONO_INST_NEW (cfg, wbarrier, OP_CARD_TABLE_WBARRIER);
3215 wbarrier->sreg1 = ptr->dreg;
3216 wbarrier->sreg2 = value->dreg;
3217 MONO_ADD_INS (cfg->cbb, wbarrier);
3218 } else if (card_table && !cfg->compile_aot && !mono_gc_card_table_nursery_check ()) {
3219 int offset_reg = alloc_preg (cfg);
3220 int card_reg;
3221 MonoInst *ins;
3223 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHR_UN_IMM, offset_reg, ptr->dreg, card_table_shift_bits);
3224 if (card_table_mask)
3225 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PAND_IMM, offset_reg, offset_reg, card_table_mask);
3227 /*We can't use PADD_IMM since the cardtable might end up in high addresses and amd64 doesn't support
3228 * IMM's larger than 32bits.
3230 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_CARD_TABLE_ADDR, NULL);
3231 card_reg = ins->dreg;
3233 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, offset_reg, offset_reg, card_reg);
3234 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI1_MEMBASE_IMM, offset_reg, 0, 1);
3235 } else {
3236 MonoMethod *write_barrier = mono_gc_get_write_barrier ();
3237 mono_emit_method_call (cfg, write_barrier, &ptr, NULL);
3240 EMIT_NEW_DUMMY_USE (cfg, dummy_use, value);
3243 static gboolean
3244 mono_emit_wb_aware_memcpy (MonoCompile *cfg, MonoClass *klass, MonoInst *iargs[4], int size, int align)
3246 int dest_ptr_reg, tmp_reg, destreg, srcreg, offset;
3247 unsigned need_wb = 0;
3249 if (align == 0)
3250 align = 4;
3252 /*types with references can't have alignment smaller than sizeof(void*) */
3253 if (align < SIZEOF_VOID_P)
3254 return FALSE;
3256 /*This value cannot be bigger than 32 due to the way we calculate the required wb bitmap.*/
3257 if (size > 32 * SIZEOF_VOID_P)
3258 return FALSE;
3260 create_write_barrier_bitmap (cfg, klass, &need_wb, 0);
3262 /* We don't unroll more than 5 stores to avoid code bloat. */
3263 if (size > 5 * SIZEOF_VOID_P) {
3264 /*This is harmless and simplify mono_gc_wbarrier_value_copy_bitmap */
3265 size += (SIZEOF_VOID_P - 1);
3266 size &= ~(SIZEOF_VOID_P - 1);
3268 EMIT_NEW_ICONST (cfg, iargs [2], size);
3269 EMIT_NEW_ICONST (cfg, iargs [3], need_wb);
3270 mono_emit_jit_icall (cfg, mono_gc_wbarrier_value_copy_bitmap, iargs);
3271 return TRUE;
3274 destreg = iargs [0]->dreg;
3275 srcreg = iargs [1]->dreg;
3276 offset = 0;
3278 dest_ptr_reg = alloc_preg (cfg);
3279 tmp_reg = alloc_preg (cfg);
3281 /*tmp = dreg*/
3282 EMIT_NEW_UNALU (cfg, iargs [0], OP_MOVE, dest_ptr_reg, destreg);
3284 while (size >= SIZEOF_VOID_P) {
3285 MonoInst *load_inst;
3286 MONO_INST_NEW (cfg, load_inst, OP_LOAD_MEMBASE);
3287 load_inst->dreg = tmp_reg;
3288 load_inst->inst_basereg = srcreg;
3289 load_inst->inst_offset = offset;
3290 MONO_ADD_INS (cfg->cbb, load_inst);
3292 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, dest_ptr_reg, 0, tmp_reg);
3294 if (need_wb & 0x1)
3295 emit_write_barrier (cfg, iargs [0], load_inst);
3297 offset += SIZEOF_VOID_P;
3298 size -= SIZEOF_VOID_P;
3299 need_wb >>= 1;
3301 /*tmp += sizeof (void*)*/
3302 if (size >= SIZEOF_VOID_P) {
3303 NEW_BIALU_IMM (cfg, iargs [0], OP_PADD_IMM, dest_ptr_reg, dest_ptr_reg, SIZEOF_VOID_P);
3304 MONO_ADD_INS (cfg->cbb, iargs [0]);
3308 /* Those cannot be references since size < sizeof (void*) */
3309 while (size >= 4) {
3310 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, tmp_reg, srcreg, offset);
3311 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, offset, tmp_reg);
3312 offset += 4;
3313 size -= 4;
3316 while (size >= 2) {
3317 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI2_MEMBASE, tmp_reg, srcreg, offset);
3318 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI2_MEMBASE_REG, destreg, offset, tmp_reg);
3319 offset += 2;
3320 size -= 2;
3323 while (size >= 1) {
3324 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, tmp_reg, srcreg, offset);
3325 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, offset, tmp_reg);
3326 offset += 1;
3327 size -= 1;
3330 return TRUE;
3334 * Emit code to copy a valuetype of type @klass whose address is stored in
3335 * @src->dreg to memory whose address is stored at @dest->dreg.
3337 void
3338 mini_emit_stobj (MonoCompile *cfg, MonoInst *dest, MonoInst *src, MonoClass *klass, gboolean native)
3340 MonoInst *iargs [4];
3341 int n;
3342 guint32 align = 0;
3343 MonoMethod *memcpy_method;
3344 MonoInst *size_ins = NULL;
3345 MonoInst *memcpy_ins = NULL;
3347 g_assert (klass);
3348 if (cfg->gshared)
3349 klass = mono_class_from_mono_type (mini_get_underlying_type (&klass->byval_arg));
3352 * This check breaks with spilled vars... need to handle it during verification anyway.
3353 * g_assert (klass && klass == src->klass && klass == dest->klass);
3356 if (mini_is_gsharedvt_klass (klass)) {
3357 g_assert (!native);
3358 size_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_VALUE_SIZE);
3359 memcpy_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_MEMCPY);
3362 if (native)
3363 n = mono_class_native_size (klass, &align);
3364 else
3365 n = mono_class_value_size (klass, &align);
3367 /* if native is true there should be no references in the struct */
3368 if (cfg->gen_write_barriers && (klass->has_references || size_ins) && !native) {
3369 /* Avoid barriers when storing to the stack */
3370 if (!((dest->opcode == OP_ADD_IMM && dest->sreg1 == cfg->frame_reg) ||
3371 (dest->opcode == OP_LDADDR))) {
3372 int context_used;
3374 iargs [0] = dest;
3375 iargs [1] = src;
3377 context_used = mini_class_check_context_used (cfg, klass);
3379 /* It's ok to intrinsify under gsharing since shared code types are layout stable. */
3380 if (!size_ins && (cfg->opt & MONO_OPT_INTRINS) && mono_emit_wb_aware_memcpy (cfg, klass, iargs, n, align)) {
3381 return;
3382 } else if (context_used) {
3383 iargs [2] = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
3384 } else {
3385 iargs [2] = emit_runtime_constant (cfg, MONO_PATCH_INFO_CLASS, klass);
3386 if (!cfg->compile_aot)
3387 mono_class_compute_gc_descriptor (klass);
3390 if (size_ins)
3391 mono_emit_jit_icall (cfg, mono_gsharedvt_value_copy, iargs);
3392 else
3393 mono_emit_jit_icall (cfg, mono_value_copy, iargs);
3394 return;
3398 if (!size_ins && (cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 8) {
3399 /* FIXME: Optimize the case when src/dest is OP_LDADDR */
3400 mini_emit_memcpy (cfg, dest->dreg, 0, src->dreg, 0, n, align);
3401 } else {
3402 iargs [0] = dest;
3403 iargs [1] = src;
3404 if (size_ins)
3405 iargs [2] = size_ins;
3406 else
3407 EMIT_NEW_ICONST (cfg, iargs [2], n);
3409 memcpy_method = get_memcpy_method ();
3410 if (memcpy_ins)
3411 mono_emit_calli (cfg, mono_method_signature (memcpy_method), iargs, memcpy_ins, NULL, NULL);
3412 else
3413 mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
3417 static MonoMethod*
3418 get_memset_method (void)
3420 static MonoMethod *memset_method = NULL;
3421 if (!memset_method) {
3422 memset_method = mono_class_get_method_from_name (mono_defaults.string_class, "memset", 3);
3423 if (!memset_method)
3424 g_error ("Old corlib found. Install a new one");
3426 return memset_method;
3429 void
3430 mini_emit_initobj (MonoCompile *cfg, MonoInst *dest, const guchar *ip, MonoClass *klass)
3432 MonoInst *iargs [3];
3433 int n;
3434 guint32 align;
3435 MonoMethod *memset_method;
3436 MonoInst *size_ins = NULL;
3437 MonoInst *bzero_ins = NULL;
3438 static MonoMethod *bzero_method;
3440 /* FIXME: Optimize this for the case when dest is an LDADDR */
3441 mono_class_init (klass);
3442 if (mini_is_gsharedvt_klass (klass)) {
3443 size_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_VALUE_SIZE);
3444 bzero_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_BZERO);
3445 if (!bzero_method)
3446 bzero_method = mono_class_get_method_from_name (mono_defaults.string_class, "bzero_aligned_1", 2);
3447 g_assert (bzero_method);
3448 iargs [0] = dest;
3449 iargs [1] = size_ins;
3450 mono_emit_calli (cfg, mono_method_signature (bzero_method), iargs, bzero_ins, NULL, NULL);
3451 return;
3454 klass = mono_class_from_mono_type (mini_get_underlying_type (&klass->byval_arg));
3456 n = mono_class_value_size (klass, &align);
3458 if (n <= sizeof (gpointer) * 8) {
3459 mini_emit_memset (cfg, dest->dreg, 0, n, 0, align);
3461 else {
3462 memset_method = get_memset_method ();
3463 iargs [0] = dest;
3464 EMIT_NEW_ICONST (cfg, iargs [1], 0);
3465 EMIT_NEW_ICONST (cfg, iargs [2], n);
3466 mono_emit_method_call (cfg, memset_method, iargs, NULL);
3471 * emit_get_rgctx:
3473 * Emit IR to return either the this pointer for instance method,
3474 * or the mrgctx for static methods.
3476 static MonoInst*
3477 emit_get_rgctx (MonoCompile *cfg, MonoMethod *method, int context_used)
3479 MonoInst *this_ins = NULL;
3481 g_assert (cfg->gshared);
3483 if (!(method->flags & METHOD_ATTRIBUTE_STATIC) &&
3484 !(context_used & MONO_GENERIC_CONTEXT_USED_METHOD) &&
3485 !method->klass->valuetype)
3486 EMIT_NEW_ARGLOAD (cfg, this_ins, 0);
3488 if (context_used & MONO_GENERIC_CONTEXT_USED_METHOD) {
3489 MonoInst *mrgctx_loc, *mrgctx_var;
3491 g_assert (!this_ins);
3492 g_assert (method->is_inflated && mono_method_get_context (method)->method_inst);
3494 mrgctx_loc = mono_get_vtable_var (cfg);
3495 EMIT_NEW_TEMPLOAD (cfg, mrgctx_var, mrgctx_loc->inst_c0);
3497 return mrgctx_var;
3498 } else if (method->flags & METHOD_ATTRIBUTE_STATIC || method->klass->valuetype) {
3499 MonoInst *vtable_loc, *vtable_var;
3501 g_assert (!this_ins);
3503 vtable_loc = mono_get_vtable_var (cfg);
3504 EMIT_NEW_TEMPLOAD (cfg, vtable_var, vtable_loc->inst_c0);
3506 if (method->is_inflated && mono_method_get_context (method)->method_inst) {
3507 MonoInst *mrgctx_var = vtable_var;
3508 int vtable_reg;
3510 vtable_reg = alloc_preg (cfg);
3511 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_var, OP_LOAD_MEMBASE, vtable_reg, mrgctx_var->dreg, MONO_STRUCT_OFFSET (MonoMethodRuntimeGenericContext, class_vtable));
3512 vtable_var->type = STACK_PTR;
3515 return vtable_var;
3516 } else {
3517 MonoInst *ins;
3518 int vtable_reg;
3520 vtable_reg = alloc_preg (cfg);
3521 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, vtable_reg, this_ins->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3522 return ins;
3526 static MonoJumpInfoRgctxEntry *
3527 mono_patch_info_rgctx_entry_new (MonoMemPool *mp, MonoMethod *method, gboolean in_mrgctx, MonoJumpInfoType patch_type, gconstpointer patch_data, MonoRgctxInfoType info_type)
3529 MonoJumpInfoRgctxEntry *res = (MonoJumpInfoRgctxEntry *)mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
3530 res->method = method;
3531 res->in_mrgctx = in_mrgctx;
3532 res->data = (MonoJumpInfo *)mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
3533 res->data->type = patch_type;
3534 res->data->data.target = patch_data;
3535 res->info_type = info_type;
3537 return res;
3540 static inline MonoInst*
3541 emit_rgctx_fetch_inline (MonoCompile *cfg, MonoInst *rgctx, MonoJumpInfoRgctxEntry *entry)
3543 MonoInst *args [16];
3544 MonoInst *call;
3546 // FIXME: No fastpath since the slot is not a compile time constant
3547 args [0] = rgctx;
3548 EMIT_NEW_AOTCONST (cfg, args [1], MONO_PATCH_INFO_RGCTX_SLOT_INDEX, entry);
3549 if (entry->in_mrgctx)
3550 call = mono_emit_jit_icall (cfg, mono_fill_method_rgctx, args);
3551 else
3552 call = mono_emit_jit_icall (cfg, mono_fill_class_rgctx, args);
3553 return call;
3554 #if 0
3556 * FIXME: This can be called during decompose, which is a problem since it creates
3557 * new bblocks.
3558 * Also, the fastpath doesn't work since the slot number is dynamically allocated.
3560 int i, slot, depth, index, rgctx_reg, val_reg, res_reg;
3561 gboolean mrgctx;
3562 MonoBasicBlock *is_null_bb, *end_bb;
3563 MonoInst *res, *ins, *call;
3564 MonoInst *args[16];
3566 slot = mini_get_rgctx_entry_slot (entry);
3568 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
3569 index = MONO_RGCTX_SLOT_INDEX (slot);
3570 if (mrgctx)
3571 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
3572 for (depth = 0; ; ++depth) {
3573 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
3575 if (index < size - 1)
3576 break;
3577 index -= size - 1;
3580 NEW_BBLOCK (cfg, end_bb);
3581 NEW_BBLOCK (cfg, is_null_bb);
3583 if (mrgctx) {
3584 rgctx_reg = rgctx->dreg;
3585 } else {
3586 rgctx_reg = alloc_preg (cfg);
3588 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, rgctx_reg, rgctx->dreg, MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context));
3589 // FIXME: Avoid this check by allocating the table when the vtable is created etc.
3590 NEW_BBLOCK (cfg, is_null_bb);
3592 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rgctx_reg, 0);
3593 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3596 for (i = 0; i < depth; ++i) {
3597 int array_reg = alloc_preg (cfg);
3599 /* load ptr to next array */
3600 if (mrgctx && i == 0)
3601 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, rgctx_reg, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
3602 else
3603 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, rgctx_reg, 0);
3604 rgctx_reg = array_reg;
3605 /* is the ptr null? */
3606 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rgctx_reg, 0);
3607 /* if yes, jump to actual trampoline */
3608 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3611 /* fetch slot */
3612 val_reg = alloc_preg (cfg);
3613 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, val_reg, rgctx_reg, (index + 1) * sizeof (gpointer));
3614 /* is the slot null? */
3615 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, val_reg, 0);
3616 /* if yes, jump to actual trampoline */
3617 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3619 /* Fastpath */
3620 res_reg = alloc_preg (cfg);
3621 MONO_INST_NEW (cfg, ins, OP_MOVE);
3622 ins->dreg = res_reg;
3623 ins->sreg1 = val_reg;
3624 MONO_ADD_INS (cfg->cbb, ins);
3625 res = ins;
3626 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3628 /* Slowpath */
3629 MONO_START_BB (cfg, is_null_bb);
3630 args [0] = rgctx;
3631 EMIT_NEW_ICONST (cfg, args [1], index);
3632 if (mrgctx)
3633 call = mono_emit_jit_icall (cfg, mono_fill_method_rgctx, args);
3634 else
3635 call = mono_emit_jit_icall (cfg, mono_fill_class_rgctx, args);
3636 MONO_INST_NEW (cfg, ins, OP_MOVE);
3637 ins->dreg = res_reg;
3638 ins->sreg1 = call->dreg;
3639 MONO_ADD_INS (cfg->cbb, ins);
3640 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3642 MONO_START_BB (cfg, end_bb);
3644 return res;
3645 #endif
3649 * emit_rgctx_fetch:
3651 * Emit IR to load the value of the rgctx entry ENTRY from the rgctx
3652 * given by RGCTX.
3654 static inline MonoInst*
3655 emit_rgctx_fetch (MonoCompile *cfg, MonoInst *rgctx, MonoJumpInfoRgctxEntry *entry)
3657 if (cfg->llvm_only)
3658 return emit_rgctx_fetch_inline (cfg, rgctx, entry);
3659 else
3660 return mono_emit_abs_call (cfg, MONO_PATCH_INFO_RGCTX_FETCH, entry, helper_sig_rgctx_lazy_fetch_trampoline, &rgctx);
3663 static MonoInst*
3664 emit_get_rgctx_klass (MonoCompile *cfg, int context_used,
3665 MonoClass *klass, MonoRgctxInfoType rgctx_type)
3667 MonoJumpInfoRgctxEntry *entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->current_method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_CLASS, klass, rgctx_type);
3668 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3670 return emit_rgctx_fetch (cfg, rgctx, entry);
3673 static MonoInst*
3674 emit_get_rgctx_sig (MonoCompile *cfg, int context_used,
3675 MonoMethodSignature *sig, MonoRgctxInfoType rgctx_type)
3677 MonoJumpInfoRgctxEntry *entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->current_method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_SIGNATURE, sig, rgctx_type);
3678 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3680 return emit_rgctx_fetch (cfg, rgctx, entry);
3683 static MonoInst*
3684 emit_get_rgctx_gsharedvt_call (MonoCompile *cfg, int context_used,
3685 MonoMethodSignature *sig, MonoMethod *cmethod, MonoRgctxInfoType rgctx_type)
3687 MonoJumpInfoGSharedVtCall *call_info;
3688 MonoJumpInfoRgctxEntry *entry;
3689 MonoInst *rgctx;
3691 call_info = (MonoJumpInfoGSharedVtCall *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoGSharedVtCall));
3692 call_info->sig = sig;
3693 call_info->method = cmethod;
3695 entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->current_method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_GSHAREDVT_CALL, call_info, rgctx_type);
3696 rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3698 return emit_rgctx_fetch (cfg, rgctx, entry);
3702 * emit_get_rgctx_virt_method:
3704 * Return data for method VIRT_METHOD for a receiver of type KLASS.
3706 static MonoInst*
3707 emit_get_rgctx_virt_method (MonoCompile *cfg, int context_used,
3708 MonoClass *klass, MonoMethod *virt_method, MonoRgctxInfoType rgctx_type)
3710 MonoJumpInfoVirtMethod *info;
3711 MonoJumpInfoRgctxEntry *entry;
3712 MonoInst *rgctx;
3714 info = (MonoJumpInfoVirtMethod *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoVirtMethod));
3715 info->klass = klass;
3716 info->method = virt_method;
3718 entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->current_method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_VIRT_METHOD, info, rgctx_type);
3719 rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3721 return emit_rgctx_fetch (cfg, rgctx, entry);
3724 static MonoInst*
3725 emit_get_rgctx_gsharedvt_method (MonoCompile *cfg, int context_used,
3726 MonoMethod *cmethod, MonoGSharedVtMethodInfo *info)
3728 MonoJumpInfoRgctxEntry *entry;
3729 MonoInst *rgctx;
3731 entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->current_method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_GSHAREDVT_METHOD, info, MONO_RGCTX_INFO_METHOD_GSHAREDVT_INFO);
3732 rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3734 return emit_rgctx_fetch (cfg, rgctx, entry);
3738 * emit_get_rgctx_method:
3740 * Emit IR to load the property RGCTX_TYPE of CMETHOD. If context_used is 0, emit
3741 * normal constants, else emit a load from the rgctx.
3743 static MonoInst*
3744 emit_get_rgctx_method (MonoCompile *cfg, int context_used,
3745 MonoMethod *cmethod, MonoRgctxInfoType rgctx_type)
3747 if (!context_used) {
3748 MonoInst *ins;
3750 switch (rgctx_type) {
3751 case MONO_RGCTX_INFO_METHOD:
3752 EMIT_NEW_METHODCONST (cfg, ins, cmethod);
3753 return ins;
3754 case MONO_RGCTX_INFO_METHOD_RGCTX:
3755 EMIT_NEW_METHOD_RGCTX_CONST (cfg, ins, cmethod);
3756 return ins;
3757 default:
3758 g_assert_not_reached ();
3760 } else {
3761 MonoJumpInfoRgctxEntry *entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->current_method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_METHODCONST, cmethod, rgctx_type);
3762 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3764 return emit_rgctx_fetch (cfg, rgctx, entry);
3768 static MonoInst*
3769 emit_get_rgctx_field (MonoCompile *cfg, int context_used,
3770 MonoClassField *field, MonoRgctxInfoType rgctx_type)
3772 MonoJumpInfoRgctxEntry *entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->current_method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_FIELD, field, rgctx_type);
3773 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3775 return emit_rgctx_fetch (cfg, rgctx, entry);
3778 static int
3779 get_gsharedvt_info_slot (MonoCompile *cfg, gpointer data, MonoRgctxInfoType rgctx_type)
3781 MonoGSharedVtMethodInfo *info = cfg->gsharedvt_info;
3782 MonoRuntimeGenericContextInfoTemplate *template_;
3783 int i, idx;
3785 g_assert (info);
3787 for (i = 0; i < info->num_entries; ++i) {
3788 MonoRuntimeGenericContextInfoTemplate *otemplate = &info->entries [i];
3790 if (otemplate->info_type == rgctx_type && otemplate->data == data && rgctx_type != MONO_RGCTX_INFO_LOCAL_OFFSET)
3791 return i;
3794 if (info->num_entries == info->count_entries) {
3795 MonoRuntimeGenericContextInfoTemplate *new_entries;
3796 int new_count_entries = info->count_entries ? info->count_entries * 2 : 16;
3798 new_entries = (MonoRuntimeGenericContextInfoTemplate *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoRuntimeGenericContextInfoTemplate) * new_count_entries);
3800 memcpy (new_entries, info->entries, sizeof (MonoRuntimeGenericContextInfoTemplate) * info->count_entries);
3801 info->entries = new_entries;
3802 info->count_entries = new_count_entries;
3805 idx = info->num_entries;
3806 template_ = &info->entries [idx];
3807 template_->info_type = rgctx_type;
3808 template_->data = data;
3810 info->num_entries ++;
3812 return idx;
3816 * emit_get_gsharedvt_info:
3818 * This is similar to emit_get_rgctx_.., but loads the data from the gsharedvt info var instead of calling an rgctx fetch trampoline.
3820 static MonoInst*
3821 emit_get_gsharedvt_info (MonoCompile *cfg, gpointer data, MonoRgctxInfoType rgctx_type)
3823 MonoInst *ins;
3824 int idx, dreg;
3826 idx = get_gsharedvt_info_slot (cfg, data, rgctx_type);
3827 /* Load info->entries [idx] */
3828 dreg = alloc_preg (cfg);
3829 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, cfg->gsharedvt_info_var->dreg, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, entries) + (idx * sizeof (gpointer)));
3831 return ins;
3834 static MonoInst*
3835 emit_get_gsharedvt_info_klass (MonoCompile *cfg, MonoClass *klass, MonoRgctxInfoType rgctx_type)
3837 return emit_get_gsharedvt_info (cfg, &klass->byval_arg, rgctx_type);
3841 * On return the caller must check @klass for load errors.
3843 static void
3844 emit_class_init (MonoCompile *cfg, MonoClass *klass)
3846 MonoInst *vtable_arg;
3847 int context_used;
3849 context_used = mini_class_check_context_used (cfg, klass);
3851 if (context_used) {
3852 vtable_arg = emit_get_rgctx_klass (cfg, context_used,
3853 klass, MONO_RGCTX_INFO_VTABLE);
3854 } else {
3855 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
3857 if (!vtable)
3858 return;
3859 EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
3862 if (!COMPILE_LLVM (cfg) && cfg->backend->have_op_generic_class_init) {
3863 MonoInst *ins;
3866 * Using an opcode instead of emitting IR here allows the hiding of the call inside the opcode,
3867 * so this doesn't have to clobber any regs and it doesn't break basic blocks.
3869 MONO_INST_NEW (cfg, ins, OP_GENERIC_CLASS_INIT);
3870 ins->sreg1 = vtable_arg->dreg;
3871 MONO_ADD_INS (cfg->cbb, ins);
3872 } else {
3873 static int byte_offset = -1;
3874 static guint8 bitmask;
3875 int bits_reg, inited_reg;
3876 MonoBasicBlock *inited_bb;
3877 MonoInst *args [16];
3879 if (byte_offset < 0)
3880 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
3882 bits_reg = alloc_ireg (cfg);
3883 inited_reg = alloc_ireg (cfg);
3885 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, bits_reg, vtable_arg->dreg, byte_offset);
3886 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, inited_reg, bits_reg, bitmask);
3888 NEW_BBLOCK (cfg, inited_bb);
3890 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, inited_reg, 0);
3891 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBNE_UN, inited_bb);
3893 args [0] = vtable_arg;
3894 mono_emit_jit_icall (cfg, mono_generic_class_init, args);
3896 MONO_START_BB (cfg, inited_bb);
3900 static void
3901 emit_seq_point (MonoCompile *cfg, MonoMethod *method, guint8* ip, gboolean intr_loc, gboolean nonempty_stack)
3903 MonoInst *ins;
3905 if (cfg->gen_seq_points && cfg->method == method) {
3906 NEW_SEQ_POINT (cfg, ins, ip - cfg->header->code, intr_loc);
3907 if (nonempty_stack)
3908 ins->flags |= MONO_INST_NONEMPTY_STACK;
3909 MONO_ADD_INS (cfg->cbb, ins);
3913 static void
3914 save_cast_details (MonoCompile *cfg, MonoClass *klass, int obj_reg, gboolean null_check)
3916 if (mini_get_debug_options ()->better_cast_details) {
3917 int vtable_reg = alloc_preg (cfg);
3918 int klass_reg = alloc_preg (cfg);
3919 MonoBasicBlock *is_null_bb = NULL;
3920 MonoInst *tls_get;
3921 int to_klass_reg, context_used;
3923 if (null_check) {
3924 NEW_BBLOCK (cfg, is_null_bb);
3926 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
3927 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3930 tls_get = mono_get_jit_tls_intrinsic (cfg);
3931 if (!tls_get) {
3932 fprintf (stderr, "error: --debug=casts not supported on this platform.\n.");
3933 exit (1);
3936 MONO_ADD_INS (cfg->cbb, tls_get);
3937 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3938 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
3940 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_from), klass_reg);
3942 context_used = mini_class_check_context_used (cfg, klass);
3943 if (context_used) {
3944 MonoInst *class_ins;
3946 class_ins = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
3947 to_klass_reg = class_ins->dreg;
3948 } else {
3949 to_klass_reg = alloc_preg (cfg);
3950 MONO_EMIT_NEW_CLASSCONST (cfg, to_klass_reg, klass);
3952 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_to), to_klass_reg);
3954 if (null_check)
3955 MONO_START_BB (cfg, is_null_bb);
3959 static void
3960 reset_cast_details (MonoCompile *cfg)
3962 /* Reset the variables holding the cast details */
3963 if (mini_get_debug_options ()->better_cast_details) {
3964 MonoInst *tls_get = mono_get_jit_tls_intrinsic (cfg);
3966 MONO_ADD_INS (cfg->cbb, tls_get);
3967 /* It is enough to reset the from field */
3968 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_from), 0);
3973 * On return the caller must check @array_class for load errors
3975 static void
3976 mini_emit_check_array_type (MonoCompile *cfg, MonoInst *obj, MonoClass *array_class)
3978 int vtable_reg = alloc_preg (cfg);
3979 int context_used;
3981 context_used = mini_class_check_context_used (cfg, array_class);
3983 save_cast_details (cfg, array_class, obj->dreg, FALSE);
3985 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, obj->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3987 if (cfg->opt & MONO_OPT_SHARED) {
3988 int class_reg = alloc_preg (cfg);
3989 MonoInst *ins;
3991 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, class_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
3992 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_CLASS, array_class);
3993 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, class_reg, ins->dreg);
3994 } else if (context_used) {
3995 MonoInst *vtable_ins;
3997 vtable_ins = emit_get_rgctx_klass (cfg, context_used, array_class, MONO_RGCTX_INFO_VTABLE);
3998 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, vtable_reg, vtable_ins->dreg);
3999 } else {
4000 if (cfg->compile_aot) {
4001 int vt_reg;
4002 MonoVTable *vtable;
4004 if (!(vtable = mono_class_vtable (cfg->domain, array_class)))
4005 return;
4006 vt_reg = alloc_preg (cfg);
4007 MONO_EMIT_NEW_VTABLECONST (cfg, vt_reg, vtable);
4008 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, vtable_reg, vt_reg);
4009 } else {
4010 MonoVTable *vtable;
4011 if (!(vtable = mono_class_vtable (cfg->domain, array_class)))
4012 return;
4013 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, vtable_reg, vtable);
4017 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "ArrayTypeMismatchException");
4019 reset_cast_details (cfg);
4023 * Handles unbox of a Nullable<T>. If context_used is non zero, then shared
4024 * generic code is generated.
4026 static MonoInst*
4027 handle_unbox_nullable (MonoCompile* cfg, MonoInst* val, MonoClass* klass, int context_used)
4029 MonoMethod* method = mono_class_get_method_from_name (klass, "Unbox", 1);
4031 if (context_used) {
4032 MonoInst *rgctx, *addr;
4034 /* FIXME: What if the class is shared? We might not
4035 have to get the address of the method from the
4036 RGCTX. */
4037 addr = emit_get_rgctx_method (cfg, context_used, method,
4038 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
4039 if (cfg->llvm_only && cfg->gsharedvt) {
4040 return emit_llvmonly_calli (cfg, mono_method_signature (method), &val, addr);
4041 } else {
4042 rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
4044 return mono_emit_calli (cfg, mono_method_signature (method), &val, addr, NULL, rgctx);
4046 } else {
4047 gboolean pass_vtable, pass_mrgctx;
4048 MonoInst *rgctx_arg = NULL;
4050 check_method_sharing (cfg, method, &pass_vtable, &pass_mrgctx);
4051 g_assert (!pass_mrgctx);
4053 if (pass_vtable) {
4054 MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
4056 g_assert (vtable);
4057 EMIT_NEW_VTABLECONST (cfg, rgctx_arg, vtable);
4060 return mono_emit_method_call_full (cfg, method, NULL, FALSE, &val, NULL, NULL, rgctx_arg);
4064 static MonoInst*
4065 handle_unbox (MonoCompile *cfg, MonoClass *klass, MonoInst **sp, int context_used)
4067 MonoInst *add;
4068 int obj_reg;
4069 int vtable_reg = alloc_dreg (cfg ,STACK_PTR);
4070 int klass_reg = alloc_dreg (cfg ,STACK_PTR);
4071 int eclass_reg = alloc_dreg (cfg ,STACK_PTR);
4072 int rank_reg = alloc_dreg (cfg ,STACK_I4);
4074 obj_reg = sp [0]->dreg;
4075 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4076 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, rank_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, rank));
4078 /* FIXME: generics */
4079 g_assert (klass->rank == 0);
4081 // Check rank == 0
4082 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rank_reg, 0);
4083 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
4085 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4086 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, eclass_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, element_class));
4088 if (context_used) {
4089 MonoInst *element_class;
4091 /* This assertion is from the unboxcast insn */
4092 g_assert (klass->rank == 0);
4094 element_class = emit_get_rgctx_klass (cfg, context_used,
4095 klass, MONO_RGCTX_INFO_ELEMENT_KLASS);
4097 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, eclass_reg, element_class->dreg);
4098 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
4099 } else {
4100 save_cast_details (cfg, klass->element_class, obj_reg, FALSE);
4101 mini_emit_class_check (cfg, eclass_reg, klass->element_class);
4102 reset_cast_details (cfg);
4105 NEW_BIALU_IMM (cfg, add, OP_ADD_IMM, alloc_dreg (cfg, STACK_MP), obj_reg, sizeof (MonoObject));
4106 MONO_ADD_INS (cfg->cbb, add);
4107 add->type = STACK_MP;
4108 add->klass = klass;
4110 return add;
4113 static MonoInst*
4114 handle_unbox_gsharedvt (MonoCompile *cfg, MonoClass *klass, MonoInst *obj)
4116 MonoInst *addr, *klass_inst, *is_ref, *args[16];
4117 MonoBasicBlock *is_ref_bb, *is_nullable_bb, *end_bb;
4118 MonoInst *ins;
4119 int dreg, addr_reg;
4121 klass_inst = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_KLASS);
4123 /* obj */
4124 args [0] = obj;
4126 /* klass */
4127 args [1] = klass_inst;
4129 /* CASTCLASS */
4130 obj = mono_emit_jit_icall (cfg, mono_object_castclass_unbox, args);
4132 NEW_BBLOCK (cfg, is_ref_bb);
4133 NEW_BBLOCK (cfg, is_nullable_bb);
4134 NEW_BBLOCK (cfg, end_bb);
4135 is_ref = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_CLASS_BOX_TYPE);
4136 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_REF);
4137 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
4139 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_NULLABLE);
4140 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_nullable_bb);
4142 /* This will contain either the address of the unboxed vtype, or an address of the temporary where the ref is stored */
4143 addr_reg = alloc_dreg (cfg, STACK_MP);
4145 /* Non-ref case */
4146 /* UNBOX */
4147 NEW_BIALU_IMM (cfg, addr, OP_ADD_IMM, addr_reg, obj->dreg, sizeof (MonoObject));
4148 MONO_ADD_INS (cfg->cbb, addr);
4150 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4152 /* Ref case */
4153 MONO_START_BB (cfg, is_ref_bb);
4155 /* Save the ref to a temporary */
4156 dreg = alloc_ireg (cfg);
4157 EMIT_NEW_VARLOADA_VREG (cfg, addr, dreg, &klass->byval_arg);
4158 addr->dreg = addr_reg;
4159 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, obj->dreg);
4160 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4162 /* Nullable case */
4163 MONO_START_BB (cfg, is_nullable_bb);
4166 MonoInst *addr = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_NULLABLE_CLASS_UNBOX);
4167 MonoInst *unbox_call;
4168 MonoMethodSignature *unbox_sig;
4170 unbox_sig = (MonoMethodSignature *)mono_mempool_alloc0 (cfg->mempool, MONO_SIZEOF_METHOD_SIGNATURE + (1 * sizeof (MonoType *)));
4171 unbox_sig->ret = &klass->byval_arg;
4172 unbox_sig->param_count = 1;
4173 unbox_sig->params [0] = &mono_defaults.object_class->byval_arg;
4175 if (cfg->llvm_only)
4176 unbox_call = emit_llvmonly_calli (cfg, unbox_sig, &obj, addr);
4177 else
4178 unbox_call = mono_emit_calli (cfg, unbox_sig, &obj, addr, NULL, NULL);
4180 EMIT_NEW_VARLOADA_VREG (cfg, addr, unbox_call->dreg, &klass->byval_arg);
4181 addr->dreg = addr_reg;
4184 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4186 /* End */
4187 MONO_START_BB (cfg, end_bb);
4189 /* LDOBJ */
4190 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr_reg, 0);
4192 return ins;
4196 * Returns NULL and set the cfg exception on error.
4198 static MonoInst*
4199 handle_alloc (MonoCompile *cfg, MonoClass *klass, gboolean for_box, int context_used)
4201 MonoInst *iargs [2];
4202 void *alloc_ftn;
4204 if (context_used) {
4205 MonoInst *data;
4206 MonoRgctxInfoType rgctx_info;
4207 MonoInst *iargs [2];
4208 gboolean known_instance_size = !mini_is_gsharedvt_klass (klass);
4210 MonoMethod *managed_alloc = mono_gc_get_managed_allocator (klass, for_box, known_instance_size);
4212 if (cfg->opt & MONO_OPT_SHARED)
4213 rgctx_info = MONO_RGCTX_INFO_KLASS;
4214 else
4215 rgctx_info = MONO_RGCTX_INFO_VTABLE;
4216 data = emit_get_rgctx_klass (cfg, context_used, klass, rgctx_info);
4218 if (cfg->opt & MONO_OPT_SHARED) {
4219 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
4220 iargs [1] = data;
4221 alloc_ftn = ves_icall_object_new;
4222 } else {
4223 iargs [0] = data;
4224 alloc_ftn = ves_icall_object_new_specific;
4227 if (managed_alloc && !(cfg->opt & MONO_OPT_SHARED)) {
4228 if (known_instance_size) {
4229 int size = mono_class_instance_size (klass);
4230 if (size < sizeof (MonoObject))
4231 g_error ("Invalid size %d for class %s", size, mono_type_get_full_name (klass));
4233 EMIT_NEW_ICONST (cfg, iargs [1], size);
4235 return mono_emit_method_call (cfg, managed_alloc, iargs, NULL);
4238 return mono_emit_jit_icall (cfg, alloc_ftn, iargs);
4241 if (cfg->opt & MONO_OPT_SHARED) {
4242 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
4243 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
4245 alloc_ftn = ves_icall_object_new;
4246 } else if (cfg->compile_aot && cfg->cbb->out_of_line && klass->type_token && klass->image == mono_defaults.corlib && !klass->generic_class) {
4247 /* This happens often in argument checking code, eg. throw new FooException... */
4248 /* Avoid relocations and save some space by calling a helper function specialized to mscorlib */
4249 EMIT_NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (klass->type_token));
4250 return mono_emit_jit_icall (cfg, mono_helper_newobj_mscorlib, iargs);
4251 } else {
4252 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4253 MonoMethod *managed_alloc = NULL;
4254 gboolean pass_lw;
4256 if (!vtable) {
4257 mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD);
4258 cfg->exception_ptr = klass;
4259 return NULL;
4262 managed_alloc = mono_gc_get_managed_allocator (klass, for_box, TRUE);
4264 if (managed_alloc) {
4265 int size = mono_class_instance_size (klass);
4266 if (size < sizeof (MonoObject))
4267 g_error ("Invalid size %d for class %s", size, mono_type_get_full_name (klass));
4269 EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
4270 EMIT_NEW_ICONST (cfg, iargs [1], size);
4271 return mono_emit_method_call (cfg, managed_alloc, iargs, NULL);
4273 alloc_ftn = mono_class_get_allocation_ftn (vtable, for_box, &pass_lw);
4274 if (pass_lw) {
4275 guint32 lw = vtable->klass->instance_size;
4276 lw = ((lw + (sizeof (gpointer) - 1)) & ~(sizeof (gpointer) - 1)) / sizeof (gpointer);
4277 EMIT_NEW_ICONST (cfg, iargs [0], lw);
4278 EMIT_NEW_VTABLECONST (cfg, iargs [1], vtable);
4280 else {
4281 EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
4285 return mono_emit_jit_icall (cfg, alloc_ftn, iargs);
4289 * Returns NULL and set the cfg exception on error.
4291 static MonoInst*
4292 handle_box (MonoCompile *cfg, MonoInst *val, MonoClass *klass, int context_used)
4294 MonoInst *alloc, *ins;
4296 if (mono_class_is_nullable (klass)) {
4297 MonoMethod* method = mono_class_get_method_from_name (klass, "Box", 1);
4299 if (context_used) {
4300 if (cfg->llvm_only && cfg->gsharedvt) {
4301 MonoInst *addr = emit_get_rgctx_method (cfg, context_used, method,
4302 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
4303 return emit_llvmonly_calli (cfg, mono_method_signature (method), &val, addr);
4304 } else {
4305 /* FIXME: What if the class is shared? We might not
4306 have to get the method address from the RGCTX. */
4307 MonoInst *addr = emit_get_rgctx_method (cfg, context_used, method,
4308 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
4309 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
4311 return mono_emit_calli (cfg, mono_method_signature (method), &val, addr, NULL, rgctx);
4313 } else {
4314 gboolean pass_vtable, pass_mrgctx;
4315 MonoInst *rgctx_arg = NULL;
4317 check_method_sharing (cfg, method, &pass_vtable, &pass_mrgctx);
4318 g_assert (!pass_mrgctx);
4320 if (pass_vtable) {
4321 MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
4323 g_assert (vtable);
4324 EMIT_NEW_VTABLECONST (cfg, rgctx_arg, vtable);
4327 return mono_emit_method_call_full (cfg, method, NULL, FALSE, &val, NULL, NULL, rgctx_arg);
4331 if (mini_is_gsharedvt_klass (klass)) {
4332 MonoBasicBlock *is_ref_bb, *is_nullable_bb, *end_bb;
4333 MonoInst *res, *is_ref, *src_var, *addr;
4334 int dreg;
4336 dreg = alloc_ireg (cfg);
4338 NEW_BBLOCK (cfg, is_ref_bb);
4339 NEW_BBLOCK (cfg, is_nullable_bb);
4340 NEW_BBLOCK (cfg, end_bb);
4341 is_ref = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_CLASS_BOX_TYPE);
4342 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_REF);
4343 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
4345 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_NULLABLE);
4346 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_nullable_bb);
4348 /* Non-ref case */
4349 alloc = handle_alloc (cfg, klass, TRUE, context_used);
4350 if (!alloc)
4351 return NULL;
4352 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, alloc->dreg, sizeof (MonoObject), val->dreg);
4353 ins->opcode = OP_STOREV_MEMBASE;
4355 EMIT_NEW_UNALU (cfg, res, OP_MOVE, dreg, alloc->dreg);
4356 res->type = STACK_OBJ;
4357 res->klass = klass;
4358 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4360 /* Ref case */
4361 MONO_START_BB (cfg, is_ref_bb);
4363 /* val is a vtype, so has to load the value manually */
4364 src_var = get_vreg_to_inst (cfg, val->dreg);
4365 if (!src_var)
4366 src_var = mono_compile_create_var_for_vreg (cfg, &klass->byval_arg, OP_LOCAL, val->dreg);
4367 EMIT_NEW_VARLOADA (cfg, addr, src_var, src_var->inst_vtype);
4368 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, addr->dreg, 0);
4369 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4371 /* Nullable case */
4372 MONO_START_BB (cfg, is_nullable_bb);
4375 MonoInst *addr = emit_get_gsharedvt_info_klass (cfg, klass,
4376 MONO_RGCTX_INFO_NULLABLE_CLASS_BOX);
4377 MonoInst *box_call;
4378 MonoMethodSignature *box_sig;
4381 * klass is Nullable<T>, need to call Nullable<T>.Box () using a gsharedvt signature, but we cannot
4382 * construct that method at JIT time, so have to do things by hand.
4384 box_sig = (MonoMethodSignature *)mono_mempool_alloc0 (cfg->mempool, MONO_SIZEOF_METHOD_SIGNATURE + (1 * sizeof (MonoType *)));
4385 box_sig->ret = &mono_defaults.object_class->byval_arg;
4386 box_sig->param_count = 1;
4387 box_sig->params [0] = &klass->byval_arg;
4389 if (cfg->llvm_only)
4390 box_call = emit_llvmonly_calli (cfg, box_sig, &val, addr);
4391 else
4392 box_call = mono_emit_calli (cfg, box_sig, &val, addr, NULL, NULL);
4393 EMIT_NEW_UNALU (cfg, res, OP_MOVE, dreg, box_call->dreg);
4394 res->type = STACK_OBJ;
4395 res->klass = klass;
4398 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4400 MONO_START_BB (cfg, end_bb);
4402 return res;
4403 } else {
4404 alloc = handle_alloc (cfg, klass, TRUE, context_used);
4405 if (!alloc)
4406 return NULL;
4408 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, alloc->dreg, sizeof (MonoObject), val->dreg);
4409 return alloc;
4413 static gboolean
4414 mini_class_has_reference_variant_generic_argument (MonoCompile *cfg, MonoClass *klass, int context_used)
4416 int i;
4417 MonoGenericContainer *container;
4418 MonoGenericInst *ginst;
4420 if (klass->generic_class) {
4421 container = klass->generic_class->container_class->generic_container;
4422 ginst = klass->generic_class->context.class_inst;
4423 } else if (klass->generic_container && context_used) {
4424 container = klass->generic_container;
4425 ginst = container->context.class_inst;
4426 } else {
4427 return FALSE;
4430 for (i = 0; i < container->type_argc; ++i) {
4431 MonoType *type;
4432 if (!(mono_generic_container_get_param_info (container, i)->flags & (MONO_GEN_PARAM_VARIANT|MONO_GEN_PARAM_COVARIANT)))
4433 continue;
4434 type = ginst->type_argv [i];
4435 if (mini_type_is_reference (type))
4436 return TRUE;
4438 return FALSE;
4441 static GHashTable* direct_icall_type_hash;
4443 static gboolean
4444 icall_is_direct_callable (MonoCompile *cfg, MonoMethod *cmethod)
4446 /* LLVM on amd64 can't handle calls to non-32 bit addresses */
4447 if (!direct_icalls_enabled (cfg))
4448 return FALSE;
4451 * An icall is directly callable if it doesn't directly or indirectly call mono_raise_exception ().
4452 * Whitelist a few icalls for now.
4454 if (!direct_icall_type_hash) {
4455 GHashTable *h = g_hash_table_new (g_str_hash, g_str_equal);
4457 g_hash_table_insert (h, (char*)"Decimal", GUINT_TO_POINTER (1));
4458 g_hash_table_insert (h, (char*)"Number", GUINT_TO_POINTER (1));
4459 g_hash_table_insert (h, (char*)"Buffer", GUINT_TO_POINTER (1));
4460 g_hash_table_insert (h, (char*)"Monitor", GUINT_TO_POINTER (1));
4461 mono_memory_barrier ();
4462 direct_icall_type_hash = h;
4465 if (cmethod->klass == mono_defaults.math_class)
4466 return TRUE;
4467 /* No locking needed */
4468 if (cmethod->klass->image == mono_defaults.corlib && g_hash_table_lookup (direct_icall_type_hash, cmethod->klass->name))
4469 return TRUE;
4470 return FALSE;
4473 #define is_complex_isinst(klass) ((klass->flags & TYPE_ATTRIBUTE_INTERFACE) || klass->rank || mono_class_is_nullable (klass) || mono_class_is_marshalbyref (klass) || (klass->flags & TYPE_ATTRIBUTE_SEALED) || klass->byval_arg.type == MONO_TYPE_VAR || klass->byval_arg.type == MONO_TYPE_MVAR)
4475 static MonoInst*
4476 emit_castclass_with_cache (MonoCompile *cfg, MonoClass *klass, MonoInst **args)
4478 MonoMethod *mono_castclass;
4479 MonoInst *res;
4481 mono_castclass = mono_marshal_get_castclass_with_cache ();
4483 save_cast_details (cfg, klass, args [0]->dreg, TRUE);
4484 res = mono_emit_method_call (cfg, mono_castclass, args, NULL);
4485 reset_cast_details (cfg);
4487 return res;
4490 static int
4491 get_castclass_cache_idx (MonoCompile *cfg)
4493 /* Each CASTCLASS_CACHE patch needs a unique index which identifies the call site */
4494 cfg->castclass_cache_index ++;
4495 return (cfg->method_index << 16) | cfg->castclass_cache_index;
4498 static MonoInst*
4499 emit_castclass_with_cache_nonshared (MonoCompile *cfg, MonoInst *obj, MonoClass *klass)
4501 MonoInst *args [3];
4502 int idx;
4504 /* obj */
4505 args [0] = obj;
4507 /* klass */
4508 EMIT_NEW_CLASSCONST (cfg, args [1], klass);
4510 /* inline cache*/
4511 idx = get_castclass_cache_idx (cfg);
4512 args [2] = emit_runtime_constant (cfg, MONO_PATCH_INFO_CASTCLASS_CACHE, GINT_TO_POINTER (idx));
4514 /*The wrapper doesn't inline well so the bloat of inlining doesn't pay off.*/
4515 return emit_castclass_with_cache (cfg, klass, args);
4519 * Returns NULL and set the cfg exception on error.
4521 static MonoInst*
4522 handle_castclass (MonoCompile *cfg, MonoClass *klass, MonoInst *src, guint8 *ip, int *inline_costs)
4524 MonoBasicBlock *is_null_bb;
4525 int obj_reg = src->dreg;
4526 int vtable_reg = alloc_preg (cfg);
4527 int context_used;
4528 MonoInst *klass_inst = NULL, *res;
4530 context_used = mini_class_check_context_used (cfg, klass);
4532 if (!context_used && mini_class_has_reference_variant_generic_argument (cfg, klass, context_used)) {
4533 res = emit_castclass_with_cache_nonshared (cfg, src, klass);
4534 (*inline_costs) += 2;
4535 return res;
4536 } else if (!context_used && (mono_class_is_marshalbyref (klass) || klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
4537 MonoMethod *mono_castclass;
4538 MonoInst *iargs [1];
4539 int costs;
4541 mono_castclass = mono_marshal_get_castclass (klass);
4542 iargs [0] = src;
4544 save_cast_details (cfg, klass, src->dreg, TRUE);
4545 costs = inline_method (cfg, mono_castclass, mono_method_signature (mono_castclass),
4546 iargs, ip, cfg->real_offset, TRUE);
4547 reset_cast_details (cfg);
4548 CHECK_CFG_EXCEPTION;
4549 g_assert (costs > 0);
4551 cfg->real_offset += 5;
4553 (*inline_costs) += costs;
4555 return src;
4558 if (context_used) {
4559 MonoInst *args [3];
4561 if(mini_class_has_reference_variant_generic_argument (cfg, klass, context_used) || is_complex_isinst (klass)) {
4562 MonoInst *cache_ins;
4564 cache_ins = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_CAST_CACHE);
4566 /* obj */
4567 args [0] = src;
4569 /* klass - it's the second element of the cache entry*/
4570 EMIT_NEW_LOAD_MEMBASE (cfg, args [1], OP_LOAD_MEMBASE, alloc_preg (cfg), cache_ins->dreg, sizeof (gpointer));
4572 /* cache */
4573 args [2] = cache_ins;
4575 return emit_castclass_with_cache (cfg, klass, args);
4578 klass_inst = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
4581 NEW_BBLOCK (cfg, is_null_bb);
4583 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
4584 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
4586 save_cast_details (cfg, klass, obj_reg, FALSE);
4588 if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4589 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4590 mini_emit_iface_cast (cfg, vtable_reg, klass, NULL, NULL);
4591 } else {
4592 int klass_reg = alloc_preg (cfg);
4594 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4596 if (!klass->rank && !cfg->compile_aot && !(cfg->opt & MONO_OPT_SHARED) && (klass->flags & TYPE_ATTRIBUTE_SEALED)) {
4597 /* the remoting code is broken, access the class for now */
4598 if (0) { /*FIXME what exactly is broken? This change refers to r39380 from 2005 and mention some remoting fixes were due.*/
4599 MonoVTable *vt = mono_class_vtable (cfg->domain, klass);
4600 if (!vt) {
4601 mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD);
4602 cfg->exception_ptr = klass;
4603 return NULL;
4605 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, vtable_reg, vt);
4606 } else {
4607 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4608 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, klass_reg, klass);
4610 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
4611 } else {
4612 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4613 mini_emit_castclass_inst (cfg, obj_reg, klass_reg, klass, klass_inst, is_null_bb);
4617 MONO_START_BB (cfg, is_null_bb);
4619 reset_cast_details (cfg);
4621 return src;
4623 exception_exit:
4624 return NULL;
4628 * Returns NULL and set the cfg exception on error.
4630 static MonoInst*
4631 handle_isinst (MonoCompile *cfg, MonoClass *klass, MonoInst *src, int context_used)
4633 MonoInst *ins;
4634 MonoBasicBlock *is_null_bb, *false_bb, *end_bb;
4635 int obj_reg = src->dreg;
4636 int vtable_reg = alloc_preg (cfg);
4637 int res_reg = alloc_ireg_ref (cfg);
4638 MonoInst *klass_inst = NULL;
4640 if (context_used) {
4641 MonoInst *args [3];
4643 if(mini_class_has_reference_variant_generic_argument (cfg, klass, context_used) || is_complex_isinst (klass)) {
4644 MonoMethod *mono_isinst = mono_marshal_get_isinst_with_cache ();
4645 MonoInst *cache_ins;
4647 cache_ins = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_CAST_CACHE);
4649 /* obj */
4650 args [0] = src;
4652 /* klass - it's the second element of the cache entry*/
4653 EMIT_NEW_LOAD_MEMBASE (cfg, args [1], OP_LOAD_MEMBASE, alloc_preg (cfg), cache_ins->dreg, sizeof (gpointer));
4655 /* cache */
4656 args [2] = cache_ins;
4658 return mono_emit_method_call (cfg, mono_isinst, args, NULL);
4661 klass_inst = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
4664 NEW_BBLOCK (cfg, is_null_bb);
4665 NEW_BBLOCK (cfg, false_bb);
4666 NEW_BBLOCK (cfg, end_bb);
4668 /* Do the assignment at the beginning, so the other assignment can be if converted */
4669 EMIT_NEW_UNALU (cfg, ins, OP_MOVE, res_reg, obj_reg);
4670 ins->type = STACK_OBJ;
4671 ins->klass = klass;
4673 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
4674 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_null_bb);
4676 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4678 if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4679 g_assert (!context_used);
4680 /* the is_null_bb target simply copies the input register to the output */
4681 mini_emit_iface_cast (cfg, vtable_reg, klass, false_bb, is_null_bb);
4682 } else {
4683 int klass_reg = alloc_preg (cfg);
4685 if (klass->rank) {
4686 int rank_reg = alloc_preg (cfg);
4687 int eclass_reg = alloc_preg (cfg);
4689 g_assert (!context_used);
4690 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, rank_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, rank));
4691 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rank_reg, klass->rank);
4692 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, false_bb);
4693 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4694 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, eclass_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, cast_class));
4695 if (klass->cast_class == mono_defaults.object_class) {
4696 int parent_reg = alloc_preg (cfg);
4697 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, parent_reg, eclass_reg, MONO_STRUCT_OFFSET (MonoClass, parent));
4698 mini_emit_class_check_branch (cfg, parent_reg, mono_defaults.enum_class->parent, OP_PBNE_UN, is_null_bb);
4699 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class, OP_PBEQ, is_null_bb);
4700 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false_bb);
4701 } else if (klass->cast_class == mono_defaults.enum_class->parent) {
4702 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class->parent, OP_PBEQ, is_null_bb);
4703 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class, OP_PBEQ, is_null_bb);
4704 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false_bb);
4705 } else if (klass->cast_class == mono_defaults.enum_class) {
4706 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class, OP_PBEQ, is_null_bb);
4707 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false_bb);
4708 } else if (klass->cast_class->flags & TYPE_ATTRIBUTE_INTERFACE) {
4709 mini_emit_iface_class_cast (cfg, eclass_reg, klass->cast_class, false_bb, is_null_bb);
4710 } else {
4711 if ((klass->rank == 1) && (klass->byval_arg.type == MONO_TYPE_SZARRAY)) {
4712 /* Check that the object is a vector too */
4713 int bounds_reg = alloc_preg (cfg);
4714 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg, obj_reg, MONO_STRUCT_OFFSET (MonoArray, bounds));
4715 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, bounds_reg, 0);
4716 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, false_bb);
4719 /* the is_null_bb target simply copies the input register to the output */
4720 mini_emit_isninst_cast (cfg, eclass_reg, klass->cast_class, false_bb, is_null_bb);
4722 } else if (mono_class_is_nullable (klass)) {
4723 g_assert (!context_used);
4724 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4725 /* the is_null_bb target simply copies the input register to the output */
4726 mini_emit_isninst_cast (cfg, klass_reg, klass->cast_class, false_bb, is_null_bb);
4727 } else {
4728 if (!cfg->compile_aot && !(cfg->opt & MONO_OPT_SHARED) && (klass->flags & TYPE_ATTRIBUTE_SEALED)) {
4729 g_assert (!context_used);
4730 /* the remoting code is broken, access the class for now */
4731 if (0) {/*FIXME what exactly is broken? This change refers to r39380 from 2005 and mention some remoting fixes were due.*/
4732 MonoVTable *vt = mono_class_vtable (cfg->domain, klass);
4733 if (!vt) {
4734 mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD);
4735 cfg->exception_ptr = klass;
4736 return NULL;
4738 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, vtable_reg, vt);
4739 } else {
4740 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4741 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, klass_reg, klass);
4743 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, false_bb);
4744 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, is_null_bb);
4745 } else {
4746 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4747 /* the is_null_bb target simply copies the input register to the output */
4748 mini_emit_isninst_cast_inst (cfg, klass_reg, klass, klass_inst, false_bb, is_null_bb);
4753 MONO_START_BB (cfg, false_bb);
4755 MONO_EMIT_NEW_PCONST (cfg, res_reg, 0);
4756 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4758 MONO_START_BB (cfg, is_null_bb);
4760 MONO_START_BB (cfg, end_bb);
4762 return ins;
4765 static MonoInst*
4766 handle_cisinst (MonoCompile *cfg, MonoClass *klass, MonoInst *src)
4768 /* This opcode takes as input an object reference and a class, and returns:
4769 0) if the object is an instance of the class,
4770 1) if the object is not instance of the class,
4771 2) if the object is a proxy whose type cannot be determined */
4773 MonoInst *ins;
4774 #ifndef DISABLE_REMOTING
4775 MonoBasicBlock *true_bb, *false_bb, *false2_bb, *end_bb, *no_proxy_bb, *interface_fail_bb;
4776 #else
4777 MonoBasicBlock *true_bb, *false_bb, *end_bb;
4778 #endif
4779 int obj_reg = src->dreg;
4780 int dreg = alloc_ireg (cfg);
4781 int tmp_reg;
4782 #ifndef DISABLE_REMOTING
4783 int klass_reg = alloc_preg (cfg);
4784 #endif
4786 NEW_BBLOCK (cfg, true_bb);
4787 NEW_BBLOCK (cfg, false_bb);
4788 NEW_BBLOCK (cfg, end_bb);
4789 #ifndef DISABLE_REMOTING
4790 NEW_BBLOCK (cfg, false2_bb);
4791 NEW_BBLOCK (cfg, no_proxy_bb);
4792 #endif
4794 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
4795 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, false_bb);
4797 if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4798 #ifndef DISABLE_REMOTING
4799 NEW_BBLOCK (cfg, interface_fail_bb);
4800 #endif
4802 tmp_reg = alloc_preg (cfg);
4803 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4804 #ifndef DISABLE_REMOTING
4805 mini_emit_iface_cast (cfg, tmp_reg, klass, interface_fail_bb, true_bb);
4806 MONO_START_BB (cfg, interface_fail_bb);
4807 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4809 mini_emit_class_check_branch (cfg, klass_reg, mono_defaults.transparent_proxy_class, OP_PBNE_UN, false_bb);
4811 tmp_reg = alloc_preg (cfg);
4812 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
4813 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
4814 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, false2_bb);
4815 #else
4816 mini_emit_iface_cast (cfg, tmp_reg, klass, false_bb, true_bb);
4817 #endif
4818 } else {
4819 #ifndef DISABLE_REMOTING
4820 tmp_reg = alloc_preg (cfg);
4821 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4822 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4824 mini_emit_class_check_branch (cfg, klass_reg, mono_defaults.transparent_proxy_class, OP_PBNE_UN, no_proxy_bb);
4825 tmp_reg = alloc_preg (cfg);
4826 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, remote_class));
4827 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoRemoteClass, proxy_class));
4829 tmp_reg = alloc_preg (cfg);
4830 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
4831 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
4832 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, no_proxy_bb);
4834 mini_emit_isninst_cast (cfg, klass_reg, klass, false2_bb, true_bb);
4835 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false2_bb);
4837 MONO_START_BB (cfg, no_proxy_bb);
4839 mini_emit_isninst_cast (cfg, klass_reg, klass, false_bb, true_bb);
4840 #else
4841 g_error ("transparent proxy support is disabled while trying to JIT code that uses it");
4842 #endif
4845 MONO_START_BB (cfg, false_bb);
4847 MONO_EMIT_NEW_ICONST (cfg, dreg, 1);
4848 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4850 #ifndef DISABLE_REMOTING
4851 MONO_START_BB (cfg, false2_bb);
4853 MONO_EMIT_NEW_ICONST (cfg, dreg, 2);
4854 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4855 #endif
4857 MONO_START_BB (cfg, true_bb);
4859 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
4861 MONO_START_BB (cfg, end_bb);
4863 /* FIXME: */
4864 MONO_INST_NEW (cfg, ins, OP_ICONST);
4865 ins->dreg = dreg;
4866 ins->type = STACK_I4;
4868 return ins;
4871 static MonoInst*
4872 handle_ccastclass (MonoCompile *cfg, MonoClass *klass, MonoInst *src)
4874 /* This opcode takes as input an object reference and a class, and returns:
4875 0) if the object is an instance of the class,
4876 1) if the object is a proxy whose type cannot be determined
4877 an InvalidCastException exception is thrown otherwhise*/
4879 MonoInst *ins;
4880 #ifndef DISABLE_REMOTING
4881 MonoBasicBlock *end_bb, *ok_result_bb, *no_proxy_bb, *interface_fail_bb, *fail_1_bb;
4882 #else
4883 MonoBasicBlock *ok_result_bb;
4884 #endif
4885 int obj_reg = src->dreg;
4886 int dreg = alloc_ireg (cfg);
4887 int tmp_reg = alloc_preg (cfg);
4889 #ifndef DISABLE_REMOTING
4890 int klass_reg = alloc_preg (cfg);
4891 NEW_BBLOCK (cfg, end_bb);
4892 #endif
4894 NEW_BBLOCK (cfg, ok_result_bb);
4896 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
4897 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, ok_result_bb);
4899 save_cast_details (cfg, klass, obj_reg, FALSE);
4901 if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4902 #ifndef DISABLE_REMOTING
4903 NEW_BBLOCK (cfg, interface_fail_bb);
4905 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4906 mini_emit_iface_cast (cfg, tmp_reg, klass, interface_fail_bb, ok_result_bb);
4907 MONO_START_BB (cfg, interface_fail_bb);
4908 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4910 mini_emit_class_check (cfg, klass_reg, mono_defaults.transparent_proxy_class);
4912 tmp_reg = alloc_preg (cfg);
4913 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
4914 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
4915 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "InvalidCastException");
4917 MONO_EMIT_NEW_ICONST (cfg, dreg, 1);
4918 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4919 #else
4920 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4921 mini_emit_iface_cast (cfg, tmp_reg, klass, NULL, NULL);
4922 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, ok_result_bb);
4923 #endif
4924 } else {
4925 #ifndef DISABLE_REMOTING
4926 NEW_BBLOCK (cfg, no_proxy_bb);
4928 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4929 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4930 mini_emit_class_check_branch (cfg, klass_reg, mono_defaults.transparent_proxy_class, OP_PBNE_UN, no_proxy_bb);
4932 tmp_reg = alloc_preg (cfg);
4933 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, remote_class));
4934 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoRemoteClass, proxy_class));
4936 tmp_reg = alloc_preg (cfg);
4937 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
4938 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
4939 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, no_proxy_bb);
4941 NEW_BBLOCK (cfg, fail_1_bb);
4943 mini_emit_isninst_cast (cfg, klass_reg, klass, fail_1_bb, ok_result_bb);
4945 MONO_START_BB (cfg, fail_1_bb);
4947 MONO_EMIT_NEW_ICONST (cfg, dreg, 1);
4948 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4950 MONO_START_BB (cfg, no_proxy_bb);
4952 mini_emit_castclass (cfg, obj_reg, klass_reg, klass, ok_result_bb);
4953 #else
4954 g_error ("Transparent proxy support is disabled while trying to JIT code that uses it");
4955 #endif
4958 MONO_START_BB (cfg, ok_result_bb);
4960 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
4962 #ifndef DISABLE_REMOTING
4963 MONO_START_BB (cfg, end_bb);
4964 #endif
4966 /* FIXME: */
4967 MONO_INST_NEW (cfg, ins, OP_ICONST);
4968 ins->dreg = dreg;
4969 ins->type = STACK_I4;
4971 return ins;
4974 static G_GNUC_UNUSED MonoInst*
4975 handle_enum_has_flag (MonoCompile *cfg, MonoClass *klass, MonoInst *enum_this, MonoInst *enum_flag)
4977 MonoType *enum_type = mono_type_get_underlying_type (&klass->byval_arg);
4978 guint32 load_opc = mono_type_to_load_membase (cfg, enum_type);
4979 gboolean is_i4;
4981 switch (enum_type->type) {
4982 case MONO_TYPE_I8:
4983 case MONO_TYPE_U8:
4984 #if SIZEOF_REGISTER == 8
4985 case MONO_TYPE_I:
4986 case MONO_TYPE_U:
4987 #endif
4988 is_i4 = FALSE;
4989 break;
4990 default:
4991 is_i4 = TRUE;
4992 break;
4996 MonoInst *load, *and_, *cmp, *ceq;
4997 int enum_reg = is_i4 ? alloc_ireg (cfg) : alloc_lreg (cfg);
4998 int and_reg = is_i4 ? alloc_ireg (cfg) : alloc_lreg (cfg);
4999 int dest_reg = alloc_ireg (cfg);
5001 EMIT_NEW_LOAD_MEMBASE (cfg, load, load_opc, enum_reg, enum_this->dreg, 0);
5002 EMIT_NEW_BIALU (cfg, and_, is_i4 ? OP_IAND : OP_LAND, and_reg, enum_reg, enum_flag->dreg);
5003 EMIT_NEW_BIALU (cfg, cmp, is_i4 ? OP_ICOMPARE : OP_LCOMPARE, -1, and_reg, enum_flag->dreg);
5004 EMIT_NEW_UNALU (cfg, ceq, is_i4 ? OP_ICEQ : OP_LCEQ, dest_reg, -1);
5006 ceq->type = STACK_I4;
5008 if (!is_i4) {
5009 load = mono_decompose_opcode (cfg, load);
5010 and_ = mono_decompose_opcode (cfg, and_);
5011 cmp = mono_decompose_opcode (cfg, cmp);
5012 ceq = mono_decompose_opcode (cfg, ceq);
5015 return ceq;
5020 * Returns NULL and set the cfg exception on error.
5022 static G_GNUC_UNUSED MonoInst*
5023 handle_delegate_ctor (MonoCompile *cfg, MonoClass *klass, MonoInst *target, MonoMethod *method, int context_used, gboolean virtual_)
5025 MonoInst *ptr;
5026 int dreg;
5027 gpointer trampoline;
5028 MonoInst *obj, *method_ins, *tramp_ins;
5029 MonoDomain *domain;
5030 guint8 **code_slot;
5032 if (virtual_ && !cfg->llvm_only) {
5033 MonoMethod *invoke = mono_get_delegate_invoke (klass);
5034 g_assert (invoke);
5036 if (!mono_get_delegate_virtual_invoke_impl (mono_method_signature (invoke), context_used ? NULL : method))
5037 return NULL;
5040 obj = handle_alloc (cfg, klass, FALSE, mono_class_check_context_used (klass));
5041 if (!obj)
5042 return NULL;
5044 /* Inline the contents of mono_delegate_ctor */
5046 /* Set target field */
5047 /* Optimize away setting of NULL target */
5048 if (!(target->opcode == OP_PCONST && target->inst_p0 == 0)) {
5049 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, target), target->dreg);
5050 if (cfg->gen_write_barriers) {
5051 dreg = alloc_preg (cfg);
5052 EMIT_NEW_BIALU_IMM (cfg, ptr, OP_PADD_IMM, dreg, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, target));
5053 emit_write_barrier (cfg, ptr, target);
5057 /* Set method field */
5058 method_ins = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_METHOD);
5059 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method), method_ins->dreg);
5062 * To avoid looking up the compiled code belonging to the target method
5063 * in mono_delegate_trampoline (), we allocate a per-domain memory slot to
5064 * store it, and we fill it after the method has been compiled.
5066 if (!method->dynamic && !(cfg->opt & MONO_OPT_SHARED)) {
5067 MonoInst *code_slot_ins;
5069 if (context_used) {
5070 code_slot_ins = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_METHOD_DELEGATE_CODE);
5071 } else {
5072 domain = mono_domain_get ();
5073 mono_domain_lock (domain);
5074 if (!domain_jit_info (domain)->method_code_hash)
5075 domain_jit_info (domain)->method_code_hash = g_hash_table_new (NULL, NULL);
5076 code_slot = (guint8 **)g_hash_table_lookup (domain_jit_info (domain)->method_code_hash, method);
5077 if (!code_slot) {
5078 code_slot = (guint8 **)mono_domain_alloc0 (domain, sizeof (gpointer));
5079 g_hash_table_insert (domain_jit_info (domain)->method_code_hash, method, code_slot);
5081 mono_domain_unlock (domain);
5083 code_slot_ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_METHOD_CODE_SLOT, method);
5085 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_code), code_slot_ins->dreg);
5088 if (cfg->llvm_only) {
5089 MonoInst *args [16];
5091 if (virtual_) {
5092 args [0] = obj;
5093 args [1] = target;
5094 args [2] = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_METHOD);
5095 mono_emit_jit_icall (cfg, mono_llvmonly_init_delegate_virtual, args);
5096 } else {
5097 args [0] = obj;
5098 mono_emit_jit_icall (cfg, mono_llvmonly_init_delegate, args);
5101 return obj;
5104 if (cfg->compile_aot) {
5105 MonoDelegateClassMethodPair *del_tramp;
5107 del_tramp = (MonoDelegateClassMethodPair *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoDelegateClassMethodPair));
5108 del_tramp->klass = klass;
5109 del_tramp->method = context_used ? NULL : method;
5110 del_tramp->is_virtual = virtual_;
5111 EMIT_NEW_AOTCONST (cfg, tramp_ins, MONO_PATCH_INFO_DELEGATE_TRAMPOLINE, del_tramp);
5112 } else {
5113 if (virtual_)
5114 trampoline = mono_create_delegate_virtual_trampoline (cfg->domain, klass, context_used ? NULL : method);
5115 else
5116 trampoline = mono_create_delegate_trampoline_info (cfg->domain, klass, context_used ? NULL : method);
5117 EMIT_NEW_PCONST (cfg, tramp_ins, trampoline);
5120 /* Set invoke_impl field */
5121 if (virtual_) {
5122 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl), tramp_ins->dreg);
5123 } else {
5124 dreg = alloc_preg (cfg);
5125 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, tramp_ins->dreg, MONO_STRUCT_OFFSET (MonoDelegateTrampInfo, invoke_impl));
5126 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl), dreg);
5128 dreg = alloc_preg (cfg);
5129 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, tramp_ins->dreg, MONO_STRUCT_OFFSET (MonoDelegateTrampInfo, method_ptr));
5130 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr), dreg);
5133 dreg = alloc_preg (cfg);
5134 MONO_EMIT_NEW_ICONST (cfg, dreg, virtual_ ? 1 : 0);
5135 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_is_virtual), dreg);
5137 /* All the checks which are in mono_delegate_ctor () are done by the delegate trampoline */
5139 return obj;
5142 static MonoInst*
5143 handle_array_new (MonoCompile *cfg, int rank, MonoInst **sp, unsigned char *ip)
5145 MonoJitICallInfo *info;
5147 /* Need to register the icall so it gets an icall wrapper */
5148 info = mono_get_array_new_va_icall (rank);
5150 cfg->flags |= MONO_CFG_HAS_VARARGS;
5152 /* mono_array_new_va () needs a vararg calling convention */
5153 cfg->exception_message = g_strdup ("array-new");
5154 cfg->disable_llvm = TRUE;
5156 /* FIXME: This uses info->sig, but it should use the signature of the wrapper */
5157 return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, sp);
5161 * handle_constrained_gsharedvt_call:
5163 * Handle constrained calls where the receiver is a gsharedvt type.
5164 * Return the instruction representing the call. Set the cfg exception on failure.
5166 static MonoInst*
5167 handle_constrained_gsharedvt_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp, MonoClass *constrained_class,
5168 gboolean *ref_emit_widen)
5170 MonoInst *ins = NULL;
5171 gboolean emit_widen = *ref_emit_widen;
5174 * Constrained calls need to behave differently at runtime dependending on whenever the receiver is instantiated as ref type or as a vtype.
5175 * This is hard to do with the current call code, since we would have to emit a branch and two different calls. So instead, we
5176 * pack the arguments into an array, and do the rest of the work in in an icall.
5178 if (((cmethod->klass == mono_defaults.object_class) || (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE) || (!cmethod->klass->valuetype && cmethod->klass->image != mono_defaults.corlib)) &&
5179 (MONO_TYPE_IS_VOID (fsig->ret) || MONO_TYPE_IS_PRIMITIVE (fsig->ret) || MONO_TYPE_IS_REFERENCE (fsig->ret) || MONO_TYPE_ISSTRUCT (fsig->ret) || mini_is_gsharedvt_type (fsig->ret)) &&
5180 (fsig->param_count == 0 || (!fsig->hasthis && fsig->param_count == 1) || (fsig->param_count == 1 && (MONO_TYPE_IS_REFERENCE (fsig->params [0]) || fsig->params [0]->byref || mini_is_gsharedvt_type (fsig->params [0]))))) {
5181 MonoInst *args [16];
5184 * This case handles calls to
5185 * - object:ToString()/Equals()/GetHashCode(),
5186 * - System.IComparable<T>:CompareTo()
5187 * - System.IEquatable<T>:Equals ()
5188 * plus some simple interface calls enough to support AsyncTaskMethodBuilder.
5191 args [0] = sp [0];
5192 if (mono_method_check_context_used (cmethod))
5193 args [1] = emit_get_rgctx_method (cfg, mono_method_check_context_used (cmethod), cmethod, MONO_RGCTX_INFO_METHOD);
5194 else
5195 EMIT_NEW_METHODCONST (cfg, args [1], cmethod);
5196 args [2] = emit_get_rgctx_klass (cfg, mono_class_check_context_used (constrained_class), constrained_class, MONO_RGCTX_INFO_KLASS);
5198 /* !fsig->hasthis is for the wrapper for the Object.GetType () icall */
5199 if (fsig->hasthis && fsig->param_count) {
5200 /* Pass the arguments using a localloc-ed array using the format expected by runtime_invoke () */
5201 MONO_INST_NEW (cfg, ins, OP_LOCALLOC_IMM);
5202 ins->dreg = alloc_preg (cfg);
5203 ins->inst_imm = fsig->param_count * sizeof (mgreg_t);
5204 MONO_ADD_INS (cfg->cbb, ins);
5205 args [4] = ins;
5207 if (mini_is_gsharedvt_type (fsig->params [0])) {
5208 int addr_reg, deref_arg_reg;
5210 ins = emit_get_gsharedvt_info_klass (cfg, mono_class_from_mono_type (fsig->params [0]), MONO_RGCTX_INFO_CLASS_BOX_TYPE);
5211 deref_arg_reg = alloc_preg (cfg);
5212 /* deref_arg = BOX_TYPE != MONO_GSHAREDVT_BOX_TYPE_VTYPE */
5213 EMIT_NEW_BIALU_IMM (cfg, args [3], OP_ISUB_IMM, deref_arg_reg, ins->dreg, 1);
5215 EMIT_NEW_VARLOADA_VREG (cfg, ins, sp [1]->dreg, fsig->params [0]);
5216 addr_reg = ins->dreg;
5217 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, args [4]->dreg, 0, addr_reg);
5218 } else {
5219 EMIT_NEW_ICONST (cfg, args [3], 0);
5220 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, args [4]->dreg, 0, sp [1]->dreg);
5222 } else {
5223 EMIT_NEW_ICONST (cfg, args [3], 0);
5224 EMIT_NEW_ICONST (cfg, args [4], 0);
5226 ins = mono_emit_jit_icall (cfg, mono_gsharedvt_constrained_call, args);
5227 emit_widen = FALSE;
5229 if (mini_is_gsharedvt_type (fsig->ret)) {
5230 ins = handle_unbox_gsharedvt (cfg, mono_class_from_mono_type (fsig->ret), ins);
5231 } else if (MONO_TYPE_IS_PRIMITIVE (fsig->ret) || MONO_TYPE_ISSTRUCT (fsig->ret)) {
5232 MonoInst *add;
5234 /* Unbox */
5235 NEW_BIALU_IMM (cfg, add, OP_ADD_IMM, alloc_dreg (cfg, STACK_MP), ins->dreg, sizeof (MonoObject));
5236 MONO_ADD_INS (cfg->cbb, add);
5237 /* Load value */
5238 NEW_LOAD_MEMBASE_TYPE (cfg, ins, fsig->ret, add->dreg, 0);
5239 MONO_ADD_INS (cfg->cbb, ins);
5240 /* ins represents the call result */
5242 } else {
5243 GSHAREDVT_FAILURE (CEE_CALLVIRT);
5246 *ref_emit_widen = emit_widen;
5248 return ins;
5250 exception_exit:
5251 return NULL;
5254 static void
5255 mono_emit_load_got_addr (MonoCompile *cfg)
5257 MonoInst *getaddr, *dummy_use;
5259 if (!cfg->got_var || cfg->got_var_allocated)
5260 return;
5262 MONO_INST_NEW (cfg, getaddr, OP_LOAD_GOTADDR);
5263 getaddr->cil_code = cfg->header->code;
5264 getaddr->dreg = cfg->got_var->dreg;
5266 /* Add it to the start of the first bblock */
5267 if (cfg->bb_entry->code) {
5268 getaddr->next = cfg->bb_entry->code;
5269 cfg->bb_entry->code = getaddr;
5271 else
5272 MONO_ADD_INS (cfg->bb_entry, getaddr);
5274 cfg->got_var_allocated = TRUE;
5277 * Add a dummy use to keep the got_var alive, since real uses might
5278 * only be generated by the back ends.
5279 * Add it to end_bblock, so the variable's lifetime covers the whole
5280 * method.
5281 * It would be better to make the usage of the got var explicit in all
5282 * cases when the backend needs it (i.e. calls, throw etc.), so this
5283 * wouldn't be needed.
5285 NEW_DUMMY_USE (cfg, dummy_use, cfg->got_var);
5286 MONO_ADD_INS (cfg->bb_exit, dummy_use);
5289 static int inline_limit;
5290 static gboolean inline_limit_inited;
5292 static gboolean
5293 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
5295 MonoMethodHeaderSummary header;
5296 MonoVTable *vtable;
5297 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
5298 MonoMethodSignature *sig = mono_method_signature (method);
5299 int i;
5300 #endif
5302 if (cfg->disable_inline)
5303 return FALSE;
5304 if (cfg->gshared)
5305 return FALSE;
5307 if (cfg->inline_depth > 10)
5308 return FALSE;
5310 if (!mono_method_get_header_summary (method, &header))
5311 return FALSE;
5313 /*runtime, icall and pinvoke are checked by summary call*/
5314 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
5315 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
5316 (mono_class_is_marshalbyref (method->klass)) ||
5317 header.has_clauses)
5318 return FALSE;
5320 /* also consider num_locals? */
5321 /* Do the size check early to avoid creating vtables */
5322 if (!inline_limit_inited) {
5323 if (g_getenv ("MONO_INLINELIMIT"))
5324 inline_limit = atoi (g_getenv ("MONO_INLINELIMIT"));
5325 else
5326 inline_limit = INLINE_LENGTH_LIMIT;
5327 inline_limit_inited = TRUE;
5329 if (header.code_size >= inline_limit && !(method->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING))
5330 return FALSE;
5333 * if we can initialize the class of the method right away, we do,
5334 * otherwise we don't allow inlining if the class needs initialization,
5335 * since it would mean inserting a call to mono_runtime_class_init()
5336 * inside the inlined code
5338 if (!(cfg->opt & MONO_OPT_SHARED)) {
5339 /* The AggressiveInlining hint is a good excuse to force that cctor to run. */
5340 if (method->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING) {
5341 vtable = mono_class_vtable (cfg->domain, method->klass);
5342 if (!vtable)
5343 return FALSE;
5344 if (!cfg->compile_aot) {
5345 MonoError error;
5346 if (!mono_runtime_class_init_full (vtable, &error)) {
5347 mono_error_cleanup (&error);
5348 return FALSE;
5351 } else if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
5352 if (cfg->run_cctors && method->klass->has_cctor) {
5353 /*FIXME it would easier and lazier to just use mono_class_try_get_vtable */
5354 if (!method->klass->runtime_info)
5355 /* No vtable created yet */
5356 return FALSE;
5357 vtable = mono_class_vtable (cfg->domain, method->klass);
5358 if (!vtable)
5359 return FALSE;
5360 /* This makes so that inline cannot trigger */
5361 /* .cctors: too many apps depend on them */
5362 /* running with a specific order... */
5363 if (! vtable->initialized)
5364 return FALSE;
5365 MonoError error;
5366 if (!mono_runtime_class_init_full (vtable, &error)) {
5367 mono_error_cleanup (&error);
5368 return FALSE;
5371 } else if (mono_class_needs_cctor_run (method->klass, NULL)) {
5372 if (!method->klass->runtime_info)
5373 /* No vtable created yet */
5374 return FALSE;
5375 vtable = mono_class_vtable (cfg->domain, method->klass);
5376 if (!vtable)
5377 return FALSE;
5378 if (!vtable->initialized)
5379 return FALSE;
5381 } else {
5383 * If we're compiling for shared code
5384 * the cctor will need to be run at aot method load time, for example,
5385 * or at the end of the compilation of the inlining method.
5387 if (mono_class_needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
5388 return FALSE;
5391 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
5392 if (mono_arch_is_soft_float ()) {
5393 /* FIXME: */
5394 if (sig->ret && sig->ret->type == MONO_TYPE_R4)
5395 return FALSE;
5396 for (i = 0; i < sig->param_count; ++i)
5397 if (!sig->params [i]->byref && sig->params [i]->type == MONO_TYPE_R4)
5398 return FALSE;
5400 #endif
5402 if (g_list_find (cfg->dont_inline, method))
5403 return FALSE;
5405 return TRUE;
5408 static gboolean
5409 mini_field_access_needs_cctor_run (MonoCompile *cfg, MonoMethod *method, MonoClass *klass, MonoVTable *vtable)
5411 if (!cfg->compile_aot) {
5412 g_assert (vtable);
5413 if (vtable->initialized)
5414 return FALSE;
5417 if (klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
5418 if (cfg->method == method)
5419 return FALSE;
5422 if (!mono_class_needs_cctor_run (klass, method))
5423 return FALSE;
5425 if (! (method->flags & METHOD_ATTRIBUTE_STATIC) && (klass == method->klass))
5426 /* The initialization is already done before the method is called */
5427 return FALSE;
5429 return TRUE;
5432 static MonoInst*
5433 mini_emit_ldelema_1_ins (MonoCompile *cfg, MonoClass *klass, MonoInst *arr, MonoInst *index, gboolean bcheck)
5435 MonoInst *ins;
5436 guint32 size;
5437 int mult_reg, add_reg, array_reg, index_reg, index2_reg;
5438 int context_used;
5440 if (mini_is_gsharedvt_variable_klass (klass)) {
5441 size = -1;
5442 } else {
5443 mono_class_init (klass);
5444 size = mono_class_array_element_size (klass);
5447 mult_reg = alloc_preg (cfg);
5448 array_reg = arr->dreg;
5449 index_reg = index->dreg;
5451 #if SIZEOF_REGISTER == 8
5452 /* The array reg is 64 bits but the index reg is only 32 */
5453 if (COMPILE_LLVM (cfg)) {
5454 /* Not needed */
5455 index2_reg = index_reg;
5456 } else {
5457 index2_reg = alloc_preg (cfg);
5458 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, index2_reg, index_reg);
5460 #else
5461 if (index->type == STACK_I8) {
5462 index2_reg = alloc_preg (cfg);
5463 MONO_EMIT_NEW_UNALU (cfg, OP_LCONV_TO_I4, index2_reg, index_reg);
5464 } else {
5465 index2_reg = index_reg;
5467 #endif
5469 if (bcheck)
5470 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index2_reg);
5472 #if defined(TARGET_X86) || defined(TARGET_AMD64)
5473 if (size == 1 || size == 2 || size == 4 || size == 8) {
5474 static const int fast_log2 [] = { 1, 0, 1, -1, 2, -1, -1, -1, 3 };
5476 EMIT_NEW_X86_LEA (cfg, ins, array_reg, index2_reg, fast_log2 [size], MONO_STRUCT_OFFSET (MonoArray, vector));
5477 ins->klass = mono_class_get_element_class (klass);
5478 ins->type = STACK_MP;
5480 return ins;
5482 #endif
5484 add_reg = alloc_ireg_mp (cfg);
5486 if (size == -1) {
5487 MonoInst *rgctx_ins;
5489 /* gsharedvt */
5490 g_assert (cfg->gshared);
5491 context_used = mini_class_check_context_used (cfg, klass);
5492 g_assert (context_used);
5493 rgctx_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_ARRAY_ELEMENT_SIZE);
5494 MONO_EMIT_NEW_BIALU (cfg, OP_IMUL, mult_reg, index2_reg, rgctx_ins->dreg);
5495 } else {
5496 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_MUL_IMM, mult_reg, index2_reg, size);
5498 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, array_reg, mult_reg);
5499 NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, add_reg, add_reg, MONO_STRUCT_OFFSET (MonoArray, vector));
5500 ins->klass = mono_class_get_element_class (klass);
5501 ins->type = STACK_MP;
5502 MONO_ADD_INS (cfg->cbb, ins);
5504 return ins;
5507 static MonoInst*
5508 mini_emit_ldelema_2_ins (MonoCompile *cfg, MonoClass *klass, MonoInst *arr, MonoInst *index_ins1, MonoInst *index_ins2)
5510 int bounds_reg = alloc_preg (cfg);
5511 int add_reg = alloc_ireg_mp (cfg);
5512 int mult_reg = alloc_preg (cfg);
5513 int mult2_reg = alloc_preg (cfg);
5514 int low1_reg = alloc_preg (cfg);
5515 int low2_reg = alloc_preg (cfg);
5516 int high1_reg = alloc_preg (cfg);
5517 int high2_reg = alloc_preg (cfg);
5518 int realidx1_reg = alloc_preg (cfg);
5519 int realidx2_reg = alloc_preg (cfg);
5520 int sum_reg = alloc_preg (cfg);
5521 int index1, index2, tmpreg;
5522 MonoInst *ins;
5523 guint32 size;
5525 mono_class_init (klass);
5526 size = mono_class_array_element_size (klass);
5528 index1 = index_ins1->dreg;
5529 index2 = index_ins2->dreg;
5531 #if SIZEOF_REGISTER == 8
5532 /* The array reg is 64 bits but the index reg is only 32 */
5533 if (COMPILE_LLVM (cfg)) {
5534 /* Not needed */
5535 } else {
5536 tmpreg = alloc_preg (cfg);
5537 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, tmpreg, index1);
5538 index1 = tmpreg;
5539 tmpreg = alloc_preg (cfg);
5540 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, tmpreg, index2);
5541 index2 = tmpreg;
5543 #else
5544 // FIXME: Do we need to do something here for i8 indexes, like in ldelema_1_ins ?
5545 tmpreg = -1;
5546 #endif
5548 /* range checking */
5549 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg,
5550 arr->dreg, MONO_STRUCT_OFFSET (MonoArray, bounds));
5552 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, low1_reg,
5553 bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
5554 MONO_EMIT_NEW_BIALU (cfg, OP_PSUB, realidx1_reg, index1, low1_reg);
5555 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, high1_reg,
5556 bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, length));
5557 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, high1_reg, realidx1_reg);
5558 MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException");
5560 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, low2_reg,
5561 bounds_reg, sizeof (MonoArrayBounds) + MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
5562 MONO_EMIT_NEW_BIALU (cfg, OP_PSUB, realidx2_reg, index2, low2_reg);
5563 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, high2_reg,
5564 bounds_reg, sizeof (MonoArrayBounds) + MONO_STRUCT_OFFSET (MonoArrayBounds, length));
5565 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, high2_reg, realidx2_reg);
5566 MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException");
5568 MONO_EMIT_NEW_BIALU (cfg, OP_PMUL, mult_reg, high2_reg, realidx1_reg);
5569 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, sum_reg, mult_reg, realidx2_reg);
5570 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PMUL_IMM, mult2_reg, sum_reg, size);
5571 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, mult2_reg, arr->dreg);
5572 NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, add_reg, add_reg, MONO_STRUCT_OFFSET (MonoArray, vector));
5574 ins->type = STACK_MP;
5575 ins->klass = klass;
5576 MONO_ADD_INS (cfg->cbb, ins);
5578 return ins;
5581 static MonoInst*
5582 mini_emit_ldelema_ins (MonoCompile *cfg, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
5584 int rank;
5585 MonoInst *addr;
5586 MonoMethod *addr_method;
5587 int element_size;
5588 MonoClass *eclass = cmethod->klass->element_class;
5590 rank = mono_method_signature (cmethod)->param_count - (is_set? 1: 0);
5592 if (rank == 1)
5593 return mini_emit_ldelema_1_ins (cfg, eclass, sp [0], sp [1], TRUE);
5595 /* emit_ldelema_2 depends on OP_LMUL */
5596 if (!cfg->backend->emulate_mul_div && rank == 2 && (cfg->opt & MONO_OPT_INTRINS) && !mini_is_gsharedvt_variable_klass (eclass)) {
5597 return mini_emit_ldelema_2_ins (cfg, eclass, sp [0], sp [1], sp [2]);
5600 if (mini_is_gsharedvt_variable_klass (eclass))
5601 element_size = 0;
5602 else
5603 element_size = mono_class_array_element_size (eclass);
5604 addr_method = mono_marshal_get_array_address (rank, element_size);
5605 addr = mono_emit_method_call (cfg, addr_method, sp, NULL);
5607 return addr;
5610 static MonoBreakPolicy
5611 always_insert_breakpoint (MonoMethod *method)
5613 return MONO_BREAK_POLICY_ALWAYS;
5616 static MonoBreakPolicyFunc break_policy_func = always_insert_breakpoint;
5619 * mono_set_break_policy:
5620 * policy_callback: the new callback function
5622 * Allow embedders to decide wherther to actually obey breakpoint instructions
5623 * (both break IL instructions and Debugger.Break () method calls), for example
5624 * to not allow an app to be aborted by a perfectly valid IL opcode when executing
5625 * untrusted or semi-trusted code.
5627 * @policy_callback will be called every time a break point instruction needs to
5628 * be inserted with the method argument being the method that calls Debugger.Break()
5629 * or has the IL break instruction. The callback should return #MONO_BREAK_POLICY_NEVER
5630 * if it wants the breakpoint to not be effective in the given method.
5631 * #MONO_BREAK_POLICY_ALWAYS is the default.
5633 void
5634 mono_set_break_policy (MonoBreakPolicyFunc policy_callback)
5636 if (policy_callback)
5637 break_policy_func = policy_callback;
5638 else
5639 break_policy_func = always_insert_breakpoint;
5642 static gboolean
5643 should_insert_brekpoint (MonoMethod *method) {
5644 switch (break_policy_func (method)) {
5645 case MONO_BREAK_POLICY_ALWAYS:
5646 return TRUE;
5647 case MONO_BREAK_POLICY_NEVER:
5648 return FALSE;
5649 case MONO_BREAK_POLICY_ON_DBG:
5650 g_warning ("mdb no longer supported");
5651 return FALSE;
5652 default:
5653 g_warning ("Incorrect value returned from break policy callback");
5654 return FALSE;
5658 /* optimize the simple GetGenericValueImpl/SetGenericValueImpl generic icalls */
5659 static MonoInst*
5660 emit_array_generic_access (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, int is_set)
5662 MonoInst *addr, *store, *load;
5663 MonoClass *eklass = mono_class_from_mono_type (fsig->params [2]);
5665 /* the bounds check is already done by the callers */
5666 addr = mini_emit_ldelema_1_ins (cfg, eklass, args [0], args [1], FALSE);
5667 if (is_set) {
5668 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, &eklass->byval_arg, args [2]->dreg, 0);
5669 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, &eklass->byval_arg, addr->dreg, 0, load->dreg);
5670 if (mini_type_is_reference (fsig->params [2]))
5671 emit_write_barrier (cfg, addr, load);
5672 } else {
5673 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, &eklass->byval_arg, addr->dreg, 0);
5674 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, &eklass->byval_arg, args [2]->dreg, 0, load->dreg);
5676 return store;
5680 static gboolean
5681 generic_class_is_reference_type (MonoCompile *cfg, MonoClass *klass)
5683 return mini_type_is_reference (&klass->byval_arg);
5686 static MonoInst*
5687 emit_array_store (MonoCompile *cfg, MonoClass *klass, MonoInst **sp, gboolean safety_checks)
5689 if (safety_checks && generic_class_is_reference_type (cfg, klass) &&
5690 !(sp [2]->opcode == OP_PCONST && sp [2]->inst_p0 == NULL)) {
5691 MonoClass *obj_array = mono_array_class_get_cached (mono_defaults.object_class, 1);
5692 MonoMethod *helper = mono_marshal_get_virtual_stelemref (obj_array);
5693 MonoInst *iargs [3];
5695 if (!helper->slot)
5696 mono_class_setup_vtable (obj_array);
5697 g_assert (helper->slot);
5699 if (sp [0]->type != STACK_OBJ)
5700 return NULL;
5701 if (sp [2]->type != STACK_OBJ)
5702 return NULL;
5704 iargs [2] = sp [2];
5705 iargs [1] = sp [1];
5706 iargs [0] = sp [0];
5708 return mono_emit_method_call (cfg, helper, iargs, sp [0]);
5709 } else {
5710 MonoInst *ins;
5712 if (mini_is_gsharedvt_variable_klass (klass)) {
5713 MonoInst *addr;
5715 // FIXME-VT: OP_ICONST optimization
5716 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
5717 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0, sp [2]->dreg);
5718 ins->opcode = OP_STOREV_MEMBASE;
5719 } else if (sp [1]->opcode == OP_ICONST) {
5720 int array_reg = sp [0]->dreg;
5721 int index_reg = sp [1]->dreg;
5722 int offset = (mono_class_array_element_size (klass) * sp [1]->inst_c0) + MONO_STRUCT_OFFSET (MonoArray, vector);
5724 if (SIZEOF_REGISTER == 8 && COMPILE_LLVM (cfg))
5725 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, index_reg, index_reg);
5727 if (safety_checks)
5728 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index_reg);
5729 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, array_reg, offset, sp [2]->dreg);
5730 } else {
5731 MonoInst *addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], safety_checks);
5732 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0, sp [2]->dreg);
5733 if (generic_class_is_reference_type (cfg, klass))
5734 emit_write_barrier (cfg, addr, sp [2]);
5736 return ins;
5740 static MonoInst*
5741 emit_array_unsafe_access (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, int is_set)
5743 MonoClass *eklass;
5745 if (is_set)
5746 eklass = mono_class_from_mono_type (fsig->params [2]);
5747 else
5748 eklass = mono_class_from_mono_type (fsig->ret);
5750 if (is_set) {
5751 return emit_array_store (cfg, eklass, args, FALSE);
5752 } else {
5753 MonoInst *ins, *addr = mini_emit_ldelema_1_ins (cfg, eklass, args [0], args [1], FALSE);
5754 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &eklass->byval_arg, addr->dreg, 0);
5755 return ins;
5759 static gboolean
5760 is_unsafe_mov_compatible (MonoCompile *cfg, MonoClass *param_klass, MonoClass *return_klass)
5762 uint32_t align;
5763 int param_size, return_size;
5765 param_klass = mono_class_from_mono_type (mini_get_underlying_type (&param_klass->byval_arg));
5766 return_klass = mono_class_from_mono_type (mini_get_underlying_type (&return_klass->byval_arg));
5768 if (cfg->verbose_level > 3)
5769 printf ("[UNSAFE-MOV-INTRISIC] %s <- %s\n", return_klass->name, param_klass->name);
5771 //Don't allow mixing reference types with value types
5772 if (param_klass->valuetype != return_klass->valuetype) {
5773 if (cfg->verbose_level > 3)
5774 printf ("[UNSAFE-MOV-INTRISIC]\tone of the args is a valuetype and the other is not\n");
5775 return FALSE;
5778 if (!param_klass->valuetype) {
5779 if (cfg->verbose_level > 3)
5780 printf ("[UNSAFE-MOV-INTRISIC]\targs are reference types\n");
5781 return TRUE;
5784 //That are blitable
5785 if (param_klass->has_references || return_klass->has_references)
5786 return FALSE;
5788 /* Avoid mixing structs and primitive types/enums, they need to be handled differently in the JIT */
5789 if ((MONO_TYPE_ISSTRUCT (&param_klass->byval_arg) && !MONO_TYPE_ISSTRUCT (&return_klass->byval_arg)) ||
5790 (!MONO_TYPE_ISSTRUCT (&param_klass->byval_arg) && MONO_TYPE_ISSTRUCT (&return_klass->byval_arg))) {
5791 if (cfg->verbose_level > 3)
5792 printf ("[UNSAFE-MOV-INTRISIC]\tmixing structs and scalars\n");
5793 return FALSE;
5796 if (param_klass->byval_arg.type == MONO_TYPE_R4 || param_klass->byval_arg.type == MONO_TYPE_R8 ||
5797 return_klass->byval_arg.type == MONO_TYPE_R4 || return_klass->byval_arg.type == MONO_TYPE_R8) {
5798 if (cfg->verbose_level > 3)
5799 printf ("[UNSAFE-MOV-INTRISIC]\tfloat or double are not supported\n");
5800 return FALSE;
5803 param_size = mono_class_value_size (param_klass, &align);
5804 return_size = mono_class_value_size (return_klass, &align);
5806 //We can do it if sizes match
5807 if (param_size == return_size) {
5808 if (cfg->verbose_level > 3)
5809 printf ("[UNSAFE-MOV-INTRISIC]\tsame size\n");
5810 return TRUE;
5813 //No simple way to handle struct if sizes don't match
5814 if (MONO_TYPE_ISSTRUCT (&param_klass->byval_arg)) {
5815 if (cfg->verbose_level > 3)
5816 printf ("[UNSAFE-MOV-INTRISIC]\tsize mismatch and type is a struct\n");
5817 return FALSE;
5821 * Same reg size category.
5822 * A quick note on why we don't require widening here.
5823 * The intrinsic is "R Array.UnsafeMov<S,R> (S s)".
5825 * Since the source value comes from a function argument, the JIT will already have
5826 * the value in a VREG and performed any widening needed before (say, when loading from a field).
5828 if (param_size <= 4 && return_size <= 4) {
5829 if (cfg->verbose_level > 3)
5830 printf ("[UNSAFE-MOV-INTRISIC]\tsize mismatch but both are of the same reg class\n");
5831 return TRUE;
5834 return FALSE;
5837 static MonoInst*
5838 emit_array_unsafe_mov (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args)
5840 MonoClass *param_klass = mono_class_from_mono_type (fsig->params [0]);
5841 MonoClass *return_klass = mono_class_from_mono_type (fsig->ret);
5843 if (mini_is_gsharedvt_variable_type (fsig->ret))
5844 return NULL;
5846 //Valuetypes that are semantically equivalent or numbers than can be widened to
5847 if (is_unsafe_mov_compatible (cfg, param_klass, return_klass))
5848 return args [0];
5850 //Arrays of valuetypes that are semantically equivalent
5851 if (param_klass->rank == 1 && return_klass->rank == 1 && is_unsafe_mov_compatible (cfg, param_klass->element_class, return_klass->element_class))
5852 return args [0];
5854 return NULL;
5857 static MonoInst*
5858 mini_emit_inst_for_ctor (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5860 #ifdef MONO_ARCH_SIMD_INTRINSICS
5861 MonoInst *ins = NULL;
5863 if (cfg->opt & MONO_OPT_SIMD) {
5864 ins = mono_emit_simd_intrinsics (cfg, cmethod, fsig, args);
5865 if (ins)
5866 return ins;
5868 #endif
5870 return mono_emit_native_types_intrinsics (cfg, cmethod, fsig, args);
5873 static MonoInst*
5874 emit_memory_barrier (MonoCompile *cfg, int kind)
5876 MonoInst *ins = NULL;
5877 MONO_INST_NEW (cfg, ins, OP_MEMORY_BARRIER);
5878 MONO_ADD_INS (cfg->cbb, ins);
5879 ins->backend.memory_barrier_kind = kind;
5881 return ins;
5884 static MonoInst*
5885 llvm_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5887 MonoInst *ins = NULL;
5888 int opcode = 0;
5890 /* The LLVM backend supports these intrinsics */
5891 if (cmethod->klass == mono_defaults.math_class) {
5892 if (strcmp (cmethod->name, "Sin") == 0) {
5893 opcode = OP_SIN;
5894 } else if (strcmp (cmethod->name, "Cos") == 0) {
5895 opcode = OP_COS;
5896 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
5897 opcode = OP_SQRT;
5898 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
5899 opcode = OP_ABS;
5902 if (opcode && fsig->param_count == 1) {
5903 MONO_INST_NEW (cfg, ins, opcode);
5904 ins->type = STACK_R8;
5905 ins->dreg = mono_alloc_freg (cfg);
5906 ins->sreg1 = args [0]->dreg;
5907 MONO_ADD_INS (cfg->cbb, ins);
5910 opcode = 0;
5911 if (cfg->opt & MONO_OPT_CMOV) {
5912 if (strcmp (cmethod->name, "Min") == 0) {
5913 if (fsig->params [0]->type == MONO_TYPE_I4)
5914 opcode = OP_IMIN;
5915 if (fsig->params [0]->type == MONO_TYPE_U4)
5916 opcode = OP_IMIN_UN;
5917 else if (fsig->params [0]->type == MONO_TYPE_I8)
5918 opcode = OP_LMIN;
5919 else if (fsig->params [0]->type == MONO_TYPE_U8)
5920 opcode = OP_LMIN_UN;
5921 } else if (strcmp (cmethod->name, "Max") == 0) {
5922 if (fsig->params [0]->type == MONO_TYPE_I4)
5923 opcode = OP_IMAX;
5924 if (fsig->params [0]->type == MONO_TYPE_U4)
5925 opcode = OP_IMAX_UN;
5926 else if (fsig->params [0]->type == MONO_TYPE_I8)
5927 opcode = OP_LMAX;
5928 else if (fsig->params [0]->type == MONO_TYPE_U8)
5929 opcode = OP_LMAX_UN;
5933 if (opcode && fsig->param_count == 2) {
5934 MONO_INST_NEW (cfg, ins, opcode);
5935 ins->type = fsig->params [0]->type == MONO_TYPE_I4 ? STACK_I4 : STACK_I8;
5936 ins->dreg = mono_alloc_ireg (cfg);
5937 ins->sreg1 = args [0]->dreg;
5938 ins->sreg2 = args [1]->dreg;
5939 MONO_ADD_INS (cfg->cbb, ins);
5943 return ins;
5946 static MonoInst*
5947 mini_emit_inst_for_sharable_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5949 if (cmethod->klass == mono_defaults.array_class) {
5950 if (strcmp (cmethod->name, "UnsafeStore") == 0)
5951 return emit_array_unsafe_access (cfg, fsig, args, TRUE);
5952 else if (strcmp (cmethod->name, "UnsafeLoad") == 0)
5953 return emit_array_unsafe_access (cfg, fsig, args, FALSE);
5954 else if (strcmp (cmethod->name, "UnsafeMov") == 0)
5955 return emit_array_unsafe_mov (cfg, fsig, args);
5958 return NULL;
5961 static MonoInst*
5962 mini_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5964 MonoInst *ins = NULL;
5966 MonoClass *runtime_helpers_class = mono_class_get_runtime_helpers_class ();
5968 if (cmethod->klass == mono_defaults.string_class) {
5969 if (strcmp (cmethod->name, "get_Chars") == 0 && fsig->param_count + fsig->hasthis == 2) {
5970 int dreg = alloc_ireg (cfg);
5971 int index_reg = alloc_preg (cfg);
5972 int add_reg = alloc_preg (cfg);
5974 #if SIZEOF_REGISTER == 8
5975 if (COMPILE_LLVM (cfg)) {
5976 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, index_reg, args [1]->dreg);
5977 } else {
5978 /* The array reg is 64 bits but the index reg is only 32 */
5979 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, index_reg, args [1]->dreg);
5981 #else
5982 index_reg = args [1]->dreg;
5983 #endif
5984 MONO_EMIT_BOUNDS_CHECK (cfg, args [0]->dreg, MonoString, length, index_reg);
5986 #if defined(TARGET_X86) || defined(TARGET_AMD64)
5987 EMIT_NEW_X86_LEA (cfg, ins, args [0]->dreg, index_reg, 1, MONO_STRUCT_OFFSET (MonoString, chars));
5988 add_reg = ins->dreg;
5989 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU2_MEMBASE, dreg,
5990 add_reg, 0);
5991 #else
5992 int mult_reg = alloc_preg (cfg);
5993 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, mult_reg, index_reg, 1);
5994 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, mult_reg, args [0]->dreg);
5995 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU2_MEMBASE, dreg,
5996 add_reg, MONO_STRUCT_OFFSET (MonoString, chars));
5997 #endif
5998 type_from_op (cfg, ins, NULL, NULL);
5999 return ins;
6000 } else if (strcmp (cmethod->name, "get_Length") == 0 && fsig->param_count + fsig->hasthis == 1) {
6001 int dreg = alloc_ireg (cfg);
6002 /* Decompose later to allow more optimizations */
6003 EMIT_NEW_UNALU (cfg, ins, OP_STRLEN, dreg, args [0]->dreg);
6004 ins->type = STACK_I4;
6005 ins->flags |= MONO_INST_FAULT;
6006 cfg->cbb->has_array_access = TRUE;
6007 cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
6009 return ins;
6010 } else
6011 return NULL;
6012 } else if (cmethod->klass == mono_defaults.object_class) {
6013 if (strcmp (cmethod->name, "GetType") == 0 && fsig->param_count + fsig->hasthis == 1) {
6014 int dreg = alloc_ireg_ref (cfg);
6015 int vt_reg = alloc_preg (cfg);
6016 MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vt_reg, args [0]->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
6017 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, vt_reg, MONO_STRUCT_OFFSET (MonoVTable, type));
6018 type_from_op (cfg, ins, NULL, NULL);
6020 return ins;
6021 } else if (!cfg->backend->emulate_mul_div && strcmp (cmethod->name, "InternalGetHashCode") == 0 && fsig->param_count == 1 && !mono_gc_is_moving ()) {
6022 int dreg = alloc_ireg (cfg);
6023 int t1 = alloc_ireg (cfg);
6025 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, t1, args [0]->dreg, 3);
6026 EMIT_NEW_BIALU_IMM (cfg, ins, OP_MUL_IMM, dreg, t1, 2654435761u);
6027 ins->type = STACK_I4;
6029 return ins;
6030 } else if (strcmp (cmethod->name, ".ctor") == 0 && fsig->param_count == 0) {
6031 MONO_INST_NEW (cfg, ins, OP_NOP);
6032 MONO_ADD_INS (cfg->cbb, ins);
6033 return ins;
6034 } else
6035 return NULL;
6036 } else if (cmethod->klass == mono_defaults.array_class) {
6037 if (strcmp (cmethod->name, "GetGenericValueImpl") == 0 && fsig->param_count + fsig->hasthis == 3 && !cfg->gsharedvt)
6038 return emit_array_generic_access (cfg, fsig, args, FALSE);
6039 else if (strcmp (cmethod->name, "SetGenericValueImpl") == 0 && fsig->param_count + fsig->hasthis == 3 && !cfg->gsharedvt)
6040 return emit_array_generic_access (cfg, fsig, args, TRUE);
6042 #ifndef MONO_BIG_ARRAYS
6044 * This is an inline version of GetLength/GetLowerBound(0) used frequently in
6045 * Array methods.
6047 else if (((strcmp (cmethod->name, "GetLength") == 0 && fsig->param_count + fsig->hasthis == 2) ||
6048 (strcmp (cmethod->name, "GetLowerBound") == 0 && fsig->param_count + fsig->hasthis == 2)) &&
6049 args [1]->opcode == OP_ICONST && args [1]->inst_c0 == 0) {
6050 int dreg = alloc_ireg (cfg);
6051 int bounds_reg = alloc_ireg_mp (cfg);
6052 MonoBasicBlock *end_bb, *szarray_bb;
6053 gboolean get_length = strcmp (cmethod->name, "GetLength") == 0;
6055 NEW_BBLOCK (cfg, end_bb);
6056 NEW_BBLOCK (cfg, szarray_bb);
6058 EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, ins, OP_LOAD_MEMBASE, bounds_reg,
6059 args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, bounds));
6060 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, bounds_reg, 0);
6061 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, szarray_bb);
6062 /* Non-szarray case */
6063 if (get_length)
6064 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
6065 bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, length));
6066 else
6067 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
6068 bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
6069 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
6070 MONO_START_BB (cfg, szarray_bb);
6071 /* Szarray case */
6072 if (get_length)
6073 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
6074 args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, max_length));
6075 else
6076 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
6077 MONO_START_BB (cfg, end_bb);
6079 EMIT_NEW_UNALU (cfg, ins, OP_MOVE, dreg, dreg);
6080 ins->type = STACK_I4;
6082 return ins;
6084 #endif
6086 if (cmethod->name [0] != 'g')
6087 return NULL;
6089 if (strcmp (cmethod->name, "get_Rank") == 0 && fsig->param_count + fsig->hasthis == 1) {
6090 int dreg = alloc_ireg (cfg);
6091 int vtable_reg = alloc_preg (cfg);
6092 MONO_EMIT_NEW_LOAD_MEMBASE_OP_FAULT (cfg, OP_LOAD_MEMBASE, vtable_reg,
6093 args [0]->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
6094 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU1_MEMBASE, dreg,
6095 vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, rank));
6096 type_from_op (cfg, ins, NULL, NULL);
6098 return ins;
6099 } else if (strcmp (cmethod->name, "get_Length") == 0 && fsig->param_count + fsig->hasthis == 1) {
6100 int dreg = alloc_ireg (cfg);
6102 EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, ins, OP_LOADI4_MEMBASE, dreg,
6103 args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, max_length));
6104 type_from_op (cfg, ins, NULL, NULL);
6106 return ins;
6107 } else
6108 return NULL;
6109 } else if (cmethod->klass == runtime_helpers_class) {
6110 if (strcmp (cmethod->name, "get_OffsetToStringData") == 0 && fsig->param_count == 0) {
6111 EMIT_NEW_ICONST (cfg, ins, MONO_STRUCT_OFFSET (MonoString, chars));
6112 return ins;
6113 } else
6114 return NULL;
6115 } else if (cmethod->klass == mono_defaults.monitor_class) {
6116 gboolean is_enter = FALSE;
6118 if (!strcmp (cmethod->name, "Enter") && mono_method_signature (cmethod)->param_count == 1)
6119 is_enter = TRUE;
6121 if (is_enter) {
6123 * To make async stack traces work, icalls which can block should have a wrapper.
6124 * For Monitor.Enter, emit two calls: a fastpath which doesn't have a wrapper, and a slowpath, which does.
6126 MonoBasicBlock *end_bb;
6128 NEW_BBLOCK (cfg, end_bb);
6130 ins = mono_emit_jit_icall (cfg, (gpointer)mono_monitor_enter_fast, args);
6131 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ICOMPARE_IMM, -1, ins->dreg, 0);
6132 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBNE_UN, end_bb);
6133 ins = mono_emit_jit_icall (cfg, (gpointer)mono_monitor_enter, args);
6134 MONO_START_BB (cfg, end_bb);
6135 return ins;
6137 } else if (cmethod->klass == mono_defaults.thread_class) {
6138 if (strcmp (cmethod->name, "SpinWait_nop") == 0 && fsig->param_count == 0) {
6139 MONO_INST_NEW (cfg, ins, OP_RELAXED_NOP);
6140 MONO_ADD_INS (cfg->cbb, ins);
6141 return ins;
6142 } else if (strcmp (cmethod->name, "MemoryBarrier") == 0 && fsig->param_count == 0) {
6143 return emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6144 } else if (!strcmp (cmethod->name, "VolatileRead") && fsig->param_count == 1) {
6145 guint32 opcode = 0;
6146 gboolean is_ref = mini_type_is_reference (fsig->params [0]);
6148 if (fsig->params [0]->type == MONO_TYPE_I1)
6149 opcode = OP_LOADI1_MEMBASE;
6150 else if (fsig->params [0]->type == MONO_TYPE_U1)
6151 opcode = OP_LOADU1_MEMBASE;
6152 else if (fsig->params [0]->type == MONO_TYPE_I2)
6153 opcode = OP_LOADI2_MEMBASE;
6154 else if (fsig->params [0]->type == MONO_TYPE_U2)
6155 opcode = OP_LOADU2_MEMBASE;
6156 else if (fsig->params [0]->type == MONO_TYPE_I4)
6157 opcode = OP_LOADI4_MEMBASE;
6158 else if (fsig->params [0]->type == MONO_TYPE_U4)
6159 opcode = OP_LOADU4_MEMBASE;
6160 else if (fsig->params [0]->type == MONO_TYPE_I8 || fsig->params [0]->type == MONO_TYPE_U8)
6161 opcode = OP_LOADI8_MEMBASE;
6162 else if (fsig->params [0]->type == MONO_TYPE_R4)
6163 opcode = OP_LOADR4_MEMBASE;
6164 else if (fsig->params [0]->type == MONO_TYPE_R8)
6165 opcode = OP_LOADR8_MEMBASE;
6166 else if (is_ref || fsig->params [0]->type == MONO_TYPE_I || fsig->params [0]->type == MONO_TYPE_U)
6167 opcode = OP_LOAD_MEMBASE;
6169 if (opcode) {
6170 MONO_INST_NEW (cfg, ins, opcode);
6171 ins->inst_basereg = args [0]->dreg;
6172 ins->inst_offset = 0;
6173 MONO_ADD_INS (cfg->cbb, ins);
6175 switch (fsig->params [0]->type) {
6176 case MONO_TYPE_I1:
6177 case MONO_TYPE_U1:
6178 case MONO_TYPE_I2:
6179 case MONO_TYPE_U2:
6180 case MONO_TYPE_I4:
6181 case MONO_TYPE_U4:
6182 ins->dreg = mono_alloc_ireg (cfg);
6183 ins->type = STACK_I4;
6184 break;
6185 case MONO_TYPE_I8:
6186 case MONO_TYPE_U8:
6187 ins->dreg = mono_alloc_lreg (cfg);
6188 ins->type = STACK_I8;
6189 break;
6190 case MONO_TYPE_I:
6191 case MONO_TYPE_U:
6192 ins->dreg = mono_alloc_ireg (cfg);
6193 #if SIZEOF_REGISTER == 8
6194 ins->type = STACK_I8;
6195 #else
6196 ins->type = STACK_I4;
6197 #endif
6198 break;
6199 case MONO_TYPE_R4:
6200 case MONO_TYPE_R8:
6201 ins->dreg = mono_alloc_freg (cfg);
6202 ins->type = STACK_R8;
6203 break;
6204 default:
6205 g_assert (mini_type_is_reference (fsig->params [0]));
6206 ins->dreg = mono_alloc_ireg_ref (cfg);
6207 ins->type = STACK_OBJ;
6208 break;
6211 if (opcode == OP_LOADI8_MEMBASE)
6212 ins = mono_decompose_opcode (cfg, ins);
6214 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6216 return ins;
6218 } else if (!strcmp (cmethod->name, "VolatileWrite") && fsig->param_count == 2) {
6219 guint32 opcode = 0;
6220 gboolean is_ref = mini_type_is_reference (fsig->params [0]);
6222 if (fsig->params [0]->type == MONO_TYPE_I1 || fsig->params [0]->type == MONO_TYPE_U1)
6223 opcode = OP_STOREI1_MEMBASE_REG;
6224 else if (fsig->params [0]->type == MONO_TYPE_I2 || fsig->params [0]->type == MONO_TYPE_U2)
6225 opcode = OP_STOREI2_MEMBASE_REG;
6226 else if (fsig->params [0]->type == MONO_TYPE_I4 || fsig->params [0]->type == MONO_TYPE_U4)
6227 opcode = OP_STOREI4_MEMBASE_REG;
6228 else if (fsig->params [0]->type == MONO_TYPE_I8 || fsig->params [0]->type == MONO_TYPE_U8)
6229 opcode = OP_STOREI8_MEMBASE_REG;
6230 else if (fsig->params [0]->type == MONO_TYPE_R4)
6231 opcode = OP_STORER4_MEMBASE_REG;
6232 else if (fsig->params [0]->type == MONO_TYPE_R8)
6233 opcode = OP_STORER8_MEMBASE_REG;
6234 else if (is_ref || fsig->params [0]->type == MONO_TYPE_I || fsig->params [0]->type == MONO_TYPE_U)
6235 opcode = OP_STORE_MEMBASE_REG;
6237 if (opcode) {
6238 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6240 MONO_INST_NEW (cfg, ins, opcode);
6241 ins->sreg1 = args [1]->dreg;
6242 ins->inst_destbasereg = args [0]->dreg;
6243 ins->inst_offset = 0;
6244 MONO_ADD_INS (cfg->cbb, ins);
6246 if (opcode == OP_STOREI8_MEMBASE_REG)
6247 ins = mono_decompose_opcode (cfg, ins);
6249 return ins;
6252 } else if (cmethod->klass->image == mono_defaults.corlib &&
6253 (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
6254 (strcmp (cmethod->klass->name, "Interlocked") == 0)) {
6255 ins = NULL;
6257 #if SIZEOF_REGISTER == 8
6258 if (!cfg->llvm_only && strcmp (cmethod->name, "Read") == 0 && fsig->param_count == 1 && (fsig->params [0]->type == MONO_TYPE_I8)) {
6259 if (!cfg->llvm_only && mono_arch_opcode_supported (OP_ATOMIC_LOAD_I8)) {
6260 MONO_INST_NEW (cfg, ins, OP_ATOMIC_LOAD_I8);
6261 ins->dreg = mono_alloc_preg (cfg);
6262 ins->sreg1 = args [0]->dreg;
6263 ins->type = STACK_I8;
6264 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_SEQ;
6265 MONO_ADD_INS (cfg->cbb, ins);
6266 } else {
6267 MonoInst *load_ins;
6269 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6271 /* 64 bit reads are already atomic */
6272 MONO_INST_NEW (cfg, load_ins, OP_LOADI8_MEMBASE);
6273 load_ins->dreg = mono_alloc_preg (cfg);
6274 load_ins->inst_basereg = args [0]->dreg;
6275 load_ins->inst_offset = 0;
6276 load_ins->type = STACK_I8;
6277 MONO_ADD_INS (cfg->cbb, load_ins);
6279 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6281 ins = load_ins;
6284 #endif
6286 if (strcmp (cmethod->name, "Increment") == 0 && fsig->param_count == 1) {
6287 MonoInst *ins_iconst;
6288 guint32 opcode = 0;
6290 if (fsig->params [0]->type == MONO_TYPE_I4) {
6291 opcode = OP_ATOMIC_ADD_I4;
6292 cfg->has_atomic_add_i4 = TRUE;
6294 #if SIZEOF_REGISTER == 8
6295 else if (fsig->params [0]->type == MONO_TYPE_I8)
6296 opcode = OP_ATOMIC_ADD_I8;
6297 #endif
6298 if (opcode) {
6299 if (!mono_arch_opcode_supported (opcode))
6300 return NULL;
6301 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
6302 ins_iconst->inst_c0 = 1;
6303 ins_iconst->dreg = mono_alloc_ireg (cfg);
6304 MONO_ADD_INS (cfg->cbb, ins_iconst);
6306 MONO_INST_NEW (cfg, ins, opcode);
6307 ins->dreg = mono_alloc_ireg (cfg);
6308 ins->inst_basereg = args [0]->dreg;
6309 ins->inst_offset = 0;
6310 ins->sreg2 = ins_iconst->dreg;
6311 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
6312 MONO_ADD_INS (cfg->cbb, ins);
6314 } else if (strcmp (cmethod->name, "Decrement") == 0 && fsig->param_count == 1) {
6315 MonoInst *ins_iconst;
6316 guint32 opcode = 0;
6318 if (fsig->params [0]->type == MONO_TYPE_I4) {
6319 opcode = OP_ATOMIC_ADD_I4;
6320 cfg->has_atomic_add_i4 = TRUE;
6322 #if SIZEOF_REGISTER == 8
6323 else if (fsig->params [0]->type == MONO_TYPE_I8)
6324 opcode = OP_ATOMIC_ADD_I8;
6325 #endif
6326 if (opcode) {
6327 if (!mono_arch_opcode_supported (opcode))
6328 return NULL;
6329 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
6330 ins_iconst->inst_c0 = -1;
6331 ins_iconst->dreg = mono_alloc_ireg (cfg);
6332 MONO_ADD_INS (cfg->cbb, ins_iconst);
6334 MONO_INST_NEW (cfg, ins, opcode);
6335 ins->dreg = mono_alloc_ireg (cfg);
6336 ins->inst_basereg = args [0]->dreg;
6337 ins->inst_offset = 0;
6338 ins->sreg2 = ins_iconst->dreg;
6339 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
6340 MONO_ADD_INS (cfg->cbb, ins);
6342 } else if (strcmp (cmethod->name, "Add") == 0 && fsig->param_count == 2) {
6343 guint32 opcode = 0;
6345 if (fsig->params [0]->type == MONO_TYPE_I4) {
6346 opcode = OP_ATOMIC_ADD_I4;
6347 cfg->has_atomic_add_i4 = TRUE;
6349 #if SIZEOF_REGISTER == 8
6350 else if (fsig->params [0]->type == MONO_TYPE_I8)
6351 opcode = OP_ATOMIC_ADD_I8;
6352 #endif
6353 if (opcode) {
6354 if (!mono_arch_opcode_supported (opcode))
6355 return NULL;
6356 MONO_INST_NEW (cfg, ins, opcode);
6357 ins->dreg = mono_alloc_ireg (cfg);
6358 ins->inst_basereg = args [0]->dreg;
6359 ins->inst_offset = 0;
6360 ins->sreg2 = args [1]->dreg;
6361 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
6362 MONO_ADD_INS (cfg->cbb, ins);
6365 else if (strcmp (cmethod->name, "Exchange") == 0 && fsig->param_count == 2) {
6366 MonoInst *f2i = NULL, *i2f;
6367 guint32 opcode, f2i_opcode, i2f_opcode;
6368 gboolean is_ref = mini_type_is_reference (fsig->params [0]);
6369 gboolean is_float = fsig->params [0]->type == MONO_TYPE_R4 || fsig->params [0]->type == MONO_TYPE_R8;
6371 if (fsig->params [0]->type == MONO_TYPE_I4 ||
6372 fsig->params [0]->type == MONO_TYPE_R4) {
6373 opcode = OP_ATOMIC_EXCHANGE_I4;
6374 f2i_opcode = OP_MOVE_F_TO_I4;
6375 i2f_opcode = OP_MOVE_I4_TO_F;
6376 cfg->has_atomic_exchange_i4 = TRUE;
6378 #if SIZEOF_REGISTER == 8
6379 else if (is_ref ||
6380 fsig->params [0]->type == MONO_TYPE_I8 ||
6381 fsig->params [0]->type == MONO_TYPE_R8 ||
6382 fsig->params [0]->type == MONO_TYPE_I) {
6383 opcode = OP_ATOMIC_EXCHANGE_I8;
6384 f2i_opcode = OP_MOVE_F_TO_I8;
6385 i2f_opcode = OP_MOVE_I8_TO_F;
6387 #else
6388 else if (is_ref || fsig->params [0]->type == MONO_TYPE_I) {
6389 opcode = OP_ATOMIC_EXCHANGE_I4;
6390 cfg->has_atomic_exchange_i4 = TRUE;
6392 #endif
6393 else
6394 return NULL;
6396 if (!mono_arch_opcode_supported (opcode))
6397 return NULL;
6399 if (is_float) {
6400 /* TODO: Decompose these opcodes instead of bailing here. */
6401 if (COMPILE_SOFT_FLOAT (cfg))
6402 return NULL;
6404 MONO_INST_NEW (cfg, f2i, f2i_opcode);
6405 f2i->dreg = mono_alloc_ireg (cfg);
6406 f2i->sreg1 = args [1]->dreg;
6407 if (f2i_opcode == OP_MOVE_F_TO_I4)
6408 f2i->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6409 MONO_ADD_INS (cfg->cbb, f2i);
6412 MONO_INST_NEW (cfg, ins, opcode);
6413 ins->dreg = is_ref ? mono_alloc_ireg_ref (cfg) : mono_alloc_ireg (cfg);
6414 ins->inst_basereg = args [0]->dreg;
6415 ins->inst_offset = 0;
6416 ins->sreg2 = is_float ? f2i->dreg : args [1]->dreg;
6417 MONO_ADD_INS (cfg->cbb, ins);
6419 switch (fsig->params [0]->type) {
6420 case MONO_TYPE_I4:
6421 ins->type = STACK_I4;
6422 break;
6423 case MONO_TYPE_I8:
6424 ins->type = STACK_I8;
6425 break;
6426 case MONO_TYPE_I:
6427 #if SIZEOF_REGISTER == 8
6428 ins->type = STACK_I8;
6429 #else
6430 ins->type = STACK_I4;
6431 #endif
6432 break;
6433 case MONO_TYPE_R4:
6434 case MONO_TYPE_R8:
6435 ins->type = STACK_R8;
6436 break;
6437 default:
6438 g_assert (mini_type_is_reference (fsig->params [0]));
6439 ins->type = STACK_OBJ;
6440 break;
6443 if (is_float) {
6444 MONO_INST_NEW (cfg, i2f, i2f_opcode);
6445 i2f->dreg = mono_alloc_freg (cfg);
6446 i2f->sreg1 = ins->dreg;
6447 i2f->type = STACK_R8;
6448 if (i2f_opcode == OP_MOVE_I4_TO_F)
6449 i2f->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6450 MONO_ADD_INS (cfg->cbb, i2f);
6452 ins = i2f;
6455 if (cfg->gen_write_barriers && is_ref)
6456 emit_write_barrier (cfg, args [0], args [1]);
6458 else if ((strcmp (cmethod->name, "CompareExchange") == 0) && fsig->param_count == 3) {
6459 MonoInst *f2i_new = NULL, *f2i_cmp = NULL, *i2f;
6460 guint32 opcode, f2i_opcode, i2f_opcode;
6461 gboolean is_ref = mini_type_is_reference (fsig->params [1]);
6462 gboolean is_float = fsig->params [1]->type == MONO_TYPE_R4 || fsig->params [1]->type == MONO_TYPE_R8;
6464 if (fsig->params [1]->type == MONO_TYPE_I4 ||
6465 fsig->params [1]->type == MONO_TYPE_R4) {
6466 opcode = OP_ATOMIC_CAS_I4;
6467 f2i_opcode = OP_MOVE_F_TO_I4;
6468 i2f_opcode = OP_MOVE_I4_TO_F;
6469 cfg->has_atomic_cas_i4 = TRUE;
6471 #if SIZEOF_REGISTER == 8
6472 else if (is_ref ||
6473 fsig->params [1]->type == MONO_TYPE_I8 ||
6474 fsig->params [1]->type == MONO_TYPE_R8 ||
6475 fsig->params [1]->type == MONO_TYPE_I) {
6476 opcode = OP_ATOMIC_CAS_I8;
6477 f2i_opcode = OP_MOVE_F_TO_I8;
6478 i2f_opcode = OP_MOVE_I8_TO_F;
6480 #else
6481 else if (is_ref || fsig->params [1]->type == MONO_TYPE_I) {
6482 opcode = OP_ATOMIC_CAS_I4;
6483 cfg->has_atomic_cas_i4 = TRUE;
6485 #endif
6486 else
6487 return NULL;
6489 if (!mono_arch_opcode_supported (opcode))
6490 return NULL;
6492 if (is_float) {
6493 /* TODO: Decompose these opcodes instead of bailing here. */
6494 if (COMPILE_SOFT_FLOAT (cfg))
6495 return NULL;
6497 MONO_INST_NEW (cfg, f2i_new, f2i_opcode);
6498 f2i_new->dreg = mono_alloc_ireg (cfg);
6499 f2i_new->sreg1 = args [1]->dreg;
6500 if (f2i_opcode == OP_MOVE_F_TO_I4)
6501 f2i_new->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6502 MONO_ADD_INS (cfg->cbb, f2i_new);
6504 MONO_INST_NEW (cfg, f2i_cmp, f2i_opcode);
6505 f2i_cmp->dreg = mono_alloc_ireg (cfg);
6506 f2i_cmp->sreg1 = args [2]->dreg;
6507 if (f2i_opcode == OP_MOVE_F_TO_I4)
6508 f2i_cmp->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6509 MONO_ADD_INS (cfg->cbb, f2i_cmp);
6512 MONO_INST_NEW (cfg, ins, opcode);
6513 ins->dreg = is_ref ? alloc_ireg_ref (cfg) : alloc_ireg (cfg);
6514 ins->sreg1 = args [0]->dreg;
6515 ins->sreg2 = is_float ? f2i_new->dreg : args [1]->dreg;
6516 ins->sreg3 = is_float ? f2i_cmp->dreg : args [2]->dreg;
6517 MONO_ADD_INS (cfg->cbb, ins);
6519 switch (fsig->params [1]->type) {
6520 case MONO_TYPE_I4:
6521 ins->type = STACK_I4;
6522 break;
6523 case MONO_TYPE_I8:
6524 ins->type = STACK_I8;
6525 break;
6526 case MONO_TYPE_I:
6527 #if SIZEOF_REGISTER == 8
6528 ins->type = STACK_I8;
6529 #else
6530 ins->type = STACK_I4;
6531 #endif
6532 break;
6533 case MONO_TYPE_R4:
6534 ins->type = cfg->r4_stack_type;
6535 break;
6536 case MONO_TYPE_R8:
6537 ins->type = STACK_R8;
6538 break;
6539 default:
6540 g_assert (mini_type_is_reference (fsig->params [1]));
6541 ins->type = STACK_OBJ;
6542 break;
6545 if (is_float) {
6546 MONO_INST_NEW (cfg, i2f, i2f_opcode);
6547 i2f->dreg = mono_alloc_freg (cfg);
6548 i2f->sreg1 = ins->dreg;
6549 i2f->type = STACK_R8;
6550 if (i2f_opcode == OP_MOVE_I4_TO_F)
6551 i2f->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6552 MONO_ADD_INS (cfg->cbb, i2f);
6554 ins = i2f;
6557 if (cfg->gen_write_barriers && is_ref)
6558 emit_write_barrier (cfg, args [0], args [1]);
6560 else if ((strcmp (cmethod->name, "CompareExchange") == 0) && fsig->param_count == 4 &&
6561 fsig->params [1]->type == MONO_TYPE_I4) {
6562 MonoInst *cmp, *ceq;
6564 if (!mono_arch_opcode_supported (OP_ATOMIC_CAS_I4))
6565 return NULL;
6567 /* int32 r = CAS (location, value, comparand); */
6568 MONO_INST_NEW (cfg, ins, OP_ATOMIC_CAS_I4);
6569 ins->dreg = alloc_ireg (cfg);
6570 ins->sreg1 = args [0]->dreg;
6571 ins->sreg2 = args [1]->dreg;
6572 ins->sreg3 = args [2]->dreg;
6573 ins->type = STACK_I4;
6574 MONO_ADD_INS (cfg->cbb, ins);
6576 /* bool result = r == comparand; */
6577 MONO_INST_NEW (cfg, cmp, OP_ICOMPARE);
6578 cmp->sreg1 = ins->dreg;
6579 cmp->sreg2 = args [2]->dreg;
6580 cmp->type = STACK_I4;
6581 MONO_ADD_INS (cfg->cbb, cmp);
6583 MONO_INST_NEW (cfg, ceq, OP_ICEQ);
6584 ceq->dreg = alloc_ireg (cfg);
6585 ceq->type = STACK_I4;
6586 MONO_ADD_INS (cfg->cbb, ceq);
6588 /* *success = result; */
6589 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, args [3]->dreg, 0, ceq->dreg);
6591 cfg->has_atomic_cas_i4 = TRUE;
6593 else if (strcmp (cmethod->name, "MemoryBarrier") == 0 && fsig->param_count == 0)
6594 ins = emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6596 if (ins)
6597 return ins;
6598 } else if (cmethod->klass->image == mono_defaults.corlib &&
6599 (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
6600 (strcmp (cmethod->klass->name, "Volatile") == 0)) {
6601 ins = NULL;
6603 if (!cfg->llvm_only && !strcmp (cmethod->name, "Read") && fsig->param_count == 1) {
6604 guint32 opcode = 0;
6605 MonoType *t = fsig->params [0];
6606 gboolean is_ref;
6607 gboolean is_float = t->type == MONO_TYPE_R4 || t->type == MONO_TYPE_R8;
6609 g_assert (t->byref);
6610 /* t is a byref type, so the reference check is more complicated */
6611 is_ref = mini_type_is_reference (&mono_class_from_mono_type (t)->byval_arg);
6612 if (t->type == MONO_TYPE_I1)
6613 opcode = OP_ATOMIC_LOAD_I1;
6614 else if (t->type == MONO_TYPE_U1 || t->type == MONO_TYPE_BOOLEAN)
6615 opcode = OP_ATOMIC_LOAD_U1;
6616 else if (t->type == MONO_TYPE_I2)
6617 opcode = OP_ATOMIC_LOAD_I2;
6618 else if (t->type == MONO_TYPE_U2)
6619 opcode = OP_ATOMIC_LOAD_U2;
6620 else if (t->type == MONO_TYPE_I4)
6621 opcode = OP_ATOMIC_LOAD_I4;
6622 else if (t->type == MONO_TYPE_U4)
6623 opcode = OP_ATOMIC_LOAD_U4;
6624 else if (t->type == MONO_TYPE_R4)
6625 opcode = OP_ATOMIC_LOAD_R4;
6626 else if (t->type == MONO_TYPE_R8)
6627 opcode = OP_ATOMIC_LOAD_R8;
6628 #if SIZEOF_REGISTER == 8
6629 else if (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_I)
6630 opcode = OP_ATOMIC_LOAD_I8;
6631 else if (is_ref || t->type == MONO_TYPE_U8 || t->type == MONO_TYPE_U)
6632 opcode = OP_ATOMIC_LOAD_U8;
6633 #else
6634 else if (t->type == MONO_TYPE_I)
6635 opcode = OP_ATOMIC_LOAD_I4;
6636 else if (is_ref || t->type == MONO_TYPE_U)
6637 opcode = OP_ATOMIC_LOAD_U4;
6638 #endif
6640 if (opcode) {
6641 if (!mono_arch_opcode_supported (opcode))
6642 return NULL;
6644 MONO_INST_NEW (cfg, ins, opcode);
6645 ins->dreg = is_ref ? mono_alloc_ireg_ref (cfg) : (is_float ? mono_alloc_freg (cfg) : mono_alloc_ireg (cfg));
6646 ins->sreg1 = args [0]->dreg;
6647 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_ACQ;
6648 MONO_ADD_INS (cfg->cbb, ins);
6650 switch (t->type) {
6651 case MONO_TYPE_BOOLEAN:
6652 case MONO_TYPE_I1:
6653 case MONO_TYPE_U1:
6654 case MONO_TYPE_I2:
6655 case MONO_TYPE_U2:
6656 case MONO_TYPE_I4:
6657 case MONO_TYPE_U4:
6658 ins->type = STACK_I4;
6659 break;
6660 case MONO_TYPE_I8:
6661 case MONO_TYPE_U8:
6662 ins->type = STACK_I8;
6663 break;
6664 case MONO_TYPE_I:
6665 case MONO_TYPE_U:
6666 #if SIZEOF_REGISTER == 8
6667 ins->type = STACK_I8;
6668 #else
6669 ins->type = STACK_I4;
6670 #endif
6671 break;
6672 case MONO_TYPE_R4:
6673 ins->type = cfg->r4_stack_type;
6674 break;
6675 case MONO_TYPE_R8:
6676 ins->type = STACK_R8;
6677 break;
6678 default:
6679 g_assert (is_ref);
6680 ins->type = STACK_OBJ;
6681 break;
6686 if (!cfg->llvm_only && !strcmp (cmethod->name, "Write") && fsig->param_count == 2) {
6687 guint32 opcode = 0;
6688 MonoType *t = fsig->params [0];
6689 gboolean is_ref;
6691 g_assert (t->byref);
6692 is_ref = mini_type_is_reference (&mono_class_from_mono_type (t)->byval_arg);
6693 if (t->type == MONO_TYPE_I1)
6694 opcode = OP_ATOMIC_STORE_I1;
6695 else if (t->type == MONO_TYPE_U1 || t->type == MONO_TYPE_BOOLEAN)
6696 opcode = OP_ATOMIC_STORE_U1;
6697 else if (t->type == MONO_TYPE_I2)
6698 opcode = OP_ATOMIC_STORE_I2;
6699 else if (t->type == MONO_TYPE_U2)
6700 opcode = OP_ATOMIC_STORE_U2;
6701 else if (t->type == MONO_TYPE_I4)
6702 opcode = OP_ATOMIC_STORE_I4;
6703 else if (t->type == MONO_TYPE_U4)
6704 opcode = OP_ATOMIC_STORE_U4;
6705 else if (t->type == MONO_TYPE_R4)
6706 opcode = OP_ATOMIC_STORE_R4;
6707 else if (t->type == MONO_TYPE_R8)
6708 opcode = OP_ATOMIC_STORE_R8;
6709 #if SIZEOF_REGISTER == 8
6710 else if (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_I)
6711 opcode = OP_ATOMIC_STORE_I8;
6712 else if (is_ref || t->type == MONO_TYPE_U8 || t->type == MONO_TYPE_U)
6713 opcode = OP_ATOMIC_STORE_U8;
6714 #else
6715 else if (t->type == MONO_TYPE_I)
6716 opcode = OP_ATOMIC_STORE_I4;
6717 else if (is_ref || t->type == MONO_TYPE_U)
6718 opcode = OP_ATOMIC_STORE_U4;
6719 #endif
6721 if (opcode) {
6722 if (!mono_arch_opcode_supported (opcode))
6723 return NULL;
6725 MONO_INST_NEW (cfg, ins, opcode);
6726 ins->dreg = args [0]->dreg;
6727 ins->sreg1 = args [1]->dreg;
6728 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_REL;
6729 MONO_ADD_INS (cfg->cbb, ins);
6731 if (cfg->gen_write_barriers && is_ref)
6732 emit_write_barrier (cfg, args [0], args [1]);
6736 if (ins)
6737 return ins;
6738 } else if (cmethod->klass->image == mono_defaults.corlib &&
6739 (strcmp (cmethod->klass->name_space, "System.Diagnostics") == 0) &&
6740 (strcmp (cmethod->klass->name, "Debugger") == 0)) {
6741 if (!strcmp (cmethod->name, "Break") && fsig->param_count == 0) {
6742 if (should_insert_brekpoint (cfg->method)) {
6743 ins = mono_emit_jit_icall (cfg, mono_debugger_agent_user_break, NULL);
6744 } else {
6745 MONO_INST_NEW (cfg, ins, OP_NOP);
6746 MONO_ADD_INS (cfg->cbb, ins);
6748 return ins;
6750 } else if (cmethod->klass->image == mono_defaults.corlib &&
6751 (strcmp (cmethod->klass->name_space, "System") == 0) &&
6752 (strcmp (cmethod->klass->name, "Environment") == 0)) {
6753 if (!strcmp (cmethod->name, "get_IsRunningOnWindows") && fsig->param_count == 0) {
6754 #ifdef TARGET_WIN32
6755 EMIT_NEW_ICONST (cfg, ins, 1);
6756 #else
6757 EMIT_NEW_ICONST (cfg, ins, 0);
6758 #endif
6760 } else if (cmethod->klass->image == mono_defaults.corlib &&
6761 (strcmp (cmethod->klass->name_space, "System.Reflection") == 0) &&
6762 (strcmp (cmethod->klass->name, "Assembly") == 0)) {
6763 if (cfg->llvm_only && !strcmp (cmethod->name, "GetExecutingAssembly")) {
6764 /* No stack walks are currently available, so implement this as an intrinsic */
6765 MonoInst *assembly_ins;
6767 EMIT_NEW_AOTCONST (cfg, assembly_ins, MONO_PATCH_INFO_IMAGE, cfg->method->klass->image);
6768 ins = mono_emit_jit_icall (cfg, mono_get_assembly_object, &assembly_ins);
6769 return ins;
6771 } else if (cmethod->klass->image == mono_defaults.corlib &&
6772 (strcmp (cmethod->klass->name_space, "System.Reflection") == 0) &&
6773 (strcmp (cmethod->klass->name, "MethodBase") == 0)) {
6774 if (cfg->llvm_only && !strcmp (cmethod->name, "GetCurrentMethod")) {
6775 /* No stack walks are currently available, so implement this as an intrinsic */
6776 MonoInst *method_ins;
6777 MonoMethod *declaring = cfg->method;
6779 /* This returns the declaring generic method */
6780 if (declaring->is_inflated)
6781 declaring = ((MonoMethodInflated*)cfg->method)->declaring;
6782 EMIT_NEW_AOTCONST (cfg, method_ins, MONO_PATCH_INFO_METHODCONST, declaring);
6783 ins = mono_emit_jit_icall (cfg, mono_get_method_object, &method_ins);
6784 cfg->no_inline = TRUE;
6785 if (cfg->method != cfg->current_method)
6786 inline_failure (cfg, "MethodBase:GetCurrentMethod ()");
6787 return ins;
6789 } else if (cmethod->klass == mono_defaults.math_class) {
6791 * There is general branchless code for Min/Max, but it does not work for
6792 * all inputs:
6793 * http://everything2.com/?node_id=1051618
6795 } else if (((!strcmp (cmethod->klass->image->assembly->aname.name, "MonoMac") ||
6796 !strcmp (cmethod->klass->image->assembly->aname.name, "monotouch")) &&
6797 !strcmp (cmethod->klass->name_space, "XamCore.ObjCRuntime") &&
6798 !strcmp (cmethod->klass->name, "Selector")) ||
6799 (!strcmp (cmethod->klass->image->assembly->aname.name, "Xamarin.iOS") &&
6800 !strcmp (cmethod->klass->name_space, "ObjCRuntime") &&
6801 !strcmp (cmethod->klass->name, "Selector"))
6803 if (cfg->backend->have_objc_get_selector &&
6804 !strcmp (cmethod->name, "GetHandle") && fsig->param_count == 1 &&
6805 (args [0]->opcode == OP_GOT_ENTRY || args [0]->opcode == OP_AOTCONST) &&
6806 cfg->compile_aot && !cfg->llvm_only) {
6807 MonoInst *pi;
6808 MonoJumpInfoToken *ji;
6809 MonoString *s;
6811 // FIXME: llvmonly
6813 cfg->exception_message = g_strdup ("GetHandle");
6814 cfg->disable_llvm = TRUE;
6816 if (args [0]->opcode == OP_GOT_ENTRY) {
6817 pi = (MonoInst *)args [0]->inst_p1;
6818 g_assert (pi->opcode == OP_PATCH_INFO);
6819 g_assert (GPOINTER_TO_INT (pi->inst_p1) == MONO_PATCH_INFO_LDSTR);
6820 ji = (MonoJumpInfoToken *)pi->inst_p0;
6821 } else {
6822 g_assert (GPOINTER_TO_INT (args [0]->inst_p1) == MONO_PATCH_INFO_LDSTR);
6823 ji = (MonoJumpInfoToken *)args [0]->inst_p0;
6826 NULLIFY_INS (args [0]);
6828 // FIXME: Ugly
6829 s = mono_ldstr_checked (cfg->domain, ji->image, mono_metadata_token_index (ji->token), &cfg->error);
6830 return_val_if_nok (&cfg->error, NULL);
6831 MONO_INST_NEW (cfg, ins, OP_OBJC_GET_SELECTOR);
6832 ins->dreg = mono_alloc_ireg (cfg);
6833 // FIXME: Leaks
6834 ins->inst_p0 = mono_string_to_utf8 (s);
6835 MONO_ADD_INS (cfg->cbb, ins);
6836 return ins;
6840 #ifdef MONO_ARCH_SIMD_INTRINSICS
6841 if (cfg->opt & MONO_OPT_SIMD) {
6842 ins = mono_emit_simd_intrinsics (cfg, cmethod, fsig, args);
6843 if (ins)
6844 return ins;
6846 #endif
6848 ins = mono_emit_native_types_intrinsics (cfg, cmethod, fsig, args);
6849 if (ins)
6850 return ins;
6852 if (COMPILE_LLVM (cfg)) {
6853 ins = llvm_emit_inst_for_method (cfg, cmethod, fsig, args);
6854 if (ins)
6855 return ins;
6858 return mono_arch_emit_inst_for_method (cfg, cmethod, fsig, args);
6862 * This entry point could be used later for arbitrary method
6863 * redirection.
6865 inline static MonoInst*
6866 mini_redirect_call (MonoCompile *cfg, MonoMethod *method,
6867 MonoMethodSignature *signature, MonoInst **args, MonoInst *this_ins)
6869 if (method->klass == mono_defaults.string_class) {
6870 /* managed string allocation support */
6871 if (strcmp (method->name, "InternalAllocateStr") == 0 && !(mono_profiler_events & MONO_PROFILE_ALLOCATIONS) && !(cfg->opt & MONO_OPT_SHARED)) {
6872 MonoInst *iargs [2];
6873 MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
6874 MonoMethod *managed_alloc = NULL;
6876 g_assert (vtable); /*Should not fail since it System.String*/
6877 #ifndef MONO_CROSS_COMPILE
6878 managed_alloc = mono_gc_get_managed_allocator (method->klass, FALSE, FALSE);
6879 #endif
6880 if (!managed_alloc)
6881 return NULL;
6882 EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
6883 iargs [1] = args [0];
6884 return mono_emit_method_call (cfg, managed_alloc, iargs, this_ins);
6887 return NULL;
6890 static void
6891 mono_save_args (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **sp)
6893 MonoInst *store, *temp;
6894 int i;
6896 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
6897 MonoType *argtype = (sig->hasthis && (i == 0)) ? type_from_stack_type (*sp) : sig->params [i - sig->hasthis];
6900 * FIXME: We should use *args++ = sp [0], but that would mean the arg
6901 * would be different than the MonoInst's used to represent arguments, and
6902 * the ldelema implementation can't deal with that.
6903 * Solution: When ldelema is used on an inline argument, create a var for
6904 * it, emit ldelema on that var, and emit the saving code below in
6905 * inline_method () if needed.
6907 temp = mono_compile_create_var (cfg, argtype, OP_LOCAL);
6908 cfg->args [i] = temp;
6909 /* This uses cfg->args [i] which is set by the preceeding line */
6910 EMIT_NEW_ARGSTORE (cfg, store, i, *sp);
6911 store->cil_code = sp [0]->cil_code;
6912 sp++;
6916 #define MONO_INLINE_CALLED_LIMITED_METHODS 1
6917 #define MONO_INLINE_CALLER_LIMITED_METHODS 1
6919 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
6920 static gboolean
6921 check_inline_called_method_name_limit (MonoMethod *called_method)
6923 int strncmp_result;
6924 static const char *limit = NULL;
6926 if (limit == NULL) {
6927 const char *limit_string = g_getenv ("MONO_INLINE_CALLED_METHOD_NAME_LIMIT");
6929 if (limit_string != NULL)
6930 limit = limit_string;
6931 else
6932 limit = "";
6935 if (limit [0] != '\0') {
6936 char *called_method_name = mono_method_full_name (called_method, TRUE);
6938 strncmp_result = strncmp (called_method_name, limit, strlen (limit));
6939 g_free (called_method_name);
6941 //return (strncmp_result <= 0);
6942 return (strncmp_result == 0);
6943 } else {
6944 return TRUE;
6947 #endif
6949 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
6950 static gboolean
6951 check_inline_caller_method_name_limit (MonoMethod *caller_method)
6953 int strncmp_result;
6954 static const char *limit = NULL;
6956 if (limit == NULL) {
6957 const char *limit_string = g_getenv ("MONO_INLINE_CALLER_METHOD_NAME_LIMIT");
6958 if (limit_string != NULL) {
6959 limit = limit_string;
6960 } else {
6961 limit = "";
6965 if (limit [0] != '\0') {
6966 char *caller_method_name = mono_method_full_name (caller_method, TRUE);
6968 strncmp_result = strncmp (caller_method_name, limit, strlen (limit));
6969 g_free (caller_method_name);
6971 //return (strncmp_result <= 0);
6972 return (strncmp_result == 0);
6973 } else {
6974 return TRUE;
6977 #endif
6979 static void
6980 emit_init_rvar (MonoCompile *cfg, int dreg, MonoType *rtype)
6982 static double r8_0 = 0.0;
6983 static float r4_0 = 0.0;
6984 MonoInst *ins;
6985 int t;
6987 rtype = mini_get_underlying_type (rtype);
6988 t = rtype->type;
6990 if (rtype->byref) {
6991 MONO_EMIT_NEW_PCONST (cfg, dreg, NULL);
6992 } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
6993 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
6994 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
6995 MONO_EMIT_NEW_I8CONST (cfg, dreg, 0);
6996 } else if (cfg->r4fp && t == MONO_TYPE_R4) {
6997 MONO_INST_NEW (cfg, ins, OP_R4CONST);
6998 ins->type = STACK_R4;
6999 ins->inst_p0 = (void*)&r4_0;
7000 ins->dreg = dreg;
7001 MONO_ADD_INS (cfg->cbb, ins);
7002 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
7003 MONO_INST_NEW (cfg, ins, OP_R8CONST);
7004 ins->type = STACK_R8;
7005 ins->inst_p0 = (void*)&r8_0;
7006 ins->dreg = dreg;
7007 MONO_ADD_INS (cfg->cbb, ins);
7008 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
7009 ((t == MONO_TYPE_GENERICINST) && mono_type_generic_inst_is_valuetype (rtype))) {
7010 MONO_EMIT_NEW_VZERO (cfg, dreg, mono_class_from_mono_type (rtype));
7011 } else if (((t == MONO_TYPE_VAR) || (t == MONO_TYPE_MVAR)) && mini_type_var_is_vt (rtype)) {
7012 MONO_EMIT_NEW_VZERO (cfg, dreg, mono_class_from_mono_type (rtype));
7013 } else {
7014 MONO_EMIT_NEW_PCONST (cfg, dreg, NULL);
7018 static void
7019 emit_dummy_init_rvar (MonoCompile *cfg, int dreg, MonoType *rtype)
7021 int t;
7023 rtype = mini_get_underlying_type (rtype);
7024 t = rtype->type;
7026 if (rtype->byref) {
7027 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_PCONST);
7028 } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
7029 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_ICONST);
7030 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
7031 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_I8CONST);
7032 } else if (cfg->r4fp && t == MONO_TYPE_R4) {
7033 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_R4CONST);
7034 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
7035 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_R8CONST);
7036 } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
7037 ((t == MONO_TYPE_GENERICINST) && mono_type_generic_inst_is_valuetype (rtype))) {
7038 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_VZERO);
7039 } else if (((t == MONO_TYPE_VAR) || (t == MONO_TYPE_MVAR)) && mini_type_var_is_vt (rtype)) {
7040 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_VZERO);
7041 } else {
7042 emit_init_rvar (cfg, dreg, rtype);
7046 /* If INIT is FALSE, emit dummy initialization statements to keep the IR valid */
7047 static void
7048 emit_init_local (MonoCompile *cfg, int local, MonoType *type, gboolean init)
7050 MonoInst *var = cfg->locals [local];
7051 if (COMPILE_SOFT_FLOAT (cfg)) {
7052 MonoInst *store;
7053 int reg = alloc_dreg (cfg, (MonoStackType)var->type);
7054 emit_init_rvar (cfg, reg, type);
7055 EMIT_NEW_LOCSTORE (cfg, store, local, cfg->cbb->last_ins);
7056 } else {
7057 if (init)
7058 emit_init_rvar (cfg, var->dreg, type);
7059 else
7060 emit_dummy_init_rvar (cfg, var->dreg, type);
7065 * inline_method:
7067 * Return the cost of inlining CMETHOD.
7069 static int
7070 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp,
7071 guchar *ip, guint real_offset, gboolean inline_always)
7073 MonoError error;
7074 MonoInst *ins, *rvar = NULL;
7075 MonoMethodHeader *cheader;
7076 MonoBasicBlock *ebblock, *sbblock;
7077 int i, costs;
7078 MonoMethod *prev_inlined_method;
7079 MonoInst **prev_locals, **prev_args;
7080 MonoType **prev_arg_types;
7081 guint prev_real_offset;
7082 GHashTable *prev_cbb_hash;
7083 MonoBasicBlock **prev_cil_offset_to_bb;
7084 MonoBasicBlock *prev_cbb;
7085 unsigned char* prev_cil_start;
7086 guint32 prev_cil_offset_to_bb_len;
7087 MonoMethod *prev_current_method;
7088 MonoGenericContext *prev_generic_context;
7089 gboolean ret_var_set, prev_ret_var_set, prev_disable_inline, virtual_ = FALSE;
7091 g_assert (cfg->exception_type == MONO_EXCEPTION_NONE);
7093 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
7094 if ((! inline_always) && ! check_inline_called_method_name_limit (cmethod))
7095 return 0;
7096 #endif
7097 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
7098 if ((! inline_always) && ! check_inline_caller_method_name_limit (cfg->method))
7099 return 0;
7100 #endif
7102 if (!fsig)
7103 fsig = mono_method_signature (cmethod);
7105 if (cfg->verbose_level > 2)
7106 printf ("INLINE START %p %s -> %s\n", cmethod, mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
7108 if (!cmethod->inline_info) {
7109 cfg->stat_inlineable_methods++;
7110 cmethod->inline_info = 1;
7113 /* allocate local variables */
7114 cheader = mono_method_get_header_checked (cmethod, &error);
7115 if (!cheader) {
7116 if (inline_always) {
7117 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
7118 mono_error_move (&cfg->error, &error);
7119 } else {
7120 mono_error_cleanup (&error);
7122 return 0;
7125 /*Must verify before creating locals as it can cause the JIT to assert.*/
7126 if (mono_compile_is_broken (cfg, cmethod, FALSE)) {
7127 mono_metadata_free_mh (cheader);
7128 return 0;
7131 /* allocate space to store the return value */
7132 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
7133 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
7136 prev_locals = cfg->locals;
7137 cfg->locals = (MonoInst **)mono_mempool_alloc0 (cfg->mempool, cheader->num_locals * sizeof (MonoInst*));
7138 for (i = 0; i < cheader->num_locals; ++i)
7139 cfg->locals [i] = mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
7141 /* allocate start and end blocks */
7142 /* This is needed so if the inline is aborted, we can clean up */
7143 NEW_BBLOCK (cfg, sbblock);
7144 sbblock->real_offset = real_offset;
7146 NEW_BBLOCK (cfg, ebblock);
7147 ebblock->block_num = cfg->num_bblocks++;
7148 ebblock->real_offset = real_offset;
7150 prev_args = cfg->args;
7151 prev_arg_types = cfg->arg_types;
7152 prev_inlined_method = cfg->inlined_method;
7153 cfg->inlined_method = cmethod;
7154 cfg->ret_var_set = FALSE;
7155 cfg->inline_depth ++;
7156 prev_real_offset = cfg->real_offset;
7157 prev_cbb_hash = cfg->cbb_hash;
7158 prev_cil_offset_to_bb = cfg->cil_offset_to_bb;
7159 prev_cil_offset_to_bb_len = cfg->cil_offset_to_bb_len;
7160 prev_cil_start = cfg->cil_start;
7161 prev_cbb = cfg->cbb;
7162 prev_current_method = cfg->current_method;
7163 prev_generic_context = cfg->generic_context;
7164 prev_ret_var_set = cfg->ret_var_set;
7165 prev_disable_inline = cfg->disable_inline;
7167 if (ip && *ip == CEE_CALLVIRT && !(cmethod->flags & METHOD_ATTRIBUTE_STATIC))
7168 virtual_ = TRUE;
7170 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, rvar, sp, real_offset, virtual_);
7172 ret_var_set = cfg->ret_var_set;
7174 cfg->inlined_method = prev_inlined_method;
7175 cfg->real_offset = prev_real_offset;
7176 cfg->cbb_hash = prev_cbb_hash;
7177 cfg->cil_offset_to_bb = prev_cil_offset_to_bb;
7178 cfg->cil_offset_to_bb_len = prev_cil_offset_to_bb_len;
7179 cfg->cil_start = prev_cil_start;
7180 cfg->locals = prev_locals;
7181 cfg->args = prev_args;
7182 cfg->arg_types = prev_arg_types;
7183 cfg->current_method = prev_current_method;
7184 cfg->generic_context = prev_generic_context;
7185 cfg->ret_var_set = prev_ret_var_set;
7186 cfg->disable_inline = prev_disable_inline;
7187 cfg->inline_depth --;
7189 if ((costs >= 0 && costs < 60) || inline_always || (costs >= 0 && (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING))) {
7190 if (cfg->verbose_level > 2)
7191 printf ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
7193 cfg->stat_inlined_methods++;
7195 /* always add some code to avoid block split failures */
7196 MONO_INST_NEW (cfg, ins, OP_NOP);
7197 MONO_ADD_INS (prev_cbb, ins);
7199 prev_cbb->next_bb = sbblock;
7200 link_bblock (cfg, prev_cbb, sbblock);
7203 * Get rid of the begin and end bblocks if possible to aid local
7204 * optimizations.
7206 mono_merge_basic_blocks (cfg, prev_cbb, sbblock);
7208 if ((prev_cbb->out_count == 1) && (prev_cbb->out_bb [0]->in_count == 1) && (prev_cbb->out_bb [0] != ebblock))
7209 mono_merge_basic_blocks (cfg, prev_cbb, prev_cbb->out_bb [0]);
7211 if ((ebblock->in_count == 1) && ebblock->in_bb [0]->out_count == 1) {
7212 MonoBasicBlock *prev = ebblock->in_bb [0];
7214 if (prev->next_bb == ebblock) {
7215 mono_merge_basic_blocks (cfg, prev, ebblock);
7216 cfg->cbb = prev;
7217 if ((prev_cbb->out_count == 1) && (prev_cbb->out_bb [0]->in_count == 1) && (prev_cbb->out_bb [0] == prev)) {
7218 mono_merge_basic_blocks (cfg, prev_cbb, prev);
7219 cfg->cbb = prev_cbb;
7221 } else {
7222 /* There could be a bblock after 'prev', and making 'prev' the current bb could cause problems */
7223 cfg->cbb = ebblock;
7225 } else {
7227 * Its possible that the rvar is set in some prev bblock, but not in others.
7228 * (#1835).
7230 if (rvar) {
7231 MonoBasicBlock *bb;
7233 for (i = 0; i < ebblock->in_count; ++i) {
7234 bb = ebblock->in_bb [i];
7236 if (bb->last_ins && bb->last_ins->opcode == OP_NOT_REACHED) {
7237 cfg->cbb = bb;
7239 emit_init_rvar (cfg, rvar->dreg, fsig->ret);
7244 cfg->cbb = ebblock;
7247 if (rvar) {
7249 * If the inlined method contains only a throw, then the ret var is not
7250 * set, so set it to a dummy value.
7252 if (!ret_var_set)
7253 emit_init_rvar (cfg, rvar->dreg, fsig->ret);
7255 EMIT_NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
7256 *sp++ = ins;
7258 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, cheader);
7259 return costs + 1;
7260 } else {
7261 if (cfg->verbose_level > 2)
7262 printf ("INLINE ABORTED %s (cost %d)\n", mono_method_full_name (cmethod, TRUE), costs);
7263 cfg->exception_type = MONO_EXCEPTION_NONE;
7265 /* This gets rid of the newly added bblocks */
7266 cfg->cbb = prev_cbb;
7268 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, cheader);
7269 return 0;
7273 * Some of these comments may well be out-of-date.
7274 * Design decisions: we do a single pass over the IL code (and we do bblock
7275 * splitting/merging in the few cases when it's required: a back jump to an IL
7276 * address that was not already seen as bblock starting point).
7277 * Code is validated as we go (full verification is still better left to metadata/verify.c).
7278 * Complex operations are decomposed in simpler ones right away. We need to let the
7279 * arch-specific code peek and poke inside this process somehow (except when the
7280 * optimizations can take advantage of the full semantic info of coarse opcodes).
7281 * All the opcodes of the form opcode.s are 'normalized' to opcode.
7282 * MonoInst->opcode initially is the IL opcode or some simplification of that
7283 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
7284 * opcode with value bigger than OP_LAST.
7285 * At this point the IR can be handed over to an interpreter, a dumb code generator
7286 * or to the optimizing code generator that will translate it to SSA form.
7288 * Profiling directed optimizations.
7289 * We may compile by default with few or no optimizations and instrument the code
7290 * or the user may indicate what methods to optimize the most either in a config file
7291 * or through repeated runs where the compiler applies offline the optimizations to
7292 * each method and then decides if it was worth it.
7295 #define CHECK_TYPE(ins) if (!(ins)->type) UNVERIFIED
7296 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) UNVERIFIED
7297 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) UNVERIFIED
7298 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) UNVERIFIED
7299 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) UNVERIFIED
7300 #define CHECK_OPSIZE(size) if (ip + size > end) UNVERIFIED
7301 #define CHECK_UNVERIFIABLE(cfg) if (cfg->unverifiable) UNVERIFIED
7302 #define CHECK_TYPELOAD(klass) if (!(klass) || mono_class_has_failure (klass)) TYPE_LOAD_ERROR ((klass))
7304 /* offset from br.s -> br like opcodes */
7305 #define BIG_BRANCH_OFFSET 13
7307 static gboolean
7308 ip_in_bb (MonoCompile *cfg, MonoBasicBlock *bb, const guint8* ip)
7310 MonoBasicBlock *b = cfg->cil_offset_to_bb [ip - cfg->cil_start];
7312 return b == NULL || b == bb;
7315 static int
7316 get_basic_blocks (MonoCompile *cfg, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
7318 unsigned char *ip = start;
7319 unsigned char *target;
7320 int i;
7321 guint cli_addr;
7322 MonoBasicBlock *bblock;
7323 const MonoOpcode *opcode;
7325 while (ip < end) {
7326 cli_addr = ip - start;
7327 i = mono_opcode_value ((const guint8 **)&ip, end);
7328 if (i < 0)
7329 UNVERIFIED;
7330 opcode = &mono_opcodes [i];
7331 switch (opcode->argument) {
7332 case MonoInlineNone:
7333 ip++;
7334 break;
7335 case MonoInlineString:
7336 case MonoInlineType:
7337 case MonoInlineField:
7338 case MonoInlineMethod:
7339 case MonoInlineTok:
7340 case MonoInlineSig:
7341 case MonoShortInlineR:
7342 case MonoInlineI:
7343 ip += 5;
7344 break;
7345 case MonoInlineVar:
7346 ip += 3;
7347 break;
7348 case MonoShortInlineVar:
7349 case MonoShortInlineI:
7350 ip += 2;
7351 break;
7352 case MonoShortInlineBrTarget:
7353 target = start + cli_addr + 2 + (signed char)ip [1];
7354 GET_BBLOCK (cfg, bblock, target);
7355 ip += 2;
7356 if (ip < end)
7357 GET_BBLOCK (cfg, bblock, ip);
7358 break;
7359 case MonoInlineBrTarget:
7360 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
7361 GET_BBLOCK (cfg, bblock, target);
7362 ip += 5;
7363 if (ip < end)
7364 GET_BBLOCK (cfg, bblock, ip);
7365 break;
7366 case MonoInlineSwitch: {
7367 guint32 n = read32 (ip + 1);
7368 guint32 j;
7369 ip += 5;
7370 cli_addr += 5 + 4 * n;
7371 target = start + cli_addr;
7372 GET_BBLOCK (cfg, bblock, target);
7374 for (j = 0; j < n; ++j) {
7375 target = start + cli_addr + (gint32)read32 (ip);
7376 GET_BBLOCK (cfg, bblock, target);
7377 ip += 4;
7379 break;
7381 case MonoInlineR:
7382 case MonoInlineI8:
7383 ip += 9;
7384 break;
7385 default:
7386 g_assert_not_reached ();
7389 if (i == CEE_THROW) {
7390 unsigned char *bb_start = ip - 1;
7392 /* Find the start of the bblock containing the throw */
7393 bblock = NULL;
7394 while ((bb_start >= start) && !bblock) {
7395 bblock = cfg->cil_offset_to_bb [(bb_start) - start];
7396 bb_start --;
7398 if (bblock)
7399 bblock->out_of_line = 1;
7402 return 0;
7403 unverified:
7404 exception_exit:
7405 *pos = ip;
7406 return 1;
7409 static inline MonoMethod *
7410 mini_get_method_allow_open (MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context, MonoError *error)
7412 MonoMethod *method;
7414 mono_error_init (error);
7416 if (m->wrapper_type != MONO_WRAPPER_NONE) {
7417 method = (MonoMethod *)mono_method_get_wrapper_data (m, token);
7418 if (context) {
7419 method = mono_class_inflate_generic_method_checked (method, context, error);
7421 } else {
7422 method = mono_get_method_checked (m->klass->image, token, klass, context, error);
7425 return method;
7428 static inline MonoMethod *
7429 mini_get_method (MonoCompile *cfg, MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context)
7431 MonoError error;
7432 MonoMethod *method = mini_get_method_allow_open (m, token, klass, context, cfg ? &cfg->error : &error);
7434 if (method && cfg && !cfg->gshared && mono_class_is_open_constructed_type (&method->klass->byval_arg)) {
7435 mono_error_set_bad_image (&cfg->error, cfg->method->klass->image, "Method with open type while not compiling gshared");
7436 method = NULL;
7439 if (!method && !cfg)
7440 mono_error_cleanup (&error); /* FIXME don't swallow the error */
7442 return method;
7445 static inline MonoClass*
7446 mini_get_class (MonoMethod *method, guint32 token, MonoGenericContext *context)
7448 MonoError error;
7449 MonoClass *klass;
7451 if (method->wrapper_type != MONO_WRAPPER_NONE) {
7452 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
7453 if (context) {
7454 klass = mono_class_inflate_generic_class_checked (klass, context, &error);
7455 mono_error_cleanup (&error); /* FIXME don't swallow the error */
7457 } else {
7458 klass = mono_class_get_and_inflate_typespec_checked (method->klass->image, token, context, &error);
7459 mono_error_cleanup (&error); /* FIXME don't swallow the error */
7461 if (klass)
7462 mono_class_init (klass);
7463 return klass;
7466 static inline MonoMethodSignature*
7467 mini_get_signature (MonoMethod *method, guint32 token, MonoGenericContext *context, MonoError *error)
7469 MonoMethodSignature *fsig;
7471 mono_error_init (error);
7472 if (method->wrapper_type != MONO_WRAPPER_NONE) {
7473 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
7474 } else {
7475 fsig = mono_metadata_parse_signature_checked (method->klass->image, token, error);
7476 return_val_if_nok (error, NULL);
7478 if (context) {
7479 fsig = mono_inflate_generic_signature(fsig, context, error);
7481 return fsig;
7484 static MonoMethod*
7485 throw_exception (void)
7487 static MonoMethod *method = NULL;
7489 if (!method) {
7490 MonoSecurityManager *secman = mono_security_manager_get_methods ();
7491 method = mono_class_get_method_from_name (secman->securitymanager, "ThrowException", 1);
7493 g_assert (method);
7494 return method;
7497 static void
7498 emit_throw_exception (MonoCompile *cfg, MonoException *ex)
7500 MonoMethod *thrower = throw_exception ();
7501 MonoInst *args [1];
7503 EMIT_NEW_PCONST (cfg, args [0], ex);
7504 mono_emit_method_call (cfg, thrower, args, NULL);
7508 * Return the original method is a wrapper is specified. We can only access
7509 * the custom attributes from the original method.
7511 static MonoMethod*
7512 get_original_method (MonoMethod *method)
7514 if (method->wrapper_type == MONO_WRAPPER_NONE)
7515 return method;
7517 /* native code (which is like Critical) can call any managed method XXX FIXME XXX to validate all usages */
7518 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED)
7519 return NULL;
7521 /* in other cases we need to find the original method */
7522 return mono_marshal_method_from_wrapper (method);
7525 static void
7526 ensure_method_is_allowed_to_access_field (MonoCompile *cfg, MonoMethod *caller, MonoClassField *field)
7528 /* we can't get the coreclr security level on wrappers since they don't have the attributes */
7529 MonoException *ex = mono_security_core_clr_is_field_access_allowed (get_original_method (caller), field);
7530 if (ex)
7531 emit_throw_exception (cfg, ex);
7534 static void
7535 ensure_method_is_allowed_to_call_method (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee)
7537 /* we can't get the coreclr security level on wrappers since they don't have the attributes */
7538 MonoException *ex = mono_security_core_clr_is_call_allowed (get_original_method (caller), callee);
7539 if (ex)
7540 emit_throw_exception (cfg, ex);
7544 * Check that the IL instructions at ip are the array initialization
7545 * sequence and return the pointer to the data and the size.
7547 static const char*
7548 initialize_array_data (MonoMethod *method, gboolean aot, unsigned char *ip, MonoClass *klass, guint32 len, int *out_size, guint32 *out_field_token)
7551 * newarr[System.Int32]
7552 * dup
7553 * ldtoken field valuetype ...
7554 * call void class [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(class [mscorlib]System.Array, valuetype [mscorlib]System.RuntimeFieldHandle)
7556 if (ip [0] == CEE_DUP && ip [1] == CEE_LDTOKEN && ip [5] == 0x4 && ip [6] == CEE_CALL) {
7557 MonoError error;
7558 guint32 token = read32 (ip + 7);
7559 guint32 field_token = read32 (ip + 2);
7560 guint32 field_index = field_token & 0xffffff;
7561 guint32 rva;
7562 const char *data_ptr;
7563 int size = 0;
7564 MonoMethod *cmethod;
7565 MonoClass *dummy_class;
7566 MonoClassField *field = mono_field_from_token_checked (method->klass->image, field_token, &dummy_class, NULL, &error);
7567 int dummy_align;
7569 if (!field) {
7570 mono_error_cleanup (&error); /* FIXME don't swallow the error */
7571 return NULL;
7574 *out_field_token = field_token;
7576 cmethod = mini_get_method (NULL, method, token, NULL, NULL);
7577 if (!cmethod)
7578 return NULL;
7579 if (strcmp (cmethod->name, "InitializeArray") || strcmp (cmethod->klass->name, "RuntimeHelpers") || cmethod->klass->image != mono_defaults.corlib)
7580 return NULL;
7581 switch (mono_type_get_underlying_type (&klass->byval_arg)->type) {
7582 case MONO_TYPE_BOOLEAN:
7583 case MONO_TYPE_I1:
7584 case MONO_TYPE_U1:
7585 size = 1; break;
7586 /* we need to swap on big endian, so punt. Should we handle R4 and R8 as well? */
7587 #if TARGET_BYTE_ORDER == G_LITTLE_ENDIAN
7588 case MONO_TYPE_CHAR:
7589 case MONO_TYPE_I2:
7590 case MONO_TYPE_U2:
7591 size = 2; break;
7592 case MONO_TYPE_I4:
7593 case MONO_TYPE_U4:
7594 case MONO_TYPE_R4:
7595 size = 4; break;
7596 case MONO_TYPE_R8:
7597 case MONO_TYPE_I8:
7598 case MONO_TYPE_U8:
7599 size = 8; break;
7600 #endif
7601 default:
7602 return NULL;
7604 size *= len;
7605 if (size > mono_type_size (field->type, &dummy_align))
7606 return NULL;
7607 *out_size = size;
7608 /*g_print ("optimized in %s: size: %d, numelems: %d\n", method->name, size, newarr->inst_newa_len->inst_c0);*/
7609 if (!image_is_dynamic (method->klass->image)) {
7610 field_index = read32 (ip + 2) & 0xffffff;
7611 mono_metadata_field_info (method->klass->image, field_index - 1, NULL, &rva, NULL);
7612 data_ptr = mono_image_rva_map (method->klass->image, rva);
7613 /*g_print ("field: 0x%08x, rva: %d, rva_ptr: %p\n", read32 (ip + 2), rva, data_ptr);*/
7614 /* for aot code we do the lookup on load */
7615 if (aot && data_ptr)
7616 return (const char *)GUINT_TO_POINTER (rva);
7617 } else {
7618 /*FIXME is it possible to AOT a SRE assembly not meant to be saved? */
7619 g_assert (!aot);
7620 data_ptr = mono_field_get_data (field);
7622 return data_ptr;
7624 return NULL;
7627 static void
7628 set_exception_type_from_invalid_il (MonoCompile *cfg, MonoMethod *method, unsigned char *ip)
7630 MonoError error;
7631 char *method_fname = mono_method_full_name (method, TRUE);
7632 char *method_code;
7633 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
7635 if (!header) {
7636 method_code = g_strdup_printf ("could not parse method body due to %s", mono_error_get_message (&error));
7637 mono_error_cleanup (&error);
7638 } else if (header->code_size == 0)
7639 method_code = g_strdup ("method body is empty.");
7640 else
7641 method_code = mono_disasm_code_one (NULL, method, ip, NULL);
7642 mono_cfg_set_exception_invalid_program (cfg, g_strdup_printf ("Invalid IL code in %s: %s\n", method_fname, method_code));
7643 g_free (method_fname);
7644 g_free (method_code);
7645 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, header);
7648 static void
7649 emit_stloc_ir (MonoCompile *cfg, MonoInst **sp, MonoMethodHeader *header, int n)
7651 MonoInst *ins;
7652 guint32 opcode = mono_type_to_regmove (cfg, header->locals [n]);
7653 if ((opcode == OP_MOVE) && cfg->cbb->last_ins == sp [0] &&
7654 ((sp [0]->opcode == OP_ICONST) || (sp [0]->opcode == OP_I8CONST))) {
7655 /* Optimize reg-reg moves away */
7657 * Can't optimize other opcodes, since sp[0] might point to
7658 * the last ins of a decomposed opcode.
7660 sp [0]->dreg = (cfg)->locals [n]->dreg;
7661 } else {
7662 EMIT_NEW_LOCSTORE (cfg, ins, n, *sp);
7667 * ldloca inhibits many optimizations so try to get rid of it in common
7668 * cases.
7670 static inline unsigned char *
7671 emit_optimized_ldloca_ir (MonoCompile *cfg, unsigned char *ip, unsigned char *end, int size)
7673 int local, token;
7674 MonoClass *klass;
7675 MonoType *type;
7677 if (size == 1) {
7678 local = ip [1];
7679 ip += 2;
7680 } else {
7681 local = read16 (ip + 2);
7682 ip += 4;
7685 if (ip + 6 < end && (ip [0] == CEE_PREFIX1) && (ip [1] == CEE_INITOBJ) && ip_in_bb (cfg, cfg->cbb, ip + 1)) {
7686 /* From the INITOBJ case */
7687 token = read32 (ip + 2);
7688 klass = mini_get_class (cfg->current_method, token, cfg->generic_context);
7689 CHECK_TYPELOAD (klass);
7690 type = mini_get_underlying_type (&klass->byval_arg);
7691 emit_init_local (cfg, local, type, TRUE);
7692 return ip + 6;
7694 exception_exit:
7695 return NULL;
7698 static MonoInst*
7699 emit_llvmonly_virtual_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, int context_used, MonoInst **sp)
7701 MonoInst *icall_args [16];
7702 MonoInst *call_target, *ins, *vtable_ins;
7703 int arg_reg, this_reg, vtable_reg;
7704 gboolean is_iface = cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE;
7705 gboolean is_gsharedvt = cfg->gsharedvt && mini_is_gsharedvt_variable_signature (fsig);
7706 gboolean variant_iface = FALSE;
7707 guint32 slot;
7708 int offset;
7711 * In llvm-only mode, vtables contain function descriptors instead of
7712 * method addresses/trampolines.
7714 MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
7716 if (is_iface)
7717 slot = mono_method_get_imt_slot (cmethod);
7718 else
7719 slot = mono_method_get_vtable_index (cmethod);
7721 this_reg = sp [0]->dreg;
7723 if (is_iface && mono_class_has_variant_generic_params (cmethod->klass))
7724 variant_iface = TRUE;
7726 if (!fsig->generic_param_count && !is_iface && !is_gsharedvt) {
7728 * The simplest case, a normal virtual call.
7730 int slot_reg = alloc_preg (cfg);
7731 int addr_reg = alloc_preg (cfg);
7732 int arg_reg = alloc_preg (cfg);
7733 MonoBasicBlock *non_null_bb;
7735 vtable_reg = alloc_preg (cfg);
7736 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_ins, OP_LOAD_MEMBASE, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
7737 offset = MONO_STRUCT_OFFSET (MonoVTable, vtable) + (slot * SIZEOF_VOID_P);
7739 /* Load the vtable slot, which contains a function descriptor. */
7740 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, slot_reg, vtable_reg, offset);
7742 NEW_BBLOCK (cfg, non_null_bb);
7744 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, slot_reg, 0);
7745 cfg->cbb->last_ins->flags |= MONO_INST_LIKELY;
7746 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, non_null_bb);
7748 /* Slow path */
7749 // FIXME: Make the wrapper use the preserveall cconv
7750 // FIXME: Use one icall per slot for small slot numbers ?
7751 icall_args [0] = vtable_ins;
7752 EMIT_NEW_ICONST (cfg, icall_args [1], slot);
7753 /* Make the icall return the vtable slot value to save some code space */
7754 ins = mono_emit_jit_icall (cfg, mono_init_vtable_slot, icall_args);
7755 ins->dreg = slot_reg;
7756 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, non_null_bb);
7758 /* Fastpath */
7759 MONO_START_BB (cfg, non_null_bb);
7760 /* Load the address + arg from the vtable slot */
7761 EMIT_NEW_LOAD_MEMBASE (cfg, call_target, OP_LOAD_MEMBASE, addr_reg, slot_reg, 0);
7762 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, arg_reg, slot_reg, SIZEOF_VOID_P);
7764 return emit_extra_arg_calli (cfg, fsig, sp, arg_reg, call_target);
7767 if (!fsig->generic_param_count && is_iface && !variant_iface && !is_gsharedvt) {
7769 * A simple interface call
7771 * We make a call through an imt slot to obtain the function descriptor we need to call.
7772 * The imt slot contains a function descriptor for a runtime function + arg.
7774 int slot_reg = alloc_preg (cfg);
7775 int addr_reg = alloc_preg (cfg);
7776 int arg_reg = alloc_preg (cfg);
7777 MonoInst *thunk_addr_ins, *thunk_arg_ins, *ftndesc_ins;
7779 vtable_reg = alloc_preg (cfg);
7780 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_ins, OP_LOAD_MEMBASE, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
7781 offset = ((gint32)slot - MONO_IMT_SIZE) * SIZEOF_VOID_P;
7784 * The slot is already initialized when the vtable is created so there is no need
7785 * to check it here.
7788 /* Load the imt slot, which contains a function descriptor. */
7789 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, slot_reg, vtable_reg, offset);
7791 /* Load the address + arg of the imt thunk from the imt slot */
7792 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_addr_ins, OP_LOAD_MEMBASE, addr_reg, slot_reg, 0);
7793 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_arg_ins, OP_LOAD_MEMBASE, arg_reg, slot_reg, SIZEOF_VOID_P);
7795 * IMT thunks in llvm-only mode are C functions which take an info argument
7796 * plus the imt method and return the ftndesc to call.
7798 icall_args [0] = thunk_arg_ins;
7799 icall_args [1] = emit_get_rgctx_method (cfg, context_used,
7800 cmethod, MONO_RGCTX_INFO_METHOD);
7801 ftndesc_ins = mono_emit_calli (cfg, helper_sig_llvmonly_imt_thunk, icall_args, thunk_addr_ins, NULL, NULL);
7803 return emit_llvmonly_calli (cfg, fsig, sp, ftndesc_ins);
7806 if ((fsig->generic_param_count || variant_iface) && !is_gsharedvt) {
7808 * This is similar to the interface case, the vtable slot points to an imt thunk which is
7809 * dynamically extended as more instantiations are discovered.
7810 * This handles generic virtual methods both on classes and interfaces.
7812 int slot_reg = alloc_preg (cfg);
7813 int addr_reg = alloc_preg (cfg);
7814 int arg_reg = alloc_preg (cfg);
7815 int ftndesc_reg = alloc_preg (cfg);
7816 MonoInst *thunk_addr_ins, *thunk_arg_ins, *ftndesc_ins;
7817 MonoBasicBlock *slowpath_bb, *end_bb;
7819 NEW_BBLOCK (cfg, slowpath_bb);
7820 NEW_BBLOCK (cfg, end_bb);
7822 vtable_reg = alloc_preg (cfg);
7823 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_ins, OP_LOAD_MEMBASE, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
7824 if (is_iface)
7825 offset = ((gint32)slot - MONO_IMT_SIZE) * SIZEOF_VOID_P;
7826 else
7827 offset = MONO_STRUCT_OFFSET (MonoVTable, vtable) + (slot * SIZEOF_VOID_P);
7829 /* Load the slot, which contains a function descriptor. */
7830 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, slot_reg, vtable_reg, offset);
7832 /* These slots are not initialized, so fall back to the slow path until they are initialized */
7833 /* That happens when mono_method_add_generic_virtual_invocation () creates an IMT thunk */
7834 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, slot_reg, 0);
7835 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, slowpath_bb);
7837 /* Fastpath */
7838 /* Same as with iface calls */
7839 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_addr_ins, OP_LOAD_MEMBASE, addr_reg, slot_reg, 0);
7840 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_arg_ins, OP_LOAD_MEMBASE, arg_reg, slot_reg, SIZEOF_VOID_P);
7841 icall_args [0] = thunk_arg_ins;
7842 icall_args [1] = emit_get_rgctx_method (cfg, context_used,
7843 cmethod, MONO_RGCTX_INFO_METHOD);
7844 ftndesc_ins = mono_emit_calli (cfg, helper_sig_llvmonly_imt_thunk, icall_args, thunk_addr_ins, NULL, NULL);
7845 ftndesc_ins->dreg = ftndesc_reg;
7847 * Unlike normal iface calls, these imt thunks can return NULL, i.e. when they are passed an instantiation
7848 * they don't know about yet. Fall back to the slowpath in that case.
7850 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, ftndesc_reg, 0);
7851 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, slowpath_bb);
7853 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
7855 /* Slowpath */
7856 MONO_START_BB (cfg, slowpath_bb);
7857 icall_args [0] = vtable_ins;
7858 EMIT_NEW_ICONST (cfg, icall_args [1], slot);
7859 icall_args [2] = emit_get_rgctx_method (cfg, context_used,
7860 cmethod, MONO_RGCTX_INFO_METHOD);
7861 if (is_iface)
7862 ftndesc_ins = mono_emit_jit_icall (cfg, mono_resolve_generic_virtual_iface_call, icall_args);
7863 else
7864 ftndesc_ins = mono_emit_jit_icall (cfg, mono_resolve_generic_virtual_call, icall_args);
7865 ftndesc_ins->dreg = ftndesc_reg;
7866 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
7868 /* Common case */
7869 MONO_START_BB (cfg, end_bb);
7870 return emit_llvmonly_calli (cfg, fsig, sp, ftndesc_ins);
7874 * Non-optimized cases
7876 icall_args [0] = sp [0];
7877 EMIT_NEW_ICONST (cfg, icall_args [1], slot);
7879 icall_args [2] = emit_get_rgctx_method (cfg, context_used,
7880 cmethod, MONO_RGCTX_INFO_METHOD);
7882 arg_reg = alloc_preg (cfg);
7883 MONO_EMIT_NEW_PCONST (cfg, arg_reg, NULL);
7884 EMIT_NEW_VARLOADA_VREG (cfg, icall_args [3], arg_reg, &mono_defaults.int_class->byval_arg);
7886 g_assert (is_gsharedvt);
7887 if (is_iface)
7888 call_target = mono_emit_jit_icall (cfg, mono_resolve_iface_call_gsharedvt, icall_args);
7889 else
7890 call_target = mono_emit_jit_icall (cfg, mono_resolve_vcall_gsharedvt, icall_args);
7893 * Pass the extra argument even if the callee doesn't receive it, most
7894 * calling conventions allow this.
7896 return emit_extra_arg_calli (cfg, fsig, sp, arg_reg, call_target);
7899 static gboolean
7900 is_exception_class (MonoClass *klass)
7902 while (klass) {
7903 if (klass == mono_defaults.exception_class)
7904 return TRUE;
7905 klass = klass->parent;
7907 return FALSE;
7911 * is_jit_optimizer_disabled:
7913 * Determine whenever M's assembly has a DebuggableAttribute with the
7914 * IsJITOptimizerDisabled flag set.
7916 static gboolean
7917 is_jit_optimizer_disabled (MonoMethod *m)
7919 MonoError error;
7920 MonoAssembly *ass = m->klass->image->assembly;
7921 MonoCustomAttrInfo* attrs;
7922 MonoClass *klass;
7923 int i;
7924 gboolean val = FALSE;
7926 g_assert (ass);
7927 if (ass->jit_optimizer_disabled_inited)
7928 return ass->jit_optimizer_disabled;
7930 klass = mono_class_try_get_debuggable_attribute_class ();
7932 if (!klass) {
7933 /* Linked away */
7934 ass->jit_optimizer_disabled = FALSE;
7935 mono_memory_barrier ();
7936 ass->jit_optimizer_disabled_inited = TRUE;
7937 return FALSE;
7940 attrs = mono_custom_attrs_from_assembly_checked (ass, &error);
7941 mono_error_cleanup (&error); /* FIXME don't swallow the error */
7942 if (attrs) {
7943 for (i = 0; i < attrs->num_attrs; ++i) {
7944 MonoCustomAttrEntry *attr = &attrs->attrs [i];
7945 const gchar *p;
7946 MonoMethodSignature *sig;
7948 if (!attr->ctor || attr->ctor->klass != klass)
7949 continue;
7950 /* Decode the attribute. See reflection.c */
7951 p = (const char*)attr->data;
7952 g_assert (read16 (p) == 0x0001);
7953 p += 2;
7955 // FIXME: Support named parameters
7956 sig = mono_method_signature (attr->ctor);
7957 if (sig->param_count != 2 || sig->params [0]->type != MONO_TYPE_BOOLEAN || sig->params [1]->type != MONO_TYPE_BOOLEAN)
7958 continue;
7959 /* Two boolean arguments */
7960 p ++;
7961 val = *p;
7963 mono_custom_attrs_free (attrs);
7966 ass->jit_optimizer_disabled = val;
7967 mono_memory_barrier ();
7968 ass->jit_optimizer_disabled_inited = TRUE;
7970 return val;
7973 static gboolean
7974 is_supported_tail_call (MonoCompile *cfg, MonoMethod *method, MonoMethod *cmethod, MonoMethodSignature *fsig, int call_opcode)
7976 gboolean supported_tail_call;
7977 int i;
7979 supported_tail_call = mono_arch_tail_call_supported (cfg, mono_method_signature (method), mono_method_signature (cmethod));
7981 for (i = 0; i < fsig->param_count; ++i) {
7982 if (fsig->params [i]->byref || fsig->params [i]->type == MONO_TYPE_PTR || fsig->params [i]->type == MONO_TYPE_FNPTR)
7983 /* These can point to the current method's stack */
7984 supported_tail_call = FALSE;
7986 if (fsig->hasthis && cmethod->klass->valuetype)
7987 /* this might point to the current method's stack */
7988 supported_tail_call = FALSE;
7989 if (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
7990 supported_tail_call = FALSE;
7991 if (cfg->method->save_lmf)
7992 supported_tail_call = FALSE;
7993 if (cmethod->wrapper_type && cmethod->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD)
7994 supported_tail_call = FALSE;
7995 if (call_opcode != CEE_CALL)
7996 supported_tail_call = FALSE;
7998 /* Debugging support */
7999 #if 0
8000 if (supported_tail_call) {
8001 if (!mono_debug_count ())
8002 supported_tail_call = FALSE;
8004 #endif
8006 return supported_tail_call;
8010 * handle_ctor_call:
8012 * Handle calls made to ctors from NEWOBJ opcodes.
8014 static void
8015 handle_ctor_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, int context_used,
8016 MonoInst **sp, guint8 *ip, int *inline_costs)
8018 MonoInst *vtable_arg = NULL, *callvirt_this_arg = NULL, *ins;
8020 if (cmethod->klass->valuetype && mono_class_generic_sharing_enabled (cmethod->klass) &&
8021 mono_method_is_generic_sharable (cmethod, TRUE)) {
8022 if (cmethod->is_inflated && mono_method_get_context (cmethod)->method_inst) {
8023 mono_class_vtable (cfg->domain, cmethod->klass);
8024 CHECK_TYPELOAD (cmethod->klass);
8026 vtable_arg = emit_get_rgctx_method (cfg, context_used,
8027 cmethod, MONO_RGCTX_INFO_METHOD_RGCTX);
8028 } else {
8029 if (context_used) {
8030 vtable_arg = emit_get_rgctx_klass (cfg, context_used,
8031 cmethod->klass, MONO_RGCTX_INFO_VTABLE);
8032 } else {
8033 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
8035 CHECK_TYPELOAD (cmethod->klass);
8036 EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
8041 /* Avoid virtual calls to ctors if possible */
8042 if (mono_class_is_marshalbyref (cmethod->klass))
8043 callvirt_this_arg = sp [0];
8045 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_ctor (cfg, cmethod, fsig, sp))) {
8046 g_assert (MONO_TYPE_IS_VOID (fsig->ret));
8047 CHECK_CFG_EXCEPTION;
8048 } else if ((cfg->opt & MONO_OPT_INLINE) && cmethod && !context_used && !vtable_arg &&
8049 mono_method_check_inlining (cfg, cmethod) &&
8050 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE)) {
8051 int costs;
8053 if ((costs = inline_method (cfg, cmethod, fsig, sp, ip, cfg->real_offset, FALSE))) {
8054 cfg->real_offset += 5;
8056 *inline_costs += costs - 5;
8057 } else {
8058 INLINE_FAILURE ("inline failure");
8059 // FIXME-VT: Clean this up
8060 if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig))
8061 GSHAREDVT_FAILURE(*ip);
8062 mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, callvirt_this_arg, NULL, NULL);
8064 } else if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) {
8065 MonoInst *addr;
8067 addr = emit_get_rgctx_gsharedvt_call (cfg, context_used, fsig, cmethod, MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE);
8069 if (cfg->llvm_only) {
8070 // FIXME: Avoid initializing vtable_arg
8071 emit_llvmonly_calli (cfg, fsig, sp, addr);
8072 } else {
8073 mono_emit_calli (cfg, fsig, sp, addr, NULL, vtable_arg);
8075 } else if (context_used &&
8076 ((!mono_method_is_generic_sharable_full (cmethod, TRUE, FALSE, FALSE) ||
8077 !mono_class_generic_sharing_enabled (cmethod->klass)) || cfg->gsharedvt)) {
8078 MonoInst *cmethod_addr;
8080 /* Generic calls made out of gsharedvt methods cannot be patched, so use an indirect call */
8082 if (cfg->llvm_only) {
8083 MonoInst *addr = emit_get_rgctx_method (cfg, context_used, cmethod,
8084 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
8085 emit_llvmonly_calli (cfg, fsig, sp, addr);
8086 } else {
8087 cmethod_addr = emit_get_rgctx_method (cfg, context_used,
8088 cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
8090 mono_emit_calli (cfg, fsig, sp, cmethod_addr, NULL, vtable_arg);
8092 } else {
8093 INLINE_FAILURE ("ctor call");
8094 ins = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp,
8095 callvirt_this_arg, NULL, vtable_arg);
8097 exception_exit:
8098 return;
8101 static void
8102 emit_setret (MonoCompile *cfg, MonoInst *val)
8104 MonoType *ret_type = mini_get_underlying_type (mono_method_signature (cfg->method)->ret);
8105 MonoInst *ins;
8107 if (mini_type_to_stind (cfg, ret_type) == CEE_STOBJ) {
8108 MonoInst *ret_addr;
8110 if (!cfg->vret_addr) {
8111 EMIT_NEW_VARSTORE (cfg, ins, cfg->ret, ret_type, val);
8112 } else {
8113 EMIT_NEW_RETLOADA (cfg, ret_addr);
8115 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STOREV_MEMBASE, ret_addr->dreg, 0, val->dreg);
8116 ins->klass = mono_class_from_mono_type (ret_type);
8118 } else {
8119 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
8120 if (COMPILE_SOFT_FLOAT (cfg) && !ret_type->byref && ret_type->type == MONO_TYPE_R4) {
8121 MonoInst *iargs [1];
8122 MonoInst *conv;
8124 iargs [0] = val;
8125 conv = mono_emit_jit_icall (cfg, mono_fload_r4_arg, iargs);
8126 mono_arch_emit_setret (cfg, cfg->method, conv);
8127 } else {
8128 mono_arch_emit_setret (cfg, cfg->method, val);
8130 #else
8131 mono_arch_emit_setret (cfg, cfg->method, val);
8132 #endif
8137 * mono_method_to_ir:
8139 * Translate the .net IL into linear IR.
8142 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
8143 MonoInst *return_var, MonoInst **inline_args,
8144 guint inline_offset, gboolean is_virtual_call)
8146 MonoError error;
8147 MonoInst *ins, **sp, **stack_start;
8148 MonoBasicBlock *tblock = NULL, *init_localsbb = NULL;
8149 MonoSimpleBasicBlock *bb = NULL, *original_bb = NULL;
8150 MonoMethod *cmethod, *method_definition;
8151 MonoInst **arg_array;
8152 MonoMethodHeader *header;
8153 MonoImage *image;
8154 guint32 token, ins_flag;
8155 MonoClass *klass;
8156 MonoClass *constrained_class = NULL;
8157 unsigned char *ip, *end, *target, *err_pos;
8158 MonoMethodSignature *sig;
8159 MonoGenericContext *generic_context = NULL;
8160 MonoGenericContainer *generic_container = NULL;
8161 MonoType **param_types;
8162 int i, n, start_new_bblock, dreg;
8163 int num_calls = 0, inline_costs = 0;
8164 int breakpoint_id = 0;
8165 guint num_args;
8166 GSList *class_inits = NULL;
8167 gboolean dont_verify, dont_verify_stloc, readonly = FALSE;
8168 int context_used;
8169 gboolean init_locals, seq_points, skip_dead_blocks;
8170 gboolean sym_seq_points = FALSE;
8171 MonoDebugMethodInfo *minfo;
8172 MonoBitSet *seq_point_locs = NULL;
8173 MonoBitSet *seq_point_set_locs = NULL;
8175 cfg->disable_inline = is_jit_optimizer_disabled (method);
8177 /* serialization and xdomain stuff may need access to private fields and methods */
8178 dont_verify = method->klass->image->assembly->corlib_internal? TRUE: FALSE;
8179 dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE;
8180 dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH;
8181 dont_verify |= method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE; /* bug #77896 */
8182 dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP;
8183 dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP_INVOKE;
8185 /* still some type unsafety issues in marshal wrappers... (unknown is PtrToStructure) */
8186 dont_verify_stloc = method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE;
8187 dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_UNKNOWN;
8188 dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED;
8189 dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_STELEMREF;
8191 image = method->klass->image;
8192 header = mono_method_get_header_checked (method, &cfg->error);
8193 if (!header) {
8194 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
8195 goto exception_exit;
8197 generic_container = mono_method_get_generic_container (method);
8198 sig = mono_method_signature (method);
8199 num_args = sig->hasthis + sig->param_count;
8200 ip = (unsigned char*)header->code;
8201 cfg->cil_start = ip;
8202 end = ip + header->code_size;
8203 cfg->stat_cil_code_size += header->code_size;
8205 seq_points = cfg->gen_seq_points && cfg->method == method;
8207 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
8208 /* We could hit a seq point before attaching to the JIT (#8338) */
8209 seq_points = FALSE;
8212 if (cfg->gen_sdb_seq_points && cfg->method == method) {
8213 minfo = mono_debug_lookup_method (method);
8214 if (minfo) {
8215 MonoSymSeqPoint *sps;
8216 int i, n_il_offsets;
8218 mono_debug_get_seq_points (minfo, NULL, NULL, NULL, &sps, &n_il_offsets);
8219 seq_point_locs = mono_bitset_mem_new (mono_mempool_alloc0 (cfg->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
8220 seq_point_set_locs = mono_bitset_mem_new (mono_mempool_alloc0 (cfg->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
8221 sym_seq_points = TRUE;
8222 for (i = 0; i < n_il_offsets; ++i) {
8223 if (sps [i].il_offset < header->code_size)
8224 mono_bitset_set_fast (seq_point_locs, sps [i].il_offset);
8226 g_free (sps);
8227 } else if (!method->wrapper_type && !method->dynamic && mono_debug_image_has_debug_info (method->klass->image)) {
8228 /* Methods without line number info like auto-generated property accessors */
8229 seq_point_locs = mono_bitset_mem_new (mono_mempool_alloc0 (cfg->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
8230 seq_point_set_locs = mono_bitset_mem_new (mono_mempool_alloc0 (cfg->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
8231 sym_seq_points = TRUE;
8236 * Methods without init_locals set could cause asserts in various passes
8237 * (#497220). To work around this, we emit dummy initialization opcodes
8238 * (OP_DUMMY_ICONST etc.) which generate no code. These are only supported
8239 * on some platforms.
8241 if ((cfg->opt & MONO_OPT_UNSAFE) && cfg->backend->have_dummy_init)
8242 init_locals = header->init_locals;
8243 else
8244 init_locals = TRUE;
8246 method_definition = method;
8247 while (method_definition->is_inflated) {
8248 MonoMethodInflated *imethod = (MonoMethodInflated *) method_definition;
8249 method_definition = imethod->declaring;
8252 /* SkipVerification is not allowed if core-clr is enabled */
8253 if (!dont_verify && mini_assembly_can_skip_verification (cfg->domain, method)) {
8254 dont_verify = TRUE;
8255 dont_verify_stloc = TRUE;
8258 if (sig->is_inflated)
8259 generic_context = mono_method_get_context (method);
8260 else if (generic_container)
8261 generic_context = &generic_container->context;
8262 cfg->generic_context = generic_context;
8264 if (!cfg->gshared)
8265 g_assert (!sig->has_type_parameters);
8267 if (sig->generic_param_count && method->wrapper_type == MONO_WRAPPER_NONE) {
8268 g_assert (method->is_inflated);
8269 g_assert (mono_method_get_context (method)->method_inst);
8271 if (method->is_inflated && mono_method_get_context (method)->method_inst)
8272 g_assert (sig->generic_param_count);
8274 if (cfg->method == method) {
8275 cfg->real_offset = 0;
8276 } else {
8277 cfg->real_offset = inline_offset;
8280 cfg->cil_offset_to_bb = (MonoBasicBlock **)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoBasicBlock*) * header->code_size);
8281 cfg->cil_offset_to_bb_len = header->code_size;
8283 cfg->current_method = method;
8285 if (cfg->verbose_level > 2)
8286 printf ("method to IR %s\n", mono_method_full_name (method, TRUE));
8288 param_types = (MonoType **)mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
8289 if (sig->hasthis)
8290 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
8291 for (n = 0; n < sig->param_count; ++n)
8292 param_types [n + sig->hasthis] = sig->params [n];
8293 cfg->arg_types = param_types;
8295 cfg->dont_inline = g_list_prepend (cfg->dont_inline, method);
8296 if (cfg->method == method) {
8298 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
8299 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
8301 /* ENTRY BLOCK */
8302 NEW_BBLOCK (cfg, start_bblock);
8303 cfg->bb_entry = start_bblock;
8304 start_bblock->cil_code = NULL;
8305 start_bblock->cil_length = 0;
8307 /* EXIT BLOCK */
8308 NEW_BBLOCK (cfg, end_bblock);
8309 cfg->bb_exit = end_bblock;
8310 end_bblock->cil_code = NULL;
8311 end_bblock->cil_length = 0;
8312 end_bblock->flags |= BB_INDIRECT_JUMP_TARGET;
8313 g_assert (cfg->num_bblocks == 2);
8315 arg_array = cfg->args;
8317 if (header->num_clauses) {
8318 cfg->spvars = g_hash_table_new (NULL, NULL);
8319 cfg->exvars = g_hash_table_new (NULL, NULL);
8321 /* handle exception clauses */
8322 for (i = 0; i < header->num_clauses; ++i) {
8323 MonoBasicBlock *try_bb;
8324 MonoExceptionClause *clause = &header->clauses [i];
8325 GET_BBLOCK (cfg, try_bb, ip + clause->try_offset);
8327 try_bb->real_offset = clause->try_offset;
8328 try_bb->try_start = TRUE;
8329 try_bb->region = ((i + 1) << 8) | clause->flags;
8330 GET_BBLOCK (cfg, tblock, ip + clause->handler_offset);
8331 tblock->real_offset = clause->handler_offset;
8332 tblock->flags |= BB_EXCEPTION_HANDLER;
8335 * Linking the try block with the EH block hinders inlining as we won't be able to
8336 * merge the bblocks from inlining and produce an artificial hole for no good reason.
8338 if (COMPILE_LLVM (cfg))
8339 link_bblock (cfg, try_bb, tblock);
8341 if (*(ip + clause->handler_offset) == CEE_POP)
8342 tblock->flags |= BB_EXCEPTION_DEAD_OBJ;
8344 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
8345 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER ||
8346 clause->flags == MONO_EXCEPTION_CLAUSE_FAULT) {
8347 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
8348 MONO_ADD_INS (tblock, ins);
8350 if (seq_points && clause->flags != MONO_EXCEPTION_CLAUSE_FINALLY && clause->flags != MONO_EXCEPTION_CLAUSE_FILTER) {
8351 /* finally clauses already have a seq point */
8352 /* seq points for filter clauses are emitted below */
8353 NEW_SEQ_POINT (cfg, ins, clause->handler_offset, TRUE);
8354 MONO_ADD_INS (tblock, ins);
8357 /* todo: is a fault block unsafe to optimize? */
8358 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
8359 tblock->flags |= BB_EXCEPTION_UNSAFE;
8362 /*printf ("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);
8363 while (p < end) {
8364 printf ("%s", mono_disasm_code_one (NULL, method, p, &p));
8366 /* catch and filter blocks get the exception object on the stack */
8367 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
8368 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
8370 /* mostly like handle_stack_args (), but just sets the input args */
8371 /* printf ("handling clause at IL_%04x\n", clause->handler_offset); */
8372 tblock->in_scount = 1;
8373 tblock->in_stack = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
8374 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
8376 cfg->cbb = tblock;
8378 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
8379 /* The EH code passes in the exception in a register to both JITted and LLVM compiled code */
8380 if (!cfg->compile_llvm) {
8381 MONO_INST_NEW (cfg, ins, OP_GET_EX_OBJ);
8382 ins->dreg = tblock->in_stack [0]->dreg;
8383 MONO_ADD_INS (tblock, ins);
8385 #else
8386 MonoInst *dummy_use;
8389 * Add a dummy use for the exvar so its liveness info will be
8390 * correct.
8392 EMIT_NEW_DUMMY_USE (cfg, dummy_use, tblock->in_stack [0]);
8393 #endif
8395 if (seq_points && clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
8396 NEW_SEQ_POINT (cfg, ins, clause->handler_offset, TRUE);
8397 MONO_ADD_INS (tblock, ins);
8400 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
8401 GET_BBLOCK (cfg, tblock, ip + clause->data.filter_offset);
8402 tblock->flags |= BB_EXCEPTION_HANDLER;
8403 tblock->real_offset = clause->data.filter_offset;
8404 tblock->in_scount = 1;
8405 tblock->in_stack = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
8406 /* The filter block shares the exvar with the handler block */
8407 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
8408 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
8409 MONO_ADD_INS (tblock, ins);
8413 if (clause->flags != MONO_EXCEPTION_CLAUSE_FILTER &&
8414 clause->data.catch_class &&
8415 cfg->gshared &&
8416 mono_class_check_context_used (clause->data.catch_class)) {
8418 * In shared generic code with catch
8419 * clauses containing type variables
8420 * the exception handling code has to
8421 * be able to get to the rgctx.
8422 * Therefore we have to make sure that
8423 * the vtable/mrgctx argument (for
8424 * static or generic methods) or the
8425 * "this" argument (for non-static
8426 * methods) are live.
8428 if ((method->flags & METHOD_ATTRIBUTE_STATIC) ||
8429 mini_method_get_context (method)->method_inst ||
8430 method->klass->valuetype) {
8431 mono_get_vtable_var (cfg);
8432 } else {
8433 MonoInst *dummy_use;
8435 EMIT_NEW_DUMMY_USE (cfg, dummy_use, arg_array [0]);
8439 } else {
8440 arg_array = (MonoInst **) alloca (sizeof (MonoInst *) * num_args);
8441 cfg->cbb = start_bblock;
8442 cfg->args = arg_array;
8443 mono_save_args (cfg, sig, inline_args);
8446 /* FIRST CODE BLOCK */
8447 NEW_BBLOCK (cfg, tblock);
8448 tblock->cil_code = ip;
8449 cfg->cbb = tblock;
8450 cfg->ip = ip;
8452 ADD_BBLOCK (cfg, tblock);
8454 if (cfg->method == method) {
8455 breakpoint_id = mono_debugger_method_has_breakpoint (method);
8456 if (breakpoint_id) {
8457 MONO_INST_NEW (cfg, ins, OP_BREAK);
8458 MONO_ADD_INS (cfg->cbb, ins);
8462 /* we use a separate basic block for the initialization code */
8463 NEW_BBLOCK (cfg, init_localsbb);
8464 cfg->bb_init = init_localsbb;
8465 init_localsbb->real_offset = cfg->real_offset;
8466 start_bblock->next_bb = init_localsbb;
8467 init_localsbb->next_bb = cfg->cbb;
8468 link_bblock (cfg, start_bblock, init_localsbb);
8469 link_bblock (cfg, init_localsbb, cfg->cbb);
8471 cfg->cbb = init_localsbb;
8473 if (cfg->gsharedvt && cfg->method == method) {
8474 MonoGSharedVtMethodInfo *info;
8475 MonoInst *var, *locals_var;
8476 int dreg;
8478 info = (MonoGSharedVtMethodInfo *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoGSharedVtMethodInfo));
8479 info->method = cfg->method;
8480 info->count_entries = 16;
8481 info->entries = (MonoRuntimeGenericContextInfoTemplate *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoRuntimeGenericContextInfoTemplate) * info->count_entries);
8482 cfg->gsharedvt_info = info;
8484 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
8485 /* prevent it from being register allocated */
8486 //var->flags |= MONO_INST_VOLATILE;
8487 cfg->gsharedvt_info_var = var;
8489 ins = emit_get_rgctx_gsharedvt_method (cfg, mini_method_check_context_used (cfg, method), method, info);
8490 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, var->dreg, ins->dreg);
8492 /* Allocate locals */
8493 locals_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
8494 /* prevent it from being register allocated */
8495 //locals_var->flags |= MONO_INST_VOLATILE;
8496 cfg->gsharedvt_locals_var = locals_var;
8498 dreg = alloc_ireg (cfg);
8499 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, dreg, var->dreg, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, locals_size));
8501 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
8502 ins->dreg = locals_var->dreg;
8503 ins->sreg1 = dreg;
8504 MONO_ADD_INS (cfg->cbb, ins);
8505 cfg->gsharedvt_locals_var_ins = ins;
8507 cfg->flags |= MONO_CFG_HAS_ALLOCA;
8509 if (init_locals)
8510 ins->flags |= MONO_INST_INIT;
8514 if (mono_security_core_clr_enabled ()) {
8515 /* check if this is native code, e.g. an icall or a p/invoke */
8516 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
8517 MonoMethod *wrapped = mono_marshal_method_from_wrapper (method);
8518 if (wrapped) {
8519 gboolean pinvk = (wrapped->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
8520 gboolean icall = (wrapped->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL);
8522 /* if this ia a native call then it can only be JITted from platform code */
8523 if ((icall || pinvk) && method->klass && method->klass->image) {
8524 if (!mono_security_core_clr_is_platform_image (method->klass->image)) {
8525 MonoException *ex = icall ? mono_get_exception_security () :
8526 mono_get_exception_method_access ();
8527 emit_throw_exception (cfg, ex);
8534 CHECK_CFG_EXCEPTION;
8536 if (header->code_size == 0)
8537 UNVERIFIED;
8539 if (get_basic_blocks (cfg, header, cfg->real_offset, ip, end, &err_pos)) {
8540 ip = err_pos;
8541 UNVERIFIED;
8544 if (cfg->method == method)
8545 mono_debug_init_method (cfg, cfg->cbb, breakpoint_id);
8547 for (n = 0; n < header->num_locals; ++n) {
8548 if (header->locals [n]->type == MONO_TYPE_VOID && !header->locals [n]->byref)
8549 UNVERIFIED;
8551 class_inits = NULL;
8553 /* We force the vtable variable here for all shared methods
8554 for the possibility that they might show up in a stack
8555 trace where their exact instantiation is needed. */
8556 if (cfg->gshared && method == cfg->method) {
8557 if ((method->flags & METHOD_ATTRIBUTE_STATIC) ||
8558 mini_method_get_context (method)->method_inst ||
8559 method->klass->valuetype) {
8560 mono_get_vtable_var (cfg);
8561 } else {
8562 /* FIXME: Is there a better way to do this?
8563 We need the variable live for the duration
8564 of the whole method. */
8565 cfg->args [0]->flags |= MONO_INST_VOLATILE;
8569 /* add a check for this != NULL to inlined methods */
8570 if (is_virtual_call) {
8571 MonoInst *arg_ins;
8573 NEW_ARGLOAD (cfg, arg_ins, 0);
8574 MONO_ADD_INS (cfg->cbb, arg_ins);
8575 MONO_EMIT_NEW_CHECK_THIS (cfg, arg_ins->dreg);
8578 skip_dead_blocks = !dont_verify;
8579 if (skip_dead_blocks) {
8580 original_bb = bb = mono_basic_block_split (method, &cfg->error, header);
8581 CHECK_CFG_ERROR;
8582 g_assert (bb);
8585 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
8586 stack_start = sp = (MonoInst **)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
8588 ins_flag = 0;
8589 start_new_bblock = 0;
8590 while (ip < end) {
8591 if (cfg->method == method)
8592 cfg->real_offset = ip - header->code;
8593 else
8594 cfg->real_offset = inline_offset;
8595 cfg->ip = ip;
8597 context_used = 0;
8599 if (start_new_bblock) {
8600 cfg->cbb->cil_length = ip - cfg->cbb->cil_code;
8601 if (start_new_bblock == 2) {
8602 g_assert (ip == tblock->cil_code);
8603 } else {
8604 GET_BBLOCK (cfg, tblock, ip);
8606 cfg->cbb->next_bb = tblock;
8607 cfg->cbb = tblock;
8608 start_new_bblock = 0;
8609 for (i = 0; i < cfg->cbb->in_scount; ++i) {
8610 if (cfg->verbose_level > 3)
8611 printf ("loading %d from temp %d\n", i, (int)cfg->cbb->in_stack [i]->inst_c0);
8612 EMIT_NEW_TEMPLOAD (cfg, ins, cfg->cbb->in_stack [i]->inst_c0);
8613 *sp++ = ins;
8615 if (class_inits)
8616 g_slist_free (class_inits);
8617 class_inits = NULL;
8618 } else {
8619 if ((tblock = cfg->cil_offset_to_bb [ip - cfg->cil_start]) && (tblock != cfg->cbb)) {
8620 link_bblock (cfg, cfg->cbb, tblock);
8621 if (sp != stack_start) {
8622 handle_stack_args (cfg, stack_start, sp - stack_start);
8623 sp = stack_start;
8624 CHECK_UNVERIFIABLE (cfg);
8626 cfg->cbb->next_bb = tblock;
8627 cfg->cbb = tblock;
8628 for (i = 0; i < cfg->cbb->in_scount; ++i) {
8629 if (cfg->verbose_level > 3)
8630 printf ("loading %d from temp %d\n", i, (int)cfg->cbb->in_stack [i]->inst_c0);
8631 EMIT_NEW_TEMPLOAD (cfg, ins, cfg->cbb->in_stack [i]->inst_c0);
8632 *sp++ = ins;
8634 g_slist_free (class_inits);
8635 class_inits = NULL;
8639 if (skip_dead_blocks) {
8640 int ip_offset = ip - header->code;
8642 if (ip_offset == bb->end)
8643 bb = bb->next;
8645 if (bb->dead) {
8646 int op_size = mono_opcode_size (ip, end);
8647 g_assert (op_size > 0); /*The BB formation pass must catch all bad ops*/
8649 if (cfg->verbose_level > 3) printf ("SKIPPING DEAD OP at %x\n", ip_offset);
8651 if (ip_offset + op_size == bb->end) {
8652 MONO_INST_NEW (cfg, ins, OP_NOP);
8653 MONO_ADD_INS (cfg->cbb, ins);
8654 start_new_bblock = 1;
8657 ip += op_size;
8658 continue;
8662 * Sequence points are points where the debugger can place a breakpoint.
8663 * Currently, we generate these automatically at points where the IL
8664 * stack is empty.
8666 if (seq_points && ((!sym_seq_points && (sp == stack_start)) || (sym_seq_points && mono_bitset_test_fast (seq_point_locs, ip - header->code)))) {
8668 * Make methods interruptable at the beginning, and at the targets of
8669 * backward branches.
8670 * Also, do this at the start of every bblock in methods with clauses too,
8671 * to be able to handle instructions with inprecise control flow like
8672 * throw/endfinally.
8673 * Backward branches are handled at the end of method-to-ir ().
8675 gboolean intr_loc = ip == header->code || (!cfg->cbb->last_ins && cfg->header->num_clauses);
8676 gboolean sym_seq_point = sym_seq_points && mono_bitset_test_fast (seq_point_locs, ip - header->code);
8678 /* Avoid sequence points on empty IL like .volatile */
8679 // FIXME: Enable this
8680 //if (!(cfg->cbb->last_ins && cfg->cbb->last_ins->opcode == OP_SEQ_POINT)) {
8681 NEW_SEQ_POINT (cfg, ins, ip - header->code, intr_loc);
8682 if ((sp != stack_start) && !sym_seq_point)
8683 ins->flags |= MONO_INST_NONEMPTY_STACK;
8684 MONO_ADD_INS (cfg->cbb, ins);
8686 if (sym_seq_points)
8687 mono_bitset_set_fast (seq_point_set_locs, ip - header->code);
8690 cfg->cbb->real_offset = cfg->real_offset;
8692 if ((cfg->method == method) && cfg->coverage_info) {
8693 guint32 cil_offset = ip - header->code;
8694 cfg->coverage_info->data [cil_offset].cil_code = ip;
8696 /* TODO: Use an increment here */
8697 #if defined(TARGET_X86)
8698 MONO_INST_NEW (cfg, ins, OP_STORE_MEM_IMM);
8699 ins->inst_p0 = &(cfg->coverage_info->data [cil_offset].count);
8700 ins->inst_imm = 1;
8701 MONO_ADD_INS (cfg->cbb, ins);
8702 #else
8703 EMIT_NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
8704 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, ins->dreg, 0, 1);
8705 #endif
8708 if (cfg->verbose_level > 3)
8709 printf ("converting (in B%d: stack: %d) %s", cfg->cbb->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
8711 switch (*ip) {
8712 case CEE_NOP:
8713 if (seq_points && !sym_seq_points && sp != stack_start) {
8715 * The C# compiler uses these nops to notify the JIT that it should
8716 * insert seq points.
8718 NEW_SEQ_POINT (cfg, ins, ip - header->code, FALSE);
8719 MONO_ADD_INS (cfg->cbb, ins);
8721 if (cfg->keep_cil_nops)
8722 MONO_INST_NEW (cfg, ins, OP_HARD_NOP);
8723 else
8724 MONO_INST_NEW (cfg, ins, OP_NOP);
8725 ip++;
8726 MONO_ADD_INS (cfg->cbb, ins);
8727 break;
8728 case CEE_BREAK:
8729 if (should_insert_brekpoint (cfg->method)) {
8730 ins = mono_emit_jit_icall (cfg, mono_debugger_agent_user_break, NULL);
8731 } else {
8732 MONO_INST_NEW (cfg, ins, OP_NOP);
8734 ip++;
8735 MONO_ADD_INS (cfg->cbb, ins);
8736 break;
8737 case CEE_LDARG_0:
8738 case CEE_LDARG_1:
8739 case CEE_LDARG_2:
8740 case CEE_LDARG_3:
8741 CHECK_STACK_OVF (1);
8742 n = (*ip)-CEE_LDARG_0;
8743 CHECK_ARG (n);
8744 EMIT_NEW_ARGLOAD (cfg, ins, n);
8745 ip++;
8746 *sp++ = ins;
8747 break;
8748 case CEE_LDLOC_0:
8749 case CEE_LDLOC_1:
8750 case CEE_LDLOC_2:
8751 case CEE_LDLOC_3:
8752 CHECK_STACK_OVF (1);
8753 n = (*ip)-CEE_LDLOC_0;
8754 CHECK_LOCAL (n);
8755 EMIT_NEW_LOCLOAD (cfg, ins, n);
8756 ip++;
8757 *sp++ = ins;
8758 break;
8759 case CEE_STLOC_0:
8760 case CEE_STLOC_1:
8761 case CEE_STLOC_2:
8762 case CEE_STLOC_3: {
8763 CHECK_STACK (1);
8764 n = (*ip)-CEE_STLOC_0;
8765 CHECK_LOCAL (n);
8766 --sp;
8767 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
8768 UNVERIFIED;
8769 emit_stloc_ir (cfg, sp, header, n);
8770 ++ip;
8771 inline_costs += 1;
8772 break;
8774 case CEE_LDARG_S:
8775 CHECK_OPSIZE (2);
8776 CHECK_STACK_OVF (1);
8777 n = ip [1];
8778 CHECK_ARG (n);
8779 EMIT_NEW_ARGLOAD (cfg, ins, n);
8780 *sp++ = ins;
8781 ip += 2;
8782 break;
8783 case CEE_LDARGA_S:
8784 CHECK_OPSIZE (2);
8785 CHECK_STACK_OVF (1);
8786 n = ip [1];
8787 CHECK_ARG (n);
8788 NEW_ARGLOADA (cfg, ins, n);
8789 MONO_ADD_INS (cfg->cbb, ins);
8790 *sp++ = ins;
8791 ip += 2;
8792 break;
8793 case CEE_STARG_S:
8794 CHECK_OPSIZE (2);
8795 CHECK_STACK (1);
8796 --sp;
8797 n = ip [1];
8798 CHECK_ARG (n);
8799 if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [ip [1]], *sp))
8800 UNVERIFIED;
8801 EMIT_NEW_ARGSTORE (cfg, ins, n, *sp);
8802 ip += 2;
8803 break;
8804 case CEE_LDLOC_S:
8805 CHECK_OPSIZE (2);
8806 CHECK_STACK_OVF (1);
8807 n = ip [1];
8808 CHECK_LOCAL (n);
8809 EMIT_NEW_LOCLOAD (cfg, ins, n);
8810 *sp++ = ins;
8811 ip += 2;
8812 break;
8813 case CEE_LDLOCA_S: {
8814 unsigned char *tmp_ip;
8815 CHECK_OPSIZE (2);
8816 CHECK_STACK_OVF (1);
8817 CHECK_LOCAL (ip [1]);
8819 if ((tmp_ip = emit_optimized_ldloca_ir (cfg, ip, end, 1))) {
8820 ip = tmp_ip;
8821 inline_costs += 1;
8822 break;
8825 EMIT_NEW_LOCLOADA (cfg, ins, ip [1]);
8826 *sp++ = ins;
8827 ip += 2;
8828 break;
8830 case CEE_STLOC_S:
8831 CHECK_OPSIZE (2);
8832 CHECK_STACK (1);
8833 --sp;
8834 CHECK_LOCAL (ip [1]);
8835 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [ip [1]], *sp))
8836 UNVERIFIED;
8837 emit_stloc_ir (cfg, sp, header, ip [1]);
8838 ip += 2;
8839 inline_costs += 1;
8840 break;
8841 case CEE_LDNULL:
8842 CHECK_STACK_OVF (1);
8843 EMIT_NEW_PCONST (cfg, ins, NULL);
8844 ins->type = STACK_OBJ;
8845 ++ip;
8846 *sp++ = ins;
8847 break;
8848 case CEE_LDC_I4_M1:
8849 CHECK_STACK_OVF (1);
8850 EMIT_NEW_ICONST (cfg, ins, -1);
8851 ++ip;
8852 *sp++ = ins;
8853 break;
8854 case CEE_LDC_I4_0:
8855 case CEE_LDC_I4_1:
8856 case CEE_LDC_I4_2:
8857 case CEE_LDC_I4_3:
8858 case CEE_LDC_I4_4:
8859 case CEE_LDC_I4_5:
8860 case CEE_LDC_I4_6:
8861 case CEE_LDC_I4_7:
8862 case CEE_LDC_I4_8:
8863 CHECK_STACK_OVF (1);
8864 EMIT_NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
8865 ++ip;
8866 *sp++ = ins;
8867 break;
8868 case CEE_LDC_I4_S:
8869 CHECK_OPSIZE (2);
8870 CHECK_STACK_OVF (1);
8871 ++ip;
8872 EMIT_NEW_ICONST (cfg, ins, *((signed char*)ip));
8873 ++ip;
8874 *sp++ = ins;
8875 break;
8876 case CEE_LDC_I4:
8877 CHECK_OPSIZE (5);
8878 CHECK_STACK_OVF (1);
8879 EMIT_NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
8880 ip += 5;
8881 *sp++ = ins;
8882 break;
8883 case CEE_LDC_I8:
8884 CHECK_OPSIZE (9);
8885 CHECK_STACK_OVF (1);
8886 MONO_INST_NEW (cfg, ins, OP_I8CONST);
8887 ins->type = STACK_I8;
8888 ins->dreg = alloc_dreg (cfg, STACK_I8);
8889 ++ip;
8890 ins->inst_l = (gint64)read64 (ip);
8891 MONO_ADD_INS (cfg->cbb, ins);
8892 ip += 8;
8893 *sp++ = ins;
8894 break;
8895 case CEE_LDC_R4: {
8896 float *f;
8897 gboolean use_aotconst = FALSE;
8899 #ifdef TARGET_POWERPC
8900 /* FIXME: Clean this up */
8901 if (cfg->compile_aot)
8902 use_aotconst = TRUE;
8903 #endif
8905 /* FIXME: we should really allocate this only late in the compilation process */
8906 f = (float *)mono_domain_alloc (cfg->domain, sizeof (float));
8907 CHECK_OPSIZE (5);
8908 CHECK_STACK_OVF (1);
8910 if (use_aotconst) {
8911 MonoInst *cons;
8912 int dreg;
8914 EMIT_NEW_AOTCONST (cfg, cons, MONO_PATCH_INFO_R4, f);
8916 dreg = alloc_freg (cfg);
8917 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADR4_MEMBASE, dreg, cons->dreg, 0);
8918 ins->type = cfg->r4_stack_type;
8919 } else {
8920 MONO_INST_NEW (cfg, ins, OP_R4CONST);
8921 ins->type = cfg->r4_stack_type;
8922 ins->dreg = alloc_dreg (cfg, STACK_R8);
8923 ins->inst_p0 = f;
8924 MONO_ADD_INS (cfg->cbb, ins);
8926 ++ip;
8927 readr4 (ip, f);
8928 ip += 4;
8929 *sp++ = ins;
8930 break;
8932 case CEE_LDC_R8: {
8933 double *d;
8934 gboolean use_aotconst = FALSE;
8936 #ifdef TARGET_POWERPC
8937 /* FIXME: Clean this up */
8938 if (cfg->compile_aot)
8939 use_aotconst = TRUE;
8940 #endif
8942 /* FIXME: we should really allocate this only late in the compilation process */
8943 d = (double *)mono_domain_alloc (cfg->domain, sizeof (double));
8944 CHECK_OPSIZE (9);
8945 CHECK_STACK_OVF (1);
8947 if (use_aotconst) {
8948 MonoInst *cons;
8949 int dreg;
8951 EMIT_NEW_AOTCONST (cfg, cons, MONO_PATCH_INFO_R8, d);
8953 dreg = alloc_freg (cfg);
8954 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADR8_MEMBASE, dreg, cons->dreg, 0);
8955 ins->type = STACK_R8;
8956 } else {
8957 MONO_INST_NEW (cfg, ins, OP_R8CONST);
8958 ins->type = STACK_R8;
8959 ins->dreg = alloc_dreg (cfg, STACK_R8);
8960 ins->inst_p0 = d;
8961 MONO_ADD_INS (cfg->cbb, ins);
8963 ++ip;
8964 readr8 (ip, d);
8965 ip += 8;
8966 *sp++ = ins;
8967 break;
8969 case CEE_DUP: {
8970 MonoInst *temp, *store;
8971 CHECK_STACK (1);
8972 CHECK_STACK_OVF (1);
8973 sp--;
8974 ins = *sp;
8976 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
8977 EMIT_NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
8979 EMIT_NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
8980 *sp++ = ins;
8982 EMIT_NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
8983 *sp++ = ins;
8985 ++ip;
8986 inline_costs += 2;
8987 break;
8989 case CEE_POP:
8990 CHECK_STACK (1);
8991 ip++;
8992 --sp;
8994 #ifdef TARGET_X86
8995 if (sp [0]->type == STACK_R8)
8996 /* we need to pop the value from the x86 FP stack */
8997 MONO_EMIT_NEW_UNALU (cfg, OP_X86_FPOP, -1, sp [0]->dreg);
8998 #endif
8999 break;
9000 case CEE_JMP: {
9001 MonoCallInst *call;
9002 MonoMethodSignature *fsig;
9003 int i, n;
9005 INLINE_FAILURE ("jmp");
9006 GSHAREDVT_FAILURE (*ip);
9008 CHECK_OPSIZE (5);
9009 if (stack_start != sp)
9010 UNVERIFIED;
9011 token = read32 (ip + 1);
9012 /* FIXME: check the signature matches */
9013 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
9014 CHECK_CFG_ERROR;
9016 if (cfg->gshared && mono_method_check_context_used (cmethod))
9017 GENERIC_SHARING_FAILURE (CEE_JMP);
9019 emit_instrumentation_call (cfg, mono_profiler_method_leave);
9021 fsig = mono_method_signature (cmethod);
9022 n = fsig->param_count + fsig->hasthis;
9023 if (cfg->llvm_only) {
9024 MonoInst **args;
9026 args = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * n);
9027 for (i = 0; i < n; ++i)
9028 EMIT_NEW_ARGLOAD (cfg, args [i], i);
9029 ins = mono_emit_method_call_full (cfg, cmethod, fsig, TRUE, args, NULL, NULL, NULL);
9031 * The code in mono-basic-block.c treats the rest of the code as dead, but we
9032 * have to emit a normal return since llvm expects it.
9034 if (cfg->ret)
9035 emit_setret (cfg, ins);
9036 MONO_INST_NEW (cfg, ins, OP_BR);
9037 ins->inst_target_bb = end_bblock;
9038 MONO_ADD_INS (cfg->cbb, ins);
9039 link_bblock (cfg, cfg->cbb, end_bblock);
9040 ip += 5;
9041 break;
9042 } else if (cfg->backend->have_op_tail_call) {
9043 /* Handle tail calls similarly to calls */
9044 DISABLE_AOT (cfg);
9046 MONO_INST_NEW_CALL (cfg, call, OP_TAILCALL);
9047 call->method = cmethod;
9048 call->tail_call = TRUE;
9049 call->signature = mono_method_signature (cmethod);
9050 call->args = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * n);
9051 call->inst.inst_p0 = cmethod;
9052 for (i = 0; i < n; ++i)
9053 EMIT_NEW_ARGLOAD (cfg, call->args [i], i);
9055 mono_arch_emit_call (cfg, call);
9056 cfg->param_area = MAX(cfg->param_area, call->stack_usage);
9057 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
9058 } else {
9059 for (i = 0; i < num_args; ++i)
9060 /* Prevent arguments from being optimized away */
9061 arg_array [i]->flags |= MONO_INST_VOLATILE;
9063 MONO_INST_NEW_CALL (cfg, call, OP_JMP);
9064 ins = (MonoInst*)call;
9065 ins->inst_p0 = cmethod;
9066 MONO_ADD_INS (cfg->cbb, ins);
9069 ip += 5;
9070 start_new_bblock = 1;
9071 break;
9073 case CEE_CALLI: {
9074 MonoInst *addr;
9075 MonoMethodSignature *fsig;
9077 CHECK_OPSIZE (5);
9078 token = read32 (ip + 1);
9080 ins = NULL;
9082 //GSHAREDVT_FAILURE (*ip);
9083 cmethod = NULL;
9084 CHECK_STACK (1);
9085 --sp;
9086 addr = *sp;
9087 fsig = mini_get_signature (method, token, generic_context, &cfg->error);
9088 CHECK_CFG_ERROR;
9090 if (method->dynamic && fsig->pinvoke) {
9091 MonoInst *args [3];
9094 * This is a call through a function pointer using a pinvoke
9095 * signature. Have to create a wrapper and call that instead.
9096 * FIXME: This is very slow, need to create a wrapper at JIT time
9097 * instead based on the signature.
9099 EMIT_NEW_IMAGECONST (cfg, args [0], method->klass->image);
9100 EMIT_NEW_PCONST (cfg, args [1], fsig);
9101 args [2] = addr;
9102 addr = mono_emit_jit_icall (cfg, mono_get_native_calli_wrapper, args);
9105 n = fsig->param_count + fsig->hasthis;
9107 CHECK_STACK (n);
9109 //g_assert (!virtual_ || fsig->hasthis);
9111 sp -= n;
9113 inline_costs += 10 * num_calls++;
9116 * Making generic calls out of gsharedvt methods.
9117 * This needs to be used for all generic calls, not just ones with a gsharedvt signature, to avoid
9118 * patching gshared method addresses into a gsharedvt method.
9120 if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) {
9122 * We pass the address to the gsharedvt trampoline in the rgctx reg
9124 MonoInst *callee = addr;
9126 if (method->wrapper_type != MONO_WRAPPER_DELEGATE_INVOKE)
9127 /* Not tested */
9128 GSHAREDVT_FAILURE (*ip);
9130 if (cfg->llvm_only)
9131 // FIXME:
9132 GSHAREDVT_FAILURE (*ip);
9134 addr = emit_get_rgctx_sig (cfg, context_used,
9135 fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
9136 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, callee);
9137 goto calli_end;
9140 /* Prevent inlining of methods with indirect calls */
9141 INLINE_FAILURE ("indirect call");
9143 if (addr->opcode == OP_PCONST || addr->opcode == OP_AOTCONST || addr->opcode == OP_GOT_ENTRY) {
9144 MonoJumpInfoType info_type;
9145 gpointer info_data;
9148 * Instead of emitting an indirect call, emit a direct call
9149 * with the contents of the aotconst as the patch info.
9151 if (addr->opcode == OP_PCONST || addr->opcode == OP_AOTCONST) {
9152 info_type = (MonoJumpInfoType)addr->inst_c1;
9153 info_data = addr->inst_p0;
9154 } else {
9155 info_type = (MonoJumpInfoType)addr->inst_right->inst_c1;
9156 info_data = addr->inst_right->inst_left;
9159 if (info_type == MONO_PATCH_INFO_ICALL_ADDR) {
9160 ins = (MonoInst*)mono_emit_abs_call (cfg, MONO_PATCH_INFO_ICALL_ADDR_CALL, info_data, fsig, sp);
9161 NULLIFY_INS (addr);
9162 goto calli_end;
9163 } else if (info_type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
9164 ins = (MonoInst*)mono_emit_abs_call (cfg, info_type, info_data, fsig, sp);
9165 NULLIFY_INS (addr);
9166 goto calli_end;
9169 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
9171 calli_end:
9173 /* End of call, INS should contain the result of the call, if any */
9175 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9176 g_assert (ins);
9177 *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
9180 CHECK_CFG_EXCEPTION;
9182 ip += 5;
9183 ins_flag = 0;
9184 constrained_class = NULL;
9185 break;
9187 case CEE_CALL:
9188 case CEE_CALLVIRT: {
9189 MonoInst *addr = NULL;
9190 MonoMethodSignature *fsig = NULL;
9191 int array_rank = 0;
9192 int virtual_ = *ip == CEE_CALLVIRT;
9193 gboolean pass_imt_from_rgctx = FALSE;
9194 MonoInst *imt_arg = NULL;
9195 MonoInst *keep_this_alive = NULL;
9196 gboolean pass_vtable = FALSE;
9197 gboolean pass_mrgctx = FALSE;
9198 MonoInst *vtable_arg = NULL;
9199 gboolean check_this = FALSE;
9200 gboolean supported_tail_call = FALSE;
9201 gboolean tail_call = FALSE;
9202 gboolean need_seq_point = FALSE;
9203 guint32 call_opcode = *ip;
9204 gboolean emit_widen = TRUE;
9205 gboolean push_res = TRUE;
9206 gboolean skip_ret = FALSE;
9207 gboolean delegate_invoke = FALSE;
9208 gboolean direct_icall = FALSE;
9209 gboolean constrained_partial_call = FALSE;
9210 MonoMethod *cil_method;
9212 CHECK_OPSIZE (5);
9213 token = read32 (ip + 1);
9215 ins = NULL;
9217 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
9218 CHECK_CFG_ERROR;
9220 cil_method = cmethod;
9222 if (constrained_class) {
9223 if ((constrained_class->byval_arg.type == MONO_TYPE_VAR || constrained_class->byval_arg.type == MONO_TYPE_MVAR) && cfg->gshared) {
9224 if (!mini_is_gsharedvt_klass (constrained_class)) {
9225 g_assert (!cmethod->klass->valuetype);
9226 if (!mini_type_is_reference (&constrained_class->byval_arg))
9227 constrained_partial_call = TRUE;
9231 if (method->wrapper_type != MONO_WRAPPER_NONE) {
9232 if (cfg->verbose_level > 2)
9233 printf ("DM Constrained call to %s\n", mono_type_get_full_name (constrained_class));
9234 if (!((constrained_class->byval_arg.type == MONO_TYPE_VAR ||
9235 constrained_class->byval_arg.type == MONO_TYPE_MVAR) &&
9236 cfg->gshared)) {
9237 cmethod = mono_get_method_constrained_with_method (image, cil_method, constrained_class, generic_context, &cfg->error);
9238 CHECK_CFG_ERROR;
9240 } else {
9241 if (cfg->verbose_level > 2)
9242 printf ("Constrained call to %s\n", mono_type_get_full_name (constrained_class));
9244 if ((constrained_class->byval_arg.type == MONO_TYPE_VAR || constrained_class->byval_arg.type == MONO_TYPE_MVAR) && cfg->gshared) {
9246 * This is needed since get_method_constrained can't find
9247 * the method in klass representing a type var.
9248 * The type var is guaranteed to be a reference type in this
9249 * case.
9251 if (!mini_is_gsharedvt_klass (constrained_class))
9252 g_assert (!cmethod->klass->valuetype);
9253 } else {
9254 cmethod = mono_get_method_constrained_checked (image, token, constrained_class, generic_context, &cil_method, &cfg->error);
9255 CHECK_CFG_ERROR;
9260 if (!dont_verify && !cfg->skip_visibility) {
9261 MonoMethod *target_method = cil_method;
9262 if (method->is_inflated) {
9263 target_method = mini_get_method_allow_open (method, token, NULL, &(mono_method_get_generic_container (method_definition)->context), &cfg->error);
9264 CHECK_CFG_ERROR;
9266 if (!mono_method_can_access_method (method_definition, target_method) &&
9267 !mono_method_can_access_method (method, cil_method))
9268 METHOD_ACCESS_FAILURE (method, cil_method);
9271 if (mono_security_core_clr_enabled ())
9272 ensure_method_is_allowed_to_call_method (cfg, method, cil_method);
9274 if (!virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_ABSTRACT))
9275 /* MS.NET seems to silently convert this to a callvirt */
9276 virtual_ = 1;
9280 * MS.NET accepts non virtual calls to virtual final methods of transparent proxy classes and
9281 * converts to a callvirt.
9283 * tests/bug-515884.il is an example of this behavior
9285 const int test_flags = METHOD_ATTRIBUTE_VIRTUAL | METHOD_ATTRIBUTE_FINAL | METHOD_ATTRIBUTE_STATIC;
9286 const int expected_flags = METHOD_ATTRIBUTE_VIRTUAL | METHOD_ATTRIBUTE_FINAL;
9287 if (!virtual_ && mono_class_is_marshalbyref (cmethod->klass) && (cmethod->flags & test_flags) == expected_flags && cfg->method->wrapper_type == MONO_WRAPPER_NONE)
9288 virtual_ = 1;
9291 if (!cmethod->klass->inited)
9292 if (!mono_class_init (cmethod->klass))
9293 TYPE_LOAD_ERROR (cmethod->klass);
9295 fsig = mono_method_signature (cmethod);
9296 if (!fsig)
9297 LOAD_ERROR;
9298 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
9299 mini_class_is_system_array (cmethod->klass)) {
9300 array_rank = cmethod->klass->rank;
9301 } else if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && icall_is_direct_callable (cfg, cmethod)) {
9302 direct_icall = TRUE;
9303 } else if (fsig->pinvoke) {
9304 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod, TRUE, cfg->compile_aot);
9305 fsig = mono_method_signature (wrapper);
9306 } else if (constrained_class) {
9307 } else {
9308 fsig = mono_method_get_signature_checked (cmethod, image, token, generic_context, &cfg->error);
9309 CHECK_CFG_ERROR;
9312 if (cfg->llvm_only && !cfg->method->wrapper_type && (!cmethod || cmethod->is_inflated))
9313 cfg->signatures = g_slist_prepend_mempool (cfg->mempool, cfg->signatures, fsig);
9315 /* See code below */
9316 if (cmethod->klass == mono_defaults.monitor_class && !strcmp (cmethod->name, "Enter") && mono_method_signature (cmethod)->param_count == 1) {
9317 MonoBasicBlock *tbb;
9319 GET_BBLOCK (cfg, tbb, ip + 5);
9320 if (tbb->try_start && MONO_REGION_FLAGS(tbb->region) == MONO_EXCEPTION_CLAUSE_FINALLY) {
9322 * We want to extend the try block to cover the call, but we can't do it if the
9323 * call is made directly since its followed by an exception check.
9325 direct_icall = FALSE;
9329 mono_save_token_info (cfg, image, token, cil_method);
9331 if (!(seq_point_locs && mono_bitset_test_fast (seq_point_locs, ip + 5 - header->code)))
9332 need_seq_point = TRUE;
9334 /* Don't support calls made using type arguments for now */
9336 if (cfg->gsharedvt) {
9337 if (mini_is_gsharedvt_signature (fsig))
9338 GSHAREDVT_FAILURE (*ip);
9342 if (cmethod->string_ctor && method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE)
9343 g_assert_not_reached ();
9345 n = fsig->param_count + fsig->hasthis;
9347 if (!cfg->gshared && cmethod->klass->generic_container)
9348 UNVERIFIED;
9350 if (!cfg->gshared)
9351 g_assert (!mono_method_check_context_used (cmethod));
9353 CHECK_STACK (n);
9355 //g_assert (!virtual_ || fsig->hasthis);
9357 sp -= n;
9360 * We have the `constrained.' prefix opcode.
9362 if (constrained_class) {
9363 if (mini_is_gsharedvt_klass (constrained_class)) {
9364 if ((cmethod->klass != mono_defaults.object_class) && constrained_class->valuetype && cmethod->klass->valuetype) {
9365 /* The 'Own method' case below */
9366 } else if (cmethod->klass->image != mono_defaults.corlib && !(cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE) && !cmethod->klass->valuetype) {
9367 /* 'The type parameter is instantiated as a reference type' case below. */
9368 } else {
9369 ins = handle_constrained_gsharedvt_call (cfg, cmethod, fsig, sp, constrained_class, &emit_widen);
9370 CHECK_CFG_EXCEPTION;
9371 g_assert (ins);
9372 goto call_end;
9376 if (constrained_partial_call) {
9377 gboolean need_box = TRUE;
9380 * The receiver is a valuetype, but the exact type is not known at compile time. This means the
9381 * called method is not known at compile time either. The called method could end up being
9382 * one of the methods on the parent classes (object/valuetype/enum), in which case we need
9383 * to box the receiver.
9384 * A simple solution would be to box always and make a normal virtual call, but that would
9385 * be bad performance wise.
9387 if (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE && cmethod->klass->generic_class) {
9389 * The parent classes implement no generic interfaces, so the called method will be a vtype method, so no boxing neccessary.
9391 need_box = FALSE;
9394 if (!(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) && (cmethod->klass == mono_defaults.object_class || cmethod->klass == mono_defaults.enum_class->parent || cmethod->klass == mono_defaults.enum_class)) {
9395 /* The called method is not virtual, i.e. Object:GetType (), the receiver is a vtype, has to box */
9396 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
9397 ins->klass = constrained_class;
9398 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
9399 CHECK_CFG_EXCEPTION;
9400 } else if (need_box) {
9401 MonoInst *box_type;
9402 MonoBasicBlock *is_ref_bb, *end_bb;
9403 MonoInst *nonbox_call;
9406 * Determine at runtime whenever the called method is defined on object/valuetype/enum, and emit a boxing call
9407 * if needed.
9408 * FIXME: It is possible to inline the called method in a lot of cases, i.e. for T_INT,
9409 * the no-box case goes to a method in Int32, while the box case goes to a method in Enum.
9411 addr = emit_get_rgctx_virt_method (cfg, mono_class_check_context_used (constrained_class), constrained_class, cmethod, MONO_RGCTX_INFO_VIRT_METHOD_CODE);
9413 NEW_BBLOCK (cfg, is_ref_bb);
9414 NEW_BBLOCK (cfg, end_bb);
9416 box_type = emit_get_rgctx_virt_method (cfg, mono_class_check_context_used (constrained_class), constrained_class, cmethod, MONO_RGCTX_INFO_VIRT_METHOD_BOX_TYPE);
9417 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, box_type->dreg, MONO_GSHAREDVT_BOX_TYPE_REF);
9418 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
9420 /* Non-ref case */
9421 nonbox_call = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
9423 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
9425 /* Ref case */
9426 MONO_START_BB (cfg, is_ref_bb);
9427 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
9428 ins->klass = constrained_class;
9429 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
9430 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
9432 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
9434 MONO_START_BB (cfg, end_bb);
9435 cfg->cbb = end_bb;
9437 nonbox_call->dreg = ins->dreg;
9438 goto call_end;
9439 } else {
9440 g_assert (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE);
9441 addr = emit_get_rgctx_virt_method (cfg, mono_class_check_context_used (constrained_class), constrained_class, cmethod, MONO_RGCTX_INFO_VIRT_METHOD_CODE);
9442 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
9443 goto call_end;
9445 } else if (constrained_class->valuetype && (cmethod->klass == mono_defaults.object_class || cmethod->klass == mono_defaults.enum_class->parent || cmethod->klass == mono_defaults.enum_class)) {
9447 * The type parameter is instantiated as a valuetype,
9448 * but that type doesn't override the method we're
9449 * calling, so we need to box `this'.
9451 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
9452 ins->klass = constrained_class;
9453 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
9454 CHECK_CFG_EXCEPTION;
9455 } else if (!constrained_class->valuetype) {
9456 int dreg = alloc_ireg_ref (cfg);
9459 * The type parameter is instantiated as a reference
9460 * type. We have a managed pointer on the stack, so
9461 * we need to dereference it here.
9463 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, sp [0]->dreg, 0);
9464 ins->type = STACK_OBJ;
9465 sp [0] = ins;
9466 } else {
9467 if (cmethod->klass->valuetype) {
9468 /* Own method */
9469 } else {
9470 /* Interface method */
9471 int ioffset, slot;
9473 mono_class_setup_vtable (constrained_class);
9474 CHECK_TYPELOAD (constrained_class);
9475 ioffset = mono_class_interface_offset (constrained_class, cmethod->klass);
9476 if (ioffset == -1)
9477 TYPE_LOAD_ERROR (constrained_class);
9478 slot = mono_method_get_vtable_slot (cmethod);
9479 if (slot == -1)
9480 TYPE_LOAD_ERROR (cmethod->klass);
9481 cmethod = constrained_class->vtable [ioffset + slot];
9483 if (cmethod->klass == mono_defaults.enum_class) {
9484 /* Enum implements some interfaces, so treat this as the first case */
9485 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
9486 ins->klass = constrained_class;
9487 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
9488 CHECK_CFG_EXCEPTION;
9491 virtual_ = 0;
9493 constrained_class = NULL;
9496 if (check_call_signature (cfg, fsig, sp))
9497 UNVERIFIED;
9499 if ((cmethod->klass->parent == mono_defaults.multicastdelegate_class) && !strcmp (cmethod->name, "Invoke"))
9500 delegate_invoke = TRUE;
9502 if ((cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_sharable_method (cfg, cmethod, fsig, sp))) {
9503 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9504 type_to_eval_stack_type ((cfg), fsig->ret, ins);
9505 emit_widen = FALSE;
9508 goto call_end;
9512 * If the callee is a shared method, then its static cctor
9513 * might not get called after the call was patched.
9515 if (cfg->gshared && cmethod->klass != method->klass && cmethod->klass->generic_class && mono_method_is_generic_sharable (cmethod, TRUE) && mono_class_needs_cctor_run (cmethod->klass, method)) {
9516 emit_class_init (cfg, cmethod->klass);
9517 CHECK_TYPELOAD (cmethod->klass);
9520 check_method_sharing (cfg, cmethod, &pass_vtable, &pass_mrgctx);
9522 if (cfg->gshared) {
9523 MonoGenericContext *cmethod_context = mono_method_get_context (cmethod);
9525 context_used = mini_method_check_context_used (cfg, cmethod);
9527 if (context_used && (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
9528 /* Generic method interface
9529 calls are resolved via a
9530 helper function and don't
9531 need an imt. */
9532 if (!cmethod_context || !cmethod_context->method_inst)
9533 pass_imt_from_rgctx = TRUE;
9537 * If a shared method calls another
9538 * shared method then the caller must
9539 * have a generic sharing context
9540 * because the magic trampoline
9541 * requires it. FIXME: We shouldn't
9542 * have to force the vtable/mrgctx
9543 * variable here. Instead there
9544 * should be a flag in the cfg to
9545 * request a generic sharing context.
9547 if (context_used &&
9548 ((method->flags & METHOD_ATTRIBUTE_STATIC) || method->klass->valuetype))
9549 mono_get_vtable_var (cfg);
9552 if (pass_vtable) {
9553 if (context_used) {
9554 vtable_arg = emit_get_rgctx_klass (cfg, context_used, cmethod->klass, MONO_RGCTX_INFO_VTABLE);
9555 } else {
9556 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
9558 CHECK_TYPELOAD (cmethod->klass);
9559 EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
9563 if (pass_mrgctx) {
9564 g_assert (!vtable_arg);
9566 if (!cfg->compile_aot) {
9568 * emit_get_rgctx_method () calls mono_class_vtable () so check
9569 * for type load errors before.
9571 mono_class_setup_vtable (cmethod->klass);
9572 CHECK_TYPELOAD (cmethod->klass);
9575 vtable_arg = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_METHOD_RGCTX);
9577 /* !marshalbyref is needed to properly handle generic methods + remoting */
9578 if ((!(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
9579 MONO_METHOD_IS_FINAL (cmethod)) &&
9580 !mono_class_is_marshalbyref (cmethod->klass)) {
9581 if (virtual_)
9582 check_this = TRUE;
9583 virtual_ = 0;
9587 if (pass_imt_from_rgctx) {
9588 g_assert (!pass_vtable);
9590 imt_arg = emit_get_rgctx_method (cfg, context_used,
9591 cmethod, MONO_RGCTX_INFO_METHOD);
9594 if (check_this)
9595 MONO_EMIT_NEW_CHECK_THIS (cfg, sp [0]->dreg);
9597 /* Calling virtual generic methods */
9598 if (virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) &&
9599 !(MONO_METHOD_IS_FINAL (cmethod) &&
9600 cmethod->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK) &&
9601 fsig->generic_param_count &&
9602 !(cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) &&
9603 !cfg->llvm_only) {
9604 MonoInst *this_temp, *this_arg_temp, *store;
9605 MonoInst *iargs [4];
9607 g_assert (fsig->is_inflated);
9609 /* Prevent inlining of methods that contain indirect calls */
9610 INLINE_FAILURE ("virtual generic call");
9612 if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig))
9613 GSHAREDVT_FAILURE (*ip);
9615 if (cfg->backend->have_generalized_imt_thunk && cfg->backend->gshared_supported && cmethod->wrapper_type == MONO_WRAPPER_NONE) {
9616 g_assert (!imt_arg);
9617 if (!context_used)
9618 g_assert (cmethod->is_inflated);
9619 imt_arg = emit_get_rgctx_method (cfg, context_used,
9620 cmethod, MONO_RGCTX_INFO_METHOD);
9621 ins = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, sp [0], imt_arg, NULL);
9622 } else {
9623 this_temp = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
9624 NEW_TEMPSTORE (cfg, store, this_temp->inst_c0, sp [0]);
9625 MONO_ADD_INS (cfg->cbb, store);
9627 /* FIXME: This should be a managed pointer */
9628 this_arg_temp = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
9630 EMIT_NEW_TEMPLOAD (cfg, iargs [0], this_temp->inst_c0);
9631 iargs [1] = emit_get_rgctx_method (cfg, context_used,
9632 cmethod, MONO_RGCTX_INFO_METHOD);
9633 EMIT_NEW_TEMPLOADA (cfg, iargs [2], this_arg_temp->inst_c0);
9634 addr = mono_emit_jit_icall (cfg,
9635 mono_helper_compile_generic_method, iargs);
9637 EMIT_NEW_TEMPLOAD (cfg, sp [0], this_arg_temp->inst_c0);
9639 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
9642 goto call_end;
9646 * Implement a workaround for the inherent races involved in locking:
9647 * Monitor.Enter ()
9648 * try {
9649 * } finally {
9650 * Monitor.Exit ()
9652 * If a thread abort happens between the call to Monitor.Enter () and the start of the
9653 * try block, the Exit () won't be executed, see:
9654 * http://www.bluebytesoftware.com/blog/2007/01/30/MonitorEnterThreadAbortsAndOrphanedLocks.aspx
9655 * To work around this, we extend such try blocks to include the last x bytes
9656 * of the Monitor.Enter () call.
9658 if (cmethod->klass == mono_defaults.monitor_class && !strcmp (cmethod->name, "Enter") && mono_method_signature (cmethod)->param_count == 1) {
9659 MonoBasicBlock *tbb;
9661 GET_BBLOCK (cfg, tbb, ip + 5);
9663 * Only extend try blocks with a finally, to avoid catching exceptions thrown
9664 * from Monitor.Enter like ArgumentNullException.
9666 if (tbb->try_start && MONO_REGION_FLAGS(tbb->region) == MONO_EXCEPTION_CLAUSE_FINALLY) {
9667 /* Mark this bblock as needing to be extended */
9668 tbb->extend_try_block = TRUE;
9672 /* Conversion to a JIT intrinsic */
9673 if ((cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_method (cfg, cmethod, fsig, sp))) {
9674 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9675 type_to_eval_stack_type ((cfg), fsig->ret, ins);
9676 emit_widen = FALSE;
9678 goto call_end;
9680 CHECK_CFG_ERROR;
9682 /* Inlining */
9683 if ((cfg->opt & MONO_OPT_INLINE) &&
9684 (!virtual_ || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || MONO_METHOD_IS_FINAL (cmethod)) &&
9685 mono_method_check_inlining (cfg, cmethod)) {
9686 int costs;
9687 gboolean always = FALSE;
9689 if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
9690 (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
9691 /* Prevent inlining of methods that call wrappers */
9692 INLINE_FAILURE ("wrapper call");
9693 cmethod = mono_marshal_get_native_wrapper (cmethod, TRUE, FALSE);
9694 always = TRUE;
9697 costs = inline_method (cfg, cmethod, fsig, sp, ip, cfg->real_offset, always);
9698 if (costs) {
9699 cfg->real_offset += 5;
9701 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9702 /* *sp is already set by inline_method */
9703 sp++;
9704 push_res = FALSE;
9707 inline_costs += costs;
9709 goto call_end;
9713 /* Tail recursion elimination */
9714 if ((cfg->opt & MONO_OPT_TAILC) && call_opcode == CEE_CALL && cmethod == method && ip [5] == CEE_RET && !vtable_arg) {
9715 gboolean has_vtargs = FALSE;
9716 int i;
9718 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
9719 INLINE_FAILURE ("tail call");
9721 /* keep it simple */
9722 for (i = fsig->param_count - 1; i >= 0; i--) {
9723 if (MONO_TYPE_ISSTRUCT (mono_method_signature (cmethod)->params [i]))
9724 has_vtargs = TRUE;
9727 if (!has_vtargs) {
9728 for (i = 0; i < n; ++i)
9729 EMIT_NEW_ARGSTORE (cfg, ins, i, sp [i]);
9730 MONO_INST_NEW (cfg, ins, OP_BR);
9731 MONO_ADD_INS (cfg->cbb, ins);
9732 tblock = start_bblock->out_bb [0];
9733 link_bblock (cfg, cfg->cbb, tblock);
9734 ins->inst_target_bb = tblock;
9735 start_new_bblock = 1;
9737 /* skip the CEE_RET, too */
9738 if (ip_in_bb (cfg, cfg->cbb, ip + 5))
9739 skip_ret = TRUE;
9740 push_res = FALSE;
9741 goto call_end;
9745 inline_costs += 10 * num_calls++;
9748 * Making generic calls out of gsharedvt methods.
9749 * This needs to be used for all generic calls, not just ones with a gsharedvt signature, to avoid
9750 * patching gshared method addresses into a gsharedvt method.
9752 if (cfg->gsharedvt && (mini_is_gsharedvt_signature (fsig) || cmethod->is_inflated || cmethod->klass->generic_class) &&
9753 !(cmethod->klass->rank && cmethod->klass->byval_arg.type != MONO_TYPE_SZARRAY) &&
9754 (!(cfg->llvm_only && virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL)))) {
9755 MonoRgctxInfoType info_type;
9757 if (virtual_) {
9758 //if (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE)
9759 //GSHAREDVT_FAILURE (*ip);
9760 // disable for possible remoting calls
9761 if (fsig->hasthis && (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class))
9762 GSHAREDVT_FAILURE (*ip);
9763 if (fsig->generic_param_count) {
9764 /* virtual generic call */
9765 g_assert (!imt_arg);
9766 /* Same as the virtual generic case above */
9767 imt_arg = emit_get_rgctx_method (cfg, context_used,
9768 cmethod, MONO_RGCTX_INFO_METHOD);
9769 /* This is not needed, as the trampoline code will pass one, and it might be passed in the same reg as the imt arg */
9770 vtable_arg = NULL;
9771 } else if ((cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE) && !imt_arg) {
9772 /* This can happen when we call a fully instantiated iface method */
9773 imt_arg = emit_get_rgctx_method (cfg, context_used,
9774 cmethod, MONO_RGCTX_INFO_METHOD);
9775 vtable_arg = NULL;
9779 if ((cmethod->klass->parent == mono_defaults.multicastdelegate_class) && (!strcmp (cmethod->name, "Invoke")))
9780 keep_this_alive = sp [0];
9782 if (virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL))
9783 info_type = MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE_VIRT;
9784 else
9785 info_type = MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE;
9786 addr = emit_get_rgctx_gsharedvt_call (cfg, context_used, fsig, cmethod, info_type);
9788 if (cfg->llvm_only) {
9789 // FIXME: Avoid initializing vtable_arg
9790 ins = emit_llvmonly_calli (cfg, fsig, sp, addr);
9791 } else {
9792 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, imt_arg, vtable_arg);
9794 goto call_end;
9797 /* Generic sharing */
9800 * Use this if the callee is gsharedvt sharable too, since
9801 * at runtime we might find an instantiation so the call cannot
9802 * be patched (the 'no_patch' code path in mini-trampolines.c).
9804 if (context_used && !imt_arg && !array_rank && !delegate_invoke &&
9805 (!mono_method_is_generic_sharable_full (cmethod, TRUE, FALSE, FALSE) ||
9806 !mono_class_generic_sharing_enabled (cmethod->klass)) &&
9807 (!virtual_ || MONO_METHOD_IS_FINAL (cmethod) ||
9808 !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL))) {
9809 INLINE_FAILURE ("gshared");
9811 g_assert (cfg->gshared && cmethod);
9812 g_assert (!addr);
9815 * We are compiling a call to a
9816 * generic method from shared code,
9817 * which means that we have to look up
9818 * the method in the rgctx and do an
9819 * indirect call.
9821 if (fsig->hasthis)
9822 MONO_EMIT_NEW_CHECK_THIS (cfg, sp [0]->dreg);
9824 if (cfg->llvm_only) {
9825 if (cfg->gsharedvt && mini_is_gsharedvt_variable_signature (fsig))
9826 addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GSHAREDVT_OUT_WRAPPER);
9827 else
9828 addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
9829 // FIXME: Avoid initializing imt_arg/vtable_arg
9830 ins = emit_llvmonly_calli (cfg, fsig, sp, addr);
9831 } else {
9832 addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
9833 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, imt_arg, vtable_arg);
9835 goto call_end;
9838 /* Direct calls to icalls */
9839 if (direct_icall) {
9840 MonoMethod *wrapper;
9841 int costs;
9843 /* Inline the wrapper */
9844 wrapper = mono_marshal_get_native_wrapper (cmethod, TRUE, cfg->compile_aot);
9846 costs = inline_method (cfg, wrapper, fsig, sp, ip, cfg->real_offset, TRUE);
9847 g_assert (costs > 0);
9848 cfg->real_offset += 5;
9850 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9851 /* *sp is already set by inline_method */
9852 sp++;
9853 push_res = FALSE;
9856 inline_costs += costs;
9858 goto call_end;
9861 /* Array methods */
9862 if (array_rank) {
9863 MonoInst *addr;
9865 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
9866 MonoInst *val = sp [fsig->param_count];
9868 if (val->type == STACK_OBJ) {
9869 MonoInst *iargs [2];
9871 iargs [0] = sp [0];
9872 iargs [1] = val;
9874 mono_emit_jit_icall (cfg, mono_helper_stelem_ref_check, iargs);
9877 addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, TRUE);
9878 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, fsig->params [fsig->param_count - 1], addr->dreg, 0, val->dreg);
9879 if (cfg->gen_write_barriers && val->type == STACK_OBJ && !(val->opcode == OP_PCONST && val->inst_c0 == 0))
9880 emit_write_barrier (cfg, addr, val);
9881 if (cfg->gen_write_barriers && mini_is_gsharedvt_klass (cmethod->klass))
9882 GSHAREDVT_FAILURE (*ip);
9883 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
9884 addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, FALSE);
9886 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, fsig->ret, addr->dreg, 0);
9887 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
9888 if (!cmethod->klass->element_class->valuetype && !readonly)
9889 mini_emit_check_array_type (cfg, sp [0], cmethod->klass);
9890 CHECK_TYPELOAD (cmethod->klass);
9892 readonly = FALSE;
9893 addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, FALSE);
9894 ins = addr;
9895 } else {
9896 g_assert_not_reached ();
9899 emit_widen = FALSE;
9900 goto call_end;
9903 ins = mini_redirect_call (cfg, cmethod, fsig, sp, virtual_ ? sp [0] : NULL);
9904 if (ins)
9905 goto call_end;
9907 /* Tail prefix / tail call optimization */
9909 /* FIXME: Enabling TAILC breaks some inlining/stack trace/etc tests */
9910 /* FIXME: runtime generic context pointer for jumps? */
9911 /* FIXME: handle this for generic sharing eventually */
9912 if ((ins_flag & MONO_INST_TAILCALL) &&
9913 !vtable_arg && !cfg->gshared && is_supported_tail_call (cfg, method, cmethod, fsig, call_opcode))
9914 supported_tail_call = TRUE;
9916 if (supported_tail_call) {
9917 MonoCallInst *call;
9919 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
9920 INLINE_FAILURE ("tail call");
9922 //printf ("HIT: %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
9924 if (cfg->backend->have_op_tail_call) {
9925 /* Handle tail calls similarly to normal calls */
9926 tail_call = TRUE;
9927 } else {
9928 emit_instrumentation_call (cfg, mono_profiler_method_leave);
9930 MONO_INST_NEW_CALL (cfg, call, OP_JMP);
9931 call->tail_call = TRUE;
9932 call->method = cmethod;
9933 call->signature = mono_method_signature (cmethod);
9936 * We implement tail calls by storing the actual arguments into the
9937 * argument variables, then emitting a CEE_JMP.
9939 for (i = 0; i < n; ++i) {
9940 /* Prevent argument from being register allocated */
9941 arg_array [i]->flags |= MONO_INST_VOLATILE;
9942 EMIT_NEW_ARGSTORE (cfg, ins, i, sp [i]);
9944 ins = (MonoInst*)call;
9945 ins->inst_p0 = cmethod;
9946 ins->inst_p1 = arg_array [0];
9947 MONO_ADD_INS (cfg->cbb, ins);
9948 link_bblock (cfg, cfg->cbb, end_bblock);
9949 start_new_bblock = 1;
9951 // FIXME: Eliminate unreachable epilogs
9954 * OP_TAILCALL has no return value, so skip the CEE_RET if it is
9955 * only reachable from this call.
9957 GET_BBLOCK (cfg, tblock, ip + 5);
9958 if (tblock == cfg->cbb || tblock->in_count == 0)
9959 skip_ret = TRUE;
9960 push_res = FALSE;
9962 goto call_end;
9967 * Synchronized wrappers.
9968 * Its hard to determine where to replace a method with its synchronized
9969 * wrapper without causing an infinite recursion. The current solution is
9970 * to add the synchronized wrapper in the trampolines, and to
9971 * change the called method to a dummy wrapper, and resolve that wrapper
9972 * to the real method in mono_jit_compile_method ().
9974 if (cfg->method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
9975 MonoMethod *orig = mono_marshal_method_from_wrapper (cfg->method);
9976 if (cmethod == orig || (cmethod->is_inflated && mono_method_get_declaring_generic_method (cmethod) == orig))
9977 cmethod = mono_marshal_get_synchronized_inner_wrapper (cmethod);
9981 * Virtual calls in llvm-only mode.
9983 if (cfg->llvm_only && virtual_ && cmethod && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
9984 ins = emit_llvmonly_virtual_call (cfg, cmethod, fsig, context_used, sp);
9985 goto call_end;
9988 /* Common call */
9989 INLINE_FAILURE ("call");
9990 ins = mono_emit_method_call_full (cfg, cmethod, fsig, tail_call, sp, virtual_ ? sp [0] : NULL,
9991 imt_arg, vtable_arg);
9993 if (tail_call && !cfg->llvm_only) {
9994 link_bblock (cfg, cfg->cbb, end_bblock);
9995 start_new_bblock = 1;
9997 // FIXME: Eliminate unreachable epilogs
10000 * OP_TAILCALL has no return value, so skip the CEE_RET if it is
10001 * only reachable from this call.
10003 GET_BBLOCK (cfg, tblock, ip + 5);
10004 if (tblock == cfg->cbb || tblock->in_count == 0)
10005 skip_ret = TRUE;
10006 push_res = FALSE;
10009 call_end:
10011 /* End of call, INS should contain the result of the call, if any */
10013 if (push_res && !MONO_TYPE_IS_VOID (fsig->ret)) {
10014 g_assert (ins);
10015 if (emit_widen)
10016 *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
10017 else
10018 *sp++ = ins;
10021 if (keep_this_alive) {
10022 MonoInst *dummy_use;
10024 /* See mono_emit_method_call_full () */
10025 EMIT_NEW_DUMMY_USE (cfg, dummy_use, keep_this_alive);
10028 CHECK_CFG_EXCEPTION;
10030 ip += 5;
10031 if (skip_ret) {
10032 g_assert (*ip == CEE_RET);
10033 ip += 1;
10035 ins_flag = 0;
10036 constrained_class = NULL;
10037 if (need_seq_point)
10038 emit_seq_point (cfg, method, ip, FALSE, TRUE);
10039 break;
10041 case CEE_RET:
10042 if (cfg->method != method) {
10043 /* return from inlined method */
10045 * If in_count == 0, that means the ret is unreachable due to
10046 * being preceeded by a throw. In that case, inline_method () will
10047 * handle setting the return value
10048 * (test case: test_0_inline_throw ()).
10050 if (return_var && cfg->cbb->in_count) {
10051 MonoType *ret_type = mono_method_signature (method)->ret;
10053 MonoInst *store;
10054 CHECK_STACK (1);
10055 --sp;
10057 if ((method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_NONE) && target_type_is_incompatible (cfg, ret_type, *sp))
10058 UNVERIFIED;
10060 //g_assert (returnvar != -1);
10061 EMIT_NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
10062 cfg->ret_var_set = TRUE;
10064 } else {
10065 emit_instrumentation_call (cfg, mono_profiler_method_leave);
10067 if (cfg->lmf_var && cfg->cbb->in_count && !cfg->llvm_only)
10068 emit_pop_lmf (cfg);
10070 if (cfg->ret) {
10071 MonoType *ret_type = mini_get_underlying_type (mono_method_signature (method)->ret);
10073 if (seq_points && !sym_seq_points) {
10075 * Place a seq point here too even through the IL stack is not
10076 * empty, so a step over on
10077 * call <FOO>
10078 * ret
10079 * will work correctly.
10081 NEW_SEQ_POINT (cfg, ins, ip - header->code, TRUE);
10082 MONO_ADD_INS (cfg->cbb, ins);
10085 g_assert (!return_var);
10086 CHECK_STACK (1);
10087 --sp;
10089 if ((method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_NONE) && target_type_is_incompatible (cfg, ret_type, *sp))
10090 UNVERIFIED;
10092 emit_setret (cfg, *sp);
10095 if (sp != stack_start)
10096 UNVERIFIED;
10097 MONO_INST_NEW (cfg, ins, OP_BR);
10098 ip++;
10099 ins->inst_target_bb = end_bblock;
10100 MONO_ADD_INS (cfg->cbb, ins);
10101 link_bblock (cfg, cfg->cbb, end_bblock);
10102 start_new_bblock = 1;
10103 break;
10104 case CEE_BR_S:
10105 CHECK_OPSIZE (2);
10106 MONO_INST_NEW (cfg, ins, OP_BR);
10107 ip++;
10108 target = ip + 1 + (signed char)(*ip);
10109 ++ip;
10110 GET_BBLOCK (cfg, tblock, target);
10111 link_bblock (cfg, cfg->cbb, tblock);
10112 ins->inst_target_bb = tblock;
10113 if (sp != stack_start) {
10114 handle_stack_args (cfg, stack_start, sp - stack_start);
10115 sp = stack_start;
10116 CHECK_UNVERIFIABLE (cfg);
10118 MONO_ADD_INS (cfg->cbb, ins);
10119 start_new_bblock = 1;
10120 inline_costs += BRANCH_COST;
10121 break;
10122 case CEE_BEQ_S:
10123 case CEE_BGE_S:
10124 case CEE_BGT_S:
10125 case CEE_BLE_S:
10126 case CEE_BLT_S:
10127 case CEE_BNE_UN_S:
10128 case CEE_BGE_UN_S:
10129 case CEE_BGT_UN_S:
10130 case CEE_BLE_UN_S:
10131 case CEE_BLT_UN_S:
10132 CHECK_OPSIZE (2);
10133 CHECK_STACK (2);
10134 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
10135 ip++;
10136 target = ip + 1 + *(signed char*)ip;
10137 ip++;
10139 ADD_BINCOND (NULL);
10141 sp = stack_start;
10142 inline_costs += BRANCH_COST;
10143 break;
10144 case CEE_BR:
10145 CHECK_OPSIZE (5);
10146 MONO_INST_NEW (cfg, ins, OP_BR);
10147 ip++;
10149 target = ip + 4 + (gint32)read32(ip);
10150 ip += 4;
10151 GET_BBLOCK (cfg, tblock, target);
10152 link_bblock (cfg, cfg->cbb, tblock);
10153 ins->inst_target_bb = tblock;
10154 if (sp != stack_start) {
10155 handle_stack_args (cfg, stack_start, sp - stack_start);
10156 sp = stack_start;
10157 CHECK_UNVERIFIABLE (cfg);
10160 MONO_ADD_INS (cfg->cbb, ins);
10162 start_new_bblock = 1;
10163 inline_costs += BRANCH_COST;
10164 break;
10165 case CEE_BRFALSE_S:
10166 case CEE_BRTRUE_S:
10167 case CEE_BRFALSE:
10168 case CEE_BRTRUE: {
10169 MonoInst *cmp;
10170 gboolean is_short = ((*ip) == CEE_BRFALSE_S) || ((*ip) == CEE_BRTRUE_S);
10171 gboolean is_true = ((*ip) == CEE_BRTRUE_S) || ((*ip) == CEE_BRTRUE);
10172 guint32 opsize = is_short ? 1 : 4;
10174 CHECK_OPSIZE (opsize);
10175 CHECK_STACK (1);
10176 if (sp [-1]->type == STACK_VTYPE || sp [-1]->type == STACK_R8)
10177 UNVERIFIED;
10178 ip ++;
10179 target = ip + opsize + (is_short ? *(signed char*)ip : (gint32)read32(ip));
10180 ip += opsize;
10182 sp--;
10184 GET_BBLOCK (cfg, tblock, target);
10185 link_bblock (cfg, cfg->cbb, tblock);
10186 GET_BBLOCK (cfg, tblock, ip);
10187 link_bblock (cfg, cfg->cbb, tblock);
10189 if (sp != stack_start) {
10190 handle_stack_args (cfg, stack_start, sp - stack_start);
10191 CHECK_UNVERIFIABLE (cfg);
10194 MONO_INST_NEW(cfg, cmp, OP_ICOMPARE_IMM);
10195 cmp->sreg1 = sp [0]->dreg;
10196 type_from_op (cfg, cmp, sp [0], NULL);
10197 CHECK_TYPE (cmp);
10199 #if SIZEOF_REGISTER == 4
10200 if (cmp->opcode == OP_LCOMPARE_IMM) {
10201 /* Convert it to OP_LCOMPARE */
10202 MONO_INST_NEW (cfg, ins, OP_I8CONST);
10203 ins->type = STACK_I8;
10204 ins->dreg = alloc_dreg (cfg, STACK_I8);
10205 ins->inst_l = 0;
10206 MONO_ADD_INS (cfg->cbb, ins);
10207 cmp->opcode = OP_LCOMPARE;
10208 cmp->sreg2 = ins->dreg;
10210 #endif
10211 MONO_ADD_INS (cfg->cbb, cmp);
10213 MONO_INST_NEW (cfg, ins, is_true ? CEE_BNE_UN : CEE_BEQ);
10214 type_from_op (cfg, ins, sp [0], NULL);
10215 MONO_ADD_INS (cfg->cbb, ins);
10216 ins->inst_many_bb = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);
10217 GET_BBLOCK (cfg, tblock, target);
10218 ins->inst_true_bb = tblock;
10219 GET_BBLOCK (cfg, tblock, ip);
10220 ins->inst_false_bb = tblock;
10221 start_new_bblock = 2;
10223 sp = stack_start;
10224 inline_costs += BRANCH_COST;
10225 break;
10227 case CEE_BEQ:
10228 case CEE_BGE:
10229 case CEE_BGT:
10230 case CEE_BLE:
10231 case CEE_BLT:
10232 case CEE_BNE_UN:
10233 case CEE_BGE_UN:
10234 case CEE_BGT_UN:
10235 case CEE_BLE_UN:
10236 case CEE_BLT_UN:
10237 CHECK_OPSIZE (5);
10238 CHECK_STACK (2);
10239 MONO_INST_NEW (cfg, ins, *ip);
10240 ip++;
10241 target = ip + 4 + (gint32)read32(ip);
10242 ip += 4;
10244 ADD_BINCOND (NULL);
10246 sp = stack_start;
10247 inline_costs += BRANCH_COST;
10248 break;
10249 case CEE_SWITCH: {
10250 MonoInst *src1;
10251 MonoBasicBlock **targets;
10252 MonoBasicBlock *default_bblock;
10253 MonoJumpInfoBBTable *table;
10254 int offset_reg = alloc_preg (cfg);
10255 int target_reg = alloc_preg (cfg);
10256 int table_reg = alloc_preg (cfg);
10257 int sum_reg = alloc_preg (cfg);
10258 gboolean use_op_switch;
10260 CHECK_OPSIZE (5);
10261 CHECK_STACK (1);
10262 n = read32 (ip + 1);
10263 --sp;
10264 src1 = sp [0];
10265 if ((src1->type != STACK_I4) && (src1->type != STACK_PTR))
10266 UNVERIFIED;
10268 ip += 5;
10269 CHECK_OPSIZE (n * sizeof (guint32));
10270 target = ip + n * sizeof (guint32);
10272 GET_BBLOCK (cfg, default_bblock, target);
10273 default_bblock->flags |= BB_INDIRECT_JUMP_TARGET;
10275 targets = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * n);
10276 for (i = 0; i < n; ++i) {
10277 GET_BBLOCK (cfg, tblock, target + (gint32)read32(ip));
10278 targets [i] = tblock;
10279 targets [i]->flags |= BB_INDIRECT_JUMP_TARGET;
10280 ip += 4;
10283 if (sp != stack_start) {
10285 * Link the current bb with the targets as well, so handle_stack_args
10286 * will set their in_stack correctly.
10288 link_bblock (cfg, cfg->cbb, default_bblock);
10289 for (i = 0; i < n; ++i)
10290 link_bblock (cfg, cfg->cbb, targets [i]);
10292 handle_stack_args (cfg, stack_start, sp - stack_start);
10293 sp = stack_start;
10294 CHECK_UNVERIFIABLE (cfg);
10296 /* Undo the links */
10297 mono_unlink_bblock (cfg, cfg->cbb, default_bblock);
10298 for (i = 0; i < n; ++i)
10299 mono_unlink_bblock (cfg, cfg->cbb, targets [i]);
10302 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ICOMPARE_IMM, -1, src1->dreg, n);
10303 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBGE_UN, default_bblock);
10305 for (i = 0; i < n; ++i)
10306 link_bblock (cfg, cfg->cbb, targets [i]);
10308 table = (MonoJumpInfoBBTable *)mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfoBBTable));
10309 table->table = targets;
10310 table->table_size = n;
10312 use_op_switch = FALSE;
10313 #ifdef TARGET_ARM
10314 /* ARM implements SWITCH statements differently */
10315 /* FIXME: Make it use the generic implementation */
10316 if (!cfg->compile_aot)
10317 use_op_switch = TRUE;
10318 #endif
10320 if (COMPILE_LLVM (cfg))
10321 use_op_switch = TRUE;
10323 cfg->cbb->has_jump_table = 1;
10325 if (use_op_switch) {
10326 MONO_INST_NEW (cfg, ins, OP_SWITCH);
10327 ins->sreg1 = src1->dreg;
10328 ins->inst_p0 = table;
10329 ins->inst_many_bb = targets;
10330 ins->klass = (MonoClass *)GUINT_TO_POINTER (n);
10331 MONO_ADD_INS (cfg->cbb, ins);
10332 } else {
10333 if (sizeof (gpointer) == 8)
10334 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, offset_reg, src1->dreg, 3);
10335 else
10336 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, offset_reg, src1->dreg, 2);
10338 #if SIZEOF_REGISTER == 8
10339 /* The upper word might not be zero, and we add it to a 64 bit address later */
10340 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, offset_reg, offset_reg);
10341 #endif
10343 if (cfg->compile_aot) {
10344 MONO_EMIT_NEW_AOTCONST (cfg, table_reg, table, MONO_PATCH_INFO_SWITCH);
10345 } else {
10346 MONO_INST_NEW (cfg, ins, OP_JUMP_TABLE);
10347 ins->inst_c1 = MONO_PATCH_INFO_SWITCH;
10348 ins->inst_p0 = table;
10349 ins->dreg = table_reg;
10350 MONO_ADD_INS (cfg->cbb, ins);
10353 /* FIXME: Use load_memindex */
10354 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, sum_reg, table_reg, offset_reg);
10355 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, target_reg, sum_reg, 0);
10356 MONO_EMIT_NEW_UNALU (cfg, OP_BR_REG, -1, target_reg);
10358 start_new_bblock = 1;
10359 inline_costs += (BRANCH_COST * 2);
10360 break;
10362 case CEE_LDIND_I1:
10363 case CEE_LDIND_U1:
10364 case CEE_LDIND_I2:
10365 case CEE_LDIND_U2:
10366 case CEE_LDIND_I4:
10367 case CEE_LDIND_U4:
10368 case CEE_LDIND_I8:
10369 case CEE_LDIND_I:
10370 case CEE_LDIND_R4:
10371 case CEE_LDIND_R8:
10372 case CEE_LDIND_REF:
10373 CHECK_STACK (1);
10374 --sp;
10376 switch (*ip) {
10377 case CEE_LDIND_R4:
10378 case CEE_LDIND_R8:
10379 dreg = alloc_freg (cfg);
10380 break;
10381 case CEE_LDIND_I8:
10382 dreg = alloc_lreg (cfg);
10383 break;
10384 case CEE_LDIND_REF:
10385 dreg = alloc_ireg_ref (cfg);
10386 break;
10387 default:
10388 dreg = alloc_preg (cfg);
10391 NEW_LOAD_MEMBASE (cfg, ins, ldind_to_load_membase (*ip), dreg, sp [0]->dreg, 0);
10392 ins->type = ldind_type [*ip - CEE_LDIND_I1];
10393 if (*ip == CEE_LDIND_R4)
10394 ins->type = cfg->r4_stack_type;
10395 ins->flags |= ins_flag;
10396 MONO_ADD_INS (cfg->cbb, ins);
10397 *sp++ = ins;
10398 if (ins_flag & MONO_INST_VOLATILE) {
10399 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
10400 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
10402 ins_flag = 0;
10403 ++ip;
10404 break;
10405 case CEE_STIND_REF:
10406 case CEE_STIND_I1:
10407 case CEE_STIND_I2:
10408 case CEE_STIND_I4:
10409 case CEE_STIND_I8:
10410 case CEE_STIND_R4:
10411 case CEE_STIND_R8:
10412 case CEE_STIND_I:
10413 CHECK_STACK (2);
10414 sp -= 2;
10416 if (ins_flag & MONO_INST_VOLATILE) {
10417 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
10418 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
10421 NEW_STORE_MEMBASE (cfg, ins, stind_to_store_membase (*ip), sp [0]->dreg, 0, sp [1]->dreg);
10422 ins->flags |= ins_flag;
10423 ins_flag = 0;
10425 MONO_ADD_INS (cfg->cbb, ins);
10427 if (cfg->gen_write_barriers && *ip == CEE_STIND_REF && method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER && !((sp [1]->opcode == OP_PCONST) && (sp [1]->inst_p0 == 0)))
10428 emit_write_barrier (cfg, sp [0], sp [1]);
10430 inline_costs += 1;
10431 ++ip;
10432 break;
10434 case CEE_MUL:
10435 CHECK_STACK (2);
10437 MONO_INST_NEW (cfg, ins, (*ip));
10438 sp -= 2;
10439 ins->sreg1 = sp [0]->dreg;
10440 ins->sreg2 = sp [1]->dreg;
10441 type_from_op (cfg, ins, sp [0], sp [1]);
10442 CHECK_TYPE (ins);
10443 ins->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type);
10445 /* Use the immediate opcodes if possible */
10446 if ((sp [1]->opcode == OP_ICONST) && mono_arch_is_inst_imm (sp [1]->inst_c0)) {
10447 int imm_opcode = mono_op_to_op_imm_noemul (ins->opcode);
10448 if (imm_opcode != -1) {
10449 ins->opcode = imm_opcode;
10450 ins->inst_p1 = (gpointer)(gssize)(sp [1]->inst_c0);
10451 ins->sreg2 = -1;
10453 NULLIFY_INS (sp [1]);
10457 MONO_ADD_INS ((cfg)->cbb, (ins));
10459 *sp++ = mono_decompose_opcode (cfg, ins);
10460 ip++;
10461 break;
10462 case CEE_ADD:
10463 case CEE_SUB:
10464 case CEE_DIV:
10465 case CEE_DIV_UN:
10466 case CEE_REM:
10467 case CEE_REM_UN:
10468 case CEE_AND:
10469 case CEE_OR:
10470 case CEE_XOR:
10471 case CEE_SHL:
10472 case CEE_SHR:
10473 case CEE_SHR_UN:
10474 CHECK_STACK (2);
10476 MONO_INST_NEW (cfg, ins, (*ip));
10477 sp -= 2;
10478 ins->sreg1 = sp [0]->dreg;
10479 ins->sreg2 = sp [1]->dreg;
10480 type_from_op (cfg, ins, sp [0], sp [1]);
10481 CHECK_TYPE (ins);
10482 add_widen_op (cfg, ins, &sp [0], &sp [1]);
10483 ins->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type);
10485 /* FIXME: Pass opcode to is_inst_imm */
10487 /* Use the immediate opcodes if possible */
10488 if (((sp [1]->opcode == OP_ICONST) || (sp [1]->opcode == OP_I8CONST)) && mono_arch_is_inst_imm (sp [1]->opcode == OP_ICONST ? sp [1]->inst_c0 : sp [1]->inst_l)) {
10489 int imm_opcode = mono_op_to_op_imm_noemul (ins->opcode);
10490 if (imm_opcode != -1) {
10491 ins->opcode = imm_opcode;
10492 if (sp [1]->opcode == OP_I8CONST) {
10493 #if SIZEOF_REGISTER == 8
10494 ins->inst_imm = sp [1]->inst_l;
10495 #else
10496 ins->inst_ls_word = sp [1]->inst_ls_word;
10497 ins->inst_ms_word = sp [1]->inst_ms_word;
10498 #endif
10500 else
10501 ins->inst_imm = (gssize)(sp [1]->inst_c0);
10502 ins->sreg2 = -1;
10504 /* Might be followed by an instruction added by add_widen_op */
10505 if (sp [1]->next == NULL)
10506 NULLIFY_INS (sp [1]);
10509 MONO_ADD_INS ((cfg)->cbb, (ins));
10511 *sp++ = mono_decompose_opcode (cfg, ins);
10512 ip++;
10513 break;
10514 case CEE_NEG:
10515 case CEE_NOT:
10516 case CEE_CONV_I1:
10517 case CEE_CONV_I2:
10518 case CEE_CONV_I4:
10519 case CEE_CONV_R4:
10520 case CEE_CONV_R8:
10521 case CEE_CONV_U4:
10522 case CEE_CONV_I8:
10523 case CEE_CONV_U8:
10524 case CEE_CONV_OVF_I8:
10525 case CEE_CONV_OVF_U8:
10526 case CEE_CONV_R_UN:
10527 CHECK_STACK (1);
10529 /* Special case this earlier so we have long constants in the IR */
10530 if ((((*ip) == CEE_CONV_I8) || ((*ip) == CEE_CONV_U8)) && (sp [-1]->opcode == OP_ICONST)) {
10531 int data = sp [-1]->inst_c0;
10532 sp [-1]->opcode = OP_I8CONST;
10533 sp [-1]->type = STACK_I8;
10534 #if SIZEOF_REGISTER == 8
10535 if ((*ip) == CEE_CONV_U8)
10536 sp [-1]->inst_c0 = (guint32)data;
10537 else
10538 sp [-1]->inst_c0 = data;
10539 #else
10540 sp [-1]->inst_ls_word = data;
10541 if ((*ip) == CEE_CONV_U8)
10542 sp [-1]->inst_ms_word = 0;
10543 else
10544 sp [-1]->inst_ms_word = (data < 0) ? -1 : 0;
10545 #endif
10546 sp [-1]->dreg = alloc_dreg (cfg, STACK_I8);
10548 else {
10549 ADD_UNOP (*ip);
10551 ip++;
10552 break;
10553 case CEE_CONV_OVF_I4:
10554 case CEE_CONV_OVF_I1:
10555 case CEE_CONV_OVF_I2:
10556 case CEE_CONV_OVF_I:
10557 case CEE_CONV_OVF_U:
10558 CHECK_STACK (1);
10560 if (sp [-1]->type == STACK_R8 || sp [-1]->type == STACK_R4) {
10561 ADD_UNOP (CEE_CONV_OVF_I8);
10562 ADD_UNOP (*ip);
10563 } else {
10564 ADD_UNOP (*ip);
10566 ip++;
10567 break;
10568 case CEE_CONV_OVF_U1:
10569 case CEE_CONV_OVF_U2:
10570 case CEE_CONV_OVF_U4:
10571 CHECK_STACK (1);
10573 if (sp [-1]->type == STACK_R8 || sp [-1]->type == STACK_R4) {
10574 ADD_UNOP (CEE_CONV_OVF_U8);
10575 ADD_UNOP (*ip);
10576 } else {
10577 ADD_UNOP (*ip);
10579 ip++;
10580 break;
10581 case CEE_CONV_OVF_I1_UN:
10582 case CEE_CONV_OVF_I2_UN:
10583 case CEE_CONV_OVF_I4_UN:
10584 case CEE_CONV_OVF_I8_UN:
10585 case CEE_CONV_OVF_U1_UN:
10586 case CEE_CONV_OVF_U2_UN:
10587 case CEE_CONV_OVF_U4_UN:
10588 case CEE_CONV_OVF_U8_UN:
10589 case CEE_CONV_OVF_I_UN:
10590 case CEE_CONV_OVF_U_UN:
10591 case CEE_CONV_U2:
10592 case CEE_CONV_U1:
10593 case CEE_CONV_I:
10594 case CEE_CONV_U:
10595 CHECK_STACK (1);
10596 ADD_UNOP (*ip);
10597 CHECK_CFG_EXCEPTION;
10598 ip++;
10599 break;
10600 case CEE_ADD_OVF:
10601 case CEE_ADD_OVF_UN:
10602 case CEE_MUL_OVF:
10603 case CEE_MUL_OVF_UN:
10604 case CEE_SUB_OVF:
10605 case CEE_SUB_OVF_UN:
10606 CHECK_STACK (2);
10607 ADD_BINOP (*ip);
10608 ip++;
10609 break;
10610 case CEE_CPOBJ:
10611 GSHAREDVT_FAILURE (*ip);
10612 CHECK_OPSIZE (5);
10613 CHECK_STACK (2);
10614 token = read32 (ip + 1);
10615 klass = mini_get_class (method, token, generic_context);
10616 CHECK_TYPELOAD (klass);
10617 sp -= 2;
10618 if (generic_class_is_reference_type (cfg, klass)) {
10619 MonoInst *store, *load;
10620 int dreg = alloc_ireg_ref (cfg);
10622 NEW_LOAD_MEMBASE (cfg, load, OP_LOAD_MEMBASE, dreg, sp [1]->dreg, 0);
10623 load->flags |= ins_flag;
10624 MONO_ADD_INS (cfg->cbb, load);
10626 NEW_STORE_MEMBASE (cfg, store, OP_STORE_MEMBASE_REG, sp [0]->dreg, 0, dreg);
10627 store->flags |= ins_flag;
10628 MONO_ADD_INS (cfg->cbb, store);
10630 if (cfg->gen_write_barriers && cfg->method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER)
10631 emit_write_barrier (cfg, sp [0], sp [1]);
10632 } else {
10633 mini_emit_stobj (cfg, sp [0], sp [1], klass, FALSE);
10635 ins_flag = 0;
10636 ip += 5;
10637 break;
10638 case CEE_LDOBJ: {
10639 int loc_index = -1;
10640 int stloc_len = 0;
10642 CHECK_OPSIZE (5);
10643 CHECK_STACK (1);
10644 --sp;
10645 token = read32 (ip + 1);
10646 klass = mini_get_class (method, token, generic_context);
10647 CHECK_TYPELOAD (klass);
10649 /* Optimize the common ldobj+stloc combination */
10650 switch (ip [5]) {
10651 case CEE_STLOC_S:
10652 loc_index = ip [6];
10653 stloc_len = 2;
10654 break;
10655 case CEE_STLOC_0:
10656 case CEE_STLOC_1:
10657 case CEE_STLOC_2:
10658 case CEE_STLOC_3:
10659 loc_index = ip [5] - CEE_STLOC_0;
10660 stloc_len = 1;
10661 break;
10662 default:
10663 break;
10666 if ((loc_index != -1) && ip_in_bb (cfg, cfg->cbb, ip + 5)) {
10667 CHECK_LOCAL (loc_index);
10669 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0);
10670 ins->dreg = cfg->locals [loc_index]->dreg;
10671 ins->flags |= ins_flag;
10672 ip += 5;
10673 ip += stloc_len;
10674 if (ins_flag & MONO_INST_VOLATILE) {
10675 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
10676 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
10678 ins_flag = 0;
10679 break;
10682 /* Optimize the ldobj+stobj combination */
10683 /* The reference case ends up being a load+store anyway */
10684 /* Skip this if the operation is volatile. */
10685 if (((ip [5] == CEE_STOBJ) && ip_in_bb (cfg, cfg->cbb, ip + 5) && read32 (ip + 6) == token) && !generic_class_is_reference_type (cfg, klass) && !(ins_flag & MONO_INST_VOLATILE)) {
10686 CHECK_STACK (1);
10688 sp --;
10690 mini_emit_stobj (cfg, sp [0], sp [1], klass, FALSE);
10692 ip += 5 + 5;
10693 ins_flag = 0;
10694 break;
10697 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0);
10698 ins->flags |= ins_flag;
10699 *sp++ = ins;
10701 if (ins_flag & MONO_INST_VOLATILE) {
10702 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
10703 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
10706 ip += 5;
10707 ins_flag = 0;
10708 inline_costs += 1;
10709 break;
10711 case CEE_LDSTR:
10712 CHECK_STACK_OVF (1);
10713 CHECK_OPSIZE (5);
10714 n = read32 (ip + 1);
10716 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
10717 EMIT_NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, n));
10718 ins->type = STACK_OBJ;
10719 *sp = ins;
10721 else if (method->wrapper_type != MONO_WRAPPER_NONE) {
10722 MonoInst *iargs [1];
10723 char *str = (char *)mono_method_get_wrapper_data (method, n);
10725 if (cfg->compile_aot)
10726 EMIT_NEW_LDSTRLITCONST (cfg, iargs [0], str);
10727 else
10728 EMIT_NEW_PCONST (cfg, iargs [0], str);
10729 *sp = mono_emit_jit_icall (cfg, mono_string_new_wrapper, iargs);
10730 } else {
10731 if (cfg->opt & MONO_OPT_SHARED) {
10732 MonoInst *iargs [3];
10734 if (cfg->compile_aot) {
10735 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, GINT_TO_POINTER (n));
10737 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
10738 EMIT_NEW_IMAGECONST (cfg, iargs [1], image);
10739 EMIT_NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
10740 *sp = mono_emit_jit_icall (cfg, ves_icall_mono_ldstr, iargs);
10741 mono_ldstr_checked (cfg->domain, image, mono_metadata_token_index (n), &cfg->error);
10742 CHECK_CFG_ERROR;
10743 } else {
10744 if (cfg->cbb->out_of_line) {
10745 MonoInst *iargs [2];
10747 if (image == mono_defaults.corlib) {
10749 * Avoid relocations in AOT and save some space by using a
10750 * version of helper_ldstr specialized to mscorlib.
10752 EMIT_NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (n));
10753 *sp = mono_emit_jit_icall (cfg, mono_helper_ldstr_mscorlib, iargs);
10754 } else {
10755 /* Avoid creating the string object */
10756 EMIT_NEW_IMAGECONST (cfg, iargs [0], image);
10757 EMIT_NEW_ICONST (cfg, iargs [1], mono_metadata_token_index (n));
10758 *sp = mono_emit_jit_icall (cfg, mono_helper_ldstr, iargs);
10761 else
10762 if (cfg->compile_aot) {
10763 NEW_LDSTRCONST (cfg, ins, image, n);
10764 *sp = ins;
10765 MONO_ADD_INS (cfg->cbb, ins);
10767 else {
10768 NEW_PCONST (cfg, ins, NULL);
10769 ins->type = STACK_OBJ;
10770 ins->inst_p0 = mono_ldstr_checked (cfg->domain, image, mono_metadata_token_index (n), &cfg->error);
10771 CHECK_CFG_ERROR;
10773 if (!ins->inst_p0)
10774 OUT_OF_MEMORY_FAILURE;
10776 *sp = ins;
10777 MONO_ADD_INS (cfg->cbb, ins);
10782 sp++;
10783 ip += 5;
10784 break;
10785 case CEE_NEWOBJ: {
10786 MonoInst *iargs [2];
10787 MonoMethodSignature *fsig;
10788 MonoInst this_ins;
10789 MonoInst *alloc;
10790 MonoInst *vtable_arg = NULL;
10792 CHECK_OPSIZE (5);
10793 token = read32 (ip + 1);
10794 cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
10795 CHECK_CFG_ERROR;
10797 fsig = mono_method_get_signature_checked (cmethod, image, token, generic_context, &cfg->error);
10798 CHECK_CFG_ERROR;
10800 mono_save_token_info (cfg, image, token, cmethod);
10802 if (!mono_class_init (cmethod->klass))
10803 TYPE_LOAD_ERROR (cmethod->klass);
10805 context_used = mini_method_check_context_used (cfg, cmethod);
10807 if (mono_security_core_clr_enabled ())
10808 ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
10810 if (cfg->gshared && cmethod && cmethod->klass != method->klass && cmethod->klass->generic_class && mono_method_is_generic_sharable (cmethod, TRUE) && mono_class_needs_cctor_run (cmethod->klass, method)) {
10811 emit_class_init (cfg, cmethod->klass);
10812 CHECK_TYPELOAD (cmethod->klass);
10816 if (cfg->gsharedvt) {
10817 if (mini_is_gsharedvt_variable_signature (sig))
10818 GSHAREDVT_FAILURE (*ip);
10822 n = fsig->param_count;
10823 CHECK_STACK (n);
10826 * Generate smaller code for the common newobj <exception> instruction in
10827 * argument checking code.
10829 if (cfg->cbb->out_of_line && cmethod->klass->image == mono_defaults.corlib &&
10830 is_exception_class (cmethod->klass) && n <= 2 &&
10831 ((n < 1) || (!fsig->params [0]->byref && fsig->params [0]->type == MONO_TYPE_STRING)) &&
10832 ((n < 2) || (!fsig->params [1]->byref && fsig->params [1]->type == MONO_TYPE_STRING))) {
10833 MonoInst *iargs [3];
10835 sp -= n;
10837 EMIT_NEW_ICONST (cfg, iargs [0], cmethod->klass->type_token);
10838 switch (n) {
10839 case 0:
10840 *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_0, iargs);
10841 break;
10842 case 1:
10843 iargs [1] = sp [0];
10844 *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_1, iargs);
10845 break;
10846 case 2:
10847 iargs [1] = sp [0];
10848 iargs [2] = sp [1];
10849 *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_2, iargs);
10850 break;
10851 default:
10852 g_assert_not_reached ();
10855 ip += 5;
10856 inline_costs += 5;
10857 break;
10860 /* move the args to allow room for 'this' in the first position */
10861 while (n--) {
10862 --sp;
10863 sp [1] = sp [0];
10866 /* check_call_signature () requires sp[0] to be set */
10867 this_ins.type = STACK_OBJ;
10868 sp [0] = &this_ins;
10869 if (check_call_signature (cfg, fsig, sp))
10870 UNVERIFIED;
10872 iargs [0] = NULL;
10874 if (mini_class_is_system_array (cmethod->klass)) {
10875 *sp = emit_get_rgctx_method (cfg, context_used,
10876 cmethod, MONO_RGCTX_INFO_METHOD);
10878 /* Avoid varargs in the common case */
10879 if (fsig->param_count == 1)
10880 alloc = mono_emit_jit_icall (cfg, mono_array_new_1, sp);
10881 else if (fsig->param_count == 2)
10882 alloc = mono_emit_jit_icall (cfg, mono_array_new_2, sp);
10883 else if (fsig->param_count == 3)
10884 alloc = mono_emit_jit_icall (cfg, mono_array_new_3, sp);
10885 else if (fsig->param_count == 4)
10886 alloc = mono_emit_jit_icall (cfg, mono_array_new_4, sp);
10887 else
10888 alloc = handle_array_new (cfg, fsig->param_count, sp, ip);
10889 } else if (cmethod->string_ctor) {
10890 g_assert (!context_used);
10891 g_assert (!vtable_arg);
10892 /* we simply pass a null pointer */
10893 EMIT_NEW_PCONST (cfg, *sp, NULL);
10894 /* now call the string ctor */
10895 alloc = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, NULL, NULL, NULL);
10896 } else {
10897 if (cmethod->klass->valuetype) {
10898 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
10899 emit_init_rvar (cfg, iargs [0]->dreg, &cmethod->klass->byval_arg);
10900 EMIT_NEW_TEMPLOADA (cfg, *sp, iargs [0]->inst_c0);
10902 alloc = NULL;
10905 * The code generated by mini_emit_virtual_call () expects
10906 * iargs [0] to be a boxed instance, but luckily the vcall
10907 * will be transformed into a normal call there.
10909 } else if (context_used) {
10910 alloc = handle_alloc (cfg, cmethod->klass, FALSE, context_used);
10911 *sp = alloc;
10912 } else {
10913 MonoVTable *vtable = NULL;
10915 if (!cfg->compile_aot)
10916 vtable = mono_class_vtable (cfg->domain, cmethod->klass);
10917 CHECK_TYPELOAD (cmethod->klass);
10920 * TypeInitializationExceptions thrown from the mono_runtime_class_init
10921 * call in mono_jit_runtime_invoke () can abort the finalizer thread.
10922 * As a workaround, we call class cctors before allocating objects.
10924 if (mini_field_access_needs_cctor_run (cfg, method, cmethod->klass, vtable) && !(g_slist_find (class_inits, cmethod->klass))) {
10925 emit_class_init (cfg, cmethod->klass);
10926 if (cfg->verbose_level > 2)
10927 printf ("class %s.%s needs init call for ctor\n", cmethod->klass->name_space, cmethod->klass->name);
10928 class_inits = g_slist_prepend (class_inits, cmethod->klass);
10931 alloc = handle_alloc (cfg, cmethod->klass, FALSE, 0);
10932 *sp = alloc;
10934 CHECK_CFG_EXCEPTION; /*for handle_alloc*/
10936 if (alloc)
10937 MONO_EMIT_NEW_UNALU (cfg, OP_NOT_NULL, -1, alloc->dreg);
10939 /* Now call the actual ctor */
10940 handle_ctor_call (cfg, cmethod, fsig, context_used, sp, ip, &inline_costs);
10941 CHECK_CFG_EXCEPTION;
10944 if (alloc == NULL) {
10945 /* Valuetype */
10946 EMIT_NEW_TEMPLOAD (cfg, ins, iargs [0]->inst_c0);
10947 type_to_eval_stack_type (cfg, &ins->klass->byval_arg, ins);
10948 *sp++= ins;
10949 } else {
10950 *sp++ = alloc;
10953 ip += 5;
10954 inline_costs += 5;
10955 if (!(seq_point_locs && mono_bitset_test_fast (seq_point_locs, ip - header->code)))
10956 emit_seq_point (cfg, method, ip, FALSE, TRUE);
10957 break;
10959 case CEE_CASTCLASS:
10960 CHECK_STACK (1);
10961 --sp;
10962 CHECK_OPSIZE (5);
10963 token = read32 (ip + 1);
10964 klass = mini_get_class (method, token, generic_context);
10965 CHECK_TYPELOAD (klass);
10966 if (sp [0]->type != STACK_OBJ)
10967 UNVERIFIED;
10969 ins = handle_castclass (cfg, klass, *sp, ip, &inline_costs);
10970 CHECK_CFG_EXCEPTION;
10972 *sp ++ = ins;
10973 ip += 5;
10974 break;
10975 case CEE_ISINST: {
10976 CHECK_STACK (1);
10977 --sp;
10978 CHECK_OPSIZE (5);
10979 token = read32 (ip + 1);
10980 klass = mini_get_class (method, token, generic_context);
10981 CHECK_TYPELOAD (klass);
10982 if (sp [0]->type != STACK_OBJ)
10983 UNVERIFIED;
10985 context_used = mini_class_check_context_used (cfg, klass);
10987 if (!context_used && mini_class_has_reference_variant_generic_argument (cfg, klass, context_used)) {
10988 MonoMethod *mono_isinst = mono_marshal_get_isinst_with_cache ();
10989 MonoInst *args [3];
10990 int idx;
10992 /* obj */
10993 args [0] = *sp;
10995 /* klass */
10996 EMIT_NEW_CLASSCONST (cfg, args [1], klass);
10998 /* inline cache*/
10999 idx = get_castclass_cache_idx (cfg);
11000 args [2] = emit_runtime_constant (cfg, MONO_PATCH_INFO_CASTCLASS_CACHE, GINT_TO_POINTER (idx));
11002 *sp++ = mono_emit_method_call (cfg, mono_isinst, args, NULL);
11003 ip += 5;
11004 inline_costs += 2;
11005 } else if (!context_used && (mono_class_is_marshalbyref (klass) || klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
11006 MonoMethod *mono_isinst;
11007 MonoInst *iargs [1];
11008 int costs;
11010 mono_isinst = mono_marshal_get_isinst (klass);
11011 iargs [0] = sp [0];
11013 costs = inline_method (cfg, mono_isinst, mono_method_signature (mono_isinst),
11014 iargs, ip, cfg->real_offset, TRUE);
11015 CHECK_CFG_EXCEPTION;
11016 g_assert (costs > 0);
11018 ip += 5;
11019 cfg->real_offset += 5;
11021 *sp++= iargs [0];
11023 inline_costs += costs;
11025 else {
11026 ins = handle_isinst (cfg, klass, *sp, context_used);
11027 CHECK_CFG_EXCEPTION;
11028 *sp ++ = ins;
11029 ip += 5;
11031 break;
11033 case CEE_UNBOX_ANY: {
11034 MonoInst *res, *addr;
11036 CHECK_STACK (1);
11037 --sp;
11038 CHECK_OPSIZE (5);
11039 token = read32 (ip + 1);
11040 klass = mini_get_class (method, token, generic_context);
11041 CHECK_TYPELOAD (klass);
11043 mono_save_token_info (cfg, image, token, klass);
11045 context_used = mini_class_check_context_used (cfg, klass);
11047 if (mini_is_gsharedvt_klass (klass)) {
11048 res = handle_unbox_gsharedvt (cfg, klass, *sp);
11049 inline_costs += 2;
11050 } else if (generic_class_is_reference_type (cfg, klass)) {
11051 res = handle_castclass (cfg, klass, *sp, ip, &inline_costs);
11052 CHECK_CFG_EXCEPTION;
11053 } else if (mono_class_is_nullable (klass)) {
11054 res = handle_unbox_nullable (cfg, *sp, klass, context_used);
11055 } else {
11056 addr = handle_unbox (cfg, klass, sp, context_used);
11057 /* LDOBJ */
11058 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
11059 res = ins;
11060 inline_costs += 2;
11063 *sp ++ = res;
11064 ip += 5;
11065 break;
11067 case CEE_BOX: {
11068 MonoInst *val;
11069 MonoClass *enum_class;
11070 MonoMethod *has_flag;
11072 CHECK_STACK (1);
11073 --sp;
11074 val = *sp;
11075 CHECK_OPSIZE (5);
11076 token = read32 (ip + 1);
11077 klass = mini_get_class (method, token, generic_context);
11078 CHECK_TYPELOAD (klass);
11080 mono_save_token_info (cfg, image, token, klass);
11082 context_used = mini_class_check_context_used (cfg, klass);
11084 if (generic_class_is_reference_type (cfg, klass)) {
11085 *sp++ = val;
11086 ip += 5;
11087 break;
11090 if (klass == mono_defaults.void_class)
11091 UNVERIFIED;
11092 if (target_type_is_incompatible (cfg, &klass->byval_arg, *sp))
11093 UNVERIFIED;
11094 /* frequent check in generic code: box (struct), brtrue */
11097 * Look for:
11099 * <push int/long ptr>
11100 * <push int/long>
11101 * box MyFlags
11102 * constrained. MyFlags
11103 * callvirt instace bool class [mscorlib] System.Enum::HasFlag (class [mscorlib] System.Enum)
11105 * If we find this sequence and the operand types on box and constrained
11106 * are equal, we can emit a specialized instruction sequence instead of
11107 * the very slow HasFlag () call.
11109 if ((cfg->opt & MONO_OPT_INTRINS) &&
11110 /* Cheap checks first. */
11111 ip + 5 + 6 + 5 < end &&
11112 ip [5] == CEE_PREFIX1 &&
11113 ip [6] == CEE_CONSTRAINED_ &&
11114 ip [11] == CEE_CALLVIRT &&
11115 ip_in_bb (cfg, cfg->cbb, ip + 5 + 6 + 5) &&
11116 mono_class_is_enum (klass) &&
11117 (enum_class = mini_get_class (method, read32 (ip + 7), generic_context)) &&
11118 (has_flag = mini_get_method (cfg, method, read32 (ip + 12), NULL, generic_context)) &&
11119 has_flag->klass == mono_defaults.enum_class &&
11120 !strcmp (has_flag->name, "HasFlag") &&
11121 has_flag->signature->hasthis &&
11122 has_flag->signature->param_count == 1) {
11123 CHECK_TYPELOAD (enum_class);
11125 if (enum_class == klass) {
11126 MonoInst *enum_this, *enum_flag;
11128 ip += 5 + 6 + 5;
11129 --sp;
11131 enum_this = sp [0];
11132 enum_flag = sp [1];
11134 *sp++ = handle_enum_has_flag (cfg, klass, enum_this, enum_flag);
11135 break;
11139 // FIXME: LLVM can't handle the inconsistent bb linking
11140 if (!mono_class_is_nullable (klass) &&
11141 !mini_is_gsharedvt_klass (klass) &&
11142 ip + 5 < end && ip_in_bb (cfg, cfg->cbb, ip + 5) &&
11143 (ip [5] == CEE_BRTRUE ||
11144 ip [5] == CEE_BRTRUE_S ||
11145 ip [5] == CEE_BRFALSE ||
11146 ip [5] == CEE_BRFALSE_S)) {
11147 gboolean is_true = ip [5] == CEE_BRTRUE || ip [5] == CEE_BRTRUE_S;
11148 int dreg;
11149 MonoBasicBlock *true_bb, *false_bb;
11151 ip += 5;
11153 if (cfg->verbose_level > 3) {
11154 printf ("converting (in B%d: stack: %d) %s", cfg->cbb->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
11155 printf ("<box+brtrue opt>\n");
11158 switch (*ip) {
11159 case CEE_BRTRUE_S:
11160 case CEE_BRFALSE_S:
11161 CHECK_OPSIZE (2);
11162 ip++;
11163 target = ip + 1 + (signed char)(*ip);
11164 ip++;
11165 break;
11166 case CEE_BRTRUE:
11167 case CEE_BRFALSE:
11168 CHECK_OPSIZE (5);
11169 ip++;
11170 target = ip + 4 + (gint)(read32 (ip));
11171 ip += 4;
11172 break;
11173 default:
11174 g_assert_not_reached ();
11178 * We need to link both bblocks, since it is needed for handling stack
11179 * arguments correctly (See test_0_box_brtrue_opt_regress_81102).
11180 * Branching to only one of them would lead to inconsistencies, so
11181 * generate an ICONST+BRTRUE, the branch opts will get rid of them.
11183 GET_BBLOCK (cfg, true_bb, target);
11184 GET_BBLOCK (cfg, false_bb, ip);
11186 mono_link_bblock (cfg, cfg->cbb, true_bb);
11187 mono_link_bblock (cfg, cfg->cbb, false_bb);
11189 if (sp != stack_start) {
11190 handle_stack_args (cfg, stack_start, sp - stack_start);
11191 sp = stack_start;
11192 CHECK_UNVERIFIABLE (cfg);
11195 if (COMPILE_LLVM (cfg)) {
11196 dreg = alloc_ireg (cfg);
11197 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
11198 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, dreg, is_true ? 0 : 1);
11200 MONO_EMIT_NEW_BRANCH_BLOCK2 (cfg, OP_IBEQ, true_bb, false_bb);
11201 } else {
11202 /* The JIT can't eliminate the iconst+compare */
11203 MONO_INST_NEW (cfg, ins, OP_BR);
11204 ins->inst_target_bb = is_true ? true_bb : false_bb;
11205 MONO_ADD_INS (cfg->cbb, ins);
11208 start_new_bblock = 1;
11209 break;
11212 *sp++ = handle_box (cfg, val, klass, context_used);
11214 CHECK_CFG_EXCEPTION;
11215 ip += 5;
11216 inline_costs += 1;
11217 break;
11219 case CEE_UNBOX: {
11220 CHECK_STACK (1);
11221 --sp;
11222 CHECK_OPSIZE (5);
11223 token = read32 (ip + 1);
11224 klass = mini_get_class (method, token, generic_context);
11225 CHECK_TYPELOAD (klass);
11227 mono_save_token_info (cfg, image, token, klass);
11229 context_used = mini_class_check_context_used (cfg, klass);
11231 if (mono_class_is_nullable (klass)) {
11232 MonoInst *val;
11234 val = handle_unbox_nullable (cfg, *sp, klass, context_used);
11235 EMIT_NEW_VARLOADA (cfg, ins, get_vreg_to_inst (cfg, val->dreg), &val->klass->byval_arg);
11237 *sp++= ins;
11238 } else {
11239 ins = handle_unbox (cfg, klass, sp, context_used);
11240 *sp++ = ins;
11242 ip += 5;
11243 inline_costs += 2;
11244 break;
11246 case CEE_LDFLD:
11247 case CEE_LDFLDA:
11248 case CEE_STFLD:
11249 case CEE_LDSFLD:
11250 case CEE_LDSFLDA:
11251 case CEE_STSFLD: {
11252 MonoClassField *field;
11253 #ifndef DISABLE_REMOTING
11254 int costs;
11255 #endif
11256 guint foffset;
11257 gboolean is_instance;
11258 int op;
11259 gpointer addr = NULL;
11260 gboolean is_special_static;
11261 MonoType *ftype;
11262 MonoInst *store_val = NULL;
11263 MonoInst *thread_ins;
11265 op = *ip;
11266 is_instance = (op == CEE_LDFLD || op == CEE_LDFLDA || op == CEE_STFLD);
11267 if (is_instance) {
11268 if (op == CEE_STFLD) {
11269 CHECK_STACK (2);
11270 sp -= 2;
11271 store_val = sp [1];
11272 } else {
11273 CHECK_STACK (1);
11274 --sp;
11276 if (sp [0]->type == STACK_I4 || sp [0]->type == STACK_I8 || sp [0]->type == STACK_R8)
11277 UNVERIFIED;
11278 if (*ip != CEE_LDFLD && sp [0]->type == STACK_VTYPE)
11279 UNVERIFIED;
11280 } else {
11281 if (op == CEE_STSFLD) {
11282 CHECK_STACK (1);
11283 sp--;
11284 store_val = sp [0];
11288 CHECK_OPSIZE (5);
11289 token = read32 (ip + 1);
11290 if (method->wrapper_type != MONO_WRAPPER_NONE) {
11291 field = (MonoClassField *)mono_method_get_wrapper_data (method, token);
11292 klass = field->parent;
11294 else {
11295 field = mono_field_from_token_checked (image, token, &klass, generic_context, &cfg->error);
11296 CHECK_CFG_ERROR;
11298 if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_field (method, field))
11299 FIELD_ACCESS_FAILURE (method, field);
11300 mono_class_init (klass);
11302 /* if the class is Critical then transparent code cannot access it's fields */
11303 if (!is_instance && mono_security_core_clr_enabled ())
11304 ensure_method_is_allowed_to_access_field (cfg, method, field);
11306 /* XXX this is technically required but, so far (SL2), no [SecurityCritical] types (not many exists) have
11307 any visible *instance* field (in fact there's a single case for a static field in Marshal) XXX
11308 if (mono_security_core_clr_enabled ())
11309 ensure_method_is_allowed_to_access_field (cfg, method, field);
11312 ftype = mono_field_get_type (field);
11315 * LDFLD etc. is usable on static fields as well, so convert those cases to
11316 * the static case.
11318 if (is_instance && ftype->attrs & FIELD_ATTRIBUTE_STATIC) {
11319 switch (op) {
11320 case CEE_LDFLD:
11321 op = CEE_LDSFLD;
11322 break;
11323 case CEE_STFLD:
11324 op = CEE_STSFLD;
11325 break;
11326 case CEE_LDFLDA:
11327 op = CEE_LDSFLDA;
11328 break;
11329 default:
11330 g_assert_not_reached ();
11332 is_instance = FALSE;
11335 context_used = mini_class_check_context_used (cfg, klass);
11337 /* INSTANCE CASE */
11339 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
11340 if (op == CEE_STFLD) {
11341 if (target_type_is_incompatible (cfg, field->type, sp [1]))
11342 UNVERIFIED;
11343 #ifndef DISABLE_REMOTING
11344 if ((mono_class_is_marshalbyref (klass) && !MONO_CHECK_THIS (sp [0])) || mono_class_is_contextbound (klass) || klass == mono_defaults.marshalbyrefobject_class) {
11345 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
11346 MonoInst *iargs [5];
11348 GSHAREDVT_FAILURE (op);
11350 iargs [0] = sp [0];
11351 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
11352 EMIT_NEW_FIELDCONST (cfg, iargs [2], field);
11353 EMIT_NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
11354 field->offset);
11355 iargs [4] = sp [1];
11357 if (cfg->opt & MONO_OPT_INLINE || cfg->compile_aot) {
11358 costs = inline_method (cfg, stfld_wrapper, mono_method_signature (stfld_wrapper),
11359 iargs, ip, cfg->real_offset, TRUE);
11360 CHECK_CFG_EXCEPTION;
11361 g_assert (costs > 0);
11363 cfg->real_offset += 5;
11365 inline_costs += costs;
11366 } else {
11367 mono_emit_method_call (cfg, stfld_wrapper, iargs, NULL);
11369 } else
11370 #endif
11372 MonoInst *store, *wbarrier_ptr_ins = NULL;
11374 MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
11376 if (mini_is_gsharedvt_klass (klass)) {
11377 MonoInst *offset_ins;
11379 context_used = mini_class_check_context_used (cfg, klass);
11381 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
11382 /* The value is offset by 1 */
11383 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
11384 dreg = alloc_ireg_mp (cfg);
11385 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
11386 wbarrier_ptr_ins = ins;
11387 /* The decomposition will call mini_emit_stobj () which will emit a wbarrier if needed */
11388 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, field->type, dreg, 0, sp [1]->dreg);
11389 } else {
11390 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, field->type, sp [0]->dreg, foffset, sp [1]->dreg);
11392 if (sp [0]->opcode != OP_LDADDR)
11393 store->flags |= MONO_INST_FAULT;
11395 if (cfg->gen_write_barriers && mini_type_to_stind (cfg, field->type) == CEE_STIND_REF && !(sp [1]->opcode == OP_PCONST && sp [1]->inst_c0 == 0)) {
11396 if (mini_is_gsharedvt_klass (klass)) {
11397 g_assert (wbarrier_ptr_ins);
11398 emit_write_barrier (cfg, wbarrier_ptr_ins, sp [1]);
11399 } else {
11400 /* insert call to write barrier */
11401 MonoInst *ptr;
11402 int dreg;
11404 dreg = alloc_ireg_mp (cfg);
11405 EMIT_NEW_BIALU_IMM (cfg, ptr, OP_PADD_IMM, dreg, sp [0]->dreg, foffset);
11406 emit_write_barrier (cfg, ptr, sp [1]);
11410 store->flags |= ins_flag;
11412 ins_flag = 0;
11413 ip += 5;
11414 break;
11417 #ifndef DISABLE_REMOTING
11418 if (is_instance && ((mono_class_is_marshalbyref (klass) && !MONO_CHECK_THIS (sp [0])) || mono_class_is_contextbound (klass) || klass == mono_defaults.marshalbyrefobject_class)) {
11419 MonoMethod *wrapper = (op == CEE_LDFLDA) ? mono_marshal_get_ldflda_wrapper (field->type) : mono_marshal_get_ldfld_wrapper (field->type);
11420 MonoInst *iargs [4];
11422 GSHAREDVT_FAILURE (op);
11424 iargs [0] = sp [0];
11425 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
11426 EMIT_NEW_FIELDCONST (cfg, iargs [2], field);
11427 EMIT_NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
11428 if (cfg->opt & MONO_OPT_INLINE || cfg->compile_aot) {
11429 costs = inline_method (cfg, wrapper, mono_method_signature (wrapper),
11430 iargs, ip, cfg->real_offset, TRUE);
11431 CHECK_CFG_EXCEPTION;
11432 g_assert (costs > 0);
11434 cfg->real_offset += 5;
11436 *sp++ = iargs [0];
11438 inline_costs += costs;
11439 } else {
11440 ins = mono_emit_method_call (cfg, wrapper, iargs, NULL);
11441 *sp++ = ins;
11443 } else
11444 #endif
11445 if (is_instance) {
11446 if (sp [0]->type == STACK_VTYPE) {
11447 MonoInst *var;
11449 /* Have to compute the address of the variable */
11451 var = get_vreg_to_inst (cfg, sp [0]->dreg);
11452 if (!var)
11453 var = mono_compile_create_var_for_vreg (cfg, &klass->byval_arg, OP_LOCAL, sp [0]->dreg);
11454 else
11455 g_assert (var->klass == klass);
11457 EMIT_NEW_VARLOADA (cfg, ins, var, &var->klass->byval_arg);
11458 sp [0] = ins;
11461 if (op == CEE_LDFLDA) {
11462 if (sp [0]->type == STACK_OBJ) {
11463 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, sp [0]->dreg, 0);
11464 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "NullReferenceException");
11467 dreg = alloc_ireg_mp (cfg);
11469 if (mini_is_gsharedvt_klass (klass)) {
11470 MonoInst *offset_ins;
11472 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
11473 /* The value is offset by 1 */
11474 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
11475 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
11476 } else {
11477 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, dreg, sp [0]->dreg, foffset);
11479 ins->klass = mono_class_from_mono_type (field->type);
11480 ins->type = STACK_MP;
11481 *sp++ = ins;
11482 } else {
11483 MonoInst *load;
11485 MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
11487 if (mini_is_gsharedvt_klass (klass)) {
11488 MonoInst *offset_ins;
11490 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
11491 /* The value is offset by 1 */
11492 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
11493 dreg = alloc_ireg_mp (cfg);
11494 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
11495 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, dreg, 0);
11496 } else {
11497 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, sp [0]->dreg, foffset);
11499 load->flags |= ins_flag;
11500 if (sp [0]->opcode != OP_LDADDR)
11501 load->flags |= MONO_INST_FAULT;
11502 *sp++ = load;
11506 if (is_instance) {
11507 ins_flag = 0;
11508 ip += 5;
11509 break;
11512 /* STATIC CASE */
11513 context_used = mini_class_check_context_used (cfg, klass);
11515 if (ftype->attrs & FIELD_ATTRIBUTE_LITERAL) {
11516 mono_error_set_field_load (&cfg->error, field->parent, field->name, "Using static instructions with literal field");
11517 CHECK_CFG_ERROR;
11520 /* The special_static_fields field is init'd in mono_class_vtable, so it needs
11521 * to be called here.
11523 if (!context_used && !(cfg->opt & MONO_OPT_SHARED)) {
11524 mono_class_vtable (cfg->domain, klass);
11525 CHECK_TYPELOAD (klass);
11527 mono_domain_lock (cfg->domain);
11528 if (cfg->domain->special_static_fields)
11529 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
11530 mono_domain_unlock (cfg->domain);
11532 is_special_static = mono_class_field_is_special_static (field);
11534 if (is_special_static && ((gsize)addr & 0x80000000) == 0)
11535 thread_ins = mono_get_thread_intrinsic (cfg);
11536 else
11537 thread_ins = NULL;
11539 /* Generate IR to compute the field address */
11540 if (is_special_static && ((gsize)addr & 0x80000000) == 0 && thread_ins && !(cfg->opt & MONO_OPT_SHARED) && !context_used) {
11542 * Fast access to TLS data
11543 * Inline version of get_thread_static_data () in
11544 * threads.c.
11546 guint32 offset;
11547 int idx, static_data_reg, array_reg, dreg;
11549 GSHAREDVT_FAILURE (op);
11551 MONO_ADD_INS (cfg->cbb, thread_ins);
11552 static_data_reg = alloc_ireg (cfg);
11553 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, static_data_reg, thread_ins->dreg, MONO_STRUCT_OFFSET (MonoInternalThread, static_data));
11555 if (cfg->compile_aot) {
11556 int offset_reg, offset2_reg, idx_reg;
11558 /* For TLS variables, this will return the TLS offset */
11559 EMIT_NEW_SFLDACONST (cfg, ins, field);
11560 offset_reg = ins->dreg;
11561 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, offset_reg, offset_reg, 0x7fffffff);
11562 idx_reg = alloc_ireg (cfg);
11563 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, idx_reg, offset_reg, 0x3f);
11564 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHL_IMM, idx_reg, idx_reg, sizeof (gpointer) == 8 ? 3 : 2);
11565 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, static_data_reg, static_data_reg, idx_reg);
11566 array_reg = alloc_ireg (cfg);
11567 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, static_data_reg, 0);
11568 offset2_reg = alloc_ireg (cfg);
11569 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHR_UN_IMM, offset2_reg, offset_reg, 6);
11570 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, offset2_reg, offset2_reg, 0x1ffffff);
11571 dreg = alloc_ireg (cfg);
11572 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, array_reg, offset2_reg);
11573 } else {
11574 offset = (gsize)addr & 0x7fffffff;
11575 idx = offset & 0x3f;
11577 array_reg = alloc_ireg (cfg);
11578 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, static_data_reg, idx * sizeof (gpointer));
11579 dreg = alloc_ireg (cfg);
11580 EMIT_NEW_BIALU_IMM (cfg, ins, OP_ADD_IMM, dreg, array_reg, ((offset >> 6) & 0x1ffffff));
11582 } else if ((cfg->opt & MONO_OPT_SHARED) ||
11583 (cfg->compile_aot && is_special_static) ||
11584 (context_used && is_special_static)) {
11585 MonoInst *iargs [2];
11587 g_assert (field->parent);
11588 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
11589 if (context_used) {
11590 iargs [1] = emit_get_rgctx_field (cfg, context_used,
11591 field, MONO_RGCTX_INFO_CLASS_FIELD);
11592 } else {
11593 EMIT_NEW_FIELDCONST (cfg, iargs [1], field);
11595 ins = mono_emit_jit_icall (cfg, mono_class_static_field_address, iargs);
11596 } else if (context_used) {
11597 MonoInst *static_data;
11600 g_print ("sharing static field access in %s.%s.%s - depth %d offset %d\n",
11601 method->klass->name_space, method->klass->name, method->name,
11602 depth, field->offset);
11605 if (mono_class_needs_cctor_run (klass, method))
11606 emit_class_init (cfg, klass);
11609 * The pointer we're computing here is
11611 * super_info.static_data + field->offset
11613 static_data = emit_get_rgctx_klass (cfg, context_used,
11614 klass, MONO_RGCTX_INFO_STATIC_DATA);
11616 if (mini_is_gsharedvt_klass (klass)) {
11617 MonoInst *offset_ins;
11619 offset_ins = emit_get_rgctx_field (cfg, context_used, field, MONO_RGCTX_INFO_FIELD_OFFSET);
11620 /* The value is offset by 1 */
11621 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
11622 dreg = alloc_ireg_mp (cfg);
11623 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, static_data->dreg, offset_ins->dreg);
11624 } else if (field->offset == 0) {
11625 ins = static_data;
11626 } else {
11627 int addr_reg = mono_alloc_preg (cfg);
11628 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, addr_reg, static_data->dreg, field->offset);
11630 } else if ((cfg->opt & MONO_OPT_SHARED) || (cfg->compile_aot && addr)) {
11631 MonoInst *iargs [2];
11633 g_assert (field->parent);
11634 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
11635 EMIT_NEW_FIELDCONST (cfg, iargs [1], field);
11636 ins = mono_emit_jit_icall (cfg, mono_class_static_field_address, iargs);
11637 } else {
11638 MonoVTable *vtable = NULL;
11640 if (!cfg->compile_aot)
11641 vtable = mono_class_vtable (cfg->domain, klass);
11642 CHECK_TYPELOAD (klass);
11644 if (!addr) {
11645 if (mini_field_access_needs_cctor_run (cfg, method, klass, vtable)) {
11646 if (!(g_slist_find (class_inits, klass))) {
11647 emit_class_init (cfg, klass);
11648 if (cfg->verbose_level > 2)
11649 printf ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, mono_field_get_name (field));
11650 class_inits = g_slist_prepend (class_inits, klass);
11652 } else {
11653 if (cfg->run_cctors) {
11654 /* This makes so that inline cannot trigger */
11655 /* .cctors: too many apps depend on them */
11656 /* running with a specific order... */
11657 g_assert (vtable);
11658 if (! vtable->initialized)
11659 INLINE_FAILURE ("class init");
11660 if (!mono_runtime_class_init_full (vtable, &cfg->error)) {
11661 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
11662 goto exception_exit;
11666 if (cfg->compile_aot)
11667 EMIT_NEW_SFLDACONST (cfg, ins, field);
11668 else {
11669 g_assert (vtable);
11670 addr = (char*)mono_vtable_get_static_field_data (vtable) + field->offset;
11671 g_assert (addr);
11672 EMIT_NEW_PCONST (cfg, ins, addr);
11674 } else {
11675 MonoInst *iargs [1];
11676 EMIT_NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
11677 ins = mono_emit_jit_icall (cfg, mono_get_special_static_data, iargs);
11681 /* Generate IR to do the actual load/store operation */
11683 if ((op == CEE_STFLD || op == CEE_STSFLD) && (ins_flag & MONO_INST_VOLATILE)) {
11684 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
11685 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
11688 if (op == CEE_LDSFLDA) {
11689 ins->klass = mono_class_from_mono_type (ftype);
11690 ins->type = STACK_PTR;
11691 *sp++ = ins;
11692 } else if (op == CEE_STSFLD) {
11693 MonoInst *store;
11695 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, ftype, ins->dreg, 0, store_val->dreg);
11696 store->flags |= ins_flag;
11697 } else {
11698 gboolean is_const = FALSE;
11699 MonoVTable *vtable = NULL;
11700 gpointer addr = NULL;
11702 if (!context_used) {
11703 vtable = mono_class_vtable (cfg->domain, klass);
11704 CHECK_TYPELOAD (klass);
11706 if ((ftype->attrs & FIELD_ATTRIBUTE_INIT_ONLY) && (((addr = mono_aot_readonly_field_override (field)) != NULL) ||
11707 (!context_used && !((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) && vtable->initialized))) {
11708 int ro_type = ftype->type;
11709 if (!addr)
11710 addr = (char*)mono_vtable_get_static_field_data (vtable) + field->offset;
11711 if (ro_type == MONO_TYPE_VALUETYPE && ftype->data.klass->enumtype) {
11712 ro_type = mono_class_enum_basetype (ftype->data.klass)->type;
11715 GSHAREDVT_FAILURE (op);
11717 /* printf ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, mono_field_get_name (field));*/
11718 is_const = TRUE;
11719 switch (ro_type) {
11720 case MONO_TYPE_BOOLEAN:
11721 case MONO_TYPE_U1:
11722 EMIT_NEW_ICONST (cfg, *sp, *((guint8 *)addr));
11723 sp++;
11724 break;
11725 case MONO_TYPE_I1:
11726 EMIT_NEW_ICONST (cfg, *sp, *((gint8 *)addr));
11727 sp++;
11728 break;
11729 case MONO_TYPE_CHAR:
11730 case MONO_TYPE_U2:
11731 EMIT_NEW_ICONST (cfg, *sp, *((guint16 *)addr));
11732 sp++;
11733 break;
11734 case MONO_TYPE_I2:
11735 EMIT_NEW_ICONST (cfg, *sp, *((gint16 *)addr));
11736 sp++;
11737 break;
11738 break;
11739 case MONO_TYPE_I4:
11740 EMIT_NEW_ICONST (cfg, *sp, *((gint32 *)addr));
11741 sp++;
11742 break;
11743 case MONO_TYPE_U4:
11744 EMIT_NEW_ICONST (cfg, *sp, *((guint32 *)addr));
11745 sp++;
11746 break;
11747 case MONO_TYPE_I:
11748 case MONO_TYPE_U:
11749 case MONO_TYPE_PTR:
11750 case MONO_TYPE_FNPTR:
11751 EMIT_NEW_PCONST (cfg, *sp, *((gpointer *)addr));
11752 type_to_eval_stack_type ((cfg), field->type, *sp);
11753 sp++;
11754 break;
11755 case MONO_TYPE_STRING:
11756 case MONO_TYPE_OBJECT:
11757 case MONO_TYPE_CLASS:
11758 case MONO_TYPE_SZARRAY:
11759 case MONO_TYPE_ARRAY:
11760 if (!mono_gc_is_moving ()) {
11761 EMIT_NEW_PCONST (cfg, *sp, *((gpointer *)addr));
11762 type_to_eval_stack_type ((cfg), field->type, *sp);
11763 sp++;
11764 } else {
11765 is_const = FALSE;
11767 break;
11768 case MONO_TYPE_I8:
11769 case MONO_TYPE_U8:
11770 EMIT_NEW_I8CONST (cfg, *sp, *((gint64 *)addr));
11771 sp++;
11772 break;
11773 case MONO_TYPE_R4:
11774 case MONO_TYPE_R8:
11775 case MONO_TYPE_VALUETYPE:
11776 default:
11777 is_const = FALSE;
11778 break;
11782 if (!is_const) {
11783 MonoInst *load;
11785 CHECK_STACK_OVF (1);
11787 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, ins->dreg, 0);
11788 load->flags |= ins_flag;
11789 ins_flag = 0;
11790 *sp++ = load;
11794 if ((op == CEE_LDFLD || op == CEE_LDSFLD) && (ins_flag & MONO_INST_VOLATILE)) {
11795 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
11796 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
11799 ins_flag = 0;
11800 ip += 5;
11801 break;
11803 case CEE_STOBJ:
11804 CHECK_STACK (2);
11805 sp -= 2;
11806 CHECK_OPSIZE (5);
11807 token = read32 (ip + 1);
11808 klass = mini_get_class (method, token, generic_context);
11809 CHECK_TYPELOAD (klass);
11810 if (ins_flag & MONO_INST_VOLATILE) {
11811 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
11812 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
11814 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
11815 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0, sp [1]->dreg);
11816 ins->flags |= ins_flag;
11817 if (cfg->gen_write_barriers && cfg->method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER &&
11818 generic_class_is_reference_type (cfg, klass)) {
11819 /* insert call to write barrier */
11820 emit_write_barrier (cfg, sp [0], sp [1]);
11822 ins_flag = 0;
11823 ip += 5;
11824 inline_costs += 1;
11825 break;
11828 * Array opcodes
11830 case CEE_NEWARR: {
11831 MonoInst *len_ins;
11832 const char *data_ptr;
11833 int data_size = 0;
11834 guint32 field_token;
11836 CHECK_STACK (1);
11837 --sp;
11839 CHECK_OPSIZE (5);
11840 token = read32 (ip + 1);
11842 klass = mini_get_class (method, token, generic_context);
11843 CHECK_TYPELOAD (klass);
11845 context_used = mini_class_check_context_used (cfg, klass);
11847 if (sp [0]->type == STACK_I8 || (SIZEOF_VOID_P == 8 && sp [0]->type == STACK_PTR)) {
11848 MONO_INST_NEW (cfg, ins, OP_LCONV_TO_OVF_U4);
11849 ins->sreg1 = sp [0]->dreg;
11850 ins->type = STACK_I4;
11851 ins->dreg = alloc_ireg (cfg);
11852 MONO_ADD_INS (cfg->cbb, ins);
11853 *sp = mono_decompose_opcode (cfg, ins);
11856 if (context_used) {
11857 MonoInst *args [3];
11858 MonoClass *array_class = mono_array_class_get (klass, 1);
11859 MonoMethod *managed_alloc = mono_gc_get_managed_array_allocator (array_class);
11861 /* FIXME: Use OP_NEWARR and decompose later to help abcrem */
11863 /* vtable */
11864 args [0] = emit_get_rgctx_klass (cfg, context_used,
11865 array_class, MONO_RGCTX_INFO_VTABLE);
11866 /* array len */
11867 args [1] = sp [0];
11869 if (managed_alloc)
11870 ins = mono_emit_method_call (cfg, managed_alloc, args, NULL);
11871 else
11872 ins = mono_emit_jit_icall (cfg, ves_icall_array_new_specific, args);
11873 } else {
11874 if (cfg->opt & MONO_OPT_SHARED) {
11875 /* Decompose now to avoid problems with references to the domainvar */
11876 MonoInst *iargs [3];
11878 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
11879 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
11880 iargs [2] = sp [0];
11882 ins = mono_emit_jit_icall (cfg, ves_icall_array_new, iargs);
11883 } else {
11884 /* Decompose later since it is needed by abcrem */
11885 MonoClass *array_type = mono_array_class_get (klass, 1);
11886 mono_class_vtable (cfg->domain, array_type);
11887 CHECK_TYPELOAD (array_type);
11889 MONO_INST_NEW (cfg, ins, OP_NEWARR);
11890 ins->dreg = alloc_ireg_ref (cfg);
11891 ins->sreg1 = sp [0]->dreg;
11892 ins->inst_newa_class = klass;
11893 ins->type = STACK_OBJ;
11894 ins->klass = array_type;
11895 MONO_ADD_INS (cfg->cbb, ins);
11896 cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
11897 cfg->cbb->has_array_access = TRUE;
11899 /* Needed so mono_emit_load_get_addr () gets called */
11900 mono_get_got_var (cfg);
11904 len_ins = sp [0];
11905 ip += 5;
11906 *sp++ = ins;
11907 inline_costs += 1;
11910 * we inline/optimize the initialization sequence if possible.
11911 * we should also allocate the array as not cleared, since we spend as much time clearing to 0 as initializing
11912 * for small sizes open code the memcpy
11913 * ensure the rva field is big enough
11915 if ((cfg->opt & MONO_OPT_INTRINS) && ip + 6 < end && ip_in_bb (cfg, cfg->cbb, ip + 6) && (len_ins->opcode == OP_ICONST) && (data_ptr = initialize_array_data (method, cfg->compile_aot, ip, klass, len_ins->inst_c0, &data_size, &field_token))) {
11916 MonoMethod *memcpy_method = get_memcpy_method ();
11917 MonoInst *iargs [3];
11918 int add_reg = alloc_ireg_mp (cfg);
11920 EMIT_NEW_BIALU_IMM (cfg, iargs [0], OP_PADD_IMM, add_reg, ins->dreg, MONO_STRUCT_OFFSET (MonoArray, vector));
11921 if (cfg->compile_aot) {
11922 EMIT_NEW_AOTCONST_TOKEN (cfg, iargs [1], MONO_PATCH_INFO_RVA, method->klass->image, GPOINTER_TO_UINT(field_token), STACK_PTR, NULL);
11923 } else {
11924 EMIT_NEW_PCONST (cfg, iargs [1], (char*)data_ptr);
11926 EMIT_NEW_ICONST (cfg, iargs [2], data_size);
11927 mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
11928 ip += 11;
11931 break;
11933 case CEE_LDLEN:
11934 CHECK_STACK (1);
11935 --sp;
11936 if (sp [0]->type != STACK_OBJ)
11937 UNVERIFIED;
11939 MONO_INST_NEW (cfg, ins, OP_LDLEN);
11940 ins->dreg = alloc_preg (cfg);
11941 ins->sreg1 = sp [0]->dreg;
11942 ins->type = STACK_I4;
11943 /* This flag will be inherited by the decomposition */
11944 ins->flags |= MONO_INST_FAULT;
11945 MONO_ADD_INS (cfg->cbb, ins);
11946 cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
11947 cfg->cbb->has_array_access = TRUE;
11948 ip ++;
11949 *sp++ = ins;
11950 break;
11951 case CEE_LDELEMA:
11952 CHECK_STACK (2);
11953 sp -= 2;
11954 CHECK_OPSIZE (5);
11955 if (sp [0]->type != STACK_OBJ)
11956 UNVERIFIED;
11958 cfg->flags |= MONO_CFG_HAS_LDELEMA;
11960 klass = mini_get_class (method, read32 (ip + 1), generic_context);
11961 CHECK_TYPELOAD (klass);
11962 /* we need to make sure that this array is exactly the type it needs
11963 * to be for correctness. the wrappers are lax with their usage
11964 * so we need to ignore them here
11966 if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE && !readonly) {
11967 MonoClass *array_class = mono_array_class_get (klass, 1);
11968 mini_emit_check_array_type (cfg, sp [0], array_class);
11969 CHECK_TYPELOAD (array_class);
11972 readonly = FALSE;
11973 ins = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
11974 *sp++ = ins;
11975 ip += 5;
11976 break;
11977 case CEE_LDELEM:
11978 case CEE_LDELEM_I1:
11979 case CEE_LDELEM_U1:
11980 case CEE_LDELEM_I2:
11981 case CEE_LDELEM_U2:
11982 case CEE_LDELEM_I4:
11983 case CEE_LDELEM_U4:
11984 case CEE_LDELEM_I8:
11985 case CEE_LDELEM_I:
11986 case CEE_LDELEM_R4:
11987 case CEE_LDELEM_R8:
11988 case CEE_LDELEM_REF: {
11989 MonoInst *addr;
11991 CHECK_STACK (2);
11992 sp -= 2;
11994 if (*ip == CEE_LDELEM) {
11995 CHECK_OPSIZE (5);
11996 token = read32 (ip + 1);
11997 klass = mini_get_class (method, token, generic_context);
11998 CHECK_TYPELOAD (klass);
11999 mono_class_init (klass);
12001 else
12002 klass = array_access_to_klass (*ip);
12004 if (sp [0]->type != STACK_OBJ)
12005 UNVERIFIED;
12007 cfg->flags |= MONO_CFG_HAS_LDELEMA;
12009 if (mini_is_gsharedvt_variable_klass (klass)) {
12010 // FIXME-VT: OP_ICONST optimization
12011 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
12012 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
12013 ins->opcode = OP_LOADV_MEMBASE;
12014 } else if (sp [1]->opcode == OP_ICONST) {
12015 int array_reg = sp [0]->dreg;
12016 int index_reg = sp [1]->dreg;
12017 int offset = (mono_class_array_element_size (klass) * sp [1]->inst_c0) + MONO_STRUCT_OFFSET (MonoArray, vector);
12019 if (SIZEOF_REGISTER == 8 && COMPILE_LLVM (cfg))
12020 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, index_reg, index_reg);
12022 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index_reg);
12023 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, array_reg, offset);
12024 } else {
12025 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
12026 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
12028 *sp++ = ins;
12029 if (*ip == CEE_LDELEM)
12030 ip += 5;
12031 else
12032 ++ip;
12033 break;
12035 case CEE_STELEM_I:
12036 case CEE_STELEM_I1:
12037 case CEE_STELEM_I2:
12038 case CEE_STELEM_I4:
12039 case CEE_STELEM_I8:
12040 case CEE_STELEM_R4:
12041 case CEE_STELEM_R8:
12042 case CEE_STELEM_REF:
12043 case CEE_STELEM: {
12044 CHECK_STACK (3);
12045 sp -= 3;
12047 cfg->flags |= MONO_CFG_HAS_LDELEMA;
12049 if (*ip == CEE_STELEM) {
12050 CHECK_OPSIZE (5);
12051 token = read32 (ip + 1);
12052 klass = mini_get_class (method, token, generic_context);
12053 CHECK_TYPELOAD (klass);
12054 mono_class_init (klass);
12056 else
12057 klass = array_access_to_klass (*ip);
12059 if (sp [0]->type != STACK_OBJ)
12060 UNVERIFIED;
12062 emit_array_store (cfg, klass, sp, TRUE);
12064 if (*ip == CEE_STELEM)
12065 ip += 5;
12066 else
12067 ++ip;
12068 inline_costs += 1;
12069 break;
12071 case CEE_CKFINITE: {
12072 CHECK_STACK (1);
12073 --sp;
12075 if (cfg->llvm_only) {
12076 MonoInst *iargs [1];
12078 iargs [0] = sp [0];
12079 *sp++ = mono_emit_jit_icall (cfg, mono_ckfinite, iargs);
12080 } else {
12081 MONO_INST_NEW (cfg, ins, OP_CKFINITE);
12082 ins->sreg1 = sp [0]->dreg;
12083 ins->dreg = alloc_freg (cfg);
12084 ins->type = STACK_R8;
12085 MONO_ADD_INS (cfg->cbb, ins);
12087 *sp++ = mono_decompose_opcode (cfg, ins);
12090 ++ip;
12091 break;
12093 case CEE_REFANYVAL: {
12094 MonoInst *src_var, *src;
12096 int klass_reg = alloc_preg (cfg);
12097 int dreg = alloc_preg (cfg);
12099 GSHAREDVT_FAILURE (*ip);
12101 CHECK_STACK (1);
12102 MONO_INST_NEW (cfg, ins, *ip);
12103 --sp;
12104 CHECK_OPSIZE (5);
12105 klass = mini_get_class (method, read32 (ip + 1), generic_context);
12106 CHECK_TYPELOAD (klass);
12108 context_used = mini_class_check_context_used (cfg, klass);
12110 // FIXME:
12111 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
12112 if (!src_var)
12113 src_var = mono_compile_create_var_for_vreg (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL, sp [0]->dreg);
12114 EMIT_NEW_VARLOADA (cfg, src, src_var, src_var->inst_vtype);
12115 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass));
12117 if (context_used) {
12118 MonoInst *klass_ins;
12120 klass_ins = emit_get_rgctx_klass (cfg, context_used,
12121 klass, MONO_RGCTX_INFO_KLASS);
12123 // FIXME:
12124 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, klass_ins->dreg);
12125 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
12126 } else {
12127 mini_emit_class_check (cfg, klass_reg, klass);
12129 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, value));
12130 ins->type = STACK_MP;
12131 ins->klass = klass;
12132 *sp++ = ins;
12133 ip += 5;
12134 break;
12136 case CEE_MKREFANY: {
12137 MonoInst *loc, *addr;
12139 GSHAREDVT_FAILURE (*ip);
12141 CHECK_STACK (1);
12142 MONO_INST_NEW (cfg, ins, *ip);
12143 --sp;
12144 CHECK_OPSIZE (5);
12145 klass = mini_get_class (method, read32 (ip + 1), generic_context);
12146 CHECK_TYPELOAD (klass);
12148 context_used = mini_class_check_context_used (cfg, klass);
12150 loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
12151 EMIT_NEW_TEMPLOADA (cfg, addr, loc->inst_c0);
12153 if (context_used) {
12154 MonoInst *const_ins;
12155 int type_reg = alloc_preg (cfg);
12157 const_ins = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
12158 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass), const_ins->dreg);
12159 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, type_reg, const_ins->dreg, MONO_STRUCT_OFFSET (MonoClass, byval_arg));
12160 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type), type_reg);
12161 } else if (cfg->compile_aot) {
12162 int const_reg = alloc_preg (cfg);
12163 int type_reg = alloc_preg (cfg);
12165 MONO_EMIT_NEW_CLASSCONST (cfg, const_reg, klass);
12166 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass), const_reg);
12167 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, type_reg, const_reg, MONO_STRUCT_OFFSET (MonoClass, byval_arg));
12168 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type), type_reg);
12169 } else {
12170 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREP_MEMBASE_IMM, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type), &klass->byval_arg);
12171 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREP_MEMBASE_IMM, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass), klass);
12173 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, value), sp [0]->dreg);
12175 EMIT_NEW_TEMPLOAD (cfg, ins, loc->inst_c0);
12176 ins->type = STACK_VTYPE;
12177 ins->klass = mono_defaults.typed_reference_class;
12178 *sp++ = ins;
12179 ip += 5;
12180 break;
12182 case CEE_LDTOKEN: {
12183 gpointer handle;
12184 MonoClass *handle_class;
12186 CHECK_STACK_OVF (1);
12188 CHECK_OPSIZE (5);
12189 n = read32 (ip + 1);
12191 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD ||
12192 method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
12193 handle = mono_method_get_wrapper_data (method, n);
12194 handle_class = (MonoClass *)mono_method_get_wrapper_data (method, n + 1);
12195 if (handle_class == mono_defaults.typehandle_class)
12196 handle = &((MonoClass*)handle)->byval_arg;
12198 else {
12199 handle = mono_ldtoken_checked (image, n, &handle_class, generic_context, &cfg->error);
12200 CHECK_CFG_ERROR;
12202 if (!handle)
12203 LOAD_ERROR;
12204 mono_class_init (handle_class);
12205 if (cfg->gshared) {
12206 if (mono_metadata_token_table (n) == MONO_TABLE_TYPEDEF ||
12207 mono_metadata_token_table (n) == MONO_TABLE_TYPEREF) {
12208 /* This case handles ldtoken
12209 of an open type, like for
12210 typeof(Gen<>). */
12211 context_used = 0;
12212 } else if (handle_class == mono_defaults.typehandle_class) {
12213 context_used = mini_class_check_context_used (cfg, mono_class_from_mono_type ((MonoType *)handle));
12214 } else if (handle_class == mono_defaults.fieldhandle_class)
12215 context_used = mini_class_check_context_used (cfg, ((MonoClassField*)handle)->parent);
12216 else if (handle_class == mono_defaults.methodhandle_class)
12217 context_used = mini_method_check_context_used (cfg, (MonoMethod *)handle);
12218 else
12219 g_assert_not_reached ();
12222 if ((cfg->opt & MONO_OPT_SHARED) &&
12223 method->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD &&
12224 method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED) {
12225 MonoInst *addr, *vtvar, *iargs [3];
12226 int method_context_used;
12228 method_context_used = mini_method_check_context_used (cfg, method);
12230 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
12232 EMIT_NEW_IMAGECONST (cfg, iargs [0], image);
12233 EMIT_NEW_ICONST (cfg, iargs [1], n);
12234 if (method_context_used) {
12235 iargs [2] = emit_get_rgctx_method (cfg, method_context_used,
12236 method, MONO_RGCTX_INFO_METHOD);
12237 ins = mono_emit_jit_icall (cfg, mono_ldtoken_wrapper_generic_shared, iargs);
12238 } else {
12239 EMIT_NEW_PCONST (cfg, iargs [2], generic_context);
12240 ins = mono_emit_jit_icall (cfg, mono_ldtoken_wrapper, iargs);
12242 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
12244 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, ins->dreg);
12246 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
12247 } else {
12248 if ((ip + 5 < end) && ip_in_bb (cfg, cfg->cbb, ip + 5) &&
12249 ((ip [5] == CEE_CALL) || (ip [5] == CEE_CALLVIRT)) &&
12250 (cmethod = mini_get_method (cfg, method, read32 (ip + 6), NULL, generic_context)) &&
12251 (cmethod->klass == mono_defaults.systemtype_class) &&
12252 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
12253 MonoClass *tclass = mono_class_from_mono_type ((MonoType *)handle);
12255 mono_class_init (tclass);
12256 if (context_used) {
12257 ins = emit_get_rgctx_klass (cfg, context_used,
12258 tclass, MONO_RGCTX_INFO_REFLECTION_TYPE);
12259 } else if (cfg->compile_aot) {
12260 if (method->wrapper_type) {
12261 mono_error_init (&error); //got to do it since there are multiple conditionals below
12262 if (mono_class_get_checked (tclass->image, tclass->type_token, &error) == tclass && !generic_context) {
12263 /* Special case for static synchronized wrappers */
12264 EMIT_NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, tclass->image, tclass->type_token, generic_context);
12265 } else {
12266 mono_error_cleanup (&error); /* FIXME don't swallow the error */
12267 /* FIXME: n is not a normal token */
12268 DISABLE_AOT (cfg);
12269 EMIT_NEW_PCONST (cfg, ins, NULL);
12271 } else {
12272 EMIT_NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n, generic_context);
12274 } else {
12275 MonoReflectionType *rt = mono_type_get_object_checked (cfg->domain, (MonoType *)handle, &cfg->error);
12276 CHECK_CFG_ERROR;
12277 EMIT_NEW_PCONST (cfg, ins, rt);
12279 ins->type = STACK_OBJ;
12280 ins->klass = cmethod->klass;
12281 ip += 5;
12282 } else {
12283 MonoInst *addr, *vtvar;
12285 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
12287 if (context_used) {
12288 if (handle_class == mono_defaults.typehandle_class) {
12289 ins = emit_get_rgctx_klass (cfg, context_used,
12290 mono_class_from_mono_type ((MonoType *)handle),
12291 MONO_RGCTX_INFO_TYPE);
12292 } else if (handle_class == mono_defaults.methodhandle_class) {
12293 ins = emit_get_rgctx_method (cfg, context_used,
12294 (MonoMethod *)handle, MONO_RGCTX_INFO_METHOD);
12295 } else if (handle_class == mono_defaults.fieldhandle_class) {
12296 ins = emit_get_rgctx_field (cfg, context_used,
12297 (MonoClassField *)handle, MONO_RGCTX_INFO_CLASS_FIELD);
12298 } else {
12299 g_assert_not_reached ();
12301 } else if (cfg->compile_aot) {
12302 EMIT_NEW_LDTOKENCONST (cfg, ins, image, n, generic_context);
12303 } else {
12304 EMIT_NEW_PCONST (cfg, ins, handle);
12306 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
12307 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, ins->dreg);
12308 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
12312 *sp++ = ins;
12313 ip += 5;
12314 break;
12316 case CEE_THROW:
12317 CHECK_STACK (1);
12318 MONO_INST_NEW (cfg, ins, OP_THROW);
12319 --sp;
12320 ins->sreg1 = sp [0]->dreg;
12321 ip++;
12322 cfg->cbb->out_of_line = TRUE;
12323 MONO_ADD_INS (cfg->cbb, ins);
12324 MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
12325 MONO_ADD_INS (cfg->cbb, ins);
12326 sp = stack_start;
12328 link_bblock (cfg, cfg->cbb, end_bblock);
12329 start_new_bblock = 1;
12330 /* This can complicate code generation for llvm since the return value might not be defined */
12331 if (COMPILE_LLVM (cfg))
12332 INLINE_FAILURE ("throw");
12333 break;
12334 case CEE_ENDFINALLY:
12335 /* mono_save_seq_point_info () depends on this */
12336 if (sp != stack_start)
12337 emit_seq_point (cfg, method, ip, FALSE, FALSE);
12338 MONO_INST_NEW (cfg, ins, OP_ENDFINALLY);
12339 MONO_ADD_INS (cfg->cbb, ins);
12340 ip++;
12341 start_new_bblock = 1;
12344 * Control will leave the method so empty the stack, otherwise
12345 * the next basic block will start with a nonempty stack.
12347 while (sp != stack_start) {
12348 sp--;
12350 break;
12351 case CEE_LEAVE:
12352 case CEE_LEAVE_S: {
12353 GList *handlers;
12355 if (*ip == CEE_LEAVE) {
12356 CHECK_OPSIZE (5);
12357 target = ip + 5 + (gint32)read32(ip + 1);
12358 } else {
12359 CHECK_OPSIZE (2);
12360 target = ip + 2 + (signed char)(ip [1]);
12363 /* empty the stack */
12364 while (sp != stack_start) {
12365 sp--;
12369 * If this leave statement is in a catch block, check for a
12370 * pending exception, and rethrow it if necessary.
12371 * We avoid doing this in runtime invoke wrappers, since those are called
12372 * by native code which excepts the wrapper to catch all exceptions.
12374 for (i = 0; i < header->num_clauses; ++i) {
12375 MonoExceptionClause *clause = &header->clauses [i];
12378 * Use <= in the final comparison to handle clauses with multiple
12379 * leave statements, like in bug #78024.
12380 * The ordering of the exception clauses guarantees that we find the
12381 * innermost clause.
12383 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code) && (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) && (ip - header->code + ((*ip == CEE_LEAVE) ? 5 : 2)) <= (clause->handler_offset + clause->handler_len) && method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE) {
12384 MonoInst *exc_ins;
12385 MonoBasicBlock *dont_throw;
12388 MonoInst *load;
12390 NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, clause->handler_offset)->inst_c0);
12393 exc_ins = mono_emit_jit_icall (cfg, mono_thread_get_undeniable_exception, NULL);
12395 NEW_BBLOCK (cfg, dont_throw);
12398 * Currently, we always rethrow the abort exception, despite the
12399 * fact that this is not correct. See thread6.cs for an example.
12400 * But propagating the abort exception is more important than
12401 * getting the sematics right.
12403 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, exc_ins->dreg, 0);
12404 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, dont_throw);
12405 MONO_EMIT_NEW_UNALU (cfg, OP_THROW, -1, exc_ins->dreg);
12407 MONO_START_BB (cfg, dont_throw);
12411 #ifdef ENABLE_LLVM
12412 cfg->cbb->try_end = (intptr_t)(ip - header->code);
12413 #endif
12415 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
12416 GList *tmp;
12417 MonoExceptionClause *clause;
12419 for (tmp = handlers; tmp; tmp = tmp->next) {
12420 clause = (MonoExceptionClause *)tmp->data;
12421 tblock = cfg->cil_offset_to_bb [clause->handler_offset];
12422 g_assert (tblock);
12423 link_bblock (cfg, cfg->cbb, tblock);
12424 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
12425 ins->inst_target_bb = tblock;
12426 ins->inst_eh_block = clause;
12427 MONO_ADD_INS (cfg->cbb, ins);
12428 cfg->cbb->has_call_handler = 1;
12429 if (COMPILE_LLVM (cfg)) {
12430 MonoBasicBlock *target_bb;
12433 * Link the finally bblock with the target, since it will
12434 * conceptually branch there.
12436 GET_BBLOCK (cfg, tblock, cfg->cil_start + clause->handler_offset + clause->handler_len - 1);
12437 GET_BBLOCK (cfg, target_bb, target);
12438 link_bblock (cfg, tblock, target_bb);
12441 g_list_free (handlers);
12444 MONO_INST_NEW (cfg, ins, OP_BR);
12445 MONO_ADD_INS (cfg->cbb, ins);
12446 GET_BBLOCK (cfg, tblock, target);
12447 link_bblock (cfg, cfg->cbb, tblock);
12448 ins->inst_target_bb = tblock;
12450 start_new_bblock = 1;
12452 if (*ip == CEE_LEAVE)
12453 ip += 5;
12454 else
12455 ip += 2;
12457 break;
12461 * Mono specific opcodes
12463 case MONO_CUSTOM_PREFIX: {
12465 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
12467 CHECK_OPSIZE (2);
12468 switch (ip [1]) {
12469 case CEE_MONO_ICALL: {
12470 gpointer func;
12471 MonoJitICallInfo *info;
12473 token = read32 (ip + 2);
12474 func = mono_method_get_wrapper_data (method, token);
12475 info = mono_find_jit_icall_by_addr (func);
12476 if (!info)
12477 g_error ("Could not find icall address in wrapper %s", mono_method_full_name (method, 1));
12478 g_assert (info);
12480 CHECK_STACK (info->sig->param_count);
12481 sp -= info->sig->param_count;
12483 ins = mono_emit_jit_icall (cfg, info->func, sp);
12484 if (!MONO_TYPE_IS_VOID (info->sig->ret))
12485 *sp++ = ins;
12487 ip += 6;
12488 inline_costs += 10 * num_calls++;
12490 break;
12492 case CEE_MONO_LDPTR_CARD_TABLE:
12493 case CEE_MONO_LDPTR_NURSERY_START:
12494 case CEE_MONO_LDPTR_NURSERY_BITS:
12495 case CEE_MONO_LDPTR_INT_REQ_FLAG: {
12496 CHECK_STACK_OVF (1);
12498 switch (ip [1]) {
12499 case CEE_MONO_LDPTR_CARD_TABLE:
12500 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_CARD_TABLE_ADDR, NULL);
12501 break;
12502 case CEE_MONO_LDPTR_NURSERY_START:
12503 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_NURSERY_START, NULL);
12504 break;
12505 case CEE_MONO_LDPTR_NURSERY_BITS:
12506 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_NURSERY_BITS, NULL);
12507 break;
12508 case CEE_MONO_LDPTR_INT_REQ_FLAG:
12509 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG, NULL);
12510 break;
12513 *sp++ = ins;
12514 ip += 2;
12515 inline_costs += 10 * num_calls++;
12516 break;
12518 case CEE_MONO_LDPTR: {
12519 gpointer ptr;
12521 CHECK_STACK_OVF (1);
12522 CHECK_OPSIZE (6);
12523 token = read32 (ip + 2);
12525 ptr = mono_method_get_wrapper_data (method, token);
12526 EMIT_NEW_PCONST (cfg, ins, ptr);
12527 *sp++ = ins;
12528 ip += 6;
12529 inline_costs += 10 * num_calls++;
12530 /* Can't embed random pointers into AOT code */
12531 DISABLE_AOT (cfg);
12532 break;
12534 case CEE_MONO_JIT_ICALL_ADDR: {
12535 MonoJitICallInfo *callinfo;
12536 gpointer ptr;
12538 CHECK_STACK_OVF (1);
12539 CHECK_OPSIZE (6);
12540 token = read32 (ip + 2);
12542 ptr = mono_method_get_wrapper_data (method, token);
12543 callinfo = mono_find_jit_icall_by_addr (ptr);
12544 g_assert (callinfo);
12545 EMIT_NEW_JIT_ICALL_ADDRCONST (cfg, ins, (char*)callinfo->name);
12546 *sp++ = ins;
12547 ip += 6;
12548 inline_costs += 10 * num_calls++;
12549 break;
12551 case CEE_MONO_ICALL_ADDR: {
12552 MonoMethod *cmethod;
12553 gpointer ptr;
12555 CHECK_STACK_OVF (1);
12556 CHECK_OPSIZE (6);
12557 token = read32 (ip + 2);
12559 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
12561 if (cfg->compile_aot) {
12562 EMIT_NEW_AOTCONST (cfg, ins, MONO_PATCH_INFO_ICALL_ADDR, cmethod);
12563 } else {
12564 ptr = mono_lookup_internal_call (cmethod);
12565 g_assert (ptr);
12566 EMIT_NEW_PCONST (cfg, ins, ptr);
12568 *sp++ = ins;
12569 ip += 6;
12570 break;
12572 case CEE_MONO_VTADDR: {
12573 MonoInst *src_var, *src;
12575 CHECK_STACK (1);
12576 --sp;
12578 // FIXME:
12579 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
12580 EMIT_NEW_VARLOADA ((cfg), (src), src_var, src_var->inst_vtype);
12581 *sp++ = src;
12582 ip += 2;
12583 break;
12585 case CEE_MONO_NEWOBJ: {
12586 MonoInst *iargs [2];
12588 CHECK_STACK_OVF (1);
12589 CHECK_OPSIZE (6);
12590 token = read32 (ip + 2);
12591 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
12592 mono_class_init (klass);
12593 NEW_DOMAINCONST (cfg, iargs [0]);
12594 MONO_ADD_INS (cfg->cbb, iargs [0]);
12595 NEW_CLASSCONST (cfg, iargs [1], klass);
12596 MONO_ADD_INS (cfg->cbb, iargs [1]);
12597 *sp++ = mono_emit_jit_icall (cfg, ves_icall_object_new, iargs);
12598 ip += 6;
12599 inline_costs += 10 * num_calls++;
12600 break;
12602 case CEE_MONO_OBJADDR:
12603 CHECK_STACK (1);
12604 --sp;
12605 MONO_INST_NEW (cfg, ins, OP_MOVE);
12606 ins->dreg = alloc_ireg_mp (cfg);
12607 ins->sreg1 = sp [0]->dreg;
12608 ins->type = STACK_MP;
12609 MONO_ADD_INS (cfg->cbb, ins);
12610 *sp++ = ins;
12611 ip += 2;
12612 break;
12613 case CEE_MONO_LDNATIVEOBJ:
12615 * Similar to LDOBJ, but instead load the unmanaged
12616 * representation of the vtype to the stack.
12618 CHECK_STACK (1);
12619 CHECK_OPSIZE (6);
12620 --sp;
12621 token = read32 (ip + 2);
12622 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
12623 g_assert (klass->valuetype);
12624 mono_class_init (klass);
12627 MonoInst *src, *dest, *temp;
12629 src = sp [0];
12630 temp = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
12631 temp->backend.is_pinvoke = 1;
12632 EMIT_NEW_TEMPLOADA (cfg, dest, temp->inst_c0);
12633 mini_emit_stobj (cfg, dest, src, klass, TRUE);
12635 EMIT_NEW_TEMPLOAD (cfg, dest, temp->inst_c0);
12636 dest->type = STACK_VTYPE;
12637 dest->klass = klass;
12639 *sp ++ = dest;
12640 ip += 6;
12642 break;
12643 case CEE_MONO_RETOBJ: {
12645 * Same as RET, but return the native representation of a vtype
12646 * to the caller.
12648 g_assert (cfg->ret);
12649 g_assert (mono_method_signature (method)->pinvoke);
12650 CHECK_STACK (1);
12651 --sp;
12653 CHECK_OPSIZE (6);
12654 token = read32 (ip + 2);
12655 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
12657 if (!cfg->vret_addr) {
12658 g_assert (cfg->ret_var_is_local);
12660 EMIT_NEW_VARLOADA (cfg, ins, cfg->ret, cfg->ret->inst_vtype);
12661 } else {
12662 EMIT_NEW_RETLOADA (cfg, ins);
12664 mini_emit_stobj (cfg, ins, sp [0], klass, TRUE);
12666 if (sp != stack_start)
12667 UNVERIFIED;
12669 MONO_INST_NEW (cfg, ins, OP_BR);
12670 ins->inst_target_bb = end_bblock;
12671 MONO_ADD_INS (cfg->cbb, ins);
12672 link_bblock (cfg, cfg->cbb, end_bblock);
12673 start_new_bblock = 1;
12674 ip += 6;
12675 break;
12677 case CEE_MONO_CISINST:
12678 case CEE_MONO_CCASTCLASS: {
12679 int token;
12680 CHECK_STACK (1);
12681 --sp;
12682 CHECK_OPSIZE (6);
12683 token = read32 (ip + 2);
12684 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
12685 if (ip [1] == CEE_MONO_CISINST)
12686 ins = handle_cisinst (cfg, klass, sp [0]);
12687 else
12688 ins = handle_ccastclass (cfg, klass, sp [0]);
12689 *sp++ = ins;
12690 ip += 6;
12691 break;
12693 case CEE_MONO_SAVE_LMF:
12694 case CEE_MONO_RESTORE_LMF:
12695 ip += 2;
12696 break;
12697 case CEE_MONO_CLASSCONST:
12698 CHECK_STACK_OVF (1);
12699 CHECK_OPSIZE (6);
12700 token = read32 (ip + 2);
12701 EMIT_NEW_CLASSCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
12702 *sp++ = ins;
12703 ip += 6;
12704 inline_costs += 10 * num_calls++;
12705 break;
12706 case CEE_MONO_NOT_TAKEN:
12707 cfg->cbb->out_of_line = TRUE;
12708 ip += 2;
12709 break;
12710 case CEE_MONO_TLS: {
12711 MonoTlsKey key;
12713 CHECK_STACK_OVF (1);
12714 CHECK_OPSIZE (6);
12715 key = (MonoTlsKey)read32 (ip + 2);
12716 g_assert (key < TLS_KEY_NUM);
12718 ins = mono_create_tls_get (cfg, key);
12719 if (!ins) {
12720 if (cfg->compile_aot) {
12721 DISABLE_AOT (cfg);
12722 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
12723 ins->dreg = alloc_preg (cfg);
12724 ins->type = STACK_PTR;
12725 } else {
12726 g_assert_not_reached ();
12729 ins->type = STACK_PTR;
12730 MONO_ADD_INS (cfg->cbb, ins);
12731 *sp++ = ins;
12732 ip += 6;
12733 break;
12735 case CEE_MONO_DYN_CALL: {
12736 MonoCallInst *call;
12738 /* It would be easier to call a trampoline, but that would put an
12739 * extra frame on the stack, confusing exception handling. So
12740 * implement it inline using an opcode for now.
12743 if (!cfg->dyn_call_var) {
12744 cfg->dyn_call_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
12745 /* prevent it from being register allocated */
12746 cfg->dyn_call_var->flags |= MONO_INST_VOLATILE;
12749 /* Has to use a call inst since it local regalloc expects it */
12750 MONO_INST_NEW_CALL (cfg, call, OP_DYN_CALL);
12751 ins = (MonoInst*)call;
12752 sp -= 2;
12753 ins->sreg1 = sp [0]->dreg;
12754 ins->sreg2 = sp [1]->dreg;
12755 MONO_ADD_INS (cfg->cbb, ins);
12757 cfg->param_area = MAX (cfg->param_area, cfg->backend->dyn_call_param_area);
12759 ip += 2;
12760 inline_costs += 10 * num_calls++;
12762 break;
12764 case CEE_MONO_MEMORY_BARRIER: {
12765 CHECK_OPSIZE (6);
12766 emit_memory_barrier (cfg, (int)read32 (ip + 2));
12767 ip += 6;
12768 break;
12770 case CEE_MONO_JIT_ATTACH: {
12771 MonoInst *args [16], *domain_ins;
12772 MonoInst *ad_ins, *jit_tls_ins;
12773 MonoBasicBlock *next_bb = NULL, *call_bb = NULL;
12775 cfg->attach_cookie = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
12776 cfg->attach_dummy = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
12778 if (mono_threads_is_coop_enabled ()) {
12779 /* AOT code is only used in the root domain */
12780 EMIT_NEW_PCONST (cfg, args [0], cfg->compile_aot ? NULL : cfg->domain);
12781 EMIT_NEW_VARLOADA (cfg, args [1], cfg->attach_dummy, cfg->attach_dummy->inst_vtype);
12782 ins = mono_emit_jit_icall (cfg, mono_jit_thread_attach, args);
12783 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->attach_cookie->dreg, ins->dreg);
12784 } else {
12785 EMIT_NEW_PCONST (cfg, ins, NULL);
12786 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->attach_cookie->dreg, ins->dreg);
12788 ad_ins = mono_get_domain_intrinsic (cfg);
12789 jit_tls_ins = mono_get_jit_tls_intrinsic (cfg);
12791 if (cfg->backend->have_tls_get && ad_ins && jit_tls_ins) {
12792 NEW_BBLOCK (cfg, next_bb);
12793 NEW_BBLOCK (cfg, call_bb);
12795 if (cfg->compile_aot) {
12796 /* AOT code is only used in the root domain */
12797 EMIT_NEW_PCONST (cfg, domain_ins, NULL);
12798 } else {
12799 EMIT_NEW_PCONST (cfg, domain_ins, cfg->domain);
12801 MONO_ADD_INS (cfg->cbb, ad_ins);
12802 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, ad_ins->dreg, domain_ins->dreg);
12803 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, call_bb);
12805 MONO_ADD_INS (cfg->cbb, jit_tls_ins);
12806 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, jit_tls_ins->dreg, 0);
12807 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, call_bb);
12809 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, next_bb);
12810 MONO_START_BB (cfg, call_bb);
12813 /* AOT code is only used in the root domain */
12814 EMIT_NEW_PCONST (cfg, args [0], cfg->compile_aot ? NULL : cfg->domain);
12815 EMIT_NEW_PCONST (cfg, args [1], NULL);
12816 ins = mono_emit_jit_icall (cfg, mono_jit_thread_attach, args);
12817 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->attach_cookie->dreg, ins->dreg);
12819 if (next_bb)
12820 MONO_START_BB (cfg, next_bb);
12823 ip += 2;
12824 break;
12826 case CEE_MONO_JIT_DETACH: {
12827 MonoInst *args [16];
12829 /* Restore the original domain */
12830 dreg = alloc_ireg (cfg);
12831 EMIT_NEW_UNALU (cfg, args [0], OP_MOVE, dreg, cfg->attach_cookie->dreg);
12832 EMIT_NEW_VARLOADA (cfg, args [1], cfg->attach_dummy, cfg->attach_dummy->inst_vtype);
12833 mono_emit_jit_icall (cfg, mono_jit_thread_detach, args);
12834 ip += 2;
12835 break;
12837 case CEE_MONO_CALLI_EXTRA_ARG: {
12838 MonoInst *addr;
12839 MonoMethodSignature *fsig;
12840 MonoInst *arg;
12843 * This is the same as CEE_CALLI, but passes an additional argument
12844 * to the called method in llvmonly mode.
12845 * This is only used by delegate invoke wrappers to call the
12846 * actual delegate method.
12848 g_assert (method->wrapper_type == MONO_WRAPPER_DELEGATE_INVOKE);
12850 CHECK_OPSIZE (6);
12851 token = read32 (ip + 2);
12853 ins = NULL;
12855 cmethod = NULL;
12856 CHECK_STACK (1);
12857 --sp;
12858 addr = *sp;
12859 fsig = mini_get_signature (method, token, generic_context, &cfg->error);
12860 CHECK_CFG_ERROR;
12862 if (cfg->llvm_only)
12863 cfg->signatures = g_slist_prepend_mempool (cfg->mempool, cfg->signatures, fsig);
12865 n = fsig->param_count + fsig->hasthis + 1;
12867 CHECK_STACK (n);
12869 sp -= n;
12870 arg = sp [n - 1];
12872 if (cfg->llvm_only) {
12874 * The lowest bit of 'arg' determines whenever the callee uses the gsharedvt
12875 * cconv. This is set by mono_init_delegate ().
12877 if (cfg->gsharedvt && mini_is_gsharedvt_variable_signature (fsig)) {
12878 MonoInst *callee = addr;
12879 MonoInst *call, *localloc_ins;
12880 MonoBasicBlock *is_gsharedvt_bb, *end_bb;
12881 int low_bit_reg = alloc_preg (cfg);
12883 NEW_BBLOCK (cfg, is_gsharedvt_bb);
12884 NEW_BBLOCK (cfg, end_bb);
12886 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PAND_IMM, low_bit_reg, arg->dreg, 1);
12887 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, low_bit_reg, 0);
12888 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, is_gsharedvt_bb);
12890 /* Normal case: callee uses a normal cconv, have to add an out wrapper */
12891 addr = emit_get_rgctx_sig (cfg, context_used,
12892 fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
12894 * ADDR points to a gsharedvt-out wrapper, have to pass <callee, arg> as an extra arg.
12896 MONO_INST_NEW (cfg, ins, OP_LOCALLOC_IMM);
12897 ins->dreg = alloc_preg (cfg);
12898 ins->inst_imm = 2 * SIZEOF_VOID_P;
12899 MONO_ADD_INS (cfg->cbb, ins);
12900 localloc_ins = ins;
12901 cfg->flags |= MONO_CFG_HAS_ALLOCA;
12902 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, 0, callee->dreg);
12903 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, SIZEOF_VOID_P, arg->dreg);
12905 call = emit_extra_arg_calli (cfg, fsig, sp, localloc_ins->dreg, addr);
12906 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
12908 /* Gsharedvt case: callee uses a gsharedvt cconv, no conversion is needed */
12909 MONO_START_BB (cfg, is_gsharedvt_bb);
12910 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PXOR_IMM, arg->dreg, arg->dreg, 1);
12911 ins = emit_extra_arg_calli (cfg, fsig, sp, arg->dreg, callee);
12912 ins->dreg = call->dreg;
12914 MONO_START_BB (cfg, end_bb);
12915 } else {
12916 /* Caller uses a normal calling conv */
12918 MonoInst *callee = addr;
12919 MonoInst *call, *localloc_ins;
12920 MonoBasicBlock *is_gsharedvt_bb, *end_bb;
12921 int low_bit_reg = alloc_preg (cfg);
12923 NEW_BBLOCK (cfg, is_gsharedvt_bb);
12924 NEW_BBLOCK (cfg, end_bb);
12926 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PAND_IMM, low_bit_reg, arg->dreg, 1);
12927 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, low_bit_reg, 0);
12928 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, is_gsharedvt_bb);
12930 /* Normal case: callee uses a normal cconv, no conversion is needed */
12931 call = emit_extra_arg_calli (cfg, fsig, sp, arg->dreg, callee);
12932 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
12933 /* Gsharedvt case: callee uses a gsharedvt cconv, have to add an in wrapper */
12934 MONO_START_BB (cfg, is_gsharedvt_bb);
12935 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PXOR_IMM, arg->dreg, arg->dreg, 1);
12936 NEW_AOTCONST (cfg, addr, MONO_PATCH_INFO_GSHAREDVT_IN_WRAPPER, fsig);
12937 MONO_ADD_INS (cfg->cbb, addr);
12939 * ADDR points to a gsharedvt-in wrapper, have to pass <callee, arg> as an extra arg.
12941 MONO_INST_NEW (cfg, ins, OP_LOCALLOC_IMM);
12942 ins->dreg = alloc_preg (cfg);
12943 ins->inst_imm = 2 * SIZEOF_VOID_P;
12944 MONO_ADD_INS (cfg->cbb, ins);
12945 localloc_ins = ins;
12946 cfg->flags |= MONO_CFG_HAS_ALLOCA;
12947 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, 0, callee->dreg);
12948 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, SIZEOF_VOID_P, arg->dreg);
12950 ins = emit_extra_arg_calli (cfg, fsig, sp, localloc_ins->dreg, addr);
12951 ins->dreg = call->dreg;
12952 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
12954 MONO_START_BB (cfg, end_bb);
12956 } else {
12957 /* Same as CEE_CALLI */
12958 if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) {
12960 * We pass the address to the gsharedvt trampoline in the rgctx reg
12962 MonoInst *callee = addr;
12964 addr = emit_get_rgctx_sig (cfg, context_used,
12965 fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
12966 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, callee);
12967 } else {
12968 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
12972 if (!MONO_TYPE_IS_VOID (fsig->ret))
12973 *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
12975 CHECK_CFG_EXCEPTION;
12977 ip += 6;
12978 ins_flag = 0;
12979 constrained_class = NULL;
12980 break;
12982 default:
12983 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
12984 break;
12986 break;
12989 case CEE_PREFIX1: {
12990 CHECK_OPSIZE (2);
12991 switch (ip [1]) {
12992 case CEE_ARGLIST: {
12993 /* somewhat similar to LDTOKEN */
12994 MonoInst *addr, *vtvar;
12995 CHECK_STACK_OVF (1);
12996 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
12998 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
12999 EMIT_NEW_UNALU (cfg, ins, OP_ARGLIST, -1, addr->dreg);
13001 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
13002 ins->type = STACK_VTYPE;
13003 ins->klass = mono_defaults.argumenthandle_class;
13004 *sp++ = ins;
13005 ip += 2;
13006 break;
13008 case CEE_CEQ:
13009 case CEE_CGT:
13010 case CEE_CGT_UN:
13011 case CEE_CLT:
13012 case CEE_CLT_UN: {
13013 MonoInst *cmp, *arg1, *arg2;
13015 CHECK_STACK (2);
13016 sp -= 2;
13017 arg1 = sp [0];
13018 arg2 = sp [1];
13021 * The following transforms:
13022 * CEE_CEQ into OP_CEQ
13023 * CEE_CGT into OP_CGT
13024 * CEE_CGT_UN into OP_CGT_UN
13025 * CEE_CLT into OP_CLT
13026 * CEE_CLT_UN into OP_CLT_UN
13028 MONO_INST_NEW (cfg, cmp, (OP_CEQ - CEE_CEQ) + ip [1]);
13030 MONO_INST_NEW (cfg, ins, cmp->opcode);
13031 cmp->sreg1 = arg1->dreg;
13032 cmp->sreg2 = arg2->dreg;
13033 type_from_op (cfg, cmp, arg1, arg2);
13034 CHECK_TYPE (cmp);
13035 add_widen_op (cfg, cmp, &arg1, &arg2);
13036 if ((arg1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((arg1->type == STACK_PTR) || (arg1->type == STACK_OBJ) || (arg1->type == STACK_MP))))
13037 cmp->opcode = OP_LCOMPARE;
13038 else if (arg1->type == STACK_R4)
13039 cmp->opcode = OP_RCOMPARE;
13040 else if (arg1->type == STACK_R8)
13041 cmp->opcode = OP_FCOMPARE;
13042 else
13043 cmp->opcode = OP_ICOMPARE;
13044 MONO_ADD_INS (cfg->cbb, cmp);
13045 ins->type = STACK_I4;
13046 ins->dreg = alloc_dreg (cfg, (MonoStackType)ins->type);
13047 type_from_op (cfg, ins, arg1, arg2);
13049 if (cmp->opcode == OP_FCOMPARE || cmp->opcode == OP_RCOMPARE) {
13051 * The backends expect the fceq opcodes to do the
13052 * comparison too.
13054 ins->sreg1 = cmp->sreg1;
13055 ins->sreg2 = cmp->sreg2;
13056 NULLIFY_INS (cmp);
13058 MONO_ADD_INS (cfg->cbb, ins);
13059 *sp++ = ins;
13060 ip += 2;
13061 break;
13063 case CEE_LDFTN: {
13064 MonoInst *argconst;
13065 MonoMethod *cil_method;
13067 CHECK_STACK_OVF (1);
13068 CHECK_OPSIZE (6);
13069 n = read32 (ip + 2);
13070 cmethod = mini_get_method (cfg, method, n, NULL, generic_context);
13071 CHECK_CFG_ERROR;
13073 mono_class_init (cmethod->klass);
13075 mono_save_token_info (cfg, image, n, cmethod);
13077 context_used = mini_method_check_context_used (cfg, cmethod);
13079 cil_method = cmethod;
13080 if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_method (method, cmethod))
13081 METHOD_ACCESS_FAILURE (method, cil_method);
13083 if (mono_security_core_clr_enabled ())
13084 ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
13087 * Optimize the common case of ldftn+delegate creation
13089 if ((sp > stack_start) && (ip + 6 + 5 < end) && ip_in_bb (cfg, cfg->cbb, ip + 6) && (ip [6] == CEE_NEWOBJ)) {
13090 MonoMethod *ctor_method = mini_get_method (cfg, method, read32 (ip + 7), NULL, generic_context);
13091 if (ctor_method && (ctor_method->klass->parent == mono_defaults.multicastdelegate_class)) {
13092 MonoInst *target_ins, *handle_ins;
13093 MonoMethod *invoke;
13094 int invoke_context_used;
13096 invoke = mono_get_delegate_invoke (ctor_method->klass);
13097 if (!invoke || !mono_method_signature (invoke))
13098 LOAD_ERROR;
13100 invoke_context_used = mini_method_check_context_used (cfg, invoke);
13102 target_ins = sp [-1];
13104 if (mono_security_core_clr_enabled ())
13105 ensure_method_is_allowed_to_call_method (cfg, method, ctor_method);
13107 if (!(cmethod->flags & METHOD_ATTRIBUTE_STATIC)) {
13108 /*LAME IMPL: We must not add a null check for virtual invoke delegates.*/
13109 if (mono_method_signature (invoke)->param_count == mono_method_signature (cmethod)->param_count) {
13110 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, target_ins->dreg, 0);
13111 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "ArgumentException");
13115 /* FIXME: SGEN support */
13116 if (invoke_context_used == 0 || cfg->llvm_only) {
13117 ip += 6;
13118 if (cfg->verbose_level > 3)
13119 g_print ("converting (in B%d: stack: %d) %s", cfg->cbb->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
13120 if ((handle_ins = handle_delegate_ctor (cfg, ctor_method->klass, target_ins, cmethod, context_used, FALSE))) {
13121 sp --;
13122 *sp = handle_ins;
13123 CHECK_CFG_EXCEPTION;
13124 ip += 5;
13125 sp ++;
13126 break;
13128 ip -= 6;
13133 argconst = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_METHOD);
13134 ins = mono_emit_jit_icall (cfg, mono_ldftn, &argconst);
13135 *sp++ = ins;
13137 ip += 6;
13138 inline_costs += 10 * num_calls++;
13139 break;
13141 case CEE_LDVIRTFTN: {
13142 MonoInst *args [2];
13144 CHECK_STACK (1);
13145 CHECK_OPSIZE (6);
13146 n = read32 (ip + 2);
13147 cmethod = mini_get_method (cfg, method, n, NULL, generic_context);
13148 CHECK_CFG_ERROR;
13150 mono_class_init (cmethod->klass);
13152 context_used = mini_method_check_context_used (cfg, cmethod);
13154 if (mono_security_core_clr_enabled ())
13155 ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
13158 * Optimize the common case of ldvirtftn+delegate creation
13160 if ((sp > stack_start) && (ip + 6 + 5 < end) && ip_in_bb (cfg, cfg->cbb, ip + 6) && (ip [6] == CEE_NEWOBJ) && (ip > header->code && ip [-1] == CEE_DUP)) {
13161 MonoMethod *ctor_method = mini_get_method (cfg, method, read32 (ip + 7), NULL, generic_context);
13162 if (ctor_method && (ctor_method->klass->parent == mono_defaults.multicastdelegate_class)) {
13163 MonoInst *target_ins, *handle_ins;
13164 MonoMethod *invoke;
13165 int invoke_context_used;
13166 gboolean is_virtual = cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL;
13168 invoke = mono_get_delegate_invoke (ctor_method->klass);
13169 if (!invoke || !mono_method_signature (invoke))
13170 LOAD_ERROR;
13172 invoke_context_used = mini_method_check_context_used (cfg, invoke);
13174 target_ins = sp [-1];
13176 if (mono_security_core_clr_enabled ())
13177 ensure_method_is_allowed_to_call_method (cfg, method, ctor_method);
13179 /* FIXME: SGEN support */
13180 if (invoke_context_used == 0 || cfg->llvm_only) {
13181 ip += 6;
13182 if (cfg->verbose_level > 3)
13183 g_print ("converting (in B%d: stack: %d) %s", cfg->cbb->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
13184 if ((handle_ins = handle_delegate_ctor (cfg, ctor_method->klass, target_ins, cmethod, context_used, is_virtual))) {
13185 sp -= 2;
13186 *sp = handle_ins;
13187 CHECK_CFG_EXCEPTION;
13188 ip += 5;
13189 sp ++;
13190 break;
13192 ip -= 6;
13197 --sp;
13198 args [0] = *sp;
13200 args [1] = emit_get_rgctx_method (cfg, context_used,
13201 cmethod, MONO_RGCTX_INFO_METHOD);
13203 if (context_used)
13204 *sp++ = mono_emit_jit_icall (cfg, mono_ldvirtfn_gshared, args);
13205 else
13206 *sp++ = mono_emit_jit_icall (cfg, mono_ldvirtfn, args);
13208 ip += 6;
13209 inline_costs += 10 * num_calls++;
13210 break;
13212 case CEE_LDARG:
13213 CHECK_STACK_OVF (1);
13214 CHECK_OPSIZE (4);
13215 n = read16 (ip + 2);
13216 CHECK_ARG (n);
13217 EMIT_NEW_ARGLOAD (cfg, ins, n);
13218 *sp++ = ins;
13219 ip += 4;
13220 break;
13221 case CEE_LDARGA:
13222 CHECK_STACK_OVF (1);
13223 CHECK_OPSIZE (4);
13224 n = read16 (ip + 2);
13225 CHECK_ARG (n);
13226 NEW_ARGLOADA (cfg, ins, n);
13227 MONO_ADD_INS (cfg->cbb, ins);
13228 *sp++ = ins;
13229 ip += 4;
13230 break;
13231 case CEE_STARG:
13232 CHECK_STACK (1);
13233 --sp;
13234 CHECK_OPSIZE (4);
13235 n = read16 (ip + 2);
13236 CHECK_ARG (n);
13237 if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [n], *sp))
13238 UNVERIFIED;
13239 EMIT_NEW_ARGSTORE (cfg, ins, n, *sp);
13240 ip += 4;
13241 break;
13242 case CEE_LDLOC:
13243 CHECK_STACK_OVF (1);
13244 CHECK_OPSIZE (4);
13245 n = read16 (ip + 2);
13246 CHECK_LOCAL (n);
13247 EMIT_NEW_LOCLOAD (cfg, ins, n);
13248 *sp++ = ins;
13249 ip += 4;
13250 break;
13251 case CEE_LDLOCA: {
13252 unsigned char *tmp_ip;
13253 CHECK_STACK_OVF (1);
13254 CHECK_OPSIZE (4);
13255 n = read16 (ip + 2);
13256 CHECK_LOCAL (n);
13258 if ((tmp_ip = emit_optimized_ldloca_ir (cfg, ip, end, 2))) {
13259 ip = tmp_ip;
13260 inline_costs += 1;
13261 break;
13264 EMIT_NEW_LOCLOADA (cfg, ins, n);
13265 *sp++ = ins;
13266 ip += 4;
13267 break;
13269 case CEE_STLOC:
13270 CHECK_STACK (1);
13271 --sp;
13272 CHECK_OPSIZE (4);
13273 n = read16 (ip + 2);
13274 CHECK_LOCAL (n);
13275 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
13276 UNVERIFIED;
13277 emit_stloc_ir (cfg, sp, header, n);
13278 ip += 4;
13279 inline_costs += 1;
13280 break;
13281 case CEE_LOCALLOC:
13282 CHECK_STACK (1);
13283 --sp;
13284 if (sp != stack_start)
13285 UNVERIFIED;
13286 if (cfg->method != method)
13288 * Inlining this into a loop in a parent could lead to
13289 * stack overflows which is different behavior than the
13290 * non-inlined case, thus disable inlining in this case.
13292 INLINE_FAILURE("localloc");
13294 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
13295 ins->dreg = alloc_preg (cfg);
13296 ins->sreg1 = sp [0]->dreg;
13297 ins->type = STACK_PTR;
13298 MONO_ADD_INS (cfg->cbb, ins);
13300 cfg->flags |= MONO_CFG_HAS_ALLOCA;
13301 if (init_locals)
13302 ins->flags |= MONO_INST_INIT;
13304 *sp++ = ins;
13305 ip += 2;
13306 break;
13307 case CEE_ENDFILTER: {
13308 MonoExceptionClause *clause, *nearest;
13309 int cc;
13311 CHECK_STACK (1);
13312 --sp;
13313 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
13314 UNVERIFIED;
13315 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
13316 ins->sreg1 = (*sp)->dreg;
13317 MONO_ADD_INS (cfg->cbb, ins);
13318 start_new_bblock = 1;
13319 ip += 2;
13321 nearest = NULL;
13322 for (cc = 0; cc < header->num_clauses; ++cc) {
13323 clause = &header->clauses [cc];
13324 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
13325 ((ip - header->code) > clause->data.filter_offset && (ip - header->code) <= clause->handler_offset) &&
13326 (!nearest || (clause->data.filter_offset < nearest->data.filter_offset)))
13327 nearest = clause;
13329 g_assert (nearest);
13330 if ((ip - header->code) != nearest->handler_offset)
13331 UNVERIFIED;
13333 break;
13335 case CEE_UNALIGNED_:
13336 ins_flag |= MONO_INST_UNALIGNED;
13337 /* FIXME: record alignment? we can assume 1 for now */
13338 CHECK_OPSIZE (3);
13339 ip += 3;
13340 break;
13341 case CEE_VOLATILE_:
13342 ins_flag |= MONO_INST_VOLATILE;
13343 ip += 2;
13344 break;
13345 case CEE_TAIL_:
13346 ins_flag |= MONO_INST_TAILCALL;
13347 cfg->flags |= MONO_CFG_HAS_TAIL;
13348 /* Can't inline tail calls at this time */
13349 inline_costs += 100000;
13350 ip += 2;
13351 break;
13352 case CEE_INITOBJ:
13353 CHECK_STACK (1);
13354 --sp;
13355 CHECK_OPSIZE (6);
13356 token = read32 (ip + 2);
13357 klass = mini_get_class (method, token, generic_context);
13358 CHECK_TYPELOAD (klass);
13359 if (generic_class_is_reference_type (cfg, klass))
13360 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, sp [0]->dreg, 0, 0);
13361 else
13362 mini_emit_initobj (cfg, *sp, NULL, klass);
13363 ip += 6;
13364 inline_costs += 1;
13365 break;
13366 case CEE_CONSTRAINED_:
13367 CHECK_OPSIZE (6);
13368 token = read32 (ip + 2);
13369 constrained_class = mini_get_class (method, token, generic_context);
13370 CHECK_TYPELOAD (constrained_class);
13371 ip += 6;
13372 break;
13373 case CEE_CPBLK:
13374 case CEE_INITBLK: {
13375 MonoInst *iargs [3];
13376 CHECK_STACK (3);
13377 sp -= 3;
13379 /* Skip optimized paths for volatile operations. */
13380 if ((ip [1] == CEE_CPBLK) && !(ins_flag & MONO_INST_VOLATILE) && (cfg->opt & MONO_OPT_INTRINS) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5)) {
13381 mini_emit_memcpy (cfg, sp [0]->dreg, 0, sp [1]->dreg, 0, sp [2]->inst_c0, 0);
13382 } else if ((ip [1] == CEE_INITBLK) && !(ins_flag & MONO_INST_VOLATILE) && (cfg->opt & MONO_OPT_INTRINS) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5) && (sp [1]->opcode == OP_ICONST) && (sp [1]->inst_c0 == 0)) {
13383 /* emit_memset only works when val == 0 */
13384 mini_emit_memset (cfg, sp [0]->dreg, 0, sp [2]->inst_c0, sp [1]->inst_c0, 0);
13385 } else {
13386 MonoInst *call;
13387 iargs [0] = sp [0];
13388 iargs [1] = sp [1];
13389 iargs [2] = sp [2];
13390 if (ip [1] == CEE_CPBLK) {
13392 * FIXME: It's unclear whether we should be emitting both the acquire
13393 * and release barriers for cpblk. It is technically both a load and
13394 * store operation, so it seems like that's the sensible thing to do.
13396 * FIXME: We emit full barriers on both sides of the operation for
13397 * simplicity. We should have a separate atomic memcpy method instead.
13399 MonoMethod *memcpy_method = get_memcpy_method ();
13401 if (ins_flag & MONO_INST_VOLATILE)
13402 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
13404 call = mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
13405 call->flags |= ins_flag;
13407 if (ins_flag & MONO_INST_VOLATILE)
13408 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
13409 } else {
13410 MonoMethod *memset_method = get_memset_method ();
13411 if (ins_flag & MONO_INST_VOLATILE) {
13412 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
13413 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
13415 call = mono_emit_method_call (cfg, memset_method, iargs, NULL);
13416 call->flags |= ins_flag;
13419 ip += 2;
13420 ins_flag = 0;
13421 inline_costs += 1;
13422 break;
13424 case CEE_NO_:
13425 CHECK_OPSIZE (3);
13426 if (ip [2] & 0x1)
13427 ins_flag |= MONO_INST_NOTYPECHECK;
13428 if (ip [2] & 0x2)
13429 ins_flag |= MONO_INST_NORANGECHECK;
13430 /* we ignore the no-nullcheck for now since we
13431 * really do it explicitly only when doing callvirt->call
13433 ip += 3;
13434 break;
13435 case CEE_RETHROW: {
13436 MonoInst *load;
13437 int handler_offset = -1;
13439 for (i = 0; i < header->num_clauses; ++i) {
13440 MonoExceptionClause *clause = &header->clauses [i];
13441 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code) && !(clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
13442 handler_offset = clause->handler_offset;
13443 break;
13447 cfg->cbb->flags |= BB_EXCEPTION_UNSAFE;
13449 if (handler_offset == -1)
13450 UNVERIFIED;
13452 EMIT_NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, handler_offset)->inst_c0);
13453 MONO_INST_NEW (cfg, ins, OP_RETHROW);
13454 ins->sreg1 = load->dreg;
13455 MONO_ADD_INS (cfg->cbb, ins);
13457 MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
13458 MONO_ADD_INS (cfg->cbb, ins);
13460 sp = stack_start;
13461 link_bblock (cfg, cfg->cbb, end_bblock);
13462 start_new_bblock = 1;
13463 ip += 2;
13464 break;
13466 case CEE_SIZEOF: {
13467 guint32 val;
13468 int ialign;
13470 CHECK_STACK_OVF (1);
13471 CHECK_OPSIZE (6);
13472 token = read32 (ip + 2);
13473 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC && !image_is_dynamic (method->klass->image) && !generic_context) {
13474 MonoType *type = mono_type_create_from_typespec_checked (image, token, &cfg->error);
13475 CHECK_CFG_ERROR;
13477 val = mono_type_size (type, &ialign);
13478 } else {
13479 MonoClass *klass = mini_get_class (method, token, generic_context);
13480 CHECK_TYPELOAD (klass);
13482 val = mono_type_size (&klass->byval_arg, &ialign);
13484 if (mini_is_gsharedvt_klass (klass))
13485 GSHAREDVT_FAILURE (*ip);
13487 EMIT_NEW_ICONST (cfg, ins, val);
13488 *sp++= ins;
13489 ip += 6;
13490 break;
13492 case CEE_REFANYTYPE: {
13493 MonoInst *src_var, *src;
13495 GSHAREDVT_FAILURE (*ip);
13497 CHECK_STACK (1);
13498 --sp;
13500 // FIXME:
13501 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
13502 if (!src_var)
13503 src_var = mono_compile_create_var_for_vreg (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL, sp [0]->dreg);
13504 EMIT_NEW_VARLOADA (cfg, src, src_var, src_var->inst_vtype);
13505 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &mono_defaults.typehandle_class->byval_arg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type));
13506 *sp++ = ins;
13507 ip += 2;
13508 break;
13510 case CEE_READONLY_:
13511 readonly = TRUE;
13512 ip += 2;
13513 break;
13515 case CEE_UNUSED56:
13516 case CEE_UNUSED57:
13517 case CEE_UNUSED70:
13518 case CEE_UNUSED:
13519 case CEE_UNUSED99:
13520 UNVERIFIED;
13522 default:
13523 g_warning ("opcode 0xfe 0x%02x not handled", ip [1]);
13524 UNVERIFIED;
13526 break;
13528 case CEE_UNUSED58:
13529 case CEE_UNUSED1:
13530 UNVERIFIED;
13532 default:
13533 g_warning ("opcode 0x%02x not handled", *ip);
13534 UNVERIFIED;
13537 if (start_new_bblock != 1)
13538 UNVERIFIED;
13540 cfg->cbb->cil_length = ip - cfg->cbb->cil_code;
13541 if (cfg->cbb->next_bb) {
13542 /* This could already be set because of inlining, #693905 */
13543 MonoBasicBlock *bb = cfg->cbb;
13545 while (bb->next_bb)
13546 bb = bb->next_bb;
13547 bb->next_bb = end_bblock;
13548 } else {
13549 cfg->cbb->next_bb = end_bblock;
13552 if (cfg->method == method && cfg->domainvar) {
13553 MonoInst *store;
13554 MonoInst *get_domain;
13556 cfg->cbb = init_localsbb;
13558 if ((get_domain = mono_get_domain_intrinsic (cfg))) {
13559 MONO_ADD_INS (cfg->cbb, get_domain);
13560 } else {
13561 get_domain = mono_emit_jit_icall (cfg, mono_domain_get, NULL);
13563 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, get_domain);
13564 MONO_ADD_INS (cfg->cbb, store);
13567 #if defined(TARGET_POWERPC) || defined(TARGET_X86)
13568 if (cfg->compile_aot)
13569 /* FIXME: The plt slots require a GOT var even if the method doesn't use it */
13570 mono_get_got_var (cfg);
13571 #endif
13573 if (cfg->method == method && cfg->got_var)
13574 mono_emit_load_got_addr (cfg);
13576 if (init_localsbb) {
13577 cfg->cbb = init_localsbb;
13578 cfg->ip = NULL;
13579 for (i = 0; i < header->num_locals; ++i) {
13580 emit_init_local (cfg, i, header->locals [i], init_locals);
13584 if (cfg->init_ref_vars && cfg->method == method) {
13585 /* Emit initialization for ref vars */
13586 // FIXME: Avoid duplication initialization for IL locals.
13587 for (i = 0; i < cfg->num_varinfo; ++i) {
13588 MonoInst *ins = cfg->varinfo [i];
13590 if (ins->opcode == OP_LOCAL && ins->type == STACK_OBJ)
13591 MONO_EMIT_NEW_PCONST (cfg, ins->dreg, NULL);
13595 if (cfg->lmf_var && cfg->method == method && !cfg->llvm_only) {
13596 cfg->cbb = init_localsbb;
13597 emit_push_lmf (cfg);
13600 cfg->cbb = init_localsbb;
13601 emit_instrumentation_call (cfg, mono_profiler_method_enter);
13603 if (seq_points) {
13604 MonoBasicBlock *bb;
13607 * Make seq points at backward branch targets interruptable.
13609 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
13610 if (bb->code && bb->in_count > 1 && bb->code->opcode == OP_SEQ_POINT)
13611 bb->code->flags |= MONO_INST_SINGLE_STEP_LOC;
13614 /* Add a sequence point for method entry/exit events */
13615 if (seq_points && cfg->gen_sdb_seq_points) {
13616 NEW_SEQ_POINT (cfg, ins, METHOD_ENTRY_IL_OFFSET, FALSE);
13617 MONO_ADD_INS (init_localsbb, ins);
13618 NEW_SEQ_POINT (cfg, ins, METHOD_EXIT_IL_OFFSET, FALSE);
13619 MONO_ADD_INS (cfg->bb_exit, ins);
13623 * Add seq points for IL offsets which have line number info, but wasn't generated a seq point during JITting because
13624 * the code they refer to was dead (#11880).
13626 if (sym_seq_points) {
13627 for (i = 0; i < header->code_size; ++i) {
13628 if (mono_bitset_test_fast (seq_point_locs, i) && !mono_bitset_test_fast (seq_point_set_locs, i)) {
13629 MonoInst *ins;
13631 NEW_SEQ_POINT (cfg, ins, i, FALSE);
13632 mono_add_seq_point (cfg, NULL, ins, SEQ_POINT_NATIVE_OFFSET_DEAD_CODE);
13637 cfg->ip = NULL;
13639 if (cfg->method == method) {
13640 MonoBasicBlock *bb;
13641 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
13642 bb->region = mono_find_block_region (cfg, bb->real_offset);
13643 if (cfg->spvars)
13644 mono_create_spvar_for_region (cfg, bb->region);
13645 if (cfg->verbose_level > 2)
13646 printf ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
13648 } else {
13649 MonoBasicBlock *bb;
13650 /* get_most_deep_clause () in mini-llvm.c depends on this for inlined bblocks */
13651 for (bb = start_bblock; bb != end_bblock; bb = bb->next_bb) {
13652 bb->real_offset = inline_offset;
13656 if (inline_costs < 0) {
13657 char *mname;
13659 /* Method is too large */
13660 mname = mono_method_full_name (method, TRUE);
13661 mono_cfg_set_exception_invalid_program (cfg, g_strdup_printf ("Method %s is too complex.", mname));
13662 g_free (mname);
13665 if ((cfg->verbose_level > 2) && (cfg->method == method))
13666 mono_print_code (cfg, "AFTER METHOD-TO-IR");
13668 goto cleanup;
13670 mono_error_exit:
13671 g_assert (!mono_error_ok (&cfg->error));
13672 goto cleanup;
13674 exception_exit:
13675 g_assert (cfg->exception_type != MONO_EXCEPTION_NONE);
13676 goto cleanup;
13678 unverified:
13679 set_exception_type_from_invalid_il (cfg, method, ip);
13680 goto cleanup;
13682 cleanup:
13683 g_slist_free (class_inits);
13684 mono_basic_block_free (original_bb);
13685 cfg->dont_inline = g_list_remove (cfg->dont_inline, method);
13686 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, header);
13687 if (cfg->exception_type)
13688 return -1;
13689 else
13690 return inline_costs;
13693 static int
13694 store_membase_reg_to_store_membase_imm (int opcode)
13696 switch (opcode) {
13697 case OP_STORE_MEMBASE_REG:
13698 return OP_STORE_MEMBASE_IMM;
13699 case OP_STOREI1_MEMBASE_REG:
13700 return OP_STOREI1_MEMBASE_IMM;
13701 case OP_STOREI2_MEMBASE_REG:
13702 return OP_STOREI2_MEMBASE_IMM;
13703 case OP_STOREI4_MEMBASE_REG:
13704 return OP_STOREI4_MEMBASE_IMM;
13705 case OP_STOREI8_MEMBASE_REG:
13706 return OP_STOREI8_MEMBASE_IMM;
13707 default:
13708 g_assert_not_reached ();
13711 return -1;
13715 mono_op_to_op_imm (int opcode)
13717 switch (opcode) {
13718 case OP_IADD:
13719 return OP_IADD_IMM;
13720 case OP_ISUB:
13721 return OP_ISUB_IMM;
13722 case OP_IDIV:
13723 return OP_IDIV_IMM;
13724 case OP_IDIV_UN:
13725 return OP_IDIV_UN_IMM;
13726 case OP_IREM:
13727 return OP_IREM_IMM;
13728 case OP_IREM_UN:
13729 return OP_IREM_UN_IMM;
13730 case OP_IMUL:
13731 return OP_IMUL_IMM;
13732 case OP_IAND:
13733 return OP_IAND_IMM;
13734 case OP_IOR:
13735 return OP_IOR_IMM;
13736 case OP_IXOR:
13737 return OP_IXOR_IMM;
13738 case OP_ISHL:
13739 return OP_ISHL_IMM;
13740 case OP_ISHR:
13741 return OP_ISHR_IMM;
13742 case OP_ISHR_UN:
13743 return OP_ISHR_UN_IMM;
13745 case OP_LADD:
13746 return OP_LADD_IMM;
13747 case OP_LSUB:
13748 return OP_LSUB_IMM;
13749 case OP_LAND:
13750 return OP_LAND_IMM;
13751 case OP_LOR:
13752 return OP_LOR_IMM;
13753 case OP_LXOR:
13754 return OP_LXOR_IMM;
13755 case OP_LSHL:
13756 return OP_LSHL_IMM;
13757 case OP_LSHR:
13758 return OP_LSHR_IMM;
13759 case OP_LSHR_UN:
13760 return OP_LSHR_UN_IMM;
13761 #if SIZEOF_REGISTER == 8
13762 case OP_LREM:
13763 return OP_LREM_IMM;
13764 #endif
13766 case OP_COMPARE:
13767 return OP_COMPARE_IMM;
13768 case OP_ICOMPARE:
13769 return OP_ICOMPARE_IMM;
13770 case OP_LCOMPARE:
13771 return OP_LCOMPARE_IMM;
13773 case OP_STORE_MEMBASE_REG:
13774 return OP_STORE_MEMBASE_IMM;
13775 case OP_STOREI1_MEMBASE_REG:
13776 return OP_STOREI1_MEMBASE_IMM;
13777 case OP_STOREI2_MEMBASE_REG:
13778 return OP_STOREI2_MEMBASE_IMM;
13779 case OP_STOREI4_MEMBASE_REG:
13780 return OP_STOREI4_MEMBASE_IMM;
13782 #if defined(TARGET_X86) || defined (TARGET_AMD64)
13783 case OP_X86_PUSH:
13784 return OP_X86_PUSH_IMM;
13785 case OP_X86_COMPARE_MEMBASE_REG:
13786 return OP_X86_COMPARE_MEMBASE_IMM;
13787 #endif
13788 #if defined(TARGET_AMD64)
13789 case OP_AMD64_ICOMPARE_MEMBASE_REG:
13790 return OP_AMD64_ICOMPARE_MEMBASE_IMM;
13791 #endif
13792 case OP_VOIDCALL_REG:
13793 return OP_VOIDCALL;
13794 case OP_CALL_REG:
13795 return OP_CALL;
13796 case OP_LCALL_REG:
13797 return OP_LCALL;
13798 case OP_FCALL_REG:
13799 return OP_FCALL;
13800 case OP_LOCALLOC:
13801 return OP_LOCALLOC_IMM;
13804 return -1;
13807 static int
13808 ldind_to_load_membase (int opcode)
13810 switch (opcode) {
13811 case CEE_LDIND_I1:
13812 return OP_LOADI1_MEMBASE;
13813 case CEE_LDIND_U1:
13814 return OP_LOADU1_MEMBASE;
13815 case CEE_LDIND_I2:
13816 return OP_LOADI2_MEMBASE;
13817 case CEE_LDIND_U2:
13818 return OP_LOADU2_MEMBASE;
13819 case CEE_LDIND_I4:
13820 return OP_LOADI4_MEMBASE;
13821 case CEE_LDIND_U4:
13822 return OP_LOADU4_MEMBASE;
13823 case CEE_LDIND_I:
13824 return OP_LOAD_MEMBASE;
13825 case CEE_LDIND_REF:
13826 return OP_LOAD_MEMBASE;
13827 case CEE_LDIND_I8:
13828 return OP_LOADI8_MEMBASE;
13829 case CEE_LDIND_R4:
13830 return OP_LOADR4_MEMBASE;
13831 case CEE_LDIND_R8:
13832 return OP_LOADR8_MEMBASE;
13833 default:
13834 g_assert_not_reached ();
13837 return -1;
13840 static int
13841 stind_to_store_membase (int opcode)
13843 switch (opcode) {
13844 case CEE_STIND_I1:
13845 return OP_STOREI1_MEMBASE_REG;
13846 case CEE_STIND_I2:
13847 return OP_STOREI2_MEMBASE_REG;
13848 case CEE_STIND_I4:
13849 return OP_STOREI4_MEMBASE_REG;
13850 case CEE_STIND_I:
13851 case CEE_STIND_REF:
13852 return OP_STORE_MEMBASE_REG;
13853 case CEE_STIND_I8:
13854 return OP_STOREI8_MEMBASE_REG;
13855 case CEE_STIND_R4:
13856 return OP_STORER4_MEMBASE_REG;
13857 case CEE_STIND_R8:
13858 return OP_STORER8_MEMBASE_REG;
13859 default:
13860 g_assert_not_reached ();
13863 return -1;
13867 mono_load_membase_to_load_mem (int opcode)
13869 // FIXME: Add a MONO_ARCH_HAVE_LOAD_MEM macro
13870 #if defined(TARGET_X86) || defined(TARGET_AMD64)
13871 switch (opcode) {
13872 case OP_LOAD_MEMBASE:
13873 return OP_LOAD_MEM;
13874 case OP_LOADU1_MEMBASE:
13875 return OP_LOADU1_MEM;
13876 case OP_LOADU2_MEMBASE:
13877 return OP_LOADU2_MEM;
13878 case OP_LOADI4_MEMBASE:
13879 return OP_LOADI4_MEM;
13880 case OP_LOADU4_MEMBASE:
13881 return OP_LOADU4_MEM;
13882 #if SIZEOF_REGISTER == 8
13883 case OP_LOADI8_MEMBASE:
13884 return OP_LOADI8_MEM;
13885 #endif
13887 #endif
13889 return -1;
13892 static inline int
13893 op_to_op_dest_membase (int store_opcode, int opcode)
13895 #if defined(TARGET_X86)
13896 if (!((store_opcode == OP_STORE_MEMBASE_REG) || (store_opcode == OP_STOREI4_MEMBASE_REG)))
13897 return -1;
13899 switch (opcode) {
13900 case OP_IADD:
13901 return OP_X86_ADD_MEMBASE_REG;
13902 case OP_ISUB:
13903 return OP_X86_SUB_MEMBASE_REG;
13904 case OP_IAND:
13905 return OP_X86_AND_MEMBASE_REG;
13906 case OP_IOR:
13907 return OP_X86_OR_MEMBASE_REG;
13908 case OP_IXOR:
13909 return OP_X86_XOR_MEMBASE_REG;
13910 case OP_ADD_IMM:
13911 case OP_IADD_IMM:
13912 return OP_X86_ADD_MEMBASE_IMM;
13913 case OP_SUB_IMM:
13914 case OP_ISUB_IMM:
13915 return OP_X86_SUB_MEMBASE_IMM;
13916 case OP_AND_IMM:
13917 case OP_IAND_IMM:
13918 return OP_X86_AND_MEMBASE_IMM;
13919 case OP_OR_IMM:
13920 case OP_IOR_IMM:
13921 return OP_X86_OR_MEMBASE_IMM;
13922 case OP_XOR_IMM:
13923 case OP_IXOR_IMM:
13924 return OP_X86_XOR_MEMBASE_IMM;
13925 case OP_MOVE:
13926 return OP_NOP;
13928 #endif
13930 #if defined(TARGET_AMD64)
13931 if (!((store_opcode == OP_STORE_MEMBASE_REG) || (store_opcode == OP_STOREI4_MEMBASE_REG) || (store_opcode == OP_STOREI8_MEMBASE_REG)))
13932 return -1;
13934 switch (opcode) {
13935 case OP_IADD:
13936 return OP_X86_ADD_MEMBASE_REG;
13937 case OP_ISUB:
13938 return OP_X86_SUB_MEMBASE_REG;
13939 case OP_IAND:
13940 return OP_X86_AND_MEMBASE_REG;
13941 case OP_IOR:
13942 return OP_X86_OR_MEMBASE_REG;
13943 case OP_IXOR:
13944 return OP_X86_XOR_MEMBASE_REG;
13945 case OP_IADD_IMM:
13946 return OP_X86_ADD_MEMBASE_IMM;
13947 case OP_ISUB_IMM:
13948 return OP_X86_SUB_MEMBASE_IMM;
13949 case OP_IAND_IMM:
13950 return OP_X86_AND_MEMBASE_IMM;
13951 case OP_IOR_IMM:
13952 return OP_X86_OR_MEMBASE_IMM;
13953 case OP_IXOR_IMM:
13954 return OP_X86_XOR_MEMBASE_IMM;
13955 case OP_LADD:
13956 return OP_AMD64_ADD_MEMBASE_REG;
13957 case OP_LSUB:
13958 return OP_AMD64_SUB_MEMBASE_REG;
13959 case OP_LAND:
13960 return OP_AMD64_AND_MEMBASE_REG;
13961 case OP_LOR:
13962 return OP_AMD64_OR_MEMBASE_REG;
13963 case OP_LXOR:
13964 return OP_AMD64_XOR_MEMBASE_REG;
13965 case OP_ADD_IMM:
13966 case OP_LADD_IMM:
13967 return OP_AMD64_ADD_MEMBASE_IMM;
13968 case OP_SUB_IMM:
13969 case OP_LSUB_IMM:
13970 return OP_AMD64_SUB_MEMBASE_IMM;
13971 case OP_AND_IMM:
13972 case OP_LAND_IMM:
13973 return OP_AMD64_AND_MEMBASE_IMM;
13974 case OP_OR_IMM:
13975 case OP_LOR_IMM:
13976 return OP_AMD64_OR_MEMBASE_IMM;
13977 case OP_XOR_IMM:
13978 case OP_LXOR_IMM:
13979 return OP_AMD64_XOR_MEMBASE_IMM;
13980 case OP_MOVE:
13981 return OP_NOP;
13983 #endif
13985 return -1;
13988 static inline int
13989 op_to_op_store_membase (int store_opcode, int opcode)
13991 #if defined(TARGET_X86) || defined(TARGET_AMD64)
13992 switch (opcode) {
13993 case OP_ICEQ:
13994 if (store_opcode == OP_STOREI1_MEMBASE_REG)
13995 return OP_X86_SETEQ_MEMBASE;
13996 case OP_CNE:
13997 if (store_opcode == OP_STOREI1_MEMBASE_REG)
13998 return OP_X86_SETNE_MEMBASE;
14000 #endif
14002 return -1;
14005 static inline int
14006 op_to_op_src1_membase (MonoCompile *cfg, int load_opcode, int opcode)
14008 #ifdef TARGET_X86
14009 /* FIXME: This has sign extension issues */
14011 if ((opcode == OP_ICOMPARE_IMM) && (load_opcode == OP_LOADU1_MEMBASE))
14012 return OP_X86_COMPARE_MEMBASE8_IMM;
14015 if (!((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE)))
14016 return -1;
14018 switch (opcode) {
14019 case OP_X86_PUSH:
14020 return OP_X86_PUSH_MEMBASE;
14021 case OP_COMPARE_IMM:
14022 case OP_ICOMPARE_IMM:
14023 return OP_X86_COMPARE_MEMBASE_IMM;
14024 case OP_COMPARE:
14025 case OP_ICOMPARE:
14026 return OP_X86_COMPARE_MEMBASE_REG;
14028 #endif
14030 #ifdef TARGET_AMD64
14031 /* FIXME: This has sign extension issues */
14033 if ((opcode == OP_ICOMPARE_IMM) && (load_opcode == OP_LOADU1_MEMBASE))
14034 return OP_X86_COMPARE_MEMBASE8_IMM;
14037 switch (opcode) {
14038 case OP_X86_PUSH:
14039 if ((load_opcode == OP_LOAD_MEMBASE && !cfg->backend->ilp32) || (load_opcode == OP_LOADI8_MEMBASE))
14040 return OP_X86_PUSH_MEMBASE;
14041 break;
14042 /* FIXME: This only works for 32 bit immediates
14043 case OP_COMPARE_IMM:
14044 case OP_LCOMPARE_IMM:
14045 if ((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI8_MEMBASE))
14046 return OP_AMD64_COMPARE_MEMBASE_IMM;
14048 case OP_ICOMPARE_IMM:
14049 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE))
14050 return OP_AMD64_ICOMPARE_MEMBASE_IMM;
14051 break;
14052 case OP_COMPARE:
14053 case OP_LCOMPARE:
14054 if (cfg->backend->ilp32 && load_opcode == OP_LOAD_MEMBASE)
14055 return OP_AMD64_ICOMPARE_MEMBASE_REG;
14056 if ((load_opcode == OP_LOAD_MEMBASE && !cfg->backend->ilp32) || (load_opcode == OP_LOADI8_MEMBASE))
14057 return OP_AMD64_COMPARE_MEMBASE_REG;
14058 break;
14059 case OP_ICOMPARE:
14060 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE))
14061 return OP_AMD64_ICOMPARE_MEMBASE_REG;
14062 break;
14064 #endif
14066 return -1;
14069 static inline int
14070 op_to_op_src2_membase (MonoCompile *cfg, int load_opcode, int opcode)
14072 #ifdef TARGET_X86
14073 if (!((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE)))
14074 return -1;
14076 switch (opcode) {
14077 case OP_COMPARE:
14078 case OP_ICOMPARE:
14079 return OP_X86_COMPARE_REG_MEMBASE;
14080 case OP_IADD:
14081 return OP_X86_ADD_REG_MEMBASE;
14082 case OP_ISUB:
14083 return OP_X86_SUB_REG_MEMBASE;
14084 case OP_IAND:
14085 return OP_X86_AND_REG_MEMBASE;
14086 case OP_IOR:
14087 return OP_X86_OR_REG_MEMBASE;
14088 case OP_IXOR:
14089 return OP_X86_XOR_REG_MEMBASE;
14091 #endif
14093 #ifdef TARGET_AMD64
14094 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE) || (load_opcode == OP_LOAD_MEMBASE && cfg->backend->ilp32)) {
14095 switch (opcode) {
14096 case OP_ICOMPARE:
14097 return OP_AMD64_ICOMPARE_REG_MEMBASE;
14098 case OP_IADD:
14099 return OP_X86_ADD_REG_MEMBASE;
14100 case OP_ISUB:
14101 return OP_X86_SUB_REG_MEMBASE;
14102 case OP_IAND:
14103 return OP_X86_AND_REG_MEMBASE;
14104 case OP_IOR:
14105 return OP_X86_OR_REG_MEMBASE;
14106 case OP_IXOR:
14107 return OP_X86_XOR_REG_MEMBASE;
14109 } else if ((load_opcode == OP_LOADI8_MEMBASE) || (load_opcode == OP_LOAD_MEMBASE && !cfg->backend->ilp32)) {
14110 switch (opcode) {
14111 case OP_COMPARE:
14112 case OP_LCOMPARE:
14113 return OP_AMD64_COMPARE_REG_MEMBASE;
14114 case OP_LADD:
14115 return OP_AMD64_ADD_REG_MEMBASE;
14116 case OP_LSUB:
14117 return OP_AMD64_SUB_REG_MEMBASE;
14118 case OP_LAND:
14119 return OP_AMD64_AND_REG_MEMBASE;
14120 case OP_LOR:
14121 return OP_AMD64_OR_REG_MEMBASE;
14122 case OP_LXOR:
14123 return OP_AMD64_XOR_REG_MEMBASE;
14126 #endif
14128 return -1;
14132 mono_op_to_op_imm_noemul (int opcode)
14134 switch (opcode) {
14135 #if SIZEOF_REGISTER == 4 && !defined(MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS)
14136 case OP_LSHR:
14137 case OP_LSHL:
14138 case OP_LSHR_UN:
14139 return -1;
14140 #endif
14141 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
14142 case OP_IDIV:
14143 case OP_IDIV_UN:
14144 case OP_IREM:
14145 case OP_IREM_UN:
14146 return -1;
14147 #endif
14148 #if defined(MONO_ARCH_EMULATE_MUL_DIV)
14149 case OP_IMUL:
14150 return -1;
14151 #endif
14152 default:
14153 return mono_op_to_op_imm (opcode);
14158 * mono_handle_global_vregs:
14160 * Make vregs used in more than one bblock 'global', i.e. allocate a variable
14161 * for them.
14163 void
14164 mono_handle_global_vregs (MonoCompile *cfg)
14166 gint32 *vreg_to_bb;
14167 MonoBasicBlock *bb;
14168 int i, pos;
14170 vreg_to_bb = (gint32 *)mono_mempool_alloc0 (cfg->mempool, sizeof (gint32*) * cfg->next_vreg + 1);
14172 #ifdef MONO_ARCH_SIMD_INTRINSICS
14173 if (cfg->uses_simd_intrinsics)
14174 mono_simd_simplify_indirection (cfg);
14175 #endif
14177 /* Find local vregs used in more than one bb */
14178 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
14179 MonoInst *ins = bb->code;
14180 int block_num = bb->block_num;
14182 if (cfg->verbose_level > 2)
14183 printf ("\nHANDLE-GLOBAL-VREGS BLOCK %d:\n", bb->block_num);
14185 cfg->cbb = bb;
14186 for (; ins; ins = ins->next) {
14187 const char *spec = INS_INFO (ins->opcode);
14188 int regtype = 0, regindex;
14189 gint32 prev_bb;
14191 if (G_UNLIKELY (cfg->verbose_level > 2))
14192 mono_print_ins (ins);
14194 g_assert (ins->opcode >= MONO_CEE_LAST);
14196 for (regindex = 0; regindex < 4; regindex ++) {
14197 int vreg = 0;
14199 if (regindex == 0) {
14200 regtype = spec [MONO_INST_DEST];
14201 if (regtype == ' ')
14202 continue;
14203 vreg = ins->dreg;
14204 } else if (regindex == 1) {
14205 regtype = spec [MONO_INST_SRC1];
14206 if (regtype == ' ')
14207 continue;
14208 vreg = ins->sreg1;
14209 } else if (regindex == 2) {
14210 regtype = spec [MONO_INST_SRC2];
14211 if (regtype == ' ')
14212 continue;
14213 vreg = ins->sreg2;
14214 } else if (regindex == 3) {
14215 regtype = spec [MONO_INST_SRC3];
14216 if (regtype == ' ')
14217 continue;
14218 vreg = ins->sreg3;
14221 #if SIZEOF_REGISTER == 4
14222 /* In the LLVM case, the long opcodes are not decomposed */
14223 if (regtype == 'l' && !COMPILE_LLVM (cfg)) {
14225 * Since some instructions reference the original long vreg,
14226 * and some reference the two component vregs, it is quite hard
14227 * to determine when it needs to be global. So be conservative.
14229 if (!get_vreg_to_inst (cfg, vreg)) {
14230 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int64_class->byval_arg, OP_LOCAL, vreg);
14232 if (cfg->verbose_level > 2)
14233 printf ("LONG VREG R%d made global.\n", vreg);
14237 * Make the component vregs volatile since the optimizations can
14238 * get confused otherwise.
14240 get_vreg_to_inst (cfg, MONO_LVREG_LS (vreg))->flags |= MONO_INST_VOLATILE;
14241 get_vreg_to_inst (cfg, MONO_LVREG_MS (vreg))->flags |= MONO_INST_VOLATILE;
14243 #endif
14245 g_assert (vreg != -1);
14247 prev_bb = vreg_to_bb [vreg];
14248 if (prev_bb == 0) {
14249 /* 0 is a valid block num */
14250 vreg_to_bb [vreg] = block_num + 1;
14251 } else if ((prev_bb != block_num + 1) && (prev_bb != -1)) {
14252 if (((regtype == 'i' && (vreg < MONO_MAX_IREGS))) || (regtype == 'f' && (vreg < MONO_MAX_FREGS)))
14253 continue;
14255 if (!get_vreg_to_inst (cfg, vreg)) {
14256 if (G_UNLIKELY (cfg->verbose_level > 2))
14257 printf ("VREG R%d used in BB%d and BB%d made global.\n", vreg, vreg_to_bb [vreg], block_num);
14259 switch (regtype) {
14260 case 'i':
14261 if (vreg_is_ref (cfg, vreg))
14262 mono_compile_create_var_for_vreg (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL, vreg);
14263 else
14264 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL, vreg);
14265 break;
14266 case 'l':
14267 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int64_class->byval_arg, OP_LOCAL, vreg);
14268 break;
14269 case 'f':
14270 mono_compile_create_var_for_vreg (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL, vreg);
14271 break;
14272 case 'v':
14273 mono_compile_create_var_for_vreg (cfg, &ins->klass->byval_arg, OP_LOCAL, vreg);
14274 break;
14275 default:
14276 g_assert_not_reached ();
14280 /* Flag as having been used in more than one bb */
14281 vreg_to_bb [vreg] = -1;
14287 /* If a variable is used in only one bblock, convert it into a local vreg */
14288 for (i = 0; i < cfg->num_varinfo; i++) {
14289 MonoInst *var = cfg->varinfo [i];
14290 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
14292 switch (var->type) {
14293 case STACK_I4:
14294 case STACK_OBJ:
14295 case STACK_PTR:
14296 case STACK_MP:
14297 case STACK_VTYPE:
14298 #if SIZEOF_REGISTER == 8
14299 case STACK_I8:
14300 #endif
14301 #if !defined(TARGET_X86)
14302 /* Enabling this screws up the fp stack on x86 */
14303 case STACK_R8:
14304 #endif
14305 if (mono_arch_is_soft_float ())
14306 break;
14309 if (var->type == STACK_VTYPE && cfg->gsharedvt && mini_is_gsharedvt_variable_type (var->inst_vtype))
14310 break;
14313 /* Arguments are implicitly global */
14314 /* Putting R4 vars into registers doesn't work currently */
14315 /* The gsharedvt vars are implicitly referenced by ldaddr opcodes, but those opcodes are only generated later */
14316 if ((var->opcode != OP_ARG) && (var != cfg->ret) && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)) && (vreg_to_bb [var->dreg] != -1) && (var->klass->byval_arg.type != MONO_TYPE_R4) && !cfg->disable_vreg_to_lvreg && var != cfg->gsharedvt_info_var && var != cfg->gsharedvt_locals_var && var != cfg->lmf_addr_var) {
14318 * Make that the variable's liveness interval doesn't contain a call, since
14319 * that would cause the lvreg to be spilled, making the whole optimization
14320 * useless.
14322 /* This is too slow for JIT compilation */
14323 #if 0
14324 if (cfg->compile_aot && vreg_to_bb [var->dreg]) {
14325 MonoInst *ins;
14326 int def_index, call_index, ins_index;
14327 gboolean spilled = FALSE;
14329 def_index = -1;
14330 call_index = -1;
14331 ins_index = 0;
14332 for (ins = vreg_to_bb [var->dreg]->code; ins; ins = ins->next) {
14333 const char *spec = INS_INFO (ins->opcode);
14335 if ((spec [MONO_INST_DEST] != ' ') && (ins->dreg == var->dreg))
14336 def_index = ins_index;
14338 if (((spec [MONO_INST_SRC1] != ' ') && (ins->sreg1 == var->dreg)) ||
14339 ((spec [MONO_INST_SRC1] != ' ') && (ins->sreg1 == var->dreg))) {
14340 if (call_index > def_index) {
14341 spilled = TRUE;
14342 break;
14346 if (MONO_IS_CALL (ins))
14347 call_index = ins_index;
14349 ins_index ++;
14352 if (spilled)
14353 break;
14355 #endif
14357 if (G_UNLIKELY (cfg->verbose_level > 2))
14358 printf ("CONVERTED R%d(%d) TO VREG.\n", var->dreg, vmv->idx);
14359 var->flags |= MONO_INST_IS_DEAD;
14360 cfg->vreg_to_inst [var->dreg] = NULL;
14362 break;
14367 * Compress the varinfo and vars tables so the liveness computation is faster and
14368 * takes up less space.
14370 pos = 0;
14371 for (i = 0; i < cfg->num_varinfo; ++i) {
14372 MonoInst *var = cfg->varinfo [i];
14373 if (pos < i && cfg->locals_start == i)
14374 cfg->locals_start = pos;
14375 if (!(var->flags & MONO_INST_IS_DEAD)) {
14376 if (pos < i) {
14377 cfg->varinfo [pos] = cfg->varinfo [i];
14378 cfg->varinfo [pos]->inst_c0 = pos;
14379 memcpy (&cfg->vars [pos], &cfg->vars [i], sizeof (MonoMethodVar));
14380 cfg->vars [pos].idx = pos;
14381 #if SIZEOF_REGISTER == 4
14382 if (cfg->varinfo [pos]->type == STACK_I8) {
14383 /* Modify the two component vars too */
14384 MonoInst *var1;
14386 var1 = get_vreg_to_inst (cfg, MONO_LVREG_LS (cfg->varinfo [pos]->dreg));
14387 var1->inst_c0 = pos;
14388 var1 = get_vreg_to_inst (cfg, MONO_LVREG_MS (cfg->varinfo [pos]->dreg));
14389 var1->inst_c0 = pos;
14391 #endif
14393 pos ++;
14396 cfg->num_varinfo = pos;
14397 if (cfg->locals_start > cfg->num_varinfo)
14398 cfg->locals_start = cfg->num_varinfo;
14402 * mono_allocate_gsharedvt_vars:
14404 * Allocate variables with gsharedvt types to entries in the MonoGSharedVtMethodRuntimeInfo.entries array.
14405 * Initialize cfg->gsharedvt_vreg_to_idx with the mapping between vregs and indexes.
14407 void
14408 mono_allocate_gsharedvt_vars (MonoCompile *cfg)
14410 int i;
14412 cfg->gsharedvt_vreg_to_idx = (int *)mono_mempool_alloc0 (cfg->mempool, sizeof (int) * cfg->next_vreg);
14414 for (i = 0; i < cfg->num_varinfo; ++i) {
14415 MonoInst *ins = cfg->varinfo [i];
14416 int idx;
14418 if (mini_is_gsharedvt_variable_type (ins->inst_vtype)) {
14419 if (i >= cfg->locals_start) {
14420 /* Local */
14421 idx = get_gsharedvt_info_slot (cfg, ins->inst_vtype, MONO_RGCTX_INFO_LOCAL_OFFSET);
14422 cfg->gsharedvt_vreg_to_idx [ins->dreg] = idx + 1;
14423 ins->opcode = OP_GSHAREDVT_LOCAL;
14424 ins->inst_imm = idx;
14425 } else {
14426 /* Arg */
14427 cfg->gsharedvt_vreg_to_idx [ins->dreg] = -1;
14428 ins->opcode = OP_GSHAREDVT_ARG_REGOFFSET;
14435 * mono_spill_global_vars:
14437 * Generate spill code for variables which are not allocated to registers,
14438 * and replace vregs with their allocated hregs. *need_local_opts is set to TRUE if
14439 * code is generated which could be optimized by the local optimization passes.
14441 void
14442 mono_spill_global_vars (MonoCompile *cfg, gboolean *need_local_opts)
14444 MonoBasicBlock *bb;
14445 char spec2 [16];
14446 int orig_next_vreg;
14447 guint32 *vreg_to_lvreg;
14448 guint32 *lvregs;
14449 guint32 i, lvregs_len;
14450 gboolean dest_has_lvreg = FALSE;
14451 MonoStackType stacktypes [128];
14452 MonoInst **live_range_start, **live_range_end;
14453 MonoBasicBlock **live_range_start_bb, **live_range_end_bb;
14455 *need_local_opts = FALSE;
14457 memset (spec2, 0, sizeof (spec2));
14459 /* FIXME: Move this function to mini.c */
14460 stacktypes ['i'] = STACK_PTR;
14461 stacktypes ['l'] = STACK_I8;
14462 stacktypes ['f'] = STACK_R8;
14463 #ifdef MONO_ARCH_SIMD_INTRINSICS
14464 stacktypes ['x'] = STACK_VTYPE;
14465 #endif
14467 #if SIZEOF_REGISTER == 4
14468 /* Create MonoInsts for longs */
14469 for (i = 0; i < cfg->num_varinfo; i++) {
14470 MonoInst *ins = cfg->varinfo [i];
14472 if ((ins->opcode != OP_REGVAR) && !(ins->flags & MONO_INST_IS_DEAD)) {
14473 switch (ins->type) {
14474 case STACK_R8:
14475 case STACK_I8: {
14476 MonoInst *tree;
14478 if (ins->type == STACK_R8 && !COMPILE_SOFT_FLOAT (cfg))
14479 break;
14481 g_assert (ins->opcode == OP_REGOFFSET);
14483 tree = get_vreg_to_inst (cfg, MONO_LVREG_LS (ins->dreg));
14484 g_assert (tree);
14485 tree->opcode = OP_REGOFFSET;
14486 tree->inst_basereg = ins->inst_basereg;
14487 tree->inst_offset = ins->inst_offset + MINI_LS_WORD_OFFSET;
14489 tree = get_vreg_to_inst (cfg, MONO_LVREG_MS (ins->dreg));
14490 g_assert (tree);
14491 tree->opcode = OP_REGOFFSET;
14492 tree->inst_basereg = ins->inst_basereg;
14493 tree->inst_offset = ins->inst_offset + MINI_MS_WORD_OFFSET;
14494 break;
14496 default:
14497 break;
14501 #endif
14503 if (cfg->compute_gc_maps) {
14504 /* registers need liveness info even for !non refs */
14505 for (i = 0; i < cfg->num_varinfo; i++) {
14506 MonoInst *ins = cfg->varinfo [i];
14508 if (ins->opcode == OP_REGVAR)
14509 ins->flags |= MONO_INST_GC_TRACK;
14513 /* FIXME: widening and truncation */
14516 * As an optimization, when a variable allocated to the stack is first loaded into
14517 * an lvreg, we will remember the lvreg and use it the next time instead of loading
14518 * the variable again.
14520 orig_next_vreg = cfg->next_vreg;
14521 vreg_to_lvreg = (guint32 *)mono_mempool_alloc0 (cfg->mempool, sizeof (guint32) * cfg->next_vreg);
14522 lvregs = (guint32 *)mono_mempool_alloc (cfg->mempool, sizeof (guint32) * 1024);
14523 lvregs_len = 0;
14526 * These arrays contain the first and last instructions accessing a given
14527 * variable.
14528 * Since we emit bblocks in the same order we process them here, and we
14529 * don't split live ranges, these will precisely describe the live range of
14530 * the variable, i.e. the instruction range where a valid value can be found
14531 * in the variables location.
14532 * The live range is computed using the liveness info computed by the liveness pass.
14533 * We can't use vmv->range, since that is an abstract live range, and we need
14534 * one which is instruction precise.
14535 * FIXME: Variables used in out-of-line bblocks have a hole in their live range.
14537 /* FIXME: Only do this if debugging info is requested */
14538 live_range_start = g_new0 (MonoInst*, cfg->next_vreg);
14539 live_range_end = g_new0 (MonoInst*, cfg->next_vreg);
14540 live_range_start_bb = g_new (MonoBasicBlock*, cfg->next_vreg);
14541 live_range_end_bb = g_new (MonoBasicBlock*, cfg->next_vreg);
14543 /* Add spill loads/stores */
14544 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
14545 MonoInst *ins;
14547 if (cfg->verbose_level > 2)
14548 printf ("\nSPILL BLOCK %d:\n", bb->block_num);
14550 /* Clear vreg_to_lvreg array */
14551 for (i = 0; i < lvregs_len; i++)
14552 vreg_to_lvreg [lvregs [i]] = 0;
14553 lvregs_len = 0;
14555 cfg->cbb = bb;
14556 MONO_BB_FOR_EACH_INS (bb, ins) {
14557 const char *spec = INS_INFO (ins->opcode);
14558 int regtype, srcindex, sreg, tmp_reg, prev_dreg, num_sregs;
14559 gboolean store, no_lvreg;
14560 int sregs [MONO_MAX_SRC_REGS];
14562 if (G_UNLIKELY (cfg->verbose_level > 2))
14563 mono_print_ins (ins);
14565 if (ins->opcode == OP_NOP)
14566 continue;
14569 * We handle LDADDR here as well, since it can only be decomposed
14570 * when variable addresses are known.
14572 if (ins->opcode == OP_LDADDR) {
14573 MonoInst *var = (MonoInst *)ins->inst_p0;
14575 if (var->opcode == OP_VTARG_ADDR) {
14576 /* Happens on SPARC/S390 where vtypes are passed by reference */
14577 MonoInst *vtaddr = var->inst_left;
14578 if (vtaddr->opcode == OP_REGVAR) {
14579 ins->opcode = OP_MOVE;
14580 ins->sreg1 = vtaddr->dreg;
14582 else if (var->inst_left->opcode == OP_REGOFFSET) {
14583 ins->opcode = OP_LOAD_MEMBASE;
14584 ins->inst_basereg = vtaddr->inst_basereg;
14585 ins->inst_offset = vtaddr->inst_offset;
14586 } else
14587 NOT_IMPLEMENTED;
14588 } else if (cfg->gsharedvt && cfg->gsharedvt_vreg_to_idx [var->dreg] < 0) {
14589 /* gsharedvt arg passed by ref */
14590 g_assert (var->opcode == OP_GSHAREDVT_ARG_REGOFFSET);
14592 ins->opcode = OP_LOAD_MEMBASE;
14593 ins->inst_basereg = var->inst_basereg;
14594 ins->inst_offset = var->inst_offset;
14595 } else if (cfg->gsharedvt && cfg->gsharedvt_vreg_to_idx [var->dreg]) {
14596 MonoInst *load, *load2, *load3;
14597 int idx = cfg->gsharedvt_vreg_to_idx [var->dreg] - 1;
14598 int reg1, reg2, reg3;
14599 MonoInst *info_var = cfg->gsharedvt_info_var;
14600 MonoInst *locals_var = cfg->gsharedvt_locals_var;
14603 * gsharedvt local.
14604 * Compute the address of the local as gsharedvt_locals_var + gsharedvt_info_var->locals_offsets [idx].
14607 g_assert (var->opcode == OP_GSHAREDVT_LOCAL);
14609 g_assert (info_var);
14610 g_assert (locals_var);
14612 /* Mark the instruction used to compute the locals var as used */
14613 cfg->gsharedvt_locals_var_ins = NULL;
14615 /* Load the offset */
14616 if (info_var->opcode == OP_REGOFFSET) {
14617 reg1 = alloc_ireg (cfg);
14618 NEW_LOAD_MEMBASE (cfg, load, OP_LOAD_MEMBASE, reg1, info_var->inst_basereg, info_var->inst_offset);
14619 } else if (info_var->opcode == OP_REGVAR) {
14620 load = NULL;
14621 reg1 = info_var->dreg;
14622 } else {
14623 g_assert_not_reached ();
14625 reg2 = alloc_ireg (cfg);
14626 NEW_LOAD_MEMBASE (cfg, load2, OP_LOADI4_MEMBASE, reg2, reg1, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, entries) + (idx * sizeof (gpointer)));
14627 /* Load the locals area address */
14628 reg3 = alloc_ireg (cfg);
14629 if (locals_var->opcode == OP_REGOFFSET) {
14630 NEW_LOAD_MEMBASE (cfg, load3, OP_LOAD_MEMBASE, reg3, locals_var->inst_basereg, locals_var->inst_offset);
14631 } else if (locals_var->opcode == OP_REGVAR) {
14632 NEW_UNALU (cfg, load3, OP_MOVE, reg3, locals_var->dreg);
14633 } else {
14634 g_assert_not_reached ();
14636 /* Compute the address */
14637 ins->opcode = OP_PADD;
14638 ins->sreg1 = reg3;
14639 ins->sreg2 = reg2;
14641 mono_bblock_insert_before_ins (bb, ins, load3);
14642 mono_bblock_insert_before_ins (bb, load3, load2);
14643 if (load)
14644 mono_bblock_insert_before_ins (bb, load2, load);
14645 } else {
14646 g_assert (var->opcode == OP_REGOFFSET);
14648 ins->opcode = OP_ADD_IMM;
14649 ins->sreg1 = var->inst_basereg;
14650 ins->inst_imm = var->inst_offset;
14653 *need_local_opts = TRUE;
14654 spec = INS_INFO (ins->opcode);
14657 if (ins->opcode < MONO_CEE_LAST) {
14658 mono_print_ins (ins);
14659 g_assert_not_reached ();
14663 * Store opcodes have destbasereg in the dreg, but in reality, it is an
14664 * src register.
14665 * FIXME:
14667 if (MONO_IS_STORE_MEMBASE (ins)) {
14668 tmp_reg = ins->dreg;
14669 ins->dreg = ins->sreg2;
14670 ins->sreg2 = tmp_reg;
14671 store = TRUE;
14673 spec2 [MONO_INST_DEST] = ' ';
14674 spec2 [MONO_INST_SRC1] = spec [MONO_INST_SRC1];
14675 spec2 [MONO_INST_SRC2] = spec [MONO_INST_DEST];
14676 spec2 [MONO_INST_SRC3] = ' ';
14677 spec = spec2;
14678 } else if (MONO_IS_STORE_MEMINDEX (ins))
14679 g_assert_not_reached ();
14680 else
14681 store = FALSE;
14682 no_lvreg = FALSE;
14684 if (G_UNLIKELY (cfg->verbose_level > 2)) {
14685 printf ("\t %.3s %d", spec, ins->dreg);
14686 num_sregs = mono_inst_get_src_registers (ins, sregs);
14687 for (srcindex = 0; srcindex < num_sregs; ++srcindex)
14688 printf (" %d", sregs [srcindex]);
14689 printf ("\n");
14692 /***************/
14693 /* DREG */
14694 /***************/
14695 regtype = spec [MONO_INST_DEST];
14696 g_assert (((ins->dreg == -1) && (regtype == ' ')) || ((ins->dreg != -1) && (regtype != ' ')));
14697 prev_dreg = -1;
14699 if ((ins->dreg != -1) && get_vreg_to_inst (cfg, ins->dreg)) {
14700 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
14701 MonoInst *store_ins;
14702 int store_opcode;
14703 MonoInst *def_ins = ins;
14704 int dreg = ins->dreg; /* The original vreg */
14706 store_opcode = mono_type_to_store_membase (cfg, var->inst_vtype);
14708 if (var->opcode == OP_REGVAR) {
14709 ins->dreg = var->dreg;
14710 } else if ((ins->dreg == ins->sreg1) && (spec [MONO_INST_DEST] == 'i') && (spec [MONO_INST_SRC1] == 'i') && !vreg_to_lvreg [ins->dreg] && (op_to_op_dest_membase (store_opcode, ins->opcode) != -1)) {
14712 * Instead of emitting a load+store, use a _membase opcode.
14714 g_assert (var->opcode == OP_REGOFFSET);
14715 if (ins->opcode == OP_MOVE) {
14716 NULLIFY_INS (ins);
14717 def_ins = NULL;
14718 } else {
14719 ins->opcode = op_to_op_dest_membase (store_opcode, ins->opcode);
14720 ins->inst_basereg = var->inst_basereg;
14721 ins->inst_offset = var->inst_offset;
14722 ins->dreg = -1;
14724 spec = INS_INFO (ins->opcode);
14725 } else {
14726 guint32 lvreg;
14728 g_assert (var->opcode == OP_REGOFFSET);
14730 prev_dreg = ins->dreg;
14732 /* Invalidate any previous lvreg for this vreg */
14733 vreg_to_lvreg [ins->dreg] = 0;
14735 lvreg = 0;
14737 if (COMPILE_SOFT_FLOAT (cfg) && store_opcode == OP_STORER8_MEMBASE_REG) {
14738 regtype = 'l';
14739 store_opcode = OP_STOREI8_MEMBASE_REG;
14742 ins->dreg = alloc_dreg (cfg, stacktypes [regtype]);
14744 #if SIZEOF_REGISTER != 8
14745 if (regtype == 'l') {
14746 NEW_STORE_MEMBASE (cfg, store_ins, OP_STOREI4_MEMBASE_REG, var->inst_basereg, var->inst_offset + MINI_LS_WORD_OFFSET, MONO_LVREG_LS (ins->dreg));
14747 mono_bblock_insert_after_ins (bb, ins, store_ins);
14748 NEW_STORE_MEMBASE (cfg, store_ins, OP_STOREI4_MEMBASE_REG, var->inst_basereg, var->inst_offset + MINI_MS_WORD_OFFSET, MONO_LVREG_MS (ins->dreg));
14749 mono_bblock_insert_after_ins (bb, ins, store_ins);
14750 def_ins = store_ins;
14752 else
14753 #endif
14755 g_assert (store_opcode != OP_STOREV_MEMBASE);
14757 /* Try to fuse the store into the instruction itself */
14758 /* FIXME: Add more instructions */
14759 if (!lvreg && ((ins->opcode == OP_ICONST) || ((ins->opcode == OP_I8CONST) && (ins->inst_c0 == 0)))) {
14760 ins->opcode = store_membase_reg_to_store_membase_imm (store_opcode);
14761 ins->inst_imm = ins->inst_c0;
14762 ins->inst_destbasereg = var->inst_basereg;
14763 ins->inst_offset = var->inst_offset;
14764 spec = INS_INFO (ins->opcode);
14765 } else if (!lvreg && ((ins->opcode == OP_MOVE) || (ins->opcode == OP_FMOVE) || (ins->opcode == OP_LMOVE) || (ins->opcode == OP_RMOVE))) {
14766 ins->opcode = store_opcode;
14767 ins->inst_destbasereg = var->inst_basereg;
14768 ins->inst_offset = var->inst_offset;
14770 no_lvreg = TRUE;
14772 tmp_reg = ins->dreg;
14773 ins->dreg = ins->sreg2;
14774 ins->sreg2 = tmp_reg;
14775 store = TRUE;
14777 spec2 [MONO_INST_DEST] = ' ';
14778 spec2 [MONO_INST_SRC1] = spec [MONO_INST_SRC1];
14779 spec2 [MONO_INST_SRC2] = spec [MONO_INST_DEST];
14780 spec2 [MONO_INST_SRC3] = ' ';
14781 spec = spec2;
14782 } else if (!lvreg && (op_to_op_store_membase (store_opcode, ins->opcode) != -1)) {
14783 // FIXME: The backends expect the base reg to be in inst_basereg
14784 ins->opcode = op_to_op_store_membase (store_opcode, ins->opcode);
14785 ins->dreg = -1;
14786 ins->inst_basereg = var->inst_basereg;
14787 ins->inst_offset = var->inst_offset;
14788 spec = INS_INFO (ins->opcode);
14789 } else {
14790 /* printf ("INS: "); mono_print_ins (ins); */
14791 /* Create a store instruction */
14792 NEW_STORE_MEMBASE (cfg, store_ins, store_opcode, var->inst_basereg, var->inst_offset, ins->dreg);
14794 /* Insert it after the instruction */
14795 mono_bblock_insert_after_ins (bb, ins, store_ins);
14797 def_ins = store_ins;
14800 * We can't assign ins->dreg to var->dreg here, since the
14801 * sregs could use it. So set a flag, and do it after
14802 * the sregs.
14804 if ((!cfg->backend->use_fpstack || ((store_opcode != OP_STORER8_MEMBASE_REG) && (store_opcode != OP_STORER4_MEMBASE_REG))) && !((var)->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
14805 dest_has_lvreg = TRUE;
14810 if (def_ins && !live_range_start [dreg]) {
14811 live_range_start [dreg] = def_ins;
14812 live_range_start_bb [dreg] = bb;
14815 if (cfg->compute_gc_maps && def_ins && (var->flags & MONO_INST_GC_TRACK)) {
14816 MonoInst *tmp;
14818 MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_DEF);
14819 tmp->inst_c1 = dreg;
14820 mono_bblock_insert_after_ins (bb, def_ins, tmp);
14824 /************/
14825 /* SREGS */
14826 /************/
14827 num_sregs = mono_inst_get_src_registers (ins, sregs);
14828 for (srcindex = 0; srcindex < 3; ++srcindex) {
14829 regtype = spec [MONO_INST_SRC1 + srcindex];
14830 sreg = sregs [srcindex];
14832 g_assert (((sreg == -1) && (regtype == ' ')) || ((sreg != -1) && (regtype != ' ')));
14833 if ((sreg != -1) && get_vreg_to_inst (cfg, sreg)) {
14834 MonoInst *var = get_vreg_to_inst (cfg, sreg);
14835 MonoInst *use_ins = ins;
14836 MonoInst *load_ins;
14837 guint32 load_opcode;
14839 if (var->opcode == OP_REGVAR) {
14840 sregs [srcindex] = var->dreg;
14841 //mono_inst_set_src_registers (ins, sregs);
14842 live_range_end [sreg] = use_ins;
14843 live_range_end_bb [sreg] = bb;
14845 if (cfg->compute_gc_maps && var->dreg < orig_next_vreg && (var->flags & MONO_INST_GC_TRACK)) {
14846 MonoInst *tmp;
14848 MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_USE);
14849 /* var->dreg is a hreg */
14850 tmp->inst_c1 = sreg;
14851 mono_bblock_insert_after_ins (bb, ins, tmp);
14854 continue;
14857 g_assert (var->opcode == OP_REGOFFSET);
14859 load_opcode = mono_type_to_load_membase (cfg, var->inst_vtype);
14861 g_assert (load_opcode != OP_LOADV_MEMBASE);
14863 if (vreg_to_lvreg [sreg]) {
14864 g_assert (vreg_to_lvreg [sreg] != -1);
14866 /* The variable is already loaded to an lvreg */
14867 if (G_UNLIKELY (cfg->verbose_level > 2))
14868 printf ("\t\tUse lvreg R%d for R%d.\n", vreg_to_lvreg [sreg], sreg);
14869 sregs [srcindex] = vreg_to_lvreg [sreg];
14870 //mono_inst_set_src_registers (ins, sregs);
14871 continue;
14874 /* Try to fuse the load into the instruction */
14875 if ((srcindex == 0) && (op_to_op_src1_membase (cfg, load_opcode, ins->opcode) != -1)) {
14876 ins->opcode = op_to_op_src1_membase (cfg, load_opcode, ins->opcode);
14877 sregs [0] = var->inst_basereg;
14878 //mono_inst_set_src_registers (ins, sregs);
14879 ins->inst_offset = var->inst_offset;
14880 } else if ((srcindex == 1) && (op_to_op_src2_membase (cfg, load_opcode, ins->opcode) != -1)) {
14881 ins->opcode = op_to_op_src2_membase (cfg, load_opcode, ins->opcode);
14882 sregs [1] = var->inst_basereg;
14883 //mono_inst_set_src_registers (ins, sregs);
14884 ins->inst_offset = var->inst_offset;
14885 } else {
14886 if (MONO_IS_REAL_MOVE (ins)) {
14887 ins->opcode = OP_NOP;
14888 sreg = ins->dreg;
14889 } else {
14890 //printf ("%d ", srcindex); mono_print_ins (ins);
14892 sreg = alloc_dreg (cfg, stacktypes [regtype]);
14894 if ((!cfg->backend->use_fpstack || ((load_opcode != OP_LOADR8_MEMBASE) && (load_opcode != OP_LOADR4_MEMBASE))) && !((var)->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)) && !no_lvreg) {
14895 if (var->dreg == prev_dreg) {
14897 * sreg refers to the value loaded by the load
14898 * emitted below, but we need to use ins->dreg
14899 * since it refers to the store emitted earlier.
14901 sreg = ins->dreg;
14903 g_assert (sreg != -1);
14904 vreg_to_lvreg [var->dreg] = sreg;
14905 g_assert (lvregs_len < 1024);
14906 lvregs [lvregs_len ++] = var->dreg;
14910 sregs [srcindex] = sreg;
14911 //mono_inst_set_src_registers (ins, sregs);
14913 #if SIZEOF_REGISTER != 8
14914 if (regtype == 'l') {
14915 NEW_LOAD_MEMBASE (cfg, load_ins, OP_LOADI4_MEMBASE, MONO_LVREG_MS (sreg), var->inst_basereg, var->inst_offset + MINI_MS_WORD_OFFSET);
14916 mono_bblock_insert_before_ins (bb, ins, load_ins);
14917 NEW_LOAD_MEMBASE (cfg, load_ins, OP_LOADI4_MEMBASE, MONO_LVREG_LS (sreg), var->inst_basereg, var->inst_offset + MINI_LS_WORD_OFFSET);
14918 mono_bblock_insert_before_ins (bb, ins, load_ins);
14919 use_ins = load_ins;
14921 else
14922 #endif
14924 #if SIZEOF_REGISTER == 4
14925 g_assert (load_opcode != OP_LOADI8_MEMBASE);
14926 #endif
14927 NEW_LOAD_MEMBASE (cfg, load_ins, load_opcode, sreg, var->inst_basereg, var->inst_offset);
14928 mono_bblock_insert_before_ins (bb, ins, load_ins);
14929 use_ins = load_ins;
14933 if (var->dreg < orig_next_vreg) {
14934 live_range_end [var->dreg] = use_ins;
14935 live_range_end_bb [var->dreg] = bb;
14938 if (cfg->compute_gc_maps && var->dreg < orig_next_vreg && (var->flags & MONO_INST_GC_TRACK)) {
14939 MonoInst *tmp;
14941 MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_USE);
14942 tmp->inst_c1 = var->dreg;
14943 mono_bblock_insert_after_ins (bb, ins, tmp);
14947 mono_inst_set_src_registers (ins, sregs);
14949 if (dest_has_lvreg) {
14950 g_assert (ins->dreg != -1);
14951 vreg_to_lvreg [prev_dreg] = ins->dreg;
14952 g_assert (lvregs_len < 1024);
14953 lvregs [lvregs_len ++] = prev_dreg;
14954 dest_has_lvreg = FALSE;
14957 if (store) {
14958 tmp_reg = ins->dreg;
14959 ins->dreg = ins->sreg2;
14960 ins->sreg2 = tmp_reg;
14963 if (MONO_IS_CALL (ins)) {
14964 /* Clear vreg_to_lvreg array */
14965 for (i = 0; i < lvregs_len; i++)
14966 vreg_to_lvreg [lvregs [i]] = 0;
14967 lvregs_len = 0;
14968 } else if (ins->opcode == OP_NOP) {
14969 ins->dreg = -1;
14970 MONO_INST_NULLIFY_SREGS (ins);
14973 if (cfg->verbose_level > 2)
14974 mono_print_ins_index (1, ins);
14977 /* Extend the live range based on the liveness info */
14978 if (cfg->compute_precise_live_ranges && bb->live_out_set && bb->code) {
14979 for (i = 0; i < cfg->num_varinfo; i ++) {
14980 MonoMethodVar *vi = MONO_VARINFO (cfg, i);
14982 if (vreg_is_volatile (cfg, vi->vreg))
14983 /* The liveness info is incomplete */
14984 continue;
14986 if (mono_bitset_test_fast (bb->live_in_set, i) && !live_range_start [vi->vreg]) {
14987 /* Live from at least the first ins of this bb */
14988 live_range_start [vi->vreg] = bb->code;
14989 live_range_start_bb [vi->vreg] = bb;
14992 if (mono_bitset_test_fast (bb->live_out_set, i)) {
14993 /* Live at least until the last ins of this bb */
14994 live_range_end [vi->vreg] = bb->last_ins;
14995 live_range_end_bb [vi->vreg] = bb;
15002 * Emit LIVERANGE_START/LIVERANGE_END opcodes, the backend will implement them
15003 * by storing the current native offset into MonoMethodVar->live_range_start/end.
15005 if (cfg->backend->have_liverange_ops && cfg->compute_precise_live_ranges && cfg->comp_done & MONO_COMP_LIVENESS) {
15006 for (i = 0; i < cfg->num_varinfo; ++i) {
15007 int vreg = MONO_VARINFO (cfg, i)->vreg;
15008 MonoInst *ins;
15010 if (live_range_start [vreg]) {
15011 MONO_INST_NEW (cfg, ins, OP_LIVERANGE_START);
15012 ins->inst_c0 = i;
15013 ins->inst_c1 = vreg;
15014 mono_bblock_insert_after_ins (live_range_start_bb [vreg], live_range_start [vreg], ins);
15016 if (live_range_end [vreg]) {
15017 MONO_INST_NEW (cfg, ins, OP_LIVERANGE_END);
15018 ins->inst_c0 = i;
15019 ins->inst_c1 = vreg;
15020 if (live_range_end [vreg] == live_range_end_bb [vreg]->last_ins)
15021 mono_add_ins_to_end (live_range_end_bb [vreg], ins);
15022 else
15023 mono_bblock_insert_after_ins (live_range_end_bb [vreg], live_range_end [vreg], ins);
15028 if (cfg->gsharedvt_locals_var_ins) {
15029 /* Nullify if unused */
15030 cfg->gsharedvt_locals_var_ins->opcode = OP_PCONST;
15031 cfg->gsharedvt_locals_var_ins->inst_imm = 0;
15034 g_free (live_range_start);
15035 g_free (live_range_end);
15036 g_free (live_range_start_bb);
15037 g_free (live_range_end_bb);
15041 * FIXME:
15042 * - use 'iadd' instead of 'int_add'
15043 * - handling ovf opcodes: decompose in method_to_ir.
15044 * - unify iregs/fregs
15045 * -> partly done, the missing parts are:
15046 * - a more complete unification would involve unifying the hregs as well, so
15047 * code wouldn't need if (fp) all over the place. but that would mean the hregs
15048 * would no longer map to the machine hregs, so the code generators would need to
15049 * be modified. Also, on ia64 for example, niregs + nfregs > 256 -> bitmasks
15050 * wouldn't work any more. Duplicating the code in mono_local_regalloc () into
15051 * fp/non-fp branches speeds it up by about 15%.
15052 * - use sext/zext opcodes instead of shifts
15053 * - add OP_ICALL
15054 * - get rid of TEMPLOADs if possible and use vregs instead
15055 * - clean up usage of OP_P/OP_ opcodes
15056 * - cleanup usage of DUMMY_USE
15057 * - cleanup the setting of ins->type for MonoInst's which are pushed on the
15058 * stack
15059 * - set the stack type and allocate a dreg in the EMIT_NEW macros
15060 * - get rid of all the <foo>2 stuff when the new JIT is ready.
15061 * - make sure handle_stack_args () is called before the branch is emitted
15062 * - when the new IR is done, get rid of all unused stuff
15063 * - COMPARE/BEQ as separate instructions or unify them ?
15064 * - keeping them separate allows specialized compare instructions like
15065 * compare_imm, compare_membase
15066 * - most back ends unify fp compare+branch, fp compare+ceq
15067 * - integrate mono_save_args into inline_method
15068 * - get rid of the empty bblocks created by MONO_EMIT_NEW_BRACH_BLOCK2
15069 * - handle long shift opts on 32 bit platforms somehow: they require
15070 * 3 sregs (2 for arg1 and 1 for arg2)
15071 * - make byref a 'normal' type.
15072 * - use vregs for bb->out_stacks if possible, handle_global_vreg will make them a
15073 * variable if needed.
15074 * - do not start a new IL level bblock when cfg->cbb is changed by a function call
15075 * like inline_method.
15076 * - remove inlining restrictions
15077 * - fix LNEG and enable cfold of INEG
15078 * - generalize x86 optimizations like ldelema as a peephole optimization
15079 * - add store_mem_imm for amd64
15080 * - optimize the loading of the interruption flag in the managed->native wrappers
15081 * - avoid special handling of OP_NOP in passes
15082 * - move code inserting instructions into one function/macro.
15083 * - try a coalescing phase after liveness analysis
15084 * - add float -> vreg conversion + local optimizations on !x86
15085 * - figure out how to handle decomposed branches during optimizations, ie.
15086 * compare+branch, op_jump_table+op_br etc.
15087 * - promote RuntimeXHandles to vregs
15088 * - vtype cleanups:
15089 * - add a NEW_VARLOADA_VREG macro
15090 * - the vtype optimizations are blocked by the LDADDR opcodes generated for
15091 * accessing vtype fields.
15092 * - get rid of I8CONST on 64 bit platforms
15093 * - dealing with the increase in code size due to branches created during opcode
15094 * decomposition:
15095 * - use extended basic blocks
15096 * - all parts of the JIT
15097 * - handle_global_vregs () && local regalloc
15098 * - avoid introducing global vregs during decomposition, like 'vtable' in isinst
15099 * - sources of increase in code size:
15100 * - vtypes
15101 * - long compares
15102 * - isinst and castclass
15103 * - lvregs not allocated to global registers even if used multiple times
15104 * - call cctors outside the JIT, to make -v output more readable and JIT timings more
15105 * meaningful.
15106 * - check for fp stack leakage in other opcodes too. (-> 'exceptions' optimization)
15107 * - add all micro optimizations from the old JIT
15108 * - put tree optimizations into the deadce pass
15109 * - decompose op_start_handler/op_endfilter/op_endfinally earlier using an arch
15110 * specific function.
15111 * - unify the float comparison opcodes with the other comparison opcodes, i.e.
15112 * fcompare + branchCC.
15113 * - create a helper function for allocating a stack slot, taking into account
15114 * MONO_CFG_HAS_SPILLUP.
15115 * - merge r68207.
15116 * - merge the ia64 switch changes.
15117 * - optimize mono_regstate2_alloc_int/float.
15118 * - fix the pessimistic handling of variables accessed in exception handler blocks.
15119 * - need to write a tree optimization pass, but the creation of trees is difficult, i.e.
15120 * parts of the tree could be separated by other instructions, killing the tree
15121 * arguments, or stores killing loads etc. Also, should we fold loads into other
15122 * instructions if the result of the load is used multiple times ?
15123 * - make the REM_IMM optimization in mini-x86.c arch-independent.
15124 * - LAST MERGE: 108395.
15125 * - when returning vtypes in registers, generate IR and append it to the end of the
15126 * last bb instead of doing it in the epilog.
15127 * - change the store opcodes so they use sreg1 instead of dreg to store the base register.
15132 NOTES
15133 -----
15135 - When to decompose opcodes:
15136 - earlier: this makes some optimizations hard to implement, since the low level IR
15137 no longer contains the neccessary information. But it is easier to do.
15138 - later: harder to implement, enables more optimizations.
15139 - Branches inside bblocks:
15140 - created when decomposing complex opcodes.
15141 - branches to another bblock: harmless, but not tracked by the branch
15142 optimizations, so need to branch to a label at the start of the bblock.
15143 - branches to inside the same bblock: very problematic, trips up the local
15144 reg allocator. Can be fixed by spitting the current bblock, but that is a
15145 complex operation, since some local vregs can become global vregs etc.
15146 - Local/global vregs:
15147 - local vregs: temporary vregs used inside one bblock. Assigned to hregs by the
15148 local register allocator.
15149 - global vregs: used in more than one bblock. Have an associated MonoMethodVar
15150 structure, created by mono_create_var (). Assigned to hregs or the stack by
15151 the global register allocator.
15152 - When to do optimizations like alu->alu_imm:
15153 - earlier -> saves work later on since the IR will be smaller/simpler
15154 - later -> can work on more instructions
15155 - Handling of valuetypes:
15156 - When a vtype is pushed on the stack, a new temporary is created, an
15157 instruction computing its address (LDADDR) is emitted and pushed on
15158 the stack. Need to optimize cases when the vtype is used immediately as in
15159 argument passing, stloc etc.
15160 - Instead of the to_end stuff in the old JIT, simply call the function handling
15161 the values on the stack before emitting the last instruction of the bb.
15164 #endif /* DISABLE_JIT */