Merge pull request #2696 from lambdageek/dev/unlock-mono_class_create_runtime_vtable
[mono.git] / mono / mini / method-to-ir.c
1 /*
2  * method-to-ir.c: Convert CIL to the JIT internal representation
3  *
4  * Author:
5  *   Paolo Molaro (lupus@ximian.com)
6  *   Dietmar Maurer (dietmar@ximian.com)
7  *
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  */
12
13 #include <config.h>
14
15 #ifndef DISABLE_JIT
16
17 #include <signal.h>
18
19 #ifdef HAVE_UNISTD_H
20 #include <unistd.h>
21 #endif
22
23 #include <math.h>
24 #include <string.h>
25 #include <ctype.h>
26
27 #ifdef HAVE_SYS_TIME_H
28 #include <sys/time.h>
29 #endif
30
31 #ifdef HAVE_ALLOCA_H
32 #include <alloca.h>
33 #endif
34
35 #include <mono/utils/memcheck.h>
36 #include "mini.h"
37 #include <mono/metadata/abi-details.h>
38 #include <mono/metadata/assembly.h>
39 #include <mono/metadata/attrdefs.h>
40 #include <mono/metadata/loader.h>
41 #include <mono/metadata/tabledefs.h>
42 #include <mono/metadata/class.h>
43 #include <mono/metadata/object.h>
44 #include <mono/metadata/exception.h>
45 #include <mono/metadata/opcodes.h>
46 #include <mono/metadata/mono-endian.h>
47 #include <mono/metadata/tokentype.h>
48 #include <mono/metadata/tabledefs.h>
49 #include <mono/metadata/marshal.h>
50 #include <mono/metadata/debug-helpers.h>
51 #include <mono/metadata/mono-debug.h>
52 #include <mono/metadata/mono-debug-debugger.h>
53 #include <mono/metadata/gc-internals.h>
54 #include <mono/metadata/security-manager.h>
55 #include <mono/metadata/threads-types.h>
56 #include <mono/metadata/security-core-clr.h>
57 #include <mono/metadata/profiler-private.h>
58 #include <mono/metadata/profiler.h>
59 #include <mono/metadata/monitor.h>
60 #include <mono/metadata/debug-mono-symfile.h>
61 #include <mono/utils/mono-compiler.h>
62 #include <mono/utils/mono-memory-model.h>
63 #include <mono/metadata/mono-basic-block.h>
64 #include <mono/metadata/reflection-internals.h>
65
66 #include "trace.h"
67
68 #include "ir-emit.h"
69
70 #include "jit-icalls.h"
71 #include "jit.h"
72 #include "debugger-agent.h"
73 #include "seq-points.h"
74 #include "aot-compiler.h"
75 #include "mini-llvm.h"
76
77 #define BRANCH_COST 10
78 #define INLINE_LENGTH_LIMIT 20
79
80 /* These have 'cfg' as an implicit argument */
81 #define INLINE_FAILURE(msg) do {                                                                        \
82         if ((cfg->method != cfg->current_method) && (cfg->current_method->wrapper_type == MONO_WRAPPER_NONE)) { \
83                 inline_failure (cfg, msg);                                                                              \
84                 goto exception_exit;                                                                                    \
85         } \
86         } while (0)
87 #define CHECK_CFG_EXCEPTION do {\
88                 if (cfg->exception_type != MONO_EXCEPTION_NONE) \
89                         goto exception_exit;                                            \
90         } while (0)
91 #define METHOD_ACCESS_FAILURE(method, cmethod) do {                     \
92                 method_access_failure ((cfg), (method), (cmethod));                     \
93                 goto exception_exit;                                                                            \
94         } while (0)
95 #define FIELD_ACCESS_FAILURE(method, field) do {                                        \
96                 field_access_failure ((cfg), (method), (field));                        \
97                 goto exception_exit;    \
98         } while (0)
99 #define GENERIC_SHARING_FAILURE(opcode) do {            \
100                 if (cfg->gshared) {                                                                     \
101                         gshared_failure (cfg, opcode, __FILE__, __LINE__);      \
102                         goto exception_exit;    \
103                 }                       \
104         } while (0)
105 #define GSHAREDVT_FAILURE(opcode) do {          \
106         if (cfg->gsharedvt) {                                                                                           \
107                 gsharedvt_failure (cfg, opcode, __FILE__, __LINE__);                    \
108                 goto exception_exit;                                                                                    \
109         }                                                                                                                                       \
110         } while (0)
111 #define OUT_OF_MEMORY_FAILURE do {      \
112                 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);                \
113                 mono_error_set_out_of_memory (&cfg->error, "");                                 \
114                 goto exception_exit;    \
115         } while (0)
116 #define DISABLE_AOT(cfg) do { \
117                 if ((cfg)->verbose_level >= 2)                                            \
118                         printf ("AOT disabled: %s:%d\n", __FILE__, __LINE__);   \
119                 (cfg)->disable_aot = TRUE;                                                        \
120         } while (0)
121 #define LOAD_ERROR do { \
122                 break_on_unverified ();                                                         \
123                 mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD); \
124                 goto exception_exit;                                                                    \
125         } while (0)
126
127 #define TYPE_LOAD_ERROR(klass) do { \
128                 cfg->exception_ptr = klass; \
129                 LOAD_ERROR;                                     \
130         } while (0)
131
132 #define CHECK_CFG_ERROR do {\
133                 if (!mono_error_ok (&cfg->error)) { \
134                         mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);        \
135                         goto mono_error_exit; \
136                 } \
137         } while (0)
138
139 /* Determine whenever 'ins' represents a load of the 'this' argument */
140 #define MONO_CHECK_THIS(ins) (mono_method_signature (cfg->method)->hasthis && ((ins)->opcode == OP_MOVE) && ((ins)->sreg1 == cfg->args [0]->dreg))
141
142 static int ldind_to_load_membase (int opcode);
143 static int stind_to_store_membase (int opcode);
144
145 int mono_op_to_op_imm (int opcode);
146 int mono_op_to_op_imm_noemul (int opcode);
147
148 MONO_API MonoInst* mono_emit_native_call (MonoCompile *cfg, gconstpointer func, MonoMethodSignature *sig, MonoInst **args);
149
150 static int inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp,
151                                                   guchar *ip, guint real_offset, gboolean inline_always);
152 static MonoInst*
153 emit_llvmonly_virtual_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, int context_used, MonoInst **sp);
154
155 /* helper methods signatures */
156 static MonoMethodSignature *helper_sig_domain_get;
157 static MonoMethodSignature *helper_sig_rgctx_lazy_fetch_trampoline;
158 static MonoMethodSignature *helper_sig_llvmonly_imt_thunk;
159
160
161 /* type loading helpers */
162 static GENERATE_GET_CLASS_WITH_CACHE (runtime_helpers, System.Runtime.CompilerServices, RuntimeHelpers)
163 static GENERATE_TRY_GET_CLASS_WITH_CACHE (debuggable_attribute, System.Diagnostics, DebuggableAttribute)
164
165 /*
166  * Instruction metadata
167  */
168 #ifdef MINI_OP
169 #undef MINI_OP
170 #endif
171 #ifdef MINI_OP3
172 #undef MINI_OP3
173 #endif
174 #define MINI_OP(a,b,dest,src1,src2) dest, src1, src2, ' ',
175 #define MINI_OP3(a,b,dest,src1,src2,src3) dest, src1, src2, src3,
176 #define NONE ' '
177 #define IREG 'i'
178 #define FREG 'f'
179 #define VREG 'v'
180 #define XREG 'x'
181 #if SIZEOF_REGISTER == 8 && SIZEOF_REGISTER == SIZEOF_VOID_P
182 #define LREG IREG
183 #else
184 #define LREG 'l'
185 #endif
186 /* keep in sync with the enum in mini.h */
187 const char
188 ins_info[] = {
189 #include "mini-ops.h"
190 };
191 #undef MINI_OP
192 #undef MINI_OP3
193
194 #define MINI_OP(a,b,dest,src1,src2) ((src2) != NONE ? 2 : ((src1) != NONE ? 1 : 0)),
195 #define MINI_OP3(a,b,dest,src1,src2,src3) ((src3) != NONE ? 3 : ((src2) != NONE ? 2 : ((src1) != NONE ? 1 : 0))),
196 /* 
197  * This should contain the index of the last sreg + 1. This is not the same
198  * as the number of sregs for opcodes like IA64_CMP_EQ_IMM.
199  */
200 const gint8 ins_sreg_counts[] = {
201 #include "mini-ops.h"
202 };
203 #undef MINI_OP
204 #undef MINI_OP3
205
206 #define MONO_INIT_VARINFO(vi,id) do { \
207         (vi)->range.first_use.pos.bid = 0xffff; \
208         (vi)->reg = -1; \
209         (vi)->idx = (id); \
210 } while (0)
211
212 guint32
213 mono_alloc_ireg (MonoCompile *cfg)
214 {
215         return alloc_ireg (cfg);
216 }
217
218 guint32
219 mono_alloc_lreg (MonoCompile *cfg)
220 {
221         return alloc_lreg (cfg);
222 }
223
224 guint32
225 mono_alloc_freg (MonoCompile *cfg)
226 {
227         return alloc_freg (cfg);
228 }
229
230 guint32
231 mono_alloc_preg (MonoCompile *cfg)
232 {
233         return alloc_preg (cfg);
234 }
235
236 guint32
237 mono_alloc_dreg (MonoCompile *cfg, MonoStackType stack_type)
238 {
239         return alloc_dreg (cfg, stack_type);
240 }
241
242 /*
243  * mono_alloc_ireg_ref:
244  *
245  *   Allocate an IREG, and mark it as holding a GC ref.
246  */
247 guint32
248 mono_alloc_ireg_ref (MonoCompile *cfg)
249 {
250         return alloc_ireg_ref (cfg);
251 }
252
253 /*
254  * mono_alloc_ireg_mp:
255  *
256  *   Allocate an IREG, and mark it as holding a managed pointer.
257  */
258 guint32
259 mono_alloc_ireg_mp (MonoCompile *cfg)
260 {
261         return alloc_ireg_mp (cfg);
262 }
263
264 /*
265  * mono_alloc_ireg_copy:
266  *
267  *   Allocate an IREG with the same GC type as VREG.
268  */
269 guint32
270 mono_alloc_ireg_copy (MonoCompile *cfg, guint32 vreg)
271 {
272         if (vreg_is_ref (cfg, vreg))
273                 return alloc_ireg_ref (cfg);
274         else if (vreg_is_mp (cfg, vreg))
275                 return alloc_ireg_mp (cfg);
276         else
277                 return alloc_ireg (cfg);
278 }
279
280 guint
281 mono_type_to_regmove (MonoCompile *cfg, MonoType *type)
282 {
283         if (type->byref)
284                 return OP_MOVE;
285
286         type = mini_get_underlying_type (type);
287 handle_enum:
288         switch (type->type) {
289         case MONO_TYPE_I1:
290         case MONO_TYPE_U1:
291                 return OP_MOVE;
292         case MONO_TYPE_I2:
293         case MONO_TYPE_U2:
294                 return OP_MOVE;
295         case MONO_TYPE_I4:
296         case MONO_TYPE_U4:
297                 return OP_MOVE;
298         case MONO_TYPE_I:
299         case MONO_TYPE_U:
300         case MONO_TYPE_PTR:
301         case MONO_TYPE_FNPTR:
302                 return OP_MOVE;
303         case MONO_TYPE_CLASS:
304         case MONO_TYPE_STRING:
305         case MONO_TYPE_OBJECT:
306         case MONO_TYPE_SZARRAY:
307         case MONO_TYPE_ARRAY:    
308                 return OP_MOVE;
309         case MONO_TYPE_I8:
310         case MONO_TYPE_U8:
311 #if SIZEOF_REGISTER == 8
312                 return OP_MOVE;
313 #else
314                 return OP_LMOVE;
315 #endif
316         case MONO_TYPE_R4:
317                 return cfg->r4fp ? OP_RMOVE : OP_FMOVE;
318         case MONO_TYPE_R8:
319                 return OP_FMOVE;
320         case MONO_TYPE_VALUETYPE:
321                 if (type->data.klass->enumtype) {
322                         type = mono_class_enum_basetype (type->data.klass);
323                         goto handle_enum;
324                 }
325                 if (MONO_CLASS_IS_SIMD (cfg, mono_class_from_mono_type (type)))
326                         return OP_XMOVE;
327                 return OP_VMOVE;
328         case MONO_TYPE_TYPEDBYREF:
329                 return OP_VMOVE;
330         case MONO_TYPE_GENERICINST:
331                 type = &type->data.generic_class->container_class->byval_arg;
332                 goto handle_enum;
333         case MONO_TYPE_VAR:
334         case MONO_TYPE_MVAR:
335                 g_assert (cfg->gshared);
336                 if (mini_type_var_is_vt (type))
337                         return OP_VMOVE;
338                 else
339                         return mono_type_to_regmove (cfg, mini_get_underlying_type (type));
340         default:
341                 g_error ("unknown type 0x%02x in type_to_regstore", type->type);
342         }
343         return -1;
344 }
345
346 void
347 mono_print_bb (MonoBasicBlock *bb, const char *msg)
348 {
349         int i;
350         MonoInst *tree;
351
352         printf ("\n%s %d: [IN: ", msg, bb->block_num);
353         for (i = 0; i < bb->in_count; ++i)
354                 printf (" BB%d(%d)", bb->in_bb [i]->block_num, bb->in_bb [i]->dfn);
355         printf (", OUT: ");
356         for (i = 0; i < bb->out_count; ++i)
357                 printf (" BB%d(%d)", bb->out_bb [i]->block_num, bb->out_bb [i]->dfn);
358         printf (" ]\n");
359         for (tree = bb->code; tree; tree = tree->next)
360                 mono_print_ins_index (-1, tree);
361 }
362
363 void
364 mono_create_helper_signatures (void)
365 {
366         helper_sig_domain_get = mono_create_icall_signature ("ptr");
367         helper_sig_rgctx_lazy_fetch_trampoline = mono_create_icall_signature ("ptr ptr");
368         helper_sig_llvmonly_imt_thunk = mono_create_icall_signature ("ptr ptr ptr");
369 }
370
371 static MONO_NEVER_INLINE void
372 break_on_unverified (void)
373 {
374         if (mini_get_debug_options ()->break_on_unverified)
375                 G_BREAKPOINT ();
376 }
377
378 static MONO_NEVER_INLINE void
379 method_access_failure (MonoCompile *cfg, MonoMethod *method, MonoMethod *cil_method)
380 {
381         char *method_fname = mono_method_full_name (method, TRUE);
382         char *cil_method_fname = mono_method_full_name (cil_method, TRUE);
383         mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
384         mono_error_set_generic_error (&cfg->error, "System", "MethodAccessException", "Method `%s' is inaccessible from method `%s'\n", cil_method_fname, method_fname);
385         g_free (method_fname);
386         g_free (cil_method_fname);
387 }
388
389 static MONO_NEVER_INLINE void
390 field_access_failure (MonoCompile *cfg, MonoMethod *method, MonoClassField *field)
391 {
392         char *method_fname = mono_method_full_name (method, TRUE);
393         char *field_fname = mono_field_full_name (field);
394         mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
395         mono_error_set_generic_error (&cfg->error, "System", "FieldAccessException", "Field `%s' is inaccessible from method `%s'\n", field_fname, method_fname);
396         g_free (method_fname);
397         g_free (field_fname);
398 }
399
400 static MONO_NEVER_INLINE void
401 inline_failure (MonoCompile *cfg, const char *msg)
402 {
403         if (cfg->verbose_level >= 2)
404                 printf ("inline failed: %s\n", msg);
405         mono_cfg_set_exception (cfg, MONO_EXCEPTION_INLINE_FAILED);
406 }
407
408 static MONO_NEVER_INLINE void
409 gshared_failure (MonoCompile *cfg, int opcode, const char *file, int line)
410 {
411         if (cfg->verbose_level > 2)                                                                                     \
412                 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);
413         mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED);
414 }
415
416 static MONO_NEVER_INLINE void
417 gsharedvt_failure (MonoCompile *cfg, int opcode, const char *file, int line)
418 {
419         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);
420         if (cfg->verbose_level >= 2)
421                 printf ("%s\n", cfg->exception_message);
422         mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED);
423 }
424
425 /*
426  * When using gsharedvt, some instatiations might be verifiable, and some might be not. i.e. 
427  * foo<T> (int i) { ldarg.0; box T; }
428  */
429 #define UNVERIFIED do { \
430         if (cfg->gsharedvt) { \
431                 if (cfg->verbose_level > 2)                                                                     \
432                         printf ("gsharedvt method failed to verify, falling back to instantiation.\n"); \
433                 mono_cfg_set_exception (cfg, MONO_EXCEPTION_GENERIC_SHARING_FAILED); \
434                 goto exception_exit;                                                                                    \
435         }                                                                                                                                       \
436         break_on_unverified ();                                                                                         \
437         goto unverified;                                                                                                        \
438 } while (0)
439
440 #define GET_BBLOCK(cfg,tblock,ip) do {  \
441                 (tblock) = cfg->cil_offset_to_bb [(ip) - cfg->cil_start]; \
442                 if (!(tblock)) {        \
443                         if ((ip) >= end || (ip) < header->code) UNVERIFIED; \
444             NEW_BBLOCK (cfg, (tblock)); \
445                         (tblock)->cil_code = (ip);      \
446                         ADD_BBLOCK (cfg, (tblock));     \
447                 } \
448         } while (0)
449
450 #if defined(TARGET_X86) || defined(TARGET_AMD64)
451 #define EMIT_NEW_X86_LEA(cfg,dest,sr1,sr2,shift,imm) do { \
452                 MONO_INST_NEW (cfg, dest, OP_X86_LEA); \
453                 (dest)->dreg = alloc_ireg_mp ((cfg)); \
454                 (dest)->sreg1 = (sr1); \
455                 (dest)->sreg2 = (sr2); \
456                 (dest)->inst_imm = (imm); \
457                 (dest)->backend.shift_amount = (shift); \
458                 MONO_ADD_INS ((cfg)->cbb, (dest)); \
459         } while (0)
460 #endif
461
462 /* Emit conversions so both operands of a binary opcode are of the same type */
463 static void
464 add_widen_op (MonoCompile *cfg, MonoInst *ins, MonoInst **arg1_ref, MonoInst **arg2_ref)
465 {
466         MonoInst *arg1 = *arg1_ref;
467         MonoInst *arg2 = *arg2_ref;
468
469         if (cfg->r4fp &&
470                 ((arg1->type == STACK_R4 && arg2->type == STACK_R8) ||
471                  (arg1->type == STACK_R8 && arg2->type == STACK_R4))) {
472                 MonoInst *conv;
473
474                 /* Mixing r4/r8 is allowed by the spec */
475                 if (arg1->type == STACK_R4) {
476                         int dreg = alloc_freg (cfg);
477
478                         EMIT_NEW_UNALU (cfg, conv, OP_RCONV_TO_R8, dreg, arg1->dreg);
479                         conv->type = STACK_R8;
480                         ins->sreg1 = dreg;
481                         *arg1_ref = conv;
482                 }
483                 if (arg2->type == STACK_R4) {
484                         int dreg = alloc_freg (cfg);
485
486                         EMIT_NEW_UNALU (cfg, conv, OP_RCONV_TO_R8, dreg, arg2->dreg);
487                         conv->type = STACK_R8;
488                         ins->sreg2 = dreg;
489                         *arg2_ref = conv;
490                 }
491         }
492
493 #if SIZEOF_REGISTER == 8
494         /* FIXME: Need to add many more cases */
495         if ((arg1)->type == STACK_PTR && (arg2)->type == STACK_I4) {
496                 MonoInst *widen;
497
498                 int dr = alloc_preg (cfg);
499                 EMIT_NEW_UNALU (cfg, widen, OP_SEXT_I4, dr, (arg2)->dreg);
500                 (ins)->sreg2 = widen->dreg;
501         }
502 #endif
503 }
504
505 #define ADD_BINOP(op) do {      \
506                 MONO_INST_NEW (cfg, ins, (op)); \
507                 sp -= 2;        \
508                 ins->sreg1 = sp [0]->dreg;      \
509                 ins->sreg2 = sp [1]->dreg;      \
510                 type_from_op (cfg, ins, sp [0], sp [1]);        \
511                 CHECK_TYPE (ins);       \
512                 /* Have to insert a widening op */               \
513         add_widen_op (cfg, ins, &sp [0], &sp [1]);               \
514         ins->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type); \
515         MONO_ADD_INS ((cfg)->cbb, (ins)); \
516         *sp++ = mono_decompose_opcode ((cfg), (ins));   \
517         } while (0)
518
519 #define ADD_UNOP(op) do {       \
520                 MONO_INST_NEW (cfg, ins, (op)); \
521                 sp--;   \
522                 ins->sreg1 = sp [0]->dreg;      \
523                 type_from_op (cfg, ins, sp [0], NULL);  \
524                 CHECK_TYPE (ins);       \
525         (ins)->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type); \
526         MONO_ADD_INS ((cfg)->cbb, (ins)); \
527                 *sp++ = mono_decompose_opcode (cfg, ins);       \
528         } while (0)
529
530 #define ADD_BINCOND(next_block) do {    \
531                 MonoInst *cmp;  \
532                 sp -= 2; \
533                 MONO_INST_NEW(cfg, cmp, OP_COMPARE);    \
534                 cmp->sreg1 = sp [0]->dreg;      \
535                 cmp->sreg2 = sp [1]->dreg;      \
536                 type_from_op (cfg, cmp, sp [0], sp [1]);        \
537                 CHECK_TYPE (cmp);       \
538                 add_widen_op (cfg, cmp, &sp [0], &sp [1]);                                              \
539                 type_from_op (cfg, ins, sp [0], sp [1]);                                                        \
540                 ins->inst_many_bb = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);   \
541                 GET_BBLOCK (cfg, tblock, target);               \
542                 link_bblock (cfg, cfg->cbb, tblock);    \
543                 ins->inst_true_bb = tblock;     \
544                 if ((next_block)) {     \
545                         link_bblock (cfg, cfg->cbb, (next_block));      \
546                         ins->inst_false_bb = (next_block);      \
547                         start_new_bblock = 1;   \
548                 } else {        \
549                         GET_BBLOCK (cfg, tblock, ip);           \
550                         link_bblock (cfg, cfg->cbb, tblock);    \
551                         ins->inst_false_bb = tblock;    \
552                         start_new_bblock = 2;   \
553                 }       \
554                 if (sp != stack_start) {                                                                        \
555                     handle_stack_args (cfg, stack_start, sp - stack_start); \
556                         CHECK_UNVERIFIABLE (cfg); \
557                 } \
558         MONO_ADD_INS (cfg->cbb, cmp); \
559                 MONO_ADD_INS (cfg->cbb, ins);   \
560         } while (0)
561
562 /* *
563  * link_bblock: Links two basic blocks
564  *
565  * links two basic blocks in the control flow graph, the 'from'
566  * argument is the starting block and the 'to' argument is the block
567  * the control flow ends to after 'from'.
568  */
569 static void
570 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
571 {
572         MonoBasicBlock **newa;
573         int i, found;
574
575 #if 0
576         if (from->cil_code) {
577                 if (to->cil_code)
578                         printf ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
579                 else
580                         printf ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
581         } else {
582                 if (to->cil_code)
583                         printf ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
584                 else
585                         printf ("edge from entry to exit\n");
586         }
587 #endif
588
589         found = FALSE;
590         for (i = 0; i < from->out_count; ++i) {
591                 if (to == from->out_bb [i]) {
592                         found = TRUE;
593                         break;
594                 }
595         }
596         if (!found) {
597                 newa = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
598                 for (i = 0; i < from->out_count; ++i) {
599                         newa [i] = from->out_bb [i];
600                 }
601                 newa [i] = to;
602                 from->out_count++;
603                 from->out_bb = newa;
604         }
605
606         found = FALSE;
607         for (i = 0; i < to->in_count; ++i) {
608                 if (from == to->in_bb [i]) {
609                         found = TRUE;
610                         break;
611                 }
612         }
613         if (!found) {
614                 newa = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
615                 for (i = 0; i < to->in_count; ++i) {
616                         newa [i] = to->in_bb [i];
617                 }
618                 newa [i] = from;
619                 to->in_count++;
620                 to->in_bb = newa;
621         }
622 }
623
624 void
625 mono_link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
626 {
627         link_bblock (cfg, from, to);
628 }
629
630 /**
631  * mono_find_block_region:
632  *
633  *   We mark each basic block with a region ID. We use that to avoid BB
634  *   optimizations when blocks are in different regions.
635  *
636  * Returns:
637  *   A region token that encodes where this region is, and information
638  *   about the clause owner for this block.
639  *
640  *   The region encodes the try/catch/filter clause that owns this block
641  *   as well as the type.  -1 is a special value that represents a block
642  *   that is in none of try/catch/filter.
643  */
644 static int
645 mono_find_block_region (MonoCompile *cfg, int offset)
646 {
647         MonoMethodHeader *header = cfg->header;
648         MonoExceptionClause *clause;
649         int i;
650
651         for (i = 0; i < header->num_clauses; ++i) {
652                 clause = &header->clauses [i];
653                 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->data.filter_offset) &&
654                     (offset < (clause->handler_offset)))
655                         return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
656                            
657                 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
658                         if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
659                                 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
660                         else if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
661                                 return ((i + 1) << 8) | MONO_REGION_FAULT | clause->flags;
662                         else
663                                 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
664                 }
665         }
666         for (i = 0; i < header->num_clauses; ++i) {
667                 clause = &header->clauses [i];
668
669                 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
670                         return ((i + 1) << 8) | clause->flags;
671         }
672
673         return -1;
674 }
675
676 static GList*
677 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
678 {
679         MonoMethodHeader *header = cfg->header;
680         MonoExceptionClause *clause;
681         int i;
682         GList *res = NULL;
683
684         for (i = 0; i < header->num_clauses; ++i) {
685                 clause = &header->clauses [i];
686                 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) && 
687                     (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
688                         if (clause->flags == type)
689                                 res = g_list_append (res, clause);
690                 }
691         }
692         return res;
693 }
694
695 static void
696 mono_create_spvar_for_region (MonoCompile *cfg, int region)
697 {
698         MonoInst *var;
699
700         var = (MonoInst *)g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
701         if (var)
702                 return;
703
704         var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
705         /* prevent it from being register allocated */
706         var->flags |= MONO_INST_VOLATILE;
707
708         g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
709 }
710
711 MonoInst *
712 mono_find_exvar_for_offset (MonoCompile *cfg, int offset)
713 {
714         return (MonoInst *)g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
715 }
716
717 static MonoInst*
718 mono_create_exvar_for_offset (MonoCompile *cfg, int offset)
719 {
720         MonoInst *var;
721
722         var = (MonoInst *)g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
723         if (var)
724                 return var;
725
726         var = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
727         /* prevent it from being register allocated */
728         var->flags |= MONO_INST_VOLATILE;
729
730         g_hash_table_insert (cfg->exvars, GINT_TO_POINTER (offset), var);
731
732         return var;
733 }
734
735 /*
736  * Returns the type used in the eval stack when @type is loaded.
737  * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
738  */
739 void
740 type_to_eval_stack_type (MonoCompile *cfg, MonoType *type, MonoInst *inst)
741 {
742         MonoClass *klass;
743
744         type = mini_get_underlying_type (type);
745         inst->klass = klass = mono_class_from_mono_type (type);
746         if (type->byref) {
747                 inst->type = STACK_MP;
748                 return;
749         }
750
751 handle_enum:
752         switch (type->type) {
753         case MONO_TYPE_VOID:
754                 inst->type = STACK_INV;
755                 return;
756         case MONO_TYPE_I1:
757         case MONO_TYPE_U1:
758         case MONO_TYPE_I2:
759         case MONO_TYPE_U2:
760         case MONO_TYPE_I4:
761         case MONO_TYPE_U4:
762                 inst->type = STACK_I4;
763                 return;
764         case MONO_TYPE_I:
765         case MONO_TYPE_U:
766         case MONO_TYPE_PTR:
767         case MONO_TYPE_FNPTR:
768                 inst->type = STACK_PTR;
769                 return;
770         case MONO_TYPE_CLASS:
771         case MONO_TYPE_STRING:
772         case MONO_TYPE_OBJECT:
773         case MONO_TYPE_SZARRAY:
774         case MONO_TYPE_ARRAY:    
775                 inst->type = STACK_OBJ;
776                 return;
777         case MONO_TYPE_I8:
778         case MONO_TYPE_U8:
779                 inst->type = STACK_I8;
780                 return;
781         case MONO_TYPE_R4:
782                 inst->type = cfg->r4_stack_type;
783                 break;
784         case MONO_TYPE_R8:
785                 inst->type = STACK_R8;
786                 return;
787         case MONO_TYPE_VALUETYPE:
788                 if (type->data.klass->enumtype) {
789                         type = mono_class_enum_basetype (type->data.klass);
790                         goto handle_enum;
791                 } else {
792                         inst->klass = klass;
793                         inst->type = STACK_VTYPE;
794                         return;
795                 }
796         case MONO_TYPE_TYPEDBYREF:
797                 inst->klass = mono_defaults.typed_reference_class;
798                 inst->type = STACK_VTYPE;
799                 return;
800         case MONO_TYPE_GENERICINST:
801                 type = &type->data.generic_class->container_class->byval_arg;
802                 goto handle_enum;
803         case MONO_TYPE_VAR:
804         case MONO_TYPE_MVAR:
805                 g_assert (cfg->gshared);
806                 if (mini_is_gsharedvt_type (type)) {
807                         g_assert (cfg->gsharedvt);
808                         inst->type = STACK_VTYPE;
809                 } else {
810                         type_to_eval_stack_type (cfg, mini_get_underlying_type (type), inst);
811                 }
812                 return;
813         default:
814                 g_error ("unknown type 0x%02x in eval stack type", type->type);
815         }
816 }
817
818 /*
819  * The following tables are used to quickly validate the IL code in type_from_op ().
820  */
821 static const char
822 bin_num_table [STACK_MAX] [STACK_MAX] = {
823         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
824         {STACK_INV, STACK_I4,  STACK_INV, STACK_PTR, STACK_INV, STACK_MP,  STACK_INV, STACK_INV},
825         {STACK_INV, STACK_INV, STACK_I8,  STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
826         {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP,  STACK_INV, STACK_INV},
827         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8,  STACK_INV, STACK_INV, STACK_INV, STACK_R8},
828         {STACK_INV, STACK_MP,  STACK_INV, STACK_MP,  STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
829         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
830         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
831         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV, STACK_R4}
832 };
833
834 static const char 
835 neg_table [] = {
836         STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV, STACK_R4
837 };
838
839 /* reduce the size of this table */
840 static const char
841 bin_int_table [STACK_MAX] [STACK_MAX] = {
842         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
843         {STACK_INV, STACK_I4,  STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
844         {STACK_INV, STACK_INV, STACK_I8,  STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
845         {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
846         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
847         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, 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 };
851
852 static const char
853 bin_comp_table [STACK_MAX] [STACK_MAX] = {
854 /*      Inv i  L  p  F  &  O  vt r4 */
855         {0},
856         {0, 1, 0, 1, 0, 0, 0, 0}, /* i, int32 */
857         {0, 0, 1, 0, 0, 0, 0, 0}, /* L, int64 */
858         {0, 1, 0, 1, 0, 2, 4, 0}, /* p, ptr */
859         {0, 0, 0, 0, 1, 0, 0, 0, 1}, /* F, R8 */
860         {0, 0, 0, 2, 0, 1, 0, 0}, /* &, managed pointer */
861         {0, 0, 0, 4, 0, 0, 3, 0}, /* O, reference */
862         {0, 0, 0, 0, 0, 0, 0, 0}, /* vt value type */
863         {0, 0, 0, 0, 1, 0, 0, 0, 1}, /* r, r4 */
864 };
865
866 /* reduce the size of this table */
867 static const char
868 shift_table [STACK_MAX] [STACK_MAX] = {
869         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
870         {STACK_INV, STACK_I4,  STACK_INV, STACK_I4,  STACK_INV, STACK_INV, STACK_INV, STACK_INV},
871         {STACK_INV, STACK_I8,  STACK_INV, STACK_I8,  STACK_INV, STACK_INV, STACK_INV, STACK_INV},
872         {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
873         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
874         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, 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 };
878
879 /*
880  * Tables to map from the non-specific opcode to the matching
881  * type-specific opcode.
882  */
883 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
884 static const guint16
885 binops_op_map [STACK_MAX] = {
886         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
887 };
888
889 /* handles from CEE_NEG to CEE_CONV_U8 */
890 static const guint16
891 unops_op_map [STACK_MAX] = {
892         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
893 };
894
895 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
896 static const guint16
897 ovfops_op_map [STACK_MAX] = {
898         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
899 };
900
901 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
902 static const guint16
903 ovf2ops_op_map [STACK_MAX] = {
904         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
905 };
906
907 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
908 static const guint16
909 ovf3ops_op_map [STACK_MAX] = {
910         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
911 };
912
913 /* handles from CEE_BEQ to CEE_BLT_UN */
914 static const guint16
915 beqops_op_map [STACK_MAX] = {
916         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
917 };
918
919 /* handles from CEE_CEQ to CEE_CLT_UN */
920 static const guint16
921 ceqops_op_map [STACK_MAX] = {
922         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
923 };
924
925 /*
926  * Sets ins->type (the type on the eval stack) according to the
927  * type of the opcode and the arguments to it.
928  * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
929  *
930  * FIXME: this function sets ins->type unconditionally in some cases, but
931  * it should set it to invalid for some types (a conv.x on an object)
932  */
933 static void
934 type_from_op (MonoCompile *cfg, MonoInst *ins, MonoInst *src1, MonoInst *src2)
935 {
936         switch (ins->opcode) {
937         /* binops */
938         case CEE_ADD:
939         case CEE_SUB:
940         case CEE_MUL:
941         case CEE_DIV:
942         case CEE_REM:
943                 /* FIXME: check unverifiable args for STACK_MP */
944                 ins->type = bin_num_table [src1->type] [src2->type];
945                 ins->opcode += binops_op_map [ins->type];
946                 break;
947         case CEE_DIV_UN:
948         case CEE_REM_UN:
949         case CEE_AND:
950         case CEE_OR:
951         case CEE_XOR:
952                 ins->type = bin_int_table [src1->type] [src2->type];
953                 ins->opcode += binops_op_map [ins->type];
954                 break;
955         case CEE_SHL:
956         case CEE_SHR:
957         case CEE_SHR_UN:
958                 ins->type = shift_table [src1->type] [src2->type];
959                 ins->opcode += binops_op_map [ins->type];
960                 break;
961         case OP_COMPARE:
962         case OP_LCOMPARE:
963         case OP_ICOMPARE:
964                 ins->type = bin_comp_table [src1->type] [src2->type] ? STACK_I4: STACK_INV;
965                 if ((src1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((src1->type == STACK_PTR) || (src1->type == STACK_OBJ) || (src1->type == STACK_MP))))
966                         ins->opcode = OP_LCOMPARE;
967                 else if (src1->type == STACK_R4)
968                         ins->opcode = OP_RCOMPARE;
969                 else if (src1->type == STACK_R8)
970                         ins->opcode = OP_FCOMPARE;
971                 else
972                         ins->opcode = OP_ICOMPARE;
973                 break;
974         case OP_ICOMPARE_IMM:
975                 ins->type = bin_comp_table [src1->type] [src1->type] ? STACK_I4 : STACK_INV;
976                 if ((src1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((src1->type == STACK_PTR) || (src1->type == STACK_OBJ) || (src1->type == STACK_MP))))
977                         ins->opcode = OP_LCOMPARE_IMM;          
978                 break;
979         case CEE_BEQ:
980         case CEE_BGE:
981         case CEE_BGT:
982         case CEE_BLE:
983         case CEE_BLT:
984         case CEE_BNE_UN:
985         case CEE_BGE_UN:
986         case CEE_BGT_UN:
987         case CEE_BLE_UN:
988         case CEE_BLT_UN:
989                 ins->opcode += beqops_op_map [src1->type];
990                 break;
991         case OP_CEQ:
992                 ins->type = bin_comp_table [src1->type] [src2->type] ? STACK_I4: STACK_INV;
993                 ins->opcode += ceqops_op_map [src1->type];
994                 break;
995         case OP_CGT:
996         case OP_CGT_UN:
997         case OP_CLT:
998         case OP_CLT_UN:
999                 ins->type = (bin_comp_table [src1->type] [src2->type] & 1) ? STACK_I4: STACK_INV;
1000                 ins->opcode += ceqops_op_map [src1->type];
1001                 break;
1002         /* unops */
1003         case CEE_NEG:
1004                 ins->type = neg_table [src1->type];
1005                 ins->opcode += unops_op_map [ins->type];
1006                 break;
1007         case CEE_NOT:
1008                 if (src1->type >= STACK_I4 && src1->type <= STACK_PTR)
1009                         ins->type = src1->type;
1010                 else
1011                         ins->type = STACK_INV;
1012                 ins->opcode += unops_op_map [ins->type];
1013                 break;
1014         case CEE_CONV_I1:
1015         case CEE_CONV_I2:
1016         case CEE_CONV_I4:
1017         case CEE_CONV_U4:
1018                 ins->type = STACK_I4;
1019                 ins->opcode += unops_op_map [src1->type];
1020                 break;
1021         case CEE_CONV_R_UN:
1022                 ins->type = STACK_R8;
1023                 switch (src1->type) {
1024                 case STACK_I4:
1025                 case STACK_PTR:
1026                         ins->opcode = OP_ICONV_TO_R_UN;
1027                         break;
1028                 case STACK_I8:
1029                         ins->opcode = OP_LCONV_TO_R_UN; 
1030                         break;
1031                 }
1032                 break;
1033         case CEE_CONV_OVF_I1:
1034         case CEE_CONV_OVF_U1:
1035         case CEE_CONV_OVF_I2:
1036         case CEE_CONV_OVF_U2:
1037         case CEE_CONV_OVF_I4:
1038         case CEE_CONV_OVF_U4:
1039                 ins->type = STACK_I4;
1040                 ins->opcode += ovf3ops_op_map [src1->type];
1041                 break;
1042         case CEE_CONV_OVF_I_UN:
1043         case CEE_CONV_OVF_U_UN:
1044                 ins->type = STACK_PTR;
1045                 ins->opcode += ovf2ops_op_map [src1->type];
1046                 break;
1047         case CEE_CONV_OVF_I1_UN:
1048         case CEE_CONV_OVF_I2_UN:
1049         case CEE_CONV_OVF_I4_UN:
1050         case CEE_CONV_OVF_U1_UN:
1051         case CEE_CONV_OVF_U2_UN:
1052         case CEE_CONV_OVF_U4_UN:
1053                 ins->type = STACK_I4;
1054                 ins->opcode += ovf2ops_op_map [src1->type];
1055                 break;
1056         case CEE_CONV_U:
1057                 ins->type = STACK_PTR;
1058                 switch (src1->type) {
1059                 case STACK_I4:
1060                         ins->opcode = OP_ICONV_TO_U;
1061                         break;
1062                 case STACK_PTR:
1063                 case STACK_MP:
1064 #if SIZEOF_VOID_P == 8
1065                         ins->opcode = OP_LCONV_TO_U;
1066 #else
1067                         ins->opcode = OP_MOVE;
1068 #endif
1069                         break;
1070                 case STACK_I8:
1071                         ins->opcode = OP_LCONV_TO_U;
1072                         break;
1073                 case STACK_R8:
1074                         ins->opcode = OP_FCONV_TO_U;
1075                         break;
1076                 }
1077                 break;
1078         case CEE_CONV_I8:
1079         case CEE_CONV_U8:
1080                 ins->type = STACK_I8;
1081                 ins->opcode += unops_op_map [src1->type];
1082                 break;
1083         case CEE_CONV_OVF_I8:
1084         case CEE_CONV_OVF_U8:
1085                 ins->type = STACK_I8;
1086                 ins->opcode += ovf3ops_op_map [src1->type];
1087                 break;
1088         case CEE_CONV_OVF_U8_UN:
1089         case CEE_CONV_OVF_I8_UN:
1090                 ins->type = STACK_I8;
1091                 ins->opcode += ovf2ops_op_map [src1->type];
1092                 break;
1093         case CEE_CONV_R4:
1094                 ins->type = cfg->r4_stack_type;
1095                 ins->opcode += unops_op_map [src1->type];
1096                 break;
1097         case CEE_CONV_R8:
1098                 ins->type = STACK_R8;
1099                 ins->opcode += unops_op_map [src1->type];
1100                 break;
1101         case OP_CKFINITE:
1102                 ins->type = STACK_R8;           
1103                 break;
1104         case CEE_CONV_U2:
1105         case CEE_CONV_U1:
1106                 ins->type = STACK_I4;
1107                 ins->opcode += ovfops_op_map [src1->type];
1108                 break;
1109         case CEE_CONV_I:
1110         case CEE_CONV_OVF_I:
1111         case CEE_CONV_OVF_U:
1112                 ins->type = STACK_PTR;
1113                 ins->opcode += ovfops_op_map [src1->type];
1114                 break;
1115         case CEE_ADD_OVF:
1116         case CEE_ADD_OVF_UN:
1117         case CEE_MUL_OVF:
1118         case CEE_MUL_OVF_UN:
1119         case CEE_SUB_OVF:
1120         case CEE_SUB_OVF_UN:
1121                 ins->type = bin_num_table [src1->type] [src2->type];
1122                 ins->opcode += ovfops_op_map [src1->type];
1123                 if (ins->type == STACK_R8)
1124                         ins->type = STACK_INV;
1125                 break;
1126         case OP_LOAD_MEMBASE:
1127                 ins->type = STACK_PTR;
1128                 break;
1129         case OP_LOADI1_MEMBASE:
1130         case OP_LOADU1_MEMBASE:
1131         case OP_LOADI2_MEMBASE:
1132         case OP_LOADU2_MEMBASE:
1133         case OP_LOADI4_MEMBASE:
1134         case OP_LOADU4_MEMBASE:
1135                 ins->type = STACK_PTR;
1136                 break;
1137         case OP_LOADI8_MEMBASE:
1138                 ins->type = STACK_I8;
1139                 break;
1140         case OP_LOADR4_MEMBASE:
1141                 ins->type = cfg->r4_stack_type;
1142                 break;
1143         case OP_LOADR8_MEMBASE:
1144                 ins->type = STACK_R8;
1145                 break;
1146         default:
1147                 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1148                 break;
1149         }
1150
1151         if (ins->type == STACK_MP)
1152                 ins->klass = mono_defaults.object_class;
1153 }
1154
1155 static const char 
1156 ldind_type [] = {
1157         STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_R8, STACK_OBJ
1158 };
1159
1160 #if 0
1161
1162 static const char
1163 param_table [STACK_MAX] [STACK_MAX] = {
1164         {0},
1165 };
1166
1167 static int
1168 check_values_to_signature (MonoInst *args, MonoType *this_ins, MonoMethodSignature *sig)
1169 {
1170         int i;
1171
1172         if (sig->hasthis) {
1173                 switch (args->type) {
1174                 case STACK_I4:
1175                 case STACK_I8:
1176                 case STACK_R8:
1177                 case STACK_VTYPE:
1178                 case STACK_INV:
1179                         return 0;
1180                 }
1181                 args++;
1182         }
1183         for (i = 0; i < sig->param_count; ++i) {
1184                 switch (args [i].type) {
1185                 case STACK_INV:
1186                         return 0;
1187                 case STACK_MP:
1188                         if (!sig->params [i]->byref)
1189                                 return 0;
1190                         continue;
1191                 case STACK_OBJ:
1192                         if (sig->params [i]->byref)
1193                                 return 0;
1194                         switch (sig->params [i]->type) {
1195                         case MONO_TYPE_CLASS:
1196                         case MONO_TYPE_STRING:
1197                         case MONO_TYPE_OBJECT:
1198                         case MONO_TYPE_SZARRAY:
1199                         case MONO_TYPE_ARRAY:
1200                                 break;
1201                         default:
1202                                 return 0;
1203                         }
1204                         continue;
1205                 case STACK_R8:
1206                         if (sig->params [i]->byref)
1207                                 return 0;
1208                         if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1209                                 return 0;
1210                         continue;
1211                 case STACK_PTR:
1212                 case STACK_I4:
1213                 case STACK_I8:
1214                 case STACK_VTYPE:
1215                         break;
1216                 }
1217                 /*if (!param_table [args [i].type] [sig->params [i]->type])
1218                         return 0;*/
1219         }
1220         return 1;
1221 }
1222 #endif
1223
1224 /*
1225  * When we need a pointer to the current domain many times in a method, we
1226  * call mono_domain_get() once and we store the result in a local variable.
1227  * This function returns the variable that represents the MonoDomain*.
1228  */
1229 inline static MonoInst *
1230 mono_get_domainvar (MonoCompile *cfg)
1231 {
1232         if (!cfg->domainvar)
1233                 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1234         return cfg->domainvar;
1235 }
1236
1237 /*
1238  * The got_var contains the address of the Global Offset Table when AOT 
1239  * compiling.
1240  */
1241 MonoInst *
1242 mono_get_got_var (MonoCompile *cfg)
1243 {
1244         if (!cfg->compile_aot || !cfg->backend->need_got_var)
1245                 return NULL;
1246         if (!cfg->got_var) {
1247                 cfg->got_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1248         }
1249         return cfg->got_var;
1250 }
1251
1252 static MonoInst *
1253 mono_get_vtable_var (MonoCompile *cfg)
1254 {
1255         g_assert (cfg->gshared);
1256
1257         if (!cfg->rgctx_var) {
1258                 cfg->rgctx_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1259                 /* force the var to be stack allocated */
1260                 cfg->rgctx_var->flags |= MONO_INST_VOLATILE;
1261         }
1262
1263         return cfg->rgctx_var;
1264 }
1265
1266 static MonoType*
1267 type_from_stack_type (MonoInst *ins) {
1268         switch (ins->type) {
1269         case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1270         case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1271         case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1272         case STACK_R4: return &mono_defaults.single_class->byval_arg;
1273         case STACK_R8: return &mono_defaults.double_class->byval_arg;
1274         case STACK_MP:
1275                 return &ins->klass->this_arg;
1276         case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1277         case STACK_VTYPE: return &ins->klass->byval_arg;
1278         default:
1279                 g_error ("stack type %d to monotype not handled\n", ins->type);
1280         }
1281         return NULL;
1282 }
1283
1284 static G_GNUC_UNUSED int
1285 type_to_stack_type (MonoCompile *cfg, MonoType *t)
1286 {
1287         t = mono_type_get_underlying_type (t);
1288         switch (t->type) {
1289         case MONO_TYPE_I1:
1290         case MONO_TYPE_U1:
1291         case MONO_TYPE_I2:
1292         case MONO_TYPE_U2:
1293         case MONO_TYPE_I4:
1294         case MONO_TYPE_U4:
1295                 return STACK_I4;
1296         case MONO_TYPE_I:
1297         case MONO_TYPE_U:
1298         case MONO_TYPE_PTR:
1299         case MONO_TYPE_FNPTR:
1300                 return STACK_PTR;
1301         case MONO_TYPE_CLASS:
1302         case MONO_TYPE_STRING:
1303         case MONO_TYPE_OBJECT:
1304         case MONO_TYPE_SZARRAY:
1305         case MONO_TYPE_ARRAY:    
1306                 return STACK_OBJ;
1307         case MONO_TYPE_I8:
1308         case MONO_TYPE_U8:
1309                 return STACK_I8;
1310         case MONO_TYPE_R4:
1311                 return cfg->r4_stack_type;
1312         case MONO_TYPE_R8:
1313                 return STACK_R8;
1314         case MONO_TYPE_VALUETYPE:
1315         case MONO_TYPE_TYPEDBYREF:
1316                 return STACK_VTYPE;
1317         case MONO_TYPE_GENERICINST:
1318                 if (mono_type_generic_inst_is_valuetype (t))
1319                         return STACK_VTYPE;
1320                 else
1321                         return STACK_OBJ;
1322                 break;
1323         default:
1324                 g_assert_not_reached ();
1325         }
1326
1327         return -1;
1328 }
1329
1330 static MonoClass*
1331 array_access_to_klass (int opcode)
1332 {
1333         switch (opcode) {
1334         case CEE_LDELEM_U1:
1335                 return mono_defaults.byte_class;
1336         case CEE_LDELEM_U2:
1337                 return mono_defaults.uint16_class;
1338         case CEE_LDELEM_I:
1339         case CEE_STELEM_I:
1340                 return mono_defaults.int_class;
1341         case CEE_LDELEM_I1:
1342         case CEE_STELEM_I1:
1343                 return mono_defaults.sbyte_class;
1344         case CEE_LDELEM_I2:
1345         case CEE_STELEM_I2:
1346                 return mono_defaults.int16_class;
1347         case CEE_LDELEM_I4:
1348         case CEE_STELEM_I4:
1349                 return mono_defaults.int32_class;
1350         case CEE_LDELEM_U4:
1351                 return mono_defaults.uint32_class;
1352         case CEE_LDELEM_I8:
1353         case CEE_STELEM_I8:
1354                 return mono_defaults.int64_class;
1355         case CEE_LDELEM_R4:
1356         case CEE_STELEM_R4:
1357                 return mono_defaults.single_class;
1358         case CEE_LDELEM_R8:
1359         case CEE_STELEM_R8:
1360                 return mono_defaults.double_class;
1361         case CEE_LDELEM_REF:
1362         case CEE_STELEM_REF:
1363                 return mono_defaults.object_class;
1364         default:
1365                 g_assert_not_reached ();
1366         }
1367         return NULL;
1368 }
1369
1370 /*
1371  * We try to share variables when possible
1372  */
1373 static MonoInst *
1374 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1375 {
1376         MonoInst *res;
1377         int pos, vnum;
1378
1379         /* inlining can result in deeper stacks */ 
1380         if (slot >= cfg->header->max_stack)
1381                 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1382
1383         pos = ins->type - 1 + slot * STACK_MAX;
1384
1385         switch (ins->type) {
1386         case STACK_I4:
1387         case STACK_I8:
1388         case STACK_R8:
1389         case STACK_PTR:
1390         case STACK_MP:
1391         case STACK_OBJ:
1392                 if ((vnum = cfg->intvars [pos]))
1393                         return cfg->varinfo [vnum];
1394                 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1395                 cfg->intvars [pos] = res->inst_c0;
1396                 break;
1397         default:
1398                 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1399         }
1400         return res;
1401 }
1402
1403 static void
1404 mono_save_token_info (MonoCompile *cfg, MonoImage *image, guint32 token, gpointer key)
1405 {
1406         /* 
1407          * Don't use this if a generic_context is set, since that means AOT can't
1408          * look up the method using just the image+token.
1409          * table == 0 means this is a reference made from a wrapper.
1410          */
1411         if (cfg->compile_aot && !cfg->generic_context && (mono_metadata_token_table (token) > 0)) {
1412                 MonoJumpInfoToken *jump_info_token = (MonoJumpInfoToken *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoToken));
1413                 jump_info_token->image = image;
1414                 jump_info_token->token = token;
1415                 g_hash_table_insert (cfg->token_info_hash, key, jump_info_token);
1416         }
1417 }
1418
1419 /*
1420  * This function is called to handle items that are left on the evaluation stack
1421  * at basic block boundaries. What happens is that we save the values to local variables
1422  * and we reload them later when first entering the target basic block (with the
1423  * handle_loaded_temps () function).
1424  * A single joint point will use the same variables (stored in the array bb->out_stack or
1425  * bb->in_stack, if the basic block is before or after the joint point).
1426  *
1427  * This function needs to be called _before_ emitting the last instruction of
1428  * the bb (i.e. before emitting a branch).
1429  * If the stack merge fails at a join point, cfg->unverifiable is set.
1430  */
1431 static void
1432 handle_stack_args (MonoCompile *cfg, MonoInst **sp, int count)
1433 {
1434         int i, bindex;
1435         MonoBasicBlock *bb = cfg->cbb;
1436         MonoBasicBlock *outb;
1437         MonoInst *inst, **locals;
1438         gboolean found;
1439
1440         if (!count)
1441                 return;
1442         if (cfg->verbose_level > 3)
1443                 printf ("%d item(s) on exit from B%d\n", count, bb->block_num);
1444         if (!bb->out_scount) {
1445                 bb->out_scount = count;
1446                 //printf ("bblock %d has out:", bb->block_num);
1447                 found = FALSE;
1448                 for (i = 0; i < bb->out_count; ++i) {
1449                         outb = bb->out_bb [i];
1450                         /* exception handlers are linked, but they should not be considered for stack args */
1451                         if (outb->flags & BB_EXCEPTION_HANDLER)
1452                                 continue;
1453                         //printf (" %d", outb->block_num);
1454                         if (outb->in_stack) {
1455                                 found = TRUE;
1456                                 bb->out_stack = outb->in_stack;
1457                                 break;
1458                         }
1459                 }
1460                 //printf ("\n");
1461                 if (!found) {
1462                         bb->out_stack = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1463                         for (i = 0; i < count; ++i) {
1464                                 /* 
1465                                  * try to reuse temps already allocated for this purpouse, if they occupy the same
1466                                  * stack slot and if they are of the same type.
1467                                  * This won't cause conflicts since if 'local' is used to 
1468                                  * store one of the values in the in_stack of a bblock, then
1469                                  * the same variable will be used for the same outgoing stack 
1470                                  * slot as well. 
1471                                  * This doesn't work when inlining methods, since the bblocks
1472                                  * in the inlined methods do not inherit their in_stack from
1473                                  * the bblock they are inlined to. See bug #58863 for an
1474                                  * example.
1475                                  */
1476                                 if (cfg->inlined_method)
1477                                         bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1478                                 else
1479                                         bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1480                         }
1481                 }
1482         }
1483
1484         for (i = 0; i < bb->out_count; ++i) {
1485                 outb = bb->out_bb [i];
1486                 /* exception handlers are linked, but they should not be considered for stack args */
1487                 if (outb->flags & BB_EXCEPTION_HANDLER)
1488                         continue;
1489                 if (outb->in_scount) {
1490                         if (outb->in_scount != bb->out_scount) {
1491                                 cfg->unverifiable = TRUE;
1492                                 return;
1493                         }
1494                         continue; /* check they are the same locals */
1495                 }
1496                 outb->in_scount = count;
1497                 outb->in_stack = bb->out_stack;
1498         }
1499
1500         locals = bb->out_stack;
1501         cfg->cbb = bb;
1502         for (i = 0; i < count; ++i) {
1503                 EMIT_NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1504                 inst->cil_code = sp [i]->cil_code;
1505                 sp [i] = locals [i];
1506                 if (cfg->verbose_level > 3)
1507                         printf ("storing %d to temp %d\n", i, (int)locals [i]->inst_c0);
1508         }
1509
1510         /*
1511          * It is possible that the out bblocks already have in_stack assigned, and
1512          * the in_stacks differ. In this case, we will store to all the different 
1513          * in_stacks.
1514          */
1515
1516         found = TRUE;
1517         bindex = 0;
1518         while (found) {
1519                 /* Find a bblock which has a different in_stack */
1520                 found = FALSE;
1521                 while (bindex < bb->out_count) {
1522                         outb = bb->out_bb [bindex];
1523                         /* exception handlers are linked, but they should not be considered for stack args */
1524                         if (outb->flags & BB_EXCEPTION_HANDLER) {
1525                                 bindex++;
1526                                 continue;
1527                         }
1528                         if (outb->in_stack != locals) {
1529                                 for (i = 0; i < count; ++i) {
1530                                         EMIT_NEW_TEMPSTORE (cfg, inst, outb->in_stack [i]->inst_c0, sp [i]);
1531                                         inst->cil_code = sp [i]->cil_code;
1532                                         sp [i] = locals [i];
1533                                         if (cfg->verbose_level > 3)
1534                                                 printf ("storing %d to temp %d\n", i, (int)outb->in_stack [i]->inst_c0);
1535                                 }
1536                                 locals = outb->in_stack;
1537                                 found = TRUE;
1538                                 break;
1539                         }
1540                         bindex ++;
1541                 }
1542         }
1543 }
1544
1545 static MonoInst*
1546 emit_runtime_constant (MonoCompile *cfg, MonoJumpInfoType patch_type, gpointer data)
1547 {
1548         MonoInst *ins;
1549
1550         if (cfg->compile_aot) {
1551                 EMIT_NEW_AOTCONST (cfg, ins, patch_type, data);
1552         } else {
1553                 MonoJumpInfo ji;
1554                 gpointer target;
1555                 MonoError error;
1556
1557                 ji.type = patch_type;
1558                 ji.data.target = data;
1559                 target = mono_resolve_patch_target (NULL, cfg->domain, NULL, &ji, FALSE, &error);
1560                 mono_error_assert_ok (&error);
1561
1562                 EMIT_NEW_PCONST (cfg, ins, target);
1563         }
1564         return ins;
1565 }
1566
1567 static void
1568 mini_emit_interface_bitmap_check (MonoCompile *cfg, int intf_bit_reg, int base_reg, int offset, MonoClass *klass)
1569 {
1570         int ibitmap_reg = alloc_preg (cfg);
1571 #ifdef COMPRESSED_INTERFACE_BITMAP
1572         MonoInst *args [2];
1573         MonoInst *res, *ins;
1574         NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, ibitmap_reg, base_reg, offset);
1575         MONO_ADD_INS (cfg->cbb, ins);
1576         args [0] = ins;
1577         args [1] = emit_runtime_constant (cfg, MONO_PATCH_INFO_IID, klass);
1578         res = mono_emit_jit_icall (cfg, mono_class_interface_match, args);
1579         MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, intf_bit_reg, res->dreg);
1580 #else
1581         int ibitmap_byte_reg = alloc_preg (cfg);
1582
1583         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, ibitmap_reg, base_reg, offset);
1584
1585         if (cfg->compile_aot) {
1586                 int iid_reg = alloc_preg (cfg);
1587                 int shifted_iid_reg = alloc_preg (cfg);
1588                 int ibitmap_byte_address_reg = alloc_preg (cfg);
1589                 int masked_iid_reg = alloc_preg (cfg);
1590                 int iid_one_bit_reg = alloc_preg (cfg);
1591                 int iid_bit_reg = alloc_preg (cfg);
1592                 MONO_EMIT_NEW_AOTCONST (cfg, iid_reg, klass, MONO_PATCH_INFO_IID);
1593                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHR_IMM, shifted_iid_reg, iid_reg, 3);
1594                 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, ibitmap_byte_address_reg, ibitmap_reg, shifted_iid_reg);
1595                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, ibitmap_byte_reg, ibitmap_byte_address_reg, 0);
1596                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, masked_iid_reg, iid_reg, 7);
1597                 MONO_EMIT_NEW_ICONST (cfg, iid_one_bit_reg, 1);
1598                 MONO_EMIT_NEW_BIALU (cfg, OP_ISHL, iid_bit_reg, iid_one_bit_reg, masked_iid_reg);
1599                 MONO_EMIT_NEW_BIALU (cfg, OP_IAND, intf_bit_reg, ibitmap_byte_reg, iid_bit_reg);
1600         } else {
1601                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, ibitmap_byte_reg, ibitmap_reg, klass->interface_id >> 3);
1602                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_AND_IMM, intf_bit_reg, ibitmap_byte_reg, 1 << (klass->interface_id & 7));
1603         }
1604 #endif
1605 }
1606
1607 /* 
1608  * Emit code which loads into "intf_bit_reg" a nonzero value if the MonoClass
1609  * stored in "klass_reg" implements the interface "klass".
1610  */
1611 static void
1612 mini_emit_load_intf_bit_reg_class (MonoCompile *cfg, int intf_bit_reg, int klass_reg, MonoClass *klass)
1613 {
1614         mini_emit_interface_bitmap_check (cfg, intf_bit_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, interface_bitmap), klass);
1615 }
1616
1617 /* 
1618  * Emit code which loads into "intf_bit_reg" a nonzero value if the MonoVTable
1619  * stored in "vtable_reg" implements the interface "klass".
1620  */
1621 static void
1622 mini_emit_load_intf_bit_reg_vtable (MonoCompile *cfg, int intf_bit_reg, int vtable_reg, MonoClass *klass)
1623 {
1624         mini_emit_interface_bitmap_check (cfg, intf_bit_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, interface_bitmap), klass);
1625 }
1626
1627 /* 
1628  * Emit code which checks whenever the interface id of @klass is smaller than
1629  * than the value given by max_iid_reg.
1630 */
1631 static void
1632 mini_emit_max_iid_check (MonoCompile *cfg, int max_iid_reg, MonoClass *klass,
1633                                                  MonoBasicBlock *false_target)
1634 {
1635         if (cfg->compile_aot) {
1636                 int iid_reg = alloc_preg (cfg);
1637                 MONO_EMIT_NEW_AOTCONST (cfg, iid_reg, klass, MONO_PATCH_INFO_IID);
1638                 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, max_iid_reg, iid_reg);
1639         }
1640         else
1641                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, max_iid_reg, klass->interface_id);
1642         if (false_target)
1643                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBLT_UN, false_target);
1644         else
1645                 MONO_EMIT_NEW_COND_EXC (cfg, LT_UN, "InvalidCastException");
1646 }
1647
1648 /* Same as above, but obtains max_iid from a vtable */
1649 static void
1650 mini_emit_max_iid_check_vtable (MonoCompile *cfg, int vtable_reg, MonoClass *klass,
1651                                                                  MonoBasicBlock *false_target)
1652 {
1653         int max_iid_reg = alloc_preg (cfg);
1654                 
1655         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, max_iid_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, max_interface_id));
1656         mini_emit_max_iid_check (cfg, max_iid_reg, klass, false_target);
1657 }
1658
1659 /* Same as above, but obtains max_iid from a klass */
1660 static void
1661 mini_emit_max_iid_check_class (MonoCompile *cfg, int klass_reg, MonoClass *klass,
1662                                                                  MonoBasicBlock *false_target)
1663 {
1664         int max_iid_reg = alloc_preg (cfg);
1665
1666         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, max_iid_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, max_interface_id));
1667         mini_emit_max_iid_check (cfg, max_iid_reg, klass, false_target);
1668 }
1669
1670 static void
1671 mini_emit_isninst_cast_inst (MonoCompile *cfg, int klass_reg, MonoClass *klass, MonoInst *klass_ins, MonoBasicBlock *false_target, MonoBasicBlock *true_target)
1672 {
1673         int idepth_reg = alloc_preg (cfg);
1674         int stypes_reg = alloc_preg (cfg);
1675         int stype = alloc_preg (cfg);
1676
1677         mono_class_setup_supertypes (klass);
1678
1679         if (klass->idepth > MONO_DEFAULT_SUPERTABLE_SIZE) {
1680                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, idepth_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, idepth));
1681                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, idepth_reg, klass->idepth);
1682                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBLT_UN, false_target);
1683         }
1684         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, stypes_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, supertypes));
1685         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, stype, stypes_reg, ((klass->idepth - 1) * SIZEOF_VOID_P));
1686         if (klass_ins) {
1687                 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, stype, klass_ins->dreg);
1688         } else if (cfg->compile_aot) {
1689                 int const_reg = alloc_preg (cfg);
1690                 MONO_EMIT_NEW_CLASSCONST (cfg, const_reg, klass);
1691                 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, stype, const_reg);
1692         } else {
1693                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, stype, klass);
1694         }
1695         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, true_target);
1696 }
1697
1698 static void
1699 mini_emit_isninst_cast (MonoCompile *cfg, int klass_reg, MonoClass *klass, MonoBasicBlock *false_target, MonoBasicBlock *true_target)
1700 {
1701         mini_emit_isninst_cast_inst (cfg, klass_reg, klass, NULL, false_target, true_target);
1702 }
1703
1704 static void
1705 mini_emit_iface_cast (MonoCompile *cfg, int vtable_reg, MonoClass *klass, MonoBasicBlock *false_target, MonoBasicBlock *true_target)
1706 {
1707         int intf_reg = alloc_preg (cfg);
1708
1709         mini_emit_max_iid_check_vtable (cfg, vtable_reg, klass, false_target);
1710         mini_emit_load_intf_bit_reg_vtable (cfg, intf_reg, vtable_reg, klass);
1711         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, intf_reg, 0);
1712         if (true_target)
1713                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, true_target);
1714         else
1715                 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "InvalidCastException");               
1716 }
1717
1718 /*
1719  * Variant of the above that takes a register to the class, not the vtable.
1720  */
1721 static void
1722 mini_emit_iface_class_cast (MonoCompile *cfg, int klass_reg, MonoClass *klass, MonoBasicBlock *false_target, MonoBasicBlock *true_target)
1723 {
1724         int intf_bit_reg = alloc_preg (cfg);
1725
1726         mini_emit_max_iid_check_class (cfg, klass_reg, klass, false_target);
1727         mini_emit_load_intf_bit_reg_class (cfg, intf_bit_reg, klass_reg, klass);
1728         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, intf_bit_reg, 0);
1729         if (true_target)
1730                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, true_target);
1731         else
1732                 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "InvalidCastException");
1733 }
1734
1735 static inline void
1736 mini_emit_class_check_inst (MonoCompile *cfg, int klass_reg, MonoClass *klass, MonoInst *klass_inst)
1737 {
1738         if (klass_inst) {
1739                 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, klass_inst->dreg);
1740         } else {
1741                 MonoInst *ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_CLASS, klass);
1742                 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, ins->dreg);
1743         }
1744         MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
1745 }
1746
1747 static inline void
1748 mini_emit_class_check (MonoCompile *cfg, int klass_reg, MonoClass *klass)
1749 {
1750         mini_emit_class_check_inst (cfg, klass_reg, klass, NULL);
1751 }
1752
1753 static inline void
1754 mini_emit_class_check_branch (MonoCompile *cfg, int klass_reg, MonoClass *klass, int branch_op, MonoBasicBlock *target)
1755 {
1756         if (cfg->compile_aot) {
1757                 int const_reg = alloc_preg (cfg);
1758                 MONO_EMIT_NEW_CLASSCONST (cfg, const_reg, klass);
1759                 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, const_reg);
1760         } else {
1761                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, klass_reg, klass);
1762         }
1763         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, branch_op, target);
1764 }
1765
1766 static void
1767 mini_emit_castclass (MonoCompile *cfg, int obj_reg, int klass_reg, MonoClass *klass, MonoBasicBlock *object_is_null);
1768         
1769 static void
1770 mini_emit_castclass_inst (MonoCompile *cfg, int obj_reg, int klass_reg, MonoClass *klass, MonoInst *klass_inst, MonoBasicBlock *object_is_null)
1771 {
1772         if (klass->rank) {
1773                 int rank_reg = alloc_preg (cfg);
1774                 int eclass_reg = alloc_preg (cfg);
1775
1776                 g_assert (!klass_inst);
1777                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, rank_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, rank));
1778                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rank_reg, klass->rank);
1779                 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
1780                 //              MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
1781                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, eclass_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, cast_class));
1782                 if (klass->cast_class == mono_defaults.object_class) {
1783                         int parent_reg = alloc_preg (cfg);
1784                         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, parent_reg, eclass_reg, MONO_STRUCT_OFFSET (MonoClass, parent));
1785                         mini_emit_class_check_branch (cfg, parent_reg, mono_defaults.enum_class->parent, OP_PBNE_UN, object_is_null);
1786                         mini_emit_class_check (cfg, eclass_reg, mono_defaults.enum_class);
1787                 } else if (klass->cast_class == mono_defaults.enum_class->parent) {
1788                         mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class->parent, OP_PBEQ, object_is_null);
1789                         mini_emit_class_check (cfg, eclass_reg, mono_defaults.enum_class);
1790                 } else if (klass->cast_class == mono_defaults.enum_class) {
1791                         mini_emit_class_check (cfg, eclass_reg, mono_defaults.enum_class);
1792                 } else if (klass->cast_class->flags & TYPE_ATTRIBUTE_INTERFACE) {
1793                         mini_emit_iface_class_cast (cfg, eclass_reg, klass->cast_class, NULL, NULL);
1794                 } else {
1795                         // Pass -1 as obj_reg to skip the check below for arrays of arrays
1796                         mini_emit_castclass (cfg, -1, eclass_reg, klass->cast_class, object_is_null);
1797                 }
1798
1799                 if ((klass->rank == 1) && (klass->byval_arg.type == MONO_TYPE_SZARRAY) && (obj_reg != -1)) {
1800                         /* Check that the object is a vector too */
1801                         int bounds_reg = alloc_preg (cfg);
1802                         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg, obj_reg, MONO_STRUCT_OFFSET (MonoArray, bounds));
1803                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, bounds_reg, 0);
1804                         MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
1805                 }
1806         } else {
1807                 int idepth_reg = alloc_preg (cfg);
1808                 int stypes_reg = alloc_preg (cfg);
1809                 int stype = alloc_preg (cfg);
1810
1811                 mono_class_setup_supertypes (klass);
1812
1813                 if (klass->idepth > MONO_DEFAULT_SUPERTABLE_SIZE) {
1814                         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, idepth_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, idepth));
1815                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, idepth_reg, klass->idepth);
1816                         MONO_EMIT_NEW_COND_EXC (cfg, LT_UN, "InvalidCastException");
1817                 }
1818                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, stypes_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, supertypes));
1819                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, stype, stypes_reg, ((klass->idepth - 1) * SIZEOF_VOID_P));
1820                 mini_emit_class_check_inst (cfg, stype, klass, klass_inst);
1821         }
1822 }
1823
1824 static void
1825 mini_emit_castclass (MonoCompile *cfg, int obj_reg, int klass_reg, MonoClass *klass, MonoBasicBlock *object_is_null)
1826 {
1827         mini_emit_castclass_inst (cfg, obj_reg, klass_reg, klass, NULL, object_is_null);
1828 }
1829
1830 static void 
1831 mini_emit_memset (MonoCompile *cfg, int destreg, int offset, int size, int val, int align)
1832 {
1833         int val_reg;
1834
1835         g_assert (val == 0);
1836
1837         if (align == 0)
1838                 align = 4;
1839
1840         if ((size <= SIZEOF_REGISTER) && (size <= align)) {
1841                 switch (size) {
1842                 case 1:
1843                         MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI1_MEMBASE_IMM, destreg, offset, val);
1844                         return;
1845                 case 2:
1846                         MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI2_MEMBASE_IMM, destreg, offset, val);
1847                         return;
1848                 case 4:
1849                         MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI4_MEMBASE_IMM, destreg, offset, val);
1850                         return;
1851 #if SIZEOF_REGISTER == 8
1852                 case 8:
1853                         MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI8_MEMBASE_IMM, destreg, offset, val);
1854                         return;
1855 #endif
1856                 }
1857         }
1858
1859         val_reg = alloc_preg (cfg);
1860
1861         if (SIZEOF_REGISTER == 8)
1862                 MONO_EMIT_NEW_I8CONST (cfg, val_reg, val);
1863         else
1864                 MONO_EMIT_NEW_ICONST (cfg, val_reg, val);
1865
1866         if (align < 4) {
1867                 /* This could be optimized further if neccesary */
1868                 while (size >= 1) {
1869                         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, offset, val_reg);
1870                         offset += 1;
1871                         size -= 1;
1872                 }
1873                 return;
1874         }       
1875
1876         if (!cfg->backend->no_unaligned_access && SIZEOF_REGISTER == 8) {
1877                 if (offset % 8) {
1878                         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, offset, val_reg);
1879                         offset += 4;
1880                         size -= 4;
1881                 }
1882                 while (size >= 8) {
1883                         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI8_MEMBASE_REG, destreg, offset, val_reg);
1884                         offset += 8;
1885                         size -= 8;
1886                 }
1887         }       
1888
1889         while (size >= 4) {
1890                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, offset, val_reg);
1891                 offset += 4;
1892                 size -= 4;
1893         }
1894         while (size >= 2) {
1895                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI2_MEMBASE_REG, destreg, offset, val_reg);
1896                 offset += 2;
1897                 size -= 2;
1898         }
1899         while (size >= 1) {
1900                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, offset, val_reg);
1901                 offset += 1;
1902                 size -= 1;
1903         }
1904 }
1905
1906 void 
1907 mini_emit_memcpy (MonoCompile *cfg, int destreg, int doffset, int srcreg, int soffset, int size, int align)
1908 {
1909         int cur_reg;
1910
1911         if (align == 0)
1912                 align = 4;
1913
1914         /*FIXME arbitrary hack to avoid unbound code expansion.*/
1915         g_assert (size < 10000);
1916
1917         if (align < 4) {
1918                 /* This could be optimized further if neccesary */
1919                 while (size >= 1) {
1920                         cur_reg = alloc_preg (cfg);
1921                         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, cur_reg, srcreg, soffset);
1922                         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, doffset, cur_reg);
1923                         doffset += 1;
1924                         soffset += 1;
1925                         size -= 1;
1926                 }
1927         }
1928
1929         if (!cfg->backend->no_unaligned_access && SIZEOF_REGISTER == 8) {
1930                 while (size >= 8) {
1931                         cur_reg = alloc_preg (cfg);
1932                         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI8_MEMBASE, cur_reg, srcreg, soffset);
1933                         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI8_MEMBASE_REG, destreg, doffset, cur_reg);
1934                         doffset += 8;
1935                         soffset += 8;
1936                         size -= 8;
1937                 }
1938         }       
1939
1940         while (size >= 4) {
1941                 cur_reg = alloc_preg (cfg);
1942                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, cur_reg, srcreg, soffset);
1943                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, doffset, cur_reg);
1944                 doffset += 4;
1945                 soffset += 4;
1946                 size -= 4;
1947         }
1948         while (size >= 2) {
1949                 cur_reg = alloc_preg (cfg);
1950                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI2_MEMBASE, cur_reg, srcreg, soffset);
1951                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI2_MEMBASE_REG, destreg, doffset, cur_reg);
1952                 doffset += 2;
1953                 soffset += 2;
1954                 size -= 2;
1955         }
1956         while (size >= 1) {
1957                 cur_reg = alloc_preg (cfg);
1958                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, cur_reg, srcreg, soffset);
1959                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, doffset, cur_reg);
1960                 doffset += 1;
1961                 soffset += 1;
1962                 size -= 1;
1963         }
1964 }
1965
1966 static void
1967 emit_tls_set (MonoCompile *cfg, int sreg1, MonoTlsKey tls_key)
1968 {
1969         MonoInst *ins, *c;
1970
1971         if (cfg->compile_aot) {
1972                 EMIT_NEW_TLS_OFFSETCONST (cfg, c, tls_key);
1973                 MONO_INST_NEW (cfg, ins, OP_TLS_SET_REG);
1974                 ins->sreg1 = sreg1;
1975                 ins->sreg2 = c->dreg;
1976                 MONO_ADD_INS (cfg->cbb, ins);
1977         } else {
1978                 MONO_INST_NEW (cfg, ins, OP_TLS_SET);
1979                 ins->sreg1 = sreg1;
1980                 ins->inst_offset = mini_get_tls_offset (tls_key);
1981                 MONO_ADD_INS (cfg->cbb, ins);
1982         }
1983 }
1984
1985 /*
1986  * emit_push_lmf:
1987  *
1988  *   Emit IR to push the current LMF onto the LMF stack.
1989  */
1990 static void
1991 emit_push_lmf (MonoCompile *cfg)
1992 {
1993         /*
1994          * Emit IR to push the LMF:
1995          * lmf_addr = <lmf_addr from tls>
1996          * lmf->lmf_addr = lmf_addr
1997          * lmf->prev_lmf = *lmf_addr
1998          * *lmf_addr = lmf
1999          */
2000         int lmf_reg, prev_lmf_reg;
2001         MonoInst *ins, *lmf_ins;
2002
2003         if (!cfg->lmf_ir)
2004                 return;
2005
2006         if (cfg->lmf_ir_mono_lmf && mini_tls_get_supported (cfg, TLS_KEY_LMF)) {
2007                 /* Load current lmf */
2008                 lmf_ins = mono_get_lmf_intrinsic (cfg);
2009                 g_assert (lmf_ins);
2010                 MONO_ADD_INS (cfg->cbb, lmf_ins);
2011                 EMIT_NEW_VARLOADA (cfg, ins, cfg->lmf_var, NULL);
2012                 lmf_reg = ins->dreg;
2013                 /* Save previous_lmf */
2014                 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), lmf_ins->dreg);
2015                 /* Set new LMF */
2016                 emit_tls_set (cfg, lmf_reg, TLS_KEY_LMF);
2017         } else {
2018                 /*
2019                  * Store lmf_addr in a variable, so it can be allocated to a global register.
2020                  */
2021                 if (!cfg->lmf_addr_var)
2022                         cfg->lmf_addr_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2023
2024 #ifdef HOST_WIN32
2025                 ins = mono_get_jit_tls_intrinsic (cfg);
2026                 if (ins) {
2027                         int jit_tls_dreg = ins->dreg;
2028
2029                         MONO_ADD_INS (cfg->cbb, ins);
2030                         lmf_reg = alloc_preg (cfg);
2031                         EMIT_NEW_BIALU_IMM (cfg, lmf_ins, OP_PADD_IMM, lmf_reg, jit_tls_dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, lmf));
2032                 } else {
2033                         lmf_ins = mono_emit_jit_icall (cfg, mono_get_lmf_addr, NULL);
2034                 }
2035 #else
2036                 lmf_ins = mono_get_lmf_addr_intrinsic (cfg);
2037                 if (lmf_ins) {
2038                         MONO_ADD_INS (cfg->cbb, lmf_ins);
2039                 } else {
2040 #ifdef TARGET_IOS
2041                         MonoInst *args [16], *jit_tls_ins, *ins;
2042
2043                         /* Inline mono_get_lmf_addr () */
2044                         /* jit_tls = pthread_getspecific (mono_jit_tls_id); lmf_addr = &jit_tls->lmf; */
2045
2046                         /* Load mono_jit_tls_id */
2047                         if (cfg->compile_aot)
2048                                 EMIT_NEW_AOTCONST (cfg, args [0], MONO_PATCH_INFO_JIT_TLS_ID, NULL);
2049                         else
2050                                 EMIT_NEW_ICONST (cfg, args [0], mono_jit_tls_id);
2051                         /* call pthread_getspecific () */
2052                         jit_tls_ins = mono_emit_jit_icall (cfg, pthread_getspecific, args);
2053                         /* lmf_addr = &jit_tls->lmf */
2054                         EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, cfg->lmf_addr_var->dreg, jit_tls_ins->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, lmf));
2055                         lmf_ins = ins;
2056 #else
2057                         lmf_ins = mono_emit_jit_icall (cfg, mono_get_lmf_addr, NULL);
2058 #endif
2059                 }
2060 #endif
2061                 lmf_ins->dreg = cfg->lmf_addr_var->dreg;
2062
2063                 EMIT_NEW_VARLOADA (cfg, ins, cfg->lmf_var, NULL);
2064                 lmf_reg = ins->dreg;
2065
2066                 prev_lmf_reg = alloc_preg (cfg);
2067                 /* Save previous_lmf */
2068                 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, prev_lmf_reg, cfg->lmf_addr_var->dreg, 0);
2069                 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), prev_lmf_reg);
2070                 /* Set new lmf */
2071                 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, cfg->lmf_addr_var->dreg, 0, lmf_reg);
2072         }
2073 }
2074
2075 /*
2076  * emit_pop_lmf:
2077  *
2078  *   Emit IR to pop the current LMF from the LMF stack.
2079  */
2080 static void
2081 emit_pop_lmf (MonoCompile *cfg)
2082 {
2083         int lmf_reg, lmf_addr_reg, prev_lmf_reg;
2084         MonoInst *ins;
2085
2086         if (!cfg->lmf_ir)
2087                 return;
2088
2089         EMIT_NEW_VARLOADA (cfg, ins, cfg->lmf_var, NULL);
2090         lmf_reg = ins->dreg;
2091
2092         if (cfg->lmf_ir_mono_lmf && mini_tls_get_supported (cfg, TLS_KEY_LMF)) {
2093                 /* Load previous_lmf */
2094                 prev_lmf_reg = alloc_preg (cfg);
2095                 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, prev_lmf_reg, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
2096                 /* Set new LMF */
2097                 emit_tls_set (cfg, prev_lmf_reg, TLS_KEY_LMF);
2098         } else {
2099                 /*
2100                  * Emit IR to pop the LMF:
2101                  * *(lmf->lmf_addr) = lmf->prev_lmf
2102                  */
2103                 /* This could be called before emit_push_lmf () */
2104                 if (!cfg->lmf_addr_var)
2105                         cfg->lmf_addr_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2106                 lmf_addr_reg = cfg->lmf_addr_var->dreg;
2107
2108                 prev_lmf_reg = alloc_preg (cfg);
2109                 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, prev_lmf_reg, lmf_reg, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
2110                 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, lmf_addr_reg, 0, prev_lmf_reg);
2111         }
2112 }
2113
2114 static void
2115 emit_instrumentation_call (MonoCompile *cfg, void *func)
2116 {
2117         MonoInst *iargs [1];
2118
2119         /*
2120          * Avoid instrumenting inlined methods since it can
2121          * distort profiling results.
2122          */
2123         if (cfg->method != cfg->current_method)
2124                 return;
2125
2126         if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE) {
2127                 EMIT_NEW_METHODCONST (cfg, iargs [0], cfg->method);
2128                 mono_emit_jit_icall (cfg, func, iargs);
2129         }
2130 }
2131
2132 static int
2133 ret_type_to_call_opcode (MonoCompile *cfg, MonoType *type, int calli, int virt)
2134 {
2135 handle_enum:
2136         type = mini_get_underlying_type (type);
2137         switch (type->type) {
2138         case MONO_TYPE_VOID:
2139                 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALL_MEMBASE: OP_VOIDCALL;
2140         case MONO_TYPE_I1:
2141         case MONO_TYPE_U1:
2142         case MONO_TYPE_I2:
2143         case MONO_TYPE_U2:
2144         case MONO_TYPE_I4:
2145         case MONO_TYPE_U4:
2146                 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
2147         case MONO_TYPE_I:
2148         case MONO_TYPE_U:
2149         case MONO_TYPE_PTR:
2150         case MONO_TYPE_FNPTR:
2151                 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
2152         case MONO_TYPE_CLASS:
2153         case MONO_TYPE_STRING:
2154         case MONO_TYPE_OBJECT:
2155         case MONO_TYPE_SZARRAY:
2156         case MONO_TYPE_ARRAY:    
2157                 return calli? OP_CALL_REG: virt? OP_CALL_MEMBASE: OP_CALL;
2158         case MONO_TYPE_I8:
2159         case MONO_TYPE_U8:
2160                 return calli? OP_LCALL_REG: virt? OP_LCALL_MEMBASE: OP_LCALL;
2161         case MONO_TYPE_R4:
2162                 if (cfg->r4fp)
2163                         return calli? OP_RCALL_REG: virt? OP_RCALL_MEMBASE: OP_RCALL;
2164                 else
2165                         return calli? OP_FCALL_REG: virt? OP_FCALL_MEMBASE: OP_FCALL;
2166         case MONO_TYPE_R8:
2167                 return calli? OP_FCALL_REG: virt? OP_FCALL_MEMBASE: OP_FCALL;
2168         case MONO_TYPE_VALUETYPE:
2169                 if (type->data.klass->enumtype) {
2170                         type = mono_class_enum_basetype (type->data.klass);
2171                         goto handle_enum;
2172                 } else
2173                         return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
2174         case MONO_TYPE_TYPEDBYREF:
2175                 return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
2176         case MONO_TYPE_GENERICINST:
2177                 type = &type->data.generic_class->container_class->byval_arg;
2178                 goto handle_enum;
2179         case MONO_TYPE_VAR:
2180         case MONO_TYPE_MVAR:
2181                 /* gsharedvt */
2182                 return calli? OP_VCALL_REG: virt? OP_VCALL_MEMBASE: OP_VCALL;
2183         default:
2184                 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
2185         }
2186         return -1;
2187 }
2188
2189 /*
2190  * target_type_is_incompatible:
2191  * @cfg: MonoCompile context
2192  *
2193  * Check that the item @arg on the evaluation stack can be stored
2194  * in the target type (can be a local, or field, etc).
2195  * The cfg arg can be used to check if we need verification or just
2196  * validity checks.
2197  *
2198  * Returns: non-0 value if arg can't be stored on a target.
2199  */
2200 static int
2201 target_type_is_incompatible (MonoCompile *cfg, MonoType *target, MonoInst *arg)
2202 {
2203         MonoType *simple_type;
2204         MonoClass *klass;
2205
2206         if (target->byref) {
2207                 /* FIXME: check that the pointed to types match */
2208                 if (arg->type == STACK_MP) {
2209                         MonoClass *base_class = mono_class_from_mono_type (target);
2210                         /* This is needed to handle gshared types + ldaddr */
2211                         simple_type = mini_get_underlying_type (&base_class->byval_arg);
2212                         return target->type != MONO_TYPE_I && arg->klass != base_class && arg->klass != mono_class_from_mono_type (simple_type);
2213                 }
2214                 if (arg->type == STACK_PTR)
2215                         return 0;
2216                 return 1;
2217         }
2218
2219         simple_type = mini_get_underlying_type (target);
2220         switch (simple_type->type) {
2221         case MONO_TYPE_VOID:
2222                 return 1;
2223         case MONO_TYPE_I1:
2224         case MONO_TYPE_U1:
2225         case MONO_TYPE_I2:
2226         case MONO_TYPE_U2:
2227         case MONO_TYPE_I4:
2228         case MONO_TYPE_U4:
2229                 if (arg->type != STACK_I4 && arg->type != STACK_PTR)
2230                         return 1;
2231                 return 0;
2232         case MONO_TYPE_PTR:
2233                 /* STACK_MP is needed when setting pinned locals */
2234                 if (arg->type != STACK_I4 && arg->type != STACK_PTR && arg->type != STACK_MP)
2235                         return 1;
2236                 return 0;
2237         case MONO_TYPE_I:
2238         case MONO_TYPE_U:
2239         case MONO_TYPE_FNPTR:
2240                 /* 
2241                  * Some opcodes like ldloca returns 'transient pointers' which can be stored in
2242                  * in native int. (#688008).
2243                  */
2244                 if (arg->type != STACK_I4 && arg->type != STACK_PTR && arg->type != STACK_MP)
2245                         return 1;
2246                 return 0;
2247         case MONO_TYPE_CLASS:
2248         case MONO_TYPE_STRING:
2249         case MONO_TYPE_OBJECT:
2250         case MONO_TYPE_SZARRAY:
2251         case MONO_TYPE_ARRAY:    
2252                 if (arg->type != STACK_OBJ)
2253                         return 1;
2254                 /* FIXME: check type compatibility */
2255                 return 0;
2256         case MONO_TYPE_I8:
2257         case MONO_TYPE_U8:
2258                 if (arg->type != STACK_I8)
2259                         return 1;
2260                 return 0;
2261         case MONO_TYPE_R4:
2262                 if (arg->type != cfg->r4_stack_type)
2263                         return 1;
2264                 return 0;
2265         case MONO_TYPE_R8:
2266                 if (arg->type != STACK_R8)
2267                         return 1;
2268                 return 0;
2269         case MONO_TYPE_VALUETYPE:
2270                 if (arg->type != STACK_VTYPE)
2271                         return 1;
2272                 klass = mono_class_from_mono_type (simple_type);
2273                 if (klass != arg->klass)
2274                         return 1;
2275                 return 0;
2276         case MONO_TYPE_TYPEDBYREF:
2277                 if (arg->type != STACK_VTYPE)
2278                         return 1;
2279                 klass = mono_class_from_mono_type (simple_type);
2280                 if (klass != arg->klass)
2281                         return 1;
2282                 return 0;
2283         case MONO_TYPE_GENERICINST:
2284                 if (mono_type_generic_inst_is_valuetype (simple_type)) {
2285                         MonoClass *target_class;
2286                         if (arg->type != STACK_VTYPE)
2287                                 return 1;
2288                         klass = mono_class_from_mono_type (simple_type);
2289                         target_class = mono_class_from_mono_type (target);
2290                         /* The second cases is needed when doing partial sharing */
2291                         if (klass != arg->klass && target_class != arg->klass && target_class != mono_class_from_mono_type (mini_get_underlying_type (&arg->klass->byval_arg)))
2292                                 return 1;
2293                         return 0;
2294                 } else {
2295                         if (arg->type != STACK_OBJ)
2296                                 return 1;
2297                         /* FIXME: check type compatibility */
2298                         return 0;
2299                 }
2300         case MONO_TYPE_VAR:
2301         case MONO_TYPE_MVAR:
2302                 g_assert (cfg->gshared);
2303                 if (mini_type_var_is_vt (simple_type)) {
2304                         if (arg->type != STACK_VTYPE)
2305                                 return 1;
2306                 } else {
2307                         if (arg->type != STACK_OBJ)
2308                                 return 1;
2309                 }
2310                 return 0;
2311         default:
2312                 g_error ("unknown type 0x%02x in target_type_is_incompatible", simple_type->type);
2313         }
2314         return 1;
2315 }
2316
2317 /*
2318  * Prepare arguments for passing to a function call.
2319  * Return a non-zero value if the arguments can't be passed to the given
2320  * signature.
2321  * The type checks are not yet complete and some conversions may need
2322  * casts on 32 or 64 bit architectures.
2323  *
2324  * FIXME: implement this using target_type_is_incompatible ()
2325  */
2326 static int
2327 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
2328 {
2329         MonoType *simple_type;
2330         int i;
2331
2332         if (sig->hasthis) {
2333                 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
2334                         return 1;
2335                 args++;
2336         }
2337         for (i = 0; i < sig->param_count; ++i) {
2338                 if (sig->params [i]->byref) {
2339                         if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
2340                                 return 1;
2341                         continue;
2342                 }
2343                 simple_type = mini_get_underlying_type (sig->params [i]);
2344 handle_enum:
2345                 switch (simple_type->type) {
2346                 case MONO_TYPE_VOID:
2347                         return 1;
2348                         continue;
2349                 case MONO_TYPE_I1:
2350                 case MONO_TYPE_U1:
2351                 case MONO_TYPE_I2:
2352                 case MONO_TYPE_U2:
2353                 case MONO_TYPE_I4:
2354                 case MONO_TYPE_U4:
2355                         if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
2356                                 return 1;
2357                         continue;
2358                 case MONO_TYPE_I:
2359                 case MONO_TYPE_U:
2360                 case MONO_TYPE_PTR:
2361                 case MONO_TYPE_FNPTR:
2362                         if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
2363                                 return 1;
2364                         continue;
2365                 case MONO_TYPE_CLASS:
2366                 case MONO_TYPE_STRING:
2367                 case MONO_TYPE_OBJECT:
2368                 case MONO_TYPE_SZARRAY:
2369                 case MONO_TYPE_ARRAY:    
2370                         if (args [i]->type != STACK_OBJ)
2371                                 return 1;
2372                         continue;
2373                 case MONO_TYPE_I8:
2374                 case MONO_TYPE_U8:
2375                         if (args [i]->type != STACK_I8)
2376                                 return 1;
2377                         continue;
2378                 case MONO_TYPE_R4:
2379                         if (args [i]->type != cfg->r4_stack_type)
2380                                 return 1;
2381                         continue;
2382                 case MONO_TYPE_R8:
2383                         if (args [i]->type != STACK_R8)
2384                                 return 1;
2385                         continue;
2386                 case MONO_TYPE_VALUETYPE:
2387                         if (simple_type->data.klass->enumtype) {
2388                                 simple_type = mono_class_enum_basetype (simple_type->data.klass);
2389                                 goto handle_enum;
2390                         }
2391                         if (args [i]->type != STACK_VTYPE)
2392                                 return 1;
2393                         continue;
2394                 case MONO_TYPE_TYPEDBYREF:
2395                         if (args [i]->type != STACK_VTYPE)
2396                                 return 1;
2397                         continue;
2398                 case MONO_TYPE_GENERICINST:
2399                         simple_type = &simple_type->data.generic_class->container_class->byval_arg;
2400                         goto handle_enum;
2401                 case MONO_TYPE_VAR:
2402                 case MONO_TYPE_MVAR:
2403                         /* gsharedvt */
2404                         if (args [i]->type != STACK_VTYPE)
2405                                 return 1;
2406                         continue;
2407                 default:
2408                         g_error ("unknown type 0x%02x in check_call_signature",
2409                                  simple_type->type);
2410                 }
2411         }
2412         return 0;
2413 }
2414
2415 static int
2416 callvirt_to_call (int opcode)
2417 {
2418         switch (opcode) {
2419         case OP_CALL_MEMBASE:
2420                 return OP_CALL;
2421         case OP_VOIDCALL_MEMBASE:
2422                 return OP_VOIDCALL;
2423         case OP_FCALL_MEMBASE:
2424                 return OP_FCALL;
2425         case OP_RCALL_MEMBASE:
2426                 return OP_RCALL;
2427         case OP_VCALL_MEMBASE:
2428                 return OP_VCALL;
2429         case OP_LCALL_MEMBASE:
2430                 return OP_LCALL;
2431         default:
2432                 g_assert_not_reached ();
2433         }
2434
2435         return -1;
2436 }
2437
2438 static int
2439 callvirt_to_call_reg (int opcode)
2440 {
2441         switch (opcode) {
2442         case OP_CALL_MEMBASE:
2443                 return OP_CALL_REG;
2444         case OP_VOIDCALL_MEMBASE:
2445                 return OP_VOIDCALL_REG;
2446         case OP_FCALL_MEMBASE:
2447                 return OP_FCALL_REG;
2448         case OP_RCALL_MEMBASE:
2449                 return OP_RCALL_REG;
2450         case OP_VCALL_MEMBASE:
2451                 return OP_VCALL_REG;
2452         case OP_LCALL_MEMBASE:
2453                 return OP_LCALL_REG;
2454         default:
2455                 g_assert_not_reached ();
2456         }
2457
2458         return -1;
2459 }
2460
2461 /* Either METHOD or IMT_ARG needs to be set */
2462 static void
2463 emit_imt_argument (MonoCompile *cfg, MonoCallInst *call, MonoMethod *method, MonoInst *imt_arg)
2464 {
2465         int method_reg;
2466
2467         if (COMPILE_LLVM (cfg)) {
2468                 if (imt_arg) {
2469                         method_reg = alloc_preg (cfg);
2470                         MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, method_reg, imt_arg->dreg);
2471                 } else {
2472                         MonoInst *ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_METHODCONST, method);
2473                         method_reg = ins->dreg;
2474                 }
2475
2476 #ifdef ENABLE_LLVM
2477                 call->imt_arg_reg = method_reg;
2478 #endif
2479                 mono_call_inst_add_outarg_reg (cfg, call, method_reg, MONO_ARCH_IMT_REG, FALSE);
2480                 return;
2481         }
2482
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;
2489         }
2490
2491         mono_call_inst_add_outarg_reg (cfg, call, method_reg, MONO_ARCH_IMT_REG, FALSE);
2492 }
2493
2494 static MonoJumpInfo *
2495 mono_patch_info_new (MonoMemPool *mp, int ip, MonoJumpInfoType type, gconstpointer target)
2496 {
2497         MonoJumpInfo *ji = (MonoJumpInfo *)mono_mempool_alloc (mp, sizeof (MonoJumpInfo));
2498
2499         ji->ip.i = ip;
2500         ji->type = type;
2501         ji->data.target = target;
2502
2503         return ji;
2504 }
2505
2506 static int
2507 mini_class_check_context_used (MonoCompile *cfg, MonoClass *klass)
2508 {
2509         if (cfg->gshared)
2510                 return mono_class_check_context_used (klass);
2511         else
2512                 return 0;
2513 }
2514
2515 static int
2516 mini_method_check_context_used (MonoCompile *cfg, MonoMethod *method)
2517 {
2518         if (cfg->gshared)
2519                 return mono_method_check_context_used (method);
2520         else
2521                 return 0;
2522 }
2523
2524 /*
2525  * check_method_sharing:
2526  *
2527  *   Check whenever the vtable or an mrgctx needs to be passed when calling CMETHOD.
2528  */
2529 static void
2530 check_method_sharing (MonoCompile *cfg, MonoMethod *cmethod, gboolean *out_pass_vtable, gboolean *out_pass_mrgctx)
2531 {
2532         gboolean pass_vtable = FALSE;
2533         gboolean pass_mrgctx = FALSE;
2534
2535         if (((cmethod->flags & METHOD_ATTRIBUTE_STATIC) || cmethod->klass->valuetype) &&
2536                 (cmethod->klass->generic_class || cmethod->klass->generic_container)) {
2537                 gboolean sharable = FALSE;
2538
2539                 if (mono_method_is_generic_sharable_full (cmethod, TRUE, TRUE, TRUE))
2540                         sharable = TRUE;
2541
2542                 /*
2543                  * Pass vtable iff target method might
2544                  * be shared, which means that sharing
2545                  * is enabled for its class and its
2546                  * context is sharable (and it's not a
2547                  * generic method).
2548                  */
2549                 if (sharable && !(mini_method_get_context (cmethod) && mini_method_get_context (cmethod)->method_inst))
2550                         pass_vtable = TRUE;
2551         }
2552
2553         if (mini_method_get_context (cmethod) &&
2554                 mini_method_get_context (cmethod)->method_inst) {
2555                 g_assert (!pass_vtable);
2556
2557                 if (mono_method_is_generic_sharable_full (cmethod, TRUE, TRUE, TRUE)) {
2558                         pass_mrgctx = TRUE;
2559                 } else {
2560                         if (cfg->gsharedvt && mini_is_gsharedvt_signature (mono_method_signature (cmethod)))
2561                                 pass_mrgctx = TRUE;
2562                 }
2563         }
2564
2565         if (out_pass_vtable)
2566                 *out_pass_vtable = pass_vtable;
2567         if (out_pass_mrgctx)
2568                 *out_pass_mrgctx = pass_mrgctx;
2569 }
2570
2571 inline static MonoCallInst *
2572 mono_emit_call_args (MonoCompile *cfg, MonoMethodSignature *sig, 
2573                                          MonoInst **args, int calli, int virtual_, int tail, int rgctx, int unbox_trampoline)
2574 {
2575         MonoType *sig_ret;
2576         MonoCallInst *call;
2577 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
2578         int i;
2579 #endif
2580
2581         if (cfg->llvm_only)
2582                 tail = FALSE;
2583
2584         if (tail) {
2585                 emit_instrumentation_call (cfg, mono_profiler_method_leave);
2586
2587                 MONO_INST_NEW_CALL (cfg, call, OP_TAILCALL);
2588         } else
2589                 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (cfg, sig->ret, calli, virtual_));
2590
2591         call->args = args;
2592         call->signature = sig;
2593         call->rgctx_reg = rgctx;
2594         sig_ret = mini_get_underlying_type (sig->ret);
2595
2596         type_to_eval_stack_type ((cfg), sig_ret, &call->inst);
2597
2598         if (tail) {
2599                 if (mini_type_is_vtype (sig_ret)) {
2600                         call->vret_var = cfg->vret_addr;
2601                         //g_assert_not_reached ();
2602                 }
2603         } else if (mini_type_is_vtype (sig_ret)) {
2604                 MonoInst *temp = mono_compile_create_var (cfg, sig_ret, OP_LOCAL);
2605                 MonoInst *loada;
2606
2607                 temp->backend.is_pinvoke = sig->pinvoke;
2608
2609                 /*
2610                  * We use a new opcode OP_OUTARG_VTRETADDR instead of LDADDR for emitting the
2611                  * address of return value to increase optimization opportunities.
2612                  * Before vtype decomposition, the dreg of the call ins itself represents the
2613                  * fact the call modifies the return value. After decomposition, the call will
2614                  * be transformed into one of the OP_VOIDCALL opcodes, and the VTRETADDR opcode
2615                  * will be transformed into an LDADDR.
2616                  */
2617                 MONO_INST_NEW (cfg, loada, OP_OUTARG_VTRETADDR);
2618                 loada->dreg = alloc_preg (cfg);
2619                 loada->inst_p0 = temp;
2620                 /* We reference the call too since call->dreg could change during optimization */
2621                 loada->inst_p1 = call;
2622                 MONO_ADD_INS (cfg->cbb, loada);
2623
2624                 call->inst.dreg = temp->dreg;
2625
2626                 call->vret_var = loada;
2627         } else if (!MONO_TYPE_IS_VOID (sig_ret))
2628                 call->inst.dreg = alloc_dreg (cfg, (MonoStackType)call->inst.type);
2629
2630 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
2631         if (COMPILE_SOFT_FLOAT (cfg)) {
2632                 /* 
2633                  * If the call has a float argument, we would need to do an r8->r4 conversion using 
2634                  * an icall, but that cannot be done during the call sequence since it would clobber
2635                  * the call registers + the stack. So we do it before emitting the call.
2636                  */
2637                 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2638                         MonoType *t;
2639                         MonoInst *in = call->args [i];
2640
2641                         if (i >= sig->hasthis)
2642                                 t = sig->params [i - sig->hasthis];
2643                         else
2644                                 t = &mono_defaults.int_class->byval_arg;
2645                         t = mono_type_get_underlying_type (t);
2646
2647                         if (!t->byref && t->type == MONO_TYPE_R4) {
2648                                 MonoInst *iargs [1];
2649                                 MonoInst *conv;
2650
2651                                 iargs [0] = in;
2652                                 conv = mono_emit_jit_icall (cfg, mono_fload_r4_arg, iargs);
2653
2654                                 /* The result will be in an int vreg */
2655                                 call->args [i] = conv;
2656                         }
2657                 }
2658         }
2659 #endif
2660
2661         call->need_unbox_trampoline = unbox_trampoline;
2662
2663 #ifdef ENABLE_LLVM
2664         if (COMPILE_LLVM (cfg))
2665                 mono_llvm_emit_call (cfg, call);
2666         else
2667                 mono_arch_emit_call (cfg, call);
2668 #else
2669         mono_arch_emit_call (cfg, call);
2670 #endif
2671
2672         cfg->param_area = MAX (cfg->param_area, call->stack_usage);
2673         cfg->flags |= MONO_CFG_HAS_CALLS;
2674         
2675         return call;
2676 }
2677
2678 static void
2679 set_rgctx_arg (MonoCompile *cfg, MonoCallInst *call, int rgctx_reg, MonoInst *rgctx_arg)
2680 {
2681         mono_call_inst_add_outarg_reg (cfg, call, rgctx_reg, MONO_ARCH_RGCTX_REG, FALSE);
2682         cfg->uses_rgctx_reg = TRUE;
2683         call->rgctx_reg = TRUE;
2684 #ifdef ENABLE_LLVM
2685         call->rgctx_arg_reg = rgctx_reg;
2686 #endif
2687 }       
2688
2689 inline static MonoInst*
2690 mono_emit_calli (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args, MonoInst *addr, MonoInst *imt_arg, MonoInst *rgctx_arg)
2691 {
2692         MonoCallInst *call;
2693         MonoInst *ins;
2694         int rgctx_reg = -1;
2695         gboolean check_sp = FALSE;
2696
2697         if (cfg->check_pinvoke_callconv && cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
2698                 WrapperInfo *info = mono_marshal_get_wrapper_info (cfg->method);
2699
2700                 if (info && info->subtype == WRAPPER_SUBTYPE_PINVOKE)
2701                         check_sp = TRUE;
2702         }
2703
2704         if (rgctx_arg) {
2705                 rgctx_reg = mono_alloc_preg (cfg);
2706                 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, rgctx_reg, rgctx_arg->dreg);
2707         }
2708
2709         if (check_sp) {
2710                 if (!cfg->stack_inbalance_var)
2711                         cfg->stack_inbalance_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2712
2713                 MONO_INST_NEW (cfg, ins, OP_GET_SP);
2714                 ins->dreg = cfg->stack_inbalance_var->dreg;
2715                 MONO_ADD_INS (cfg->cbb, ins);
2716         }
2717
2718         call = mono_emit_call_args (cfg, sig, args, TRUE, FALSE, FALSE, rgctx_arg ? TRUE : FALSE, FALSE);
2719
2720         call->inst.sreg1 = addr->dreg;
2721
2722         if (imt_arg)
2723                 emit_imt_argument (cfg, call, NULL, imt_arg);
2724
2725         MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2726
2727         if (check_sp) {
2728                 int sp_reg;
2729
2730                 sp_reg = mono_alloc_preg (cfg);
2731
2732                 MONO_INST_NEW (cfg, ins, OP_GET_SP);
2733                 ins->dreg = sp_reg;
2734                 MONO_ADD_INS (cfg->cbb, ins);
2735
2736                 /* Restore the stack so we don't crash when throwing the exception */
2737                 MONO_INST_NEW (cfg, ins, OP_SET_SP);
2738                 ins->sreg1 = cfg->stack_inbalance_var->dreg;
2739                 MONO_ADD_INS (cfg->cbb, ins);
2740
2741                 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, cfg->stack_inbalance_var->dreg, sp_reg);
2742                 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "ExecutionEngineException");
2743         }
2744
2745         if (rgctx_arg)
2746                 set_rgctx_arg (cfg, call, rgctx_reg, rgctx_arg);
2747
2748         return (MonoInst*)call;
2749 }
2750
2751 static MonoInst*
2752 emit_get_gsharedvt_info_klass (MonoCompile *cfg, MonoClass *klass, MonoRgctxInfoType rgctx_type);
2753
2754 static MonoInst*
2755 emit_get_rgctx_method (MonoCompile *cfg, int context_used, MonoMethod *cmethod, MonoRgctxInfoType rgctx_type);
2756 static MonoInst*
2757 emit_get_rgctx_klass (MonoCompile *cfg, int context_used, MonoClass *klass, MonoRgctxInfoType rgctx_type);
2758
2759 static MonoInst*
2760 mono_emit_method_call_full (MonoCompile *cfg, MonoMethod *method, MonoMethodSignature *sig, gboolean tail,
2761                                                         MonoInst **args, MonoInst *this_ins, MonoInst *imt_arg, MonoInst *rgctx_arg)
2762 {
2763 #ifndef DISABLE_REMOTING
2764         gboolean might_be_remote = FALSE;
2765 #endif
2766         gboolean virtual_ = this_ins != NULL;
2767         gboolean enable_for_aot = TRUE;
2768         int context_used;
2769         MonoCallInst *call;
2770         MonoInst *call_target = NULL;
2771         int rgctx_reg = 0;
2772         gboolean need_unbox_trampoline;
2773
2774         if (!sig)
2775                 sig = mono_method_signature (method);
2776
2777         if (cfg->llvm_only && (method->klass->flags & TYPE_ATTRIBUTE_INTERFACE))
2778                 g_assert_not_reached ();
2779
2780         if (rgctx_arg) {
2781                 rgctx_reg = mono_alloc_preg (cfg);
2782                 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, rgctx_reg, rgctx_arg->dreg);
2783         }
2784
2785         if (method->string_ctor) {
2786                 /* Create the real signature */
2787                 /* FIXME: Cache these */
2788                 MonoMethodSignature *ctor_sig = mono_metadata_signature_dup_mempool (cfg->mempool, sig);
2789                 ctor_sig->ret = &mono_defaults.string_class->byval_arg;
2790
2791                 sig = ctor_sig;
2792         }
2793
2794         context_used = mini_method_check_context_used (cfg, method);
2795
2796 #ifndef DISABLE_REMOTING
2797         might_be_remote = this_ins && sig->hasthis &&
2798                 (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class) &&
2799                 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && (!MONO_CHECK_THIS (this_ins) || context_used);
2800
2801         if (might_be_remote && context_used) {
2802                 MonoInst *addr;
2803
2804                 g_assert (cfg->gshared);
2805
2806                 addr = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_REMOTING_INVOKE_WITH_CHECK);
2807
2808                 return mono_emit_calli (cfg, sig, args, addr, NULL, NULL);
2809         }
2810 #endif
2811
2812         if (cfg->llvm_only && !call_target && virtual_ && (method->flags & METHOD_ATTRIBUTE_VIRTUAL))
2813                 return emit_llvmonly_virtual_call (cfg, method, sig, 0, args);
2814
2815         need_unbox_trampoline = method->klass == mono_defaults.object_class || (method->klass->flags & TYPE_ATTRIBUTE_INTERFACE);
2816
2817         call = mono_emit_call_args (cfg, sig, args, FALSE, virtual_, tail, rgctx_arg ? TRUE : FALSE, need_unbox_trampoline);
2818
2819 #ifndef DISABLE_REMOTING
2820         if (might_be_remote)
2821                 call->method = mono_marshal_get_remoting_invoke_with_check (method);
2822         else
2823 #endif
2824                 call->method = method;
2825         call->inst.flags |= MONO_INST_HAS_METHOD;
2826         call->inst.inst_left = this_ins;
2827         call->tail_call = tail;
2828
2829         if (virtual_) {
2830                 int vtable_reg, slot_reg, this_reg;
2831                 int offset;
2832
2833                 this_reg = this_ins->dreg;
2834
2835                 if (!cfg->llvm_only && (method->klass->parent == mono_defaults.multicastdelegate_class) && !strcmp (method->name, "Invoke")) {
2836                         MonoInst *dummy_use;
2837
2838                         MONO_EMIT_NULL_CHECK (cfg, this_reg);
2839
2840                         /* Make a call to delegate->invoke_impl */
2841                         call->inst.inst_basereg = this_reg;
2842                         call->inst.inst_offset = MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl);
2843                         MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2844
2845                         /* We must emit a dummy use here because the delegate trampoline will
2846                         replace the 'this' argument with the delegate target making this activation
2847                         no longer a root for the delegate.
2848                         This is an issue for delegates that target collectible code such as dynamic
2849                         methods of GC'able assemblies.
2850
2851                         For a test case look into #667921.
2852
2853                         FIXME: a dummy use is not the best way to do it as the local register allocator
2854                         will put it on a caller save register and spil it around the call. 
2855                         Ideally, we would either put it on a callee save register or only do the store part.  
2856                          */
2857                         EMIT_NEW_DUMMY_USE (cfg, dummy_use, args [0]);
2858
2859                         return (MonoInst*)call;
2860                 }
2861
2862                 if ((!cfg->compile_aot || enable_for_aot) && 
2863                         (!(method->flags & METHOD_ATTRIBUTE_VIRTUAL) || 
2864                          (MONO_METHOD_IS_FINAL (method) &&
2865                           method->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)) &&
2866                         !(mono_class_is_marshalbyref (method->klass) && context_used)) {
2867                         /* 
2868                          * the method is not virtual, we just need to ensure this is not null
2869                          * and then we can call the method directly.
2870                          */
2871 #ifndef DISABLE_REMOTING
2872                         if (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class) {
2873                                 /* 
2874                                  * The check above ensures method is not gshared, this is needed since
2875                                  * gshared methods can't have wrappers.
2876                                  */
2877                                 method = call->method = mono_marshal_get_remoting_invoke_with_check (method);
2878                         }
2879 #endif
2880
2881                         if (!method->string_ctor)
2882                                 MONO_EMIT_NEW_CHECK_THIS (cfg, this_reg);
2883
2884                         call->inst.opcode = callvirt_to_call (call->inst.opcode);
2885                 } else if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && MONO_METHOD_IS_FINAL (method)) {
2886                         /*
2887                          * the method is virtual, but we can statically dispatch since either
2888                          * it's class or the method itself are sealed.
2889                          * But first we need to ensure it's not a null reference.
2890                          */
2891                         MONO_EMIT_NEW_CHECK_THIS (cfg, this_reg);
2892
2893                         call->inst.opcode = callvirt_to_call (call->inst.opcode);
2894                 } else if (call_target) {
2895                         vtable_reg = alloc_preg (cfg);
2896                         MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
2897
2898                         call->inst.opcode = callvirt_to_call_reg (call->inst.opcode);
2899                         call->inst.sreg1 = call_target->dreg;
2900                         call->inst.flags &= !MONO_INST_HAS_METHOD;
2901                 } else {
2902                         vtable_reg = alloc_preg (cfg);
2903                         MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
2904                         if (method->klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
2905                                 guint32 imt_slot = mono_method_get_imt_slot (method);
2906                                 emit_imt_argument (cfg, call, call->method, imt_arg);
2907                                 slot_reg = vtable_reg;
2908                                 offset = ((gint32)imt_slot - MONO_IMT_SIZE) * SIZEOF_VOID_P;
2909                         } else {
2910                                 slot_reg = vtable_reg;
2911                                 offset = MONO_STRUCT_OFFSET (MonoVTable, vtable) +
2912                                         ((mono_method_get_vtable_index (method)) * (SIZEOF_VOID_P));
2913                                 if (imt_arg) {
2914                                         g_assert (mono_method_signature (method)->generic_param_count);
2915                                         emit_imt_argument (cfg, call, call->method, imt_arg);
2916                                 }
2917                         }
2918
2919                         call->inst.sreg1 = slot_reg;
2920                         call->inst.inst_offset = offset;
2921                         call->is_virtual = TRUE;
2922                 }
2923         }
2924
2925         MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2926
2927         if (rgctx_arg)
2928                 set_rgctx_arg (cfg, call, rgctx_reg, rgctx_arg);
2929
2930         return (MonoInst*)call;
2931 }
2932
2933 MonoInst*
2934 mono_emit_method_call (MonoCompile *cfg, MonoMethod *method, MonoInst **args, MonoInst *this_ins)
2935 {
2936         return mono_emit_method_call_full (cfg, method, mono_method_signature (method), FALSE, args, this_ins, NULL, NULL);
2937 }
2938
2939 MonoInst*
2940 mono_emit_native_call (MonoCompile *cfg, gconstpointer func, MonoMethodSignature *sig,
2941                                            MonoInst **args)
2942 {
2943         MonoCallInst *call;
2944
2945         g_assert (sig);
2946
2947         call = mono_emit_call_args (cfg, sig, args, FALSE, FALSE, FALSE, FALSE, FALSE);
2948         call->fptr = func;
2949
2950         MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
2951
2952         return (MonoInst*)call;
2953 }
2954
2955 MonoInst*
2956 mono_emit_jit_icall (MonoCompile *cfg, gconstpointer func, MonoInst **args)
2957 {
2958         MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
2959
2960         g_assert (info);
2961
2962         return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, args);
2963 }
2964
2965 /*
2966  * mono_emit_abs_call:
2967  *
2968  *   Emit a call to the runtime function described by PATCH_TYPE and DATA.
2969  */
2970 inline static MonoInst*
2971 mono_emit_abs_call (MonoCompile *cfg, MonoJumpInfoType patch_type, gconstpointer data, 
2972                                         MonoMethodSignature *sig, MonoInst **args)
2973 {
2974         MonoJumpInfo *ji = mono_patch_info_new (cfg->mempool, 0, patch_type, data);
2975         MonoInst *ins;
2976
2977         /* 
2978          * We pass ji as the call address, the PATCH_INFO_ABS resolving code will
2979          * handle it.
2980          */
2981         if (cfg->abs_patches == NULL)
2982                 cfg->abs_patches = g_hash_table_new (NULL, NULL);
2983         g_hash_table_insert (cfg->abs_patches, ji, ji);
2984         ins = mono_emit_native_call (cfg, ji, sig, args);
2985         ((MonoCallInst*)ins)->fptr_is_patch = TRUE;
2986         return ins;
2987 }
2988
2989 static MonoMethodSignature*
2990 sig_to_rgctx_sig (MonoMethodSignature *sig)
2991 {
2992         // FIXME: memory allocation
2993         MonoMethodSignature *res;
2994         int i;
2995
2996         res = (MonoMethodSignature *)g_malloc (MONO_SIZEOF_METHOD_SIGNATURE + (sig->param_count + 1) * sizeof (MonoType*));
2997         memcpy (res, sig, MONO_SIZEOF_METHOD_SIGNATURE);
2998         res->param_count = sig->param_count + 1;
2999         for (i = 0; i < sig->param_count; ++i)
3000                 res->params [i] = sig->params [i];
3001         res->params [sig->param_count] = &mono_defaults.int_class->this_arg;
3002         return res;
3003 }
3004
3005 /* Make an indirect call to FSIG passing an additional argument */
3006 static MonoInst*
3007 emit_extra_arg_calli (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **orig_args, int arg_reg, MonoInst *call_target)
3008 {
3009         MonoMethodSignature *csig;
3010         MonoInst *args_buf [16];
3011         MonoInst **args;
3012         int i, pindex, tmp_reg;
3013
3014         /* Make a call with an rgctx/extra arg */
3015         if (fsig->param_count + 2 < 16)
3016                 args = args_buf;
3017         else
3018                 args = (MonoInst **)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (fsig->param_count + 2));
3019         pindex = 0;
3020         if (fsig->hasthis)
3021                 args [pindex ++] = orig_args [0];
3022         for (i = 0; i < fsig->param_count; ++i)
3023                 args [pindex ++] = orig_args [fsig->hasthis + i];
3024         tmp_reg = alloc_preg (cfg);
3025         EMIT_NEW_UNALU (cfg, args [pindex], OP_MOVE, tmp_reg, arg_reg);
3026         csig = sig_to_rgctx_sig (fsig);
3027         return mono_emit_calli (cfg, csig, args, call_target, NULL, NULL);
3028 }
3029
3030 /* Emit an indirect call to the function descriptor ADDR */
3031 static MonoInst*
3032 emit_llvmonly_calli (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, MonoInst *addr)
3033 {
3034         int addr_reg, arg_reg;
3035         MonoInst *call_target;
3036
3037         g_assert (cfg->llvm_only);
3038
3039         /*
3040          * addr points to a <addr, arg> pair, load both of them, and
3041          * make a call to addr, passing arg as an extra arg.
3042          */
3043         addr_reg = alloc_preg (cfg);
3044         EMIT_NEW_LOAD_MEMBASE (cfg, call_target, OP_LOAD_MEMBASE, addr_reg, addr->dreg, 0);
3045         arg_reg = alloc_preg (cfg);
3046         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, arg_reg, addr->dreg, sizeof (gpointer));
3047
3048         return emit_extra_arg_calli (cfg, fsig, args, arg_reg, call_target);
3049 }
3050
3051 static gboolean
3052 direct_icalls_enabled (MonoCompile *cfg)
3053 {
3054         /* LLVM on amd64 can't handle calls to non-32 bit addresses */
3055 #ifdef TARGET_AMD64
3056         if (cfg->compile_llvm)
3057                 return FALSE;
3058 #endif
3059         if (cfg->gen_sdb_seq_points || cfg->disable_direct_icalls)
3060                 return FALSE;
3061         return TRUE;
3062 }
3063
3064 MonoInst*
3065 mono_emit_jit_icall_by_info (MonoCompile *cfg, MonoJitICallInfo *info, MonoInst **args)
3066 {
3067         /*
3068          * Call the jit icall without a wrapper if possible.
3069          * The wrapper is needed for the following reasons:
3070          * - to handle exceptions thrown using mono_raise_exceptions () from the
3071          *   icall function. The EH code needs the lmf frame pushed by the
3072          *   wrapper to be able to unwind back to managed code.
3073          * - to be able to do stack walks for asynchronously suspended
3074          *   threads when debugging.
3075          */
3076         if (info->no_raise && direct_icalls_enabled (cfg)) {
3077                 char *name;
3078                 int costs;
3079
3080                 if (!info->wrapper_method) {
3081                         name = g_strdup_printf ("__icall_wrapper_%s", info->name);
3082                         info->wrapper_method = mono_marshal_get_icall_wrapper (info->sig, name, info->func, TRUE);
3083                         g_free (name);
3084                         mono_memory_barrier ();
3085                 }
3086
3087                 /*
3088                  * Inline the wrapper method, which is basically a call to the C icall, and
3089                  * an exception check.
3090                  */
3091                 costs = inline_method (cfg, info->wrapper_method, NULL,
3092                                                            args, NULL, cfg->real_offset, TRUE);
3093                 g_assert (costs > 0);
3094                 g_assert (!MONO_TYPE_IS_VOID (info->sig->ret));
3095
3096                 return args [0];
3097         } else {
3098                 return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, args);
3099         }
3100 }
3101  
3102 static MonoInst*
3103 mono_emit_widen_call_res (MonoCompile *cfg, MonoInst *ins, MonoMethodSignature *fsig)
3104 {
3105         if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3106                 if ((fsig->pinvoke || LLVM_ENABLED) && !fsig->ret->byref) {
3107                         int widen_op = -1;
3108
3109                         /* 
3110                          * Native code might return non register sized integers 
3111                          * without initializing the upper bits.
3112                          */
3113                         switch (mono_type_to_load_membase (cfg, fsig->ret)) {
3114                         case OP_LOADI1_MEMBASE:
3115                                 widen_op = OP_ICONV_TO_I1;
3116                                 break;
3117                         case OP_LOADU1_MEMBASE:
3118                                 widen_op = OP_ICONV_TO_U1;
3119                                 break;
3120                         case OP_LOADI2_MEMBASE:
3121                                 widen_op = OP_ICONV_TO_I2;
3122                                 break;
3123                         case OP_LOADU2_MEMBASE:
3124                                 widen_op = OP_ICONV_TO_U2;
3125                                 break;
3126                         default:
3127                                 break;
3128                         }
3129
3130                         if (widen_op != -1) {
3131                                 int dreg = alloc_preg (cfg);
3132                                 MonoInst *widen;
3133
3134                                 EMIT_NEW_UNALU (cfg, widen, widen_op, dreg, ins->dreg);
3135                                 widen->type = ins->type;
3136                                 ins = widen;
3137                         }
3138                 }
3139         }
3140
3141         return ins;
3142 }
3143
3144 static MonoMethod*
3145 get_memcpy_method (void)
3146 {
3147         static MonoMethod *memcpy_method = NULL;
3148         if (!memcpy_method) {
3149                 memcpy_method = mono_class_get_method_from_name (mono_defaults.string_class, "memcpy", 3);
3150                 if (!memcpy_method)
3151                         g_error ("Old corlib found. Install a new one");
3152         }
3153         return memcpy_method;
3154 }
3155
3156 static void
3157 create_write_barrier_bitmap (MonoCompile *cfg, MonoClass *klass, unsigned *wb_bitmap, int offset)
3158 {
3159         MonoClassField *field;
3160         gpointer iter = NULL;
3161
3162         while ((field = mono_class_get_fields (klass, &iter))) {
3163                 int foffset;
3164
3165                 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
3166                         continue;
3167                 foffset = klass->valuetype ? field->offset - sizeof (MonoObject): field->offset;
3168                 if (mini_type_is_reference (mono_field_get_type (field))) {
3169                         g_assert ((foffset % SIZEOF_VOID_P) == 0);
3170                         *wb_bitmap |= 1 << ((offset + foffset) / SIZEOF_VOID_P);
3171                 } else {
3172                         MonoClass *field_class = mono_class_from_mono_type (field->type);
3173                         if (field_class->has_references)
3174                                 create_write_barrier_bitmap (cfg, field_class, wb_bitmap, offset + foffset);
3175                 }
3176         }
3177 }
3178
3179 static void
3180 emit_write_barrier (MonoCompile *cfg, MonoInst *ptr, MonoInst *value)
3181 {
3182         int card_table_shift_bits;
3183         gpointer card_table_mask;
3184         guint8 *card_table;
3185         MonoInst *dummy_use;
3186         int nursery_shift_bits;
3187         size_t nursery_size;
3188
3189         if (!cfg->gen_write_barriers)
3190                 return;
3191
3192         card_table = mono_gc_get_card_table (&card_table_shift_bits, &card_table_mask);
3193
3194         mono_gc_get_nursery (&nursery_shift_bits, &nursery_size);
3195
3196         if (cfg->backend->have_card_table_wb && !cfg->compile_aot && card_table && nursery_shift_bits > 0 && !COMPILE_LLVM (cfg)) {
3197                 MonoInst *wbarrier;
3198
3199                 MONO_INST_NEW (cfg, wbarrier, OP_CARD_TABLE_WBARRIER);
3200                 wbarrier->sreg1 = ptr->dreg;
3201                 wbarrier->sreg2 = value->dreg;
3202                 MONO_ADD_INS (cfg->cbb, wbarrier);
3203         } else if (card_table && !cfg->compile_aot && !mono_gc_card_table_nursery_check ()) {
3204                 int offset_reg = alloc_preg (cfg);
3205                 int card_reg;
3206                 MonoInst *ins;
3207
3208                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHR_UN_IMM, offset_reg, ptr->dreg, card_table_shift_bits);
3209                 if (card_table_mask)
3210                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PAND_IMM, offset_reg, offset_reg, card_table_mask);
3211
3212                 /*We can't use PADD_IMM since the cardtable might end up in high addresses and amd64 doesn't support
3213                  * IMM's larger than 32bits.
3214                  */
3215                 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_CARD_TABLE_ADDR, NULL);
3216                 card_reg = ins->dreg;
3217
3218                 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, offset_reg, offset_reg, card_reg);
3219                 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREI1_MEMBASE_IMM, offset_reg, 0, 1);
3220         } else {
3221                 MonoMethod *write_barrier = mono_gc_get_write_barrier ();
3222                 mono_emit_method_call (cfg, write_barrier, &ptr, NULL);
3223         }
3224
3225         EMIT_NEW_DUMMY_USE (cfg, dummy_use, value);
3226 }
3227
3228 static gboolean
3229 mono_emit_wb_aware_memcpy (MonoCompile *cfg, MonoClass *klass, MonoInst *iargs[4], int size, int align)
3230 {
3231         int dest_ptr_reg, tmp_reg, destreg, srcreg, offset;
3232         unsigned need_wb = 0;
3233
3234         if (align == 0)
3235                 align = 4;
3236
3237         /*types with references can't have alignment smaller than sizeof(void*) */
3238         if (align < SIZEOF_VOID_P)
3239                 return FALSE;
3240
3241         /*This value cannot be bigger than 32 due to the way we calculate the required wb bitmap.*/
3242         if (size > 32 * SIZEOF_VOID_P)
3243                 return FALSE;
3244
3245         create_write_barrier_bitmap (cfg, klass, &need_wb, 0);
3246
3247         /* We don't unroll more than 5 stores to avoid code bloat. */
3248         if (size > 5 * SIZEOF_VOID_P) {
3249                 /*This is harmless and simplify mono_gc_wbarrier_value_copy_bitmap */
3250                 size += (SIZEOF_VOID_P - 1);
3251                 size &= ~(SIZEOF_VOID_P - 1);
3252
3253                 EMIT_NEW_ICONST (cfg, iargs [2], size);
3254                 EMIT_NEW_ICONST (cfg, iargs [3], need_wb);
3255                 mono_emit_jit_icall (cfg, mono_gc_wbarrier_value_copy_bitmap, iargs);
3256                 return TRUE;
3257         }
3258
3259         destreg = iargs [0]->dreg;
3260         srcreg = iargs [1]->dreg;
3261         offset = 0;
3262
3263         dest_ptr_reg = alloc_preg (cfg);
3264         tmp_reg = alloc_preg (cfg);
3265
3266         /*tmp = dreg*/
3267         EMIT_NEW_UNALU (cfg, iargs [0], OP_MOVE, dest_ptr_reg, destreg);
3268
3269         while (size >= SIZEOF_VOID_P) {
3270                 MonoInst *load_inst;
3271                 MONO_INST_NEW (cfg, load_inst, OP_LOAD_MEMBASE);
3272                 load_inst->dreg = tmp_reg;
3273                 load_inst->inst_basereg = srcreg;
3274                 load_inst->inst_offset = offset;
3275                 MONO_ADD_INS (cfg->cbb, load_inst);
3276
3277                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, dest_ptr_reg, 0, tmp_reg);
3278
3279                 if (need_wb & 0x1)
3280                         emit_write_barrier (cfg, iargs [0], load_inst);
3281
3282                 offset += SIZEOF_VOID_P;
3283                 size -= SIZEOF_VOID_P;
3284                 need_wb >>= 1;
3285
3286                 /*tmp += sizeof (void*)*/
3287                 if (size >= SIZEOF_VOID_P) {
3288                         NEW_BIALU_IMM (cfg, iargs [0], OP_PADD_IMM, dest_ptr_reg, dest_ptr_reg, SIZEOF_VOID_P);
3289                         MONO_ADD_INS (cfg->cbb, iargs [0]);
3290                 }
3291         }
3292
3293         /* Those cannot be references since size < sizeof (void*) */
3294         while (size >= 4) {
3295                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, tmp_reg, srcreg, offset);
3296                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, offset, tmp_reg);
3297                 offset += 4;
3298                 size -= 4;
3299         }
3300
3301         while (size >= 2) {
3302                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI2_MEMBASE, tmp_reg, srcreg, offset);
3303                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI2_MEMBASE_REG, destreg, offset, tmp_reg);
3304                 offset += 2;
3305                 size -= 2;
3306         }
3307
3308         while (size >= 1) {
3309                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, tmp_reg, srcreg, offset);
3310                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, offset, tmp_reg);
3311                 offset += 1;
3312                 size -= 1;
3313         }
3314
3315         return TRUE;
3316 }
3317
3318 /*
3319  * Emit code to copy a valuetype of type @klass whose address is stored in
3320  * @src->dreg to memory whose address is stored at @dest->dreg.
3321  */
3322 void
3323 mini_emit_stobj (MonoCompile *cfg, MonoInst *dest, MonoInst *src, MonoClass *klass, gboolean native)
3324 {
3325         MonoInst *iargs [4];
3326         int n;
3327         guint32 align = 0;
3328         MonoMethod *memcpy_method;
3329         MonoInst *size_ins = NULL;
3330         MonoInst *memcpy_ins = NULL;
3331
3332         g_assert (klass);
3333         if (cfg->gshared)
3334                 klass = mono_class_from_mono_type (mini_get_underlying_type (&klass->byval_arg));
3335
3336         /*
3337          * This check breaks with spilled vars... need to handle it during verification anyway.
3338          * g_assert (klass && klass == src->klass && klass == dest->klass);
3339          */
3340
3341         if (mini_is_gsharedvt_klass (klass)) {
3342                 g_assert (!native);
3343                 size_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_VALUE_SIZE);
3344                 memcpy_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_MEMCPY);
3345         }
3346
3347         if (native)
3348                 n = mono_class_native_size (klass, &align);
3349         else
3350                 n = mono_class_value_size (klass, &align);
3351
3352         /* if native is true there should be no references in the struct */
3353         if (cfg->gen_write_barriers && (klass->has_references || size_ins) && !native) {
3354                 /* Avoid barriers when storing to the stack */
3355                 if (!((dest->opcode == OP_ADD_IMM && dest->sreg1 == cfg->frame_reg) ||
3356                           (dest->opcode == OP_LDADDR))) {
3357                         int context_used;
3358
3359                         iargs [0] = dest;
3360                         iargs [1] = src;
3361
3362                         context_used = mini_class_check_context_used (cfg, klass);
3363
3364                         /* It's ok to intrinsify under gsharing since shared code types are layout stable. */
3365                         if (!size_ins && (cfg->opt & MONO_OPT_INTRINS) && mono_emit_wb_aware_memcpy (cfg, klass, iargs, n, align)) {
3366                                 return;
3367                         } else if (context_used) {
3368                                 iargs [2] = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
3369                         }  else {
3370                                 iargs [2] = emit_runtime_constant (cfg, MONO_PATCH_INFO_CLASS, klass);
3371                                 if (!cfg->compile_aot)
3372                                         mono_class_compute_gc_descriptor (klass);
3373                         }
3374
3375                         if (size_ins)
3376                                 mono_emit_jit_icall (cfg, mono_gsharedvt_value_copy, iargs);
3377                         else
3378                                 mono_emit_jit_icall (cfg, mono_value_copy, iargs);
3379                         return;
3380                 }
3381         }
3382
3383         if (!size_ins && (cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 8) {
3384                 /* FIXME: Optimize the case when src/dest is OP_LDADDR */
3385                 mini_emit_memcpy (cfg, dest->dreg, 0, src->dreg, 0, n, align);
3386         } else {
3387                 iargs [0] = dest;
3388                 iargs [1] = src;
3389                 if (size_ins)
3390                         iargs [2] = size_ins;
3391                 else
3392                         EMIT_NEW_ICONST (cfg, iargs [2], n);
3393                 
3394                 memcpy_method = get_memcpy_method ();
3395                 if (memcpy_ins)
3396                         mono_emit_calli (cfg, mono_method_signature (memcpy_method), iargs, memcpy_ins, NULL, NULL);
3397                 else
3398                         mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
3399         }
3400 }
3401
3402 static MonoMethod*
3403 get_memset_method (void)
3404 {
3405         static MonoMethod *memset_method = NULL;
3406         if (!memset_method) {
3407                 memset_method = mono_class_get_method_from_name (mono_defaults.string_class, "memset", 3);
3408                 if (!memset_method)
3409                         g_error ("Old corlib found. Install a new one");
3410         }
3411         return memset_method;
3412 }
3413
3414 void
3415 mini_emit_initobj (MonoCompile *cfg, MonoInst *dest, const guchar *ip, MonoClass *klass)
3416 {
3417         MonoInst *iargs [3];
3418         int n;
3419         guint32 align;
3420         MonoMethod *memset_method;
3421         MonoInst *size_ins = NULL;
3422         MonoInst *bzero_ins = NULL;
3423         static MonoMethod *bzero_method;
3424
3425         /* FIXME: Optimize this for the case when dest is an LDADDR */
3426         mono_class_init (klass);
3427         if (mini_is_gsharedvt_klass (klass)) {
3428                 size_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_VALUE_SIZE);
3429                 bzero_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_BZERO);
3430                 if (!bzero_method)
3431                         bzero_method = mono_class_get_method_from_name (mono_defaults.string_class, "bzero_aligned_1", 2);
3432                 g_assert (bzero_method);
3433                 iargs [0] = dest;
3434                 iargs [1] = size_ins;
3435                 mono_emit_calli (cfg, mono_method_signature (bzero_method), iargs, bzero_ins, NULL, NULL);
3436                 return;
3437         }
3438
3439         klass = mono_class_from_mono_type (mini_get_underlying_type (&klass->byval_arg));
3440
3441         n = mono_class_value_size (klass, &align);
3442
3443         if (n <= sizeof (gpointer) * 8) {
3444                 mini_emit_memset (cfg, dest->dreg, 0, n, 0, align);
3445         }
3446         else {
3447                 memset_method = get_memset_method ();
3448                 iargs [0] = dest;
3449                 EMIT_NEW_ICONST (cfg, iargs [1], 0);
3450                 EMIT_NEW_ICONST (cfg, iargs [2], n);
3451                 mono_emit_method_call (cfg, memset_method, iargs, NULL);
3452         }
3453 }
3454
3455 /*
3456  * emit_get_rgctx:
3457  *
3458  *   Emit IR to return either the this pointer for instance method,
3459  * or the mrgctx for static methods.
3460  */
3461 static MonoInst*
3462 emit_get_rgctx (MonoCompile *cfg, MonoMethod *method, int context_used)
3463 {
3464         MonoInst *this_ins = NULL;
3465
3466         g_assert (cfg->gshared);
3467
3468         if (!(method->flags & METHOD_ATTRIBUTE_STATIC) &&
3469                         !(context_used & MONO_GENERIC_CONTEXT_USED_METHOD) &&
3470                         !method->klass->valuetype)
3471                 EMIT_NEW_ARGLOAD (cfg, this_ins, 0);
3472
3473         if (context_used & MONO_GENERIC_CONTEXT_USED_METHOD) {
3474                 MonoInst *mrgctx_loc, *mrgctx_var;
3475
3476                 g_assert (!this_ins);
3477                 g_assert (method->is_inflated && mono_method_get_context (method)->method_inst);
3478
3479                 mrgctx_loc = mono_get_vtable_var (cfg);
3480                 EMIT_NEW_TEMPLOAD (cfg, mrgctx_var, mrgctx_loc->inst_c0);
3481
3482                 return mrgctx_var;
3483         } else if (method->flags & METHOD_ATTRIBUTE_STATIC || method->klass->valuetype) {
3484                 MonoInst *vtable_loc, *vtable_var;
3485
3486                 g_assert (!this_ins);
3487
3488                 vtable_loc = mono_get_vtable_var (cfg);
3489                 EMIT_NEW_TEMPLOAD (cfg, vtable_var, vtable_loc->inst_c0);
3490
3491                 if (method->is_inflated && mono_method_get_context (method)->method_inst) {
3492                         MonoInst *mrgctx_var = vtable_var;
3493                         int vtable_reg;
3494
3495                         vtable_reg = alloc_preg (cfg);
3496                         EMIT_NEW_LOAD_MEMBASE (cfg, vtable_var, OP_LOAD_MEMBASE, vtable_reg, mrgctx_var->dreg, MONO_STRUCT_OFFSET (MonoMethodRuntimeGenericContext, class_vtable));
3497                         vtable_var->type = STACK_PTR;
3498                 }
3499
3500                 return vtable_var;
3501         } else {
3502                 MonoInst *ins;
3503                 int vtable_reg;
3504         
3505                 vtable_reg = alloc_preg (cfg);
3506                 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, vtable_reg, this_ins->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3507                 return ins;
3508         }
3509 }
3510
3511 static MonoJumpInfoRgctxEntry *
3512 mono_patch_info_rgctx_entry_new (MonoMemPool *mp, MonoMethod *method, gboolean in_mrgctx, MonoJumpInfoType patch_type, gconstpointer patch_data, MonoRgctxInfoType info_type)
3513 {
3514         MonoJumpInfoRgctxEntry *res = (MonoJumpInfoRgctxEntry *)mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
3515         res->method = method;
3516         res->in_mrgctx = in_mrgctx;
3517         res->data = (MonoJumpInfo *)mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
3518         res->data->type = patch_type;
3519         res->data->data.target = patch_data;
3520         res->info_type = info_type;
3521
3522         return res;
3523 }
3524
3525 static inline MonoInst*
3526 emit_rgctx_fetch_inline (MonoCompile *cfg, MonoInst *rgctx, MonoJumpInfoRgctxEntry *entry)
3527 {
3528         MonoInst *args [16];
3529         MonoInst *call;
3530
3531         // FIXME: No fastpath since the slot is not a compile time constant
3532         args [0] = rgctx;
3533         EMIT_NEW_AOTCONST (cfg, args [1], MONO_PATCH_INFO_RGCTX_SLOT_INDEX, entry);
3534         if (entry->in_mrgctx)
3535                 call = mono_emit_jit_icall (cfg, mono_fill_method_rgctx, args);
3536         else
3537                 call = mono_emit_jit_icall (cfg, mono_fill_class_rgctx, args);
3538         return call;
3539 #if 0
3540         /*
3541          * FIXME: This can be called during decompose, which is a problem since it creates
3542          * new bblocks.
3543          * Also, the fastpath doesn't work since the slot number is dynamically allocated.
3544          */
3545         int i, slot, depth, index, rgctx_reg, val_reg, res_reg;
3546         gboolean mrgctx;
3547         MonoBasicBlock *is_null_bb, *end_bb;
3548         MonoInst *res, *ins, *call;
3549         MonoInst *args[16];
3550
3551         slot = mini_get_rgctx_entry_slot (entry);
3552
3553         mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
3554         index = MONO_RGCTX_SLOT_INDEX (slot);
3555         if (mrgctx)
3556                 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
3557         for (depth = 0; ; ++depth) {
3558                 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
3559
3560                 if (index < size - 1)
3561                         break;
3562                 index -= size - 1;
3563         }
3564
3565         NEW_BBLOCK (cfg, end_bb);
3566         NEW_BBLOCK (cfg, is_null_bb);
3567
3568         if (mrgctx) {
3569                 rgctx_reg = rgctx->dreg;
3570         } else {
3571                 rgctx_reg = alloc_preg (cfg);
3572
3573                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, rgctx_reg, rgctx->dreg, MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context));
3574                 // FIXME: Avoid this check by allocating the table when the vtable is created etc.
3575                 NEW_BBLOCK (cfg, is_null_bb);
3576
3577                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rgctx_reg, 0);
3578                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3579         }
3580
3581         for (i = 0; i < depth; ++i) {
3582                 int array_reg = alloc_preg (cfg);
3583
3584                 /* load ptr to next array */
3585                 if (mrgctx && i == 0)
3586                         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, rgctx_reg, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
3587                 else
3588                         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, rgctx_reg, 0);
3589                 rgctx_reg = array_reg;
3590                 /* is the ptr null? */
3591                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rgctx_reg, 0);
3592                 /* if yes, jump to actual trampoline */
3593                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3594         }
3595
3596         /* fetch slot */
3597         val_reg = alloc_preg (cfg);
3598         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, val_reg, rgctx_reg, (index + 1) * sizeof (gpointer));
3599         /* is the slot null? */
3600         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, val_reg, 0);
3601         /* if yes, jump to actual trampoline */
3602         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3603
3604         /* Fastpath */
3605         res_reg = alloc_preg (cfg);
3606         MONO_INST_NEW (cfg, ins, OP_MOVE);
3607         ins->dreg = res_reg;
3608         ins->sreg1 = val_reg;
3609         MONO_ADD_INS (cfg->cbb, ins);
3610         res = ins;
3611         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3612
3613         /* Slowpath */
3614         MONO_START_BB (cfg, is_null_bb);
3615         args [0] = rgctx;
3616         EMIT_NEW_ICONST (cfg, args [1], index);
3617         if (mrgctx)
3618                 call = mono_emit_jit_icall (cfg, mono_fill_method_rgctx, args);
3619         else
3620                 call = mono_emit_jit_icall (cfg, mono_fill_class_rgctx, args);
3621         MONO_INST_NEW (cfg, ins, OP_MOVE);
3622         ins->dreg = res_reg;
3623         ins->sreg1 = call->dreg;
3624         MONO_ADD_INS (cfg->cbb, ins);
3625         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
3626
3627         MONO_START_BB (cfg, end_bb);
3628
3629         return res;
3630 #endif
3631 }
3632
3633 /*
3634  * emit_rgctx_fetch:
3635  *
3636  *   Emit IR to load the value of the rgctx entry ENTRY from the rgctx
3637  * given by RGCTX.
3638  */
3639 static inline MonoInst*
3640 emit_rgctx_fetch (MonoCompile *cfg, MonoInst *rgctx, MonoJumpInfoRgctxEntry *entry)
3641 {
3642         if (cfg->llvm_only)
3643                 return emit_rgctx_fetch_inline (cfg, rgctx, entry);
3644         else
3645                 return mono_emit_abs_call (cfg, MONO_PATCH_INFO_RGCTX_FETCH, entry, helper_sig_rgctx_lazy_fetch_trampoline, &rgctx);
3646 }
3647
3648 static MonoInst*
3649 emit_get_rgctx_klass (MonoCompile *cfg, int context_used,
3650                                           MonoClass *klass, MonoRgctxInfoType rgctx_type)
3651 {
3652         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);
3653         MonoInst *rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3654
3655         return emit_rgctx_fetch (cfg, rgctx, entry);
3656 }
3657
3658 static MonoInst*
3659 emit_get_rgctx_sig (MonoCompile *cfg, int context_used,
3660                                         MonoMethodSignature *sig, MonoRgctxInfoType rgctx_type)
3661 {
3662         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);
3663         MonoInst *rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3664
3665         return emit_rgctx_fetch (cfg, rgctx, entry);
3666 }
3667
3668 static MonoInst*
3669 emit_get_rgctx_gsharedvt_call (MonoCompile *cfg, int context_used,
3670                                                            MonoMethodSignature *sig, MonoMethod *cmethod, MonoRgctxInfoType rgctx_type)
3671 {
3672         MonoJumpInfoGSharedVtCall *call_info;
3673         MonoJumpInfoRgctxEntry *entry;
3674         MonoInst *rgctx;
3675
3676         call_info = (MonoJumpInfoGSharedVtCall *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoGSharedVtCall));
3677         call_info->sig = sig;
3678         call_info->method = cmethod;
3679
3680         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);
3681         rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3682
3683         return emit_rgctx_fetch (cfg, rgctx, entry);
3684 }
3685
3686 /*
3687  * emit_get_rgctx_virt_method:
3688  *
3689  *   Return data for method VIRT_METHOD for a receiver of type KLASS.
3690  */
3691 static MonoInst*
3692 emit_get_rgctx_virt_method (MonoCompile *cfg, int context_used,
3693                                                         MonoClass *klass, MonoMethod *virt_method, MonoRgctxInfoType rgctx_type)
3694 {
3695         MonoJumpInfoVirtMethod *info;
3696         MonoJumpInfoRgctxEntry *entry;
3697         MonoInst *rgctx;
3698
3699         info = (MonoJumpInfoVirtMethod *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoVirtMethod));
3700         info->klass = klass;
3701         info->method = virt_method;
3702
3703         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);
3704         rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3705
3706         return emit_rgctx_fetch (cfg, rgctx, entry);
3707 }
3708
3709 static MonoInst*
3710 emit_get_rgctx_gsharedvt_method (MonoCompile *cfg, int context_used,
3711                                                                  MonoMethod *cmethod, MonoGSharedVtMethodInfo *info)
3712 {
3713         MonoJumpInfoRgctxEntry *entry;
3714         MonoInst *rgctx;
3715
3716         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);
3717         rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3718
3719         return emit_rgctx_fetch (cfg, rgctx, entry);
3720 }
3721
3722 /*
3723  * emit_get_rgctx_method:
3724  *
3725  *   Emit IR to load the property RGCTX_TYPE of CMETHOD. If context_used is 0, emit
3726  * normal constants, else emit a load from the rgctx.
3727  */
3728 static MonoInst*
3729 emit_get_rgctx_method (MonoCompile *cfg, int context_used,
3730                                            MonoMethod *cmethod, MonoRgctxInfoType rgctx_type)
3731 {
3732         if (!context_used) {
3733                 MonoInst *ins;
3734
3735                 switch (rgctx_type) {
3736                 case MONO_RGCTX_INFO_METHOD:
3737                         EMIT_NEW_METHODCONST (cfg, ins, cmethod);
3738                         return ins;
3739                 case MONO_RGCTX_INFO_METHOD_RGCTX:
3740                         EMIT_NEW_METHOD_RGCTX_CONST (cfg, ins, cmethod);
3741                         return ins;
3742                 default:
3743                         g_assert_not_reached ();
3744                 }
3745         } else {
3746                 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);
3747                 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3748
3749                 return emit_rgctx_fetch (cfg, rgctx, entry);
3750         }
3751 }
3752
3753 static MonoInst*
3754 emit_get_rgctx_field (MonoCompile *cfg, int context_used,
3755                                           MonoClassField *field, MonoRgctxInfoType rgctx_type)
3756 {
3757         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);
3758         MonoInst *rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
3759
3760         return emit_rgctx_fetch (cfg, rgctx, entry);
3761 }
3762
3763 static int
3764 get_gsharedvt_info_slot (MonoCompile *cfg, gpointer data, MonoRgctxInfoType rgctx_type)
3765 {
3766         MonoGSharedVtMethodInfo *info = cfg->gsharedvt_info;
3767         MonoRuntimeGenericContextInfoTemplate *template_;
3768         int i, idx;
3769
3770         g_assert (info);
3771
3772         for (i = 0; i < info->num_entries; ++i) {
3773                 MonoRuntimeGenericContextInfoTemplate *otemplate = &info->entries [i];
3774
3775                 if (otemplate->info_type == rgctx_type && otemplate->data == data && rgctx_type != MONO_RGCTX_INFO_LOCAL_OFFSET)
3776                         return i;
3777         }
3778
3779         if (info->num_entries == info->count_entries) {
3780                 MonoRuntimeGenericContextInfoTemplate *new_entries;
3781                 int new_count_entries = info->count_entries ? info->count_entries * 2 : 16;
3782
3783                 new_entries = (MonoRuntimeGenericContextInfoTemplate *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoRuntimeGenericContextInfoTemplate) * new_count_entries);
3784
3785                 memcpy (new_entries, info->entries, sizeof (MonoRuntimeGenericContextInfoTemplate) * info->count_entries);
3786                 info->entries = new_entries;
3787                 info->count_entries = new_count_entries;
3788         }
3789
3790         idx = info->num_entries;
3791         template_ = &info->entries [idx];
3792         template_->info_type = rgctx_type;
3793         template_->data = data;
3794
3795         info->num_entries ++;
3796
3797         return idx;
3798 }
3799
3800 /*
3801  * emit_get_gsharedvt_info:
3802  *
3803  *   This is similar to emit_get_rgctx_.., but loads the data from the gsharedvt info var instead of calling an rgctx fetch trampoline.
3804  */
3805 static MonoInst*
3806 emit_get_gsharedvt_info (MonoCompile *cfg, gpointer data, MonoRgctxInfoType rgctx_type)
3807 {
3808         MonoInst *ins;
3809         int idx, dreg;
3810
3811         idx = get_gsharedvt_info_slot (cfg, data, rgctx_type);
3812         /* Load info->entries [idx] */
3813         dreg = alloc_preg (cfg);
3814         EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, cfg->gsharedvt_info_var->dreg, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, entries) + (idx * sizeof (gpointer)));
3815
3816         return ins;
3817 }
3818
3819 static MonoInst*
3820 emit_get_gsharedvt_info_klass (MonoCompile *cfg, MonoClass *klass, MonoRgctxInfoType rgctx_type)
3821 {
3822         return emit_get_gsharedvt_info (cfg, &klass->byval_arg, rgctx_type);
3823 }
3824
3825 /*
3826  * On return the caller must check @klass for load errors.
3827  */
3828 static void
3829 emit_class_init (MonoCompile *cfg, MonoClass *klass)
3830 {
3831         MonoInst *vtable_arg;
3832         int context_used;
3833
3834         context_used = mini_class_check_context_used (cfg, klass);
3835
3836         if (context_used) {
3837                 vtable_arg = emit_get_rgctx_klass (cfg, context_used,
3838                                                                                    klass, MONO_RGCTX_INFO_VTABLE);
3839         } else {
3840                 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
3841
3842                 if (!vtable)
3843                         return;
3844                 EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
3845         }
3846
3847         if (!COMPILE_LLVM (cfg) && cfg->backend->have_op_generic_class_init) {
3848                 MonoInst *ins;
3849
3850                 /*
3851                  * Using an opcode instead of emitting IR here allows the hiding of the call inside the opcode,
3852                  * so this doesn't have to clobber any regs and it doesn't break basic blocks.
3853                  */
3854                 MONO_INST_NEW (cfg, ins, OP_GENERIC_CLASS_INIT);
3855                 ins->sreg1 = vtable_arg->dreg;
3856                 MONO_ADD_INS (cfg->cbb, ins);
3857         } else {
3858                 static int byte_offset = -1;
3859                 static guint8 bitmask;
3860                 int bits_reg, inited_reg;
3861                 MonoBasicBlock *inited_bb;
3862                 MonoInst *args [16];
3863
3864                 if (byte_offset < 0)
3865                         mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
3866
3867                 bits_reg = alloc_ireg (cfg);
3868                 inited_reg = alloc_ireg (cfg);
3869
3870                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, bits_reg, vtable_arg->dreg, byte_offset);
3871                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, inited_reg, bits_reg, bitmask);
3872
3873                 NEW_BBLOCK (cfg, inited_bb);
3874
3875                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, inited_reg, 0);
3876                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBNE_UN, inited_bb);
3877
3878                 args [0] = vtable_arg;
3879                 mono_emit_jit_icall (cfg, mono_generic_class_init, args);
3880
3881                 MONO_START_BB (cfg, inited_bb);
3882         }
3883 }
3884
3885 static void
3886 emit_seq_point (MonoCompile *cfg, MonoMethod *method, guint8* ip, gboolean intr_loc, gboolean nonempty_stack)
3887 {
3888         MonoInst *ins;
3889
3890         if (cfg->gen_seq_points && cfg->method == method) {
3891                 NEW_SEQ_POINT (cfg, ins, ip - cfg->header->code, intr_loc);
3892                 if (nonempty_stack)
3893                         ins->flags |= MONO_INST_NONEMPTY_STACK;
3894                 MONO_ADD_INS (cfg->cbb, ins);
3895         }
3896 }
3897
3898 static void
3899 save_cast_details (MonoCompile *cfg, MonoClass *klass, int obj_reg, gboolean null_check)
3900 {
3901         if (mini_get_debug_options ()->better_cast_details) {
3902                 int vtable_reg = alloc_preg (cfg);
3903                 int klass_reg = alloc_preg (cfg);
3904                 MonoBasicBlock *is_null_bb = NULL;
3905                 MonoInst *tls_get;
3906                 int to_klass_reg, context_used;
3907
3908                 if (null_check) {
3909                         NEW_BBLOCK (cfg, is_null_bb);
3910
3911                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
3912                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
3913                 }
3914
3915                 tls_get = mono_get_jit_tls_intrinsic (cfg);
3916                 if (!tls_get) {
3917                         fprintf (stderr, "error: --debug=casts not supported on this platform.\n.");
3918                         exit (1);
3919                 }
3920
3921                 MONO_ADD_INS (cfg->cbb, tls_get);
3922                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3923                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
3924
3925                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_from), klass_reg);
3926
3927                 context_used = mini_class_check_context_used (cfg, klass);
3928                 if (context_used) {
3929                         MonoInst *class_ins;
3930
3931                         class_ins = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
3932                         to_klass_reg = class_ins->dreg;
3933                 } else {
3934                         to_klass_reg = alloc_preg (cfg);
3935                         MONO_EMIT_NEW_CLASSCONST (cfg, to_klass_reg, klass);
3936                 }
3937                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_to), to_klass_reg);
3938
3939                 if (null_check)
3940                         MONO_START_BB (cfg, is_null_bb);
3941         }
3942 }
3943
3944 static void
3945 reset_cast_details (MonoCompile *cfg)
3946 {
3947         /* Reset the variables holding the cast details */
3948         if (mini_get_debug_options ()->better_cast_details) {
3949                 MonoInst *tls_get = mono_get_jit_tls_intrinsic (cfg);
3950
3951                 MONO_ADD_INS (cfg->cbb, tls_get);
3952                 /* It is enough to reset the from field */
3953                 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, tls_get->dreg, MONO_STRUCT_OFFSET (MonoJitTlsData, class_cast_from), 0);
3954         }
3955 }
3956
3957 /*
3958  * On return the caller must check @array_class for load errors
3959  */
3960 static void
3961 mini_emit_check_array_type (MonoCompile *cfg, MonoInst *obj, MonoClass *array_class)
3962 {
3963         int vtable_reg = alloc_preg (cfg);
3964         int context_used;
3965
3966         context_used = mini_class_check_context_used (cfg, array_class);
3967
3968         save_cast_details (cfg, array_class, obj->dreg, FALSE);
3969
3970         MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, obj->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
3971
3972         if (cfg->opt & MONO_OPT_SHARED) {
3973                 int class_reg = alloc_preg (cfg);
3974                 MonoInst *ins;
3975
3976                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, class_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
3977                 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_CLASS, array_class);
3978                 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, class_reg, ins->dreg);
3979         } else if (context_used) {
3980                 MonoInst *vtable_ins;
3981
3982                 vtable_ins = emit_get_rgctx_klass (cfg, context_used, array_class, MONO_RGCTX_INFO_VTABLE);
3983                 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, vtable_reg, vtable_ins->dreg);
3984         } else {
3985                 if (cfg->compile_aot) {
3986                         int vt_reg;
3987                         MonoVTable *vtable;
3988
3989                         if (!(vtable = mono_class_vtable (cfg->domain, array_class)))
3990                                 return;
3991                         vt_reg = alloc_preg (cfg);
3992                         MONO_EMIT_NEW_VTABLECONST (cfg, vt_reg, vtable);
3993                         MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, vtable_reg, vt_reg);
3994                 } else {
3995                         MonoVTable *vtable;
3996                         if (!(vtable = mono_class_vtable (cfg->domain, array_class)))
3997                                 return;
3998                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, vtable_reg, vtable);
3999                 }
4000         }
4001         
4002         MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "ArrayTypeMismatchException");
4003
4004         reset_cast_details (cfg);
4005 }
4006
4007 /**
4008  * Handles unbox of a Nullable<T>. If context_used is non zero, then shared 
4009  * generic code is generated.
4010  */
4011 static MonoInst*
4012 handle_unbox_nullable (MonoCompile* cfg, MonoInst* val, MonoClass* klass, int context_used)
4013 {
4014         MonoMethod* method = mono_class_get_method_from_name (klass, "Unbox", 1);
4015
4016         if (context_used) {
4017                 MonoInst *rgctx, *addr;
4018
4019                 /* FIXME: What if the class is shared?  We might not
4020                    have to get the address of the method from the
4021                    RGCTX. */
4022                 addr = emit_get_rgctx_method (cfg, context_used, method,
4023                                                                           MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
4024                 if (cfg->llvm_only && cfg->gsharedvt) {
4025                         return emit_llvmonly_calli (cfg, mono_method_signature (method), &val, addr);
4026                 } else {
4027                         rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
4028
4029                         return mono_emit_calli (cfg, mono_method_signature (method), &val, addr, NULL, rgctx);
4030                 }
4031         } else {
4032                 gboolean pass_vtable, pass_mrgctx;
4033                 MonoInst *rgctx_arg = NULL;
4034
4035                 check_method_sharing (cfg, method, &pass_vtable, &pass_mrgctx);
4036                 g_assert (!pass_mrgctx);
4037
4038                 if (pass_vtable) {
4039                         MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
4040
4041                         g_assert (vtable);
4042                         EMIT_NEW_VTABLECONST (cfg, rgctx_arg, vtable);
4043                 }
4044
4045                 return mono_emit_method_call_full (cfg, method, NULL, FALSE, &val, NULL, NULL, rgctx_arg);
4046         }
4047 }
4048
4049 static MonoInst*
4050 handle_unbox (MonoCompile *cfg, MonoClass *klass, MonoInst **sp, int context_used)
4051 {
4052         MonoInst *add;
4053         int obj_reg;
4054         int vtable_reg = alloc_dreg (cfg ,STACK_PTR);
4055         int klass_reg = alloc_dreg (cfg ,STACK_PTR);
4056         int eclass_reg = alloc_dreg (cfg ,STACK_PTR);
4057         int rank_reg = alloc_dreg (cfg ,STACK_I4);
4058
4059         obj_reg = sp [0]->dreg;
4060         MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4061         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, rank_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, rank));
4062
4063         /* FIXME: generics */
4064         g_assert (klass->rank == 0);
4065                         
4066         // Check rank == 0
4067         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rank_reg, 0);
4068         MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
4069
4070         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4071         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, eclass_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, element_class));
4072
4073         if (context_used) {
4074                 MonoInst *element_class;
4075
4076                 /* This assertion is from the unboxcast insn */
4077                 g_assert (klass->rank == 0);
4078
4079                 element_class = emit_get_rgctx_klass (cfg, context_used,
4080                                 klass, MONO_RGCTX_INFO_ELEMENT_KLASS);
4081
4082                 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, eclass_reg, element_class->dreg);
4083                 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
4084         } else {
4085                 save_cast_details (cfg, klass->element_class, obj_reg, FALSE);
4086                 mini_emit_class_check (cfg, eclass_reg, klass->element_class);
4087                 reset_cast_details (cfg);
4088         }
4089
4090         NEW_BIALU_IMM (cfg, add, OP_ADD_IMM, alloc_dreg (cfg, STACK_MP), obj_reg, sizeof (MonoObject));
4091         MONO_ADD_INS (cfg->cbb, add);
4092         add->type = STACK_MP;
4093         add->klass = klass;
4094
4095         return add;
4096 }
4097
4098 static MonoInst*
4099 handle_unbox_gsharedvt (MonoCompile *cfg, MonoClass *klass, MonoInst *obj)
4100 {
4101         MonoInst *addr, *klass_inst, *is_ref, *args[16];
4102         MonoBasicBlock *is_ref_bb, *is_nullable_bb, *end_bb;
4103         MonoInst *ins;
4104         int dreg, addr_reg;
4105
4106         klass_inst = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_KLASS);
4107
4108         /* obj */
4109         args [0] = obj;
4110
4111         /* klass */
4112         args [1] = klass_inst;
4113
4114         /* CASTCLASS */
4115         obj = mono_emit_jit_icall (cfg, mono_object_castclass_unbox, args);
4116
4117         NEW_BBLOCK (cfg, is_ref_bb);
4118         NEW_BBLOCK (cfg, is_nullable_bb);
4119         NEW_BBLOCK (cfg, end_bb);
4120         is_ref = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_CLASS_BOX_TYPE);
4121         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_REF);
4122         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
4123
4124         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_NULLABLE);
4125         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_nullable_bb);
4126
4127         /* This will contain either the address of the unboxed vtype, or an address of the temporary where the ref is stored */
4128         addr_reg = alloc_dreg (cfg, STACK_MP);
4129
4130         /* Non-ref case */
4131         /* UNBOX */
4132         NEW_BIALU_IMM (cfg, addr, OP_ADD_IMM, addr_reg, obj->dreg, sizeof (MonoObject));
4133         MONO_ADD_INS (cfg->cbb, addr);
4134
4135         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4136
4137         /* Ref case */
4138         MONO_START_BB (cfg, is_ref_bb);
4139
4140         /* Save the ref to a temporary */
4141         dreg = alloc_ireg (cfg);
4142         EMIT_NEW_VARLOADA_VREG (cfg, addr, dreg, &klass->byval_arg);
4143         addr->dreg = addr_reg;
4144         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, obj->dreg);
4145         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4146
4147         /* Nullable case */
4148         MONO_START_BB (cfg, is_nullable_bb);
4149
4150         {
4151                 MonoInst *addr = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_NULLABLE_CLASS_UNBOX);
4152                 MonoInst *unbox_call;
4153                 MonoMethodSignature *unbox_sig;
4154
4155                 unbox_sig = (MonoMethodSignature *)mono_mempool_alloc0 (cfg->mempool, MONO_SIZEOF_METHOD_SIGNATURE + (1 * sizeof (MonoType *)));
4156                 unbox_sig->ret = &klass->byval_arg;
4157                 unbox_sig->param_count = 1;
4158                 unbox_sig->params [0] = &mono_defaults.object_class->byval_arg;
4159
4160                 if (cfg->llvm_only)
4161                         unbox_call = emit_llvmonly_calli (cfg, unbox_sig, &obj, addr);
4162                 else
4163                         unbox_call = mono_emit_calli (cfg, unbox_sig, &obj, addr, NULL, NULL);
4164
4165                 EMIT_NEW_VARLOADA_VREG (cfg, addr, unbox_call->dreg, &klass->byval_arg);
4166                 addr->dreg = addr_reg;
4167         }
4168
4169         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4170
4171         /* End */
4172         MONO_START_BB (cfg, end_bb);
4173
4174         /* LDOBJ */
4175         EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr_reg, 0);
4176
4177         return ins;
4178 }
4179
4180 /*
4181  * Returns NULL and set the cfg exception on error.
4182  */
4183 static MonoInst*
4184 handle_alloc (MonoCompile *cfg, MonoClass *klass, gboolean for_box, int context_used)
4185 {
4186         MonoInst *iargs [2];
4187         void *alloc_ftn;
4188
4189         if (context_used) {
4190                 MonoInst *data;
4191                 MonoRgctxInfoType rgctx_info;
4192                 MonoInst *iargs [2];
4193                 gboolean known_instance_size = !mini_is_gsharedvt_klass (klass);
4194
4195                 MonoMethod *managed_alloc = mono_gc_get_managed_allocator (klass, for_box, known_instance_size);
4196
4197                 if (cfg->opt & MONO_OPT_SHARED)
4198                         rgctx_info = MONO_RGCTX_INFO_KLASS;
4199                 else
4200                         rgctx_info = MONO_RGCTX_INFO_VTABLE;
4201                 data = emit_get_rgctx_klass (cfg, context_used, klass, rgctx_info);
4202
4203                 if (cfg->opt & MONO_OPT_SHARED) {
4204                         EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
4205                         iargs [1] = data;
4206                         alloc_ftn = ves_icall_object_new;
4207                 } else {
4208                         iargs [0] = data;
4209                         alloc_ftn = ves_icall_object_new_specific;
4210                 }
4211
4212                 if (managed_alloc && !(cfg->opt & MONO_OPT_SHARED)) {
4213                         if (known_instance_size) {
4214                                 int size = mono_class_instance_size (klass);
4215                                 if (size < sizeof (MonoObject))
4216                                         g_error ("Invalid size %d for class %s", size, mono_type_get_full_name (klass));
4217
4218                                 EMIT_NEW_ICONST (cfg, iargs [1], mono_gc_get_aligned_size_for_allocator (size));
4219                         }
4220                         return mono_emit_method_call (cfg, managed_alloc, iargs, NULL);
4221                 }
4222
4223                 return mono_emit_jit_icall (cfg, alloc_ftn, iargs);
4224         }
4225
4226         if (cfg->opt & MONO_OPT_SHARED) {
4227                 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
4228                 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
4229
4230                 alloc_ftn = ves_icall_object_new;
4231         } else if (cfg->compile_aot && cfg->cbb->out_of_line && klass->type_token && klass->image == mono_defaults.corlib && !klass->generic_class) {
4232                 /* This happens often in argument checking code, eg. throw new FooException... */
4233                 /* Avoid relocations and save some space by calling a helper function specialized to mscorlib */
4234                 EMIT_NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (klass->type_token));
4235                 return mono_emit_jit_icall (cfg, mono_helper_newobj_mscorlib, iargs);
4236         } else {
4237                 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4238                 MonoMethod *managed_alloc = NULL;
4239                 gboolean pass_lw;
4240
4241                 if (!vtable) {
4242                         mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD);
4243                         cfg->exception_ptr = klass;
4244                         return NULL;
4245                 }
4246
4247                 managed_alloc = mono_gc_get_managed_allocator (klass, for_box, TRUE);
4248
4249                 if (managed_alloc) {
4250                         int size = mono_class_instance_size (klass);
4251                         if (size < sizeof (MonoObject))
4252                                 g_error ("Invalid size %d for class %s", size, mono_type_get_full_name (klass));
4253
4254                         EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
4255                         EMIT_NEW_ICONST (cfg, iargs [1], mono_gc_get_aligned_size_for_allocator (size));
4256                         return mono_emit_method_call (cfg, managed_alloc, iargs, NULL);
4257                 }
4258                 alloc_ftn = mono_class_get_allocation_ftn (vtable, for_box, &pass_lw);
4259                 if (pass_lw) {
4260                         guint32 lw = vtable->klass->instance_size;
4261                         lw = ((lw + (sizeof (gpointer) - 1)) & ~(sizeof (gpointer) - 1)) / sizeof (gpointer);
4262                         EMIT_NEW_ICONST (cfg, iargs [0], lw);
4263                         EMIT_NEW_VTABLECONST (cfg, iargs [1], vtable);
4264                 }
4265                 else {
4266                         EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
4267                 }
4268         }
4269
4270         return mono_emit_jit_icall (cfg, alloc_ftn, iargs);
4271 }
4272         
4273 /*
4274  * Returns NULL and set the cfg exception on error.
4275  */     
4276 static MonoInst*
4277 handle_box (MonoCompile *cfg, MonoInst *val, MonoClass *klass, int context_used)
4278 {
4279         MonoInst *alloc, *ins;
4280
4281         if (mono_class_is_nullable (klass)) {
4282                 MonoMethod* method = mono_class_get_method_from_name (klass, "Box", 1);
4283
4284                 if (context_used) {
4285                         if (cfg->llvm_only && cfg->gsharedvt) {
4286                                 MonoInst *addr = emit_get_rgctx_method (cfg, context_used, method,
4287                                                                                                                 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
4288                                 return emit_llvmonly_calli (cfg, mono_method_signature (method), &val, addr);
4289                         } else {
4290                                 /* FIXME: What if the class is shared?  We might not
4291                                    have to get the method address from the RGCTX. */
4292                                 MonoInst *addr = emit_get_rgctx_method (cfg, context_used, method,
4293                                                                                                                 MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
4294                                 MonoInst *rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
4295
4296                                 return mono_emit_calli (cfg, mono_method_signature (method), &val, addr, NULL, rgctx);
4297                         }
4298                 } else {
4299                         gboolean pass_vtable, pass_mrgctx;
4300                         MonoInst *rgctx_arg = NULL;
4301
4302                         check_method_sharing (cfg, method, &pass_vtable, &pass_mrgctx);
4303                         g_assert (!pass_mrgctx);
4304
4305                         if (pass_vtable) {
4306                                 MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
4307
4308                                 g_assert (vtable);
4309                                 EMIT_NEW_VTABLECONST (cfg, rgctx_arg, vtable);
4310                         }
4311
4312                         return mono_emit_method_call_full (cfg, method, NULL, FALSE, &val, NULL, NULL, rgctx_arg);
4313                 }
4314         }
4315
4316         if (mini_is_gsharedvt_klass (klass)) {
4317                 MonoBasicBlock *is_ref_bb, *is_nullable_bb, *end_bb;
4318                 MonoInst *res, *is_ref, *src_var, *addr;
4319                 int dreg;
4320
4321                 dreg = alloc_ireg (cfg);
4322
4323                 NEW_BBLOCK (cfg, is_ref_bb);
4324                 NEW_BBLOCK (cfg, is_nullable_bb);
4325                 NEW_BBLOCK (cfg, end_bb);
4326                 is_ref = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_CLASS_BOX_TYPE);
4327                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_REF);
4328                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
4329
4330                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, MONO_GSHAREDVT_BOX_TYPE_NULLABLE);
4331                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_nullable_bb);
4332
4333                 /* Non-ref case */
4334                 alloc = handle_alloc (cfg, klass, TRUE, context_used);
4335                 if (!alloc)
4336                         return NULL;
4337                 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, alloc->dreg, sizeof (MonoObject), val->dreg);
4338                 ins->opcode = OP_STOREV_MEMBASE;
4339
4340                 EMIT_NEW_UNALU (cfg, res, OP_MOVE, dreg, alloc->dreg);
4341                 res->type = STACK_OBJ;
4342                 res->klass = klass;
4343                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4344                 
4345                 /* Ref case */
4346                 MONO_START_BB (cfg, is_ref_bb);
4347
4348                 /* val is a vtype, so has to load the value manually */
4349                 src_var = get_vreg_to_inst (cfg, val->dreg);
4350                 if (!src_var)
4351                         src_var = mono_compile_create_var_for_vreg (cfg, &klass->byval_arg, OP_LOCAL, val->dreg);
4352                 EMIT_NEW_VARLOADA (cfg, addr, src_var, src_var->inst_vtype);
4353                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, addr->dreg, 0);
4354                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4355
4356                 /* Nullable case */
4357                 MONO_START_BB (cfg, is_nullable_bb);
4358
4359                 {
4360                         MonoInst *addr = emit_get_gsharedvt_info_klass (cfg, klass,
4361                                                                                                         MONO_RGCTX_INFO_NULLABLE_CLASS_BOX);
4362                         MonoInst *box_call;
4363                         MonoMethodSignature *box_sig;
4364
4365                         /*
4366                          * klass is Nullable<T>, need to call Nullable<T>.Box () using a gsharedvt signature, but we cannot
4367                          * construct that method at JIT time, so have to do things by hand.
4368                          */
4369                         box_sig = (MonoMethodSignature *)mono_mempool_alloc0 (cfg->mempool, MONO_SIZEOF_METHOD_SIGNATURE + (1 * sizeof (MonoType *)));
4370                         box_sig->ret = &mono_defaults.object_class->byval_arg;
4371                         box_sig->param_count = 1;
4372                         box_sig->params [0] = &klass->byval_arg;
4373
4374                         if (cfg->llvm_only)
4375                                 box_call = emit_llvmonly_calli (cfg, box_sig, &val, addr);
4376                         else
4377                                 box_call = mono_emit_calli (cfg, box_sig, &val, addr, NULL, NULL);
4378                         EMIT_NEW_UNALU (cfg, res, OP_MOVE, dreg, box_call->dreg);
4379                         res->type = STACK_OBJ;
4380                         res->klass = klass;
4381                 }
4382
4383                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4384
4385                 MONO_START_BB (cfg, end_bb);
4386
4387                 return res;
4388         } else {
4389                 alloc = handle_alloc (cfg, klass, TRUE, context_used);
4390                 if (!alloc)
4391                         return NULL;
4392
4393                 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, alloc->dreg, sizeof (MonoObject), val->dreg);
4394                 return alloc;
4395         }
4396 }
4397
4398 static gboolean
4399 mini_class_has_reference_variant_generic_argument (MonoCompile *cfg, MonoClass *klass, int context_used)
4400 {
4401         int i;
4402         MonoGenericContainer *container;
4403         MonoGenericInst *ginst;
4404
4405         if (klass->generic_class) {
4406                 container = klass->generic_class->container_class->generic_container;
4407                 ginst = klass->generic_class->context.class_inst;
4408         } else if (klass->generic_container && context_used) {
4409                 container = klass->generic_container;
4410                 ginst = container->context.class_inst;
4411         } else {
4412                 return FALSE;
4413         }
4414
4415         for (i = 0; i < container->type_argc; ++i) {
4416                 MonoType *type;
4417                 if (!(mono_generic_container_get_param_info (container, i)->flags & (MONO_GEN_PARAM_VARIANT|MONO_GEN_PARAM_COVARIANT)))
4418                         continue;
4419                 type = ginst->type_argv [i];
4420                 if (mini_type_is_reference (type))
4421                         return TRUE;
4422         }
4423         return FALSE;
4424 }
4425
4426 static GHashTable* direct_icall_type_hash;
4427
4428 static gboolean
4429 icall_is_direct_callable (MonoCompile *cfg, MonoMethod *cmethod)
4430 {
4431         /* LLVM on amd64 can't handle calls to non-32 bit addresses */
4432         if (!direct_icalls_enabled (cfg))
4433                 return FALSE;
4434
4435         /*
4436          * An icall is directly callable if it doesn't directly or indirectly call mono_raise_exception ().
4437          * Whitelist a few icalls for now.
4438          */
4439         if (!direct_icall_type_hash) {
4440                 GHashTable *h = g_hash_table_new (g_str_hash, g_str_equal);
4441
4442                 g_hash_table_insert (h, (char*)"Decimal", GUINT_TO_POINTER (1));
4443                 g_hash_table_insert (h, (char*)"Number", GUINT_TO_POINTER (1));
4444                 g_hash_table_insert (h, (char*)"Buffer", GUINT_TO_POINTER (1));
4445                 g_hash_table_insert (h, (char*)"Monitor", GUINT_TO_POINTER (1));
4446                 mono_memory_barrier ();
4447                 direct_icall_type_hash = h;
4448         }
4449
4450         if (cmethod->klass == mono_defaults.math_class)
4451                 return TRUE;
4452         /* No locking needed */
4453         if (cmethod->klass->image == mono_defaults.corlib && g_hash_table_lookup (direct_icall_type_hash, cmethod->klass->name))
4454                 return TRUE;
4455         return FALSE;
4456 }
4457
4458 #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)
4459
4460 static MonoInst*
4461 emit_castclass_with_cache (MonoCompile *cfg, MonoClass *klass, MonoInst **args)
4462 {
4463         MonoMethod *mono_castclass;
4464         MonoInst *res;
4465
4466         mono_castclass = mono_marshal_get_castclass_with_cache ();
4467
4468         save_cast_details (cfg, klass, args [0]->dreg, TRUE);
4469         res = mono_emit_method_call (cfg, mono_castclass, args, NULL);
4470         reset_cast_details (cfg);
4471
4472         return res;
4473 }
4474
4475 static int
4476 get_castclass_cache_idx (MonoCompile *cfg)
4477 {
4478         /* Each CASTCLASS_CACHE patch needs a unique index which identifies the call site */
4479         cfg->castclass_cache_index ++;
4480         return (cfg->method_index << 16) | cfg->castclass_cache_index;
4481 }
4482
4483 static MonoInst*
4484 emit_castclass_with_cache_nonshared (MonoCompile *cfg, MonoInst *obj, MonoClass *klass)
4485 {
4486         MonoInst *args [3];
4487         int idx;
4488
4489         /* obj */
4490         args [0] = obj;
4491
4492         /* klass */
4493         EMIT_NEW_CLASSCONST (cfg, args [1], klass);
4494
4495         /* inline cache*/
4496         idx = get_castclass_cache_idx (cfg);
4497         args [2] = emit_runtime_constant (cfg, MONO_PATCH_INFO_CASTCLASS_CACHE, GINT_TO_POINTER (idx));
4498
4499         /*The wrapper doesn't inline well so the bloat of inlining doesn't pay off.*/
4500         return emit_castclass_with_cache (cfg, klass, args);
4501 }
4502
4503 /*
4504  * Returns NULL and set the cfg exception on error.
4505  */
4506 static MonoInst*
4507 handle_castclass (MonoCompile *cfg, MonoClass *klass, MonoInst *src, guint8 *ip, int *inline_costs)
4508 {
4509         MonoBasicBlock *is_null_bb;
4510         int obj_reg = src->dreg;
4511         int vtable_reg = alloc_preg (cfg);
4512         int context_used;
4513         MonoInst *klass_inst = NULL, *res;
4514
4515         context_used = mini_class_check_context_used (cfg, klass);
4516
4517         if (!context_used && mini_class_has_reference_variant_generic_argument (cfg, klass, context_used)) {
4518                 res = emit_castclass_with_cache_nonshared (cfg, src, klass);
4519                 (*inline_costs) += 2;
4520                 return res;
4521         } else if (!context_used && (mono_class_is_marshalbyref (klass) || klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
4522                 MonoMethod *mono_castclass;
4523                 MonoInst *iargs [1];
4524                 int costs;
4525
4526                 mono_castclass = mono_marshal_get_castclass (klass); 
4527                 iargs [0] = src;
4528                                 
4529                 save_cast_details (cfg, klass, src->dreg, TRUE);
4530                 costs = inline_method (cfg, mono_castclass, mono_method_signature (mono_castclass), 
4531                                                            iargs, ip, cfg->real_offset, TRUE);
4532                 reset_cast_details (cfg);
4533                 CHECK_CFG_EXCEPTION;
4534                 g_assert (costs > 0);
4535                                 
4536                 cfg->real_offset += 5;
4537
4538                 (*inline_costs) += costs;
4539
4540                 return src;
4541         }
4542
4543         if (context_used) {
4544                 MonoInst *args [3];
4545
4546                 if(mini_class_has_reference_variant_generic_argument (cfg, klass, context_used) || is_complex_isinst (klass)) {
4547                         MonoInst *cache_ins;
4548
4549                         cache_ins = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_CAST_CACHE);
4550
4551                         /* obj */
4552                         args [0] = src;
4553
4554                         /* klass - it's the second element of the cache entry*/
4555                         EMIT_NEW_LOAD_MEMBASE (cfg, args [1], OP_LOAD_MEMBASE, alloc_preg (cfg), cache_ins->dreg, sizeof (gpointer));
4556
4557                         /* cache */
4558                         args [2] = cache_ins;
4559
4560                         return emit_castclass_with_cache (cfg, klass, args);
4561                 }
4562
4563                 klass_inst = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
4564         }
4565
4566         NEW_BBLOCK (cfg, is_null_bb);
4567
4568         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
4569         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, is_null_bb);
4570
4571         save_cast_details (cfg, klass, obj_reg, FALSE);
4572
4573         if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4574                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4575                 mini_emit_iface_cast (cfg, vtable_reg, klass, NULL, NULL);
4576         } else {
4577                 int klass_reg = alloc_preg (cfg);
4578
4579                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4580
4581                 if (!klass->rank && !cfg->compile_aot && !(cfg->opt & MONO_OPT_SHARED) && (klass->flags & TYPE_ATTRIBUTE_SEALED)) {
4582                         /* the remoting code is broken, access the class for now */
4583                         if (0) { /*FIXME what exactly is broken? This change refers to r39380 from 2005 and mention some remoting fixes were due.*/
4584                                 MonoVTable *vt = mono_class_vtable (cfg->domain, klass);
4585                                 if (!vt) {
4586                                         mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD);
4587                                         cfg->exception_ptr = klass;
4588                                         return NULL;
4589                                 }
4590                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, vtable_reg, vt);
4591                         } else {
4592                                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4593                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, klass_reg, klass);
4594                         }
4595                         MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
4596                 } else {
4597                         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4598                         mini_emit_castclass_inst (cfg, obj_reg, klass_reg, klass, klass_inst, is_null_bb);
4599                 }
4600         }
4601
4602         MONO_START_BB (cfg, is_null_bb);
4603
4604         reset_cast_details (cfg);
4605
4606         return src;
4607
4608 exception_exit:
4609         return NULL;
4610 }
4611
4612 /*
4613  * Returns NULL and set the cfg exception on error.
4614  */
4615 static MonoInst*
4616 handle_isinst (MonoCompile *cfg, MonoClass *klass, MonoInst *src, int context_used)
4617 {
4618         MonoInst *ins;
4619         MonoBasicBlock *is_null_bb, *false_bb, *end_bb;
4620         int obj_reg = src->dreg;
4621         int vtable_reg = alloc_preg (cfg);
4622         int res_reg = alloc_ireg_ref (cfg);
4623         MonoInst *klass_inst = NULL;
4624
4625         if (context_used) {
4626                 MonoInst *args [3];
4627
4628                 if(mini_class_has_reference_variant_generic_argument (cfg, klass, context_used) || is_complex_isinst (klass)) {
4629                         MonoMethod *mono_isinst = mono_marshal_get_isinst_with_cache ();
4630                         MonoInst *cache_ins;
4631
4632                         cache_ins = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_CAST_CACHE);
4633
4634                         /* obj */
4635                         args [0] = src;
4636
4637                         /* klass - it's the second element of the cache entry*/
4638                         EMIT_NEW_LOAD_MEMBASE (cfg, args [1], OP_LOAD_MEMBASE, alloc_preg (cfg), cache_ins->dreg, sizeof (gpointer));
4639
4640                         /* cache */
4641                         args [2] = cache_ins;
4642
4643                         return mono_emit_method_call (cfg, mono_isinst, args, NULL);
4644                 }
4645
4646                 klass_inst = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
4647         }
4648
4649         NEW_BBLOCK (cfg, is_null_bb);
4650         NEW_BBLOCK (cfg, false_bb);
4651         NEW_BBLOCK (cfg, end_bb);
4652
4653         /* Do the assignment at the beginning, so the other assignment can be if converted */
4654         EMIT_NEW_UNALU (cfg, ins, OP_MOVE, res_reg, obj_reg);
4655         ins->type = STACK_OBJ;
4656         ins->klass = klass;
4657
4658         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
4659         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_null_bb);
4660
4661         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4662
4663         if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4664                 g_assert (!context_used);
4665                 /* the is_null_bb target simply copies the input register to the output */
4666                 mini_emit_iface_cast (cfg, vtable_reg, klass, false_bb, is_null_bb);
4667         } else {
4668                 int klass_reg = alloc_preg (cfg);
4669
4670                 if (klass->rank) {
4671                         int rank_reg = alloc_preg (cfg);
4672                         int eclass_reg = alloc_preg (cfg);
4673
4674                         g_assert (!context_used);
4675                         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, rank_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, rank));
4676                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, rank_reg, klass->rank);
4677                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, false_bb);
4678                         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4679                         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, eclass_reg, klass_reg, MONO_STRUCT_OFFSET (MonoClass, cast_class));
4680                         if (klass->cast_class == mono_defaults.object_class) {
4681                                 int parent_reg = alloc_preg (cfg);
4682                                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, parent_reg, eclass_reg, MONO_STRUCT_OFFSET (MonoClass, parent));
4683                                 mini_emit_class_check_branch (cfg, parent_reg, mono_defaults.enum_class->parent, OP_PBNE_UN, is_null_bb);
4684                                 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class, OP_PBEQ, is_null_bb);
4685                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false_bb);
4686                         } else if (klass->cast_class == mono_defaults.enum_class->parent) {
4687                                 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class->parent, OP_PBEQ, is_null_bb);
4688                                 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class, OP_PBEQ, is_null_bb);                          
4689                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false_bb);
4690                         } else if (klass->cast_class == mono_defaults.enum_class) {
4691                                 mini_emit_class_check_branch (cfg, eclass_reg, mono_defaults.enum_class, OP_PBEQ, is_null_bb);
4692                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false_bb);
4693                         } else if (klass->cast_class->flags & TYPE_ATTRIBUTE_INTERFACE) {
4694                                 mini_emit_iface_class_cast (cfg, eclass_reg, klass->cast_class, false_bb, is_null_bb);
4695                         } else {
4696                                 if ((klass->rank == 1) && (klass->byval_arg.type == MONO_TYPE_SZARRAY)) {
4697                                         /* Check that the object is a vector too */
4698                                         int bounds_reg = alloc_preg (cfg);
4699                                         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg, obj_reg, MONO_STRUCT_OFFSET (MonoArray, bounds));
4700                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, bounds_reg, 0);
4701                                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, false_bb);
4702                                 }
4703
4704                                 /* the is_null_bb target simply copies the input register to the output */
4705                                 mini_emit_isninst_cast (cfg, eclass_reg, klass->cast_class, false_bb, is_null_bb);
4706                         }
4707                 } else if (mono_class_is_nullable (klass)) {
4708                         g_assert (!context_used);
4709                         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4710                         /* the is_null_bb target simply copies the input register to the output */
4711                         mini_emit_isninst_cast (cfg, klass_reg, klass->cast_class, false_bb, is_null_bb);
4712                 } else {
4713                         if (!cfg->compile_aot && !(cfg->opt & MONO_OPT_SHARED) && (klass->flags & TYPE_ATTRIBUTE_SEALED)) {
4714                                 g_assert (!context_used);
4715                                 /* the remoting code is broken, access the class for now */
4716                                 if (0) {/*FIXME what exactly is broken? This change refers to r39380 from 2005 and mention some remoting fixes were due.*/
4717                                         MonoVTable *vt = mono_class_vtable (cfg->domain, klass);
4718                                         if (!vt) {
4719                                                 mono_cfg_set_exception (cfg, MONO_EXCEPTION_TYPE_LOAD);
4720                                                 cfg->exception_ptr = klass;
4721                                                 return NULL;
4722                                         }
4723                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, vtable_reg, vt);
4724                                 } else {
4725                                         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4726                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, klass_reg, klass);
4727                                 }
4728                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, false_bb);
4729                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, is_null_bb);
4730                         } else {
4731                                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4732                                 /* the is_null_bb target simply copies the input register to the output */
4733                                 mini_emit_isninst_cast_inst (cfg, klass_reg, klass, klass_inst, false_bb, is_null_bb);
4734                         }
4735                 }
4736         }
4737
4738         MONO_START_BB (cfg, false_bb);
4739
4740         MONO_EMIT_NEW_PCONST (cfg, res_reg, 0);
4741         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4742
4743         MONO_START_BB (cfg, is_null_bb);
4744
4745         MONO_START_BB (cfg, end_bb);
4746
4747         return ins;
4748 }
4749
4750 static MonoInst*
4751 handle_cisinst (MonoCompile *cfg, MonoClass *klass, MonoInst *src)
4752 {
4753         /* This opcode takes as input an object reference and a class, and returns:
4754         0) if the object is an instance of the class,
4755         1) if the object is not instance of the class,
4756         2) if the object is a proxy whose type cannot be determined */
4757
4758         MonoInst *ins;
4759 #ifndef DISABLE_REMOTING
4760         MonoBasicBlock *true_bb, *false_bb, *false2_bb, *end_bb, *no_proxy_bb, *interface_fail_bb;
4761 #else
4762         MonoBasicBlock *true_bb, *false_bb, *end_bb;
4763 #endif
4764         int obj_reg = src->dreg;
4765         int dreg = alloc_ireg (cfg);
4766         int tmp_reg;
4767 #ifndef DISABLE_REMOTING
4768         int klass_reg = alloc_preg (cfg);
4769 #endif
4770
4771         NEW_BBLOCK (cfg, true_bb);
4772         NEW_BBLOCK (cfg, false_bb);
4773         NEW_BBLOCK (cfg, end_bb);
4774 #ifndef DISABLE_REMOTING
4775         NEW_BBLOCK (cfg, false2_bb);
4776         NEW_BBLOCK (cfg, no_proxy_bb);
4777 #endif
4778
4779         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
4780         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, false_bb);
4781
4782         if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4783 #ifndef DISABLE_REMOTING
4784                 NEW_BBLOCK (cfg, interface_fail_bb);
4785 #endif
4786
4787                 tmp_reg = alloc_preg (cfg);
4788                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4789 #ifndef DISABLE_REMOTING
4790                 mini_emit_iface_cast (cfg, tmp_reg, klass, interface_fail_bb, true_bb);
4791                 MONO_START_BB (cfg, interface_fail_bb);
4792                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4793                 
4794                 mini_emit_class_check_branch (cfg, klass_reg, mono_defaults.transparent_proxy_class, OP_PBNE_UN, false_bb);
4795
4796                 tmp_reg = alloc_preg (cfg);
4797                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
4798                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
4799                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, false2_bb);                
4800 #else
4801                 mini_emit_iface_cast (cfg, tmp_reg, klass, false_bb, true_bb);
4802 #endif
4803         } else {
4804 #ifndef DISABLE_REMOTING
4805                 tmp_reg = alloc_preg (cfg);
4806                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4807                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4808
4809                 mini_emit_class_check_branch (cfg, klass_reg, mono_defaults.transparent_proxy_class, OP_PBNE_UN, no_proxy_bb);          
4810                 tmp_reg = alloc_preg (cfg);
4811                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, remote_class));
4812                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoRemoteClass, proxy_class));
4813
4814                 tmp_reg = alloc_preg (cfg);             
4815                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
4816                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
4817                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, no_proxy_bb);
4818                 
4819                 mini_emit_isninst_cast (cfg, klass_reg, klass, false2_bb, true_bb);
4820                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, false2_bb);
4821
4822                 MONO_START_BB (cfg, no_proxy_bb);
4823
4824                 mini_emit_isninst_cast (cfg, klass_reg, klass, false_bb, true_bb);
4825 #else
4826                 g_error ("transparent proxy support is disabled while trying to JIT code that uses it");
4827 #endif
4828         }
4829
4830         MONO_START_BB (cfg, false_bb);
4831
4832         MONO_EMIT_NEW_ICONST (cfg, dreg, 1);
4833         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4834
4835 #ifndef DISABLE_REMOTING
4836         MONO_START_BB (cfg, false2_bb);
4837
4838         MONO_EMIT_NEW_ICONST (cfg, dreg, 2);
4839         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4840 #endif
4841
4842         MONO_START_BB (cfg, true_bb);
4843
4844         MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
4845
4846         MONO_START_BB (cfg, end_bb);
4847
4848         /* FIXME: */
4849         MONO_INST_NEW (cfg, ins, OP_ICONST);
4850         ins->dreg = dreg;
4851         ins->type = STACK_I4;
4852
4853         return ins;
4854 }
4855
4856 static MonoInst*
4857 handle_ccastclass (MonoCompile *cfg, MonoClass *klass, MonoInst *src)
4858 {
4859         /* This opcode takes as input an object reference and a class, and returns:
4860         0) if the object is an instance of the class,
4861         1) if the object is a proxy whose type cannot be determined
4862         an InvalidCastException exception is thrown otherwhise*/
4863         
4864         MonoInst *ins;
4865 #ifndef DISABLE_REMOTING
4866         MonoBasicBlock *end_bb, *ok_result_bb, *no_proxy_bb, *interface_fail_bb, *fail_1_bb;
4867 #else
4868         MonoBasicBlock *ok_result_bb;
4869 #endif
4870         int obj_reg = src->dreg;
4871         int dreg = alloc_ireg (cfg);
4872         int tmp_reg = alloc_preg (cfg);
4873
4874 #ifndef DISABLE_REMOTING
4875         int klass_reg = alloc_preg (cfg);
4876         NEW_BBLOCK (cfg, end_bb);
4877 #endif
4878
4879         NEW_BBLOCK (cfg, ok_result_bb);
4880
4881         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
4882         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, ok_result_bb);
4883
4884         save_cast_details (cfg, klass, obj_reg, FALSE);
4885
4886         if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4887 #ifndef DISABLE_REMOTING
4888                 NEW_BBLOCK (cfg, interface_fail_bb);
4889         
4890                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4891                 mini_emit_iface_cast (cfg, tmp_reg, klass, interface_fail_bb, ok_result_bb);
4892                 MONO_START_BB (cfg, interface_fail_bb);
4893                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4894
4895                 mini_emit_class_check (cfg, klass_reg, mono_defaults.transparent_proxy_class);
4896
4897                 tmp_reg = alloc_preg (cfg);             
4898                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
4899                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
4900                 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "InvalidCastException");
4901                 
4902                 MONO_EMIT_NEW_ICONST (cfg, dreg, 1);
4903                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4904 #else
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, NULL, NULL);
4907                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, ok_result_bb);
4908 #endif
4909         } else {
4910 #ifndef DISABLE_REMOTING
4911                 NEW_BBLOCK (cfg, no_proxy_bb);
4912
4913                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
4914                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoVTable, klass));
4915                 mini_emit_class_check_branch (cfg, klass_reg, mono_defaults.transparent_proxy_class, OP_PBNE_UN, no_proxy_bb);          
4916
4917                 tmp_reg = alloc_preg (cfg);
4918                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, remote_class));
4919                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, MONO_STRUCT_OFFSET (MonoRemoteClass, proxy_class));
4920
4921                 tmp_reg = alloc_preg (cfg);
4922                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, MONO_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
4923                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
4924                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, no_proxy_bb);
4925
4926                 NEW_BBLOCK (cfg, fail_1_bb);
4927                 
4928                 mini_emit_isninst_cast (cfg, klass_reg, klass, fail_1_bb, ok_result_bb);
4929
4930                 MONO_START_BB (cfg, fail_1_bb);
4931
4932                 MONO_EMIT_NEW_ICONST (cfg, dreg, 1);
4933                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
4934
4935                 MONO_START_BB (cfg, no_proxy_bb);
4936
4937                 mini_emit_castclass (cfg, obj_reg, klass_reg, klass, ok_result_bb);
4938 #else
4939                 g_error ("Transparent proxy support is disabled while trying to JIT code that uses it");
4940 #endif
4941         }
4942
4943         MONO_START_BB (cfg, ok_result_bb);
4944
4945         MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
4946
4947 #ifndef DISABLE_REMOTING
4948         MONO_START_BB (cfg, end_bb);
4949 #endif
4950
4951         /* FIXME: */
4952         MONO_INST_NEW (cfg, ins, OP_ICONST);
4953         ins->dreg = dreg;
4954         ins->type = STACK_I4;
4955
4956         return ins;
4957 }
4958
4959 static G_GNUC_UNUSED MonoInst*
4960 handle_enum_has_flag (MonoCompile *cfg, MonoClass *klass, MonoInst *enum_this, MonoInst *enum_flag)
4961 {
4962         MonoType *enum_type = mono_type_get_underlying_type (&klass->byval_arg);
4963         guint32 load_opc = mono_type_to_load_membase (cfg, enum_type);
4964         gboolean is_i4;
4965
4966         switch (enum_type->type) {
4967         case MONO_TYPE_I8:
4968         case MONO_TYPE_U8:
4969 #if SIZEOF_REGISTER == 8
4970         case MONO_TYPE_I:
4971         case MONO_TYPE_U:
4972 #endif
4973                 is_i4 = FALSE;
4974                 break;
4975         default:
4976                 is_i4 = TRUE;
4977                 break;
4978         }
4979
4980         {
4981                 MonoInst *load, *and_, *cmp, *ceq;
4982                 int enum_reg = is_i4 ? alloc_ireg (cfg) : alloc_lreg (cfg);
4983                 int and_reg = is_i4 ? alloc_ireg (cfg) : alloc_lreg (cfg);
4984                 int dest_reg = alloc_ireg (cfg);
4985
4986                 EMIT_NEW_LOAD_MEMBASE (cfg, load, load_opc, enum_reg, enum_this->dreg, 0);
4987                 EMIT_NEW_BIALU (cfg, and_, is_i4 ? OP_IAND : OP_LAND, and_reg, enum_reg, enum_flag->dreg);
4988                 EMIT_NEW_BIALU (cfg, cmp, is_i4 ? OP_ICOMPARE : OP_LCOMPARE, -1, and_reg, enum_flag->dreg);
4989                 EMIT_NEW_UNALU (cfg, ceq, is_i4 ? OP_ICEQ : OP_LCEQ, dest_reg, -1);
4990
4991                 ceq->type = STACK_I4;
4992
4993                 if (!is_i4) {
4994                         load = mono_decompose_opcode (cfg, load);
4995                         and_ = mono_decompose_opcode (cfg, and_);
4996                         cmp = mono_decompose_opcode (cfg, cmp);
4997                         ceq = mono_decompose_opcode (cfg, ceq);
4998                 }
4999
5000                 return ceq;
5001         }
5002 }
5003
5004 /*
5005  * Returns NULL and set the cfg exception on error.
5006  */
5007 static G_GNUC_UNUSED MonoInst*
5008 handle_delegate_ctor (MonoCompile *cfg, MonoClass *klass, MonoInst *target, MonoMethod *method, int context_used, gboolean virtual_)
5009 {
5010         MonoInst *ptr;
5011         int dreg;
5012         gpointer trampoline;
5013         MonoInst *obj, *method_ins, *tramp_ins;
5014         MonoDomain *domain;
5015         guint8 **code_slot;
5016
5017         if (virtual_ && !cfg->llvm_only) {
5018                 MonoMethod *invoke = mono_get_delegate_invoke (klass);
5019                 g_assert (invoke);
5020
5021                 if (!mono_get_delegate_virtual_invoke_impl (mono_method_signature (invoke), context_used ? NULL : method))
5022                         return NULL;
5023         }
5024
5025         obj = handle_alloc (cfg, klass, FALSE, mono_class_check_context_used (klass));
5026         if (!obj)
5027                 return NULL;
5028
5029         /* Inline the contents of mono_delegate_ctor */
5030
5031         /* Set target field */
5032         /* Optimize away setting of NULL target */
5033         if (!(target->opcode == OP_PCONST && target->inst_p0 == 0)) {
5034                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, target), target->dreg);
5035                 if (cfg->gen_write_barriers) {
5036                         dreg = alloc_preg (cfg);
5037                         EMIT_NEW_BIALU_IMM (cfg, ptr, OP_PADD_IMM, dreg, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, target));
5038                         emit_write_barrier (cfg, ptr, target);
5039                 }
5040         }
5041
5042         /* Set method field */
5043         method_ins = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_METHOD);
5044         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method), method_ins->dreg);
5045
5046         /* 
5047          * To avoid looking up the compiled code belonging to the target method
5048          * in mono_delegate_trampoline (), we allocate a per-domain memory slot to
5049          * store it, and we fill it after the method has been compiled.
5050          */
5051         if (!method->dynamic && !(cfg->opt & MONO_OPT_SHARED)) {
5052                 MonoInst *code_slot_ins;
5053
5054                 if (context_used) {
5055                         code_slot_ins = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_METHOD_DELEGATE_CODE);
5056                 } else {
5057                         domain = mono_domain_get ();
5058                         mono_domain_lock (domain);
5059                         if (!domain_jit_info (domain)->method_code_hash)
5060                                 domain_jit_info (domain)->method_code_hash = g_hash_table_new (NULL, NULL);
5061                         code_slot = (guint8 **)g_hash_table_lookup (domain_jit_info (domain)->method_code_hash, method);
5062                         if (!code_slot) {
5063                                 code_slot = (guint8 **)mono_domain_alloc0 (domain, sizeof (gpointer));
5064                                 g_hash_table_insert (domain_jit_info (domain)->method_code_hash, method, code_slot);
5065                         }
5066                         mono_domain_unlock (domain);
5067
5068                         code_slot_ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_METHOD_CODE_SLOT, method);
5069                 }
5070                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_code), code_slot_ins->dreg);                
5071         }
5072
5073         if (cfg->llvm_only) {
5074                 MonoInst *args [16];
5075
5076                 if (virtual_) {
5077                         args [0] = obj;
5078                         args [1] = target;
5079                         args [2] = emit_get_rgctx_method (cfg, context_used, method, MONO_RGCTX_INFO_METHOD);
5080                         mono_emit_jit_icall (cfg, mono_llvmonly_init_delegate_virtual, args);
5081                 } else {
5082                         args [0] = obj;
5083                         mono_emit_jit_icall (cfg, mono_llvmonly_init_delegate, args);
5084                 }
5085
5086                 return obj;
5087         }
5088
5089         if (cfg->compile_aot) {
5090                 MonoDelegateClassMethodPair *del_tramp;
5091
5092                 del_tramp = (MonoDelegateClassMethodPair *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoDelegateClassMethodPair));
5093                 del_tramp->klass = klass;
5094                 del_tramp->method = context_used ? NULL : method;
5095                 del_tramp->is_virtual = virtual_;
5096                 EMIT_NEW_AOTCONST (cfg, tramp_ins, MONO_PATCH_INFO_DELEGATE_TRAMPOLINE, del_tramp);
5097         } else {
5098                 if (virtual_)
5099                         trampoline = mono_create_delegate_virtual_trampoline (cfg->domain, klass, context_used ? NULL : method);
5100                 else
5101                         trampoline = mono_create_delegate_trampoline_info (cfg->domain, klass, context_used ? NULL : method);
5102                 EMIT_NEW_PCONST (cfg, tramp_ins, trampoline);
5103         }
5104
5105         /* Set invoke_impl field */
5106         if (virtual_) {
5107                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl), tramp_ins->dreg);
5108         } else {
5109                 dreg = alloc_preg (cfg);
5110                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, tramp_ins->dreg, MONO_STRUCT_OFFSET (MonoDelegateTrampInfo, invoke_impl));
5111                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, invoke_impl), dreg);
5112
5113                 dreg = alloc_preg (cfg);
5114                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, tramp_ins->dreg, MONO_STRUCT_OFFSET (MonoDelegateTrampInfo, method_ptr));
5115                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr), dreg);
5116         }
5117
5118         dreg = alloc_preg (cfg);
5119         MONO_EMIT_NEW_ICONST (cfg, dreg, virtual_ ? 1 : 0);
5120         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, obj->dreg, MONO_STRUCT_OFFSET (MonoDelegate, method_is_virtual), dreg);
5121
5122         /* All the checks which are in mono_delegate_ctor () are done by the delegate trampoline */
5123
5124         return obj;
5125 }
5126
5127 static MonoInst*
5128 handle_array_new (MonoCompile *cfg, int rank, MonoInst **sp, unsigned char *ip)
5129 {
5130         MonoJitICallInfo *info;
5131
5132         /* Need to register the icall so it gets an icall wrapper */
5133         info = mono_get_array_new_va_icall (rank);
5134
5135         cfg->flags |= MONO_CFG_HAS_VARARGS;
5136
5137         /* mono_array_new_va () needs a vararg calling convention */
5138         cfg->exception_message = g_strdup ("array-new");
5139         cfg->disable_llvm = TRUE;
5140
5141         /* FIXME: This uses info->sig, but it should use the signature of the wrapper */
5142         return mono_emit_native_call (cfg, mono_icall_get_wrapper (info), info->sig, sp);
5143 }
5144
5145 /*
5146  * handle_constrained_gsharedvt_call:
5147  *
5148  *   Handle constrained calls where the receiver is a gsharedvt type.
5149  * Return the instruction representing the call. Set the cfg exception on failure.
5150  */
5151 static MonoInst*
5152 handle_constrained_gsharedvt_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp, MonoClass *constrained_class,
5153                                                                    gboolean *ref_emit_widen)
5154 {
5155         MonoInst *ins = NULL;
5156         gboolean emit_widen = *ref_emit_widen;
5157
5158         /*
5159          * Constrained calls need to behave differently at runtime dependending on whenever the receiver is instantiated as ref type or as a vtype.
5160          * 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
5161          * pack the arguments into an array, and do the rest of the work in in an icall.
5162          */
5163         if (((cmethod->klass == mono_defaults.object_class) || (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE) || (!cmethod->klass->valuetype && cmethod->klass->image != mono_defaults.corlib)) &&
5164                 (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)) &&
5165                 (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]))))) {
5166                 MonoInst *args [16];
5167
5168                 /*
5169                  * This case handles calls to
5170                  * - object:ToString()/Equals()/GetHashCode(),
5171                  * - System.IComparable<T>:CompareTo()
5172                  * - System.IEquatable<T>:Equals ()
5173                  * plus some simple interface calls enough to support AsyncTaskMethodBuilder.
5174                  */
5175
5176                 args [0] = sp [0];
5177                 if (mono_method_check_context_used (cmethod))
5178                         args [1] = emit_get_rgctx_method (cfg, mono_method_check_context_used (cmethod), cmethod, MONO_RGCTX_INFO_METHOD);
5179                 else
5180                         EMIT_NEW_METHODCONST (cfg, args [1], cmethod);
5181                 args [2] = emit_get_rgctx_klass (cfg, mono_class_check_context_used (constrained_class), constrained_class, MONO_RGCTX_INFO_KLASS);
5182
5183                 /* !fsig->hasthis is for the wrapper for the Object.GetType () icall */
5184                 if (fsig->hasthis && fsig->param_count) {
5185                         /* Pass the arguments using a localloc-ed array using the format expected by runtime_invoke () */
5186                         MONO_INST_NEW (cfg, ins, OP_LOCALLOC_IMM);
5187                         ins->dreg = alloc_preg (cfg);
5188                         ins->inst_imm = fsig->param_count * sizeof (mgreg_t);
5189                         MONO_ADD_INS (cfg->cbb, ins);
5190                         args [4] = ins;
5191
5192                         if (mini_is_gsharedvt_type (fsig->params [0])) {
5193                                 int addr_reg, deref_arg_reg;
5194
5195                                 ins = emit_get_gsharedvt_info_klass (cfg, mono_class_from_mono_type (fsig->params [0]), MONO_RGCTX_INFO_CLASS_BOX_TYPE);
5196                                 deref_arg_reg = alloc_preg (cfg);
5197                                 /* deref_arg = BOX_TYPE != MONO_GSHAREDVT_BOX_TYPE_VTYPE */
5198                                 EMIT_NEW_BIALU_IMM (cfg, args [3], OP_ISUB_IMM, deref_arg_reg, ins->dreg, 1);
5199
5200                                 EMIT_NEW_VARLOADA_VREG (cfg, ins, sp [1]->dreg, fsig->params [0]);
5201                                 addr_reg = ins->dreg;
5202                                 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, args [4]->dreg, 0, addr_reg);
5203                         } else {
5204                                 EMIT_NEW_ICONST (cfg, args [3], 0);
5205                                 EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STORE_MEMBASE_REG, args [4]->dreg, 0, sp [1]->dreg);
5206                         }
5207                 } else {
5208                         EMIT_NEW_ICONST (cfg, args [3], 0);
5209                         EMIT_NEW_ICONST (cfg, args [4], 0);
5210                 }
5211                 ins = mono_emit_jit_icall (cfg, mono_gsharedvt_constrained_call, args);
5212                 emit_widen = FALSE;
5213
5214                 if (mini_is_gsharedvt_type (fsig->ret)) {
5215                         ins = handle_unbox_gsharedvt (cfg, mono_class_from_mono_type (fsig->ret), ins);
5216                 } else if (MONO_TYPE_IS_PRIMITIVE (fsig->ret) || MONO_TYPE_ISSTRUCT (fsig->ret)) {
5217                         MonoInst *add;
5218
5219                         /* Unbox */
5220                         NEW_BIALU_IMM (cfg, add, OP_ADD_IMM, alloc_dreg (cfg, STACK_MP), ins->dreg, sizeof (MonoObject));
5221                         MONO_ADD_INS (cfg->cbb, add);
5222                         /* Load value */
5223                         NEW_LOAD_MEMBASE_TYPE (cfg, ins, fsig->ret, add->dreg, 0);
5224                         MONO_ADD_INS (cfg->cbb, ins);
5225                         /* ins represents the call result */
5226                 }
5227         } else {
5228                 GSHAREDVT_FAILURE (CEE_CALLVIRT);
5229         }
5230
5231         *ref_emit_widen = emit_widen;
5232
5233         return ins;
5234
5235  exception_exit:
5236         return NULL;
5237 }
5238
5239 static void
5240 mono_emit_load_got_addr (MonoCompile *cfg)
5241 {
5242         MonoInst *getaddr, *dummy_use;
5243
5244         if (!cfg->got_var || cfg->got_var_allocated)
5245                 return;
5246
5247         MONO_INST_NEW (cfg, getaddr, OP_LOAD_GOTADDR);
5248         getaddr->cil_code = cfg->header->code;
5249         getaddr->dreg = cfg->got_var->dreg;
5250
5251         /* Add it to the start of the first bblock */
5252         if (cfg->bb_entry->code) {
5253                 getaddr->next = cfg->bb_entry->code;
5254                 cfg->bb_entry->code = getaddr;
5255         }
5256         else
5257                 MONO_ADD_INS (cfg->bb_entry, getaddr);
5258
5259         cfg->got_var_allocated = TRUE;
5260
5261         /* 
5262          * Add a dummy use to keep the got_var alive, since real uses might
5263          * only be generated by the back ends.
5264          * Add it to end_bblock, so the variable's lifetime covers the whole
5265          * method.
5266          * It would be better to make the usage of the got var explicit in all
5267          * cases when the backend needs it (i.e. calls, throw etc.), so this
5268          * wouldn't be needed.
5269          */
5270         NEW_DUMMY_USE (cfg, dummy_use, cfg->got_var);
5271         MONO_ADD_INS (cfg->bb_exit, dummy_use);
5272 }
5273
5274 static int inline_limit;
5275 static gboolean inline_limit_inited;
5276
5277 static gboolean
5278 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
5279 {
5280         MonoMethodHeaderSummary header;
5281         MonoVTable *vtable;
5282 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
5283         MonoMethodSignature *sig = mono_method_signature (method);
5284         int i;
5285 #endif
5286
5287         if (cfg->disable_inline)
5288                 return FALSE;
5289         if (cfg->gshared)
5290                 return FALSE;
5291
5292         if (cfg->inline_depth > 10)
5293                 return FALSE;
5294
5295         if (!mono_method_get_header_summary (method, &header))
5296                 return FALSE;
5297
5298         /*runtime, icall and pinvoke are checked by summary call*/
5299         if ((method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
5300             (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
5301             (mono_class_is_marshalbyref (method->klass)) ||
5302             header.has_clauses)
5303                 return FALSE;
5304
5305         /* also consider num_locals? */
5306         /* Do the size check early to avoid creating vtables */
5307         if (!inline_limit_inited) {
5308                 if (g_getenv ("MONO_INLINELIMIT"))
5309                         inline_limit = atoi (g_getenv ("MONO_INLINELIMIT"));
5310                 else
5311                         inline_limit = INLINE_LENGTH_LIMIT;
5312                 inline_limit_inited = TRUE;
5313         }
5314         if (header.code_size >= inline_limit && !(method->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING))
5315                 return FALSE;
5316
5317         /*
5318          * if we can initialize the class of the method right away, we do,
5319          * otherwise we don't allow inlining if the class needs initialization,
5320          * since it would mean inserting a call to mono_runtime_class_init()
5321          * inside the inlined code
5322          */
5323         if (!(cfg->opt & MONO_OPT_SHARED)) {
5324                 /* The AggressiveInlining hint is a good excuse to force that cctor to run. */
5325                 if (method->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING) {
5326                         vtable = mono_class_vtable (cfg->domain, method->klass);
5327                         if (!vtable)
5328                                 return FALSE;
5329                         if (!cfg->compile_aot) {
5330                                 MonoError error;
5331                                 if (!mono_runtime_class_init_full (vtable, &error))
5332                                         mono_error_raise_exception (&error); /* FIXME don't raise here */
5333                         }
5334                 } else if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
5335                         if (cfg->run_cctors && method->klass->has_cctor) {
5336                                 /*FIXME it would easier and lazier to just use mono_class_try_get_vtable */
5337                                 if (!method->klass->runtime_info)
5338                                         /* No vtable created yet */
5339                                         return FALSE;
5340                                 vtable = mono_class_vtable (cfg->domain, method->klass);
5341                                 if (!vtable)
5342                                         return FALSE;
5343                                 /* This makes so that inline cannot trigger */
5344                                 /* .cctors: too many apps depend on them */
5345                                 /* running with a specific order... */
5346                                 if (! vtable->initialized)
5347                                         return FALSE;
5348                                 MonoError error;
5349                                 if (!mono_runtime_class_init_full (vtable, &error))
5350                                         mono_error_raise_exception (&error); /* FIXME don't raise here */
5351                         }
5352                 } else if (mono_class_needs_cctor_run (method->klass, NULL)) {
5353                         if (!method->klass->runtime_info)
5354                                 /* No vtable created yet */
5355                                 return FALSE;
5356                         vtable = mono_class_vtable (cfg->domain, method->klass);
5357                         if (!vtable)
5358                                 return FALSE;
5359                         if (!vtable->initialized)
5360                                 return FALSE;
5361                 }
5362         } else {
5363                 /* 
5364                  * If we're compiling for shared code
5365                  * the cctor will need to be run at aot method load time, for example,
5366                  * or at the end of the compilation of the inlining method.
5367                  */
5368                 if (mono_class_needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
5369                         return FALSE;
5370         }
5371
5372 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
5373         if (mono_arch_is_soft_float ()) {
5374                 /* FIXME: */
5375                 if (sig->ret && sig->ret->type == MONO_TYPE_R4)
5376                         return FALSE;
5377                 for (i = 0; i < sig->param_count; ++i)
5378                         if (!sig->params [i]->byref && sig->params [i]->type == MONO_TYPE_R4)
5379                                 return FALSE;
5380         }
5381 #endif
5382
5383         if (g_list_find (cfg->dont_inline, method))
5384                 return FALSE;
5385
5386         return TRUE;
5387 }
5388
5389 static gboolean
5390 mini_field_access_needs_cctor_run (MonoCompile *cfg, MonoMethod *method, MonoClass *klass, MonoVTable *vtable)
5391 {
5392         if (!cfg->compile_aot) {
5393                 g_assert (vtable);
5394                 if (vtable->initialized)
5395                         return FALSE;
5396         }
5397
5398         if (klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
5399                 if (cfg->method == method)
5400                         return FALSE;
5401         }
5402
5403         if (!mono_class_needs_cctor_run (klass, method))
5404                 return FALSE;
5405
5406         if (! (method->flags & METHOD_ATTRIBUTE_STATIC) && (klass == method->klass))
5407                 /* The initialization is already done before the method is called */
5408                 return FALSE;
5409
5410         return TRUE;
5411 }
5412
5413 static MonoInst*
5414 mini_emit_ldelema_1_ins (MonoCompile *cfg, MonoClass *klass, MonoInst *arr, MonoInst *index, gboolean bcheck)
5415 {
5416         MonoInst *ins;
5417         guint32 size;
5418         int mult_reg, add_reg, array_reg, index_reg, index2_reg;
5419         int context_used;
5420
5421         if (mini_is_gsharedvt_variable_klass (klass)) {
5422                 size = -1;
5423         } else {
5424                 mono_class_init (klass);
5425                 size = mono_class_array_element_size (klass);
5426         }
5427
5428         mult_reg = alloc_preg (cfg);
5429         array_reg = arr->dreg;
5430         index_reg = index->dreg;
5431
5432 #if SIZEOF_REGISTER == 8
5433         /* The array reg is 64 bits but the index reg is only 32 */
5434         if (COMPILE_LLVM (cfg)) {
5435                 /* Not needed */
5436                 index2_reg = index_reg;
5437         } else {
5438                 index2_reg = alloc_preg (cfg);
5439                 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, index2_reg, index_reg);
5440         }
5441 #else
5442         if (index->type == STACK_I8) {
5443                 index2_reg = alloc_preg (cfg);
5444                 MONO_EMIT_NEW_UNALU (cfg, OP_LCONV_TO_I4, index2_reg, index_reg);
5445         } else {
5446                 index2_reg = index_reg;
5447         }
5448 #endif
5449
5450         if (bcheck)
5451                 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index2_reg);
5452
5453 #if defined(TARGET_X86) || defined(TARGET_AMD64)
5454         if (size == 1 || size == 2 || size == 4 || size == 8) {
5455                 static const int fast_log2 [] = { 1, 0, 1, -1, 2, -1, -1, -1, 3 };
5456
5457                 EMIT_NEW_X86_LEA (cfg, ins, array_reg, index2_reg, fast_log2 [size], MONO_STRUCT_OFFSET (MonoArray, vector));
5458                 ins->klass = mono_class_get_element_class (klass);
5459                 ins->type = STACK_MP;
5460
5461                 return ins;
5462         }
5463 #endif          
5464
5465         add_reg = alloc_ireg_mp (cfg);
5466
5467         if (size == -1) {
5468                 MonoInst *rgctx_ins;
5469
5470                 /* gsharedvt */
5471                 g_assert (cfg->gshared);
5472                 context_used = mini_class_check_context_used (cfg, klass);
5473                 g_assert (context_used);
5474                 rgctx_ins = emit_get_gsharedvt_info_klass (cfg, klass, MONO_RGCTX_INFO_ARRAY_ELEMENT_SIZE);
5475                 MONO_EMIT_NEW_BIALU (cfg, OP_IMUL, mult_reg, index2_reg, rgctx_ins->dreg);
5476         } else {
5477                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_MUL_IMM, mult_reg, index2_reg, size);
5478         }
5479         MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, array_reg, mult_reg);
5480         NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, add_reg, add_reg, MONO_STRUCT_OFFSET (MonoArray, vector));
5481         ins->klass = mono_class_get_element_class (klass);
5482         ins->type = STACK_MP;
5483         MONO_ADD_INS (cfg->cbb, ins);
5484
5485         return ins;
5486 }
5487
5488 static MonoInst*
5489 mini_emit_ldelema_2_ins (MonoCompile *cfg, MonoClass *klass, MonoInst *arr, MonoInst *index_ins1, MonoInst *index_ins2)
5490 {
5491         int bounds_reg = alloc_preg (cfg);
5492         int add_reg = alloc_ireg_mp (cfg);
5493         int mult_reg = alloc_preg (cfg);
5494         int mult2_reg = alloc_preg (cfg);
5495         int low1_reg = alloc_preg (cfg);
5496         int low2_reg = alloc_preg (cfg);
5497         int high1_reg = alloc_preg (cfg);
5498         int high2_reg = alloc_preg (cfg);
5499         int realidx1_reg = alloc_preg (cfg);
5500         int realidx2_reg = alloc_preg (cfg);
5501         int sum_reg = alloc_preg (cfg);
5502         int index1, index2, tmpreg;
5503         MonoInst *ins;
5504         guint32 size;
5505
5506         mono_class_init (klass);
5507         size = mono_class_array_element_size (klass);
5508
5509         index1 = index_ins1->dreg;
5510         index2 = index_ins2->dreg;
5511
5512 #if SIZEOF_REGISTER == 8
5513         /* The array reg is 64 bits but the index reg is only 32 */
5514         if (COMPILE_LLVM (cfg)) {
5515                 /* Not needed */
5516         } else {
5517                 tmpreg = alloc_preg (cfg);
5518                 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, tmpreg, index1);
5519                 index1 = tmpreg;
5520                 tmpreg = alloc_preg (cfg);
5521                 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, tmpreg, index2);
5522                 index2 = tmpreg;
5523         }
5524 #else
5525         // FIXME: Do we need to do something here for i8 indexes, like in ldelema_1_ins ?
5526         tmpreg = -1;
5527 #endif
5528
5529         /* range checking */
5530         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg, 
5531                                        arr->dreg, MONO_STRUCT_OFFSET (MonoArray, bounds));
5532
5533         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, low1_reg, 
5534                                        bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
5535         MONO_EMIT_NEW_BIALU (cfg, OP_PSUB, realidx1_reg, index1, low1_reg);
5536         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, high1_reg, 
5537                                        bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, length));
5538         MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, high1_reg, realidx1_reg);
5539         MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException");
5540
5541         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, low2_reg, 
5542                                        bounds_reg, sizeof (MonoArrayBounds) + MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
5543         MONO_EMIT_NEW_BIALU (cfg, OP_PSUB, realidx2_reg, index2, low2_reg);
5544         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, high2_reg, 
5545                                        bounds_reg, sizeof (MonoArrayBounds) + MONO_STRUCT_OFFSET (MonoArrayBounds, length));
5546         MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, high2_reg, realidx2_reg);
5547         MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException");
5548
5549         MONO_EMIT_NEW_BIALU (cfg, OP_PMUL, mult_reg, high2_reg, realidx1_reg);
5550         MONO_EMIT_NEW_BIALU (cfg, OP_PADD, sum_reg, mult_reg, realidx2_reg);
5551         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PMUL_IMM, mult2_reg, sum_reg, size);
5552         MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, mult2_reg, arr->dreg);
5553         NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, add_reg, add_reg, MONO_STRUCT_OFFSET (MonoArray, vector));
5554
5555         ins->type = STACK_MP;
5556         ins->klass = klass;
5557         MONO_ADD_INS (cfg->cbb, ins);
5558
5559         return ins;
5560 }
5561
5562 static MonoInst*
5563 mini_emit_ldelema_ins (MonoCompile *cfg, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
5564 {
5565         int rank;
5566         MonoInst *addr;
5567         MonoMethod *addr_method;
5568         int element_size;
5569         MonoClass *eclass = cmethod->klass->element_class;
5570
5571         rank = mono_method_signature (cmethod)->param_count - (is_set? 1: 0);
5572
5573         if (rank == 1)
5574                 return mini_emit_ldelema_1_ins (cfg, eclass, sp [0], sp [1], TRUE);
5575
5576         /* emit_ldelema_2 depends on OP_LMUL */
5577         if (!cfg->backend->emulate_mul_div && rank == 2 && (cfg->opt & MONO_OPT_INTRINS) && !mini_is_gsharedvt_variable_klass (eclass)) {
5578                 return mini_emit_ldelema_2_ins (cfg, eclass, sp [0], sp [1], sp [2]);
5579         }
5580
5581         if (mini_is_gsharedvt_variable_klass (eclass))
5582                 element_size = 0;
5583         else
5584                 element_size = mono_class_array_element_size (eclass);
5585         addr_method = mono_marshal_get_array_address (rank, element_size);
5586         addr = mono_emit_method_call (cfg, addr_method, sp, NULL);
5587
5588         return addr;
5589 }
5590
5591 static MonoBreakPolicy
5592 always_insert_breakpoint (MonoMethod *method)
5593 {
5594         return MONO_BREAK_POLICY_ALWAYS;
5595 }
5596
5597 static MonoBreakPolicyFunc break_policy_func = always_insert_breakpoint;
5598
5599 /**
5600  * mono_set_break_policy:
5601  * policy_callback: the new callback function
5602  *
5603  * Allow embedders to decide wherther to actually obey breakpoint instructions
5604  * (both break IL instructions and Debugger.Break () method calls), for example
5605  * to not allow an app to be aborted by a perfectly valid IL opcode when executing
5606  * untrusted or semi-trusted code.
5607  *
5608  * @policy_callback will be called every time a break point instruction needs to
5609  * be inserted with the method argument being the method that calls Debugger.Break()
5610  * or has the IL break instruction. The callback should return #MONO_BREAK_POLICY_NEVER
5611  * if it wants the breakpoint to not be effective in the given method.
5612  * #MONO_BREAK_POLICY_ALWAYS is the default.
5613  */
5614 void
5615 mono_set_break_policy (MonoBreakPolicyFunc policy_callback)
5616 {
5617         if (policy_callback)
5618                 break_policy_func = policy_callback;
5619         else
5620                 break_policy_func = always_insert_breakpoint;
5621 }
5622
5623 static gboolean
5624 should_insert_brekpoint (MonoMethod *method) {
5625         switch (break_policy_func (method)) {
5626         case MONO_BREAK_POLICY_ALWAYS:
5627                 return TRUE;
5628         case MONO_BREAK_POLICY_NEVER:
5629                 return FALSE;
5630         case MONO_BREAK_POLICY_ON_DBG:
5631                 g_warning ("mdb no longer supported");
5632                 return FALSE;
5633         default:
5634                 g_warning ("Incorrect value returned from break policy callback");
5635                 return FALSE;
5636         }
5637 }
5638
5639 /* optimize the simple GetGenericValueImpl/SetGenericValueImpl generic icalls */
5640 static MonoInst*
5641 emit_array_generic_access (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, int is_set)
5642 {
5643         MonoInst *addr, *store, *load;
5644         MonoClass *eklass = mono_class_from_mono_type (fsig->params [2]);
5645
5646         /* the bounds check is already done by the callers */
5647         addr = mini_emit_ldelema_1_ins (cfg, eklass, args [0], args [1], FALSE);
5648         if (is_set) {
5649                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, &eklass->byval_arg, args [2]->dreg, 0);
5650                 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, &eklass->byval_arg, addr->dreg, 0, load->dreg);
5651                 if (mini_type_is_reference (fsig->params [2]))
5652                         emit_write_barrier (cfg, addr, load);
5653         } else {
5654                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, &eklass->byval_arg, addr->dreg, 0);
5655                 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, &eklass->byval_arg, args [2]->dreg, 0, load->dreg);
5656         }
5657         return store;
5658 }
5659
5660
5661 static gboolean
5662 generic_class_is_reference_type (MonoCompile *cfg, MonoClass *klass)
5663 {
5664         return mini_type_is_reference (&klass->byval_arg);
5665 }
5666
5667 static MonoInst*
5668 emit_array_store (MonoCompile *cfg, MonoClass *klass, MonoInst **sp, gboolean safety_checks)
5669 {
5670         if (safety_checks && generic_class_is_reference_type (cfg, klass) &&
5671                 !(sp [2]->opcode == OP_PCONST && sp [2]->inst_p0 == NULL)) {
5672                 MonoClass *obj_array = mono_array_class_get_cached (mono_defaults.object_class, 1);
5673                 MonoMethod *helper = mono_marshal_get_virtual_stelemref (obj_array);
5674                 MonoInst *iargs [3];
5675
5676                 if (!helper->slot)
5677                         mono_class_setup_vtable (obj_array);
5678                 g_assert (helper->slot);
5679
5680                 if (sp [0]->type != STACK_OBJ)
5681                         return NULL;
5682                 if (sp [2]->type != STACK_OBJ)
5683                         return NULL;
5684
5685                 iargs [2] = sp [2];
5686                 iargs [1] = sp [1];
5687                 iargs [0] = sp [0];
5688
5689                 return mono_emit_method_call (cfg, helper, iargs, sp [0]);
5690         } else {
5691                 MonoInst *ins;
5692
5693                 if (mini_is_gsharedvt_variable_klass (klass)) {
5694                         MonoInst *addr;
5695
5696                         // FIXME-VT: OP_ICONST optimization
5697                         addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
5698                         EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0, sp [2]->dreg);
5699                         ins->opcode = OP_STOREV_MEMBASE;
5700                 } else if (sp [1]->opcode == OP_ICONST) {
5701                         int array_reg = sp [0]->dreg;
5702                         int index_reg = sp [1]->dreg;
5703                         int offset = (mono_class_array_element_size (klass) * sp [1]->inst_c0) + MONO_STRUCT_OFFSET (MonoArray, vector);
5704
5705                         if (SIZEOF_REGISTER == 8 && COMPILE_LLVM (cfg))
5706                                 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, index_reg, index_reg);
5707
5708                         if (safety_checks)
5709                                 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index_reg);
5710                         EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, array_reg, offset, sp [2]->dreg);
5711                 } else {
5712                         MonoInst *addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], safety_checks);
5713                         EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0, sp [2]->dreg);
5714                         if (generic_class_is_reference_type (cfg, klass))
5715                                 emit_write_barrier (cfg, addr, sp [2]);
5716                 }
5717                 return ins;
5718         }
5719 }
5720
5721 static MonoInst*
5722 emit_array_unsafe_access (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args, int is_set)
5723 {
5724         MonoClass *eklass;
5725         
5726         if (is_set)
5727                 eklass = mono_class_from_mono_type (fsig->params [2]);
5728         else
5729                 eklass = mono_class_from_mono_type (fsig->ret);
5730
5731         if (is_set) {
5732                 return emit_array_store (cfg, eklass, args, FALSE);
5733         } else {
5734                 MonoInst *ins, *addr = mini_emit_ldelema_1_ins (cfg, eklass, args [0], args [1], FALSE);
5735                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &eklass->byval_arg, addr->dreg, 0);
5736                 return ins;
5737         }
5738 }
5739
5740 static gboolean
5741 is_unsafe_mov_compatible (MonoCompile *cfg, MonoClass *param_klass, MonoClass *return_klass)
5742 {
5743         uint32_t align;
5744         int param_size, return_size;
5745
5746         param_klass = mono_class_from_mono_type (mini_get_underlying_type (&param_klass->byval_arg));
5747         return_klass = mono_class_from_mono_type (mini_get_underlying_type (&return_klass->byval_arg));
5748
5749         if (cfg->verbose_level > 3)
5750                 printf ("[UNSAFE-MOV-INTRISIC] %s <- %s\n", return_klass->name, param_klass->name);
5751
5752         //Don't allow mixing reference types with value types
5753         if (param_klass->valuetype != return_klass->valuetype) {
5754                 if (cfg->verbose_level > 3)
5755                         printf ("[UNSAFE-MOV-INTRISIC]\tone of the args is a valuetype and the other is not\n");
5756                 return FALSE;
5757         }
5758
5759         if (!param_klass->valuetype) {
5760                 if (cfg->verbose_level > 3)
5761                         printf ("[UNSAFE-MOV-INTRISIC]\targs are reference types\n");
5762                 return TRUE;
5763         }
5764
5765         //That are blitable
5766         if (param_klass->has_references || return_klass->has_references)
5767                 return FALSE;
5768
5769         /* Avoid mixing structs and primitive types/enums, they need to be handled differently in the JIT */
5770         if ((MONO_TYPE_ISSTRUCT (&param_klass->byval_arg) && !MONO_TYPE_ISSTRUCT (&return_klass->byval_arg)) ||
5771                 (!MONO_TYPE_ISSTRUCT (&param_klass->byval_arg) && MONO_TYPE_ISSTRUCT (&return_klass->byval_arg))) {
5772                         if (cfg->verbose_level > 3)
5773                                 printf ("[UNSAFE-MOV-INTRISIC]\tmixing structs and scalars\n");
5774                 return FALSE;
5775         }
5776
5777         if (param_klass->byval_arg.type == MONO_TYPE_R4 || param_klass->byval_arg.type == MONO_TYPE_R8 ||
5778                 return_klass->byval_arg.type == MONO_TYPE_R4 || return_klass->byval_arg.type == MONO_TYPE_R8) {
5779                 if (cfg->verbose_level > 3)
5780                         printf ("[UNSAFE-MOV-INTRISIC]\tfloat or double are not supported\n");
5781                 return FALSE;
5782         }
5783
5784         param_size = mono_class_value_size (param_klass, &align);
5785         return_size = mono_class_value_size (return_klass, &align);
5786
5787         //We can do it if sizes match
5788         if (param_size == return_size) {
5789                 if (cfg->verbose_level > 3)
5790                         printf ("[UNSAFE-MOV-INTRISIC]\tsame size\n");
5791                 return TRUE;
5792         }
5793
5794         //No simple way to handle struct if sizes don't match
5795         if (MONO_TYPE_ISSTRUCT (&param_klass->byval_arg)) {
5796                 if (cfg->verbose_level > 3)
5797                         printf ("[UNSAFE-MOV-INTRISIC]\tsize mismatch and type is a struct\n");
5798                 return FALSE;
5799         }
5800
5801         /*
5802          * Same reg size category.
5803          * A quick note on why we don't require widening here.
5804          * The intrinsic is "R Array.UnsafeMov<S,R> (S s)".
5805          *
5806          * Since the source value comes from a function argument, the JIT will already have
5807          * the value in a VREG and performed any widening needed before (say, when loading from a field).
5808          */
5809         if (param_size <= 4 && return_size <= 4) {
5810                 if (cfg->verbose_level > 3)
5811                         printf ("[UNSAFE-MOV-INTRISIC]\tsize mismatch but both are of the same reg class\n");
5812                 return TRUE;
5813         }
5814
5815         return FALSE;
5816 }
5817
5818 static MonoInst*
5819 emit_array_unsafe_mov (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst **args)
5820 {
5821         MonoClass *param_klass = mono_class_from_mono_type (fsig->params [0]);
5822         MonoClass *return_klass = mono_class_from_mono_type (fsig->ret);
5823
5824         if (mini_is_gsharedvt_variable_type (fsig->ret))
5825                 return NULL;
5826
5827         //Valuetypes that are semantically equivalent or numbers than can be widened to
5828         if (is_unsafe_mov_compatible (cfg, param_klass, return_klass))
5829                 return args [0];
5830
5831         //Arrays of valuetypes that are semantically equivalent
5832         if (param_klass->rank == 1 && return_klass->rank == 1 && is_unsafe_mov_compatible (cfg, param_klass->element_class, return_klass->element_class))
5833                 return args [0];
5834
5835         return NULL;
5836 }
5837
5838 static MonoInst*
5839 mini_emit_inst_for_ctor (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5840 {
5841 #ifdef MONO_ARCH_SIMD_INTRINSICS
5842         MonoInst *ins = NULL;
5843
5844         if (cfg->opt & MONO_OPT_SIMD) {
5845                 ins = mono_emit_simd_intrinsics (cfg, cmethod, fsig, args);
5846                 if (ins)
5847                         return ins;
5848         }
5849 #endif
5850
5851         return mono_emit_native_types_intrinsics (cfg, cmethod, fsig, args);
5852 }
5853
5854 static MonoInst*
5855 emit_memory_barrier (MonoCompile *cfg, int kind)
5856 {
5857         MonoInst *ins = NULL;
5858         MONO_INST_NEW (cfg, ins, OP_MEMORY_BARRIER);
5859         MONO_ADD_INS (cfg->cbb, ins);
5860         ins->backend.memory_barrier_kind = kind;
5861
5862         return ins;
5863 }
5864
5865 static MonoInst*
5866 llvm_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5867 {
5868         MonoInst *ins = NULL;
5869         int opcode = 0;
5870
5871         /* The LLVM backend supports these intrinsics */
5872         if (cmethod->klass == mono_defaults.math_class) {
5873                 if (strcmp (cmethod->name, "Sin") == 0) {
5874                         opcode = OP_SIN;
5875                 } else if (strcmp (cmethod->name, "Cos") == 0) {
5876                         opcode = OP_COS;
5877                 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
5878                         opcode = OP_SQRT;
5879                 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
5880                         opcode = OP_ABS;
5881                 }
5882
5883                 if (opcode && fsig->param_count == 1) {
5884                         MONO_INST_NEW (cfg, ins, opcode);
5885                         ins->type = STACK_R8;
5886                         ins->dreg = mono_alloc_freg (cfg);
5887                         ins->sreg1 = args [0]->dreg;
5888                         MONO_ADD_INS (cfg->cbb, ins);
5889                 }
5890
5891                 opcode = 0;
5892                 if (cfg->opt & MONO_OPT_CMOV) {
5893                         if (strcmp (cmethod->name, "Min") == 0) {
5894                                 if (fsig->params [0]->type == MONO_TYPE_I4)
5895                                         opcode = OP_IMIN;
5896                                 if (fsig->params [0]->type == MONO_TYPE_U4)
5897                                         opcode = OP_IMIN_UN;
5898                                 else if (fsig->params [0]->type == MONO_TYPE_I8)
5899                                         opcode = OP_LMIN;
5900                                 else if (fsig->params [0]->type == MONO_TYPE_U8)
5901                                         opcode = OP_LMIN_UN;
5902                         } else if (strcmp (cmethod->name, "Max") == 0) {
5903                                 if (fsig->params [0]->type == MONO_TYPE_I4)
5904                                         opcode = OP_IMAX;
5905                                 if (fsig->params [0]->type == MONO_TYPE_U4)
5906                                         opcode = OP_IMAX_UN;
5907                                 else if (fsig->params [0]->type == MONO_TYPE_I8)
5908                                         opcode = OP_LMAX;
5909                                 else if (fsig->params [0]->type == MONO_TYPE_U8)
5910                                         opcode = OP_LMAX_UN;
5911                         }
5912                 }
5913
5914                 if (opcode && fsig->param_count == 2) {
5915                         MONO_INST_NEW (cfg, ins, opcode);
5916                         ins->type = fsig->params [0]->type == MONO_TYPE_I4 ? STACK_I4 : STACK_I8;
5917                         ins->dreg = mono_alloc_ireg (cfg);
5918                         ins->sreg1 = args [0]->dreg;
5919                         ins->sreg2 = args [1]->dreg;
5920                         MONO_ADD_INS (cfg->cbb, ins);
5921                 }
5922         }
5923
5924         return ins;
5925 }
5926
5927 static MonoInst*
5928 mini_emit_inst_for_sharable_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5929 {
5930         if (cmethod->klass == mono_defaults.array_class) {
5931                 if (strcmp (cmethod->name, "UnsafeStore") == 0)
5932                         return emit_array_unsafe_access (cfg, fsig, args, TRUE);
5933                 else if (strcmp (cmethod->name, "UnsafeLoad") == 0)
5934                         return emit_array_unsafe_access (cfg, fsig, args, FALSE);
5935                 else if (strcmp (cmethod->name, "UnsafeMov") == 0)
5936                         return emit_array_unsafe_mov (cfg, fsig, args);
5937         }
5938
5939         return NULL;
5940 }
5941
5942 static MonoInst*
5943 mini_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5944 {
5945         MonoInst *ins = NULL;
5946
5947          MonoClass *runtime_helpers_class = mono_class_get_runtime_helpers_class ();
5948
5949         if (cmethod->klass == mono_defaults.string_class) {
5950                 if (strcmp (cmethod->name, "get_Chars") == 0 && fsig->param_count + fsig->hasthis == 2) {
5951                         int dreg = alloc_ireg (cfg);
5952                         int index_reg = alloc_preg (cfg);
5953                         int add_reg = alloc_preg (cfg);
5954
5955 #if SIZEOF_REGISTER == 8
5956                         if (COMPILE_LLVM (cfg)) {
5957                                 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, index_reg, args [1]->dreg);
5958                         } else {
5959                                 /* The array reg is 64 bits but the index reg is only 32 */
5960                                 MONO_EMIT_NEW_UNALU (cfg, OP_SEXT_I4, index_reg, args [1]->dreg);
5961                         }
5962 #else
5963                         index_reg = args [1]->dreg;
5964 #endif  
5965                         MONO_EMIT_BOUNDS_CHECK (cfg, args [0]->dreg, MonoString, length, index_reg);
5966
5967 #if defined(TARGET_X86) || defined(TARGET_AMD64)
5968                         EMIT_NEW_X86_LEA (cfg, ins, args [0]->dreg, index_reg, 1, MONO_STRUCT_OFFSET (MonoString, chars));
5969                         add_reg = ins->dreg;
5970                         EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU2_MEMBASE, dreg, 
5971                                                                    add_reg, 0);
5972 #else
5973                         int mult_reg = alloc_preg (cfg);
5974                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, mult_reg, index_reg, 1);
5975                         MONO_EMIT_NEW_BIALU (cfg, OP_PADD, add_reg, mult_reg, args [0]->dreg);
5976                         EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU2_MEMBASE, dreg, 
5977                                                                    add_reg, MONO_STRUCT_OFFSET (MonoString, chars));
5978 #endif
5979                         type_from_op (cfg, ins, NULL, NULL);
5980                         return ins;
5981                 } else if (strcmp (cmethod->name, "get_Length") == 0 && fsig->param_count + fsig->hasthis == 1) {
5982                         int dreg = alloc_ireg (cfg);
5983                         /* Decompose later to allow more optimizations */
5984                         EMIT_NEW_UNALU (cfg, ins, OP_STRLEN, dreg, args [0]->dreg);
5985                         ins->type = STACK_I4;
5986                         ins->flags |= MONO_INST_FAULT;
5987                         cfg->cbb->has_array_access = TRUE;
5988                         cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
5989
5990                         return ins;
5991                 } else 
5992                         return NULL;
5993         } else if (cmethod->klass == mono_defaults.object_class) {
5994                 if (strcmp (cmethod->name, "GetType") == 0 && fsig->param_count + fsig->hasthis == 1) {
5995                         int dreg = alloc_ireg_ref (cfg);
5996                         int vt_reg = alloc_preg (cfg);
5997                         MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, vt_reg, args [0]->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
5998                         EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, vt_reg, MONO_STRUCT_OFFSET (MonoVTable, type));
5999                         type_from_op (cfg, ins, NULL, NULL);
6000
6001                         return ins;
6002                 } else if (!cfg->backend->emulate_mul_div && strcmp (cmethod->name, "InternalGetHashCode") == 0 && fsig->param_count == 1 && !mono_gc_is_moving ()) {
6003                         int dreg = alloc_ireg (cfg);
6004                         int t1 = alloc_ireg (cfg);
6005         
6006                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, t1, args [0]->dreg, 3);
6007                         EMIT_NEW_BIALU_IMM (cfg, ins, OP_MUL_IMM, dreg, t1, 2654435761u);
6008                         ins->type = STACK_I4;
6009
6010                         return ins;
6011                 } else if (strcmp (cmethod->name, ".ctor") == 0 && fsig->param_count == 0) {
6012                         MONO_INST_NEW (cfg, ins, OP_NOP);
6013                         MONO_ADD_INS (cfg->cbb, ins);
6014                         return ins;
6015                 } else
6016                         return NULL;
6017         } else if (cmethod->klass == mono_defaults.array_class) {
6018                 if (strcmp (cmethod->name, "GetGenericValueImpl") == 0 && fsig->param_count + fsig->hasthis == 3 && !cfg->gsharedvt)
6019                         return emit_array_generic_access (cfg, fsig, args, FALSE);
6020                 else if (strcmp (cmethod->name, "SetGenericValueImpl") == 0 && fsig->param_count + fsig->hasthis == 3 && !cfg->gsharedvt)
6021                         return emit_array_generic_access (cfg, fsig, args, TRUE);
6022
6023 #ifndef MONO_BIG_ARRAYS
6024                 /*
6025                  * This is an inline version of GetLength/GetLowerBound(0) used frequently in
6026                  * Array methods.
6027                  */
6028                 else if (((strcmp (cmethod->name, "GetLength") == 0 && fsig->param_count + fsig->hasthis == 2) ||
6029                          (strcmp (cmethod->name, "GetLowerBound") == 0 && fsig->param_count + fsig->hasthis == 2)) &&
6030                          args [1]->opcode == OP_ICONST && args [1]->inst_c0 == 0) {
6031                         int dreg = alloc_ireg (cfg);
6032                         int bounds_reg = alloc_ireg_mp (cfg);
6033                         MonoBasicBlock *end_bb, *szarray_bb;
6034                         gboolean get_length = strcmp (cmethod->name, "GetLength") == 0;
6035
6036                         NEW_BBLOCK (cfg, end_bb);
6037                         NEW_BBLOCK (cfg, szarray_bb);
6038
6039                         EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, ins, OP_LOAD_MEMBASE, bounds_reg,
6040                                                                                  args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, bounds));
6041                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, bounds_reg, 0);
6042                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, szarray_bb);
6043                         /* Non-szarray case */
6044                         if (get_length)
6045                                 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
6046                                                                            bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, length));
6047                         else
6048                                 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
6049                                                                            bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound));
6050                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
6051                         MONO_START_BB (cfg, szarray_bb);
6052                         /* Szarray case */
6053                         if (get_length)
6054                                 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADI4_MEMBASE, dreg,
6055                                                                            args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, max_length));
6056                         else
6057                                 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
6058                         MONO_START_BB (cfg, end_bb);
6059
6060                         EMIT_NEW_UNALU (cfg, ins, OP_MOVE, dreg, dreg);
6061                         ins->type = STACK_I4;
6062                         
6063                         return ins;
6064                 }
6065 #endif
6066
6067                 if (cmethod->name [0] != 'g')
6068                         return NULL;
6069
6070                 if (strcmp (cmethod->name, "get_Rank") == 0 && fsig->param_count + fsig->hasthis == 1) {
6071                         int dreg = alloc_ireg (cfg);
6072                         int vtable_reg = alloc_preg (cfg);
6073                         MONO_EMIT_NEW_LOAD_MEMBASE_OP_FAULT (cfg, OP_LOAD_MEMBASE, vtable_reg, 
6074                                                                                                  args [0]->dreg, MONO_STRUCT_OFFSET (MonoObject, vtable));
6075                         EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADU1_MEMBASE, dreg,
6076                                                                    vtable_reg, MONO_STRUCT_OFFSET (MonoVTable, rank));
6077                         type_from_op (cfg, ins, NULL, NULL);
6078
6079                         return ins;
6080                 } else if (strcmp (cmethod->name, "get_Length") == 0 && fsig->param_count + fsig->hasthis == 1) {
6081                         int dreg = alloc_ireg (cfg);
6082
6083                         EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, ins, OP_LOADI4_MEMBASE, dreg, 
6084                                                                                  args [0]->dreg, MONO_STRUCT_OFFSET (MonoArray, max_length));
6085                         type_from_op (cfg, ins, NULL, NULL);
6086
6087                         return ins;
6088                 } else
6089                         return NULL;
6090         } else if (cmethod->klass == runtime_helpers_class) {
6091                 if (strcmp (cmethod->name, "get_OffsetToStringData") == 0 && fsig->param_count == 0) {
6092                         EMIT_NEW_ICONST (cfg, ins, MONO_STRUCT_OFFSET (MonoString, chars));
6093                         return ins;
6094                 } else
6095                         return NULL;
6096         } else if (cmethod->klass == mono_defaults.monitor_class) {
6097                 gboolean is_enter = FALSE;
6098                 gboolean is_v4 = FALSE;
6099
6100                 if (!strcmp (cmethod->name, "enter_with_atomic_var") && mono_method_signature (cmethod)->param_count == 2) {
6101                         is_enter = TRUE;
6102                         is_v4 = TRUE;
6103                 }
6104                 if (!strcmp (cmethod->name, "Enter") && mono_method_signature (cmethod)->param_count == 1)
6105                         is_enter = TRUE;
6106
6107                 if (is_enter) {
6108                         /*
6109                          * To make async stack traces work, icalls which can block should have a wrapper.
6110                          * For Monitor.Enter, emit two calls: a fastpath which doesn't have a wrapper, and a slowpath, which does.
6111                          */
6112                         MonoBasicBlock *end_bb;
6113
6114                         NEW_BBLOCK (cfg, end_bb);
6115
6116                         ins = mono_emit_jit_icall (cfg, is_v4 ? (gpointer)mono_monitor_enter_v4_fast : (gpointer)mono_monitor_enter_fast, args);
6117                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ICOMPARE_IMM, -1, ins->dreg, 0);
6118                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBNE_UN, end_bb);
6119                         ins = mono_emit_jit_icall (cfg, is_v4 ? (gpointer)mono_monitor_enter_v4 : (gpointer)mono_monitor_enter, args);
6120                         MONO_START_BB (cfg, end_bb);
6121                         return ins;
6122                 }
6123         } else if (cmethod->klass == mono_defaults.thread_class) {
6124                 if (strcmp (cmethod->name, "SpinWait_nop") == 0 && fsig->param_count == 0) {
6125                         MONO_INST_NEW (cfg, ins, OP_RELAXED_NOP);
6126                         MONO_ADD_INS (cfg->cbb, ins);
6127                         return ins;
6128                 } else if (strcmp (cmethod->name, "MemoryBarrier") == 0 && fsig->param_count == 0) {
6129                         return emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6130                 } else if (!strcmp (cmethod->name, "VolatileRead") && fsig->param_count == 1) {
6131                         guint32 opcode = 0;
6132                         gboolean is_ref = mini_type_is_reference (fsig->params [0]);
6133
6134                         if (fsig->params [0]->type == MONO_TYPE_I1)
6135                                 opcode = OP_LOADI1_MEMBASE;
6136                         else if (fsig->params [0]->type == MONO_TYPE_U1)
6137                                 opcode = OP_LOADU1_MEMBASE;
6138                         else if (fsig->params [0]->type == MONO_TYPE_I2)
6139                                 opcode = OP_LOADI2_MEMBASE;
6140                         else if (fsig->params [0]->type == MONO_TYPE_U2)
6141                                 opcode = OP_LOADU2_MEMBASE;
6142                         else if (fsig->params [0]->type == MONO_TYPE_I4)
6143                                 opcode = OP_LOADI4_MEMBASE;
6144                         else if (fsig->params [0]->type == MONO_TYPE_U4)
6145                                 opcode = OP_LOADU4_MEMBASE;
6146                         else if (fsig->params [0]->type == MONO_TYPE_I8 || fsig->params [0]->type == MONO_TYPE_U8)
6147                                 opcode = OP_LOADI8_MEMBASE;
6148                         else if (fsig->params [0]->type == MONO_TYPE_R4)
6149                                 opcode = OP_LOADR4_MEMBASE;
6150                         else if (fsig->params [0]->type == MONO_TYPE_R8)
6151                                 opcode = OP_LOADR8_MEMBASE;
6152                         else if (is_ref || fsig->params [0]->type == MONO_TYPE_I || fsig->params [0]->type == MONO_TYPE_U)
6153                                 opcode = OP_LOAD_MEMBASE;
6154
6155                         if (opcode) {
6156                                 MONO_INST_NEW (cfg, ins, opcode);
6157                                 ins->inst_basereg = args [0]->dreg;
6158                                 ins->inst_offset = 0;
6159                                 MONO_ADD_INS (cfg->cbb, ins);
6160
6161                                 switch (fsig->params [0]->type) {
6162                                 case MONO_TYPE_I1:
6163                                 case MONO_TYPE_U1:
6164                                 case MONO_TYPE_I2:
6165                                 case MONO_TYPE_U2:
6166                                 case MONO_TYPE_I4:
6167                                 case MONO_TYPE_U4:
6168                                         ins->dreg = mono_alloc_ireg (cfg);
6169                                         ins->type = STACK_I4;
6170                                         break;
6171                                 case MONO_TYPE_I8:
6172                                 case MONO_TYPE_U8:
6173                                         ins->dreg = mono_alloc_lreg (cfg);
6174                                         ins->type = STACK_I8;
6175                                         break;
6176                                 case MONO_TYPE_I:
6177                                 case MONO_TYPE_U:
6178                                         ins->dreg = mono_alloc_ireg (cfg);
6179 #if SIZEOF_REGISTER == 8
6180                                         ins->type = STACK_I8;
6181 #else
6182                                         ins->type = STACK_I4;
6183 #endif
6184                                         break;
6185                                 case MONO_TYPE_R4:
6186                                 case MONO_TYPE_R8:
6187                                         ins->dreg = mono_alloc_freg (cfg);
6188                                         ins->type = STACK_R8;
6189                                         break;
6190                                 default:
6191                                         g_assert (mini_type_is_reference (fsig->params [0]));
6192                                         ins->dreg = mono_alloc_ireg_ref (cfg);
6193                                         ins->type = STACK_OBJ;
6194                                         break;
6195                                 }
6196
6197                                 if (opcode == OP_LOADI8_MEMBASE)
6198                                         ins = mono_decompose_opcode (cfg, ins);
6199
6200                                 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6201
6202                                 return ins;
6203                         }
6204                 } else if (!strcmp (cmethod->name, "VolatileWrite") && fsig->param_count == 2) {
6205                         guint32 opcode = 0;
6206                         gboolean is_ref = mini_type_is_reference (fsig->params [0]);
6207
6208                         if (fsig->params [0]->type == MONO_TYPE_I1 || fsig->params [0]->type == MONO_TYPE_U1)
6209                                 opcode = OP_STOREI1_MEMBASE_REG;
6210                         else if (fsig->params [0]->type == MONO_TYPE_I2 || fsig->params [0]->type == MONO_TYPE_U2)
6211                                 opcode = OP_STOREI2_MEMBASE_REG;
6212                         else if (fsig->params [0]->type == MONO_TYPE_I4 || fsig->params [0]->type == MONO_TYPE_U4)
6213                                 opcode = OP_STOREI4_MEMBASE_REG;
6214                         else if (fsig->params [0]->type == MONO_TYPE_I8 || fsig->params [0]->type == MONO_TYPE_U8)
6215                                 opcode = OP_STOREI8_MEMBASE_REG;
6216                         else if (fsig->params [0]->type == MONO_TYPE_R4)
6217                                 opcode = OP_STORER4_MEMBASE_REG;
6218                         else if (fsig->params [0]->type == MONO_TYPE_R8)
6219                                 opcode = OP_STORER8_MEMBASE_REG;
6220                         else if (is_ref || fsig->params [0]->type == MONO_TYPE_I || fsig->params [0]->type == MONO_TYPE_U)
6221                                 opcode = OP_STORE_MEMBASE_REG;
6222
6223                         if (opcode) {
6224                                 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6225
6226                                 MONO_INST_NEW (cfg, ins, opcode);
6227                                 ins->sreg1 = args [1]->dreg;
6228                                 ins->inst_destbasereg = args [0]->dreg;
6229                                 ins->inst_offset = 0;
6230                                 MONO_ADD_INS (cfg->cbb, ins);
6231
6232                                 if (opcode == OP_STOREI8_MEMBASE_REG)
6233                                         ins = mono_decompose_opcode (cfg, ins);
6234
6235                                 return ins;
6236                         }
6237                 }
6238         } else if (cmethod->klass->image == mono_defaults.corlib &&
6239                            (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
6240                            (strcmp (cmethod->klass->name, "Interlocked") == 0)) {
6241                 ins = NULL;
6242
6243 #if SIZEOF_REGISTER == 8
6244                 if (!cfg->llvm_only && strcmp (cmethod->name, "Read") == 0 && fsig->param_count == 1 && (fsig->params [0]->type == MONO_TYPE_I8)) {
6245                         if (!cfg->llvm_only && mono_arch_opcode_supported (OP_ATOMIC_LOAD_I8)) {
6246                                 MONO_INST_NEW (cfg, ins, OP_ATOMIC_LOAD_I8);
6247                                 ins->dreg = mono_alloc_preg (cfg);
6248                                 ins->sreg1 = args [0]->dreg;
6249                                 ins->type = STACK_I8;
6250                                 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_SEQ;
6251                                 MONO_ADD_INS (cfg->cbb, ins);
6252                         } else {
6253                                 MonoInst *load_ins;
6254
6255                                 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6256
6257                                 /* 64 bit reads are already atomic */
6258                                 MONO_INST_NEW (cfg, load_ins, OP_LOADI8_MEMBASE);
6259                                 load_ins->dreg = mono_alloc_preg (cfg);
6260                                 load_ins->inst_basereg = args [0]->dreg;
6261                                 load_ins->inst_offset = 0;
6262                                 load_ins->type = STACK_I8;
6263                                 MONO_ADD_INS (cfg->cbb, load_ins);
6264
6265                                 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6266
6267                                 ins = load_ins;
6268                         }
6269                 }
6270 #endif
6271
6272                 if (strcmp (cmethod->name, "Increment") == 0 && fsig->param_count == 1) {
6273                         MonoInst *ins_iconst;
6274                         guint32 opcode = 0;
6275
6276                         if (fsig->params [0]->type == MONO_TYPE_I4) {
6277                                 opcode = OP_ATOMIC_ADD_I4;
6278                                 cfg->has_atomic_add_i4 = TRUE;
6279                         }
6280 #if SIZEOF_REGISTER == 8
6281                         else if (fsig->params [0]->type == MONO_TYPE_I8)
6282                                 opcode = OP_ATOMIC_ADD_I8;
6283 #endif
6284                         if (opcode) {
6285                                 if (!mono_arch_opcode_supported (opcode))
6286                                         return NULL;
6287                                 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
6288                                 ins_iconst->inst_c0 = 1;
6289                                 ins_iconst->dreg = mono_alloc_ireg (cfg);
6290                                 MONO_ADD_INS (cfg->cbb, ins_iconst);
6291
6292                                 MONO_INST_NEW (cfg, ins, opcode);
6293                                 ins->dreg = mono_alloc_ireg (cfg);
6294                                 ins->inst_basereg = args [0]->dreg;
6295                                 ins->inst_offset = 0;
6296                                 ins->sreg2 = ins_iconst->dreg;
6297                                 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
6298                                 MONO_ADD_INS (cfg->cbb, ins);
6299                         }
6300                 } else if (strcmp (cmethod->name, "Decrement") == 0 && fsig->param_count == 1) {
6301                         MonoInst *ins_iconst;
6302                         guint32 opcode = 0;
6303
6304                         if (fsig->params [0]->type == MONO_TYPE_I4) {
6305                                 opcode = OP_ATOMIC_ADD_I4;
6306                                 cfg->has_atomic_add_i4 = TRUE;
6307                         }
6308 #if SIZEOF_REGISTER == 8
6309                         else if (fsig->params [0]->type == MONO_TYPE_I8)
6310                                 opcode = OP_ATOMIC_ADD_I8;
6311 #endif
6312                         if (opcode) {
6313                                 if (!mono_arch_opcode_supported (opcode))
6314                                         return NULL;
6315                                 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
6316                                 ins_iconst->inst_c0 = -1;
6317                                 ins_iconst->dreg = mono_alloc_ireg (cfg);
6318                                 MONO_ADD_INS (cfg->cbb, ins_iconst);
6319
6320                                 MONO_INST_NEW (cfg, ins, opcode);
6321                                 ins->dreg = mono_alloc_ireg (cfg);
6322                                 ins->inst_basereg = args [0]->dreg;
6323                                 ins->inst_offset = 0;
6324                                 ins->sreg2 = ins_iconst->dreg;
6325                                 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
6326                                 MONO_ADD_INS (cfg->cbb, ins);
6327                         }
6328                 } else if (strcmp (cmethod->name, "Add") == 0 && fsig->param_count == 2) {
6329                         guint32 opcode = 0;
6330
6331                         if (fsig->params [0]->type == MONO_TYPE_I4) {
6332                                 opcode = OP_ATOMIC_ADD_I4;
6333                                 cfg->has_atomic_add_i4 = TRUE;
6334                         }
6335 #if SIZEOF_REGISTER == 8
6336                         else if (fsig->params [0]->type == MONO_TYPE_I8)
6337                                 opcode = OP_ATOMIC_ADD_I8;
6338 #endif
6339                         if (opcode) {
6340                                 if (!mono_arch_opcode_supported (opcode))
6341                                         return NULL;
6342                                 MONO_INST_NEW (cfg, ins, opcode);
6343                                 ins->dreg = mono_alloc_ireg (cfg);
6344                                 ins->inst_basereg = args [0]->dreg;
6345                                 ins->inst_offset = 0;
6346                                 ins->sreg2 = args [1]->dreg;
6347                                 ins->type = (opcode == OP_ATOMIC_ADD_I4) ? STACK_I4 : STACK_I8;
6348                                 MONO_ADD_INS (cfg->cbb, ins);
6349                         }
6350                 }
6351                 else if (strcmp (cmethod->name, "Exchange") == 0 && fsig->param_count == 2) {
6352                         MonoInst *f2i = NULL, *i2f;
6353                         guint32 opcode, f2i_opcode, i2f_opcode;
6354                         gboolean is_ref = mini_type_is_reference (fsig->params [0]);
6355                         gboolean is_float = fsig->params [0]->type == MONO_TYPE_R4 || fsig->params [0]->type == MONO_TYPE_R8;
6356
6357                         if (fsig->params [0]->type == MONO_TYPE_I4 ||
6358                             fsig->params [0]->type == MONO_TYPE_R4) {
6359                                 opcode = OP_ATOMIC_EXCHANGE_I4;
6360                                 f2i_opcode = OP_MOVE_F_TO_I4;
6361                                 i2f_opcode = OP_MOVE_I4_TO_F;
6362                                 cfg->has_atomic_exchange_i4 = TRUE;
6363                         }
6364 #if SIZEOF_REGISTER == 8
6365                         else if (is_ref ||
6366                                  fsig->params [0]->type == MONO_TYPE_I8 ||
6367                                  fsig->params [0]->type == MONO_TYPE_R8 ||
6368                                  fsig->params [0]->type == MONO_TYPE_I) {
6369                                 opcode = OP_ATOMIC_EXCHANGE_I8;
6370                                 f2i_opcode = OP_MOVE_F_TO_I8;
6371                                 i2f_opcode = OP_MOVE_I8_TO_F;
6372                         }
6373 #else
6374                         else if (is_ref || fsig->params [0]->type == MONO_TYPE_I) {
6375                                 opcode = OP_ATOMIC_EXCHANGE_I4;
6376                                 cfg->has_atomic_exchange_i4 = TRUE;
6377                         }
6378 #endif
6379                         else
6380                                 return NULL;
6381
6382                         if (!mono_arch_opcode_supported (opcode))
6383                                 return NULL;
6384
6385                         if (is_float) {
6386                                 /* TODO: Decompose these opcodes instead of bailing here. */
6387                                 if (COMPILE_SOFT_FLOAT (cfg))
6388                                         return NULL;
6389
6390                                 MONO_INST_NEW (cfg, f2i, f2i_opcode);
6391                                 f2i->dreg = mono_alloc_ireg (cfg);
6392                                 f2i->sreg1 = args [1]->dreg;
6393                                 if (f2i_opcode == OP_MOVE_F_TO_I4)
6394                                         f2i->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6395                                 MONO_ADD_INS (cfg->cbb, f2i);
6396                         }
6397
6398                         MONO_INST_NEW (cfg, ins, opcode);
6399                         ins->dreg = is_ref ? mono_alloc_ireg_ref (cfg) : mono_alloc_ireg (cfg);
6400                         ins->inst_basereg = args [0]->dreg;
6401                         ins->inst_offset = 0;
6402                         ins->sreg2 = is_float ? f2i->dreg : args [1]->dreg;
6403                         MONO_ADD_INS (cfg->cbb, ins);
6404
6405                         switch (fsig->params [0]->type) {
6406                         case MONO_TYPE_I4:
6407                                 ins->type = STACK_I4;
6408                                 break;
6409                         case MONO_TYPE_I8:
6410                                 ins->type = STACK_I8;
6411                                 break;
6412                         case MONO_TYPE_I:
6413 #if SIZEOF_REGISTER == 8
6414                                 ins->type = STACK_I8;
6415 #else
6416                                 ins->type = STACK_I4;
6417 #endif
6418                                 break;
6419                         case MONO_TYPE_R4:
6420                         case MONO_TYPE_R8:
6421                                 ins->type = STACK_R8;
6422                                 break;
6423                         default:
6424                                 g_assert (mini_type_is_reference (fsig->params [0]));
6425                                 ins->type = STACK_OBJ;
6426                                 break;
6427                         }
6428
6429                         if (is_float) {
6430                                 MONO_INST_NEW (cfg, i2f, i2f_opcode);
6431                                 i2f->dreg = mono_alloc_freg (cfg);
6432                                 i2f->sreg1 = ins->dreg;
6433                                 i2f->type = STACK_R8;
6434                                 if (i2f_opcode == OP_MOVE_I4_TO_F)
6435                                         i2f->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6436                                 MONO_ADD_INS (cfg->cbb, i2f);
6437
6438                                 ins = i2f;
6439                         }
6440
6441                         if (cfg->gen_write_barriers && is_ref)
6442                                 emit_write_barrier (cfg, args [0], args [1]);
6443                 }
6444                 else if ((strcmp (cmethod->name, "CompareExchange") == 0) && fsig->param_count == 3) {
6445                         MonoInst *f2i_new = NULL, *f2i_cmp = NULL, *i2f;
6446                         guint32 opcode, f2i_opcode, i2f_opcode;
6447                         gboolean is_ref = mini_type_is_reference (fsig->params [1]);
6448                         gboolean is_float = fsig->params [1]->type == MONO_TYPE_R4 || fsig->params [1]->type == MONO_TYPE_R8;
6449
6450                         if (fsig->params [1]->type == MONO_TYPE_I4 ||
6451                             fsig->params [1]->type == MONO_TYPE_R4) {
6452                                 opcode = OP_ATOMIC_CAS_I4;
6453                                 f2i_opcode = OP_MOVE_F_TO_I4;
6454                                 i2f_opcode = OP_MOVE_I4_TO_F;
6455                                 cfg->has_atomic_cas_i4 = TRUE;
6456                         }
6457 #if SIZEOF_REGISTER == 8
6458                         else if (is_ref ||
6459                                  fsig->params [1]->type == MONO_TYPE_I8 ||
6460                                  fsig->params [1]->type == MONO_TYPE_R8 ||
6461                                  fsig->params [1]->type == MONO_TYPE_I) {
6462                                 opcode = OP_ATOMIC_CAS_I8;
6463                                 f2i_opcode = OP_MOVE_F_TO_I8;
6464                                 i2f_opcode = OP_MOVE_I8_TO_F;
6465                         }
6466 #else
6467                         else if (is_ref || fsig->params [1]->type == MONO_TYPE_I) {
6468                                 opcode = OP_ATOMIC_CAS_I4;
6469                                 cfg->has_atomic_cas_i4 = TRUE;
6470                         }
6471 #endif
6472                         else
6473                                 return NULL;
6474
6475                         if (!mono_arch_opcode_supported (opcode))
6476                                 return NULL;
6477
6478                         if (is_float) {
6479                                 /* TODO: Decompose these opcodes instead of bailing here. */
6480                                 if (COMPILE_SOFT_FLOAT (cfg))
6481                                         return NULL;
6482
6483                                 MONO_INST_NEW (cfg, f2i_new, f2i_opcode);
6484                                 f2i_new->dreg = mono_alloc_ireg (cfg);
6485                                 f2i_new->sreg1 = args [1]->dreg;
6486                                 if (f2i_opcode == OP_MOVE_F_TO_I4)
6487                                         f2i_new->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6488                                 MONO_ADD_INS (cfg->cbb, f2i_new);
6489
6490                                 MONO_INST_NEW (cfg, f2i_cmp, f2i_opcode);
6491                                 f2i_cmp->dreg = mono_alloc_ireg (cfg);
6492                                 f2i_cmp->sreg1 = args [2]->dreg;
6493                                 if (f2i_opcode == OP_MOVE_F_TO_I4)
6494                                         f2i_cmp->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6495                                 MONO_ADD_INS (cfg->cbb, f2i_cmp);
6496                         }
6497
6498                         MONO_INST_NEW (cfg, ins, opcode);
6499                         ins->dreg = is_ref ? alloc_ireg_ref (cfg) : alloc_ireg (cfg);
6500                         ins->sreg1 = args [0]->dreg;
6501                         ins->sreg2 = is_float ? f2i_new->dreg : args [1]->dreg;
6502                         ins->sreg3 = is_float ? f2i_cmp->dreg : args [2]->dreg;
6503                         MONO_ADD_INS (cfg->cbb, ins);
6504
6505                         switch (fsig->params [1]->type) {
6506                         case MONO_TYPE_I4:
6507                                 ins->type = STACK_I4;
6508                                 break;
6509                         case MONO_TYPE_I8:
6510                                 ins->type = STACK_I8;
6511                                 break;
6512                         case MONO_TYPE_I:
6513 #if SIZEOF_REGISTER == 8
6514                                 ins->type = STACK_I8;
6515 #else
6516                                 ins->type = STACK_I4;
6517 #endif
6518                                 break;
6519                         case MONO_TYPE_R4:
6520                                 ins->type = cfg->r4_stack_type;
6521                                 break;
6522                         case MONO_TYPE_R8:
6523                                 ins->type = STACK_R8;
6524                                 break;
6525                         default:
6526                                 g_assert (mini_type_is_reference (fsig->params [1]));
6527                                 ins->type = STACK_OBJ;
6528                                 break;
6529                         }
6530
6531                         if (is_float) {
6532                                 MONO_INST_NEW (cfg, i2f, i2f_opcode);
6533                                 i2f->dreg = mono_alloc_freg (cfg);
6534                                 i2f->sreg1 = ins->dreg;
6535                                 i2f->type = STACK_R8;
6536                                 if (i2f_opcode == OP_MOVE_I4_TO_F)
6537                                         i2f->backend.spill_var = mini_get_int_to_float_spill_area (cfg);
6538                                 MONO_ADD_INS (cfg->cbb, i2f);
6539
6540                                 ins = i2f;
6541                         }
6542
6543                         if (cfg->gen_write_barriers && is_ref)
6544                                 emit_write_barrier (cfg, args [0], args [1]);
6545                 }
6546                 else if ((strcmp (cmethod->name, "CompareExchange") == 0) && fsig->param_count == 4 &&
6547                          fsig->params [1]->type == MONO_TYPE_I4) {
6548                         MonoInst *cmp, *ceq;
6549
6550                         if (!mono_arch_opcode_supported (OP_ATOMIC_CAS_I4))
6551                                 return NULL;
6552
6553                         /* int32 r = CAS (location, value, comparand); */
6554                         MONO_INST_NEW (cfg, ins, OP_ATOMIC_CAS_I4);
6555                         ins->dreg = alloc_ireg (cfg);
6556                         ins->sreg1 = args [0]->dreg;
6557                         ins->sreg2 = args [1]->dreg;
6558                         ins->sreg3 = args [2]->dreg;
6559                         ins->type = STACK_I4;
6560                         MONO_ADD_INS (cfg->cbb, ins);
6561
6562                         /* bool result = r == comparand; */
6563                         MONO_INST_NEW (cfg, cmp, OP_ICOMPARE);
6564                         cmp->sreg1 = ins->dreg;
6565                         cmp->sreg2 = args [2]->dreg;
6566                         cmp->type = STACK_I4;
6567                         MONO_ADD_INS (cfg->cbb, cmp);
6568
6569                         MONO_INST_NEW (cfg, ceq, OP_ICEQ);
6570                         ceq->dreg = alloc_ireg (cfg);
6571                         ceq->type = STACK_I4;
6572                         MONO_ADD_INS (cfg->cbb, ceq);
6573
6574                         /* *success = result; */
6575                         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, args [3]->dreg, 0, ceq->dreg);
6576
6577                         cfg->has_atomic_cas_i4 = TRUE;
6578                 }
6579                 else if (strcmp (cmethod->name, "MemoryBarrier") == 0 && fsig->param_count == 0)
6580                         ins = emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
6581
6582                 if (ins)
6583                         return ins;
6584         } else if (cmethod->klass->image == mono_defaults.corlib &&
6585                            (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
6586                            (strcmp (cmethod->klass->name, "Volatile") == 0)) {
6587                 ins = NULL;
6588
6589                 if (!cfg->llvm_only && !strcmp (cmethod->name, "Read") && fsig->param_count == 1) {
6590                         guint32 opcode = 0;
6591                         MonoType *t = fsig->params [0];
6592                         gboolean is_ref;
6593                         gboolean is_float = t->type == MONO_TYPE_R4 || t->type == MONO_TYPE_R8;
6594
6595                         g_assert (t->byref);
6596                         /* t is a byref type, so the reference check is more complicated */
6597                         is_ref = mini_type_is_reference (&mono_class_from_mono_type (t)->byval_arg);
6598                         if (t->type == MONO_TYPE_I1)
6599                                 opcode = OP_ATOMIC_LOAD_I1;
6600                         else if (t->type == MONO_TYPE_U1 || t->type == MONO_TYPE_BOOLEAN)
6601                                 opcode = OP_ATOMIC_LOAD_U1;
6602                         else if (t->type == MONO_TYPE_I2)
6603                                 opcode = OP_ATOMIC_LOAD_I2;
6604                         else if (t->type == MONO_TYPE_U2)
6605                                 opcode = OP_ATOMIC_LOAD_U2;
6606                         else if (t->type == MONO_TYPE_I4)
6607                                 opcode = OP_ATOMIC_LOAD_I4;
6608                         else if (t->type == MONO_TYPE_U4)
6609                                 opcode = OP_ATOMIC_LOAD_U4;
6610                         else if (t->type == MONO_TYPE_R4)
6611                                 opcode = OP_ATOMIC_LOAD_R4;
6612                         else if (t->type == MONO_TYPE_R8)
6613                                 opcode = OP_ATOMIC_LOAD_R8;
6614 #if SIZEOF_REGISTER == 8
6615                         else if (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_I)
6616                                 opcode = OP_ATOMIC_LOAD_I8;
6617                         else if (is_ref || t->type == MONO_TYPE_U8 || t->type == MONO_TYPE_U)
6618                                 opcode = OP_ATOMIC_LOAD_U8;
6619 #else
6620                         else if (t->type == MONO_TYPE_I)
6621                                 opcode = OP_ATOMIC_LOAD_I4;
6622                         else if (is_ref || t->type == MONO_TYPE_U)
6623                                 opcode = OP_ATOMIC_LOAD_U4;
6624 #endif
6625
6626                         if (opcode) {
6627                                 if (!mono_arch_opcode_supported (opcode))
6628                                         return NULL;
6629
6630                                 MONO_INST_NEW (cfg, ins, opcode);
6631                                 ins->dreg = is_ref ? mono_alloc_ireg_ref (cfg) : (is_float ? mono_alloc_freg (cfg) : mono_alloc_ireg (cfg));
6632                                 ins->sreg1 = args [0]->dreg;
6633                                 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_ACQ;
6634                                 MONO_ADD_INS (cfg->cbb, ins);
6635
6636                                 switch (t->type) {
6637                                 case MONO_TYPE_BOOLEAN:
6638                                 case MONO_TYPE_I1:
6639                                 case MONO_TYPE_U1:
6640                                 case MONO_TYPE_I2:
6641                                 case MONO_TYPE_U2:
6642                                 case MONO_TYPE_I4:
6643                                 case MONO_TYPE_U4:
6644                                         ins->type = STACK_I4;
6645                                         break;
6646                                 case MONO_TYPE_I8:
6647                                 case MONO_TYPE_U8:
6648                                         ins->type = STACK_I8;
6649                                         break;
6650                                 case MONO_TYPE_I:
6651                                 case MONO_TYPE_U:
6652 #if SIZEOF_REGISTER == 8
6653                                         ins->type = STACK_I8;
6654 #else
6655                                         ins->type = STACK_I4;
6656 #endif
6657                                         break;
6658                                 case MONO_TYPE_R4:
6659                                         ins->type = cfg->r4_stack_type;
6660                                         break;
6661                                 case MONO_TYPE_R8:
6662                                         ins->type = STACK_R8;
6663                                         break;
6664                                 default:
6665                                         g_assert (is_ref);
6666                                         ins->type = STACK_OBJ;
6667                                         break;
6668                                 }
6669                         }
6670                 }
6671
6672                 if (!cfg->llvm_only && !strcmp (cmethod->name, "Write") && fsig->param_count == 2) {
6673                         guint32 opcode = 0;
6674                         MonoType *t = fsig->params [0];
6675                         gboolean is_ref;
6676
6677                         g_assert (t->byref);
6678                         is_ref = mini_type_is_reference (&mono_class_from_mono_type (t)->byval_arg);
6679                         if (t->type == MONO_TYPE_I1)
6680                                 opcode = OP_ATOMIC_STORE_I1;
6681                         else if (t->type == MONO_TYPE_U1 || t->type == MONO_TYPE_BOOLEAN)
6682                                 opcode = OP_ATOMIC_STORE_U1;
6683                         else if (t->type == MONO_TYPE_I2)
6684                                 opcode = OP_ATOMIC_STORE_I2;
6685                         else if (t->type == MONO_TYPE_U2)
6686                                 opcode = OP_ATOMIC_STORE_U2;
6687                         else if (t->type == MONO_TYPE_I4)
6688                                 opcode = OP_ATOMIC_STORE_I4;
6689                         else if (t->type == MONO_TYPE_U4)
6690                                 opcode = OP_ATOMIC_STORE_U4;
6691                         else if (t->type == MONO_TYPE_R4)
6692                                 opcode = OP_ATOMIC_STORE_R4;
6693                         else if (t->type == MONO_TYPE_R8)
6694                                 opcode = OP_ATOMIC_STORE_R8;
6695 #if SIZEOF_REGISTER == 8
6696                         else if (t->type == MONO_TYPE_I8 || t->type == MONO_TYPE_I)
6697                                 opcode = OP_ATOMIC_STORE_I8;
6698                         else if (is_ref || t->type == MONO_TYPE_U8 || t->type == MONO_TYPE_U)
6699                                 opcode = OP_ATOMIC_STORE_U8;
6700 #else
6701                         else if (t->type == MONO_TYPE_I)
6702                                 opcode = OP_ATOMIC_STORE_I4;
6703                         else if (is_ref || t->type == MONO_TYPE_U)
6704                                 opcode = OP_ATOMIC_STORE_U4;
6705 #endif
6706
6707                         if (opcode) {
6708                                 if (!mono_arch_opcode_supported (opcode))
6709                                         return NULL;
6710
6711                                 MONO_INST_NEW (cfg, ins, opcode);
6712                                 ins->dreg = args [0]->dreg;
6713                                 ins->sreg1 = args [1]->dreg;
6714                                 ins->backend.memory_barrier_kind = MONO_MEMORY_BARRIER_REL;
6715                                 MONO_ADD_INS (cfg->cbb, ins);
6716
6717                                 if (cfg->gen_write_barriers && is_ref)
6718                                         emit_write_barrier (cfg, args [0], args [1]);
6719                         }
6720                 }
6721
6722                 if (ins)
6723                         return ins;
6724         } else if (cmethod->klass->image == mono_defaults.corlib &&
6725                            (strcmp (cmethod->klass->name_space, "System.Diagnostics") == 0) &&
6726                            (strcmp (cmethod->klass->name, "Debugger") == 0)) {
6727                 if (!strcmp (cmethod->name, "Break") && fsig->param_count == 0) {
6728                         if (should_insert_brekpoint (cfg->method)) {
6729                                 ins = mono_emit_jit_icall (cfg, mono_debugger_agent_user_break, NULL);
6730                         } else {
6731                                 MONO_INST_NEW (cfg, ins, OP_NOP);
6732                                 MONO_ADD_INS (cfg->cbb, ins);
6733                         }
6734                         return ins;
6735                 }
6736         } else if (cmethod->klass->image == mono_defaults.corlib &&
6737                    (strcmp (cmethod->klass->name_space, "System") == 0) &&
6738                    (strcmp (cmethod->klass->name, "Environment") == 0)) {
6739                 if (!strcmp (cmethod->name, "get_IsRunningOnWindows") && fsig->param_count == 0) {
6740 #ifdef TARGET_WIN32
6741                         EMIT_NEW_ICONST (cfg, ins, 1);
6742 #else
6743                         EMIT_NEW_ICONST (cfg, ins, 0);
6744 #endif
6745                 }
6746         } else if (cmethod->klass->image == mono_defaults.corlib &&
6747                            (strcmp (cmethod->klass->name_space, "System.Reflection") == 0) &&
6748                            (strcmp (cmethod->klass->name, "Assembly") == 0)) {
6749                 if (cfg->llvm_only && !strcmp (cmethod->name, "GetExecutingAssembly")) {
6750                         /* No stack walks are currently available, so implement this as an intrinsic */
6751                         MonoInst *assembly_ins;
6752
6753                         EMIT_NEW_AOTCONST (cfg, assembly_ins, MONO_PATCH_INFO_IMAGE, cfg->method->klass->image);
6754                         ins = mono_emit_jit_icall (cfg, mono_get_assembly_object, &assembly_ins);
6755                         return ins;
6756                 }
6757         } else if (cmethod->klass->image == mono_defaults.corlib &&
6758                            (strcmp (cmethod->klass->name_space, "System.Reflection") == 0) &&
6759                            (strcmp (cmethod->klass->name, "MethodBase") == 0)) {
6760                 if (cfg->llvm_only && !strcmp (cmethod->name, "GetCurrentMethod")) {
6761                         /* No stack walks are currently available, so implement this as an intrinsic */
6762                         MonoInst *method_ins;
6763                         MonoMethod *declaring = cfg->method;
6764
6765                         /* This returns the declaring generic method */
6766                         if (declaring->is_inflated)
6767                                 declaring = ((MonoMethodInflated*)cfg->method)->declaring;
6768                         EMIT_NEW_AOTCONST (cfg, method_ins, MONO_PATCH_INFO_METHODCONST, declaring);
6769                         ins = mono_emit_jit_icall (cfg, mono_get_method_object, &method_ins);
6770                         cfg->no_inline = TRUE;
6771                         if (cfg->method != cfg->current_method)
6772                                 inline_failure (cfg, "MethodBase:GetCurrentMethod ()");
6773                         return ins;
6774                 }
6775         } else if (cmethod->klass == mono_defaults.math_class) {
6776                 /* 
6777                  * There is general branchless code for Min/Max, but it does not work for 
6778                  * all inputs:
6779                  * http://everything2.com/?node_id=1051618
6780                  */
6781         } else if (((!strcmp (cmethod->klass->image->assembly->aname.name, "MonoMac") ||
6782                     !strcmp (cmethod->klass->image->assembly->aname.name, "monotouch")) &&
6783                                 !strcmp (cmethod->klass->name_space, "XamCore.ObjCRuntime") &&
6784                                 !strcmp (cmethod->klass->name, "Selector")) ||
6785                            (!strcmp (cmethod->klass->image->assembly->aname.name, "Xamarin.iOS") &&
6786                                 !strcmp (cmethod->klass->name_space, "ObjCRuntime") &&
6787                                 !strcmp (cmethod->klass->name, "Selector"))
6788                            ) {
6789                 if (cfg->backend->have_objc_get_selector &&
6790                         !strcmp (cmethod->name, "GetHandle") && fsig->param_count == 1 &&
6791                     (args [0]->opcode == OP_GOT_ENTRY || args [0]->opcode == OP_AOTCONST) &&
6792                     cfg->compile_aot && !cfg->llvm_only) {
6793                         MonoInst *pi;
6794                         MonoJumpInfoToken *ji;
6795                         MonoString *s;
6796
6797                         // FIXME: llvmonly
6798
6799                         cfg->exception_message = g_strdup ("GetHandle");
6800                         cfg->disable_llvm = TRUE;
6801
6802                         if (args [0]->opcode == OP_GOT_ENTRY) {
6803                                 pi = (MonoInst *)args [0]->inst_p1;
6804                                 g_assert (pi->opcode == OP_PATCH_INFO);
6805                                 g_assert (GPOINTER_TO_INT (pi->inst_p1) == MONO_PATCH_INFO_LDSTR);
6806                                 ji = (MonoJumpInfoToken *)pi->inst_p0;
6807                         } else {
6808                                 g_assert (GPOINTER_TO_INT (args [0]->inst_p1) == MONO_PATCH_INFO_LDSTR);
6809                                 ji = (MonoJumpInfoToken *)args [0]->inst_p0;
6810                         }
6811
6812                         NULLIFY_INS (args [0]);
6813
6814                         // FIXME: Ugly
6815                         s = mono_ldstr (cfg->domain, ji->image, mono_metadata_token_index (ji->token));
6816                         MONO_INST_NEW (cfg, ins, OP_OBJC_GET_SELECTOR);
6817                         ins->dreg = mono_alloc_ireg (cfg);
6818                         // FIXME: Leaks
6819                         ins->inst_p0 = mono_string_to_utf8 (s);
6820                         MONO_ADD_INS (cfg->cbb, ins);
6821                         return ins;
6822                 }
6823         }
6824
6825 #ifdef MONO_ARCH_SIMD_INTRINSICS
6826         if (cfg->opt & MONO_OPT_SIMD) {
6827                 ins = mono_emit_simd_intrinsics (cfg, cmethod, fsig, args);
6828                 if (ins)
6829                         return ins;
6830         }
6831 #endif
6832
6833         ins = mono_emit_native_types_intrinsics (cfg, cmethod, fsig, args);
6834         if (ins)
6835                 return ins;
6836
6837         if (COMPILE_LLVM (cfg)) {
6838                 ins = llvm_emit_inst_for_method (cfg, cmethod, fsig, args);
6839                 if (ins)
6840                         return ins;
6841         }
6842
6843         return mono_arch_emit_inst_for_method (cfg, cmethod, fsig, args);
6844 }
6845
6846 /*
6847  * This entry point could be used later for arbitrary method
6848  * redirection.
6849  */
6850 inline static MonoInst*
6851 mini_redirect_call (MonoCompile *cfg, MonoMethod *method,  
6852                                         MonoMethodSignature *signature, MonoInst **args, MonoInst *this_ins)
6853 {
6854         if (method->klass == mono_defaults.string_class) {
6855                 /* managed string allocation support */
6856                 if (strcmp (method->name, "InternalAllocateStr") == 0 && !(mono_profiler_events & MONO_PROFILE_ALLOCATIONS) && !(cfg->opt & MONO_OPT_SHARED)) {
6857                         MonoInst *iargs [2];
6858                         MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
6859                         MonoMethod *managed_alloc = NULL;
6860
6861                         g_assert (vtable); /*Should not fail since it System.String*/
6862 #ifndef MONO_CROSS_COMPILE
6863                         managed_alloc = mono_gc_get_managed_allocator (method->klass, FALSE, FALSE);
6864 #endif
6865                         if (!managed_alloc)
6866                                 return NULL;
6867                         EMIT_NEW_VTABLECONST (cfg, iargs [0], vtable);
6868                         iargs [1] = args [0];
6869                         return mono_emit_method_call (cfg, managed_alloc, iargs, this_ins);
6870                 }
6871         }
6872         return NULL;
6873 }
6874
6875 static void
6876 mono_save_args (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **sp)
6877 {
6878         MonoInst *store, *temp;
6879         int i;
6880
6881         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
6882                 MonoType *argtype = (sig->hasthis && (i == 0)) ? type_from_stack_type (*sp) : sig->params [i - sig->hasthis];
6883
6884                 /*
6885                  * FIXME: We should use *args++ = sp [0], but that would mean the arg
6886                  * would be different than the MonoInst's used to represent arguments, and
6887                  * the ldelema implementation can't deal with that.
6888                  * Solution: When ldelema is used on an inline argument, create a var for 
6889                  * it, emit ldelema on that var, and emit the saving code below in
6890                  * inline_method () if needed.
6891                  */
6892                 temp = mono_compile_create_var (cfg, argtype, OP_LOCAL);
6893                 cfg->args [i] = temp;
6894                 /* This uses cfg->args [i] which is set by the preceeding line */
6895                 EMIT_NEW_ARGSTORE (cfg, store, i, *sp);
6896                 store->cil_code = sp [0]->cil_code;
6897                 sp++;
6898         }
6899 }
6900
6901 #define MONO_INLINE_CALLED_LIMITED_METHODS 1
6902 #define MONO_INLINE_CALLER_LIMITED_METHODS 1
6903
6904 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
6905 static gboolean
6906 check_inline_called_method_name_limit (MonoMethod *called_method)
6907 {
6908         int strncmp_result;
6909         static const char *limit = NULL;
6910         
6911         if (limit == NULL) {
6912                 const char *limit_string = g_getenv ("MONO_INLINE_CALLED_METHOD_NAME_LIMIT");
6913
6914                 if (limit_string != NULL)
6915                         limit = limit_string;
6916                 else
6917                         limit = "";
6918         }
6919
6920         if (limit [0] != '\0') {
6921                 char *called_method_name = mono_method_full_name (called_method, TRUE);
6922
6923                 strncmp_result = strncmp (called_method_name, limit, strlen (limit));
6924                 g_free (called_method_name);
6925         
6926                 //return (strncmp_result <= 0);
6927                 return (strncmp_result == 0);
6928         } else {
6929                 return TRUE;
6930         }
6931 }
6932 #endif
6933
6934 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
6935 static gboolean
6936 check_inline_caller_method_name_limit (MonoMethod *caller_method)
6937 {
6938         int strncmp_result;
6939         static const char *limit = NULL;
6940         
6941         if (limit == NULL) {
6942                 const char *limit_string = g_getenv ("MONO_INLINE_CALLER_METHOD_NAME_LIMIT");
6943                 if (limit_string != NULL) {
6944                         limit = limit_string;
6945                 } else {
6946                         limit = "";
6947                 }
6948         }
6949
6950         if (limit [0] != '\0') {
6951                 char *caller_method_name = mono_method_full_name (caller_method, TRUE);
6952
6953                 strncmp_result = strncmp (caller_method_name, limit, strlen (limit));
6954                 g_free (caller_method_name);
6955         
6956                 //return (strncmp_result <= 0);
6957                 return (strncmp_result == 0);
6958         } else {
6959                 return TRUE;
6960         }
6961 }
6962 #endif
6963
6964 static void
6965 emit_init_rvar (MonoCompile *cfg, int dreg, MonoType *rtype)
6966 {
6967         static double r8_0 = 0.0;
6968         static float r4_0 = 0.0;
6969         MonoInst *ins;
6970         int t;
6971
6972         rtype = mini_get_underlying_type (rtype);
6973         t = rtype->type;
6974
6975         if (rtype->byref) {
6976                 MONO_EMIT_NEW_PCONST (cfg, dreg, NULL);
6977         } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
6978                 MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
6979         } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
6980                 MONO_EMIT_NEW_I8CONST (cfg, dreg, 0);
6981         } else if (cfg->r4fp && t == MONO_TYPE_R4) {
6982                 MONO_INST_NEW (cfg, ins, OP_R4CONST);
6983                 ins->type = STACK_R4;
6984                 ins->inst_p0 = (void*)&r4_0;
6985                 ins->dreg = dreg;
6986                 MONO_ADD_INS (cfg->cbb, ins);
6987         } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
6988                 MONO_INST_NEW (cfg, ins, OP_R8CONST);
6989                 ins->type = STACK_R8;
6990                 ins->inst_p0 = (void*)&r8_0;
6991                 ins->dreg = dreg;
6992                 MONO_ADD_INS (cfg->cbb, ins);
6993         } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
6994                    ((t == MONO_TYPE_GENERICINST) && mono_type_generic_inst_is_valuetype (rtype))) {
6995                 MONO_EMIT_NEW_VZERO (cfg, dreg, mono_class_from_mono_type (rtype));
6996         } else if (((t == MONO_TYPE_VAR) || (t == MONO_TYPE_MVAR)) && mini_type_var_is_vt (rtype)) {
6997                 MONO_EMIT_NEW_VZERO (cfg, dreg, mono_class_from_mono_type (rtype));
6998         } else {
6999                 MONO_EMIT_NEW_PCONST (cfg, dreg, NULL);
7000         }
7001 }
7002
7003 static void
7004 emit_dummy_init_rvar (MonoCompile *cfg, int dreg, MonoType *rtype)
7005 {
7006         int t;
7007
7008         rtype = mini_get_underlying_type (rtype);
7009         t = rtype->type;
7010
7011         if (rtype->byref) {
7012                 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_PCONST);
7013         } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
7014                 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_ICONST);
7015         } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
7016                 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_I8CONST);
7017         } else if (cfg->r4fp && t == MONO_TYPE_R4) {
7018                 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_R4CONST);
7019         } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
7020                 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_R8CONST);
7021         } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
7022                    ((t == MONO_TYPE_GENERICINST) && mono_type_generic_inst_is_valuetype (rtype))) {
7023                 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_VZERO);
7024         } else if (((t == MONO_TYPE_VAR) || (t == MONO_TYPE_MVAR)) && mini_type_var_is_vt (rtype)) {
7025                 MONO_EMIT_NEW_DUMMY_INIT (cfg, dreg, OP_DUMMY_VZERO);
7026         } else {
7027                 emit_init_rvar (cfg, dreg, rtype);
7028         }
7029 }
7030
7031 /* If INIT is FALSE, emit dummy initialization statements to keep the IR valid */
7032 static void
7033 emit_init_local (MonoCompile *cfg, int local, MonoType *type, gboolean init)
7034 {
7035         MonoInst *var = cfg->locals [local];
7036         if (COMPILE_SOFT_FLOAT (cfg)) {
7037                 MonoInst *store;
7038                 int reg = alloc_dreg (cfg, (MonoStackType)var->type);
7039                 emit_init_rvar (cfg, reg, type);
7040                 EMIT_NEW_LOCSTORE (cfg, store, local, cfg->cbb->last_ins);
7041         } else {
7042                 if (init)
7043                         emit_init_rvar (cfg, var->dreg, type);
7044                 else
7045                         emit_dummy_init_rvar (cfg, var->dreg, type);
7046         }
7047 }
7048
7049 /*
7050  * inline_method:
7051  *
7052  *   Return the cost of inlining CMETHOD.
7053  */
7054 static int
7055 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **sp,
7056                            guchar *ip, guint real_offset, gboolean inline_always)
7057 {
7058         MonoError error;
7059         MonoInst *ins, *rvar = NULL;
7060         MonoMethodHeader *cheader;
7061         MonoBasicBlock *ebblock, *sbblock;
7062         int i, costs;
7063         MonoMethod *prev_inlined_method;
7064         MonoInst **prev_locals, **prev_args;
7065         MonoType **prev_arg_types;
7066         guint prev_real_offset;
7067         GHashTable *prev_cbb_hash;
7068         MonoBasicBlock **prev_cil_offset_to_bb;
7069         MonoBasicBlock *prev_cbb;
7070         unsigned char* prev_cil_start;
7071         guint32 prev_cil_offset_to_bb_len;
7072         MonoMethod *prev_current_method;
7073         MonoGenericContext *prev_generic_context;
7074         gboolean ret_var_set, prev_ret_var_set, prev_disable_inline, virtual_ = FALSE;
7075
7076         g_assert (cfg->exception_type == MONO_EXCEPTION_NONE);
7077
7078 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
7079         if ((! inline_always) && ! check_inline_called_method_name_limit (cmethod))
7080                 return 0;
7081 #endif
7082 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
7083         if ((! inline_always) && ! check_inline_caller_method_name_limit (cfg->method))
7084                 return 0;
7085 #endif
7086
7087         if (!fsig)
7088                 fsig = mono_method_signature (cmethod);
7089
7090         if (cfg->verbose_level > 2)
7091                 printf ("INLINE START %p %s -> %s\n", cmethod,  mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
7092
7093         if (!cmethod->inline_info) {
7094                 cfg->stat_inlineable_methods++;
7095                 cmethod->inline_info = 1;
7096         }
7097
7098         /* allocate local variables */
7099         cheader = mono_method_get_header_checked (cmethod, &error);
7100         if (!cheader) {
7101                 if (inline_always) {
7102                         mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
7103                         mono_error_move (&cfg->error, &error);
7104                 } else {
7105                         mono_error_cleanup (&error);
7106                 }
7107                 return 0;
7108         }
7109
7110         /*Must verify before creating locals as it can cause the JIT to assert.*/
7111         if (mono_compile_is_broken (cfg, cmethod, FALSE)) {
7112                 mono_metadata_free_mh (cheader);
7113                 return 0;
7114         }
7115
7116         /* allocate space to store the return value */
7117         if (!MONO_TYPE_IS_VOID (fsig->ret)) {
7118                 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
7119         }
7120
7121         prev_locals = cfg->locals;
7122         cfg->locals = (MonoInst **)mono_mempool_alloc0 (cfg->mempool, cheader->num_locals * sizeof (MonoInst*));
7123         for (i = 0; i < cheader->num_locals; ++i)
7124                 cfg->locals [i] = mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
7125
7126         /* allocate start and end blocks */
7127         /* This is needed so if the inline is aborted, we can clean up */
7128         NEW_BBLOCK (cfg, sbblock);
7129         sbblock->real_offset = real_offset;
7130
7131         NEW_BBLOCK (cfg, ebblock);
7132         ebblock->block_num = cfg->num_bblocks++;
7133         ebblock->real_offset = real_offset;
7134
7135         prev_args = cfg->args;
7136         prev_arg_types = cfg->arg_types;
7137         prev_inlined_method = cfg->inlined_method;
7138         cfg->inlined_method = cmethod;
7139         cfg->ret_var_set = FALSE;
7140         cfg->inline_depth ++;
7141         prev_real_offset = cfg->real_offset;
7142         prev_cbb_hash = cfg->cbb_hash;
7143         prev_cil_offset_to_bb = cfg->cil_offset_to_bb;
7144         prev_cil_offset_to_bb_len = cfg->cil_offset_to_bb_len;
7145         prev_cil_start = cfg->cil_start;
7146         prev_cbb = cfg->cbb;
7147         prev_current_method = cfg->current_method;
7148         prev_generic_context = cfg->generic_context;
7149         prev_ret_var_set = cfg->ret_var_set;
7150         prev_disable_inline = cfg->disable_inline;
7151
7152         if (ip && *ip == CEE_CALLVIRT && !(cmethod->flags & METHOD_ATTRIBUTE_STATIC))
7153                 virtual_ = TRUE;
7154
7155         costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, rvar, sp, real_offset, virtual_);
7156
7157         ret_var_set = cfg->ret_var_set;
7158
7159         cfg->inlined_method = prev_inlined_method;
7160         cfg->real_offset = prev_real_offset;
7161         cfg->cbb_hash = prev_cbb_hash;
7162         cfg->cil_offset_to_bb = prev_cil_offset_to_bb;
7163         cfg->cil_offset_to_bb_len = prev_cil_offset_to_bb_len;
7164         cfg->cil_start = prev_cil_start;
7165         cfg->locals = prev_locals;
7166         cfg->args = prev_args;
7167         cfg->arg_types = prev_arg_types;
7168         cfg->current_method = prev_current_method;
7169         cfg->generic_context = prev_generic_context;
7170         cfg->ret_var_set = prev_ret_var_set;
7171         cfg->disable_inline = prev_disable_inline;
7172         cfg->inline_depth --;
7173
7174         if ((costs >= 0 && costs < 60) || inline_always || (costs >= 0 && (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_AGGRESSIVE_INLINING))) {
7175                 if (cfg->verbose_level > 2)
7176                         printf ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
7177                 
7178                 cfg->stat_inlined_methods++;
7179
7180                 /* always add some code to avoid block split failures */
7181                 MONO_INST_NEW (cfg, ins, OP_NOP);
7182                 MONO_ADD_INS (prev_cbb, ins);
7183
7184                 prev_cbb->next_bb = sbblock;
7185                 link_bblock (cfg, prev_cbb, sbblock);
7186
7187                 /* 
7188                  * Get rid of the begin and end bblocks if possible to aid local
7189                  * optimizations.
7190                  */
7191                 mono_merge_basic_blocks (cfg, prev_cbb, sbblock);
7192
7193                 if ((prev_cbb->out_count == 1) && (prev_cbb->out_bb [0]->in_count == 1) && (prev_cbb->out_bb [0] != ebblock))
7194                         mono_merge_basic_blocks (cfg, prev_cbb, prev_cbb->out_bb [0]);
7195
7196                 if ((ebblock->in_count == 1) && ebblock->in_bb [0]->out_count == 1) {
7197                         MonoBasicBlock *prev = ebblock->in_bb [0];
7198
7199                         if (prev->next_bb == ebblock) {
7200                                 mono_merge_basic_blocks (cfg, prev, ebblock);
7201                                 cfg->cbb = prev;
7202                                 if ((prev_cbb->out_count == 1) && (prev_cbb->out_bb [0]->in_count == 1) && (prev_cbb->out_bb [0] == prev)) {
7203                                         mono_merge_basic_blocks (cfg, prev_cbb, prev);
7204                                         cfg->cbb = prev_cbb;
7205                                 }
7206                         } else {
7207                                 /* There could be a bblock after 'prev', and making 'prev' the current bb could cause problems */
7208                                 cfg->cbb = ebblock;
7209                         }
7210                 } else {
7211                         /* 
7212                          * Its possible that the rvar is set in some prev bblock, but not in others.
7213                          * (#1835).
7214                          */
7215                         if (rvar) {
7216                                 MonoBasicBlock *bb;
7217
7218                                 for (i = 0; i < ebblock->in_count; ++i) {
7219                                         bb = ebblock->in_bb [i];
7220
7221                                         if (bb->last_ins && bb->last_ins->opcode == OP_NOT_REACHED) {
7222                                                 cfg->cbb = bb;
7223
7224                                                 emit_init_rvar (cfg, rvar->dreg, fsig->ret);
7225                                         }
7226                                 }
7227                         }
7228
7229                         cfg->cbb = ebblock;
7230                 }
7231
7232                 if (rvar) {
7233                         /*
7234                          * If the inlined method contains only a throw, then the ret var is not 
7235                          * set, so set it to a dummy value.
7236                          */
7237                         if (!ret_var_set)
7238                                 emit_init_rvar (cfg, rvar->dreg, fsig->ret);
7239
7240                         EMIT_NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
7241                         *sp++ = ins;
7242                 }
7243                 cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, cheader);
7244                 return costs + 1;
7245         } else {
7246                 if (cfg->verbose_level > 2)
7247                         printf ("INLINE ABORTED %s (cost %d)\n", mono_method_full_name (cmethod, TRUE), costs);
7248                 cfg->exception_type = MONO_EXCEPTION_NONE;
7249                 mono_loader_clear_error ();
7250
7251                 /* This gets rid of the newly added bblocks */
7252                 cfg->cbb = prev_cbb;
7253         }
7254         cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, cheader);
7255         return 0;
7256 }
7257
7258 /*
7259  * Some of these comments may well be out-of-date.
7260  * Design decisions: we do a single pass over the IL code (and we do bblock 
7261  * splitting/merging in the few cases when it's required: a back jump to an IL
7262  * address that was not already seen as bblock starting point).
7263  * Code is validated as we go (full verification is still better left to metadata/verify.c).
7264  * Complex operations are decomposed in simpler ones right away. We need to let the 
7265  * arch-specific code peek and poke inside this process somehow (except when the 
7266  * optimizations can take advantage of the full semantic info of coarse opcodes).
7267  * All the opcodes of the form opcode.s are 'normalized' to opcode.
7268  * MonoInst->opcode initially is the IL opcode or some simplification of that 
7269  * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific 
7270  * opcode with value bigger than OP_LAST.
7271  * At this point the IR can be handed over to an interpreter, a dumb code generator
7272  * or to the optimizing code generator that will translate it to SSA form.
7273  *
7274  * Profiling directed optimizations.
7275  * We may compile by default with few or no optimizations and instrument the code
7276  * or the user may indicate what methods to optimize the most either in a config file
7277  * or through repeated runs where the compiler applies offline the optimizations to 
7278  * each method and then decides if it was worth it.
7279  */
7280
7281 #define CHECK_TYPE(ins) if (!(ins)->type) UNVERIFIED
7282 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) UNVERIFIED
7283 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) UNVERIFIED
7284 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) UNVERIFIED
7285 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) UNVERIFIED
7286 #define CHECK_OPSIZE(size) if (ip + size > end) UNVERIFIED
7287 #define CHECK_UNVERIFIABLE(cfg) if (cfg->unverifiable) UNVERIFIED
7288 #define CHECK_TYPELOAD(klass) if (!(klass) || mono_class_has_failure (klass)) TYPE_LOAD_ERROR ((klass))
7289
7290 /* offset from br.s -> br like opcodes */
7291 #define BIG_BRANCH_OFFSET 13
7292
7293 static gboolean
7294 ip_in_bb (MonoCompile *cfg, MonoBasicBlock *bb, const guint8* ip)
7295 {
7296         MonoBasicBlock *b = cfg->cil_offset_to_bb [ip - cfg->cil_start];
7297
7298         return b == NULL || b == bb;
7299 }
7300
7301 static int
7302 get_basic_blocks (MonoCompile *cfg, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
7303 {
7304         unsigned char *ip = start;
7305         unsigned char *target;
7306         int i;
7307         guint cli_addr;
7308         MonoBasicBlock *bblock;
7309         const MonoOpcode *opcode;
7310
7311         while (ip < end) {
7312                 cli_addr = ip - start;
7313                 i = mono_opcode_value ((const guint8 **)&ip, end);
7314                 if (i < 0)
7315                         UNVERIFIED;
7316                 opcode = &mono_opcodes [i];
7317                 switch (opcode->argument) {
7318                 case MonoInlineNone:
7319                         ip++; 
7320                         break;
7321                 case MonoInlineString:
7322                 case MonoInlineType:
7323                 case MonoInlineField:
7324                 case MonoInlineMethod:
7325                 case MonoInlineTok:
7326                 case MonoInlineSig:
7327                 case MonoShortInlineR:
7328                 case MonoInlineI:
7329                         ip += 5;
7330                         break;
7331                 case MonoInlineVar:
7332                         ip += 3;
7333                         break;
7334                 case MonoShortInlineVar:
7335                 case MonoShortInlineI:
7336                         ip += 2;
7337                         break;
7338                 case MonoShortInlineBrTarget:
7339                         target = start + cli_addr + 2 + (signed char)ip [1];
7340                         GET_BBLOCK (cfg, bblock, target);
7341                         ip += 2;
7342                         if (ip < end)
7343                                 GET_BBLOCK (cfg, bblock, ip);
7344                         break;
7345                 case MonoInlineBrTarget:
7346                         target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
7347                         GET_BBLOCK (cfg, bblock, target);
7348                         ip += 5;
7349                         if (ip < end)
7350                                 GET_BBLOCK (cfg, bblock, ip);
7351                         break;
7352                 case MonoInlineSwitch: {
7353                         guint32 n = read32 (ip + 1);
7354                         guint32 j;
7355                         ip += 5;
7356                         cli_addr += 5 + 4 * n;
7357                         target = start + cli_addr;
7358                         GET_BBLOCK (cfg, bblock, target);
7359                         
7360                         for (j = 0; j < n; ++j) {
7361                                 target = start + cli_addr + (gint32)read32 (ip);
7362                                 GET_BBLOCK (cfg, bblock, target);
7363                                 ip += 4;
7364                         }
7365                         break;
7366                 }
7367                 case MonoInlineR:
7368                 case MonoInlineI8:
7369                         ip += 9;
7370                         break;
7371                 default:
7372                         g_assert_not_reached ();
7373                 }
7374
7375                 if (i == CEE_THROW) {
7376                         unsigned char *bb_start = ip - 1;
7377                         
7378                         /* Find the start of the bblock containing the throw */
7379                         bblock = NULL;
7380                         while ((bb_start >= start) && !bblock) {
7381                                 bblock = cfg->cil_offset_to_bb [(bb_start) - start];
7382                                 bb_start --;
7383                         }
7384                         if (bblock)
7385                                 bblock->out_of_line = 1;
7386                 }
7387         }
7388         return 0;
7389 unverified:
7390 exception_exit:
7391         *pos = ip;
7392         return 1;
7393 }
7394
7395 static inline MonoMethod *
7396 mini_get_method_allow_open (MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context, MonoError *error)
7397 {
7398         MonoMethod *method;
7399
7400         mono_error_init (error);
7401
7402         if (m->wrapper_type != MONO_WRAPPER_NONE) {
7403                 method = (MonoMethod *)mono_method_get_wrapper_data (m, token);
7404                 if (context) {
7405                         method = mono_class_inflate_generic_method_checked (method, context, error);
7406                 }
7407         } else {
7408                 method = mono_get_method_checked (m->klass->image, token, klass, context, error);
7409         }
7410
7411         return method;
7412 }
7413
7414 static inline MonoMethod *
7415 mini_get_method (MonoCompile *cfg, MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context)
7416 {
7417         MonoError error;
7418         MonoMethod *method = mini_get_method_allow_open (m, token, klass, context, cfg ? &cfg->error : &error);
7419
7420         if (method && cfg && !cfg->gshared && mono_class_is_open_constructed_type (&method->klass->byval_arg)) {
7421                 mono_error_set_bad_image (&cfg->error, cfg->method->klass->image, "Method with open type while not compiling gshared");
7422                 method = NULL;
7423         }
7424
7425         if (!method && !cfg)
7426                 mono_error_cleanup (&error); /* FIXME don't swallow the error */
7427
7428         return method;
7429 }
7430
7431 static inline MonoClass*
7432 mini_get_class (MonoMethod *method, guint32 token, MonoGenericContext *context)
7433 {
7434         MonoError error;
7435         MonoClass *klass;
7436
7437         if (method->wrapper_type != MONO_WRAPPER_NONE) {
7438                 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
7439                 if (context) {
7440                         klass = mono_class_inflate_generic_class_checked (klass, context, &error);
7441                         mono_error_cleanup (&error); /* FIXME don't swallow the error */
7442                 }
7443         } else {
7444                 klass = mono_class_get_and_inflate_typespec_checked (method->klass->image, token, context, &error);
7445                 mono_error_cleanup (&error); /* FIXME don't swallow the error */
7446         }
7447         if (klass)
7448                 mono_class_init (klass);
7449         return klass;
7450 }
7451
7452 static inline MonoMethodSignature*
7453 mini_get_signature (MonoMethod *method, guint32 token, MonoGenericContext *context)
7454 {
7455         MonoMethodSignature *fsig;
7456
7457         if (method->wrapper_type != MONO_WRAPPER_NONE) {
7458                 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
7459         } else {
7460                 fsig = mono_metadata_parse_signature (method->klass->image, token);
7461         }
7462         if (context) {
7463                 MonoError error;
7464                 fsig = mono_inflate_generic_signature(fsig, context, &error);
7465                 // FIXME:
7466                 g_assert(mono_error_ok(&error));
7467         }
7468         return fsig;
7469 }
7470
7471 static MonoMethod*
7472 throw_exception (void)
7473 {
7474         static MonoMethod *method = NULL;
7475
7476         if (!method) {
7477                 MonoSecurityManager *secman = mono_security_manager_get_methods ();
7478                 method = mono_class_get_method_from_name (secman->securitymanager, "ThrowException", 1);
7479         }
7480         g_assert (method);
7481         return method;
7482 }
7483
7484 static void
7485 emit_throw_exception (MonoCompile *cfg, MonoException *ex)
7486 {
7487         MonoMethod *thrower = throw_exception ();
7488         MonoInst *args [1];
7489
7490         EMIT_NEW_PCONST (cfg, args [0], ex);
7491         mono_emit_method_call (cfg, thrower, args, NULL);
7492 }
7493
7494 /*
7495  * Return the original method is a wrapper is specified. We can only access 
7496  * the custom attributes from the original method.
7497  */
7498 static MonoMethod*
7499 get_original_method (MonoMethod *method)
7500 {
7501         if (method->wrapper_type == MONO_WRAPPER_NONE)
7502                 return method;
7503
7504         /* native code (which is like Critical) can call any managed method XXX FIXME XXX to validate all usages */
7505         if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED)
7506                 return NULL;
7507
7508         /* in other cases we need to find the original method */
7509         return mono_marshal_method_from_wrapper (method);
7510 }
7511
7512 static void
7513 ensure_method_is_allowed_to_access_field (MonoCompile *cfg, MonoMethod *caller, MonoClassField *field)
7514 {
7515         /* we can't get the coreclr security level on wrappers since they don't have the attributes */
7516         MonoException *ex = mono_security_core_clr_is_field_access_allowed (get_original_method (caller), field);
7517         if (ex)
7518                 emit_throw_exception (cfg, ex);
7519 }
7520
7521 static void
7522 ensure_method_is_allowed_to_call_method (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee)
7523 {
7524         /* we can't get the coreclr security level on wrappers since they don't have the attributes */
7525         MonoException *ex = mono_security_core_clr_is_call_allowed (get_original_method (caller), callee);
7526         if (ex)
7527                 emit_throw_exception (cfg, ex);
7528 }
7529
7530 /*
7531  * Check that the IL instructions at ip are the array initialization
7532  * sequence and return the pointer to the data and the size.
7533  */
7534 static const char*
7535 initialize_array_data (MonoMethod *method, gboolean aot, unsigned char *ip, MonoClass *klass, guint32 len, int *out_size, guint32 *out_field_token)
7536 {
7537         /*
7538          * newarr[System.Int32]
7539          * dup
7540          * ldtoken field valuetype ...
7541          * call void class [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(class [mscorlib]System.Array, valuetype [mscorlib]System.RuntimeFieldHandle)
7542          */
7543         if (ip [0] == CEE_DUP && ip [1] == CEE_LDTOKEN && ip [5] == 0x4 && ip [6] == CEE_CALL) {
7544                 MonoError error;
7545                 guint32 token = read32 (ip + 7);
7546                 guint32 field_token = read32 (ip + 2);
7547                 guint32 field_index = field_token & 0xffffff;
7548                 guint32 rva;
7549                 const char *data_ptr;
7550                 int size = 0;
7551                 MonoMethod *cmethod;
7552                 MonoClass *dummy_class;
7553                 MonoClassField *field = mono_field_from_token_checked (method->klass->image, field_token, &dummy_class, NULL, &error);
7554                 int dummy_align;
7555
7556                 if (!field) {
7557                         mono_error_cleanup (&error); /* FIXME don't swallow the error */
7558                         return NULL;
7559                 }
7560
7561                 *out_field_token = field_token;
7562
7563                 cmethod = mini_get_method (NULL, method, token, NULL, NULL);
7564                 if (!cmethod)
7565                         return NULL;
7566                 if (strcmp (cmethod->name, "InitializeArray") || strcmp (cmethod->klass->name, "RuntimeHelpers") || cmethod->klass->image != mono_defaults.corlib)
7567                         return NULL;
7568                 switch (mono_type_get_underlying_type (&klass->byval_arg)->type) {
7569                 case MONO_TYPE_BOOLEAN:
7570                 case MONO_TYPE_I1:
7571                 case MONO_TYPE_U1:
7572                         size = 1; break;
7573                 /* we need to swap on big endian, so punt. Should we handle R4 and R8 as well? */
7574 #if TARGET_BYTE_ORDER == G_LITTLE_ENDIAN
7575                 case MONO_TYPE_CHAR:
7576                 case MONO_TYPE_I2:
7577                 case MONO_TYPE_U2:
7578                         size = 2; break;
7579                 case MONO_TYPE_I4:
7580                 case MONO_TYPE_U4:
7581                 case MONO_TYPE_R4:
7582                         size = 4; break;
7583                 case MONO_TYPE_R8:
7584                 case MONO_TYPE_I8:
7585                 case MONO_TYPE_U8:
7586                         size = 8; break;
7587 #endif
7588                 default:
7589                         return NULL;
7590                 }
7591                 size *= len;
7592                 if (size > mono_type_size (field->type, &dummy_align))
7593                     return NULL;
7594                 *out_size = size;
7595                 /*g_print ("optimized in %s: size: %d, numelems: %d\n", method->name, size, newarr->inst_newa_len->inst_c0);*/
7596                 if (!image_is_dynamic (method->klass->image)) {
7597                         field_index = read32 (ip + 2) & 0xffffff;
7598                         mono_metadata_field_info (method->klass->image, field_index - 1, NULL, &rva, NULL);
7599                         data_ptr = mono_image_rva_map (method->klass->image, rva);
7600                         /*g_print ("field: 0x%08x, rva: %d, rva_ptr: %p\n", read32 (ip + 2), rva, data_ptr);*/
7601                         /* for aot code we do the lookup on load */
7602                         if (aot && data_ptr)
7603                                 return (const char *)GUINT_TO_POINTER (rva);
7604                 } else {
7605                         /*FIXME is it possible to AOT a SRE assembly not meant to be saved? */ 
7606                         g_assert (!aot);
7607                         data_ptr = mono_field_get_data (field);
7608                 }
7609                 return data_ptr;
7610         }
7611         return NULL;
7612 }
7613
7614 static void
7615 set_exception_type_from_invalid_il (MonoCompile *cfg, MonoMethod *method, unsigned char *ip)
7616 {
7617         MonoError error;
7618         char *method_fname = mono_method_full_name (method, TRUE);
7619         char *method_code;
7620         MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
7621
7622         if (!header) {
7623                 method_code = g_strdup_printf ("could not parse method body due to %s", mono_error_get_message (&error));
7624                 mono_error_cleanup (&error);
7625         } else if (header->code_size == 0)
7626                 method_code = g_strdup ("method body is empty.");
7627         else
7628                 method_code = mono_disasm_code_one (NULL, method, ip, NULL);
7629         mono_cfg_set_exception_invalid_program (cfg, g_strdup_printf ("Invalid IL code in %s: %s\n", method_fname, method_code));
7630         g_free (method_fname);
7631         g_free (method_code);
7632         cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, header);
7633 }
7634
7635 static void
7636 emit_stloc_ir (MonoCompile *cfg, MonoInst **sp, MonoMethodHeader *header, int n)
7637 {
7638         MonoInst *ins;
7639         guint32 opcode = mono_type_to_regmove (cfg, header->locals [n]);
7640         if ((opcode == OP_MOVE) && cfg->cbb->last_ins == sp [0]  &&
7641                         ((sp [0]->opcode == OP_ICONST) || (sp [0]->opcode == OP_I8CONST))) {
7642                 /* Optimize reg-reg moves away */
7643                 /* 
7644                  * Can't optimize other opcodes, since sp[0] might point to
7645                  * the last ins of a decomposed opcode.
7646                  */
7647                 sp [0]->dreg = (cfg)->locals [n]->dreg;
7648         } else {
7649                 EMIT_NEW_LOCSTORE (cfg, ins, n, *sp);
7650         }
7651 }
7652
7653 /*
7654  * ldloca inhibits many optimizations so try to get rid of it in common
7655  * cases.
7656  */
7657 static inline unsigned char *
7658 emit_optimized_ldloca_ir (MonoCompile *cfg, unsigned char *ip, unsigned char *end, int size)
7659 {
7660         int local, token;
7661         MonoClass *klass;
7662         MonoType *type;
7663
7664         if (size == 1) {
7665                 local = ip [1];
7666                 ip += 2;
7667         } else {
7668                 local = read16 (ip + 2);
7669                 ip += 4;
7670         }
7671         
7672         if (ip + 6 < end && (ip [0] == CEE_PREFIX1) && (ip [1] == CEE_INITOBJ) && ip_in_bb (cfg, cfg->cbb, ip + 1)) {
7673                 /* From the INITOBJ case */
7674                 token = read32 (ip + 2);
7675                 klass = mini_get_class (cfg->current_method, token, cfg->generic_context);
7676                 CHECK_TYPELOAD (klass);
7677                 type = mini_get_underlying_type (&klass->byval_arg);
7678                 emit_init_local (cfg, local, type, TRUE);
7679                 return ip + 6;
7680         }
7681  exception_exit:
7682         return NULL;
7683 }
7684
7685 static MonoInst*
7686 emit_llvmonly_virtual_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, int context_used, MonoInst **sp)
7687 {
7688         MonoInst *icall_args [16];
7689         MonoInst *call_target, *ins, *vtable_ins;
7690         int arg_reg, this_reg, vtable_reg;
7691         gboolean is_iface = cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE;
7692         gboolean is_gsharedvt = cfg->gsharedvt && mini_is_gsharedvt_variable_signature (fsig);
7693         gboolean variant_iface = FALSE;
7694         guint32 slot;
7695         int offset;
7696
7697         /*
7698          * In llvm-only mode, vtables contain function descriptors instead of
7699          * method addresses/trampolines.
7700          */
7701         MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
7702
7703         if (is_iface)
7704                 slot = mono_method_get_imt_slot (cmethod);
7705         else
7706                 slot = mono_method_get_vtable_index (cmethod);
7707
7708         this_reg = sp [0]->dreg;
7709
7710         if (is_iface && mono_class_has_variant_generic_params (cmethod->klass))
7711                 variant_iface = TRUE;
7712
7713         if (!fsig->generic_param_count && !is_iface && !is_gsharedvt) {
7714                 /*
7715                  * The simplest case, a normal virtual call.
7716                  */
7717                 int slot_reg = alloc_preg (cfg);
7718                 int addr_reg = alloc_preg (cfg);
7719                 int arg_reg = alloc_preg (cfg);
7720                 MonoBasicBlock *non_null_bb;
7721
7722                 vtable_reg = alloc_preg (cfg);
7723                 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_ins, OP_LOAD_MEMBASE, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
7724                 offset = MONO_STRUCT_OFFSET (MonoVTable, vtable) + (slot * SIZEOF_VOID_P);
7725
7726                 /* Load the vtable slot, which contains a function descriptor. */
7727                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, slot_reg, vtable_reg, offset);
7728
7729                 NEW_BBLOCK (cfg, non_null_bb);
7730
7731                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, slot_reg, 0);
7732                 cfg->cbb->last_ins->flags |= MONO_INST_LIKELY;
7733                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, non_null_bb);
7734
7735                 /* Slow path */
7736                 // FIXME: Make the wrapper use the preserveall cconv
7737                 // FIXME: Use one icall per slot for small slot numbers ?
7738                 icall_args [0] = vtable_ins;
7739                 EMIT_NEW_ICONST (cfg, icall_args [1], slot);
7740                 /* Make the icall return the vtable slot value to save some code space */
7741                 ins = mono_emit_jit_icall (cfg, mono_init_vtable_slot, icall_args);
7742                 ins->dreg = slot_reg;
7743                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, non_null_bb);
7744
7745                 /* Fastpath */
7746                 MONO_START_BB (cfg, non_null_bb);
7747                 /* Load the address + arg from the vtable slot */
7748                 EMIT_NEW_LOAD_MEMBASE (cfg, call_target, OP_LOAD_MEMBASE, addr_reg, slot_reg, 0);
7749                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, arg_reg, slot_reg, SIZEOF_VOID_P);
7750
7751                 return emit_extra_arg_calli (cfg, fsig, sp, arg_reg, call_target);
7752         }
7753
7754         if (!fsig->generic_param_count && is_iface && !variant_iface && !is_gsharedvt) {
7755                 /*
7756                  * A simple interface call
7757                  *
7758                  * We make a call through an imt slot to obtain the function descriptor we need to call.
7759                  * The imt slot contains a function descriptor for a runtime function + arg.
7760                  */
7761                 int slot_reg = alloc_preg (cfg);
7762                 int addr_reg = alloc_preg (cfg);
7763                 int arg_reg = alloc_preg (cfg);
7764                 MonoInst *thunk_addr_ins, *thunk_arg_ins, *ftndesc_ins;
7765
7766                 vtable_reg = alloc_preg (cfg);
7767                 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_ins, OP_LOAD_MEMBASE, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
7768                 offset = ((gint32)slot - MONO_IMT_SIZE) * SIZEOF_VOID_P;
7769
7770                 /*
7771                  * The slot is already initialized when the vtable is created so there is no need
7772                  * to check it here.
7773                  */
7774
7775                 /* Load the imt slot, which contains a function descriptor. */
7776                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, slot_reg, vtable_reg, offset);
7777
7778                 /* Load the address + arg of the imt thunk from the imt slot */
7779                 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_addr_ins, OP_LOAD_MEMBASE, addr_reg, slot_reg, 0);
7780                 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_arg_ins, OP_LOAD_MEMBASE, arg_reg, slot_reg, SIZEOF_VOID_P);
7781                 /*
7782                  * IMT thunks in llvm-only mode are C functions which take an info argument
7783                  * plus the imt method and return the ftndesc to call.
7784                  */
7785                 icall_args [0] = thunk_arg_ins;
7786                 icall_args [1] = emit_get_rgctx_method (cfg, context_used,
7787                                                                                                 cmethod, MONO_RGCTX_INFO_METHOD);
7788                 ftndesc_ins = mono_emit_calli (cfg, helper_sig_llvmonly_imt_thunk, icall_args, thunk_addr_ins, NULL, NULL);
7789
7790                 return emit_llvmonly_calli (cfg, fsig, sp, ftndesc_ins);
7791         }
7792
7793         if ((fsig->generic_param_count || variant_iface) && !is_gsharedvt) {
7794                 /*
7795                  * This is similar to the interface case, the vtable slot points to an imt thunk which is
7796                  * dynamically extended as more instantiations are discovered.
7797                  * This handles generic virtual methods both on classes and interfaces.
7798                  */
7799                 int slot_reg = alloc_preg (cfg);
7800                 int addr_reg = alloc_preg (cfg);
7801                 int arg_reg = alloc_preg (cfg);
7802                 int ftndesc_reg = alloc_preg (cfg);
7803                 MonoInst *thunk_addr_ins, *thunk_arg_ins, *ftndesc_ins;
7804                 MonoBasicBlock *slowpath_bb, *end_bb;
7805
7806                 NEW_BBLOCK (cfg, slowpath_bb);
7807                 NEW_BBLOCK (cfg, end_bb);
7808
7809                 vtable_reg = alloc_preg (cfg);
7810                 EMIT_NEW_LOAD_MEMBASE (cfg, vtable_ins, OP_LOAD_MEMBASE, vtable_reg, this_reg, MONO_STRUCT_OFFSET (MonoObject, vtable));
7811                 if (is_iface)
7812                         offset = ((gint32)slot - MONO_IMT_SIZE) * SIZEOF_VOID_P;
7813                 else
7814                         offset = MONO_STRUCT_OFFSET (MonoVTable, vtable) + (slot * SIZEOF_VOID_P);
7815
7816                 /* Load the slot, which contains a function descriptor. */
7817                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, slot_reg, vtable_reg, offset);
7818
7819                 /* These slots are not initialized, so fall back to the slow path until they are initialized */
7820                 /* That happens when mono_method_add_generic_virtual_invocation () creates an IMT thunk */
7821                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, slot_reg, 0);
7822                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, slowpath_bb);
7823
7824                 /* Fastpath */
7825                 /* Same as with iface calls */
7826                 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_addr_ins, OP_LOAD_MEMBASE, addr_reg, slot_reg, 0);
7827                 EMIT_NEW_LOAD_MEMBASE (cfg, thunk_arg_ins, OP_LOAD_MEMBASE, arg_reg, slot_reg, SIZEOF_VOID_P);
7828                 icall_args [0] = thunk_arg_ins;
7829                 icall_args [1] = emit_get_rgctx_method (cfg, context_used,
7830                                                                                                 cmethod, MONO_RGCTX_INFO_METHOD);
7831                 ftndesc_ins = mono_emit_calli (cfg, helper_sig_llvmonly_imt_thunk, icall_args, thunk_addr_ins, NULL, NULL);
7832                 ftndesc_ins->dreg = ftndesc_reg;
7833                 /*
7834                  * Unlike normal iface calls, these imt thunks can return NULL, i.e. when they are passed an instantiation
7835                  * they don't know about yet. Fall back to the slowpath in that case.
7836                  */
7837                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, ftndesc_reg, 0);
7838                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, slowpath_bb);
7839
7840                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
7841
7842                 /* Slowpath */
7843                 MONO_START_BB (cfg, slowpath_bb);
7844                 icall_args [0] = vtable_ins;
7845                 EMIT_NEW_ICONST (cfg, icall_args [1], slot);
7846                 icall_args [2] = emit_get_rgctx_method (cfg, context_used,
7847                                                                                                 cmethod, MONO_RGCTX_INFO_METHOD);
7848                 if (is_iface)
7849                         ftndesc_ins = mono_emit_jit_icall (cfg, mono_resolve_generic_virtual_iface_call, icall_args);
7850                 else
7851                         ftndesc_ins = mono_emit_jit_icall (cfg, mono_resolve_generic_virtual_call, icall_args);
7852                 ftndesc_ins->dreg = ftndesc_reg;
7853                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
7854
7855                 /* Common case */
7856                 MONO_START_BB (cfg, end_bb);
7857                 return emit_llvmonly_calli (cfg, fsig, sp, ftndesc_ins);
7858         }
7859
7860         /*
7861          * Non-optimized cases
7862          */
7863         icall_args [0] = sp [0];
7864         EMIT_NEW_ICONST (cfg, icall_args [1], slot);
7865
7866         icall_args [2] = emit_get_rgctx_method (cfg, context_used,
7867                                                                                         cmethod, MONO_RGCTX_INFO_METHOD);
7868
7869         arg_reg = alloc_preg (cfg);
7870         MONO_EMIT_NEW_PCONST (cfg, arg_reg, NULL);
7871         EMIT_NEW_VARLOADA_VREG (cfg, icall_args [3], arg_reg, &mono_defaults.int_class->byval_arg);
7872
7873         g_assert (is_gsharedvt);
7874         if (is_iface)
7875                 call_target = mono_emit_jit_icall (cfg, mono_resolve_iface_call_gsharedvt, icall_args);
7876         else
7877                 call_target = mono_emit_jit_icall (cfg, mono_resolve_vcall_gsharedvt, icall_args);
7878
7879         /*
7880          * Pass the extra argument even if the callee doesn't receive it, most
7881          * calling conventions allow this.
7882          */
7883         return emit_extra_arg_calli (cfg, fsig, sp, arg_reg, call_target);
7884 }
7885
7886 static gboolean
7887 is_exception_class (MonoClass *klass)
7888 {
7889         while (klass) {
7890                 if (klass == mono_defaults.exception_class)
7891                         return TRUE;
7892                 klass = klass->parent;
7893         }
7894         return FALSE;
7895 }
7896
7897 /*
7898  * is_jit_optimizer_disabled:
7899  *
7900  *   Determine whenever M's assembly has a DebuggableAttribute with the
7901  * IsJITOptimizerDisabled flag set.
7902  */
7903 static gboolean
7904 is_jit_optimizer_disabled (MonoMethod *m)
7905 {
7906         MonoError error;
7907         MonoAssembly *ass = m->klass->image->assembly;
7908         MonoCustomAttrInfo* attrs;
7909         MonoClass *klass;
7910         int i;
7911         gboolean val = FALSE;
7912
7913         g_assert (ass);
7914         if (ass->jit_optimizer_disabled_inited)
7915                 return ass->jit_optimizer_disabled;
7916
7917         klass = mono_class_try_get_debuggable_attribute_class ();
7918
7919         if (!klass) {
7920                 /* Linked away */
7921                 ass->jit_optimizer_disabled = FALSE;
7922                 mono_memory_barrier ();
7923                 ass->jit_optimizer_disabled_inited = TRUE;
7924                 return FALSE;
7925         }
7926
7927         attrs = mono_custom_attrs_from_assembly_checked (ass, &error);
7928         mono_error_cleanup (&error); /* FIXME don't swallow the error */
7929         if (attrs) {
7930                 for (i = 0; i < attrs->num_attrs; ++i) {
7931                         MonoCustomAttrEntry *attr = &attrs->attrs [i];
7932                         const gchar *p;
7933                         MonoMethodSignature *sig;
7934
7935                         if (!attr->ctor || attr->ctor->klass != klass)
7936                                 continue;
7937                         /* Decode the attribute. See reflection.c */
7938                         p = (const char*)attr->data;
7939                         g_assert (read16 (p) == 0x0001);
7940                         p += 2;
7941
7942                         // FIXME: Support named parameters
7943                         sig = mono_method_signature (attr->ctor);
7944                         if (sig->param_count != 2 || sig->params [0]->type != MONO_TYPE_BOOLEAN || sig->params [1]->type != MONO_TYPE_BOOLEAN)
7945                                 continue;
7946                         /* Two boolean arguments */
7947                         p ++;
7948                         val = *p;
7949                 }
7950                 mono_custom_attrs_free (attrs);
7951         }
7952
7953         ass->jit_optimizer_disabled = val;
7954         mono_memory_barrier ();
7955         ass->jit_optimizer_disabled_inited = TRUE;
7956
7957         return val;
7958 }
7959
7960 static gboolean
7961 is_supported_tail_call (MonoCompile *cfg, MonoMethod *method, MonoMethod *cmethod, MonoMethodSignature *fsig, int call_opcode)
7962 {
7963         gboolean supported_tail_call;
7964         int i;
7965
7966         supported_tail_call = mono_arch_tail_call_supported (cfg, mono_method_signature (method), mono_method_signature (cmethod));
7967
7968         for (i = 0; i < fsig->param_count; ++i) {
7969                 if (fsig->params [i]->byref || fsig->params [i]->type == MONO_TYPE_PTR || fsig->params [i]->type == MONO_TYPE_FNPTR)
7970                         /* These can point to the current method's stack */
7971                         supported_tail_call = FALSE;
7972         }
7973         if (fsig->hasthis && cmethod->klass->valuetype)
7974                 /* this might point to the current method's stack */
7975                 supported_tail_call = FALSE;
7976         if (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
7977                 supported_tail_call = FALSE;
7978         if (cfg->method->save_lmf)
7979                 supported_tail_call = FALSE;
7980         if (cmethod->wrapper_type && cmethod->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD)
7981                 supported_tail_call = FALSE;
7982         if (call_opcode != CEE_CALL)
7983                 supported_tail_call = FALSE;
7984
7985         /* Debugging support */
7986 #if 0
7987         if (supported_tail_call) {
7988                 if (!mono_debug_count ())
7989                         supported_tail_call = FALSE;
7990         }
7991 #endif
7992
7993         return supported_tail_call;
7994 }
7995
7996 /*
7997  * handle_ctor_call:
7998  *
7999  *   Handle calls made to ctors from NEWOBJ opcodes.
8000  */
8001 static void
8002 handle_ctor_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, int context_used,
8003                                   MonoInst **sp, guint8 *ip, int *inline_costs)
8004 {
8005         MonoInst *vtable_arg = NULL, *callvirt_this_arg = NULL, *ins;
8006
8007         if (cmethod->klass->valuetype && mono_class_generic_sharing_enabled (cmethod->klass) &&
8008                                         mono_method_is_generic_sharable (cmethod, TRUE)) {
8009                 if (cmethod->is_inflated && mono_method_get_context (cmethod)->method_inst) {
8010                         mono_class_vtable (cfg->domain, cmethod->klass);
8011                         CHECK_TYPELOAD (cmethod->klass);
8012
8013                         vtable_arg = emit_get_rgctx_method (cfg, context_used,
8014                                                                                                 cmethod, MONO_RGCTX_INFO_METHOD_RGCTX);
8015                 } else {
8016                         if (context_used) {
8017                                 vtable_arg = emit_get_rgctx_klass (cfg, context_used,
8018                                                                                                    cmethod->klass, MONO_RGCTX_INFO_VTABLE);
8019                         } else {
8020                                 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
8021
8022                                 CHECK_TYPELOAD (cmethod->klass);
8023                                 EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
8024                         }
8025                 }
8026         }
8027
8028         /* Avoid virtual calls to ctors if possible */
8029         if (mono_class_is_marshalbyref (cmethod->klass))
8030                 callvirt_this_arg = sp [0];
8031
8032         if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_ctor (cfg, cmethod, fsig, sp))) {
8033                 g_assert (MONO_TYPE_IS_VOID (fsig->ret));
8034                 CHECK_CFG_EXCEPTION;
8035         } else if ((cfg->opt & MONO_OPT_INLINE) && cmethod && !context_used && !vtable_arg &&
8036                            mono_method_check_inlining (cfg, cmethod) &&
8037                            !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE)) {
8038                 int costs;
8039
8040                 if ((costs = inline_method (cfg, cmethod, fsig, sp, ip, cfg->real_offset, FALSE))) {
8041                         cfg->real_offset += 5;
8042
8043                         *inline_costs += costs - 5;
8044                 } else {
8045                         INLINE_FAILURE ("inline failure");
8046                         // FIXME-VT: Clean this up
8047                         if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig))
8048                                 GSHAREDVT_FAILURE(*ip);
8049                         mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, callvirt_this_arg, NULL, NULL);
8050                 }
8051         } else if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) {
8052                 MonoInst *addr;
8053
8054                 addr = emit_get_rgctx_gsharedvt_call (cfg, context_used, fsig, cmethod, MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE);
8055
8056                 if (cfg->llvm_only) {
8057                         // FIXME: Avoid initializing vtable_arg
8058                         emit_llvmonly_calli (cfg, fsig, sp, addr);
8059                 } else {
8060                         mono_emit_calli (cfg, fsig, sp, addr, NULL, vtable_arg);
8061                 }
8062         } else if (context_used &&
8063                            ((!mono_method_is_generic_sharable_full (cmethod, TRUE, FALSE, FALSE) ||
8064                                  !mono_class_generic_sharing_enabled (cmethod->klass)) || cfg->gsharedvt)) {
8065                 MonoInst *cmethod_addr;
8066
8067                 /* Generic calls made out of gsharedvt methods cannot be patched, so use an indirect call */
8068
8069                 if (cfg->llvm_only) {
8070                         MonoInst *addr = emit_get_rgctx_method (cfg, context_used, cmethod,
8071                                                                                                         MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
8072                         emit_llvmonly_calli (cfg, fsig, sp, addr);
8073                 } else {
8074                         cmethod_addr = emit_get_rgctx_method (cfg, context_used,
8075                                                                                                   cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
8076
8077                         mono_emit_calli (cfg, fsig, sp, cmethod_addr, NULL, vtable_arg);
8078                 }
8079         } else {
8080                 INLINE_FAILURE ("ctor call");
8081                 ins = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp,
8082                                                                                   callvirt_this_arg, NULL, vtable_arg);
8083         }
8084  exception_exit:
8085         return;
8086 }
8087
8088 static void
8089 emit_setret (MonoCompile *cfg, MonoInst *val)
8090 {
8091         MonoType *ret_type = mini_get_underlying_type (mono_method_signature (cfg->method)->ret);
8092         MonoInst *ins;
8093
8094         if (mini_type_to_stind (cfg, ret_type) == CEE_STOBJ) {
8095                 MonoInst *ret_addr;
8096
8097                 if (!cfg->vret_addr) {
8098                         EMIT_NEW_VARSTORE (cfg, ins, cfg->ret, ret_type, val);
8099                 } else {
8100                         EMIT_NEW_RETLOADA (cfg, ret_addr);
8101
8102                         EMIT_NEW_STORE_MEMBASE (cfg, ins, OP_STOREV_MEMBASE, ret_addr->dreg, 0, val->dreg);
8103                         ins->klass = mono_class_from_mono_type (ret_type);
8104                 }
8105         } else {
8106 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
8107                 if (COMPILE_SOFT_FLOAT (cfg) && !ret_type->byref && ret_type->type == MONO_TYPE_R4) {
8108                         MonoInst *iargs [1];
8109                         MonoInst *conv;
8110
8111                         iargs [0] = val;
8112                         conv = mono_emit_jit_icall (cfg, mono_fload_r4_arg, iargs);
8113                         mono_arch_emit_setret (cfg, cfg->method, conv);
8114                 } else {
8115                         mono_arch_emit_setret (cfg, cfg->method, val);
8116                 }
8117 #else
8118                 mono_arch_emit_setret (cfg, cfg->method, val);
8119 #endif
8120         }
8121 }
8122
8123 /*
8124  * mono_method_to_ir:
8125  *
8126  *   Translate the .net IL into linear IR.
8127  */
8128 int
8129 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock, 
8130                    MonoInst *return_var, MonoInst **inline_args, 
8131                    guint inline_offset, gboolean is_virtual_call)
8132 {
8133         MonoError error;
8134         MonoInst *ins, **sp, **stack_start;
8135         MonoBasicBlock *tblock = NULL, *init_localsbb = NULL;
8136         MonoSimpleBasicBlock *bb = NULL, *original_bb = NULL;
8137         MonoMethod *cmethod, *method_definition;
8138         MonoInst **arg_array;
8139         MonoMethodHeader *header;
8140         MonoImage *image;
8141         guint32 token, ins_flag;
8142         MonoClass *klass;
8143         MonoClass *constrained_class = NULL;
8144         unsigned char *ip, *end, *target, *err_pos;
8145         MonoMethodSignature *sig;
8146         MonoGenericContext *generic_context = NULL;
8147         MonoGenericContainer *generic_container = NULL;
8148         MonoType **param_types;
8149         int i, n, start_new_bblock, dreg;
8150         int num_calls = 0, inline_costs = 0;
8151         int breakpoint_id = 0;
8152         guint num_args;
8153         GSList *class_inits = NULL;
8154         gboolean dont_verify, dont_verify_stloc, readonly = FALSE;
8155         int context_used;
8156         gboolean init_locals, seq_points, skip_dead_blocks;
8157         gboolean sym_seq_points = FALSE;
8158         MonoDebugMethodInfo *minfo;
8159         MonoBitSet *seq_point_locs = NULL;
8160         MonoBitSet *seq_point_set_locs = NULL;
8161
8162         cfg->disable_inline = is_jit_optimizer_disabled (method);
8163
8164         /* serialization and xdomain stuff may need access to private fields and methods */
8165         dont_verify = method->klass->image->assembly->corlib_internal? TRUE: FALSE;
8166         dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE;
8167         dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH;
8168         dont_verify |= method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE; /* bug #77896 */
8169         dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP;
8170         dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP_INVOKE;
8171
8172         /* still some type unsafety issues in marshal wrappers... (unknown is PtrToStructure) */
8173         dont_verify_stloc = method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE;
8174         dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_UNKNOWN;
8175         dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED;
8176         dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_STELEMREF;
8177
8178         image = method->klass->image;
8179         header = mono_method_get_header_checked (method, &cfg->error);
8180         if (!header) {
8181                 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
8182                 goto exception_exit;
8183         }
8184         generic_container = mono_method_get_generic_container (method);
8185         sig = mono_method_signature (method);
8186         num_args = sig->hasthis + sig->param_count;
8187         ip = (unsigned char*)header->code;
8188         cfg->cil_start = ip;
8189         end = ip + header->code_size;
8190         cfg->stat_cil_code_size += header->code_size;
8191
8192         seq_points = cfg->gen_seq_points && cfg->method == method;
8193
8194         if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
8195                 /* We could hit a seq point before attaching to the JIT (#8338) */
8196                 seq_points = FALSE;
8197         }
8198
8199         if (cfg->gen_sdb_seq_points && cfg->method == method) {
8200                 minfo = mono_debug_lookup_method (method);
8201                 if (minfo) {
8202                         MonoSymSeqPoint *sps;
8203                         int i, n_il_offsets;
8204
8205                         mono_debug_get_seq_points (minfo, NULL, NULL, NULL, &sps, &n_il_offsets);
8206                         seq_point_locs = mono_bitset_mem_new (mono_mempool_alloc0 (cfg->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
8207                         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);
8208                         sym_seq_points = TRUE;
8209                         for (i = 0; i < n_il_offsets; ++i) {
8210                                 if (sps [i].il_offset < header->code_size)
8211                                         mono_bitset_set_fast (seq_point_locs, sps [i].il_offset);
8212                         }
8213                         g_free (sps);
8214                 } else if (!method->wrapper_type && !method->dynamic && mono_debug_image_has_debug_info (method->klass->image)) {
8215                         /* Methods without line number info like auto-generated property accessors */
8216                         seq_point_locs = mono_bitset_mem_new (mono_mempool_alloc0 (cfg->mempool, mono_bitset_alloc_size (header->code_size, 0)), header->code_size, 0);
8217                         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);
8218                         sym_seq_points = TRUE;
8219                 }
8220         }
8221
8222         /* 
8223          * Methods without init_locals set could cause asserts in various passes
8224          * (#497220). To work around this, we emit dummy initialization opcodes
8225          * (OP_DUMMY_ICONST etc.) which generate no code. These are only supported
8226          * on some platforms.
8227          */
8228         if ((cfg->opt & MONO_OPT_UNSAFE) && cfg->backend->have_dummy_init)
8229                 init_locals = header->init_locals;
8230         else
8231                 init_locals = TRUE;
8232
8233         method_definition = method;
8234         while (method_definition->is_inflated) {
8235                 MonoMethodInflated *imethod = (MonoMethodInflated *) method_definition;
8236                 method_definition = imethod->declaring;
8237         }
8238
8239         /* SkipVerification is not allowed if core-clr is enabled */
8240         if (!dont_verify && mini_assembly_can_skip_verification (cfg->domain, method)) {
8241                 dont_verify = TRUE;
8242                 dont_verify_stloc = TRUE;
8243         }
8244
8245         if (sig->is_inflated)
8246                 generic_context = mono_method_get_context (method);
8247         else if (generic_container)
8248                 generic_context = &generic_container->context;
8249         cfg->generic_context = generic_context;
8250
8251         if (!cfg->gshared)
8252                 g_assert (!sig->has_type_parameters);
8253
8254         if (sig->generic_param_count && method->wrapper_type == MONO_WRAPPER_NONE) {
8255                 g_assert (method->is_inflated);
8256                 g_assert (mono_method_get_context (method)->method_inst);
8257         }
8258         if (method->is_inflated && mono_method_get_context (method)->method_inst)
8259                 g_assert (sig->generic_param_count);
8260
8261         if (cfg->method == method) {
8262                 cfg->real_offset = 0;
8263         } else {
8264                 cfg->real_offset = inline_offset;
8265         }
8266
8267         cfg->cil_offset_to_bb = (MonoBasicBlock **)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoBasicBlock*) * header->code_size);
8268         cfg->cil_offset_to_bb_len = header->code_size;
8269
8270         cfg->current_method = method;
8271
8272         if (cfg->verbose_level > 2)
8273                 printf ("method to IR %s\n", mono_method_full_name (method, TRUE));
8274
8275         param_types = (MonoType **)mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
8276         if (sig->hasthis)
8277                 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
8278         for (n = 0; n < sig->param_count; ++n)
8279                 param_types [n + sig->hasthis] = sig->params [n];
8280         cfg->arg_types = param_types;
8281
8282         cfg->dont_inline = g_list_prepend (cfg->dont_inline, method);
8283         if (cfg->method == method) {
8284
8285                 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
8286                         cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
8287
8288                 /* ENTRY BLOCK */
8289                 NEW_BBLOCK (cfg, start_bblock);
8290                 cfg->bb_entry = start_bblock;
8291                 start_bblock->cil_code = NULL;
8292                 start_bblock->cil_length = 0;
8293
8294                 /* EXIT BLOCK */
8295                 NEW_BBLOCK (cfg, end_bblock);
8296                 cfg->bb_exit = end_bblock;
8297                 end_bblock->cil_code = NULL;
8298                 end_bblock->cil_length = 0;
8299                 end_bblock->flags |= BB_INDIRECT_JUMP_TARGET;
8300                 g_assert (cfg->num_bblocks == 2);
8301
8302                 arg_array = cfg->args;
8303
8304                 if (header->num_clauses) {
8305                         cfg->spvars = g_hash_table_new (NULL, NULL);
8306                         cfg->exvars = g_hash_table_new (NULL, NULL);
8307                 }
8308                 /* handle exception clauses */
8309                 for (i = 0; i < header->num_clauses; ++i) {
8310                         MonoBasicBlock *try_bb;
8311                         MonoExceptionClause *clause = &header->clauses [i];
8312                         GET_BBLOCK (cfg, try_bb, ip + clause->try_offset);
8313
8314                         try_bb->real_offset = clause->try_offset;
8315                         try_bb->try_start = TRUE;
8316                         try_bb->region = ((i + 1) << 8) | clause->flags;
8317                         GET_BBLOCK (cfg, tblock, ip + clause->handler_offset);
8318                         tblock->real_offset = clause->handler_offset;
8319                         tblock->flags |= BB_EXCEPTION_HANDLER;
8320
8321                         /*
8322                          * Linking the try block with the EH block hinders inlining as we won't be able to 
8323                          * merge the bblocks from inlining and produce an artificial hole for no good reason.
8324                          */
8325                         if (COMPILE_LLVM (cfg))
8326                                 link_bblock (cfg, try_bb, tblock);
8327
8328                         if (*(ip + clause->handler_offset) == CEE_POP)
8329                                 tblock->flags |= BB_EXCEPTION_DEAD_OBJ;
8330
8331                         if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
8332                             clause->flags == MONO_EXCEPTION_CLAUSE_FILTER ||
8333                             clause->flags == MONO_EXCEPTION_CLAUSE_FAULT) {
8334                                 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
8335                                 MONO_ADD_INS (tblock, ins);
8336
8337                                 if (seq_points && clause->flags != MONO_EXCEPTION_CLAUSE_FINALLY && clause->flags != MONO_EXCEPTION_CLAUSE_FILTER) {
8338                                         /* finally clauses already have a seq point */
8339                                         /* seq points for filter clauses are emitted below */
8340                                         NEW_SEQ_POINT (cfg, ins, clause->handler_offset, TRUE);
8341                                         MONO_ADD_INS (tblock, ins);
8342                                 }
8343
8344                                 /* todo: is a fault block unsafe to optimize? */
8345                                 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
8346                                         tblock->flags |= BB_EXCEPTION_UNSAFE;
8347                         }
8348
8349                         /*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);
8350                           while (p < end) {
8351                           printf ("%s", mono_disasm_code_one (NULL, method, p, &p));
8352                           }*/
8353                         /* catch and filter blocks get the exception object on the stack */
8354                         if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
8355                             clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
8356
8357                                 /* mostly like handle_stack_args (), but just sets the input args */
8358                                 /* printf ("handling clause at IL_%04x\n", clause->handler_offset); */
8359                                 tblock->in_scount = 1;
8360                                 tblock->in_stack = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
8361                                 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
8362
8363                                 cfg->cbb = tblock;
8364
8365 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
8366                                 /* The EH code passes in the exception in a register to both JITted and LLVM compiled code */
8367                                 if (!cfg->compile_llvm) {
8368                                         MONO_INST_NEW (cfg, ins, OP_GET_EX_OBJ);
8369                                         ins->dreg = tblock->in_stack [0]->dreg;
8370                                         MONO_ADD_INS (tblock, ins);
8371                                 }
8372 #else
8373                                 MonoInst *dummy_use;
8374
8375                                 /* 
8376                                  * Add a dummy use for the exvar so its liveness info will be
8377                                  * correct.
8378                                  */
8379                                 EMIT_NEW_DUMMY_USE (cfg, dummy_use, tblock->in_stack [0]);
8380 #endif
8381
8382                                 if (seq_points && clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
8383                                         NEW_SEQ_POINT (cfg, ins, clause->handler_offset, TRUE);
8384                                         MONO_ADD_INS (tblock, ins);
8385                                 }
8386                                 
8387                                 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
8388                                         GET_BBLOCK (cfg, tblock, ip + clause->data.filter_offset);
8389                                         tblock->flags |= BB_EXCEPTION_HANDLER;
8390                                         tblock->real_offset = clause->data.filter_offset;
8391                                         tblock->in_scount = 1;
8392                                         tblock->in_stack = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
8393                                         /* The filter block shares the exvar with the handler block */
8394                                         tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
8395                                         MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
8396                                         MONO_ADD_INS (tblock, ins);
8397                                 }
8398                         }
8399
8400                         if (clause->flags != MONO_EXCEPTION_CLAUSE_FILTER &&
8401                                         clause->data.catch_class &&
8402                                         cfg->gshared &&
8403                                         mono_class_check_context_used (clause->data.catch_class)) {
8404                                 /*
8405                                  * In shared generic code with catch
8406                                  * clauses containing type variables
8407                                  * the exception handling code has to
8408                                  * be able to get to the rgctx.
8409                                  * Therefore we have to make sure that
8410                                  * the vtable/mrgctx argument (for
8411                                  * static or generic methods) or the
8412                                  * "this" argument (for non-static
8413                                  * methods) are live.
8414                                  */
8415                                 if ((method->flags & METHOD_ATTRIBUTE_STATIC) ||
8416                                                 mini_method_get_context (method)->method_inst ||
8417                                                 method->klass->valuetype) {
8418                                         mono_get_vtable_var (cfg);
8419                                 } else {
8420                                         MonoInst *dummy_use;
8421
8422                                         EMIT_NEW_DUMMY_USE (cfg, dummy_use, arg_array [0]);
8423                                 }
8424                         }
8425                 }
8426         } else {
8427                 arg_array = (MonoInst **) alloca (sizeof (MonoInst *) * num_args);
8428                 cfg->cbb = start_bblock;
8429                 cfg->args = arg_array;
8430                 mono_save_args (cfg, sig, inline_args);
8431         }
8432
8433         /* FIRST CODE BLOCK */
8434         NEW_BBLOCK (cfg, tblock);
8435         tblock->cil_code = ip;
8436         cfg->cbb = tblock;
8437         cfg->ip = ip;
8438
8439         ADD_BBLOCK (cfg, tblock);
8440
8441         if (cfg->method == method) {
8442                 breakpoint_id = mono_debugger_method_has_breakpoint (method);
8443                 if (breakpoint_id) {
8444                         MONO_INST_NEW (cfg, ins, OP_BREAK);
8445                         MONO_ADD_INS (cfg->cbb, ins);
8446                 }
8447         }
8448
8449         /* we use a separate basic block for the initialization code */
8450         NEW_BBLOCK (cfg, init_localsbb);
8451         cfg->bb_init = init_localsbb;
8452         init_localsbb->real_offset = cfg->real_offset;
8453         start_bblock->next_bb = init_localsbb;
8454         init_localsbb->next_bb = cfg->cbb;
8455         link_bblock (cfg, start_bblock, init_localsbb);
8456         link_bblock (cfg, init_localsbb, cfg->cbb);
8457                 
8458         cfg->cbb = init_localsbb;
8459
8460         if (cfg->gsharedvt && cfg->method == method) {
8461                 MonoGSharedVtMethodInfo *info;
8462                 MonoInst *var, *locals_var;
8463                 int dreg;
8464
8465                 info = (MonoGSharedVtMethodInfo *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoGSharedVtMethodInfo));
8466                 info->method = cfg->method;
8467                 info->count_entries = 16;
8468                 info->entries = (MonoRuntimeGenericContextInfoTemplate *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoRuntimeGenericContextInfoTemplate) * info->count_entries);
8469                 cfg->gsharedvt_info = info;
8470
8471                 var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
8472                 /* prevent it from being register allocated */
8473                 //var->flags |= MONO_INST_VOLATILE;
8474                 cfg->gsharedvt_info_var = var;
8475
8476                 ins = emit_get_rgctx_gsharedvt_method (cfg, mini_method_check_context_used (cfg, method), method, info);
8477                 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, var->dreg, ins->dreg);
8478
8479                 /* Allocate locals */
8480                 locals_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
8481                 /* prevent it from being register allocated */
8482                 //locals_var->flags |= MONO_INST_VOLATILE;
8483                 cfg->gsharedvt_locals_var = locals_var;
8484
8485                 dreg = alloc_ireg (cfg);
8486                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, dreg, var->dreg, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, locals_size));
8487
8488                 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
8489                 ins->dreg = locals_var->dreg;
8490                 ins->sreg1 = dreg;
8491                 MONO_ADD_INS (cfg->cbb, ins);
8492                 cfg->gsharedvt_locals_var_ins = ins;
8493                 
8494                 cfg->flags |= MONO_CFG_HAS_ALLOCA;
8495                 /*
8496                 if (init_locals)
8497                         ins->flags |= MONO_INST_INIT;
8498                 */
8499         }
8500
8501         if (mono_security_core_clr_enabled ()) {
8502                 /* check if this is native code, e.g. an icall or a p/invoke */
8503                 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
8504                         MonoMethod *wrapped = mono_marshal_method_from_wrapper (method);
8505                         if (wrapped) {
8506                                 gboolean pinvk = (wrapped->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
8507                                 gboolean icall = (wrapped->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL);
8508
8509                                 /* if this ia a native call then it can only be JITted from platform code */
8510                                 if ((icall || pinvk) && method->klass && method->klass->image) {
8511                                         if (!mono_security_core_clr_is_platform_image (method->klass->image)) {
8512                                                 MonoException *ex = icall ? mono_get_exception_security () : 
8513                                                         mono_get_exception_method_access ();
8514                                                 emit_throw_exception (cfg, ex);
8515                                         }
8516                                 }
8517                         }
8518                 }
8519         }
8520
8521         CHECK_CFG_EXCEPTION;
8522
8523         if (header->code_size == 0)
8524                 UNVERIFIED;
8525
8526         if (get_basic_blocks (cfg, header, cfg->real_offset, ip, end, &err_pos)) {
8527                 ip = err_pos;
8528                 UNVERIFIED;
8529         }
8530
8531         if (cfg->method == method)
8532                 mono_debug_init_method (cfg, cfg->cbb, breakpoint_id);
8533
8534         for (n = 0; n < header->num_locals; ++n) {
8535                 if (header->locals [n]->type == MONO_TYPE_VOID && !header->locals [n]->byref)
8536                         UNVERIFIED;
8537         }
8538         class_inits = NULL;
8539
8540         /* We force the vtable variable here for all shared methods
8541            for the possibility that they might show up in a stack
8542            trace where their exact instantiation is needed. */
8543         if (cfg->gshared && method == cfg->method) {
8544                 if ((method->flags & METHOD_ATTRIBUTE_STATIC) ||
8545                                 mini_method_get_context (method)->method_inst ||
8546                                 method->klass->valuetype) {
8547                         mono_get_vtable_var (cfg);
8548                 } else {
8549                         /* FIXME: Is there a better way to do this?
8550                            We need the variable live for the duration
8551                            of the whole method. */
8552                         cfg->args [0]->flags |= MONO_INST_VOLATILE;
8553                 }
8554         }
8555
8556         /* add a check for this != NULL to inlined methods */
8557         if (is_virtual_call) {
8558                 MonoInst *arg_ins;
8559
8560                 NEW_ARGLOAD (cfg, arg_ins, 0);
8561                 MONO_ADD_INS (cfg->cbb, arg_ins);
8562                 MONO_EMIT_NEW_CHECK_THIS (cfg, arg_ins->dreg);
8563         }
8564
8565         skip_dead_blocks = !dont_verify;
8566         if (skip_dead_blocks) {
8567                 original_bb = bb = mono_basic_block_split (method, &cfg->error);
8568                 CHECK_CFG_ERROR;
8569                 g_assert (bb);
8570         }
8571
8572         /* we use a spare stack slot in SWITCH and NEWOBJ and others */
8573         stack_start = sp = (MonoInst **)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
8574
8575         ins_flag = 0;
8576         start_new_bblock = 0;
8577         while (ip < end) {
8578                 if (cfg->method == method)
8579                         cfg->real_offset = ip - header->code;
8580                 else
8581                         cfg->real_offset = inline_offset;
8582                 cfg->ip = ip;
8583
8584                 context_used = 0;
8585
8586                 if (start_new_bblock) {
8587                         cfg->cbb->cil_length = ip - cfg->cbb->cil_code;
8588                         if (start_new_bblock == 2) {
8589                                 g_assert (ip == tblock->cil_code);
8590                         } else {
8591                                 GET_BBLOCK (cfg, tblock, ip);
8592                         }
8593                         cfg->cbb->next_bb = tblock;
8594                         cfg->cbb = tblock;
8595                         start_new_bblock = 0;
8596                         for (i = 0; i < cfg->cbb->in_scount; ++i) {
8597                                 if (cfg->verbose_level > 3)
8598                                         printf ("loading %d from temp %d\n", i, (int)cfg->cbb->in_stack [i]->inst_c0);
8599                                 EMIT_NEW_TEMPLOAD (cfg, ins, cfg->cbb->in_stack [i]->inst_c0);
8600                                 *sp++ = ins;
8601                         }
8602                         if (class_inits)
8603                                 g_slist_free (class_inits);
8604                         class_inits = NULL;
8605                 } else {
8606                         if ((tblock = cfg->cil_offset_to_bb [ip - cfg->cil_start]) && (tblock != cfg->cbb)) {
8607                                 link_bblock (cfg, cfg->cbb, tblock);
8608                                 if (sp != stack_start) {
8609                                         handle_stack_args (cfg, stack_start, sp - stack_start);
8610                                         sp = stack_start;
8611                                         CHECK_UNVERIFIABLE (cfg);
8612                                 }
8613                                 cfg->cbb->next_bb = tblock;
8614                                 cfg->cbb = tblock;
8615                                 for (i = 0; i < cfg->cbb->in_scount; ++i) {
8616                                         if (cfg->verbose_level > 3)
8617                                                 printf ("loading %d from temp %d\n", i, (int)cfg->cbb->in_stack [i]->inst_c0);
8618                                         EMIT_NEW_TEMPLOAD (cfg, ins, cfg->cbb->in_stack [i]->inst_c0);
8619                                         *sp++ = ins;
8620                                 }
8621                                 g_slist_free (class_inits);
8622                                 class_inits = NULL;
8623                         }
8624                 }
8625
8626                 if (skip_dead_blocks) {
8627                         int ip_offset = ip - header->code;
8628
8629                         if (ip_offset == bb->end)
8630                                 bb = bb->next;
8631
8632                         if (bb->dead) {
8633                                 int op_size = mono_opcode_size (ip, end);
8634                                 g_assert (op_size > 0); /*The BB formation pass must catch all bad ops*/
8635
8636                                 if (cfg->verbose_level > 3) printf ("SKIPPING DEAD OP at %x\n", ip_offset);
8637
8638                                 if (ip_offset + op_size == bb->end) {
8639                                         MONO_INST_NEW (cfg, ins, OP_NOP);
8640                                         MONO_ADD_INS (cfg->cbb, ins);
8641                                         start_new_bblock = 1;
8642                                 }
8643
8644                                 ip += op_size;
8645                                 continue;
8646                         }
8647                 }
8648                 /*
8649                  * Sequence points are points where the debugger can place a breakpoint.
8650                  * Currently, we generate these automatically at points where the IL
8651                  * stack is empty.
8652                  */
8653                 if (seq_points && ((!sym_seq_points && (sp == stack_start)) || (sym_seq_points && mono_bitset_test_fast (seq_point_locs, ip - header->code)))) {
8654                         /*
8655                          * Make methods interruptable at the beginning, and at the targets of
8656                          * backward branches.
8657                          * Also, do this at the start of every bblock in methods with clauses too,
8658                          * to be able to handle instructions with inprecise control flow like
8659                          * throw/endfinally.
8660                          * Backward branches are handled at the end of method-to-ir ().
8661                          */
8662                         gboolean intr_loc = ip == header->code || (!cfg->cbb->last_ins && cfg->header->num_clauses);
8663                         gboolean sym_seq_point = sym_seq_points && mono_bitset_test_fast (seq_point_locs, ip - header->code);
8664
8665                         /* Avoid sequence points on empty IL like .volatile */
8666                         // FIXME: Enable this
8667                         //if (!(cfg->cbb->last_ins && cfg->cbb->last_ins->opcode == OP_SEQ_POINT)) {
8668                         NEW_SEQ_POINT (cfg, ins, ip - header->code, intr_loc);
8669                         if ((sp != stack_start) && !sym_seq_point)
8670                                 ins->flags |= MONO_INST_NONEMPTY_STACK;
8671                         MONO_ADD_INS (cfg->cbb, ins);
8672
8673                         if (sym_seq_points)
8674                                 mono_bitset_set_fast (seq_point_set_locs, ip - header->code);
8675                 }
8676
8677                 cfg->cbb->real_offset = cfg->real_offset;
8678
8679                 if ((cfg->method == method) && cfg->coverage_info) {
8680                         guint32 cil_offset = ip - header->code;
8681                         cfg->coverage_info->data [cil_offset].cil_code = ip;
8682
8683                         /* TODO: Use an increment here */
8684 #if defined(TARGET_X86)
8685                         MONO_INST_NEW (cfg, ins, OP_STORE_MEM_IMM);
8686                         ins->inst_p0 = &(cfg->coverage_info->data [cil_offset].count);
8687                         ins->inst_imm = 1;
8688                         MONO_ADD_INS (cfg->cbb, ins);
8689 #else
8690                         EMIT_NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
8691                         MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, ins->dreg, 0, 1);
8692 #endif
8693                 }
8694
8695                 if (cfg->verbose_level > 3)
8696                         printf ("converting (in B%d: stack: %d) %s", cfg->cbb->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
8697
8698                 switch (*ip) {
8699                 case CEE_NOP:
8700                         if (seq_points && !sym_seq_points && sp != stack_start) {
8701                                 /*
8702                                  * The C# compiler uses these nops to notify the JIT that it should
8703                                  * insert seq points.
8704                                  */
8705                                 NEW_SEQ_POINT (cfg, ins, ip - header->code, FALSE);
8706                                 MONO_ADD_INS (cfg->cbb, ins);
8707                         }
8708                         if (cfg->keep_cil_nops)
8709                                 MONO_INST_NEW (cfg, ins, OP_HARD_NOP);
8710                         else
8711                                 MONO_INST_NEW (cfg, ins, OP_NOP);
8712                         ip++;
8713                         MONO_ADD_INS (cfg->cbb, ins);
8714                         break;
8715                 case CEE_BREAK:
8716                         if (should_insert_brekpoint (cfg->method)) {
8717                                 ins = mono_emit_jit_icall (cfg, mono_debugger_agent_user_break, NULL);
8718                         } else {
8719                                 MONO_INST_NEW (cfg, ins, OP_NOP);
8720                         }
8721                         ip++;
8722                         MONO_ADD_INS (cfg->cbb, ins);
8723                         break;
8724                 case CEE_LDARG_0:
8725                 case CEE_LDARG_1:
8726                 case CEE_LDARG_2:
8727                 case CEE_LDARG_3:
8728                         CHECK_STACK_OVF (1);
8729                         n = (*ip)-CEE_LDARG_0;
8730                         CHECK_ARG (n);
8731                         EMIT_NEW_ARGLOAD (cfg, ins, n);
8732                         ip++;
8733                         *sp++ = ins;
8734                         break;
8735                 case CEE_LDLOC_0:
8736                 case CEE_LDLOC_1:
8737                 case CEE_LDLOC_2:
8738                 case CEE_LDLOC_3:
8739                         CHECK_STACK_OVF (1);
8740                         n = (*ip)-CEE_LDLOC_0;
8741                         CHECK_LOCAL (n);
8742                         EMIT_NEW_LOCLOAD (cfg, ins, n);
8743                         ip++;
8744                         *sp++ = ins;
8745                         break;
8746                 case CEE_STLOC_0:
8747                 case CEE_STLOC_1:
8748                 case CEE_STLOC_2:
8749                 case CEE_STLOC_3: {
8750                         CHECK_STACK (1);
8751                         n = (*ip)-CEE_STLOC_0;
8752                         CHECK_LOCAL (n);
8753                         --sp;
8754                         if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
8755                                 UNVERIFIED;
8756                         emit_stloc_ir (cfg, sp, header, n);
8757                         ++ip;
8758                         inline_costs += 1;
8759                         break;
8760                         }
8761                 case CEE_LDARG_S:
8762                         CHECK_OPSIZE (2);
8763                         CHECK_STACK_OVF (1);
8764                         n = ip [1];
8765                         CHECK_ARG (n);
8766                         EMIT_NEW_ARGLOAD (cfg, ins, n);
8767                         *sp++ = ins;
8768                         ip += 2;
8769                         break;
8770                 case CEE_LDARGA_S:
8771                         CHECK_OPSIZE (2);
8772                         CHECK_STACK_OVF (1);
8773                         n = ip [1];
8774                         CHECK_ARG (n);
8775                         NEW_ARGLOADA (cfg, ins, n);
8776                         MONO_ADD_INS (cfg->cbb, ins);
8777                         *sp++ = ins;
8778                         ip += 2;
8779                         break;
8780                 case CEE_STARG_S:
8781                         CHECK_OPSIZE (2);
8782                         CHECK_STACK (1);
8783                         --sp;
8784                         n = ip [1];
8785                         CHECK_ARG (n);
8786                         if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [ip [1]], *sp))
8787                                 UNVERIFIED;
8788                         EMIT_NEW_ARGSTORE (cfg, ins, n, *sp);
8789                         ip += 2;
8790                         break;
8791                 case CEE_LDLOC_S:
8792                         CHECK_OPSIZE (2);
8793                         CHECK_STACK_OVF (1);
8794                         n = ip [1];
8795                         CHECK_LOCAL (n);
8796                         EMIT_NEW_LOCLOAD (cfg, ins, n);
8797                         *sp++ = ins;
8798                         ip += 2;
8799                         break;
8800                 case CEE_LDLOCA_S: {
8801                         unsigned char *tmp_ip;
8802                         CHECK_OPSIZE (2);
8803                         CHECK_STACK_OVF (1);
8804                         CHECK_LOCAL (ip [1]);
8805
8806                         if ((tmp_ip = emit_optimized_ldloca_ir (cfg, ip, end, 1))) {
8807                                 ip = tmp_ip;
8808                                 inline_costs += 1;
8809                                 break;
8810                         }
8811
8812                         EMIT_NEW_LOCLOADA (cfg, ins, ip [1]);
8813                         *sp++ = ins;
8814                         ip += 2;
8815                         break;
8816                 }
8817                 case CEE_STLOC_S:
8818                         CHECK_OPSIZE (2);
8819                         CHECK_STACK (1);
8820                         --sp;
8821                         CHECK_LOCAL (ip [1]);
8822                         if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [ip [1]], *sp))
8823                                 UNVERIFIED;
8824                         emit_stloc_ir (cfg, sp, header, ip [1]);
8825                         ip += 2;
8826                         inline_costs += 1;
8827                         break;
8828                 case CEE_LDNULL:
8829                         CHECK_STACK_OVF (1);
8830                         EMIT_NEW_PCONST (cfg, ins, NULL);
8831                         ins->type = STACK_OBJ;
8832                         ++ip;
8833                         *sp++ = ins;
8834                         break;
8835                 case CEE_LDC_I4_M1:
8836                         CHECK_STACK_OVF (1);
8837                         EMIT_NEW_ICONST (cfg, ins, -1);
8838                         ++ip;
8839                         *sp++ = ins;
8840                         break;
8841                 case CEE_LDC_I4_0:
8842                 case CEE_LDC_I4_1:
8843                 case CEE_LDC_I4_2:
8844                 case CEE_LDC_I4_3:
8845                 case CEE_LDC_I4_4:
8846                 case CEE_LDC_I4_5:
8847                 case CEE_LDC_I4_6:
8848                 case CEE_LDC_I4_7:
8849                 case CEE_LDC_I4_8:
8850                         CHECK_STACK_OVF (1);
8851                         EMIT_NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
8852                         ++ip;
8853                         *sp++ = ins;
8854                         break;
8855                 case CEE_LDC_I4_S:
8856                         CHECK_OPSIZE (2);
8857                         CHECK_STACK_OVF (1);
8858                         ++ip;
8859                         EMIT_NEW_ICONST (cfg, ins, *((signed char*)ip));
8860                         ++ip;
8861                         *sp++ = ins;
8862                         break;
8863                 case CEE_LDC_I4:
8864                         CHECK_OPSIZE (5);
8865                         CHECK_STACK_OVF (1);
8866                         EMIT_NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
8867                         ip += 5;
8868                         *sp++ = ins;
8869                         break;
8870                 case CEE_LDC_I8:
8871                         CHECK_OPSIZE (9);
8872                         CHECK_STACK_OVF (1);
8873                         MONO_INST_NEW (cfg, ins, OP_I8CONST);
8874                         ins->type = STACK_I8;
8875                         ins->dreg = alloc_dreg (cfg, STACK_I8);
8876                         ++ip;
8877                         ins->inst_l = (gint64)read64 (ip);
8878                         MONO_ADD_INS (cfg->cbb, ins);
8879                         ip += 8;
8880                         *sp++ = ins;
8881                         break;
8882                 case CEE_LDC_R4: {
8883                         float *f;
8884                         gboolean use_aotconst = FALSE;
8885
8886 #ifdef TARGET_POWERPC
8887                         /* FIXME: Clean this up */
8888                         if (cfg->compile_aot)
8889                                 use_aotconst = TRUE;
8890 #endif
8891
8892                         /* FIXME: we should really allocate this only late in the compilation process */
8893                         f = (float *)mono_domain_alloc (cfg->domain, sizeof (float));
8894                         CHECK_OPSIZE (5);
8895                         CHECK_STACK_OVF (1);
8896
8897                         if (use_aotconst) {
8898                                 MonoInst *cons;
8899                                 int dreg;
8900
8901                                 EMIT_NEW_AOTCONST (cfg, cons, MONO_PATCH_INFO_R4, f);
8902
8903                                 dreg = alloc_freg (cfg);
8904                                 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADR4_MEMBASE, dreg, cons->dreg, 0);
8905                                 ins->type = cfg->r4_stack_type;
8906                         } else {
8907                                 MONO_INST_NEW (cfg, ins, OP_R4CONST);
8908                                 ins->type = cfg->r4_stack_type;
8909                                 ins->dreg = alloc_dreg (cfg, STACK_R8);
8910                                 ins->inst_p0 = f;
8911                                 MONO_ADD_INS (cfg->cbb, ins);
8912                         }
8913                         ++ip;
8914                         readr4 (ip, f);
8915                         ip += 4;
8916                         *sp++ = ins;                    
8917                         break;
8918                 }
8919                 case CEE_LDC_R8: {
8920                         double *d;
8921                         gboolean use_aotconst = FALSE;
8922
8923 #ifdef TARGET_POWERPC
8924                         /* FIXME: Clean this up */
8925                         if (cfg->compile_aot)
8926                                 use_aotconst = TRUE;
8927 #endif
8928
8929                         /* FIXME: we should really allocate this only late in the compilation process */
8930                         d = (double *)mono_domain_alloc (cfg->domain, sizeof (double));
8931                         CHECK_OPSIZE (9);
8932                         CHECK_STACK_OVF (1);
8933
8934                         if (use_aotconst) {
8935                                 MonoInst *cons;
8936                                 int dreg;
8937
8938                                 EMIT_NEW_AOTCONST (cfg, cons, MONO_PATCH_INFO_R8, d);
8939
8940                                 dreg = alloc_freg (cfg);
8941                                 EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOADR8_MEMBASE, dreg, cons->dreg, 0);
8942                                 ins->type = STACK_R8;
8943                         } else {
8944                                 MONO_INST_NEW (cfg, ins, OP_R8CONST);
8945                                 ins->type = STACK_R8;
8946                                 ins->dreg = alloc_dreg (cfg, STACK_R8);
8947                                 ins->inst_p0 = d;
8948                                 MONO_ADD_INS (cfg->cbb, ins);
8949                         }
8950                         ++ip;
8951                         readr8 (ip, d);
8952                         ip += 8;
8953                         *sp++ = ins;
8954                         break;
8955                 }
8956                 case CEE_DUP: {
8957                         MonoInst *temp, *store;
8958                         CHECK_STACK (1);
8959                         CHECK_STACK_OVF (1);
8960                         sp--;
8961                         ins = *sp;
8962
8963                         temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
8964                         EMIT_NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
8965
8966                         EMIT_NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
8967                         *sp++ = ins;
8968
8969                         EMIT_NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
8970                         *sp++ = ins;
8971
8972                         ++ip;
8973                         inline_costs += 2;
8974                         break;
8975                 }
8976                 case CEE_POP:
8977                         CHECK_STACK (1);
8978                         ip++;
8979                         --sp;
8980
8981 #ifdef TARGET_X86
8982                         if (sp [0]->type == STACK_R8)
8983                                 /* we need to pop the value from the x86 FP stack */
8984                                 MONO_EMIT_NEW_UNALU (cfg, OP_X86_FPOP, -1, sp [0]->dreg);
8985 #endif
8986                         break;
8987                 case CEE_JMP: {
8988                         MonoCallInst *call;
8989                         MonoMethodSignature *fsig;
8990                         int i, n;
8991
8992                         INLINE_FAILURE ("jmp");
8993                         GSHAREDVT_FAILURE (*ip);
8994
8995                         CHECK_OPSIZE (5);
8996                         if (stack_start != sp)
8997                                 UNVERIFIED;
8998                         token = read32 (ip + 1);
8999                         /* FIXME: check the signature matches */
9000                         cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
9001                         CHECK_CFG_ERROR;
9002  
9003                         if (cfg->gshared && mono_method_check_context_used (cmethod))
9004                                 GENERIC_SHARING_FAILURE (CEE_JMP);
9005
9006                         emit_instrumentation_call (cfg, mono_profiler_method_leave);
9007
9008                         fsig = mono_method_signature (cmethod);
9009                         n = fsig->param_count + fsig->hasthis;
9010                         if (cfg->llvm_only) {
9011                                 MonoInst **args;
9012
9013                                 args = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * n);
9014                                 for (i = 0; i < n; ++i)
9015                                         EMIT_NEW_ARGLOAD (cfg, args [i], i);
9016                                 ins = mono_emit_method_call_full (cfg, cmethod, fsig, TRUE, args, NULL, NULL, NULL);
9017                                 /*
9018                                  * The code in mono-basic-block.c treats the rest of the code as dead, but we
9019                                  * have to emit a normal return since llvm expects it.
9020                                  */
9021                                 if (cfg->ret)
9022                                         emit_setret (cfg, ins);
9023                                 MONO_INST_NEW (cfg, ins, OP_BR);
9024                                 ins->inst_target_bb = end_bblock;
9025                                 MONO_ADD_INS (cfg->cbb, ins);
9026                                 link_bblock (cfg, cfg->cbb, end_bblock);
9027                                 ip += 5;
9028                                 break;
9029                         } else if (cfg->backend->have_op_tail_call) {
9030                                 /* Handle tail calls similarly to calls */
9031                                 DISABLE_AOT (cfg);
9032
9033                                 MONO_INST_NEW_CALL (cfg, call, OP_TAILCALL);
9034                                 call->method = cmethod;
9035                                 call->tail_call = TRUE;
9036                                 call->signature = mono_method_signature (cmethod);
9037                                 call->args = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * n);
9038                                 call->inst.inst_p0 = cmethod;
9039                                 for (i = 0; i < n; ++i)
9040                                         EMIT_NEW_ARGLOAD (cfg, call->args [i], i);
9041
9042                                 mono_arch_emit_call (cfg, call);
9043                                 cfg->param_area = MAX(cfg->param_area, call->stack_usage);
9044                                 MONO_ADD_INS (cfg->cbb, (MonoInst*)call);
9045                         } else {
9046                                 for (i = 0; i < num_args; ++i)
9047                                         /* Prevent arguments from being optimized away */
9048                                         arg_array [i]->flags |= MONO_INST_VOLATILE;
9049
9050                                 MONO_INST_NEW_CALL (cfg, call, OP_JMP);
9051                                 ins = (MonoInst*)call;
9052                                 ins->inst_p0 = cmethod;
9053                                 MONO_ADD_INS (cfg->cbb, ins);
9054                         }
9055
9056                         ip += 5;
9057                         start_new_bblock = 1;
9058                         break;
9059                 }
9060                 case CEE_CALLI: {
9061                         MonoInst *addr;
9062                         MonoMethodSignature *fsig;
9063
9064                         CHECK_OPSIZE (5);
9065                         token = read32 (ip + 1);
9066
9067                         ins = NULL;
9068
9069                         //GSHAREDVT_FAILURE (*ip);
9070                         cmethod = NULL;
9071                         CHECK_STACK (1);
9072                         --sp;
9073                         addr = *sp;
9074                         fsig = mini_get_signature (method, token, generic_context);
9075
9076                         if (method->dynamic && fsig->pinvoke) {
9077                                 MonoInst *args [3];
9078
9079                                 /*
9080                                  * This is a call through a function pointer using a pinvoke
9081                                  * signature. Have to create a wrapper and call that instead.
9082                                  * FIXME: This is very slow, need to create a wrapper at JIT time
9083                                  * instead based on the signature.
9084                                  */
9085                                 EMIT_NEW_IMAGECONST (cfg, args [0], method->klass->image);
9086                                 EMIT_NEW_PCONST (cfg, args [1], fsig);
9087                                 args [2] = addr;
9088                                 addr = mono_emit_jit_icall (cfg, mono_get_native_calli_wrapper, args);
9089                         }
9090
9091                         n = fsig->param_count + fsig->hasthis;
9092
9093                         CHECK_STACK (n);
9094
9095                         //g_assert (!virtual_ || fsig->hasthis);
9096
9097                         sp -= n;
9098
9099                         inline_costs += 10 * num_calls++;
9100
9101                         /*
9102                          * Making generic calls out of gsharedvt methods.
9103                          * This needs to be used for all generic calls, not just ones with a gsharedvt signature, to avoid
9104                          * patching gshared method addresses into a gsharedvt method.
9105                          */
9106                         if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) {
9107                                 /*
9108                                  * We pass the address to the gsharedvt trampoline in the rgctx reg
9109                                  */
9110                                 MonoInst *callee = addr;
9111
9112                                 if (method->wrapper_type != MONO_WRAPPER_DELEGATE_INVOKE)
9113                                         /* Not tested */
9114                                         GSHAREDVT_FAILURE (*ip);
9115
9116                                 if (cfg->llvm_only)
9117                                         // FIXME:
9118                                         GSHAREDVT_FAILURE (*ip);
9119
9120                                 addr = emit_get_rgctx_sig (cfg, context_used,
9121                                                                                           fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
9122                                 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, callee);
9123                                 goto calli_end;
9124                         }
9125
9126                         /* Prevent inlining of methods with indirect calls */
9127                         INLINE_FAILURE ("indirect call");
9128
9129                         if (addr->opcode == OP_PCONST || addr->opcode == OP_AOTCONST || addr->opcode == OP_GOT_ENTRY) {
9130                                 MonoJumpInfoType info_type;
9131                                 gpointer info_data;
9132
9133                                 /*
9134                                  * Instead of emitting an indirect call, emit a direct call
9135                                  * with the contents of the aotconst as the patch info.
9136                                  */
9137                                 if (addr->opcode == OP_PCONST || addr->opcode == OP_AOTCONST) {
9138                                         info_type = (MonoJumpInfoType)addr->inst_c1;
9139                                         info_data = addr->inst_p0;
9140                                 } else {
9141                                         info_type = (MonoJumpInfoType)addr->inst_right->inst_c1;
9142                                         info_data = addr->inst_right->inst_left;
9143                                 }
9144
9145                                 if (info_type == MONO_PATCH_INFO_ICALL_ADDR || info_type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
9146                                         ins = (MonoInst*)mono_emit_abs_call (cfg, info_type, info_data, fsig, sp);
9147                                         NULLIFY_INS (addr);
9148                                         goto calli_end;
9149                                 }
9150                         }
9151                         ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
9152
9153                         calli_end:
9154
9155                         /* End of call, INS should contain the result of the call, if any */
9156
9157                         if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9158                                 g_assert (ins);
9159                                 *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
9160                         }
9161
9162                         CHECK_CFG_EXCEPTION;
9163
9164                         ip += 5;
9165                         ins_flag = 0;
9166                         constrained_class = NULL;
9167                         break;
9168                 }
9169                 case CEE_CALL:
9170                 case CEE_CALLVIRT: {
9171                         MonoInst *addr = NULL;
9172                         MonoMethodSignature *fsig = NULL;
9173                         int array_rank = 0;
9174                         int virtual_ = *ip == CEE_CALLVIRT;
9175                         gboolean pass_imt_from_rgctx = FALSE;
9176                         MonoInst *imt_arg = NULL;
9177                         MonoInst *keep_this_alive = NULL;
9178                         gboolean pass_vtable = FALSE;
9179                         gboolean pass_mrgctx = FALSE;
9180                         MonoInst *vtable_arg = NULL;
9181                         gboolean check_this = FALSE;
9182                         gboolean supported_tail_call = FALSE;
9183                         gboolean tail_call = FALSE;
9184                         gboolean need_seq_point = FALSE;
9185                         guint32 call_opcode = *ip;
9186                         gboolean emit_widen = TRUE;
9187                         gboolean push_res = TRUE;
9188                         gboolean skip_ret = FALSE;
9189                         gboolean delegate_invoke = FALSE;
9190                         gboolean direct_icall = FALSE;
9191                         gboolean constrained_partial_call = FALSE;
9192                         MonoMethod *cil_method;
9193
9194                         CHECK_OPSIZE (5);
9195                         token = read32 (ip + 1);
9196
9197                         ins = NULL;
9198
9199                         cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
9200                         CHECK_CFG_ERROR;
9201
9202                         cil_method = cmethod;
9203                                 
9204                         if (constrained_class) {
9205                                 if ((constrained_class->byval_arg.type == MONO_TYPE_VAR || constrained_class->byval_arg.type == MONO_TYPE_MVAR) && cfg->gshared) {
9206                                         if (!mini_is_gsharedvt_klass (constrained_class)) {
9207                                                 g_assert (!cmethod->klass->valuetype);
9208                                                 if (!mini_type_is_reference (&constrained_class->byval_arg))
9209                                                         constrained_partial_call = TRUE;
9210                                         }
9211                                 }
9212
9213                                 if (method->wrapper_type != MONO_WRAPPER_NONE) {
9214                                         if (cfg->verbose_level > 2)
9215                                                 printf ("DM Constrained call to %s\n", mono_type_get_full_name (constrained_class));
9216                                         if (!((constrained_class->byval_arg.type == MONO_TYPE_VAR ||
9217                                                    constrained_class->byval_arg.type == MONO_TYPE_MVAR) &&
9218                                                   cfg->gshared)) {
9219                                                 cmethod = mono_get_method_constrained_with_method (image, cil_method, constrained_class, generic_context, &cfg->error);
9220                                                 CHECK_CFG_ERROR;
9221                                         }
9222                                 } else {
9223                                         if (cfg->verbose_level > 2)
9224                                                 printf ("Constrained call to %s\n", mono_type_get_full_name (constrained_class));
9225
9226                                         if ((constrained_class->byval_arg.type == MONO_TYPE_VAR || constrained_class->byval_arg.type == MONO_TYPE_MVAR) && cfg->gshared) {
9227                                                 /* 
9228                                                  * This is needed since get_method_constrained can't find 
9229                                                  * the method in klass representing a type var.
9230                                                  * The type var is guaranteed to be a reference type in this
9231                                                  * case.
9232                                                  */
9233                                                 if (!mini_is_gsharedvt_klass (constrained_class))
9234                                                         g_assert (!cmethod->klass->valuetype);
9235                                         } else {
9236                                                 cmethod = mono_get_method_constrained_checked (image, token, constrained_class, generic_context, &cil_method, &cfg->error);
9237                                                 CHECK_CFG_ERROR;
9238                                         }
9239                                 }
9240                         }
9241                                         
9242                         if (!cmethod || mono_loader_get_last_error ()) {
9243                                 if (mono_loader_get_last_error ()) {
9244                                         mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
9245                                         mono_error_set_from_loader_error (&cfg->error);
9246                                         CHECK_CFG_ERROR;
9247                                 } else {
9248                                         LOAD_ERROR;
9249                                 }
9250                         }
9251                         if (!dont_verify && !cfg->skip_visibility) {
9252                                 MonoMethod *target_method = cil_method;
9253                                 if (method->is_inflated) {
9254                                         target_method = mini_get_method_allow_open (method, token, NULL, &(mono_method_get_generic_container (method_definition)->context), &cfg->error);
9255                                         CHECK_CFG_ERROR;
9256                                 }
9257                                 if (!mono_method_can_access_method (method_definition, target_method) &&
9258                                         !mono_method_can_access_method (method, cil_method))
9259                                         METHOD_ACCESS_FAILURE (method, cil_method);
9260                         }
9261
9262                         if (mono_security_core_clr_enabled ())
9263                                 ensure_method_is_allowed_to_call_method (cfg, method, cil_method);
9264
9265                         if (!virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_ABSTRACT))
9266                                 /* MS.NET seems to silently convert this to a callvirt */
9267                                 virtual_ = 1;
9268
9269                         {
9270                                 /*
9271                                  * MS.NET accepts non virtual calls to virtual final methods of transparent proxy classes and
9272                                  * converts to a callvirt.
9273                                  *
9274                                  * tests/bug-515884.il is an example of this behavior
9275                                  */
9276                                 const int test_flags = METHOD_ATTRIBUTE_VIRTUAL | METHOD_ATTRIBUTE_FINAL | METHOD_ATTRIBUTE_STATIC;
9277                                 const int expected_flags = METHOD_ATTRIBUTE_VIRTUAL | METHOD_ATTRIBUTE_FINAL;
9278                                 if (!virtual_ && mono_class_is_marshalbyref (cmethod->klass) && (cmethod->flags & test_flags) == expected_flags && cfg->method->wrapper_type == MONO_WRAPPER_NONE)
9279                                         virtual_ = 1;
9280                         }
9281
9282                         if (!cmethod->klass->inited)
9283                                 if (!mono_class_init (cmethod->klass))
9284                                         TYPE_LOAD_ERROR (cmethod->klass);
9285
9286                         fsig = mono_method_signature (cmethod);
9287                         if (!fsig)
9288                                 LOAD_ERROR;
9289                         if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
9290                                 mini_class_is_system_array (cmethod->klass)) {
9291                                 array_rank = cmethod->klass->rank;
9292                         } else if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && icall_is_direct_callable (cfg, cmethod)) {
9293                                 direct_icall = TRUE;
9294                         } else if (fsig->pinvoke) {
9295                                 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod, TRUE, cfg->compile_aot);
9296                                 fsig = mono_method_signature (wrapper);
9297                         } else if (constrained_class) {
9298                         } else {
9299                                 fsig = mono_method_get_signature_checked (cmethod, image, token, generic_context, &cfg->error);
9300                                 CHECK_CFG_ERROR;
9301                         }
9302
9303                         if (cfg->llvm_only && !cfg->method->wrapper_type)
9304                                 cfg->signatures = g_slist_prepend_mempool (cfg->mempool, cfg->signatures, fsig);
9305
9306                         /* See code below */
9307                         if (cmethod->klass == mono_defaults.monitor_class && !strcmp (cmethod->name, "Enter") && mono_method_signature (cmethod)->param_count == 1) {
9308                                 MonoBasicBlock *tbb;
9309
9310                                 GET_BBLOCK (cfg, tbb, ip + 5);
9311                                 if (tbb->try_start && MONO_REGION_FLAGS(tbb->region) == MONO_EXCEPTION_CLAUSE_FINALLY) {
9312                                         /*
9313                                          * We want to extend the try block to cover the call, but we can't do it if the
9314                                          * call is made directly since its followed by an exception check.
9315                                          */
9316                                         direct_icall = FALSE;
9317                                 }
9318                         }
9319
9320                         mono_save_token_info (cfg, image, token, cil_method);
9321
9322                         if (!(seq_point_locs && mono_bitset_test_fast (seq_point_locs, ip + 5 - header->code)))
9323                                 need_seq_point = TRUE;
9324
9325                         /* Don't support calls made using type arguments for now */
9326                         /*
9327                           if (cfg->gsharedvt) {
9328                           if (mini_is_gsharedvt_signature (fsig))
9329                           GSHAREDVT_FAILURE (*ip);
9330                           }
9331                         */
9332
9333                         if (cmethod->string_ctor && method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE)
9334                                 g_assert_not_reached ();
9335
9336                         n = fsig->param_count + fsig->hasthis;
9337
9338                         if (!cfg->gshared && cmethod->klass->generic_container)
9339                                 UNVERIFIED;
9340
9341                         if (!cfg->gshared)
9342                                 g_assert (!mono_method_check_context_used (cmethod));
9343
9344                         CHECK_STACK (n);
9345
9346                         //g_assert (!virtual_ || fsig->hasthis);
9347
9348                         sp -= n;
9349
9350                         /*
9351                          * We have the `constrained.' prefix opcode.
9352                          */
9353                         if (constrained_class) {
9354                                 if (mini_is_gsharedvt_klass (constrained_class)) {
9355                                         if ((cmethod->klass != mono_defaults.object_class) && constrained_class->valuetype && cmethod->klass->valuetype) {
9356                                                 /* The 'Own method' case below */
9357                                         } else if (cmethod->klass->image != mono_defaults.corlib && !(cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE) && !cmethod->klass->valuetype) {
9358                                                 /* 'The type parameter is instantiated as a reference type' case below. */
9359                                         } else {
9360                                                 ins = handle_constrained_gsharedvt_call (cfg, cmethod, fsig, sp, constrained_class, &emit_widen);
9361                                                 CHECK_CFG_EXCEPTION;
9362                                                 g_assert (ins);
9363                                                 goto call_end;
9364                                         }
9365                                 }
9366
9367                                 if (constrained_partial_call) {
9368                                         gboolean need_box = TRUE;
9369
9370                                         /*
9371                                          * The receiver is a valuetype, but the exact type is not known at compile time. This means the
9372                                          * called method is not known at compile time either. The called method could end up being
9373                                          * one of the methods on the parent classes (object/valuetype/enum), in which case we need
9374                                          * to box the receiver.
9375                                          * A simple solution would be to box always and make a normal virtual call, but that would
9376                                          * be bad performance wise.
9377                                          */
9378                                         if (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE && cmethod->klass->generic_class) {
9379                                                 /*
9380                                                  * The parent classes implement no generic interfaces, so the called method will be a vtype method, so no boxing neccessary.
9381                                                  */
9382                                                 need_box = FALSE;
9383                                         }
9384
9385                                         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)) {
9386                                                 /* The called method is not virtual, i.e. Object:GetType (), the receiver is a vtype, has to box */
9387                                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
9388                                                 ins->klass = constrained_class;
9389                                                 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
9390                                                 CHECK_CFG_EXCEPTION;
9391                                         } else if (need_box) {
9392                                                 MonoInst *box_type;
9393                                                 MonoBasicBlock *is_ref_bb, *end_bb;
9394                                                 MonoInst *nonbox_call;
9395
9396                                                 /*
9397                                                  * Determine at runtime whenever the called method is defined on object/valuetype/enum, and emit a boxing call
9398                                                  * if needed.
9399                                                  * FIXME: It is possible to inline the called method in a lot of cases, i.e. for T_INT,
9400                                                  * the no-box case goes to a method in Int32, while the box case goes to a method in Enum.
9401                                                  */
9402                                                 addr = emit_get_rgctx_virt_method (cfg, mono_class_check_context_used (constrained_class), constrained_class, cmethod, MONO_RGCTX_INFO_VIRT_METHOD_CODE);
9403
9404                                                 NEW_BBLOCK (cfg, is_ref_bb);
9405                                                 NEW_BBLOCK (cfg, end_bb);
9406
9407                                                 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);
9408                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, box_type->dreg, MONO_GSHAREDVT_BOX_TYPE_REF);
9409                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
9410
9411                                                 /* Non-ref case */
9412                                                 nonbox_call = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
9413
9414                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
9415
9416                                                 /* Ref case */
9417                                                 MONO_START_BB (cfg, is_ref_bb);
9418                                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
9419                                                 ins->klass = constrained_class;
9420                                                 sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
9421                                                 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
9422
9423                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
9424
9425                                                 MONO_START_BB (cfg, end_bb);
9426                                                 cfg->cbb = end_bb;
9427
9428                                                 nonbox_call->dreg = ins->dreg;
9429                                                 goto call_end;
9430                                         } else {
9431                                                 g_assert (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE);
9432                                                 addr = emit_get_rgctx_virt_method (cfg, mono_class_check_context_used (constrained_class), constrained_class, cmethod, MONO_RGCTX_INFO_VIRT_METHOD_CODE);
9433                                                 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
9434                                                 goto call_end;
9435                                         }
9436                                 } else if (constrained_class->valuetype && (cmethod->klass == mono_defaults.object_class || cmethod->klass == mono_defaults.enum_class->parent || cmethod->klass == mono_defaults.enum_class)) {
9437                                         /*
9438                                          * The type parameter is instantiated as a valuetype,
9439                                          * but that type doesn't override the method we're
9440                                          * calling, so we need to box `this'.
9441                                          */
9442                                         EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
9443                                         ins->klass = constrained_class;
9444                                         sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
9445                                         CHECK_CFG_EXCEPTION;
9446                                 } else if (!constrained_class->valuetype) {
9447                                         int dreg = alloc_ireg_ref (cfg);
9448
9449                                         /*
9450                                          * The type parameter is instantiated as a reference
9451                                          * type.  We have a managed pointer on the stack, so
9452                                          * we need to dereference it here.
9453                                          */
9454                                         EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, sp [0]->dreg, 0);
9455                                         ins->type = STACK_OBJ;
9456                                         sp [0] = ins;
9457                                 } else {
9458                                         if (cmethod->klass->valuetype) {
9459                                                 /* Own method */
9460                                         } else {
9461                                                 /* Interface method */
9462                                                 int ioffset, slot;
9463
9464                                                 mono_class_setup_vtable (constrained_class);
9465                                                 CHECK_TYPELOAD (constrained_class);
9466                                                 ioffset = mono_class_interface_offset (constrained_class, cmethod->klass);
9467                                                 if (ioffset == -1)
9468                                                         TYPE_LOAD_ERROR (constrained_class);
9469                                                 slot = mono_method_get_vtable_slot (cmethod);
9470                                                 if (slot == -1)
9471                                                         TYPE_LOAD_ERROR (cmethod->klass);
9472                                                 cmethod = constrained_class->vtable [ioffset + slot];
9473
9474                                                 if (cmethod->klass == mono_defaults.enum_class) {
9475                                                         /* Enum implements some interfaces, so treat this as the first case */
9476                                                         EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_class->byval_arg, sp [0]->dreg, 0);
9477                                                         ins->klass = constrained_class;
9478                                                         sp [0] = handle_box (cfg, ins, constrained_class, mono_class_check_context_used (constrained_class));
9479                                                         CHECK_CFG_EXCEPTION;
9480                                                 }
9481                                         }
9482                                         virtual_ = 0;
9483                                 }
9484                                 constrained_class = NULL;
9485                         }
9486
9487                         if (check_call_signature (cfg, fsig, sp))
9488                                 UNVERIFIED;
9489
9490                         if ((cmethod->klass->parent == mono_defaults.multicastdelegate_class) && !strcmp (cmethod->name, "Invoke"))
9491                                 delegate_invoke = TRUE;
9492
9493                         if ((cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_sharable_method (cfg, cmethod, fsig, sp))) {
9494                                 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9495                                         type_to_eval_stack_type ((cfg), fsig->ret, ins);
9496                                         emit_widen = FALSE;
9497                                 }
9498
9499                                 goto call_end;
9500                         }
9501
9502                         /* 
9503                          * If the callee is a shared method, then its static cctor
9504                          * might not get called after the call was patched.
9505                          */
9506                         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)) {
9507                                 emit_class_init (cfg, cmethod->klass);
9508                                 CHECK_TYPELOAD (cmethod->klass);
9509                         }
9510
9511                         check_method_sharing (cfg, cmethod, &pass_vtable, &pass_mrgctx);
9512
9513                         if (cfg->gshared) {
9514                                 MonoGenericContext *cmethod_context = mono_method_get_context (cmethod);
9515
9516                                 context_used = mini_method_check_context_used (cfg, cmethod);
9517
9518                                 if (context_used && (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
9519                                         /* Generic method interface
9520                                            calls are resolved via a
9521                                            helper function and don't
9522                                            need an imt. */
9523                                         if (!cmethod_context || !cmethod_context->method_inst)
9524                                                 pass_imt_from_rgctx = TRUE;
9525                                 }
9526
9527                                 /*
9528                                  * If a shared method calls another
9529                                  * shared method then the caller must
9530                                  * have a generic sharing context
9531                                  * because the magic trampoline
9532                                  * requires it.  FIXME: We shouldn't
9533                                  * have to force the vtable/mrgctx
9534                                  * variable here.  Instead there
9535                                  * should be a flag in the cfg to
9536                                  * request a generic sharing context.
9537                                  */
9538                                 if (context_used &&
9539                                                 ((method->flags & METHOD_ATTRIBUTE_STATIC) || method->klass->valuetype))
9540                                         mono_get_vtable_var (cfg);
9541                         }
9542
9543                         if (pass_vtable) {
9544                                 if (context_used) {
9545                                         vtable_arg = emit_get_rgctx_klass (cfg, context_used, cmethod->klass, MONO_RGCTX_INFO_VTABLE);
9546                                 } else {
9547                                         MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
9548
9549                                         CHECK_TYPELOAD (cmethod->klass);
9550                                         EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
9551                                 }
9552                         }
9553
9554                         if (pass_mrgctx) {
9555                                 g_assert (!vtable_arg);
9556
9557                                 if (!cfg->compile_aot) {
9558                                         /* 
9559                                          * emit_get_rgctx_method () calls mono_class_vtable () so check 
9560                                          * for type load errors before.
9561                                          */
9562                                         mono_class_setup_vtable (cmethod->klass);
9563                                         CHECK_TYPELOAD (cmethod->klass);
9564                                 }
9565
9566                                 vtable_arg = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_METHOD_RGCTX);
9567
9568                                 /* !marshalbyref is needed to properly handle generic methods + remoting */
9569                                 if ((!(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
9570                                          MONO_METHOD_IS_FINAL (cmethod)) &&
9571                                         !mono_class_is_marshalbyref (cmethod->klass)) {
9572                                         if (virtual_)
9573                                                 check_this = TRUE;
9574                                         virtual_ = 0;
9575                                 }
9576                         }
9577
9578                         if (pass_imt_from_rgctx) {
9579                                 g_assert (!pass_vtable);
9580
9581                                 imt_arg = emit_get_rgctx_method (cfg, context_used,
9582                                         cmethod, MONO_RGCTX_INFO_METHOD);
9583                         }
9584
9585                         if (check_this)
9586                                 MONO_EMIT_NEW_CHECK_THIS (cfg, sp [0]->dreg);
9587
9588                         /* Calling virtual generic methods */
9589                         if (virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) &&
9590                             !(MONO_METHOD_IS_FINAL (cmethod) && 
9591                               cmethod->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK) &&
9592                             fsig->generic_param_count && 
9593                                 !(cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) &&
9594                                 !cfg->llvm_only) {
9595                                 MonoInst *this_temp, *this_arg_temp, *store;
9596                                 MonoInst *iargs [4];
9597
9598                                 g_assert (fsig->is_inflated);
9599
9600                                 /* Prevent inlining of methods that contain indirect calls */
9601                                 INLINE_FAILURE ("virtual generic call");
9602
9603                                 if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig))
9604                                         GSHAREDVT_FAILURE (*ip);
9605
9606                                 if (cfg->backend->have_generalized_imt_thunk && cfg->backend->gshared_supported && cmethod->wrapper_type == MONO_WRAPPER_NONE) {
9607                                         g_assert (!imt_arg);
9608                                         if (!context_used)
9609                                                 g_assert (cmethod->is_inflated);
9610                                         imt_arg = emit_get_rgctx_method (cfg, context_used,
9611                                                                                                          cmethod, MONO_RGCTX_INFO_METHOD);
9612                                         ins = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, sp [0], imt_arg, NULL);
9613                                 } else {
9614                                         this_temp = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
9615                                         NEW_TEMPSTORE (cfg, store, this_temp->inst_c0, sp [0]);
9616                                         MONO_ADD_INS (cfg->cbb, store);
9617
9618                                         /* FIXME: This should be a managed pointer */
9619                                         this_arg_temp = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
9620
9621                                         EMIT_NEW_TEMPLOAD (cfg, iargs [0], this_temp->inst_c0);
9622                                         iargs [1] = emit_get_rgctx_method (cfg, context_used,
9623                                                                                                            cmethod, MONO_RGCTX_INFO_METHOD);
9624                                         EMIT_NEW_TEMPLOADA (cfg, iargs [2], this_arg_temp->inst_c0);
9625                                         addr = mono_emit_jit_icall (cfg,
9626                                                                                                 mono_helper_compile_generic_method, iargs);
9627
9628                                         EMIT_NEW_TEMPLOAD (cfg, sp [0], this_arg_temp->inst_c0);
9629
9630                                         ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
9631                                 }
9632
9633                                 goto call_end;
9634                         }
9635
9636                         /*
9637                          * Implement a workaround for the inherent races involved in locking:
9638                          * Monitor.Enter ()
9639                          * try {
9640                          * } finally {
9641                          *    Monitor.Exit ()
9642                          * }
9643                          * If a thread abort happens between the call to Monitor.Enter () and the start of the
9644                          * try block, the Exit () won't be executed, see:
9645                          * http://www.bluebytesoftware.com/blog/2007/01/30/MonitorEnterThreadAbortsAndOrphanedLocks.aspx
9646                          * To work around this, we extend such try blocks to include the last x bytes
9647                          * of the Monitor.Enter () call.
9648                          */
9649                         if (cmethod->klass == mono_defaults.monitor_class && !strcmp (cmethod->name, "Enter") && mono_method_signature (cmethod)->param_count == 1) {
9650                                 MonoBasicBlock *tbb;
9651
9652                                 GET_BBLOCK (cfg, tbb, ip + 5);
9653                                 /* 
9654                                  * Only extend try blocks with a finally, to avoid catching exceptions thrown
9655                                  * from Monitor.Enter like ArgumentNullException.
9656                                  */
9657                                 if (tbb->try_start && MONO_REGION_FLAGS(tbb->region) == MONO_EXCEPTION_CLAUSE_FINALLY) {
9658                                         /* Mark this bblock as needing to be extended */
9659                                         tbb->extend_try_block = TRUE;
9660                                 }
9661                         }
9662
9663                         /* Conversion to a JIT intrinsic */
9664                         if ((cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_method (cfg, cmethod, fsig, sp))) {
9665                                 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9666                                         type_to_eval_stack_type ((cfg), fsig->ret, ins);
9667                                         emit_widen = FALSE;
9668                                 }
9669                                 goto call_end;
9670                         }
9671
9672                         /* Inlining */
9673                         if ((cfg->opt & MONO_OPT_INLINE) &&
9674                                 (!virtual_ || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || MONO_METHOD_IS_FINAL (cmethod)) &&
9675                             mono_method_check_inlining (cfg, cmethod)) {
9676                                 int costs;
9677                                 gboolean always = FALSE;
9678
9679                                 if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
9680                                         (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
9681                                         /* Prevent inlining of methods that call wrappers */
9682                                         INLINE_FAILURE ("wrapper call");
9683                                         cmethod = mono_marshal_get_native_wrapper (cmethod, TRUE, FALSE);
9684                                         always = TRUE;
9685                                 }
9686
9687                                 costs = inline_method (cfg, cmethod, fsig, sp, ip, cfg->real_offset, always);
9688                                 if (costs) {
9689                                         cfg->real_offset += 5;
9690
9691                                         if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9692                                                 /* *sp is already set by inline_method */
9693                                                 sp++;
9694                                                 push_res = FALSE;
9695                                         }
9696
9697                                         inline_costs += costs;
9698
9699                                         goto call_end;
9700                                 }
9701                         }
9702
9703                         /* Tail recursion elimination */
9704                         if ((cfg->opt & MONO_OPT_TAILC) && call_opcode == CEE_CALL && cmethod == method && ip [5] == CEE_RET && !vtable_arg) {
9705                                 gboolean has_vtargs = FALSE;
9706                                 int i;
9707
9708                                 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
9709                                 INLINE_FAILURE ("tail call");
9710
9711                                 /* keep it simple */
9712                                 for (i =  fsig->param_count - 1; i >= 0; i--) {
9713                                         if (MONO_TYPE_ISSTRUCT (mono_method_signature (cmethod)->params [i])) 
9714                                                 has_vtargs = TRUE;
9715                                 }
9716
9717                                 if (!has_vtargs) {
9718                                         for (i = 0; i < n; ++i)
9719                                                 EMIT_NEW_ARGSTORE (cfg, ins, i, sp [i]);
9720                                         MONO_INST_NEW (cfg, ins, OP_BR);
9721                                         MONO_ADD_INS (cfg->cbb, ins);
9722                                         tblock = start_bblock->out_bb [0];
9723                                         link_bblock (cfg, cfg->cbb, tblock);
9724                                         ins->inst_target_bb = tblock;
9725                                         start_new_bblock = 1;
9726
9727                                         /* skip the CEE_RET, too */
9728                                         if (ip_in_bb (cfg, cfg->cbb, ip + 5))
9729                                                 skip_ret = TRUE;
9730                                         push_res = FALSE;
9731                                         goto call_end;
9732                                 }
9733                         }
9734
9735                         inline_costs += 10 * num_calls++;
9736
9737                         /*
9738                          * Making generic calls out of gsharedvt methods.
9739                          * This needs to be used for all generic calls, not just ones with a gsharedvt signature, to avoid
9740                          * patching gshared method addresses into a gsharedvt method.
9741                          */
9742                         if (cfg->gsharedvt && (mini_is_gsharedvt_signature (fsig) || cmethod->is_inflated || cmethod->klass->generic_class) &&
9743                                 !(cmethod->klass->rank && cmethod->klass->byval_arg.type != MONO_TYPE_SZARRAY) &&
9744                                 (!(cfg->llvm_only && virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL)))) {
9745                                 MonoRgctxInfoType info_type;
9746
9747                                 if (virtual_) {
9748                                         //if (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE)
9749                                                 //GSHAREDVT_FAILURE (*ip);
9750                                         // disable for possible remoting calls
9751                                         if (fsig->hasthis && (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class))
9752                                                 GSHAREDVT_FAILURE (*ip);
9753                                         if (fsig->generic_param_count) {
9754                                                 /* virtual generic call */
9755                                                 g_assert (!imt_arg);
9756                                                 /* Same as the virtual generic case above */
9757                                                 imt_arg = emit_get_rgctx_method (cfg, context_used,
9758                                                                                                                  cmethod, MONO_RGCTX_INFO_METHOD);
9759                                                 /* This is not needed, as the trampoline code will pass one, and it might be passed in the same reg as the imt arg */
9760                                                 vtable_arg = NULL;
9761                                         } else if ((cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE) && !imt_arg) {
9762                                                 /* This can happen when we call a fully instantiated iface method */
9763                                                 imt_arg = emit_get_rgctx_method (cfg, context_used,
9764                                                                                                                  cmethod, MONO_RGCTX_INFO_METHOD);
9765                                                 vtable_arg = NULL;
9766                                         }
9767                                 }
9768
9769                                 if ((cmethod->klass->parent == mono_defaults.multicastdelegate_class) && (!strcmp (cmethod->name, "Invoke")))
9770                                         keep_this_alive = sp [0];
9771
9772                                 if (virtual_ && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL))
9773                                         info_type = MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE_VIRT;
9774                                 else
9775                                         info_type = MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE;
9776                                 addr = emit_get_rgctx_gsharedvt_call (cfg, context_used, fsig, cmethod, info_type);
9777
9778                                 if (cfg->llvm_only) {
9779                                         // FIXME: Avoid initializing vtable_arg
9780                                         ins = emit_llvmonly_calli (cfg, fsig, sp, addr);
9781                                 } else {
9782                                         ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, imt_arg, vtable_arg);
9783                                 }
9784                                 goto call_end;
9785                         }
9786
9787                         /* Generic sharing */
9788
9789                         /*
9790                          * Use this if the callee is gsharedvt sharable too, since
9791                          * at runtime we might find an instantiation so the call cannot
9792                          * be patched (the 'no_patch' code path in mini-trampolines.c).
9793                          */
9794                         if (context_used && !imt_arg && !array_rank && !delegate_invoke &&
9795                                 (!mono_method_is_generic_sharable_full (cmethod, TRUE, FALSE, FALSE) ||
9796                                  !mono_class_generic_sharing_enabled (cmethod->klass)) &&
9797                                 (!virtual_ || MONO_METHOD_IS_FINAL (cmethod) ||
9798                                  !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL))) {
9799                                 INLINE_FAILURE ("gshared");
9800
9801                                 g_assert (cfg->gshared && cmethod);
9802                                 g_assert (!addr);
9803
9804                                 /*
9805                                  * We are compiling a call to a
9806                                  * generic method from shared code,
9807                                  * which means that we have to look up
9808                                  * the method in the rgctx and do an
9809                                  * indirect call.
9810                                  */
9811                                 if (fsig->hasthis)
9812                                         MONO_EMIT_NEW_CHECK_THIS (cfg, sp [0]->dreg);
9813
9814                                 if (cfg->llvm_only) {
9815                                         if (cfg->gsharedvt && mini_is_gsharedvt_variable_signature (fsig))
9816                                                 addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GSHAREDVT_OUT_WRAPPER);
9817                                         else
9818                                                 addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
9819                                         // FIXME: Avoid initializing imt_arg/vtable_arg
9820                                         ins = emit_llvmonly_calli (cfg, fsig, sp, addr);
9821                                 } else {
9822                                         addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
9823                                         ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, imt_arg, vtable_arg);
9824                                 }
9825                                 goto call_end;
9826                         }
9827
9828                         /* Direct calls to icalls */
9829                         if (direct_icall) {
9830                                 MonoMethod *wrapper;
9831                                 int costs;
9832
9833                                 /* Inline the wrapper */
9834                                 wrapper = mono_marshal_get_native_wrapper (cmethod, TRUE, cfg->compile_aot);
9835
9836                                 costs = inline_method (cfg, wrapper, fsig, sp, ip, cfg->real_offset, TRUE);
9837                                 g_assert (costs > 0);
9838                                 cfg->real_offset += 5;
9839
9840                                 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
9841                                         /* *sp is already set by inline_method */
9842                                         sp++;
9843                                         push_res = FALSE;
9844                                 }
9845
9846                                 inline_costs += costs;
9847
9848                                 goto call_end;
9849                         }
9850                                         
9851                         /* Array methods */
9852                         if (array_rank) {
9853                                 MonoInst *addr;
9854
9855                                 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */ 
9856                                         MonoInst *val = sp [fsig->param_count];
9857
9858                                         if (val->type == STACK_OBJ) {
9859                                                 MonoInst *iargs [2];
9860
9861                                                 iargs [0] = sp [0];
9862                                                 iargs [1] = val;
9863                                                 
9864                                                 mono_emit_jit_icall (cfg, mono_helper_stelem_ref_check, iargs);
9865                                         }
9866                                         
9867                                         addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, TRUE);
9868                                         EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, fsig->params [fsig->param_count - 1], addr->dreg, 0, val->dreg);
9869                                         if (cfg->gen_write_barriers && val->type == STACK_OBJ && !(val->opcode == OP_PCONST && val->inst_c0 == 0))
9870                                                 emit_write_barrier (cfg, addr, val);
9871                                         if (cfg->gen_write_barriers && mini_is_gsharedvt_klass (cmethod->klass))
9872                                                 GSHAREDVT_FAILURE (*ip);
9873                                 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
9874                                         addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, FALSE);
9875
9876                                         EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, fsig->ret, addr->dreg, 0);
9877                                 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
9878                                         if (!cmethod->klass->element_class->valuetype && !readonly)
9879                                                 mini_emit_check_array_type (cfg, sp [0], cmethod->klass);
9880                                         CHECK_TYPELOAD (cmethod->klass);
9881                                         
9882                                         readonly = FALSE;
9883                                         addr = mini_emit_ldelema_ins (cfg, cmethod, sp, ip, FALSE);
9884                                         ins = addr;
9885                                 } else {
9886                                         g_assert_not_reached ();
9887                                 }
9888
9889                                 emit_widen = FALSE;
9890                                 goto call_end;
9891                         }
9892
9893                         ins = mini_redirect_call (cfg, cmethod, fsig, sp, virtual_ ? sp [0] : NULL);
9894                         if (ins)
9895                                 goto call_end;
9896
9897                         /* Tail prefix / tail call optimization */
9898
9899                         /* FIXME: Enabling TAILC breaks some inlining/stack trace/etc tests */
9900                         /* FIXME: runtime generic context pointer for jumps? */
9901                         /* FIXME: handle this for generic sharing eventually */
9902                         if ((ins_flag & MONO_INST_TAILCALL) &&
9903                                 !vtable_arg && !cfg->gshared && is_supported_tail_call (cfg, method, cmethod, fsig, call_opcode))
9904                                 supported_tail_call = TRUE;
9905
9906                         if (supported_tail_call) {
9907                                 MonoCallInst *call;
9908
9909                                 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
9910                                 INLINE_FAILURE ("tail call");
9911
9912                                 //printf ("HIT: %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
9913
9914                                 if (cfg->backend->have_op_tail_call) {
9915                                         /* Handle tail calls similarly to normal calls */
9916                                         tail_call = TRUE;
9917                                 } else {
9918                                         emit_instrumentation_call (cfg, mono_profiler_method_leave);
9919
9920                                         MONO_INST_NEW_CALL (cfg, call, OP_JMP);
9921                                         call->tail_call = TRUE;
9922                                         call->method = cmethod;
9923                                         call->signature = mono_method_signature (cmethod);
9924
9925                                         /*
9926                                          * We implement tail calls by storing the actual arguments into the 
9927                                          * argument variables, then emitting a CEE_JMP.
9928                                          */
9929                                         for (i = 0; i < n; ++i) {
9930                                                 /* Prevent argument from being register allocated */
9931                                                 arg_array [i]->flags |= MONO_INST_VOLATILE;
9932                                                 EMIT_NEW_ARGSTORE (cfg, ins, i, sp [i]);
9933                                         }
9934                                         ins = (MonoInst*)call;
9935                                         ins->inst_p0 = cmethod;
9936                                         ins->inst_p1 = arg_array [0];
9937                                         MONO_ADD_INS (cfg->cbb, ins);
9938                                         link_bblock (cfg, cfg->cbb, end_bblock);
9939                                         start_new_bblock = 1;
9940
9941                                         // FIXME: Eliminate unreachable epilogs
9942
9943                                         /*
9944                                          * OP_TAILCALL has no return value, so skip the CEE_RET if it is
9945                                          * only reachable from this call.
9946                                          */
9947                                         GET_BBLOCK (cfg, tblock, ip + 5);
9948                                         if (tblock == cfg->cbb || tblock->in_count == 0)
9949                                                 skip_ret = TRUE;
9950                                         push_res = FALSE;
9951
9952                                         goto call_end;
9953                                 }
9954                         }
9955
9956                         /* 
9957                          * Synchronized wrappers.
9958                          * Its hard to determine where to replace a method with its synchronized
9959                          * wrapper without causing an infinite recursion. The current solution is
9960                          * to add the synchronized wrapper in the trampolines, and to
9961                          * change the called method to a dummy wrapper, and resolve that wrapper
9962                          * to the real method in mono_jit_compile_method ().
9963                          */
9964                         if (cfg->method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
9965                                 MonoMethod *orig = mono_marshal_method_from_wrapper (cfg->method);
9966                                 if (cmethod == orig || (cmethod->is_inflated && mono_method_get_declaring_generic_method (cmethod) == orig))
9967                                         cmethod = mono_marshal_get_synchronized_inner_wrapper (cmethod);
9968                         }
9969
9970                         /*
9971                          * Virtual calls in llvm-only mode.
9972                          */
9973                         if (cfg->llvm_only && virtual_ && cmethod && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
9974                                 ins = emit_llvmonly_virtual_call (cfg, cmethod, fsig, context_used, sp);
9975                                 goto call_end;
9976                         }
9977
9978                         /* Common call */
9979                         INLINE_FAILURE ("call");
9980                         ins = mono_emit_method_call_full (cfg, cmethod, fsig, tail_call, sp, virtual_ ? sp [0] : NULL,
9981                                                                                           imt_arg, vtable_arg);
9982
9983                         if (tail_call && !cfg->llvm_only) {
9984                                 link_bblock (cfg, cfg->cbb, end_bblock);
9985                                 start_new_bblock = 1;
9986
9987                                 // FIXME: Eliminate unreachable epilogs
9988
9989                                 /*
9990                                  * OP_TAILCALL has no return value, so skip the CEE_RET if it is
9991                                  * only reachable from this call.
9992                                  */
9993                                 GET_BBLOCK (cfg, tblock, ip + 5);
9994                                 if (tblock == cfg->cbb || tblock->in_count == 0)
9995                                         skip_ret = TRUE;
9996                                 push_res = FALSE;
9997                         }
9998
9999                         call_end:
10000
10001                         /* End of call, INS should contain the result of the call, if any */
10002
10003                         if (push_res && !MONO_TYPE_IS_VOID (fsig->ret)) {
10004                                 g_assert (ins);
10005                                 if (emit_widen)
10006                                         *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
10007                                 else
10008                                         *sp++ = ins;
10009                         }
10010
10011                         if (keep_this_alive) {
10012                                 MonoInst *dummy_use;
10013
10014                                 /* See mono_emit_method_call_full () */
10015                                 EMIT_NEW_DUMMY_USE (cfg, dummy_use, keep_this_alive);
10016                         }
10017
10018                         CHECK_CFG_EXCEPTION;
10019
10020                         ip += 5;
10021                         if (skip_ret) {
10022                                 g_assert (*ip == CEE_RET);
10023                                 ip += 1;
10024                         }
10025                         ins_flag = 0;
10026                         constrained_class = NULL;
10027                         if (need_seq_point)
10028                                 emit_seq_point (cfg, method, ip, FALSE, TRUE);
10029                         break;
10030                 }
10031                 case CEE_RET:
10032                         if (cfg->method != method) {
10033                                 /* return from inlined method */
10034                                 /* 
10035                                  * If in_count == 0, that means the ret is unreachable due to
10036                                  * being preceeded by a throw. In that case, inline_method () will
10037                                  * handle setting the return value 
10038                                  * (test case: test_0_inline_throw ()).
10039                                  */
10040                                 if (return_var && cfg->cbb->in_count) {
10041                                         MonoType *ret_type = mono_method_signature (method)->ret;
10042
10043                                         MonoInst *store;
10044                                         CHECK_STACK (1);
10045                                         --sp;
10046
10047                                         if ((method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_NONE) && target_type_is_incompatible (cfg, ret_type, *sp))
10048                                                 UNVERIFIED;
10049
10050                                         //g_assert (returnvar != -1);
10051                                         EMIT_NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
10052                                         cfg->ret_var_set = TRUE;
10053                                 } 
10054                         } else {
10055                                 emit_instrumentation_call (cfg, mono_profiler_method_leave);
10056
10057                                 if (cfg->lmf_var && cfg->cbb->in_count && !cfg->llvm_only)
10058                                         emit_pop_lmf (cfg);
10059
10060                                 if (cfg->ret) {
10061                                         MonoType *ret_type = mini_get_underlying_type (mono_method_signature (method)->ret);
10062
10063                                         if (seq_points && !sym_seq_points) {
10064                                                 /* 
10065                                                  * Place a seq point here too even through the IL stack is not
10066                                                  * empty, so a step over on
10067                                                  * call <FOO>
10068                                                  * ret
10069                                                  * will work correctly.
10070                                                  */
10071                                                 NEW_SEQ_POINT (cfg, ins, ip - header->code, TRUE);
10072                                                 MONO_ADD_INS (cfg->cbb, ins);
10073                                         }
10074
10075                                         g_assert (!return_var);
10076                                         CHECK_STACK (1);
10077                                         --sp;
10078
10079                                         if ((method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_NONE) && target_type_is_incompatible (cfg, ret_type, *sp))
10080                                                 UNVERIFIED;
10081
10082                                         emit_setret (cfg, *sp);
10083                                 }
10084                         }
10085                         if (sp != stack_start)
10086                                 UNVERIFIED;
10087                         MONO_INST_NEW (cfg, ins, OP_BR);
10088                         ip++;
10089                         ins->inst_target_bb = end_bblock;
10090                         MONO_ADD_INS (cfg->cbb, ins);
10091                         link_bblock (cfg, cfg->cbb, end_bblock);
10092                         start_new_bblock = 1;
10093                         break;
10094                 case CEE_BR_S:
10095                         CHECK_OPSIZE (2);
10096                         MONO_INST_NEW (cfg, ins, OP_BR);
10097                         ip++;
10098                         target = ip + 1 + (signed char)(*ip);
10099                         ++ip;
10100                         GET_BBLOCK (cfg, tblock, target);
10101                         link_bblock (cfg, cfg->cbb, tblock);
10102                         ins->inst_target_bb = tblock;
10103                         if (sp != stack_start) {
10104                                 handle_stack_args (cfg, stack_start, sp - stack_start);
10105                                 sp = stack_start;
10106                                 CHECK_UNVERIFIABLE (cfg);
10107                         }
10108                         MONO_ADD_INS (cfg->cbb, ins);
10109                         start_new_bblock = 1;
10110                         inline_costs += BRANCH_COST;
10111                         break;
10112                 case CEE_BEQ_S:
10113                 case CEE_BGE_S:
10114                 case CEE_BGT_S:
10115                 case CEE_BLE_S:
10116                 case CEE_BLT_S:
10117                 case CEE_BNE_UN_S:
10118                 case CEE_BGE_UN_S:
10119                 case CEE_BGT_UN_S:
10120                 case CEE_BLE_UN_S:
10121                 case CEE_BLT_UN_S:
10122                         CHECK_OPSIZE (2);
10123                         CHECK_STACK (2);
10124                         MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
10125                         ip++;
10126                         target = ip + 1 + *(signed char*)ip;
10127                         ip++;
10128
10129                         ADD_BINCOND (NULL);
10130
10131                         sp = stack_start;
10132                         inline_costs += BRANCH_COST;
10133                         break;
10134                 case CEE_BR:
10135                         CHECK_OPSIZE (5);
10136                         MONO_INST_NEW (cfg, ins, OP_BR);
10137                         ip++;
10138
10139                         target = ip + 4 + (gint32)read32(ip);
10140                         ip += 4;
10141                         GET_BBLOCK (cfg, tblock, target);
10142                         link_bblock (cfg, cfg->cbb, tblock);
10143                         ins->inst_target_bb = tblock;
10144                         if (sp != stack_start) {
10145                                 handle_stack_args (cfg, stack_start, sp - stack_start);
10146                                 sp = stack_start;
10147                                 CHECK_UNVERIFIABLE (cfg);
10148                         }
10149
10150                         MONO_ADD_INS (cfg->cbb, ins);
10151
10152                         start_new_bblock = 1;
10153                         inline_costs += BRANCH_COST;
10154                         break;
10155                 case CEE_BRFALSE_S:
10156                 case CEE_BRTRUE_S:
10157                 case CEE_BRFALSE:
10158                 case CEE_BRTRUE: {
10159                         MonoInst *cmp;
10160                         gboolean is_short = ((*ip) == CEE_BRFALSE_S) || ((*ip) == CEE_BRTRUE_S);
10161                         gboolean is_true = ((*ip) == CEE_BRTRUE_S) || ((*ip) == CEE_BRTRUE);
10162                         guint32 opsize = is_short ? 1 : 4;
10163
10164                         CHECK_OPSIZE (opsize);
10165                         CHECK_STACK (1);
10166                         if (sp [-1]->type == STACK_VTYPE || sp [-1]->type == STACK_R8)
10167                                 UNVERIFIED;
10168                         ip ++;
10169                         target = ip + opsize + (is_short ? *(signed char*)ip : (gint32)read32(ip));
10170                         ip += opsize;
10171
10172                         sp--;
10173
10174                         GET_BBLOCK (cfg, tblock, target);
10175                         link_bblock (cfg, cfg->cbb, tblock);
10176                         GET_BBLOCK (cfg, tblock, ip);
10177                         link_bblock (cfg, cfg->cbb, tblock);
10178
10179                         if (sp != stack_start) {
10180                                 handle_stack_args (cfg, stack_start, sp - stack_start);
10181                                 CHECK_UNVERIFIABLE (cfg);
10182                         }
10183
10184                         MONO_INST_NEW(cfg, cmp, OP_ICOMPARE_IMM);
10185                         cmp->sreg1 = sp [0]->dreg;
10186                         type_from_op (cfg, cmp, sp [0], NULL);
10187                         CHECK_TYPE (cmp);
10188
10189 #if SIZEOF_REGISTER == 4
10190                         if (cmp->opcode == OP_LCOMPARE_IMM) {
10191                                 /* Convert it to OP_LCOMPARE */
10192                                 MONO_INST_NEW (cfg, ins, OP_I8CONST);
10193                                 ins->type = STACK_I8;
10194                                 ins->dreg = alloc_dreg (cfg, STACK_I8);
10195                                 ins->inst_l = 0;
10196                                 MONO_ADD_INS (cfg->cbb, ins);
10197                                 cmp->opcode = OP_LCOMPARE;
10198                                 cmp->sreg2 = ins->dreg;
10199                         }
10200 #endif
10201                         MONO_ADD_INS (cfg->cbb, cmp);
10202
10203                         MONO_INST_NEW (cfg, ins, is_true ? CEE_BNE_UN : CEE_BEQ);
10204                         type_from_op (cfg, ins, sp [0], NULL);
10205                         MONO_ADD_INS (cfg->cbb, ins);
10206                         ins->inst_many_bb = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);
10207                         GET_BBLOCK (cfg, tblock, target);
10208                         ins->inst_true_bb = tblock;
10209                         GET_BBLOCK (cfg, tblock, ip);
10210                         ins->inst_false_bb = tblock;
10211                         start_new_bblock = 2;
10212
10213                         sp = stack_start;
10214                         inline_costs += BRANCH_COST;
10215                         break;
10216                 }
10217                 case CEE_BEQ:
10218                 case CEE_BGE:
10219                 case CEE_BGT:
10220                 case CEE_BLE:
10221                 case CEE_BLT:
10222                 case CEE_BNE_UN:
10223                 case CEE_BGE_UN:
10224                 case CEE_BGT_UN:
10225                 case CEE_BLE_UN:
10226                 case CEE_BLT_UN:
10227                         CHECK_OPSIZE (5);
10228                         CHECK_STACK (2);
10229                         MONO_INST_NEW (cfg, ins, *ip);
10230                         ip++;
10231                         target = ip + 4 + (gint32)read32(ip);
10232                         ip += 4;
10233
10234                         ADD_BINCOND (NULL);
10235
10236                         sp = stack_start;
10237                         inline_costs += BRANCH_COST;
10238                         break;
10239                 case CEE_SWITCH: {
10240                         MonoInst *src1;
10241                         MonoBasicBlock **targets;
10242                         MonoBasicBlock *default_bblock;
10243                         MonoJumpInfoBBTable *table;
10244                         int offset_reg = alloc_preg (cfg);
10245                         int target_reg = alloc_preg (cfg);
10246                         int table_reg = alloc_preg (cfg);
10247                         int sum_reg = alloc_preg (cfg);
10248                         gboolean use_op_switch;
10249
10250                         CHECK_OPSIZE (5);
10251                         CHECK_STACK (1);
10252                         n = read32 (ip + 1);
10253                         --sp;
10254                         src1 = sp [0];
10255                         if ((src1->type != STACK_I4) && (src1->type != STACK_PTR)) 
10256                                 UNVERIFIED;
10257
10258                         ip += 5;
10259                         CHECK_OPSIZE (n * sizeof (guint32));
10260                         target = ip + n * sizeof (guint32);
10261
10262                         GET_BBLOCK (cfg, default_bblock, target);
10263                         default_bblock->flags |= BB_INDIRECT_JUMP_TARGET;
10264
10265                         targets = (MonoBasicBlock **)mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * n);
10266                         for (i = 0; i < n; ++i) {
10267                                 GET_BBLOCK (cfg, tblock, target + (gint32)read32(ip));
10268                                 targets [i] = tblock;
10269                                 targets [i]->flags |= BB_INDIRECT_JUMP_TARGET;
10270                                 ip += 4;
10271                         }
10272
10273                         if (sp != stack_start) {
10274                                 /* 
10275                                  * Link the current bb with the targets as well, so handle_stack_args
10276                                  * will set their in_stack correctly.
10277                                  */
10278                                 link_bblock (cfg, cfg->cbb, default_bblock);
10279                                 for (i = 0; i < n; ++i)
10280                                         link_bblock (cfg, cfg->cbb, targets [i]);
10281
10282                                 handle_stack_args (cfg, stack_start, sp - stack_start);
10283                                 sp = stack_start;
10284                                 CHECK_UNVERIFIABLE (cfg);
10285
10286                                 /* Undo the links */
10287                                 mono_unlink_bblock (cfg, cfg->cbb, default_bblock);
10288                                 for (i = 0; i < n; ++i)
10289                                         mono_unlink_bblock (cfg, cfg->cbb, targets [i]);
10290                         }
10291
10292                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ICOMPARE_IMM, -1, src1->dreg, n);
10293                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBGE_UN, default_bblock);
10294
10295                         for (i = 0; i < n; ++i)
10296                                 link_bblock (cfg, cfg->cbb, targets [i]);
10297
10298                         table = (MonoJumpInfoBBTable *)mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfoBBTable));
10299                         table->table = targets;
10300                         table->table_size = n;
10301
10302                         use_op_switch = FALSE;
10303 #ifdef TARGET_ARM
10304                         /* ARM implements SWITCH statements differently */
10305                         /* FIXME: Make it use the generic implementation */
10306                         if (!cfg->compile_aot)
10307                                 use_op_switch = TRUE;
10308 #endif
10309
10310                         if (COMPILE_LLVM (cfg))
10311                                 use_op_switch = TRUE;
10312
10313                         cfg->cbb->has_jump_table = 1;
10314
10315                         if (use_op_switch) {
10316                                 MONO_INST_NEW (cfg, ins, OP_SWITCH);
10317                                 ins->sreg1 = src1->dreg;
10318                                 ins->inst_p0 = table;
10319                                 ins->inst_many_bb = targets;
10320                                 ins->klass = (MonoClass *)GUINT_TO_POINTER (n);
10321                                 MONO_ADD_INS (cfg->cbb, ins);
10322                         } else {
10323                                 if (sizeof (gpointer) == 8)
10324                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, offset_reg, src1->dreg, 3);
10325                                 else
10326                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, offset_reg, src1->dreg, 2);
10327
10328 #if SIZEOF_REGISTER == 8
10329                                 /* The upper word might not be zero, and we add it to a 64 bit address later */
10330                                 MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, offset_reg, offset_reg);
10331 #endif
10332
10333                                 if (cfg->compile_aot) {
10334                                         MONO_EMIT_NEW_AOTCONST (cfg, table_reg, table, MONO_PATCH_INFO_SWITCH);
10335                                 } else {
10336                                         MONO_INST_NEW (cfg, ins, OP_JUMP_TABLE);
10337                                         ins->inst_c1 = MONO_PATCH_INFO_SWITCH;
10338                                         ins->inst_p0 = table;
10339                                         ins->dreg = table_reg;
10340                                         MONO_ADD_INS (cfg->cbb, ins);
10341                                 }
10342
10343                                 /* FIXME: Use load_memindex */
10344                                 MONO_EMIT_NEW_BIALU (cfg, OP_PADD, sum_reg, table_reg, offset_reg);
10345                                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, target_reg, sum_reg, 0);
10346                                 MONO_EMIT_NEW_UNALU (cfg, OP_BR_REG, -1, target_reg);
10347                         }
10348                         start_new_bblock = 1;
10349                         inline_costs += (BRANCH_COST * 2);
10350                         break;
10351                 }
10352                 case CEE_LDIND_I1:
10353                 case CEE_LDIND_U1:
10354                 case CEE_LDIND_I2:
10355                 case CEE_LDIND_U2:
10356                 case CEE_LDIND_I4:
10357                 case CEE_LDIND_U4:
10358                 case CEE_LDIND_I8:
10359                 case CEE_LDIND_I:
10360                 case CEE_LDIND_R4:
10361                 case CEE_LDIND_R8:
10362                 case CEE_LDIND_REF:
10363                         CHECK_STACK (1);
10364                         --sp;
10365
10366                         switch (*ip) {
10367                         case CEE_LDIND_R4:
10368                         case CEE_LDIND_R8:
10369                                 dreg = alloc_freg (cfg);
10370                                 break;
10371                         case CEE_LDIND_I8:
10372                                 dreg = alloc_lreg (cfg);
10373                                 break;
10374                         case CEE_LDIND_REF:
10375                                 dreg = alloc_ireg_ref (cfg);
10376                                 break;
10377                         default:
10378                                 dreg = alloc_preg (cfg);
10379                         }
10380
10381                         NEW_LOAD_MEMBASE (cfg, ins, ldind_to_load_membase (*ip), dreg, sp [0]->dreg, 0);
10382                         ins->type = ldind_type [*ip - CEE_LDIND_I1];
10383                         if (*ip == CEE_LDIND_R4)
10384                                 ins->type = cfg->r4_stack_type;
10385                         ins->flags |= ins_flag;
10386                         MONO_ADD_INS (cfg->cbb, ins);
10387                         *sp++ = ins;
10388                         if (ins_flag & MONO_INST_VOLATILE) {
10389                                 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
10390                                 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
10391                         }
10392                         ins_flag = 0;
10393                         ++ip;
10394                         break;
10395                 case CEE_STIND_REF:
10396                 case CEE_STIND_I1:
10397                 case CEE_STIND_I2:
10398                 case CEE_STIND_I4:
10399                 case CEE_STIND_I8:
10400                 case CEE_STIND_R4:
10401                 case CEE_STIND_R8:
10402                 case CEE_STIND_I:
10403                         CHECK_STACK (2);
10404                         sp -= 2;
10405
10406                         if (ins_flag & MONO_INST_VOLATILE) {
10407                                 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
10408                                 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
10409                         }
10410
10411                         NEW_STORE_MEMBASE (cfg, ins, stind_to_store_membase (*ip), sp [0]->dreg, 0, sp [1]->dreg);
10412                         ins->flags |= ins_flag;
10413                         ins_flag = 0;
10414
10415                         MONO_ADD_INS (cfg->cbb, ins);
10416
10417                         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)))
10418                                 emit_write_barrier (cfg, sp [0], sp [1]);
10419
10420                         inline_costs += 1;
10421                         ++ip;
10422                         break;
10423
10424                 case CEE_MUL:
10425                         CHECK_STACK (2);
10426
10427                         MONO_INST_NEW (cfg, ins, (*ip));
10428                         sp -= 2;
10429                         ins->sreg1 = sp [0]->dreg;
10430                         ins->sreg2 = sp [1]->dreg;
10431                         type_from_op (cfg, ins, sp [0], sp [1]);
10432                         CHECK_TYPE (ins);
10433                         ins->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type);
10434
10435                         /* Use the immediate opcodes if possible */
10436                         if ((sp [1]->opcode == OP_ICONST) && mono_arch_is_inst_imm (sp [1]->inst_c0)) {
10437                                 int imm_opcode = mono_op_to_op_imm_noemul (ins->opcode);
10438                                 if (imm_opcode != -1) {
10439                                         ins->opcode = imm_opcode;
10440                                         ins->inst_p1 = (gpointer)(gssize)(sp [1]->inst_c0);
10441                                         ins->sreg2 = -1;
10442
10443                                         NULLIFY_INS (sp [1]);
10444                                 }
10445                         }
10446
10447                         MONO_ADD_INS ((cfg)->cbb, (ins));
10448
10449                         *sp++ = mono_decompose_opcode (cfg, ins);
10450                         ip++;
10451                         break;
10452                 case CEE_ADD:
10453                 case CEE_SUB:
10454                 case CEE_DIV:
10455                 case CEE_DIV_UN:
10456                 case CEE_REM:
10457                 case CEE_REM_UN:
10458                 case CEE_AND:
10459                 case CEE_OR:
10460                 case CEE_XOR:
10461                 case CEE_SHL:
10462                 case CEE_SHR:
10463                 case CEE_SHR_UN:
10464                         CHECK_STACK (2);
10465
10466                         MONO_INST_NEW (cfg, ins, (*ip));
10467                         sp -= 2;
10468                         ins->sreg1 = sp [0]->dreg;
10469                         ins->sreg2 = sp [1]->dreg;
10470                         type_from_op (cfg, ins, sp [0], sp [1]);
10471                         CHECK_TYPE (ins);
10472                         add_widen_op (cfg, ins, &sp [0], &sp [1]);
10473                         ins->dreg = alloc_dreg ((cfg), (MonoStackType)(ins)->type);
10474
10475                         /* FIXME: Pass opcode to is_inst_imm */
10476
10477                         /* Use the immediate opcodes if possible */
10478                         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)) {
10479                                 int imm_opcode = mono_op_to_op_imm_noemul (ins->opcode);
10480                                 if (imm_opcode != -1) {
10481                                         ins->opcode = imm_opcode;
10482                                         if (sp [1]->opcode == OP_I8CONST) {
10483 #if SIZEOF_REGISTER == 8
10484                                                 ins->inst_imm = sp [1]->inst_l;
10485 #else
10486                                                 ins->inst_ls_word = sp [1]->inst_ls_word;
10487                                                 ins->inst_ms_word = sp [1]->inst_ms_word;
10488 #endif
10489                                         }
10490                                         else
10491                                                 ins->inst_imm = (gssize)(sp [1]->inst_c0);
10492                                         ins->sreg2 = -1;
10493
10494                                         /* Might be followed by an instruction added by add_widen_op */
10495                                         if (sp [1]->next == NULL)
10496                                                 NULLIFY_INS (sp [1]);
10497                                 }
10498                         }
10499                         MONO_ADD_INS ((cfg)->cbb, (ins));
10500
10501                         *sp++ = mono_decompose_opcode (cfg, ins);
10502                         ip++;
10503                         break;
10504                 case CEE_NEG:
10505                 case CEE_NOT:
10506                 case CEE_CONV_I1:
10507                 case CEE_CONV_I2:
10508                 case CEE_CONV_I4:
10509                 case CEE_CONV_R4:
10510                 case CEE_CONV_R8:
10511                 case CEE_CONV_U4:
10512                 case CEE_CONV_I8:
10513                 case CEE_CONV_U8:
10514                 case CEE_CONV_OVF_I8:
10515                 case CEE_CONV_OVF_U8:
10516                 case CEE_CONV_R_UN:
10517                         CHECK_STACK (1);
10518
10519                         /* Special case this earlier so we have long constants in the IR */
10520                         if ((((*ip) == CEE_CONV_I8) || ((*ip) == CEE_CONV_U8)) && (sp [-1]->opcode == OP_ICONST)) {
10521                                 int data = sp [-1]->inst_c0;
10522                                 sp [-1]->opcode = OP_I8CONST;
10523                                 sp [-1]->type = STACK_I8;
10524 #if SIZEOF_REGISTER == 8
10525                                 if ((*ip) == CEE_CONV_U8)
10526                                         sp [-1]->inst_c0 = (guint32)data;
10527                                 else
10528                                         sp [-1]->inst_c0 = data;
10529 #else
10530                                 sp [-1]->inst_ls_word = data;
10531                                 if ((*ip) == CEE_CONV_U8)
10532                                         sp [-1]->inst_ms_word = 0;
10533                                 else
10534                                         sp [-1]->inst_ms_word = (data < 0) ? -1 : 0;
10535 #endif
10536                                 sp [-1]->dreg = alloc_dreg (cfg, STACK_I8);
10537                         }
10538                         else {
10539                                 ADD_UNOP (*ip);
10540                         }
10541                         ip++;
10542                         break;
10543                 case CEE_CONV_OVF_I4:
10544                 case CEE_CONV_OVF_I1:
10545                 case CEE_CONV_OVF_I2:
10546                 case CEE_CONV_OVF_I:
10547                 case CEE_CONV_OVF_U:
10548                         CHECK_STACK (1);
10549
10550                         if (sp [-1]->type == STACK_R8 || sp [-1]->type == STACK_R4) {
10551                                 ADD_UNOP (CEE_CONV_OVF_I8);
10552                                 ADD_UNOP (*ip);
10553                         } else {
10554                                 ADD_UNOP (*ip);
10555                         }
10556                         ip++;
10557                         break;
10558                 case CEE_CONV_OVF_U1:
10559                 case CEE_CONV_OVF_U2:
10560                 case CEE_CONV_OVF_U4:
10561                         CHECK_STACK (1);
10562
10563                         if (sp [-1]->type == STACK_R8 || sp [-1]->type == STACK_R4) {
10564                                 ADD_UNOP (CEE_CONV_OVF_U8);
10565                                 ADD_UNOP (*ip);
10566                         } else {
10567                                 ADD_UNOP (*ip);
10568                         }
10569                         ip++;
10570                         break;
10571                 case CEE_CONV_OVF_I1_UN:
10572                 case CEE_CONV_OVF_I2_UN:
10573                 case CEE_CONV_OVF_I4_UN:
10574                 case CEE_CONV_OVF_I8_UN:
10575                 case CEE_CONV_OVF_U1_UN:
10576                 case CEE_CONV_OVF_U2_UN:
10577                 case CEE_CONV_OVF_U4_UN:
10578                 case CEE_CONV_OVF_U8_UN:
10579                 case CEE_CONV_OVF_I_UN:
10580                 case CEE_CONV_OVF_U_UN:
10581                 case CEE_CONV_U2:
10582                 case CEE_CONV_U1:
10583                 case CEE_CONV_I:
10584                 case CEE_CONV_U:
10585                         CHECK_STACK (1);
10586                         ADD_UNOP (*ip);
10587                         CHECK_CFG_EXCEPTION;
10588                         ip++;
10589                         break;
10590                 case CEE_ADD_OVF:
10591                 case CEE_ADD_OVF_UN:
10592                 case CEE_MUL_OVF:
10593                 case CEE_MUL_OVF_UN:
10594                 case CEE_SUB_OVF:
10595                 case CEE_SUB_OVF_UN:
10596                         CHECK_STACK (2);
10597                         ADD_BINOP (*ip);
10598                         ip++;
10599                         break;
10600                 case CEE_CPOBJ:
10601                         GSHAREDVT_FAILURE (*ip);
10602                         CHECK_OPSIZE (5);
10603                         CHECK_STACK (2);
10604                         token = read32 (ip + 1);
10605                         klass = mini_get_class (method, token, generic_context);
10606                         CHECK_TYPELOAD (klass);
10607                         sp -= 2;
10608                         if (generic_class_is_reference_type (cfg, klass)) {
10609                                 MonoInst *store, *load;
10610                                 int dreg = alloc_ireg_ref (cfg);
10611
10612                                 NEW_LOAD_MEMBASE (cfg, load, OP_LOAD_MEMBASE, dreg, sp [1]->dreg, 0);
10613                                 load->flags |= ins_flag;
10614                                 MONO_ADD_INS (cfg->cbb, load);
10615
10616                                 NEW_STORE_MEMBASE (cfg, store, OP_STORE_MEMBASE_REG, sp [0]->dreg, 0, dreg);
10617                                 store->flags |= ins_flag;
10618                                 MONO_ADD_INS (cfg->cbb, store);
10619
10620                                 if (cfg->gen_write_barriers && cfg->method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER)
10621                                         emit_write_barrier (cfg, sp [0], sp [1]);
10622                         } else {
10623                                 mini_emit_stobj (cfg, sp [0], sp [1], klass, FALSE);
10624                         }
10625                         ins_flag = 0;
10626                         ip += 5;
10627                         break;
10628                 case CEE_LDOBJ: {
10629                         int loc_index = -1;
10630                         int stloc_len = 0;
10631
10632                         CHECK_OPSIZE (5);
10633                         CHECK_STACK (1);
10634                         --sp;
10635                         token = read32 (ip + 1);
10636                         klass = mini_get_class (method, token, generic_context);
10637                         CHECK_TYPELOAD (klass);
10638
10639                         /* Optimize the common ldobj+stloc combination */
10640                         switch (ip [5]) {
10641                         case CEE_STLOC_S:
10642                                 loc_index = ip [6];
10643                                 stloc_len = 2;
10644                                 break;
10645                         case CEE_STLOC_0:
10646                         case CEE_STLOC_1:
10647                         case CEE_STLOC_2:
10648                         case CEE_STLOC_3:
10649                                 loc_index = ip [5] - CEE_STLOC_0;
10650                                 stloc_len = 1;
10651                                 break;
10652                         default:
10653                                 break;
10654                         }
10655
10656                         if ((loc_index != -1) && ip_in_bb (cfg, cfg->cbb, ip + 5)) {
10657                                 CHECK_LOCAL (loc_index);
10658
10659                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0);
10660                                 ins->dreg = cfg->locals [loc_index]->dreg;
10661                                 ins->flags |= ins_flag;
10662                                 ip += 5;
10663                                 ip += stloc_len;
10664                                 if (ins_flag & MONO_INST_VOLATILE) {
10665                                         /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
10666                                         emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
10667                                 }
10668                                 ins_flag = 0;
10669                                 break;
10670                         }
10671
10672                         /* Optimize the ldobj+stobj combination */
10673                         /* The reference case ends up being a load+store anyway */
10674                         /* Skip this if the operation is volatile. */
10675                         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)) {
10676                                 CHECK_STACK (1);
10677
10678                                 sp --;
10679
10680                                 mini_emit_stobj (cfg, sp [0], sp [1], klass, FALSE);
10681
10682                                 ip += 5 + 5;
10683                                 ins_flag = 0;
10684                                 break;
10685                         }
10686
10687                         EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0);
10688                         ins->flags |= ins_flag;
10689                         *sp++ = ins;
10690
10691                         if (ins_flag & MONO_INST_VOLATILE) {
10692                                 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
10693                                 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
10694                         }
10695
10696                         ip += 5;
10697                         ins_flag = 0;
10698                         inline_costs += 1;
10699                         break;
10700                 }
10701                 case CEE_LDSTR:
10702                         CHECK_STACK_OVF (1);
10703                         CHECK_OPSIZE (5);
10704                         n = read32 (ip + 1);
10705
10706                         if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
10707                                 EMIT_NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, n));
10708                                 ins->type = STACK_OBJ;
10709                                 *sp = ins;
10710                         }
10711                         else if (method->wrapper_type != MONO_WRAPPER_NONE) {
10712                                 MonoInst *iargs [1];
10713                                 char *str = (char *)mono_method_get_wrapper_data (method, n);
10714
10715                                 if (cfg->compile_aot)
10716                                         EMIT_NEW_LDSTRLITCONST (cfg, iargs [0], str);
10717                                 else
10718                                         EMIT_NEW_PCONST (cfg, iargs [0], str);
10719                                 *sp = mono_emit_jit_icall (cfg, mono_string_new_wrapper, iargs);
10720                         } else {
10721                                 if (cfg->opt & MONO_OPT_SHARED) {
10722                                         MonoInst *iargs [3];
10723
10724                                         if (cfg->compile_aot) {
10725                                                 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, GINT_TO_POINTER (n));
10726                                         }
10727                                         EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
10728                                         EMIT_NEW_IMAGECONST (cfg, iargs [1], image);
10729                                         EMIT_NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
10730                                         *sp = mono_emit_jit_icall (cfg, mono_ldstr, iargs);
10731                                         mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
10732                                 } else {
10733                                         if (cfg->cbb->out_of_line) {
10734                                                 MonoInst *iargs [2];
10735
10736                                                 if (image == mono_defaults.corlib) {
10737                                                         /* 
10738                                                          * Avoid relocations in AOT and save some space by using a 
10739                                                          * version of helper_ldstr specialized to mscorlib.
10740                                                          */
10741                                                         EMIT_NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (n));
10742                                                         *sp = mono_emit_jit_icall (cfg, mono_helper_ldstr_mscorlib, iargs);
10743                                                 } else {
10744                                                         /* Avoid creating the string object */
10745                                                         EMIT_NEW_IMAGECONST (cfg, iargs [0], image);
10746                                                         EMIT_NEW_ICONST (cfg, iargs [1], mono_metadata_token_index (n));
10747                                                         *sp = mono_emit_jit_icall (cfg, mono_helper_ldstr, iargs);
10748                                                 }
10749                                         } 
10750                                         else
10751                                         if (cfg->compile_aot) {
10752                                                 NEW_LDSTRCONST (cfg, ins, image, n);
10753                                                 *sp = ins;
10754                                                 MONO_ADD_INS (cfg->cbb, ins);
10755                                         } 
10756                                         else {
10757                                                 NEW_PCONST (cfg, ins, NULL);
10758                                                 ins->type = STACK_OBJ;
10759                                                 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
10760                                                 if (!ins->inst_p0)
10761                                                         OUT_OF_MEMORY_FAILURE;
10762
10763                                                 *sp = ins;
10764                                                 MONO_ADD_INS (cfg->cbb, ins);
10765                                         }
10766                                 }
10767                         }
10768
10769                         sp++;
10770                         ip += 5;
10771                         break;
10772                 case CEE_NEWOBJ: {
10773                         MonoInst *iargs [2];
10774                         MonoMethodSignature *fsig;
10775                         MonoInst this_ins;
10776                         MonoInst *alloc;
10777                         MonoInst *vtable_arg = NULL;
10778
10779                         CHECK_OPSIZE (5);
10780                         token = read32 (ip + 1);
10781                         cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
10782                         CHECK_CFG_ERROR;
10783
10784                         fsig = mono_method_get_signature_checked (cmethod, image, token, generic_context, &cfg->error);
10785                         CHECK_CFG_ERROR;
10786
10787                         mono_save_token_info (cfg, image, token, cmethod);
10788
10789                         if (!mono_class_init (cmethod->klass))
10790                                 TYPE_LOAD_ERROR (cmethod->klass);
10791
10792                         context_used = mini_method_check_context_used (cfg, cmethod);
10793
10794                         if (mono_security_core_clr_enabled ())
10795                                 ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
10796
10797                         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)) {
10798                                 emit_class_init (cfg, cmethod->klass);
10799                                 CHECK_TYPELOAD (cmethod->klass);
10800                         }
10801
10802                         /*
10803                         if (cfg->gsharedvt) {
10804                                 if (mini_is_gsharedvt_variable_signature (sig))
10805                                         GSHAREDVT_FAILURE (*ip);
10806                         }
10807                         */
10808
10809                         n = fsig->param_count;
10810                         CHECK_STACK (n);
10811
10812                         /* 
10813                          * Generate smaller code for the common newobj <exception> instruction in
10814                          * argument checking code.
10815                          */
10816                         if (cfg->cbb->out_of_line && cmethod->klass->image == mono_defaults.corlib &&
10817                                 is_exception_class (cmethod->klass) && n <= 2 &&
10818                                 ((n < 1) || (!fsig->params [0]->byref && fsig->params [0]->type == MONO_TYPE_STRING)) && 
10819                                 ((n < 2) || (!fsig->params [1]->byref && fsig->params [1]->type == MONO_TYPE_STRING))) {
10820                                 MonoInst *iargs [3];
10821
10822                                 sp -= n;
10823
10824                                 EMIT_NEW_ICONST (cfg, iargs [0], cmethod->klass->type_token);
10825                                 switch (n) {
10826                                 case 0:
10827                                         *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_0, iargs);
10828                                         break;
10829                                 case 1:
10830                                         iargs [1] = sp [0];
10831                                         *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_1, iargs);
10832                                         break;
10833                                 case 2:
10834                                         iargs [1] = sp [0];
10835                                         iargs [2] = sp [1];
10836                                         *sp ++ = mono_emit_jit_icall (cfg, mono_create_corlib_exception_2, iargs);
10837                                         break;
10838                                 default:
10839                                         g_assert_not_reached ();
10840                                 }
10841
10842                                 ip += 5;
10843                                 inline_costs += 5;
10844                                 break;
10845                         }
10846
10847                         /* move the args to allow room for 'this' in the first position */
10848                         while (n--) {
10849                                 --sp;
10850                                 sp [1] = sp [0];
10851                         }
10852
10853                         /* check_call_signature () requires sp[0] to be set */
10854                         this_ins.type = STACK_OBJ;
10855                         sp [0] = &this_ins;
10856                         if (check_call_signature (cfg, fsig, sp))
10857                                 UNVERIFIED;
10858
10859                         iargs [0] = NULL;
10860
10861                         if (mini_class_is_system_array (cmethod->klass)) {
10862                                 *sp = emit_get_rgctx_method (cfg, context_used,
10863                                                                                          cmethod, MONO_RGCTX_INFO_METHOD);
10864
10865                                 /* Avoid varargs in the common case */
10866                                 if (fsig->param_count == 1)
10867                                         alloc = mono_emit_jit_icall (cfg, mono_array_new_1, sp);
10868                                 else if (fsig->param_count == 2)
10869                                         alloc = mono_emit_jit_icall (cfg, mono_array_new_2, sp);
10870                                 else if (fsig->param_count == 3)
10871                                         alloc = mono_emit_jit_icall (cfg, mono_array_new_3, sp);
10872                                 else if (fsig->param_count == 4)
10873                                         alloc = mono_emit_jit_icall (cfg, mono_array_new_4, sp);
10874                                 else
10875                                         alloc = handle_array_new (cfg, fsig->param_count, sp, ip);
10876                         } else if (cmethod->string_ctor) {
10877                                 g_assert (!context_used);
10878                                 g_assert (!vtable_arg);
10879                                 /* we simply pass a null pointer */
10880                                 EMIT_NEW_PCONST (cfg, *sp, NULL); 
10881                                 /* now call the string ctor */
10882                                 alloc = mono_emit_method_call_full (cfg, cmethod, fsig, FALSE, sp, NULL, NULL, NULL);
10883                         } else {
10884                                 if (cmethod->klass->valuetype) {
10885                                         iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
10886                                         emit_init_rvar (cfg, iargs [0]->dreg, &cmethod->klass->byval_arg);
10887                                         EMIT_NEW_TEMPLOADA (cfg, *sp, iargs [0]->inst_c0);
10888
10889                                         alloc = NULL;
10890
10891                                         /* 
10892                                          * The code generated by mini_emit_virtual_call () expects
10893                                          * iargs [0] to be a boxed instance, but luckily the vcall
10894                                          * will be transformed into a normal call there.
10895                                          */
10896                                 } else if (context_used) {
10897                                         alloc = handle_alloc (cfg, cmethod->klass, FALSE, context_used);
10898                                         *sp = alloc;
10899                                 } else {
10900                                         MonoVTable *vtable = NULL;
10901
10902                                         if (!cfg->compile_aot)
10903                                                 vtable = mono_class_vtable (cfg->domain, cmethod->klass);
10904                                         CHECK_TYPELOAD (cmethod->klass);
10905
10906                                         /*
10907                                          * TypeInitializationExceptions thrown from the mono_runtime_class_init
10908                                          * call in mono_jit_runtime_invoke () can abort the finalizer thread.
10909                                          * As a workaround, we call class cctors before allocating objects.
10910                                          */
10911                                         if (mini_field_access_needs_cctor_run (cfg, method, cmethod->klass, vtable) && !(g_slist_find (class_inits, cmethod->klass))) {
10912                                                 emit_class_init (cfg, cmethod->klass);
10913                                                 if (cfg->verbose_level > 2)
10914                                                         printf ("class %s.%s needs init call for ctor\n", cmethod->klass->name_space, cmethod->klass->name);
10915                                                 class_inits = g_slist_prepend (class_inits, cmethod->klass);
10916                                         }
10917
10918                                         alloc = handle_alloc (cfg, cmethod->klass, FALSE, 0);
10919                                         *sp = alloc;
10920                                 }
10921                                 CHECK_CFG_EXCEPTION; /*for handle_alloc*/
10922
10923                                 if (alloc)
10924                                         MONO_EMIT_NEW_UNALU (cfg, OP_NOT_NULL, -1, alloc->dreg);
10925
10926                                 /* Now call the actual ctor */
10927                                 handle_ctor_call (cfg, cmethod, fsig, context_used, sp, ip, &inline_costs);
10928                                 CHECK_CFG_EXCEPTION;
10929                         }
10930
10931                         if (alloc == NULL) {
10932                                 /* Valuetype */
10933                                 EMIT_NEW_TEMPLOAD (cfg, ins, iargs [0]->inst_c0);
10934                                 type_to_eval_stack_type (cfg, &ins->klass->byval_arg, ins);
10935                                 *sp++= ins;
10936                         } else {
10937                                 *sp++ = alloc;
10938                         }
10939                         
10940                         ip += 5;
10941                         inline_costs += 5;
10942                         if (!(seq_point_locs && mono_bitset_test_fast (seq_point_locs, ip - header->code)))
10943                                 emit_seq_point (cfg, method, ip, FALSE, TRUE);
10944                         break;
10945                 }
10946                 case CEE_CASTCLASS:
10947                         CHECK_STACK (1);
10948                         --sp;
10949                         CHECK_OPSIZE (5);
10950                         token = read32 (ip + 1);
10951                         klass = mini_get_class (method, token, generic_context);
10952                         CHECK_TYPELOAD (klass);
10953                         if (sp [0]->type != STACK_OBJ)
10954                                 UNVERIFIED;
10955
10956                         ins = handle_castclass (cfg, klass, *sp, ip, &inline_costs);
10957                         CHECK_CFG_EXCEPTION;
10958
10959                         *sp ++ = ins;
10960                         ip += 5;
10961                         break;
10962                 case CEE_ISINST: {
10963                         CHECK_STACK (1);
10964                         --sp;
10965                         CHECK_OPSIZE (5);
10966                         token = read32 (ip + 1);
10967                         klass = mini_get_class (method, token, generic_context);
10968                         CHECK_TYPELOAD (klass);
10969                         if (sp [0]->type != STACK_OBJ)
10970                                 UNVERIFIED;
10971  
10972                         context_used = mini_class_check_context_used (cfg, klass);
10973
10974                         if (!context_used && mini_class_has_reference_variant_generic_argument (cfg, klass, context_used)) {
10975                                 MonoMethod *mono_isinst = mono_marshal_get_isinst_with_cache ();
10976                                 MonoInst *args [3];
10977                                 int idx;
10978
10979                                 /* obj */
10980                                 args [0] = *sp;
10981
10982                                 /* klass */
10983                                 EMIT_NEW_CLASSCONST (cfg, args [1], klass);
10984
10985                                 /* inline cache*/
10986                                 idx = get_castclass_cache_idx (cfg);
10987                                 args [2] = emit_runtime_constant (cfg, MONO_PATCH_INFO_CASTCLASS_CACHE, GINT_TO_POINTER (idx));
10988
10989                                 *sp++ = mono_emit_method_call (cfg, mono_isinst, args, NULL);
10990                                 ip += 5;
10991                                 inline_costs += 2;
10992                         } else if (!context_used && (mono_class_is_marshalbyref (klass) || klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
10993                                 MonoMethod *mono_isinst;
10994                                 MonoInst *iargs [1];
10995                                 int costs;
10996
10997                                 mono_isinst = mono_marshal_get_isinst (klass); 
10998                                 iargs [0] = sp [0];
10999
11000                                 costs = inline_method (cfg, mono_isinst, mono_method_signature (mono_isinst), 
11001                                                                            iargs, ip, cfg->real_offset, TRUE);
11002                                 CHECK_CFG_EXCEPTION;
11003                                 g_assert (costs > 0);
11004                                 
11005                                 ip += 5;
11006                                 cfg->real_offset += 5;
11007
11008                                 *sp++= iargs [0];
11009
11010                                 inline_costs += costs;
11011                         }
11012                         else {
11013                                 ins = handle_isinst (cfg, klass, *sp, context_used);
11014                                 CHECK_CFG_EXCEPTION;
11015                                 *sp ++ = ins;
11016                                 ip += 5;
11017                         }
11018                         break;
11019                 }
11020                 case CEE_UNBOX_ANY: {
11021                         MonoInst *res, *addr;
11022
11023                         CHECK_STACK (1);
11024                         --sp;
11025                         CHECK_OPSIZE (5);
11026                         token = read32 (ip + 1);
11027                         klass = mini_get_class (method, token, generic_context);
11028                         CHECK_TYPELOAD (klass);
11029
11030                         mono_save_token_info (cfg, image, token, klass);
11031
11032                         context_used = mini_class_check_context_used (cfg, klass);
11033
11034                         if (mini_is_gsharedvt_klass (klass)) {
11035                                 res = handle_unbox_gsharedvt (cfg, klass, *sp);
11036                                 inline_costs += 2;
11037                         } else if (generic_class_is_reference_type (cfg, klass)) {
11038                                 res = handle_castclass (cfg, klass, *sp, ip, &inline_costs);
11039                                 CHECK_CFG_EXCEPTION;
11040                         } else if (mono_class_is_nullable (klass)) {
11041                                 res = handle_unbox_nullable (cfg, *sp, klass, context_used);
11042                         } else {
11043                                 addr = handle_unbox (cfg, klass, sp, context_used);
11044                                 /* LDOBJ */
11045                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
11046                                 res = ins;
11047                                 inline_costs += 2;
11048                         }
11049
11050                         *sp ++ = res;
11051                         ip += 5;
11052                         break;
11053                 }
11054                 case CEE_BOX: {
11055                         MonoInst *val;
11056                         MonoClass *enum_class;
11057                         MonoMethod *has_flag;
11058
11059                         CHECK_STACK (1);
11060                         --sp;
11061                         val = *sp;
11062                         CHECK_OPSIZE (5);
11063                         token = read32 (ip + 1);
11064                         klass = mini_get_class (method, token, generic_context);
11065                         CHECK_TYPELOAD (klass);
11066
11067                         mono_save_token_info (cfg, image, token, klass);
11068
11069                         context_used = mini_class_check_context_used (cfg, klass);
11070
11071                         if (generic_class_is_reference_type (cfg, klass)) {
11072                                 *sp++ = val;
11073                                 ip += 5;
11074                                 break;
11075                         }
11076
11077                         if (klass == mono_defaults.void_class)
11078                                 UNVERIFIED;
11079                         if (target_type_is_incompatible (cfg, &klass->byval_arg, *sp))
11080                                 UNVERIFIED;
11081                         /* frequent check in generic code: box (struct), brtrue */
11082
11083                         /*
11084                          * Look for:
11085                          *
11086                          *   <push int/long ptr>
11087                          *   <push int/long>
11088                          *   box MyFlags
11089                          *   constrained. MyFlags
11090                          *   callvirt instace bool class [mscorlib] System.Enum::HasFlag (class [mscorlib] System.Enum)
11091                          *
11092                          * If we find this sequence and the operand types on box and constrained
11093                          * are equal, we can emit a specialized instruction sequence instead of
11094                          * the very slow HasFlag () call.
11095                          */
11096                         if ((cfg->opt & MONO_OPT_INTRINS) &&
11097                             /* Cheap checks first. */
11098                             ip + 5 + 6 + 5 < end &&
11099                             ip [5] == CEE_PREFIX1 &&
11100                             ip [6] == CEE_CONSTRAINED_ &&
11101                             ip [11] == CEE_CALLVIRT &&
11102                             ip_in_bb (cfg, cfg->cbb, ip + 5 + 6 + 5) &&
11103                             mono_class_is_enum (klass) &&
11104                             (enum_class = mini_get_class (method, read32 (ip + 7), generic_context)) &&
11105                             (has_flag = mini_get_method (cfg, method, read32 (ip + 12), NULL, generic_context)) &&
11106                             has_flag->klass == mono_defaults.enum_class &&
11107                             !strcmp (has_flag->name, "HasFlag") &&
11108                             has_flag->signature->hasthis &&
11109                             has_flag->signature->param_count == 1) {
11110                                 CHECK_TYPELOAD (enum_class);
11111
11112                                 if (enum_class == klass) {
11113                                         MonoInst *enum_this, *enum_flag;
11114
11115                                         ip += 5 + 6 + 5;
11116                                         --sp;
11117
11118                                         enum_this = sp [0];
11119                                         enum_flag = sp [1];
11120
11121                                         *sp++ = handle_enum_has_flag (cfg, klass, enum_this, enum_flag);
11122                                         break;
11123                                 }
11124                         }
11125
11126                         // FIXME: LLVM can't handle the inconsistent bb linking
11127                         if (!mono_class_is_nullable (klass) &&
11128                                 !mini_is_gsharedvt_klass (klass) &&
11129                                 ip + 5 < end && ip_in_bb (cfg, cfg->cbb, ip + 5) &&
11130                                 (ip [5] == CEE_BRTRUE || 
11131                                  ip [5] == CEE_BRTRUE_S ||
11132                                  ip [5] == CEE_BRFALSE ||
11133                                  ip [5] == CEE_BRFALSE_S)) {
11134                                 gboolean is_true = ip [5] == CEE_BRTRUE || ip [5] == CEE_BRTRUE_S;
11135                                 int dreg;
11136                                 MonoBasicBlock *true_bb, *false_bb;
11137
11138                                 ip += 5;
11139
11140                                 if (cfg->verbose_level > 3) {
11141                                         printf ("converting (in B%d: stack: %d) %s", cfg->cbb->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
11142                                         printf ("<box+brtrue opt>\n");
11143                                 }
11144
11145                                 switch (*ip) {
11146                                 case CEE_BRTRUE_S:
11147                                 case CEE_BRFALSE_S:
11148                                         CHECK_OPSIZE (2);
11149                                         ip++;
11150                                         target = ip + 1 + (signed char)(*ip);
11151                                         ip++;
11152                                         break;
11153                                 case CEE_BRTRUE:
11154                                 case CEE_BRFALSE:
11155                                         CHECK_OPSIZE (5);
11156                                         ip++;
11157                                         target = ip + 4 + (gint)(read32 (ip));
11158                                         ip += 4;
11159                                         break;
11160                                 default:
11161                                         g_assert_not_reached ();
11162                                 }
11163
11164                                 /* 
11165                                  * We need to link both bblocks, since it is needed for handling stack
11166                                  * arguments correctly (See test_0_box_brtrue_opt_regress_81102).
11167                                  * Branching to only one of them would lead to inconsistencies, so
11168                                  * generate an ICONST+BRTRUE, the branch opts will get rid of them.
11169                                  */
11170                                 GET_BBLOCK (cfg, true_bb, target);
11171                                 GET_BBLOCK (cfg, false_bb, ip);
11172
11173                                 mono_link_bblock (cfg, cfg->cbb, true_bb);
11174                                 mono_link_bblock (cfg, cfg->cbb, false_bb);
11175
11176                                 if (sp != stack_start) {
11177                                         handle_stack_args (cfg, stack_start, sp - stack_start);
11178                                         sp = stack_start;
11179                                         CHECK_UNVERIFIABLE (cfg);
11180                                 }
11181
11182                                 if (COMPILE_LLVM (cfg)) {
11183                                         dreg = alloc_ireg (cfg);
11184                                         MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
11185                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, dreg, is_true ? 0 : 1);
11186
11187                                         MONO_EMIT_NEW_BRANCH_BLOCK2 (cfg, OP_IBEQ, true_bb, false_bb);
11188                                 } else {
11189                                         /* The JIT can't eliminate the iconst+compare */
11190                                         MONO_INST_NEW (cfg, ins, OP_BR);
11191                                         ins->inst_target_bb = is_true ? true_bb : false_bb;
11192                                         MONO_ADD_INS (cfg->cbb, ins);
11193                                 }
11194
11195                                 start_new_bblock = 1;
11196                                 break;
11197                         }
11198
11199                         *sp++ = handle_box (cfg, val, klass, context_used);
11200
11201                         CHECK_CFG_EXCEPTION;
11202                         ip += 5;
11203                         inline_costs += 1;
11204                         break;
11205                 }
11206                 case CEE_UNBOX: {
11207                         CHECK_STACK (1);
11208                         --sp;
11209                         CHECK_OPSIZE (5);
11210                         token = read32 (ip + 1);
11211                         klass = mini_get_class (method, token, generic_context);
11212                         CHECK_TYPELOAD (klass);
11213
11214                         mono_save_token_info (cfg, image, token, klass);
11215
11216                         context_used = mini_class_check_context_used (cfg, klass);
11217
11218                         if (mono_class_is_nullable (klass)) {
11219                                 MonoInst *val;
11220
11221                                 val = handle_unbox_nullable (cfg, *sp, klass, context_used);
11222                                 EMIT_NEW_VARLOADA (cfg, ins, get_vreg_to_inst (cfg, val->dreg), &val->klass->byval_arg);
11223
11224                                 *sp++= ins;
11225                         } else {
11226                                 ins = handle_unbox (cfg, klass, sp, context_used);
11227                                 *sp++ = ins;
11228                         }
11229                         ip += 5;
11230                         inline_costs += 2;
11231                         break;
11232                 }
11233                 case CEE_LDFLD:
11234                 case CEE_LDFLDA:
11235                 case CEE_STFLD:
11236                 case CEE_LDSFLD:
11237                 case CEE_LDSFLDA:
11238                 case CEE_STSFLD: {
11239                         MonoClassField *field;
11240 #ifndef DISABLE_REMOTING
11241                         int costs;
11242 #endif
11243                         guint foffset;
11244                         gboolean is_instance;
11245                         int op;
11246                         gpointer addr = NULL;
11247                         gboolean is_special_static;
11248                         MonoType *ftype;
11249                         MonoInst *store_val = NULL;
11250                         MonoInst *thread_ins;
11251
11252                         op = *ip;
11253                         is_instance = (op == CEE_LDFLD || op == CEE_LDFLDA || op == CEE_STFLD);
11254                         if (is_instance) {
11255                                 if (op == CEE_STFLD) {
11256                                         CHECK_STACK (2);
11257                                         sp -= 2;
11258                                         store_val = sp [1];
11259                                 } else {
11260                                         CHECK_STACK (1);
11261                                         --sp;
11262                                 }
11263                                 if (sp [0]->type == STACK_I4 || sp [0]->type == STACK_I8 || sp [0]->type == STACK_R8)
11264                                         UNVERIFIED;
11265                                 if (*ip != CEE_LDFLD && sp [0]->type == STACK_VTYPE)
11266                                         UNVERIFIED;
11267                         } else {
11268                                 if (op == CEE_STSFLD) {
11269                                         CHECK_STACK (1);
11270                                         sp--;
11271                                         store_val = sp [0];
11272                                 }
11273                         }
11274
11275                         CHECK_OPSIZE (5);
11276                         token = read32 (ip + 1);
11277                         if (method->wrapper_type != MONO_WRAPPER_NONE) {
11278                                 field = (MonoClassField *)mono_method_get_wrapper_data (method, token);
11279                                 klass = field->parent;
11280                         }
11281                         else {
11282                                 field = mono_field_from_token_checked (image, token, &klass, generic_context, &cfg->error);
11283                                 CHECK_CFG_ERROR;
11284                         }
11285                         if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_field (method, field))
11286                                 FIELD_ACCESS_FAILURE (method, field);
11287                         mono_class_init (klass);
11288
11289                         /* if the class is Critical then transparent code cannot access it's fields */
11290                         if (!is_instance && mono_security_core_clr_enabled ())
11291                                 ensure_method_is_allowed_to_access_field (cfg, method, field);
11292
11293                         /* XXX this is technically required but, so far (SL2), no [SecurityCritical] types (not many exists) have
11294                            any visible *instance* field  (in fact there's a single case for a static field in Marshal) XXX
11295                         if (mono_security_core_clr_enabled ())
11296                                 ensure_method_is_allowed_to_access_field (cfg, method, field);
11297                         */
11298
11299                         ftype = mono_field_get_type (field);
11300
11301                         /*
11302                          * LDFLD etc. is usable on static fields as well, so convert those cases to
11303                          * the static case.
11304                          */
11305                         if (is_instance && ftype->attrs & FIELD_ATTRIBUTE_STATIC) {
11306                                 switch (op) {
11307                                 case CEE_LDFLD:
11308                                         op = CEE_LDSFLD;
11309                                         break;
11310                                 case CEE_STFLD:
11311                                         op = CEE_STSFLD;
11312                                         break;
11313                                 case CEE_LDFLDA:
11314                                         op = CEE_LDSFLDA;
11315                                         break;
11316                                 default:
11317                                         g_assert_not_reached ();
11318                                 }
11319                                 is_instance = FALSE;
11320                         }
11321
11322                         context_used = mini_class_check_context_used (cfg, klass);
11323
11324                         /* INSTANCE CASE */
11325
11326                         foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
11327                         if (op == CEE_STFLD) {
11328                                 if (target_type_is_incompatible (cfg, field->type, sp [1]))
11329                                         UNVERIFIED;
11330 #ifndef DISABLE_REMOTING
11331                                 if ((mono_class_is_marshalbyref (klass) && !MONO_CHECK_THIS (sp [0])) || mono_class_is_contextbound (klass) || klass == mono_defaults.marshalbyrefobject_class) {
11332                                         MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type); 
11333                                         MonoInst *iargs [5];
11334
11335                                         GSHAREDVT_FAILURE (op);
11336
11337                                         iargs [0] = sp [0];
11338                                         EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
11339                                         EMIT_NEW_FIELDCONST (cfg, iargs [2], field);
11340                                         EMIT_NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : 
11341                                                     field->offset);
11342                                         iargs [4] = sp [1];
11343
11344                                         if (cfg->opt & MONO_OPT_INLINE || cfg->compile_aot) {
11345                                                 costs = inline_method (cfg, stfld_wrapper, mono_method_signature (stfld_wrapper), 
11346                                                                                            iargs, ip, cfg->real_offset, TRUE);
11347                                                 CHECK_CFG_EXCEPTION;
11348                                                 g_assert (costs > 0);
11349                                                       
11350                                                 cfg->real_offset += 5;
11351
11352                                                 inline_costs += costs;
11353                                         } else {
11354                                                 mono_emit_method_call (cfg, stfld_wrapper, iargs, NULL);
11355                                         }
11356                                 } else
11357 #endif
11358                                 {
11359                                         MonoInst *store;
11360
11361                                         MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
11362
11363                                         if (mini_is_gsharedvt_klass (klass)) {
11364                                                 MonoInst *offset_ins;
11365
11366                                                 context_used = mini_class_check_context_used (cfg, klass);
11367
11368                                                 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
11369                                                 /* The value is offset by 1 */
11370                                                 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
11371                                                 dreg = alloc_ireg_mp (cfg);
11372                                                 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
11373                                                 /* The decomposition will call mini_emit_stobj () which will emit a wbarrier if needed */
11374                                                 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, field->type, dreg, 0, sp [1]->dreg);
11375                                         } else {
11376                                                 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, field->type, sp [0]->dreg, foffset, sp [1]->dreg);
11377                                         }
11378                                         if (sp [0]->opcode != OP_LDADDR)
11379                                                 store->flags |= MONO_INST_FAULT;
11380
11381                                 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)) {
11382                                         /* insert call to write barrier */
11383                                         MonoInst *ptr;
11384                                         int dreg;
11385
11386                                         dreg = alloc_ireg_mp (cfg);
11387                                         EMIT_NEW_BIALU_IMM (cfg, ptr, OP_PADD_IMM, dreg, sp [0]->dreg, foffset);
11388                                         emit_write_barrier (cfg, ptr, sp [1]);
11389                                 }
11390
11391                                         store->flags |= ins_flag;
11392                                 }
11393                                 ins_flag = 0;
11394                                 ip += 5;
11395                                 break;
11396                         }
11397
11398 #ifndef DISABLE_REMOTING
11399                         if (is_instance && ((mono_class_is_marshalbyref (klass) && !MONO_CHECK_THIS (sp [0])) || mono_class_is_contextbound (klass) || klass == mono_defaults.marshalbyrefobject_class)) {
11400                                 MonoMethod *wrapper = (op == CEE_LDFLDA) ? mono_marshal_get_ldflda_wrapper (field->type) : mono_marshal_get_ldfld_wrapper (field->type); 
11401                                 MonoInst *iargs [4];
11402
11403                                 GSHAREDVT_FAILURE (op);
11404
11405                                 iargs [0] = sp [0];
11406                                 EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
11407                                 EMIT_NEW_FIELDCONST (cfg, iargs [2], field);
11408                                 EMIT_NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
11409                                 if (cfg->opt & MONO_OPT_INLINE || cfg->compile_aot) {
11410                                         costs = inline_method (cfg, wrapper, mono_method_signature (wrapper), 
11411                                                                                    iargs, ip, cfg->real_offset, TRUE);
11412                                         CHECK_CFG_EXCEPTION;
11413                                         g_assert (costs > 0);
11414                                                       
11415                                         cfg->real_offset += 5;
11416
11417                                         *sp++ = iargs [0];
11418
11419                                         inline_costs += costs;
11420                                 } else {
11421                                         ins = mono_emit_method_call (cfg, wrapper, iargs, NULL);
11422                                         *sp++ = ins;
11423                                 }
11424                         } else 
11425 #endif
11426                         if (is_instance) {
11427                                 if (sp [0]->type == STACK_VTYPE) {
11428                                         MonoInst *var;
11429
11430                                         /* Have to compute the address of the variable */
11431
11432                                         var = get_vreg_to_inst (cfg, sp [0]->dreg);
11433                                         if (!var)
11434                                                 var = mono_compile_create_var_for_vreg (cfg, &klass->byval_arg, OP_LOCAL, sp [0]->dreg);
11435                                         else
11436                                                 g_assert (var->klass == klass);
11437                                         
11438                                         EMIT_NEW_VARLOADA (cfg, ins, var, &var->klass->byval_arg);
11439                                         sp [0] = ins;
11440                                 }
11441
11442                                 if (op == CEE_LDFLDA) {
11443                                         if (sp [0]->type == STACK_OBJ) {
11444                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, sp [0]->dreg, 0);
11445                                                 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "NullReferenceException");
11446                                         }
11447
11448                                         dreg = alloc_ireg_mp (cfg);
11449
11450                                         if (mini_is_gsharedvt_klass (klass)) {
11451                                                 MonoInst *offset_ins;
11452
11453                                                 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
11454                                                 /* The value is offset by 1 */
11455                                                 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
11456                                                 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
11457                                         } else {
11458                                                 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, dreg, sp [0]->dreg, foffset);
11459                                         }
11460                                         ins->klass = mono_class_from_mono_type (field->type);
11461                                         ins->type = STACK_MP;
11462                                         *sp++ = ins;
11463                                 } else {
11464                                         MonoInst *load;
11465
11466                                         MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
11467
11468                                         if (mini_is_gsharedvt_klass (klass)) {
11469                                                 MonoInst *offset_ins;
11470
11471                                                 offset_ins = emit_get_gsharedvt_info (cfg, field, MONO_RGCTX_INFO_FIELD_OFFSET);
11472                                                 /* The value is offset by 1 */
11473                                                 EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
11474                                                 dreg = alloc_ireg_mp (cfg);
11475                                                 EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, sp [0]->dreg, offset_ins->dreg);
11476                                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, dreg, 0);
11477                                         } else {
11478                                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, sp [0]->dreg, foffset);
11479                                         }
11480                                         load->flags |= ins_flag;
11481                                         if (sp [0]->opcode != OP_LDADDR)
11482                                                 load->flags |= MONO_INST_FAULT;
11483                                         *sp++ = load;
11484                                 }
11485                         }
11486
11487                         if (is_instance) {
11488                                 ins_flag = 0;
11489                                 ip += 5;
11490                                 break;
11491                         }
11492
11493                         /* STATIC CASE */
11494                         context_used = mini_class_check_context_used (cfg, klass);
11495
11496                         if (ftype->attrs & FIELD_ATTRIBUTE_LITERAL)
11497                                 UNVERIFIED;
11498
11499                         /* The special_static_fields field is init'd in mono_class_vtable, so it needs
11500                          * to be called here.
11501                          */
11502                         if (!context_used && !(cfg->opt & MONO_OPT_SHARED)) {
11503                                 mono_class_vtable (cfg->domain, klass);
11504                                 CHECK_TYPELOAD (klass);
11505                         }
11506                         mono_domain_lock (cfg->domain);
11507                         if (cfg->domain->special_static_fields)
11508                                 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
11509                         mono_domain_unlock (cfg->domain);
11510
11511                         is_special_static = mono_class_field_is_special_static (field);
11512
11513                         if (is_special_static && ((gsize)addr & 0x80000000) == 0)
11514                                 thread_ins = mono_get_thread_intrinsic (cfg);
11515                         else
11516                                 thread_ins = NULL;
11517
11518                         /* Generate IR to compute the field address */
11519                         if (is_special_static && ((gsize)addr & 0x80000000) == 0 && thread_ins && !(cfg->opt & MONO_OPT_SHARED) && !context_used) {
11520                                 /*
11521                                  * Fast access to TLS data
11522                                  * Inline version of get_thread_static_data () in
11523                                  * threads.c.
11524                                  */
11525                                 guint32 offset;
11526                                 int idx, static_data_reg, array_reg, dreg;
11527
11528                                 GSHAREDVT_FAILURE (op);
11529
11530                                 MONO_ADD_INS (cfg->cbb, thread_ins);
11531                                 static_data_reg = alloc_ireg (cfg);
11532                                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, static_data_reg, thread_ins->dreg, MONO_STRUCT_OFFSET (MonoInternalThread, static_data));
11533
11534                                 if (cfg->compile_aot) {
11535                                         int offset_reg, offset2_reg, idx_reg;
11536
11537                                         /* For TLS variables, this will return the TLS offset */
11538                                         EMIT_NEW_SFLDACONST (cfg, ins, field);
11539                                         offset_reg = ins->dreg;
11540                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, offset_reg, offset_reg, 0x7fffffff);
11541                                         idx_reg = alloc_ireg (cfg);
11542                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, idx_reg, offset_reg, 0x3f);
11543                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHL_IMM, idx_reg, idx_reg, sizeof (gpointer) == 8 ? 3 : 2);
11544                                         MONO_EMIT_NEW_BIALU (cfg, OP_PADD, static_data_reg, static_data_reg, idx_reg);
11545                                         array_reg = alloc_ireg (cfg);
11546                                         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, static_data_reg, 0);
11547                                         offset2_reg = alloc_ireg (cfg);
11548                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ISHR_UN_IMM, offset2_reg, offset_reg, 6);
11549                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_IAND_IMM, offset2_reg, offset2_reg, 0x1ffffff);
11550                                         dreg = alloc_ireg (cfg);
11551                                         EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, array_reg, offset2_reg);
11552                                 } else {
11553                                         offset = (gsize)addr & 0x7fffffff;
11554                                         idx = offset & 0x3f;
11555
11556                                         array_reg = alloc_ireg (cfg);
11557                                         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, array_reg, static_data_reg, idx * sizeof (gpointer));
11558                                         dreg = alloc_ireg (cfg);
11559                                         EMIT_NEW_BIALU_IMM (cfg, ins, OP_ADD_IMM, dreg, array_reg, ((offset >> 6) & 0x1ffffff));
11560                                 }
11561                         } else if ((cfg->opt & MONO_OPT_SHARED) ||
11562                                         (cfg->compile_aot && is_special_static) ||
11563                                         (context_used && is_special_static)) {
11564                                 MonoInst *iargs [2];
11565
11566                                 g_assert (field->parent);
11567                                 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
11568                                 if (context_used) {
11569                                         iargs [1] = emit_get_rgctx_field (cfg, context_used,
11570                                                 field, MONO_RGCTX_INFO_CLASS_FIELD);
11571                                 } else {
11572                                         EMIT_NEW_FIELDCONST (cfg, iargs [1], field);
11573                                 }
11574                                 ins = mono_emit_jit_icall (cfg, mono_class_static_field_address, iargs);
11575                         } else if (context_used) {
11576                                 MonoInst *static_data;
11577
11578                                 /*
11579                                 g_print ("sharing static field access in %s.%s.%s - depth %d offset %d\n",
11580                                         method->klass->name_space, method->klass->name, method->name,
11581                                         depth, field->offset);
11582                                 */
11583
11584                                 if (mono_class_needs_cctor_run (klass, method))
11585                                         emit_class_init (cfg, klass);
11586
11587                                 /*
11588                                  * The pointer we're computing here is
11589                                  *
11590                                  *   super_info.static_data + field->offset
11591                                  */
11592                                 static_data = emit_get_rgctx_klass (cfg, context_used,
11593                                         klass, MONO_RGCTX_INFO_STATIC_DATA);
11594
11595                                 if (mini_is_gsharedvt_klass (klass)) {
11596                                         MonoInst *offset_ins;
11597
11598                                         offset_ins = emit_get_rgctx_field (cfg, context_used, field, MONO_RGCTX_INFO_FIELD_OFFSET);
11599                                         /* The value is offset by 1 */
11600                                         EMIT_NEW_BIALU_IMM (cfg, ins, OP_PSUB_IMM, offset_ins->dreg, offset_ins->dreg, 1);
11601                                         dreg = alloc_ireg_mp (cfg);
11602                                         EMIT_NEW_BIALU (cfg, ins, OP_PADD, dreg, static_data->dreg, offset_ins->dreg);
11603                                 } else if (field->offset == 0) {
11604                                         ins = static_data;
11605                                 } else {
11606                                         int addr_reg = mono_alloc_preg (cfg);
11607                                         EMIT_NEW_BIALU_IMM (cfg, ins, OP_PADD_IMM, addr_reg, static_data->dreg, field->offset);
11608                                 }
11609                                 } else if ((cfg->opt & MONO_OPT_SHARED) || (cfg->compile_aot && addr)) {
11610                                 MonoInst *iargs [2];
11611
11612                                 g_assert (field->parent);
11613                                 EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
11614                                 EMIT_NEW_FIELDCONST (cfg, iargs [1], field);
11615                                 ins = mono_emit_jit_icall (cfg, mono_class_static_field_address, iargs);
11616                         } else {
11617                                 MonoVTable *vtable = NULL;
11618
11619                                 if (!cfg->compile_aot)
11620                                         vtable = mono_class_vtable (cfg->domain, klass);
11621                                 CHECK_TYPELOAD (klass);
11622
11623                                 if (!addr) {
11624                                         if (mini_field_access_needs_cctor_run (cfg, method, klass, vtable)) {
11625                                                 if (!(g_slist_find (class_inits, klass))) {
11626                                                         emit_class_init (cfg, klass);
11627                                                         if (cfg->verbose_level > 2)
11628                                                                 printf ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, mono_field_get_name (field));
11629                                                         class_inits = g_slist_prepend (class_inits, klass);
11630                                                 }
11631                                         } else {
11632                                                 if (cfg->run_cctors) {
11633                                                         /* This makes so that inline cannot trigger */
11634                                                         /* .cctors: too many apps depend on them */
11635                                                         /* running with a specific order... */
11636                                                         g_assert (vtable);
11637                                                         if (! vtable->initialized)
11638                                                                 INLINE_FAILURE ("class init");
11639                                                         if (!mono_runtime_class_init_full (vtable, &cfg->error)) {
11640                                                                 mono_cfg_set_exception (cfg, MONO_EXCEPTION_MONO_ERROR);
11641                                                                 g_assert_not_reached ();
11642                                                                 goto exception_exit;
11643                                                         }
11644                                                 }
11645                                         }
11646                                         if (cfg->compile_aot)
11647                                                 EMIT_NEW_SFLDACONST (cfg, ins, field);
11648                                         else {
11649                                                 g_assert (vtable);
11650                                                 addr = (char*)mono_vtable_get_static_field_data (vtable) + field->offset;
11651                                                 g_assert (addr);
11652                                                 EMIT_NEW_PCONST (cfg, ins, addr);
11653                                         }
11654                                 } else {
11655                                         MonoInst *iargs [1];
11656                                         EMIT_NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
11657                                         ins = mono_emit_jit_icall (cfg, mono_get_special_static_data, iargs);
11658                                 }
11659                         }
11660
11661                         /* Generate IR to do the actual load/store operation */
11662
11663                         if ((op == CEE_STFLD || op == CEE_STSFLD) && (ins_flag & MONO_INST_VOLATILE)) {
11664                                 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
11665                                 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
11666                         }
11667
11668                         if (op == CEE_LDSFLDA) {
11669                                 ins->klass = mono_class_from_mono_type (ftype);
11670                                 ins->type = STACK_PTR;
11671                                 *sp++ = ins;
11672                         } else if (op == CEE_STSFLD) {
11673                                 MonoInst *store;
11674
11675                                 EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, ftype, ins->dreg, 0, store_val->dreg);
11676                                 store->flags |= ins_flag;
11677                         } else {
11678                                 gboolean is_const = FALSE;
11679                                 MonoVTable *vtable = NULL;
11680                                 gpointer addr = NULL;
11681
11682                                 if (!context_used) {
11683                                         vtable = mono_class_vtable (cfg->domain, klass);
11684                                         CHECK_TYPELOAD (klass);
11685                                 }
11686                                 if ((ftype->attrs & FIELD_ATTRIBUTE_INIT_ONLY) && (((addr = mono_aot_readonly_field_override (field)) != NULL) ||
11687                                                 (!context_used && !((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) && vtable->initialized))) {
11688                                         int ro_type = ftype->type;
11689                                         if (!addr)
11690                                                 addr = (char*)mono_vtable_get_static_field_data (vtable) + field->offset;
11691                                         if (ro_type == MONO_TYPE_VALUETYPE && ftype->data.klass->enumtype) {
11692                                                 ro_type = mono_class_enum_basetype (ftype->data.klass)->type;
11693                                         }
11694
11695                                         GSHAREDVT_FAILURE (op);
11696
11697                                         /* printf ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, mono_field_get_name (field));*/
11698                                         is_const = TRUE;
11699                                         switch (ro_type) {
11700                                         case MONO_TYPE_BOOLEAN:
11701                                         case MONO_TYPE_U1:
11702                                                 EMIT_NEW_ICONST (cfg, *sp, *((guint8 *)addr));
11703                                                 sp++;
11704                                                 break;
11705                                         case MONO_TYPE_I1:
11706                                                 EMIT_NEW_ICONST (cfg, *sp, *((gint8 *)addr));
11707                                                 sp++;
11708                                                 break;                                          
11709                                         case MONO_TYPE_CHAR:
11710                                         case MONO_TYPE_U2:
11711                                                 EMIT_NEW_ICONST (cfg, *sp, *((guint16 *)addr));
11712                                                 sp++;
11713                                                 break;
11714                                         case MONO_TYPE_I2:
11715                                                 EMIT_NEW_ICONST (cfg, *sp, *((gint16 *)addr));
11716                                                 sp++;
11717                                                 break;
11718                                                 break;
11719                                         case MONO_TYPE_I4:
11720                                                 EMIT_NEW_ICONST (cfg, *sp, *((gint32 *)addr));
11721                                                 sp++;
11722                                                 break;                                          
11723                                         case MONO_TYPE_U4:
11724                                                 EMIT_NEW_ICONST (cfg, *sp, *((guint32 *)addr));
11725                                                 sp++;
11726                                                 break;
11727                                         case MONO_TYPE_I:
11728                                         case MONO_TYPE_U:
11729                                         case MONO_TYPE_PTR:
11730                                         case MONO_TYPE_FNPTR:
11731                                                 EMIT_NEW_PCONST (cfg, *sp, *((gpointer *)addr));
11732                                                 type_to_eval_stack_type ((cfg), field->type, *sp);
11733                                                 sp++;
11734                                                 break;
11735                                         case MONO_TYPE_STRING:
11736                                         case MONO_TYPE_OBJECT:
11737                                         case MONO_TYPE_CLASS:
11738                                         case MONO_TYPE_SZARRAY:
11739                                         case MONO_TYPE_ARRAY:
11740                                                 if (!mono_gc_is_moving ()) {
11741                                                         EMIT_NEW_PCONST (cfg, *sp, *((gpointer *)addr));
11742                                                         type_to_eval_stack_type ((cfg), field->type, *sp);
11743                                                         sp++;
11744                                                 } else {
11745                                                         is_const = FALSE;
11746                                                 }
11747                                                 break;
11748                                         case MONO_TYPE_I8:
11749                                         case MONO_TYPE_U8:
11750                                                 EMIT_NEW_I8CONST (cfg, *sp, *((gint64 *)addr));
11751                                                 sp++;
11752                                                 break;
11753                                         case MONO_TYPE_R4:
11754                                         case MONO_TYPE_R8:
11755                                         case MONO_TYPE_VALUETYPE:
11756                                         default:
11757                                                 is_const = FALSE;
11758                                                 break;
11759                                         }
11760                                 }
11761
11762                                 if (!is_const) {
11763                                         MonoInst *load;
11764
11765                                         CHECK_STACK_OVF (1);
11766
11767                                         EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, ins->dreg, 0);
11768                                         load->flags |= ins_flag;
11769                                         ins_flag = 0;
11770                                         *sp++ = load;
11771                                 }
11772                         }
11773
11774                         if ((op == CEE_LDFLD || op == CEE_LDSFLD) && (ins_flag & MONO_INST_VOLATILE)) {
11775                                 /* Volatile loads have acquire semantics, see 12.6.7 in Ecma 335 */
11776                                 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_ACQ);
11777                         }
11778
11779                         ins_flag = 0;
11780                         ip += 5;
11781                         break;
11782                 }
11783                 case CEE_STOBJ:
11784                         CHECK_STACK (2);
11785                         sp -= 2;
11786                         CHECK_OPSIZE (5);
11787                         token = read32 (ip + 1);
11788                         klass = mini_get_class (method, token, generic_context);
11789                         CHECK_TYPELOAD (klass);
11790                         if (ins_flag & MONO_INST_VOLATILE) {
11791                                 /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
11792                                 emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
11793                         }
11794                         /* FIXME: should check item at sp [1] is compatible with the type of the store. */
11795                         EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0, sp [1]->dreg);
11796                         ins->flags |= ins_flag;
11797                         if (cfg->gen_write_barriers && cfg->method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER &&
11798                                         generic_class_is_reference_type (cfg, klass)) {
11799                                 /* insert call to write barrier */
11800                                 emit_write_barrier (cfg, sp [0], sp [1]);
11801                         }
11802                         ins_flag = 0;
11803                         ip += 5;
11804                         inline_costs += 1;
11805                         break;
11806
11807                         /*
11808                          * Array opcodes
11809                          */
11810                 case CEE_NEWARR: {
11811                         MonoInst *len_ins;
11812                         const char *data_ptr;
11813                         int data_size = 0;
11814                         guint32 field_token;
11815
11816                         CHECK_STACK (1);
11817                         --sp;
11818
11819                         CHECK_OPSIZE (5);
11820                         token = read32 (ip + 1);
11821
11822                         klass = mini_get_class (method, token, generic_context);
11823                         CHECK_TYPELOAD (klass);
11824
11825                         context_used = mini_class_check_context_used (cfg, klass);
11826
11827                         if (sp [0]->type == STACK_I8 || (SIZEOF_VOID_P == 8 && sp [0]->type == STACK_PTR)) {
11828                                 MONO_INST_NEW (cfg, ins, OP_LCONV_TO_OVF_U4);
11829                                 ins->sreg1 = sp [0]->dreg;
11830                                 ins->type = STACK_I4;
11831                                 ins->dreg = alloc_ireg (cfg);
11832                                 MONO_ADD_INS (cfg->cbb, ins);
11833                                 *sp = mono_decompose_opcode (cfg, ins);
11834                         }
11835
11836                         if (context_used) {
11837                                 MonoInst *args [3];
11838                                 MonoClass *array_class = mono_array_class_get (klass, 1);
11839                                 MonoMethod *managed_alloc = mono_gc_get_managed_array_allocator (array_class);
11840
11841                                 /* FIXME: Use OP_NEWARR and decompose later to help abcrem */
11842
11843                                 /* vtable */
11844                                 args [0] = emit_get_rgctx_klass (cfg, context_used,
11845                                         array_class, MONO_RGCTX_INFO_VTABLE);
11846                                 /* array len */
11847                                 args [1] = sp [0];
11848
11849                                 if (managed_alloc)
11850                                         ins = mono_emit_method_call (cfg, managed_alloc, args, NULL);
11851                                 else
11852                                         ins = mono_emit_jit_icall (cfg, ves_icall_array_new_specific, args);
11853                         } else {
11854                                 if (cfg->opt & MONO_OPT_SHARED) {
11855                                         /* Decompose now to avoid problems with references to the domainvar */
11856                                         MonoInst *iargs [3];
11857
11858                                         EMIT_NEW_DOMAINCONST (cfg, iargs [0]);
11859                                         EMIT_NEW_CLASSCONST (cfg, iargs [1], klass);
11860                                         iargs [2] = sp [0];
11861
11862                                         ins = mono_emit_jit_icall (cfg, mono_array_new, iargs);
11863                                 } else {
11864                                         /* Decompose later since it is needed by abcrem */
11865                                         MonoClass *array_type = mono_array_class_get (klass, 1);
11866                                         mono_class_vtable (cfg->domain, array_type);
11867                                         CHECK_TYPELOAD (array_type);
11868
11869                                         MONO_INST_NEW (cfg, ins, OP_NEWARR);
11870                                         ins->dreg = alloc_ireg_ref (cfg);
11871                                         ins->sreg1 = sp [0]->dreg;
11872                                         ins->inst_newa_class = klass;
11873                                         ins->type = STACK_OBJ;
11874                                         ins->klass = array_type;
11875                                         MONO_ADD_INS (cfg->cbb, ins);
11876                                         cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
11877                                         cfg->cbb->has_array_access = TRUE;
11878
11879                                         /* Needed so mono_emit_load_get_addr () gets called */
11880                                         mono_get_got_var (cfg);
11881                                 }
11882                         }
11883
11884                         len_ins = sp [0];
11885                         ip += 5;
11886                         *sp++ = ins;
11887                         inline_costs += 1;
11888
11889                         /* 
11890                          * we inline/optimize the initialization sequence if possible.
11891                          * we should also allocate the array as not cleared, since we spend as much time clearing to 0 as initializing
11892                          * for small sizes open code the memcpy
11893                          * ensure the rva field is big enough
11894                          */
11895                         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))) {
11896                                 MonoMethod *memcpy_method = get_memcpy_method ();
11897                                 MonoInst *iargs [3];
11898                                 int add_reg = alloc_ireg_mp (cfg);
11899
11900                                 EMIT_NEW_BIALU_IMM (cfg, iargs [0], OP_PADD_IMM, add_reg, ins->dreg, MONO_STRUCT_OFFSET (MonoArray, vector));
11901                                 if (cfg->compile_aot) {
11902                                         EMIT_NEW_AOTCONST_TOKEN (cfg, iargs [1], MONO_PATCH_INFO_RVA, method->klass->image, GPOINTER_TO_UINT(field_token), STACK_PTR, NULL);
11903                                 } else {
11904                                         EMIT_NEW_PCONST (cfg, iargs [1], (char*)data_ptr);
11905                                 }
11906                                 EMIT_NEW_ICONST (cfg, iargs [2], data_size);
11907                                 mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
11908                                 ip += 11;
11909                         }
11910
11911                         break;
11912                 }
11913                 case CEE_LDLEN:
11914                         CHECK_STACK (1);
11915                         --sp;
11916                         if (sp [0]->type != STACK_OBJ)
11917                                 UNVERIFIED;
11918
11919                         MONO_INST_NEW (cfg, ins, OP_LDLEN);
11920                         ins->dreg = alloc_preg (cfg);
11921                         ins->sreg1 = sp [0]->dreg;
11922                         ins->type = STACK_I4;
11923                         /* This flag will be inherited by the decomposition */
11924                         ins->flags |= MONO_INST_FAULT;
11925                         MONO_ADD_INS (cfg->cbb, ins);
11926                         cfg->flags |= MONO_CFG_HAS_ARRAY_ACCESS;
11927                         cfg->cbb->has_array_access = TRUE;
11928                         ip ++;
11929                         *sp++ = ins;
11930                         break;
11931                 case CEE_LDELEMA:
11932                         CHECK_STACK (2);
11933                         sp -= 2;
11934                         CHECK_OPSIZE (5);
11935                         if (sp [0]->type != STACK_OBJ)
11936                                 UNVERIFIED;
11937
11938                         cfg->flags |= MONO_CFG_HAS_LDELEMA;
11939
11940                         klass = mini_get_class (method, read32 (ip + 1), generic_context);
11941                         CHECK_TYPELOAD (klass);
11942                         /* we need to make sure that this array is exactly the type it needs
11943                          * to be for correctness. the wrappers are lax with their usage
11944                          * so we need to ignore them here
11945                          */
11946                         if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE && !readonly) {
11947                                 MonoClass *array_class = mono_array_class_get (klass, 1);
11948                                 mini_emit_check_array_type (cfg, sp [0], array_class);
11949                                 CHECK_TYPELOAD (array_class);
11950                         }
11951
11952                         readonly = FALSE;
11953                         ins = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
11954                         *sp++ = ins;
11955                         ip += 5;
11956                         break;
11957                 case CEE_LDELEM:
11958                 case CEE_LDELEM_I1:
11959                 case CEE_LDELEM_U1:
11960                 case CEE_LDELEM_I2:
11961                 case CEE_LDELEM_U2:
11962                 case CEE_LDELEM_I4:
11963                 case CEE_LDELEM_U4:
11964                 case CEE_LDELEM_I8:
11965                 case CEE_LDELEM_I:
11966                 case CEE_LDELEM_R4:
11967                 case CEE_LDELEM_R8:
11968                 case CEE_LDELEM_REF: {
11969                         MonoInst *addr;
11970
11971                         CHECK_STACK (2);
11972                         sp -= 2;
11973
11974                         if (*ip == CEE_LDELEM) {
11975                                 CHECK_OPSIZE (5);
11976                                 token = read32 (ip + 1);
11977                                 klass = mini_get_class (method, token, generic_context);
11978                                 CHECK_TYPELOAD (klass);
11979                                 mono_class_init (klass);
11980                         }
11981                         else
11982                                 klass = array_access_to_klass (*ip);
11983
11984                         if (sp [0]->type != STACK_OBJ)
11985                                 UNVERIFIED;
11986
11987                         cfg->flags |= MONO_CFG_HAS_LDELEMA;
11988
11989                         if (mini_is_gsharedvt_variable_klass (klass)) {
11990                                 // FIXME-VT: OP_ICONST optimization
11991                                 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
11992                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
11993                                 ins->opcode = OP_LOADV_MEMBASE;
11994                         } else if (sp [1]->opcode == OP_ICONST) {
11995                                 int array_reg = sp [0]->dreg;
11996                                 int index_reg = sp [1]->dreg;
11997                                 int offset = (mono_class_array_element_size (klass) * sp [1]->inst_c0) + MONO_STRUCT_OFFSET (MonoArray, vector);
11998
11999                                 if (SIZEOF_REGISTER == 8 && COMPILE_LLVM (cfg))
12000                                         MONO_EMIT_NEW_UNALU (cfg, OP_ZEXT_I4, index_reg, index_reg);
12001
12002                                 MONO_EMIT_BOUNDS_CHECK (cfg, array_reg, MonoArray, max_length, index_reg);
12003                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, array_reg, offset);
12004                         } else {
12005                                 addr = mini_emit_ldelema_1_ins (cfg, klass, sp [0], sp [1], TRUE);
12006                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
12007                         }
12008                         *sp++ = ins;
12009                         if (*ip == CEE_LDELEM)
12010                                 ip += 5;
12011                         else
12012                                 ++ip;
12013                         break;
12014                 }
12015                 case CEE_STELEM_I:
12016                 case CEE_STELEM_I1:
12017                 case CEE_STELEM_I2:
12018                 case CEE_STELEM_I4:
12019                 case CEE_STELEM_I8:
12020                 case CEE_STELEM_R4:
12021                 case CEE_STELEM_R8:
12022                 case CEE_STELEM_REF:
12023                 case CEE_STELEM: {
12024                         CHECK_STACK (3);
12025                         sp -= 3;
12026
12027                         cfg->flags |= MONO_CFG_HAS_LDELEMA;
12028
12029                         if (*ip == CEE_STELEM) {
12030                                 CHECK_OPSIZE (5);
12031                                 token = read32 (ip + 1);
12032                                 klass = mini_get_class (method, token, generic_context);
12033                                 CHECK_TYPELOAD (klass);
12034                                 mono_class_init (klass);
12035                         }
12036                         else
12037                                 klass = array_access_to_klass (*ip);
12038
12039                         if (sp [0]->type != STACK_OBJ)
12040                                 UNVERIFIED;
12041
12042                         emit_array_store (cfg, klass, sp, TRUE);
12043
12044                         if (*ip == CEE_STELEM)
12045                                 ip += 5;
12046                         else
12047                                 ++ip;
12048                         inline_costs += 1;
12049                         break;
12050                 }
12051                 case CEE_CKFINITE: {
12052                         CHECK_STACK (1);
12053                         --sp;
12054
12055                         if (cfg->llvm_only) {
12056                                 MonoInst *iargs [1];
12057
12058                                 iargs [0] = sp [0];
12059                                 *sp++ = mono_emit_jit_icall (cfg, mono_ckfinite, iargs);
12060                         } else  {
12061                                 MONO_INST_NEW (cfg, ins, OP_CKFINITE);
12062                                 ins->sreg1 = sp [0]->dreg;
12063                                 ins->dreg = alloc_freg (cfg);
12064                                 ins->type = STACK_R8;
12065                                 MONO_ADD_INS (cfg->cbb, ins);
12066
12067                                 *sp++ = mono_decompose_opcode (cfg, ins);
12068                         }
12069
12070                         ++ip;
12071                         break;
12072                 }
12073                 case CEE_REFANYVAL: {
12074                         MonoInst *src_var, *src;
12075
12076                         int klass_reg = alloc_preg (cfg);
12077                         int dreg = alloc_preg (cfg);
12078
12079                         GSHAREDVT_FAILURE (*ip);
12080
12081                         CHECK_STACK (1);
12082                         MONO_INST_NEW (cfg, ins, *ip);
12083                         --sp;
12084                         CHECK_OPSIZE (5);
12085                         klass = mini_get_class (method, read32 (ip + 1), generic_context);
12086                         CHECK_TYPELOAD (klass);
12087
12088                         context_used = mini_class_check_context_used (cfg, klass);
12089
12090                         // FIXME:
12091                         src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
12092                         if (!src_var)
12093                                 src_var = mono_compile_create_var_for_vreg (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL, sp [0]->dreg);
12094                         EMIT_NEW_VARLOADA (cfg, src, src_var, src_var->inst_vtype);
12095                         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass));
12096
12097                         if (context_used) {
12098                                 MonoInst *klass_ins;
12099
12100                                 klass_ins = emit_get_rgctx_klass (cfg, context_used,
12101                                                 klass, MONO_RGCTX_INFO_KLASS);
12102
12103                                 // FIXME:
12104                                 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, klass_reg, klass_ins->dreg);
12105                                 MONO_EMIT_NEW_COND_EXC (cfg, NE_UN, "InvalidCastException");
12106                         } else {
12107                                 mini_emit_class_check (cfg, klass_reg, klass);
12108                         }
12109                         EMIT_NEW_LOAD_MEMBASE (cfg, ins, OP_LOAD_MEMBASE, dreg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, value));
12110                         ins->type = STACK_MP;
12111                         ins->klass = klass;
12112                         *sp++ = ins;
12113                         ip += 5;
12114                         break;
12115                 }
12116                 case CEE_MKREFANY: {
12117                         MonoInst *loc, *addr;
12118
12119                         GSHAREDVT_FAILURE (*ip);
12120
12121                         CHECK_STACK (1);
12122                         MONO_INST_NEW (cfg, ins, *ip);
12123                         --sp;
12124                         CHECK_OPSIZE (5);
12125                         klass = mini_get_class (method, read32 (ip + 1), generic_context);
12126                         CHECK_TYPELOAD (klass);
12127
12128                         context_used = mini_class_check_context_used (cfg, klass);
12129
12130                         loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
12131                         EMIT_NEW_TEMPLOADA (cfg, addr, loc->inst_c0);
12132
12133                         if (context_used) {
12134                                 MonoInst *const_ins;
12135                                 int type_reg = alloc_preg (cfg);
12136
12137                                 const_ins = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
12138                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass), const_ins->dreg);
12139                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, type_reg, const_ins->dreg, MONO_STRUCT_OFFSET (MonoClass, byval_arg));
12140                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type), type_reg);
12141                         } else if (cfg->compile_aot) {
12142                                 int const_reg = alloc_preg (cfg);
12143                                 int type_reg = alloc_preg (cfg);
12144
12145                                 MONO_EMIT_NEW_CLASSCONST (cfg, const_reg, klass);
12146                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass), const_reg);
12147                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADD_IMM, type_reg, const_reg, MONO_STRUCT_OFFSET (MonoClass, byval_arg));
12148                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type), type_reg);
12149                         } else {
12150                                 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREP_MEMBASE_IMM, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type), &klass->byval_arg);
12151                                 MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STOREP_MEMBASE_IMM, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, klass), klass);
12152                         }
12153                         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, addr->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, value), sp [0]->dreg);
12154
12155                         EMIT_NEW_TEMPLOAD (cfg, ins, loc->inst_c0);
12156                         ins->type = STACK_VTYPE;
12157                         ins->klass = mono_defaults.typed_reference_class;
12158                         *sp++ = ins;
12159                         ip += 5;
12160                         break;
12161                 }
12162                 case CEE_LDTOKEN: {
12163                         gpointer handle;
12164                         MonoClass *handle_class;
12165
12166                         CHECK_STACK_OVF (1);
12167
12168                         CHECK_OPSIZE (5);
12169                         n = read32 (ip + 1);
12170
12171                         if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD ||
12172                                         method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
12173                                 handle = mono_method_get_wrapper_data (method, n);
12174                                 handle_class = (MonoClass *)mono_method_get_wrapper_data (method, n + 1);
12175                                 if (handle_class == mono_defaults.typehandle_class)
12176                                         handle = &((MonoClass*)handle)->byval_arg;
12177                         }
12178                         else {
12179                                 handle = mono_ldtoken_checked (image, n, &handle_class, generic_context, &cfg->error);
12180                                 CHECK_CFG_ERROR;
12181                         }
12182                         if (!handle)
12183                                 LOAD_ERROR;
12184                         mono_class_init (handle_class);
12185                         if (cfg->gshared) {
12186                                 if (mono_metadata_token_table (n) == MONO_TABLE_TYPEDEF ||
12187                                                 mono_metadata_token_table (n) == MONO_TABLE_TYPEREF) {
12188                                         /* This case handles ldtoken
12189                                            of an open type, like for
12190                                            typeof(Gen<>). */
12191                                         context_used = 0;
12192                                 } else if (handle_class == mono_defaults.typehandle_class) {
12193                                         context_used = mini_class_check_context_used (cfg, mono_class_from_mono_type ((MonoType *)handle));
12194                                 } else if (handle_class == mono_defaults.fieldhandle_class)
12195                                         context_used = mini_class_check_context_used (cfg, ((MonoClassField*)handle)->parent);
12196                                 else if (handle_class == mono_defaults.methodhandle_class)
12197                                         context_used = mini_method_check_context_used (cfg, (MonoMethod *)handle);
12198                                 else
12199                                         g_assert_not_reached ();
12200                         }
12201
12202                         if ((cfg->opt & MONO_OPT_SHARED) &&
12203                                         method->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD &&
12204                                         method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED) {
12205                                 MonoInst *addr, *vtvar, *iargs [3];
12206                                 int method_context_used;
12207
12208                                 method_context_used = mini_method_check_context_used (cfg, method);
12209
12210                                 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL); 
12211
12212                                 EMIT_NEW_IMAGECONST (cfg, iargs [0], image);
12213                                 EMIT_NEW_ICONST (cfg, iargs [1], n);
12214                                 if (method_context_used) {
12215                                         iargs [2] = emit_get_rgctx_method (cfg, method_context_used,
12216                                                 method, MONO_RGCTX_INFO_METHOD);
12217                                         ins = mono_emit_jit_icall (cfg, mono_ldtoken_wrapper_generic_shared, iargs);
12218                                 } else {
12219                                         EMIT_NEW_PCONST (cfg, iargs [2], generic_context);
12220                                         ins = mono_emit_jit_icall (cfg, mono_ldtoken_wrapper, iargs);
12221                                 }
12222                                 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
12223
12224                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, ins->dreg);
12225
12226                                 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
12227                         } else {
12228                                 if ((ip + 5 < end) && ip_in_bb (cfg, cfg->cbb, ip + 5) && 
12229                                         ((ip [5] == CEE_CALL) || (ip [5] == CEE_CALLVIRT)) && 
12230                                         (cmethod = mini_get_method (cfg, method, read32 (ip + 6), NULL, generic_context)) &&
12231                                         (cmethod->klass == mono_defaults.systemtype_class) &&
12232                                         (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
12233                                         MonoClass *tclass = mono_class_from_mono_type ((MonoType *)handle);
12234
12235                                         mono_class_init (tclass);
12236                                         if (context_used) {
12237                                                 ins = emit_get_rgctx_klass (cfg, context_used,
12238                                                         tclass, MONO_RGCTX_INFO_REFLECTION_TYPE);
12239                                         } else if (cfg->compile_aot) {
12240                                                 if (method->wrapper_type) {
12241                                                         mono_error_init (&error); //got to do it since there are multiple conditionals below
12242                                                         if (mono_class_get_checked (tclass->image, tclass->type_token, &error) == tclass && !generic_context) {
12243                                                                 /* Special case for static synchronized wrappers */
12244                                                                 EMIT_NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, tclass->image, tclass->type_token, generic_context);
12245                                                         } else {
12246                                                                 mono_error_cleanup (&error); /* FIXME don't swallow the error */
12247                                                                 /* FIXME: n is not a normal token */
12248                                                                 DISABLE_AOT (cfg);
12249                                                                 EMIT_NEW_PCONST (cfg, ins, NULL);
12250                                                         }
12251                                                 } else {
12252                                                         EMIT_NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n, generic_context);
12253                                                 }
12254                                         } else {
12255                                                 MonoReflectionType *rt = mono_type_get_object_checked (cfg->domain, (MonoType *)handle, &cfg->error);
12256                                                 CHECK_CFG_ERROR;
12257                                                 EMIT_NEW_PCONST (cfg, ins, rt);
12258                                         }
12259                                         ins->type = STACK_OBJ;
12260                                         ins->klass = cmethod->klass;
12261                                         ip += 5;
12262                                 } else {
12263                                         MonoInst *addr, *vtvar;
12264
12265                                         vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
12266
12267                                         if (context_used) {
12268                                                 if (handle_class == mono_defaults.typehandle_class) {
12269                                                         ins = emit_get_rgctx_klass (cfg, context_used,
12270                                                                         mono_class_from_mono_type ((MonoType *)handle),
12271                                                                         MONO_RGCTX_INFO_TYPE);
12272                                                 } else if (handle_class == mono_defaults.methodhandle_class) {
12273                                                         ins = emit_get_rgctx_method (cfg, context_used,
12274                                                                         (MonoMethod *)handle, MONO_RGCTX_INFO_METHOD);
12275                                                 } else if (handle_class == mono_defaults.fieldhandle_class) {
12276                                                         ins = emit_get_rgctx_field (cfg, context_used,
12277                                                                         (MonoClassField *)handle, MONO_RGCTX_INFO_CLASS_FIELD);
12278                                                 } else {
12279                                                         g_assert_not_reached ();
12280                                                 }
12281                                         } else if (cfg->compile_aot) {
12282                                                 EMIT_NEW_LDTOKENCONST (cfg, ins, image, n, generic_context);
12283                                         } else {
12284                                                 EMIT_NEW_PCONST (cfg, ins, handle);
12285                                         }
12286                                         EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
12287                                         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, addr->dreg, 0, ins->dreg);
12288                                         EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
12289                                 }
12290                         }
12291
12292                         *sp++ = ins;
12293                         ip += 5;
12294                         break;
12295                 }
12296                 case CEE_THROW:
12297                         CHECK_STACK (1);
12298                         MONO_INST_NEW (cfg, ins, OP_THROW);
12299                         --sp;
12300                         ins->sreg1 = sp [0]->dreg;
12301                         ip++;
12302                         cfg->cbb->out_of_line = TRUE;
12303                         MONO_ADD_INS (cfg->cbb, ins);
12304                         MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
12305                         MONO_ADD_INS (cfg->cbb, ins);
12306                         sp = stack_start;
12307                         
12308                         link_bblock (cfg, cfg->cbb, end_bblock);
12309                         start_new_bblock = 1;
12310                         /* This can complicate code generation for llvm since the return value might not be defined */
12311                         if (COMPILE_LLVM (cfg))
12312                                 INLINE_FAILURE ("throw");
12313                         break;
12314                 case CEE_ENDFINALLY:
12315                         /* mono_save_seq_point_info () depends on this */
12316                         if (sp != stack_start)
12317                                 emit_seq_point (cfg, method, ip, FALSE, FALSE);
12318                         MONO_INST_NEW (cfg, ins, OP_ENDFINALLY);
12319                         MONO_ADD_INS (cfg->cbb, ins);
12320                         ip++;
12321                         start_new_bblock = 1;
12322
12323                         /*
12324                          * Control will leave the method so empty the stack, otherwise
12325                          * the next basic block will start with a nonempty stack.
12326                          */
12327                         while (sp != stack_start) {
12328                                 sp--;
12329                         }
12330                         break;
12331                 case CEE_LEAVE:
12332                 case CEE_LEAVE_S: {
12333                         GList *handlers;
12334
12335                         if (*ip == CEE_LEAVE) {
12336                                 CHECK_OPSIZE (5);
12337                                 target = ip + 5 + (gint32)read32(ip + 1);
12338                         } else {
12339                                 CHECK_OPSIZE (2);
12340                                 target = ip + 2 + (signed char)(ip [1]);
12341                         }
12342
12343                         /* empty the stack */
12344                         while (sp != stack_start) {
12345                                 sp--;
12346                         }
12347
12348                         /* 
12349                          * If this leave statement is in a catch block, check for a
12350                          * pending exception, and rethrow it if necessary.
12351                          * We avoid doing this in runtime invoke wrappers, since those are called
12352                          * by native code which excepts the wrapper to catch all exceptions.
12353                          */
12354                         for (i = 0; i < header->num_clauses; ++i) {
12355                                 MonoExceptionClause *clause = &header->clauses [i];
12356
12357                                 /* 
12358                                  * Use <= in the final comparison to handle clauses with multiple
12359                                  * leave statements, like in bug #78024.
12360                                  * The ordering of the exception clauses guarantees that we find the
12361                                  * innermost clause.
12362                                  */
12363                                 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) {
12364                                         MonoInst *exc_ins;
12365                                         MonoBasicBlock *dont_throw;
12366
12367                                         /*
12368                                           MonoInst *load;
12369
12370                                           NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, clause->handler_offset)->inst_c0);
12371                                         */
12372
12373                                         exc_ins = mono_emit_jit_icall (cfg, mono_thread_get_undeniable_exception, NULL);
12374
12375                                         NEW_BBLOCK (cfg, dont_throw);
12376
12377                                         /*
12378                                          * Currently, we always rethrow the abort exception, despite the 
12379                                          * fact that this is not correct. See thread6.cs for an example. 
12380                                          * But propagating the abort exception is more important than 
12381                                          * getting the sematics right.
12382                                          */
12383                                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, exc_ins->dreg, 0);
12384                                         MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, dont_throw);
12385                                         MONO_EMIT_NEW_UNALU (cfg, OP_THROW, -1, exc_ins->dreg);
12386
12387                                         MONO_START_BB (cfg, dont_throw);
12388                                 }
12389                         }
12390
12391 #ifdef ENABLE_LLVM
12392                         cfg->cbb->try_end = (intptr_t)(ip - header->code);
12393 #endif
12394
12395                         if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
12396                                 GList *tmp;
12397                                 MonoExceptionClause *clause;
12398
12399                                 for (tmp = handlers; tmp; tmp = tmp->next) {
12400                                         clause = (MonoExceptionClause *)tmp->data;
12401                                         tblock = cfg->cil_offset_to_bb [clause->handler_offset];
12402                                         g_assert (tblock);
12403                                         link_bblock (cfg, cfg->cbb, tblock);
12404                                         MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
12405                                         ins->inst_target_bb = tblock;
12406                                         ins->inst_eh_block = clause;
12407                                         MONO_ADD_INS (cfg->cbb, ins);
12408                                         cfg->cbb->has_call_handler = 1;
12409                                         if (COMPILE_LLVM (cfg)) {
12410                                                 MonoBasicBlock *target_bb;
12411
12412                                                 /* 
12413                                                  * Link the finally bblock with the target, since it will
12414                                                  * conceptually branch there.
12415                                                  */
12416                                                 GET_BBLOCK (cfg, tblock, cfg->cil_start + clause->handler_offset + clause->handler_len - 1);
12417                                                 GET_BBLOCK (cfg, target_bb, target);
12418                                                 link_bblock (cfg, tblock, target_bb);
12419                                         }
12420                                 }
12421                                 g_list_free (handlers);
12422                         } 
12423
12424                         MONO_INST_NEW (cfg, ins, OP_BR);
12425                         MONO_ADD_INS (cfg->cbb, ins);
12426                         GET_BBLOCK (cfg, tblock, target);
12427                         link_bblock (cfg, cfg->cbb, tblock);
12428                         ins->inst_target_bb = tblock;
12429
12430                         start_new_bblock = 1;
12431
12432                         if (*ip == CEE_LEAVE)
12433                                 ip += 5;
12434                         else
12435                                 ip += 2;
12436
12437                         break;
12438                 }
12439
12440                         /*
12441                          * Mono specific opcodes
12442                          */
12443                 case MONO_CUSTOM_PREFIX: {
12444
12445                         g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
12446
12447                         CHECK_OPSIZE (2);
12448                         switch (ip [1]) {
12449                         case CEE_MONO_ICALL: {
12450                                 gpointer func;
12451                                 MonoJitICallInfo *info;
12452
12453                                 token = read32 (ip + 2);
12454                                 func = mono_method_get_wrapper_data (method, token);
12455                                 info = mono_find_jit_icall_by_addr (func);
12456                                 if (!info)
12457                                         g_error ("Could not find icall address in wrapper %s", mono_method_full_name (method, 1));
12458                                 g_assert (info);
12459
12460                                 CHECK_STACK (info->sig->param_count);
12461                                 sp -= info->sig->param_count;
12462
12463                                 ins = mono_emit_jit_icall (cfg, info->func, sp);
12464                                 if (!MONO_TYPE_IS_VOID (info->sig->ret))
12465                                         *sp++ = ins;
12466
12467                                 ip += 6;
12468                                 inline_costs += 10 * num_calls++;
12469
12470                                 break;
12471                         }
12472                         case CEE_MONO_LDPTR_CARD_TABLE:
12473                         case CEE_MONO_LDPTR_NURSERY_START:
12474                         case CEE_MONO_LDPTR_NURSERY_BITS:
12475                         case CEE_MONO_LDPTR_INT_REQ_FLAG: {
12476                                 CHECK_STACK_OVF (1);
12477
12478                                 switch (ip [1]) {
12479                                         case CEE_MONO_LDPTR_CARD_TABLE:
12480                                                 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_CARD_TABLE_ADDR, NULL);
12481                                                 break;
12482                                         case CEE_MONO_LDPTR_NURSERY_START:
12483                                                 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_NURSERY_START, NULL);
12484                                                 break;
12485                                         case CEE_MONO_LDPTR_NURSERY_BITS:
12486                                                 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_GC_NURSERY_BITS, NULL);
12487                                                 break;
12488                                         case CEE_MONO_LDPTR_INT_REQ_FLAG:
12489                                                 ins = emit_runtime_constant (cfg, MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG, NULL);
12490                                                 break;
12491                                 }
12492
12493                                 *sp++ = ins;
12494                                 ip += 2;
12495                                 inline_costs += 10 * num_calls++;
12496                                 break;
12497                         }
12498                         case CEE_MONO_LDPTR: {
12499                                 gpointer ptr;
12500
12501                                 CHECK_STACK_OVF (1);
12502                                 CHECK_OPSIZE (6);
12503                                 token = read32 (ip + 2);
12504
12505                                 ptr = mono_method_get_wrapper_data (method, token);
12506                                 EMIT_NEW_PCONST (cfg, ins, ptr);
12507                                 *sp++ = ins;
12508                                 ip += 6;
12509                                 inline_costs += 10 * num_calls++;
12510                                 /* Can't embed random pointers into AOT code */
12511                                 DISABLE_AOT (cfg);
12512                                 break;
12513                         }
12514                         case CEE_MONO_JIT_ICALL_ADDR: {
12515                                 MonoJitICallInfo *callinfo;
12516                                 gpointer ptr;
12517
12518                                 CHECK_STACK_OVF (1);
12519                                 CHECK_OPSIZE (6);
12520                                 token = read32 (ip + 2);
12521
12522                                 ptr = mono_method_get_wrapper_data (method, token);
12523                                 callinfo = mono_find_jit_icall_by_addr (ptr);
12524                                 g_assert (callinfo);
12525                                 EMIT_NEW_JIT_ICALL_ADDRCONST (cfg, ins, (char*)callinfo->name);
12526                                 *sp++ = ins;
12527                                 ip += 6;
12528                                 inline_costs += 10 * num_calls++;
12529                                 break;
12530                         }
12531                         case CEE_MONO_ICALL_ADDR: {
12532                                 MonoMethod *cmethod;
12533                                 gpointer ptr;
12534
12535                                 CHECK_STACK_OVF (1);
12536                                 CHECK_OPSIZE (6);
12537                                 token = read32 (ip + 2);
12538
12539                                 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
12540
12541                                 if (cfg->compile_aot) {
12542                                         EMIT_NEW_AOTCONST (cfg, ins, MONO_PATCH_INFO_ICALL_ADDR, cmethod);
12543                                 } else {
12544                                         ptr = mono_lookup_internal_call (cmethod);
12545                                         g_assert (ptr);
12546                                         EMIT_NEW_PCONST (cfg, ins, ptr);
12547                                 }
12548                                 *sp++ = ins;
12549                                 ip += 6;
12550                                 break;
12551                         }
12552                         case CEE_MONO_VTADDR: {
12553                                 MonoInst *src_var, *src;
12554
12555                                 CHECK_STACK (1);
12556                                 --sp;
12557
12558                                 // FIXME:
12559                                 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
12560                                 EMIT_NEW_VARLOADA ((cfg), (src), src_var, src_var->inst_vtype);
12561                                 *sp++ = src;
12562                                 ip += 2;
12563                                 break;
12564                         }
12565                         case CEE_MONO_NEWOBJ: {
12566                                 MonoInst *iargs [2];
12567
12568                                 CHECK_STACK_OVF (1);
12569                                 CHECK_OPSIZE (6);
12570                                 token = read32 (ip + 2);
12571                                 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
12572                                 mono_class_init (klass);
12573                                 NEW_DOMAINCONST (cfg, iargs [0]);
12574                                 MONO_ADD_INS (cfg->cbb, iargs [0]);
12575                                 NEW_CLASSCONST (cfg, iargs [1], klass);
12576                                 MONO_ADD_INS (cfg->cbb, iargs [1]);
12577                                 *sp++ = mono_emit_jit_icall (cfg, ves_icall_object_new, iargs);
12578                                 ip += 6;
12579                                 inline_costs += 10 * num_calls++;
12580                                 break;
12581                         }
12582                         case CEE_MONO_OBJADDR:
12583                                 CHECK_STACK (1);
12584                                 --sp;
12585                                 MONO_INST_NEW (cfg, ins, OP_MOVE);
12586                                 ins->dreg = alloc_ireg_mp (cfg);
12587                                 ins->sreg1 = sp [0]->dreg;
12588                                 ins->type = STACK_MP;
12589                                 MONO_ADD_INS (cfg->cbb, ins);
12590                                 *sp++ = ins;
12591                                 ip += 2;
12592                                 break;
12593                         case CEE_MONO_LDNATIVEOBJ:
12594                                 /*
12595                                  * Similar to LDOBJ, but instead load the unmanaged 
12596                                  * representation of the vtype to the stack.
12597                                  */
12598                                 CHECK_STACK (1);
12599                                 CHECK_OPSIZE (6);
12600                                 --sp;
12601                                 token = read32 (ip + 2);
12602                                 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
12603                                 g_assert (klass->valuetype);
12604                                 mono_class_init (klass);
12605
12606                                 {
12607                                         MonoInst *src, *dest, *temp;
12608
12609                                         src = sp [0];
12610                                         temp = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
12611                                         temp->backend.is_pinvoke = 1;
12612                                         EMIT_NEW_TEMPLOADA (cfg, dest, temp->inst_c0);
12613                                         mini_emit_stobj (cfg, dest, src, klass, TRUE);
12614
12615                                         EMIT_NEW_TEMPLOAD (cfg, dest, temp->inst_c0);
12616                                         dest->type = STACK_VTYPE;
12617                                         dest->klass = klass;
12618
12619                                         *sp ++ = dest;
12620                                         ip += 6;
12621                                 }
12622                                 break;
12623                         case CEE_MONO_RETOBJ: {
12624                                 /*
12625                                  * Same as RET, but return the native representation of a vtype
12626                                  * to the caller.
12627                                  */
12628                                 g_assert (cfg->ret);
12629                                 g_assert (mono_method_signature (method)->pinvoke); 
12630                                 CHECK_STACK (1);
12631                                 --sp;
12632                                 
12633                                 CHECK_OPSIZE (6);
12634                                 token = read32 (ip + 2);    
12635                                 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
12636
12637                                 if (!cfg->vret_addr) {
12638                                         g_assert (cfg->ret_var_is_local);
12639
12640                                         EMIT_NEW_VARLOADA (cfg, ins, cfg->ret, cfg->ret->inst_vtype);
12641                                 } else {
12642                                         EMIT_NEW_RETLOADA (cfg, ins);
12643                                 }
12644                                 mini_emit_stobj (cfg, ins, sp [0], klass, TRUE);
12645                                 
12646                                 if (sp != stack_start)
12647                                         UNVERIFIED;
12648                                 
12649                                 MONO_INST_NEW (cfg, ins, OP_BR);
12650                                 ins->inst_target_bb = end_bblock;
12651                                 MONO_ADD_INS (cfg->cbb, ins);
12652                                 link_bblock (cfg, cfg->cbb, end_bblock);
12653                                 start_new_bblock = 1;
12654                                 ip += 6;
12655                                 break;
12656                         }
12657                         case CEE_MONO_CISINST:
12658                         case CEE_MONO_CCASTCLASS: {
12659                                 int token;
12660                                 CHECK_STACK (1);
12661                                 --sp;
12662                                 CHECK_OPSIZE (6);
12663                                 token = read32 (ip + 2);
12664                                 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
12665                                 if (ip [1] == CEE_MONO_CISINST)
12666                                         ins = handle_cisinst (cfg, klass, sp [0]);
12667                                 else
12668                                         ins = handle_ccastclass (cfg, klass, sp [0]);
12669                                 *sp++ = ins;
12670                                 ip += 6;
12671                                 break;
12672                         }
12673                         case CEE_MONO_SAVE_LMF:
12674                         case CEE_MONO_RESTORE_LMF:
12675                                 ip += 2;
12676                                 break;
12677                         case CEE_MONO_CLASSCONST:
12678                                 CHECK_STACK_OVF (1);
12679                                 CHECK_OPSIZE (6);
12680                                 token = read32 (ip + 2);
12681                                 EMIT_NEW_CLASSCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
12682                                 *sp++ = ins;
12683                                 ip += 6;
12684                                 inline_costs += 10 * num_calls++;
12685                                 break;
12686                         case CEE_MONO_NOT_TAKEN:
12687                                 cfg->cbb->out_of_line = TRUE;
12688                                 ip += 2;
12689                                 break;
12690                         case CEE_MONO_TLS: {
12691                                 MonoTlsKey key;
12692
12693                                 CHECK_STACK_OVF (1);
12694                                 CHECK_OPSIZE (6);
12695                                 key = (MonoTlsKey)read32 (ip + 2);
12696                                 g_assert (key < TLS_KEY_NUM);
12697
12698                                 ins = mono_create_tls_get (cfg, key);
12699                                 if (!ins) {
12700                                         if (cfg->compile_aot) {
12701                                                 DISABLE_AOT (cfg);
12702                                                 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
12703                                                 ins->dreg = alloc_preg (cfg);
12704                                                 ins->type = STACK_PTR;
12705                                         } else {
12706                                                 g_assert_not_reached ();
12707                                         }
12708                                 }
12709                                 ins->type = STACK_PTR;
12710                                 MONO_ADD_INS (cfg->cbb, ins);
12711                                 *sp++ = ins;
12712                                 ip += 6;
12713                                 break;
12714                         }
12715                         case CEE_MONO_DYN_CALL: {
12716                                 MonoCallInst *call;
12717
12718                                 /* It would be easier to call a trampoline, but that would put an
12719                                  * extra frame on the stack, confusing exception handling. So
12720                                  * implement it inline using an opcode for now.
12721                                  */
12722
12723                                 if (!cfg->dyn_call_var) {
12724                                         cfg->dyn_call_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
12725                                         /* prevent it from being register allocated */
12726                                         cfg->dyn_call_var->flags |= MONO_INST_VOLATILE;
12727                                 }
12728
12729                                 /* Has to use a call inst since it local regalloc expects it */
12730                                 MONO_INST_NEW_CALL (cfg, call, OP_DYN_CALL);
12731                                 ins = (MonoInst*)call;
12732                                 sp -= 2;
12733                                 ins->sreg1 = sp [0]->dreg;
12734                                 ins->sreg2 = sp [1]->dreg;
12735                                 MONO_ADD_INS (cfg->cbb, ins);
12736
12737                                 cfg->param_area = MAX (cfg->param_area, cfg->backend->dyn_call_param_area);
12738
12739                                 ip += 2;
12740                                 inline_costs += 10 * num_calls++;
12741
12742                                 break;
12743                         }
12744                         case CEE_MONO_MEMORY_BARRIER: {
12745                                 CHECK_OPSIZE (6);
12746                                 emit_memory_barrier (cfg, (int)read32 (ip + 2));
12747                                 ip += 6;
12748                                 break;
12749                         }
12750                         case CEE_MONO_JIT_ATTACH: {
12751                                 MonoInst *args [16], *domain_ins;
12752                                 MonoInst *ad_ins, *jit_tls_ins;
12753                                 MonoBasicBlock *next_bb = NULL, *call_bb = NULL;
12754
12755                                 cfg->attach_cookie = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
12756                                 cfg->attach_dummy = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
12757
12758                                 if (mono_threads_is_coop_enabled ()) {
12759                                         /* AOT code is only used in the root domain */
12760                                         EMIT_NEW_PCONST (cfg, args [0], cfg->compile_aot ? NULL : cfg->domain);
12761                                         EMIT_NEW_VARLOADA (cfg, args [1], cfg->attach_dummy, cfg->attach_dummy->inst_vtype);
12762                                         ins = mono_emit_jit_icall (cfg, mono_jit_thread_attach, args);
12763                                         MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->attach_cookie->dreg, ins->dreg);
12764                                 } else {
12765                                         EMIT_NEW_PCONST (cfg, ins, NULL);
12766                                         MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->attach_cookie->dreg, ins->dreg);
12767
12768                                         ad_ins = mono_get_domain_intrinsic (cfg);
12769                                         jit_tls_ins = mono_get_jit_tls_intrinsic (cfg);
12770
12771                                         if (cfg->backend->have_tls_get && ad_ins && jit_tls_ins) {
12772                                                 NEW_BBLOCK (cfg, next_bb);
12773                                                 NEW_BBLOCK (cfg, call_bb);
12774
12775                                                 if (cfg->compile_aot) {
12776                                                         /* AOT code is only used in the root domain */
12777                                                         EMIT_NEW_PCONST (cfg, domain_ins, NULL);
12778                                                 } else {
12779                                                         EMIT_NEW_PCONST (cfg, domain_ins, cfg->domain);
12780                                                 }
12781                                                 MONO_ADD_INS (cfg->cbb, ad_ins);
12782                                                 MONO_EMIT_NEW_BIALU (cfg, OP_COMPARE, -1, ad_ins->dreg, domain_ins->dreg);
12783                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, call_bb);
12784
12785                                                 MONO_ADD_INS (cfg->cbb, jit_tls_ins);
12786                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, jit_tls_ins->dreg, 0);
12787                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, call_bb);
12788
12789                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, next_bb);
12790                                                 MONO_START_BB (cfg, call_bb);
12791                                         }
12792
12793                                         /* AOT code is only used in the root domain */
12794                                         EMIT_NEW_PCONST (cfg, args [0], cfg->compile_aot ? NULL : cfg->domain);
12795                                         EMIT_NEW_PCONST (cfg, args [1], NULL);
12796                                         ins = mono_emit_jit_icall (cfg, mono_jit_thread_attach, args);
12797                                         MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->attach_cookie->dreg, ins->dreg);
12798
12799                                         if (next_bb)
12800                                                 MONO_START_BB (cfg, next_bb);
12801                                 }
12802
12803                                 ip += 2;
12804                                 break;
12805                         }
12806                         case CEE_MONO_JIT_DETACH: {
12807                                 MonoInst *args [16];
12808
12809                                 /* Restore the original domain */
12810                                 dreg = alloc_ireg (cfg);
12811                                 EMIT_NEW_UNALU (cfg, args [0], OP_MOVE, dreg, cfg->attach_cookie->dreg);
12812                                 EMIT_NEW_VARLOADA (cfg, args [1], cfg->attach_dummy, cfg->attach_dummy->inst_vtype);
12813                                 mono_emit_jit_icall (cfg, mono_jit_thread_detach, args);
12814                                 ip += 2;
12815                                 break;
12816                         }
12817                         case CEE_MONO_CALLI_EXTRA_ARG: {
12818                                 MonoInst *addr;
12819                                 MonoMethodSignature *fsig;
12820                                 MonoInst *arg;
12821
12822                                 /*
12823                                  * This is the same as CEE_CALLI, but passes an additional argument
12824                                  * to the called method in llvmonly mode.
12825                                  * This is only used by delegate invoke wrappers to call the
12826                                  * actual delegate method.
12827                                  */
12828                                 g_assert (method->wrapper_type == MONO_WRAPPER_DELEGATE_INVOKE);
12829
12830                                 CHECK_OPSIZE (6);
12831                                 token = read32 (ip + 2);
12832
12833                                 ins = NULL;
12834
12835                                 cmethod = NULL;
12836                                 CHECK_STACK (1);
12837                                 --sp;
12838                                 addr = *sp;
12839                                 fsig = mini_get_signature (method, token, generic_context);
12840
12841                                 if (cfg->llvm_only)
12842                                         cfg->signatures = g_slist_prepend_mempool (cfg->mempool, cfg->signatures, fsig);
12843
12844                                 n = fsig->param_count + fsig->hasthis + 1;
12845
12846                                 CHECK_STACK (n);
12847
12848                                 sp -= n;
12849                                 arg = sp [n - 1];
12850
12851                                 if (cfg->llvm_only) {
12852                                         /*
12853                                          * The lowest bit of 'arg' determines whenever the callee uses the gsharedvt
12854                                          * cconv. This is set by mono_init_delegate ().
12855                                          */
12856                                         if (cfg->gsharedvt && mini_is_gsharedvt_variable_signature (fsig)) {
12857                                                 MonoInst *callee = addr;
12858                                                 MonoInst *call, *localloc_ins;
12859                                                 MonoBasicBlock *is_gsharedvt_bb, *end_bb;
12860                                                 int low_bit_reg = alloc_preg (cfg);
12861
12862                                                 NEW_BBLOCK (cfg, is_gsharedvt_bb);
12863                                                 NEW_BBLOCK (cfg, end_bb);
12864
12865                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PAND_IMM, low_bit_reg, arg->dreg, 1);
12866                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, low_bit_reg, 0);
12867                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, is_gsharedvt_bb);
12868
12869                                                 /* Normal case: callee uses a normal cconv, have to add an out wrapper */
12870                                                 addr = emit_get_rgctx_sig (cfg, context_used,
12871                                                                                                    fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
12872                                                 /*
12873                                                  * ADDR points to a gsharedvt-out wrapper, have to pass <callee, arg> as an extra arg.
12874                                                  */
12875                                                 MONO_INST_NEW (cfg, ins, OP_LOCALLOC_IMM);
12876                                                 ins->dreg = alloc_preg (cfg);
12877                                                 ins->inst_imm = 2 * SIZEOF_VOID_P;
12878                                                 MONO_ADD_INS (cfg->cbb, ins);
12879                                                 localloc_ins = ins;
12880                                                 cfg->flags |= MONO_CFG_HAS_ALLOCA;
12881                                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, 0, callee->dreg);
12882                                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, SIZEOF_VOID_P, arg->dreg);
12883
12884                                                 call = emit_extra_arg_calli (cfg, fsig, sp, localloc_ins->dreg, addr);
12885                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
12886
12887                                                 /* Gsharedvt case: callee uses a gsharedvt cconv, no conversion is needed */
12888                                                 MONO_START_BB (cfg, is_gsharedvt_bb);
12889                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PXOR_IMM, arg->dreg, arg->dreg, 1);
12890                                                 ins = emit_extra_arg_calli (cfg, fsig, sp, arg->dreg, callee);
12891                                                 ins->dreg = call->dreg;
12892
12893                                                 MONO_START_BB (cfg, end_bb);
12894                                         } else {
12895                                                 /* Caller uses a normal calling conv */
12896
12897                                                 MonoInst *callee = addr;
12898                                                 MonoInst *call, *localloc_ins;
12899                                                 MonoBasicBlock *is_gsharedvt_bb, *end_bb;
12900                                                 int low_bit_reg = alloc_preg (cfg);
12901
12902                                                 NEW_BBLOCK (cfg, is_gsharedvt_bb);
12903                                                 NEW_BBLOCK (cfg, end_bb);
12904
12905                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PAND_IMM, low_bit_reg, arg->dreg, 1);
12906                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, low_bit_reg, 0);
12907                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, is_gsharedvt_bb);
12908
12909                                                 /* Normal case: callee uses a normal cconv, no conversion is needed */
12910                                                 call = emit_extra_arg_calli (cfg, fsig, sp, arg->dreg, callee);
12911                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
12912                                                 /* Gsharedvt case: callee uses a gsharedvt cconv, have to add an in wrapper */
12913                                                 MONO_START_BB (cfg, is_gsharedvt_bb);
12914                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_PXOR_IMM, arg->dreg, arg->dreg, 1);
12915                                                 NEW_AOTCONST (cfg, addr, MONO_PATCH_INFO_GSHAREDVT_IN_WRAPPER, fsig);
12916                                                 MONO_ADD_INS (cfg->cbb, addr);
12917                                                 /*
12918                                                  * ADDR points to a gsharedvt-in wrapper, have to pass <callee, arg> as an extra arg.
12919                                                  */
12920                                                 MONO_INST_NEW (cfg, ins, OP_LOCALLOC_IMM);
12921                                                 ins->dreg = alloc_preg (cfg);
12922                                                 ins->inst_imm = 2 * SIZEOF_VOID_P;
12923                                                 MONO_ADD_INS (cfg->cbb, ins);
12924                                                 localloc_ins = ins;
12925                                                 cfg->flags |= MONO_CFG_HAS_ALLOCA;
12926                                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, 0, callee->dreg);
12927                                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, localloc_ins->dreg, SIZEOF_VOID_P, arg->dreg);
12928
12929                                                 ins = emit_extra_arg_calli (cfg, fsig, sp, localloc_ins->dreg, addr);
12930                                                 ins->dreg = call->dreg;
12931                                                 MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
12932
12933                                                 MONO_START_BB (cfg, end_bb);
12934                                         }
12935                                 } else {
12936                                         /* Same as CEE_CALLI */
12937                                         if (cfg->gsharedvt && mini_is_gsharedvt_signature (fsig)) {
12938                                                 /*
12939                                                  * We pass the address to the gsharedvt trampoline in the rgctx reg
12940                                                  */
12941                                                 MonoInst *callee = addr;
12942
12943                                                 addr = emit_get_rgctx_sig (cfg, context_used,
12944                                                                                                    fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
12945                                                 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, callee);
12946                                         } else {
12947                                                 ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, NULL);
12948                                         }
12949                                 }
12950
12951                                 if (!MONO_TYPE_IS_VOID (fsig->ret))
12952                                         *sp++ = mono_emit_widen_call_res (cfg, ins, fsig);
12953
12954                                 CHECK_CFG_EXCEPTION;
12955
12956                                 ip += 6;
12957                                 ins_flag = 0;
12958                                 constrained_class = NULL;
12959                                 break;
12960                         }
12961                         default:
12962                                 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
12963                                 break;
12964                         }
12965                         break;
12966                 }
12967
12968                 case CEE_PREFIX1: {
12969                         CHECK_OPSIZE (2);
12970                         switch (ip [1]) {
12971                         case CEE_ARGLIST: {
12972                                 /* somewhat similar to LDTOKEN */
12973                                 MonoInst *addr, *vtvar;
12974                                 CHECK_STACK_OVF (1);
12975                                 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL); 
12976
12977                                 EMIT_NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
12978                                 EMIT_NEW_UNALU (cfg, ins, OP_ARGLIST, -1, addr->dreg);
12979
12980                                 EMIT_NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
12981                                 ins->type = STACK_VTYPE;
12982                                 ins->klass = mono_defaults.argumenthandle_class;
12983                                 *sp++ = ins;
12984                                 ip += 2;
12985                                 break;
12986                         }
12987                         case CEE_CEQ:
12988                         case CEE_CGT:
12989                         case CEE_CGT_UN:
12990                         case CEE_CLT:
12991                         case CEE_CLT_UN: {
12992                                 MonoInst *cmp, *arg1, *arg2;
12993
12994                                 CHECK_STACK (2);
12995                                 sp -= 2;
12996                                 arg1 = sp [0];
12997                                 arg2 = sp [1];
12998
12999                                 /*
13000                                  * The following transforms:
13001                                  *    CEE_CEQ    into OP_CEQ
13002                                  *    CEE_CGT    into OP_CGT
13003                                  *    CEE_CGT_UN into OP_CGT_UN
13004                                  *    CEE_CLT    into OP_CLT
13005                                  *    CEE_CLT_UN into OP_CLT_UN
13006                                  */
13007                                 MONO_INST_NEW (cfg, cmp, (OP_CEQ - CEE_CEQ) + ip [1]);
13008
13009                                 MONO_INST_NEW (cfg, ins, cmp->opcode);
13010                                 cmp->sreg1 = arg1->dreg;
13011                                 cmp->sreg2 = arg2->dreg;
13012                                 type_from_op (cfg, cmp, arg1, arg2);
13013                                 CHECK_TYPE (cmp);
13014                                 add_widen_op (cfg, cmp, &arg1, &arg2);
13015                                 if ((arg1->type == STACK_I8) || ((SIZEOF_VOID_P == 8) && ((arg1->type == STACK_PTR) || (arg1->type == STACK_OBJ) || (arg1->type == STACK_MP))))
13016                                         cmp->opcode = OP_LCOMPARE;
13017                                 else if (arg1->type == STACK_R4)
13018                                         cmp->opcode = OP_RCOMPARE;
13019                                 else if (arg1->type == STACK_R8)
13020                                         cmp->opcode = OP_FCOMPARE;
13021                                 else
13022                                         cmp->opcode = OP_ICOMPARE;
13023                                 MONO_ADD_INS (cfg->cbb, cmp);
13024                                 ins->type = STACK_I4;
13025                                 ins->dreg = alloc_dreg (cfg, (MonoStackType)ins->type);
13026                                 type_from_op (cfg, ins, arg1, arg2);
13027
13028                                 if (cmp->opcode == OP_FCOMPARE || cmp->opcode == OP_RCOMPARE) {
13029                                         /*
13030                                          * The backends expect the fceq opcodes to do the
13031                                          * comparison too.
13032                                          */
13033                                         ins->sreg1 = cmp->sreg1;
13034                                         ins->sreg2 = cmp->sreg2;
13035                                         NULLIFY_INS (cmp);
13036                                 }
13037                                 MONO_ADD_INS (cfg->cbb, ins);
13038                                 *sp++ = ins;
13039                                 ip += 2;
13040                                 break;
13041                         }
13042                         case CEE_LDFTN: {
13043                                 MonoInst *argconst;
13044                                 MonoMethod *cil_method;
13045
13046                                 CHECK_STACK_OVF (1);
13047                                 CHECK_OPSIZE (6);
13048                                 n = read32 (ip + 2);
13049                                 cmethod = mini_get_method (cfg, method, n, NULL, generic_context);
13050                                 CHECK_CFG_ERROR;
13051
13052                                 mono_class_init (cmethod->klass);
13053
13054                                 mono_save_token_info (cfg, image, n, cmethod);
13055
13056                                 context_used = mini_method_check_context_used (cfg, cmethod);
13057
13058                                 cil_method = cmethod;
13059                                 if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_method (method, cmethod))
13060                                         METHOD_ACCESS_FAILURE (method, cil_method);
13061
13062                                 if (mono_security_core_clr_enabled ())
13063                                         ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
13064
13065                                 /* 
13066                                  * Optimize the common case of ldftn+delegate creation
13067                                  */
13068                                 if ((sp > stack_start) && (ip + 6 + 5 < end) && ip_in_bb (cfg, cfg->cbb, ip + 6) && (ip [6] == CEE_NEWOBJ)) {
13069                                         MonoMethod *ctor_method = mini_get_method (cfg, method, read32 (ip + 7), NULL, generic_context);
13070                                         if (ctor_method && (ctor_method->klass->parent == mono_defaults.multicastdelegate_class)) {
13071                                                 MonoInst *target_ins, *handle_ins;
13072                                                 MonoMethod *invoke;
13073                                                 int invoke_context_used;
13074
13075                                                 invoke = mono_get_delegate_invoke (ctor_method->klass);
13076                                                 if (!invoke || !mono_method_signature (invoke))
13077                                                         LOAD_ERROR;
13078
13079                                                 invoke_context_used = mini_method_check_context_used (cfg, invoke);
13080
13081                                                 target_ins = sp [-1];
13082
13083                                                 if (mono_security_core_clr_enabled ())
13084                                                         ensure_method_is_allowed_to_call_method (cfg, method, ctor_method);
13085
13086                                                 if (!(cmethod->flags & METHOD_ATTRIBUTE_STATIC)) {
13087                                                         /*LAME IMPL: We must not add a null check for virtual invoke delegates.*/
13088                                                         if (mono_method_signature (invoke)->param_count == mono_method_signature (cmethod)->param_count) {
13089                                                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, target_ins->dreg, 0);
13090                                                                 MONO_EMIT_NEW_COND_EXC (cfg, EQ, "ArgumentException");
13091                                                         }
13092                                                 }
13093
13094                                                 /* FIXME: SGEN support */
13095                                                 if (invoke_context_used == 0 || cfg->llvm_only) {
13096                                                         ip += 6;
13097                                                         if (cfg->verbose_level > 3)
13098                                                                 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));
13099                                                         if ((handle_ins = handle_delegate_ctor (cfg, ctor_method->klass, target_ins, cmethod, context_used, FALSE))) {
13100                                                                 sp --;
13101                                                                 *sp = handle_ins;
13102                                                                 CHECK_CFG_EXCEPTION;
13103                                                                 ip += 5;
13104                                                                 sp ++;
13105                                                                 break;
13106                                                         }
13107                                                         ip -= 6;
13108                                                 }
13109                                         }
13110                                 }
13111
13112                                 argconst = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_METHOD);
13113                                 ins = mono_emit_jit_icall (cfg, mono_ldftn, &argconst);
13114                                 *sp++ = ins;
13115                                 
13116                                 ip += 6;
13117                                 inline_costs += 10 * num_calls++;
13118                                 break;
13119                         }
13120                         case CEE_LDVIRTFTN: {
13121                                 MonoInst *args [2];
13122
13123                                 CHECK_STACK (1);
13124                                 CHECK_OPSIZE (6);
13125                                 n = read32 (ip + 2);
13126                                 cmethod = mini_get_method (cfg, method, n, NULL, generic_context);
13127                                 CHECK_CFG_ERROR;
13128
13129                                 mono_class_init (cmethod->klass);
13130  
13131                                 context_used = mini_method_check_context_used (cfg, cmethod);
13132
13133                                 if (mono_security_core_clr_enabled ())
13134                                         ensure_method_is_allowed_to_call_method (cfg, method, cmethod);
13135
13136                                 /*
13137                                  * Optimize the common case of ldvirtftn+delegate creation
13138                                  */
13139                                 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)) {
13140                                         MonoMethod *ctor_method = mini_get_method (cfg, method, read32 (ip + 7), NULL, generic_context);
13141                                         if (ctor_method && (ctor_method->klass->parent == mono_defaults.multicastdelegate_class)) {
13142                                                 MonoInst *target_ins, *handle_ins;
13143                                                 MonoMethod *invoke;
13144                                                 int invoke_context_used;
13145                                                 gboolean is_virtual = cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL;
13146
13147                                                 invoke = mono_get_delegate_invoke (ctor_method->klass);
13148                                                 if (!invoke || !mono_method_signature (invoke))
13149                                                         LOAD_ERROR;
13150
13151                                                 invoke_context_used = mini_method_check_context_used (cfg, invoke);
13152
13153                                                 target_ins = sp [-1];
13154
13155                                                 if (mono_security_core_clr_enabled ())
13156                                                         ensure_method_is_allowed_to_call_method (cfg, method, ctor_method);
13157
13158                                                 /* FIXME: SGEN support */
13159                                                 if (invoke_context_used == 0 || cfg->llvm_only) {
13160                                                         ip += 6;
13161                                                         if (cfg->verbose_level > 3)
13162                                                                 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));
13163                                                         if ((handle_ins = handle_delegate_ctor (cfg, ctor_method->klass, target_ins, cmethod, context_used, is_virtual))) {
13164                                                                 sp -= 2;
13165                                                                 *sp = handle_ins;
13166                                                                 CHECK_CFG_EXCEPTION;
13167                                                                 ip += 5;
13168                                                                 sp ++;
13169                                                                 break;
13170                                                         }
13171                                                         ip -= 6;
13172                                                 }
13173                                         }
13174                                 }
13175
13176                                 --sp;
13177                                 args [0] = *sp;
13178
13179                                 args [1] = emit_get_rgctx_method (cfg, context_used,
13180                                                                                                   cmethod, MONO_RGCTX_INFO_METHOD);
13181
13182                                 if (context_used)
13183                                         *sp++ = mono_emit_jit_icall (cfg, mono_ldvirtfn_gshared, args);
13184                                 else
13185                                         *sp++ = mono_emit_jit_icall (cfg, mono_ldvirtfn, args);
13186
13187                                 ip += 6;
13188                                 inline_costs += 10 * num_calls++;
13189                                 break;
13190                         }
13191                         case CEE_LDARG:
13192                                 CHECK_STACK_OVF (1);
13193                                 CHECK_OPSIZE (4);
13194                                 n = read16 (ip + 2);
13195                                 CHECK_ARG (n);
13196                                 EMIT_NEW_ARGLOAD (cfg, ins, n);
13197                                 *sp++ = ins;
13198                                 ip += 4;
13199                                 break;
13200                         case CEE_LDARGA:
13201                                 CHECK_STACK_OVF (1);
13202                                 CHECK_OPSIZE (4);
13203                                 n = read16 (ip + 2);
13204                                 CHECK_ARG (n);
13205                                 NEW_ARGLOADA (cfg, ins, n);
13206                                 MONO_ADD_INS (cfg->cbb, ins);
13207                                 *sp++ = ins;
13208                                 ip += 4;
13209                                 break;
13210                         case CEE_STARG:
13211                                 CHECK_STACK (1);
13212                                 --sp;
13213                                 CHECK_OPSIZE (4);
13214                                 n = read16 (ip + 2);
13215                                 CHECK_ARG (n);
13216                                 if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [n], *sp))
13217                                         UNVERIFIED;
13218                                 EMIT_NEW_ARGSTORE (cfg, ins, n, *sp);
13219                                 ip += 4;
13220                                 break;
13221                         case CEE_LDLOC:
13222                                 CHECK_STACK_OVF (1);
13223                                 CHECK_OPSIZE (4);
13224                                 n = read16 (ip + 2);
13225                                 CHECK_LOCAL (n);
13226                                 EMIT_NEW_LOCLOAD (cfg, ins, n);
13227                                 *sp++ = ins;
13228                                 ip += 4;
13229                                 break;
13230                         case CEE_LDLOCA: {
13231                                 unsigned char *tmp_ip;
13232                                 CHECK_STACK_OVF (1);
13233                                 CHECK_OPSIZE (4);
13234                                 n = read16 (ip + 2);
13235                                 CHECK_LOCAL (n);
13236
13237                                 if ((tmp_ip = emit_optimized_ldloca_ir (cfg, ip, end, 2))) {
13238                                         ip = tmp_ip;
13239                                         inline_costs += 1;
13240                                         break;
13241                                 }                       
13242                                 
13243                                 EMIT_NEW_LOCLOADA (cfg, ins, n);
13244                                 *sp++ = ins;
13245                                 ip += 4;
13246                                 break;
13247                         }
13248                         case CEE_STLOC:
13249                                 CHECK_STACK (1);
13250                                 --sp;
13251                                 CHECK_OPSIZE (4);
13252                                 n = read16 (ip + 2);
13253                                 CHECK_LOCAL (n);
13254                                 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
13255                                         UNVERIFIED;
13256                                 emit_stloc_ir (cfg, sp, header, n);
13257                                 ip += 4;
13258                                 inline_costs += 1;
13259                                 break;
13260                         case CEE_LOCALLOC:
13261                                 CHECK_STACK (1);
13262                                 --sp;
13263                                 if (sp != stack_start) 
13264                                         UNVERIFIED;
13265                                 if (cfg->method != method) 
13266                                         /* 
13267                                          * Inlining this into a loop in a parent could lead to 
13268                                          * stack overflows which is different behavior than the
13269                                          * non-inlined case, thus disable inlining in this case.
13270                                          */
13271                                         INLINE_FAILURE("localloc");
13272
13273                                 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
13274                                 ins->dreg = alloc_preg (cfg);
13275                                 ins->sreg1 = sp [0]->dreg;
13276                                 ins->type = STACK_PTR;
13277                                 MONO_ADD_INS (cfg->cbb, ins);
13278
13279                                 cfg->flags |= MONO_CFG_HAS_ALLOCA;
13280                                 if (init_locals)
13281                                         ins->flags |= MONO_INST_INIT;
13282
13283                                 *sp++ = ins;
13284                                 ip += 2;
13285                                 break;
13286                         case CEE_ENDFILTER: {
13287                                 MonoExceptionClause *clause, *nearest;
13288                                 int cc;
13289
13290                                 CHECK_STACK (1);
13291                                 --sp;
13292                                 if ((sp != stack_start) || (sp [0]->type != STACK_I4)) 
13293                                         UNVERIFIED;
13294                                 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
13295                                 ins->sreg1 = (*sp)->dreg;
13296                                 MONO_ADD_INS (cfg->cbb, ins);
13297                                 start_new_bblock = 1;
13298                                 ip += 2;
13299
13300                                 nearest = NULL;
13301                                 for (cc = 0; cc < header->num_clauses; ++cc) {
13302                                         clause = &header->clauses [cc];
13303                                         if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
13304                                                 ((ip - header->code) > clause->data.filter_offset && (ip - header->code) <= clause->handler_offset) &&
13305                                             (!nearest || (clause->data.filter_offset < nearest->data.filter_offset)))
13306                                                 nearest = clause;
13307                                 }
13308                                 g_assert (nearest);
13309                                 if ((ip - header->code) != nearest->handler_offset)
13310                                         UNVERIFIED;
13311
13312                                 break;
13313                         }
13314                         case CEE_UNALIGNED_:
13315                                 ins_flag |= MONO_INST_UNALIGNED;
13316                                 /* FIXME: record alignment? we can assume 1 for now */
13317                                 CHECK_OPSIZE (3);
13318                                 ip += 3;
13319                                 break;
13320                         case CEE_VOLATILE_:
13321                                 ins_flag |= MONO_INST_VOLATILE;
13322                                 ip += 2;
13323                                 break;
13324                         case CEE_TAIL_:
13325                                 ins_flag   |= MONO_INST_TAILCALL;
13326                                 cfg->flags |= MONO_CFG_HAS_TAIL;
13327                                 /* Can't inline tail calls at this time */
13328                                 inline_costs += 100000;
13329                                 ip += 2;
13330                                 break;
13331                         case CEE_INITOBJ:
13332                                 CHECK_STACK (1);
13333                                 --sp;
13334                                 CHECK_OPSIZE (6);
13335                                 token = read32 (ip + 2);
13336                                 klass = mini_get_class (method, token, generic_context);
13337                                 CHECK_TYPELOAD (klass);
13338                                 if (generic_class_is_reference_type (cfg, klass))
13339                                         MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, sp [0]->dreg, 0, 0);
13340                                 else
13341                                         mini_emit_initobj (cfg, *sp, NULL, klass);
13342                                 ip += 6;
13343                                 inline_costs += 1;
13344                                 break;
13345                         case CEE_CONSTRAINED_:
13346                                 CHECK_OPSIZE (6);
13347                                 token = read32 (ip + 2);
13348                                 constrained_class = mini_get_class (method, token, generic_context);
13349                                 CHECK_TYPELOAD (constrained_class);
13350                                 ip += 6;
13351                                 break;
13352                         case CEE_CPBLK:
13353                         case CEE_INITBLK: {
13354                                 MonoInst *iargs [3];
13355                                 CHECK_STACK (3);
13356                                 sp -= 3;
13357
13358                                 /* Skip optimized paths for volatile operations. */
13359                                 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)) {
13360                                         mini_emit_memcpy (cfg, sp [0]->dreg, 0, sp [1]->dreg, 0, sp [2]->inst_c0, 0);
13361                                 } 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)) {
13362                                         /* emit_memset only works when val == 0 */
13363                                         mini_emit_memset (cfg, sp [0]->dreg, 0, sp [2]->inst_c0, sp [1]->inst_c0, 0);
13364                                 } else {
13365                                         MonoInst *call;
13366                                         iargs [0] = sp [0];
13367                                         iargs [1] = sp [1];
13368                                         iargs [2] = sp [2];
13369                                         if (ip [1] == CEE_CPBLK) {
13370                                                 /*
13371                                                  * FIXME: It's unclear whether we should be emitting both the acquire
13372                                                  * and release barriers for cpblk. It is technically both a load and
13373                                                  * store operation, so it seems like that's the sensible thing to do.
13374                                                  *
13375                                                  * FIXME: We emit full barriers on both sides of the operation for
13376                                                  * simplicity. We should have a separate atomic memcpy method instead.
13377                                                  */
13378                                                 MonoMethod *memcpy_method = get_memcpy_method ();
13379
13380                                                 if (ins_flag & MONO_INST_VOLATILE)
13381                                                         emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
13382
13383                                                 call = mono_emit_method_call (cfg, memcpy_method, iargs, NULL);
13384                                                 call->flags |= ins_flag;
13385
13386                                                 if (ins_flag & MONO_INST_VOLATILE)
13387                                                         emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_SEQ);
13388                                         } else {
13389                                                 MonoMethod *memset_method = get_memset_method ();
13390                                                 if (ins_flag & MONO_INST_VOLATILE) {
13391                                                         /* Volatile stores have release semantics, see 12.6.7 in Ecma 335 */
13392                                                         emit_memory_barrier (cfg, MONO_MEMORY_BARRIER_REL);
13393                                                 }
13394                                                 call = mono_emit_method_call (cfg, memset_method, iargs, NULL);
13395                                                 call->flags |= ins_flag;
13396                                         }
13397                                 }
13398                                 ip += 2;
13399                                 ins_flag = 0;
13400                                 inline_costs += 1;
13401                                 break;
13402                         }
13403                         case CEE_NO_:
13404                                 CHECK_OPSIZE (3);
13405                                 if (ip [2] & 0x1)
13406                                         ins_flag |= MONO_INST_NOTYPECHECK;
13407                                 if (ip [2] & 0x2)
13408                                         ins_flag |= MONO_INST_NORANGECHECK;
13409                                 /* we ignore the no-nullcheck for now since we
13410                                  * really do it explicitly only when doing callvirt->call
13411                                  */
13412                                 ip += 3;
13413                                 break;
13414                         case CEE_RETHROW: {
13415                                 MonoInst *load;
13416                                 int handler_offset = -1;
13417
13418                                 for (i = 0; i < header->num_clauses; ++i) {
13419                                         MonoExceptionClause *clause = &header->clauses [i];
13420                                         if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code) && !(clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
13421                                                 handler_offset = clause->handler_offset;
13422                                                 break;
13423                                         }
13424                                 }
13425
13426                                 cfg->cbb->flags |= BB_EXCEPTION_UNSAFE;
13427
13428                                 if (handler_offset == -1)
13429                                         UNVERIFIED;
13430
13431                                 EMIT_NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, handler_offset)->inst_c0);
13432                                 MONO_INST_NEW (cfg, ins, OP_RETHROW);
13433                                 ins->sreg1 = load->dreg;
13434                                 MONO_ADD_INS (cfg->cbb, ins);
13435
13436                                 MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
13437                                 MONO_ADD_INS (cfg->cbb, ins);
13438
13439                                 sp = stack_start;
13440                                 link_bblock (cfg, cfg->cbb, end_bblock);
13441                                 start_new_bblock = 1;
13442                                 ip += 2;
13443                                 break;
13444                         }
13445                         case CEE_SIZEOF: {
13446                                 guint32 val;
13447                                 int ialign;
13448
13449                                 CHECK_STACK_OVF (1);
13450                                 CHECK_OPSIZE (6);
13451                                 token = read32 (ip + 2);
13452                                 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC && !image_is_dynamic (method->klass->image) && !generic_context) {
13453                                         MonoType *type = mono_type_create_from_typespec_checked (image, token, &cfg->error);
13454                                         CHECK_CFG_ERROR;
13455
13456                                         val = mono_type_size (type, &ialign);
13457                                 } else {
13458                                         MonoClass *klass = mini_get_class (method, token, generic_context);
13459                                         CHECK_TYPELOAD (klass);
13460
13461                                         val = mono_type_size (&klass->byval_arg, &ialign);
13462
13463                                         if (mini_is_gsharedvt_klass (klass))
13464                                                 GSHAREDVT_FAILURE (*ip);
13465                                 }
13466                                 EMIT_NEW_ICONST (cfg, ins, val);
13467                                 *sp++= ins;
13468                                 ip += 6;
13469                                 break;
13470                         }
13471                         case CEE_REFANYTYPE: {
13472                                 MonoInst *src_var, *src;
13473
13474                                 GSHAREDVT_FAILURE (*ip);
13475
13476                                 CHECK_STACK (1);
13477                                 --sp;
13478
13479                                 // FIXME:
13480                                 src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
13481                                 if (!src_var)
13482                                         src_var = mono_compile_create_var_for_vreg (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL, sp [0]->dreg);
13483                                 EMIT_NEW_VARLOADA (cfg, src, src_var, src_var->inst_vtype);
13484                                 EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &mono_defaults.typehandle_class->byval_arg, src->dreg, MONO_STRUCT_OFFSET (MonoTypedRef, type));
13485                                 *sp++ = ins;
13486                                 ip += 2;
13487                                 break;
13488                         }
13489                         case CEE_READONLY_:
13490                                 readonly = TRUE;
13491                                 ip += 2;
13492                                 break;
13493
13494                         case CEE_UNUSED56:
13495                         case CEE_UNUSED57:
13496                         case CEE_UNUSED70:
13497                         case CEE_UNUSED:
13498                         case CEE_UNUSED99:
13499                                 UNVERIFIED;
13500                                 
13501                         default:
13502                                 g_warning ("opcode 0xfe 0x%02x not handled", ip [1]);
13503                                 UNVERIFIED;
13504                         }
13505                         break;
13506                 }
13507                 case CEE_UNUSED58:
13508                 case CEE_UNUSED1:
13509                         UNVERIFIED;
13510
13511                 default:
13512                         g_warning ("opcode 0x%02x not handled", *ip);
13513                         UNVERIFIED;
13514                 }
13515         }
13516         if (start_new_bblock != 1)
13517                 UNVERIFIED;
13518
13519         cfg->cbb->cil_length = ip - cfg->cbb->cil_code;
13520         if (cfg->cbb->next_bb) {
13521                 /* This could already be set because of inlining, #693905 */
13522                 MonoBasicBlock *bb = cfg->cbb;
13523
13524                 while (bb->next_bb)
13525                         bb = bb->next_bb;
13526                 bb->next_bb = end_bblock;
13527         } else {
13528                 cfg->cbb->next_bb = end_bblock;
13529         }
13530
13531         if (cfg->method == method && cfg->domainvar) {
13532                 MonoInst *store;
13533                 MonoInst *get_domain;
13534
13535                 cfg->cbb = init_localsbb;
13536
13537                 if ((get_domain = mono_get_domain_intrinsic (cfg))) {
13538                         MONO_ADD_INS (cfg->cbb, get_domain);
13539                 } else {
13540                         get_domain = mono_emit_jit_icall (cfg, mono_domain_get, NULL);
13541                 }
13542                 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, get_domain);
13543                 MONO_ADD_INS (cfg->cbb, store);
13544         }
13545
13546 #if defined(TARGET_POWERPC) || defined(TARGET_X86)
13547         if (cfg->compile_aot)
13548                 /* FIXME: The plt slots require a GOT var even if the method doesn't use it */
13549                 mono_get_got_var (cfg);
13550 #endif
13551
13552         if (cfg->method == method && cfg->got_var)
13553                 mono_emit_load_got_addr (cfg);
13554
13555         if (init_localsbb) {
13556                 cfg->cbb = init_localsbb;
13557                 cfg->ip = NULL;
13558                 for (i = 0; i < header->num_locals; ++i) {
13559                         emit_init_local (cfg, i, header->locals [i], init_locals);
13560                 }
13561         }
13562
13563         if (cfg->init_ref_vars && cfg->method == method) {
13564                 /* Emit initialization for ref vars */
13565                 // FIXME: Avoid duplication initialization for IL locals.
13566                 for (i = 0; i < cfg->num_varinfo; ++i) {
13567                         MonoInst *ins = cfg->varinfo [i];
13568
13569                         if (ins->opcode == OP_LOCAL && ins->type == STACK_OBJ)
13570                                 MONO_EMIT_NEW_PCONST (cfg, ins->dreg, NULL);
13571                 }
13572         }
13573
13574         if (cfg->lmf_var && cfg->method == method && !cfg->llvm_only) {
13575                 cfg->cbb = init_localsbb;
13576                 emit_push_lmf (cfg);
13577         }
13578
13579         cfg->cbb = init_localsbb;
13580         emit_instrumentation_call (cfg, mono_profiler_method_enter);
13581
13582         if (seq_points) {
13583                 MonoBasicBlock *bb;
13584
13585                 /*
13586                  * Make seq points at backward branch targets interruptable.
13587                  */
13588                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
13589                         if (bb->code && bb->in_count > 1 && bb->code->opcode == OP_SEQ_POINT)
13590                                 bb->code->flags |= MONO_INST_SINGLE_STEP_LOC;
13591         }
13592
13593         /* Add a sequence point for method entry/exit events */
13594         if (seq_points && cfg->gen_sdb_seq_points) {
13595                 NEW_SEQ_POINT (cfg, ins, METHOD_ENTRY_IL_OFFSET, FALSE);
13596                 MONO_ADD_INS (init_localsbb, ins);
13597                 NEW_SEQ_POINT (cfg, ins, METHOD_EXIT_IL_OFFSET, FALSE);
13598                 MONO_ADD_INS (cfg->bb_exit, ins);
13599         }
13600
13601         /*
13602          * Add seq points for IL offsets which have line number info, but wasn't generated a seq point during JITting because
13603          * the code they refer to was dead (#11880).
13604          */
13605         if (sym_seq_points) {
13606                 for (i = 0; i < header->code_size; ++i) {
13607                         if (mono_bitset_test_fast (seq_point_locs, i) && !mono_bitset_test_fast (seq_point_set_locs, i)) {
13608                                 MonoInst *ins;
13609
13610                                 NEW_SEQ_POINT (cfg, ins, i, FALSE);
13611                                 mono_add_seq_point (cfg, NULL, ins, SEQ_POINT_NATIVE_OFFSET_DEAD_CODE);
13612                         }
13613                 }
13614         }
13615
13616         cfg->ip = NULL;
13617
13618         if (cfg->method == method) {
13619                 MonoBasicBlock *bb;
13620                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
13621                         bb->region = mono_find_block_region (cfg, bb->real_offset);
13622                         if (cfg->spvars)
13623                                 mono_create_spvar_for_region (cfg, bb->region);
13624                         if (cfg->verbose_level > 2)
13625                                 printf ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
13626                 }
13627         }
13628
13629         if (inline_costs < 0) {
13630                 char *mname;
13631
13632                 /* Method is too large */
13633                 mname = mono_method_full_name (method, TRUE);
13634                 mono_cfg_set_exception_invalid_program (cfg, g_strdup_printf ("Method %s is too complex.", mname));
13635                 g_free (mname);
13636         }
13637
13638         if ((cfg->verbose_level > 2) && (cfg->method == method)) 
13639                 mono_print_code (cfg, "AFTER METHOD-TO-IR");
13640
13641         goto cleanup;
13642
13643 mono_error_exit:
13644         g_assert (!mono_error_ok (&cfg->error));
13645         goto cleanup;
13646  
13647  exception_exit:
13648         g_assert (cfg->exception_type != MONO_EXCEPTION_NONE);
13649         goto cleanup;
13650
13651  unverified:
13652         set_exception_type_from_invalid_il (cfg, method, ip);
13653         goto cleanup;
13654
13655  cleanup:
13656         g_slist_free (class_inits);
13657         mono_basic_block_free (original_bb);
13658         cfg->dont_inline = g_list_remove (cfg->dont_inline, method);
13659         cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, header);
13660         if (cfg->exception_type)
13661                 return -1;
13662         else
13663                 return inline_costs;
13664 }
13665
13666 static int
13667 store_membase_reg_to_store_membase_imm (int opcode)
13668 {
13669         switch (opcode) {
13670         case OP_STORE_MEMBASE_REG:
13671                 return OP_STORE_MEMBASE_IMM;
13672         case OP_STOREI1_MEMBASE_REG:
13673                 return OP_STOREI1_MEMBASE_IMM;
13674         case OP_STOREI2_MEMBASE_REG:
13675                 return OP_STOREI2_MEMBASE_IMM;
13676         case OP_STOREI4_MEMBASE_REG:
13677                 return OP_STOREI4_MEMBASE_IMM;
13678         case OP_STOREI8_MEMBASE_REG:
13679                 return OP_STOREI8_MEMBASE_IMM;
13680         default:
13681                 g_assert_not_reached ();
13682         }
13683
13684         return -1;
13685 }               
13686
13687 int
13688 mono_op_to_op_imm (int opcode)
13689 {
13690         switch (opcode) {
13691         case OP_IADD:
13692                 return OP_IADD_IMM;
13693         case OP_ISUB:
13694                 return OP_ISUB_IMM;
13695         case OP_IDIV:
13696                 return OP_IDIV_IMM;
13697         case OP_IDIV_UN:
13698                 return OP_IDIV_UN_IMM;
13699         case OP_IREM:
13700                 return OP_IREM_IMM;
13701         case OP_IREM_UN:
13702                 return OP_IREM_UN_IMM;
13703         case OP_IMUL:
13704                 return OP_IMUL_IMM;
13705         case OP_IAND:
13706                 return OP_IAND_IMM;
13707         case OP_IOR:
13708                 return OP_IOR_IMM;
13709         case OP_IXOR:
13710                 return OP_IXOR_IMM;
13711         case OP_ISHL:
13712                 return OP_ISHL_IMM;
13713         case OP_ISHR:
13714                 return OP_ISHR_IMM;
13715         case OP_ISHR_UN:
13716                 return OP_ISHR_UN_IMM;
13717
13718         case OP_LADD:
13719                 return OP_LADD_IMM;
13720         case OP_LSUB:
13721                 return OP_LSUB_IMM;
13722         case OP_LAND:
13723                 return OP_LAND_IMM;
13724         case OP_LOR:
13725                 return OP_LOR_IMM;
13726         case OP_LXOR:
13727                 return OP_LXOR_IMM;
13728         case OP_LSHL:
13729                 return OP_LSHL_IMM;
13730         case OP_LSHR:
13731                 return OP_LSHR_IMM;
13732         case OP_LSHR_UN:
13733                 return OP_LSHR_UN_IMM;
13734 #if SIZEOF_REGISTER == 8
13735         case OP_LREM:
13736                 return OP_LREM_IMM;
13737 #endif
13738
13739         case OP_COMPARE:
13740                 return OP_COMPARE_IMM;
13741         case OP_ICOMPARE:
13742                 return OP_ICOMPARE_IMM;
13743         case OP_LCOMPARE:
13744                 return OP_LCOMPARE_IMM;
13745
13746         case OP_STORE_MEMBASE_REG:
13747                 return OP_STORE_MEMBASE_IMM;
13748         case OP_STOREI1_MEMBASE_REG:
13749                 return OP_STOREI1_MEMBASE_IMM;
13750         case OP_STOREI2_MEMBASE_REG:
13751                 return OP_STOREI2_MEMBASE_IMM;
13752         case OP_STOREI4_MEMBASE_REG:
13753                 return OP_STOREI4_MEMBASE_IMM;
13754
13755 #if defined(TARGET_X86) || defined (TARGET_AMD64)
13756         case OP_X86_PUSH:
13757                 return OP_X86_PUSH_IMM;
13758         case OP_X86_COMPARE_MEMBASE_REG:
13759                 return OP_X86_COMPARE_MEMBASE_IMM;
13760 #endif
13761 #if defined(TARGET_AMD64)
13762         case OP_AMD64_ICOMPARE_MEMBASE_REG:
13763                 return OP_AMD64_ICOMPARE_MEMBASE_IMM;
13764 #endif
13765         case OP_VOIDCALL_REG:
13766                 return OP_VOIDCALL;
13767         case OP_CALL_REG:
13768                 return OP_CALL;
13769         case OP_LCALL_REG:
13770                 return OP_LCALL;
13771         case OP_FCALL_REG:
13772                 return OP_FCALL;
13773         case OP_LOCALLOC:
13774                 return OP_LOCALLOC_IMM;
13775         }
13776
13777         return -1;
13778 }
13779
13780 static int
13781 ldind_to_load_membase (int opcode)
13782 {
13783         switch (opcode) {
13784         case CEE_LDIND_I1:
13785                 return OP_LOADI1_MEMBASE;
13786         case CEE_LDIND_U1:
13787                 return OP_LOADU1_MEMBASE;
13788         case CEE_LDIND_I2:
13789                 return OP_LOADI2_MEMBASE;
13790         case CEE_LDIND_U2:
13791                 return OP_LOADU2_MEMBASE;
13792         case CEE_LDIND_I4:
13793                 return OP_LOADI4_MEMBASE;
13794         case CEE_LDIND_U4:
13795                 return OP_LOADU4_MEMBASE;
13796         case CEE_LDIND_I:
13797                 return OP_LOAD_MEMBASE;
13798         case CEE_LDIND_REF:
13799                 return OP_LOAD_MEMBASE;
13800         case CEE_LDIND_I8:
13801                 return OP_LOADI8_MEMBASE;
13802         case CEE_LDIND_R4:
13803                 return OP_LOADR4_MEMBASE;
13804         case CEE_LDIND_R8:
13805                 return OP_LOADR8_MEMBASE;
13806         default:
13807                 g_assert_not_reached ();
13808         }
13809
13810         return -1;
13811 }
13812
13813 static int
13814 stind_to_store_membase (int opcode)
13815 {
13816         switch (opcode) {
13817         case CEE_STIND_I1:
13818                 return OP_STOREI1_MEMBASE_REG;
13819         case CEE_STIND_I2:
13820                 return OP_STOREI2_MEMBASE_REG;
13821         case CEE_STIND_I4:
13822                 return OP_STOREI4_MEMBASE_REG;
13823         case CEE_STIND_I:
13824         case CEE_STIND_REF:
13825                 return OP_STORE_MEMBASE_REG;
13826         case CEE_STIND_I8:
13827                 return OP_STOREI8_MEMBASE_REG;
13828         case CEE_STIND_R4:
13829                 return OP_STORER4_MEMBASE_REG;
13830         case CEE_STIND_R8:
13831                 return OP_STORER8_MEMBASE_REG;
13832         default:
13833                 g_assert_not_reached ();
13834         }
13835
13836         return -1;
13837 }
13838
13839 int
13840 mono_load_membase_to_load_mem (int opcode)
13841 {
13842         // FIXME: Add a MONO_ARCH_HAVE_LOAD_MEM macro
13843 #if defined(TARGET_X86) || defined(TARGET_AMD64)
13844         switch (opcode) {
13845         case OP_LOAD_MEMBASE:
13846                 return OP_LOAD_MEM;
13847         case OP_LOADU1_MEMBASE:
13848                 return OP_LOADU1_MEM;
13849         case OP_LOADU2_MEMBASE:
13850                 return OP_LOADU2_MEM;
13851         case OP_LOADI4_MEMBASE:
13852                 return OP_LOADI4_MEM;
13853         case OP_LOADU4_MEMBASE:
13854                 return OP_LOADU4_MEM;
13855 #if SIZEOF_REGISTER == 8
13856         case OP_LOADI8_MEMBASE:
13857                 return OP_LOADI8_MEM;
13858 #endif
13859         }
13860 #endif
13861
13862         return -1;
13863 }
13864
13865 static inline int
13866 op_to_op_dest_membase (int store_opcode, int opcode)
13867 {
13868 #if defined(TARGET_X86)
13869         if (!((store_opcode == OP_STORE_MEMBASE_REG) || (store_opcode == OP_STOREI4_MEMBASE_REG)))
13870                 return -1;
13871
13872         switch (opcode) {
13873         case OP_IADD:
13874                 return OP_X86_ADD_MEMBASE_REG;
13875         case OP_ISUB:
13876                 return OP_X86_SUB_MEMBASE_REG;
13877         case OP_IAND:
13878                 return OP_X86_AND_MEMBASE_REG;
13879         case OP_IOR:
13880                 return OP_X86_OR_MEMBASE_REG;
13881         case OP_IXOR:
13882                 return OP_X86_XOR_MEMBASE_REG;
13883         case OP_ADD_IMM:
13884         case OP_IADD_IMM:
13885                 return OP_X86_ADD_MEMBASE_IMM;
13886         case OP_SUB_IMM:
13887         case OP_ISUB_IMM:
13888                 return OP_X86_SUB_MEMBASE_IMM;
13889         case OP_AND_IMM:
13890         case OP_IAND_IMM:
13891                 return OP_X86_AND_MEMBASE_IMM;
13892         case OP_OR_IMM:
13893         case OP_IOR_IMM:
13894                 return OP_X86_OR_MEMBASE_IMM;
13895         case OP_XOR_IMM:
13896         case OP_IXOR_IMM:
13897                 return OP_X86_XOR_MEMBASE_IMM;
13898         case OP_MOVE:
13899                 return OP_NOP;
13900         }
13901 #endif
13902
13903 #if defined(TARGET_AMD64)
13904         if (!((store_opcode == OP_STORE_MEMBASE_REG) || (store_opcode == OP_STOREI4_MEMBASE_REG) || (store_opcode == OP_STOREI8_MEMBASE_REG)))
13905                 return -1;
13906
13907         switch (opcode) {
13908         case OP_IADD:
13909                 return OP_X86_ADD_MEMBASE_REG;
13910         case OP_ISUB:
13911                 return OP_X86_SUB_MEMBASE_REG;
13912         case OP_IAND:
13913                 return OP_X86_AND_MEMBASE_REG;
13914         case OP_IOR:
13915                 return OP_X86_OR_MEMBASE_REG;
13916         case OP_IXOR:
13917                 return OP_X86_XOR_MEMBASE_REG;
13918         case OP_IADD_IMM:
13919                 return OP_X86_ADD_MEMBASE_IMM;
13920         case OP_ISUB_IMM:
13921                 return OP_X86_SUB_MEMBASE_IMM;
13922         case OP_IAND_IMM:
13923                 return OP_X86_AND_MEMBASE_IMM;
13924         case OP_IOR_IMM:
13925                 return OP_X86_OR_MEMBASE_IMM;
13926         case OP_IXOR_IMM:
13927                 return OP_X86_XOR_MEMBASE_IMM;
13928         case OP_LADD:
13929                 return OP_AMD64_ADD_MEMBASE_REG;
13930         case OP_LSUB:
13931                 return OP_AMD64_SUB_MEMBASE_REG;
13932         case OP_LAND:
13933                 return OP_AMD64_AND_MEMBASE_REG;
13934         case OP_LOR:
13935                 return OP_AMD64_OR_MEMBASE_REG;
13936         case OP_LXOR:
13937                 return OP_AMD64_XOR_MEMBASE_REG;
13938         case OP_ADD_IMM:
13939         case OP_LADD_IMM:
13940                 return OP_AMD64_ADD_MEMBASE_IMM;
13941         case OP_SUB_IMM:
13942         case OP_LSUB_IMM:
13943                 return OP_AMD64_SUB_MEMBASE_IMM;
13944         case OP_AND_IMM:
13945         case OP_LAND_IMM:
13946                 return OP_AMD64_AND_MEMBASE_IMM;
13947         case OP_OR_IMM:
13948         case OP_LOR_IMM:
13949                 return OP_AMD64_OR_MEMBASE_IMM;
13950         case OP_XOR_IMM:
13951         case OP_LXOR_IMM:
13952                 return OP_AMD64_XOR_MEMBASE_IMM;
13953         case OP_MOVE:
13954                 return OP_NOP;
13955         }
13956 #endif
13957
13958         return -1;
13959 }
13960
13961 static inline int
13962 op_to_op_store_membase (int store_opcode, int opcode)
13963 {
13964 #if defined(TARGET_X86) || defined(TARGET_AMD64)
13965         switch (opcode) {
13966         case OP_ICEQ:
13967                 if (store_opcode == OP_STOREI1_MEMBASE_REG)
13968                         return OP_X86_SETEQ_MEMBASE;
13969         case OP_CNE:
13970                 if (store_opcode == OP_STOREI1_MEMBASE_REG)
13971                         return OP_X86_SETNE_MEMBASE;
13972         }
13973 #endif
13974
13975         return -1;
13976 }
13977
13978 static inline int
13979 op_to_op_src1_membase (MonoCompile *cfg, int load_opcode, int opcode)
13980 {
13981 #ifdef TARGET_X86
13982         /* FIXME: This has sign extension issues */
13983         /*
13984         if ((opcode == OP_ICOMPARE_IMM) && (load_opcode == OP_LOADU1_MEMBASE))
13985                 return OP_X86_COMPARE_MEMBASE8_IMM;
13986         */
13987
13988         if (!((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE)))
13989                 return -1;
13990
13991         switch (opcode) {
13992         case OP_X86_PUSH:
13993                 return OP_X86_PUSH_MEMBASE;
13994         case OP_COMPARE_IMM:
13995         case OP_ICOMPARE_IMM:
13996                 return OP_X86_COMPARE_MEMBASE_IMM;
13997         case OP_COMPARE:
13998         case OP_ICOMPARE:
13999                 return OP_X86_COMPARE_MEMBASE_REG;
14000         }
14001 #endif
14002
14003 #ifdef TARGET_AMD64
14004         /* FIXME: This has sign extension issues */
14005         /*
14006         if ((opcode == OP_ICOMPARE_IMM) && (load_opcode == OP_LOADU1_MEMBASE))
14007                 return OP_X86_COMPARE_MEMBASE8_IMM;
14008         */
14009
14010         switch (opcode) {
14011         case OP_X86_PUSH:
14012                 if ((load_opcode == OP_LOAD_MEMBASE && !cfg->backend->ilp32) || (load_opcode == OP_LOADI8_MEMBASE))
14013                         return OP_X86_PUSH_MEMBASE;
14014                 break;
14015                 /* FIXME: This only works for 32 bit immediates
14016         case OP_COMPARE_IMM:
14017         case OP_LCOMPARE_IMM:
14018                 if ((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI8_MEMBASE))
14019                         return OP_AMD64_COMPARE_MEMBASE_IMM;
14020                 */
14021         case OP_ICOMPARE_IMM:
14022                 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE))
14023                         return OP_AMD64_ICOMPARE_MEMBASE_IMM;
14024                 break;
14025         case OP_COMPARE:
14026         case OP_LCOMPARE:
14027                 if (cfg->backend->ilp32 && load_opcode == OP_LOAD_MEMBASE)
14028                         return OP_AMD64_ICOMPARE_MEMBASE_REG;
14029                 if ((load_opcode == OP_LOAD_MEMBASE && !cfg->backend->ilp32) || (load_opcode == OP_LOADI8_MEMBASE))
14030                         return OP_AMD64_COMPARE_MEMBASE_REG;
14031                 break;
14032         case OP_ICOMPARE:
14033                 if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE))
14034                         return OP_AMD64_ICOMPARE_MEMBASE_REG;
14035                 break;
14036         }
14037 #endif
14038
14039         return -1;
14040 }
14041
14042 static inline int
14043 op_to_op_src2_membase (MonoCompile *cfg, int load_opcode, int opcode)
14044 {
14045 #ifdef TARGET_X86
14046         if (!((load_opcode == OP_LOAD_MEMBASE) || (load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE)))
14047                 return -1;
14048         
14049         switch (opcode) {
14050         case OP_COMPARE:
14051         case OP_ICOMPARE:
14052                 return OP_X86_COMPARE_REG_MEMBASE;
14053         case OP_IADD:
14054                 return OP_X86_ADD_REG_MEMBASE;
14055         case OP_ISUB:
14056                 return OP_X86_SUB_REG_MEMBASE;
14057         case OP_IAND:
14058                 return OP_X86_AND_REG_MEMBASE;
14059         case OP_IOR:
14060                 return OP_X86_OR_REG_MEMBASE;
14061         case OP_IXOR:
14062                 return OP_X86_XOR_REG_MEMBASE;
14063         }
14064 #endif
14065
14066 #ifdef TARGET_AMD64
14067         if ((load_opcode == OP_LOADI4_MEMBASE) || (load_opcode == OP_LOADU4_MEMBASE) || (load_opcode == OP_LOAD_MEMBASE && cfg->backend->ilp32)) {
14068                 switch (opcode) {
14069                 case OP_ICOMPARE:
14070                         return OP_AMD64_ICOMPARE_REG_MEMBASE;
14071                 case OP_IADD:
14072                         return OP_X86_ADD_REG_MEMBASE;
14073                 case OP_ISUB:
14074                         return OP_X86_SUB_REG_MEMBASE;
14075                 case OP_IAND:
14076                         return OP_X86_AND_REG_MEMBASE;
14077                 case OP_IOR:
14078                         return OP_X86_OR_REG_MEMBASE;
14079                 case OP_IXOR:
14080                         return OP_X86_XOR_REG_MEMBASE;
14081                 }
14082         } else if ((load_opcode == OP_LOADI8_MEMBASE) || (load_opcode == OP_LOAD_MEMBASE && !cfg->backend->ilp32)) {
14083                 switch (opcode) {
14084                 case OP_COMPARE:
14085                 case OP_LCOMPARE:
14086                         return OP_AMD64_COMPARE_REG_MEMBASE;
14087                 case OP_LADD:
14088                         return OP_AMD64_ADD_REG_MEMBASE;
14089                 case OP_LSUB:
14090                         return OP_AMD64_SUB_REG_MEMBASE;
14091                 case OP_LAND:
14092                         return OP_AMD64_AND_REG_MEMBASE;
14093                 case OP_LOR:
14094                         return OP_AMD64_OR_REG_MEMBASE;
14095                 case OP_LXOR:
14096                         return OP_AMD64_XOR_REG_MEMBASE;
14097                 }
14098         }
14099 #endif
14100
14101         return -1;
14102 }
14103
14104 int
14105 mono_op_to_op_imm_noemul (int opcode)
14106 {
14107         switch (opcode) {
14108 #if SIZEOF_REGISTER == 4 && !defined(MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS)
14109         case OP_LSHR:
14110         case OP_LSHL:
14111         case OP_LSHR_UN:
14112                 return -1;
14113 #endif
14114 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
14115         case OP_IDIV:
14116         case OP_IDIV_UN:
14117         case OP_IREM:
14118         case OP_IREM_UN:
14119                 return -1;
14120 #endif
14121 #if defined(MONO_ARCH_EMULATE_MUL_DIV)
14122         case OP_IMUL:
14123                 return -1;
14124 #endif
14125         default:
14126                 return mono_op_to_op_imm (opcode);
14127         }
14128 }
14129
14130 /**
14131  * mono_handle_global_vregs:
14132  *
14133  *   Make vregs used in more than one bblock 'global', i.e. allocate a variable
14134  * for them.
14135  */
14136 void
14137 mono_handle_global_vregs (MonoCompile *cfg)
14138 {
14139         gint32 *vreg_to_bb;
14140         MonoBasicBlock *bb;
14141         int i, pos;
14142
14143         vreg_to_bb = (gint32 *)mono_mempool_alloc0 (cfg->mempool, sizeof (gint32*) * cfg->next_vreg + 1);
14144
14145 #ifdef MONO_ARCH_SIMD_INTRINSICS
14146         if (cfg->uses_simd_intrinsics)
14147                 mono_simd_simplify_indirection (cfg);
14148 #endif
14149
14150         /* Find local vregs used in more than one bb */
14151         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
14152                 MonoInst *ins = bb->code;       
14153                 int block_num = bb->block_num;
14154
14155                 if (cfg->verbose_level > 2)
14156                         printf ("\nHANDLE-GLOBAL-VREGS BLOCK %d:\n", bb->block_num);
14157
14158                 cfg->cbb = bb;
14159                 for (; ins; ins = ins->next) {
14160                         const char *spec = INS_INFO (ins->opcode);
14161                         int regtype = 0, regindex;
14162                         gint32 prev_bb;
14163
14164                         if (G_UNLIKELY (cfg->verbose_level > 2))
14165                                 mono_print_ins (ins);
14166
14167                         g_assert (ins->opcode >= MONO_CEE_LAST);
14168
14169                         for (regindex = 0; regindex < 4; regindex ++) {
14170                                 int vreg = 0;
14171
14172                                 if (regindex == 0) {
14173                                         regtype = spec [MONO_INST_DEST];
14174                                         if (regtype == ' ')
14175                                                 continue;
14176                                         vreg = ins->dreg;
14177                                 } else if (regindex == 1) {
14178                                         regtype = spec [MONO_INST_SRC1];
14179                                         if (regtype == ' ')
14180                                                 continue;
14181                                         vreg = ins->sreg1;
14182                                 } else if (regindex == 2) {
14183                                         regtype = spec [MONO_INST_SRC2];
14184                                         if (regtype == ' ')
14185                                                 continue;
14186                                         vreg = ins->sreg2;
14187                                 } else if (regindex == 3) {
14188                                         regtype = spec [MONO_INST_SRC3];
14189                                         if (regtype == ' ')
14190                                                 continue;
14191                                         vreg = ins->sreg3;
14192                                 }
14193
14194 #if SIZEOF_REGISTER == 4
14195                                 /* In the LLVM case, the long opcodes are not decomposed */
14196                                 if (regtype == 'l' && !COMPILE_LLVM (cfg)) {
14197                                         /*
14198                                          * Since some instructions reference the original long vreg,
14199                                          * and some reference the two component vregs, it is quite hard
14200                                          * to determine when it needs to be global. So be conservative.
14201                                          */
14202                                         if (!get_vreg_to_inst (cfg, vreg)) {
14203                                                 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int64_class->byval_arg, OP_LOCAL, vreg);
14204
14205                                                 if (cfg->verbose_level > 2)
14206                                                         printf ("LONG VREG R%d made global.\n", vreg);
14207                                         }
14208
14209                                         /*
14210                                          * Make the component vregs volatile since the optimizations can
14211                                          * get confused otherwise.
14212                                          */
14213                                         get_vreg_to_inst (cfg, MONO_LVREG_LS (vreg))->flags |= MONO_INST_VOLATILE;
14214                                         get_vreg_to_inst (cfg, MONO_LVREG_MS (vreg))->flags |= MONO_INST_VOLATILE;
14215                                 }
14216 #endif
14217
14218                                 g_assert (vreg != -1);
14219
14220                                 prev_bb = vreg_to_bb [vreg];
14221                                 if (prev_bb == 0) {
14222                                         /* 0 is a valid block num */
14223                                         vreg_to_bb [vreg] = block_num + 1;
14224                                 } else if ((prev_bb != block_num + 1) && (prev_bb != -1)) {
14225                                         if (((regtype == 'i' && (vreg < MONO_MAX_IREGS))) || (regtype == 'f' && (vreg < MONO_MAX_FREGS)))
14226                                                 continue;
14227
14228                                         if (!get_vreg_to_inst (cfg, vreg)) {
14229                                                 if (G_UNLIKELY (cfg->verbose_level > 2))
14230                                                         printf ("VREG R%d used in BB%d and BB%d made global.\n", vreg, vreg_to_bb [vreg], block_num);
14231
14232                                                 switch (regtype) {
14233                                                 case 'i':
14234                                                         if (vreg_is_ref (cfg, vreg))
14235                                                                 mono_compile_create_var_for_vreg (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL, vreg);
14236                                                         else
14237                                                                 mono_compile_create_var_for_vreg (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL, vreg);
14238                                                         break;
14239                                                 case 'l':
14240                                                         mono_compile_create_var_for_vreg (cfg, &mono_defaults.int64_class->byval_arg, OP_LOCAL, vreg);
14241                                                         break;
14242                                                 case 'f':
14243                                                         mono_compile_create_var_for_vreg (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL, vreg);
14244                                                         break;
14245                                                 case 'v':
14246                                                         mono_compile_create_var_for_vreg (cfg, &ins->klass->byval_arg, OP_LOCAL, vreg);
14247                                                         break;
14248                                                 default:
14249                                                         g_assert_not_reached ();
14250                                                 }
14251                                         }
14252
14253                                         /* Flag as having been used in more than one bb */
14254                                         vreg_to_bb [vreg] = -1;
14255                                 }
14256                         }
14257                 }
14258         }
14259
14260         /* If a variable is used in only one bblock, convert it into a local vreg */
14261         for (i = 0; i < cfg->num_varinfo; i++) {
14262                 MonoInst *var = cfg->varinfo [i];
14263                 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
14264
14265                 switch (var->type) {
14266                 case STACK_I4:
14267                 case STACK_OBJ:
14268                 case STACK_PTR:
14269                 case STACK_MP:
14270                 case STACK_VTYPE:
14271 #if SIZEOF_REGISTER == 8
14272                 case STACK_I8:
14273 #endif
14274 #if !defined(TARGET_X86)
14275                 /* Enabling this screws up the fp stack on x86 */
14276                 case STACK_R8:
14277 #endif
14278                         if (mono_arch_is_soft_float ())
14279                                 break;
14280
14281                         /*
14282                         if (var->type == STACK_VTYPE && cfg->gsharedvt && mini_is_gsharedvt_variable_type (var->inst_vtype))
14283                                 break;
14284                         */
14285
14286                         /* Arguments are implicitly global */
14287                         /* Putting R4 vars into registers doesn't work currently */
14288                         /* The gsharedvt vars are implicitly referenced by ldaddr opcodes, but those opcodes are only generated later */
14289                         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) {
14290                                 /* 
14291                                  * Make that the variable's liveness interval doesn't contain a call, since
14292                                  * that would cause the lvreg to be spilled, making the whole optimization
14293                                  * useless.
14294                                  */
14295                                 /* This is too slow for JIT compilation */
14296 #if 0
14297                                 if (cfg->compile_aot && vreg_to_bb [var->dreg]) {
14298                                         MonoInst *ins;
14299                                         int def_index, call_index, ins_index;
14300                                         gboolean spilled = FALSE;
14301
14302                                         def_index = -1;
14303                                         call_index = -1;
14304                                         ins_index = 0;
14305                                         for (ins = vreg_to_bb [var->dreg]->code; ins; ins = ins->next) {
14306                                                 const char *spec = INS_INFO (ins->opcode);
14307
14308                                                 if ((spec [MONO_INST_DEST] != ' ') && (ins->dreg == var->dreg))
14309                                                         def_index = ins_index;
14310
14311                                                 if (((spec [MONO_INST_SRC1] != ' ') && (ins->sreg1 == var->dreg)) ||
14312                                                         ((spec [MONO_INST_SRC1] != ' ') && (ins->sreg1 == var->dreg))) {
14313                                                         if (call_index > def_index) {
14314                                                                 spilled = TRUE;
14315                                                                 break;
14316                                                         }
14317                                                 }
14318
14319                                                 if (MONO_IS_CALL (ins))
14320                                                         call_index = ins_index;
14321
14322                                                 ins_index ++;
14323                                         }
14324
14325                                         if (spilled)
14326                                                 break;
14327                                 }
14328 #endif
14329
14330                                 if (G_UNLIKELY (cfg->verbose_level > 2))
14331                                         printf ("CONVERTED R%d(%d) TO VREG.\n", var->dreg, vmv->idx);
14332                                 var->flags |= MONO_INST_IS_DEAD;
14333                                 cfg->vreg_to_inst [var->dreg] = NULL;
14334                         }
14335                         break;
14336                 }
14337         }
14338
14339         /* 
14340          * Compress the varinfo and vars tables so the liveness computation is faster and
14341          * takes up less space.
14342          */
14343         pos = 0;
14344         for (i = 0; i < cfg->num_varinfo; ++i) {
14345                 MonoInst *var = cfg->varinfo [i];
14346                 if (pos < i && cfg->locals_start == i)
14347                         cfg->locals_start = pos;
14348                 if (!(var->flags & MONO_INST_IS_DEAD)) {
14349                         if (pos < i) {
14350                                 cfg->varinfo [pos] = cfg->varinfo [i];
14351                                 cfg->varinfo [pos]->inst_c0 = pos;
14352                                 memcpy (&cfg->vars [pos], &cfg->vars [i], sizeof (MonoMethodVar));
14353                                 cfg->vars [pos].idx = pos;
14354 #if SIZEOF_REGISTER == 4
14355                                 if (cfg->varinfo [pos]->type == STACK_I8) {
14356                                         /* Modify the two component vars too */
14357                                         MonoInst *var1;
14358
14359                                         var1 = get_vreg_to_inst (cfg, MONO_LVREG_LS (cfg->varinfo [pos]->dreg));
14360                                         var1->inst_c0 = pos;
14361                                         var1 = get_vreg_to_inst (cfg, MONO_LVREG_MS (cfg->varinfo [pos]->dreg));
14362                                         var1->inst_c0 = pos;
14363                                 }
14364 #endif
14365                         }
14366                         pos ++;
14367                 }
14368         }
14369         cfg->num_varinfo = pos;
14370         if (cfg->locals_start > cfg->num_varinfo)
14371                 cfg->locals_start = cfg->num_varinfo;
14372 }
14373
14374 /*
14375  * mono_allocate_gsharedvt_vars:
14376  *
14377  *   Allocate variables with gsharedvt types to entries in the MonoGSharedVtMethodRuntimeInfo.entries array.
14378  * Initialize cfg->gsharedvt_vreg_to_idx with the mapping between vregs and indexes.
14379  */
14380 void
14381 mono_allocate_gsharedvt_vars (MonoCompile *cfg)
14382 {
14383         int i;
14384
14385         cfg->gsharedvt_vreg_to_idx = (int *)mono_mempool_alloc0 (cfg->mempool, sizeof (int) * cfg->next_vreg);
14386
14387         for (i = 0; i < cfg->num_varinfo; ++i) {
14388                 MonoInst *ins = cfg->varinfo [i];
14389                 int idx;
14390
14391                 if (mini_is_gsharedvt_variable_type (ins->inst_vtype)) {
14392                         if (i >= cfg->locals_start) {
14393                                 /* Local */
14394                                 idx = get_gsharedvt_info_slot (cfg, ins->inst_vtype, MONO_RGCTX_INFO_LOCAL_OFFSET);
14395                                 cfg->gsharedvt_vreg_to_idx [ins->dreg] = idx + 1;
14396                                 ins->opcode = OP_GSHAREDVT_LOCAL;
14397                                 ins->inst_imm = idx;
14398                         } else {
14399                                 /* Arg */
14400                                 cfg->gsharedvt_vreg_to_idx [ins->dreg] = -1;
14401                                 ins->opcode = OP_GSHAREDVT_ARG_REGOFFSET;
14402                         }
14403                 }
14404         }
14405 }
14406
14407 /**
14408  * mono_spill_global_vars:
14409  *
14410  *   Generate spill code for variables which are not allocated to registers, 
14411  * and replace vregs with their allocated hregs. *need_local_opts is set to TRUE if
14412  * code is generated which could be optimized by the local optimization passes.
14413  */
14414 void
14415 mono_spill_global_vars (MonoCompile *cfg, gboolean *need_local_opts)
14416 {
14417         MonoBasicBlock *bb;
14418         char spec2 [16];
14419         int orig_next_vreg;
14420         guint32 *vreg_to_lvreg;
14421         guint32 *lvregs;
14422         guint32 i, lvregs_len;
14423         gboolean dest_has_lvreg = FALSE;
14424         MonoStackType stacktypes [128];
14425         MonoInst **live_range_start, **live_range_end;
14426         MonoBasicBlock **live_range_start_bb, **live_range_end_bb;
14427
14428         *need_local_opts = FALSE;
14429
14430         memset (spec2, 0, sizeof (spec2));
14431
14432         /* FIXME: Move this function to mini.c */
14433         stacktypes ['i'] = STACK_PTR;
14434         stacktypes ['l'] = STACK_I8;
14435         stacktypes ['f'] = STACK_R8;
14436 #ifdef MONO_ARCH_SIMD_INTRINSICS
14437         stacktypes ['x'] = STACK_VTYPE;
14438 #endif
14439
14440 #if SIZEOF_REGISTER == 4
14441         /* Create MonoInsts for longs */
14442         for (i = 0; i < cfg->num_varinfo; i++) {
14443                 MonoInst *ins = cfg->varinfo [i];
14444
14445                 if ((ins->opcode != OP_REGVAR) && !(ins->flags & MONO_INST_IS_DEAD)) {
14446                         switch (ins->type) {
14447                         case STACK_R8:
14448                         case STACK_I8: {
14449                                 MonoInst *tree;
14450
14451                                 if (ins->type == STACK_R8 && !COMPILE_SOFT_FLOAT (cfg))
14452                                         break;
14453
14454                                 g_assert (ins->opcode == OP_REGOFFSET);
14455
14456                                 tree = get_vreg_to_inst (cfg, MONO_LVREG_LS (ins->dreg));
14457                                 g_assert (tree);
14458                                 tree->opcode = OP_REGOFFSET;
14459                                 tree->inst_basereg = ins->inst_basereg;
14460                                 tree->inst_offset = ins->inst_offset + MINI_LS_WORD_OFFSET;
14461
14462                                 tree = get_vreg_to_inst (cfg, MONO_LVREG_MS (ins->dreg));
14463                                 g_assert (tree);
14464                                 tree->opcode = OP_REGOFFSET;
14465                                 tree->inst_basereg = ins->inst_basereg;
14466                                 tree->inst_offset = ins->inst_offset + MINI_MS_WORD_OFFSET;
14467                                 break;
14468                         }
14469                         default:
14470                                 break;
14471                         }
14472                 }
14473         }
14474 #endif
14475
14476         if (cfg->compute_gc_maps) {
14477                 /* registers need liveness info even for !non refs */
14478                 for (i = 0; i < cfg->num_varinfo; i++) {
14479                         MonoInst *ins = cfg->varinfo [i];
14480
14481                         if (ins->opcode == OP_REGVAR)
14482                                 ins->flags |= MONO_INST_GC_TRACK;
14483                 }
14484         }
14485                 
14486         /* FIXME: widening and truncation */
14487
14488         /*
14489          * As an optimization, when a variable allocated to the stack is first loaded into 
14490          * an lvreg, we will remember the lvreg and use it the next time instead of loading
14491          * the variable again.
14492          */
14493         orig_next_vreg = cfg->next_vreg;
14494         vreg_to_lvreg = (guint32 *)mono_mempool_alloc0 (cfg->mempool, sizeof (guint32) * cfg->next_vreg);
14495         lvregs = (guint32 *)mono_mempool_alloc (cfg->mempool, sizeof (guint32) * 1024);
14496         lvregs_len = 0;
14497
14498         /* 
14499          * These arrays contain the first and last instructions accessing a given
14500          * variable.
14501          * Since we emit bblocks in the same order we process them here, and we
14502          * don't split live ranges, these will precisely describe the live range of
14503          * the variable, i.e. the instruction range where a valid value can be found
14504          * in the variables location.
14505          * The live range is computed using the liveness info computed by the liveness pass.
14506          * We can't use vmv->range, since that is an abstract live range, and we need
14507          * one which is instruction precise.
14508          * FIXME: Variables used in out-of-line bblocks have a hole in their live range.
14509          */
14510         /* FIXME: Only do this if debugging info is requested */
14511         live_range_start = g_new0 (MonoInst*, cfg->next_vreg);
14512         live_range_end = g_new0 (MonoInst*, cfg->next_vreg);
14513         live_range_start_bb = g_new (MonoBasicBlock*, cfg->next_vreg);
14514         live_range_end_bb = g_new (MonoBasicBlock*, cfg->next_vreg);
14515         
14516         /* Add spill loads/stores */
14517         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
14518                 MonoInst *ins;
14519
14520                 if (cfg->verbose_level > 2)
14521                         printf ("\nSPILL BLOCK %d:\n", bb->block_num);
14522
14523                 /* Clear vreg_to_lvreg array */
14524                 for (i = 0; i < lvregs_len; i++)
14525                         vreg_to_lvreg [lvregs [i]] = 0;
14526                 lvregs_len = 0;
14527
14528                 cfg->cbb = bb;
14529                 MONO_BB_FOR_EACH_INS (bb, ins) {
14530                         const char *spec = INS_INFO (ins->opcode);
14531                         int regtype, srcindex, sreg, tmp_reg, prev_dreg, num_sregs;
14532                         gboolean store, no_lvreg;
14533                         int sregs [MONO_MAX_SRC_REGS];
14534
14535                         if (G_UNLIKELY (cfg->verbose_level > 2))
14536                                 mono_print_ins (ins);
14537
14538                         if (ins->opcode == OP_NOP)
14539                                 continue;
14540
14541                         /* 
14542                          * We handle LDADDR here as well, since it can only be decomposed
14543                          * when variable addresses are known.
14544                          */
14545                         if (ins->opcode == OP_LDADDR) {
14546                                 MonoInst *var = (MonoInst *)ins->inst_p0;
14547
14548                                 if (var->opcode == OP_VTARG_ADDR) {
14549                                         /* Happens on SPARC/S390 where vtypes are passed by reference */
14550                                         MonoInst *vtaddr = var->inst_left;
14551                                         if (vtaddr->opcode == OP_REGVAR) {
14552                                                 ins->opcode = OP_MOVE;
14553                                                 ins->sreg1 = vtaddr->dreg;
14554                                         }
14555                                         else if (var->inst_left->opcode == OP_REGOFFSET) {
14556                                                 ins->opcode = OP_LOAD_MEMBASE;
14557                                                 ins->inst_basereg = vtaddr->inst_basereg;
14558                                                 ins->inst_offset = vtaddr->inst_offset;
14559                                         } else
14560                                                 NOT_IMPLEMENTED;
14561                                 } else if (cfg->gsharedvt && cfg->gsharedvt_vreg_to_idx [var->dreg] < 0) {
14562                                         /* gsharedvt arg passed by ref */
14563                                         g_assert (var->opcode == OP_GSHAREDVT_ARG_REGOFFSET);
14564
14565                                         ins->opcode = OP_LOAD_MEMBASE;
14566                                         ins->inst_basereg = var->inst_basereg;
14567                                         ins->inst_offset = var->inst_offset;
14568                                 } else if (cfg->gsharedvt && cfg->gsharedvt_vreg_to_idx [var->dreg]) {
14569                                         MonoInst *load, *load2, *load3;
14570                                         int idx = cfg->gsharedvt_vreg_to_idx [var->dreg] - 1;
14571                                         int reg1, reg2, reg3;
14572                                         MonoInst *info_var = cfg->gsharedvt_info_var;
14573                                         MonoInst *locals_var = cfg->gsharedvt_locals_var;
14574
14575                                         /*
14576                                          * gsharedvt local.
14577                                          * Compute the address of the local as gsharedvt_locals_var + gsharedvt_info_var->locals_offsets [idx].
14578                                          */
14579
14580                                         g_assert (var->opcode == OP_GSHAREDVT_LOCAL);
14581
14582                                         g_assert (info_var);
14583                                         g_assert (locals_var);
14584
14585                                         /* Mark the instruction used to compute the locals var as used */
14586                                         cfg->gsharedvt_locals_var_ins = NULL;
14587
14588                                         /* Load the offset */
14589                                         if (info_var->opcode == OP_REGOFFSET) {
14590                                                 reg1 = alloc_ireg (cfg);
14591                                                 NEW_LOAD_MEMBASE (cfg, load, OP_LOAD_MEMBASE, reg1, info_var->inst_basereg, info_var->inst_offset);
14592                                         } else if (info_var->opcode == OP_REGVAR) {
14593                                                 load = NULL;
14594                                                 reg1 = info_var->dreg;
14595                                         } else {
14596                                                 g_assert_not_reached ();
14597                                         }
14598                                         reg2 = alloc_ireg (cfg);
14599                                         NEW_LOAD_MEMBASE (cfg, load2, OP_LOADI4_MEMBASE, reg2, reg1, MONO_STRUCT_OFFSET (MonoGSharedVtMethodRuntimeInfo, entries) + (idx * sizeof (gpointer)));
14600                                         /* Load the locals area address */
14601                                         reg3 = alloc_ireg (cfg);
14602                                         if (locals_var->opcode == OP_REGOFFSET) {
14603                                                 NEW_LOAD_MEMBASE (cfg, load3, OP_LOAD_MEMBASE, reg3, locals_var->inst_basereg, locals_var->inst_offset);
14604                                         } else if (locals_var->opcode == OP_REGVAR) {
14605                                                 NEW_UNALU (cfg, load3, OP_MOVE, reg3, locals_var->dreg);
14606                                         } else {
14607                                                 g_assert_not_reached ();
14608                                         }
14609                                         /* Compute the address */
14610                                         ins->opcode = OP_PADD;
14611                                         ins->sreg1 = reg3;
14612                                         ins->sreg2 = reg2;
14613
14614                                         mono_bblock_insert_before_ins (bb, ins, load3);
14615                                         mono_bblock_insert_before_ins (bb, load3, load2);
14616                                         if (load)
14617                                                 mono_bblock_insert_before_ins (bb, load2, load);
14618                                 } else {
14619                                         g_assert (var->opcode == OP_REGOFFSET);
14620
14621                                         ins->opcode = OP_ADD_IMM;
14622                                         ins->sreg1 = var->inst_basereg;
14623                                         ins->inst_imm = var->inst_offset;
14624                                 }
14625
14626                                 *need_local_opts = TRUE;
14627                                 spec = INS_INFO (ins->opcode);
14628                         }
14629
14630                         if (ins->opcode < MONO_CEE_LAST) {
14631                                 mono_print_ins (ins);
14632                                 g_assert_not_reached ();
14633                         }
14634
14635                         /*
14636                          * Store opcodes have destbasereg in the dreg, but in reality, it is an
14637                          * src register.
14638                          * FIXME:
14639                          */
14640                         if (MONO_IS_STORE_MEMBASE (ins)) {
14641                                 tmp_reg = ins->dreg;
14642                                 ins->dreg = ins->sreg2;
14643                                 ins->sreg2 = tmp_reg;
14644                                 store = TRUE;
14645
14646                                 spec2 [MONO_INST_DEST] = ' ';
14647                                 spec2 [MONO_INST_SRC1] = spec [MONO_INST_SRC1];
14648                                 spec2 [MONO_INST_SRC2] = spec [MONO_INST_DEST];
14649                                 spec2 [MONO_INST_SRC3] = ' ';
14650                                 spec = spec2;
14651                         } else if (MONO_IS_STORE_MEMINDEX (ins))
14652                                 g_assert_not_reached ();
14653                         else
14654                                 store = FALSE;
14655                         no_lvreg = FALSE;
14656
14657                         if (G_UNLIKELY (cfg->verbose_level > 2)) {
14658                                 printf ("\t %.3s %d", spec, ins->dreg);
14659                                 num_sregs = mono_inst_get_src_registers (ins, sregs);
14660                                 for (srcindex = 0; srcindex < num_sregs; ++srcindex)
14661                                         printf (" %d", sregs [srcindex]);
14662                                 printf ("\n");
14663                         }
14664
14665                         /***************/
14666                         /*    DREG     */
14667                         /***************/
14668                         regtype = spec [MONO_INST_DEST];
14669                         g_assert (((ins->dreg == -1) && (regtype == ' ')) || ((ins->dreg != -1) && (regtype != ' ')));
14670                         prev_dreg = -1;
14671
14672                         if ((ins->dreg != -1) && get_vreg_to_inst (cfg, ins->dreg)) {
14673                                 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
14674                                 MonoInst *store_ins;
14675                                 int store_opcode;
14676                                 MonoInst *def_ins = ins;
14677                                 int dreg = ins->dreg; /* The original vreg */
14678
14679                                 store_opcode = mono_type_to_store_membase (cfg, var->inst_vtype);
14680
14681                                 if (var->opcode == OP_REGVAR) {
14682                                         ins->dreg = var->dreg;
14683                                 } 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)) {
14684                                         /* 
14685                                          * Instead of emitting a load+store, use a _membase opcode.
14686                                          */
14687                                         g_assert (var->opcode == OP_REGOFFSET);
14688                                         if (ins->opcode == OP_MOVE) {
14689                                                 NULLIFY_INS (ins);
14690                                                 def_ins = NULL;
14691                                         } else {
14692                                                 ins->opcode = op_to_op_dest_membase (store_opcode, ins->opcode);
14693                                                 ins->inst_basereg = var->inst_basereg;
14694                                                 ins->inst_offset = var->inst_offset;
14695                                                 ins->dreg = -1;
14696                                         }
14697                                         spec = INS_INFO (ins->opcode);
14698                                 } else {
14699                                         guint32 lvreg;
14700
14701                                         g_assert (var->opcode == OP_REGOFFSET);
14702
14703                                         prev_dreg = ins->dreg;
14704
14705                                         /* Invalidate any previous lvreg for this vreg */
14706                                         vreg_to_lvreg [ins->dreg] = 0;
14707
14708                                         lvreg = 0;
14709
14710                                         if (COMPILE_SOFT_FLOAT (cfg) && store_opcode == OP_STORER8_MEMBASE_REG) {
14711                                                 regtype = 'l';
14712                                                 store_opcode = OP_STOREI8_MEMBASE_REG;
14713                                         }
14714
14715                                         ins->dreg = alloc_dreg (cfg, stacktypes [regtype]);
14716
14717 #if SIZEOF_REGISTER != 8
14718                                         if (regtype == 'l') {
14719                                                 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));
14720                                                 mono_bblock_insert_after_ins (bb, ins, store_ins);
14721                                                 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));
14722                                                 mono_bblock_insert_after_ins (bb, ins, store_ins);
14723                                                 def_ins = store_ins;
14724                                         }
14725                                         else
14726 #endif
14727                                         {
14728                                                 g_assert (store_opcode != OP_STOREV_MEMBASE);
14729
14730                                                 /* Try to fuse the store into the instruction itself */
14731                                                 /* FIXME: Add more instructions */
14732                                                 if (!lvreg && ((ins->opcode == OP_ICONST) || ((ins->opcode == OP_I8CONST) && (ins->inst_c0 == 0)))) {
14733                                                         ins->opcode = store_membase_reg_to_store_membase_imm (store_opcode);
14734                                                         ins->inst_imm = ins->inst_c0;
14735                                                         ins->inst_destbasereg = var->inst_basereg;
14736                                                         ins->inst_offset = var->inst_offset;
14737                                                         spec = INS_INFO (ins->opcode);
14738                                                 } else if (!lvreg && ((ins->opcode == OP_MOVE) || (ins->opcode == OP_FMOVE) || (ins->opcode == OP_LMOVE) || (ins->opcode == OP_RMOVE))) {
14739                                                         ins->opcode = store_opcode;
14740                                                         ins->inst_destbasereg = var->inst_basereg;
14741                                                         ins->inst_offset = var->inst_offset;
14742
14743                                                         no_lvreg = TRUE;
14744
14745                                                         tmp_reg = ins->dreg;
14746                                                         ins->dreg = ins->sreg2;
14747                                                         ins->sreg2 = tmp_reg;
14748                                                         store = TRUE;
14749
14750                                                         spec2 [MONO_INST_DEST] = ' ';
14751                                                         spec2 [MONO_INST_SRC1] = spec [MONO_INST_SRC1];
14752                                                         spec2 [MONO_INST_SRC2] = spec [MONO_INST_DEST];
14753                                                         spec2 [MONO_INST_SRC3] = ' ';
14754                                                         spec = spec2;
14755                                                 } else if (!lvreg && (op_to_op_store_membase (store_opcode, ins->opcode) != -1)) {
14756                                                         // FIXME: The backends expect the base reg to be in inst_basereg
14757                                                         ins->opcode = op_to_op_store_membase (store_opcode, ins->opcode);
14758                                                         ins->dreg = -1;
14759                                                         ins->inst_basereg = var->inst_basereg;
14760                                                         ins->inst_offset = var->inst_offset;
14761                                                         spec = INS_INFO (ins->opcode);
14762                                                 } else {
14763                                                         /* printf ("INS: "); mono_print_ins (ins); */
14764                                                         /* Create a store instruction */
14765                                                         NEW_STORE_MEMBASE (cfg, store_ins, store_opcode, var->inst_basereg, var->inst_offset, ins->dreg);
14766
14767                                                         /* Insert it after the instruction */
14768                                                         mono_bblock_insert_after_ins (bb, ins, store_ins);
14769
14770                                                         def_ins = store_ins;
14771
14772                                                         /* 
14773                                                          * We can't assign ins->dreg to var->dreg here, since the
14774                                                          * sregs could use it. So set a flag, and do it after
14775                                                          * the sregs.
14776                                                          */
14777                                                         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)))
14778                                                                 dest_has_lvreg = TRUE;
14779                                                 }
14780                                         }
14781                                 }
14782
14783                                 if (def_ins && !live_range_start [dreg]) {
14784                                         live_range_start [dreg] = def_ins;
14785                                         live_range_start_bb [dreg] = bb;
14786                                 }
14787
14788                                 if (cfg->compute_gc_maps && def_ins && (var->flags & MONO_INST_GC_TRACK)) {
14789                                         MonoInst *tmp;
14790
14791                                         MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_DEF);
14792                                         tmp->inst_c1 = dreg;
14793                                         mono_bblock_insert_after_ins (bb, def_ins, tmp);
14794                                 }
14795                         }
14796
14797                         /************/
14798                         /*  SREGS   */
14799                         /************/
14800                         num_sregs = mono_inst_get_src_registers (ins, sregs);
14801                         for (srcindex = 0; srcindex < 3; ++srcindex) {
14802                                 regtype = spec [MONO_INST_SRC1 + srcindex];
14803                                 sreg = sregs [srcindex];
14804
14805                                 g_assert (((sreg == -1) && (regtype == ' ')) || ((sreg != -1) && (regtype != ' ')));
14806                                 if ((sreg != -1) && get_vreg_to_inst (cfg, sreg)) {
14807                                         MonoInst *var = get_vreg_to_inst (cfg, sreg);
14808                                         MonoInst *use_ins = ins;
14809                                         MonoInst *load_ins;
14810                                         guint32 load_opcode;
14811
14812                                         if (var->opcode == OP_REGVAR) {
14813                                                 sregs [srcindex] = var->dreg;
14814                                                 //mono_inst_set_src_registers (ins, sregs);
14815                                                 live_range_end [sreg] = use_ins;
14816                                                 live_range_end_bb [sreg] = bb;
14817
14818                                                 if (cfg->compute_gc_maps && var->dreg < orig_next_vreg && (var->flags & MONO_INST_GC_TRACK)) {
14819                                                         MonoInst *tmp;
14820
14821                                                         MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_USE);
14822                                                         /* var->dreg is a hreg */
14823                                                         tmp->inst_c1 = sreg;
14824                                                         mono_bblock_insert_after_ins (bb, ins, tmp);
14825                                                 }
14826
14827                                                 continue;
14828                                         }
14829
14830                                         g_assert (var->opcode == OP_REGOFFSET);
14831                                                 
14832                                         load_opcode = mono_type_to_load_membase (cfg, var->inst_vtype);
14833
14834                                         g_assert (load_opcode != OP_LOADV_MEMBASE);
14835
14836                                         if (vreg_to_lvreg [sreg]) {
14837                                                 g_assert (vreg_to_lvreg [sreg] != -1);
14838
14839                                                 /* The variable is already loaded to an lvreg */
14840                                                 if (G_UNLIKELY (cfg->verbose_level > 2))
14841                                                         printf ("\t\tUse lvreg R%d for R%d.\n", vreg_to_lvreg [sreg], sreg);
14842                                                 sregs [srcindex] = vreg_to_lvreg [sreg];
14843                                                 //mono_inst_set_src_registers (ins, sregs);
14844                                                 continue;
14845                                         }
14846
14847                                         /* Try to fuse the load into the instruction */
14848                                         if ((srcindex == 0) && (op_to_op_src1_membase (cfg, load_opcode, ins->opcode) != -1)) {
14849                                                 ins->opcode = op_to_op_src1_membase (cfg, load_opcode, ins->opcode);
14850                                                 sregs [0] = var->inst_basereg;
14851                                                 //mono_inst_set_src_registers (ins, sregs);
14852                                                 ins->inst_offset = var->inst_offset;
14853                                         } else if ((srcindex == 1) && (op_to_op_src2_membase (cfg, load_opcode, ins->opcode) != -1)) {
14854                                                 ins->opcode = op_to_op_src2_membase (cfg, load_opcode, ins->opcode);
14855                                                 sregs [1] = var->inst_basereg;
14856                                                 //mono_inst_set_src_registers (ins, sregs);
14857                                                 ins->inst_offset = var->inst_offset;
14858                                         } else {
14859                                                 if (MONO_IS_REAL_MOVE (ins)) {
14860                                                         ins->opcode = OP_NOP;
14861                                                         sreg = ins->dreg;
14862                                                 } else {
14863                                                         //printf ("%d ", srcindex); mono_print_ins (ins);
14864
14865                                                         sreg = alloc_dreg (cfg, stacktypes [regtype]);
14866
14867                                                         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) {
14868                                                                 if (var->dreg == prev_dreg) {
14869                                                                         /*
14870                                                                          * sreg refers to the value loaded by the load
14871                                                                          * emitted below, but we need to use ins->dreg
14872                                                                          * since it refers to the store emitted earlier.
14873                                                                          */
14874                                                                         sreg = ins->dreg;
14875                                                                 }
14876                                                                 g_assert (sreg != -1);
14877                                                                 vreg_to_lvreg [var->dreg] = sreg;
14878                                                                 g_assert (lvregs_len < 1024);
14879                                                                 lvregs [lvregs_len ++] = var->dreg;
14880                                                         }
14881                                                 }
14882
14883                                                 sregs [srcindex] = sreg;
14884                                                 //mono_inst_set_src_registers (ins, sregs);
14885
14886 #if SIZEOF_REGISTER != 8
14887                                                 if (regtype == 'l') {
14888                                                         NEW_LOAD_MEMBASE (cfg, load_ins, OP_LOADI4_MEMBASE, MONO_LVREG_MS (sreg), var->inst_basereg, var->inst_offset + MINI_MS_WORD_OFFSET);
14889                                                         mono_bblock_insert_before_ins (bb, ins, load_ins);
14890                                                         NEW_LOAD_MEMBASE (cfg, load_ins, OP_LOADI4_MEMBASE, MONO_LVREG_LS (sreg), var->inst_basereg, var->inst_offset + MINI_LS_WORD_OFFSET);
14891                                                         mono_bblock_insert_before_ins (bb, ins, load_ins);
14892                                                         use_ins = load_ins;
14893                                                 }
14894                                                 else
14895 #endif
14896                                                 {
14897 #if SIZEOF_REGISTER == 4
14898                                                         g_assert (load_opcode != OP_LOADI8_MEMBASE);
14899 #endif
14900                                                         NEW_LOAD_MEMBASE (cfg, load_ins, load_opcode, sreg, var->inst_basereg, var->inst_offset);
14901                                                         mono_bblock_insert_before_ins (bb, ins, load_ins);
14902                                                         use_ins = load_ins;
14903                                                 }
14904                                         }
14905
14906                                         if (var->dreg < orig_next_vreg) {
14907                                                 live_range_end [var->dreg] = use_ins;
14908                                                 live_range_end_bb [var->dreg] = bb;
14909                                         }
14910
14911                                         if (cfg->compute_gc_maps && var->dreg < orig_next_vreg && (var->flags & MONO_INST_GC_TRACK)) {
14912                                                 MonoInst *tmp;
14913
14914                                                 MONO_INST_NEW (cfg, tmp, OP_GC_LIVENESS_USE);
14915                                                 tmp->inst_c1 = var->dreg;
14916                                                 mono_bblock_insert_after_ins (bb, ins, tmp);
14917                                         }
14918                                 }
14919                         }
14920                         mono_inst_set_src_registers (ins, sregs);
14921
14922                         if (dest_has_lvreg) {
14923                                 g_assert (ins->dreg != -1);
14924                                 vreg_to_lvreg [prev_dreg] = ins->dreg;
14925                                 g_assert (lvregs_len < 1024);
14926                                 lvregs [lvregs_len ++] = prev_dreg;
14927                                 dest_has_lvreg = FALSE;
14928                         }
14929
14930                         if (store) {
14931                                 tmp_reg = ins->dreg;
14932                                 ins->dreg = ins->sreg2;
14933                                 ins->sreg2 = tmp_reg;
14934                         }
14935
14936                         if (MONO_IS_CALL (ins)) {
14937                                 /* Clear vreg_to_lvreg array */
14938                                 for (i = 0; i < lvregs_len; i++)
14939                                         vreg_to_lvreg [lvregs [i]] = 0;
14940                                 lvregs_len = 0;
14941                         } else if (ins->opcode == OP_NOP) {
14942                                 ins->dreg = -1;
14943                                 MONO_INST_NULLIFY_SREGS (ins);
14944                         }
14945
14946                         if (cfg->verbose_level > 2)
14947                                 mono_print_ins_index (1, ins);
14948                 }
14949
14950                 /* Extend the live range based on the liveness info */
14951                 if (cfg->compute_precise_live_ranges && bb->live_out_set && bb->code) {
14952                         for (i = 0; i < cfg->num_varinfo; i ++) {
14953                                 MonoMethodVar *vi = MONO_VARINFO (cfg, i);
14954
14955                                 if (vreg_is_volatile (cfg, vi->vreg))
14956                                         /* The liveness info is incomplete */
14957                                         continue;
14958
14959                                 if (mono_bitset_test_fast (bb->live_in_set, i) && !live_range_start [vi->vreg]) {
14960                                         /* Live from at least the first ins of this bb */
14961                                         live_range_start [vi->vreg] = bb->code;
14962                                         live_range_start_bb [vi->vreg] = bb;
14963                                 }
14964
14965                                 if (mono_bitset_test_fast (bb->live_out_set, i)) {
14966                                         /* Live at least until the last ins of this bb */
14967                                         live_range_end [vi->vreg] = bb->last_ins;
14968                                         live_range_end_bb [vi->vreg] = bb;
14969                                 }
14970                         }
14971                 }
14972         }
14973         
14974         /*
14975          * Emit LIVERANGE_START/LIVERANGE_END opcodes, the backend will implement them
14976          * by storing the current native offset into MonoMethodVar->live_range_start/end.
14977          */
14978         if (cfg->backend->have_liverange_ops && cfg->compute_precise_live_ranges && cfg->comp_done & MONO_COMP_LIVENESS) {
14979                 for (i = 0; i < cfg->num_varinfo; ++i) {
14980                         int vreg = MONO_VARINFO (cfg, i)->vreg;
14981                         MonoInst *ins;
14982
14983                         if (live_range_start [vreg]) {
14984                                 MONO_INST_NEW (cfg, ins, OP_LIVERANGE_START);
14985                                 ins->inst_c0 = i;
14986                                 ins->inst_c1 = vreg;
14987                                 mono_bblock_insert_after_ins (live_range_start_bb [vreg], live_range_start [vreg], ins);
14988                         }
14989                         if (live_range_end [vreg]) {
14990                                 MONO_INST_NEW (cfg, ins, OP_LIVERANGE_END);
14991                                 ins->inst_c0 = i;
14992                                 ins->inst_c1 = vreg;
14993                                 if (live_range_end [vreg] == live_range_end_bb [vreg]->last_ins)
14994                                         mono_add_ins_to_end (live_range_end_bb [vreg], ins);
14995                                 else
14996                                         mono_bblock_insert_after_ins (live_range_end_bb [vreg], live_range_end [vreg], ins);
14997                         }
14998                 }
14999         }
15000
15001         if (cfg->gsharedvt_locals_var_ins) {
15002                 /* Nullify if unused */
15003                 cfg->gsharedvt_locals_var_ins->opcode = OP_PCONST;
15004                 cfg->gsharedvt_locals_var_ins->inst_imm = 0;
15005         }
15006
15007         g_free (live_range_start);
15008         g_free (live_range_end);
15009         g_free (live_range_start_bb);
15010         g_free (live_range_end_bb);
15011 }
15012
15013 /**
15014  * FIXME:
15015  * - use 'iadd' instead of 'int_add'
15016  * - handling ovf opcodes: decompose in method_to_ir.
15017  * - unify iregs/fregs
15018  *   -> partly done, the missing parts are:
15019  *   - a more complete unification would involve unifying the hregs as well, so
15020  *     code wouldn't need if (fp) all over the place. but that would mean the hregs
15021  *     would no longer map to the machine hregs, so the code generators would need to
15022  *     be modified. Also, on ia64 for example, niregs + nfregs > 256 -> bitmasks
15023  *     wouldn't work any more. Duplicating the code in mono_local_regalloc () into
15024  *     fp/non-fp branches speeds it up by about 15%.
15025  * - use sext/zext opcodes instead of shifts
15026  * - add OP_ICALL
15027  * - get rid of TEMPLOADs if possible and use vregs instead
15028  * - clean up usage of OP_P/OP_ opcodes
15029  * - cleanup usage of DUMMY_USE
15030  * - cleanup the setting of ins->type for MonoInst's which are pushed on the 
15031  *   stack
15032  * - set the stack type and allocate a dreg in the EMIT_NEW macros
15033  * - get rid of all the <foo>2 stuff when the new JIT is ready.
15034  * - make sure handle_stack_args () is called before the branch is emitted
15035  * - when the new IR is done, get rid of all unused stuff
15036  * - COMPARE/BEQ as separate instructions or unify them ?
15037  *   - keeping them separate allows specialized compare instructions like
15038  *     compare_imm, compare_membase
15039  *   - most back ends unify fp compare+branch, fp compare+ceq
15040  * - integrate mono_save_args into inline_method
15041  * - get rid of the empty bblocks created by MONO_EMIT_NEW_BRACH_BLOCK2
15042  * - handle long shift opts on 32 bit platforms somehow: they require 
15043  *   3 sregs (2 for arg1 and 1 for arg2)
15044  * - make byref a 'normal' type.
15045  * - use vregs for bb->out_stacks if possible, handle_global_vreg will make them a
15046  *   variable if needed.
15047  * - do not start a new IL level bblock when cfg->cbb is changed by a function call
15048  *   like inline_method.
15049  * - remove inlining restrictions
15050  * - fix LNEG and enable cfold of INEG
15051  * - generalize x86 optimizations like ldelema as a peephole optimization
15052  * - add store_mem_imm for amd64
15053  * - optimize the loading of the interruption flag in the managed->native wrappers
15054  * - avoid special handling of OP_NOP in passes
15055  * - move code inserting instructions into one function/macro.
15056  * - try a coalescing phase after liveness analysis
15057  * - add float -> vreg conversion + local optimizations on !x86
15058  * - figure out how to handle decomposed branches during optimizations, ie.
15059  *   compare+branch, op_jump_table+op_br etc.
15060  * - promote RuntimeXHandles to vregs
15061  * - vtype cleanups:
15062  *   - add a NEW_VARLOADA_VREG macro
15063  * - the vtype optimizations are blocked by the LDADDR opcodes generated for 
15064  *   accessing vtype fields.
15065  * - get rid of I8CONST on 64 bit platforms
15066  * - dealing with the increase in code size due to branches created during opcode
15067  *   decomposition:
15068  *   - use extended basic blocks
15069  *     - all parts of the JIT
15070  *     - handle_global_vregs () && local regalloc
15071  *   - avoid introducing global vregs during decomposition, like 'vtable' in isinst
15072  * - sources of increase in code size:
15073  *   - vtypes
15074  *   - long compares
15075  *   - isinst and castclass
15076  *   - lvregs not allocated to global registers even if used multiple times
15077  * - call cctors outside the JIT, to make -v output more readable and JIT timings more
15078  *   meaningful.
15079  * - check for fp stack leakage in other opcodes too. (-> 'exceptions' optimization)
15080  * - add all micro optimizations from the old JIT
15081  * - put tree optimizations into the deadce pass
15082  * - decompose op_start_handler/op_endfilter/op_endfinally earlier using an arch
15083  *   specific function.
15084  * - unify the float comparison opcodes with the other comparison opcodes, i.e.
15085  *   fcompare + branchCC.
15086  * - create a helper function for allocating a stack slot, taking into account 
15087  *   MONO_CFG_HAS_SPILLUP.
15088  * - merge r68207.
15089  * - merge the ia64 switch changes.
15090  * - optimize mono_regstate2_alloc_int/float.
15091  * - fix the pessimistic handling of variables accessed in exception handler blocks.
15092  * - need to write a tree optimization pass, but the creation of trees is difficult, i.e.
15093  *   parts of the tree could be separated by other instructions, killing the tree
15094  *   arguments, or stores killing loads etc. Also, should we fold loads into other
15095  *   instructions if the result of the load is used multiple times ?
15096  * - make the REM_IMM optimization in mini-x86.c arch-independent.
15097  * - LAST MERGE: 108395.
15098  * - when returning vtypes in registers, generate IR and append it to the end of the
15099  *   last bb instead of doing it in the epilog.
15100  * - change the store opcodes so they use sreg1 instead of dreg to store the base register.
15101  */
15102
15103 /*
15104
15105 NOTES
15106 -----
15107
15108 - When to decompose opcodes:
15109   - earlier: this makes some optimizations hard to implement, since the low level IR
15110   no longer contains the neccessary information. But it is easier to do.
15111   - later: harder to implement, enables more optimizations.
15112 - Branches inside bblocks:
15113   - created when decomposing complex opcodes. 
15114     - branches to another bblock: harmless, but not tracked by the branch 
15115       optimizations, so need to branch to a label at the start of the bblock.
15116     - branches to inside the same bblock: very problematic, trips up the local
15117       reg allocator. Can be fixed by spitting the current bblock, but that is a
15118       complex operation, since some local vregs can become global vregs etc.
15119 - Local/global vregs:
15120   - local vregs: temporary vregs used inside one bblock. Assigned to hregs by the
15121     local register allocator.
15122   - global vregs: used in more than one bblock. Have an associated MonoMethodVar
15123     structure, created by mono_create_var (). Assigned to hregs or the stack by
15124     the global register allocator.
15125 - When to do optimizations like alu->alu_imm:
15126   - earlier -> saves work later on since the IR will be smaller/simpler
15127   - later -> can work on more instructions
15128 - Handling of valuetypes:
15129   - When a vtype is pushed on the stack, a new temporary is created, an 
15130     instruction computing its address (LDADDR) is emitted and pushed on
15131     the stack. Need to optimize cases when the vtype is used immediately as in
15132     argument passing, stloc etc.
15133 - Instead of the to_end stuff in the old JIT, simply call the function handling
15134   the values on the stack before emitting the last instruction of the bb.
15135 */
15136
15137 #endif /* DISABLE_JIT */